kernel/nata: Deal with ATA_DEV() and atadev->unit.
[dragonfly.git] / sys / dev / disk / nata / chipsets / ata-ati.c
1 /*-
2  * Copyright (c) 1998 - 2008 Søren Schmidt <sos@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    without modification, immediately at the beginning of the file.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 /* local prototypes */
28 static int ata_ati_chipinit(device_t dev);
29 static void ata_ati_setmode(device_t dev, int mode);
30
31 /* misc defines */
32 #define ATI_AHCI        0x04
33 #define SII_MEMIO       1
34
35 /*
36  * ATI chipset support functions
37  */
38 int
39 ata_ati_ident(device_t dev)
40 {
41     struct ata_pci_controller *ctlr = device_get_softc(dev);
42     static const struct ata_chip_id ids[] =
43     {{ ATA_ATI_IXP200,    0x00, 0,        0, ATA_UDMA5, "IXP200" },
44      { ATA_ATI_IXP300,    0x00, 0,        0, ATA_UDMA6, "IXP300" },
45      { ATA_ATI_IXP300_S1, 0x00, SII_MEMIO, 0, ATA_SA150, "IXP300" },
46      { ATA_ATI_IXP400,    0x00, 0,        0, ATA_UDMA6, "IXP400" },
47      { ATA_ATI_IXP400_S1, 0x00, SII_MEMIO, 0, ATA_SA150, "IXP400" },
48      { ATA_ATI_IXP400_S2, 0x00, SII_MEMIO, 0, ATA_SA150, "IXP400" },
49      { ATA_ATI_IXP600,    0x00, 0,        0, ATA_UDMA6, "IXP600" },
50      { ATA_ATI_IXP600_S1, 0x00, ATI_AHCI, 0, ATA_SA300, "IXP600" },
51      { ATA_ATI_IXP600_S2, 0x00, ATI_AHCI, 0, ATA_SA300, "IXP600" },
52      { 0, 0, 0, 0, 0, 0}};
53
54     if (pci_get_vendor(dev) != ATA_ATI_ID)
55         return ENXIO;
56
57     if (!(ctlr->chip = ata_match_chip(dev, ids)))
58         return ENXIO;
59
60     ata_set_desc(dev);
61
62     /*
63      * The ATI SATA controllers are actually a SiI 3112 controller, except
64      * for the SB600.
65      */
66     if (ctlr->chip->cfg1 & SII_MEMIO)
67         ctlr->chipinit = ata_sii_chipinit;
68     else
69         ctlr->chipinit = ata_ati_chipinit;
70     return 0;
71 }
72
73 static int
74 ata_ati_chipinit(device_t dev)
75 {
76     struct ata_pci_controller *ctlr = device_get_softc(dev);
77
78     if (ata_setup_interrupt(dev, ata_generic_intr))
79         return ENXIO;
80
81     /* The SB600 needs special treatment. */
82     if (ctlr->chip->cfg1 & ATI_AHCI) {
83         /* Check if the chip is configured as an AHCI part. */
84         if ((pci_get_subclass(dev) == PCIS_STORAGE_SATA) &&
85             (pci_read_config(dev, PCIR_PROGIF, 1) == PCIP_STORAGE_SATA_AHCI_1_0)) {
86             if (ata_ahci_chipinit(dev) != ENXIO)
87                 return 0;
88         }
89     }
90
91     ctlr->setmode = ata_ati_setmode;
92     return 0;
93 }
94
95 static void
96 ata_ati_setmode(device_t dev, int mode)
97 {
98         device_t gparent = GRANDPARENT(dev);
99         struct ata_pci_controller *ctlr = device_get_softc(gparent);
100         struct ata_channel *ch = device_get_softc(device_get_parent(dev));
101         struct ata_device *atadev = device_get_softc(dev);
102         int devno = (ch->unit << 1) + atadev->unit;
103         int offset = (devno ^ 0x01) << 3;
104         int error;
105         static const uint8_t piotimings[] =
106                             { 0x5d, 0x47, 0x34, 0x22, 0x20, 0x34, 0x22, 0x20,
107                               0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
108         static const uint8_t dmatimings[] = { 0x77, 0x21, 0x20 };
109
110     mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
111
112     mode = ata_check_80pin(dev, mode);
113
114     error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
115
116     if (bootverbose)
117         device_printf(dev, "%ssetting %s on %s chip\n",
118                       (error) ? "FAILURE " : "",
119                       ata_mode2str(mode), ctlr->chip->text);
120     if (!error) {
121         if (mode >= ATA_UDMA0) {
122             pci_write_config(gparent, 0x56,
123                              (pci_read_config(gparent, 0x56, 2) &
124                               ~(0xf << (devno << 2))) |
125                              ((mode & ATA_MODE_MASK) << (devno << 2)), 2);
126             pci_write_config(gparent, 0x54,
127                              pci_read_config(gparent, 0x54, 1) |
128                              (0x01 << devno), 1);
129             pci_write_config(gparent, 0x44,
130                              (pci_read_config(gparent, 0x44, 4) &
131                               ~(0xff << offset)) |
132                              (dmatimings[2] << offset), 4);
133         }
134         else if (mode >= ATA_WDMA0) {
135             pci_write_config(gparent, 0x54,
136                              pci_read_config(gparent, 0x54, 1) &
137                               ~(0x01 << devno), 1);
138             pci_write_config(gparent, 0x44,
139                              (pci_read_config(gparent, 0x44, 4) &
140                               ~(0xff << offset)) |
141                              (dmatimings[mode & ATA_MODE_MASK] << offset), 4);
142         }
143         else
144             pci_write_config(gparent, 0x54,
145                              pci_read_config(gparent, 0x54, 1) &
146                              ~(0x01 << devno), 1);
147
148         pci_write_config(gparent, 0x4a,
149                          (pci_read_config(gparent, 0x4a, 2) &
150                           ~(0xf << (devno << 2))) |
151                          (((mode - ATA_PIO0) & ATA_MODE_MASK) << (devno<<2)),2);
152         pci_write_config(gparent, 0x40,
153                          (pci_read_config(gparent, 0x40, 4) &
154                           ~(0xff << offset)) |
155                          (piotimings[ata_mode2idx(mode)] << offset), 4);
156         atadev->mode = mode;
157     }
158 }