davinci_mdio.c 12 KB

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