1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2011 Freescale Semiconductor, Inc.
5 * Author: Tang Yuantian <b29983@freescale.com>
13 #include <asm/byteorder.h>
20 #include <linux/delay.h>
22 #if CONFIG_IS_ENABLED(BLK)
25 #include <dm/device-internal.h>
31 #define virt_to_bus(devno, v) dm_pci_virt_to_mem(devno, (void *) (v))
33 #define virt_to_bus(devno, v) pci_virt_to_mem(devno, (void *) (v))
36 /* just compatible ahci_ops */
40 int (*scan)(struct udevice *dev);
43 static struct sata_info sata_info;
45 static struct pci_device_id supported[] = {
46 { PCI_DEVICE(PCI_VENDOR_ID_SILICONIMAGE, PCI_DEVICE_ID_SIL3131) },
47 { PCI_DEVICE(PCI_VENDOR_ID_SILICONIMAGE, PCI_DEVICE_ID_SIL3132) },
48 { PCI_DEVICE(PCI_VENDOR_ID_SILICONIMAGE, PCI_DEVICE_ID_SIL3124) },
52 static void sil_sata_dump_fis(struct sata_fis_d2h *s)
54 printf("Status FIS dump:\n");
55 printf("fis_type: %02x\n", s->fis_type);
56 printf("pm_port_i: %02x\n", s->pm_port_i);
57 printf("status: %02x\n", s->status);
58 printf("error: %02x\n", s->error);
59 printf("lba_low: %02x\n", s->lba_low);
60 printf("lba_mid: %02x\n", s->lba_mid);
61 printf("lba_high: %02x\n", s->lba_high);
62 printf("device: %02x\n", s->device);
63 printf("lba_low_exp: %02x\n", s->lba_low_exp);
64 printf("lba_mid_exp: %02x\n", s->lba_mid_exp);
65 printf("lba_high_exp: %02x\n", s->lba_high_exp);
66 printf("res1: %02x\n", s->res1);
67 printf("sector_count: %02x\n", s->sector_count);
68 printf("sector_count_exp: %02x\n", s->sector_count_exp);
71 static const char *sata_spd_string(unsigned int speed)
73 static const char * const spd_str[] = {
82 return spd_str[speed - 1];
85 static u32 ata_wait_register(void *reg, u32 mask,
86 u32 val, int timeout_msec)
91 while ((tmp & mask) == val && timeout_msec > 0) {
100 static void sil_config_port(void *port)
102 /* configure IRQ WoC */
103 writel(PORT_CS_IRQ_WOC, port + PORT_CTRL_CLR);
105 /* zero error counters. */
106 writew(0x8000, port + PORT_DECODE_ERR_THRESH);
107 writew(0x8000, port + PORT_CRC_ERR_THRESH);
108 writew(0x8000, port + PORT_HSHK_ERR_THRESH);
109 writew(0x0000, port + PORT_DECODE_ERR_CNT);
110 writew(0x0000, port + PORT_CRC_ERR_CNT);
111 writew(0x0000, port + PORT_HSHK_ERR_CNT);
113 /* always use 64bit activation */
114 writel(PORT_CS_32BIT_ACTV, port + PORT_CTRL_CLR);
116 /* clear port multiplier enable and resume bits */
117 writel(PORT_CS_PMP_EN | PORT_CS_PMP_RESUME, port + PORT_CTRL_CLR);
120 static int sil_init_port(void *port)
124 writel(PORT_CS_INIT, port + PORT_CTRL_STAT);
125 ata_wait_register(port + PORT_CTRL_STAT,
126 PORT_CS_INIT, PORT_CS_INIT, 100);
127 tmp = ata_wait_register(port + PORT_CTRL_STAT,
128 PORT_CS_RDY, 0, 100);
130 if ((tmp & (PORT_CS_INIT | PORT_CS_RDY)) != PORT_CS_RDY)
136 static void sil_read_fis(struct sil_sata *sata, int tag,
137 struct sata_fis_d2h *fis)
139 void *port = sata->port;
144 prb = port + PORT_LRAM + tag * PORT_LRAM_SLOT_SZ;
145 src = (u32 *)&prb->fis;
147 for (i = 0; i < sizeof(struct sata_fis_h2d); i += 4)
148 *dst++ = readl(src++);
151 static int sil_exec_cmd(struct sil_sata *sata, struct sil_cmd_block *pcmd,
154 void *port = sata->port;
155 u64 paddr = virt_to_bus(sata->devno, pcmd);
156 u32 irq_mask, irq_stat;
159 writel(PORT_IRQ_COMPLETE | PORT_IRQ_ERROR, port + PORT_IRQ_ENABLE_CLR);
161 /* better to add momery barrior here */
162 writel((u32)paddr, port + PORT_CMD_ACTIVATE + tag * 8);
163 writel((u64)paddr >> 32, port + PORT_CMD_ACTIVATE + tag * 8 + 4);
165 irq_mask = (PORT_IRQ_COMPLETE | PORT_IRQ_ERROR) << PORT_IRQ_RAW_SHIFT;
166 irq_stat = ata_wait_register(port + PORT_IRQ_STAT, irq_mask,
170 writel(irq_mask, port + PORT_IRQ_STAT);
171 irq_stat >>= PORT_IRQ_RAW_SHIFT;
173 if (irq_stat & PORT_IRQ_COMPLETE)
176 /* force port into known state */
178 if (irq_stat & PORT_IRQ_ERROR)
187 static int sil_cmd_set_feature(struct sil_sata *sata)
189 struct sil_cmd_block cmdb, *pcmd = &cmdb;
190 struct sata_fis_d2h fis;
194 memset((void *)&cmdb, 0, sizeof(struct sil_cmd_block));
195 pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
196 pcmd->prb.fis.pm_port_c = (1 << 7);
197 pcmd->prb.fis.command = ATA_CMD_SET_FEATURES;
198 pcmd->prb.fis.features = SETFEATURES_XFER;
200 /* First check the device capablity */
201 udma_cap = (u8)(sata->udma & 0xff);
202 debug("udma_cap %02x\n", udma_cap);
204 if (udma_cap == ATA_UDMA6)
205 pcmd->prb.fis.sector_count = XFER_UDMA_6;
206 if (udma_cap == ATA_UDMA5)
207 pcmd->prb.fis.sector_count = XFER_UDMA_5;
208 if (udma_cap == ATA_UDMA4)
209 pcmd->prb.fis.sector_count = XFER_UDMA_4;
210 if (udma_cap == ATA_UDMA3)
211 pcmd->prb.fis.sector_count = XFER_UDMA_3;
213 ret = sil_exec_cmd(sata, pcmd, 0);
215 sil_read_fis(sata, 0, &fis);
216 printf("Err: exe cmd(0x%x).\n",
217 readl(sata->port + PORT_SERROR));
218 sil_sata_dump_fis(&fis);
225 static void sil_sata_init_wcache(struct sil_sata *sata, u16 *id)
227 if (ata_id_has_wcache(id) && ata_id_wcache_enabled(id))
229 if (ata_id_has_flush(id))
231 if (ata_id_has_flush_ext(id))
235 static void sil_sata_set_feature_by_id(struct sil_sata *sata, u16 *id)
238 /* Check if support LBA48 */
239 if (ata_id_has_lba48(id)) {
241 debug("Device supports LBA48\n");
243 debug("Device supports LBA28\n");
247 sil_sata_init_wcache(sata, id);
248 sil_cmd_set_feature(sata);
251 static int sil_cmd_identify_device(struct sil_sata *sata, u16 *id)
253 struct sil_cmd_block cmdb, *pcmd = &cmdb;
254 struct sata_fis_d2h fis;
257 memset((void *)&cmdb, 0, sizeof(struct sil_cmd_block));
258 pcmd->prb.ctrl = cpu_to_le16(PRB_CTRL_PROTOCOL);
259 pcmd->prb.prot = cpu_to_le16(PRB_PROT_READ);
260 pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
261 pcmd->prb.fis.pm_port_c = (1 << 7);
262 pcmd->prb.fis.command = ATA_CMD_ID_ATA;
263 pcmd->sge.addr = cpu_to_le64(virt_to_bus(sata->devno, id));
264 pcmd->sge.cnt = cpu_to_le32(sizeof(id[0]) * ATA_ID_WORDS);
265 pcmd->sge.flags = cpu_to_le32(SGE_TRM);
267 ret = sil_exec_cmd(sata, pcmd, 0);
269 sil_read_fis(sata, 0, &fis);
270 printf("Err: id cmd(0x%x).\n", readl(sata->port + PORT_SERROR));
271 sil_sata_dump_fis(&fis);
274 ata_swap_buf_le16(id, ATA_ID_WORDS);
279 static int sil_cmd_soft_reset(struct sil_sata *sata)
281 struct sil_cmd_block cmdb, *pcmd = &cmdb;
282 struct sata_fis_d2h fis;
283 void *port = sata->port;
286 /* put the port into known state */
287 if (sil_init_port(port)) {
288 printf("SRST: port %d not ready\n", sata->id);
292 memset((void *)&cmdb, 0, sizeof(struct sil_cmd_block));
294 pcmd->prb.ctrl = cpu_to_le16(PRB_CTRL_SRST);
295 pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
296 pcmd->prb.fis.pm_port_c = 0xf;
298 ret = sil_exec_cmd(sata, &cmdb, 0);
300 sil_read_fis(sata, 0, &fis);
301 printf("SRST cmd error.\n");
302 sil_sata_dump_fis(&fis);
309 static ulong sil_sata_rw_cmd(struct sil_sata *sata, ulong start, ulong blkcnt,
310 u8 *buffer, int is_write)
312 struct sil_cmd_block cmdb, *pcmd = &cmdb;
313 struct sata_fis_d2h fis;
318 memset(pcmd, 0, sizeof(struct sil_cmd_block));
319 pcmd->prb.ctrl = cpu_to_le16(PRB_CTRL_PROTOCOL);
320 pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
321 pcmd->prb.fis.pm_port_c = (1 << 7);
323 pcmd->prb.fis.command = ATA_CMD_WRITE;
324 pcmd->prb.prot = cpu_to_le16(PRB_PROT_WRITE);
326 pcmd->prb.fis.command = ATA_CMD_READ;
327 pcmd->prb.prot = cpu_to_le16(PRB_PROT_READ);
330 pcmd->prb.fis.device = ATA_LBA;
331 pcmd->prb.fis.device |= (block >> 24) & 0xf;
332 pcmd->prb.fis.lba_high = (block >> 16) & 0xff;
333 pcmd->prb.fis.lba_mid = (block >> 8) & 0xff;
334 pcmd->prb.fis.lba_low = block & 0xff;
335 pcmd->prb.fis.sector_count = (u8)blkcnt & 0xff;
337 pcmd->sge.addr = cpu_to_le64(virt_to_bus(sata->devno, buffer));
338 pcmd->sge.cnt = cpu_to_le32(blkcnt * ATA_SECT_SIZE);
339 pcmd->sge.flags = cpu_to_le32(SGE_TRM);
341 ret = sil_exec_cmd(sata, pcmd, 0);
343 sil_read_fis(sata, 0, &fis);
344 printf("Err: rw cmd(0x%08x).\n",
345 readl(sata->port + PORT_SERROR));
346 sil_sata_dump_fis(&fis);
353 static ulong sil_sata_rw_cmd_ext(struct sil_sata *sata, ulong start,
354 ulong blkcnt, u8 *buffer, int is_write)
356 struct sil_cmd_block cmdb, *pcmd = &cmdb;
357 struct sata_fis_d2h fis;
362 memset(pcmd, 0, sizeof(struct sil_cmd_block));
363 pcmd->prb.ctrl = cpu_to_le16(PRB_CTRL_PROTOCOL);
364 pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
365 pcmd->prb.fis.pm_port_c = (1 << 7);
367 pcmd->prb.fis.command = ATA_CMD_WRITE_EXT;
368 pcmd->prb.prot = cpu_to_le16(PRB_PROT_WRITE);
370 pcmd->prb.fis.command = ATA_CMD_READ_EXT;
371 pcmd->prb.prot = cpu_to_le16(PRB_PROT_READ);
374 pcmd->prb.fis.lba_high_exp = (block >> 40) & 0xff;
375 pcmd->prb.fis.lba_mid_exp = (block >> 32) & 0xff;
376 pcmd->prb.fis.lba_low_exp = (block >> 24) & 0xff;
377 pcmd->prb.fis.lba_high = (block >> 16) & 0xff;
378 pcmd->prb.fis.lba_mid = (block >> 8) & 0xff;
379 pcmd->prb.fis.lba_low = block & 0xff;
380 pcmd->prb.fis.device = ATA_LBA;
381 pcmd->prb.fis.sector_count_exp = (blkcnt >> 8) & 0xff;
382 pcmd->prb.fis.sector_count = blkcnt & 0xff;
384 pcmd->sge.addr = cpu_to_le64(virt_to_bus(sata->devno, buffer));
385 pcmd->sge.cnt = cpu_to_le32(blkcnt * ATA_SECT_SIZE);
386 pcmd->sge.flags = cpu_to_le32(SGE_TRM);
388 ret = sil_exec_cmd(sata, pcmd, 0);
390 sil_read_fis(sata, 0, &fis);
391 printf("Err: rw ext cmd(0x%08x).\n",
392 readl(sata->port + PORT_SERROR));
393 sil_sata_dump_fis(&fis);
400 static ulong sil_sata_rw_lba28(struct sil_sata *sata, ulong blknr,
401 lbaint_t blkcnt, const void *buffer,
404 ulong start, blks, max_blks;
411 max_blks = ATA_MAX_SECTORS;
413 if (blks > max_blks) {
414 sil_sata_rw_cmd(sata, start, max_blks, addr, is_write);
417 addr += ATA_SECT_SIZE * max_blks;
419 sil_sata_rw_cmd(sata, start, blks, addr, is_write);
422 addr += ATA_SECT_SIZE * blks;
429 static ulong sil_sata_rw_lba48(struct sil_sata *sata, ulong blknr,
430 lbaint_t blkcnt, const void *buffer,
433 ulong start, blks, max_blks;
440 max_blks = ATA_MAX_SECTORS_LBA48;
442 if (blks > max_blks) {
443 sil_sata_rw_cmd_ext(sata, start, max_blks,
447 addr += ATA_SECT_SIZE * max_blks;
449 sil_sata_rw_cmd_ext(sata, start, blks,
453 addr += ATA_SECT_SIZE * blks;
460 static void sil_sata_cmd_flush_cache(struct sil_sata *sata)
462 struct sil_cmd_block cmdb, *pcmd = &cmdb;
464 memset((void *)pcmd, 0, sizeof(struct sil_cmd_block));
465 pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
466 pcmd->prb.fis.pm_port_c = (1 << 7);
467 pcmd->prb.fis.command = ATA_CMD_FLUSH;
469 sil_exec_cmd(sata, pcmd, 0);
472 static void sil_sata_cmd_flush_cache_ext(struct sil_sata *sata)
474 struct sil_cmd_block cmdb, *pcmd = &cmdb;
476 memset((void *)pcmd, 0, sizeof(struct sil_cmd_block));
477 pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
478 pcmd->prb.fis.pm_port_c = (1 << 7);
479 pcmd->prb.fis.command = ATA_CMD_FLUSH_EXT;
481 sil_exec_cmd(sata, pcmd, 0);
485 * SATA interface between low level driver and command layer
487 #if !CONFIG_IS_ENABLED(BLK)
488 ulong sata_read(int dev, ulong blknr, lbaint_t blkcnt, void *buffer)
490 struct sil_sata *sata = (struct sil_sata *)sata_dev_desc[dev].priv;
492 static ulong sata_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
495 struct sil_sata_priv *priv = dev_get_plat(dev);
496 int port_number = priv->port_num;
497 struct sil_sata *sata = priv->sil_sata_desc[port_number];
502 rc = sil_sata_rw_lba48(sata, blknr, blkcnt, buffer, READ_CMD);
504 rc = sil_sata_rw_lba28(sata, blknr, blkcnt, buffer, READ_CMD);
510 * SATA interface between low level driver and command layer
512 #if !CONFIG_IS_ENABLED(BLK)
513 ulong sata_write(int dev, ulong blknr, lbaint_t blkcnt, const void *buffer)
515 struct sil_sata *sata = (struct sil_sata *)sata_dev_desc[dev].priv;
517 ulong sata_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
520 struct sil_sata_priv *priv = dev_get_plat(dev);
521 int port_number = priv->port_num;
522 struct sil_sata *sata = priv->sil_sata_desc[port_number];
527 rc = sil_sata_rw_lba48(sata, blknr, blkcnt, buffer, WRITE_CMD);
528 if (sata->wcache && sata->flush_ext)
529 sil_sata_cmd_flush_cache_ext(sata);
531 rc = sil_sata_rw_lba28(sata, blknr, blkcnt, buffer, WRITE_CMD);
532 if (sata->wcache && sata->flush)
533 sil_sata_cmd_flush_cache(sata);
539 #if !CONFIG_IS_ENABLED(BLK)
540 static int sil_init_sata(int dev)
543 static int sil_init_sata(struct udevice *uc_dev, int dev)
545 struct sil_sata_priv *priv = dev_get_plat(uc_dev);
547 struct sil_sata *sata;
552 printf("SATA#%d:\n", dev);
554 port = (void *)sata_info.iobase[1] +
555 PORT_REGS_SIZE * (dev - sata_info.portbase);
557 /* Initial PHY setting */
558 writel(0x20c, port + PORT_PHY_CFG);
561 tmp = readl(port + PORT_CTRL_STAT);
562 if (tmp & PORT_CS_PORT_RST) {
563 writel(PORT_CS_PORT_RST, port + PORT_CTRL_CLR);
564 tmp = ata_wait_register(port + PORT_CTRL_STAT,
565 PORT_CS_PORT_RST, PORT_CS_PORT_RST, 100);
566 if (tmp & PORT_CS_PORT_RST)
567 printf("Err: Failed to clear port RST\n");
570 /* Check if device is present */
571 for (cnt = 0; cnt < 100; cnt++) {
572 tmp = readl(port + PORT_SSTATUS);
573 if ((tmp & 0xF) == 0x3)
578 tmp = readl(port + PORT_SSTATUS);
579 if ((tmp & 0xf) != 0x3) {
580 printf(" (No RDY)\n");
584 /* Wait for port ready */
585 tmp = ata_wait_register(port + PORT_CTRL_STAT,
586 PORT_CS_RDY, PORT_CS_RDY, 100);
587 if ((tmp & PORT_CS_RDY) != PORT_CS_RDY) {
588 printf("%d port not ready.\n", dev);
593 sil_config_port(port);
596 writel(PORT_CS_DEV_RST, port + PORT_CTRL_STAT);
597 readl(port + PORT_CTRL_STAT);
598 tmp = ata_wait_register(port + PORT_CTRL_STAT, PORT_CS_DEV_RST,
599 PORT_CS_DEV_RST, 100);
600 if (tmp & PORT_CS_DEV_RST) {
601 printf("%d port reset failed.\n", dev);
605 sata = (struct sil_sata *)malloc(sizeof(struct sil_sata));
607 printf("%d no memory.\n", dev);
610 memset((void *)sata, 0, sizeof(struct sil_sata));
612 /* Save the private struct to block device struct */
613 #if !CONFIG_IS_ENABLED(BLK)
614 sata_dev_desc[dev].priv = (void *)sata;
615 sata->devno = sata_info.devno;
617 priv->sil_sata_desc[dev] = sata;
618 priv->port_num = dev;
620 sata->devno = uc_dev->parent;
622 sata->devno = sata_info.devno;
623 #endif /* CONFIG_DM_PCI */
627 sprintf(sata->name, "SATA#%d", dev);
628 sil_cmd_soft_reset(sata);
629 tmp = readl(port + PORT_SSTATUS);
630 tmp = (tmp >> 4) & 0xf;
631 printf(" (%s)\n", sata_spd_string(tmp));
636 #if !CONFIG_IS_ENABLED(BLK)
638 * SATA interface between low level driver and command layer
640 int init_sata(int dev)
642 static int init_done, idx;
646 if (init_done == 1 && dev < sata_info.maxport)
651 /* Find PCI device(s) */
652 devno = pci_find_devices(supported, idx++);
656 pci_read_config_word(devno, PCI_DEVICE_ID, &word);
658 /* get the port count */
661 sata_info.portbase = 0;
662 sata_info.maxport = sata_info.portbase + word;
663 sata_info.devno = devno;
665 /* Read out all BARs */
666 sata_info.iobase[0] = (ulong)pci_map_bar(devno,
667 PCI_BASE_ADDRESS_0, PCI_REGION_MEM);
668 sata_info.iobase[1] = (ulong)pci_map_bar(devno,
669 PCI_BASE_ADDRESS_2, PCI_REGION_MEM);
671 /* mask out the unused bits */
672 sata_info.iobase[0] &= 0xffffff80;
673 sata_info.iobase[1] &= 0xfffffc00;
675 /* Enable Bus Mastering and memory region */
676 pci_write_config_word(devno, PCI_COMMAND,
677 PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
679 /* Check if mem accesses and Bus Mastering are enabled. */
680 pci_read_config_word(devno, PCI_COMMAND, &word);
681 if (!(word & PCI_COMMAND_MEMORY) ||
682 (!(word & PCI_COMMAND_MASTER))) {
683 printf("Error: Can not enable MEM access or Bus Mastering.\n");
684 debug("PCI command: %04x\n", word);
689 writel(0, (void *)(sata_info.iobase[0] + HOST_FLASH_CMD));
690 /* clear global reset & mask interrupts during initialization */
691 writel(0, (void *)(sata_info.iobase[0] + HOST_CTRL));
694 return sil_init_sata(dev);
697 int reset_sata(int dev)
703 * SATA interface between low level driver and command layer
705 int scan_sata(int dev)
707 struct sil_sata *sata = (struct sil_sata *)sata_dev_desc[dev].priv;
709 static int scan_sata(struct udevice *blk_dev, int dev)
711 struct blk_desc *desc = dev_get_uclass_plat(blk_dev);
712 struct sil_sata_priv *priv = dev_get_plat(blk_dev);
713 struct sil_sata *sata = priv->sil_sata_desc[dev];
715 unsigned char serial[ATA_ID_SERNO_LEN + 1];
716 unsigned char firmware[ATA_ID_FW_REV_LEN + 1];
717 unsigned char product[ATA_ID_PROD_LEN + 1];
720 id = (u16 *)malloc(ATA_ID_WORDS * 2);
722 printf("Id malloc failed\n");
725 sil_cmd_identify_device(sata, id);
727 sil_sata_set_feature_by_id(sata, id);
730 ata_id_c_string(id, serial, ATA_ID_SERNO, sizeof(serial));
732 /* Firmware version */
733 ata_id_c_string(id, firmware, ATA_ID_FW_REV, sizeof(firmware));
736 ata_id_c_string(id, product, ATA_ID_PROD, sizeof(product));
738 #if !CONFIG_IS_ENABLED(BLK)
739 memcpy(sata_dev_desc[dev].product, serial, sizeof(serial));
740 memcpy(sata_dev_desc[dev].revision, firmware, sizeof(firmware));
741 memcpy(sata_dev_desc[dev].vendor, product, sizeof(product));
743 sata_dev_desc[dev].lba = ata_id_n_sectors(id);
745 sata_dev_desc[dev].lba48 = sata->lba48;
748 memcpy(desc->product, serial, sizeof(serial));
749 memcpy(desc->revision, firmware, sizeof(firmware));
750 memcpy(desc->vendor, product, sizeof(product));
751 desc->lba = ata_id_n_sectors(id);
753 desc->lba48 = sata->lba48;
765 #if CONFIG_IS_ENABLED(BLK)
766 static const struct blk_ops sata_sil_blk_ops = {
771 U_BOOT_DRIVER(sata_sil_driver) = {
772 .name = "sata_sil_blk",
774 .ops = &sata_sil_blk_ops,
775 .plat_auto = sizeof(struct sil_sata_priv),
778 static int sil_unbind_device(struct udevice *dev)
782 ret = device_remove(dev, DM_REMOVE_NORMAL);
786 ret = device_unbind(dev);
793 static int sil_pci_probe(struct udevice *dev)
805 /* Get PCI device number */
806 devno = dm_pci_get_bdf(dev);
810 dm_pci_read_config16(dev, PCI_DEVICE_ID, &word);
812 /* get the port count */
815 sata_info.portbase = 0;
816 sata_info.maxport = sata_info.portbase + word;
817 sata_info.devno = devno;
819 /* Read out all BARs */
820 sata_info.iobase[0] = (ulong)dm_pci_map_bar(dev,
821 PCI_BASE_ADDRESS_0, PCI_REGION_MEM);
822 sata_info.iobase[1] = (ulong)dm_pci_map_bar(dev,
823 PCI_BASE_ADDRESS_2, PCI_REGION_MEM);
825 /* mask out the unused bits */
826 sata_info.iobase[0] &= 0xffffff80;
827 sata_info.iobase[1] &= 0xfffffc00;
829 /* Enable Bus Mastering and memory region */
830 dm_pci_write_config16(dev, PCI_COMMAND,
831 PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
833 /* Check if mem accesses and Bus Mastering are enabled. */
834 dm_pci_read_config16(dev, PCI_COMMAND, &word);
835 if (!(word & PCI_COMMAND_MEMORY) ||
836 (!(word & PCI_COMMAND_MASTER))) {
837 printf("Error: Can not enable MEM access or Bus Mastering.\n");
838 debug("PCI command: %04x\n", word);
843 writel(0, (void *)(sata_info.iobase[0] + HOST_FLASH_CMD));
844 /* clear global reset & mask interrupts during initialization */
845 writel(0, (void *)(sata_info.iobase[0] + HOST_CTRL));
847 for (i = sata_info.portbase; i < sata_info.maxport; i++) {
848 snprintf(sata_name, sizeof(sata_name), "sil_sata%d", i);
849 ret = blk_create_devicef(dev, "sata_sil_blk", sata_name,
850 IF_TYPE_SATA, -1, 512, 0, &blk);
852 debug("Can't create device\n");
856 ret = sil_init_sata(blk, i);
858 ret = sil_unbind_device(blk);
866 ret = scan_sata(blk, i);
868 ret = sil_unbind_device(blk);
877 if (failed_number == sata_info.maxport)
883 static int sil_pci_remove(struct udevice *dev)
886 struct sil_sata *sata;
887 struct sil_sata_priv *priv;
889 priv = dev_get_priv(dev);
891 for (i = sata_info.portbase; i < sata_info.maxport; i++) {
892 sata = priv->sil_sata_desc[i];
900 static int sata_sil_scan(struct udevice *dev)
902 /* Nothing to do here */
907 struct sil_ops sata_sil_ops = {
908 .scan = sata_sil_scan,
911 static const struct udevice_id sil_pci_ids[] = {
912 { .compatible = "sil-pci-sample" },
916 U_BOOT_DRIVER(sil_ahci_pci) = {
917 .name = "sil_ahci_pci",
919 .of_match = sil_pci_ids,
920 .ops = &sata_sil_ops,
921 .probe = sil_pci_probe,
922 .remove = sil_pci_remove,
923 .priv_auto = sizeof(struct sil_sata_priv),
926 U_BOOT_PCI_DEVICE(sil_ahci_pci, supported);