ahci.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Copyright 2012 Calxeda, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 2 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <common.h>
  18. #include <ahci.h>
  19. #include <asm/io.h>
  20. #define CPHY_MAP(dev, addr) ((((dev) & 0x1f) << 7) | (((addr) >> 9) & 0x7f))
  21. #define CPHY_ADDR(base, dev, addr) ((base) | (((addr) & 0x1ff) << 2))
  22. #define CPHY_BASE 0xfff58000
  23. #define CPHY_WIDTH 0x1000
  24. #define CPHY_DTE_XS 5
  25. #define CPHY_MII 31
  26. #define SERDES_CR_CTL 0x80a0
  27. #define SERDES_CR_ADDR 0x80a1
  28. #define SERDES_CR_DATA 0x80a2
  29. #define CR_BUSY 0x0001
  30. #define CR_START 0x0001
  31. #define CR_WR_RDN 0x0002
  32. #define CPHY_TX_INPUT_STS 0x2001
  33. #define CPHY_RX_INPUT_STS 0x2002
  34. #define CPHY_SATA_TX_OVERRIDE_BIT 0x8000
  35. #define CPHY_SATA_RX_OVERRIDE_BIT 0x4000
  36. #define CPHY_TX_INPUT_OVERRIDE 0x2004
  37. #define CPHY_RX_INPUT_OVERRIDE 0x2005
  38. #define SPHY_LANE 0x100
  39. #define SPHY_HALF_RATE 0x0001
  40. #define CPHY_SATA_DPLL_MODE 0x0700
  41. #define CPHY_SATA_DPLL_SHIFT 8
  42. #define CPHY_SATA_TX_ATTEN 0x1c00
  43. #define CPHY_SATA_TX_ATTEN_SHIFT 10
  44. #define HB_SREG_SATA_ATTEN 0xfff3cf24
  45. #define SATA_PORT_BASE 0xffe08000
  46. #define SATA_VERSIONR 0xf8
  47. #define SATA_HB_VERSION 0x3332302a
  48. static u32 __combo_phy_reg_read(u8 phy, u8 dev, u32 addr)
  49. {
  50. u32 data;
  51. writel(CPHY_MAP(dev, addr), CPHY_BASE + 0x800 + CPHY_WIDTH * phy);
  52. data = readl(CPHY_ADDR(CPHY_BASE + CPHY_WIDTH * phy, dev, addr));
  53. return data;
  54. }
  55. static void __combo_phy_reg_write(u8 phy, u8 dev, u32 addr, u32 data)
  56. {
  57. writel(CPHY_MAP(dev, addr), CPHY_BASE + 0x800 + CPHY_WIDTH * phy);
  58. writel(data, CPHY_ADDR(CPHY_BASE + CPHY_WIDTH * phy, dev, addr));
  59. }
  60. static u32 combo_phy_read(u8 phy, u32 addr)
  61. {
  62. u8 dev = CPHY_DTE_XS;
  63. if (phy == 5)
  64. dev = CPHY_MII;
  65. while (__combo_phy_reg_read(phy, dev, SERDES_CR_CTL) & CR_BUSY)
  66. udelay(5);
  67. __combo_phy_reg_write(phy, dev, SERDES_CR_ADDR, addr);
  68. __combo_phy_reg_write(phy, dev, SERDES_CR_CTL, CR_START);
  69. while (__combo_phy_reg_read(phy, dev, SERDES_CR_CTL) & CR_BUSY)
  70. udelay(5);
  71. return __combo_phy_reg_read(phy, dev, SERDES_CR_DATA);
  72. }
  73. static void combo_phy_write(u8 phy, u32 addr, u32 data)
  74. {
  75. u8 dev = CPHY_DTE_XS;
  76. if (phy == 5)
  77. dev = CPHY_MII;
  78. while (__combo_phy_reg_read(phy, dev, SERDES_CR_CTL) & CR_BUSY)
  79. udelay(5);
  80. __combo_phy_reg_write(phy, dev, SERDES_CR_ADDR, addr);
  81. __combo_phy_reg_write(phy, dev, SERDES_CR_DATA, data);
  82. __combo_phy_reg_write(phy, dev, SERDES_CR_CTL, CR_WR_RDN | CR_START);
  83. }
  84. static void cphy_spread_spectrum_override(u8 phy, u8 lane, u32 val)
  85. {
  86. u32 tmp;
  87. tmp = combo_phy_read(phy, CPHY_RX_INPUT_STS + lane * SPHY_LANE);
  88. tmp &= ~CPHY_SATA_RX_OVERRIDE_BIT;
  89. combo_phy_write(phy, CPHY_RX_INPUT_OVERRIDE + lane * SPHY_LANE, tmp);
  90. tmp |= CPHY_SATA_RX_OVERRIDE_BIT;
  91. combo_phy_write(phy, CPHY_RX_INPUT_OVERRIDE + lane * SPHY_LANE, tmp);
  92. tmp &= ~CPHY_SATA_DPLL_MODE;
  93. tmp |= (val << CPHY_SATA_DPLL_SHIFT) & CPHY_SATA_DPLL_MODE;
  94. combo_phy_write(phy, CPHY_RX_INPUT_OVERRIDE + lane * SPHY_LANE, tmp);
  95. }
  96. static void cphy_tx_attenuation_override(u8 phy, u8 lane)
  97. {
  98. u32 val;
  99. u32 tmp;
  100. u8 shift;
  101. shift = ((phy == 5) ? 4 : lane) * 4;
  102. val = (readl(HB_SREG_SATA_ATTEN) >> shift) & 0xf;
  103. if (val & 0x8)
  104. return;
  105. tmp = combo_phy_read(phy, CPHY_TX_INPUT_STS + lane * SPHY_LANE);
  106. tmp &= ~CPHY_SATA_TX_OVERRIDE_BIT;
  107. combo_phy_write(phy, CPHY_TX_INPUT_OVERRIDE + lane * SPHY_LANE, tmp);
  108. tmp |= CPHY_SATA_TX_OVERRIDE_BIT;
  109. combo_phy_write(phy, CPHY_TX_INPUT_OVERRIDE + lane * SPHY_LANE, tmp);
  110. tmp |= (val << CPHY_SATA_TX_ATTEN_SHIFT) & CPHY_SATA_TX_ATTEN;
  111. combo_phy_write(phy, CPHY_TX_INPUT_OVERRIDE + lane * SPHY_LANE, tmp);
  112. }
  113. static void cphy_disable_port_overrides(u8 port)
  114. {
  115. u32 tmp;
  116. u8 lane = 0, phy = 0;
  117. if (port == 0)
  118. phy = 5;
  119. else if (port < 5)
  120. lane = port - 1;
  121. else
  122. return;
  123. tmp = combo_phy_read(phy, CPHY_RX_INPUT_STS + lane * SPHY_LANE);
  124. tmp &= ~CPHY_SATA_RX_OVERRIDE_BIT;
  125. combo_phy_write(phy, CPHY_RX_INPUT_OVERRIDE + lane * SPHY_LANE, tmp);
  126. tmp = combo_phy_read(phy, CPHY_TX_INPUT_OVERRIDE + lane * SPHY_LANE);
  127. tmp &= ~CPHY_SATA_TX_OVERRIDE_BIT;
  128. combo_phy_write(phy, CPHY_TX_INPUT_OVERRIDE + lane * SPHY_LANE, tmp);
  129. }
  130. void cphy_disable_overrides(void)
  131. {
  132. int i;
  133. u32 port_map;
  134. port_map = readl(0xffe08000 + HOST_PORTS_IMPL);
  135. for (i = 0; i < 5; i++) {
  136. if (port_map & (1 << i))
  137. cphy_disable_port_overrides(i);
  138. }
  139. }
  140. static void cphy_override_lane(u8 port)
  141. {
  142. u32 tmp, k = 0;
  143. u8 lane = 0, phy = 0;
  144. if (port == 0)
  145. phy = 5;
  146. else if (port < 5)
  147. lane = port - 1;
  148. else
  149. return;
  150. do {
  151. tmp = combo_phy_read(0, CPHY_RX_INPUT_STS +
  152. lane * SPHY_LANE);
  153. } while ((tmp & SPHY_HALF_RATE) && (k++ < 1000));
  154. cphy_spread_spectrum_override(phy, lane, 3);
  155. cphy_tx_attenuation_override(phy, lane);
  156. }
  157. #define WAIT_MS_LINKUP 4
  158. int ahci_link_up(struct ahci_probe_ent *probe_ent, int port)
  159. {
  160. u32 tmp;
  161. int j = 0;
  162. u8 *port_mmio = (u8 *)probe_ent->port[port].port_mmio;
  163. u32 is_highbank = readl(SATA_PORT_BASE + SATA_VERSIONR) ==
  164. SATA_HB_VERSION ? 1 : 0;
  165. /* Bring up SATA link.
  166. * SATA link bringup time is usually less than 1 ms; only very
  167. * rarely has it taken between 1-2 ms. Never seen it above 2 ms.
  168. */
  169. while (j < WAIT_MS_LINKUP) {
  170. if (is_highbank && (j == 0)) {
  171. cphy_disable_port_overrides(port);
  172. writel(0x301, port_mmio + PORT_SCR_CTL);
  173. udelay(1000);
  174. writel(0x300, port_mmio + PORT_SCR_CTL);
  175. udelay(1000);
  176. cphy_override_lane(port);
  177. }
  178. tmp = readl(port_mmio + PORT_SCR_STAT);
  179. if ((tmp & 0xf) == 0x3)
  180. return 0;
  181. udelay(1000);
  182. j++;
  183. if ((j == WAIT_MS_LINKUP) && (tmp & 0xf))
  184. j = 0; /* retry phy reset */
  185. }
  186. return 1;
  187. }