davinci_mdio.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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,
  144. "davinci mdio revision %d.%d, bus freq %ld\n",
  145. (ver >> 8) & 0xff, ver & 0xff,
  146. data->pdata.bus_freq);
  147. if (data->skip_scan)
  148. goto done;
  149. /* get phy mask from the alive register */
  150. phy_mask = __raw_readl(&data->regs->alive);
  151. if (phy_mask) {
  152. /* restrict mdio bus to live phys only */
  153. dev_info(data->dev, "detected phy mask %x\n", ~phy_mask);
  154. phy_mask = ~phy_mask;
  155. } else {
  156. /* desperately scan all phys */
  157. dev_warn(data->dev, "no live phy, scanning all\n");
  158. phy_mask = 0;
  159. }
  160. data->bus->phy_mask = phy_mask;
  161. done:
  162. pm_runtime_mark_last_busy(data->dev);
  163. pm_runtime_put_autosuspend(data->dev);
  164. return 0;
  165. }
  166. /* wait until hardware is ready for another user access */
  167. static inline int wait_for_user_access(struct davinci_mdio_data *data)
  168. {
  169. struct davinci_mdio_regs __iomem *regs = data->regs;
  170. unsigned long timeout = jiffies + msecs_to_jiffies(MDIO_TIMEOUT);
  171. u32 reg;
  172. while (time_after(timeout, jiffies)) {
  173. reg = __raw_readl(&regs->user[0].access);
  174. if ((reg & USERACCESS_GO) == 0)
  175. return 0;
  176. reg = __raw_readl(&regs->control);
  177. if ((reg & CONTROL_IDLE) == 0) {
  178. usleep_range(100, 200);
  179. continue;
  180. }
  181. /*
  182. * An emac soft_reset may have clobbered the mdio controller's
  183. * state machine. We need to reset and retry the current
  184. * operation
  185. */
  186. dev_warn(data->dev, "resetting idled controller\n");
  187. davinci_mdio_enable(data);
  188. return -EAGAIN;
  189. }
  190. reg = __raw_readl(&regs->user[0].access);
  191. if ((reg & USERACCESS_GO) == 0)
  192. return 0;
  193. dev_err(data->dev, "timed out waiting for user access\n");
  194. return -ETIMEDOUT;
  195. }
  196. /* wait until hardware state machine is idle */
  197. static inline int wait_for_idle(struct davinci_mdio_data *data)
  198. {
  199. struct davinci_mdio_regs __iomem *regs = data->regs;
  200. unsigned long timeout = jiffies + msecs_to_jiffies(MDIO_TIMEOUT);
  201. while (time_after(timeout, jiffies)) {
  202. if (__raw_readl(&regs->control) & CONTROL_IDLE)
  203. return 0;
  204. }
  205. dev_err(data->dev, "timed out waiting for idle\n");
  206. return -ETIMEDOUT;
  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. #if IS_ENABLED(CONFIG_OF)
  274. static int davinci_mdio_probe_dt(struct mdio_platform_data *data,
  275. struct platform_device *pdev)
  276. {
  277. struct device_node *node = pdev->dev.of_node;
  278. u32 prop;
  279. if (!node)
  280. return -EINVAL;
  281. if (of_property_read_u32(node, "bus_freq", &prop)) {
  282. dev_err(&pdev->dev, "Missing bus_freq property in the DT.\n");
  283. return -EINVAL;
  284. }
  285. data->bus_freq = prop;
  286. return 0;
  287. }
  288. #endif
  289. #if IS_ENABLED(CONFIG_OF)
  290. static const struct davinci_mdio_of_param of_cpsw_mdio_data = {
  291. .autosuspend_delay_ms = 100,
  292. };
  293. static const struct of_device_id davinci_mdio_of_mtable[] = {
  294. { .compatible = "ti,davinci_mdio", },
  295. { .compatible = "ti,cpsw-mdio", .data = &of_cpsw_mdio_data},
  296. { /* sentinel */ },
  297. };
  298. MODULE_DEVICE_TABLE(of, davinci_mdio_of_mtable);
  299. #endif
  300. static int davinci_mdio_probe(struct platform_device *pdev)
  301. {
  302. struct mdio_platform_data *pdata = dev_get_platdata(&pdev->dev);
  303. struct device *dev = &pdev->dev;
  304. struct davinci_mdio_data *data;
  305. struct resource *res;
  306. struct phy_device *phy;
  307. int ret, addr;
  308. int autosuspend_delay_ms = -1;
  309. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  310. if (!data)
  311. return -ENOMEM;
  312. data->bus = devm_mdiobus_alloc(dev);
  313. if (!data->bus) {
  314. dev_err(dev, "failed to alloc mii bus\n");
  315. return -ENOMEM;
  316. }
  317. if (dev->of_node) {
  318. const struct of_device_id *of_id;
  319. ret = davinci_mdio_probe_dt(&data->pdata, pdev);
  320. if (ret)
  321. return ret;
  322. snprintf(data->bus->id, MII_BUS_ID_SIZE, "%s", pdev->name);
  323. of_id = of_match_device(davinci_mdio_of_mtable, &pdev->dev);
  324. if (of_id) {
  325. const struct davinci_mdio_of_param *of_mdio_data;
  326. of_mdio_data = of_id->data;
  327. if (of_mdio_data)
  328. autosuspend_delay_ms =
  329. of_mdio_data->autosuspend_delay_ms;
  330. }
  331. } else {
  332. data->pdata = pdata ? (*pdata) : default_pdata;
  333. snprintf(data->bus->id, MII_BUS_ID_SIZE, "%s-%x",
  334. pdev->name, pdev->id);
  335. }
  336. data->bus->name = dev_name(dev);
  337. data->bus->read = davinci_mdio_read,
  338. data->bus->write = davinci_mdio_write,
  339. data->bus->reset = davinci_mdio_reset,
  340. data->bus->parent = dev;
  341. data->bus->priv = data;
  342. data->clk = devm_clk_get(dev, "fck");
  343. if (IS_ERR(data->clk)) {
  344. dev_err(dev, "failed to get device clock\n");
  345. return PTR_ERR(data->clk);
  346. }
  347. dev_set_drvdata(dev, data);
  348. data->dev = dev;
  349. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  350. data->regs = devm_ioremap_resource(dev, res);
  351. if (IS_ERR(data->regs))
  352. return PTR_ERR(data->regs);
  353. davinci_mdio_init_clk(data);
  354. pm_runtime_set_autosuspend_delay(&pdev->dev, autosuspend_delay_ms);
  355. pm_runtime_use_autosuspend(&pdev->dev);
  356. pm_runtime_enable(&pdev->dev);
  357. /* register the mii bus
  358. * Create PHYs from DT only in case if PHY child nodes are explicitly
  359. * defined to support backward compatibility with DTs which assume that
  360. * Davinci MDIO will always scan the bus for PHYs detection.
  361. */
  362. if (dev->of_node && of_get_child_count(dev->of_node)) {
  363. data->skip_scan = true;
  364. ret = of_mdiobus_register(data->bus, dev->of_node);
  365. } else {
  366. ret = mdiobus_register(data->bus);
  367. }
  368. if (ret)
  369. goto bail_out;
  370. /* scan and dump the bus */
  371. for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
  372. phy = mdiobus_get_phy(data->bus, addr);
  373. if (phy) {
  374. dev_info(dev, "phy[%d]: device %s, driver %s\n",
  375. phy->mdio.addr, phydev_name(phy),
  376. phy->drv ? phy->drv->name : "unknown");
  377. }
  378. }
  379. return 0;
  380. bail_out:
  381. pm_runtime_dont_use_autosuspend(&pdev->dev);
  382. pm_runtime_disable(&pdev->dev);
  383. return ret;
  384. }
  385. static int davinci_mdio_remove(struct platform_device *pdev)
  386. {
  387. struct davinci_mdio_data *data = platform_get_drvdata(pdev);
  388. if (data->bus)
  389. mdiobus_unregister(data->bus);
  390. pm_runtime_dont_use_autosuspend(&pdev->dev);
  391. pm_runtime_disable(&pdev->dev);
  392. return 0;
  393. }
  394. #ifdef CONFIG_PM
  395. static int davinci_mdio_runtime_suspend(struct device *dev)
  396. {
  397. struct davinci_mdio_data *data = dev_get_drvdata(dev);
  398. u32 ctrl;
  399. /* shutdown the scan state machine */
  400. ctrl = __raw_readl(&data->regs->control);
  401. ctrl &= ~CONTROL_ENABLE;
  402. __raw_writel(ctrl, &data->regs->control);
  403. wait_for_idle(data);
  404. return 0;
  405. }
  406. static int davinci_mdio_runtime_resume(struct device *dev)
  407. {
  408. struct davinci_mdio_data *data = dev_get_drvdata(dev);
  409. davinci_mdio_enable(data);
  410. return 0;
  411. }
  412. #endif
  413. #ifdef CONFIG_PM_SLEEP
  414. static int davinci_mdio_suspend(struct device *dev)
  415. {
  416. struct davinci_mdio_data *data = dev_get_drvdata(dev);
  417. int ret = 0;
  418. data->active_in_suspend = !pm_runtime_status_suspended(dev);
  419. if (data->active_in_suspend)
  420. ret = pm_runtime_force_suspend(dev);
  421. if (ret < 0)
  422. return ret;
  423. /* Select sleep pin state */
  424. pinctrl_pm_select_sleep_state(dev);
  425. return 0;
  426. }
  427. static int davinci_mdio_resume(struct device *dev)
  428. {
  429. struct davinci_mdio_data *data = dev_get_drvdata(dev);
  430. /* Select default pin state */
  431. pinctrl_pm_select_default_state(dev);
  432. if (data->active_in_suspend)
  433. pm_runtime_force_resume(dev);
  434. return 0;
  435. }
  436. #endif
  437. static const struct dev_pm_ops davinci_mdio_pm_ops = {
  438. SET_RUNTIME_PM_OPS(davinci_mdio_runtime_suspend,
  439. davinci_mdio_runtime_resume, NULL)
  440. SET_LATE_SYSTEM_SLEEP_PM_OPS(davinci_mdio_suspend, davinci_mdio_resume)
  441. };
  442. static struct platform_driver davinci_mdio_driver = {
  443. .driver = {
  444. .name = "davinci_mdio",
  445. .pm = &davinci_mdio_pm_ops,
  446. .of_match_table = of_match_ptr(davinci_mdio_of_mtable),
  447. },
  448. .probe = davinci_mdio_probe,
  449. .remove = davinci_mdio_remove,
  450. };
  451. static int __init davinci_mdio_init(void)
  452. {
  453. return platform_driver_register(&davinci_mdio_driver);
  454. }
  455. device_initcall(davinci_mdio_init);
  456. static void __exit davinci_mdio_exit(void)
  457. {
  458. platform_driver_unregister(&davinci_mdio_driver);
  459. }
  460. module_exit(davinci_mdio_exit);
  461. MODULE_LICENSE("GPL");
  462. MODULE_DESCRIPTION("DaVinci MDIO driver");