2 * Copyright (C) 2011 Freescale Semiconductor, Inc.
3 * Author: Tang Yuantian <b29983@freescale.com>
5 * SPDX-License-Identifier: GPL-2.0+
11 #include <asm/byteorder.h>
20 /* Convert sectorsize to wordsize */
21 #define ATA_SECTOR_WORDS (ATA_SECT_SIZE/2)
22 #define virt_to_bus(devno, v) pci_virt_to_mem(devno, (void *) (v))
24 static struct sata_info sata_info;
26 static struct pci_device_id supported[] = {
27 {PCI_VENDOR_ID_SILICONIMAGE, PCI_DEVICE_ID_SIL3131},
28 {PCI_VENDOR_ID_SILICONIMAGE, PCI_DEVICE_ID_SIL3132},
29 {PCI_VENDOR_ID_SILICONIMAGE, PCI_DEVICE_ID_SIL3124},
33 static void sil_sata_dump_fis(struct sata_fis_d2h *s)
35 printf("Status FIS dump:\n");
36 printf("fis_type: %02x\n", s->fis_type);
37 printf("pm_port_i: %02x\n", s->pm_port_i);
38 printf("status: %02x\n", s->status);
39 printf("error: %02x\n", s->error);
40 printf("lba_low: %02x\n", s->lba_low);
41 printf("lba_mid: %02x\n", s->lba_mid);
42 printf("lba_high: %02x\n", s->lba_high);
43 printf("device: %02x\n", s->device);
44 printf("lba_low_exp: %02x\n", s->lba_low_exp);
45 printf("lba_mid_exp: %02x\n", s->lba_mid_exp);
46 printf("lba_high_exp: %02x\n", s->lba_high_exp);
47 printf("res1: %02x\n", s->res1);
48 printf("sector_count: %02x\n", s->sector_count);
49 printf("sector_count_exp: %02x\n", s->sector_count_exp);
52 static const char *sata_spd_string(unsigned int speed)
54 static const char * const spd_str[] = {
63 return spd_str[speed - 1];
66 static u32 ata_wait_register(void *reg, u32 mask,
67 u32 val, int timeout_msec)
72 while ((tmp & mask) == val && timeout_msec > 0) {
81 static void sil_config_port(void *port)
83 /* configure IRQ WoC */
84 writel(PORT_CS_IRQ_WOC, port + PORT_CTRL_CLR);
86 /* zero error counters. */
87 writew(0x8000, port + PORT_DECODE_ERR_THRESH);
88 writew(0x8000, port + PORT_CRC_ERR_THRESH);
89 writew(0x8000, port + PORT_HSHK_ERR_THRESH);
90 writew(0x0000, port + PORT_DECODE_ERR_CNT);
91 writew(0x0000, port + PORT_CRC_ERR_CNT);
92 writew(0x0000, port + PORT_HSHK_ERR_CNT);
94 /* always use 64bit activation */
95 writel(PORT_CS_32BIT_ACTV, port + PORT_CTRL_CLR);
97 /* clear port multiplier enable and resume bits */
98 writel(PORT_CS_PMP_EN | PORT_CS_PMP_RESUME, port + PORT_CTRL_CLR);
101 static int sil_init_port(void *port)
105 writel(PORT_CS_INIT, port + PORT_CTRL_STAT);
106 ata_wait_register(port + PORT_CTRL_STAT,
107 PORT_CS_INIT, PORT_CS_INIT, 100);
108 tmp = ata_wait_register(port + PORT_CTRL_STAT,
109 PORT_CS_RDY, 0, 100);
111 if ((tmp & (PORT_CS_INIT | PORT_CS_RDY)) != PORT_CS_RDY)
117 static void sil_read_fis(int dev, int tag, struct sata_fis_d2h *fis)
119 struct sil_sata *sata = sata_dev_desc[dev].priv;
120 void *port = sata->port;
125 prb = port + PORT_LRAM + tag * PORT_LRAM_SLOT_SZ;
126 src = (u32 *)&prb->fis;
128 for (i = 0; i < sizeof(struct sata_fis_h2d); i += 4)
129 *dst++ = readl(src++);
132 static int sil_exec_cmd(int dev, struct sil_cmd_block *pcmd, int tag)
134 struct sil_sata *sata = sata_dev_desc[dev].priv;
135 void *port = sata->port;
136 u64 paddr = virt_to_bus(sata->devno, pcmd);
137 u32 irq_mask, irq_stat;
140 writel(PORT_IRQ_COMPLETE | PORT_IRQ_ERROR, port + PORT_IRQ_ENABLE_CLR);
142 /* better to add momery barrior here */
143 writel((u32)paddr, port + PORT_CMD_ACTIVATE + tag * 8);
144 writel((u64)paddr >> 32, port + PORT_CMD_ACTIVATE + tag * 8 + 4);
146 irq_mask = (PORT_IRQ_COMPLETE | PORT_IRQ_ERROR) << PORT_IRQ_RAW_SHIFT;
147 irq_stat = ata_wait_register(port + PORT_IRQ_STAT, irq_mask,
151 writel(irq_mask, port + PORT_IRQ_STAT);
152 irq_stat >>= PORT_IRQ_RAW_SHIFT;
154 if (irq_stat & PORT_IRQ_COMPLETE)
157 /* force port into known state */
159 if (irq_stat & PORT_IRQ_ERROR)
168 static int sil_cmd_set_feature(int dev)
170 struct sil_sata *sata = sata_dev_desc[dev].priv;
171 struct sil_cmd_block cmdb, *pcmd = &cmdb;
172 struct sata_fis_d2h fis;
176 memset((void *)&cmdb, 0, sizeof(struct sil_cmd_block));
177 pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
178 pcmd->prb.fis.pm_port_c = (1 << 7);
179 pcmd->prb.fis.command = ATA_CMD_SET_FEATURES;
180 pcmd->prb.fis.features = SETFEATURES_XFER;
182 /* First check the device capablity */
183 udma_cap = (u8)(sata->udma & 0xff);
184 debug("udma_cap %02x\n", udma_cap);
186 if (udma_cap == ATA_UDMA6)
187 pcmd->prb.fis.sector_count = XFER_UDMA_6;
188 if (udma_cap == ATA_UDMA5)
189 pcmd->prb.fis.sector_count = XFER_UDMA_5;
190 if (udma_cap == ATA_UDMA4)
191 pcmd->prb.fis.sector_count = XFER_UDMA_4;
192 if (udma_cap == ATA_UDMA3)
193 pcmd->prb.fis.sector_count = XFER_UDMA_3;
195 ret = sil_exec_cmd(dev, pcmd, 0);
197 sil_read_fis(dev, 0, &fis);
198 printf("Err: exe cmd(0x%x).\n",
199 readl(sata->port + PORT_SERROR));
200 sil_sata_dump_fis(&fis);
207 static int sil_cmd_identify_device(int dev, u16 *id)
209 struct sil_sata *sata = sata_dev_desc[dev].priv;
210 struct sil_cmd_block cmdb, *pcmd = &cmdb;
211 struct sata_fis_d2h fis;
214 memset((void *)&cmdb, 0, sizeof(struct sil_cmd_block));
215 pcmd->prb.ctrl = cpu_to_le16(PRB_CTRL_PROTOCOL);
216 pcmd->prb.prot = cpu_to_le16(PRB_PROT_READ);
217 pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
218 pcmd->prb.fis.pm_port_c = (1 << 7);
219 pcmd->prb.fis.command = ATA_CMD_ID_ATA;
220 pcmd->sge.addr = cpu_to_le64(virt_to_bus(sata->devno, id));
221 pcmd->sge.cnt = cpu_to_le32(sizeof(id[0]) * ATA_ID_WORDS);
222 pcmd->sge.flags = cpu_to_le32(SGE_TRM);
224 ret = sil_exec_cmd(dev, pcmd, 0);
226 sil_read_fis(dev, 0, &fis);
227 printf("Err: id cmd(0x%x).\n", readl(sata->port + PORT_SERROR));
228 sil_sata_dump_fis(&fis);
231 ata_swap_buf_le16(id, ATA_ID_WORDS);
236 static int sil_cmd_soft_reset(int dev)
238 struct sil_cmd_block cmdb, *pcmd = &cmdb;
239 struct sil_sata *sata = sata_dev_desc[dev].priv;
240 struct sata_fis_d2h fis;
241 void *port = sata->port;
244 /* put the port into known state */
245 if (sil_init_port(port)) {
246 printf("SRST: port %d not ready\n", dev);
250 memset((void *)&cmdb, 0, sizeof(struct sil_cmd_block));
252 pcmd->prb.ctrl = cpu_to_le16(PRB_CTRL_SRST);
253 pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
254 pcmd->prb.fis.pm_port_c = 0xf;
256 ret = sil_exec_cmd(dev, &cmdb, 0);
258 sil_read_fis(dev, 0, &fis);
259 printf("SRST cmd error.\n");
260 sil_sata_dump_fis(&fis);
267 static ulong sil_sata_rw_cmd(int dev, ulong start, ulong blkcnt,
268 u8 *buffer, int is_write)
270 struct sil_sata *sata = sata_dev_desc[dev].priv;
271 struct sil_cmd_block cmdb, *pcmd = &cmdb;
272 struct sata_fis_d2h fis;
277 memset(pcmd, 0, sizeof(struct sil_cmd_block));
278 pcmd->prb.ctrl = cpu_to_le16(PRB_CTRL_PROTOCOL);
279 pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
280 pcmd->prb.fis.pm_port_c = (1 << 7);
282 pcmd->prb.fis.command = ATA_CMD_WRITE;
283 pcmd->prb.prot = cpu_to_le16(PRB_PROT_WRITE);
285 pcmd->prb.fis.command = ATA_CMD_READ;
286 pcmd->prb.prot = cpu_to_le16(PRB_PROT_READ);
289 pcmd->prb.fis.device = ATA_LBA;
290 pcmd->prb.fis.device |= (block >> 24) & 0xf;
291 pcmd->prb.fis.lba_high = (block >> 16) & 0xff;
292 pcmd->prb.fis.lba_mid = (block >> 8) & 0xff;
293 pcmd->prb.fis.lba_low = block & 0xff;
294 pcmd->prb.fis.sector_count = (u8)blkcnt & 0xff;
296 pcmd->sge.addr = cpu_to_le64(virt_to_bus(sata->devno, buffer));
297 pcmd->sge.cnt = cpu_to_le32(blkcnt * ATA_SECT_SIZE);
298 pcmd->sge.flags = cpu_to_le32(SGE_TRM);
300 ret = sil_exec_cmd(dev, pcmd, 0);
302 sil_read_fis(dev, 0, &fis);
303 printf("Err: rw cmd(0x%08x).\n",
304 readl(sata->port + PORT_SERROR));
305 sil_sata_dump_fis(&fis);
312 static ulong sil_sata_rw_cmd_ext(int dev, ulong start, ulong blkcnt,
313 u8 *buffer, int is_write)
315 struct sil_sata *sata = sata_dev_desc[dev].priv;
316 struct sil_cmd_block cmdb, *pcmd = &cmdb;
317 struct sata_fis_d2h fis;
322 memset(pcmd, 0, sizeof(struct sil_cmd_block));
323 pcmd->prb.ctrl = cpu_to_le16(PRB_CTRL_PROTOCOL);
324 pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
325 pcmd->prb.fis.pm_port_c = (1 << 7);
327 pcmd->prb.fis.command = ATA_CMD_WRITE_EXT;
328 pcmd->prb.prot = cpu_to_le16(PRB_PROT_WRITE);
330 pcmd->prb.fis.command = ATA_CMD_READ_EXT;
331 pcmd->prb.prot = cpu_to_le16(PRB_PROT_READ);
334 pcmd->prb.fis.lba_high_exp = (block >> 40) & 0xff;
335 pcmd->prb.fis.lba_mid_exp = (block >> 32) & 0xff;
336 pcmd->prb.fis.lba_low_exp = (block >> 24) & 0xff;
337 pcmd->prb.fis.lba_high = (block >> 16) & 0xff;
338 pcmd->prb.fis.lba_mid = (block >> 8) & 0xff;
339 pcmd->prb.fis.lba_low = block & 0xff;
340 pcmd->prb.fis.device = ATA_LBA;
341 pcmd->prb.fis.sector_count_exp = (blkcnt >> 8) & 0xff;
342 pcmd->prb.fis.sector_count = blkcnt & 0xff;
344 pcmd->sge.addr = cpu_to_le64(virt_to_bus(sata->devno, buffer));
345 pcmd->sge.cnt = cpu_to_le32(blkcnt * ATA_SECT_SIZE);
346 pcmd->sge.flags = cpu_to_le32(SGE_TRM);
348 ret = sil_exec_cmd(dev, pcmd, 0);
350 sil_read_fis(dev, 0, &fis);
351 printf("Err: rw ext cmd(0x%08x).\n",
352 readl(sata->port + PORT_SERROR));
353 sil_sata_dump_fis(&fis);
360 static ulong sil_sata_rw_lba28(int dev, ulong blknr, lbaint_t blkcnt,
361 const void *buffer, int is_write)
363 ulong start, blks, max_blks;
370 max_blks = ATA_MAX_SECTORS;
372 if (blks > max_blks) {
373 sil_sata_rw_cmd(dev, start, max_blks, addr, is_write);
376 addr += ATA_SECT_SIZE * max_blks;
378 sil_sata_rw_cmd(dev, start, blks, addr, is_write);
381 addr += ATA_SECT_SIZE * blks;
388 static ulong sil_sata_rw_lba48(int dev, ulong blknr, lbaint_t blkcnt,
389 const void *buffer, int is_write)
391 ulong start, blks, max_blks;
398 max_blks = ATA_MAX_SECTORS_LBA48;
400 if (blks > max_blks) {
401 sil_sata_rw_cmd_ext(dev, start, max_blks,
405 addr += ATA_SECT_SIZE * max_blks;
407 sil_sata_rw_cmd_ext(dev, start, blks,
411 addr += ATA_SECT_SIZE * blks;
418 static void sil_sata_cmd_flush_cache(int dev)
420 struct sil_cmd_block cmdb, *pcmd = &cmdb;
422 memset((void *)pcmd, 0, sizeof(struct sil_cmd_block));
423 pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
424 pcmd->prb.fis.pm_port_c = (1 << 7);
425 pcmd->prb.fis.command = ATA_CMD_FLUSH;
427 sil_exec_cmd(dev, pcmd, 0);
430 static void sil_sata_cmd_flush_cache_ext(int dev)
432 struct sil_cmd_block cmdb, *pcmd = &cmdb;
434 memset((void *)pcmd, 0, sizeof(struct sil_cmd_block));
435 pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
436 pcmd->prb.fis.pm_port_c = (1 << 7);
437 pcmd->prb.fis.command = ATA_CMD_FLUSH_EXT;
439 sil_exec_cmd(dev, pcmd, 0);
442 static void sil_sata_init_wcache(int dev, u16 *id)
444 struct sil_sata *sata = sata_dev_desc[dev].priv;
446 if (ata_id_has_wcache(id) && ata_id_wcache_enabled(id))
448 if (ata_id_has_flush(id))
450 if (ata_id_has_flush_ext(id))
454 static int sil_sata_get_wcache(int dev)
456 struct sil_sata *sata = sata_dev_desc[dev].priv;
461 static int sil_sata_get_flush(int dev)
463 struct sil_sata *sata = sata_dev_desc[dev].priv;
468 static int sil_sata_get_flush_ext(int dev)
470 struct sil_sata *sata = sata_dev_desc[dev].priv;
472 return sata->flush_ext;
476 * SATA interface between low level driver and command layer
478 ulong sata_read(int dev, ulong blknr, lbaint_t blkcnt, void *buffer)
480 struct sil_sata *sata = sata_dev_desc[dev].priv;
484 rc = sil_sata_rw_lba48(dev, blknr, blkcnt, buffer, READ_CMD);
486 rc = sil_sata_rw_lba28(dev, blknr, blkcnt, buffer, READ_CMD);
492 * SATA interface between low level driver and command layer
494 ulong sata_write(int dev, ulong blknr, lbaint_t blkcnt, const void *buffer)
496 struct sil_sata *sata = sata_dev_desc[dev].priv;
500 rc = sil_sata_rw_lba48(dev, blknr, blkcnt, buffer, WRITE_CMD);
501 if (sil_sata_get_wcache(dev) && sil_sata_get_flush_ext(dev))
502 sil_sata_cmd_flush_cache_ext(dev);
504 rc = sil_sata_rw_lba28(dev, blknr, blkcnt, buffer, WRITE_CMD);
505 if (sil_sata_get_wcache(dev) && sil_sata_get_flush(dev))
506 sil_sata_cmd_flush_cache(dev);
513 * SATA interface between low level driver and command layer
515 int init_sata(int dev)
517 static int init_done, idx;
521 if (init_done == 1 && dev < sata_info.maxport)
526 /* Find PCI device(s) */
527 devno = pci_find_devices(supported, idx++);
531 pci_read_config_word(devno, PCI_DEVICE_ID, &word);
533 /* get the port count */
536 sata_info.portbase = sata_info.maxport;
537 sata_info.maxport = sata_info.portbase + word;
538 sata_info.devno = devno;
540 /* Read out all BARs */
541 sata_info.iobase[0] = (ulong)pci_map_bar(devno,
542 PCI_BASE_ADDRESS_0, PCI_REGION_MEM);
543 sata_info.iobase[1] = (ulong)pci_map_bar(devno,
544 PCI_BASE_ADDRESS_2, PCI_REGION_MEM);
545 sata_info.iobase[2] = (ulong)pci_map_bar(devno,
546 PCI_BASE_ADDRESS_4, PCI_REGION_MEM);
548 /* mask out the unused bits */
549 sata_info.iobase[0] &= 0xffffff80;
550 sata_info.iobase[1] &= 0xfffffc00;
551 sata_info.iobase[2] &= 0xffffff80;
553 /* Enable Bus Mastering and memory region */
554 pci_write_config_word(devno, PCI_COMMAND,
555 PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
557 /* Check if mem accesses and Bus Mastering are enabled. */
558 pci_read_config_word(devno, PCI_COMMAND, &word);
559 if (!(word & PCI_COMMAND_MEMORY) ||
560 (!(word & PCI_COMMAND_MASTER))) {
561 printf("Error: Can not enable MEM access or Bus Mastering.\n");
562 debug("PCI command: %04x\n", word);
567 writel(0, (void *)(sata_info.iobase[0] + HOST_FLASH_CMD));
568 /* clear global reset & mask interrupts during initialization */
569 writel(0, (void *)(sata_info.iobase[0] + HOST_CTRL));
574 int reset_sata(int dev)
580 * SATA interface between low level driver and command layer
582 int scan_sata(int dev)
584 unsigned char serial[ATA_ID_SERNO_LEN + 1];
585 unsigned char firmware[ATA_ID_FW_REV_LEN + 1];
586 unsigned char product[ATA_ID_PROD_LEN + 1];
587 struct sil_sata *sata;
593 if (dev >= sata_info.maxport) {
594 printf("SATA#%d is not present\n", dev);
598 printf("SATA#%d\n", dev);
599 port = (void *)sata_info.iobase[1] +
600 PORT_REGS_SIZE * (dev - sata_info.portbase);
602 /* Initial PHY setting */
603 writel(0x20c, port + PORT_PHY_CFG);
606 tmp = readl(port + PORT_CTRL_STAT);
607 if (tmp & PORT_CS_PORT_RST) {
608 writel(PORT_CS_PORT_RST, port + PORT_CTRL_CLR);
609 tmp = ata_wait_register(port + PORT_CTRL_STAT,
610 PORT_CS_PORT_RST, PORT_CS_PORT_RST, 100);
611 if (tmp & PORT_CS_PORT_RST)
612 printf("Err: Failed to clear port RST\n");
615 /* Check if device is present */
616 for (cnt = 0; cnt < 100; cnt++) {
617 tmp = readl(port + PORT_SSTATUS);
618 if ((tmp & 0xF) == 0x3)
623 tmp = readl(port + PORT_SSTATUS);
624 if ((tmp & 0xf) != 0x3) {
625 printf(" (No RDY)\n");
629 /* Wait for port ready */
630 tmp = ata_wait_register(port + PORT_CTRL_STAT,
631 PORT_CS_RDY, PORT_CS_RDY, 100);
632 if ((tmp & PORT_CS_RDY) != PORT_CS_RDY) {
633 printf("%d port not ready.\n", dev);
638 sil_config_port(port);
641 writel(PORT_CS_DEV_RST, port + PORT_CTRL_STAT);
642 readl(port + PORT_CTRL_STAT);
643 tmp = ata_wait_register(port + PORT_CTRL_STAT, PORT_CS_DEV_RST,
644 PORT_CS_DEV_RST, 100);
645 if (tmp & PORT_CS_DEV_RST) {
646 printf("%d port reset failed.\n", dev);
650 sata = (struct sil_sata *)malloc(sizeof(struct sil_sata));
652 printf("%d no memory.\n", dev);
655 memset((void *)sata, 0, sizeof(struct sil_sata));
657 /* turn on port interrupt */
658 tmp = readl((void *)(sata_info.iobase[0] + HOST_CTRL));
659 tmp |= (1 << (dev - sata_info.portbase));
660 writel(tmp, (void *)(sata_info.iobase[0] + HOST_CTRL));
662 /* Save the private struct to block device struct */
663 sata_dev_desc[dev].priv = (void *)sata;
665 sata->devno = sata_info.devno;
666 sprintf(sata->name, "SATA#%d", dev);
667 sil_cmd_soft_reset(dev);
668 tmp = readl(port + PORT_SSTATUS);
669 tmp = (tmp >> 4) & 0xf;
670 printf(" (%s)\n", sata_spd_string(tmp));
672 id = (u16 *)malloc(ATA_ID_WORDS * 2);
674 printf("Id malloc failed\n");
678 sil_cmd_identify_device(dev, id);
681 /* Check if support LBA48 */
682 if (ata_id_has_lba48(id)) {
683 sata_dev_desc[dev].lba48 = 1;
685 debug("Device supports LBA48\n");
687 debug("Device supports LBA28\n");
691 ata_id_c_string(id, serial, ATA_ID_SERNO, sizeof(serial));
692 memcpy(sata_dev_desc[dev].product, serial, sizeof(serial));
694 /* Firmware version */
695 ata_id_c_string(id, firmware, ATA_ID_FW_REV, sizeof(firmware));
696 memcpy(sata_dev_desc[dev].revision, firmware, sizeof(firmware));
699 ata_id_c_string(id, product, ATA_ID_PROD, sizeof(product));
700 memcpy(sata_dev_desc[dev].vendor, product, sizeof(product));
703 sata_dev_desc[dev].lba = ata_id_n_sectors(id);
705 sil_sata_init_wcache(dev, id);
706 sil_cmd_set_feature(dev);
709 sil_cmd_identify_device(dev, id);