1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2011 Freescale Semiconductor, Inc.
4 * Author: Tang Yuantian <b29983@freescale.com>
10 #include <asm/byteorder.h>
19 /* Convert sectorsize to wordsize */
20 #define ATA_SECTOR_WORDS (ATA_SECT_SIZE/2)
21 #define virt_to_bus(devno, v) pci_virt_to_mem(devno, (void *) (v))
23 static struct sata_info sata_info;
25 static struct pci_device_id supported[] = {
26 {PCI_VENDOR_ID_SILICONIMAGE, PCI_DEVICE_ID_SIL3131},
27 {PCI_VENDOR_ID_SILICONIMAGE, PCI_DEVICE_ID_SIL3132},
28 {PCI_VENDOR_ID_SILICONIMAGE, PCI_DEVICE_ID_SIL3124},
32 static void sil_sata_dump_fis(struct sata_fis_d2h *s)
34 printf("Status FIS dump:\n");
35 printf("fis_type: %02x\n", s->fis_type);
36 printf("pm_port_i: %02x\n", s->pm_port_i);
37 printf("status: %02x\n", s->status);
38 printf("error: %02x\n", s->error);
39 printf("lba_low: %02x\n", s->lba_low);
40 printf("lba_mid: %02x\n", s->lba_mid);
41 printf("lba_high: %02x\n", s->lba_high);
42 printf("device: %02x\n", s->device);
43 printf("lba_low_exp: %02x\n", s->lba_low_exp);
44 printf("lba_mid_exp: %02x\n", s->lba_mid_exp);
45 printf("lba_high_exp: %02x\n", s->lba_high_exp);
46 printf("res1: %02x\n", s->res1);
47 printf("sector_count: %02x\n", s->sector_count);
48 printf("sector_count_exp: %02x\n", s->sector_count_exp);
51 static const char *sata_spd_string(unsigned int speed)
53 static const char * const spd_str[] = {
62 return spd_str[speed - 1];
65 static u32 ata_wait_register(void *reg, u32 mask,
66 u32 val, int timeout_msec)
71 while ((tmp & mask) == val && timeout_msec > 0) {
80 static void sil_config_port(void *port)
82 /* configure IRQ WoC */
83 writel(PORT_CS_IRQ_WOC, port + PORT_CTRL_CLR);
85 /* zero error counters. */
86 writew(0x8000, port + PORT_DECODE_ERR_THRESH);
87 writew(0x8000, port + PORT_CRC_ERR_THRESH);
88 writew(0x8000, port + PORT_HSHK_ERR_THRESH);
89 writew(0x0000, port + PORT_DECODE_ERR_CNT);
90 writew(0x0000, port + PORT_CRC_ERR_CNT);
91 writew(0x0000, port + PORT_HSHK_ERR_CNT);
93 /* always use 64bit activation */
94 writel(PORT_CS_32BIT_ACTV, port + PORT_CTRL_CLR);
96 /* clear port multiplier enable and resume bits */
97 writel(PORT_CS_PMP_EN | PORT_CS_PMP_RESUME, port + PORT_CTRL_CLR);
100 static int sil_init_port(void *port)
104 writel(PORT_CS_INIT, port + PORT_CTRL_STAT);
105 ata_wait_register(port + PORT_CTRL_STAT,
106 PORT_CS_INIT, PORT_CS_INIT, 100);
107 tmp = ata_wait_register(port + PORT_CTRL_STAT,
108 PORT_CS_RDY, 0, 100);
110 if ((tmp & (PORT_CS_INIT | PORT_CS_RDY)) != PORT_CS_RDY)
116 static void sil_read_fis(int dev, int tag, struct sata_fis_d2h *fis)
118 struct sil_sata *sata = sata_dev_desc[dev].priv;
119 void *port = sata->port;
124 prb = port + PORT_LRAM + tag * PORT_LRAM_SLOT_SZ;
125 src = (u32 *)&prb->fis;
127 for (i = 0; i < sizeof(struct sata_fis_h2d); i += 4)
128 *dst++ = readl(src++);
131 static int sil_exec_cmd(int dev, struct sil_cmd_block *pcmd, int tag)
133 struct sil_sata *sata = sata_dev_desc[dev].priv;
134 void *port = sata->port;
135 u64 paddr = virt_to_bus(sata->devno, pcmd);
136 u32 irq_mask, irq_stat;
139 writel(PORT_IRQ_COMPLETE | PORT_IRQ_ERROR, port + PORT_IRQ_ENABLE_CLR);
141 /* better to add momery barrior here */
142 writel((u32)paddr, port + PORT_CMD_ACTIVATE + tag * 8);
143 writel((u64)paddr >> 32, port + PORT_CMD_ACTIVATE + tag * 8 + 4);
145 irq_mask = (PORT_IRQ_COMPLETE | PORT_IRQ_ERROR) << PORT_IRQ_RAW_SHIFT;
146 irq_stat = ata_wait_register(port + PORT_IRQ_STAT, irq_mask,
150 writel(irq_mask, port + PORT_IRQ_STAT);
151 irq_stat >>= PORT_IRQ_RAW_SHIFT;
153 if (irq_stat & PORT_IRQ_COMPLETE)
156 /* force port into known state */
158 if (irq_stat & PORT_IRQ_ERROR)
167 static int sil_cmd_set_feature(int dev)
169 struct sil_sata *sata = sata_dev_desc[dev].priv;
170 struct sil_cmd_block cmdb, *pcmd = &cmdb;
171 struct sata_fis_d2h fis;
175 memset((void *)&cmdb, 0, sizeof(struct sil_cmd_block));
176 pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
177 pcmd->prb.fis.pm_port_c = (1 << 7);
178 pcmd->prb.fis.command = ATA_CMD_SET_FEATURES;
179 pcmd->prb.fis.features = SETFEATURES_XFER;
181 /* First check the device capablity */
182 udma_cap = (u8)(sata->udma & 0xff);
183 debug("udma_cap %02x\n", udma_cap);
185 if (udma_cap == ATA_UDMA6)
186 pcmd->prb.fis.sector_count = XFER_UDMA_6;
187 if (udma_cap == ATA_UDMA5)
188 pcmd->prb.fis.sector_count = XFER_UDMA_5;
189 if (udma_cap == ATA_UDMA4)
190 pcmd->prb.fis.sector_count = XFER_UDMA_4;
191 if (udma_cap == ATA_UDMA3)
192 pcmd->prb.fis.sector_count = XFER_UDMA_3;
194 ret = sil_exec_cmd(dev, pcmd, 0);
196 sil_read_fis(dev, 0, &fis);
197 printf("Err: exe cmd(0x%x).\n",
198 readl(sata->port + PORT_SERROR));
199 sil_sata_dump_fis(&fis);
206 static int sil_cmd_identify_device(int dev, u16 *id)
208 struct sil_sata *sata = sata_dev_desc[dev].priv;
209 struct sil_cmd_block cmdb, *pcmd = &cmdb;
210 struct sata_fis_d2h fis;
213 memset((void *)&cmdb, 0, sizeof(struct sil_cmd_block));
214 pcmd->prb.ctrl = cpu_to_le16(PRB_CTRL_PROTOCOL);
215 pcmd->prb.prot = cpu_to_le16(PRB_PROT_READ);
216 pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
217 pcmd->prb.fis.pm_port_c = (1 << 7);
218 pcmd->prb.fis.command = ATA_CMD_ID_ATA;
219 pcmd->sge.addr = cpu_to_le64(virt_to_bus(sata->devno, id));
220 pcmd->sge.cnt = cpu_to_le32(sizeof(id[0]) * ATA_ID_WORDS);
221 pcmd->sge.flags = cpu_to_le32(SGE_TRM);
223 ret = sil_exec_cmd(dev, pcmd, 0);
225 sil_read_fis(dev, 0, &fis);
226 printf("Err: id cmd(0x%x).\n", readl(sata->port + PORT_SERROR));
227 sil_sata_dump_fis(&fis);
230 ata_swap_buf_le16(id, ATA_ID_WORDS);
235 static int sil_cmd_soft_reset(int dev)
237 struct sil_cmd_block cmdb, *pcmd = &cmdb;
238 struct sil_sata *sata = sata_dev_desc[dev].priv;
239 struct sata_fis_d2h fis;
240 void *port = sata->port;
243 /* put the port into known state */
244 if (sil_init_port(port)) {
245 printf("SRST: port %d not ready\n", dev);
249 memset((void *)&cmdb, 0, sizeof(struct sil_cmd_block));
251 pcmd->prb.ctrl = cpu_to_le16(PRB_CTRL_SRST);
252 pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
253 pcmd->prb.fis.pm_port_c = 0xf;
255 ret = sil_exec_cmd(dev, &cmdb, 0);
257 sil_read_fis(dev, 0, &fis);
258 printf("SRST cmd error.\n");
259 sil_sata_dump_fis(&fis);
266 static ulong sil_sata_rw_cmd(int dev, ulong start, ulong blkcnt,
267 u8 *buffer, int is_write)
269 struct sil_sata *sata = sata_dev_desc[dev].priv;
270 struct sil_cmd_block cmdb, *pcmd = &cmdb;
271 struct sata_fis_d2h fis;
276 memset(pcmd, 0, sizeof(struct sil_cmd_block));
277 pcmd->prb.ctrl = cpu_to_le16(PRB_CTRL_PROTOCOL);
278 pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
279 pcmd->prb.fis.pm_port_c = (1 << 7);
281 pcmd->prb.fis.command = ATA_CMD_WRITE;
282 pcmd->prb.prot = cpu_to_le16(PRB_PROT_WRITE);
284 pcmd->prb.fis.command = ATA_CMD_READ;
285 pcmd->prb.prot = cpu_to_le16(PRB_PROT_READ);
288 pcmd->prb.fis.device = ATA_LBA;
289 pcmd->prb.fis.device |= (block >> 24) & 0xf;
290 pcmd->prb.fis.lba_high = (block >> 16) & 0xff;
291 pcmd->prb.fis.lba_mid = (block >> 8) & 0xff;
292 pcmd->prb.fis.lba_low = block & 0xff;
293 pcmd->prb.fis.sector_count = (u8)blkcnt & 0xff;
295 pcmd->sge.addr = cpu_to_le64(virt_to_bus(sata->devno, buffer));
296 pcmd->sge.cnt = cpu_to_le32(blkcnt * ATA_SECT_SIZE);
297 pcmd->sge.flags = cpu_to_le32(SGE_TRM);
299 ret = sil_exec_cmd(dev, pcmd, 0);
301 sil_read_fis(dev, 0, &fis);
302 printf("Err: rw cmd(0x%08x).\n",
303 readl(sata->port + PORT_SERROR));
304 sil_sata_dump_fis(&fis);
311 static ulong sil_sata_rw_cmd_ext(int dev, ulong start, ulong blkcnt,
312 u8 *buffer, int is_write)
314 struct sil_sata *sata = sata_dev_desc[dev].priv;
315 struct sil_cmd_block cmdb, *pcmd = &cmdb;
316 struct sata_fis_d2h fis;
321 memset(pcmd, 0, sizeof(struct sil_cmd_block));
322 pcmd->prb.ctrl = cpu_to_le16(PRB_CTRL_PROTOCOL);
323 pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
324 pcmd->prb.fis.pm_port_c = (1 << 7);
326 pcmd->prb.fis.command = ATA_CMD_WRITE_EXT;
327 pcmd->prb.prot = cpu_to_le16(PRB_PROT_WRITE);
329 pcmd->prb.fis.command = ATA_CMD_READ_EXT;
330 pcmd->prb.prot = cpu_to_le16(PRB_PROT_READ);
333 pcmd->prb.fis.lba_high_exp = (block >> 40) & 0xff;
334 pcmd->prb.fis.lba_mid_exp = (block >> 32) & 0xff;
335 pcmd->prb.fis.lba_low_exp = (block >> 24) & 0xff;
336 pcmd->prb.fis.lba_high = (block >> 16) & 0xff;
337 pcmd->prb.fis.lba_mid = (block >> 8) & 0xff;
338 pcmd->prb.fis.lba_low = block & 0xff;
339 pcmd->prb.fis.device = ATA_LBA;
340 pcmd->prb.fis.sector_count_exp = (blkcnt >> 8) & 0xff;
341 pcmd->prb.fis.sector_count = blkcnt & 0xff;
343 pcmd->sge.addr = cpu_to_le64(virt_to_bus(sata->devno, buffer));
344 pcmd->sge.cnt = cpu_to_le32(blkcnt * ATA_SECT_SIZE);
345 pcmd->sge.flags = cpu_to_le32(SGE_TRM);
347 ret = sil_exec_cmd(dev, pcmd, 0);
349 sil_read_fis(dev, 0, &fis);
350 printf("Err: rw ext cmd(0x%08x).\n",
351 readl(sata->port + PORT_SERROR));
352 sil_sata_dump_fis(&fis);
359 static ulong sil_sata_rw_lba28(int dev, ulong blknr, lbaint_t blkcnt,
360 const void *buffer, int is_write)
362 ulong start, blks, max_blks;
369 max_blks = ATA_MAX_SECTORS;
371 if (blks > max_blks) {
372 sil_sata_rw_cmd(dev, start, max_blks, addr, is_write);
375 addr += ATA_SECT_SIZE * max_blks;
377 sil_sata_rw_cmd(dev, start, blks, addr, is_write);
380 addr += ATA_SECT_SIZE * blks;
387 static ulong sil_sata_rw_lba48(int dev, ulong blknr, lbaint_t blkcnt,
388 const void *buffer, int is_write)
390 ulong start, blks, max_blks;
397 max_blks = ATA_MAX_SECTORS_LBA48;
399 if (blks > max_blks) {
400 sil_sata_rw_cmd_ext(dev, start, max_blks,
404 addr += ATA_SECT_SIZE * max_blks;
406 sil_sata_rw_cmd_ext(dev, start, blks,
410 addr += ATA_SECT_SIZE * blks;
417 static void sil_sata_cmd_flush_cache(int dev)
419 struct sil_cmd_block cmdb, *pcmd = &cmdb;
421 memset((void *)pcmd, 0, sizeof(struct sil_cmd_block));
422 pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
423 pcmd->prb.fis.pm_port_c = (1 << 7);
424 pcmd->prb.fis.command = ATA_CMD_FLUSH;
426 sil_exec_cmd(dev, pcmd, 0);
429 static void sil_sata_cmd_flush_cache_ext(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_EXT;
438 sil_exec_cmd(dev, pcmd, 0);
441 static void sil_sata_init_wcache(int dev, u16 *id)
443 struct sil_sata *sata = sata_dev_desc[dev].priv;
445 if (ata_id_has_wcache(id) && ata_id_wcache_enabled(id))
447 if (ata_id_has_flush(id))
449 if (ata_id_has_flush_ext(id))
453 static int sil_sata_get_wcache(int dev)
455 struct sil_sata *sata = sata_dev_desc[dev].priv;
460 static int sil_sata_get_flush(int dev)
462 struct sil_sata *sata = sata_dev_desc[dev].priv;
467 static int sil_sata_get_flush_ext(int dev)
469 struct sil_sata *sata = sata_dev_desc[dev].priv;
471 return sata->flush_ext;
475 * SATA interface between low level driver and command layer
477 ulong sata_read(int dev, ulong blknr, lbaint_t blkcnt, void *buffer)
479 struct sil_sata *sata = sata_dev_desc[dev].priv;
483 rc = sil_sata_rw_lba48(dev, blknr, blkcnt, buffer, READ_CMD);
485 rc = sil_sata_rw_lba28(dev, blknr, blkcnt, buffer, READ_CMD);
491 * SATA interface between low level driver and command layer
493 ulong sata_write(int dev, ulong blknr, lbaint_t blkcnt, const void *buffer)
495 struct sil_sata *sata = sata_dev_desc[dev].priv;
499 rc = sil_sata_rw_lba48(dev, blknr, blkcnt, buffer, WRITE_CMD);
500 if (sil_sata_get_wcache(dev) && sil_sata_get_flush_ext(dev))
501 sil_sata_cmd_flush_cache_ext(dev);
503 rc = sil_sata_rw_lba28(dev, blknr, blkcnt, buffer, WRITE_CMD);
504 if (sil_sata_get_wcache(dev) && sil_sata_get_flush(dev))
505 sil_sata_cmd_flush_cache(dev);
512 * SATA interface between low level driver and command layer
514 int init_sata(int dev)
516 static int init_done, idx;
520 if (init_done == 1 && dev < sata_info.maxport)
525 /* Find PCI device(s) */
526 devno = pci_find_devices(supported, idx++);
530 pci_read_config_word(devno, PCI_DEVICE_ID, &word);
532 /* get the port count */
535 sata_info.portbase = sata_info.maxport;
536 sata_info.maxport = sata_info.portbase + word;
537 sata_info.devno = devno;
539 /* Read out all BARs */
540 sata_info.iobase[0] = (ulong)pci_map_bar(devno,
541 PCI_BASE_ADDRESS_0, PCI_REGION_MEM);
542 sata_info.iobase[1] = (ulong)pci_map_bar(devno,
543 PCI_BASE_ADDRESS_2, PCI_REGION_MEM);
544 sata_info.iobase[2] = (ulong)pci_map_bar(devno,
545 PCI_BASE_ADDRESS_4, PCI_REGION_MEM);
547 /* mask out the unused bits */
548 sata_info.iobase[0] &= 0xffffff80;
549 sata_info.iobase[1] &= 0xfffffc00;
550 sata_info.iobase[2] &= 0xffffff80;
552 /* Enable Bus Mastering and memory region */
553 pci_write_config_word(devno, PCI_COMMAND,
554 PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
556 /* Check if mem accesses and Bus Mastering are enabled. */
557 pci_read_config_word(devno, PCI_COMMAND, &word);
558 if (!(word & PCI_COMMAND_MEMORY) ||
559 (!(word & PCI_COMMAND_MASTER))) {
560 printf("Error: Can not enable MEM access or Bus Mastering.\n");
561 debug("PCI command: %04x\n", word);
566 writel(0, (void *)(sata_info.iobase[0] + HOST_FLASH_CMD));
567 /* clear global reset & mask interrupts during initialization */
568 writel(0, (void *)(sata_info.iobase[0] + HOST_CTRL));
573 int reset_sata(int dev)
579 * SATA interface between low level driver and command layer
581 int scan_sata(int dev)
583 unsigned char serial[ATA_ID_SERNO_LEN + 1];
584 unsigned char firmware[ATA_ID_FW_REV_LEN + 1];
585 unsigned char product[ATA_ID_PROD_LEN + 1];
586 struct sil_sata *sata;
592 if (dev >= sata_info.maxport) {
593 printf("SATA#%d is not present\n", dev);
597 printf("SATA#%d\n", dev);
598 port = (void *)sata_info.iobase[1] +
599 PORT_REGS_SIZE * (dev - sata_info.portbase);
601 /* Initial PHY setting */
602 writel(0x20c, port + PORT_PHY_CFG);
605 tmp = readl(port + PORT_CTRL_STAT);
606 if (tmp & PORT_CS_PORT_RST) {
607 writel(PORT_CS_PORT_RST, port + PORT_CTRL_CLR);
608 tmp = ata_wait_register(port + PORT_CTRL_STAT,
609 PORT_CS_PORT_RST, PORT_CS_PORT_RST, 100);
610 if (tmp & PORT_CS_PORT_RST)
611 printf("Err: Failed to clear port RST\n");
614 /* Check if device is present */
615 for (cnt = 0; cnt < 100; cnt++) {
616 tmp = readl(port + PORT_SSTATUS);
617 if ((tmp & 0xF) == 0x3)
622 tmp = readl(port + PORT_SSTATUS);
623 if ((tmp & 0xf) != 0x3) {
624 printf(" (No RDY)\n");
628 /* Wait for port ready */
629 tmp = ata_wait_register(port + PORT_CTRL_STAT,
630 PORT_CS_RDY, PORT_CS_RDY, 100);
631 if ((tmp & PORT_CS_RDY) != PORT_CS_RDY) {
632 printf("%d port not ready.\n", dev);
637 sil_config_port(port);
640 writel(PORT_CS_DEV_RST, port + PORT_CTRL_STAT);
641 readl(port + PORT_CTRL_STAT);
642 tmp = ata_wait_register(port + PORT_CTRL_STAT, PORT_CS_DEV_RST,
643 PORT_CS_DEV_RST, 100);
644 if (tmp & PORT_CS_DEV_RST) {
645 printf("%d port reset failed.\n", dev);
649 sata = (struct sil_sata *)malloc(sizeof(struct sil_sata));
651 printf("%d no memory.\n", dev);
654 memset((void *)sata, 0, sizeof(struct sil_sata));
656 /* turn on port interrupt */
657 tmp = readl((void *)(sata_info.iobase[0] + HOST_CTRL));
658 tmp |= (1 << (dev - sata_info.portbase));
659 writel(tmp, (void *)(sata_info.iobase[0] + HOST_CTRL));
661 /* Save the private struct to block device struct */
662 sata_dev_desc[dev].priv = (void *)sata;
664 sata->devno = sata_info.devno;
665 sprintf(sata->name, "SATA#%d", dev);
666 sil_cmd_soft_reset(dev);
667 tmp = readl(port + PORT_SSTATUS);
668 tmp = (tmp >> 4) & 0xf;
669 printf(" (%s)\n", sata_spd_string(tmp));
671 id = (u16 *)malloc(ATA_ID_WORDS * 2);
673 printf("Id malloc failed\n");
677 sil_cmd_identify_device(dev, id);
680 /* Check if support LBA48 */
681 if (ata_id_has_lba48(id)) {
682 sata_dev_desc[dev].lba48 = 1;
684 debug("Device supports LBA48\n");
686 debug("Device supports LBA28\n");
690 ata_id_c_string(id, serial, ATA_ID_SERNO, sizeof(serial));
691 memcpy(sata_dev_desc[dev].product, serial, sizeof(serial));
693 /* Firmware version */
694 ata_id_c_string(id, firmware, ATA_ID_FW_REV, sizeof(firmware));
695 memcpy(sata_dev_desc[dev].revision, firmware, sizeof(firmware));
698 ata_id_c_string(id, product, ATA_ID_PROD, sizeof(product));
699 memcpy(sata_dev_desc[dev].vendor, product, sizeof(product));
702 sata_dev_desc[dev].lba = ata_id_n_sectors(id);
704 sil_sata_init_wcache(dev, id);
705 sil_cmd_set_feature(dev);
708 sil_cmd_identify_device(dev, id);