1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2011 Freescale Semiconductor, Inc.
13 #include <asm/byteorder.h>
21 #if CONFIG_IS_ENABLED(BLK)
24 #include <dm/device-internal.h>
29 #define virt_to_bus(devno, v) pci_virt_to_mem(devno, (void *) (v))
31 /* just compatible ahci_ops */
35 int (*scan)(struct udevice *dev);
38 static struct sata_info sata_info;
40 static struct pci_device_id supported[] = {
41 { PCI_DEVICE(PCI_VENDOR_ID_SILICONIMAGE, PCI_DEVICE_ID_SIL3131) },
42 { PCI_DEVICE(PCI_VENDOR_ID_SILICONIMAGE, PCI_DEVICE_ID_SIL3132) },
43 { PCI_DEVICE(PCI_VENDOR_ID_SILICONIMAGE, PCI_DEVICE_ID_SIL3124) },
47 static void sil_sata_dump_fis(struct sata_fis_d2h *s)
49 printf("Status FIS dump:\n");
50 printf("fis_type: %02x\n", s->fis_type);
51 printf("pm_port_i: %02x\n", s->pm_port_i);
52 printf("status: %02x\n", s->status);
53 printf("error: %02x\n", s->error);
54 printf("lba_low: %02x\n", s->lba_low);
55 printf("lba_mid: %02x\n", s->lba_mid);
56 printf("lba_high: %02x\n", s->lba_high);
57 printf("device: %02x\n", s->device);
58 printf("lba_low_exp: %02x\n", s->lba_low_exp);
59 printf("lba_mid_exp: %02x\n", s->lba_mid_exp);
60 printf("lba_high_exp: %02x\n", s->lba_high_exp);
61 printf("res1: %02x\n", s->res1);
62 printf("sector_count: %02x\n", s->sector_count);
63 printf("sector_count_exp: %02x\n", s->sector_count_exp);
66 static const char *sata_spd_string(unsigned int speed)
68 static const char * const spd_str[] = {
77 return spd_str[speed - 1];
80 static u32 ata_wait_register(void *reg, u32 mask,
81 u32 val, int timeout_msec)
86 while ((tmp & mask) == val && timeout_msec > 0) {
95 static void sil_config_port(void *port)
97 /* configure IRQ WoC */
98 writel(PORT_CS_IRQ_WOC, port + PORT_CTRL_CLR);
100 /* zero error counters. */
101 writew(0x8000, port + PORT_DECODE_ERR_THRESH);
102 writew(0x8000, port + PORT_CRC_ERR_THRESH);
103 writew(0x8000, port + PORT_HSHK_ERR_THRESH);
104 writew(0x0000, port + PORT_DECODE_ERR_CNT);
105 writew(0x0000, port + PORT_CRC_ERR_CNT);
106 writew(0x0000, port + PORT_HSHK_ERR_CNT);
108 /* always use 64bit activation */
109 writel(PORT_CS_32BIT_ACTV, port + PORT_CTRL_CLR);
111 /* clear port multiplier enable and resume bits */
112 writel(PORT_CS_PMP_EN | PORT_CS_PMP_RESUME, port + PORT_CTRL_CLR);
115 static int sil_init_port(void *port)
119 writel(PORT_CS_INIT, port + PORT_CTRL_STAT);
120 ata_wait_register(port + PORT_CTRL_STAT,
121 PORT_CS_INIT, PORT_CS_INIT, 100);
122 tmp = ata_wait_register(port + PORT_CTRL_STAT,
123 PORT_CS_RDY, 0, 100);
125 if ((tmp & (PORT_CS_INIT | PORT_CS_RDY)) != PORT_CS_RDY)
131 static void sil_read_fis(struct sil_sata *sata, int tag,
132 struct sata_fis_d2h *fis)
134 void *port = sata->port;
139 prb = port + PORT_LRAM + tag * PORT_LRAM_SLOT_SZ;
140 src = (u32 *)&prb->fis;
142 for (i = 0; i < sizeof(struct sata_fis_h2d); i += 4)
143 *dst++ = readl(src++);
146 static int sil_exec_cmd(struct sil_sata *sata, struct sil_cmd_block *pcmd,
149 void *port = sata->port;
150 u64 paddr = virt_to_bus(sata->devno, pcmd);
151 u32 irq_mask, irq_stat;
154 writel(PORT_IRQ_COMPLETE | PORT_IRQ_ERROR, port + PORT_IRQ_ENABLE_CLR);
156 /* better to add momery barrior here */
157 writel((u32)paddr, port + PORT_CMD_ACTIVATE + tag * 8);
158 writel((u64)paddr >> 32, port + PORT_CMD_ACTIVATE + tag * 8 + 4);
160 irq_mask = (PORT_IRQ_COMPLETE | PORT_IRQ_ERROR) << PORT_IRQ_RAW_SHIFT;
161 irq_stat = ata_wait_register(port + PORT_IRQ_STAT, irq_mask,
165 writel(irq_mask, port + PORT_IRQ_STAT);
166 irq_stat >>= PORT_IRQ_RAW_SHIFT;
168 if (irq_stat & PORT_IRQ_COMPLETE)
171 /* force port into known state */
173 if (irq_stat & PORT_IRQ_ERROR)
182 static int sil_cmd_set_feature(struct sil_sata *sata)
184 struct sil_cmd_block cmdb, *pcmd = &cmdb;
185 struct sata_fis_d2h fis;
189 memset((void *)&cmdb, 0, sizeof(struct sil_cmd_block));
190 pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
191 pcmd->prb.fis.pm_port_c = (1 << 7);
192 pcmd->prb.fis.command = ATA_CMD_SET_FEATURES;
193 pcmd->prb.fis.features = SETFEATURES_XFER;
195 /* First check the device capablity */
196 udma_cap = (u8)(sata->udma & 0xff);
197 debug("udma_cap %02x\n", udma_cap);
199 if (udma_cap == ATA_UDMA6)
200 pcmd->prb.fis.sector_count = XFER_UDMA_6;
201 if (udma_cap == ATA_UDMA5)
202 pcmd->prb.fis.sector_count = XFER_UDMA_5;
203 if (udma_cap == ATA_UDMA4)
204 pcmd->prb.fis.sector_count = XFER_UDMA_4;
205 if (udma_cap == ATA_UDMA3)
206 pcmd->prb.fis.sector_count = XFER_UDMA_3;
208 ret = sil_exec_cmd(sata, pcmd, 0);
210 sil_read_fis(sata, 0, &fis);
211 printf("Err: exe cmd(0x%x).\n",
212 readl(sata->port + PORT_SERROR));
213 sil_sata_dump_fis(&fis);
220 static void sil_sata_init_wcache(struct sil_sata *sata, u16 *id)
222 if (ata_id_has_wcache(id) && ata_id_wcache_enabled(id))
224 if (ata_id_has_flush(id))
226 if (ata_id_has_flush_ext(id))
230 static void sil_sata_set_feature_by_id(struct sil_sata *sata, u16 *id)
233 /* Check if support LBA48 */
234 if (ata_id_has_lba48(id)) {
236 debug("Device supports LBA48\n");
238 debug("Device supports LBA28\n");
242 sil_sata_init_wcache(sata, id);
243 sil_cmd_set_feature(sata);
246 static int sil_cmd_identify_device(struct sil_sata *sata, u16 *id)
248 struct sil_cmd_block cmdb, *pcmd = &cmdb;
249 struct sata_fis_d2h fis;
252 memset((void *)&cmdb, 0, sizeof(struct sil_cmd_block));
253 pcmd->prb.ctrl = cpu_to_le16(PRB_CTRL_PROTOCOL);
254 pcmd->prb.prot = cpu_to_le16(PRB_PROT_READ);
255 pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
256 pcmd->prb.fis.pm_port_c = (1 << 7);
257 pcmd->prb.fis.command = ATA_CMD_ID_ATA;
258 pcmd->sge.addr = cpu_to_le64(virt_to_bus(sata->devno, id));
259 pcmd->sge.cnt = cpu_to_le32(sizeof(id[0]) * ATA_ID_WORDS);
260 pcmd->sge.flags = cpu_to_le32(SGE_TRM);
262 ret = sil_exec_cmd(sata, pcmd, 0);
264 sil_read_fis(sata, 0, &fis);
265 printf("Err: id cmd(0x%x).\n", readl(sata->port + PORT_SERROR));
266 sil_sata_dump_fis(&fis);
269 ata_swap_buf_le16(id, ATA_ID_WORDS);
274 static int sil_cmd_soft_reset(struct sil_sata *sata)
276 struct sil_cmd_block cmdb, *pcmd = &cmdb;
277 struct sata_fis_d2h fis;
278 void *port = sata->port;
281 /* put the port into known state */
282 if (sil_init_port(port)) {
283 printf("SRST: port %d not ready\n", sata->id);
287 memset((void *)&cmdb, 0, sizeof(struct sil_cmd_block));
289 pcmd->prb.ctrl = cpu_to_le16(PRB_CTRL_SRST);
290 pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
291 pcmd->prb.fis.pm_port_c = 0xf;
293 ret = sil_exec_cmd(sata, &cmdb, 0);
295 sil_read_fis(sata, 0, &fis);
296 printf("SRST cmd error.\n");
297 sil_sata_dump_fis(&fis);
304 static ulong sil_sata_rw_cmd(struct sil_sata *sata, ulong start, ulong blkcnt,
305 u8 *buffer, int is_write)
307 struct sil_cmd_block cmdb, *pcmd = &cmdb;
308 struct sata_fis_d2h fis;
313 memset(pcmd, 0, sizeof(struct sil_cmd_block));
314 pcmd->prb.ctrl = cpu_to_le16(PRB_CTRL_PROTOCOL);
315 pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
316 pcmd->prb.fis.pm_port_c = (1 << 7);
318 pcmd->prb.fis.command = ATA_CMD_WRITE;
319 pcmd->prb.prot = cpu_to_le16(PRB_PROT_WRITE);
321 pcmd->prb.fis.command = ATA_CMD_READ;
322 pcmd->prb.prot = cpu_to_le16(PRB_PROT_READ);
325 pcmd->prb.fis.device = ATA_LBA;
326 pcmd->prb.fis.device |= (block >> 24) & 0xf;
327 pcmd->prb.fis.lba_high = (block >> 16) & 0xff;
328 pcmd->prb.fis.lba_mid = (block >> 8) & 0xff;
329 pcmd->prb.fis.lba_low = block & 0xff;
330 pcmd->prb.fis.sector_count = (u8)blkcnt & 0xff;
332 pcmd->sge.addr = cpu_to_le64(virt_to_bus(sata->devno, buffer));
333 pcmd->sge.cnt = cpu_to_le32(blkcnt * ATA_SECT_SIZE);
334 pcmd->sge.flags = cpu_to_le32(SGE_TRM);
336 ret = sil_exec_cmd(sata, pcmd, 0);
338 sil_read_fis(sata, 0, &fis);
339 printf("Err: rw cmd(0x%08x).\n",
340 readl(sata->port + PORT_SERROR));
341 sil_sata_dump_fis(&fis);
348 static ulong sil_sata_rw_cmd_ext(struct sil_sata *sata, ulong start,
349 ulong blkcnt, u8 *buffer, int is_write)
351 struct sil_cmd_block cmdb, *pcmd = &cmdb;
352 struct sata_fis_d2h fis;
357 memset(pcmd, 0, sizeof(struct sil_cmd_block));
358 pcmd->prb.ctrl = cpu_to_le16(PRB_CTRL_PROTOCOL);
359 pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
360 pcmd->prb.fis.pm_port_c = (1 << 7);
362 pcmd->prb.fis.command = ATA_CMD_WRITE_EXT;
363 pcmd->prb.prot = cpu_to_le16(PRB_PROT_WRITE);
365 pcmd->prb.fis.command = ATA_CMD_READ_EXT;
366 pcmd->prb.prot = cpu_to_le16(PRB_PROT_READ);
369 pcmd->prb.fis.lba_high_exp = (block >> 40) & 0xff;
370 pcmd->prb.fis.lba_mid_exp = (block >> 32) & 0xff;
371 pcmd->prb.fis.lba_low_exp = (block >> 24) & 0xff;
372 pcmd->prb.fis.lba_high = (block >> 16) & 0xff;
373 pcmd->prb.fis.lba_mid = (block >> 8) & 0xff;
374 pcmd->prb.fis.lba_low = block & 0xff;
375 pcmd->prb.fis.device = ATA_LBA;
376 pcmd->prb.fis.sector_count_exp = (blkcnt >> 8) & 0xff;
377 pcmd->prb.fis.sector_count = blkcnt & 0xff;
379 pcmd->sge.addr = cpu_to_le64(virt_to_bus(sata->devno, buffer));
380 pcmd->sge.cnt = cpu_to_le32(blkcnt * ATA_SECT_SIZE);
381 pcmd->sge.flags = cpu_to_le32(SGE_TRM);
383 ret = sil_exec_cmd(sata, pcmd, 0);
385 sil_read_fis(sata, 0, &fis);
386 printf("Err: rw ext cmd(0x%08x).\n",
387 readl(sata->port + PORT_SERROR));
388 sil_sata_dump_fis(&fis);
395 static ulong sil_sata_rw_lba28(struct sil_sata *sata, ulong blknr,
396 lbaint_t blkcnt, const void *buffer,
399 ulong start, blks, max_blks;
406 max_blks = ATA_MAX_SECTORS;
408 if (blks > max_blks) {
409 sil_sata_rw_cmd(sata, start, max_blks, addr, is_write);
412 addr += ATA_SECT_SIZE * max_blks;
414 sil_sata_rw_cmd(sata, start, blks, addr, is_write);
417 addr += ATA_SECT_SIZE * blks;
424 static ulong sil_sata_rw_lba48(struct sil_sata *sata, ulong blknr,
425 lbaint_t blkcnt, const void *buffer,
428 ulong start, blks, max_blks;
435 max_blks = ATA_MAX_SECTORS_LBA48;
437 if (blks > max_blks) {
438 sil_sata_rw_cmd_ext(sata, start, max_blks,
442 addr += ATA_SECT_SIZE * max_blks;
444 sil_sata_rw_cmd_ext(sata, start, blks,
448 addr += ATA_SECT_SIZE * blks;
455 static void sil_sata_cmd_flush_cache(struct sil_sata *sata)
457 struct sil_cmd_block cmdb, *pcmd = &cmdb;
459 memset((void *)pcmd, 0, sizeof(struct sil_cmd_block));
460 pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
461 pcmd->prb.fis.pm_port_c = (1 << 7);
462 pcmd->prb.fis.command = ATA_CMD_FLUSH;
464 sil_exec_cmd(sata, pcmd, 0);
467 static void sil_sata_cmd_flush_cache_ext(struct sil_sata *sata)
469 struct sil_cmd_block cmdb, *pcmd = &cmdb;
471 memset((void *)pcmd, 0, sizeof(struct sil_cmd_block));
472 pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
473 pcmd->prb.fis.pm_port_c = (1 << 7);
474 pcmd->prb.fis.command = ATA_CMD_FLUSH_EXT;
476 sil_exec_cmd(sata, pcmd, 0);
480 * SATA interface between low level driver and command layer
482 #if !CONFIG_IS_ENABLED(BLK)
483 ulong sata_read(int dev, ulong blknr, lbaint_t blkcnt, void *buffer)
485 struct sil_sata *sata = (struct sil_sata *)sata_dev_desc[dev].priv;
487 static ulong sata_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
490 struct sil_sata_priv *priv = dev_get_platdata(dev);
491 int port_number = priv->port_num;
492 struct sil_sata *sata = priv->sil_sata_desc[port_number];
497 rc = sil_sata_rw_lba48(sata, blknr, blkcnt, buffer, READ_CMD);
499 rc = sil_sata_rw_lba28(sata, blknr, blkcnt, buffer, READ_CMD);
505 * SATA interface between low level driver and command layer
507 #if !CONFIG_IS_ENABLED(BLK)
508 ulong sata_write(int dev, ulong blknr, lbaint_t blkcnt, const void *buffer)
510 struct sil_sata *sata = (struct sil_sata *)sata_dev_desc[dev].priv;
512 ulong sata_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
515 struct sil_sata_priv *priv = dev_get_platdata(dev);
516 int port_number = priv->port_num;
517 struct sil_sata *sata = priv->sil_sata_desc[port_number];
522 rc = sil_sata_rw_lba48(sata, blknr, blkcnt, buffer, WRITE_CMD);
523 if (sata->wcache && sata->flush_ext)
524 sil_sata_cmd_flush_cache_ext(sata);
526 rc = sil_sata_rw_lba28(sata, blknr, blkcnt, buffer, WRITE_CMD);
527 if (sata->wcache && sata->flush)
528 sil_sata_cmd_flush_cache(sata);
534 #if !CONFIG_IS_ENABLED(BLK)
535 static int sil_init_sata(int dev)
538 static int sil_init_sata(struct udevice *uc_dev, int dev)
540 struct sil_sata_priv *priv = dev_get_platdata(uc_dev);
542 struct sil_sata *sata;
547 printf("SATA#%d:\n", dev);
549 port = (void *)sata_info.iobase[1] +
550 PORT_REGS_SIZE * (dev - sata_info.portbase);
552 /* Initial PHY setting */
553 writel(0x20c, port + PORT_PHY_CFG);
556 tmp = readl(port + PORT_CTRL_STAT);
557 if (tmp & PORT_CS_PORT_RST) {
558 writel(PORT_CS_PORT_RST, port + PORT_CTRL_CLR);
559 tmp = ata_wait_register(port + PORT_CTRL_STAT,
560 PORT_CS_PORT_RST, PORT_CS_PORT_RST, 100);
561 if (tmp & PORT_CS_PORT_RST)
562 printf("Err: Failed to clear port RST\n");
565 /* Check if device is present */
566 for (cnt = 0; cnt < 100; cnt++) {
567 tmp = readl(port + PORT_SSTATUS);
568 if ((tmp & 0xF) == 0x3)
573 tmp = readl(port + PORT_SSTATUS);
574 if ((tmp & 0xf) != 0x3) {
575 printf(" (No RDY)\n");
579 /* Wait for port ready */
580 tmp = ata_wait_register(port + PORT_CTRL_STAT,
581 PORT_CS_RDY, PORT_CS_RDY, 100);
582 if ((tmp & PORT_CS_RDY) != PORT_CS_RDY) {
583 printf("%d port not ready.\n", dev);
588 sil_config_port(port);
591 writel(PORT_CS_DEV_RST, port + PORT_CTRL_STAT);
592 readl(port + PORT_CTRL_STAT);
593 tmp = ata_wait_register(port + PORT_CTRL_STAT, PORT_CS_DEV_RST,
594 PORT_CS_DEV_RST, 100);
595 if (tmp & PORT_CS_DEV_RST) {
596 printf("%d port reset failed.\n", dev);
600 sata = (struct sil_sata *)malloc(sizeof(struct sil_sata));
602 printf("%d no memory.\n", dev);
605 memset((void *)sata, 0, sizeof(struct sil_sata));
607 /* Save the private struct to block device struct */
608 #if !CONFIG_IS_ENABLED(BLK)
609 sata_dev_desc[dev].priv = (void *)sata;
611 priv->sil_sata_desc[dev] = sata;
612 priv->port_num = dev;
616 sata->devno = sata_info.devno;
617 sprintf(sata->name, "SATA#%d", dev);
618 sil_cmd_soft_reset(sata);
619 tmp = readl(port + PORT_SSTATUS);
620 tmp = (tmp >> 4) & 0xf;
621 printf(" (%s)\n", sata_spd_string(tmp));
626 #if !CONFIG_IS_ENABLED(BLK)
628 * SATA interface between low level driver and command layer
630 int init_sata(int dev)
632 static int init_done, idx;
636 if (init_done == 1 && dev < sata_info.maxport)
641 /* Find PCI device(s) */
642 devno = pci_find_devices(supported, idx++);
646 pci_read_config_word(devno, PCI_DEVICE_ID, &word);
648 /* get the port count */
651 sata_info.portbase = 0;
652 sata_info.maxport = sata_info.portbase + word;
653 sata_info.devno = devno;
655 /* Read out all BARs */
656 sata_info.iobase[0] = (ulong)pci_map_bar(devno,
657 PCI_BASE_ADDRESS_0, PCI_REGION_MEM);
658 sata_info.iobase[1] = (ulong)pci_map_bar(devno,
659 PCI_BASE_ADDRESS_2, PCI_REGION_MEM);
661 /* mask out the unused bits */
662 sata_info.iobase[0] &= 0xffffff80;
663 sata_info.iobase[1] &= 0xfffffc00;
665 /* Enable Bus Mastering and memory region */
666 pci_write_config_word(devno, PCI_COMMAND,
667 PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
669 /* Check if mem accesses and Bus Mastering are enabled. */
670 pci_read_config_word(devno, PCI_COMMAND, &word);
671 if (!(word & PCI_COMMAND_MEMORY) ||
672 (!(word & PCI_COMMAND_MASTER))) {
673 printf("Error: Can not enable MEM access or Bus Mastering.\n");
674 debug("PCI command: %04x\n", word);
679 writel(0, (void *)(sata_info.iobase[0] + HOST_FLASH_CMD));
680 /* clear global reset & mask interrupts during initialization */
681 writel(0, (void *)(sata_info.iobase[0] + HOST_CTRL));
684 return sil_init_sata(dev);
687 int reset_sata(int dev)
693 * SATA interface between low level driver and command layer
695 int scan_sata(int dev)
697 struct sil_sata *sata = (struct sil_sata *)sata_dev_desc[dev].priv;
699 static int scan_sata(struct udevice *blk_dev, int dev)
701 struct blk_desc *desc = dev_get_uclass_platdata(blk_dev);
702 struct sil_sata_priv *priv = dev_get_platdata(blk_dev);
703 struct sil_sata *sata = priv->sil_sata_desc[dev];
705 unsigned char serial[ATA_ID_SERNO_LEN + 1];
706 unsigned char firmware[ATA_ID_FW_REV_LEN + 1];
707 unsigned char product[ATA_ID_PROD_LEN + 1];
710 id = (u16 *)malloc(ATA_ID_WORDS * 2);
712 printf("Id malloc failed\n");
715 sil_cmd_identify_device(sata, id);
717 sil_sata_set_feature_by_id(sata, id);
720 ata_id_c_string(id, serial, ATA_ID_SERNO, sizeof(serial));
722 /* Firmware version */
723 ata_id_c_string(id, firmware, ATA_ID_FW_REV, sizeof(firmware));
726 ata_id_c_string(id, product, ATA_ID_PROD, sizeof(product));
728 #if !CONFIG_IS_ENABLED(BLK)
729 memcpy(sata_dev_desc[dev].product, serial, sizeof(serial));
730 memcpy(sata_dev_desc[dev].revision, firmware, sizeof(firmware));
731 memcpy(sata_dev_desc[dev].vendor, product, sizeof(product));
733 sata_dev_desc[dev].lba = ata_id_n_sectors(id);
735 sata_dev_desc[dev].lba48 = sata->lba48;
738 memcpy(desc->product, serial, sizeof(serial));
739 memcpy(desc->revision, firmware, sizeof(firmware));
740 memcpy(desc->vendor, product, sizeof(product));
741 desc->lba = ata_id_n_sectors(id);
743 desc->lba48 = sata->lba48;
755 #if CONFIG_IS_ENABLED(BLK)
756 static const struct blk_ops sata_sil_blk_ops = {
761 U_BOOT_DRIVER(sata_sil_driver) = {
762 .name = "sata_sil_blk",
764 .ops = &sata_sil_blk_ops,
765 .platdata_auto_alloc_size = sizeof(struct sil_sata_priv),
768 static int sil_unbind_device(struct udevice *dev)
772 ret = device_remove(dev, DM_REMOVE_NORMAL);
776 ret = device_unbind(dev);
783 static int sil_pci_probe(struct udevice *dev)
795 /* Get PCI device number */
796 devno = dm_pci_get_bdf(dev);
800 dm_pci_read_config16(dev, PCI_DEVICE_ID, &word);
802 /* get the port count */
805 sata_info.portbase = 0;
806 sata_info.maxport = sata_info.portbase + word;
807 sata_info.devno = devno;
809 /* Read out all BARs */
810 sata_info.iobase[0] = (ulong)dm_pci_map_bar(dev,
811 PCI_BASE_ADDRESS_0, PCI_REGION_MEM);
812 sata_info.iobase[1] = (ulong)dm_pci_map_bar(dev,
813 PCI_BASE_ADDRESS_2, PCI_REGION_MEM);
815 /* mask out the unused bits */
816 sata_info.iobase[0] &= 0xffffff80;
817 sata_info.iobase[1] &= 0xfffffc00;
819 /* Enable Bus Mastering and memory region */
820 dm_pci_write_config16(dev, PCI_COMMAND,
821 PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
823 /* Check if mem accesses and Bus Mastering are enabled. */
824 dm_pci_read_config16(dev, PCI_COMMAND, &word);
825 if (!(word & PCI_COMMAND_MEMORY) ||
826 (!(word & PCI_COMMAND_MASTER))) {
827 printf("Error: Can not enable MEM access or Bus Mastering.\n");
828 debug("PCI command: %04x\n", word);
833 writel(0, (void *)(sata_info.iobase[0] + HOST_FLASH_CMD));
834 /* clear global reset & mask interrupts during initialization */
835 writel(0, (void *)(sata_info.iobase[0] + HOST_CTRL));
837 for (i = sata_info.portbase; i < sata_info.maxport; i++) {
838 snprintf(sata_name, sizeof(sata_name), "sil_sata%d", i);
839 ret = blk_create_devicef(dev, "sata_sil_blk", sata_name,
840 IF_TYPE_SATA, -1, 512, 0, &blk);
842 debug("Can't create device\n");
846 ret = sil_init_sata(blk, i);
848 ret = sil_unbind_device(blk);
856 ret = scan_sata(blk, i);
858 ret = sil_unbind_device(blk);
867 if (failed_number == sata_info.maxport)
873 static int sil_pci_remove(struct udevice *dev)
876 struct sil_sata *sata;
877 struct sil_sata_priv *priv;
879 priv = dev_get_priv(dev);
881 for (i = sata_info.portbase; i < sata_info.maxport; i++) {
882 sata = priv->sil_sata_desc[i];
890 static int sata_sil_scan(struct udevice *dev)
892 /* Nothing to do here */
897 struct sil_ops sata_sil_ops = {
898 .scan = sata_sil_scan,
901 static const struct udevice_id sil_pci_ids[] = {
902 { .compatible = "sil-pci-sample" },
906 U_BOOT_DRIVER(sil_ahci_pci) = {
907 .name = "sil_ahci_pci",
909 .of_match = sil_pci_ids,
910 .ops = &sata_sil_ops,
911 .probe = sil_pci_probe,
912 .remove = sil_pci_remove,
913 .priv_auto_alloc_size = sizeof(struct sil_sata_priv),
916 U_BOOT_PCI_DEVICE(sil_ahci_pci, supported);