1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0-or-later 2 /* 3 * Copyright 2008 - 2015 Freescale Semiconductor Inc. 4 */ 5 6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 7 8 #include <linux/init.h> 9 #include <linux/module.h> 10 #include <linux/of_address.h> 11 #include <linux/of_platform.h> 12 #include <linux/of_net.h> 13 #include <linux/of_mdio.h> 14 #include <linux/device.h> 15 #include <linux/of_platform.h> 16 #include <linux/phy.h> 17 #include <linux/netdevice.h> 18 #include <linux/phy_fixed.h> 19 #include <linux/phylink.h> 20 #include <linux/etherdevice.h> 21 #include <linux/libfdt_env.h> 22 #include <linux/platform_device.h> 23 24 #include "mac.h" 25 #include "fman_mac.h" 26 #include "fman_dtsec.h" 27 #include "fman_tgec.h" 28 #include "fman_memac.h" 29 30 MODULE_LICENSE("Dual BSD/GPL"); 31 MODULE_DESCRIPTION("FSL FMan MAC API based driver"); 32 33 struct mac_priv_s { 34 u8 cell_index; 35 struct fman *fman; 36 /* List of multicast addresses */ 37 struct list_head mc_addr_list; 38 struct platform_device *eth_dev; 39 u16 speed; 40 }; 41 42 struct mac_address { 43 u8 addr[ETH_ALEN]; 44 struct list_head list; 45 }; 46 47 static void mac_exception(struct mac_device *mac_dev, 48 enum fman_mac_exceptions ex) 49 { 50 if (ex == FM_MAC_EX_10G_RX_FIFO_OVFL) { 51 /* don't flag RX FIFO after the first */ 52 mac_dev->set_exception(mac_dev->fman_mac, 53 FM_MAC_EX_10G_RX_FIFO_OVFL, false); 54 dev_err(mac_dev->dev, "10G MAC got RX FIFO Error = %x\n", ex); 55 } 56 57 dev_dbg(mac_dev->dev, "%s:%s() -> %d\n", KBUILD_BASENAME ".c", 58 __func__, ex); 59 } 60 61 int fman_set_multi(struct net_device *net_dev, struct mac_device *mac_dev) 62 { 63 struct mac_priv_s *priv; 64 struct mac_address *old_addr, *tmp; 65 struct netdev_hw_addr *ha; 66 int err; 67 enet_addr_t *addr; 68 69 priv = mac_dev->priv; 70 71 /* Clear previous address list */ 72 list_for_each_entry_safe(old_addr, tmp, &priv->mc_addr_list, list) { 73 addr = (enet_addr_t *)old_addr->addr; 74 err = mac_dev->remove_hash_mac_addr(mac_dev->fman_mac, addr); 75 if (err < 0) 76 return err; 77 78 list_del(&old_addr->list); 79 kfree(old_addr); 80 } 81 82 /* Add all the addresses from the new list */ 83 netdev_for_each_mc_addr(ha, net_dev) { 84 addr = (enet_addr_t *)ha->addr; 85 err = mac_dev->add_hash_mac_addr(mac_dev->fman_mac, addr); 86 if (err < 0) 87 return err; 88 89 tmp = kmalloc(sizeof(*tmp), GFP_ATOMIC); 90 if (!tmp) 91 return -ENOMEM; 92 93 ether_addr_copy(tmp->addr, ha->addr); 94 list_add(&tmp->list, &priv->mc_addr_list); 95 } 96 return 0; 97 } 98 99 static DEFINE_MUTEX(eth_lock); 100 101 static struct platform_device *dpaa_eth_add_device(int fman_id, 102 struct mac_device *mac_dev) 103 { 104 struct platform_device *pdev; 105 struct dpaa_eth_data data; 106 struct mac_priv_s *priv; 107 static int dpaa_eth_dev_cnt; 108 int ret; 109 110 priv = mac_dev->priv; 111 112 data.mac_dev = mac_dev; 113 data.mac_hw_id = priv->cell_index; 114 data.fman_hw_id = fman_id; 115 116 mutex_lock(ð_lock); 117 pdev = platform_device_alloc("dpaa-ethernet", dpaa_eth_dev_cnt); 118 if (!pdev) { 119 ret = -ENOMEM; 120 goto no_mem; 121 } 122 123 pdev->dev.parent = mac_dev->dev; 124 125 ret = platform_device_add_data(pdev, &data, sizeof(data)); 126 if (ret) 127 goto err; 128 129 ret = platform_device_add(pdev); 130 if (ret) 131 goto err; 132 133 dpaa_eth_dev_cnt++; 134 mutex_unlock(ð_lock); 135 136 return pdev; 137 138 err: 139 platform_device_put(pdev); 140 no_mem: 141 mutex_unlock(ð_lock); 142 143 return ERR_PTR(ret); 144 } 145 146 static const struct of_device_id mac_match[] = { 147 { .compatible = "fsl,fman-dtsec", .data = dtsec_initialization }, 148 { .compatible = "fsl,fman-xgec", .data = tgec_initialization }, 149 { .compatible = "fsl,fman-memac", .data = memac_initialization }, 150 {} 151 }; 152 MODULE_DEVICE_TABLE(of, mac_match); 153 154 static int mac_probe(struct platform_device *_of_dev) 155 { 156 int err, i, nph; 157 int (*init)(struct mac_device *mac_dev, struct device_node *mac_node, 158 struct fman_mac_params *params); 159 struct device *dev; 160 struct device_node *mac_node, *dev_node; 161 struct mac_device *mac_dev; 162 struct platform_device *of_dev; 163 struct mac_priv_s *priv; 164 struct fman_mac_params params; 165 u32 val; 166 u8 fman_id; 167 phy_interface_t phy_if; 168 169 dev = &_of_dev->dev; 170 mac_node = dev->of_node; 171 init = of_device_get_match_data(dev); 172 173 mac_dev = devm_kzalloc(dev, sizeof(*mac_dev), GFP_KERNEL); 174 if (!mac_dev) 175 return -ENOMEM; 176 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 177 if (!priv) 178 return -ENOMEM; 179 platform_set_drvdata(_of_dev, mac_dev); 180 181 /* Save private information */ 182 mac_dev->priv = priv; 183 mac_dev->dev = dev; 184 185 INIT_LIST_HEAD(&priv->mc_addr_list); 186 187 /* Get the FM node */ 188 dev_node = of_get_parent(mac_node); 189 if (!dev_node) { 190 dev_err(dev, "of_get_parent(%pOF) failed\n", 191 mac_node); 192 return -EINVAL; 193 } 194 195 of_dev = of_find_device_by_node(dev_node); 196 if (!of_dev) { 197 dev_err(dev, "of_find_device_by_node(%pOF) failed\n", dev_node); 198 err = -EINVAL; 199 goto _return_of_node_put; 200 } 201 202 /* Get the FMan cell-index */ 203 err = of_property_read_u32(dev_node, "cell-index", &val); 204 if (err) { 205 dev_err(dev, "failed to read cell-index for %pOF\n", dev_node); 206 err = -EINVAL; 207 goto _return_of_node_put; 208 } 209 /* cell-index 0 => FMan id 1 */ 210 fman_id = (u8)(val + 1); 211 212 priv->fman = fman_bind(&of_dev->dev); 213 if (!priv->fman) { 214 dev_err(dev, "fman_bind(%pOF) failed\n", dev_node); 215 err = -ENODEV; 216 goto _return_of_node_put; 217 } 218 219 of_node_put(dev_node); 220 221 /* Get the address of the memory mapped registers */ 222 mac_dev->res = platform_get_mem_or_io(_of_dev, 0); 223 if (!mac_dev->res) { 224 dev_err(dev, "could not get registers\n"); 225 return -EINVAL; 226 } 227 228 err = devm_request_resource(dev, fman_get_mem_region(priv->fman), 229 mac_dev->res); 230 if (err) { 231 dev_err_probe(dev, err, "could not request resource\n"); 232 return err; 233 } 234 235 mac_dev->vaddr = devm_ioremap(dev, mac_dev->res->start, 236 resource_size(mac_dev->res)); 237 if (!mac_dev->vaddr) { 238 dev_err(dev, "devm_ioremap() failed\n"); 239 return -EIO; 240 } 241 242 if (!of_device_is_available(mac_node)) 243 return -ENODEV; 244 245 /* Get the cell-index */ 246 err = of_property_read_u32(mac_node, "cell-index", &val); 247 if (err) { 248 dev_err(dev, "failed to read cell-index for %pOF\n", mac_node); 249 return -EINVAL; 250 } 251 priv->cell_index = (u8)val; 252 253 /* Get the MAC address */ 254 err = of_get_mac_address(mac_node, mac_dev->addr); 255 if (err) 256 dev_warn(dev, "of_get_mac_address(%pOF) failed\n", mac_node); 257 258 /* Get the port handles */ 259 nph = of_count_phandle_with_args(mac_node, "fsl,fman-ports", NULL); 260 if (unlikely(nph < 0)) { 261 dev_err(dev, "of_count_phandle_with_args(%pOF, fsl,fman-ports) failed\n", 262 mac_node); 263 return nph; 264 } 265 266 if (nph != ARRAY_SIZE(mac_dev->port)) { 267 dev_err(dev, "Not supported number of fman-ports handles of mac node %pOF from device tree\n", 268 mac_node); 269 return -EINVAL; 270 } 271 272 for (i = 0; i < ARRAY_SIZE(mac_dev->port); i++) { 273 /* Find the port node */ 274 dev_node = of_parse_phandle(mac_node, "fsl,fman-ports", i); 275 if (!dev_node) { 276 dev_err(dev, "of_parse_phandle(%pOF, fsl,fman-ports) failed\n", 277 mac_node); 278 return -EINVAL; 279 } 280 281 of_dev = of_find_device_by_node(dev_node); 282 if (!of_dev) { 283 dev_err(dev, "of_find_device_by_node(%pOF) failed\n", 284 dev_node); 285 err = -EINVAL; 286 goto _return_of_node_put; 287 } 288 289 mac_dev->port[i] = fman_port_bind(&of_dev->dev); 290 if (!mac_dev->port[i]) { 291 dev_err(dev, "dev_get_drvdata(%pOF) failed\n", 292 dev_node); 293 err = -EINVAL; 294 goto _return_of_node_put; 295 } 296 of_node_put(dev_node); 297 } 298 299 /* Get the PHY connection type */ 300 err = of_get_phy_mode(mac_node, &phy_if); 301 if (err) { 302 dev_warn(dev, 303 "of_get_phy_mode() for %pOF failed. Defaulting to SGMII\n", 304 mac_node); 305 phy_if = PHY_INTERFACE_MODE_SGMII; 306 } 307 mac_dev->phy_if = phy_if; 308 309 params.mac_id = priv->cell_index; 310 params.fm = (void *)priv->fman; 311 params.exception_cb = mac_exception; 312 params.event_cb = mac_exception; 313 314 err = init(mac_dev, mac_node, ¶ms); 315 if (err < 0) 316 return err; 317 318 if (!is_zero_ether_addr(mac_dev->addr)) 319 dev_info(dev, "FMan MAC address: %pM\n", mac_dev->addr); 320 321 priv->eth_dev = dpaa_eth_add_device(fman_id, mac_dev); 322 if (IS_ERR(priv->eth_dev)) { 323 err = PTR_ERR(priv->eth_dev); 324 dev_err(dev, "failed to add Ethernet platform device for MAC %d\n", 325 priv->cell_index); 326 priv->eth_dev = NULL; 327 } 328 329 return err; 330 331 _return_of_node_put: 332 of_node_put(dev_node); 333 return err; 334 } 335 336 static void mac_remove(struct platform_device *pdev) 337 { 338 struct mac_device *mac_dev = platform_get_drvdata(pdev); 339 340 platform_device_unregister(mac_dev->priv->eth_dev); 341 } 342 343 static struct platform_driver mac_driver = { 344 .driver = { 345 .name = KBUILD_MODNAME, 346 .of_match_table = mac_match, 347 }, 348 .probe = mac_probe, 349 .remove_new = mac_remove, 350 }; 351 352 builtin_platform_driver(mac_driver); 353