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_ahci_ch_attach(device_t dev);
56 static int ata_ahci_ch_detach(device_t dev);
57 static int ata_ahci_ch_suspend(device_t dev);
58 static int ata_ahci_ch_resume(device_t dev);
59 static int ata_ahci_ctlr_reset(device_t dev);
60 static void ata_ahci_reset(device_t dev);
61 static int ata_ahci_suspend(device_t dev);
62 static int ata_ahci_status(device_t dev);
63 static int ata_ahci_begin_transaction(struct ata_request *request);
64 static int ata_ahci_end_transaction(struct ata_request *request);
65 static int ata_ahci_pm_read(device_t dev, int port, int reg, u_int32_t *result);
66 static int ata_ahci_pm_write(device_t dev, int port, int reg, u_int32_t result);
67 static int ata_ahci_hardreset(device_t dev, int port, uint32_t *signature);
68 static u_int32_t ata_ahci_softreset(device_t dev, int port);
69 static void ata_ahci_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs, int error);
70 static int ata_ahci_setup_fis(struct ata_ahci_cmd_tab *ctp, struct ata_request *equest);
71 static void ata_ahci_dmainit(device_t dev);
72 static void ata_ahci_start(device_t dev);
73 static void ata_ahci_stop(device_t dev);
74 static void ata_ahci_clo(device_t dev);
75 static void ata_ahci_start_fr(device_t dev);
76 static void ata_ahci_stop_fr(device_t dev);
79 * AHCI v1.x compliant SATA chipset support functions
82 ata_ahci_probe(device_t dev)
84 struct ata_pci_controller *ctlr = device_get_softc(dev);
87 /* is this a possible AHCI candidate ? */
88 if (pci_get_class(dev) != PCIC_STORAGE ||
89 pci_get_subclass(dev) != PCIS_STORAGE_SATA)
92 /* is this PCI device flagged as an AHCI compliant chip ? */
93 if (pci_get_progif(dev) != PCIP_STORAGE_SATA_AHCI_1_0)
97 sprintf(buffer, "%s (ID=%08x) AHCI controller",
98 ata_pcivendor2str(dev), pci_get_devid(dev));
100 sprintf(buffer, "%s AHCI controller", ata_pcivendor2str(dev));
101 device_set_desc_copy(dev, buffer);
102 ctlr->chipinit = ata_ahci_chipinit;
103 return (BUS_PROBE_GENERIC);
107 ata_ahci_ata_probe(device_t dev)
109 struct ata_pci_controller *ctlr = device_get_softc(dev);
111 if ((intptr_t)device_get_ivars(dev) >= 0)
113 device_set_desc_copy(dev, "AHCI SATA controller");
114 ctlr->chipinit = ata_ahci_chipinit;
115 return (BUS_PROBE_GENERIC);
119 ata_ahci_ata_attach(device_t dev)
121 struct ata_pci_controller *ctlr = device_get_softc(dev);
125 /* do chipset specific setups only needed once */
127 ctlr->ichannels = -1;
128 ctlr->ch_attach = ata_pci_ch_attach;
129 ctlr->ch_detach = ata_pci_ch_detach;
131 if (ctlr->chipinit(dev))
133 /* attach all channels on this controller */
134 for (unit = 0; unit < ctlr->channels; unit++) {
135 if ((ctlr->ichannels & (1 << unit)) == 0)
137 child = device_add_child(dev, "ata",
138 ((unit == 0 || unit == 1) && ctlr->legacy) ?
139 unit : devclass_find_free_unit(ata_devclass, 2));
141 device_printf(dev, "failed to add ata child device\n");
143 device_set_ivars(child, (void *)(intptr_t)unit);
145 bus_generic_attach(dev);
150 ata_ahci_chipinit(device_t dev)
152 struct ata_pci_controller *ctlr = device_get_softc(dev);
154 u_int32_t caps, version;
156 /* if we have a memory BAR(5) we are likely on an AHCI part */
157 ctlr->r_type2 = SYS_RES_MEMORY;
158 ctlr->r_rid2 = PCIR_BAR(5);
159 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
160 &ctlr->r_rid2, RF_ACTIVE)))
163 /* setup interrupt delivery if not done allready by a vendor driver */
165 if (ata_setup_interrupt(dev, ata_generic_intr)) {
166 bus_release_resource(dev, ctlr->r_type2, ctlr->r_rid2, ctlr->r_res2);
171 device_printf(dev, "AHCI called from vendor specific driver\n");
173 /* reset controller */
174 if ((error = ata_ahci_ctlr_reset(dev)) != 0) {
175 bus_release_resource(dev, ctlr->r_type2, ctlr->r_rid2, ctlr->r_res2);
179 /* get the number of HW channels */
180 ctlr->ichannels = ATA_INL(ctlr->r_res2, ATA_AHCI_PI);
181 ctlr->channels = MAX(flsl(ctlr->ichannels),
182 (ATA_INL(ctlr->r_res2, ATA_AHCI_CAP) & ATA_AHCI_CAP_NPMASK) + 1);
183 if (pci_get_devid(dev) == ATA_M88SX6111)
185 else if (pci_get_devid(dev) == ATA_M88SX6121)
187 else if (pci_get_devid(dev) == ATA_M88SX6141 ||
188 pci_get_devid(dev) == ATA_M88SX6145)
191 ctlr->reset = ata_ahci_reset;
192 ctlr->ch_attach = ata_ahci_ch_attach;
193 ctlr->ch_detach = ata_ahci_ch_detach;
194 ctlr->ch_suspend = ata_ahci_ch_suspend;
195 ctlr->ch_resume = ata_ahci_ch_resume;
196 ctlr->setmode = ata_sata_setmode;
197 ctlr->getrev = ata_sata_getrev;
198 ctlr->suspend = ata_ahci_suspend;
199 ctlr->resume = ata_ahci_ctlr_reset;
201 /* announce we support the HW */
202 version = ATA_INL(ctlr->r_res2, ATA_AHCI_VS);
203 caps = ATA_INL(ctlr->r_res2, ATA_AHCI_CAP);
204 speed = (caps & ATA_AHCI_CAP_ISS) >> ATA_AHCI_CAP_ISS_SHIFT;
206 "AHCI v%x.%02x controller with %d %sGbps ports, PM %s\n",
207 ((version >> 20) & 0xf0) + ((version >> 16) & 0x0f),
208 ((version >> 4) & 0xf0) + (version & 0x0f),
209 (caps & ATA_AHCI_CAP_NPMASK) + 1,
210 ((speed == 1) ? "1.5":((speed == 2) ? "3":
211 ((speed == 3) ? "6":"?"))),
212 (caps & ATA_AHCI_CAP_SPM) ?
213 "supported" : "not supported");
215 device_printf(dev, "Caps:%s%s%s%s%s%s%s%s %sGbps",
216 (caps & ATA_AHCI_CAP_64BIT) ? " 64bit":"",
217 (caps & ATA_AHCI_CAP_SNCQ) ? " NCQ":"",
218 (caps & ATA_AHCI_CAP_SSNTF) ? " SNTF":"",
219 (caps & ATA_AHCI_CAP_SMPS) ? " MPS":"",
220 (caps & ATA_AHCI_CAP_SSS) ? " SS":"",
221 (caps & ATA_AHCI_CAP_SALP) ? " ALP":"",
222 (caps & ATA_AHCI_CAP_SAL) ? " AL":"",
223 (caps & ATA_AHCI_CAP_SCLO) ? " CLO":"",
224 ((speed == 1) ? "1.5":((speed == 2) ? "3":
225 ((speed == 3) ? "6":"?"))));
226 printf("%s%s%s%s%s%s %dcmd%s%s%s %dports\n",
227 (caps & ATA_AHCI_CAP_SAM) ? " AM":"",
228 (caps & ATA_AHCI_CAP_SPM) ? " PM":"",
229 (caps & ATA_AHCI_CAP_FBSS) ? " FBS":"",
230 (caps & ATA_AHCI_CAP_PMD) ? " PMD":"",
231 (caps & ATA_AHCI_CAP_SSC) ? " SSC":"",
232 (caps & ATA_AHCI_CAP_PSC) ? " PSC":"",
233 ((caps & ATA_AHCI_CAP_NCS) >> ATA_AHCI_CAP_NCS_SHIFT) + 1,
234 (caps & ATA_AHCI_CAP_CCCS) ? " CCC":"",
235 (caps & ATA_AHCI_CAP_EMS) ? " EM":"",
236 (caps & ATA_AHCI_CAP_SXS) ? " eSATA":"",
237 (caps & ATA_AHCI_CAP_NPMASK) + 1);
243 ata_ahci_ctlr_reset(device_t dev)
245 struct ata_pci_controller *ctlr = device_get_softc(dev);
248 /* enable AHCI mode */
249 ATA_OUTL(ctlr->r_res2, ATA_AHCI_GHC, ATA_AHCI_GHC_AE);
251 /* reset AHCI controller */
252 ATA_OUTL(ctlr->r_res2, ATA_AHCI_GHC, ATA_AHCI_GHC_AE|ATA_AHCI_GHC_HR);
253 for (timeout = 1000; timeout > 0; timeout--) {
255 if ((ATA_INL(ctlr->r_res2, ATA_AHCI_GHC) & ATA_AHCI_GHC_HR) == 0)
259 device_printf(dev, "AHCI controller reset failure\n");
263 /* reenable AHCI mode */
264 ATA_OUTL(ctlr->r_res2, ATA_AHCI_GHC, ATA_AHCI_GHC_AE);
266 /* clear interrupts */
267 ATA_OUTL(ctlr->r_res2, ATA_AHCI_IS, ATA_INL(ctlr->r_res2, ATA_AHCI_IS));
269 /* enable AHCI interrupts */
270 ATA_OUTL(ctlr->r_res2, ATA_AHCI_GHC,
271 ATA_INL(ctlr->r_res2, ATA_AHCI_GHC) | ATA_AHCI_GHC_IE);
277 ata_ahci_suspend(device_t dev)
279 struct ata_pci_controller *ctlr = device_get_softc(dev);
281 /* disable interupts so the state change(s) doesn't trigger */
282 ATA_OUTL(ctlr->r_res2, ATA_AHCI_GHC,
283 ATA_INL(ctlr->r_res2, ATA_AHCI_GHC) & (~ATA_AHCI_GHC_IE));
288 ata_ahci_ch_attach(device_t dev)
290 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
291 struct ata_channel *ch = device_get_softc(dev);
292 int offset = ch->unit << 7;
294 ata_ahci_dmainit(dev);
296 /* set the SATA resources */
297 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
298 ch->r_io[ATA_SSTATUS].offset = ATA_AHCI_P_SSTS + offset;
299 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
300 ch->r_io[ATA_SERROR].offset = ATA_AHCI_P_SERR + offset;
301 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
302 ch->r_io[ATA_SCONTROL].offset = ATA_AHCI_P_SCTL + offset;
303 ch->r_io[ATA_SACTIVE].res = ctlr->r_res2;
304 ch->r_io[ATA_SACTIVE].offset = ATA_AHCI_P_SACT + offset;
306 ch->hw.status = ata_ahci_status;
307 ch->hw.begin_transaction = ata_ahci_begin_transaction;
308 ch->hw.end_transaction = ata_ahci_end_transaction;
309 ch->hw.command = NULL; /* not used here */
310 ch->hw.softreset = ata_ahci_softreset;
311 ch->hw.pm_read = ata_ahci_pm_read;
312 ch->hw.pm_write = ata_ahci_pm_write;
313 ch->flags |= ATA_NO_SLAVE;
314 ch->flags |= ATA_SATA;
316 ata_ahci_ch_resume(dev);
321 ata_ahci_ch_detach(device_t dev)
324 ata_ahci_ch_suspend(dev);
330 ata_ahci_ch_suspend(device_t dev)
332 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
333 struct ata_channel *ch = device_get_softc(dev);
334 int offset = ch->unit << 7;
336 /* Disable port interrupts. */
337 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_IE + offset, 0);
338 /* Reset command register. */
340 ata_ahci_stop_fr(dev);
341 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset, 0);
343 /* Allow everything including partial and slumber modes. */
344 ATA_IDX_OUTL(ch, ATA_SCONTROL, 0);
345 /* Request slumber mode transition and give some time to get there. */
346 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset, ATA_AHCI_P_CMD_SLUMBER);
349 ATA_IDX_OUTL(ch, ATA_SCONTROL, ATA_SC_DET_DISABLE);
355 ata_ahci_ch_resume(device_t dev)
357 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
358 struct ata_channel *ch = device_get_softc(dev);
360 int offset = ch->unit << 7;
362 /* Disable port interrupts */
363 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_IE + offset, 0);
365 /* setup work areas */
366 work = ch->dma.work_bus + ATA_AHCI_CL_OFFSET;
367 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CLB + offset, work & 0xffffffff);
368 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CLBU + offset, work >> 32);
370 work = ch->dma.work_bus + ATA_AHCI_FB_OFFSET;
371 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_FB + offset, work & 0xffffffff);
372 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_FBU + offset, work >> 32);
374 /* activate the channel and power/spin up device */
375 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset,
376 (ATA_AHCI_P_CMD_ACTIVE | ATA_AHCI_P_CMD_POD | ATA_AHCI_P_CMD_SUD |
377 ((ch->pm_level > 1) ? ATA_AHCI_P_CMD_ALPE : 0) |
378 ((ch->pm_level > 2) ? ATA_AHCI_P_CMD_ASP : 0 )));
379 ata_ahci_start_fr(dev);
386 ata_ahci_status(device_t dev)
388 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
389 struct ata_channel *ch = device_get_softc(dev);
390 u_int32_t action = ATA_INL(ctlr->r_res2, ATA_AHCI_IS);
391 int offset = ch->unit << 7;
393 #define ATA_AHCI_STATBITS \
394 (ATA_AHCI_P_IX_IF|ATA_AHCI_P_IX_HBD|ATA_AHCI_P_IX_HBF|ATA_AHCI_P_IX_TFE)
396 if (action & (1 << ch->unit)) {
397 u_int32_t istatus = ATA_INL(ctlr->r_res2, ATA_AHCI_P_IS + offset);
398 u_int32_t cstatus = ATA_INL(ctlr->r_res2, ATA_AHCI_P_CI + offset);
400 /* clear interrupt(s) */
401 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_IS + offset, istatus);
402 ATA_OUTL(ctlr->r_res2, ATA_AHCI_IS, 1 << ch->unit);
404 /* do we have any PHY events ? */
405 if (istatus & (ATA_AHCI_P_IX_PRC | ATA_AHCI_P_IX_PC))
406 ata_sata_phy_check_events(dev);
408 /* do we have a potentially hanging engine to take care of? */
409 /* XXX SOS what todo on NCQ */
410 if ((istatus & ATA_AHCI_STATBITS) && (cstatus & 1)) {
412 u_int32_t cmd = ATA_INL(ctlr->r_res2, ATA_AHCI_P_CMD + offset);
415 /* kill off all activity on this channel */
416 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset,
417 cmd & ~(ATA_AHCI_P_CMD_FRE | ATA_AHCI_P_CMD_ST));
419 /* XXX SOS this is not entirely wrong */
422 if (timeout++ > 1000) {
423 device_printf(dev, "stopping AHCI engine failed\n");
426 } while (ATA_INL(ctlr->r_res2,
427 ATA_AHCI_P_CMD + offset) & ATA_AHCI_P_CMD_CR);
429 /* start operations on this channel */
430 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset,
431 cmd | (ATA_AHCI_P_CMD_FRE | ATA_AHCI_P_CMD_ST));
436 /* XXX SOS what todo on NCQ */
437 return (!(cstatus & 1));
442 /* must be called with ATA channel locked and state_mtx held */
444 ata_ahci_begin_transaction(struct ata_request *request)
446 struct ata_pci_controller *ctlr=device_get_softc(device_get_parent(request->parent));
447 struct ata_channel *ch = device_get_softc(request->parent);
448 struct ata_ahci_cmd_tab *ctp;
449 struct ata_ahci_cmd_list *clp;
450 int offset = ch->unit << 7;
451 int port = request->unit & 0x0f;
455 /* get a piece of the workspace for this request */
456 ctp = (struct ata_ahci_cmd_tab *)
457 (ch->dma.work + ATA_AHCI_CT_OFFSET);
459 /* setup the FIS for this request */
460 if (!(fis_size = ata_ahci_setup_fis(ctp, request))) {
461 device_printf(request->parent, "setting up SATA FIS failed\n");
462 request->result = EIO;
463 return ATA_OP_FINISHED;
466 /* if request moves data setup and load SG list */
467 if (request->flags & (ATA_R_READ | ATA_R_WRITE)) {
468 if (ch->dma.load(request, ctp->prd_tab, &entries)) {
469 device_printf(request->parent, "setting up DMA failed\n");
470 request->result = EIO;
471 return ATA_OP_FINISHED;
475 /* setup the command list entry */
476 clp = (struct ata_ahci_cmd_list *)
477 (ch->dma.work + ATA_AHCI_CL_OFFSET);
479 clp->prd_length = entries;
480 clp->cmd_flags = (request->flags & ATA_R_WRITE ? ATA_AHCI_CMD_WRITE : 0) |
481 (request->flags & ATA_R_ATAPI ?
482 (ATA_AHCI_CMD_ATAPI | ATA_AHCI_CMD_PREFETCH) : 0) |
483 (fis_size / sizeof(u_int32_t)) |
486 clp->cmd_table_phys = htole64(ch->dma.work_bus + ATA_AHCI_CT_OFFSET);
488 /* set command type bit */
489 if (request->flags & ATA_R_ATAPI)
490 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset,
491 ATA_INL(ctlr->r_res2, ATA_AHCI_P_CMD + offset) |
492 ATA_AHCI_P_CMD_ATAPI);
494 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset,
495 ATA_INL(ctlr->r_res2, ATA_AHCI_P_CMD + offset) &
496 ~ATA_AHCI_P_CMD_ATAPI);
498 /* issue command to controller */
499 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CI + offset, 1);
501 if (!(request->flags & ATA_R_ATAPI)) {
502 /* device reset doesn't interrupt */
503 if (request->u.ata.command == ATA_DEVICE_RESET) {
505 int timeout = 1000000;
509 tf_data = ATA_INL(ctlr->r_res2, ATA_AHCI_P_TFD + (ch->unit<<7));
510 } while ((tf_data & ATA_S_BUSY) && timeout--);
512 device_printf(ch->dev, "device_reset timeout=%dus\n",
513 (1000000-timeout)*10);
514 request->status = tf_data;
515 if (request->status & ATA_S_ERROR)
516 request->error = tf_data >> 8;
517 return ATA_OP_FINISHED;
521 /* start the timeout */
522 callout_reset(&request->callout, request->timeout * hz,
523 (timeout_t*)ata_timeout, request);
524 return ATA_OP_CONTINUES;
527 /* must be called with ATA channel locked and state_mtx held */
529 ata_ahci_end_transaction(struct ata_request *request)
531 struct ata_pci_controller *ctlr=device_get_softc(device_get_parent(request->parent));
532 struct ata_channel *ch = device_get_softc(request->parent);
533 struct ata_ahci_cmd_list *clp;
535 int offset = ch->unit << 7;
537 /* kill the timeout */
538 callout_stop(&request->callout);
541 tf_data = ATA_INL(ctlr->r_res2, ATA_AHCI_P_TFD + offset);
542 request->status = tf_data;
544 /* if error status get details */
545 if (request->status & ATA_S_ERROR)
546 request->error = tf_data >> 8;
548 /* on control commands read back registers to the request struct */
549 if (request->flags & ATA_R_CONTROL) {
550 u_int8_t *fis = ch->dma.work + ATA_AHCI_FB_OFFSET + 0x40;
552 request->u.ata.count = fis[12] | ((u_int16_t)fis[13] << 8);
553 request->u.ata.lba = fis[4] | ((u_int64_t)fis[5] << 8) |
554 ((u_int64_t)fis[6] << 16);
555 if (request->flags & ATA_R_48BIT)
556 request->u.ata.lba |= ((u_int64_t)fis[8] << 24) |
557 ((u_int64_t)fis[9] << 32) |
558 ((u_int64_t)fis[10] << 40);
560 request->u.ata.lba |= ((u_int64_t)(fis[7] & 0x0f) << 24);
563 /* record how much data we actually moved */
564 clp = (struct ata_ahci_cmd_list *)
565 (ch->dma.work + ATA_AHCI_CL_OFFSET);
566 request->donecount = clp->bytecount;
568 /* release SG list etc */
569 ch->dma.unload(request);
571 return ATA_OP_FINISHED;
575 ata_ahci_issue_cmd(device_t dev, u_int16_t flags, int timeout)
577 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
578 struct ata_channel *ch = device_get_softc(dev);
579 struct ata_ahci_cmd_list *clp =
580 (struct ata_ahci_cmd_list *)(ch->dma.work + ATA_AHCI_CL_OFFSET);
581 struct ata_ahci_cmd_tab *ctp =
582 (struct ata_ahci_cmd_tab *)(ch->dma.work + ATA_AHCI_CT_OFFSET);
583 u_int32_t status = 0;
584 int offset = ch->unit << 7;
585 int port = (ctp->cfis[1] & 0x0f);
589 clp->cmd_flags = (20 / sizeof(u_int32_t)) | flags | (port << 12);
591 clp->cmd_table_phys = htole64(ch->dma.work_bus + ATA_AHCI_CT_OFFSET);
593 /* issue command to controller */
594 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CI + offset, 1);
596 /* poll for command finished */
597 for (count = 0; count < timeout; count++) {
599 if (!((status = ATA_INL(ctlr->r_res2, ATA_AHCI_P_CI + offset)) & 1))
603 /* clear interrupts */
604 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_IS + offset,
605 ATA_INL(ctlr->r_res2, ATA_AHCI_P_IS + offset));
607 if (timeout && (count >= timeout)) {
609 device_printf(dev, "ahci_issue_cmd timeout: %d of %dms, status=%08x\n",
610 count, timeout, status);
619 ata_ahci_pm_read(device_t dev, int port, int reg, u_int32_t *result)
621 struct ata_channel *ch = device_get_softc(dev);
622 struct ata_ahci_cmd_tab *ctp =
623 (struct ata_ahci_cmd_tab *)(ch->dma.work + ATA_AHCI_CT_OFFSET);
624 u_int8_t *fis = ch->dma.work + ATA_AHCI_FB_OFFSET + 0x40;
626 bzero(ctp->cfis, 64);
627 ctp->cfis[0] = 0x27; /* host to device */
628 ctp->cfis[1] = 0x8f; /* command FIS to PM port */
629 ctp->cfis[2] = ATA_READ_PM;
631 ctp->cfis[7] = port | ATA_D_LBA;
632 ctp->cfis[15] = ATA_A_4BIT;
634 if (ata_ahci_issue_cmd(dev, 0, 10)) {
635 device_printf(dev, "error reading PM port\n");
639 *result = fis[12] | (fis[4] << 8) | (fis[5] << 16) | (fis[6] << 24);
644 ata_ahci_pm_write(device_t dev, int port, int reg, u_int32_t value)
646 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
647 struct ata_channel *ch = device_get_softc(dev);
648 struct ata_ahci_cmd_tab *ctp =
649 (struct ata_ahci_cmd_tab *)(ch->dma.work + ATA_AHCI_CT_OFFSET);
650 int offset = ch->unit << 7;
652 bzero(ctp->cfis, 64);
653 ctp->cfis[0] = 0x27; /* host to device */
654 ctp->cfis[1] = 0x8f; /* command FIS to PM port */
655 ctp->cfis[2] = ATA_WRITE_PM;
657 ctp->cfis[7] = port | ATA_D_LBA;
658 ctp->cfis[12] = value & 0xff;
659 ctp->cfis[4] = (value >> 8) & 0xff;
660 ctp->cfis[5] = (value >> 16) & 0xff;
661 ctp->cfis[6] = (value >> 24) & 0xff;
662 ctp->cfis[15] = ATA_A_4BIT;
664 if (ata_ahci_issue_cmd(dev, 0, 100)) {
665 device_printf(dev, "error writing PM port\n");
669 return (ATA_INL(ctlr->r_res2, ATA_AHCI_P_TFD + offset) >> 8) & 0xff;
673 ata_ahci_stop(device_t dev)
675 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
676 struct ata_channel *ch = device_get_softc(dev);
678 int offset = ch->unit << 7;
681 /* kill off all activity on this channel */
682 cmd = ATA_INL(ctlr->r_res2, ATA_AHCI_P_CMD + offset);
683 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset,
684 cmd & ~ATA_AHCI_P_CMD_ST);
686 /* XXX SOS this is not entirely wrong */
690 if (timeout++ > 1000) {
691 device_printf(dev, "stopping AHCI engine failed\n");
695 while (ATA_INL(ctlr->r_res2, ATA_AHCI_P_CMD + offset) & ATA_AHCI_P_CMD_CR);
699 ata_ahci_clo(device_t dev)
701 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
702 struct ata_channel *ch = device_get_softc(dev);
704 int offset = ch->unit << 7;
707 /* issue Command List Override if supported */
708 if (ATA_INL(ctlr->r_res2, ATA_AHCI_CAP) & ATA_AHCI_CAP_SCLO) {
709 cmd = ATA_INL(ctlr->r_res2, ATA_AHCI_P_CMD + offset);
710 cmd |= ATA_AHCI_P_CMD_CLO;
711 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset, cmd);
715 if (timeout++ > 1000) {
716 device_printf(dev, "executing CLO failed\n");
720 while (ATA_INL(ctlr->r_res2, ATA_AHCI_P_CMD+offset)&ATA_AHCI_P_CMD_CLO);
725 ata_ahci_start(device_t dev)
727 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
728 struct ata_channel *ch = device_get_softc(dev);
730 int offset = ch->unit << 7;
732 /* clear SATA error register */
733 ATA_IDX_OUTL(ch, ATA_SERROR, ATA_IDX_INL(ch, ATA_SERROR));
735 /* clear any interrupts pending on this channel */
736 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_IS + offset,
737 ATA_INL(ctlr->r_res2, ATA_AHCI_P_IS + offset));
739 /* start operations on this channel */
740 cmd = ATA_INL(ctlr->r_res2, ATA_AHCI_P_CMD + offset);
741 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset,
742 cmd | ATA_AHCI_P_CMD_ST |
743 (ch->devices & ATA_PORTMULTIPLIER ? ATA_AHCI_P_CMD_PMA : 0));
747 ata_ahci_stop_fr(device_t dev)
749 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
750 struct ata_channel *ch = device_get_softc(dev);
752 int offset = ch->unit << 7;
755 /* kill off all activity on this channel */
756 cmd = ATA_INL(ctlr->r_res2, ATA_AHCI_P_CMD + offset);
757 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset, cmd & ~ATA_AHCI_P_CMD_FRE);
762 if (timeout++ > 1000) {
763 device_printf(dev, "stopping AHCI FR engine failed\n");
767 while (ATA_INL(ctlr->r_res2, ATA_AHCI_P_CMD + offset) & ATA_AHCI_P_CMD_FR);
771 ata_ahci_start_fr(device_t dev)
773 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
774 struct ata_channel *ch = device_get_softc(dev);
776 int offset = ch->unit << 7;
778 /* start FIS reception on this channel */
779 cmd = ATA_INL(ctlr->r_res2, ATA_AHCI_P_CMD + offset);
780 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset, cmd | ATA_AHCI_P_CMD_FRE);
784 ata_ahci_wait_ready(device_t dev, int t)
786 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
787 struct ata_channel *ch = device_get_softc(dev);
788 int offset = ch->unit << 7;
792 while ((val = ATA_INL(ctlr->r_res2, ATA_AHCI_P_TFD + offset)) &
793 (ATA_S_BUSY | ATA_S_DRQ)) {
796 device_printf(dev, "port is not ready (timeout %dms) tfd = %08x\n", t, val);
801 device_printf(dev, "ready wait time=%dms\n", timeout);
806 ata_ahci_hardreset(device_t dev, int port, uint32_t *signature)
808 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
809 struct ata_channel *ch = device_get_softc(dev);
810 int offset = ch->unit << 7;
812 *signature = 0xffffffff;
815 if (!ata_sata_phy_reset(dev, port, 0))
817 /* Wait for clearing busy status. */
818 if (ata_ahci_wait_ready(dev, 10000)) {
819 device_printf(dev, "hardware reset timeout\n");
822 *signature = ATA_INL(ctlr->r_res2, ATA_AHCI_P_SIG + offset);
828 ata_ahci_softreset(device_t dev, int port)
830 struct ata_channel *ch = device_get_softc(dev);
831 struct ata_ahci_cmd_tab *ctp =
832 (struct ata_ahci_cmd_tab *)(ch->dma.work + ATA_AHCI_CT_OFFSET);
833 u_int8_t *fis = ch->dma.work + ATA_AHCI_FB_OFFSET + 0x40;
836 device_printf(dev, "software reset port %d...\n", port);
838 /* kick controller into sane state */
843 /* pull reset active */
844 bzero(ctp->cfis, 64);
846 ctp->cfis[1] = port & 0x0f;
847 //ctp->cfis[7] = ATA_D_LBA | ATA_D_IBM;
848 ctp->cfis[15] = (ATA_A_4BIT | ATA_A_RESET);
850 if (ata_ahci_issue_cmd(dev, ATA_AHCI_CMD_RESET | ATA_AHCI_CMD_CLR_BUSY,100)) {
851 device_printf(dev, "software reset set timeout\n");
857 /* pull reset inactive -> device softreset */
858 bzero(ctp->cfis, 64);
860 ctp->cfis[1] = port & 0x0f;
861 //ctp->cfis[7] = ATA_D_LBA | ATA_D_IBM;
862 ctp->cfis[15] = ATA_A_4BIT;
863 ata_ahci_issue_cmd(dev, 0, 3000);
865 if (ata_ahci_wait_ready(dev, 0)) {
866 device_printf(dev, "software reset clear timeout\n");
870 return (((u_int32_t)fis[6] << 24) |
871 ((u_int32_t)fis[5] << 16) |
872 ((u_int32_t)fis[4] << 8) |
877 ata_ahci_reset(device_t dev)
879 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
880 struct ata_channel *ch = device_get_softc(dev);
882 int offset = ch->unit << 7;
885 device_printf(dev, "AHCI reset...\n");
887 /* Disable port interrupts */
888 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_IE + offset, 0);
890 if (ata_ahci_hardreset(dev, -1, &signature)) {
892 device_printf(dev, "AHCI reset done: phy reset found no device\n");
895 /* enable wanted port interrupts */
896 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_IE + offset,
897 (ATA_AHCI_P_IX_CPD | ATA_AHCI_P_IX_PRC | ATA_AHCI_P_IX_PC));
901 /* enable wanted port interrupts */
902 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_IE + offset,
903 (ATA_AHCI_P_IX_CPD | ATA_AHCI_P_IX_TFE | ATA_AHCI_P_IX_HBF |
904 ATA_AHCI_P_IX_HBD | ATA_AHCI_P_IX_IF | ATA_AHCI_P_IX_OF |
905 ((ch->pm_level == 0) ? ATA_AHCI_P_IX_PRC | ATA_AHCI_P_IX_PC : 0) |
906 ATA_AHCI_P_IX_DP | ATA_AHCI_P_IX_UF | ATA_AHCI_P_IX_SDB |
907 ATA_AHCI_P_IX_DS | ATA_AHCI_P_IX_PS | ATA_AHCI_P_IX_DHR));
909 * Only probe for PortMultiplier if HW has support.
910 * Ignore Marvell, which is not working,
912 if ((ATA_INL(ctlr->r_res2, ATA_AHCI_CAP) & ATA_AHCI_CAP_SPM) &&
913 pci_get_vendor(ctlr->dev) != 0x11ab) {
914 signature = ata_ahci_softreset(dev, ATA_PM);
915 /* Workaround for some ATI chips, failing to soft-reset
916 * when port multiplicator supported, but absent.
917 * XXX: We can also check PxIS.IPMS==1 here to be sure. */
918 if (signature == 0xffffffff)
919 signature = ata_ahci_softreset(dev, 0);
921 signature = ata_ahci_softreset(dev, 0);
924 device_printf(dev, "SIGNATURE: %08x\n", signature);
926 switch (signature >> 16) {
928 ch->devices = ATA_ATA_MASTER;
931 ch->devices = ATA_PORTMULTIPLIER;
932 ata_pm_identify(dev);
935 ch->devices = ATA_ATAPI_MASTER;
937 default: /* SOS XXX */
939 device_printf(dev, "Unknown signature, assuming disk device\n");
940 ch->devices = ATA_ATA_MASTER;
943 device_printf(dev, "AHCI reset done: devices=%08x\n", ch->devices);
947 ata_ahci_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
949 struct ata_dmasetprd_args *args = xsc;
950 struct ata_ahci_dma_prd *prd = args->dmatab;
953 if (!(args->error = error)) {
954 for (i = 0; i < nsegs; i++) {
955 prd[i].dba = htole64(segs[i].ds_addr);
956 prd[i].dbc = htole32((segs[i].ds_len - 1) & ATA_AHCI_PRD_MASK);
960 KASSERT(nsegs <= ATA_AHCI_DMA_ENTRIES, ("too many DMA segment entries\n"));
965 ata_ahci_dmainit(device_t dev)
967 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
968 struct ata_channel *ch = device_get_softc(dev);
971 /* note start and stop are not used here */
972 ch->dma.setprd = ata_ahci_dmasetprd;
973 ch->dma.max_iosize = (ATA_AHCI_DMA_ENTRIES - 1) * PAGE_SIZE;
974 if (ATA_INL(ctlr->r_res2, ATA_AHCI_CAP) & ATA_AHCI_CAP_64BIT)
975 ch->dma.max_address = BUS_SPACE_MAXADDR;
979 ata_ahci_setup_fis(struct ata_ahci_cmd_tab *ctp, struct ata_request *request)
981 bzero(ctp->cfis, 64);
982 if (request->flags & ATA_R_ATAPI) {
983 bzero(ctp->acmd, 32);
984 bcopy(request->u.atapi.ccb, ctp->acmd, 16);
986 return ata_request2fis_h2d(request, &ctp->cfis[0]);
989 ATA_DECLARE_DRIVER(ata_ahci);
990 static device_method_t ata_ahci_ata_methods[] = {
991 DEVMETHOD(device_probe, ata_ahci_ata_probe),
992 DEVMETHOD(device_attach, ata_ahci_ata_attach),
993 DEVMETHOD(device_detach, ata_pci_detach),
994 DEVMETHOD(device_suspend, ata_pci_suspend),
995 DEVMETHOD(device_resume, ata_pci_resume),
996 DEVMETHOD(device_shutdown, bus_generic_shutdown),
997 DEVMETHOD(bus_read_ivar, ata_pci_read_ivar),
998 DEVMETHOD(bus_write_ivar, ata_pci_write_ivar),
999 DEVMETHOD(bus_alloc_resource, ata_pci_alloc_resource),
1000 DEVMETHOD(bus_release_resource, ata_pci_release_resource),
1001 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
1002 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
1003 DEVMETHOD(bus_setup_intr, ata_pci_setup_intr),
1004 DEVMETHOD(bus_teardown_intr, ata_pci_teardown_intr),
1007 static driver_t ata_ahci_ata_driver = {
1009 ata_ahci_ata_methods,
1010 sizeof(struct ata_pci_controller)
1012 DRIVER_MODULE(ata_ahci_ata, atapci, ata_ahci_ata_driver, ata_pci_devclass, 0, 0);