bgmac-platform.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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/etherdevice.h>
  16. #include <linux/of_address.h>
  17. #include <linux/of_net.h>
  18. #include "bgmac.h"
  19. static u32 platform_bgmac_read(struct bgmac *bgmac, u16 offset)
  20. {
  21. return readl(bgmac->plat.base + offset);
  22. }
  23. static void platform_bgmac_write(struct bgmac *bgmac, u16 offset, u32 value)
  24. {
  25. writel(value, bgmac->plat.base + offset);
  26. }
  27. static u32 platform_bgmac_idm_read(struct bgmac *bgmac, u16 offset)
  28. {
  29. return readl(bgmac->plat.idm_base + offset);
  30. }
  31. static void platform_bgmac_idm_write(struct bgmac *bgmac, u16 offset, u32 value)
  32. {
  33. return writel(value, bgmac->plat.idm_base + offset);
  34. }
  35. static bool platform_bgmac_clk_enabled(struct bgmac *bgmac)
  36. {
  37. if ((bgmac_idm_read(bgmac, BCMA_IOCTL) &
  38. (BCMA_IOCTL_CLK | BCMA_IOCTL_FGC)) != BCMA_IOCTL_CLK)
  39. return false;
  40. if (bgmac_idm_read(bgmac, BCMA_RESET_CTL) & BCMA_RESET_CTL_RESET)
  41. return false;
  42. return true;
  43. }
  44. static void platform_bgmac_clk_enable(struct bgmac *bgmac, u32 flags)
  45. {
  46. bgmac_idm_write(bgmac, BCMA_IOCTL,
  47. (BCMA_IOCTL_CLK | BCMA_IOCTL_FGC | flags));
  48. bgmac_idm_read(bgmac, BCMA_IOCTL);
  49. bgmac_idm_write(bgmac, BCMA_RESET_CTL, 0);
  50. bgmac_idm_read(bgmac, BCMA_RESET_CTL);
  51. udelay(1);
  52. bgmac_idm_write(bgmac, BCMA_IOCTL, (BCMA_IOCTL_CLK | flags));
  53. bgmac_idm_read(bgmac, BCMA_IOCTL);
  54. udelay(1);
  55. }
  56. static void platform_bgmac_cco_ctl_maskset(struct bgmac *bgmac, u32 offset,
  57. u32 mask, u32 set)
  58. {
  59. /* This shouldn't be encountered */
  60. WARN_ON(1);
  61. }
  62. static u32 platform_bgmac_get_bus_clock(struct bgmac *bgmac)
  63. {
  64. /* This shouldn't be encountered */
  65. WARN_ON(1);
  66. return 0;
  67. }
  68. static void platform_bgmac_cmn_maskset32(struct bgmac *bgmac, u16 offset,
  69. u32 mask, u32 set)
  70. {
  71. /* This shouldn't be encountered */
  72. WARN_ON(1);
  73. }
  74. static int bgmac_probe(struct platform_device *pdev)
  75. {
  76. struct device_node *np = pdev->dev.of_node;
  77. struct bgmac *bgmac;
  78. struct resource *regs;
  79. const u8 *mac_addr;
  80. bgmac = devm_kzalloc(&pdev->dev, sizeof(*bgmac), GFP_KERNEL);
  81. if (!bgmac)
  82. return -ENOMEM;
  83. platform_set_drvdata(pdev, bgmac);
  84. /* Set the features of the 4707 family */
  85. bgmac->feature_flags |= BGMAC_FEAT_CLKCTLST;
  86. bgmac->feature_flags |= BGMAC_FEAT_NO_RESET;
  87. bgmac->feature_flags |= BGMAC_FEAT_FORCE_SPEED_2500;
  88. bgmac->feature_flags |= BGMAC_FEAT_CMDCFG_SR_REV4;
  89. bgmac->feature_flags |= BGMAC_FEAT_TX_MASK_SETUP;
  90. bgmac->feature_flags |= BGMAC_FEAT_RX_MASK_SETUP;
  91. bgmac->dev = &pdev->dev;
  92. bgmac->dma_dev = &pdev->dev;
  93. mac_addr = of_get_mac_address(np);
  94. if (mac_addr)
  95. ether_addr_copy(bgmac->mac_addr, mac_addr);
  96. else
  97. dev_warn(&pdev->dev, "MAC address not present in device tree\n");
  98. bgmac->irq = platform_get_irq(pdev, 0);
  99. if (bgmac->irq < 0) {
  100. dev_err(&pdev->dev, "Unable to obtain IRQ\n");
  101. return bgmac->irq;
  102. }
  103. regs = platform_get_resource_byname(pdev, IORESOURCE_MEM, "amac_base");
  104. if (!regs) {
  105. dev_err(&pdev->dev, "Unable to obtain base resource\n");
  106. return -EINVAL;
  107. }
  108. bgmac->plat.base = devm_ioremap_resource(&pdev->dev, regs);
  109. if (IS_ERR(bgmac->plat.base))
  110. return PTR_ERR(bgmac->plat.base);
  111. regs = platform_get_resource_byname(pdev, IORESOURCE_MEM, "idm_base");
  112. if (!regs) {
  113. dev_err(&pdev->dev, "Unable to obtain idm resource\n");
  114. return -EINVAL;
  115. }
  116. bgmac->plat.idm_base = devm_ioremap_resource(&pdev->dev, regs);
  117. if (IS_ERR(bgmac->plat.idm_base))
  118. return PTR_ERR(bgmac->plat.idm_base);
  119. bgmac->read = platform_bgmac_read;
  120. bgmac->write = platform_bgmac_write;
  121. bgmac->idm_read = platform_bgmac_idm_read;
  122. bgmac->idm_write = platform_bgmac_idm_write;
  123. bgmac->clk_enabled = platform_bgmac_clk_enabled;
  124. bgmac->clk_enable = platform_bgmac_clk_enable;
  125. bgmac->cco_ctl_maskset = platform_bgmac_cco_ctl_maskset;
  126. bgmac->get_bus_clock = platform_bgmac_get_bus_clock;
  127. bgmac->cmn_maskset32 = platform_bgmac_cmn_maskset32;
  128. return bgmac_enet_probe(bgmac);
  129. }
  130. static int bgmac_remove(struct platform_device *pdev)
  131. {
  132. struct bgmac *bgmac = platform_get_drvdata(pdev);
  133. bgmac_enet_remove(bgmac);
  134. return 0;
  135. }
  136. static const struct of_device_id bgmac_of_enet_match[] = {
  137. {.compatible = "brcm,amac",},
  138. {.compatible = "brcm,nsp-amac",},
  139. {},
  140. };
  141. MODULE_DEVICE_TABLE(of, bgmac_of_enet_match);
  142. static struct platform_driver bgmac_enet_driver = {
  143. .driver = {
  144. .name = "bgmac-enet",
  145. .of_match_table = bgmac_of_enet_match,
  146. },
  147. .probe = bgmac_probe,
  148. .remove = bgmac_remove,
  149. };
  150. module_platform_driver(bgmac_enet_driver);
  151. MODULE_LICENSE("GPL");