davinci_mdio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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 (0xff)
  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. dev_err(data->dev, "timed out waiting for user access\n");
  161. return -ETIMEDOUT;
  162. }
  163. /* wait until hardware state machine is idle */
  164. static inline int wait_for_idle(struct davinci_mdio_data *data)
  165. {
  166. struct davinci_mdio_regs __iomem *regs = data->regs;
  167. unsigned long timeout = jiffies + msecs_to_jiffies(MDIO_TIMEOUT);
  168. while (time_after(timeout, jiffies)) {
  169. if (__raw_readl(&regs->control) & CONTROL_IDLE)
  170. return 0;
  171. }
  172. dev_err(data->dev, "timed out waiting for idle\n");
  173. return -ETIMEDOUT;
  174. }
  175. static int davinci_mdio_read(struct mii_bus *bus, int phy_id, int phy_reg)
  176. {
  177. struct davinci_mdio_data *data = bus->priv;
  178. u32 reg;
  179. int ret;
  180. if (phy_reg & ~PHY_REG_MASK || phy_id & ~PHY_ID_MASK)
  181. return -EINVAL;
  182. spin_lock(&data->lock);
  183. if (data->suspended) {
  184. spin_unlock(&data->lock);
  185. return -ENODEV;
  186. }
  187. reg = (USERACCESS_GO | USERACCESS_READ | (phy_reg << 21) |
  188. (phy_id << 16));
  189. while (1) {
  190. ret = wait_for_user_access(data);
  191. if (ret == -EAGAIN)
  192. continue;
  193. if (ret < 0)
  194. break;
  195. __raw_writel(reg, &data->regs->user[0].access);
  196. ret = wait_for_user_access(data);
  197. if (ret == -EAGAIN)
  198. continue;
  199. if (ret < 0)
  200. break;
  201. reg = __raw_readl(&data->regs->user[0].access);
  202. ret = (reg & USERACCESS_ACK) ? (reg & USERACCESS_DATA) : -EIO;
  203. break;
  204. }
  205. spin_unlock(&data->lock);
  206. return ret;
  207. }
  208. static int davinci_mdio_write(struct mii_bus *bus, int phy_id,
  209. int phy_reg, u16 phy_data)
  210. {
  211. struct davinci_mdio_data *data = bus->priv;
  212. u32 reg;
  213. int ret;
  214. if (phy_reg & ~PHY_REG_MASK || phy_id & ~PHY_ID_MASK)
  215. return -EINVAL;
  216. spin_lock(&data->lock);
  217. if (data->suspended) {
  218. spin_unlock(&data->lock);
  219. return -ENODEV;
  220. }
  221. reg = (USERACCESS_GO | USERACCESS_WRITE | (phy_reg << 21) |
  222. (phy_id << 16) | (phy_data & USERACCESS_DATA));
  223. while (1) {
  224. ret = wait_for_user_access(data);
  225. if (ret == -EAGAIN)
  226. continue;
  227. if (ret < 0)
  228. break;
  229. __raw_writel(reg, &data->regs->user[0].access);
  230. ret = wait_for_user_access(data);
  231. if (ret == -EAGAIN)
  232. continue;
  233. break;
  234. }
  235. spin_unlock(&data->lock);
  236. return 0;
  237. }
  238. static int __devinit davinci_mdio_probe(struct platform_device *pdev)
  239. {
  240. struct mdio_platform_data *pdata = pdev->dev.platform_data;
  241. struct device *dev = &pdev->dev;
  242. struct davinci_mdio_data *data;
  243. struct resource *res;
  244. struct phy_device *phy;
  245. int ret, addr;
  246. data = kzalloc(sizeof(*data), GFP_KERNEL);
  247. if (!data) {
  248. dev_err(dev, "failed to alloc device data\n");
  249. return -ENOMEM;
  250. }
  251. data->pdata = pdata ? (*pdata) : default_pdata;
  252. data->bus = mdiobus_alloc();
  253. if (!data->bus) {
  254. dev_err(dev, "failed to alloc mii bus\n");
  255. ret = -ENOMEM;
  256. goto bail_out;
  257. }
  258. data->bus->name = dev_name(dev);
  259. data->bus->read = davinci_mdio_read,
  260. data->bus->write = davinci_mdio_write,
  261. data->bus->reset = davinci_mdio_reset,
  262. data->bus->parent = dev;
  263. data->bus->priv = data;
  264. snprintf(data->bus->id, MII_BUS_ID_SIZE, "%x", pdev->id);
  265. data->clk = clk_get(dev, NULL);
  266. if (IS_ERR(data->clk)) {
  267. data->clk = NULL;
  268. dev_err(dev, "failed to get device clock\n");
  269. ret = PTR_ERR(data->clk);
  270. goto bail_out;
  271. }
  272. clk_enable(data->clk);
  273. dev_set_drvdata(dev, data);
  274. data->dev = dev;
  275. spin_lock_init(&data->lock);
  276. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  277. if (!res) {
  278. dev_err(dev, "could not find register map resource\n");
  279. ret = -ENOENT;
  280. goto bail_out;
  281. }
  282. res = devm_request_mem_region(dev, res->start, resource_size(res),
  283. dev_name(dev));
  284. if (!res) {
  285. dev_err(dev, "could not allocate register map resource\n");
  286. ret = -ENXIO;
  287. goto bail_out;
  288. }
  289. data->regs = devm_ioremap_nocache(dev, res->start, resource_size(res));
  290. if (!data->regs) {
  291. dev_err(dev, "could not map mdio registers\n");
  292. ret = -ENOMEM;
  293. goto bail_out;
  294. }
  295. /* register the mii bus */
  296. ret = mdiobus_register(data->bus);
  297. if (ret)
  298. goto bail_out;
  299. /* scan and dump the bus */
  300. for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
  301. phy = data->bus->phy_map[addr];
  302. if (phy) {
  303. dev_info(dev, "phy[%d]: device %s, driver %s\n",
  304. phy->addr, dev_name(&phy->dev),
  305. phy->drv ? phy->drv->name : "unknown");
  306. }
  307. }
  308. return 0;
  309. bail_out:
  310. if (data->bus)
  311. mdiobus_free(data->bus);
  312. if (data->clk) {
  313. clk_disable(data->clk);
  314. clk_put(data->clk);
  315. }
  316. kfree(data);
  317. return ret;
  318. }
  319. static int __devexit davinci_mdio_remove(struct platform_device *pdev)
  320. {
  321. struct device *dev = &pdev->dev;
  322. struct davinci_mdio_data *data = dev_get_drvdata(dev);
  323. if (data->bus)
  324. mdiobus_free(data->bus);
  325. if (data->clk) {
  326. clk_disable(data->clk);
  327. clk_put(data->clk);
  328. }
  329. dev_set_drvdata(dev, NULL);
  330. kfree(data);
  331. return 0;
  332. }
  333. static int davinci_mdio_suspend(struct device *dev)
  334. {
  335. struct davinci_mdio_data *data = dev_get_drvdata(dev);
  336. u32 ctrl;
  337. spin_lock(&data->lock);
  338. /* shutdown the scan state machine */
  339. ctrl = __raw_readl(&data->regs->control);
  340. ctrl &= ~CONTROL_ENABLE;
  341. __raw_writel(ctrl, &data->regs->control);
  342. wait_for_idle(data);
  343. if (data->clk)
  344. clk_disable(data->clk);
  345. data->suspended = true;
  346. spin_unlock(&data->lock);
  347. return 0;
  348. }
  349. static int davinci_mdio_resume(struct device *dev)
  350. {
  351. struct davinci_mdio_data *data = dev_get_drvdata(dev);
  352. u32 ctrl;
  353. spin_lock(&data->lock);
  354. if (data->clk)
  355. clk_enable(data->clk);
  356. /* restart the scan state machine */
  357. ctrl = __raw_readl(&data->regs->control);
  358. ctrl |= CONTROL_ENABLE;
  359. __raw_writel(ctrl, &data->regs->control);
  360. data->suspended = false;
  361. spin_unlock(&data->lock);
  362. return 0;
  363. }
  364. static const struct dev_pm_ops davinci_mdio_pm_ops = {
  365. .suspend = davinci_mdio_suspend,
  366. .resume = davinci_mdio_resume,
  367. };
  368. static struct platform_driver davinci_mdio_driver = {
  369. .driver = {
  370. .name = "davinci_mdio",
  371. .owner = THIS_MODULE,
  372. .pm = &davinci_mdio_pm_ops,
  373. },
  374. .probe = davinci_mdio_probe,
  375. .remove = __devexit_p(davinci_mdio_remove),
  376. };
  377. static int __init davinci_mdio_init(void)
  378. {
  379. return platform_driver_register(&davinci_mdio_driver);
  380. }
  381. device_initcall(davinci_mdio_init);
  382. static void __exit davinci_mdio_exit(void)
  383. {
  384. platform_driver_unregister(&davinci_mdio_driver);
  385. }
  386. module_exit(davinci_mdio_exit);
  387. MODULE_LICENSE("GPL");
  388. MODULE_DESCRIPTION("DaVinci MDIO driver");