2 * Copyright (c) 1998 - 2008 Søren Schmidt <sos@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/module.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
37 #include <sys/endian.h>
38 #include <sys/malloc.h>
40 #include <sys/mutex.h>
42 #include <sys/taskqueue.h>
44 #include <machine/stdarg.h>
45 #include <machine/resource.h>
46 #include <machine/bus.h>
48 #include <dev/pci/pcivar.h>
49 #include <dev/pci/pcireg.h>
50 #include <dev/ata/ata-all.h>
51 #include <dev/ata/ata-pci.h>
54 /* local prototypes */
55 static int ata_marvell_pata_chipinit(device_t dev);
56 static int ata_marvell_pata_ch_attach(device_t dev);
57 static void ata_marvell_pata_setmode(device_t dev, int mode);
58 static int ata_marvell_edma_ch_attach(device_t dev);
59 static int ata_marvell_edma_ch_detach(device_t dev);
60 static int ata_marvell_edma_status(device_t dev);
61 static int ata_marvell_edma_begin_transaction(struct ata_request *request);
62 static int ata_marvell_edma_end_transaction(struct ata_request *request);
63 static void ata_marvell_edma_reset(device_t dev);
64 static void ata_marvell_edma_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs, int error);
65 static void ata_marvell_edma_dmainit(device_t dev);
74 * Marvell chipset support functions
76 #define ATA_MV_HOST_BASE(ch) \
77 ((ch->unit & 3) * 0x0100) + (ch->unit > 3 ? 0x30000 : 0x20000)
78 #define ATA_MV_EDMA_BASE(ch) \
79 ((ch->unit & 3) * 0x2000) + (ch->unit > 3 ? 0x30000 : 0x20000)
81 struct ata_marvell_response {
88 struct ata_marvell_dma_prdentry {
96 ata_marvell_probe(device_t dev)
98 struct ata_pci_controller *ctlr = device_get_softc(dev);
99 static struct ata_chip_id ids[] =
100 {{ ATA_M88SX5040, 0, 4, MV_50XX, ATA_SA150, "88SX5040" },
101 { ATA_M88SX5041, 0, 4, MV_50XX, ATA_SA150, "88SX5041" },
102 { ATA_M88SX5080, 0, 8, MV_50XX, ATA_SA150, "88SX5080" },
103 { ATA_M88SX5081, 0, 8, MV_50XX, ATA_SA150, "88SX5081" },
104 { ATA_M88SX6041, 0, 4, MV_60XX, ATA_SA300, "88SX6041" },
105 { ATA_M88SX6081, 0, 8, MV_60XX, ATA_SA300, "88SX6081" },
106 { ATA_M88SX6101, 0, 1, MV_61XX, ATA_UDMA6, "88SX6101" },
107 { ATA_M88SX6121, 0, 1, MV_61XX, ATA_UDMA6, "88SX6121" },
108 { ATA_M88SX6145, 0, 2, MV_61XX, ATA_UDMA6, "88SX6145" },
109 { 0, 0, 0, 0, 0, 0}};
111 if (pci_get_vendor(dev) != ATA_MARVELL_ID)
114 if (!(ctlr->chip = ata_match_chip(dev, ids)))
119 switch (ctlr->chip->cfg2) {
122 ctlr->chipinit = ata_marvell_edma_chipinit;
125 ctlr->chipinit = ata_marvell_pata_chipinit;
128 return (BUS_PROBE_DEFAULT);
132 ata_marvell_pata_chipinit(device_t dev)
134 struct ata_pci_controller *ctlr = device_get_softc(dev);
136 if (ata_setup_interrupt(dev, ata_generic_intr))
139 ctlr->ch_attach = ata_marvell_pata_ch_attach;
140 ctlr->ch_detach = ata_pci_ch_detach;
141 ctlr->setmode = ata_marvell_pata_setmode;
142 ctlr->channels = ctlr->chip->cfg1;
147 ata_marvell_pata_ch_attach(device_t dev)
149 struct ata_channel *ch = device_get_softc(dev);
151 /* setup the usual register normal pci style */
152 if (ata_pci_ch_attach(dev))
155 /* dont use 32 bit PIO transfers */
156 ch->flags |= ATA_USE_16BIT;
162 ata_marvell_pata_setmode(device_t dev, int mode)
164 device_t gparent = GRANDPARENT(dev);
165 struct ata_pci_controller *ctlr = device_get_softc(gparent);
166 struct ata_device *atadev = device_get_softc(dev);
168 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
169 mode = ata_check_80pin(dev, mode);
170 if (!ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode))
175 ata_marvell_edma_chipinit(device_t dev)
177 struct ata_pci_controller *ctlr = device_get_softc(dev);
179 if (ata_setup_interrupt(dev, ata_generic_intr))
182 ctlr->r_type1 = SYS_RES_MEMORY;
183 ctlr->r_rid1 = PCIR_BAR(0);
184 if (!(ctlr->r_res1 = bus_alloc_resource_any(dev, ctlr->r_type1,
185 &ctlr->r_rid1, RF_ACTIVE)))
188 /* mask all host controller interrupts */
189 ATA_OUTL(ctlr->r_res1, 0x01d64, 0x00000000);
191 /* mask all PCI interrupts */
192 ATA_OUTL(ctlr->r_res1, 0x01d5c, 0x00000000);
194 ctlr->ch_attach = ata_marvell_edma_ch_attach;
195 ctlr->ch_detach = ata_marvell_edma_ch_detach;
196 ctlr->reset = ata_marvell_edma_reset;
197 ctlr->setmode = ata_sata_setmode;
198 ctlr->channels = ctlr->chip->cfg1;
200 /* clear host controller interrupts */
201 ATA_OUTL(ctlr->r_res1, 0x20014, 0x00000000);
202 if (ctlr->chip->cfg1 > 4)
203 ATA_OUTL(ctlr->r_res1, 0x30014, 0x00000000);
205 /* clear PCI interrupts */
206 ATA_OUTL(ctlr->r_res1, 0x01d58, 0x00000000);
208 /* unmask PCI interrupts we want */
209 ATA_OUTL(ctlr->r_res1, 0x01d5c, 0x007fffff);
211 /* unmask host controller interrupts we want */
212 ATA_OUTL(ctlr->r_res1, 0x01d64, 0x000000ff/*HC0*/ | 0x0001fe00/*HC1*/ |
213 /*(1<<19) | (1<<20) | (1<<21) |*/(1<<22) | (1<<24) | (0x7f << 25));
219 ata_marvell_edma_ch_attach(device_t dev)
221 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
222 struct ata_channel *ch = device_get_softc(dev);
226 ata_marvell_edma_dmainit(dev);
227 work = ch->dma.work_bus;
228 /* clear work area */
229 bzero(ch->dma.work, 1024+256);
230 bus_dmamap_sync(ch->dma.work_tag, ch->dma.work_map,
231 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
233 /* set legacy ATA resources */
234 for (i = ATA_DATA; i <= ATA_COMMAND; i++) {
235 ch->r_io[i].res = ctlr->r_res1;
236 ch->r_io[i].offset = 0x02100 + (i << 2) + ATA_MV_EDMA_BASE(ch);
238 ch->r_io[ATA_CONTROL].res = ctlr->r_res1;
239 ch->r_io[ATA_CONTROL].offset = 0x02120 + ATA_MV_EDMA_BASE(ch);
240 ch->r_io[ATA_IDX_ADDR].res = ctlr->r_res1;
241 ata_default_registers(dev);
243 /* set SATA resources */
244 switch (ctlr->chip->cfg2) {
246 ch->r_io[ATA_SSTATUS].res = ctlr->r_res1;
247 ch->r_io[ATA_SSTATUS].offset = 0x00100 + ATA_MV_HOST_BASE(ch);
248 ch->r_io[ATA_SERROR].res = ctlr->r_res1;
249 ch->r_io[ATA_SERROR].offset = 0x00104 + ATA_MV_HOST_BASE(ch);
250 ch->r_io[ATA_SCONTROL].res = ctlr->r_res1;
251 ch->r_io[ATA_SCONTROL].offset = 0x00108 + ATA_MV_HOST_BASE(ch);
254 ch->r_io[ATA_SSTATUS].res = ctlr->r_res1;
255 ch->r_io[ATA_SSTATUS].offset = 0x02300 + ATA_MV_EDMA_BASE(ch);
256 ch->r_io[ATA_SERROR].res = ctlr->r_res1;
257 ch->r_io[ATA_SERROR].offset = 0x02304 + ATA_MV_EDMA_BASE(ch);
258 ch->r_io[ATA_SCONTROL].res = ctlr->r_res1;
259 ch->r_io[ATA_SCONTROL].offset = 0x02308 + ATA_MV_EDMA_BASE(ch);
260 ch->r_io[ATA_SACTIVE].res = ctlr->r_res1;
261 ch->r_io[ATA_SACTIVE].offset = 0x02350 + ATA_MV_EDMA_BASE(ch);
265 ch->flags |= ATA_NO_SLAVE;
266 ch->flags |= ATA_USE_16BIT; /* XXX SOS needed ? */
268 ch->hw.begin_transaction = ata_marvell_edma_begin_transaction;
269 ch->hw.end_transaction = ata_marvell_edma_end_transaction;
270 ch->hw.status = ata_marvell_edma_status;
272 /* disable the EDMA machinery */
273 ATA_OUTL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch), 0x00000002);
274 DELAY(100000); /* SOS should poll for disabled */
276 /* set configuration to non-queued 128b read transfers stop on error */
277 ATA_OUTL(ctlr->r_res1, 0x02000 + ATA_MV_EDMA_BASE(ch), (1<<11) | (1<<13));
279 /* request queue base high */
280 ATA_OUTL(ctlr->r_res1, 0x02010 + ATA_MV_EDMA_BASE(ch), work >> 32);
282 /* request queue in ptr */
283 ATA_OUTL(ctlr->r_res1, 0x02014 + ATA_MV_EDMA_BASE(ch), work & 0xffffffff);
285 /* request queue out ptr */
286 ATA_OUTL(ctlr->r_res1, 0x02018 + ATA_MV_EDMA_BASE(ch), 0x0);
288 /* response queue base high */
290 ATA_OUTL(ctlr->r_res1, 0x0201c + ATA_MV_EDMA_BASE(ch), work >> 32);
292 /* response queue in ptr */
293 ATA_OUTL(ctlr->r_res1, 0x02020 + ATA_MV_EDMA_BASE(ch), 0x0);
295 /* response queue out ptr */
296 ATA_OUTL(ctlr->r_res1, 0x02024 + ATA_MV_EDMA_BASE(ch), work & 0xffffffff);
298 /* clear SATA error register */
299 ATA_IDX_OUTL(ch, ATA_SERROR, ATA_IDX_INL(ch, ATA_SERROR));
301 /* clear any outstanding error interrupts */
302 ATA_OUTL(ctlr->r_res1, 0x02008 + ATA_MV_EDMA_BASE(ch), 0x0);
304 /* unmask all error interrupts */
305 ATA_OUTL(ctlr->r_res1, 0x0200c + ATA_MV_EDMA_BASE(ch), ~0x0);
307 /* enable EDMA machinery */
308 ATA_OUTL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch), 0x00000001);
313 ata_marvell_edma_ch_detach(device_t dev)
315 struct ata_channel *ch = device_get_softc(dev);
317 if (ch->dma.work_tag && ch->dma.work_map)
318 bus_dmamap_sync(ch->dma.work_tag, ch->dma.work_map,
319 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
325 ata_marvell_edma_status(device_t dev)
327 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
328 struct ata_channel *ch = device_get_softc(dev);
329 u_int32_t cause = ATA_INL(ctlr->r_res1, 0x01d60);
330 int shift = (ch->unit << 1) + (ch->unit > 3);
332 if (cause & (1 << shift)) {
334 /* clear interrupt(s) */
335 ATA_OUTL(ctlr->r_res1, 0x02008 + ATA_MV_EDMA_BASE(ch), 0x0);
337 /* do we have any PHY events ? */
338 ata_sata_phy_check_events(dev);
341 /* do we have any device action ? */
342 return (cause & (2 << shift));
345 /* must be called with ATA channel locked and state_mtx held */
347 ata_marvell_edma_begin_transaction(struct ata_request *request)
349 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
350 struct ata_channel *ch = device_get_softc(request->parent);
356 /* only DMA R/W goes through the EMDA machine */
357 if (request->u.ata.command != ATA_READ_DMA &&
358 request->u.ata.command != ATA_WRITE_DMA) {
360 /* disable the EDMA machinery */
361 if (ATA_INL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch)) & 0x00000001)
362 ATA_OUTL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch), 0x00000002);
363 return ata_begin_transaction(request);
366 /* check for 48 bit access and convert if needed */
367 ata_modify_if_48bit(request);
369 /* check sanity, setup SG list and DMA engine */
370 if ((error = ch->dma.load(request, NULL, NULL))) {
371 device_printf(request->dev, "setting up DMA failed\n");
372 request->result = error;
373 return ATA_OP_FINISHED;
376 /* get next free request queue slot */
377 req_in = ATA_INL(ctlr->r_res1, 0x02014 + ATA_MV_EDMA_BASE(ch));
378 slot = (((req_in & ~0xfffffc00) >> 5) + 0) & 0x1f;
379 bytep = (u_int8_t *)(ch->dma.work);
380 bytep += (slot << 5);
382 /* fill in this request */
383 le32enc(bytep + 0 * sizeof(u_int32_t),
384 request->dma->sg_bus & 0xffffffff);
385 le32enc(bytep + 1 * sizeof(u_int32_t),
386 (u_int64_t)request->dma->sg_bus >> 32);
387 le16enc(bytep + 4 * sizeof(u_int16_t),
388 (request->flags & ATA_R_READ ? 0x01 : 0x00) | (request->tag << 1));
391 bytep[i++] = (request->u.ata.count >> 8) & 0xff;
392 bytep[i++] = 0x10 | ATA_COUNT;
393 bytep[i++] = request->u.ata.count & 0xff;
394 bytep[i++] = 0x10 | ATA_COUNT;
396 bytep[i++] = (request->u.ata.lba >> 24) & 0xff;
397 bytep[i++] = 0x10 | ATA_SECTOR;
398 bytep[i++] = request->u.ata.lba & 0xff;
399 bytep[i++] = 0x10 | ATA_SECTOR;
401 bytep[i++] = (request->u.ata.lba >> 32) & 0xff;
402 bytep[i++] = 0x10 | ATA_CYL_LSB;
403 bytep[i++] = (request->u.ata.lba >> 8) & 0xff;
404 bytep[i++] = 0x10 | ATA_CYL_LSB;
406 bytep[i++] = (request->u.ata.lba >> 40) & 0xff;
407 bytep[i++] = 0x10 | ATA_CYL_MSB;
408 bytep[i++] = (request->u.ata.lba >> 16) & 0xff;
409 bytep[i++] = 0x10 | ATA_CYL_MSB;
411 bytep[i++] = ATA_D_LBA | ATA_D_IBM | ((request->u.ata.lba >> 24) & 0xf);
412 bytep[i++] = 0x10 | ATA_DRIVE;
414 bytep[i++] = request->u.ata.command;
415 bytep[i++] = 0x90 | ATA_COMMAND;
417 bus_dmamap_sync(ch->dma.work_tag, ch->dma.work_map,
418 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
420 /* enable EDMA machinery if needed */
421 if (!(ATA_INL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch)) & 0x00000001)) {
422 ATA_OUTL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch), 0x00000001);
423 while (!(ATA_INL(ctlr->r_res1,
424 0x02028 + ATA_MV_EDMA_BASE(ch)) & 0x00000001))
428 /* tell EDMA it has a new request */
429 slot = (((req_in & ~0xfffffc00) >> 5) + 1) & 0x1f;
430 req_in &= 0xfffffc00;
431 req_in += (slot << 5);
432 ATA_OUTL(ctlr->r_res1, 0x02014 + ATA_MV_EDMA_BASE(ch), req_in);
434 return ATA_OP_CONTINUES;
437 /* must be called with ATA channel locked and state_mtx held */
439 ata_marvell_edma_end_transaction(struct ata_request *request)
441 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
442 struct ata_channel *ch = device_get_softc(request->parent);
443 int offset = (ch->unit > 3 ? 0x30014 : 0x20014);
444 u_int32_t icr = ATA_INL(ctlr->r_res1, offset);
448 if ((icr & (0x0001 << (ch->unit & 3)))) {
449 struct ata_marvell_response *response;
450 u_int32_t rsp_in, rsp_out;
454 callout_stop(&request->callout);
456 /* get response ptr's */
457 rsp_in = ATA_INL(ctlr->r_res1, 0x02020 + ATA_MV_EDMA_BASE(ch));
458 rsp_out = ATA_INL(ctlr->r_res1, 0x02024 + ATA_MV_EDMA_BASE(ch));
459 slot = (((rsp_in & ~0xffffff00) >> 3)) & 0x1f;
460 rsp_out &= 0xffffff00;
461 rsp_out += (slot << 3);
462 bus_dmamap_sync(ch->dma.work_tag, ch->dma.work_map,
463 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
464 response = (struct ata_marvell_response *)
465 (ch->dma.work + 1024 + (slot << 3));
467 /* record status for this request */
468 request->status = response->dev_status;
472 ATA_OUTL(ctlr->r_res1, 0x02024 + ATA_MV_EDMA_BASE(ch), rsp_out);
474 /* update progress */
475 if (!(request->status & ATA_S_ERROR) &&
476 !(request->flags & ATA_R_TIMEOUT))
477 request->donecount = request->bytecount;
480 ch->dma.unload(request);
482 res = ATA_OP_FINISHED;
485 /* legacy ATA interrupt */
487 res = ata_end_transaction(request);
491 ATA_OUTL(ctlr->r_res1, offset, ~(icr & (0x0101 << (ch->unit & 3))));
496 ata_marvell_edma_reset(device_t dev)
498 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
499 struct ata_channel *ch = device_get_softc(dev);
501 /* disable the EDMA machinery */
502 ATA_OUTL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch), 0x00000002);
503 while ((ATA_INL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch)) & 0x00000001))
506 /* clear SATA error register */
507 ATA_IDX_OUTL(ch, ATA_SERROR, ATA_IDX_INL(ch, ATA_SERROR));
509 /* clear any outstanding error interrupts */
510 ATA_OUTL(ctlr->r_res1, 0x02008 + ATA_MV_EDMA_BASE(ch), 0x0);
512 /* unmask all error interrupts */
513 ATA_OUTL(ctlr->r_res1, 0x0200c + ATA_MV_EDMA_BASE(ch), ~0x0);
515 /* enable channel and test for devices */
516 if (ata_sata_phy_reset(dev, -1, 1))
517 ata_generic_reset(dev);
519 /* enable EDMA machinery */
520 ATA_OUTL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch), 0x00000001);
524 ata_marvell_edma_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs,
527 struct ata_dmasetprd_args *args = xsc;
528 struct ata_marvell_dma_prdentry *prd = args->dmatab;
531 if ((args->error = error))
534 for (i = 0; i < nsegs; i++) {
535 prd[i].addrlo = htole32(segs[i].ds_addr);
536 prd[i].count = htole32(segs[i].ds_len);
537 prd[i].addrhi = htole32((u_int64_t)segs[i].ds_addr >> 32);
540 prd[i - 1].count |= htole32(ATA_DMA_EOT);
541 KASSERT(nsegs <= ATA_DMA_ENTRIES, ("too many DMA segment entries\n"));
546 ata_marvell_edma_dmainit(device_t dev)
548 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
549 struct ata_channel *ch = device_get_softc(dev);
552 /* note start and stop are not used here */
553 ch->dma.setprd = ata_marvell_edma_dmasetprd;
555 /* if 64bit support present adjust max address used */
556 if (ATA_INL(ctlr->r_res1, 0x00d00) & 0x00000004)
557 ch->dma.max_address = BUS_SPACE_MAXADDR;
559 /* chip does not reliably do 64K DMA transfers */
560 ch->dma.max_iosize = 64 * DEV_BSIZE;
563 ATA_DECLARE_DRIVER(ata_marvell);