davinci_mdio.c 11 KB

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