b53_mdio.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /*
  2. * B53 register access through MII registers
  3. *
  4. * Copyright (C) 2011-2013 Jonas Gorski <jogo@openwrt.org>
  5. *
  6. * Permission to use, copy, modify, and/or distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/phy.h>
  20. #include <linux/module.h>
  21. #include "b53_priv.h"
  22. #define B53_PSEUDO_PHY 0x1e /* Register Access Pseudo PHY */
  23. /* MII registers */
  24. #define REG_MII_PAGE 0x10 /* MII Page register */
  25. #define REG_MII_ADDR 0x11 /* MII Address register */
  26. #define REG_MII_DATA0 0x18 /* MII Data register 0 */
  27. #define REG_MII_DATA1 0x19 /* MII Data register 1 */
  28. #define REG_MII_DATA2 0x1a /* MII Data register 2 */
  29. #define REG_MII_DATA3 0x1b /* MII Data register 3 */
  30. #define REG_MII_PAGE_ENABLE BIT(0)
  31. #define REG_MII_ADDR_WRITE BIT(0)
  32. #define REG_MII_ADDR_READ BIT(1)
  33. static int b53_mdio_op(struct b53_device *dev, u8 page, u8 reg, u16 op)
  34. {
  35. int i;
  36. u16 v;
  37. int ret;
  38. struct mii_bus *bus = dev->priv;
  39. if (dev->current_page != page) {
  40. /* set page number */
  41. v = (page << 8) | REG_MII_PAGE_ENABLE;
  42. ret = mdiobus_write(bus, B53_PSEUDO_PHY, REG_MII_PAGE, v);
  43. if (ret)
  44. return ret;
  45. dev->current_page = page;
  46. }
  47. /* set register address */
  48. v = (reg << 8) | op;
  49. ret = mdiobus_write(bus, B53_PSEUDO_PHY, REG_MII_ADDR, v);
  50. if (ret)
  51. return ret;
  52. /* check if operation completed */
  53. for (i = 0; i < 5; ++i) {
  54. v = mdiobus_read(bus, B53_PSEUDO_PHY, REG_MII_ADDR);
  55. if (!(v & (REG_MII_ADDR_WRITE | REG_MII_ADDR_READ)))
  56. break;
  57. usleep_range(10, 100);
  58. }
  59. if (WARN_ON(i == 5))
  60. return -EIO;
  61. return 0;
  62. }
  63. static int b53_mdio_read8(struct b53_device *dev, u8 page, u8 reg, u8 *val)
  64. {
  65. struct mii_bus *bus = dev->priv;
  66. int ret;
  67. ret = b53_mdio_op(dev, page, reg, REG_MII_ADDR_READ);
  68. if (ret)
  69. return ret;
  70. *val = mdiobus_read(bus, B53_PSEUDO_PHY, REG_MII_DATA0) & 0xff;
  71. return 0;
  72. }
  73. static int b53_mdio_read16(struct b53_device *dev, u8 page, u8 reg, u16 *val)
  74. {
  75. struct mii_bus *bus = dev->priv;
  76. int ret;
  77. ret = b53_mdio_op(dev, page, reg, REG_MII_ADDR_READ);
  78. if (ret)
  79. return ret;
  80. *val = mdiobus_read(bus, B53_PSEUDO_PHY, REG_MII_DATA0);
  81. return 0;
  82. }
  83. static int b53_mdio_read32(struct b53_device *dev, u8 page, u8 reg, u32 *val)
  84. {
  85. struct mii_bus *bus = dev->priv;
  86. int ret;
  87. ret = b53_mdio_op(dev, page, reg, REG_MII_ADDR_READ);
  88. if (ret)
  89. return ret;
  90. *val = mdiobus_read(bus, B53_PSEUDO_PHY, REG_MII_DATA0);
  91. *val |= mdiobus_read(bus, B53_PSEUDO_PHY, REG_MII_DATA1) << 16;
  92. return 0;
  93. }
  94. static int b53_mdio_read48(struct b53_device *dev, u8 page, u8 reg, u64 *val)
  95. {
  96. struct mii_bus *bus = dev->priv;
  97. u64 temp = 0;
  98. int i;
  99. int ret;
  100. ret = b53_mdio_op(dev, page, reg, REG_MII_ADDR_READ);
  101. if (ret)
  102. return ret;
  103. for (i = 2; i >= 0; i--) {
  104. temp <<= 16;
  105. temp |= mdiobus_read(bus, B53_PSEUDO_PHY, REG_MII_DATA0 + i);
  106. }
  107. *val = temp;
  108. return 0;
  109. }
  110. static int b53_mdio_read64(struct b53_device *dev, u8 page, u8 reg, u64 *val)
  111. {
  112. struct mii_bus *bus = dev->priv;
  113. u64 temp = 0;
  114. int i;
  115. int ret;
  116. ret = b53_mdio_op(dev, page, reg, REG_MII_ADDR_READ);
  117. if (ret)
  118. return ret;
  119. for (i = 3; i >= 0; i--) {
  120. temp <<= 16;
  121. temp |= mdiobus_read(bus, B53_PSEUDO_PHY, REG_MII_DATA0 + i);
  122. }
  123. *val = temp;
  124. return 0;
  125. }
  126. static int b53_mdio_write8(struct b53_device *dev, u8 page, u8 reg, u8 value)
  127. {
  128. struct mii_bus *bus = dev->priv;
  129. int ret;
  130. ret = mdiobus_write(bus, B53_PSEUDO_PHY, REG_MII_DATA0, value);
  131. if (ret)
  132. return ret;
  133. return b53_mdio_op(dev, page, reg, REG_MII_ADDR_WRITE);
  134. }
  135. static int b53_mdio_write16(struct b53_device *dev, u8 page, u8 reg,
  136. u16 value)
  137. {
  138. struct mii_bus *bus = dev->priv;
  139. int ret;
  140. ret = mdiobus_write(bus, B53_PSEUDO_PHY, REG_MII_DATA0, value);
  141. if (ret)
  142. return ret;
  143. return b53_mdio_op(dev, page, reg, REG_MII_ADDR_WRITE);
  144. }
  145. static int b53_mdio_write32(struct b53_device *dev, u8 page, u8 reg,
  146. u32 value)
  147. {
  148. struct mii_bus *bus = dev->priv;
  149. unsigned int i;
  150. u32 temp = value;
  151. for (i = 0; i < 2; i++) {
  152. int ret = mdiobus_write(bus, B53_PSEUDO_PHY, REG_MII_DATA0 + i,
  153. temp & 0xffff);
  154. if (ret)
  155. return ret;
  156. temp >>= 16;
  157. }
  158. return b53_mdio_op(dev, page, reg, REG_MII_ADDR_WRITE);
  159. }
  160. static int b53_mdio_write48(struct b53_device *dev, u8 page, u8 reg,
  161. u64 value)
  162. {
  163. struct mii_bus *bus = dev->priv;
  164. unsigned i;
  165. u64 temp = value;
  166. for (i = 0; i < 3; i++) {
  167. int ret = mdiobus_write(bus, B53_PSEUDO_PHY, REG_MII_DATA0 + i,
  168. temp & 0xffff);
  169. if (ret)
  170. return ret;
  171. temp >>= 16;
  172. }
  173. return b53_mdio_op(dev, page, reg, REG_MII_ADDR_WRITE);
  174. }
  175. static int b53_mdio_write64(struct b53_device *dev, u8 page, u8 reg,
  176. u64 value)
  177. {
  178. struct mii_bus *bus = dev->priv;
  179. unsigned i;
  180. u64 temp = value;
  181. for (i = 0; i < 4; i++) {
  182. int ret = mdiobus_write(bus, B53_PSEUDO_PHY, REG_MII_DATA0 + i,
  183. temp & 0xffff);
  184. if (ret)
  185. return ret;
  186. temp >>= 16;
  187. }
  188. return b53_mdio_op(dev, page, reg, REG_MII_ADDR_WRITE);
  189. }
  190. static int b53_mdio_phy_read16(struct b53_device *dev, int addr, u8 reg,
  191. u16 *value)
  192. {
  193. struct mii_bus *bus = dev->priv;
  194. *value = mdiobus_read(bus, addr, reg);
  195. return 0;
  196. }
  197. static int b53_mdio_phy_write16(struct b53_device *dev, int addr, u8 reg,
  198. u16 value)
  199. {
  200. struct mii_bus *bus = dev->priv;
  201. return mdiobus_write(bus, addr, reg, value);
  202. }
  203. static struct b53_io_ops b53_mdio_ops = {
  204. .read8 = b53_mdio_read8,
  205. .read16 = b53_mdio_read16,
  206. .read32 = b53_mdio_read32,
  207. .read48 = b53_mdio_read48,
  208. .read64 = b53_mdio_read64,
  209. .write8 = b53_mdio_write8,
  210. .write16 = b53_mdio_write16,
  211. .write32 = b53_mdio_write32,
  212. .write48 = b53_mdio_write48,
  213. .write64 = b53_mdio_write64,
  214. .phy_read16 = b53_mdio_phy_read16,
  215. .phy_write16 = b53_mdio_phy_write16,
  216. };
  217. static int b53_phy_probe(struct phy_device *phydev)
  218. {
  219. struct b53_device dev;
  220. int ret;
  221. /* allow the generic phy driver to take over */
  222. if (phydev->addr != B53_PSEUDO_PHY && phydev->addr != 0)
  223. return -ENODEV;
  224. dev.current_page = 0xff;
  225. dev.priv = phydev->bus;
  226. dev.ops = &b53_mdio_ops;
  227. dev.pdata = NULL;
  228. mutex_init(&dev.reg_mutex);
  229. ret = b53_switch_detect(&dev);
  230. if (ret)
  231. return ret;
  232. if (is5325(&dev) || is5365(&dev))
  233. phydev->supported = SUPPORTED_100baseT_Full;
  234. else
  235. phydev->supported = SUPPORTED_1000baseT_Full;
  236. phydev->advertising = phydev->supported;
  237. return 0;
  238. }
  239. static int b53_phy_config_init(struct phy_device *phydev)
  240. {
  241. struct b53_device *dev;
  242. int ret;
  243. dev = b53_switch_alloc(&phydev->dev, &b53_mdio_ops, phydev->bus);
  244. if (!dev)
  245. return -ENOMEM;
  246. /* we don't use page 0xff, so force a page set */
  247. dev->current_page = 0xff;
  248. /* force the ethX as alias */
  249. dev->sw_dev.alias = phydev->attached_dev->name;
  250. ret = b53_switch_register(dev);
  251. if (ret) {
  252. dev_err(dev->dev, "failed to register switch: %i\n", ret);
  253. return ret;
  254. }
  255. phydev->priv = dev;
  256. return 0;
  257. }
  258. static void b53_phy_remove(struct phy_device *phydev)
  259. {
  260. struct b53_device *priv = phydev->priv;
  261. if (!priv)
  262. return;
  263. b53_switch_remove(priv);
  264. phydev->priv = NULL;
  265. }
  266. static int b53_phy_config_aneg(struct phy_device *phydev)
  267. {
  268. return 0;
  269. }
  270. static int b53_phy_read_status(struct phy_device *phydev)
  271. {
  272. struct b53_device *priv = phydev->priv;
  273. if (is5325(priv) || is5365(priv))
  274. phydev->speed = 100;
  275. else
  276. phydev->speed = 1000;
  277. phydev->duplex = DUPLEX_FULL;
  278. phydev->link = 1;
  279. phydev->state = PHY_RUNNING;
  280. netif_carrier_on(phydev->attached_dev);
  281. phydev->adjust_link(phydev->attached_dev);
  282. return 0;
  283. }
  284. /* BCM5325, BCM539x */
  285. static struct phy_driver b53_phy_driver_id1 = {
  286. .phy_id = 0x0143bc00,
  287. .name = "Broadcom B53 (1)",
  288. .phy_id_mask = 0x1ffffc00,
  289. .features = 0,
  290. .probe = b53_phy_probe,
  291. .remove = b53_phy_remove,
  292. .config_aneg = b53_phy_config_aneg,
  293. .config_init = b53_phy_config_init,
  294. .read_status = b53_phy_read_status,
  295. .driver = {
  296. .owner = THIS_MODULE,
  297. },
  298. };
  299. /* BCM53125, BCM53128 */
  300. static struct phy_driver b53_phy_driver_id2 = {
  301. .phy_id = 0x03625c00,
  302. .name = "Broadcom B53 (2)",
  303. .phy_id_mask = 0x1ffffc00,
  304. .features = 0,
  305. .probe = b53_phy_probe,
  306. .remove = b53_phy_remove,
  307. .config_aneg = b53_phy_config_aneg,
  308. .config_init = b53_phy_config_init,
  309. .read_status = b53_phy_read_status,
  310. .driver = {
  311. .owner = THIS_MODULE,
  312. },
  313. };
  314. /* BCM5365 */
  315. static struct phy_driver b53_phy_driver_id3 = {
  316. .phy_id = 0x00406000,
  317. .name = "Broadcom B53 (3)",
  318. .phy_id_mask = 0x1ffffc00,
  319. .features = 0,
  320. .probe = b53_phy_probe,
  321. .remove = b53_phy_remove,
  322. .config_aneg = b53_phy_config_aneg,
  323. .config_init = b53_phy_config_init,
  324. .read_status = b53_phy_read_status,
  325. .driver = {
  326. .owner = THIS_MODULE,
  327. },
  328. };
  329. int __init b53_phy_driver_register(void)
  330. {
  331. int ret;
  332. ret = phy_driver_register(&b53_phy_driver_id1);
  333. if (ret)
  334. return ret;
  335. ret = phy_driver_register(&b53_phy_driver_id2);
  336. if (ret)
  337. goto err1;
  338. ret = phy_driver_register(&b53_phy_driver_id3);
  339. if (!ret)
  340. return 0;
  341. phy_driver_unregister(&b53_phy_driver_id2);
  342. err1:
  343. phy_driver_unregister(&b53_phy_driver_id1);
  344. return ret;
  345. }
  346. void __exit b53_phy_driver_unregister(void)
  347. {
  348. phy_driver_unregister(&b53_phy_driver_id3);
  349. phy_driver_unregister(&b53_phy_driver_id2);
  350. phy_driver_unregister(&b53_phy_driver_id1);
  351. }
  352. module_init(b53_phy_driver_register);
  353. module_exit(b53_phy_driver_unregister);
  354. MODULE_DESCRIPTION("B53 MDIO access driver");
  355. MODULE_LICENSE("Dual BSD/GPL");