Debian Codegraph @

drivers/net/dsa/lantiq/lantiq_gswip.c

raw
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Lantiq / Intel GSWIP switch driver for VRX200, xRX300 and xRX330 SoCs
4 *
5 * Copyright (C) 2025 Daniel Golle <[email protected]>
6 * Copyright (C) 2017 - 2019 Hauke Mehrtens <[email protected]>
7 * Copyright (C) 2012 John Crispin <[email protected]>
8 * Copyright (C) 2010 Lantiq Deutschland
9 */
10
11 #include "lantiq_gswip.h"
12 #include "lantiq_pce.h"
13
14 #include <linux/clk.h>
15 #include <linux/delay.h>
16 #include <linux/firmware.h>
17 #include <linux/mfd/syscon.h>
18 #include <linux/module.h>
19 #include <linux/of_platform.h>
20 #include <linux/platform_device.h>
21 #include <linux/regmap.h>
22 #include <linux/reset.h>
23 #include <dt-bindings/mips/lantiq_rcu_gphy.h>
24
25 #include <net/dsa.h>
26
27 struct xway_gphy_match_data {
28 char *fe_firmware_name;
29 char *ge_firmware_name;
30 };
31
32 static void gswip_xrx200_phylink_get_caps(struct dsa_switch *ds, int port,
33 struct phylink_config *config)
34 {
35 switch (port) {
36 case 0 ... 1:
37 phy_interface_set_rgmii(config->supported_interfaces);
38 __set_bit(PHY_INTERFACE_MODE_MII,
39 config->supported_interfaces);
40 __set_bit(PHY_INTERFACE_MODE_REVMII,
41 config->supported_interfaces);
42 __set_bit(PHY_INTERFACE_MODE_RMII,
43 config->supported_interfaces);
44 break;
45
46 case 2 ... 4:
47 case 6:
48 __set_bit(PHY_INTERFACE_MODE_INTERNAL,
49 config->supported_interfaces);
50 break;
51
52 case 5:
53 phy_interface_set_rgmii(config->supported_interfaces);
54 __set_bit(PHY_INTERFACE_MODE_INTERNAL,
55 config->supported_interfaces);
56 break;
57 }
58
59 config->mac_capabilities = MAC_ASYM_PAUSE | MAC_SYM_PAUSE |
60 MAC_10 | MAC_100 | MAC_1000;
61 }
62
63 static void gswip_xrx300_phylink_get_caps(struct dsa_switch *ds, int port,
64 struct phylink_config *config)
65 {
66 switch (port) {
67 case 0:
68 phy_interface_set_rgmii(config->supported_interfaces);
69 __set_bit(PHY_INTERFACE_MODE_GMII,
70 config->supported_interfaces);
71 __set_bit(PHY_INTERFACE_MODE_RMII,
72 config->supported_interfaces);
73 break;
74
75 case 1 ... 4:
76 case 6:
77 __set_bit(PHY_INTERFACE_MODE_INTERNAL,
78 config->supported_interfaces);
79 break;
80
81 case 5:
82 phy_interface_set_rgmii(config->supported_interfaces);
83 __set_bit(PHY_INTERFACE_MODE_INTERNAL,
84 config->supported_interfaces);
85 __set_bit(PHY_INTERFACE_MODE_RMII,
86 config->supported_interfaces);
87 break;
88 }
89
90 config->mac_capabilities = MAC_ASYM_PAUSE | MAC_SYM_PAUSE |
91 MAC_10 | MAC_100 | MAC_1000;
92 }
93
94 static const struct xway_gphy_match_data xrx200a1x_gphy_data = {
95 .fe_firmware_name = "lantiq/xrx200_phy22f_a14.bin",
96 .ge_firmware_name = "lantiq/xrx200_phy11g_a14.bin",
97 };
98
99 static const struct xway_gphy_match_data xrx200a2x_gphy_data = {
100 .fe_firmware_name = "lantiq/xrx200_phy22f_a22.bin",
101 .ge_firmware_name = "lantiq/xrx200_phy11g_a22.bin",
102 };
103
104 static const struct xway_gphy_match_data xrx300_gphy_data = {
105 .fe_firmware_name = "lantiq/xrx300_phy22f_a21.bin",
106 .ge_firmware_name = "lantiq/xrx300_phy11g_a21.bin",
107 };
108
109 static const struct of_device_id xway_gphy_match[] __maybe_unused = {
110 { .compatible = "lantiq,xrx200-gphy-fw", .data = NULL },
111 { .compatible = "lantiq,xrx200a1x-gphy-fw", .data = &xrx200a1x_gphy_data },
112 { .compatible = "lantiq,xrx200a2x-gphy-fw", .data = &xrx200a2x_gphy_data },
113 { .compatible = "lantiq,xrx300-gphy-fw", .data = &xrx300_gphy_data },
114 { .compatible = "lantiq,xrx330-gphy-fw", .data = &xrx300_gphy_data },
115 {},
116 };
117
118 static int gswip_gphy_fw_load(struct gswip_priv *priv, struct gswip_gphy_fw *gphy_fw)
119 {
120 struct device *dev = priv->dev;
121 const struct firmware *fw;
122 void *fw_addr;
123 dma_addr_t dma_addr;
124 dma_addr_t dev_addr;
125 size_t size;
126 int ret;
127
128 ret = clk_prepare_enable(gphy_fw->clk_gate);
129 if (ret)
130 return ret;
131
132 reset_control_assert(gphy_fw->reset);
133
134 /* The vendor BSP uses a 200ms delay after asserting the reset line.
135 * Without this some users are observing that the PHY is not coming up
136 * on the MDIO bus.
137 */
138 msleep(200);
139
140 ret = request_firmware(&fw, gphy_fw->fw_name, dev);
141 if (ret)
142 return dev_err_probe(dev, ret, "failed to load firmware: %s\n",
143 gphy_fw->fw_name);
144
145 /* GPHY cores need the firmware code in a persistent and contiguous
146 * memory area with a 16 kB boundary aligned start address.
147 */
148 size = fw->size + XRX200_GPHY_FW_ALIGN;
149
150 fw_addr = dmam_alloc_coherent(dev, size, &dma_addr, GFP_KERNEL);
151 if (fw_addr) {
152 fw_addr = PTR_ALIGN(fw_addr, XRX200_GPHY_FW_ALIGN);
153 dev_addr = ALIGN(dma_addr, XRX200_GPHY_FW_ALIGN);
154 memcpy(fw_addr, fw->data, fw->size);
155 } else {
156 release_firmware(fw);
157 return -ENOMEM;
158 }
159
160 release_firmware(fw);
161
162 ret = regmap_write(priv->rcu_regmap, gphy_fw->fw_addr_offset, dev_addr);
163 if (ret)
164 return ret;
165
166 reset_control_deassert(gphy_fw->reset);
167
168 return ret;
169 }
170
171 static int gswip_gphy_fw_probe(struct gswip_priv *priv,
172 struct gswip_gphy_fw *gphy_fw,
173 struct device_node *gphy_fw_np, int i)
174 {
175 struct device *dev = priv->dev;
176 u32 gphy_mode;
177 int ret;
178 char gphyname[10];
179
180 snprintf(gphyname, sizeof(gphyname), "gphy%d", i);
181
182 gphy_fw->clk_gate = devm_clk_get(dev, gphyname);
183 if (IS_ERR(gphy_fw->clk_gate)) {
184 return dev_err_probe(dev, PTR_ERR(gphy_fw->clk_gate),
185 "Failed to lookup gate clock\n");
186 }
187
188 ret = of_property_read_u32(gphy_fw_np, "reg", &gphy_fw->fw_addr_offset);
189 if (ret)
190 return ret;
191
192 ret = of_property_read_u32(gphy_fw_np, "lantiq,gphy-mode", &gphy_mode);
193 /* Default to GE mode */
194 if (ret)
195 gphy_mode = GPHY_MODE_GE;
196
197 switch (gphy_mode) {
198 case GPHY_MODE_FE:
199 gphy_fw->fw_name = priv->gphy_fw_name_cfg->fe_firmware_name;
200 break;
201 case GPHY_MODE_GE:
202 gphy_fw->fw_name = priv->gphy_fw_name_cfg->ge_firmware_name;
203 break;
204 default:
205 return dev_err_probe(dev, -EINVAL, "Unknown GPHY mode %d\n",
206 gphy_mode);
207 }
208
209 gphy_fw->reset = of_reset_control_array_get_exclusive(gphy_fw_np);
210 if (IS_ERR(gphy_fw->reset))
211 return dev_err_probe(dev, PTR_ERR(gphy_fw->reset),
212 "Failed to lookup gphy reset\n");
213
214 return gswip_gphy_fw_load(priv, gphy_fw);
215 }
216
217 static void gswip_gphy_fw_remove(struct gswip_priv *priv,
218 struct gswip_gphy_fw *gphy_fw)
219 {
220 int ret;
221
222 /* check if the device was fully probed */
223 if (!gphy_fw->fw_name)
224 return;
225
226 ret = regmap_write(priv->rcu_regmap, gphy_fw->fw_addr_offset, 0);
227 if (ret)
228 dev_err(priv->dev, "can not reset GPHY FW pointer\n");
229
230 clk_disable_unprepare(gphy_fw->clk_gate);
231
232 reset_control_put(gphy_fw->reset);
233 }
234
235 static int gswip_gphy_fw_list(struct gswip_priv *priv,
236 struct device_node *gphy_fw_list_np, u32 version)
237 {
238 struct device *dev = priv->dev;
239 struct device_node *gphy_fw_np;
240 const struct of_device_id *match;
241 int err;
242 int i = 0;
243
244 /* The VRX200 rev 1.1 uses the GSWIP 2.0 and needs the older
245 * GPHY firmware. The VRX200 rev 1.2 uses the GSWIP 2.1 and also
246 * needs a different GPHY firmware.
247 */
248 if (of_device_is_compatible(gphy_fw_list_np, "lantiq,xrx200-gphy-fw")) {
249 switch (version) {
250 case GSWIP_VERSION_2_0:
251 priv->gphy_fw_name_cfg = &xrx200a1x_gphy_data;
252 break;
253 case GSWIP_VERSION_2_1:
254 priv->gphy_fw_name_cfg = &xrx200a2x_gphy_data;
255 break;
256 default:
257 return dev_err_probe(dev, -ENOENT,
258 "unknown GSWIP version: 0x%x\n",
259 version);
260 }
261 }
262
263 match = of_match_node(xway_gphy_match, gphy_fw_list_np);
264 if (match && match->data)
265 priv->gphy_fw_name_cfg = match->data;
266
267 if (!priv->gphy_fw_name_cfg)
268 return dev_err_probe(dev, -ENOENT,
269 "GPHY compatible type not supported\n");
270
271 priv->num_gphy_fw = of_get_available_child_count(gphy_fw_list_np);
272 if (!priv->num_gphy_fw)
273 return -ENOENT;
274
275 priv->rcu_regmap = syscon_regmap_lookup_by_phandle(gphy_fw_list_np,
276 "lantiq,rcu");
277 if (IS_ERR(priv->rcu_regmap))
278 return PTR_ERR(priv->rcu_regmap);
279
280 priv->gphy_fw = devm_kmalloc_array(dev, priv->num_gphy_fw,
281 sizeof(*priv->gphy_fw),
282 GFP_KERNEL | __GFP_ZERO);
283 if (!priv->gphy_fw)
284 return -ENOMEM;
285
286 for_each_available_child_of_node(gphy_fw_list_np, gphy_fw_np) {
287 err = gswip_gphy_fw_probe(priv, &priv->gphy_fw[i],
288 gphy_fw_np, i);
289 if (err) {
290 of_node_put(gphy_fw_np);
291 goto remove_gphy;
292 }
293 i++;
294 }
295
296 /* The standalone PHY11G requires 300ms to be fully
297 * initialized and ready for any MDIO communication after being
298 * taken out of reset. For the SoC-internal GPHY variant there
299 * is no (known) documentation for the minimum time after a
300 * reset. Use the same value as for the standalone variant as
301 * some users have reported internal PHYs not being detected
302 * without any delay.
303 */
304 msleep(300);
305
306 return 0;
307
308 remove_gphy:
309 for (i = 0; i < priv->num_gphy_fw; i++)
310 gswip_gphy_fw_remove(priv, &priv->gphy_fw[i]);
311 return err;
312 }
313
314 static const struct regmap_config sw_regmap_config = {
315 .name = "switch",
316 .reg_bits = 32,
317 .val_bits = 32,
318 .reg_shift = REGMAP_UPSHIFT(2),
319 .val_format_endian = REGMAP_ENDIAN_NATIVE,
320 .max_register = GSWIP_SDMA_PCTRLp(6),
321 };
322
323 static const struct regmap_config mdio_regmap_config = {
324 .name = "mdio",
325 .reg_bits = 32,
326 .val_bits = 32,
327 .reg_shift = REGMAP_UPSHIFT(2),
328 .val_format_endian = REGMAP_ENDIAN_NATIVE,
329 .max_register = GSWIP_MDIO_PHYp(0),
330 };
331
332 static const struct regmap_config mii_regmap_config = {
333 .name = "mii",
334 .reg_bits = 32,
335 .val_bits = 32,
336 .reg_shift = REGMAP_UPSHIFT(2),
337 .val_format_endian = REGMAP_ENDIAN_NATIVE,
338 .max_register = GSWIP_MII_CFGp(6),
339 };
340
341 static int gswip_probe(struct platform_device *pdev)
342 {
343 struct device_node *np, *gphy_fw_np;
344 __iomem void *gswip, *mdio, *mii;
345 struct device *dev = &pdev->dev;
346 struct gswip_priv *priv;
347 int err;
348 int i;
349 u32 version;
350
351 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
352 if (!priv)
353 return -ENOMEM;
354
355 gswip = devm_platform_ioremap_resource(pdev, 0);
356 if (IS_ERR(gswip))
357 return PTR_ERR(gswip);
358
359 mdio = devm_platform_ioremap_resource(pdev, 1);
360 if (IS_ERR(mdio))
361 return PTR_ERR(mdio);
362
363 mii = devm_platform_ioremap_resource(pdev, 2);
364 if (IS_ERR(mii))
365 return PTR_ERR(mii);
366
367 priv->gswip = devm_regmap_init_mmio(dev, gswip, &sw_regmap_config);
368 if (IS_ERR(priv->gswip))
369 return PTR_ERR(priv->gswip);
370
371 priv->mdio = devm_regmap_init_mmio(dev, mdio, &mdio_regmap_config);
372 if (IS_ERR(priv->mdio))
373 return PTR_ERR(priv->mdio);
374
375 priv->mii = devm_regmap_init_mmio(dev, mii, &mii_regmap_config);
376 if (IS_ERR(priv->mii))
377 return PTR_ERR(priv->mii);
378
379 priv->hw_info = of_device_get_match_data(dev);
380 if (!priv->hw_info)
381 return -EINVAL;
382
383 priv->ds = devm_kzalloc(dev, sizeof(*priv->ds), GFP_KERNEL);
384 if (!priv->ds)
385 return -ENOMEM;
386
387 priv->dev = dev;
388
389 regmap_read(priv->gswip, GSWIP_VERSION, &version);
390
391 np = dev->of_node;
392 switch (version) {
393 case GSWIP_VERSION_2_0:
394 case GSWIP_VERSION_2_1:
395 if (!of_device_is_compatible(np, "lantiq,xrx200-gswip"))
396 return -EINVAL;
397 break;
398 case GSWIP_VERSION_2_2:
399 case GSWIP_VERSION_2_2_ETC:
400 if (!of_device_is_compatible(np, "lantiq,xrx300-gswip") &&
401 !of_device_is_compatible(np, "lantiq,xrx330-gswip"))
402 return -EINVAL;
403 break;
404 default:
405 return dev_err_probe(dev, -ENOENT,
406 "unknown GSWIP version: 0x%x\n", version);
407 }
408
409 /* bring up the mdio bus */
410 gphy_fw_np = of_get_compatible_child(dev->of_node, "lantiq,gphy-fw");
411 if (gphy_fw_np) {
412 err = gswip_gphy_fw_list(priv, gphy_fw_np, version);
413 of_node_put(gphy_fw_np);
414 if (err)
415 return dev_err_probe(dev, err,
416 "gphy fw probe failed\n");
417 }
418
419 err = gswip_probe_common(priv, version);
420 if (err)
421 goto gphy_fw_remove;
422
423 platform_set_drvdata(pdev, priv);
424
425 return 0;
426
427 gphy_fw_remove:
428 for (i = 0; i < priv->num_gphy_fw; i++)
429 gswip_gphy_fw_remove(priv, &priv->gphy_fw[i]);
430 return err;
431 }
432
433 static void gswip_remove(struct platform_device *pdev)
434 {
435 struct gswip_priv *priv = platform_get_drvdata(pdev);
436 int i;
437
438 if (!priv)
439 return;
440
441 dsa_unregister_switch(priv->ds);
442
443 for (i = 0; i < priv->num_gphy_fw; i++)
444 gswip_gphy_fw_remove(priv, &priv->gphy_fw[i]);
445 }
446
447 static void gswip_shutdown(struct platform_device *pdev)
448 {
449 struct gswip_priv *priv = platform_get_drvdata(pdev);
450
451 if (!priv)
452 return;
453
454 dsa_switch_shutdown(priv->ds);
455
456 platform_set_drvdata(pdev, NULL);
457 }
458
459 static const struct gswip_hw_info gswip_xrx200 = {
460 .max_ports = GSWIP_MAX_PORTS,
461 .allowed_cpu_ports = BIT(6),
462 .mii_cfg = {
463 [0] = GSWIP_MII_CFGp(0),
464 [1] = GSWIP_MII_CFGp(1),
465 [2 ... 4] = -1,
466 [5] = GSWIP_MII_CFGp(5),
467 [6] = -1,
468 },
469 .mii_pcdu = {
470 [0] = GSWIP_MII_PCDU0,
471 [1] = GSWIP_MII_PCDU1,
472 [2 ... 4] = -1,
473 [5] = GSWIP_MII_PCDU5,
474 [6] = -1,
475 },
476 .phylink_get_caps = gswip_xrx200_phylink_get_caps,
477 .pce_microcode = &gswip_pce_microcode,
478 .pce_microcode_size = ARRAY_SIZE(gswip_pce_microcode),
479 .tag_protocol = DSA_TAG_PROTO_GSWIP,
480 };
481
482 static const struct gswip_hw_info gswip_xrx300 = {
483 .max_ports = GSWIP_MAX_PORTS,
484 .allowed_cpu_ports = BIT(6),
485 .mii_cfg = {
486 [0] = GSWIP_MII_CFGp(0),
487 [1 ... 4] = -1,
488 [5] = GSWIP_MII_CFGp(5),
489 [6] = -1,
490 },
491 .mii_pcdu = {
492 [0] = GSWIP_MII_PCDU0,
493 [1 ... 4] = -1,
494 [5] = GSWIP_MII_PCDU5,
495 [6] = -1,
496 },
497 .phylink_get_caps = gswip_xrx300_phylink_get_caps,
498 .pce_microcode = &gswip_pce_microcode,
499 .pce_microcode_size = ARRAY_SIZE(gswip_pce_microcode),
500 .tag_protocol = DSA_TAG_PROTO_GSWIP,
501 };
502
503 static const struct of_device_id gswip_of_match[] = {
504 { .compatible = "lantiq,xrx200-gswip", .data = &gswip_xrx200 },
505 { .compatible = "lantiq,xrx300-gswip", .data = &gswip_xrx300 },
506 { .compatible = "lantiq,xrx330-gswip", .data = &gswip_xrx300 },
507 {},
508 };
509 MODULE_DEVICE_TABLE(of, gswip_of_match);
510
511 static struct platform_driver gswip_driver = {
512 .probe = gswip_probe,
513 .remove = gswip_remove,
514 .shutdown = gswip_shutdown,
515 .driver = {
516 .name = "gswip",
517 .of_match_table = gswip_of_match,
518 },
519 };
520
521 module_platform_driver(gswip_driver);
522
523 MODULE_FIRMWARE("lantiq/xrx300_phy11g_a21.bin");
524 MODULE_FIRMWARE("lantiq/xrx300_phy22f_a21.bin");
525 MODULE_FIRMWARE("lantiq/xrx200_phy11g_a14.bin");
526 MODULE_FIRMWARE("lantiq/xrx200_phy11g_a22.bin");
527 MODULE_FIRMWARE("lantiq/xrx200_phy22f_a14.bin");
528 MODULE_FIRMWARE("lantiq/xrx200_phy22f_a22.bin");
529 MODULE_AUTHOR("Hauke Mehrtens <[email protected]>");
530 MODULE_DESCRIPTION("Lantiq / Intel GSWIP driver");
531 MODULE_LICENSE("GPL v2");