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