bgmac-bcma.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * Driver for (BCM4706)? GBit MAC core on BCMA bus.
  3. *
  4. * Copyright (C) 2012 Rafał Miłecki <zajec5@gmail.com>
  5. *
  6. * Licensed under the GNU/GPL. See COPYING for details.
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #include <linux/bcma/bcma.h>
  10. #include <linux/brcmphy.h>
  11. #include <linux/etherdevice.h>
  12. #include <linux/of_net.h>
  13. #include "bgmac.h"
  14. static inline bool bgmac_is_bcm4707_family(struct bcma_device *core)
  15. {
  16. switch (core->bus->chipinfo.id) {
  17. case BCMA_CHIP_ID_BCM4707:
  18. case BCMA_CHIP_ID_BCM47094:
  19. case BCMA_CHIP_ID_BCM53018:
  20. return true;
  21. default:
  22. return false;
  23. }
  24. }
  25. /**************************************************
  26. * BCMA bus ops
  27. **************************************************/
  28. static u32 bcma_bgmac_read(struct bgmac *bgmac, u16 offset)
  29. {
  30. return bcma_read32(bgmac->bcma.core, offset);
  31. }
  32. static void bcma_bgmac_write(struct bgmac *bgmac, u16 offset, u32 value)
  33. {
  34. bcma_write32(bgmac->bcma.core, offset, value);
  35. }
  36. static u32 bcma_bgmac_idm_read(struct bgmac *bgmac, u16 offset)
  37. {
  38. return bcma_aread32(bgmac->bcma.core, offset);
  39. }
  40. static void bcma_bgmac_idm_write(struct bgmac *bgmac, u16 offset, u32 value)
  41. {
  42. return bcma_awrite32(bgmac->bcma.core, offset, value);
  43. }
  44. static bool bcma_bgmac_clk_enabled(struct bgmac *bgmac)
  45. {
  46. return bcma_core_is_enabled(bgmac->bcma.core);
  47. }
  48. static void bcma_bgmac_clk_enable(struct bgmac *bgmac, u32 flags)
  49. {
  50. bcma_core_enable(bgmac->bcma.core, flags);
  51. }
  52. static void bcma_bgmac_cco_ctl_maskset(struct bgmac *bgmac, u32 offset,
  53. u32 mask, u32 set)
  54. {
  55. struct bcma_drv_cc *cc = &bgmac->bcma.core->bus->drv_cc;
  56. bcma_chipco_chipctl_maskset(cc, offset, mask, set);
  57. }
  58. static u32 bcma_bgmac_get_bus_clock(struct bgmac *bgmac)
  59. {
  60. struct bcma_drv_cc *cc = &bgmac->bcma.core->bus->drv_cc;
  61. return bcma_pmu_get_bus_clock(cc);
  62. }
  63. static void bcma_bgmac_cmn_maskset32(struct bgmac *bgmac, u16 offset, u32 mask,
  64. u32 set)
  65. {
  66. bcma_maskset32(bgmac->bcma.cmn, offset, mask, set);
  67. }
  68. static int bcma_phy_connect(struct bgmac *bgmac)
  69. {
  70. struct phy_device *phy_dev;
  71. char bus_id[MII_BUS_ID_SIZE + 3];
  72. /* Connect to the PHY */
  73. snprintf(bus_id, sizeof(bus_id), PHY_ID_FMT, bgmac->mii_bus->id,
  74. bgmac->phyaddr);
  75. phy_dev = phy_connect(bgmac->net_dev, bus_id, bgmac_adjust_link,
  76. PHY_INTERFACE_MODE_MII);
  77. if (IS_ERR(phy_dev)) {
  78. dev_err(bgmac->dev, "PHY connection failed\n");
  79. return PTR_ERR(phy_dev);
  80. }
  81. return 0;
  82. }
  83. static const struct bcma_device_id bgmac_bcma_tbl[] = {
  84. BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_4706_MAC_GBIT,
  85. BCMA_ANY_REV, BCMA_ANY_CLASS),
  86. BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_MAC_GBIT, BCMA_ANY_REV,
  87. BCMA_ANY_CLASS),
  88. {},
  89. };
  90. MODULE_DEVICE_TABLE(bcma, bgmac_bcma_tbl);
  91. /* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipattach */
  92. static int bgmac_probe(struct bcma_device *core)
  93. {
  94. struct bcma_chipinfo *ci = &core->bus->chipinfo;
  95. struct ssb_sprom *sprom = &core->bus->sprom;
  96. struct mii_bus *mii_bus;
  97. struct bgmac *bgmac;
  98. const u8 *mac = NULL;
  99. int err;
  100. bgmac = bgmac_alloc(&core->dev);
  101. if (!bgmac)
  102. return -ENOMEM;
  103. bgmac->bcma.core = core;
  104. bgmac->dma_dev = core->dma_dev;
  105. bgmac->irq = core->irq;
  106. bcma_set_drvdata(core, bgmac);
  107. if (bgmac->dev->of_node)
  108. mac = of_get_mac_address(bgmac->dev->of_node);
  109. /* If no MAC address assigned via device tree, check SPROM */
  110. if (!mac) {
  111. switch (core->core_unit) {
  112. case 0:
  113. mac = sprom->et0mac;
  114. break;
  115. case 1:
  116. mac = sprom->et1mac;
  117. break;
  118. case 2:
  119. mac = sprom->et2mac;
  120. break;
  121. default:
  122. dev_err(bgmac->dev, "Unsupported core_unit %d\n",
  123. core->core_unit);
  124. err = -ENOTSUPP;
  125. goto err;
  126. }
  127. }
  128. ether_addr_copy(bgmac->net_dev->dev_addr, mac);
  129. /* On BCM4706 we need common core to access PHY */
  130. if (core->id.id == BCMA_CORE_4706_MAC_GBIT &&
  131. !core->bus->drv_gmac_cmn.core) {
  132. dev_err(bgmac->dev, "GMAC CMN core not found (required for BCM4706)\n");
  133. err = -ENODEV;
  134. goto err;
  135. }
  136. bgmac->bcma.cmn = core->bus->drv_gmac_cmn.core;
  137. switch (core->core_unit) {
  138. case 0:
  139. bgmac->phyaddr = sprom->et0phyaddr;
  140. break;
  141. case 1:
  142. bgmac->phyaddr = sprom->et1phyaddr;
  143. break;
  144. case 2:
  145. bgmac->phyaddr = sprom->et2phyaddr;
  146. break;
  147. }
  148. bgmac->phyaddr &= BGMAC_PHY_MASK;
  149. if (bgmac->phyaddr == BGMAC_PHY_MASK) {
  150. dev_err(bgmac->dev, "No PHY found\n");
  151. err = -ENODEV;
  152. goto err;
  153. }
  154. dev_info(bgmac->dev, "Found PHY addr: %d%s\n", bgmac->phyaddr,
  155. bgmac->phyaddr == BGMAC_PHY_NOREGS ? " (NOREGS)" : "");
  156. if (!bgmac_is_bcm4707_family(core) &&
  157. !(ci->id == BCMA_CHIP_ID_BCM53573 && core->core_unit == 1)) {
  158. struct phy_device *phydev;
  159. mii_bus = bcma_mdio_mii_register(bgmac);
  160. if (IS_ERR(mii_bus)) {
  161. err = PTR_ERR(mii_bus);
  162. goto err;
  163. }
  164. bgmac->mii_bus = mii_bus;
  165. phydev = mdiobus_get_phy(bgmac->mii_bus, bgmac->phyaddr);
  166. if (ci->id == BCMA_CHIP_ID_BCM53573 && phydev &&
  167. (phydev->drv->phy_id & phydev->drv->phy_id_mask) == PHY_ID_BCM54210E)
  168. phydev->dev_flags |= PHY_BRCM_EN_MASTER_MODE;
  169. }
  170. if (core->bus->hosttype == BCMA_HOSTTYPE_PCI) {
  171. dev_err(bgmac->dev, "PCI setup not implemented\n");
  172. err = -ENOTSUPP;
  173. goto err1;
  174. }
  175. bgmac->has_robosw = !!(sprom->boardflags_lo & BGMAC_BFL_ENETROBO);
  176. if (bgmac->has_robosw)
  177. dev_warn(bgmac->dev, "Support for Roboswitch not implemented\n");
  178. if (sprom->boardflags_lo & BGMAC_BFL_ENETADM)
  179. dev_warn(bgmac->dev, "Support for ADMtek ethernet switch not implemented\n");
  180. /* Feature Flags */
  181. switch (ci->id) {
  182. /* BCM 471X/535X family */
  183. case BCMA_CHIP_ID_BCM4716:
  184. bgmac->feature_flags |= BGMAC_FEAT_CLKCTLST;
  185. /* fallthrough */
  186. case BCMA_CHIP_ID_BCM47162:
  187. bgmac->feature_flags |= BGMAC_FEAT_FLW_CTRL2;
  188. bgmac->feature_flags |= BGMAC_FEAT_SET_RXQ_CLK;
  189. break;
  190. case BCMA_CHIP_ID_BCM5357:
  191. case BCMA_CHIP_ID_BCM53572:
  192. bgmac->feature_flags |= BGMAC_FEAT_SET_RXQ_CLK;
  193. bgmac->feature_flags |= BGMAC_FEAT_CLKCTLST;
  194. bgmac->feature_flags |= BGMAC_FEAT_FLW_CTRL1;
  195. bgmac->feature_flags |= BGMAC_FEAT_SW_TYPE_PHY;
  196. if (ci->pkg == BCMA_PKG_ID_BCM47188 ||
  197. ci->pkg == BCMA_PKG_ID_BCM47186) {
  198. bgmac->feature_flags |= BGMAC_FEAT_SW_TYPE_RGMII;
  199. bgmac->feature_flags |= BGMAC_FEAT_IOST_ATTACHED;
  200. }
  201. if (ci->pkg == BCMA_PKG_ID_BCM5358)
  202. bgmac->feature_flags |= BGMAC_FEAT_SW_TYPE_EPHYRMII;
  203. break;
  204. case BCMA_CHIP_ID_BCM53573:
  205. bgmac->feature_flags |= BGMAC_FEAT_CLKCTLST;
  206. bgmac->feature_flags |= BGMAC_FEAT_SET_RXQ_CLK;
  207. if (ci->pkg == BCMA_PKG_ID_BCM47189)
  208. bgmac->feature_flags |= BGMAC_FEAT_IOST_ATTACHED;
  209. if (core->core_unit == 0) {
  210. bgmac->feature_flags |= BGMAC_FEAT_CC4_IF_SW_TYPE;
  211. if (ci->pkg == BCMA_PKG_ID_BCM47189)
  212. bgmac->feature_flags |=
  213. BGMAC_FEAT_CC4_IF_SW_TYPE_RGMII;
  214. } else if (core->core_unit == 1) {
  215. bgmac->feature_flags |= BGMAC_FEAT_IRQ_ID_OOB_6;
  216. bgmac->feature_flags |= BGMAC_FEAT_CC7_IF_TYPE_RGMII;
  217. }
  218. break;
  219. case BCMA_CHIP_ID_BCM4749:
  220. bgmac->feature_flags |= BGMAC_FEAT_SET_RXQ_CLK;
  221. bgmac->feature_flags |= BGMAC_FEAT_CLKCTLST;
  222. bgmac->feature_flags |= BGMAC_FEAT_FLW_CTRL1;
  223. bgmac->feature_flags |= BGMAC_FEAT_SW_TYPE_PHY;
  224. if (ci->pkg == 10) {
  225. bgmac->feature_flags |= BGMAC_FEAT_SW_TYPE_RGMII;
  226. bgmac->feature_flags |= BGMAC_FEAT_IOST_ATTACHED;
  227. }
  228. break;
  229. /* bcm4707_family */
  230. case BCMA_CHIP_ID_BCM4707:
  231. case BCMA_CHIP_ID_BCM47094:
  232. case BCMA_CHIP_ID_BCM53018:
  233. bgmac->feature_flags |= BGMAC_FEAT_CLKCTLST;
  234. bgmac->feature_flags |= BGMAC_FEAT_NO_RESET;
  235. bgmac->feature_flags |= BGMAC_FEAT_FORCE_SPEED_2500;
  236. break;
  237. default:
  238. bgmac->feature_flags |= BGMAC_FEAT_CLKCTLST;
  239. bgmac->feature_flags |= BGMAC_FEAT_SET_RXQ_CLK;
  240. }
  241. if (!bgmac_is_bcm4707_family(core) && core->id.rev > 2)
  242. bgmac->feature_flags |= BGMAC_FEAT_MISC_PLL_REQ;
  243. if (core->id.id == BCMA_CORE_4706_MAC_GBIT) {
  244. bgmac->feature_flags |= BGMAC_FEAT_CMN_PHY_CTL;
  245. bgmac->feature_flags |= BGMAC_FEAT_NO_CLR_MIB;
  246. }
  247. if (core->id.rev >= 4) {
  248. bgmac->feature_flags |= BGMAC_FEAT_CMDCFG_SR_REV4;
  249. bgmac->feature_flags |= BGMAC_FEAT_TX_MASK_SETUP;
  250. bgmac->feature_flags |= BGMAC_FEAT_RX_MASK_SETUP;
  251. }
  252. bgmac->read = bcma_bgmac_read;
  253. bgmac->write = bcma_bgmac_write;
  254. bgmac->idm_read = bcma_bgmac_idm_read;
  255. bgmac->idm_write = bcma_bgmac_idm_write;
  256. bgmac->clk_enabled = bcma_bgmac_clk_enabled;
  257. bgmac->clk_enable = bcma_bgmac_clk_enable;
  258. bgmac->cco_ctl_maskset = bcma_bgmac_cco_ctl_maskset;
  259. bgmac->get_bus_clock = bcma_bgmac_get_bus_clock;
  260. bgmac->cmn_maskset32 = bcma_bgmac_cmn_maskset32;
  261. if (bgmac->mii_bus)
  262. bgmac->phy_connect = bcma_phy_connect;
  263. else
  264. bgmac->phy_connect = bgmac_phy_connect_direct;
  265. err = bgmac_enet_probe(bgmac);
  266. if (err)
  267. goto err1;
  268. return 0;
  269. err1:
  270. bcma_mdio_mii_unregister(bgmac->mii_bus);
  271. err:
  272. bcma_set_drvdata(core, NULL);
  273. return err;
  274. }
  275. static void bgmac_remove(struct bcma_device *core)
  276. {
  277. struct bgmac *bgmac = bcma_get_drvdata(core);
  278. bcma_mdio_mii_unregister(bgmac->mii_bus);
  279. bgmac_enet_remove(bgmac);
  280. bcma_set_drvdata(core, NULL);
  281. kfree(bgmac);
  282. }
  283. static struct bcma_driver bgmac_bcma_driver = {
  284. .name = KBUILD_MODNAME,
  285. .id_table = bgmac_bcma_tbl,
  286. .probe = bgmac_probe,
  287. .remove = bgmac_remove,
  288. };
  289. static int __init bgmac_init(void)
  290. {
  291. int err;
  292. err = bcma_driver_register(&bgmac_bcma_driver);
  293. if (err)
  294. return err;
  295. pr_info("Broadcom 47xx GBit MAC driver loaded\n");
  296. return 0;
  297. }
  298. static void __exit bgmac_exit(void)
  299. {
  300. bcma_driver_unregister(&bgmac_bcma_driver);
  301. }
  302. module_init(bgmac_init)
  303. module_exit(bgmac_exit)
  304. MODULE_AUTHOR("Rafał Miłecki");
  305. MODULE_LICENSE("GPL");