davinci_mdio.c 13 KB

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