davinci_mdio.c 11 KB

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