phy-comphy-3700.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  1. /*
  2. * Copyright (C) 2018-2021 Marvell International Ltd.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. * https://spdx.org/licenses
  6. */
  7. #include <errno.h>
  8. #include <common/debug.h>
  9. #include <drivers/delay_timer.h>
  10. #include <lib/mmio.h>
  11. #include <lib/spinlock.h>
  12. #include <mvebu.h>
  13. #include <mvebu_def.h>
  14. #include <plat_marvell.h>
  15. #include "phy-comphy-3700.h"
  16. #include "phy-comphy-common.h"
  17. /*
  18. * COMPHY_INDIRECT_REG points to ahci address space but the ahci region used in
  19. * Linux is up to 0x178 so none will access it from Linux in runtime
  20. * concurrently.
  21. */
  22. #define COMPHY_INDIRECT_REG (MVEBU_REGS_BASE + 0xE0178)
  23. /* The USB3_GBE1_PHY range is above USB3 registers used in dts */
  24. #define USB3_GBE1_PHY (MVEBU_REGS_BASE + 0x5C000)
  25. #define COMPHY_SD_ADDR (MVEBU_REGS_BASE + 0x1F000)
  26. struct sgmii_phy_init_data_fix {
  27. uint16_t addr;
  28. uint16_t value;
  29. };
  30. /* Changes to 40M1G25 mode data required for running 40M3G125 init mode */
  31. static struct sgmii_phy_init_data_fix sgmii_phy_init_fix[] = {
  32. {0x005, 0x07CC}, {0x015, 0x0000}, {0x01B, 0x0000}, {0x01D, 0x0000},
  33. {0x01E, 0x0000}, {0x01F, 0x0000}, {0x020, 0x0000}, {0x021, 0x0030},
  34. {0x026, 0x0888}, {0x04D, 0x0152}, {0x04F, 0xA020}, {0x050, 0x07CC},
  35. {0x053, 0xE9CA}, {0x055, 0xBD97}, {0x071, 0x3015}, {0x076, 0x03AA},
  36. {0x07C, 0x0FDF}, {0x0C2, 0x3030}, {0x0C3, 0x8000}, {0x0E2, 0x5550},
  37. {0x0E3, 0x12A4}, {0x0E4, 0x7D00}, {0x0E6, 0x0C83}, {0x101, 0xFCC0},
  38. {0x104, 0x0C10}
  39. };
  40. /* 40M1G25 mode init data */
  41. static uint16_t sgmii_phy_init[512] = {
  42. /* 0 1 2 3 4 5 6 7 */
  43. /*-----------------------------------------------------------*/
  44. /* 8 9 A B C D E F */
  45. 0x3110, 0xFD83, 0x6430, 0x412F, 0x82C0, 0x06FA, 0x4500, 0x6D26, /* 00 */
  46. 0xAFC0, 0x8000, 0xC000, 0x0000, 0x2000, 0x49CC, 0x0BC9, 0x2A52, /* 08 */
  47. 0x0BD2, 0x0CDE, 0x13D2, 0x0CE8, 0x1149, 0x10E0, 0x0000, 0x0000, /* 10 */
  48. 0x0000, 0x0000, 0x0000, 0x0001, 0x0000, 0x4134, 0x0D2D, 0xFFFF, /* 18 */
  49. 0xFFE0, 0x4030, 0x1016, 0x0030, 0x0000, 0x0800, 0x0866, 0x0000, /* 20 */
  50. 0x0000, 0x0000, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, /* 28 */
  51. 0xFFFF, 0xFFFF, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /* 30 */
  52. 0x0000, 0x0000, 0x000F, 0x6A62, 0x1988, 0x3100, 0x3100, 0x3100, /* 38 */
  53. 0x3100, 0xA708, 0x2430, 0x0830, 0x1030, 0x4610, 0xFF00, 0xFF00, /* 40 */
  54. 0x0060, 0x1000, 0x0400, 0x0040, 0x00F0, 0x0155, 0x1100, 0xA02A, /* 48 */
  55. 0x06FA, 0x0080, 0xB008, 0xE3ED, 0x5002, 0xB592, 0x7A80, 0x0001, /* 50 */
  56. 0x020A, 0x8820, 0x6014, 0x8054, 0xACAA, 0xFC88, 0x2A02, 0x45CF, /* 58 */
  57. 0x000F, 0x1817, 0x2860, 0x064F, 0x0000, 0x0204, 0x1800, 0x6000, /* 60 */
  58. 0x810F, 0x4F23, 0x4000, 0x4498, 0x0850, 0x0000, 0x000E, 0x1002, /* 68 */
  59. 0x9D3A, 0x3009, 0xD066, 0x0491, 0x0001, 0x6AB0, 0x0399, 0x3780, /* 70 */
  60. 0x0040, 0x5AC0, 0x4A80, 0x0000, 0x01DF, 0x0000, 0x0007, 0x0000, /* 78 */
  61. 0x2D54, 0x00A1, 0x4000, 0x0100, 0xA20A, 0x0000, 0x0000, 0x0000, /* 80 */
  62. 0x0000, 0x0000, 0x0000, 0x7400, 0x0E81, 0x1000, 0x1242, 0x0210, /* 88 */
  63. 0x80DF, 0x0F1F, 0x2F3F, 0x4F5F, 0x6F7F, 0x0F1F, 0x2F3F, 0x4F5F, /* 90 */
  64. 0x6F7F, 0x4BAD, 0x0000, 0x0000, 0x0800, 0x0000, 0x2400, 0xB651, /* 98 */
  65. 0xC9E0, 0x4247, 0x0A24, 0x0000, 0xAF19, 0x1004, 0x0000, 0x0000, /* A0 */
  66. 0x0000, 0x0013, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /* A8 */
  67. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /* B0 */
  68. 0x0000, 0x0000, 0x0000, 0x0060, 0x0000, 0x0000, 0x0000, 0x0000, /* B8 */
  69. 0x0000, 0x0000, 0x3010, 0xFA00, 0x0000, 0x0000, 0x0000, 0x0003, /* C0 */
  70. 0x1618, 0x8200, 0x8000, 0x0400, 0x050F, 0x0000, 0x0000, 0x0000, /* C8 */
  71. 0x4C93, 0x0000, 0x1000, 0x1120, 0x0010, 0x1242, 0x1242, 0x1E00, /* D0 */
  72. 0x0000, 0x0000, 0x0000, 0x00F8, 0x0000, 0x0041, 0x0800, 0x0000, /* D8 */
  73. 0x82A0, 0x572E, 0x2490, 0x14A9, 0x4E00, 0x0000, 0x0803, 0x0541, /* E0 */
  74. 0x0C15, 0x0000, 0x0000, 0x0400, 0x2626, 0x0000, 0x0000, 0x4200, /* E8 */
  75. 0x0000, 0xAA55, 0x1020, 0x0000, 0x0000, 0x5010, 0x0000, 0x0000, /* F0 */
  76. 0x0000, 0x0000, 0x5000, 0x0000, 0x0000, 0x0000, 0x02F2, 0x0000, /* F8 */
  77. 0x101F, 0xFDC0, 0x4000, 0x8010, 0x0110, 0x0006, 0x0000, 0x0000, /*100 */
  78. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*108 */
  79. 0x04CF, 0x0000, 0x04CF, 0x0000, 0x04CF, 0x0000, 0x04C6, 0x0000, /*110 */
  80. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*118 */
  81. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*120 */
  82. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*128 */
  83. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*130 */
  84. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*138 */
  85. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*140 */
  86. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*148 */
  87. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*150 */
  88. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*158 */
  89. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*160 */
  90. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*168 */
  91. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*170 */
  92. 0x0000, 0x0000, 0x0000, 0x00F0, 0x08A2, 0x3112, 0x0A14, 0x0000, /*178 */
  93. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*180 */
  94. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*188 */
  95. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*190 */
  96. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*198 */
  97. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*1A0 */
  98. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*1A8 */
  99. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*1B0 */
  100. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*1B8 */
  101. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*1C0 */
  102. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*1C8 */
  103. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*1D0 */
  104. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*1D8 */
  105. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*1E0 */
  106. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*1E8 */
  107. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*1F0 */
  108. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 /*1F8 */
  109. };
  110. /* PHY selector configures with corresponding modes */
  111. static int mvebu_a3700_comphy_set_phy_selector(uint8_t comphy_index,
  112. uint32_t comphy_mode)
  113. {
  114. uint32_t reg;
  115. int mode = COMPHY_GET_MODE(comphy_mode);
  116. reg = mmio_read_32(MVEBU_COMPHY_REG_BASE + COMPHY_SELECTOR_PHY_REG);
  117. switch (mode) {
  118. case (COMPHY_SATA_MODE):
  119. /* SATA must be in Lane2 */
  120. if (comphy_index == COMPHY_LANE2)
  121. reg &= ~COMPHY_SELECTOR_USB3_PHY_SEL_BIT;
  122. else
  123. goto error;
  124. break;
  125. case (COMPHY_SGMII_MODE):
  126. case (COMPHY_2500BASEX_MODE):
  127. if (comphy_index == COMPHY_LANE0)
  128. reg &= ~COMPHY_SELECTOR_USB3_GBE1_SEL_BIT;
  129. else if (comphy_index == COMPHY_LANE1)
  130. reg &= ~COMPHY_SELECTOR_PCIE_GBE0_SEL_BIT;
  131. else
  132. goto error;
  133. break;
  134. case (COMPHY_USB3H_MODE):
  135. case (COMPHY_USB3D_MODE):
  136. case (COMPHY_USB3_MODE):
  137. if (comphy_index == COMPHY_LANE2)
  138. reg |= COMPHY_SELECTOR_USB3_PHY_SEL_BIT;
  139. else if (comphy_index == COMPHY_LANE0)
  140. reg |= COMPHY_SELECTOR_USB3_GBE1_SEL_BIT;
  141. else
  142. goto error;
  143. break;
  144. case (COMPHY_PCIE_MODE):
  145. /* PCIE must be in Lane1 */
  146. if (comphy_index == COMPHY_LANE1)
  147. reg |= COMPHY_SELECTOR_PCIE_GBE0_SEL_BIT;
  148. else
  149. goto error;
  150. break;
  151. default:
  152. goto error;
  153. }
  154. mmio_write_32(MVEBU_COMPHY_REG_BASE + COMPHY_SELECTOR_PHY_REG, reg);
  155. return 0;
  156. error:
  157. ERROR("COMPHY[%d] mode[%d] is invalid\n", comphy_index, mode);
  158. return -EINVAL;
  159. }
  160. /*
  161. * This is something like the inverse of the previous function: for given
  162. * lane it returns COMPHY_*_MODE.
  163. *
  164. * It is useful when powering the phy off.
  165. *
  166. * This function returns COMPHY_USB3_MODE even if the phy was configured
  167. * with COMPHY_USB3D_MODE or COMPHY_USB3H_MODE. (The usb3 phy initialization
  168. * code does not differentiate between these modes.)
  169. * Also it returns COMPHY_SGMII_MODE even if the phy was configures with
  170. * COMPHY_2500BASEX_MODE. (The sgmii phy initialization code does differentiate
  171. * between these modes, but it is irrelevant when powering the phy off.)
  172. */
  173. static int mvebu_a3700_comphy_get_mode(uint8_t comphy_index)
  174. {
  175. uint32_t reg;
  176. reg = mmio_read_32(MVEBU_COMPHY_REG_BASE + COMPHY_SELECTOR_PHY_REG);
  177. switch (comphy_index) {
  178. case COMPHY_LANE0:
  179. if ((reg & COMPHY_SELECTOR_USB3_GBE1_SEL_BIT) != 0)
  180. return COMPHY_USB3_MODE;
  181. else
  182. return COMPHY_SGMII_MODE;
  183. case COMPHY_LANE1:
  184. if ((reg & COMPHY_SELECTOR_PCIE_GBE0_SEL_BIT) != 0)
  185. return COMPHY_PCIE_MODE;
  186. else
  187. return COMPHY_SGMII_MODE;
  188. case COMPHY_LANE2:
  189. if ((reg & COMPHY_SELECTOR_USB3_PHY_SEL_BIT) != 0)
  190. return COMPHY_USB3_MODE;
  191. else
  192. return COMPHY_SATA_MODE;
  193. }
  194. return COMPHY_UNUSED;
  195. }
  196. /* It is only used for SATA and USB3 on comphy lane2. */
  197. static void comphy_set_indirect(uintptr_t addr, uint32_t offset, uint16_t data,
  198. uint16_t mask, bool is_sata)
  199. {
  200. /*
  201. * When Lane 2 PHY is for USB3, access the PHY registers
  202. * through indirect Address and Data registers:
  203. * INDIR_ACC_PHY_ADDR (RD00E0178h [31:0]),
  204. * INDIR_ACC_PHY_DATA (RD00E017Ch [31:0]),
  205. * within the SATA Host Controller registers, Lane 2 base register
  206. * offset is 0x200
  207. */
  208. if (is_sata) {
  209. mmio_write_32(addr + COMPHY_LANE2_INDIR_ADDR_OFFSET, offset);
  210. } else {
  211. mmio_write_32(addr + COMPHY_LANE2_INDIR_ADDR_OFFSET,
  212. offset + USB3PHY_LANE2_REG_BASE_OFFSET);
  213. }
  214. reg_set(addr + COMPHY_LANE2_INDIR_DATA_OFFSET, data, mask);
  215. }
  216. /* It is only used for SATA on comphy lane2. */
  217. static void comphy_sata_set_indirect(uintptr_t addr, uint32_t reg_offset,
  218. uint16_t data, uint16_t mask)
  219. {
  220. comphy_set_indirect(addr, reg_offset, data, mask, true);
  221. }
  222. /* It is only used for USB3 indirect access on comphy lane2. */
  223. static void comphy_usb3_set_indirect(uintptr_t addr, uint32_t reg_offset,
  224. uint16_t data, uint16_t mask)
  225. {
  226. comphy_set_indirect(addr, reg_offset, data, mask, false);
  227. }
  228. /* It is only used for USB3 direct access not on comphy lane2. */
  229. static void comphy_usb3_set_direct(uintptr_t addr, uint32_t reg_offset,
  230. uint16_t data, uint16_t mask)
  231. {
  232. reg_set16((reg_offset * PHY_SHFT(USB3) + addr), data, mask);
  233. }
  234. static void comphy_sgmii_phy_init(uintptr_t sd_ip_addr, bool is_1gbps)
  235. {
  236. const int fix_arr_sz = ARRAY_SIZE(sgmii_phy_init_fix);
  237. int addr, fix_idx;
  238. uint16_t val;
  239. fix_idx = 0;
  240. for (addr = 0; addr < 512; addr++) {
  241. /*
  242. * All PHY register values are defined in full for 3.125Gbps
  243. * SERDES speed. The values required for 1.25 Gbps are almost
  244. * the same and only few registers should be "fixed" in
  245. * comparison to 3.125 Gbps values. These register values are
  246. * stored in "sgmii_phy_init_fix" array.
  247. */
  248. if (!is_1gbps && sgmii_phy_init_fix[fix_idx].addr == addr) {
  249. /* Use new value */
  250. val = sgmii_phy_init_fix[fix_idx].value;
  251. if (fix_idx < fix_arr_sz)
  252. fix_idx++;
  253. } else {
  254. val = sgmii_phy_init[addr];
  255. }
  256. reg_set16(SGMIIPHY_ADDR(addr, sd_ip_addr), val, 0xFFFF);
  257. }
  258. }
  259. static int mvebu_a3700_comphy_sata_power_on(uint8_t comphy_index,
  260. uint32_t comphy_mode)
  261. {
  262. int ret;
  263. uint32_t offset, data = 0, ref_clk;
  264. uintptr_t comphy_indir_regs = COMPHY_INDIRECT_REG;
  265. int invert = COMPHY_GET_POLARITY_INVERT(comphy_mode);
  266. debug_enter();
  267. /* Configure phy selector for SATA */
  268. ret = mvebu_a3700_comphy_set_phy_selector(comphy_index, comphy_mode);
  269. if (ret) {
  270. return ret;
  271. }
  272. /* Clear phy isolation mode to make it work in normal mode */
  273. offset = COMPHY_ISOLATION_CTRL + SATAPHY_LANE2_REG_BASE_OFFSET;
  274. comphy_sata_set_indirect(comphy_indir_regs, offset, 0, PHY_ISOLATE_MODE);
  275. /* 0. Check the Polarity invert bits */
  276. if (invert & COMPHY_POLARITY_TXD_INVERT)
  277. data |= TXD_INVERT_BIT;
  278. if (invert & COMPHY_POLARITY_RXD_INVERT)
  279. data |= RXD_INVERT_BIT;
  280. offset = COMPHY_SYNC_PATTERN + SATAPHY_LANE2_REG_BASE_OFFSET;
  281. comphy_sata_set_indirect(comphy_indir_regs, offset, data, TXD_INVERT_BIT |
  282. RXD_INVERT_BIT);
  283. /* 1. Select 40-bit data width width */
  284. offset = COMPHY_DIG_LOOPBACK_EN + SATAPHY_LANE2_REG_BASE_OFFSET;
  285. comphy_sata_set_indirect(comphy_indir_regs, offset, DATA_WIDTH_40BIT,
  286. SEL_DATA_WIDTH_MASK);
  287. /* 2. Select reference clock(25M) and PHY mode (SATA) */
  288. offset = COMPHY_POWER_PLL_CTRL + SATAPHY_LANE2_REG_BASE_OFFSET;
  289. if (get_ref_clk() == 40)
  290. ref_clk = REF_FREF_SEL_SERDES_40MHZ;
  291. else
  292. ref_clk = REF_FREF_SEL_SERDES_25MHZ;
  293. comphy_sata_set_indirect(comphy_indir_regs, offset, ref_clk | PHY_MODE_SATA,
  294. REF_FREF_SEL_MASK | PHY_MODE_MASK);
  295. /* 3. Use maximum PLL rate (no power save) */
  296. offset = COMPHY_KVCO_CAL_CTRL + SATAPHY_LANE2_REG_BASE_OFFSET;
  297. comphy_sata_set_indirect(comphy_indir_regs, offset, USE_MAX_PLL_RATE_BIT,
  298. USE_MAX_PLL_RATE_BIT);
  299. /* 4. Reset reserved bit */
  300. comphy_sata_set_indirect(comphy_indir_regs, COMPHY_RESERVED_REG, 0,
  301. PHYCTRL_FRM_PIN_BIT);
  302. /* 5. Set vendor-specific configuration (It is done in sata driver) */
  303. /* XXX: in U-Boot below sequence was executed in this place, in Linux
  304. * not. Now it is done only in U-Boot before this comphy
  305. * initialization - tests shows that it works ok, but in case of any
  306. * future problem it is left for reference.
  307. * reg_set(MVEBU_REGS_BASE + 0xe00a0, 0, 0xffffffff);
  308. * reg_set(MVEBU_REGS_BASE + 0xe00a4, BIT(6), BIT(6));
  309. */
  310. /* Wait for > 55 us to allow PLL be enabled */
  311. udelay(PLL_SET_DELAY_US);
  312. /* Polling status */
  313. mmio_write_32(comphy_indir_regs + COMPHY_LANE2_INDIR_ADDR_OFFSET,
  314. COMPHY_DIG_LOOPBACK_EN + SATAPHY_LANE2_REG_BASE_OFFSET);
  315. ret = polling_with_timeout(comphy_indir_regs +
  316. COMPHY_LANE2_INDIR_DATA_OFFSET,
  317. PLL_READY_TX_BIT, PLL_READY_TX_BIT,
  318. COMPHY_PLL_TIMEOUT, REG_32BIT);
  319. if (ret) {
  320. return -ETIMEDOUT;
  321. }
  322. debug_exit();
  323. return 0;
  324. }
  325. static int mvebu_a3700_comphy_sgmii_power_on(uint8_t comphy_index,
  326. uint32_t comphy_mode)
  327. {
  328. int ret;
  329. uint32_t mask, data;
  330. uintptr_t offset;
  331. uintptr_t sd_ip_addr;
  332. int mode = COMPHY_GET_MODE(comphy_mode);
  333. int invert = COMPHY_GET_POLARITY_INVERT(comphy_mode);
  334. debug_enter();
  335. /* Set selector */
  336. ret = mvebu_a3700_comphy_set_phy_selector(comphy_index, comphy_mode);
  337. if (ret) {
  338. return ret;
  339. }
  340. /* Serdes IP Base address
  341. * COMPHY Lane0 -- USB3/GBE1
  342. * COMPHY Lane1 -- PCIe/GBE0
  343. */
  344. if (comphy_index == COMPHY_LANE0) {
  345. /* Get usb3 and gbe */
  346. sd_ip_addr = USB3_GBE1_PHY;
  347. } else
  348. sd_ip_addr = COMPHY_SD_ADDR;
  349. /*
  350. * 1. Reset PHY by setting PHY input port PIN_RESET=1.
  351. * 2. Set PHY input port PIN_TX_IDLE=1, PIN_PU_IVREF=1 to keep
  352. * PHY TXP/TXN output to idle state during PHY initialization
  353. * 3. Set PHY input port PIN_PU_PLL=0, PIN_PU_RX=0, PIN_PU_TX=0.
  354. */
  355. data = PIN_PU_IVREF_BIT | PIN_TX_IDLE_BIT | PIN_RESET_COMPHY_BIT;
  356. mask = data | PIN_RESET_CORE_BIT | PIN_PU_PLL_BIT | PIN_PU_RX_BIT |
  357. PIN_PU_TX_BIT;
  358. offset = MVEBU_COMPHY_REG_BASE + COMPHY_PHY_CFG1_OFFSET(comphy_index);
  359. reg_set(offset, data, mask);
  360. /* 4. Release reset to the PHY by setting PIN_RESET=0. */
  361. data = 0;
  362. mask = PIN_RESET_COMPHY_BIT;
  363. reg_set(offset, data, mask);
  364. /*
  365. * 5. Set PIN_PHY_GEN_TX[3:0] and PIN_PHY_GEN_RX[3:0] to decide COMPHY
  366. * bit rate
  367. */
  368. if (mode == COMPHY_SGMII_MODE) {
  369. /* SGMII 1G, SerDes speed 1.25G */
  370. data |= SD_SPEED_1_25_G << GEN_RX_SEL_OFFSET;
  371. data |= SD_SPEED_1_25_G << GEN_TX_SEL_OFFSET;
  372. } else if (mode == COMPHY_2500BASEX_MODE) {
  373. /* 2500Base-X, SerDes speed 3.125G */
  374. data |= SD_SPEED_3_125_G << GEN_RX_SEL_OFFSET;
  375. data |= SD_SPEED_3_125_G << GEN_TX_SEL_OFFSET;
  376. } else {
  377. /* Other rates are not supported */
  378. ERROR("unsupported SGMII speed on comphy lane%d\n",
  379. comphy_index);
  380. return -EINVAL;
  381. }
  382. mask = GEN_RX_SEL_MASK | GEN_TX_SEL_MASK;
  383. reg_set(offset, data, mask);
  384. /*
  385. * 6. Wait 10mS for bandgap and reference clocks to stabilize; then
  386. * start SW programming.
  387. */
  388. mdelay(10);
  389. /* 7. Program COMPHY register PHY_MODE */
  390. data = PHY_MODE_SGMII;
  391. mask = PHY_MODE_MASK;
  392. reg_set16(SGMIIPHY_ADDR(COMPHY_POWER_PLL_CTRL, sd_ip_addr), data, mask);
  393. /*
  394. * 8. Set COMPHY register REFCLK_SEL to select the correct REFCLK
  395. * source
  396. */
  397. data = 0;
  398. mask = PHY_REF_CLK_SEL;
  399. reg_set16(SGMIIPHY_ADDR(COMPHY_MISC_CTRL0, sd_ip_addr), data, mask);
  400. /*
  401. * 9. Set correct reference clock frequency in COMPHY register
  402. * REF_FREF_SEL.
  403. */
  404. if (get_ref_clk() == 40)
  405. data = REF_FREF_SEL_SERDES_50MHZ;
  406. else
  407. data = REF_FREF_SEL_SERDES_25MHZ;
  408. mask = REF_FREF_SEL_MASK;
  409. reg_set16(SGMIIPHY_ADDR(COMPHY_POWER_PLL_CTRL, sd_ip_addr), data, mask);
  410. /* 10. Program COMPHY register PHY_GEN_MAX[1:0]
  411. * This step is mentioned in the flow received from verification team.
  412. * However the PHY_GEN_MAX value is only meaningful for other interfaces
  413. * (not SGMII). For instance, it selects SATA speed 1.5/3/6 Gbps or PCIe
  414. * speed 2.5/5 Gbps
  415. */
  416. /*
  417. * 11. Program COMPHY register SEL_BITS to set correct parallel data
  418. * bus width
  419. */
  420. data = DATA_WIDTH_10BIT;
  421. mask = SEL_DATA_WIDTH_MASK;
  422. reg_set16(SGMIIPHY_ADDR(COMPHY_DIG_LOOPBACK_EN, sd_ip_addr),
  423. data, mask);
  424. /*
  425. * 12. As long as DFE function needs to be enabled in any mode,
  426. * COMPHY register DFE_UPDATE_EN[5:0] shall be programmed to 0x3F
  427. * for real chip during COMPHY power on.
  428. * The step 14 exists (and empty) in the original initialization flow
  429. * obtained from the verification team. According to the functional
  430. * specification DFE_UPDATE_EN already has the default value 0x3F
  431. */
  432. /*
  433. * 13. Program COMPHY GEN registers.
  434. * These registers should be programmed based on the lab testing result
  435. * to achieve optimal performance. Please contact the CEA group to get
  436. * the related GEN table during real chip bring-up. We only required to
  437. * run though the entire registers programming flow defined by
  438. * "comphy_sgmii_phy_init" when the REF clock is 40 MHz. For REF clock
  439. * 25 MHz the default values stored in PHY registers are OK.
  440. */
  441. debug("Running C-DPI phy init %s mode\n",
  442. mode == COMPHY_2500BASEX_MODE ? "2G5" : "1G");
  443. if (get_ref_clk() == 40)
  444. comphy_sgmii_phy_init(sd_ip_addr, mode != COMPHY_2500BASEX_MODE);
  445. /*
  446. * 14. [Simulation Only] should not be used for real chip.
  447. * By pass power up calibration by programming EXT_FORCE_CAL_DONE
  448. * (R02h[9]) to 1 to shorten COMPHY simulation time.
  449. */
  450. /*
  451. * 15. [Simulation Only: should not be used for real chip]
  452. * Program COMPHY register FAST_DFE_TIMER_EN=1 to shorten RX training
  453. * simulation time.
  454. */
  455. /*
  456. * 16. Check the PHY Polarity invert bit
  457. */
  458. data = 0x0;
  459. if (invert & COMPHY_POLARITY_TXD_INVERT)
  460. data |= TXD_INVERT_BIT;
  461. if (invert & COMPHY_POLARITY_RXD_INVERT)
  462. data |= RXD_INVERT_BIT;
  463. mask = TXD_INVERT_BIT | RXD_INVERT_BIT;
  464. reg_set16(SGMIIPHY_ADDR(COMPHY_SYNC_PATTERN, sd_ip_addr), data, mask);
  465. /*
  466. * 17. Set PHY input ports PIN_PU_PLL, PIN_PU_TX and PIN_PU_RX to 1 to
  467. * start PHY power up sequence. All the PHY register programming should
  468. * be done before PIN_PU_PLL=1. There should be no register programming
  469. * for normal PHY operation from this point.
  470. */
  471. reg_set(MVEBU_COMPHY_REG_BASE + COMPHY_PHY_CFG1_OFFSET(comphy_index),
  472. PIN_PU_PLL_BIT | PIN_PU_RX_BIT | PIN_PU_TX_BIT,
  473. PIN_PU_PLL_BIT | PIN_PU_RX_BIT | PIN_PU_TX_BIT);
  474. /*
  475. * 18. Wait for PHY power up sequence to finish by checking output ports
  476. * PIN_PLL_READY_TX=1 and PIN_PLL_READY_RX=1.
  477. */
  478. ret = polling_with_timeout(MVEBU_COMPHY_REG_BASE +
  479. COMPHY_PHY_STATUS_OFFSET(comphy_index),
  480. PHY_PLL_READY_TX_BIT | PHY_PLL_READY_RX_BIT,
  481. PHY_PLL_READY_TX_BIT | PHY_PLL_READY_RX_BIT,
  482. COMPHY_PLL_TIMEOUT, REG_32BIT);
  483. if (ret) {
  484. ERROR("Failed to lock PLL for SGMII PHY %d\n", comphy_index);
  485. return -ETIMEDOUT;
  486. }
  487. /*
  488. * 19. Set COMPHY input port PIN_TX_IDLE=0
  489. */
  490. reg_set(MVEBU_COMPHY_REG_BASE + COMPHY_PHY_CFG1_OFFSET(comphy_index),
  491. 0x0, PIN_TX_IDLE_BIT);
  492. /*
  493. * 20. After valid data appear on PIN_RXDATA bus, set PIN_RX_INIT=1. To
  494. * start RX initialization. PIN_RX_INIT_DONE will be cleared to 0 by the
  495. * PHY After RX initialization is done, PIN_RX_INIT_DONE will be set to
  496. * 1 by COMPHY Set PIN_RX_INIT=0 after PIN_RX_INIT_DONE= 1. Please
  497. * refer to RX initialization part for details.
  498. */
  499. reg_set(MVEBU_COMPHY_REG_BASE + COMPHY_PHY_CFG1_OFFSET(comphy_index),
  500. PHY_RX_INIT_BIT, PHY_RX_INIT_BIT);
  501. ret = polling_with_timeout(MVEBU_COMPHY_REG_BASE +
  502. COMPHY_PHY_STATUS_OFFSET(comphy_index),
  503. PHY_PLL_READY_TX_BIT | PHY_PLL_READY_RX_BIT,
  504. PHY_PLL_READY_TX_BIT | PHY_PLL_READY_RX_BIT,
  505. COMPHY_PLL_TIMEOUT, REG_32BIT);
  506. if (ret) {
  507. ERROR("Failed to lock PLL for SGMII PHY %d\n", comphy_index);
  508. return -ETIMEDOUT;
  509. }
  510. ret = polling_with_timeout(MVEBU_COMPHY_REG_BASE +
  511. COMPHY_PHY_STATUS_OFFSET(comphy_index),
  512. PHY_RX_INIT_DONE_BIT, PHY_RX_INIT_DONE_BIT,
  513. COMPHY_PLL_TIMEOUT, REG_32BIT);
  514. if (ret) {
  515. ERROR("Failed to init RX of SGMII PHY %d\n", comphy_index);
  516. return -ETIMEDOUT;
  517. }
  518. debug_exit();
  519. return 0;
  520. }
  521. static int mvebu_a3700_comphy_sgmii_power_off(uint8_t comphy_index)
  522. {
  523. uintptr_t offset;
  524. uint32_t mask, data;
  525. debug_enter();
  526. data = PIN_RESET_CORE_BIT | PIN_RESET_COMPHY_BIT;
  527. mask = data;
  528. offset = MVEBU_COMPHY_REG_BASE + COMPHY_PHY_CFG1_OFFSET(comphy_index);
  529. reg_set(offset, data, mask);
  530. debug_exit();
  531. return 0;
  532. }
  533. static int mvebu_a3700_comphy_usb3_power_on(uint8_t comphy_index,
  534. uint32_t comphy_mode)
  535. {
  536. int ret;
  537. uintptr_t reg_base = 0;
  538. uintptr_t addr;
  539. uint32_t mask, data, cfg, ref_clk;
  540. void (*usb3_reg_set)(uintptr_t addr, uint32_t reg_offset, uint16_t data,
  541. uint16_t mask);
  542. int invert = COMPHY_GET_POLARITY_INVERT(comphy_mode);
  543. debug_enter();
  544. /* Set phy seclector */
  545. ret = mvebu_a3700_comphy_set_phy_selector(comphy_index, comphy_mode);
  546. if (ret) {
  547. return ret;
  548. }
  549. /* Set usb3 reg access func, Lane2 is indirect access */
  550. if (comphy_index == COMPHY_LANE2) {
  551. usb3_reg_set = &comphy_usb3_set_indirect;
  552. reg_base = COMPHY_INDIRECT_REG;
  553. } else {
  554. /* Get the direct access register resource and map */
  555. usb3_reg_set = &comphy_usb3_set_direct;
  556. reg_base = USB3_GBE1_PHY;
  557. }
  558. /*
  559. * 0. Set PHY OTG Control(0x5d034), bit 4, Power up OTG module The
  560. * register belong to UTMI module, so it is set in UTMI phy driver.
  561. */
  562. /*
  563. * 1. Set PRD_TXDEEMPH (3.5db de-emph)
  564. */
  565. mask = PRD_TXDEEMPH0_MASK | PRD_TXMARGIN_MASK | PRD_TXSWING_MASK |
  566. CFG_TX_ALIGN_POS_MASK;
  567. usb3_reg_set(reg_base, COMPHY_LANE_CFG0, PRD_TXDEEMPH0_MASK, mask);
  568. /*
  569. * 2. Set BIT0: enable transmitter in high impedance mode
  570. * Set BIT[3:4]: delay 2 clock cycles for HiZ off latency
  571. * Set BIT6: Tx detect Rx at HiZ mode
  572. * Unset BIT15: set to 0 to set USB3 De-emphasize level to -3.5db
  573. * together with bit 0 of COMPHY_LANE_CFG0 register
  574. */
  575. mask = PRD_TXDEEMPH1_MASK | TX_DET_RX_MODE | GEN2_TX_DATA_DLY_MASK |
  576. TX_ELEC_IDLE_MODE_EN;
  577. data = TX_DET_RX_MODE | GEN2_TX_DATA_DLY_DEFT | TX_ELEC_IDLE_MODE_EN;
  578. usb3_reg_set(reg_base, COMPHY_LANE_CFG1, data, mask);
  579. /*
  580. * 3. Set Spread Spectrum Clock Enabled
  581. */
  582. usb3_reg_set(reg_base, COMPHY_LANE_CFG4,
  583. SPREAD_SPECTRUM_CLK_EN, SPREAD_SPECTRUM_CLK_EN);
  584. /*
  585. * 4. Set Override Margining Controls From the MAC:
  586. * Use margining signals from lane configuration
  587. */
  588. usb3_reg_set(reg_base, COMPHY_TEST_MODE_CTRL,
  589. MODE_MARGIN_OVERRIDE, REG_16_BIT_MASK);
  590. /*
  591. * 5. Set Lane-to-Lane Bundle Clock Sampling Period = per PCLK cycles
  592. * set Mode Clock Source = PCLK is generated from REFCLK
  593. */
  594. usb3_reg_set(reg_base, COMPHY_CLK_SRC_LO, 0x0,
  595. (MODE_CLK_SRC | BUNDLE_PERIOD_SEL |
  596. BUNDLE_PERIOD_SCALE_MASK | BUNDLE_SAMPLE_CTRL |
  597. PLL_READY_DLY_MASK));
  598. /*
  599. * 6. Set G2 Spread Spectrum Clock Amplitude at 4K
  600. */
  601. usb3_reg_set(reg_base, COMPHY_GEN2_SET2,
  602. GS2_TX_SSC_AMP_VALUE_20, GS2_TX_SSC_AMP_MASK);
  603. /*
  604. * 7. Unset G3 Spread Spectrum Clock Amplitude
  605. * set G3 TX and RX Register Master Current Select
  606. */
  607. mask = GS2_TX_SSC_AMP_MASK | GS2_VREG_RXTX_MAS_ISET_MASK |
  608. GS2_RSVD_6_0_MASK;
  609. usb3_reg_set(reg_base, COMPHY_GEN3_SET2,
  610. GS2_VREG_RXTX_MAS_ISET_60U, mask);
  611. /*
  612. * 8. Check crystal jumper setting and program the Power and PLL Control
  613. * accordingly Change RX wait
  614. */
  615. if (get_ref_clk() == 40) {
  616. ref_clk = REF_FREF_SEL_PCIE_USB3_40MHZ;
  617. cfg = CFG_PM_RXDLOZ_WAIT_12_UNIT;
  618. } else {
  619. /* 25 MHz */
  620. ref_clk = REF_FREF_SEL_PCIE_USB3_25MHZ;
  621. cfg = CFG_PM_RXDLOZ_WAIT_7_UNIT;
  622. }
  623. mask = PU_IVREF_BIT | PU_PLL_BIT | PU_RX_BIT | PU_TX_BIT |
  624. PU_TX_INTP_BIT | PU_DFE_BIT | PLL_LOCK_BIT | PHY_MODE_MASK |
  625. REF_FREF_SEL_MASK;
  626. data = PU_IVREF_BIT | PU_PLL_BIT | PU_RX_BIT | PU_TX_BIT |
  627. PU_TX_INTP_BIT | PU_DFE_BIT | PHY_MODE_USB3 | ref_clk;
  628. usb3_reg_set(reg_base, COMPHY_POWER_PLL_CTRL, data, mask);
  629. mask = CFG_PM_OSCCLK_WAIT_MASK | CFG_PM_RXDEN_WAIT_MASK |
  630. CFG_PM_RXDLOZ_WAIT_MASK;
  631. data = CFG_PM_RXDEN_WAIT_1_UNIT | cfg;
  632. usb3_reg_set(reg_base, COMPHY_PWR_MGM_TIM1, data, mask);
  633. /*
  634. * 9. Enable idle sync
  635. */
  636. data = IDLE_SYNC_EN_DEFAULT_VALUE | IDLE_SYNC_EN;
  637. usb3_reg_set(reg_base, COMPHY_IDLE_SYNC_EN, data, REG_16_BIT_MASK);
  638. /*
  639. * 10. Enable the output of 500M clock
  640. */
  641. data = MISC_CTRL0_DEFAULT_VALUE | CLK500M_EN;
  642. usb3_reg_set(reg_base, COMPHY_MISC_CTRL0, data, REG_16_BIT_MASK);
  643. /*
  644. * 11. Set 20-bit data width
  645. */
  646. usb3_reg_set(reg_base, COMPHY_DIG_LOOPBACK_EN, DATA_WIDTH_20BIT,
  647. REG_16_BIT_MASK);
  648. /*
  649. * 12. Override Speed_PLL value and use MAC PLL
  650. */
  651. usb3_reg_set(reg_base, COMPHY_KVCO_CAL_CTRL,
  652. (SPEED_PLL_VALUE_16 | USE_MAX_PLL_RATE_BIT),
  653. REG_16_BIT_MASK);
  654. /*
  655. * 13. Check the Polarity invert bit
  656. */
  657. data = 0U;
  658. if (invert & COMPHY_POLARITY_TXD_INVERT) {
  659. data |= TXD_INVERT_BIT;
  660. }
  661. if (invert & COMPHY_POLARITY_RXD_INVERT) {
  662. data |= RXD_INVERT_BIT;
  663. }
  664. mask = TXD_INVERT_BIT | RXD_INVERT_BIT;
  665. usb3_reg_set(reg_base, COMPHY_SYNC_PATTERN, data, mask);
  666. /*
  667. * 14. Set max speed generation to USB3.0 5Gbps
  668. */
  669. usb3_reg_set(reg_base, COMPHY_SYNC_MASK_GEN, PHY_GEN_MAX_USB3_5G,
  670. PHY_GEN_MAX_MASK);
  671. /*
  672. * 15. Set capacitor value for FFE gain peaking to 0xF
  673. */
  674. usb3_reg_set(reg_base, COMPHY_GEN2_SET3,
  675. GS3_FFE_CAP_SEL_VALUE, GS3_FFE_CAP_SEL_MASK);
  676. /*
  677. * 16. Release SW reset
  678. */
  679. data = MODE_CORE_CLK_FREQ_SEL | MODE_PIPE_WIDTH_32 | MODE_REFDIV_BY_4;
  680. usb3_reg_set(reg_base, COMPHY_RST_CLK_CTRL, data, REG_16_BIT_MASK);
  681. /* Wait for > 55 us to allow PCLK be enabled */
  682. udelay(PLL_SET_DELAY_US);
  683. if (comphy_index == COMPHY_LANE2) {
  684. data = COMPHY_LANE_STAT1 + USB3PHY_LANE2_REG_BASE_OFFSET;
  685. mmio_write_32(reg_base + COMPHY_LANE2_INDIR_ADDR_OFFSET,
  686. data);
  687. addr = reg_base + COMPHY_LANE2_INDIR_DATA_OFFSET;
  688. ret = polling_with_timeout(addr, TXDCLK_PCLK_EN, TXDCLK_PCLK_EN,
  689. COMPHY_PLL_TIMEOUT, REG_32BIT);
  690. } else {
  691. ret = polling_with_timeout(LANE_STAT1_ADDR(USB3) + reg_base,
  692. TXDCLK_PCLK_EN, TXDCLK_PCLK_EN,
  693. COMPHY_PLL_TIMEOUT, REG_16BIT);
  694. }
  695. if (ret) {
  696. ERROR("Failed to lock USB3 PLL\n");
  697. return -ETIMEDOUT;
  698. }
  699. debug_exit();
  700. return 0;
  701. }
  702. static int mvebu_a3700_comphy_pcie_power_on(uint8_t comphy_index,
  703. uint32_t comphy_mode)
  704. {
  705. int ret;
  706. uint32_t ref_clk;
  707. uint32_t mask, data;
  708. int invert = COMPHY_GET_POLARITY_INVERT(comphy_mode);
  709. debug_enter();
  710. /* Configure phy selector for PCIe */
  711. ret = mvebu_a3700_comphy_set_phy_selector(comphy_index, comphy_mode);
  712. if (ret) {
  713. return ret;
  714. }
  715. /* 1. Enable max PLL. */
  716. reg_set16(LANE_CFG1_ADDR(PCIE) + COMPHY_SD_ADDR,
  717. USE_MAX_PLL_RATE_EN, USE_MAX_PLL_RATE_EN);
  718. /* 2. Select 20 bit SERDES interface. */
  719. reg_set16(CLK_SRC_LO_ADDR(PCIE) + COMPHY_SD_ADDR,
  720. CFG_SEL_20B, CFG_SEL_20B);
  721. /* 3. Force to use reg setting for PCIe mode */
  722. reg_set16(MISC_CTRL1_ADDR(PCIE) + COMPHY_SD_ADDR,
  723. SEL_BITS_PCIE_FORCE, SEL_BITS_PCIE_FORCE);
  724. /* 4. Change RX wait */
  725. reg_set16(PWR_MGM_TIM1_ADDR(PCIE) + COMPHY_SD_ADDR,
  726. CFG_PM_RXDEN_WAIT_1_UNIT | CFG_PM_RXDLOZ_WAIT_12_UNIT,
  727. (CFG_PM_OSCCLK_WAIT_MASK | CFG_PM_RXDEN_WAIT_MASK |
  728. CFG_PM_RXDLOZ_WAIT_MASK));
  729. /* 5. Enable idle sync */
  730. reg_set16(IDLE_SYNC_EN_ADDR(PCIE) + COMPHY_SD_ADDR,
  731. IDLE_SYNC_EN_DEFAULT_VALUE | IDLE_SYNC_EN, REG_16_BIT_MASK);
  732. /* 6. Enable the output of 100M/125M/500M clock */
  733. reg_set16(MISC_CTRL0_ADDR(PCIE) + COMPHY_SD_ADDR,
  734. MISC_CTRL0_DEFAULT_VALUE | CLK500M_EN | TXDCLK_2X_SEL | CLK100M_125M_EN,
  735. REG_16_BIT_MASK);
  736. /*
  737. * 7. Enable TX, PCIE global register, 0xd0074814, it is done in
  738. * PCI-E driver
  739. */
  740. /*
  741. * 8. Check crystal jumper setting and program the Power and PLL
  742. * Control accordingly
  743. */
  744. if (get_ref_clk() == 40)
  745. ref_clk = REF_FREF_SEL_PCIE_USB3_40MHZ;
  746. else
  747. ref_clk = REF_FREF_SEL_PCIE_USB3_25MHZ;
  748. reg_set16(PWR_PLL_CTRL_ADDR(PCIE) + COMPHY_SD_ADDR,
  749. (PU_IVREF_BIT | PU_PLL_BIT | PU_RX_BIT | PU_TX_BIT |
  750. PU_TX_INTP_BIT | PU_DFE_BIT | ref_clk | PHY_MODE_PCIE),
  751. REG_16_BIT_MASK);
  752. /* 9. Override Speed_PLL value and use MAC PLL */
  753. reg_set16(KVCO_CAL_CTRL_ADDR(PCIE) + COMPHY_SD_ADDR,
  754. SPEED_PLL_VALUE_16 | USE_MAX_PLL_RATE_BIT, REG_16_BIT_MASK);
  755. /* 10. Check the Polarity invert bit */
  756. data = 0U;
  757. if (invert & COMPHY_POLARITY_TXD_INVERT) {
  758. data |= TXD_INVERT_BIT;
  759. }
  760. if (invert & COMPHY_POLARITY_RXD_INVERT) {
  761. data |= RXD_INVERT_BIT;
  762. }
  763. mask = TXD_INVERT_BIT | RXD_INVERT_BIT;
  764. reg_set16(SYNC_PATTERN_ADDR(PCIE) + COMPHY_SD_ADDR, data, mask);
  765. /* 11. Release SW reset */
  766. data = MODE_CORE_CLK_FREQ_SEL | MODE_PIPE_WIDTH_32;
  767. mask = data | SOFT_RESET | MODE_REFDIV_MASK;
  768. reg_set16(RST_CLK_CTRL_ADDR(PCIE) + COMPHY_SD_ADDR, data, mask);
  769. /* Wait for > 55 us to allow PCLK be enabled */
  770. udelay(PLL_SET_DELAY_US);
  771. ret = polling_with_timeout(LANE_STAT1_ADDR(PCIE) + COMPHY_SD_ADDR,
  772. TXDCLK_PCLK_EN, TXDCLK_PCLK_EN,
  773. COMPHY_PLL_TIMEOUT, REG_16BIT);
  774. if (ret) {
  775. ERROR("Failed to lock PCIE PLL\n");
  776. return -ETIMEDOUT;
  777. }
  778. debug_exit();
  779. return 0;
  780. }
  781. int mvebu_3700_comphy_power_on(uint8_t comphy_index, uint32_t comphy_mode)
  782. {
  783. int mode = COMPHY_GET_MODE(comphy_mode);
  784. int ret = 0;
  785. debug_enter();
  786. switch (mode) {
  787. case(COMPHY_SATA_MODE):
  788. ret = mvebu_a3700_comphy_sata_power_on(comphy_index,
  789. comphy_mode);
  790. break;
  791. case(COMPHY_SGMII_MODE):
  792. case(COMPHY_2500BASEX_MODE):
  793. ret = mvebu_a3700_comphy_sgmii_power_on(comphy_index,
  794. comphy_mode);
  795. break;
  796. case (COMPHY_USB3_MODE):
  797. case (COMPHY_USB3H_MODE):
  798. ret = mvebu_a3700_comphy_usb3_power_on(comphy_index,
  799. comphy_mode);
  800. break;
  801. case (COMPHY_PCIE_MODE):
  802. ret = mvebu_a3700_comphy_pcie_power_on(comphy_index,
  803. comphy_mode);
  804. break;
  805. default:
  806. ERROR("comphy%d: unsupported comphy mode\n", comphy_index);
  807. ret = -EINVAL;
  808. break;
  809. }
  810. debug_exit();
  811. return ret;
  812. }
  813. static int mvebu_a3700_comphy_usb3_power_off(void)
  814. {
  815. /*
  816. * Currently the USB3 MAC will control the USB3 PHY to set it to low
  817. * state, thus do not need to power off USB3 PHY again.
  818. */
  819. debug_enter();
  820. debug_exit();
  821. return 0;
  822. }
  823. static int mvebu_a3700_comphy_sata_power_off(void)
  824. {
  825. uintptr_t comphy_indir_regs = COMPHY_INDIRECT_REG;
  826. uint32_t offset;
  827. debug_enter();
  828. /* Set phy isolation mode */
  829. offset = COMPHY_ISOLATION_CTRL + SATAPHY_LANE2_REG_BASE_OFFSET;
  830. comphy_sata_set_indirect(comphy_indir_regs, offset, PHY_ISOLATE_MODE,
  831. PHY_ISOLATE_MODE);
  832. /* Power off PLL, Tx, Rx */
  833. offset = COMPHY_POWER_PLL_CTRL + SATAPHY_LANE2_REG_BASE_OFFSET;
  834. comphy_sata_set_indirect(comphy_indir_regs, offset, 0,
  835. PU_PLL_BIT | PU_RX_BIT | PU_TX_BIT);
  836. debug_exit();
  837. return 0;
  838. }
  839. int mvebu_3700_comphy_power_off(uint8_t comphy_index, uint32_t comphy_mode)
  840. {
  841. int mode = COMPHY_GET_MODE(comphy_mode);
  842. int err = 0;
  843. debug_enter();
  844. if (!mode) {
  845. /*
  846. * The user did not specify which mode should be powered off.
  847. * In this case we can identify this by reading the phy selector
  848. * register.
  849. */
  850. mode = mvebu_a3700_comphy_get_mode(comphy_index);
  851. }
  852. switch (mode) {
  853. case(COMPHY_SGMII_MODE):
  854. case(COMPHY_2500BASEX_MODE):
  855. err = mvebu_a3700_comphy_sgmii_power_off(comphy_index);
  856. break;
  857. case (COMPHY_USB3_MODE):
  858. case (COMPHY_USB3H_MODE):
  859. err = mvebu_a3700_comphy_usb3_power_off();
  860. break;
  861. case (COMPHY_SATA_MODE):
  862. err = mvebu_a3700_comphy_sata_power_off();
  863. break;
  864. default:
  865. debug("comphy%d: power off is not implemented for mode %d\n",
  866. comphy_index, mode);
  867. break;
  868. }
  869. debug_exit();
  870. return err;
  871. }
  872. static int mvebu_a3700_comphy_sata_is_pll_locked(void)
  873. {
  874. uint32_t data, addr;
  875. uintptr_t comphy_indir_regs = COMPHY_INDIRECT_REG;
  876. int ret = 0;
  877. debug_enter();
  878. /* Polling status */
  879. mmio_write_32(comphy_indir_regs + COMPHY_LANE2_INDIR_ADDR_OFFSET,
  880. COMPHY_DIG_LOOPBACK_EN + SATAPHY_LANE2_REG_BASE_OFFSET);
  881. addr = comphy_indir_regs + COMPHY_LANE2_INDIR_DATA_OFFSET;
  882. data = polling_with_timeout(addr, PLL_READY_TX_BIT, PLL_READY_TX_BIT,
  883. COMPHY_PLL_TIMEOUT, REG_32BIT);
  884. if (data != 0) {
  885. ERROR("TX PLL is not locked\n");
  886. ret = -ETIMEDOUT;
  887. }
  888. debug_exit();
  889. return ret;
  890. }
  891. int mvebu_3700_comphy_is_pll_locked(uint8_t comphy_index, uint32_t comphy_mode)
  892. {
  893. int mode = COMPHY_GET_MODE(comphy_mode);
  894. int ret = 0;
  895. debug_enter();
  896. switch (mode) {
  897. case(COMPHY_SATA_MODE):
  898. ret = mvebu_a3700_comphy_sata_is_pll_locked();
  899. break;
  900. default:
  901. ERROR("comphy[%d] mode[%d] doesn't support PLL lock check\n",
  902. comphy_index, mode);
  903. ret = -EINVAL;
  904. break;
  905. }
  906. debug_exit();
  907. return ret;
  908. }