2 * Copyright (c) 1998 - 2008 Søren Schmidt <sos@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/module.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
36 #include <sys/endian.h>
37 #include <sys/malloc.h>
39 #include <sys/mutex.h>
41 #include <sys/taskqueue.h>
43 #include <machine/stdarg.h>
44 #include <machine/resource.h>
45 #include <machine/bus.h>
47 #include <dev/pci/pcivar.h>
48 #include <dev/pci/pcireg.h>
49 #include <dev/ata/ata-all.h>
50 #include <dev/ata/ata-pci.h>
53 /* local prototypes */
54 static int ata_cmd_ch_attach(device_t dev);
55 static int ata_cmd_status(device_t dev);
56 static int ata_cmd_setmode(device_t dev, int target, int mode);
57 static int ata_sii_ch_attach(device_t dev);
58 static int ata_sii_ch_detach(device_t dev);
59 static int ata_sii_status(device_t dev);
60 static void ata_sii_reset(device_t dev);
61 static int ata_sii_setmode(device_t dev, int target, int mode);
66 #define SII_SETCLK 0x02
71 * Silicon Image Inc. (SiI) (former CMD) chipset support functions
74 ata_sii_probe(device_t dev)
76 struct ata_pci_controller *ctlr = device_get_softc(dev);
77 static const struct ata_chip_id ids[] =
78 {{ ATA_SII3114, 0x00, SII_MEMIO, SII_4CH, ATA_SA150, "3114" },
79 { ATA_SII3512, 0x02, SII_MEMIO, 0, ATA_SA150, "3512" },
80 { ATA_SII3112, 0x02, SII_MEMIO, 0, ATA_SA150, "3112" },
81 { ATA_SII3112_1, 0x02, SII_MEMIO, 0, ATA_SA150, "3112" },
82 { ATA_SII3512, 0x00, SII_MEMIO, SII_BUG, ATA_SA150, "3512" },
83 { ATA_SII3112, 0x00, SII_MEMIO, SII_BUG, ATA_SA150, "3112" },
84 { ATA_SII3112_1, 0x00, SII_MEMIO, SII_BUG, ATA_SA150, "3112" },
85 { ATA_SII0680, 0x00, SII_MEMIO, SII_SETCLK, ATA_UDMA6, "680" },
86 { ATA_CMD649, 0x00, 0, SII_INTR, ATA_UDMA5, "(CMD) 649" },
87 { ATA_CMD648, 0x00, 0, SII_INTR, ATA_UDMA4, "(CMD) 648" },
88 { ATA_CMD646, 0x07, 0, 0, ATA_UDMA2, "(CMD) 646U2" },
89 { ATA_CMD646, 0x00, 0, 0, ATA_WDMA2, "(CMD) 646" },
92 if (pci_get_vendor(dev) != ATA_SILICON_IMAGE_ID)
95 if (!(ctlr->chip = ata_match_chip(dev, ids)))
99 ctlr->chipinit = ata_sii_chipinit;
100 return (BUS_PROBE_LOW_PRIORITY);
104 ata_sii_chipinit(device_t dev)
106 struct ata_pci_controller *ctlr = device_get_softc(dev);
108 if (ata_setup_interrupt(dev, ata_generic_intr))
111 switch (ctlr->chip->cfg1) {
113 ctlr->r_type2 = SYS_RES_MEMORY;
114 ctlr->r_rid2 = PCIR_BAR(5);
115 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
116 &ctlr->r_rid2, RF_ACTIVE))){
117 if (ctlr->chip->chipid != ATA_SII0680 ||
118 (pci_read_config(dev, 0x8a, 1) & 1))
122 if (ctlr->chip->cfg2 & SII_SETCLK) {
123 if ((pci_read_config(dev, 0x8a, 1) & 0x30) != 0x10)
124 pci_write_config(dev, 0x8a,
125 (pci_read_config(dev, 0x8a, 1) & 0xcf)|0x10,1);
126 if ((pci_read_config(dev, 0x8a, 1) & 0x30) != 0x10)
127 device_printf(dev, "%s could not set ATA133 clock\n",
131 /* if we have 4 channels enable the second set */
132 if (ctlr->chip->cfg2 & SII_4CH) {
133 ATA_OUTL(ctlr->r_res2, 0x0200, 0x00000002);
137 /* dont block interrupts from any channel */
138 pci_write_config(dev, 0x48,
139 (pci_read_config(dev, 0x48, 4) & ~0x03c00000), 4);
141 /* enable PCI interrupt as BIOS might not */
142 pci_write_config(dev, 0x8a, (pci_read_config(dev, 0x8a, 1) & 0x3f), 1);
145 ctlr->ch_attach = ata_sii_ch_attach;
146 ctlr->ch_detach = ata_sii_ch_detach;
149 if (ctlr->chip->max_dma >= ATA_SA150) {
150 ctlr->reset = ata_sii_reset;
151 ctlr->setmode = ata_sata_setmode;
152 ctlr->getrev = ata_sata_getrev;
155 ctlr->setmode = ata_sii_setmode;
159 if ((pci_read_config(dev, 0x51, 1) & 0x08) != 0x08) {
160 device_printf(dev, "HW has secondary channel disabled\n");
164 /* enable interrupt as BIOS might not */
165 pci_write_config(dev, 0x71, 0x01, 1);
167 ctlr->ch_attach = ata_cmd_ch_attach;
168 ctlr->ch_detach = ata_pci_ch_detach;
169 ctlr->setmode = ata_cmd_setmode;
176 ata_cmd_ch_attach(device_t dev)
178 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
179 struct ata_channel *ch = device_get_softc(dev);
181 /* setup the usual register normal pci style */
182 if (ata_pci_ch_attach(dev))
185 if (ctlr->chip->cfg2 & SII_INTR)
186 ch->hw.status = ata_cmd_status;
188 ch->flags |= ATA_NO_ATAPI_DMA;
194 ata_cmd_status(device_t dev)
196 struct ata_channel *ch = device_get_softc(dev);
199 if (((reg71 = pci_read_config(device_get_parent(dev), 0x71, 1)) &
200 (ch->unit ? 0x08 : 0x04))) {
201 pci_write_config(device_get_parent(dev), 0x71,
202 reg71 & ~(ch->unit ? 0x04 : 0x08), 1);
203 return ata_pci_status(dev);
209 ata_cmd_setmode(device_t dev, int target, int mode)
211 device_t parent = device_get_parent(dev);
212 struct ata_pci_controller *ctlr = device_get_softc(parent);
213 struct ata_channel *ch = device_get_softc(dev);
214 int devno = (ch->unit << 1) + target;
215 int treg = 0x54 + ((devno < 3) ? (devno << 1) : 7);
216 int ureg = ch->unit ? 0x7b : 0x73;
218 static const uint8_t piotimings[] =
219 { 0xa9, 0x57, 0x44, 0x32, 0x3f, 0x87, 0x32, 0x3f };
220 static const uint8_t udmatimings[][2] =
221 { { 0x31, 0xc2 }, { 0x21, 0x82 }, { 0x11, 0x42 },
222 { 0x25, 0x8a }, { 0x15, 0x4a }, { 0x05, 0x0a } };
224 mode = min(mode, ctlr->chip->max_dma);
225 if (mode >= ATA_UDMA0) {
226 u_int8_t umode = pci_read_config(parent, ureg, 1);
228 umode &= ~(target == 0 ? 0x35 : 0xca);
229 umode |= udmatimings[mode & ATA_MODE_MASK][target];
230 pci_write_config(parent, ureg, umode, 1);
233 pci_write_config(parent, ureg,
234 pci_read_config(parent, ureg, 1) &
235 ~(target == 0 ? 0x35 : 0xca), 1);
238 pci_write_config(parent, treg, piotimings[ata_mode2idx(piomode)], 1);
243 ata_sii_ch_attach(device_t dev)
245 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
246 struct ata_channel *ch = device_get_softc(dev);
247 int unit01 = (ch->unit & 1), unit10 = (ch->unit & 2);
250 for (i = ATA_DATA; i <= ATA_COMMAND; i++) {
251 ch->r_io[i].res = ctlr->r_res2;
252 ch->r_io[i].offset = 0x80 + i + (unit01 << 6) + (unit10 << 8);
254 ch->r_io[ATA_CONTROL].res = ctlr->r_res2;
255 ch->r_io[ATA_CONTROL].offset = 0x8a + (unit01 << 6) + (unit10 << 8);
256 ch->r_io[ATA_IDX_ADDR].res = ctlr->r_res2;
257 ata_default_registers(dev);
259 ch->r_io[ATA_BMCMD_PORT].res = ctlr->r_res2;
260 ch->r_io[ATA_BMCMD_PORT].offset = 0x00 + (unit01 << 3) + (unit10 << 8);
261 ch->r_io[ATA_BMSTAT_PORT].res = ctlr->r_res2;
262 ch->r_io[ATA_BMSTAT_PORT].offset = 0x02 + (unit01 << 3) + (unit10 << 8);
263 ch->r_io[ATA_BMDTP_PORT].res = ctlr->r_res2;
264 ch->r_io[ATA_BMDTP_PORT].offset = 0x04 + (unit01 << 3) + (unit10 << 8);
266 if (ctlr->chip->max_dma >= ATA_SA150) {
267 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
268 ch->r_io[ATA_SSTATUS].offset = 0x104 + (unit01 << 7) + (unit10 << 8);
269 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
270 ch->r_io[ATA_SERROR].offset = 0x108 + (unit01 << 7) + (unit10 << 8);
271 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
272 ch->r_io[ATA_SCONTROL].offset = 0x100 + (unit01 << 7) + (unit10 << 8);
273 ch->flags |= ATA_NO_SLAVE;
274 ch->flags |= ATA_SATA;
275 ch->flags |= ATA_KNOWN_PRESENCE;
277 /* enable PHY state change interrupt */
278 ATA_OUTL(ctlr->r_res2, 0x148 + (unit01 << 7) + (unit10 << 8),(1 << 16));
281 if (ctlr->chip->cfg2 & SII_BUG) {
282 /* work around errata in early chips */
283 ch->dma.boundary = 8192;
284 ch->dma.segsize = 15 * DEV_BSIZE;
288 ch->hw.status = ata_sii_status;
289 if (ctlr->chip->cfg2 & SII_SETCLK)
290 ch->flags |= ATA_CHECKS_CABLE;
292 ata_pci_dmainit(dev);
298 ata_sii_ch_detach(device_t dev)
301 ata_pci_dmafini(dev);
306 ata_sii_status(device_t dev)
308 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
309 struct ata_channel *ch = device_get_softc(dev);
310 int offset0 = ((ch->unit & 1) << 3) + ((ch->unit & 2) << 8);
311 int offset1 = ((ch->unit & 1) << 6) + ((ch->unit & 2) << 8);
313 /* do we have any PHY events ? */
314 if (ctlr->chip->max_dma >= ATA_SA150 &&
315 (ATA_INL(ctlr->r_res2, 0x10 + offset0) & 0x00000010))
316 ata_sata_phy_check_events(dev, -1);
318 if (ATA_INL(ctlr->r_res2, 0xa0 + offset1) & 0x00000800)
319 return ata_pci_status(dev);
325 ata_sii_reset(device_t dev)
327 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
328 struct ata_channel *ch = device_get_softc(dev);
329 int offset = ((ch->unit & 1) << 7) + ((ch->unit & 2) << 8);
332 /* Apply R_ERR on DMA activate FIS errata workaround. */
333 val = ATA_INL(ctlr->r_res2, 0x14c + offset);
334 if ((val & 0x3) == 0x1)
335 ATA_OUTL(ctlr->r_res2, 0x14c + offset, val & ~0x3);
337 if (ata_sata_phy_reset(dev, -1, 1))
338 ata_generic_reset(dev);
344 ata_sii_setmode(device_t dev, int target, int mode)
346 device_t parent = device_get_parent(dev);
347 struct ata_pci_controller *ctlr = device_get_softc(parent);
348 struct ata_channel *ch = device_get_softc(dev);
349 int rego = (ch->unit << 4) + (target << 1);
350 int mreg = ch->unit ? 0x84 : 0x80;
351 int mask = 0x03 << (target << 2);
352 int mval = pci_read_config(parent, mreg, 1) & ~mask;
354 u_int8_t preg = 0xa4 + rego;
355 u_int8_t dreg = 0xa8 + rego;
356 u_int8_t ureg = 0xac + rego;
357 static const uint16_t piotimings[] =
358 { 0x328a, 0x2283, 0x1104, 0x10c3, 0x10c1 };
359 static const uint16_t dmatimings[] = { 0x2208, 0x10c2, 0x10c1 };
360 static const uint8_t udmatimings[] =
361 { 0xf, 0xb, 0x7, 0x5, 0x3, 0x2, 0x1 };
363 mode = min(mode, ctlr->chip->max_dma);
365 if (ctlr->chip->cfg2 & SII_SETCLK) {
366 if (ata_dma_check_80pin && mode > ATA_UDMA2 &&
367 (pci_read_config(parent, 0x79, 1) &
368 (ch->unit ? 0x02 : 0x01))) {
369 ata_print_cable(dev, "controller");
373 if (mode >= ATA_UDMA0) {
374 pci_write_config(parent, mreg,
375 mval | (0x03 << (target << 2)), 1);
376 pci_write_config(parent, ureg,
377 (pci_read_config(parent, ureg, 1) & ~0x3f) |
378 udmatimings[mode & ATA_MODE_MASK], 1);
380 } else if (mode >= ATA_WDMA0) {
381 pci_write_config(parent, mreg,
382 mval | (0x02 << (target << 2)), 1);
383 pci_write_config(parent, dreg, dmatimings[mode & ATA_MODE_MASK], 2);
384 piomode = (mode == ATA_WDMA0) ? ATA_PIO0 :
385 (mode == ATA_WDMA1) ? ATA_PIO3 : ATA_PIO4;
387 pci_write_config(parent, mreg,
388 mval | (0x01 << (target << 2)), 1);
391 pci_write_config(parent, preg, piotimings[ata_mode2idx(piomode)], 2);
395 ATA_DECLARE_DRIVER(ata_sii);