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