mvmdio.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /*
  2. * Driver for the MDIO interface of Marvell network interfaces.
  3. *
  4. * Since the MDIO interface of Marvell network interfaces is shared
  5. * between all network interfaces, having a single driver allows to
  6. * handle concurrent accesses properly (you may have four Ethernet
  7. * ports, but they in fact share the same SMI interface to access
  8. * the MDIO bus). This driver is currently used by the mvneta and
  9. * mv643xx_eth drivers.
  10. *
  11. * Copyright (C) 2012 Marvell
  12. *
  13. * Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
  14. *
  15. * This file is licensed under the terms of the GNU General Public
  16. * License version 2. This program is licensed "as is" without any
  17. * warranty of any kind, whether express or implied.
  18. */
  19. #include <linux/acpi.h>
  20. #include <linux/acpi_mdio.h>
  21. #include <linux/clk.h>
  22. #include <linux/delay.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/io.h>
  25. #include <linux/iopoll.h>
  26. #include <linux/kernel.h>
  27. #include <linux/mod_devicetable.h>
  28. #include <linux/module.h>
  29. #include <linux/of_mdio.h>
  30. #include <linux/phy.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/sched.h>
  33. #include <linux/wait.h>
  34. #define MVMDIO_SMI_DATA_SHIFT 0
  35. #define MVMDIO_SMI_PHY_ADDR_SHIFT 16
  36. #define MVMDIO_SMI_PHY_REG_SHIFT 21
  37. #define MVMDIO_SMI_READ_OPERATION BIT(26)
  38. #define MVMDIO_SMI_WRITE_OPERATION 0
  39. #define MVMDIO_SMI_READ_VALID BIT(27)
  40. #define MVMDIO_SMI_BUSY BIT(28)
  41. #define MVMDIO_ERR_INT_CAUSE 0x007C
  42. #define MVMDIO_ERR_INT_SMI_DONE 0x00000010
  43. #define MVMDIO_ERR_INT_MASK 0x0080
  44. #define MVMDIO_XSMI_MGNT_REG 0x0
  45. #define MVMDIO_XSMI_PHYADDR_SHIFT 16
  46. #define MVMDIO_XSMI_DEVADDR_SHIFT 21
  47. #define MVMDIO_XSMI_WRITE_OPERATION (0x5 << 26)
  48. #define MVMDIO_XSMI_READ_OPERATION (0x7 << 26)
  49. #define MVMDIO_XSMI_READ_VALID BIT(29)
  50. #define MVMDIO_XSMI_BUSY BIT(30)
  51. #define MVMDIO_XSMI_ADDR_REG 0x8
  52. #define MVMDIO_XSMI_CFG_REG 0xc
  53. #define MVMDIO_XSMI_CLKDIV_MASK 0x3
  54. #define MVMDIO_XSMI_CLKDIV_256 0x0
  55. #define MVMDIO_XSMI_CLKDIV_64 0x1
  56. #define MVMDIO_XSMI_CLKDIV_32 0x2
  57. #define MVMDIO_XSMI_CLKDIV_8 0x3
  58. /*
  59. * SMI Timeout measurements:
  60. * - Kirkwood 88F6281 (Globalscale Dreamplug): 45us to 95us (Interrupt)
  61. * - Armada 370 (Globalscale Mirabox): 41us to 43us (Polled)
  62. */
  63. #define MVMDIO_SMI_TIMEOUT 1000 /* 1000us = 1ms */
  64. struct orion_mdio_dev {
  65. void __iomem *regs;
  66. struct clk *clk[4];
  67. /*
  68. * If we have access to the error interrupt pin (which is
  69. * somewhat misnamed as it not only reflects internal errors
  70. * but also reflects SMI completion), use that to wait for
  71. * SMI access completion instead of polling the SMI busy bit.
  72. */
  73. int err_interrupt;
  74. wait_queue_head_t smi_busy_wait;
  75. };
  76. enum orion_mdio_bus_type {
  77. BUS_TYPE_SMI,
  78. BUS_TYPE_XSMI
  79. };
  80. struct orion_mdio_ops {
  81. int (*is_done)(struct orion_mdio_dev *);
  82. };
  83. /* Wait for the SMI unit to be ready for another operation
  84. */
  85. static int orion_mdio_wait_ready(const struct orion_mdio_ops *ops,
  86. struct mii_bus *bus)
  87. {
  88. struct orion_mdio_dev *dev = bus->priv;
  89. unsigned long timeout;
  90. int done;
  91. if (dev->err_interrupt <= 0) {
  92. if (!read_poll_timeout_atomic(ops->is_done, done, done, 2,
  93. MVMDIO_SMI_TIMEOUT, false, dev))
  94. return 0;
  95. } else {
  96. /* wait_event_timeout does not guarantee a delay of at
  97. * least one whole jiffy, so timeout must be no less
  98. * than two.
  99. */
  100. timeout = max(usecs_to_jiffies(MVMDIO_SMI_TIMEOUT), 2);
  101. if (wait_event_timeout(dev->smi_busy_wait,
  102. ops->is_done(dev), timeout))
  103. return 0;
  104. }
  105. dev_err(bus->parent, "Timeout: SMI busy for too long\n");
  106. return -ETIMEDOUT;
  107. }
  108. static int orion_mdio_smi_is_done(struct orion_mdio_dev *dev)
  109. {
  110. return !(readl(dev->regs) & MVMDIO_SMI_BUSY);
  111. }
  112. static const struct orion_mdio_ops orion_mdio_smi_ops = {
  113. .is_done = orion_mdio_smi_is_done,
  114. };
  115. static int orion_mdio_smi_read(struct mii_bus *bus, int mii_id,
  116. int regnum)
  117. {
  118. struct orion_mdio_dev *dev = bus->priv;
  119. u32 val;
  120. int ret;
  121. ret = orion_mdio_wait_ready(&orion_mdio_smi_ops, bus);
  122. if (ret < 0)
  123. return ret;
  124. writel(((mii_id << MVMDIO_SMI_PHY_ADDR_SHIFT) |
  125. (regnum << MVMDIO_SMI_PHY_REG_SHIFT) |
  126. MVMDIO_SMI_READ_OPERATION),
  127. dev->regs);
  128. ret = orion_mdio_wait_ready(&orion_mdio_smi_ops, bus);
  129. if (ret < 0)
  130. return ret;
  131. val = readl(dev->regs);
  132. if (!(val & MVMDIO_SMI_READ_VALID)) {
  133. dev_err(bus->parent, "SMI bus read not valid\n");
  134. return -ENODEV;
  135. }
  136. return val & GENMASK(15, 0);
  137. }
  138. static int orion_mdio_smi_write(struct mii_bus *bus, int mii_id,
  139. int regnum, u16 value)
  140. {
  141. struct orion_mdio_dev *dev = bus->priv;
  142. int ret;
  143. ret = orion_mdio_wait_ready(&orion_mdio_smi_ops, bus);
  144. if (ret < 0)
  145. return ret;
  146. writel(((mii_id << MVMDIO_SMI_PHY_ADDR_SHIFT) |
  147. (regnum << MVMDIO_SMI_PHY_REG_SHIFT) |
  148. MVMDIO_SMI_WRITE_OPERATION |
  149. (value << MVMDIO_SMI_DATA_SHIFT)),
  150. dev->regs);
  151. return 0;
  152. }
  153. static int orion_mdio_xsmi_is_done(struct orion_mdio_dev *dev)
  154. {
  155. return !(readl(dev->regs + MVMDIO_XSMI_MGNT_REG) & MVMDIO_XSMI_BUSY);
  156. }
  157. static const struct orion_mdio_ops orion_mdio_xsmi_ops = {
  158. .is_done = orion_mdio_xsmi_is_done,
  159. };
  160. static int orion_mdio_xsmi_read_c45(struct mii_bus *bus, int mii_id,
  161. int dev_addr, int regnum)
  162. {
  163. struct orion_mdio_dev *dev = bus->priv;
  164. int ret;
  165. ret = orion_mdio_wait_ready(&orion_mdio_xsmi_ops, bus);
  166. if (ret < 0)
  167. return ret;
  168. writel(regnum, dev->regs + MVMDIO_XSMI_ADDR_REG);
  169. writel((mii_id << MVMDIO_XSMI_PHYADDR_SHIFT) |
  170. (dev_addr << MVMDIO_XSMI_DEVADDR_SHIFT) |
  171. MVMDIO_XSMI_READ_OPERATION,
  172. dev->regs + MVMDIO_XSMI_MGNT_REG);
  173. ret = orion_mdio_wait_ready(&orion_mdio_xsmi_ops, bus);
  174. if (ret < 0)
  175. return ret;
  176. if (!(readl(dev->regs + MVMDIO_XSMI_MGNT_REG) &
  177. MVMDIO_XSMI_READ_VALID)) {
  178. dev_err(bus->parent, "XSMI bus read not valid\n");
  179. return -ENODEV;
  180. }
  181. return readl(dev->regs + MVMDIO_XSMI_MGNT_REG) & GENMASK(15, 0);
  182. }
  183. static int orion_mdio_xsmi_write_c45(struct mii_bus *bus, int mii_id,
  184. int dev_addr, int regnum, u16 value)
  185. {
  186. struct orion_mdio_dev *dev = bus->priv;
  187. int ret;
  188. ret = orion_mdio_wait_ready(&orion_mdio_xsmi_ops, bus);
  189. if (ret < 0)
  190. return ret;
  191. writel(regnum, dev->regs + MVMDIO_XSMI_ADDR_REG);
  192. writel((mii_id << MVMDIO_XSMI_PHYADDR_SHIFT) |
  193. (dev_addr << MVMDIO_XSMI_DEVADDR_SHIFT) |
  194. MVMDIO_XSMI_WRITE_OPERATION | value,
  195. dev->regs + MVMDIO_XSMI_MGNT_REG);
  196. return 0;
  197. }
  198. static void orion_mdio_xsmi_set_mdc_freq(struct mii_bus *bus)
  199. {
  200. struct orion_mdio_dev *dev = bus->priv;
  201. struct clk *mg_core;
  202. u32 div, freq, cfg;
  203. if (device_property_read_u32(bus->parent, "clock-frequency", &freq))
  204. return;
  205. mg_core = of_clk_get_by_name(bus->parent->of_node, "mg_core_clk");
  206. if (IS_ERR(mg_core)) {
  207. dev_err(bus->parent,
  208. "MG core clock unknown, not changing MDC frequency");
  209. return;
  210. }
  211. div = clk_get_rate(mg_core) / (freq + 1) + 1;
  212. clk_put(mg_core);
  213. if (div <= 8)
  214. div = MVMDIO_XSMI_CLKDIV_8;
  215. else if (div <= 32)
  216. div = MVMDIO_XSMI_CLKDIV_32;
  217. else if (div <= 64)
  218. div = MVMDIO_XSMI_CLKDIV_64;
  219. else
  220. div = MVMDIO_XSMI_CLKDIV_256;
  221. cfg = readl(dev->regs + MVMDIO_XSMI_CFG_REG);
  222. cfg &= ~MVMDIO_XSMI_CLKDIV_MASK;
  223. cfg |= div;
  224. writel(cfg, dev->regs + MVMDIO_XSMI_CFG_REG);
  225. }
  226. static irqreturn_t orion_mdio_err_irq(int irq, void *dev_id)
  227. {
  228. struct orion_mdio_dev *dev = dev_id;
  229. if (readl(dev->regs + MVMDIO_ERR_INT_CAUSE) &
  230. MVMDIO_ERR_INT_SMI_DONE) {
  231. writel(~MVMDIO_ERR_INT_SMI_DONE,
  232. dev->regs + MVMDIO_ERR_INT_CAUSE);
  233. wake_up(&dev->smi_busy_wait);
  234. return IRQ_HANDLED;
  235. }
  236. return IRQ_NONE;
  237. }
  238. static int orion_mdio_probe(struct platform_device *pdev)
  239. {
  240. enum orion_mdio_bus_type type;
  241. struct resource *r;
  242. struct mii_bus *bus;
  243. struct orion_mdio_dev *dev;
  244. int i, ret;
  245. type = (uintptr_t)device_get_match_data(&pdev->dev);
  246. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  247. if (!r) {
  248. dev_err(&pdev->dev, "No SMI register address given\n");
  249. return -ENODEV;
  250. }
  251. bus = devm_mdiobus_alloc_size(&pdev->dev,
  252. sizeof(struct orion_mdio_dev));
  253. if (!bus)
  254. return -ENOMEM;
  255. switch (type) {
  256. case BUS_TYPE_SMI:
  257. bus->read = orion_mdio_smi_read;
  258. bus->write = orion_mdio_smi_write;
  259. break;
  260. case BUS_TYPE_XSMI:
  261. bus->read_c45 = orion_mdio_xsmi_read_c45;
  262. bus->write_c45 = orion_mdio_xsmi_write_c45;
  263. break;
  264. }
  265. bus->name = "orion_mdio_bus";
  266. snprintf(bus->id, MII_BUS_ID_SIZE, "%s-mii",
  267. dev_name(&pdev->dev));
  268. bus->parent = &pdev->dev;
  269. dev = bus->priv;
  270. dev->regs = devm_ioremap(&pdev->dev, r->start, resource_size(r));
  271. if (!dev->regs) {
  272. dev_err(&pdev->dev, "Unable to remap SMI register\n");
  273. return -ENODEV;
  274. }
  275. init_waitqueue_head(&dev->smi_busy_wait);
  276. if (pdev->dev.of_node) {
  277. for (i = 0; i < ARRAY_SIZE(dev->clk); i++) {
  278. dev->clk[i] = of_clk_get(pdev->dev.of_node, i);
  279. if (PTR_ERR(dev->clk[i]) == -EPROBE_DEFER) {
  280. ret = -EPROBE_DEFER;
  281. goto out_clk;
  282. }
  283. if (IS_ERR(dev->clk[i]))
  284. break;
  285. clk_prepare_enable(dev->clk[i]);
  286. }
  287. if (!IS_ERR(of_clk_get(pdev->dev.of_node,
  288. ARRAY_SIZE(dev->clk))))
  289. dev_warn(&pdev->dev,
  290. "unsupported number of clocks, limiting to the first "
  291. __stringify(ARRAY_SIZE(dev->clk)) "\n");
  292. if (type == BUS_TYPE_XSMI)
  293. orion_mdio_xsmi_set_mdc_freq(bus);
  294. } else {
  295. dev->clk[0] = clk_get(&pdev->dev, NULL);
  296. if (PTR_ERR(dev->clk[0]) == -EPROBE_DEFER) {
  297. ret = -EPROBE_DEFER;
  298. goto out_clk;
  299. }
  300. if (!IS_ERR(dev->clk[0]))
  301. clk_prepare_enable(dev->clk[0]);
  302. }
  303. dev->err_interrupt = platform_get_irq_optional(pdev, 0);
  304. if (dev->err_interrupt > 0 &&
  305. resource_size(r) < MVMDIO_ERR_INT_MASK + 4) {
  306. dev_err(&pdev->dev,
  307. "disabling interrupt, resource size is too small\n");
  308. dev->err_interrupt = 0;
  309. }
  310. if (dev->err_interrupt > 0) {
  311. ret = devm_request_irq(&pdev->dev, dev->err_interrupt,
  312. orion_mdio_err_irq,
  313. IRQF_SHARED, pdev->name, dev);
  314. if (ret)
  315. goto out_mdio;
  316. writel(MVMDIO_ERR_INT_SMI_DONE,
  317. dev->regs + MVMDIO_ERR_INT_MASK);
  318. } else if (dev->err_interrupt == -EPROBE_DEFER) {
  319. ret = -EPROBE_DEFER;
  320. goto out_mdio;
  321. }
  322. /* For the platforms not supporting DT/ACPI fall-back
  323. * to mdiobus_register via of_mdiobus_register.
  324. */
  325. if (is_acpi_node(pdev->dev.fwnode))
  326. ret = acpi_mdiobus_register(bus, pdev->dev.fwnode);
  327. else
  328. ret = of_mdiobus_register(bus, pdev->dev.of_node);
  329. if (ret < 0) {
  330. dev_err(&pdev->dev, "Cannot register MDIO bus (%d)\n", ret);
  331. goto out_mdio;
  332. }
  333. platform_set_drvdata(pdev, bus);
  334. return 0;
  335. out_mdio:
  336. if (dev->err_interrupt > 0)
  337. writel(0, dev->regs + MVMDIO_ERR_INT_MASK);
  338. out_clk:
  339. for (i = 0; i < ARRAY_SIZE(dev->clk); i++) {
  340. if (IS_ERR(dev->clk[i]))
  341. break;
  342. clk_disable_unprepare(dev->clk[i]);
  343. clk_put(dev->clk[i]);
  344. }
  345. return ret;
  346. }
  347. static void orion_mdio_remove(struct platform_device *pdev)
  348. {
  349. struct mii_bus *bus = platform_get_drvdata(pdev);
  350. struct orion_mdio_dev *dev = bus->priv;
  351. int i;
  352. if (dev->err_interrupt > 0)
  353. writel(0, dev->regs + MVMDIO_ERR_INT_MASK);
  354. mdiobus_unregister(bus);
  355. for (i = 0; i < ARRAY_SIZE(dev->clk); i++) {
  356. if (IS_ERR(dev->clk[i]))
  357. break;
  358. clk_disable_unprepare(dev->clk[i]);
  359. clk_put(dev->clk[i]);
  360. }
  361. }
  362. static const struct of_device_id orion_mdio_match[] = {
  363. { .compatible = "marvell,orion-mdio", .data = (void *)BUS_TYPE_SMI },
  364. { .compatible = "marvell,xmdio", .data = (void *)BUS_TYPE_XSMI },
  365. { }
  366. };
  367. MODULE_DEVICE_TABLE(of, orion_mdio_match);
  368. #ifdef CONFIG_ACPI
  369. static const struct acpi_device_id orion_mdio_acpi_match[] = {
  370. { "MRVL0100", BUS_TYPE_SMI },
  371. { "MRVL0101", BUS_TYPE_XSMI },
  372. { },
  373. };
  374. MODULE_DEVICE_TABLE(acpi, orion_mdio_acpi_match);
  375. #endif
  376. static struct platform_driver orion_mdio_driver = {
  377. .probe = orion_mdio_probe,
  378. .remove_new = orion_mdio_remove,
  379. .driver = {
  380. .name = "orion-mdio",
  381. .of_match_table = orion_mdio_match,
  382. .acpi_match_table = ACPI_PTR(orion_mdio_acpi_match),
  383. },
  384. };
  385. module_platform_driver(orion_mdio_driver);
  386. MODULE_DESCRIPTION("Marvell MDIO interface driver");
  387. MODULE_AUTHOR("Thomas Petazzoni <thomas.petazzoni@free-electrons.com>");
  388. MODULE_LICENSE("GPL");
  389. MODULE_ALIAS("platform:orion-mdio");