2 * Copyright 2002 by Peter Grehan. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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
31 * Mac-io ATA controller
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
39 #include <sys/malloc.h>
41 #include <sys/taskqueue.h>
43 #include <machine/stdarg.h>
44 #include <machine/resource.h>
45 #include <machine/bus.h>
48 #include <dev/ata/ata-all.h>
51 #include <dev/ofw/ofw_bus.h>
53 #include "ata_dbdma.h"
56 * Offset to control registers from base
58 #define ATA_MACIO_ALTOFFSET 0x160
61 * Define the gap between registers
63 #define ATA_MACIO_REGGAP 16
66 * Whether or not to bind to the DBDMA IRQ
68 #define USE_DBDMA_IRQ 0
73 #define ATA_MACIO_TIMINGREG 0x200
75 #define ATA_TIME_TO_TICK(rev,time) howmany(time, (rev == 4) ? 15 : 30)
76 #define PIO_REC_OFFSET 4
79 #define DMA_REC_OFFSET 1
84 int cycle; /* minimum cycle time [ns] */
85 int active; /* minimum command active time [ns] */
88 struct ide_timings pio_timings[5] = {
89 { 600, 180 }, /* PIO 0 */
90 { 390, 150 }, /* PIO 1 */
91 { 240, 105 }, /* PIO 2 */
92 { 180, 90 }, /* PIO 3 */
93 { 120, 75 } /* PIO 4 */
96 static const struct ide_timings dma_timings[3] = {
97 { 480, 240 }, /* WDMA 0 */
98 { 165, 90 }, /* WDMA 1 */
99 { 120, 75 } /* WDMA 2 */
102 static const struct ide_timings udma_timings[5] = {
103 { 120, 180 }, /* UDMA 0 */
104 { 90, 150 }, /* UDMA 1 */
105 { 60, 120 }, /* UDMA 2 */
106 { 45, 90 }, /* UDMA 3 */
107 { 30, 90 } /* UDMA 4 */
111 * Define the macio ata bus attachment.
113 static int ata_macio_probe(device_t dev);
114 static void ata_macio_setmode(device_t parent, device_t dev);
115 static int ata_macio_attach(device_t dev);
116 static int ata_macio_begin_transaction(struct ata_request *request);
118 static device_method_t ata_macio_methods[] = {
119 /* Device interface */
120 DEVMETHOD(device_probe, ata_macio_probe),
121 DEVMETHOD(device_attach, ata_macio_attach),
124 DEVMETHOD(ata_setmode, ata_macio_setmode),
128 struct ata_macio_softc {
129 struct ata_dbdma_channel sc_ch;
133 struct resource *sc_mem;
135 uint32_t udmaconf[2];
136 uint32_t wdmaconf[2];
140 static driver_t ata_macio_driver = {
143 sizeof(struct ata_macio_softc),
146 DRIVER_MODULE(ata, macio, ata_macio_driver, ata_devclass, 0, 0);
147 MODULE_DEPEND(ata, ata, 1, 1, 1);
150 ata_macio_probe(device_t dev)
152 const char *type = ofw_bus_get_type(dev);
153 const char *name = ofw_bus_get_name(dev);
154 struct ata_macio_softc *sc;
155 struct ata_channel *ch;
158 if (strcmp(type, "ata") != 0 &&
159 strcmp(type, "ide") != 0)
162 sc = device_get_softc(dev);
163 bzero(sc, sizeof(struct ata_macio_softc));
164 ch = &sc->sc_ch.sc_ch;
166 if (strcmp(name,"ata-4") == 0) {
167 device_set_desc(dev,"Apple MacIO Ultra ATA Controller");
169 sc->max_mode = ATA_UDMA4;
171 device_set_desc(dev,"Apple MacIO ATA Controller");
173 sc->max_mode = ATA_WDMA2;
177 sc->sc_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
179 if (sc->sc_mem == NULL) {
180 device_printf(dev, "could not allocate memory\n");
185 * Set up the resource vectors
187 for (i = ATA_DATA; i <= ATA_COMMAND; i++) {
188 ch->r_io[i].res = sc->sc_mem;
189 ch->r_io[i].offset = i * ATA_MACIO_REGGAP;
191 ch->r_io[ATA_CONTROL].res = sc->sc_mem;
192 ch->r_io[ATA_CONTROL].offset = ATA_MACIO_ALTOFFSET;
193 ata_default_registers(dev);
196 ch->flags |= ATA_USE_16BIT;
199 return (ata_probe(dev));
203 ata_macio_attach(device_t dev)
205 struct ata_macio_softc *sc = device_get_softc(dev);
209 int dbdma_irq_rid = 1;
210 struct resource *dbdma_irq;
214 /* Init DMA engine */
216 sc->sc_ch.dbdma_rid = 1;
217 sc->sc_ch.dbdma_regs = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
218 &sc->sc_ch.dbdma_rid, RF_ACTIVE);
220 ata_dbdma_dmainit(dev);
222 /* Configure initial timings */
223 timingreg = bus_read_4(sc->sc_mem, ATA_MACIO_TIMINGREG);
225 sc->udmaconf[0] = sc->udmaconf[1] = timingreg & 0x1ff00000;
226 sc->wdmaconf[0] = sc->wdmaconf[1] = timingreg & 0x001ffc00;
227 sc->pioconf[0] = sc->pioconf[1] = timingreg & 0x000003ff;
229 sc->udmaconf[0] = sc->udmaconf[1] = 0;
230 sc->wdmaconf[0] = sc->wdmaconf[1] = timingreg & 0xfffff800;
231 sc->pioconf[0] = sc->pioconf[1] = timingreg & 0x000007ff;
235 /* Bind to DBDMA interrupt as well */
237 if ((dbdma_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
238 &dbdma_irq_rid, RF_SHAREABLE | RF_ACTIVE)) != NULL) {
239 bus_setup_intr(dev, dbdma_irq, ATA_INTR_FLAGS, NULL,
240 (driver_intr_t *)ata_interrupt, sc,&cookie);
244 /* Set begin_transaction */
245 sc->sc_ch.sc_ch.hw.begin_transaction = ata_macio_begin_transaction;
247 return ata_attach(dev);
251 ata_macio_setmode(device_t parent, device_t dev)
253 struct ata_device *atadev = device_get_softc(dev);
254 struct ata_macio_softc *sc = device_get_softc(parent);
255 int mode = atadev->mode;
257 int min_cycle = 0, min_active = 0;
258 int cycle_tick = 0, act_tick = 0, inact_tick = 0, half_tick;
260 mode = ata_limit_mode(dev, mode, sc->max_mode);
262 /* XXX Some controllers don't work correctly with ATAPI DMA */
263 if (atadev->param.config & ATA_PROTO_ATAPI)
264 mode = ata_limit_mode(dev, mode, ATA_PIO_MAX);
266 if (ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode))
271 if ((mode & ATA_DMA_MASK) == ATA_UDMA0) {
272 min_cycle = udma_timings[mode & ATA_MODE_MASK].cycle;
273 min_active = udma_timings[mode & ATA_MODE_MASK].active;
275 cycle_tick = ATA_TIME_TO_TICK(sc->rev,min_cycle);
276 act_tick = ATA_TIME_TO_TICK(sc->rev,min_active);
278 /* mask: 0x1ff00000 */
279 sc->udmaconf[atadev->unit] =
280 (cycle_tick << 21) | (act_tick << 25) | 0x100000;
281 } else if ((mode & ATA_DMA_MASK) == ATA_WDMA0) {
282 min_cycle = dma_timings[mode & ATA_MODE_MASK].cycle;
283 min_active = dma_timings[mode & ATA_MODE_MASK].active;
285 cycle_tick = ATA_TIME_TO_TICK(sc->rev,min_cycle);
286 act_tick = ATA_TIME_TO_TICK(sc->rev,min_active);
289 inact_tick = cycle_tick - act_tick;
290 /* mask: 0x001ffc00 */
291 sc->wdmaconf[atadev->unit] =
292 (act_tick << 10) | (inact_tick << 15);
294 inact_tick = cycle_tick - act_tick - DMA_REC_OFFSET;
295 if (inact_tick < DMA_REC_MIN)
296 inact_tick = DMA_REC_MIN;
297 half_tick = 0; /* XXX */
299 /* mask: 0xfffff800 */
300 sc->wdmaconf[atadev->unit] = (half_tick << 21)
301 | (inact_tick << 16) | (act_tick << 11);
305 pio_timings[(mode & ATA_MODE_MASK) - ATA_PIO0].cycle;
307 pio_timings[(mode & ATA_MODE_MASK) - ATA_PIO0].active;
309 cycle_tick = ATA_TIME_TO_TICK(sc->rev,min_cycle);
310 act_tick = ATA_TIME_TO_TICK(sc->rev,min_active);
313 inact_tick = cycle_tick - act_tick;
315 /* mask: 0x000003ff */
316 sc->pioconf[atadev->unit] =
317 (inact_tick << 5) | act_tick;
319 if (act_tick < PIO_ACT_MIN)
320 act_tick = PIO_ACT_MIN;
322 inact_tick = cycle_tick - act_tick - PIO_REC_OFFSET;
323 if (inact_tick < PIO_REC_MIN)
324 inact_tick = PIO_REC_MIN;
326 /* mask: 0x000007ff */
327 sc->pioconf[atadev->unit] =
328 (inact_tick << 5) | act_tick;
334 ata_macio_begin_transaction(struct ata_request *request)
336 struct ata_device *atadev = device_get_softc(request->dev);
337 struct ata_macio_softc *sc = device_get_softc(request->parent);
339 bus_write_4(sc->sc_mem, ATA_MACIO_TIMINGREG,
340 sc->udmaconf[atadev->unit] | sc->wdmaconf[atadev->unit]
341 | sc->pioconf[atadev->unit]);
343 return ata_begin_transaction(request);