xref: /freebsd/sys/dev/ata/ata-pci.c (revision 7660b554bc59a07be0431c17e0e33815818baa69)
1 /*-
2  * Copyright (c) 1998 - 2003 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  * 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.
16  *
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.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include "opt_ata.h"
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/bus.h>
38 #include <sys/malloc.h>
39 #include <sys/taskqueue.h>
40 #include <machine/stdarg.h>
41 #include <machine/resource.h>
42 #include <machine/bus.h>
43 #ifdef __alpha__
44 #include <machine/md_var.h>
45 #endif
46 #include <sys/rman.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>
51 
52 /* local vars */
53 static MALLOC_DEFINE(M_ATAPCI, "ATA PCI", "ATA driver PCI");
54 
55 /* misc defines */
56 #define IOMASK			0xfffffffc
57 
58 /* prototypes */
59 static int ata_pci_allocate(device_t, struct ata_channel *);
60 static void ata_pci_dmainit(struct ata_channel *);
61 static void ata_pci_locknoop(struct ata_channel *, int);
62 
63 static int
64 ata_pci_probe(device_t dev)
65 {
66     if (pci_get_class(dev) != PCIC_STORAGE)
67 	return ENXIO;
68 
69     switch (pci_get_vendor(dev)) {
70     case ATA_ACARD_ID:
71 	return ata_acard_ident(dev);
72     case ATA_ACER_LABS_ID:
73 	return ata_ali_ident(dev);
74     case ATA_AMD_ID:
75 	return ata_amd_ident(dev);
76     case ATA_CYRIX_ID:
77 	return ata_cyrix_ident(dev);
78     case ATA_CYPRESS_ID:
79 	return ata_cypress_ident(dev);
80     case ATA_HIGHPOINT_ID:
81 	return ata_highpoint_ident(dev);
82     case ATA_INTEL_ID:
83 	return ata_intel_ident(dev);
84     case ATA_NATIONAL_ID:
85 	return ata_national_ident(dev);
86     case ATA_NVIDIA_ID:
87 	return ata_nvidia_ident(dev);
88     case ATA_PROMISE_ID:
89 	return ata_promise_ident(dev);
90     case ATA_SERVERWORKS_ID:
91 	return ata_serverworks_ident(dev);
92     case ATA_SILICON_IMAGE_ID:
93 	return ata_sii_ident(dev);
94     case ATA_SIS_ID:
95 	return ata_sis_ident(dev);
96     case ATA_VIA_ID:
97 	return ata_via_ident(dev);
98 
99     case 0x16ca:
100 	if (pci_get_devid(dev) == 0x000116ca) {
101 	    ata_generic_ident(dev);
102 	    device_set_desc(dev, "Cenatek Rocket Drive controller");
103 	    return 0;
104 	}
105 	return ENXIO;
106 
107     case 0x1042:
108 	if (pci_get_devid(dev)==0x10001042 || pci_get_devid(dev)==0x10011042) {
109 	    ata_generic_ident(dev);
110 	    device_set_desc(dev,
111 		"RZ 100? ATA controller !WARNING! buggy HW data loss possible");
112 	    return 0;
113 	}
114 	return ENXIO;
115 
116     /* unknown chipset, try generic DMA if it seems possible */
117     default:
118 	if (pci_get_class(dev) == PCIC_STORAGE &&
119 	    (pci_get_subclass(dev) == PCIS_STORAGE_IDE))
120 	    return ata_generic_ident(dev);
121     }
122     return ENXIO;
123 }
124 
125 static int
126 ata_pci_attach(device_t dev)
127 {
128     struct ata_pci_controller *ctlr = device_get_softc(dev);
129     u_int8_t class, subclass;
130     u_int32_t type, cmd;
131     int unit;
132 
133     /* set up vendor-specific stuff */
134     type = pci_get_devid(dev);
135     class = pci_get_class(dev);
136     subclass = pci_get_subclass(dev);
137     cmd = pci_read_config(dev, PCIR_COMMAND, 2);
138 
139     if (!(cmd & PCIM_CMD_PORTEN)) {
140 	device_printf(dev, "ATA channel disabled by BIOS\n");
141 	return 0;
142     }
143 
144     /* do chipset specific setups only needed once */
145     if (ATA_MASTERDEV(dev) || pci_read_config(dev, 0x18, 4) & IOMASK)
146 	ctlr->channels = 2;
147     else
148 	ctlr->channels = 1;
149     ctlr->allocate = ata_pci_allocate;
150     ctlr->dmainit = ata_pci_dmainit;
151     ctlr->locking = ata_pci_locknoop;
152 
153 #ifdef __sparc64__
154     if (!(cmd & PCIM_CMD_BUSMASTEREN)) {
155 	pci_write_config(dev, PCIR_COMMAND, cmd | PCIM_CMD_BUSMASTEREN, 2);
156 	cmd = pci_read_config(dev, PCIR_COMMAND, 2);
157     }
158 #endif
159     /* if busmastering configured get the I/O resource */
160     if ((cmd & PCIM_CMD_BUSMASTEREN) == PCIM_CMD_BUSMASTEREN) {
161 	int rid = ATA_BMADDR_RID;
162 
163 	ctlr->r_io1 = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
164 					 0, ~0, 1, RF_ACTIVE);
165     }
166 
167     ctlr->chipinit(dev);
168 
169     /* attach all channels on this controller */
170     for (unit = 0; unit < ctlr->channels; unit++)
171 	device_add_child(dev, "ata", ATA_MASTERDEV(dev) ?
172 			 unit : devclass_find_free_unit(ata_devclass, 2));
173 
174     return bus_generic_attach(dev);
175 }
176 
177 
178 static int
179 ata_pci_print_child(device_t dev, device_t child)
180 {
181     struct ata_channel *ch = device_get_softc(child);
182     int retval = 0;
183 
184     retval += bus_print_child_header(dev, child);
185     retval += printf(": at 0x%lx", rman_get_start(ch->r_io[ATA_IDX_ADDR].res));
186 
187     if (ATA_MASTERDEV(dev))
188 	retval += printf(" irq %d", 14 + ch->unit);
189 
190     retval += bus_print_child_footer(dev, child);
191 
192     return retval;
193 }
194 
195 static struct resource *
196 ata_pci_alloc_resource(device_t dev, device_t child, int type, int *rid,
197 		       u_long start, u_long end, u_long count, u_int flags)
198 {
199     struct ata_pci_controller *controller = device_get_softc(dev);
200     int unit = ((struct ata_channel *)device_get_softc(child))->unit;
201     struct resource *res = NULL;
202     int myrid;
203 
204     if (type == SYS_RES_IOPORT) {
205 	switch (*rid) {
206 	case ATA_IOADDR_RID:
207 	    if (ATA_MASTERDEV(dev)) {
208 		myrid = 0;
209 		start = (unit ? ATA_SECONDARY : ATA_PRIMARY);
210 		end = start + ATA_IOSIZE - 1;
211 		count = ATA_IOSIZE;
212 		res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
213 					 SYS_RES_IOPORT, &myrid,
214 					 start, end, count, flags);
215 	    }
216 	    else {
217 		myrid = 0x10 + 8 * unit;
218 		res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
219 					 SYS_RES_IOPORT, &myrid,
220 					 start, end, count, flags);
221 	    }
222 	    break;
223 
224 	case ATA_ALTADDR_RID:
225 	    if (ATA_MASTERDEV(dev)) {
226 		myrid = 0;
227 		start = (unit ? ATA_SECONDARY : ATA_PRIMARY) + ATA_ALTOFFSET;
228 		end = start + ATA_ALTIOSIZE - 1;
229 		count = ATA_ALTIOSIZE;
230 		res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
231 					 SYS_RES_IOPORT, &myrid,
232 					 start, end, count, flags);
233 	    }
234 	    else {
235 		myrid = 0x14 + 8 * unit;
236 		res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
237 					 SYS_RES_IOPORT, &myrid,
238 					 start, end, count, flags);
239 		if (res) {
240 			start = rman_get_start(res) + 2;
241 			end = start + ATA_ALTIOSIZE - 1;
242 			count = ATA_ALTIOSIZE;
243 			BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
244 					     SYS_RES_IOPORT, myrid, res);
245 			res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
246 						 SYS_RES_IOPORT, &myrid,
247 						 start, end, count, flags);
248 		}
249 	    }
250 	    break;
251 	}
252 	return res;
253     }
254 
255     if (type == SYS_RES_IRQ && *rid == ATA_IRQ_RID) {
256 		if (ATA_MASTERDEV(dev)) {
257 #ifdef __alpha__
258 	    return alpha_platform_alloc_ide_intr(unit);
259 #else
260 	    int irq = (unit == 0 ? 14 : 15);
261 
262 	    return BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
263 				      SYS_RES_IRQ, rid, irq, irq, 1, flags);
264 #endif
265 	}
266 	else {
267 	    return controller->r_irq;
268 	}
269     }
270     return 0;
271 }
272 
273 static int
274 ata_pci_release_resource(device_t dev, device_t child, int type, int rid,
275 			 struct resource *r)
276 {
277     int unit = ((struct ata_channel *)device_get_softc(child))->unit;
278 
279     if (type == SYS_RES_IOPORT) {
280 	switch (rid) {
281 	case ATA_IOADDR_RID:
282 	    if (ATA_MASTERDEV(dev))
283 		return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
284 					    SYS_RES_IOPORT, 0x0, r);
285 	    else
286 		return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
287 					    SYS_RES_IOPORT, 0x10 + 8 * unit, r);
288 	    break;
289 
290 	case ATA_ALTADDR_RID:
291 	    if (ATA_MASTERDEV(dev))
292 		return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
293 					    SYS_RES_IOPORT, 0x0, r);
294 	    else
295 		return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
296 					    SYS_RES_IOPORT, 0x14 + 8 * unit, r);
297 	    break;
298 	default:
299 	    return ENOENT;
300 	}
301     }
302     if (type == SYS_RES_IRQ) {
303 	if (rid != ATA_IRQ_RID)
304 	    return ENOENT;
305 
306 	if (ATA_MASTERDEV(dev)) {
307 #ifdef __alpha__
308 	    return alpha_platform_release_ide_intr(unit, r);
309 #else
310 	    return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
311 					SYS_RES_IRQ, rid, r);
312 #endif
313 	}
314 	else
315 	    return 0;
316     }
317     return EINVAL;
318 }
319 
320 static int
321 ata_pci_setup_intr(device_t dev, device_t child, struct resource *irq,
322 		   int flags, driver_intr_t *function, void *argument,
323 		   void **cookiep)
324 {
325     if (ATA_MASTERDEV(dev)) {
326 #ifdef __alpha__
327 	return alpha_platform_setup_ide_intr(child, irq, function, argument,
328 					     cookiep);
329 #else
330 	return BUS_SETUP_INTR(device_get_parent(dev), child, irq,
331 			      flags, function, argument, cookiep);
332 #endif
333     }
334     else {
335 	struct ata_pci_controller *controller = device_get_softc(dev);
336 	int unit = ((struct ata_channel *)device_get_softc(child))->unit;
337 
338 	controller->interrupt[unit].function = function;
339 	controller->interrupt[unit].argument = argument;
340 	*cookiep = controller;
341 	return 0;
342     }
343 }
344 
345 static int
346 ata_pci_teardown_intr(device_t dev, device_t child, struct resource *irq,
347 		      void *cookie)
348 {
349     if (ATA_MASTERDEV(dev)) {
350 #ifdef __alpha__
351 	return alpha_platform_teardown_ide_intr(child, irq, cookie);
352 #else
353 	return BUS_TEARDOWN_INTR(device_get_parent(dev), child, irq, cookie);
354 #endif
355     }
356     else {
357 	struct ata_pci_controller *controller = device_get_softc(dev);
358 	int unit = ((struct ata_channel *)device_get_softc(child))->unit;
359 
360 	controller->interrupt[unit].function = NULL;
361 	controller->interrupt[unit].argument = NULL;
362 	return 0;
363     }
364 }
365 
366 static int
367 ata_pci_allocate(device_t dev, struct ata_channel *ch)
368 {
369     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
370     struct resource *io = NULL, *altio = NULL;
371     int i, rid;
372 
373     rid = ATA_IOADDR_RID;
374     io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
375 			    0, ~0, ATA_IOSIZE, RF_ACTIVE);
376     if (!io)
377 	return ENXIO;
378 
379     rid = ATA_ALTADDR_RID;
380     altio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
381 			       0, ~0, ATA_ALTIOSIZE, RF_ACTIVE);
382     if (!altio) {
383 	bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
384 	return ENXIO;
385     }
386 
387     for (i = ATA_DATA; i <= ATA_STATUS; i ++) {
388 	ch->r_io[i].res = io;
389 	ch->r_io[i].offset = i;
390     }
391     ch->r_io[ATA_ALTSTAT].res = altio;
392     ch->r_io[ATA_ALTSTAT].offset = 0;
393     ch->r_io[ATA_IDX_ADDR].res = io;
394 
395     if (ctlr->r_io1) {
396 	for (i = ATA_BMCMD_PORT; i <= ATA_BMDTP_PORT; i++) {
397 	    ch->r_io[i].res = ctlr->r_io1;
398 	    ch->r_io[i].offset = (i - ATA_BMCMD_PORT)+(ch->unit * ATA_BMIOSIZE);
399 	}
400 
401 	/* if simplex controller, only allow DMA on primary channel */
402 	ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_IDX_INB(ch, ATA_BMSTAT_PORT) &
403 		     (ATA_BMSTAT_DMA_MASTER | ATA_BMSTAT_DMA_SLAVE));
404 	if (ch->unit > 0 &&
405 	    (ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_DMA_SIMPLEX))
406 	    device_printf(dev, "simplex device, DMA on primary only\n");
407 	else
408 	    ctlr->dmainit(ch);
409     }
410     return 0;
411 }
412 
413 static int
414 ata_pci_dmastart(struct ata_channel *ch, caddr_t data, int32_t count, int dir)
415 {
416     int error;
417 
418     if ((error = ata_dmastart(ch, data, count, dir)))
419 	return error;
420     ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, (ATA_IDX_INB(ch, ATA_BMSTAT_PORT) |
421 		 (ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR)));
422     ATA_IDX_OUTL(ch, ATA_BMDTP_PORT, ch->dma->mdmatab);
423     ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
424 		 (dir ? ATA_BMCMD_WRITE_READ : 0) | ATA_BMCMD_START_STOP);
425     return 0;
426 }
427 
428 static int
429 ata_pci_dmastop(struct ata_channel *ch)
430 {
431     int error;
432 
433     error = ATA_IDX_INB(ch, ATA_BMSTAT_PORT);
434     ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
435 		 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
436     ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
437     ata_dmastop(ch);
438     return error;
439 }
440 
441 static void
442 ata_pci_dmainit(struct ata_channel *ch)
443 {
444     ata_dmainit(ch);
445     if (ch->dma) {
446 	ch->dma->start = ata_pci_dmastart;
447 	ch->dma->stop = ata_pci_dmastop;
448     }
449 }
450 
451 static void
452 ata_pci_locknoop(struct ata_channel *ch, int flags)
453 {
454 }
455 
456 static device_method_t ata_pci_methods[] = {
457     /* device interface */
458     DEVMETHOD(device_probe,		ata_pci_probe),
459     DEVMETHOD(device_attach,		ata_pci_attach),
460     DEVMETHOD(device_shutdown,		bus_generic_shutdown),
461     DEVMETHOD(device_suspend,		bus_generic_suspend),
462     DEVMETHOD(device_resume,		bus_generic_resume),
463 
464     /* bus methods */
465     DEVMETHOD(bus_print_child,		ata_pci_print_child),
466     DEVMETHOD(bus_alloc_resource,	ata_pci_alloc_resource),
467     DEVMETHOD(bus_release_resource,	ata_pci_release_resource),
468     DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
469     DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
470     DEVMETHOD(bus_setup_intr,		ata_pci_setup_intr),
471     DEVMETHOD(bus_teardown_intr,	ata_pci_teardown_intr),
472     { 0, 0 }
473 };
474 
475 static driver_t ata_pci_driver = {
476     "atapci",
477     ata_pci_methods,
478     sizeof(struct ata_pci_controller),
479 };
480 
481 static devclass_t ata_pci_devclass;
482 
483 DRIVER_MODULE(atapci, pci, ata_pci_driver, ata_pci_devclass, 0, 0);
484 
485 static int
486 ata_pcisub_probe(device_t dev)
487 {
488     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
489     struct ata_channel *ch = device_get_softc(dev);
490     device_t *children;
491     int count, error, i;
492 
493     /* find channel number on this controller */
494     device_get_children(device_get_parent(dev), &children, &count);
495     for (i = 0; i < count; i++) {
496 	if (children[i] == dev)
497 	    ch->unit = i;
498     }
499     free(children, M_TEMP);
500 
501     if ((error = ctlr->allocate(dev, ch)))
502 	return error;
503 
504     ch->device[MASTER].setmode = ctlr->setmode;
505     ch->device[SLAVE].setmode = ctlr->setmode;
506     ch->locking = ctlr->locking;
507     return ata_probe(dev);
508 }
509 
510 static device_method_t ata_pcisub_methods[] = {
511     /* device interface */
512     DEVMETHOD(device_probe,	ata_pcisub_probe),
513     DEVMETHOD(device_attach,	ata_attach),
514     DEVMETHOD(device_detach,	ata_detach),
515     DEVMETHOD(device_suspend,	ata_suspend),
516     DEVMETHOD(device_resume,	ata_resume),
517     { 0, 0 }
518 };
519 
520 static driver_t ata_pcisub_driver = {
521     "ata",
522     ata_pcisub_methods,
523     sizeof(struct ata_channel),
524 };
525 
526 DRIVER_MODULE(ata, atapci, ata_pcisub_driver, ata_devclass, 0, 0);
527