r8a779f0-ether-serdes.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Renesas Ethernet SERDES device driver
  3. *
  4. * Copyright (C) 2022 Renesas Electronics Corporation
  5. */
  6. #include <linux/delay.h>
  7. #include <linux/err.h>
  8. #include <linux/iopoll.h>
  9. #include <linux/kernel.h>
  10. #include <linux/of.h>
  11. #include <linux/phy.h>
  12. #include <linux/phy/phy.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/reset.h>
  15. #define R8A779F0_ETH_SERDES_NUM 3
  16. #define R8A779F0_ETH_SERDES_OFFSET 0x0400
  17. #define R8A779F0_ETH_SERDES_BANK_SELECT 0x03fc
  18. #define R8A779F0_ETH_SERDES_TIMEOUT_US 100000
  19. #define R8A779F0_ETH_SERDES_NUM_RETRY_LINKUP 3
  20. struct r8a779f0_eth_serdes_drv_data;
  21. struct r8a779f0_eth_serdes_channel {
  22. struct r8a779f0_eth_serdes_drv_data *dd;
  23. struct phy *phy;
  24. void __iomem *addr;
  25. phy_interface_t phy_interface;
  26. int speed;
  27. int index;
  28. };
  29. struct r8a779f0_eth_serdes_drv_data {
  30. void __iomem *addr;
  31. struct platform_device *pdev;
  32. struct reset_control *reset;
  33. struct r8a779f0_eth_serdes_channel channel[R8A779F0_ETH_SERDES_NUM];
  34. bool initialized;
  35. };
  36. /*
  37. * The datasheet describes initialization procedure without any information
  38. * about registers' name/bits. So, this is all black magic to initialize
  39. * the hardware.
  40. */
  41. static void r8a779f0_eth_serdes_write32(void __iomem *addr, u32 offs, u32 bank, u32 data)
  42. {
  43. iowrite32(bank, addr + R8A779F0_ETH_SERDES_BANK_SELECT);
  44. iowrite32(data, addr + offs);
  45. }
  46. static int
  47. r8a779f0_eth_serdes_reg_wait(struct r8a779f0_eth_serdes_channel *channel,
  48. u32 offs, u32 bank, u32 mask, u32 expected)
  49. {
  50. int ret;
  51. u32 val;
  52. iowrite32(bank, channel->addr + R8A779F0_ETH_SERDES_BANK_SELECT);
  53. ret = readl_poll_timeout_atomic(channel->addr + offs, val,
  54. (val & mask) == expected,
  55. 1, R8A779F0_ETH_SERDES_TIMEOUT_US);
  56. if (ret)
  57. dev_dbg(&channel->phy->dev,
  58. "%s: index %d, offs %x, bank %x, mask %x, expected %x\n",
  59. __func__, channel->index, offs, bank, mask, expected);
  60. return ret;
  61. }
  62. static int
  63. r8a779f0_eth_serdes_common_init_ram(struct r8a779f0_eth_serdes_drv_data *dd)
  64. {
  65. struct r8a779f0_eth_serdes_channel *channel;
  66. int i, ret;
  67. for (i = 0; i < R8A779F0_ETH_SERDES_NUM; i++) {
  68. channel = &dd->channel[i];
  69. ret = r8a779f0_eth_serdes_reg_wait(channel, 0x026c, 0x180, BIT(0), 0x01);
  70. if (ret)
  71. return ret;
  72. }
  73. r8a779f0_eth_serdes_write32(dd->addr, 0x026c, 0x180, 0x03);
  74. return ret;
  75. }
  76. static int
  77. r8a779f0_eth_serdes_common_setting(struct r8a779f0_eth_serdes_channel *channel)
  78. {
  79. struct r8a779f0_eth_serdes_drv_data *dd = channel->dd;
  80. switch (channel->phy_interface) {
  81. case PHY_INTERFACE_MODE_SGMII:
  82. r8a779f0_eth_serdes_write32(dd->addr, 0x0244, 0x180, 0x0097);
  83. r8a779f0_eth_serdes_write32(dd->addr, 0x01d0, 0x180, 0x0060);
  84. r8a779f0_eth_serdes_write32(dd->addr, 0x01d8, 0x180, 0x2200);
  85. r8a779f0_eth_serdes_write32(dd->addr, 0x01d4, 0x180, 0x0000);
  86. r8a779f0_eth_serdes_write32(dd->addr, 0x01e0, 0x180, 0x003d);
  87. return 0;
  88. default:
  89. return -EOPNOTSUPP;
  90. }
  91. }
  92. static int
  93. r8a779f0_eth_serdes_chan_setting(struct r8a779f0_eth_serdes_channel *channel)
  94. {
  95. int ret;
  96. switch (channel->phy_interface) {
  97. case PHY_INTERFACE_MODE_SGMII:
  98. r8a779f0_eth_serdes_write32(channel->addr, 0x0000, 0x380, 0x2000);
  99. r8a779f0_eth_serdes_write32(channel->addr, 0x01c0, 0x180, 0x0011);
  100. r8a779f0_eth_serdes_write32(channel->addr, 0x0248, 0x180, 0x0540);
  101. r8a779f0_eth_serdes_write32(channel->addr, 0x0258, 0x180, 0x0015);
  102. r8a779f0_eth_serdes_write32(channel->addr, 0x0144, 0x180, 0x0100);
  103. r8a779f0_eth_serdes_write32(channel->addr, 0x01a0, 0x180, 0x0000);
  104. r8a779f0_eth_serdes_write32(channel->addr, 0x00d0, 0x180, 0x0002);
  105. r8a779f0_eth_serdes_write32(channel->addr, 0x0150, 0x180, 0x0003);
  106. r8a779f0_eth_serdes_write32(channel->addr, 0x00c8, 0x180, 0x0100);
  107. r8a779f0_eth_serdes_write32(channel->addr, 0x0148, 0x180, 0x0100);
  108. r8a779f0_eth_serdes_write32(channel->addr, 0x0174, 0x180, 0x0000);
  109. r8a779f0_eth_serdes_write32(channel->addr, 0x0160, 0x180, 0x0007);
  110. r8a779f0_eth_serdes_write32(channel->addr, 0x01ac, 0x180, 0x0000);
  111. r8a779f0_eth_serdes_write32(channel->addr, 0x00c4, 0x180, 0x0310);
  112. r8a779f0_eth_serdes_write32(channel->addr, 0x00c8, 0x180, 0x0101);
  113. ret = r8a779f0_eth_serdes_reg_wait(channel, 0x00c8, 0x0180, BIT(0), 0);
  114. if (ret)
  115. return ret;
  116. r8a779f0_eth_serdes_write32(channel->addr, 0x0148, 0x180, 0x0101);
  117. ret = r8a779f0_eth_serdes_reg_wait(channel, 0x0148, 0x0180, BIT(0), 0);
  118. if (ret)
  119. return ret;
  120. r8a779f0_eth_serdes_write32(channel->addr, 0x00c4, 0x180, 0x1310);
  121. r8a779f0_eth_serdes_write32(channel->addr, 0x00d8, 0x180, 0x1800);
  122. r8a779f0_eth_serdes_write32(channel->addr, 0x00dc, 0x180, 0x0000);
  123. r8a779f0_eth_serdes_write32(channel->addr, 0x001c, 0x300, 0x0001);
  124. r8a779f0_eth_serdes_write32(channel->addr, 0x0000, 0x380, 0x2100);
  125. ret = r8a779f0_eth_serdes_reg_wait(channel, 0x0000, 0x0380, BIT(8), 0);
  126. if (ret)
  127. return ret;
  128. if (channel->speed == 1000)
  129. r8a779f0_eth_serdes_write32(channel->addr, 0x0000, 0x1f00, 0x0140);
  130. else if (channel->speed == 100)
  131. r8a779f0_eth_serdes_write32(channel->addr, 0x0000, 0x1f00, 0x2100);
  132. /* For AN_ON */
  133. r8a779f0_eth_serdes_write32(channel->addr, 0x0004, 0x1f80, 0x0005);
  134. r8a779f0_eth_serdes_write32(channel->addr, 0x0028, 0x1f80, 0x07a1);
  135. r8a779f0_eth_serdes_write32(channel->addr, 0x0000, 0x1f80, 0x0208);
  136. break;
  137. default:
  138. return -EOPNOTSUPP;
  139. }
  140. return 0;
  141. }
  142. static int
  143. r8a779f0_eth_serdes_chan_speed(struct r8a779f0_eth_serdes_channel *channel)
  144. {
  145. int ret;
  146. switch (channel->phy_interface) {
  147. case PHY_INTERFACE_MODE_SGMII:
  148. /* For AN_ON */
  149. if (channel->speed == 1000)
  150. r8a779f0_eth_serdes_write32(channel->addr, 0x0000, 0x1f00, 0x1140);
  151. else if (channel->speed == 100)
  152. r8a779f0_eth_serdes_write32(channel->addr, 0x0000, 0x1f00, 0x3100);
  153. ret = r8a779f0_eth_serdes_reg_wait(channel, 0x0008, 0x1f80, BIT(0), 1);
  154. if (ret)
  155. return ret;
  156. r8a779f0_eth_serdes_write32(channel->addr, 0x0008, 0x1f80, 0x0000);
  157. break;
  158. default:
  159. return -EOPNOTSUPP;
  160. }
  161. return 0;
  162. }
  163. static int r8a779f0_eth_serdes_monitor_linkup(struct r8a779f0_eth_serdes_channel *channel)
  164. {
  165. int i, ret;
  166. for (i = 0; i < R8A779F0_ETH_SERDES_NUM_RETRY_LINKUP; i++) {
  167. ret = r8a779f0_eth_serdes_reg_wait(channel, 0x0004, 0x300,
  168. BIT(2), BIT(2));
  169. if (!ret)
  170. break;
  171. /* restart */
  172. r8a779f0_eth_serdes_write32(channel->addr, 0x0144, 0x180, 0x0100);
  173. udelay(1);
  174. r8a779f0_eth_serdes_write32(channel->addr, 0x0144, 0x180, 0x0000);
  175. }
  176. return ret;
  177. }
  178. static int r8a779f0_eth_serdes_hw_init(struct r8a779f0_eth_serdes_channel *channel)
  179. {
  180. struct r8a779f0_eth_serdes_drv_data *dd = channel->dd;
  181. int i, ret;
  182. if (dd->initialized)
  183. return 0;
  184. reset_control_reset(dd->reset);
  185. usleep_range(1000, 2000);
  186. ret = r8a779f0_eth_serdes_common_init_ram(dd);
  187. if (ret)
  188. return ret;
  189. for (i = 0; i < R8A779F0_ETH_SERDES_NUM; i++) {
  190. ret = r8a779f0_eth_serdes_reg_wait(&dd->channel[i], 0x0000,
  191. 0x300, BIT(15), 0);
  192. if (ret)
  193. return ret;
  194. }
  195. for (i = 0; i < R8A779F0_ETH_SERDES_NUM; i++)
  196. r8a779f0_eth_serdes_write32(dd->channel[i].addr, 0x03d4, 0x380, 0x0443);
  197. ret = r8a779f0_eth_serdes_common_setting(channel);
  198. if (ret)
  199. return ret;
  200. for (i = 0; i < R8A779F0_ETH_SERDES_NUM; i++)
  201. r8a779f0_eth_serdes_write32(dd->channel[i].addr, 0x03d0, 0x380, 0x0001);
  202. r8a779f0_eth_serdes_write32(dd->addr, 0x0000, 0x380, 0x8000);
  203. ret = r8a779f0_eth_serdes_common_init_ram(dd);
  204. if (ret)
  205. return ret;
  206. return r8a779f0_eth_serdes_reg_wait(&dd->channel[0], 0x0000, 0x380, BIT(15), 0);
  207. }
  208. static int r8a779f0_eth_serdes_init(struct phy *p)
  209. {
  210. struct r8a779f0_eth_serdes_channel *channel = phy_get_drvdata(p);
  211. int ret;
  212. ret = r8a779f0_eth_serdes_hw_init(channel);
  213. if (!ret)
  214. channel->dd->initialized = true;
  215. return ret;
  216. }
  217. static int r8a779f0_eth_serdes_exit(struct phy *p)
  218. {
  219. struct r8a779f0_eth_serdes_channel *channel = phy_get_drvdata(p);
  220. channel->dd->initialized = false;
  221. return 0;
  222. }
  223. static int r8a779f0_eth_serdes_hw_init_late(struct r8a779f0_eth_serdes_channel
  224. *channel)
  225. {
  226. int ret;
  227. ret = r8a779f0_eth_serdes_chan_setting(channel);
  228. if (ret)
  229. return ret;
  230. ret = r8a779f0_eth_serdes_chan_speed(channel);
  231. if (ret)
  232. return ret;
  233. r8a779f0_eth_serdes_write32(channel->addr, 0x03c0, 0x380, 0x0000);
  234. r8a779f0_eth_serdes_write32(channel->addr, 0x03d0, 0x380, 0x0000);
  235. return r8a779f0_eth_serdes_monitor_linkup(channel);
  236. }
  237. static int r8a779f0_eth_serdes_power_on(struct phy *p)
  238. {
  239. struct r8a779f0_eth_serdes_channel *channel = phy_get_drvdata(p);
  240. return r8a779f0_eth_serdes_hw_init_late(channel);
  241. }
  242. static int r8a779f0_eth_serdes_set_mode(struct phy *p, enum phy_mode mode,
  243. int submode)
  244. {
  245. struct r8a779f0_eth_serdes_channel *channel = phy_get_drvdata(p);
  246. if (mode != PHY_MODE_ETHERNET)
  247. return -EOPNOTSUPP;
  248. switch (submode) {
  249. case PHY_INTERFACE_MODE_GMII:
  250. case PHY_INTERFACE_MODE_SGMII:
  251. case PHY_INTERFACE_MODE_USXGMII:
  252. channel->phy_interface = submode;
  253. return 0;
  254. default:
  255. return -EOPNOTSUPP;
  256. }
  257. }
  258. static int r8a779f0_eth_serdes_set_speed(struct phy *p, int speed)
  259. {
  260. struct r8a779f0_eth_serdes_channel *channel = phy_get_drvdata(p);
  261. channel->speed = speed;
  262. return 0;
  263. }
  264. static const struct phy_ops r8a779f0_eth_serdes_ops = {
  265. .init = r8a779f0_eth_serdes_init,
  266. .exit = r8a779f0_eth_serdes_exit,
  267. .power_on = r8a779f0_eth_serdes_power_on,
  268. .set_mode = r8a779f0_eth_serdes_set_mode,
  269. .set_speed = r8a779f0_eth_serdes_set_speed,
  270. };
  271. static struct phy *r8a779f0_eth_serdes_xlate(struct device *dev,
  272. const struct of_phandle_args *args)
  273. {
  274. struct r8a779f0_eth_serdes_drv_data *dd = dev_get_drvdata(dev);
  275. if (args->args[0] >= R8A779F0_ETH_SERDES_NUM)
  276. return ERR_PTR(-ENODEV);
  277. return dd->channel[args->args[0]].phy;
  278. }
  279. static const struct of_device_id r8a779f0_eth_serdes_of_table[] = {
  280. { .compatible = "renesas,r8a779f0-ether-serdes", },
  281. { }
  282. };
  283. MODULE_DEVICE_TABLE(of, r8a779f0_eth_serdes_of_table);
  284. static int r8a779f0_eth_serdes_probe(struct platform_device *pdev)
  285. {
  286. struct r8a779f0_eth_serdes_drv_data *dd;
  287. struct phy_provider *provider;
  288. int i;
  289. dd = devm_kzalloc(&pdev->dev, sizeof(*dd), GFP_KERNEL);
  290. if (!dd)
  291. return -ENOMEM;
  292. platform_set_drvdata(pdev, dd);
  293. dd->pdev = pdev;
  294. dd->addr = devm_platform_ioremap_resource(pdev, 0);
  295. if (IS_ERR(dd->addr))
  296. return PTR_ERR(dd->addr);
  297. dd->reset = devm_reset_control_get(&pdev->dev, NULL);
  298. if (IS_ERR(dd->reset))
  299. return PTR_ERR(dd->reset);
  300. for (i = 0; i < R8A779F0_ETH_SERDES_NUM; i++) {
  301. struct r8a779f0_eth_serdes_channel *channel = &dd->channel[i];
  302. channel->phy = devm_phy_create(&pdev->dev, NULL,
  303. &r8a779f0_eth_serdes_ops);
  304. if (IS_ERR(channel->phy))
  305. return PTR_ERR(channel->phy);
  306. channel->addr = dd->addr + R8A779F0_ETH_SERDES_OFFSET * i;
  307. channel->dd = dd;
  308. channel->index = i;
  309. phy_set_drvdata(channel->phy, channel);
  310. }
  311. provider = devm_of_phy_provider_register(&pdev->dev,
  312. r8a779f0_eth_serdes_xlate);
  313. if (IS_ERR(provider))
  314. return PTR_ERR(provider);
  315. pm_runtime_enable(&pdev->dev);
  316. pm_runtime_get_sync(&pdev->dev);
  317. return 0;
  318. }
  319. static void r8a779f0_eth_serdes_remove(struct platform_device *pdev)
  320. {
  321. pm_runtime_put(&pdev->dev);
  322. pm_runtime_disable(&pdev->dev);
  323. platform_set_drvdata(pdev, NULL);
  324. }
  325. static struct platform_driver r8a779f0_eth_serdes_driver_platform = {
  326. .probe = r8a779f0_eth_serdes_probe,
  327. .remove_new = r8a779f0_eth_serdes_remove,
  328. .driver = {
  329. .name = "r8a779f0_eth_serdes",
  330. .of_match_table = r8a779f0_eth_serdes_of_table,
  331. }
  332. };
  333. module_platform_driver(r8a779f0_eth_serdes_driver_platform);
  334. MODULE_AUTHOR("Yoshihiro Shimoda");
  335. MODULE_DESCRIPTION("Renesas Ethernet SERDES device driver");
  336. MODULE_LICENSE("GPL");