davinci_mdio.c 12 KB

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