bgmac-platform.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * Copyright (C) 2016 Broadcom
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation version 2.
  7. *
  8. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  9. * kind, whether express or implied; without even the implied warranty
  10. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  14. #include <linux/bcma/bcma.h>
  15. #include <linux/brcmphy.h>
  16. #include <linux/etherdevice.h>
  17. #include <linux/of_address.h>
  18. #include <linux/of_mdio.h>
  19. #include <linux/of_net.h>
  20. #include "bgmac.h"
  21. #define NICPM_PADRING_CFG 0x00000004
  22. #define NICPM_IOMUX_CTRL 0x00000008
  23. #define NICPM_PADRING_CFG_INIT_VAL 0x74000000
  24. #define NICPM_IOMUX_CTRL_INIT_VAL_AX 0x21880000
  25. #define NICPM_IOMUX_CTRL_INIT_VAL 0x3196e000
  26. #define NICPM_IOMUX_CTRL_SPD_SHIFT 10
  27. #define NICPM_IOMUX_CTRL_SPD_10M 0
  28. #define NICPM_IOMUX_CTRL_SPD_100M 1
  29. #define NICPM_IOMUX_CTRL_SPD_1000M 2
  30. static u32 platform_bgmac_read(struct bgmac *bgmac, u16 offset)
  31. {
  32. return readl(bgmac->plat.base + offset);
  33. }
  34. static void platform_bgmac_write(struct bgmac *bgmac, u16 offset, u32 value)
  35. {
  36. writel(value, bgmac->plat.base + offset);
  37. }
  38. static u32 platform_bgmac_idm_read(struct bgmac *bgmac, u16 offset)
  39. {
  40. return readl(bgmac->plat.idm_base + offset);
  41. }
  42. static void platform_bgmac_idm_write(struct bgmac *bgmac, u16 offset, u32 value)
  43. {
  44. writel(value, bgmac->plat.idm_base + offset);
  45. }
  46. static bool platform_bgmac_clk_enabled(struct bgmac *bgmac)
  47. {
  48. if (!bgmac->plat.idm_base)
  49. return true;
  50. if ((bgmac_idm_read(bgmac, BCMA_IOCTL) & BGMAC_CLK_EN) != BGMAC_CLK_EN)
  51. return false;
  52. if (bgmac_idm_read(bgmac, BCMA_RESET_CTL) & BCMA_RESET_CTL_RESET)
  53. return false;
  54. return true;
  55. }
  56. static void platform_bgmac_clk_enable(struct bgmac *bgmac, u32 flags)
  57. {
  58. u32 val;
  59. if (!bgmac->plat.idm_base)
  60. return;
  61. /* The Reset Control register only contains a single bit to show if the
  62. * controller is currently in reset. Do a sanity check here, just in
  63. * case the bootloader happened to leave the device in reset.
  64. */
  65. val = bgmac_idm_read(bgmac, BCMA_RESET_CTL);
  66. if (val) {
  67. bgmac_idm_write(bgmac, BCMA_RESET_CTL, 0);
  68. bgmac_idm_read(bgmac, BCMA_RESET_CTL);
  69. udelay(1);
  70. }
  71. val = bgmac_idm_read(bgmac, BCMA_IOCTL);
  72. /* Some bits of BCMA_IOCTL set by HW/ATF and should not change */
  73. val |= flags & ~(BGMAC_AWCACHE | BGMAC_ARCACHE | BGMAC_AWUSER |
  74. BGMAC_ARUSER);
  75. val |= BGMAC_CLK_EN;
  76. bgmac_idm_write(bgmac, BCMA_IOCTL, val);
  77. bgmac_idm_read(bgmac, BCMA_IOCTL);
  78. udelay(1);
  79. }
  80. static void platform_bgmac_cco_ctl_maskset(struct bgmac *bgmac, u32 offset,
  81. u32 mask, u32 set)
  82. {
  83. /* This shouldn't be encountered */
  84. WARN_ON(1);
  85. }
  86. static u32 platform_bgmac_get_bus_clock(struct bgmac *bgmac)
  87. {
  88. /* This shouldn't be encountered */
  89. WARN_ON(1);
  90. return 0;
  91. }
  92. static void platform_bgmac_cmn_maskset32(struct bgmac *bgmac, u16 offset,
  93. u32 mask, u32 set)
  94. {
  95. /* This shouldn't be encountered */
  96. WARN_ON(1);
  97. }
  98. static void bgmac_nicpm_speed_set(struct net_device *net_dev)
  99. {
  100. struct bgmac *bgmac = netdev_priv(net_dev);
  101. u32 val;
  102. if (!bgmac->plat.nicpm_base)
  103. return;
  104. /* SET RGMII IO CONFIG */
  105. writel(NICPM_PADRING_CFG_INIT_VAL,
  106. bgmac->plat.nicpm_base + NICPM_PADRING_CFG);
  107. val = NICPM_IOMUX_CTRL_INIT_VAL;
  108. switch (bgmac->net_dev->phydev->speed) {
  109. default:
  110. netdev_err(net_dev, "Unsupported speed. Defaulting to 1000Mb\n");
  111. /* fall through */
  112. case SPEED_1000:
  113. val |= NICPM_IOMUX_CTRL_SPD_1000M << NICPM_IOMUX_CTRL_SPD_SHIFT;
  114. break;
  115. case SPEED_100:
  116. val |= NICPM_IOMUX_CTRL_SPD_100M << NICPM_IOMUX_CTRL_SPD_SHIFT;
  117. break;
  118. case SPEED_10:
  119. val |= NICPM_IOMUX_CTRL_SPD_10M << NICPM_IOMUX_CTRL_SPD_SHIFT;
  120. break;
  121. }
  122. writel(val, bgmac->plat.nicpm_base + NICPM_IOMUX_CTRL);
  123. bgmac_adjust_link(bgmac->net_dev);
  124. }
  125. static int platform_phy_connect(struct bgmac *bgmac)
  126. {
  127. struct phy_device *phy_dev;
  128. if (bgmac->plat.nicpm_base)
  129. phy_dev = of_phy_get_and_connect(bgmac->net_dev,
  130. bgmac->dev->of_node,
  131. bgmac_nicpm_speed_set);
  132. else
  133. phy_dev = of_phy_get_and_connect(bgmac->net_dev,
  134. bgmac->dev->of_node,
  135. bgmac_adjust_link);
  136. if (!phy_dev) {
  137. dev_err(bgmac->dev, "PHY connection failed\n");
  138. return -ENODEV;
  139. }
  140. return 0;
  141. }
  142. static int bgmac_probe(struct platform_device *pdev)
  143. {
  144. struct device_node *np = pdev->dev.of_node;
  145. struct bgmac *bgmac;
  146. struct resource *regs;
  147. const u8 *mac_addr;
  148. bgmac = bgmac_alloc(&pdev->dev);
  149. if (!bgmac)
  150. return -ENOMEM;
  151. platform_set_drvdata(pdev, bgmac);
  152. /* Set the features of the 4707 family */
  153. bgmac->feature_flags |= BGMAC_FEAT_CLKCTLST;
  154. bgmac->feature_flags |= BGMAC_FEAT_NO_RESET;
  155. bgmac->feature_flags |= BGMAC_FEAT_CMDCFG_SR_REV4;
  156. bgmac->feature_flags |= BGMAC_FEAT_TX_MASK_SETUP;
  157. bgmac->feature_flags |= BGMAC_FEAT_RX_MASK_SETUP;
  158. bgmac->feature_flags |= BGMAC_FEAT_IDM_MASK;
  159. bgmac->dev = &pdev->dev;
  160. bgmac->dma_dev = &pdev->dev;
  161. mac_addr = of_get_mac_address(np);
  162. if (mac_addr)
  163. ether_addr_copy(bgmac->net_dev->dev_addr, mac_addr);
  164. else
  165. dev_warn(&pdev->dev, "MAC address not present in device tree\n");
  166. bgmac->irq = platform_get_irq(pdev, 0);
  167. if (bgmac->irq < 0) {
  168. dev_err(&pdev->dev, "Unable to obtain IRQ\n");
  169. return bgmac->irq;
  170. }
  171. regs = platform_get_resource_byname(pdev, IORESOURCE_MEM, "amac_base");
  172. if (!regs) {
  173. dev_err(&pdev->dev, "Unable to obtain base resource\n");
  174. return -EINVAL;
  175. }
  176. bgmac->plat.base = devm_ioremap_resource(&pdev->dev, regs);
  177. if (IS_ERR(bgmac->plat.base))
  178. return PTR_ERR(bgmac->plat.base);
  179. regs = platform_get_resource_byname(pdev, IORESOURCE_MEM, "idm_base");
  180. if (regs) {
  181. bgmac->plat.idm_base = devm_ioremap_resource(&pdev->dev, regs);
  182. if (IS_ERR(bgmac->plat.idm_base))
  183. return PTR_ERR(bgmac->plat.idm_base);
  184. bgmac->feature_flags &= ~BGMAC_FEAT_IDM_MASK;
  185. }
  186. regs = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nicpm_base");
  187. if (regs) {
  188. bgmac->plat.nicpm_base = devm_ioremap_resource(&pdev->dev,
  189. regs);
  190. if (IS_ERR(bgmac->plat.nicpm_base))
  191. return PTR_ERR(bgmac->plat.nicpm_base);
  192. }
  193. bgmac->read = platform_bgmac_read;
  194. bgmac->write = platform_bgmac_write;
  195. bgmac->idm_read = platform_bgmac_idm_read;
  196. bgmac->idm_write = platform_bgmac_idm_write;
  197. bgmac->clk_enabled = platform_bgmac_clk_enabled;
  198. bgmac->clk_enable = platform_bgmac_clk_enable;
  199. bgmac->cco_ctl_maskset = platform_bgmac_cco_ctl_maskset;
  200. bgmac->get_bus_clock = platform_bgmac_get_bus_clock;
  201. bgmac->cmn_maskset32 = platform_bgmac_cmn_maskset32;
  202. if (of_parse_phandle(np, "phy-handle", 0)) {
  203. bgmac->phy_connect = platform_phy_connect;
  204. } else {
  205. bgmac->phy_connect = bgmac_phy_connect_direct;
  206. bgmac->feature_flags |= BGMAC_FEAT_FORCE_SPEED_2500;
  207. }
  208. return bgmac_enet_probe(bgmac);
  209. }
  210. static int bgmac_remove(struct platform_device *pdev)
  211. {
  212. struct bgmac *bgmac = platform_get_drvdata(pdev);
  213. bgmac_enet_remove(bgmac);
  214. return 0;
  215. }
  216. #ifdef CONFIG_PM
  217. static int bgmac_suspend(struct device *dev)
  218. {
  219. struct bgmac *bgmac = dev_get_drvdata(dev);
  220. return bgmac_enet_suspend(bgmac);
  221. }
  222. static int bgmac_resume(struct device *dev)
  223. {
  224. struct bgmac *bgmac = dev_get_drvdata(dev);
  225. return bgmac_enet_resume(bgmac);
  226. }
  227. static const struct dev_pm_ops bgmac_pm_ops = {
  228. .suspend = bgmac_suspend,
  229. .resume = bgmac_resume
  230. };
  231. #define BGMAC_PM_OPS (&bgmac_pm_ops)
  232. #else
  233. #define BGMAC_PM_OPS NULL
  234. #endif /* CONFIG_PM */
  235. static const struct of_device_id bgmac_of_enet_match[] = {
  236. {.compatible = "brcm,amac",},
  237. {.compatible = "brcm,nsp-amac",},
  238. {.compatible = "brcm,ns2-amac",},
  239. {},
  240. };
  241. MODULE_DEVICE_TABLE(of, bgmac_of_enet_match);
  242. static struct platform_driver bgmac_enet_driver = {
  243. .driver = {
  244. .name = "bgmac-enet",
  245. .of_match_table = bgmac_of_enet_match,
  246. .pm = BGMAC_PM_OPS
  247. },
  248. .probe = bgmac_probe,
  249. .remove = bgmac_remove,
  250. };
  251. module_platform_driver(bgmac_enet_driver);
  252. MODULE_LICENSE("GPL");