ahci.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #include <common.h>
  2. #include <ahci.h>
  3. #include <dm.h>
  4. #include <scsi.h>
  5. #include <errno.h>
  6. #include <asm/io.h>
  7. #include <asm/gpio.h>
  8. #define AHCI_PHYCS0R 0x00c0
  9. #define AHCI_PHYCS1R 0x00c4
  10. #define AHCI_PHYCS2R 0x00c8
  11. #define AHCI_RWCR 0x00fc
  12. /* This magic PHY initialisation was taken from the Allwinner releases
  13. * and Linux driver, but is completely undocumented.
  14. */
  15. static int sunxi_ahci_phy_init(u8 *reg_base)
  16. {
  17. u32 reg_val;
  18. int timeout;
  19. writel(0, reg_base + AHCI_RWCR);
  20. mdelay(5);
  21. setbits_le32(reg_base + AHCI_PHYCS1R, 0x1 << 19);
  22. clrsetbits_le32(reg_base + AHCI_PHYCS0R,
  23. (0x7 << 24),
  24. (0x5 << 24) | (0x1 << 23) | (0x1 << 18));
  25. clrsetbits_le32(reg_base + AHCI_PHYCS1R,
  26. (0x3 << 16) | (0x1f << 8) | (0x3 << 6),
  27. (0x2 << 16) | (0x6 << 8) | (0x2 << 6));
  28. setbits_le32(reg_base + AHCI_PHYCS1R, (0x1 << 28) | (0x1 << 15));
  29. clrbits_le32(reg_base + AHCI_PHYCS1R, (0x1 << 19));
  30. clrsetbits_le32(reg_base + AHCI_PHYCS0R, (0x7 << 20), (0x3 << 20));
  31. clrsetbits_le32(reg_base + AHCI_PHYCS2R, (0x1f << 5), (0x19 << 5));
  32. mdelay(5);
  33. setbits_le32(reg_base + AHCI_PHYCS0R, (0x1 << 19));
  34. timeout = 250; /* Power up takes approx 50 us */
  35. for (;;) {
  36. reg_val = readl(reg_base + AHCI_PHYCS0R) & (0x7 << 28);
  37. if (reg_val == (0x2 << 28))
  38. break;
  39. if (--timeout == 0) {
  40. printf("AHCI PHY power up failed.\n");
  41. return -EIO;
  42. }
  43. udelay(1);
  44. };
  45. setbits_le32(reg_base + AHCI_PHYCS2R, (0x1 << 24));
  46. timeout = 100; /* Calibration takes approx 10 us */
  47. for (;;) {
  48. reg_val = readl(reg_base + AHCI_PHYCS2R) & (0x1 << 24);
  49. if (reg_val == 0x0)
  50. break;
  51. if (--timeout == 0) {
  52. printf("AHCI PHY calibration failed.\n");
  53. return -EIO;
  54. }
  55. udelay(1);
  56. }
  57. mdelay(15);
  58. writel(0x7, reg_base + AHCI_RWCR);
  59. return 0;
  60. }
  61. #ifndef CONFIG_DM_SCSI
  62. void scsi_init(void)
  63. {
  64. if (sunxi_ahci_phy_init((u8 *)SUNXI_SATA_BASE) < 0)
  65. return;
  66. ahci_init((void __iomem *)SUNXI_SATA_BASE);
  67. }
  68. #else
  69. static int sunxi_sata_probe(struct udevice *dev)
  70. {
  71. ulong base;
  72. u8 *reg;
  73. int ret;
  74. base = dev_read_addr(dev);
  75. if (base == FDT_ADDR_T_NONE) {
  76. debug("%s: Failed to find address (err=%d\n)", __func__, ret);
  77. return -EINVAL;
  78. }
  79. reg = (u8 *)base;
  80. ret = sunxi_ahci_phy_init(reg);
  81. if (ret) {
  82. debug("%s: Failed to init phy (err=%d\n)", __func__, ret);
  83. return ret;
  84. }
  85. ret = ahci_probe_scsi(dev, base);
  86. if (ret) {
  87. debug("%s: Failed to probe (err=%d\n)", __func__, ret);
  88. return ret;
  89. }
  90. return 0;
  91. }
  92. static int sunxi_sata_bind(struct udevice *dev)
  93. {
  94. struct udevice *scsi_dev;
  95. int ret;
  96. ret = ahci_bind_scsi(dev, &scsi_dev);
  97. if (ret) {
  98. debug("%s: Failed to bind (err=%d\n)", __func__, ret);
  99. return ret;
  100. }
  101. return 0;
  102. }
  103. static const struct udevice_id sunxi_ahci_ids[] = {
  104. { .compatible = "allwinner,sun4i-a10-ahci" },
  105. { }
  106. };
  107. U_BOOT_DRIVER(ahci_sunxi_drv) = {
  108. .name = "ahci_sunxi",
  109. .id = UCLASS_AHCI,
  110. .of_match = sunxi_ahci_ids,
  111. .bind = sunxi_sata_bind,
  112. .probe = sunxi_sata_probe,
  113. };
  114. #endif