mdio-mux-bcm-iproc.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2016 Broadcom
  4. */
  5. #include <linux/align.h>
  6. #include <linux/clk.h>
  7. #include <linux/delay.h>
  8. #include <linux/device.h>
  9. #include <linux/iopoll.h>
  10. #include <linux/mdio-mux.h>
  11. #include <linux/module.h>
  12. #include <linux/of_mdio.h>
  13. #include <linux/phy.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/sizes.h>
  16. #define MDIO_RATE_ADJ_EXT_OFFSET 0x000
  17. #define MDIO_RATE_ADJ_INT_OFFSET 0x004
  18. #define MDIO_RATE_ADJ_DIVIDENT_SHIFT 16
  19. #define MDIO_SCAN_CTRL_OFFSET 0x008
  20. #define MDIO_SCAN_CTRL_OVRIDE_EXT_MSTR 28
  21. #define MDIO_PARAM_OFFSET 0x23c
  22. #define MDIO_PARAM_MIIM_CYCLE 29
  23. #define MDIO_PARAM_INTERNAL_SEL 25
  24. #define MDIO_PARAM_BUS_ID 22
  25. #define MDIO_PARAM_C45_SEL 21
  26. #define MDIO_PARAM_PHY_ID 16
  27. #define MDIO_PARAM_PHY_DATA 0
  28. #define MDIO_READ_OFFSET 0x240
  29. #define MDIO_READ_DATA_MASK 0xffff
  30. #define MDIO_ADDR_OFFSET 0x244
  31. #define MDIO_CTRL_OFFSET 0x248
  32. #define MDIO_CTRL_WRITE_OP 0x1
  33. #define MDIO_CTRL_READ_OP 0x2
  34. #define MDIO_STAT_OFFSET 0x24c
  35. #define MDIO_STAT_DONE 1
  36. #define BUS_MAX_ADDR 32
  37. #define EXT_BUS_START_ADDR 16
  38. #define MDIO_REG_ADDR_SPACE_SIZE 0x250
  39. #define MDIO_OPERATING_FREQUENCY 11000000
  40. #define MDIO_RATE_ADJ_DIVIDENT 1
  41. struct iproc_mdiomux_desc {
  42. void *mux_handle;
  43. void __iomem *base;
  44. struct device *dev;
  45. struct mii_bus *mii_bus;
  46. struct clk *core_clk;
  47. };
  48. static void mdio_mux_iproc_config(struct iproc_mdiomux_desc *md)
  49. {
  50. u32 divisor;
  51. u32 val;
  52. /* Disable external mdio master access */
  53. val = readl(md->base + MDIO_SCAN_CTRL_OFFSET);
  54. val |= BIT(MDIO_SCAN_CTRL_OVRIDE_EXT_MSTR);
  55. writel(val, md->base + MDIO_SCAN_CTRL_OFFSET);
  56. if (md->core_clk) {
  57. /* use rate adjust regs to derive the mdio's operating
  58. * frequency from the specified core clock
  59. */
  60. divisor = clk_get_rate(md->core_clk) / MDIO_OPERATING_FREQUENCY;
  61. divisor = divisor / (MDIO_RATE_ADJ_DIVIDENT + 1);
  62. val = divisor;
  63. val |= MDIO_RATE_ADJ_DIVIDENT << MDIO_RATE_ADJ_DIVIDENT_SHIFT;
  64. writel(val, md->base + MDIO_RATE_ADJ_EXT_OFFSET);
  65. writel(val, md->base + MDIO_RATE_ADJ_INT_OFFSET);
  66. }
  67. }
  68. static int iproc_mdio_wait_for_idle(void __iomem *base, bool result)
  69. {
  70. u32 val;
  71. return readl_poll_timeout(base + MDIO_STAT_OFFSET, val,
  72. (val & MDIO_STAT_DONE) == result,
  73. 2000, 1000000);
  74. }
  75. /* start_miim_ops- Program and start MDIO transaction over mdio bus.
  76. * @base: Base address
  77. * @phyid: phyid of the selected bus.
  78. * @reg: register offset to be read/written.
  79. * @val :0 if read op else value to be written in @reg;
  80. * @op: Operation that need to be carried out.
  81. * MDIO_CTRL_READ_OP: Read transaction.
  82. * MDIO_CTRL_WRITE_OP: Write transaction.
  83. *
  84. * Return value: Successful Read operation returns read reg values and write
  85. * operation returns 0. Failure operation returns negative error code.
  86. */
  87. static int start_miim_ops(void __iomem *base, bool c45,
  88. u16 phyid, u32 reg, u16 val, u32 op)
  89. {
  90. u32 param;
  91. int ret;
  92. writel(0, base + MDIO_CTRL_OFFSET);
  93. ret = iproc_mdio_wait_for_idle(base, 0);
  94. if (ret)
  95. goto err;
  96. param = readl(base + MDIO_PARAM_OFFSET);
  97. param |= phyid << MDIO_PARAM_PHY_ID;
  98. param |= val << MDIO_PARAM_PHY_DATA;
  99. if (c45)
  100. param |= BIT(MDIO_PARAM_C45_SEL);
  101. writel(param, base + MDIO_PARAM_OFFSET);
  102. writel(reg, base + MDIO_ADDR_OFFSET);
  103. writel(op, base + MDIO_CTRL_OFFSET);
  104. ret = iproc_mdio_wait_for_idle(base, 1);
  105. if (ret)
  106. goto err;
  107. if (op == MDIO_CTRL_READ_OP)
  108. ret = readl(base + MDIO_READ_OFFSET) & MDIO_READ_DATA_MASK;
  109. err:
  110. return ret;
  111. }
  112. static int iproc_mdiomux_read_c22(struct mii_bus *bus, int phyid, int reg)
  113. {
  114. struct iproc_mdiomux_desc *md = bus->priv;
  115. int ret;
  116. ret = start_miim_ops(md->base, false, phyid, reg, 0, MDIO_CTRL_READ_OP);
  117. if (ret < 0)
  118. dev_err(&bus->dev, "mdiomux c22 read operation failed!!!");
  119. return ret;
  120. }
  121. static int iproc_mdiomux_read_c45(struct mii_bus *bus, int phyid, int devad,
  122. int reg)
  123. {
  124. struct iproc_mdiomux_desc *md = bus->priv;
  125. int ret;
  126. ret = start_miim_ops(md->base, true, phyid, reg | devad << 16, 0,
  127. MDIO_CTRL_READ_OP);
  128. if (ret < 0)
  129. dev_err(&bus->dev, "mdiomux read c45 operation failed!!!");
  130. return ret;
  131. }
  132. static int iproc_mdiomux_write_c22(struct mii_bus *bus,
  133. int phyid, int reg, u16 val)
  134. {
  135. struct iproc_mdiomux_desc *md = bus->priv;
  136. int ret;
  137. /* Write val at reg offset */
  138. ret = start_miim_ops(md->base, false, phyid, reg, val,
  139. MDIO_CTRL_WRITE_OP);
  140. if (ret < 0)
  141. dev_err(&bus->dev, "mdiomux write c22 operation failed!!!");
  142. return ret;
  143. }
  144. static int iproc_mdiomux_write_c45(struct mii_bus *bus,
  145. int phyid, int devad, int reg, u16 val)
  146. {
  147. struct iproc_mdiomux_desc *md = bus->priv;
  148. int ret;
  149. /* Write val at reg offset */
  150. ret = start_miim_ops(md->base, true, phyid, reg | devad << 16, val,
  151. MDIO_CTRL_WRITE_OP);
  152. if (ret < 0)
  153. dev_err(&bus->dev, "mdiomux write c45 operation failed!!!");
  154. return ret;
  155. }
  156. static int mdio_mux_iproc_switch_fn(int current_child, int desired_child,
  157. void *data)
  158. {
  159. struct iproc_mdiomux_desc *md = data;
  160. u32 param, bus_id;
  161. bool bus_dir;
  162. /* select bus and its properties */
  163. bus_dir = (desired_child < EXT_BUS_START_ADDR);
  164. bus_id = bus_dir ? desired_child : (desired_child - EXT_BUS_START_ADDR);
  165. param = (bus_dir ? 1 : 0) << MDIO_PARAM_INTERNAL_SEL;
  166. param |= (bus_id << MDIO_PARAM_BUS_ID);
  167. writel(param, md->base + MDIO_PARAM_OFFSET);
  168. return 0;
  169. }
  170. static int mdio_mux_iproc_probe(struct platform_device *pdev)
  171. {
  172. struct iproc_mdiomux_desc *md;
  173. struct mii_bus *bus;
  174. struct resource *res;
  175. int rc;
  176. md = devm_kzalloc(&pdev->dev, sizeof(*md), GFP_KERNEL);
  177. if (!md)
  178. return -ENOMEM;
  179. md->dev = &pdev->dev;
  180. md->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
  181. if (IS_ERR(md->base))
  182. return PTR_ERR(md->base);
  183. if (!IS_ALIGNED(res->start, SZ_4K)) {
  184. /* For backward compatibility in case the
  185. * base address is specified with an offset.
  186. */
  187. dev_info(&pdev->dev, "fix base address in dt-blob\n");
  188. res->start = ALIGN_DOWN(res->start, SZ_4K);
  189. res->end = res->start + MDIO_REG_ADDR_SPACE_SIZE - 1;
  190. }
  191. md->mii_bus = devm_mdiobus_alloc(&pdev->dev);
  192. if (!md->mii_bus) {
  193. dev_err(&pdev->dev, "mdiomux bus alloc failed\n");
  194. return -ENOMEM;
  195. }
  196. md->core_clk = devm_clk_get(&pdev->dev, NULL);
  197. if (md->core_clk == ERR_PTR(-ENOENT) ||
  198. md->core_clk == ERR_PTR(-EINVAL))
  199. md->core_clk = NULL;
  200. else if (IS_ERR(md->core_clk))
  201. return PTR_ERR(md->core_clk);
  202. rc = clk_prepare_enable(md->core_clk);
  203. if (rc) {
  204. dev_err(&pdev->dev, "failed to enable core clk\n");
  205. return rc;
  206. }
  207. bus = md->mii_bus;
  208. bus->priv = md;
  209. bus->name = "iProc MDIO mux bus";
  210. snprintf(bus->id, MII_BUS_ID_SIZE, "%s-%d", pdev->name, pdev->id);
  211. bus->parent = &pdev->dev;
  212. bus->read = iproc_mdiomux_read_c22;
  213. bus->write = iproc_mdiomux_write_c22;
  214. bus->read_c45 = iproc_mdiomux_read_c45;
  215. bus->write_c45 = iproc_mdiomux_write_c45;
  216. bus->phy_mask = ~0;
  217. bus->dev.of_node = pdev->dev.of_node;
  218. rc = mdiobus_register(bus);
  219. if (rc) {
  220. dev_err(&pdev->dev, "mdiomux registration failed\n");
  221. goto out_clk;
  222. }
  223. platform_set_drvdata(pdev, md);
  224. rc = mdio_mux_init(md->dev, md->dev->of_node, mdio_mux_iproc_switch_fn,
  225. &md->mux_handle, md, md->mii_bus);
  226. if (rc) {
  227. dev_info(md->dev, "mdiomux initialization failed\n");
  228. goto out_register;
  229. }
  230. mdio_mux_iproc_config(md);
  231. dev_info(md->dev, "iProc mdiomux registered\n");
  232. return 0;
  233. out_register:
  234. mdiobus_unregister(bus);
  235. out_clk:
  236. clk_disable_unprepare(md->core_clk);
  237. return rc;
  238. }
  239. static void mdio_mux_iproc_remove(struct platform_device *pdev)
  240. {
  241. struct iproc_mdiomux_desc *md = platform_get_drvdata(pdev);
  242. mdio_mux_uninit(md->mux_handle);
  243. mdiobus_unregister(md->mii_bus);
  244. clk_disable_unprepare(md->core_clk);
  245. }
  246. #ifdef CONFIG_PM_SLEEP
  247. static int mdio_mux_iproc_suspend(struct device *dev)
  248. {
  249. struct iproc_mdiomux_desc *md = dev_get_drvdata(dev);
  250. clk_disable_unprepare(md->core_clk);
  251. return 0;
  252. }
  253. static int mdio_mux_iproc_resume(struct device *dev)
  254. {
  255. struct iproc_mdiomux_desc *md = dev_get_drvdata(dev);
  256. int rc;
  257. rc = clk_prepare_enable(md->core_clk);
  258. if (rc) {
  259. dev_err(md->dev, "failed to enable core clk\n");
  260. return rc;
  261. }
  262. mdio_mux_iproc_config(md);
  263. return 0;
  264. }
  265. #endif
  266. static SIMPLE_DEV_PM_OPS(mdio_mux_iproc_pm_ops,
  267. mdio_mux_iproc_suspend, mdio_mux_iproc_resume);
  268. static const struct of_device_id mdio_mux_iproc_match[] = {
  269. {
  270. .compatible = "brcm,mdio-mux-iproc",
  271. },
  272. {},
  273. };
  274. MODULE_DEVICE_TABLE(of, mdio_mux_iproc_match);
  275. static struct platform_driver mdiomux_iproc_driver = {
  276. .driver = {
  277. .name = "mdio-mux-iproc",
  278. .of_match_table = mdio_mux_iproc_match,
  279. .pm = &mdio_mux_iproc_pm_ops,
  280. },
  281. .probe = mdio_mux_iproc_probe,
  282. .remove_new = mdio_mux_iproc_remove,
  283. };
  284. module_platform_driver(mdiomux_iproc_driver);
  285. MODULE_DESCRIPTION("iProc MDIO Mux Bus Driver");
  286. MODULE_AUTHOR("Pramod Kumar <pramod.kumar@broadcom.com>");
  287. MODULE_LICENSE("GPL v2");