Jon Mason | f6a95a2 | 2016-07-07 19:08:57 -0400 | [diff] [blame] | 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 | |
| 14 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 15 | |
| 16 | #include <linux/bcma/bcma.h> |
Jon Mason | dd5c5d0 | 2016-11-04 01:11:01 -0400 | [diff] [blame] | 17 | #include <linux/brcmphy.h> |
Jon Mason | f6a95a2 | 2016-07-07 19:08:57 -0400 | [diff] [blame] | 18 | #include <linux/etherdevice.h> |
| 19 | #include <linux/of_address.h> |
Jon Mason | 1676aba5ef | 2016-11-04 01:11:00 -0400 | [diff] [blame] | 20 | #include <linux/of_mdio.h> |
Jon Mason | f6a95a2 | 2016-07-07 19:08:57 -0400 | [diff] [blame] | 21 | #include <linux/of_net.h> |
| 22 | #include "bgmac.h" |
| 23 | |
Jon Mason | dd5c5d0 | 2016-11-04 01:11:01 -0400 | [diff] [blame] | 24 | #define NICPM_IOMUX_CTRL 0x00000008 |
| 25 | |
| 26 | #define NICPM_IOMUX_CTRL_INIT_VAL 0x3196e000 |
| 27 | #define NICPM_IOMUX_CTRL_SPD_SHIFT 10 |
| 28 | #define NICPM_IOMUX_CTRL_SPD_10M 0 |
| 29 | #define NICPM_IOMUX_CTRL_SPD_100M 1 |
| 30 | #define NICPM_IOMUX_CTRL_SPD_1000M 2 |
| 31 | |
Jon Mason | f6a95a2 | 2016-07-07 19:08:57 -0400 | [diff] [blame] | 32 | static u32 platform_bgmac_read(struct bgmac *bgmac, u16 offset) |
| 33 | { |
| 34 | return readl(bgmac->plat.base + offset); |
| 35 | } |
| 36 | |
| 37 | static void platform_bgmac_write(struct bgmac *bgmac, u16 offset, u32 value) |
| 38 | { |
| 39 | writel(value, bgmac->plat.base + offset); |
| 40 | } |
| 41 | |
| 42 | static u32 platform_bgmac_idm_read(struct bgmac *bgmac, u16 offset) |
| 43 | { |
| 44 | return readl(bgmac->plat.idm_base + offset); |
| 45 | } |
| 46 | |
| 47 | static void platform_bgmac_idm_write(struct bgmac *bgmac, u16 offset, u32 value) |
| 48 | { |
| 49 | return writel(value, bgmac->plat.idm_base + offset); |
| 50 | } |
| 51 | |
| 52 | static bool platform_bgmac_clk_enabled(struct bgmac *bgmac) |
| 53 | { |
Jon Mason | 1620652 | 2017-03-02 17:59:56 -0500 | [diff] [blame^] | 54 | if ((bgmac_idm_read(bgmac, BCMA_IOCTL) & BGMAC_CLK_EN) != BGMAC_CLK_EN) |
Jon Mason | f6a95a2 | 2016-07-07 19:08:57 -0400 | [diff] [blame] | 55 | return false; |
| 56 | if (bgmac_idm_read(bgmac, BCMA_RESET_CTL) & BCMA_RESET_CTL_RESET) |
| 57 | return false; |
| 58 | return true; |
| 59 | } |
| 60 | |
| 61 | static void platform_bgmac_clk_enable(struct bgmac *bgmac, u32 flags) |
| 62 | { |
Jon Mason | 1620652 | 2017-03-02 17:59:56 -0500 | [diff] [blame^] | 63 | u32 val; |
Jon Mason | f6a95a2 | 2016-07-07 19:08:57 -0400 | [diff] [blame] | 64 | |
Jon Mason | 1620652 | 2017-03-02 17:59:56 -0500 | [diff] [blame^] | 65 | /* The Reset Control register only contains a single bit to show if the |
| 66 | * controller is currently in reset. Do a sanity check here, just in |
| 67 | * case the bootloader happened to leave the device in reset. |
| 68 | */ |
| 69 | val = bgmac_idm_read(bgmac, BCMA_RESET_CTL); |
| 70 | if (val) { |
| 71 | bgmac_idm_write(bgmac, BCMA_RESET_CTL, 0); |
| 72 | bgmac_idm_read(bgmac, BCMA_RESET_CTL); |
| 73 | udelay(1); |
| 74 | } |
Jon Mason | f6a95a2 | 2016-07-07 19:08:57 -0400 | [diff] [blame] | 75 | |
Jon Mason | 1620652 | 2017-03-02 17:59:56 -0500 | [diff] [blame^] | 76 | val = bgmac_idm_read(bgmac, BCMA_IOCTL); |
| 77 | /* Some bits of BCMA_IOCTL set by HW/ATF and should not change */ |
| 78 | val |= flags & ~(BGMAC_AWCACHE | BGMAC_ARCACHE | BGMAC_AWUSER | |
| 79 | BGMAC_ARUSER); |
| 80 | val |= BGMAC_CLK_EN; |
| 81 | bgmac_idm_write(bgmac, BCMA_IOCTL, val); |
Jon Mason | f6a95a2 | 2016-07-07 19:08:57 -0400 | [diff] [blame] | 82 | bgmac_idm_read(bgmac, BCMA_IOCTL); |
| 83 | udelay(1); |
| 84 | } |
| 85 | |
| 86 | static void platform_bgmac_cco_ctl_maskset(struct bgmac *bgmac, u32 offset, |
| 87 | u32 mask, u32 set) |
| 88 | { |
| 89 | /* This shouldn't be encountered */ |
| 90 | WARN_ON(1); |
| 91 | } |
| 92 | |
| 93 | static u32 platform_bgmac_get_bus_clock(struct bgmac *bgmac) |
| 94 | { |
| 95 | /* This shouldn't be encountered */ |
| 96 | WARN_ON(1); |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | static void platform_bgmac_cmn_maskset32(struct bgmac *bgmac, u16 offset, |
| 102 | u32 mask, u32 set) |
| 103 | { |
| 104 | /* This shouldn't be encountered */ |
| 105 | WARN_ON(1); |
| 106 | } |
| 107 | |
Jon Mason | dd5c5d0 | 2016-11-04 01:11:01 -0400 | [diff] [blame] | 108 | static void bgmac_nicpm_speed_set(struct net_device *net_dev) |
| 109 | { |
| 110 | struct bgmac *bgmac = netdev_priv(net_dev); |
| 111 | u32 val; |
| 112 | |
| 113 | if (!bgmac->plat.nicpm_base) |
| 114 | return; |
| 115 | |
| 116 | val = NICPM_IOMUX_CTRL_INIT_VAL; |
| 117 | switch (bgmac->net_dev->phydev->speed) { |
| 118 | default: |
| 119 | netdev_err(net_dev, "Unsupported speed. Defaulting to 1000Mb\n"); |
| 120 | case SPEED_1000: |
| 121 | val |= NICPM_IOMUX_CTRL_SPD_1000M << NICPM_IOMUX_CTRL_SPD_SHIFT; |
| 122 | break; |
| 123 | case SPEED_100: |
| 124 | val |= NICPM_IOMUX_CTRL_SPD_100M << NICPM_IOMUX_CTRL_SPD_SHIFT; |
| 125 | break; |
| 126 | case SPEED_10: |
| 127 | val |= NICPM_IOMUX_CTRL_SPD_10M << NICPM_IOMUX_CTRL_SPD_SHIFT; |
| 128 | break; |
| 129 | } |
| 130 | |
| 131 | writel(val, bgmac->plat.nicpm_base + NICPM_IOMUX_CTRL); |
| 132 | |
| 133 | bgmac_adjust_link(bgmac->net_dev); |
| 134 | } |
| 135 | |
Jon Mason | 1676aba5ef | 2016-11-04 01:11:00 -0400 | [diff] [blame] | 136 | static int platform_phy_connect(struct bgmac *bgmac) |
| 137 | { |
| 138 | struct phy_device *phy_dev; |
| 139 | |
Jon Mason | dd5c5d0 | 2016-11-04 01:11:01 -0400 | [diff] [blame] | 140 | if (bgmac->plat.nicpm_base) |
| 141 | phy_dev = of_phy_get_and_connect(bgmac->net_dev, |
| 142 | bgmac->dev->of_node, |
| 143 | bgmac_nicpm_speed_set); |
| 144 | else |
| 145 | phy_dev = of_phy_get_and_connect(bgmac->net_dev, |
| 146 | bgmac->dev->of_node, |
| 147 | bgmac_adjust_link); |
Jon Mason | 1676aba5ef | 2016-11-04 01:11:00 -0400 | [diff] [blame] | 148 | if (!phy_dev) { |
| 149 | dev_err(bgmac->dev, "PHY connection failed\n"); |
| 150 | return -ENODEV; |
| 151 | } |
| 152 | |
| 153 | return 0; |
| 154 | } |
| 155 | |
Jon Mason | f6a95a2 | 2016-07-07 19:08:57 -0400 | [diff] [blame] | 156 | static int bgmac_probe(struct platform_device *pdev) |
| 157 | { |
| 158 | struct device_node *np = pdev->dev.of_node; |
| 159 | struct bgmac *bgmac; |
| 160 | struct resource *regs; |
| 161 | const u8 *mac_addr; |
| 162 | |
Rafał Miłecki | 34a5102 | 2017-01-31 19:37:54 +0100 | [diff] [blame] | 163 | bgmac = bgmac_alloc(&pdev->dev); |
Jon Mason | f6a95a2 | 2016-07-07 19:08:57 -0400 | [diff] [blame] | 164 | if (!bgmac) |
| 165 | return -ENOMEM; |
| 166 | |
| 167 | platform_set_drvdata(pdev, bgmac); |
| 168 | |
| 169 | /* Set the features of the 4707 family */ |
| 170 | bgmac->feature_flags |= BGMAC_FEAT_CLKCTLST; |
| 171 | bgmac->feature_flags |= BGMAC_FEAT_NO_RESET; |
Jon Mason | f6a95a2 | 2016-07-07 19:08:57 -0400 | [diff] [blame] | 172 | bgmac->feature_flags |= BGMAC_FEAT_CMDCFG_SR_REV4; |
| 173 | bgmac->feature_flags |= BGMAC_FEAT_TX_MASK_SETUP; |
| 174 | bgmac->feature_flags |= BGMAC_FEAT_RX_MASK_SETUP; |
| 175 | |
| 176 | bgmac->dev = &pdev->dev; |
| 177 | bgmac->dma_dev = &pdev->dev; |
| 178 | |
| 179 | mac_addr = of_get_mac_address(np); |
| 180 | if (mac_addr) |
Tobias Klauser | 6850f8b | 2017-02-16 15:11:19 +0100 | [diff] [blame] | 181 | ether_addr_copy(bgmac->net_dev->dev_addr, mac_addr); |
Jon Mason | f6a95a2 | 2016-07-07 19:08:57 -0400 | [diff] [blame] | 182 | else |
| 183 | dev_warn(&pdev->dev, "MAC address not present in device tree\n"); |
| 184 | |
| 185 | bgmac->irq = platform_get_irq(pdev, 0); |
| 186 | if (bgmac->irq < 0) { |
| 187 | dev_err(&pdev->dev, "Unable to obtain IRQ\n"); |
| 188 | return bgmac->irq; |
| 189 | } |
| 190 | |
| 191 | regs = platform_get_resource_byname(pdev, IORESOURCE_MEM, "amac_base"); |
| 192 | if (!regs) { |
| 193 | dev_err(&pdev->dev, "Unable to obtain base resource\n"); |
| 194 | return -EINVAL; |
| 195 | } |
| 196 | |
| 197 | bgmac->plat.base = devm_ioremap_resource(&pdev->dev, regs); |
Wei Yongjun | ce3a380 | 2016-07-13 12:46:57 +0000 | [diff] [blame] | 198 | if (IS_ERR(bgmac->plat.base)) |
Jon Mason | f6a95a2 | 2016-07-07 19:08:57 -0400 | [diff] [blame] | 199 | return PTR_ERR(bgmac->plat.base); |
Jon Mason | f6a95a2 | 2016-07-07 19:08:57 -0400 | [diff] [blame] | 200 | |
| 201 | regs = platform_get_resource_byname(pdev, IORESOURCE_MEM, "idm_base"); |
| 202 | if (!regs) { |
| 203 | dev_err(&pdev->dev, "Unable to obtain idm resource\n"); |
| 204 | return -EINVAL; |
| 205 | } |
| 206 | |
| 207 | bgmac->plat.idm_base = devm_ioremap_resource(&pdev->dev, regs); |
Wei Yongjun | ce3a380 | 2016-07-13 12:46:57 +0000 | [diff] [blame] | 208 | if (IS_ERR(bgmac->plat.idm_base)) |
Jon Mason | f6a95a2 | 2016-07-07 19:08:57 -0400 | [diff] [blame] | 209 | return PTR_ERR(bgmac->plat.idm_base); |
Jon Mason | f6a95a2 | 2016-07-07 19:08:57 -0400 | [diff] [blame] | 210 | |
Jon Mason | dd5c5d0 | 2016-11-04 01:11:01 -0400 | [diff] [blame] | 211 | regs = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nicpm_base"); |
| 212 | if (regs) { |
| 213 | bgmac->plat.nicpm_base = devm_ioremap_resource(&pdev->dev, |
| 214 | regs); |
| 215 | if (IS_ERR(bgmac->plat.nicpm_base)) |
| 216 | return PTR_ERR(bgmac->plat.nicpm_base); |
| 217 | } |
| 218 | |
Jon Mason | f6a95a2 | 2016-07-07 19:08:57 -0400 | [diff] [blame] | 219 | bgmac->read = platform_bgmac_read; |
| 220 | bgmac->write = platform_bgmac_write; |
| 221 | bgmac->idm_read = platform_bgmac_idm_read; |
| 222 | bgmac->idm_write = platform_bgmac_idm_write; |
| 223 | bgmac->clk_enabled = platform_bgmac_clk_enabled; |
| 224 | bgmac->clk_enable = platform_bgmac_clk_enable; |
| 225 | bgmac->cco_ctl_maskset = platform_bgmac_cco_ctl_maskset; |
| 226 | bgmac->get_bus_clock = platform_bgmac_get_bus_clock; |
| 227 | bgmac->cmn_maskset32 = platform_bgmac_cmn_maskset32; |
Jon Mason | 1676aba5ef | 2016-11-04 01:11:00 -0400 | [diff] [blame] | 228 | if (of_parse_phandle(np, "phy-handle", 0)) { |
| 229 | bgmac->phy_connect = platform_phy_connect; |
| 230 | } else { |
| 231 | bgmac->phy_connect = bgmac_phy_connect_direct; |
| 232 | bgmac->feature_flags |= BGMAC_FEAT_FORCE_SPEED_2500; |
| 233 | } |
Jon Mason | f6a95a2 | 2016-07-07 19:08:57 -0400 | [diff] [blame] | 234 | |
| 235 | return bgmac_enet_probe(bgmac); |
| 236 | } |
| 237 | |
| 238 | static int bgmac_remove(struct platform_device *pdev) |
| 239 | { |
| 240 | struct bgmac *bgmac = platform_get_drvdata(pdev); |
| 241 | |
| 242 | bgmac_enet_remove(bgmac); |
| 243 | |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | static const struct of_device_id bgmac_of_enet_match[] = { |
| 248 | {.compatible = "brcm,amac",}, |
| 249 | {.compatible = "brcm,nsp-amac",}, |
Jon Mason | dd5c5d0 | 2016-11-04 01:11:01 -0400 | [diff] [blame] | 250 | {.compatible = "brcm,ns2-amac",}, |
Jon Mason | f6a95a2 | 2016-07-07 19:08:57 -0400 | [diff] [blame] | 251 | {}, |
| 252 | }; |
| 253 | |
| 254 | MODULE_DEVICE_TABLE(of, bgmac_of_enet_match); |
| 255 | |
| 256 | static struct platform_driver bgmac_enet_driver = { |
| 257 | .driver = { |
| 258 | .name = "bgmac-enet", |
| 259 | .of_match_table = bgmac_of_enet_match, |
| 260 | }, |
| 261 | .probe = bgmac_probe, |
| 262 | .remove = bgmac_remove, |
| 263 | }; |
| 264 | |
| 265 | module_platform_driver(bgmac_enet_driver); |
| 266 | MODULE_LICENSE("GPL"); |