2 * Copyright (C) Freescale Semiconductor, Inc. 2006. All rights reserved.
3 * Author: Jason Jin<Jason.jin@freescale.com>
4 * Zhang Wei<wei.zhang@freescale.com>
6 * See file CREDITS for list of people who contributed to this
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24 * with the reference on libata and ahci drvier in kernel
29 #ifdef CONFIG_SCSI_AHCI
33 #include <asm/processor.h>
34 #include <asm/errno.h>
39 #include <linux/ctype.h>
42 struct ahci_probe_ent *probe_ent = NULL;
43 hd_driveid_t *ataid[AHCI_MAX_PORTS];
45 #define writel_with_flush(a,b) do { writel(a,b); readl(b); } while (0)
48 static inline u32 ahci_port_base(u32 base, u32 port)
50 return base + 0x100 + (port * 0x80);
54 static void ahci_setup_port(struct ahci_ioports *port, unsigned long base,
55 unsigned int port_idx)
57 base = ahci_port_base(base, port_idx);
59 port->cmd_addr = base;
60 port->scr_addr = base + PORT_SCR;
64 #define msleep(a) udelay(a * 1000)
65 #define ssleep(a) msleep(a * 1000)
67 static int waiting_for_cmd_completed(volatile u8 *offset,
74 for (i = 0; ((status = readl(offset)) & sign) && i < timeout_msec; i++)
77 return (i < timeout_msec) ? 0 : -1;
81 static int ahci_host_init(struct ahci_probe_ent *probe_ent)
83 pci_dev_t pdev = probe_ent->dev;
84 volatile u8 *mmio = (volatile u8 *)probe_ent->mmio_base;
88 volatile u8 *port_mmio;
89 unsigned short vendor;
91 cap_save = readl(mmio + HOST_CAP);
92 cap_save &= ((1 << 28) | (1 << 17));
93 cap_save |= (1 << 27);
95 /* global controller reset */
96 tmp = readl(mmio + HOST_CTL);
97 if ((tmp & HOST_RESET) == 0)
98 writel_with_flush(tmp | HOST_RESET, mmio + HOST_CTL);
100 /* reset must complete within 1 second, or
101 * the hardware should be considered fried.
105 tmp = readl(mmio + HOST_CTL);
106 if (tmp & HOST_RESET) {
107 debug("controller reset failed (0x%x)\n", tmp);
111 writel_with_flush(HOST_AHCI_EN, mmio + HOST_CTL);
112 writel(cap_save, mmio + HOST_CAP);
113 writel_with_flush(0xf, mmio + HOST_PORTS_IMPL);
115 pci_read_config_word(pdev, PCI_VENDOR_ID, &vendor);
117 if (vendor == PCI_VENDOR_ID_INTEL) {
119 pci_read_config_word(pdev, 0x92, &tmp16);
121 pci_write_config_word(pdev, 0x92, tmp16);
124 probe_ent->cap = readl(mmio + HOST_CAP);
125 probe_ent->port_map = readl(mmio + HOST_PORTS_IMPL);
126 probe_ent->n_ports = (probe_ent->cap & 0x1f) + 1;
128 debug("cap 0x%x port_map 0x%x n_ports %d\n",
129 probe_ent->cap, probe_ent->port_map, probe_ent->n_ports);
131 for (i = 0; i < probe_ent->n_ports; i++) {
132 probe_ent->port[i].port_mmio = ahci_port_base((u32) mmio, i);
133 port_mmio = (u8 *) probe_ent->port[i].port_mmio;
134 ahci_setup_port(&probe_ent->port[i], (unsigned long)mmio, i);
136 /* make sure port is not active */
137 tmp = readl(port_mmio + PORT_CMD);
138 if (tmp & (PORT_CMD_LIST_ON | PORT_CMD_FIS_ON |
139 PORT_CMD_FIS_RX | PORT_CMD_START)) {
140 tmp &= ~(PORT_CMD_LIST_ON | PORT_CMD_FIS_ON |
141 PORT_CMD_FIS_RX | PORT_CMD_START);
142 writel_with_flush(tmp, port_mmio + PORT_CMD);
144 /* spec says 500 msecs for each bit, so
145 * this is slightly incorrect.
150 writel(PORT_CMD_SPIN_UP, port_mmio + PORT_CMD);
155 tmp = readl(port_mmio + PORT_SCR_STAT);
156 if ((tmp & 0xf) == 0x3)
161 tmp = readl(port_mmio + PORT_SCR_ERR);
162 debug("PORT_SCR_ERR 0x%x\n", tmp);
163 writel(tmp, port_mmio + PORT_SCR_ERR);
165 /* ack any pending irq events for this port */
166 tmp = readl(port_mmio + PORT_IRQ_STAT);
167 debug("PORT_IRQ_STAT 0x%x\n", tmp);
169 writel(tmp, port_mmio + PORT_IRQ_STAT);
171 writel(1 << i, mmio + HOST_IRQ_STAT);
173 /* set irq mask (enables interrupts) */
174 writel(DEF_PORT_IRQ, port_mmio + PORT_IRQ_MASK);
176 /*register linkup ports */
177 tmp = readl(port_mmio + PORT_SCR_STAT);
178 debug("Port %d status: 0x%x\n", i, tmp);
179 if ((tmp & 0xf) == 0x03)
180 probe_ent->link_port_map |= (0x01 << i);
183 tmp = readl(mmio + HOST_CTL);
184 debug("HOST_CTL 0x%x\n", tmp);
185 writel(tmp | HOST_IRQ_EN, mmio + HOST_CTL);
186 tmp = readl(mmio + HOST_CTL);
187 debug("HOST_CTL 0x%x\n", tmp);
189 pci_read_config_word(pdev, PCI_COMMAND, &tmp16);
190 tmp |= PCI_COMMAND_MASTER;
191 pci_write_config_word(pdev, PCI_COMMAND, tmp16);
197 static void ahci_print_info(struct ahci_probe_ent *probe_ent)
199 pci_dev_t pdev = probe_ent->dev;
200 volatile u8 *mmio = (volatile u8 *)probe_ent->mmio_base;
201 u32 vers, cap, impl, speed;
206 vers = readl(mmio + HOST_VERSION);
207 cap = probe_ent->cap;
208 impl = probe_ent->port_map;
210 speed = (cap >> 20) & 0xf;
218 pci_read_config_word(pdev, 0x0a, &cc);
221 else if (cc == 0x0106)
223 else if (cc == 0x0104)
228 printf("AHCI %02x%02x.%02x%02x "
229 "%u slots %u ports %s Gbps 0x%x impl %s mode\n",
234 ((cap >> 8) & 0x1f) + 1, (cap & 0x1f) + 1, speed_s, impl, scc_s);
239 cap & (1 << 31) ? "64bit " : "",
240 cap & (1 << 30) ? "ncq " : "",
241 cap & (1 << 28) ? "ilck " : "",
242 cap & (1 << 27) ? "stag " : "",
243 cap & (1 << 26) ? "pm " : "",
244 cap & (1 << 25) ? "led " : "",
245 cap & (1 << 24) ? "clo " : "",
246 cap & (1 << 19) ? "nz " : "",
247 cap & (1 << 18) ? "only " : "",
248 cap & (1 << 17) ? "pmp " : "",
249 cap & (1 << 15) ? "pio " : "",
250 cap & (1 << 14) ? "slum " : "",
251 cap & (1 << 13) ? "part " : "");
254 static int ahci_init_one(pci_dev_t pdev)
259 memset((void *)ataid, 0, sizeof(hd_driveid_t *) * AHCI_MAX_PORTS);
261 probe_ent = malloc(sizeof(probe_ent));
262 memset(probe_ent, 0, sizeof(probe_ent));
263 probe_ent->dev = pdev;
265 pci_read_config_dword(pdev, AHCI_PCI_BAR, &iobase);
268 probe_ent->host_flags = ATA_FLAG_SATA
273 probe_ent->pio_mask = 0x1f;
274 probe_ent->udma_mask = 0x7f; /*Fixme,assume to support UDMA6 */
276 probe_ent->mmio_base = iobase;
279 * JMicron-specific fixup:
280 * make sure we're in AHCI mode
282 pci_read_config_word(pdev, PCI_VENDOR_ID, &vendor);
283 if (vendor == 0x197b)
284 pci_write_config_byte(pdev, 0x41, 0xa1);
286 /* initialize adapter */
287 rc = ahci_host_init(probe_ent);
291 ahci_print_info(probe_ent);
300 #define MAX_DATA_BYTE_COUNT (4*1024*1024)
302 static int ahci_fill_sg(u8 port, unsigned char *buf, int buf_len)
304 struct ahci_ioports *pp = &(probe_ent->port[port]);
305 struct ahci_sg *ahci_sg = pp->cmd_tbl_sg;
309 sg_count = ((buf_len - 1) / MAX_DATA_BYTE_COUNT) + 1;
310 if (sg_count > AHCI_MAX_SG) {
311 printf("Error:Too much sg!\n");
315 for (i = 0; i < sg_count; i++) {
317 cpu_to_le32((u32) buf + i * MAX_DATA_BYTE_COUNT);
318 ahci_sg->addr_hi = 0;
319 ahci_sg->flags_size = cpu_to_le32(0x3fffff &
320 (buf_len < MAX_DATA_BYTE_COUNT
322 : (MAX_DATA_BYTE_COUNT - 1)));
324 buf_len -= MAX_DATA_BYTE_COUNT;
331 static void ahci_fill_cmd_slot(struct ahci_ioports *pp, u32 opts)
333 pp->cmd_slot->opts = cpu_to_le32(opts);
334 pp->cmd_slot->status = 0;
335 pp->cmd_slot->tbl_addr = cpu_to_le32(pp->cmd_tbl & 0xffffffff);
336 pp->cmd_slot->tbl_addr_hi = 0;
340 static void ahci_set_feature(u8 port)
342 struct ahci_ioports *pp = &(probe_ent->port[port]);
343 volatile u8 *port_mmio = (volatile u8 *)pp->port_mmio;
344 u32 cmd_fis_len = 5; /* five dwords */
351 fis[2] = ATA_CMD_SETF;
352 fis[3] = SETFEATURES_XFER;
353 fis[12] = __ilog2(probe_ent->udma_mask + 1) + 0x40 - 0x01;
355 memcpy((unsigned char *)pp->cmd_tbl, fis, 20);
356 ahci_fill_cmd_slot(pp, cmd_fis_len);
357 writel(1, port_mmio + PORT_CMD_ISSUE);
358 readl(port_mmio + PORT_CMD_ISSUE);
360 if (waiting_for_cmd_completed(port_mmio + PORT_CMD_ISSUE, 150, 0x1)) {
361 printf("set feature error!\n");
366 static int ahci_port_start(u8 port)
368 struct ahci_ioports *pp = &(probe_ent->port[port]);
369 volatile u8 *port_mmio = (volatile u8 *)pp->port_mmio;
373 debug("Enter start port: %d\n", port);
374 port_status = readl(port_mmio + PORT_SCR_STAT);
375 debug("Port %d status: %x\n", port, port_status);
376 if ((port_status & 0xf) != 0x03) {
377 printf("No Link on this port!\n");
381 mem = (u32) malloc(AHCI_PORT_PRIV_DMA_SZ + 2048);
384 printf("No mem for table!\n");
388 mem = (mem + 0x800) & (~0x7ff); /* Aligned to 2048-bytes */
389 memset((u8 *) mem, 0, AHCI_PORT_PRIV_DMA_SZ);
392 * First item in chunk of DMA memory: 32-slot command table,
393 * 32 bytes each in size
395 pp->cmd_slot = (struct ahci_cmd_hdr *)mem;
396 debug("cmd_slot = 0x%x\n", pp->cmd_slot);
397 mem += (AHCI_CMD_SLOT_SZ + 224);
400 * Second item: Received-FIS area
403 mem += AHCI_RX_FIS_SZ;
406 * Third item: data area for storing a single command
407 * and its scatter-gather table
410 debug("cmd_tbl_dma = 0x%x\n", pp->cmd_tbl);
412 mem += AHCI_CMD_TBL_HDR;
413 pp->cmd_tbl_sg = (struct ahci_sg *)mem;
415 writel_with_flush((u32) pp->cmd_slot, port_mmio + PORT_LST_ADDR);
417 writel_with_flush(pp->rx_fis, port_mmio + PORT_FIS_ADDR);
419 writel_with_flush(PORT_CMD_ICC_ACTIVE | PORT_CMD_FIS_RX |
420 PORT_CMD_POWER_ON | PORT_CMD_SPIN_UP |
421 PORT_CMD_START, port_mmio + PORT_CMD);
423 debug("Exit start port %d\n", port);
429 static int get_ahci_device_data(u8 port, u8 *fis, int fis_len, u8 *buf,
433 struct ahci_ioports *pp = &(probe_ent->port[port]);
434 volatile u8 *port_mmio = (volatile u8 *)pp->port_mmio;
439 debug("Enter get_ahci_device_data: for port %d\n", port);
441 if (port > probe_ent->n_ports) {
442 printf("Invaild port number %d\n", port);
446 port_status = readl(port_mmio + PORT_SCR_STAT);
447 if ((port_status & 0xf) != 0x03) {
448 debug("No Link on port %d!\n", port);
452 memcpy((unsigned char *)pp->cmd_tbl, fis, fis_len);
454 sg_count = ahci_fill_sg(port, buf, buf_len);
455 opts = (fis_len >> 2) | (sg_count << 16);
456 ahci_fill_cmd_slot(pp, opts);
458 writel_with_flush(1, port_mmio + PORT_CMD_ISSUE);
460 if (waiting_for_cmd_completed(port_mmio + PORT_CMD_ISSUE, 150, 0x1)) {
461 printf("timeout exit!\n");
464 debug("get_ahci_device_data: %d byte transferred.\n",
465 pp->cmd_slot->status);
471 static char *ata_id_strcpy(u16 *target, u16 *src, int len)
474 for (i = 0; i < len / 2; i++)
475 target[i] = le16_to_cpu(src[i]);
476 return (char *)target;
480 static void dump_ataid(hd_driveid_t *ataid)
482 debug("(49)ataid->capability = 0x%x\n", ataid->capability);
483 debug("(53)ataid->field_valid =0x%x\n", ataid->field_valid);
484 debug("(63)ataid->dma_mword = 0x%x\n", ataid->dma_mword);
485 debug("(64)ataid->eide_pio_modes = 0x%x\n", ataid->eide_pio_modes);
486 debug("(75)ataid->queue_depth = 0x%x\n", ataid->queue_depth);
487 debug("(80)ataid->major_rev_num = 0x%x\n", ataid->major_rev_num);
488 debug("(81)ataid->minor_rev_num = 0x%x\n", ataid->minor_rev_num);
489 debug("(82)ataid->command_set_1 = 0x%x\n", ataid->command_set_1);
490 debug("(83)ataid->command_set_2 = 0x%x\n", ataid->command_set_2);
491 debug("(84)ataid->cfsse = 0x%x\n", ataid->cfsse);
492 debug("(85)ataid->cfs_enable_1 = 0x%x\n", ataid->cfs_enable_1);
493 debug("(86)ataid->cfs_enable_2 = 0x%x\n", ataid->cfs_enable_2);
494 debug("(87)ataid->csf_default = 0x%x\n", ataid->csf_default);
495 debug("(88)ataid->dma_ultra = 0x%x\n", ataid->dma_ultra);
496 debug("(93)ataid->hw_config = 0x%x\n", ataid->hw_config);
501 * SCSI INQUIRY command operation.
503 static int ata_scsiop_inquiry(ccb *pccb)
508 0x5, /* claim SPC-3 version compatibility */
516 /* Clean ccb data buffer */
517 memset(pccb->pdata, 0, pccb->datalen);
519 memcpy(pccb->pdata, hdr, sizeof(hdr));
521 if (pccb->datalen <= 35)
525 /* Construct the FIS */
526 fis[0] = 0x27; /* Host to device FIS. */
527 fis[1] = 1 << 7; /* Command FIS. */
528 fis[2] = ATA_CMD_IDENT; /* Command byte. */
530 /* Read id from sata */
532 if (!(tmpid = malloc(sizeof(hd_driveid_t))))
535 if (get_ahci_device_data(port, (u8 *) & fis, 20,
536 tmpid, sizeof(hd_driveid_t))) {
537 debug("scsi_ahci: SCSI inquiry command failure.\n");
543 ataid[port] = (hd_driveid_t *) tmpid;
545 memcpy(&pccb->pdata[8], "ATA ", 8);
546 ata_id_strcpy((u16 *) &pccb->pdata[16], (u16 *)ataid[port]->model, 16);
547 ata_id_strcpy((u16 *) &pccb->pdata[32], (u16 *)ataid[port]->fw_rev, 4);
549 dump_ataid(ataid[port]);
555 * SCSI READ10 command operation.
557 static int ata_scsiop_read10(ccb * pccb)
563 lba = (((u64) pccb->cmd[2]) << 24) | (((u64) pccb->cmd[3]) << 16)
564 | (((u64) pccb->cmd[4]) << 8) | ((u64) pccb->cmd[5]);
565 len = (((u32) pccb->cmd[7]) << 8) | ((u32) pccb->cmd[8]);
567 /* For 10-byte and 16-byte SCSI R/W commands, transfer
568 * length 0 means transfer 0 block of data.
569 * However, for ATA R/W commands, sector count 0 means
570 * 256 or 65536 sectors, not 0 sectors as in SCSI.
572 * WARNING: one or two older ATA drives treat 0 as 0...
578 /* Construct the FIS */
579 fis[0] = 0x27; /* Host to device FIS. */
580 fis[1] = 1 << 7; /* Command FIS. */
581 fis[2] = ATA_CMD_RD_DMA; /* Command byte. */
583 /* LBA address, only support LBA28 in this driver */
584 fis[4] = pccb->cmd[5];
585 fis[5] = pccb->cmd[4];
586 fis[6] = pccb->cmd[3];
587 fis[7] = (pccb->cmd[2] & 0x0f) | 0xe0;
590 fis[12] = pccb->cmd[8];
591 fis[13] = pccb->cmd[7];
594 if (get_ahci_device_data(pccb->target, (u8 *) & fis, 20,
595 pccb->pdata, pccb->datalen)) {
596 debug("scsi_ahci: SCSI READ10 command failure.\n");
605 * SCSI READ CAPACITY10 command operation.
607 static int ata_scsiop_read_capacity10(ccb *pccb)
611 if (!ataid[pccb->target]) {
612 printf("scsi_ahci: SCSI READ CAPACITY10 command failure. "
614 "\tPlease run SCSI commmand INQUIRY firstly!\n");
620 *(u32 *) buf = le32_to_cpu(ataid[pccb->target]->lba_capacity);
625 memcpy(pccb->pdata, buf, 8);
632 * SCSI TEST UNIT READY command operation.
634 static int ata_scsiop_test_unit_ready(ccb *pccb)
636 return (ataid[pccb->target]) ? 0 : -EPERM;
640 int scsi_exec(ccb *pccb)
644 switch (pccb->cmd[0]) {
646 ret = ata_scsiop_read10(pccb);
649 ret = ata_scsiop_read_capacity10(pccb);
652 ret = ata_scsiop_test_unit_ready(pccb);
655 ret = ata_scsiop_inquiry(pccb);
658 printf("Unsupport SCSI command 0x%02x\n", pccb->cmd[0]);
663 debug("SCSI command 0x%02x ret errno %d\n", pccb->cmd[0], ret);
671 void scsi_low_level_init(int busdevfunc)
676 ahci_init_one(busdevfunc);
678 linkmap = probe_ent->link_port_map;
680 for (i = 0; i < CFG_SCSI_MAX_SCSI_ID; i++) {
681 if (((linkmap >> i) & 0x01)) {
682 if (ahci_port_start((u8) i)) {
683 printf("Can not start port %d\n", i);
686 ahci_set_feature((u8) i);
692 void scsi_bus_reset(void)
698 void scsi_print_error(ccb * pccb)
700 /*The ahci error info can be read in the ahci driver*/