kauai.c revision 1.2 1 /* $NetBSD: kauai.c,v 1.2 2003/07/15 02:43:29 lukem Exp $ */
2
3 /*-
4 * Copyright (c) 2003 Tsubai Masanari. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: kauai.c,v 1.2 2003/07/15 02:43:29 lukem Exp $");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/device.h>
35 #include <sys/malloc.h>
36
37 #include <uvm/uvm_extern.h>
38
39 #include <machine/bus.h>
40
41 #include <dev/ata/atareg.h>
42 #include <dev/ata/atavar.h>
43 #include <dev/ic/wdcvar.h>
44
45 #include <dev/ofw/openfirm.h>
46
47 #include <dev/pci/pcivar.h>
48 #include <dev/pci/pcireg.h>
49 #include <dev/pci/pcidevs.h>
50
51 #include <macppc/dev/dbdma.h>
52
53 #define WDC_REG_NPORTS 8
54 #define WDC_AUXREG_OFFSET 0x16
55
56 #define PIO_CONFIG_REG (0x200 >> 4) /* PIO and DMA access timing */
57 #define DMA_CONFIG_REG (0x210 >> 4) /* UDMA access timing */
58
59 struct kauai_softc {
60 struct wdc_softc sc_wdcdev;
61 struct channel_softc *wdc_chanptr;
62 struct channel_softc wdc_channel;
63 struct channel_queue wdc_queue;
64 dbdma_regmap_t *sc_dmareg;
65 dbdma_command_t *sc_dmacmd;
66 u_int sc_piotiming_r[2];
67 u_int sc_piotiming_w[2];
68 u_int sc_dmatiming_r[2];
69 u_int sc_dmatiming_w[2];
70 void (*sc_calc_timing)(struct kauai_softc *, int);
71 };
72
73 int kauai_match __P((struct device *, struct cfdata *, void *));
74 void kauai_attach __P((struct device *, struct device *, void *));
75 int kauai_dma_init __P((void *, int, int, void *, size_t, int));
76 void kauai_dma_start __P((void *, int, int));
77 int kauai_dma_finish __P((void *, int, int, int));
78 void kauai_set_modes __P((struct channel_softc *));
79 static void calc_timing_kauai __P((struct kauai_softc *, int));
80 static int getnodebypci(pci_chipset_tag_t, pcitag_t);
81
82 CFATTACH_DECL(kauai, sizeof(struct kauai_softc),
83 kauai_match, kauai_attach, NULL, wdcactivate);
84
85 int
86 kauai_match(parent, match, aux)
87 struct device *parent;
88 struct cfdata *match;
89 void *aux;
90 {
91 struct pci_attach_args *pa = aux;
92
93 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE &&
94 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_KAUAI)
95 return 5;
96
97 return 0;
98 }
99
100 void
101 kauai_attach(parent, self, aux)
102 struct device *parent, *self;
103 void *aux;
104 {
105 struct kauai_softc *sc = (void *)self;
106 struct pci_attach_args *pa = aux;
107 struct channel_softc *chp = &sc->wdc_channel;
108 pci_intr_handle_t ih;
109 paddr_t regbase, dmabase;
110 int node, reg[5];
111
112 #ifdef DIAGNOSTIC
113 if ((vaddr_t)sc->sc_dmacmd & 0x0f) {
114 printf(": bad dbdma alignment\n");
115 return;
116 }
117 #endif
118
119 node = getnodebypci(pa->pa_pc, pa->pa_tag);
120 if (node == 0) {
121 printf(": cannot find gmac node\n");
122 return;
123 }
124
125 if (OF_getprop(node, "assigned-addresses", reg, sizeof reg) < 12) {
126 printf(": cannot get address property\n");
127 return;
128 }
129 regbase = reg[2] + 0x2000;
130 dmabase = reg[2] + 0x1000;
131
132 /*
133 * XXX PCI_INTERRUPT_REG seems to be wired to 0.
134 * XXX So use fixed intrpin and intrline values.
135 */
136 if (pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_INTERRUPT_REG) == 0) {
137 pa->pa_intrpin = 1;
138 pa->pa_intrline = 39;
139 }
140
141 if (pci_intr_map(pa, &ih)) {
142 printf(": unable to map interrupt\n");
143 return;
144 }
145 printf(": interrupting at %s\n", pci_intr_string(pa->pa_pc, ih));
146
147 chp->cmd_iot = chp->ctl_iot = macppc_make_bus_space_tag(regbase, 4);
148
149 if (bus_space_map(chp->cmd_iot, 0, WDC_REG_NPORTS, 0, &chp->cmd_ioh) ||
150 bus_space_subregion(chp->cmd_iot, chp->cmd_ioh,
151 WDC_AUXREG_OFFSET, 1, &chp->ctl_ioh)) {
152 printf("%s: couldn't map registers\n", self->dv_xname);
153 return;
154 }
155
156 if (pci_intr_establish(pa->pa_pc, ih, IPL_BIO, wdcintr, chp) == NULL) {
157 printf("%s: unable to establish interrupt\n", self->dv_xname);
158 return;
159 }
160
161
162 sc->sc_wdcdev.PIO_cap = 4;
163 sc->sc_wdcdev.DMA_cap = 2;
164 sc->sc_wdcdev.UDMA_cap = 5;
165 sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_MODE;
166 sc->sc_wdcdev.cap |= WDC_CAPABILITY_DMA | WDC_CAPABILITY_UDMA;
167 sc->wdc_chanptr = chp;
168 sc->sc_wdcdev.channels = &sc->wdc_chanptr;
169 sc->sc_wdcdev.nchannels = 1;
170 sc->sc_wdcdev.dma_arg = sc;
171 sc->sc_wdcdev.dma_init = kauai_dma_init;
172 sc->sc_wdcdev.dma_start = kauai_dma_start;
173 sc->sc_wdcdev.dma_finish = kauai_dma_finish;
174 sc->sc_wdcdev.set_modes = kauai_set_modes;
175 sc->sc_calc_timing = calc_timing_kauai;
176 sc->sc_dmareg = (void *)dmabase;
177
178 chp->channel = 0;
179 chp->wdc = &sc->sc_wdcdev;
180 chp->ch_queue = &sc->wdc_queue;
181
182 wdcattach(chp);
183
184 /* Modify access timings. */
185 kauai_set_modes(chp);
186 }
187
188 void
189 kauai_set_modes(chp)
190 struct channel_softc *chp;
191 {
192 struct kauai_softc *sc = (void *)chp->wdc;
193 struct ata_drive_datas *drvp0 = &chp->ch_drive[0];
194 struct ata_drive_datas *drvp1 = &chp->ch_drive[1];
195 struct ata_drive_datas *drvp;
196 int drive;
197
198 if ((drvp0->drive_flags & DRIVE) && (drvp1->drive_flags & DRIVE)) {
199 drvp0->PIO_mode = drvp1->PIO_mode =
200 min(drvp0->PIO_mode, drvp1->PIO_mode);
201 }
202
203 for (drive = 0; drive < 2; drive++) {
204 drvp = &chp->ch_drive[drive];
205 if (drvp->drive_flags & DRIVE) {
206 (*sc->sc_calc_timing)(sc, drive);
207 bus_space_write_4(chp->cmd_iot, chp->cmd_ioh,
208 PIO_CONFIG_REG, sc->sc_piotiming_r[drive]);
209 bus_space_write_4(chp->cmd_iot, chp->cmd_ioh,
210 DMA_CONFIG_REG, sc->sc_dmatiming_r[drive]);
211 }
212 }
213
214 wdc_print_modes(chp);
215 }
216
217 /*
218 * IDE transfer timings
219 */
220 static const u_int pio_timing_kauai[] = { /* 0xff000fff */
221 0x08000a92, /* Mode 0 */
222 0x0800060f, /* 1 */
223 0x0800038b, /* 2 */
224 0x05000249, /* 3 */
225 0x04000148 /* 4 */
226 };
227 static const u_int dma_timing_kauai[] = { /* 0x00fff000 */
228 0x00618000, /* Mode 0 */
229 0x00209000, /* 1 */
230 0x00148000 /* 2 */
231 };
232 static const u_int udma_timing_kauai[] = { /* 0x0000ffff */
233 0x000070c0, /* Mode 0 */
234 0x00005d80, /* 1 */
235 0x00004a60, /* 2 */
236 0x00003a50, /* 3 */
237 0x00002a30, /* 4 */
238 0x00002921 /* 5 */
239 };
240
241 /*
242 * Timing calculation for Kauai.
243 */
244 void
245 calc_timing_kauai(sc, drive)
246 struct kauai_softc *sc;
247 int drive;
248 {
249 struct channel_softc *chp = &sc->wdc_channel;
250 struct ata_drive_datas *drvp = &chp->ch_drive[drive];
251 int piomode = drvp->PIO_mode;
252 int dmamode = drvp->DMA_mode;
253 int udmamode = drvp->UDMA_mode;
254 u_int pioconf, dmaconf;
255
256 pioconf = pio_timing_kauai[piomode];
257
258 dmaconf = 0;
259 if (drvp->drive_flags & DRIVE_DMA)
260 dmaconf |= dma_timing_kauai[dmamode];
261 if (drvp->drive_flags & DRIVE_UDMA)
262 dmaconf |= udma_timing_kauai[udmamode];
263
264 if (drvp->drive_flags & DRIVE_UDMA)
265 dmaconf |= 1;
266
267 sc->sc_piotiming_r[drive] = sc->sc_piotiming_w[drive] = pioconf;
268 sc->sc_dmatiming_r[drive] = sc->sc_dmatiming_w[drive] = dmaconf;
269 }
270
271 int
272 kauai_dma_init(v, channel, drive, databuf, datalen, flags)
273 void *v;
274 void *databuf;
275 size_t datalen;
276 int flags;
277 {
278 struct kauai_softc *sc = v;
279 dbdma_command_t *cmdp = sc->sc_dmacmd;
280 struct channel_softc *chp = &sc->wdc_channel;
281 vaddr_t va = (vaddr_t)databuf;
282 int read = flags & WDC_DMA_READ;
283 int cmd = read ? DBDMA_CMD_IN_MORE : DBDMA_CMD_OUT_MORE;
284 u_int offset;
285
286 bus_space_write_4(chp->cmd_iot, chp->cmd_ioh, DMA_CONFIG_REG,
287 read ? sc->sc_dmatiming_r[drive] : sc->sc_dmatiming_w[drive]);
288 bus_space_read_4(chp->cmd_iot, chp->cmd_ioh, DMA_CONFIG_REG);
289
290 offset = va & PGOFSET;
291
292 /* if va is not page-aligned, setup the first page */
293 if (offset != 0) {
294 int rest = PAGE_SIZE - offset; /* the rest of the page */
295
296 if (datalen > rest) { /* if continues to next page */
297 DBDMA_BUILD(cmdp, cmd, 0, rest, vtophys(va),
298 DBDMA_INT_NEVER, DBDMA_WAIT_NEVER,
299 DBDMA_BRANCH_NEVER);
300 datalen -= rest;
301 va += rest;
302 cmdp++;
303 }
304 }
305
306 /* now va is page-aligned */
307 while (datalen > PAGE_SIZE) {
308 DBDMA_BUILD(cmdp, cmd, 0, PAGE_SIZE, vtophys(va),
309 DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
310 datalen -= PAGE_SIZE;
311 va += PAGE_SIZE;
312 cmdp++;
313 }
314
315 /* the last page (datalen <= PAGE_SIZE here) */
316 cmd = read ? DBDMA_CMD_IN_LAST : DBDMA_CMD_OUT_LAST;
317 DBDMA_BUILD(cmdp, cmd, 0, datalen, vtophys(va),
318 DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
319 cmdp++;
320
321 DBDMA_BUILD(cmdp, DBDMA_CMD_STOP, 0, 0, 0,
322 DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
323
324 return 0;
325 }
326
327 void
328 kauai_dma_start(v, channel, drive)
329 void *v;
330 int channel, drive;
331 {
332 struct kauai_softc *sc = v;
333
334 dbdma_start(sc->sc_dmareg, sc->sc_dmacmd);
335 }
336
337 int
338 kauai_dma_finish(v, channel, drive, read)
339 void *v;
340 int channel, drive;
341 int read;
342 {
343 struct kauai_softc *sc = v;
344
345 dbdma_stop(sc->sc_dmareg);
346 return 0;
347 }
348
349 /*
350 * Find OF-device corresponding to the PCI device.
351 */
352 int
353 getnodebypci(pc, tag)
354 pci_chipset_tag_t pc;
355 pcitag_t tag;
356 {
357 int bus, dev, func;
358 u_int reg[5];
359 int p, q;
360 int l, b, d, f;
361
362 pci_decompose_tag(pc, tag, &bus, &dev, &func);
363
364 for (q = OF_peer(0); q; q = p) {
365 l = OF_getprop(q, "assigned-addresses", reg, sizeof(reg));
366 if (l > 4) {
367 b = (reg[0] >> 16) & 0xff;
368 d = (reg[0] >> 11) & 0x1f;
369 f = (reg[0] >> 8) & 0x07;
370
371 if (b == bus && d == dev && f == func)
372 return q;
373 }
374 if ((p = OF_child(q)))
375 continue;
376 while (q) {
377 if ((p = OF_peer(q)))
378 break;
379 q = OF_parent(q);
380 }
381 }
382 return 0;
383 }
384