xref: /freebsd/sys/powerpc/powermac/ata_kauai.c (revision 6af83ee0d2941d18880b6aaa2b4facd1d30c6106)
1 /*-
2  * Copyright 2004 by Peter Grehan. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  * 3. The name of the author may not be used to endorse or promote products
13  *    derived from this software without specific prior written permission.
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,
20  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  */
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 /*
32  * Mac 'Kauai' PCI ATA controller
33  */
34 #include "opt_ata.h"
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <sys/bus.h>
40 #include <sys/malloc.h>
41 #include <sys/sema.h>
42 #include <sys/taskqueue.h>
43 #include <vm/uma.h>
44 #include <machine/stdarg.h>
45 #include <machine/resource.h>
46 #include <machine/bus.h>
47 #include <sys/rman.h>
48 #include <sys/ata.h>
49 #include <dev/ata/ata-all.h>
50 
51 #include <dev/ofw/openfirm.h>
52 
53 #include <dev/pci/pcivar.h>
54 #include <dev/pci/pcireg.h>
55 
56 #define  ATA_KAUAI_REGOFFSET	0x2000
57 
58 /*
59  * Offset to alt-control register from base
60  */
61 #define  ATA_KAUAI_ALTOFFSET    (ATA_KAUAI_REGOFFSET + 0x160)
62 
63 /*
64  * Define the gap between registers
65  */
66 #define ATA_KAUAI_REGGAP        16
67 
68 /*
69  * Define the kauai pci bus attachment.
70  */
71 static  int  ata_kauai_probe(device_t dev);
72 
73 static device_method_t ata_kauai_methods[] = {
74         /* Device interface */
75 	DEVMETHOD(device_probe,		ata_kauai_probe),
76 	DEVMETHOD(device_attach,	ata_attach),
77 	DEVMETHOD(device_detach,	bus_generic_detach),
78 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
79 	DEVMETHOD(device_suspend,	bus_generic_suspend),
80 	DEVMETHOD(device_resume,	bus_generic_resume),
81 
82 	{ 0, 0 }
83 };
84 
85 static driver_t ata_kauai_driver = {
86 	"ata",
87 	ata_kauai_methods,
88 	sizeof(struct ata_channel),
89 };
90 
91 DRIVER_MODULE(ata, pci, ata_kauai_driver, ata_devclass, 0, 0);
92 
93 static int
94 ata_kauai_locknoop(struct ata_channel *ch, int type)
95 {
96 	/* XXX SMP ? */
97 	return (0);
98 }
99 
100 static void
101 ata_kauai_setmode(struct ata_device *atadev, int mode)
102 {
103 	atadev->mode = ATA_PIO;
104 }
105 
106 
107 /*
108  * PCI ID search table
109  */
110 static struct kauai_pci_dev {
111         u_int32_t  kpd_devid;
112         char    *kpd_desc;
113 } kauai_pci_devlist[] = {
114         { 0x0033106b, "Uninorth2 Kauai ATA Controller" },
115         { 0x003b106b, "Intrepid Kauai ATA Controller" },
116         { 0x0043106b, "K2 Kauai ATA Controller" },
117         { 0, NULL }
118 };
119 
120 static int
121 ata_kauai_probe(device_t dev)
122 {
123 	struct ata_channel *ch;
124 	struct resource *mem;
125 	u_long startp, countp;
126 	u_int32_t devid;
127 	int i, found, rid, status;
128 
129 	found = 0;
130 	devid = pci_get_devid(dev);
131         for (i = 0; kauai_pci_devlist[i].kpd_desc != NULL; i++) {
132                 if (devid == kauai_pci_devlist[i].kpd_devid) {
133 			found = 1;
134                         device_set_desc(dev, kauai_pci_devlist[i].kpd_desc);
135 		}
136 	}
137 
138 	if (!found)
139 		return (ENXIO);
140 
141 	/*
142 	 * This device seems to ignore writes to the interrupt
143 	 * config register, resulting in interrupt resources
144 	 * not being attached. If this is the case, use
145 	 * Open Firmware to determine the irq, and then attach
146 	 * the resource. This allows the ATA common code to
147 	 * allocate the irq.
148 	 */
149 	status = bus_get_resource(dev, SYS_RES_IRQ, 0, &startp, &countp);
150 	if (status == ENOENT) {
151 		int irq;
152 
153 		/*
154 		 * Aargh, hideous hack until ofw pci intr routine is
155 		 * exported
156 		 */
157 		irq = 39; /* XXX */
158 		bus_set_resource(dev, SYS_RES_IRQ, 0, irq, 1);
159 
160 		/*
161 		 * Sanity check...
162 		 */
163 		status = bus_get_resource(dev, SYS_RES_IRQ, 0, &startp,
164 		    &countp);
165 		if (status == ENOENT ||
166 		    startp != 39) {
167 			printf("kauai irq not set!\n");
168 			return (ENXIO);
169 		}
170 	}
171 
172 	ch = device_get_softc(dev);
173 	bzero(ch, sizeof(struct ata_channel));
174 
175         rid = PCIR_BARS;
176 	mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
177         if (mem == NULL) {
178                 device_printf(dev, "could not allocate memory\n");
179                 return (ENXIO);
180         }
181 
182 	/*
183 	 * Set up the resource vectors
184 	 */
185         for (i = ATA_DATA; i <= ATA_STATUS; i++) {
186                 ch->r_io[i].res = mem;
187                 ch->r_io[i].offset = i*ATA_KAUAI_REGGAP + ATA_KAUAI_REGOFFSET;
188         }
189         ch->r_io[ATA_ALTSTAT].res = mem;
190         ch->r_io[ATA_ALTSTAT].offset = ATA_KAUAI_ALTOFFSET;
191 
192         ch->unit = 0;
193         ch->flags |= ATA_USE_16BIT|ATA_NO_SLAVE;
194         ch->locking = ata_kauai_locknoop;
195         ch->device[MASTER].setmode = ata_kauai_setmode;
196         ch->device[SLAVE].setmode = ata_kauai_setmode;
197 	ata_generic_hw(ch);
198 
199         return (ata_probe(dev));
200 }
201 
202