fixed_phy.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /*
  2. * Fixed MDIO bus (MDIO bus emulation with fixed PHYs)
  3. *
  4. * Author: Vitaly Bordug <vbordug@ru.mvista.com>
  5. * Anton Vorontsov <avorontsov@ru.mvista.com>
  6. *
  7. * Copyright (c) 2006-2007 MontaVista Software, Inc.
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/list.h>
  18. #include <linux/mii.h>
  19. #include <linux/phy.h>
  20. #include <linux/phy_fixed.h>
  21. #include <linux/err.h>
  22. #include <linux/slab.h>
  23. #include <linux/of.h>
  24. #include <linux/gpio.h>
  25. #define MII_REGS_NUM 29
  26. struct fixed_mdio_bus {
  27. int irqs[PHY_MAX_ADDR];
  28. struct mii_bus *mii_bus;
  29. struct list_head phys;
  30. };
  31. struct fixed_phy {
  32. int addr;
  33. u16 regs[MII_REGS_NUM];
  34. struct phy_device *phydev;
  35. struct fixed_phy_status status;
  36. int (*link_update)(struct net_device *, struct fixed_phy_status *);
  37. struct list_head node;
  38. int link_gpio;
  39. };
  40. static struct platform_device *pdev;
  41. static struct fixed_mdio_bus platform_fmb = {
  42. .phys = LIST_HEAD_INIT(platform_fmb.phys),
  43. };
  44. static int fixed_phy_update_regs(struct fixed_phy *fp)
  45. {
  46. u16 bmsr = BMSR_ANEGCAPABLE;
  47. u16 bmcr = 0;
  48. u16 lpagb = 0;
  49. u16 lpa = 0;
  50. if (gpio_is_valid(fp->link_gpio))
  51. fp->status.link = !!gpio_get_value_cansleep(fp->link_gpio);
  52. if (fp->status.duplex) {
  53. switch (fp->status.speed) {
  54. case 1000:
  55. bmsr |= BMSR_ESTATEN;
  56. break;
  57. case 100:
  58. bmsr |= BMSR_100FULL;
  59. break;
  60. case 10:
  61. bmsr |= BMSR_10FULL;
  62. break;
  63. default:
  64. break;
  65. }
  66. } else {
  67. switch (fp->status.speed) {
  68. case 1000:
  69. bmsr |= BMSR_ESTATEN;
  70. break;
  71. case 100:
  72. bmsr |= BMSR_100HALF;
  73. break;
  74. case 10:
  75. bmsr |= BMSR_10HALF;
  76. break;
  77. default:
  78. break;
  79. }
  80. }
  81. if (fp->status.link) {
  82. bmsr |= BMSR_LSTATUS | BMSR_ANEGCOMPLETE;
  83. if (fp->status.duplex) {
  84. bmcr |= BMCR_FULLDPLX;
  85. switch (fp->status.speed) {
  86. case 1000:
  87. bmcr |= BMCR_SPEED1000;
  88. lpagb |= LPA_1000FULL;
  89. break;
  90. case 100:
  91. bmcr |= BMCR_SPEED100;
  92. lpa |= LPA_100FULL;
  93. break;
  94. case 10:
  95. lpa |= LPA_10FULL;
  96. break;
  97. default:
  98. pr_warn("fixed phy: unknown speed\n");
  99. return -EINVAL;
  100. }
  101. } else {
  102. switch (fp->status.speed) {
  103. case 1000:
  104. bmcr |= BMCR_SPEED1000;
  105. lpagb |= LPA_1000HALF;
  106. break;
  107. case 100:
  108. bmcr |= BMCR_SPEED100;
  109. lpa |= LPA_100HALF;
  110. break;
  111. case 10:
  112. lpa |= LPA_10HALF;
  113. break;
  114. default:
  115. pr_warn("fixed phy: unknown speed\n");
  116. return -EINVAL;
  117. }
  118. }
  119. if (fp->status.pause)
  120. lpa |= LPA_PAUSE_CAP;
  121. if (fp->status.asym_pause)
  122. lpa |= LPA_PAUSE_ASYM;
  123. }
  124. fp->regs[MII_PHYSID1] = 0;
  125. fp->regs[MII_PHYSID2] = 0;
  126. fp->regs[MII_BMSR] = bmsr;
  127. fp->regs[MII_BMCR] = bmcr;
  128. fp->regs[MII_LPA] = lpa;
  129. fp->regs[MII_STAT1000] = lpagb;
  130. return 0;
  131. }
  132. static int fixed_mdio_read(struct mii_bus *bus, int phy_addr, int reg_num)
  133. {
  134. struct fixed_mdio_bus *fmb = bus->priv;
  135. struct fixed_phy *fp;
  136. if (reg_num >= MII_REGS_NUM)
  137. return -1;
  138. /* We do not support emulating Clause 45 over Clause 22 register reads
  139. * return an error instead of bogus data.
  140. */
  141. switch (reg_num) {
  142. case MII_MMD_CTRL:
  143. case MII_MMD_DATA:
  144. return -1;
  145. default:
  146. break;
  147. }
  148. list_for_each_entry(fp, &fmb->phys, node) {
  149. if (fp->addr == phy_addr) {
  150. /* Issue callback if user registered it. */
  151. if (fp->link_update) {
  152. fp->link_update(fp->phydev->attached_dev,
  153. &fp->status);
  154. fixed_phy_update_regs(fp);
  155. }
  156. return fp->regs[reg_num];
  157. }
  158. }
  159. return 0xFFFF;
  160. }
  161. static int fixed_mdio_write(struct mii_bus *bus, int phy_addr, int reg_num,
  162. u16 val)
  163. {
  164. return 0;
  165. }
  166. /*
  167. * If something weird is required to be done with link/speed,
  168. * network driver is able to assign a function to implement this.
  169. * May be useful for PHY's that need to be software-driven.
  170. */
  171. int fixed_phy_set_link_update(struct phy_device *phydev,
  172. int (*link_update)(struct net_device *,
  173. struct fixed_phy_status *))
  174. {
  175. struct fixed_mdio_bus *fmb = &platform_fmb;
  176. struct fixed_phy *fp;
  177. if (!phydev || !phydev->bus)
  178. return -EINVAL;
  179. list_for_each_entry(fp, &fmb->phys, node) {
  180. if (fp->addr == phydev->addr) {
  181. fp->link_update = link_update;
  182. fp->phydev = phydev;
  183. return 0;
  184. }
  185. }
  186. return -ENOENT;
  187. }
  188. EXPORT_SYMBOL_GPL(fixed_phy_set_link_update);
  189. int fixed_phy_update_state(struct phy_device *phydev,
  190. const struct fixed_phy_status *status,
  191. const struct fixed_phy_status *changed)
  192. {
  193. struct fixed_mdio_bus *fmb = &platform_fmb;
  194. struct fixed_phy *fp;
  195. if (!phydev || phydev->bus != fmb->mii_bus)
  196. return -EINVAL;
  197. list_for_each_entry(fp, &fmb->phys, node) {
  198. if (fp->addr == phydev->addr) {
  199. #define _UPD(x) if (changed->x) \
  200. fp->status.x = status->x
  201. _UPD(link);
  202. _UPD(speed);
  203. _UPD(duplex);
  204. _UPD(pause);
  205. _UPD(asym_pause);
  206. #undef _UPD
  207. fixed_phy_update_regs(fp);
  208. return 0;
  209. }
  210. }
  211. return -ENOENT;
  212. }
  213. EXPORT_SYMBOL(fixed_phy_update_state);
  214. int fixed_phy_add(unsigned int irq, int phy_addr,
  215. struct fixed_phy_status *status,
  216. int link_gpio)
  217. {
  218. int ret;
  219. struct fixed_mdio_bus *fmb = &platform_fmb;
  220. struct fixed_phy *fp;
  221. fp = kzalloc(sizeof(*fp), GFP_KERNEL);
  222. if (!fp)
  223. return -ENOMEM;
  224. memset(fp->regs, 0xFF, sizeof(fp->regs[0]) * MII_REGS_NUM);
  225. fmb->irqs[phy_addr] = irq;
  226. fp->addr = phy_addr;
  227. fp->status = *status;
  228. fp->link_gpio = link_gpio;
  229. if (gpio_is_valid(fp->link_gpio)) {
  230. ret = gpio_request_one(fp->link_gpio, GPIOF_DIR_IN,
  231. "fixed-link-gpio-link");
  232. if (ret)
  233. goto err_regs;
  234. }
  235. ret = fixed_phy_update_regs(fp);
  236. if (ret)
  237. goto err_gpio;
  238. list_add_tail(&fp->node, &fmb->phys);
  239. return 0;
  240. err_gpio:
  241. if (gpio_is_valid(fp->link_gpio))
  242. gpio_free(fp->link_gpio);
  243. err_regs:
  244. kfree(fp);
  245. return ret;
  246. }
  247. EXPORT_SYMBOL_GPL(fixed_phy_add);
  248. void fixed_phy_del(int phy_addr)
  249. {
  250. struct fixed_mdio_bus *fmb = &platform_fmb;
  251. struct fixed_phy *fp, *tmp;
  252. list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
  253. if (fp->addr == phy_addr) {
  254. list_del(&fp->node);
  255. if (gpio_is_valid(fp->link_gpio))
  256. gpio_free(fp->link_gpio);
  257. kfree(fp);
  258. return;
  259. }
  260. }
  261. }
  262. EXPORT_SYMBOL_GPL(fixed_phy_del);
  263. static int phy_fixed_addr;
  264. static DEFINE_SPINLOCK(phy_fixed_addr_lock);
  265. struct phy_device *fixed_phy_register(unsigned int irq,
  266. struct fixed_phy_status *status,
  267. int link_gpio,
  268. struct device_node *np)
  269. {
  270. struct fixed_mdio_bus *fmb = &platform_fmb;
  271. struct phy_device *phy;
  272. int phy_addr;
  273. int ret;
  274. /* Get the next available PHY address, up to PHY_MAX_ADDR */
  275. spin_lock(&phy_fixed_addr_lock);
  276. if (phy_fixed_addr == PHY_MAX_ADDR) {
  277. spin_unlock(&phy_fixed_addr_lock);
  278. return ERR_PTR(-ENOSPC);
  279. }
  280. phy_addr = phy_fixed_addr++;
  281. spin_unlock(&phy_fixed_addr_lock);
  282. ret = fixed_phy_add(irq, phy_addr, status, link_gpio);
  283. if (ret < 0)
  284. return ERR_PTR(ret);
  285. phy = get_phy_device(fmb->mii_bus, phy_addr, false);
  286. if (!phy || IS_ERR(phy)) {
  287. fixed_phy_del(phy_addr);
  288. return ERR_PTR(-EINVAL);
  289. }
  290. /* propagate the fixed link values to struct phy_device */
  291. phy->link = status->link;
  292. if (status->link) {
  293. phy->speed = status->speed;
  294. phy->duplex = status->duplex;
  295. phy->pause = status->pause;
  296. phy->asym_pause = status->asym_pause;
  297. }
  298. of_node_get(np);
  299. phy->dev.of_node = np;
  300. phy->is_pseudo_fixed_link = true;
  301. switch (status->speed) {
  302. case SPEED_1000:
  303. phy->supported = PHY_1000BT_FEATURES;
  304. break;
  305. case SPEED_100:
  306. phy->supported = PHY_100BT_FEATURES;
  307. break;
  308. case SPEED_10:
  309. default:
  310. phy->supported = PHY_10BT_FEATURES;
  311. }
  312. ret = phy_device_register(phy);
  313. if (ret) {
  314. phy_device_free(phy);
  315. of_node_put(np);
  316. fixed_phy_del(phy_addr);
  317. return ERR_PTR(ret);
  318. }
  319. return phy;
  320. }
  321. EXPORT_SYMBOL_GPL(fixed_phy_register);
  322. static int __init fixed_mdio_bus_init(void)
  323. {
  324. struct fixed_mdio_bus *fmb = &platform_fmb;
  325. int ret;
  326. pdev = platform_device_register_simple("Fixed MDIO bus", 0, NULL, 0);
  327. if (IS_ERR(pdev)) {
  328. ret = PTR_ERR(pdev);
  329. goto err_pdev;
  330. }
  331. fmb->mii_bus = mdiobus_alloc();
  332. if (fmb->mii_bus == NULL) {
  333. ret = -ENOMEM;
  334. goto err_mdiobus_reg;
  335. }
  336. snprintf(fmb->mii_bus->id, MII_BUS_ID_SIZE, "fixed-0");
  337. fmb->mii_bus->name = "Fixed MDIO Bus";
  338. fmb->mii_bus->priv = fmb;
  339. fmb->mii_bus->parent = &pdev->dev;
  340. fmb->mii_bus->read = &fixed_mdio_read;
  341. fmb->mii_bus->write = &fixed_mdio_write;
  342. fmb->mii_bus->irq = fmb->irqs;
  343. ret = mdiobus_register(fmb->mii_bus);
  344. if (ret)
  345. goto err_mdiobus_alloc;
  346. return 0;
  347. err_mdiobus_alloc:
  348. mdiobus_free(fmb->mii_bus);
  349. err_mdiobus_reg:
  350. platform_device_unregister(pdev);
  351. err_pdev:
  352. return ret;
  353. }
  354. module_init(fixed_mdio_bus_init);
  355. static void __exit fixed_mdio_bus_exit(void)
  356. {
  357. struct fixed_mdio_bus *fmb = &platform_fmb;
  358. struct fixed_phy *fp, *tmp;
  359. mdiobus_unregister(fmb->mii_bus);
  360. mdiobus_free(fmb->mii_bus);
  361. platform_device_unregister(pdev);
  362. list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
  363. list_del(&fp->node);
  364. kfree(fp);
  365. }
  366. }
  367. module_exit(fixed_mdio_bus_exit);
  368. MODULE_DESCRIPTION("Fixed MDIO bus (MDIO bus emulation with fixed PHYs)");
  369. MODULE_AUTHOR("Vitaly Bordug");
  370. MODULE_LICENSE("GPL");