mdio-xgene.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /* Applied Micro X-Gene SoC MDIO Driver
  3. *
  4. * Copyright (c) 2016, Applied Micro Circuits Corporation
  5. * Author: Iyappan Subramanian <isubramanian@apm.com>
  6. */
  7. #include <linux/acpi.h>
  8. #include <linux/clk.h>
  9. #include <linux/device.h>
  10. #include <linux/efi.h>
  11. #include <linux/if_vlan.h>
  12. #include <linux/io.h>
  13. #include <linux/module.h>
  14. #include <linux/of_platform.h>
  15. #include <linux/of_net.h>
  16. #include <linux/of_mdio.h>
  17. #include <linux/prefetch.h>
  18. #include <linux/phy.h>
  19. #include <net/ip.h>
  20. #include "mdio-xgene.h"
  21. static bool xgene_mdio_status;
  22. u32 xgene_mdio_rd_mac(struct xgene_mdio_pdata *pdata, u32 rd_addr)
  23. {
  24. void __iomem *addr, *rd, *cmd, *cmd_done;
  25. u32 done, rd_data = BUSY_MASK;
  26. u8 wait = 10;
  27. addr = pdata->mac_csr_addr + MAC_ADDR_REG_OFFSET;
  28. rd = pdata->mac_csr_addr + MAC_READ_REG_OFFSET;
  29. cmd = pdata->mac_csr_addr + MAC_COMMAND_REG_OFFSET;
  30. cmd_done = pdata->mac_csr_addr + MAC_COMMAND_DONE_REG_OFFSET;
  31. spin_lock(&pdata->mac_lock);
  32. iowrite32(rd_addr, addr);
  33. iowrite32(XGENE_ENET_RD_CMD, cmd);
  34. while (!(done = ioread32(cmd_done)) && wait--)
  35. udelay(1);
  36. if (done)
  37. rd_data = ioread32(rd);
  38. iowrite32(0, cmd);
  39. spin_unlock(&pdata->mac_lock);
  40. return rd_data;
  41. }
  42. EXPORT_SYMBOL(xgene_mdio_rd_mac);
  43. void xgene_mdio_wr_mac(struct xgene_mdio_pdata *pdata, u32 wr_addr, u32 data)
  44. {
  45. void __iomem *addr, *wr, *cmd, *cmd_done;
  46. u8 wait = 10;
  47. u32 done;
  48. addr = pdata->mac_csr_addr + MAC_ADDR_REG_OFFSET;
  49. wr = pdata->mac_csr_addr + MAC_WRITE_REG_OFFSET;
  50. cmd = pdata->mac_csr_addr + MAC_COMMAND_REG_OFFSET;
  51. cmd_done = pdata->mac_csr_addr + MAC_COMMAND_DONE_REG_OFFSET;
  52. spin_lock(&pdata->mac_lock);
  53. iowrite32(wr_addr, addr);
  54. iowrite32(data, wr);
  55. iowrite32(XGENE_ENET_WR_CMD, cmd);
  56. while (!(done = ioread32(cmd_done)) && wait--)
  57. udelay(1);
  58. if (!done)
  59. pr_err("MCX mac write failed, addr: 0x%04x\n", wr_addr);
  60. iowrite32(0, cmd);
  61. spin_unlock(&pdata->mac_lock);
  62. }
  63. EXPORT_SYMBOL(xgene_mdio_wr_mac);
  64. int xgene_mdio_rgmii_read(struct mii_bus *bus, int phy_id, int reg)
  65. {
  66. struct xgene_mdio_pdata *pdata = (struct xgene_mdio_pdata *)bus->priv;
  67. u32 data, done;
  68. u8 wait = 10;
  69. data = SET_VAL(PHY_ADDR, phy_id) | SET_VAL(REG_ADDR, reg);
  70. xgene_mdio_wr_mac(pdata, MII_MGMT_ADDRESS_ADDR, data);
  71. xgene_mdio_wr_mac(pdata, MII_MGMT_COMMAND_ADDR, READ_CYCLE_MASK);
  72. do {
  73. usleep_range(5, 10);
  74. done = xgene_mdio_rd_mac(pdata, MII_MGMT_INDICATORS_ADDR);
  75. } while ((done & BUSY_MASK) && wait--);
  76. if (done & BUSY_MASK) {
  77. dev_err(&bus->dev, "MII_MGMT read failed\n");
  78. return -EBUSY;
  79. }
  80. data = xgene_mdio_rd_mac(pdata, MII_MGMT_STATUS_ADDR);
  81. xgene_mdio_wr_mac(pdata, MII_MGMT_COMMAND_ADDR, 0);
  82. return data;
  83. }
  84. EXPORT_SYMBOL(xgene_mdio_rgmii_read);
  85. int xgene_mdio_rgmii_write(struct mii_bus *bus, int phy_id, int reg, u16 data)
  86. {
  87. struct xgene_mdio_pdata *pdata = (struct xgene_mdio_pdata *)bus->priv;
  88. u32 val, done;
  89. u8 wait = 10;
  90. val = SET_VAL(PHY_ADDR, phy_id) | SET_VAL(REG_ADDR, reg);
  91. xgene_mdio_wr_mac(pdata, MII_MGMT_ADDRESS_ADDR, val);
  92. xgene_mdio_wr_mac(pdata, MII_MGMT_CONTROL_ADDR, data);
  93. do {
  94. usleep_range(5, 10);
  95. done = xgene_mdio_rd_mac(pdata, MII_MGMT_INDICATORS_ADDR);
  96. } while ((done & BUSY_MASK) && wait--);
  97. if (done & BUSY_MASK) {
  98. dev_err(&bus->dev, "MII_MGMT write failed\n");
  99. return -EBUSY;
  100. }
  101. return 0;
  102. }
  103. EXPORT_SYMBOL(xgene_mdio_rgmii_write);
  104. static u32 xgene_menet_rd_diag_csr(struct xgene_mdio_pdata *pdata, u32 offset)
  105. {
  106. return ioread32(pdata->diag_csr_addr + offset);
  107. }
  108. static void xgene_menet_wr_diag_csr(struct xgene_mdio_pdata *pdata,
  109. u32 offset, u32 val)
  110. {
  111. iowrite32(val, pdata->diag_csr_addr + offset);
  112. }
  113. static int xgene_enet_ecc_init(struct xgene_mdio_pdata *pdata)
  114. {
  115. u32 data;
  116. u8 wait = 10;
  117. xgene_menet_wr_diag_csr(pdata, MENET_CFG_MEM_RAM_SHUTDOWN_ADDR, 0x0);
  118. do {
  119. usleep_range(100, 110);
  120. data = xgene_menet_rd_diag_csr(pdata, MENET_BLOCK_MEM_RDY_ADDR);
  121. } while ((data != 0xffffffff) && wait--);
  122. if (data != 0xffffffff) {
  123. dev_err(pdata->dev, "Failed to release memory from shutdown\n");
  124. return -ENODEV;
  125. }
  126. return 0;
  127. }
  128. static void xgene_gmac_reset(struct xgene_mdio_pdata *pdata)
  129. {
  130. xgene_mdio_wr_mac(pdata, MAC_CONFIG_1_ADDR, SOFT_RESET);
  131. xgene_mdio_wr_mac(pdata, MAC_CONFIG_1_ADDR, 0);
  132. }
  133. static int xgene_mdio_reset(struct xgene_mdio_pdata *pdata)
  134. {
  135. int ret;
  136. if (pdata->dev->of_node) {
  137. clk_prepare_enable(pdata->clk);
  138. udelay(5);
  139. clk_disable_unprepare(pdata->clk);
  140. udelay(5);
  141. clk_prepare_enable(pdata->clk);
  142. udelay(5);
  143. } else {
  144. #ifdef CONFIG_ACPI
  145. acpi_evaluate_object(ACPI_HANDLE(pdata->dev),
  146. "_RST", NULL, NULL);
  147. #endif
  148. }
  149. ret = xgene_enet_ecc_init(pdata);
  150. if (ret) {
  151. if (pdata->dev->of_node)
  152. clk_disable_unprepare(pdata->clk);
  153. return ret;
  154. }
  155. xgene_gmac_reset(pdata);
  156. return 0;
  157. }
  158. static void xgene_enet_rd_mdio_csr(void __iomem *base_addr,
  159. u32 offset, u32 *val)
  160. {
  161. void __iomem *addr = base_addr + offset;
  162. *val = ioread32(addr);
  163. }
  164. static void xgene_enet_wr_mdio_csr(void __iomem *base_addr,
  165. u32 offset, u32 val)
  166. {
  167. void __iomem *addr = base_addr + offset;
  168. iowrite32(val, addr);
  169. }
  170. static int xgene_xfi_mdio_write(struct mii_bus *bus, int phy_id,
  171. int reg, u16 data)
  172. {
  173. void __iomem *addr = (void __iomem *)bus->priv;
  174. int timeout = 100;
  175. u32 status, val;
  176. val = SET_VAL(HSTPHYADX, phy_id) | SET_VAL(HSTREGADX, reg) |
  177. SET_VAL(HSTMIIMWRDAT, data);
  178. xgene_enet_wr_mdio_csr(addr, MIIM_FIELD_ADDR, val);
  179. val = HSTLDCMD | SET_VAL(HSTMIIMCMD, MIIM_CMD_LEGACY_WRITE);
  180. xgene_enet_wr_mdio_csr(addr, MIIM_COMMAND_ADDR, val);
  181. do {
  182. usleep_range(5, 10);
  183. xgene_enet_rd_mdio_csr(addr, MIIM_INDICATOR_ADDR, &status);
  184. } while ((status & BUSY_MASK) && timeout--);
  185. xgene_enet_wr_mdio_csr(addr, MIIM_COMMAND_ADDR, 0);
  186. return 0;
  187. }
  188. static int xgene_xfi_mdio_read(struct mii_bus *bus, int phy_id, int reg)
  189. {
  190. void __iomem *addr = (void __iomem *)bus->priv;
  191. u32 data, status, val;
  192. int timeout = 100;
  193. val = SET_VAL(HSTPHYADX, phy_id) | SET_VAL(HSTREGADX, reg);
  194. xgene_enet_wr_mdio_csr(addr, MIIM_FIELD_ADDR, val);
  195. val = HSTLDCMD | SET_VAL(HSTMIIMCMD, MIIM_CMD_LEGACY_READ);
  196. xgene_enet_wr_mdio_csr(addr, MIIM_COMMAND_ADDR, val);
  197. do {
  198. usleep_range(5, 10);
  199. xgene_enet_rd_mdio_csr(addr, MIIM_INDICATOR_ADDR, &status);
  200. } while ((status & BUSY_MASK) && timeout--);
  201. if (status & BUSY_MASK) {
  202. pr_err("XGENET_MII_MGMT write failed\n");
  203. return -EBUSY;
  204. }
  205. xgene_enet_rd_mdio_csr(addr, MIIMRD_FIELD_ADDR, &data);
  206. xgene_enet_wr_mdio_csr(addr, MIIM_COMMAND_ADDR, 0);
  207. return data;
  208. }
  209. struct phy_device *xgene_enet_phy_register(struct mii_bus *bus, int phy_addr)
  210. {
  211. struct phy_device *phy_dev;
  212. phy_dev = get_phy_device(bus, phy_addr, false);
  213. if (!phy_dev || IS_ERR(phy_dev))
  214. return NULL;
  215. if (phy_device_register(phy_dev))
  216. phy_device_free(phy_dev);
  217. return phy_dev;
  218. }
  219. EXPORT_SYMBOL(xgene_enet_phy_register);
  220. #ifdef CONFIG_ACPI
  221. static acpi_status acpi_register_phy(acpi_handle handle, u32 lvl,
  222. void *context, void **ret)
  223. {
  224. struct mii_bus *mdio = context;
  225. struct acpi_device *adev;
  226. struct phy_device *phy_dev;
  227. const union acpi_object *obj;
  228. u32 phy_addr;
  229. if (acpi_bus_get_device(handle, &adev))
  230. return AE_OK;
  231. if (acpi_dev_get_property(adev, "phy-channel", ACPI_TYPE_INTEGER, &obj))
  232. return AE_OK;
  233. phy_addr = obj->integer.value;
  234. phy_dev = xgene_enet_phy_register(mdio, phy_addr);
  235. adev->driver_data = phy_dev;
  236. return AE_OK;
  237. }
  238. #endif
  239. static const struct of_device_id xgene_mdio_of_match[] = {
  240. {
  241. .compatible = "apm,xgene-mdio-rgmii",
  242. .data = (void *)XGENE_MDIO_RGMII
  243. },
  244. {
  245. .compatible = "apm,xgene-mdio-xfi",
  246. .data = (void *)XGENE_MDIO_XFI
  247. },
  248. {},
  249. };
  250. MODULE_DEVICE_TABLE(of, xgene_mdio_of_match);
  251. #ifdef CONFIG_ACPI
  252. static const struct acpi_device_id xgene_mdio_acpi_match[] = {
  253. { "APMC0D65", XGENE_MDIO_RGMII },
  254. { "APMC0D66", XGENE_MDIO_XFI },
  255. { }
  256. };
  257. MODULE_DEVICE_TABLE(acpi, xgene_mdio_acpi_match);
  258. #endif
  259. static int xgene_mdio_probe(struct platform_device *pdev)
  260. {
  261. struct device *dev = &pdev->dev;
  262. struct mii_bus *mdio_bus;
  263. const struct of_device_id *of_id;
  264. struct xgene_mdio_pdata *pdata;
  265. void __iomem *csr_base;
  266. int mdio_id = 0, ret = 0;
  267. of_id = of_match_device(xgene_mdio_of_match, &pdev->dev);
  268. if (of_id) {
  269. mdio_id = (enum xgene_mdio_id)of_id->data;
  270. } else {
  271. #ifdef CONFIG_ACPI
  272. const struct acpi_device_id *acpi_id;
  273. acpi_id = acpi_match_device(xgene_mdio_acpi_match, &pdev->dev);
  274. if (acpi_id)
  275. mdio_id = (enum xgene_mdio_id)acpi_id->driver_data;
  276. #endif
  277. }
  278. if (!mdio_id)
  279. return -ENODEV;
  280. pdata = devm_kzalloc(dev, sizeof(struct xgene_mdio_pdata), GFP_KERNEL);
  281. if (!pdata)
  282. return -ENOMEM;
  283. pdata->mdio_id = mdio_id;
  284. pdata->dev = dev;
  285. csr_base = devm_platform_ioremap_resource(pdev, 0);
  286. if (IS_ERR(csr_base))
  287. return PTR_ERR(csr_base);
  288. pdata->mac_csr_addr = csr_base;
  289. pdata->mdio_csr_addr = csr_base + BLOCK_XG_MDIO_CSR_OFFSET;
  290. pdata->diag_csr_addr = csr_base + BLOCK_DIAG_CSR_OFFSET;
  291. if (mdio_id == XGENE_MDIO_RGMII)
  292. spin_lock_init(&pdata->mac_lock);
  293. if (dev->of_node) {
  294. pdata->clk = devm_clk_get(dev, NULL);
  295. if (IS_ERR(pdata->clk)) {
  296. dev_err(dev, "Unable to retrieve clk\n");
  297. return PTR_ERR(pdata->clk);
  298. }
  299. }
  300. ret = xgene_mdio_reset(pdata);
  301. if (ret)
  302. return ret;
  303. mdio_bus = mdiobus_alloc();
  304. if (!mdio_bus) {
  305. ret = -ENOMEM;
  306. goto out_clk;
  307. }
  308. mdio_bus->name = "APM X-Gene MDIO bus";
  309. if (mdio_id == XGENE_MDIO_RGMII) {
  310. mdio_bus->read = xgene_mdio_rgmii_read;
  311. mdio_bus->write = xgene_mdio_rgmii_write;
  312. mdio_bus->priv = (void __force *)pdata;
  313. snprintf(mdio_bus->id, MII_BUS_ID_SIZE, "%s",
  314. "xgene-mii-rgmii");
  315. } else {
  316. mdio_bus->read = xgene_xfi_mdio_read;
  317. mdio_bus->write = xgene_xfi_mdio_write;
  318. mdio_bus->priv = (void __force *)pdata->mdio_csr_addr;
  319. snprintf(mdio_bus->id, MII_BUS_ID_SIZE, "%s",
  320. "xgene-mii-xfi");
  321. }
  322. mdio_bus->parent = dev;
  323. platform_set_drvdata(pdev, pdata);
  324. if (dev->of_node) {
  325. ret = of_mdiobus_register(mdio_bus, dev->of_node);
  326. } else {
  327. #ifdef CONFIG_ACPI
  328. /* Mask out all PHYs from auto probing. */
  329. mdio_bus->phy_mask = ~0;
  330. ret = mdiobus_register(mdio_bus);
  331. if (ret)
  332. goto out_mdiobus;
  333. acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_HANDLE(dev), 1,
  334. acpi_register_phy, NULL, mdio_bus, NULL);
  335. #endif
  336. }
  337. if (ret)
  338. goto out_mdiobus;
  339. pdata->mdio_bus = mdio_bus;
  340. xgene_mdio_status = true;
  341. return 0;
  342. out_mdiobus:
  343. mdiobus_free(mdio_bus);
  344. out_clk:
  345. if (dev->of_node)
  346. clk_disable_unprepare(pdata->clk);
  347. return ret;
  348. }
  349. static int xgene_mdio_remove(struct platform_device *pdev)
  350. {
  351. struct xgene_mdio_pdata *pdata = platform_get_drvdata(pdev);
  352. struct mii_bus *mdio_bus = pdata->mdio_bus;
  353. struct device *dev = &pdev->dev;
  354. mdiobus_unregister(mdio_bus);
  355. mdiobus_free(mdio_bus);
  356. if (dev->of_node)
  357. clk_disable_unprepare(pdata->clk);
  358. return 0;
  359. }
  360. static struct platform_driver xgene_mdio_driver = {
  361. .driver = {
  362. .name = "xgene-mdio",
  363. .of_match_table = of_match_ptr(xgene_mdio_of_match),
  364. .acpi_match_table = ACPI_PTR(xgene_mdio_acpi_match),
  365. },
  366. .probe = xgene_mdio_probe,
  367. .remove = xgene_mdio_remove,
  368. };
  369. module_platform_driver(xgene_mdio_driver);
  370. MODULE_DESCRIPTION("APM X-Gene SoC MDIO driver");
  371. MODULE_AUTHOR("Iyappan Subramanian <isubramanian@apm.com>");
  372. MODULE_LICENSE("GPL");