2 * Copyright (C) 2011 Freescale Semiconductor, Inc.
3 * Author: Tang Yuantian <b29983@freescale.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of
8 * the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24 #include <asm/byteorder.h>
31 /* Convert sectorsize to wordsize */
32 #define ATA_SECTOR_WORDS (ATA_SECT_SIZE/2)
33 #define virt_to_bus(devno, v) pci_virt_to_mem(devno, (void *) (v))
35 static struct sata_info sata_info;
37 static struct pci_device_id supported[] = {
38 {PCI_VENDOR_ID_SILICONIMAGE, PCI_DEVICE_ID_SIL3131},
39 {PCI_VENDOR_ID_SILICONIMAGE, PCI_DEVICE_ID_SIL3132},
40 {PCI_VENDOR_ID_SILICONIMAGE, PCI_DEVICE_ID_SIL3124},
44 static void sil_sata_dump_fis(struct sata_fis_d2h *s)
46 printf("Status FIS dump:\n");
47 printf("fis_type: %02x\n", s->fis_type);
48 printf("pm_port_i: %02x\n", s->pm_port_i);
49 printf("status: %02x\n", s->status);
50 printf("error: %02x\n", s->error);
51 printf("lba_low: %02x\n", s->lba_low);
52 printf("lba_mid: %02x\n", s->lba_mid);
53 printf("lba_high: %02x\n", s->lba_high);
54 printf("device: %02x\n", s->device);
55 printf("lba_low_exp: %02x\n", s->lba_low_exp);
56 printf("lba_mid_exp: %02x\n", s->lba_mid_exp);
57 printf("lba_high_exp: %02x\n", s->lba_high_exp);
58 printf("res1: %02x\n", s->res1);
59 printf("sector_count: %02x\n", s->sector_count);
60 printf("sector_count_exp: %02x\n", s->sector_count_exp);
63 static const char *sata_spd_string(unsigned int speed)
65 static const char * const spd_str[] = {
74 return spd_str[speed - 1];
77 static u32 ata_wait_register(void *reg, u32 mask,
78 u32 val, int timeout_msec)
83 while ((tmp & mask) == val && timeout_msec > 0) {
92 static void sil_config_port(void *port)
94 /* configure IRQ WoC */
95 writel(PORT_CS_IRQ_WOC, port + PORT_CTRL_CLR);
97 /* zero error counters. */
98 writew(0x8000, port + PORT_DECODE_ERR_THRESH);
99 writew(0x8000, port + PORT_CRC_ERR_THRESH);
100 writew(0x8000, port + PORT_HSHK_ERR_THRESH);
101 writew(0x0000, port + PORT_DECODE_ERR_CNT);
102 writew(0x0000, port + PORT_CRC_ERR_CNT);
103 writew(0x0000, port + PORT_HSHK_ERR_CNT);
105 /* always use 64bit activation */
106 writel(PORT_CS_32BIT_ACTV, port + PORT_CTRL_CLR);
108 /* clear port multiplier enable and resume bits */
109 writel(PORT_CS_PMP_EN | PORT_CS_PMP_RESUME, port + PORT_CTRL_CLR);
112 static int sil_init_port(void *port)
116 writel(PORT_CS_INIT, port + PORT_CTRL_STAT);
117 ata_wait_register(port + PORT_CTRL_STAT,
118 PORT_CS_INIT, PORT_CS_INIT, 100);
119 tmp = ata_wait_register(port + PORT_CTRL_STAT,
120 PORT_CS_RDY, 0, 100);
122 if ((tmp & (PORT_CS_INIT | PORT_CS_RDY)) != PORT_CS_RDY)
128 static void sil_read_fis(int dev, int tag, struct sata_fis_d2h *fis)
130 struct sil_sata *sata = sata_dev_desc[dev].priv;
131 void *port = sata->port;
136 prb = port + PORT_LRAM + tag * PORT_LRAM_SLOT_SZ;
137 src = (u32 *)&prb->fis;
139 for (i = 0; i < sizeof(struct sata_fis_h2d); i += 4)
140 *dst++ = readl(src++);
143 static int sil_exec_cmd(int dev, struct sil_cmd_block *pcmd, int tag)
145 struct sil_sata *sata = sata_dev_desc[dev].priv;
146 void *port = sata->port;
147 u64 paddr = virt_to_bus(sata->devno, pcmd);
148 u32 irq_mask, irq_stat;
151 writel(PORT_IRQ_COMPLETE | PORT_IRQ_ERROR, port + PORT_IRQ_ENABLE_CLR);
153 /* better to add momery barrior here */
154 writel((u32)paddr, port + PORT_CMD_ACTIVATE + tag * 8);
155 writel((u64)paddr >> 32, port + PORT_CMD_ACTIVATE + tag * 8 + 4);
157 irq_mask = (PORT_IRQ_COMPLETE | PORT_IRQ_ERROR) << PORT_IRQ_RAW_SHIFT;
158 irq_stat = ata_wait_register(port + PORT_IRQ_STAT, irq_mask,
162 writel(irq_mask, port + PORT_IRQ_STAT);
163 irq_stat >>= PORT_IRQ_RAW_SHIFT;
165 if (irq_stat & PORT_IRQ_COMPLETE)
168 /* force port into known state */
170 if (irq_stat & PORT_IRQ_ERROR)
179 static int sil_cmd_set_feature(int dev)
181 struct sil_sata *sata = sata_dev_desc[dev].priv;
182 struct sil_cmd_block cmdb, *pcmd = &cmdb;
183 struct sata_fis_d2h fis;
187 memset((void *)&cmdb, 0, sizeof(struct sil_cmd_block));
188 pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
189 pcmd->prb.fis.pm_port_c = (1 << 7);
190 pcmd->prb.fis.command = ATA_CMD_SET_FEATURES;
191 pcmd->prb.fis.features = SETFEATURES_XFER;
193 /* First check the device capablity */
194 udma_cap = (u8)(sata->udma & 0xff);
195 debug("udma_cap %02x\n", udma_cap);
197 if (udma_cap == ATA_UDMA6)
198 pcmd->prb.fis.sector_count = XFER_UDMA_6;
199 if (udma_cap == ATA_UDMA5)
200 pcmd->prb.fis.sector_count = XFER_UDMA_5;
201 if (udma_cap == ATA_UDMA4)
202 pcmd->prb.fis.sector_count = XFER_UDMA_4;
203 if (udma_cap == ATA_UDMA3)
204 pcmd->prb.fis.sector_count = XFER_UDMA_3;
206 ret = sil_exec_cmd(dev, pcmd, 0);
208 sil_read_fis(dev, 0, &fis);
209 printf("Err: exe cmd(0x%x).\n",
210 readl(sata->port + PORT_SERROR));
211 sil_sata_dump_fis(&fis);
218 static int sil_cmd_identify_device(int dev, u16 *id)
220 struct sil_sata *sata = sata_dev_desc[dev].priv;
221 struct sil_cmd_block cmdb, *pcmd = &cmdb;
222 struct sata_fis_d2h fis;
225 memset((void *)&cmdb, 0, sizeof(struct sil_cmd_block));
226 pcmd->prb.ctrl = cpu_to_le16(PRB_CTRL_PROTOCOL);
227 pcmd->prb.prot = cpu_to_le16(PRB_PROT_READ);
228 pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
229 pcmd->prb.fis.pm_port_c = (1 << 7);
230 pcmd->prb.fis.command = ATA_CMD_ID_ATA;
231 pcmd->sge.addr = cpu_to_le64(virt_to_bus(sata->devno, id));
232 pcmd->sge.cnt = cpu_to_le32(sizeof(id[0]) * ATA_ID_WORDS);
233 pcmd->sge.flags = cpu_to_le32(SGE_TRM);
235 ret = sil_exec_cmd(dev, pcmd, 0);
237 sil_read_fis(dev, 0, &fis);
238 printf("Err: id cmd(0x%x).\n", readl(sata->port + PORT_SERROR));
239 sil_sata_dump_fis(&fis);
242 ata_swap_buf_le16(id, ATA_ID_WORDS);
247 static int sil_cmd_soft_reset(int dev)
249 struct sil_cmd_block cmdb, *pcmd = &cmdb;
250 struct sil_sata *sata = sata_dev_desc[dev].priv;
251 struct sata_fis_d2h fis;
252 void *port = sata->port;
255 /* put the port into known state */
256 if (sil_init_port(port)) {
257 printf("SRST: port %d not ready\n", dev);
261 memset((void *)&cmdb, 0, sizeof(struct sil_cmd_block));
263 pcmd->prb.ctrl = cpu_to_le16(PRB_CTRL_SRST);
264 pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
265 pcmd->prb.fis.pm_port_c = 0xf;
267 ret = sil_exec_cmd(dev, &cmdb, 0);
269 sil_read_fis(dev, 0, &fis);
270 printf("SRST cmd error.\n");
271 sil_sata_dump_fis(&fis);
278 static ulong sil_sata_rw_cmd(int dev, ulong start, ulong blkcnt,
279 u8 *buffer, int is_write)
281 struct sil_sata *sata = sata_dev_desc[dev].priv;
282 struct sil_cmd_block cmdb, *pcmd = &cmdb;
283 struct sata_fis_d2h fis;
288 memset(pcmd, 0, sizeof(struct sil_cmd_block));
289 pcmd->prb.ctrl = cpu_to_le16(PRB_CTRL_PROTOCOL);
290 pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
291 pcmd->prb.fis.pm_port_c = (1 << 7);
293 pcmd->prb.fis.command = ATA_CMD_WRITE;
294 pcmd->prb.prot = cpu_to_le16(PRB_PROT_WRITE);
296 pcmd->prb.fis.command = ATA_CMD_READ;
297 pcmd->prb.prot = cpu_to_le16(PRB_PROT_READ);
300 pcmd->prb.fis.device = ATA_LBA;
301 pcmd->prb.fis.device |= (block >> 24) & 0xf;
302 pcmd->prb.fis.lba_high = (block >> 16) & 0xff;
303 pcmd->prb.fis.lba_mid = (block >> 8) & 0xff;
304 pcmd->prb.fis.lba_low = block & 0xff;
305 pcmd->prb.fis.sector_count = (u8)blkcnt & 0xff;
307 pcmd->sge.addr = cpu_to_le64(virt_to_bus(sata->devno, buffer));
308 pcmd->sge.cnt = cpu_to_le32(blkcnt * ATA_SECT_SIZE);
309 pcmd->sge.flags = cpu_to_le32(SGE_TRM);
311 ret = sil_exec_cmd(dev, pcmd, 0);
313 sil_read_fis(dev, 0, &fis);
314 printf("Err: rw cmd(0x%08x).\n",
315 readl(sata->port + PORT_SERROR));
316 sil_sata_dump_fis(&fis);
323 static ulong sil_sata_rw_cmd_ext(int dev, ulong start, ulong blkcnt,
324 u8 *buffer, int is_write)
326 struct sil_sata *sata = sata_dev_desc[dev].priv;
327 struct sil_cmd_block cmdb, *pcmd = &cmdb;
328 struct sata_fis_d2h fis;
333 memset(pcmd, 0, sizeof(struct sil_cmd_block));
334 pcmd->prb.ctrl = cpu_to_le16(PRB_CTRL_PROTOCOL);
335 pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
336 pcmd->prb.fis.pm_port_c = (1 << 7);
338 pcmd->prb.fis.command = ATA_CMD_WRITE_EXT;
339 pcmd->prb.prot = cpu_to_le16(PRB_PROT_WRITE);
341 pcmd->prb.fis.command = ATA_CMD_READ_EXT;
342 pcmd->prb.prot = cpu_to_le16(PRB_PROT_READ);
345 pcmd->prb.fis.lba_high_exp = (block >> 40) & 0xff;
346 pcmd->prb.fis.lba_mid_exp = (block >> 32) & 0xff;
347 pcmd->prb.fis.lba_low_exp = (block >> 24) & 0xff;
348 pcmd->prb.fis.lba_high = (block >> 16) & 0xff;
349 pcmd->prb.fis.lba_mid = (block >> 8) & 0xff;
350 pcmd->prb.fis.lba_low = block & 0xff;
351 pcmd->prb.fis.device = ATA_LBA;
352 pcmd->prb.fis.sector_count_exp = (blkcnt >> 8) & 0xff;
353 pcmd->prb.fis.sector_count = blkcnt & 0xff;
355 pcmd->sge.addr = cpu_to_le64(virt_to_bus(sata->devno, buffer));
356 pcmd->sge.cnt = cpu_to_le32(blkcnt * ATA_SECT_SIZE);
357 pcmd->sge.flags = cpu_to_le32(SGE_TRM);
359 ret = sil_exec_cmd(dev, pcmd, 0);
361 sil_read_fis(dev, 0, &fis);
362 printf("Err: rw ext cmd(0x%08x).\n",
363 readl(sata->port + PORT_SERROR));
364 sil_sata_dump_fis(&fis);
371 ulong sil_sata_rw_lba28(int dev, ulong blknr, lbaint_t blkcnt,
372 void *buffer, int is_write)
374 ulong start, blks, max_blks;
381 max_blks = ATA_MAX_SECTORS;
383 if (blks > max_blks) {
384 sil_sata_rw_cmd(dev, start, max_blks, addr, is_write);
387 addr += ATA_SECT_SIZE * max_blks;
389 sil_sata_rw_cmd(dev, start, blks, addr, is_write);
392 addr += ATA_SECT_SIZE * blks;
399 ulong sil_sata_rw_lba48(int dev, ulong blknr, lbaint_t blkcnt,
400 void *buffer, int is_write)
402 ulong start, blks, max_blks;
409 max_blks = ATA_MAX_SECTORS_LBA48;
411 if (blks > max_blks) {
412 sil_sata_rw_cmd_ext(dev, start, max_blks,
416 addr += ATA_SECT_SIZE * max_blks;
418 sil_sata_rw_cmd_ext(dev, start, blks,
422 addr += ATA_SECT_SIZE * blks;
429 void sil_sata_cmd_flush_cache(int dev)
431 struct sil_cmd_block cmdb, *pcmd = &cmdb;
433 memset((void *)pcmd, 0, sizeof(struct sil_cmd_block));
434 pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
435 pcmd->prb.fis.pm_port_c = (1 << 7);
436 pcmd->prb.fis.command = ATA_CMD_FLUSH;
438 sil_exec_cmd(dev, pcmd, 0);
441 void sil_sata_cmd_flush_cache_ext(int dev)
443 struct sil_cmd_block cmdb, *pcmd = &cmdb;
445 memset((void *)pcmd, 0, sizeof(struct sil_cmd_block));
446 pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
447 pcmd->prb.fis.pm_port_c = (1 << 7);
448 pcmd->prb.fis.command = ATA_CMD_FLUSH_EXT;
450 sil_exec_cmd(dev, pcmd, 0);
453 static void sil_sata_init_wcache(int dev, u16 *id)
455 struct sil_sata *sata = sata_dev_desc[dev].priv;
457 if (ata_id_has_wcache(id) && ata_id_wcache_enabled(id))
459 if (ata_id_has_flush(id))
461 if (ata_id_has_flush_ext(id))
465 static int sil_sata_get_wcache(int dev)
467 struct sil_sata *sata = sata_dev_desc[dev].priv;
472 static int sil_sata_get_flush(int dev)
474 struct sil_sata *sata = sata_dev_desc[dev].priv;
479 static int sil_sata_get_flush_ext(int dev)
481 struct sil_sata *sata = sata_dev_desc[dev].priv;
483 return sata->flush_ext;
487 * SATA interface between low level driver and command layer
489 ulong sata_read(int dev, ulong blknr, lbaint_t blkcnt, void *buffer)
491 struct sil_sata *sata = sata_dev_desc[dev].priv;
495 rc = sil_sata_rw_lba48(dev, blknr, blkcnt, buffer, READ_CMD);
497 rc = sil_sata_rw_lba28(dev, blknr, blkcnt, buffer, READ_CMD);
503 * SATA interface between low level driver and command layer
505 ulong sata_write(int dev, ulong blknr, lbaint_t blkcnt, void *buffer)
507 struct sil_sata *sata = sata_dev_desc[dev].priv;
511 rc = sil_sata_rw_lba48(dev, blknr, blkcnt, buffer, WRITE_CMD);
512 if (sil_sata_get_wcache(dev) && sil_sata_get_flush_ext(dev))
513 sil_sata_cmd_flush_cache_ext(dev);
515 rc = sil_sata_rw_lba28(dev, blknr, blkcnt, buffer, WRITE_CMD);
516 if (sil_sata_get_wcache(dev) && sil_sata_get_flush(dev))
517 sil_sata_cmd_flush_cache(dev);
524 * SATA interface between low level driver and command layer
526 int init_sata(int dev)
528 static int init_done, idx;
532 if (init_done == 1 && dev < sata_info.maxport)
537 /* Find PCI device(s) */
538 devno = pci_find_devices(supported, idx++);
542 pci_read_config_word(devno, PCI_DEVICE_ID, &word);
544 /* get the port count */
547 sata_info.portbase = sata_info.maxport;
548 sata_info.maxport = sata_info.portbase + word;
549 sata_info.devno = devno;
551 /* Read out all BARs */
552 sata_info.iobase[0] = (ulong)pci_map_bar(devno,
553 PCI_BASE_ADDRESS_0, PCI_REGION_MEM);
554 sata_info.iobase[1] = (ulong)pci_map_bar(devno,
555 PCI_BASE_ADDRESS_2, PCI_REGION_MEM);
556 sata_info.iobase[2] = (ulong)pci_map_bar(devno,
557 PCI_BASE_ADDRESS_4, PCI_REGION_MEM);
559 /* mask out the unused bits */
560 sata_info.iobase[0] &= 0xffffff80;
561 sata_info.iobase[1] &= 0xfffffc00;
562 sata_info.iobase[2] &= 0xffffff80;
564 /* Enable Bus Mastering and memory region */
565 pci_write_config_word(devno, PCI_COMMAND,
566 PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
568 /* Check if mem accesses and Bus Mastering are enabled. */
569 pci_read_config_word(devno, PCI_COMMAND, &word);
570 if (!(word & PCI_COMMAND_MEMORY) ||
571 (!(word & PCI_COMMAND_MASTER))) {
572 printf("Error: Can not enable MEM access or Bus Mastering.\n");
573 debug("PCI command: %04x\n", word);
578 writel(0, (void *)(sata_info.iobase[0] + HOST_FLASH_CMD));
579 /* clear global reset & mask interrupts during initialization */
580 writel(0, (void *)(sata_info.iobase[0] + HOST_CTRL));
586 * SATA interface between low level driver and command layer
588 int scan_sata(int dev)
590 unsigned char serial[ATA_ID_SERNO_LEN + 1];
591 unsigned char firmware[ATA_ID_FW_REV_LEN + 1];
592 unsigned char product[ATA_ID_PROD_LEN + 1];
593 struct sil_sata *sata;
599 if (dev >= sata_info.maxport) {
600 printf("SATA#%d is not present\n", dev);
604 printf("SATA#%d\n", dev);
605 port = (void *)sata_info.iobase[1] +
606 PORT_REGS_SIZE * (dev - sata_info.portbase);
608 /* Initial PHY setting */
609 writel(0x20c, port + PORT_PHY_CFG);
612 tmp = readl(port + PORT_CTRL_STAT);
613 if (tmp & PORT_CS_PORT_RST) {
614 writel(PORT_CS_PORT_RST, port + PORT_CTRL_CLR);
615 tmp = ata_wait_register(port + PORT_CTRL_STAT,
616 PORT_CS_PORT_RST, PORT_CS_PORT_RST, 100);
617 if (tmp & PORT_CS_PORT_RST)
618 printf("Err: Failed to clear port RST\n");
621 /* Check if device is present */
622 for (cnt = 0; cnt < 100; cnt++) {
623 tmp = readl(port + PORT_SSTATUS);
624 if ((tmp & 0xF) == 0x3)
629 tmp = readl(port + PORT_SSTATUS);
630 if ((tmp & 0xf) != 0x3) {
631 printf(" (No RDY)\n");
635 /* Wait for port ready */
636 tmp = ata_wait_register(port + PORT_CTRL_STAT,
637 PORT_CS_RDY, PORT_CS_RDY, 100);
638 if ((tmp & PORT_CS_RDY) != PORT_CS_RDY) {
639 printf("%d port not ready.\n", dev);
644 sil_config_port(port);
647 writel(PORT_CS_DEV_RST, port + PORT_CTRL_STAT);
648 readl(port + PORT_CTRL_STAT);
649 tmp = ata_wait_register(port + PORT_CTRL_STAT, PORT_CS_DEV_RST,
650 PORT_CS_DEV_RST, 100);
651 if (tmp & PORT_CS_DEV_RST) {
652 printf("%d port reset failed.\n", dev);
656 sata = (struct sil_sata *)malloc(sizeof(struct sil_sata));
658 printf("%d no memory.\n", dev);
661 memset((void *)sata, 0, sizeof(struct sil_sata));
663 /* turn on port interrupt */
664 tmp = readl((void *)(sata_info.iobase[0] + HOST_CTRL));
665 tmp |= (1 << (dev - sata_info.portbase));
666 writel(tmp, (void *)(sata_info.iobase[0] + HOST_CTRL));
668 /* Save the private struct to block device struct */
669 sata_dev_desc[dev].priv = (void *)sata;
671 sata->devno = sata_info.devno;
672 sprintf(sata->name, "SATA#%d", dev);
673 sil_cmd_soft_reset(dev);
674 tmp = readl(port + PORT_SSTATUS);
675 tmp = (tmp >> 4) & 0xf;
676 printf(" (%s)\n", sata_spd_string(tmp));
678 id = (u16 *)malloc(ATA_ID_WORDS * 2);
680 printf("Id malloc failed\n");
684 sil_cmd_identify_device(dev, id);
687 /* Check if support LBA48 */
688 if (ata_id_has_lba48(id)) {
689 sata_dev_desc[dev].lba48 = 1;
691 debug("Device supports LBA48\n");
693 debug("Device supports LBA28\n");
697 ata_id_c_string(id, serial, ATA_ID_SERNO, sizeof(serial));
698 memcpy(sata_dev_desc[dev].product, serial, sizeof(serial));
700 /* Firmware version */
701 ata_id_c_string(id, firmware, ATA_ID_FW_REV, sizeof(firmware));
702 memcpy(sata_dev_desc[dev].revision, firmware, sizeof(firmware));
705 ata_id_c_string(id, product, ATA_ID_PROD, sizeof(product));
706 memcpy(sata_dev_desc[dev].vendor, product, sizeof(product));
709 sata_dev_desc[dev].lba = ata_id_n_sectors(id);
711 sil_sata_init_wcache(dev, id);
712 sil_cmd_set_feature(dev);
715 sil_cmd_identify_device(dev, id);