kauai.c revision 1.38 1 /* $NetBSD: kauai.c,v 1.38 2017/10/20 07:06:07 jdolecek 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.38 2017/10/20 07:06:07 jdolecek 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 <sys/bus.h>
40 #include <machine/pio.h>
41
42 #include <dev/ata/atareg.h>
43 #include <dev/ata/atavar.h>
44 #include <dev/ic/wdcvar.h>
45
46 #include <dev/ofw/openfirm.h>
47
48 #include <dev/pci/pcivar.h>
49 #include <dev/pci/pcireg.h>
50 #include <dev/pci/pcidevs.h>
51
52 #include <macppc/dev/dbdma.h>
53
54 #define WDC_REG_NPORTS 8
55 #define WDC_AUXREG_OFFSET 0x16
56 #define WDC_AUXREG_NPORTS 1
57
58 #define PIO_CONFIG_REG 0x200 /* PIO and DMA access timing */
59 #define DMA_CONFIG_REG 0x210 /* UDMA access timing */
60
61 struct kauai_softc {
62 struct wdc_softc sc_wdcdev;
63 struct ata_channel *sc_chanptr;
64 struct ata_channel sc_channel;
65 struct wdc_regs sc_wdc_regs;
66 dbdma_regmap_t *sc_dmareg;
67 dbdma_command_t *sc_dmacmd;
68 u_int sc_piotiming_r[2];
69 u_int sc_piotiming_w[2];
70 u_int sc_dmatiming_r[2];
71 u_int sc_dmatiming_w[2];
72 void (*sc_calc_timing)(struct kauai_softc *, int);
73 };
74
75 static int kauai_match(device_t, cfdata_t, void *);
76 static void kauai_attach(device_t, device_t, void *);
77 static int kauai_dma_init(void *, int, int, void *, size_t, int);
78 static void kauai_dma_start(void *, int, int);
79 static int kauai_dma_finish(void *, int, int, int);
80 static void kauai_set_modes(struct ata_channel *);
81 static void calc_timing_kauai(struct kauai_softc *, int);
82
83 CFATTACH_DECL_NEW(kauai, sizeof(struct kauai_softc),
84 kauai_match, kauai_attach, NULL, NULL);
85
86 int
87 kauai_match(device_t parent, cfdata_t match, void *aux)
88 {
89 struct pci_attach_args *pa = aux;
90
91 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE) {
92 switch (PCI_PRODUCT(pa->pa_id)) {
93 case PCI_PRODUCT_APPLE_KAUAI:
94 case PCI_PRODUCT_APPLE_UNINORTH_ATA:
95 case PCI_PRODUCT_APPLE_INTREPID2_ATA:
96 case PCI_PRODUCT_APPLE_SHASTA_ATA:
97 return 5;
98 }
99 }
100
101 return 0;
102 }
103
104 void
105 kauai_attach(device_t parent, device_t self, void *aux)
106 {
107 struct kauai_softc *sc = device_private(self);
108 struct pci_attach_args *pa = aux;
109 struct ata_channel *chp = &sc->sc_channel;
110 struct wdc_regs *wdr;
111 pci_intr_handle_t ih;
112 paddr_t regbase, dmabase;
113 int node, reg[5], i;
114 uint32_t intrs[4], intr;
115 char buf[PCI_INTRSTR_LEN];
116
117 sc->sc_wdcdev.sc_atac.atac_dev = self;
118
119 sc->sc_dmacmd = dbdma_alloc(sizeof(dbdma_command_t) * 20, NULL);
120 node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
121 if (node == 0) {
122 aprint_error(": cannot find kauai node\n");
123 return;
124 }
125
126 if (OF_getprop(node, "assigned-addresses", reg, sizeof reg) < 12) {
127 aprint_error(": cannot get address property\n");
128 return;
129 }
130 regbase = reg[2] + 0x2000;
131 dmabase = reg[2] + 0x1000;
132
133 /*
134 * XXX PCI_INTERRUPT_REG seems to be wired to 0.
135 * XXX So use fixed intrpin and intrline values if the interrupts
136 * XXX property contains no IRQ line
137 */
138 intr = 0;
139 pa->pa_intrpin = 1;
140 if (OF_getprop(node, "interrupts", intrs, sizeof(intrs)) >= 4) {
141 intr = intrs[0];
142 /*
143 * the interrupts property on my iBook G4's kauai contains
144 * 0x00000001 0x00000000, so fix that up here
145 * TODO: use parent's interrupt-map property to do this right
146 */
147 if (intr < 10)
148 intr = 0;
149 aprint_debug_dev(self,
150 "got %d from interrupts property\n", intr);
151 }
152 if (intr == 0) {
153 if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_SHASTA_ATA) {
154 intr = 38;
155 } else
156 intr = 39;
157 }
158 pa->pa_intrline = intr;
159
160 if (pci_intr_map(pa, &ih)) {
161 aprint_error(": unable to map interrupt\n");
162 return;
163 }
164 aprint_normal(": interrupting at %s\n", pci_intr_string(pa->pa_pc, ih,
165 buf, sizeof(buf)));
166
167 sc->sc_wdcdev.regs = wdr = &sc->sc_wdc_regs;
168
169 wdr->cmd_iot = wdr->ctl_iot = pa->pa_memt;
170
171 if (bus_space_map(wdr->cmd_iot, regbase, WDC_REG_NPORTS << 4, 0,
172 &wdr->cmd_baseioh) ||
173 bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh,
174 WDC_AUXREG_OFFSET << 4, 1, &wdr->ctl_ioh)) {
175 aprint_error_dev(self, "couldn't map registers\n");
176 return;
177 }
178 for (i = 0; i < WDC_NREG; i++) {
179 if (bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh, i << 4,
180 i == 0 ? 4 : 1, &wdr->cmd_iohs[i]) != 0) {
181 bus_space_unmap(wdr->cmd_iot, wdr->cmd_baseioh,
182 WDC_REG_NPORTS << 4);
183 aprint_error_dev(self,
184 "couldn't subregion registers\n");
185 return;
186 }
187 }
188
189 if (pci_intr_establish(pa->pa_pc, ih, IPL_BIO, wdcintr, chp) == NULL) {
190 aprint_error_dev(self, "unable to establish interrupt\n");
191 return;
192 }
193
194
195 sc->sc_wdcdev.sc_atac.atac_pio_cap = 4;
196 sc->sc_wdcdev.sc_atac.atac_dma_cap = 2;
197 sc->sc_wdcdev.sc_atac.atac_udma_cap = 5;
198 sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DATA16;
199 sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DMA | ATAC_CAP_UDMA;
200 sc->sc_chanptr = chp;
201 sc->sc_wdcdev.sc_atac.atac_channels = &sc->sc_chanptr;
202 sc->sc_wdcdev.sc_atac.atac_nchannels = 1;
203 sc->sc_wdcdev.wdc_maxdrives = 2;
204 sc->sc_wdcdev.dma_arg = sc;
205 sc->sc_wdcdev.dma_init = kauai_dma_init;
206 sc->sc_wdcdev.dma_start = kauai_dma_start;
207 sc->sc_wdcdev.dma_finish = kauai_dma_finish;
208 sc->sc_wdcdev.sc_atac.atac_set_modes = kauai_set_modes;
209 sc->sc_calc_timing = calc_timing_kauai;
210 sc->sc_dmareg = mapiodev(dmabase, 0x1000, false);
211
212 chp->ch_channel = 0;
213 chp->ch_atac = &sc->sc_wdcdev.sc_atac;
214
215 wdc_init_shadow_regs(wdr);
216
217 wdcattach(chp);
218 }
219
220 void
221 kauai_set_modes(struct ata_channel *chp)
222 {
223 struct kauai_softc *sc = (void *)chp->ch_atac;
224 struct wdc_regs *wdr = CHAN_TO_WDC_REGS(chp);
225 struct ata_drive_datas *drvp0 = &chp->ch_drive[0];
226 struct ata_drive_datas *drvp1 = &chp->ch_drive[1];
227 struct ata_drive_datas *drvp;
228 int drive;
229
230 if (drvp0->drive_type != ATA_DRIVET_NONE &&
231 drvp1->drive_type != ATA_DRIVET_NONE) {
232 drvp0->PIO_mode = drvp1->PIO_mode =
233 min(drvp0->PIO_mode, drvp1->PIO_mode);
234 }
235
236 for (drive = 0; drive < 2; drive++) {
237 drvp = &chp->ch_drive[drive];
238 if (drvp->drive_type != ATA_DRIVET_NONE) {
239 (*sc->sc_calc_timing)(sc, drive);
240 bus_space_write_4(wdr->cmd_iot, wdr->cmd_baseioh,
241 PIO_CONFIG_REG, sc->sc_piotiming_r[drive]);
242 bus_space_write_4(wdr->cmd_iot, wdr->cmd_baseioh,
243 DMA_CONFIG_REG, sc->sc_dmatiming_r[drive]);
244 }
245 }
246 }
247
248 /*
249 * IDE transfer timings
250 */
251 static const u_int pio_timing_kauai[] = { /* 0xff000fff */
252 0x08000a92, /* Mode 0 */
253 0x0800060f, /* 1 */
254 0x0800038b, /* 2 */
255 0x05000249, /* 3 */
256 0x04000148 /* 4 */
257 };
258 static const u_int dma_timing_kauai[] = { /* 0x00fff000 */
259 0x00618000, /* Mode 0 */
260 0x00209000, /* 1 */
261 0x00148000 /* 2 */
262 };
263 static const u_int udma_timing_kauai[] = { /* 0x0000ffff */
264 0x000070c0, /* Mode 0 */
265 0x00005d80, /* 1 */
266 0x00004a60, /* 2 */
267 0x00003a50, /* 3 */
268 0x00002a30, /* 4 */
269 0x00002921 /* 5 */
270 };
271
272 /*
273 * Timing calculation for Kauai.
274 */
275 void
276 calc_timing_kauai(struct kauai_softc *sc, int drive)
277 {
278 struct ata_channel *chp = &sc->sc_channel;
279 struct ata_drive_datas *drvp = &chp->ch_drive[drive];
280 int piomode = drvp->PIO_mode;
281 int dmamode = drvp->DMA_mode;
282 int udmamode = drvp->UDMA_mode;
283 u_int pioconf, dmaconf;
284
285 pioconf = pio_timing_kauai[piomode];
286
287 dmaconf = 0;
288 if (drvp->drive_flags & ATA_DRIVE_DMA)
289 dmaconf |= dma_timing_kauai[dmamode];
290 if (drvp->drive_flags & ATA_DRIVE_UDMA)
291 dmaconf |= udma_timing_kauai[udmamode];
292
293 if (drvp->drive_flags & ATA_DRIVE_UDMA)
294 dmaconf |= 1;
295
296 sc->sc_piotiming_r[drive] = sc->sc_piotiming_w[drive] = pioconf;
297 sc->sc_dmatiming_r[drive] = sc->sc_dmatiming_w[drive] = dmaconf;
298 }
299
300 int
301 kauai_dma_init(void *v, int channel, int drive, void *databuf,
302 size_t datalen, int flags)
303 {
304 struct kauai_softc *sc = v;
305 dbdma_command_t *cmdp = sc->sc_dmacmd;
306 struct ata_channel *chp = &sc->sc_channel;
307 struct wdc_regs *wdr = CHAN_TO_WDC_REGS(chp);
308 vaddr_t va = (vaddr_t)databuf;
309 int read = flags & WDC_DMA_READ;
310 int cmd = read ? DBDMA_CMD_IN_MORE : DBDMA_CMD_OUT_MORE;
311 u_int offset;
312
313 bus_space_write_4(wdr->cmd_iot, wdr->cmd_baseioh, DMA_CONFIG_REG,
314 read ? sc->sc_dmatiming_r[drive] : sc->sc_dmatiming_w[drive]);
315 bus_space_read_4(wdr->cmd_iot, wdr->cmd_baseioh, DMA_CONFIG_REG);
316
317 offset = va & PGOFSET;
318
319 /* if va is not page-aligned, setup the first page */
320 if (offset != 0) {
321 int rest = PAGE_SIZE - offset; /* the rest of the page */
322
323 if (datalen > rest) { /* if continues to next page */
324 DBDMA_BUILD(cmdp, cmd, 0, rest, vtophys(va),
325 DBDMA_INT_NEVER, DBDMA_WAIT_NEVER,
326 DBDMA_BRANCH_NEVER);
327 datalen -= rest;
328 va += rest;
329 cmdp++;
330 }
331 }
332
333 /* now va is page-aligned */
334 while (datalen > PAGE_SIZE) {
335 DBDMA_BUILD(cmdp, cmd, 0, PAGE_SIZE, vtophys(va),
336 DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
337 datalen -= PAGE_SIZE;
338 va += PAGE_SIZE;
339 cmdp++;
340 }
341
342 /* the last page (datalen <= PAGE_SIZE here) */
343 cmd = read ? DBDMA_CMD_IN_LAST : DBDMA_CMD_OUT_LAST;
344 DBDMA_BUILD(cmdp, cmd, 0, datalen, vtophys(va),
345 DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
346 cmdp++;
347
348 DBDMA_BUILD(cmdp, DBDMA_CMD_STOP, 0, 0, 0,
349 DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
350
351 return 0;
352 }
353
354 void
355 kauai_dma_start(void *v, int channel, int drive)
356 {
357 struct kauai_softc *sc = v;
358
359 dbdma_start(sc->sc_dmareg, sc->sc_dmacmd);
360 }
361
362 int
363 kauai_dma_finish(void *v, int channel, int drive, int read)
364 {
365 struct kauai_softc *sc = v;
366
367 dbdma_stop(sc->sc_dmareg);
368 return 0;
369 }
370