davinci_mdio.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /*
  2. * DaVinci MDIO Module driver
  3. *
  4. * Copyright (C) 2010 Texas Instruments.
  5. *
  6. * Shamelessly ripped out of davinci_emac.c, original copyrights follow:
  7. *
  8. * Copyright (C) 2009 Texas Instruments.
  9. *
  10. * ---------------------------------------------------------------------------
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. * ---------------------------------------------------------------------------
  26. */
  27. #include <linux/module.h>
  28. #include <linux/kernel.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/delay.h>
  31. #include <linux/sched.h>
  32. #include <linux/slab.h>
  33. #include <linux/phy.h>
  34. #include <linux/clk.h>
  35. #include <linux/err.h>
  36. #include <linux/io.h>
  37. #include <linux/iopoll.h>
  38. #include <linux/pm_runtime.h>
  39. #include <linux/davinci_emac.h>
  40. #include <linux/of.h>
  41. #include <linux/of_device.h>
  42. #include <linux/of_mdio.h>
  43. #include <linux/pinctrl/consumer.h>
  44. /*
  45. * This timeout definition is a worst-case ultra defensive measure against
  46. * unexpected controller lock ups. Ideally, we should never ever hit this
  47. * scenario in practice.
  48. */
  49. #define MDIO_TIMEOUT 100 /* msecs */
  50. #define PHY_REG_MASK 0x1f
  51. #define PHY_ID_MASK 0x1f
  52. #define DEF_OUT_FREQ 2200000 /* 2.2 MHz */
  53. struct davinci_mdio_of_param {
  54. int autosuspend_delay_ms;
  55. };
  56. struct davinci_mdio_regs {
  57. u32 version;
  58. u32 control;
  59. #define CONTROL_IDLE BIT(31)
  60. #define CONTROL_ENABLE BIT(30)
  61. #define CONTROL_MAX_DIV (0xffff)
  62. u32 alive;
  63. u32 link;
  64. u32 linkintraw;
  65. u32 linkintmasked;
  66. u32 __reserved_0[2];
  67. u32 userintraw;
  68. u32 userintmasked;
  69. u32 userintmaskset;
  70. u32 userintmaskclr;
  71. u32 __reserved_1[20];
  72. struct {
  73. u32 access;
  74. #define USERACCESS_GO BIT(31)
  75. #define USERACCESS_WRITE BIT(30)
  76. #define USERACCESS_ACK BIT(29)
  77. #define USERACCESS_READ (0)
  78. #define USERACCESS_DATA (0xffff)
  79. u32 physel;
  80. } user[0];
  81. };
  82. static const struct mdio_platform_data default_pdata = {
  83. .bus_freq = DEF_OUT_FREQ,
  84. };
  85. struct davinci_mdio_data {
  86. struct mdio_platform_data pdata;
  87. struct davinci_mdio_regs __iomem *regs;
  88. struct clk *clk;
  89. struct device *dev;
  90. struct mii_bus *bus;
  91. bool active_in_suspend;
  92. unsigned long access_time; /* jiffies */
  93. /* Indicates that driver shouldn't modify phy_mask in case
  94. * if MDIO bus is registered from DT.
  95. */
  96. bool skip_scan;
  97. u32 clk_div;
  98. };
  99. static void davinci_mdio_init_clk(struct davinci_mdio_data *data)
  100. {
  101. u32 mdio_in, div, mdio_out_khz, access_time;
  102. mdio_in = clk_get_rate(data->clk);
  103. div = (mdio_in / data->pdata.bus_freq) - 1;
  104. if (div > CONTROL_MAX_DIV)
  105. div = CONTROL_MAX_DIV;
  106. data->clk_div = div;
  107. /*
  108. * One mdio transaction consists of:
  109. * 32 bits of preamble
  110. * 32 bits of transferred data
  111. * 24 bits of bus yield (not needed unless shared?)
  112. */
  113. mdio_out_khz = mdio_in / (1000 * (div + 1));
  114. access_time = (88 * 1000) / mdio_out_khz;
  115. /*
  116. * In the worst case, we could be kicking off a user-access immediately
  117. * after the mdio bus scan state-machine triggered its own read. If
  118. * so, our request could get deferred by one access cycle. We
  119. * defensively allow for 4 access cycles.
  120. */
  121. data->access_time = usecs_to_jiffies(access_time * 4);
  122. if (!data->access_time)
  123. data->access_time = 1;
  124. }
  125. static void davinci_mdio_enable(struct davinci_mdio_data *data)
  126. {
  127. /* set enable and clock divider */
  128. __raw_writel(data->clk_div | CONTROL_ENABLE, &data->regs->control);
  129. }
  130. static int davinci_mdio_reset(struct mii_bus *bus)
  131. {
  132. struct davinci_mdio_data *data = bus->priv;
  133. u32 phy_mask, ver;
  134. int ret;
  135. ret = pm_runtime_get_sync(data->dev);
  136. if (ret < 0) {
  137. pm_runtime_put_noidle(data->dev);
  138. return ret;
  139. }
  140. /* wait for scan logic to settle */
  141. msleep(PHY_MAX_ADDR * data->access_time);
  142. /* dump hardware version info */
  143. ver = __raw_readl(&data->regs->version);
  144. dev_info(data->dev,
  145. "davinci mdio revision %d.%d, bus freq %ld\n",
  146. (ver >> 8) & 0xff, ver & 0xff,
  147. data->pdata.bus_freq);
  148. if (data->skip_scan)
  149. goto done;
  150. /* get phy mask from the alive register */
  151. phy_mask = __raw_readl(&data->regs->alive);
  152. if (phy_mask) {
  153. /* restrict mdio bus to live phys only */
  154. dev_info(data->dev, "detected phy mask %x\n", ~phy_mask);
  155. phy_mask = ~phy_mask;
  156. } else {
  157. /* desperately scan all phys */
  158. dev_warn(data->dev, "no live phy, scanning all\n");
  159. phy_mask = 0;
  160. }
  161. data->bus->phy_mask = phy_mask;
  162. done:
  163. pm_runtime_mark_last_busy(data->dev);
  164. pm_runtime_put_autosuspend(data->dev);
  165. return 0;
  166. }
  167. /* wait until hardware is ready for another user access */
  168. static inline int wait_for_user_access(struct davinci_mdio_data *data)
  169. {
  170. struct davinci_mdio_regs __iomem *regs = data->regs;
  171. unsigned long timeout = jiffies + msecs_to_jiffies(MDIO_TIMEOUT);
  172. u32 reg;
  173. while (time_after(timeout, jiffies)) {
  174. reg = __raw_readl(&regs->user[0].access);
  175. if ((reg & USERACCESS_GO) == 0)
  176. return 0;
  177. reg = __raw_readl(&regs->control);
  178. if ((reg & CONTROL_IDLE) == 0) {
  179. usleep_range(100, 200);
  180. continue;
  181. }
  182. /*
  183. * An emac soft_reset may have clobbered the mdio controller's
  184. * state machine. We need to reset and retry the current
  185. * operation
  186. */
  187. dev_warn(data->dev, "resetting idled controller\n");
  188. davinci_mdio_enable(data);
  189. return -EAGAIN;
  190. }
  191. reg = __raw_readl(&regs->user[0].access);
  192. if ((reg & USERACCESS_GO) == 0)
  193. return 0;
  194. dev_err(data->dev, "timed out waiting for user access\n");
  195. return -ETIMEDOUT;
  196. }
  197. /* wait until hardware state machine is idle */
  198. static inline int wait_for_idle(struct davinci_mdio_data *data)
  199. {
  200. struct davinci_mdio_regs __iomem *regs = data->regs;
  201. u32 val, ret;
  202. ret = readl_poll_timeout(&regs->control, val, val & CONTROL_IDLE,
  203. 0, MDIO_TIMEOUT * 1000);
  204. if (ret)
  205. dev_err(data->dev, "timed out waiting for idle\n");
  206. return ret;
  207. }
  208. static int davinci_mdio_read(struct mii_bus *bus, int phy_id, int phy_reg)
  209. {
  210. struct davinci_mdio_data *data = bus->priv;
  211. u32 reg;
  212. int ret;
  213. if (phy_reg & ~PHY_REG_MASK || phy_id & ~PHY_ID_MASK)
  214. return -EINVAL;
  215. ret = pm_runtime_get_sync(data->dev);
  216. if (ret < 0) {
  217. pm_runtime_put_noidle(data->dev);
  218. return ret;
  219. }
  220. reg = (USERACCESS_GO | USERACCESS_READ | (phy_reg << 21) |
  221. (phy_id << 16));
  222. while (1) {
  223. ret = wait_for_user_access(data);
  224. if (ret == -EAGAIN)
  225. continue;
  226. if (ret < 0)
  227. break;
  228. __raw_writel(reg, &data->regs->user[0].access);
  229. ret = wait_for_user_access(data);
  230. if (ret == -EAGAIN)
  231. continue;
  232. if (ret < 0)
  233. break;
  234. reg = __raw_readl(&data->regs->user[0].access);
  235. ret = (reg & USERACCESS_ACK) ? (reg & USERACCESS_DATA) : -EIO;
  236. break;
  237. }
  238. pm_runtime_mark_last_busy(data->dev);
  239. pm_runtime_put_autosuspend(data->dev);
  240. return ret;
  241. }
  242. static int davinci_mdio_write(struct mii_bus *bus, int phy_id,
  243. int phy_reg, u16 phy_data)
  244. {
  245. struct davinci_mdio_data *data = bus->priv;
  246. u32 reg;
  247. int ret;
  248. if (phy_reg & ~PHY_REG_MASK || phy_id & ~PHY_ID_MASK)
  249. return -EINVAL;
  250. ret = pm_runtime_get_sync(data->dev);
  251. if (ret < 0) {
  252. pm_runtime_put_noidle(data->dev);
  253. return ret;
  254. }
  255. reg = (USERACCESS_GO | USERACCESS_WRITE | (phy_reg << 21) |
  256. (phy_id << 16) | (phy_data & USERACCESS_DATA));
  257. while (1) {
  258. ret = wait_for_user_access(data);
  259. if (ret == -EAGAIN)
  260. continue;
  261. if (ret < 0)
  262. break;
  263. __raw_writel(reg, &data->regs->user[0].access);
  264. ret = wait_for_user_access(data);
  265. if (ret == -EAGAIN)
  266. continue;
  267. break;
  268. }
  269. pm_runtime_mark_last_busy(data->dev);
  270. pm_runtime_put_autosuspend(data->dev);
  271. return ret;
  272. }
  273. static int davinci_mdio_probe_dt(struct mdio_platform_data *data,
  274. struct platform_device *pdev)
  275. {
  276. struct device_node *node = pdev->dev.of_node;
  277. u32 prop;
  278. if (!node)
  279. return -EINVAL;
  280. if (of_property_read_u32(node, "bus_freq", &prop)) {
  281. dev_err(&pdev->dev, "Missing bus_freq property in the DT.\n");
  282. return -EINVAL;
  283. }
  284. data->bus_freq = prop;
  285. return 0;
  286. }
  287. #if IS_ENABLED(CONFIG_OF)
  288. static const struct davinci_mdio_of_param of_cpsw_mdio_data = {
  289. .autosuspend_delay_ms = 100,
  290. };
  291. static const struct of_device_id davinci_mdio_of_mtable[] = {
  292. { .compatible = "ti,davinci_mdio", },
  293. { .compatible = "ti,cpsw-mdio", .data = &of_cpsw_mdio_data},
  294. { /* sentinel */ },
  295. };
  296. MODULE_DEVICE_TABLE(of, davinci_mdio_of_mtable);
  297. #endif
  298. static int davinci_mdio_probe(struct platform_device *pdev)
  299. {
  300. struct mdio_platform_data *pdata = dev_get_platdata(&pdev->dev);
  301. struct device *dev = &pdev->dev;
  302. struct davinci_mdio_data *data;
  303. struct resource *res;
  304. struct phy_device *phy;
  305. int ret, addr;
  306. int autosuspend_delay_ms = -1;
  307. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  308. if (!data)
  309. return -ENOMEM;
  310. data->bus = devm_mdiobus_alloc(dev);
  311. if (!data->bus) {
  312. dev_err(dev, "failed to alloc mii bus\n");
  313. return -ENOMEM;
  314. }
  315. if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
  316. const struct of_device_id *of_id;
  317. ret = davinci_mdio_probe_dt(&data->pdata, pdev);
  318. if (ret)
  319. return ret;
  320. snprintf(data->bus->id, MII_BUS_ID_SIZE, "%s", pdev->name);
  321. of_id = of_match_device(davinci_mdio_of_mtable, &pdev->dev);
  322. if (of_id) {
  323. const struct davinci_mdio_of_param *of_mdio_data;
  324. of_mdio_data = of_id->data;
  325. if (of_mdio_data)
  326. autosuspend_delay_ms =
  327. of_mdio_data->autosuspend_delay_ms;
  328. }
  329. } else {
  330. data->pdata = pdata ? (*pdata) : default_pdata;
  331. snprintf(data->bus->id, MII_BUS_ID_SIZE, "%s-%x",
  332. pdev->name, pdev->id);
  333. }
  334. data->bus->name = dev_name(dev);
  335. data->bus->read = davinci_mdio_read,
  336. data->bus->write = davinci_mdio_write,
  337. data->bus->reset = davinci_mdio_reset,
  338. data->bus->parent = dev;
  339. data->bus->priv = data;
  340. data->clk = devm_clk_get(dev, "fck");
  341. if (IS_ERR(data->clk)) {
  342. dev_err(dev, "failed to get device clock\n");
  343. return PTR_ERR(data->clk);
  344. }
  345. dev_set_drvdata(dev, data);
  346. data->dev = dev;
  347. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  348. data->regs = devm_ioremap_resource(dev, res);
  349. if (IS_ERR(data->regs))
  350. return PTR_ERR(data->regs);
  351. davinci_mdio_init_clk(data);
  352. pm_runtime_set_autosuspend_delay(&pdev->dev, autosuspend_delay_ms);
  353. pm_runtime_use_autosuspend(&pdev->dev);
  354. pm_runtime_enable(&pdev->dev);
  355. /* register the mii bus
  356. * Create PHYs from DT only in case if PHY child nodes are explicitly
  357. * defined to support backward compatibility with DTs which assume that
  358. * Davinci MDIO will always scan the bus for PHYs detection.
  359. */
  360. if (dev->of_node && of_get_child_count(dev->of_node))
  361. data->skip_scan = true;
  362. ret = of_mdiobus_register(data->bus, dev->of_node);
  363. if (ret)
  364. goto bail_out;
  365. /* scan and dump the bus */
  366. for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
  367. phy = mdiobus_get_phy(data->bus, addr);
  368. if (phy) {
  369. dev_info(dev, "phy[%d]: device %s, driver %s\n",
  370. phy->mdio.addr, phydev_name(phy),
  371. phy->drv ? phy->drv->name : "unknown");
  372. }
  373. }
  374. return 0;
  375. bail_out:
  376. pm_runtime_dont_use_autosuspend(&pdev->dev);
  377. pm_runtime_disable(&pdev->dev);
  378. return ret;
  379. }
  380. static int davinci_mdio_remove(struct platform_device *pdev)
  381. {
  382. struct davinci_mdio_data *data = platform_get_drvdata(pdev);
  383. if (data->bus)
  384. mdiobus_unregister(data->bus);
  385. pm_runtime_dont_use_autosuspend(&pdev->dev);
  386. pm_runtime_disable(&pdev->dev);
  387. return 0;
  388. }
  389. #ifdef CONFIG_PM
  390. static int davinci_mdio_runtime_suspend(struct device *dev)
  391. {
  392. struct davinci_mdio_data *data = dev_get_drvdata(dev);
  393. u32 ctrl;
  394. /* shutdown the scan state machine */
  395. ctrl = __raw_readl(&data->regs->control);
  396. ctrl &= ~CONTROL_ENABLE;
  397. __raw_writel(ctrl, &data->regs->control);
  398. wait_for_idle(data);
  399. return 0;
  400. }
  401. static int davinci_mdio_runtime_resume(struct device *dev)
  402. {
  403. struct davinci_mdio_data *data = dev_get_drvdata(dev);
  404. davinci_mdio_enable(data);
  405. return 0;
  406. }
  407. #endif
  408. #ifdef CONFIG_PM_SLEEP
  409. static int davinci_mdio_suspend(struct device *dev)
  410. {
  411. struct davinci_mdio_data *data = dev_get_drvdata(dev);
  412. int ret = 0;
  413. data->active_in_suspend = !pm_runtime_status_suspended(dev);
  414. if (data->active_in_suspend)
  415. ret = pm_runtime_force_suspend(dev);
  416. if (ret < 0)
  417. return ret;
  418. /* Select sleep pin state */
  419. pinctrl_pm_select_sleep_state(dev);
  420. return 0;
  421. }
  422. static int davinci_mdio_resume(struct device *dev)
  423. {
  424. struct davinci_mdio_data *data = dev_get_drvdata(dev);
  425. /* Select default pin state */
  426. pinctrl_pm_select_default_state(dev);
  427. if (data->active_in_suspend)
  428. pm_runtime_force_resume(dev);
  429. return 0;
  430. }
  431. #endif
  432. static const struct dev_pm_ops davinci_mdio_pm_ops = {
  433. SET_RUNTIME_PM_OPS(davinci_mdio_runtime_suspend,
  434. davinci_mdio_runtime_resume, NULL)
  435. SET_LATE_SYSTEM_SLEEP_PM_OPS(davinci_mdio_suspend, davinci_mdio_resume)
  436. };
  437. static struct platform_driver davinci_mdio_driver = {
  438. .driver = {
  439. .name = "davinci_mdio",
  440. .pm = &davinci_mdio_pm_ops,
  441. .of_match_table = of_match_ptr(davinci_mdio_of_mtable),
  442. },
  443. .probe = davinci_mdio_probe,
  444. .remove = davinci_mdio_remove,
  445. };
  446. static int __init davinci_mdio_init(void)
  447. {
  448. return platform_driver_register(&davinci_mdio_driver);
  449. }
  450. device_initcall(davinci_mdio_init);
  451. static void __exit davinci_mdio_exit(void)
  452. {
  453. platform_driver_unregister(&davinci_mdio_driver);
  454. }
  455. module_exit(davinci_mdio_exit);
  456. MODULE_LICENSE("GPL");
  457. MODULE_DESCRIPTION("DaVinci MDIO driver");