2 * Copyright (c) 1998 - 2004 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.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
38 #include <sys/malloc.h>
40 #include <sys/taskqueue.h>
42 #include <machine/stdarg.h>
43 #include <machine/resource.h>
44 #include <machine/bus.h>
46 #include <machine/md_var.h>
49 #include <dev/pci/pcivar.h>
50 #include <dev/pci/pcireg.h>
51 #include <dev/ata/ata-all.h>
52 #include <dev/ata/ata-pci.h>
55 static MALLOC_DEFINE(M_ATAPCI, "ATA PCI", "ATA driver PCI");
58 #define IOMASK 0xfffffffc
61 static int ata_pci_allocate(device_t, struct ata_channel *);
62 static void ata_pci_dmainit(struct ata_channel *);
63 static void ata_pci_locknoop(struct ata_channel *, int);
66 ata_pci_probe(device_t dev)
68 if (pci_get_class(dev) != PCIC_STORAGE)
71 switch (pci_get_vendor(dev)) {
73 if (!ata_acard_ident(dev))
76 case ATA_ACER_LABS_ID:
77 if (!ata_ali_ident(dev))
81 if (!ata_amd_ident(dev))
85 if (!ata_cyrix_ident(dev))
89 if (!ata_cypress_ident(dev))
92 case ATA_HIGHPOINT_ID:
93 if (!ata_highpoint_ident(dev))
97 if (!ata_intel_ident(dev))
100 case ATA_NATIONAL_ID:
101 if (!ata_national_ident(dev))
105 if (!ata_nvidia_ident(dev))
109 if (!ata_promise_ident(dev))
112 case ATA_SERVERWORKS_ID:
113 if (!ata_serverworks_ident(dev))
116 case ATA_SILICON_IMAGE_ID:
117 if (!ata_sii_ident(dev))
121 if (!ata_sis_ident(dev))
125 if (!ata_via_ident(dev))
129 if (pci_get_devid(dev) == 0x000116ca) {
130 ata_generic_ident(dev);
131 device_set_desc(dev, "Cenatek Rocket Drive controller");
136 if (pci_get_devid(dev)==0x10001042 || pci_get_devid(dev)==0x10011042) {
137 ata_generic_ident(dev);
139 "RZ 100? ATA controller !WARNING! buggy HW data loss possible");
145 /* unknown chipset, try generic DMA if it seems possible */
146 if ((pci_get_class(dev) == PCIC_STORAGE) &&
147 (pci_get_subclass(dev) == PCIS_STORAGE_IDE))
148 return ata_generic_ident(dev);
154 ata_pci_attach(device_t dev)
156 struct ata_pci_controller *ctlr = device_get_softc(dev);
160 /* do chipset specific setups only needed once */
161 if (ATA_MASTERDEV(dev) || pci_read_config(dev, 0x18, 4) & IOMASK)
165 ctlr->allocate = ata_pci_allocate;
166 ctlr->dmainit = ata_pci_dmainit;
167 ctlr->locking = ata_pci_locknoop;
169 /* if needed try to enable busmastering */
170 cmd = pci_read_config(dev, PCIR_COMMAND, 2);
171 if (!(cmd & PCIM_CMD_BUSMASTEREN)) {
172 pci_write_config(dev, PCIR_COMMAND, cmd | PCIM_CMD_BUSMASTEREN, 2);
173 cmd = pci_read_config(dev, PCIR_COMMAND, 2);
176 /* if busmastering mode "stuck" use it */
177 if ((cmd & PCIM_CMD_BUSMASTEREN) == PCIM_CMD_BUSMASTEREN) {
178 ctlr->r_type1 = SYS_RES_IOPORT;
179 ctlr->r_rid1 = ATA_BMADDR_RID;
180 ctlr->r_res1 = bus_alloc_resource(dev, ctlr->r_type1, &ctlr->r_rid1,
181 0, ~0, 1, RF_ACTIVE);
186 /* attach all channels on this controller */
187 for (unit = 0; unit < ctlr->channels; unit++)
188 device_add_child(dev, "ata", ATA_MASTERDEV(dev) ?
189 unit : devclass_find_free_unit(ata_devclass, 2));
191 return bus_generic_attach(dev); }
194 ata_pci_detach(device_t dev)
196 struct ata_pci_controller *ctlr = device_get_softc(dev);
197 struct ata_channel *ch;
200 /* mark HW as gone, we dont want to issue commands to HW no longer there */
201 for (unit = 0; unit < ctlr->channels; unit++) {
202 if ((ch = ctlr->interrupt[unit].argument))
203 ch->flags |= ATA_HWGONE;
206 bus_generic_detach(dev);
209 bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle);
210 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ctlr->r_irq);
213 bus_release_resource(dev, ctlr->r_type2, ctlr->r_rid2, ctlr->r_res2);
215 bus_release_resource(dev, ctlr->r_type1, ctlr->r_rid1, ctlr->r_res1);
221 ata_pci_print_child(device_t dev, device_t child)
223 struct ata_channel *ch = device_get_softc(child);
226 retval += bus_print_child_header(dev, child);
227 retval += printf(": at 0x%lx", rman_get_start(ch->r_io[ATA_IDX_ADDR].res));
229 if (ATA_MASTERDEV(dev))
230 retval += printf(" irq %d", 14 + ch->unit);
232 retval += bus_print_child_footer(dev, child);
237 static struct resource *
238 ata_pci_alloc_resource(device_t dev, device_t child, int type, int *rid,
239 u_long start, u_long end, u_long count, u_int flags)
241 struct ata_pci_controller *controller = device_get_softc(dev);
242 int unit = ((struct ata_channel *)device_get_softc(child))->unit;
243 struct resource *res = NULL;
246 if (type == SYS_RES_IOPORT) {
249 if (ATA_MASTERDEV(dev)) {
251 start = (unit ? ATA_SECONDARY : ATA_PRIMARY);
252 end = start + ATA_IOSIZE - 1;
254 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
255 SYS_RES_IOPORT, &myrid,
256 start, end, count, flags);
259 myrid = 0x10 + 8 * unit;
260 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
261 SYS_RES_IOPORT, &myrid,
262 start, end, count, flags);
266 case ATA_ALTADDR_RID:
267 if (ATA_MASTERDEV(dev)) {
269 start = (unit ? ATA_SECONDARY : ATA_PRIMARY) + ATA_ALTOFFSET;
270 end = start + ATA_ALTIOSIZE - 1;
271 count = ATA_ALTIOSIZE;
272 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
273 SYS_RES_IOPORT, &myrid,
274 start, end, count, flags);
277 myrid = 0x14 + 8 * unit;
278 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
279 SYS_RES_IOPORT, &myrid,
280 start, end, count, flags);
282 start = rman_get_start(res) + 2;
283 end = start + ATA_ALTIOSIZE - 1;
284 count = ATA_ALTIOSIZE;
285 BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
286 SYS_RES_IOPORT, myrid, res);
287 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
288 SYS_RES_IOPORT, &myrid,
289 start, end, count, flags);
297 if (type == SYS_RES_IRQ && *rid == ATA_IRQ_RID) {
298 if (ATA_MASTERDEV(dev)) {
300 return alpha_platform_alloc_ide_intr(unit);
302 int irq = (unit == 0 ? 14 : 15);
304 return BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
305 SYS_RES_IRQ, rid, irq, irq, 1, flags);
309 return controller->r_irq;
316 ata_pci_release_resource(device_t dev, device_t child, int type, int rid,
319 int unit = ((struct ata_channel *)device_get_softc(child))->unit;
321 if (type == SYS_RES_IOPORT) {
324 if (ATA_MASTERDEV(dev))
325 return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
326 SYS_RES_IOPORT, 0x0, r);
328 return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
329 SYS_RES_IOPORT, 0x10 + 8 * unit, r);
332 case ATA_ALTADDR_RID:
333 if (ATA_MASTERDEV(dev))
334 return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
335 SYS_RES_IOPORT, 0x0, r);
337 return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
338 SYS_RES_IOPORT, 0x14 + 8 * unit, r);
344 if (type == SYS_RES_IRQ) {
345 if (rid != ATA_IRQ_RID)
348 if (ATA_MASTERDEV(dev)) {
350 return alpha_platform_release_ide_intr(unit, r);
352 return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
353 SYS_RES_IRQ, rid, r);
363 ata_pci_setup_intr(device_t dev, device_t child, struct resource *irq,
364 int flags, driver_intr_t *function, void *argument,
367 if (ATA_MASTERDEV(dev)) {
369 return alpha_platform_setup_ide_intr(child, irq, function, argument,
372 return BUS_SETUP_INTR(device_get_parent(dev), child, irq,
373 flags, function, argument, cookiep);
377 struct ata_pci_controller *controller = device_get_softc(dev);
378 int unit = ((struct ata_channel *)device_get_softc(child))->unit;
380 controller->interrupt[unit].function = function;
381 controller->interrupt[unit].argument = argument;
382 *cookiep = controller;
388 ata_pci_teardown_intr(device_t dev, device_t child, struct resource *irq,
391 if (ATA_MASTERDEV(dev)) {
393 return alpha_platform_teardown_ide_intr(child, irq, cookie);
395 return BUS_TEARDOWN_INTR(device_get_parent(dev), child, irq, cookie);
399 struct ata_pci_controller *controller = device_get_softc(dev);
400 int unit = ((struct ata_channel *)device_get_softc(child))->unit;
402 controller->interrupt[unit].function = NULL;
403 controller->interrupt[unit].argument = NULL;
409 ata_pci_allocate(device_t dev, struct ata_channel *ch)
411 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
412 struct resource *io = NULL, *altio = NULL;
415 rid = ATA_IOADDR_RID;
416 io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
417 0, ~0, ATA_IOSIZE, RF_ACTIVE);
421 rid = ATA_ALTADDR_RID;
422 altio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
423 0, ~0, ATA_ALTIOSIZE, RF_ACTIVE);
425 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
429 for (i = ATA_DATA; i <= ATA_STATUS; i ++) {
430 ch->r_io[i].res = io;
431 ch->r_io[i].offset = i;
433 ch->r_io[ATA_ALTSTAT].res = altio;
434 ch->r_io[ATA_ALTSTAT].offset = 0;
435 ch->r_io[ATA_IDX_ADDR].res = io;
438 for (i = ATA_BMCMD_PORT; i <= ATA_BMDTP_PORT; i++) {
439 ch->r_io[i].res = ctlr->r_res1;
440 ch->r_io[i].offset = (i - ATA_BMCMD_PORT)+(ch->unit * ATA_BMIOSIZE);
443 /* if simplex controller, only allow DMA on primary channel */
444 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_IDX_INB(ch, ATA_BMSTAT_PORT) &
445 (ATA_BMSTAT_DMA_MASTER | ATA_BMSTAT_DMA_SLAVE));
447 (ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_DMA_SIMPLEX))
448 device_printf(dev, "simplex device, DMA on primary only\n");
456 ata_pci_dmastart(struct ata_channel *ch)
458 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, (ATA_IDX_INB(ch, ATA_BMSTAT_PORT) |
459 (ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR)));
460 ATA_IDX_OUTL(ch, ATA_BMDTP_PORT, ch->dma->mdmatab);
461 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
462 ((ch->dma->flags & ATA_DMA_READ) ? ATA_BMCMD_WRITE_READ : 0) |
463 ATA_BMCMD_START_STOP);
468 ata_pci_dmastop(struct ata_channel *ch)
472 error = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
473 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
474 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
475 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
480 ata_pci_dmainit(struct ata_channel *ch)
484 ch->dma->start = ata_pci_dmastart;
485 ch->dma->stop = ata_pci_dmastop;
490 ata_pci_locknoop(struct ata_channel *ch, int flags)
494 static device_method_t ata_pci_methods[] = {
495 /* device interface */
496 DEVMETHOD(device_probe, ata_pci_probe),
497 DEVMETHOD(device_attach, ata_pci_attach),
498 DEVMETHOD(device_detach, ata_pci_detach),
499 DEVMETHOD(device_shutdown, bus_generic_shutdown),
500 DEVMETHOD(device_suspend, bus_generic_suspend),
501 DEVMETHOD(device_resume, bus_generic_resume),
504 DEVMETHOD(bus_print_child, ata_pci_print_child),
505 DEVMETHOD(bus_alloc_resource, ata_pci_alloc_resource),
506 DEVMETHOD(bus_release_resource, ata_pci_release_resource),
507 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
508 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
509 DEVMETHOD(bus_setup_intr, ata_pci_setup_intr),
510 DEVMETHOD(bus_teardown_intr, ata_pci_teardown_intr),
514 static driver_t ata_pci_driver = {
517 sizeof(struct ata_pci_controller),
520 static devclass_t ata_pci_devclass;
522 DRIVER_MODULE(atapci, pci, ata_pci_driver, ata_pci_devclass, 0, 0);
525 ata_pcisub_probe(device_t dev)
527 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
528 struct ata_channel *ch = device_get_softc(dev);
532 /* take care of green memory */
533 bzero(ch, sizeof(struct ata_channel));
535 /* find channel number on this controller */
536 device_get_children(device_get_parent(dev), &children, &count);
537 for (i = 0; i < count; i++) {
538 if (children[i] == dev)
541 free(children, M_TEMP);
543 if ((error = ctlr->allocate(dev, ch)))
546 ch->device[MASTER].setmode = ctlr->setmode;
547 ch->device[SLAVE].setmode = ctlr->setmode;
548 ch->locking = ctlr->locking;
551 return ata_probe(dev);
554 static device_method_t ata_pcisub_methods[] = {
555 /* device interface */
556 DEVMETHOD(device_probe, ata_pcisub_probe),
557 DEVMETHOD(device_attach, ata_attach),
558 DEVMETHOD(device_detach, ata_detach),
559 DEVMETHOD(device_suspend, ata_suspend),
560 DEVMETHOD(device_resume, ata_resume),
564 static driver_t ata_pcisub_driver = {
567 sizeof(struct ata_channel),
570 DRIVER_MODULE(ata, atapci, ata_pcisub_driver, ata_devclass, 0, 0);