1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2022 Schneider Electric
5 * Clément Léger <clement.leger@bootlin.com>
9 #include <linux/device.h>
10 #include <linux/mdio.h>
12 #include <linux/of_platform.h>
13 #include <linux/pcs-rzn1-miic.h>
14 #include <linux/phylink.h>
15 #include <linux/pm_runtime.h>
16 #include <dt-bindings/net/pcs-rzn1-miic.h>
18 #define MIIC_PRCMD 0x0
19 #define MIIC_ESID_CODE 0x4
21 #define MIIC_MODCTRL 0x20
22 #define MIIC_MODCTRL_SW_MODE GENMASK(4, 0)
24 #define MIIC_CONVCTRL(port) (0x100 + (port) * 4)
26 #define MIIC_CONVCTRL_CONV_SPEED GENMASK(1, 0)
27 #define CONV_MODE_10MBPS 0
28 #define CONV_MODE_100MBPS 1
29 #define CONV_MODE_1000MBPS 2
31 #define MIIC_CONVCTRL_CONV_MODE GENMASK(3, 2)
32 #define CONV_MODE_MII 0
33 #define CONV_MODE_RMII 1
34 #define CONV_MODE_RGMII 2
36 #define MIIC_CONVCTRL_FULLD BIT(8)
37 #define MIIC_CONVCTRL_RGMII_LINK BIT(12)
38 #define MIIC_CONVCTRL_RGMII_DUPLEX BIT(13)
39 #define MIIC_CONVCTRL_RGMII_SPEED GENMASK(15, 14)
41 #define MIIC_CONVRST 0x114
42 #define MIIC_CONVRST_PHYIF_RST(port) BIT(port)
43 #define MIIC_CONVRST_PHYIF_RST_MASK GENMASK(4, 0)
45 #define MIIC_SWCTRL 0x304
46 #define MIIC_SWDUPC 0x308
48 #define MIIC_MAX_NR_PORTS 5
50 #define MIIC_MODCTRL_CONF_CONV_NUM 6
51 #define MIIC_MODCTRL_CONF_NONE -1
54 * struct modctrl_match - Matching table entry for convctrl configuration
55 * See section 8.2.1 of manual.
56 * @mode_cfg: Configuration value for convctrl
57 * @conv: Configuration of ethernet port muxes. First index is SWITCH_PORTIN,
58 * then index 1 - 5 are CONV1 - CONV5.
60 struct modctrl_match {
62 u8 conv[MIIC_MODCTRL_CONF_CONV_NUM];
65 static struct modctrl_match modctrl_match_table[] = {
66 {0x0, {MIIC_RTOS_PORT, MIIC_GMAC1_PORT, MIIC_SWITCH_PORTD,
67 MIIC_SWITCH_PORTC, MIIC_SERCOS_PORTB, MIIC_SERCOS_PORTA}},
68 {0x1, {MIIC_RTOS_PORT, MIIC_GMAC1_PORT, MIIC_SWITCH_PORTD,
69 MIIC_SWITCH_PORTC, MIIC_ETHERCAT_PORTB, MIIC_ETHERCAT_PORTA}},
70 {0x2, {MIIC_RTOS_PORT, MIIC_GMAC1_PORT, MIIC_SWITCH_PORTD,
71 MIIC_ETHERCAT_PORTC, MIIC_ETHERCAT_PORTB, MIIC_ETHERCAT_PORTA}},
72 {0x3, {MIIC_RTOS_PORT, MIIC_GMAC1_PORT, MIIC_SWITCH_PORTD,
73 MIIC_SWITCH_PORTC, MIIC_SWITCH_PORTB, MIIC_SWITCH_PORTA}},
75 {0x8, {MIIC_RTOS_PORT, MIIC_GMAC1_PORT, MIIC_SWITCH_PORTD,
76 MIIC_SWITCH_PORTC, MIIC_SERCOS_PORTB, MIIC_SERCOS_PORTA}},
77 {0x9, {MIIC_RTOS_PORT, MIIC_GMAC1_PORT, MIIC_SWITCH_PORTD,
78 MIIC_SWITCH_PORTC, MIIC_ETHERCAT_PORTB, MIIC_ETHERCAT_PORTA}},
79 {0xA, {MIIC_RTOS_PORT, MIIC_GMAC1_PORT, MIIC_SWITCH_PORTD,
80 MIIC_ETHERCAT_PORTC, MIIC_ETHERCAT_PORTB, MIIC_ETHERCAT_PORTA}},
81 {0xB, {MIIC_RTOS_PORT, MIIC_GMAC1_PORT, MIIC_SWITCH_PORTD,
82 MIIC_SWITCH_PORTC, MIIC_SWITCH_PORTB, MIIC_SWITCH_PORTA}},
84 {0x10, {MIIC_GMAC2_PORT, MIIC_GMAC1_PORT, MIIC_SWITCH_PORTD,
85 MIIC_SWITCH_PORTC, MIIC_SERCOS_PORTB, MIIC_SERCOS_PORTA}},
86 {0x11, {MIIC_GMAC2_PORT, MIIC_GMAC1_PORT, MIIC_SWITCH_PORTD,
87 MIIC_SWITCH_PORTC, MIIC_ETHERCAT_PORTB, MIIC_ETHERCAT_PORTA}},
88 {0x12, {MIIC_GMAC2_PORT, MIIC_GMAC1_PORT, MIIC_SWITCH_PORTD,
89 MIIC_ETHERCAT_PORTC, MIIC_ETHERCAT_PORTB, MIIC_ETHERCAT_PORTA}},
90 {0x13, {MIIC_GMAC2_PORT, MIIC_GMAC1_PORT, MIIC_SWITCH_PORTD,
91 MIIC_SWITCH_PORTC, MIIC_SWITCH_PORTB, MIIC_SWITCH_PORTA}}
94 static const char * const conf_to_string[] = {
95 [MIIC_GMAC1_PORT] = "GMAC1_PORT",
96 [MIIC_GMAC2_PORT] = "GMAC2_PORT",
97 [MIIC_RTOS_PORT] = "RTOS_PORT",
98 [MIIC_SERCOS_PORTA] = "SERCOS_PORTA",
99 [MIIC_SERCOS_PORTB] = "SERCOS_PORTB",
100 [MIIC_ETHERCAT_PORTA] = "ETHERCAT_PORTA",
101 [MIIC_ETHERCAT_PORTB] = "ETHERCAT_PORTB",
102 [MIIC_ETHERCAT_PORTC] = "ETHERCAT_PORTC",
103 [MIIC_SWITCH_PORTA] = "SWITCH_PORTA",
104 [MIIC_SWITCH_PORTB] = "SWITCH_PORTB",
105 [MIIC_SWITCH_PORTC] = "SWITCH_PORTC",
106 [MIIC_SWITCH_PORTD] = "SWITCH_PORTD",
107 [MIIC_HSR_PORTA] = "HSR_PORTA",
108 [MIIC_HSR_PORTB] = "HSR_PORTB",
111 static const char *index_to_string[MIIC_MODCTRL_CONF_CONV_NUM] = {
121 * struct miic - MII converter structure
122 * @base: base address of the MII converter
123 * @dev: Device associated to the MII converter
124 * @lock: Lock used for read-modify-write access
133 * struct miic_port - Per port MII converter struct
134 * @miic: backiling to MII converter structure
135 * @pcs: PCS structure associated to the port
137 * @interface: interface mode of the port
141 struct phylink_pcs pcs;
143 phy_interface_t interface;
146 static struct miic_port *phylink_pcs_to_miic_port(struct phylink_pcs *pcs)
148 return container_of(pcs, struct miic_port, pcs);
151 static void miic_reg_writel(struct miic *miic, int offset, u32 value)
153 writel(value, miic->base + offset);
156 static u32 miic_reg_readl(struct miic *miic, int offset)
158 return readl(miic->base + offset);
161 static void miic_reg_rmw(struct miic *miic, int offset, u32 mask, u32 val)
165 spin_lock(&miic->lock);
167 reg = miic_reg_readl(miic, offset);
170 miic_reg_writel(miic, offset, reg);
172 spin_unlock(&miic->lock);
175 static void miic_converter_enable(struct miic *miic, int port, int enable)
180 val = MIIC_CONVRST_PHYIF_RST(port);
182 miic_reg_rmw(miic, MIIC_CONVRST, MIIC_CONVRST_PHYIF_RST(port), val);
185 static int miic_config(struct phylink_pcs *pcs, unsigned int mode,
186 phy_interface_t interface,
187 const unsigned long *advertising, bool permit)
189 struct miic_port *miic_port = phylink_pcs_to_miic_port(pcs);
190 struct miic *miic = miic_port->miic;
191 u32 speed, conv_mode, val, mask;
192 int port = miic_port->port;
195 case PHY_INTERFACE_MODE_RMII:
196 conv_mode = CONV_MODE_RMII;
197 speed = CONV_MODE_100MBPS;
199 case PHY_INTERFACE_MODE_RGMII:
200 case PHY_INTERFACE_MODE_RGMII_ID:
201 case PHY_INTERFACE_MODE_RGMII_TXID:
202 case PHY_INTERFACE_MODE_RGMII_RXID:
203 conv_mode = CONV_MODE_RGMII;
204 speed = CONV_MODE_1000MBPS;
206 case PHY_INTERFACE_MODE_MII:
207 conv_mode = CONV_MODE_MII;
208 /* When in MII mode, speed should be set to 0 (which is actually
211 speed = CONV_MODE_10MBPS;
217 val = FIELD_PREP(MIIC_CONVCTRL_CONV_MODE, conv_mode);
218 mask = MIIC_CONVCTRL_CONV_MODE;
220 /* Update speed only if we are going to change the interface because
221 * the link might already be up and it would break it if the speed is
224 if (interface != miic_port->interface) {
225 val |= FIELD_PREP(MIIC_CONVCTRL_CONV_SPEED, speed);
226 mask |= MIIC_CONVCTRL_CONV_SPEED;
227 miic_port->interface = interface;
230 miic_reg_rmw(miic, MIIC_CONVCTRL(port), mask, val);
231 miic_converter_enable(miic, miic_port->port, 1);
236 static void miic_link_up(struct phylink_pcs *pcs, unsigned int mode,
237 phy_interface_t interface, int speed, int duplex)
239 struct miic_port *miic_port = phylink_pcs_to_miic_port(pcs);
240 struct miic *miic = miic_port->miic;
241 u32 conv_speed = 0, val = 0;
242 int port = miic_port->port;
244 if (duplex == DUPLEX_FULL)
245 val |= MIIC_CONVCTRL_FULLD;
247 /* No speed in MII through-mode */
248 if (interface != PHY_INTERFACE_MODE_MII) {
251 conv_speed = CONV_MODE_1000MBPS;
254 conv_speed = CONV_MODE_100MBPS;
257 conv_speed = CONV_MODE_10MBPS;
264 val |= FIELD_PREP(MIIC_CONVCTRL_CONV_SPEED, conv_speed);
266 miic_reg_rmw(miic, MIIC_CONVCTRL(port),
267 (MIIC_CONVCTRL_CONV_SPEED | MIIC_CONVCTRL_FULLD), val);
270 static int miic_validate(struct phylink_pcs *pcs, unsigned long *supported,
271 const struct phylink_link_state *state)
273 if (phy_interface_mode_is_rgmii(state->interface) ||
274 state->interface == PHY_INTERFACE_MODE_RMII ||
275 state->interface == PHY_INTERFACE_MODE_MII)
281 static const struct phylink_pcs_ops miic_phylink_ops = {
282 .pcs_validate = miic_validate,
283 .pcs_config = miic_config,
284 .pcs_link_up = miic_link_up,
287 struct phylink_pcs *miic_create(struct device *dev, struct device_node *np)
289 struct platform_device *pdev;
290 struct miic_port *miic_port;
291 struct device_node *pcs_np;
295 if (!of_device_is_available(np))
296 return ERR_PTR(-ENODEV);
298 if (of_property_read_u32(np, "reg", &port))
299 return ERR_PTR(-EINVAL);
301 if (port > MIIC_MAX_NR_PORTS || port < 1)
302 return ERR_PTR(-EINVAL);
304 /* The PCS pdev is attached to the parent node */
305 pcs_np = of_get_parent(np);
307 return ERR_PTR(-ENODEV);
309 if (!of_device_is_available(pcs_np)) {
311 return ERR_PTR(-ENODEV);
314 pdev = of_find_device_by_node(pcs_np);
316 if (!pdev || !platform_get_drvdata(pdev))
317 return ERR_PTR(-EPROBE_DEFER);
319 miic_port = kzalloc(sizeof(*miic_port), GFP_KERNEL);
321 return ERR_PTR(-ENOMEM);
323 miic = platform_get_drvdata(pdev);
324 device_link_add(dev, miic->dev, DL_FLAG_AUTOREMOVE_CONSUMER);
326 miic_port->miic = miic;
327 miic_port->port = port - 1;
328 miic_port->pcs.ops = &miic_phylink_ops;
330 return &miic_port->pcs;
332 EXPORT_SYMBOL(miic_create);
334 void miic_destroy(struct phylink_pcs *pcs)
336 struct miic_port *miic_port = phylink_pcs_to_miic_port(pcs);
338 miic_converter_enable(miic_port->miic, miic_port->port, 0);
341 EXPORT_SYMBOL(miic_destroy);
343 static int miic_init_hw(struct miic *miic, u32 cfg_mode)
347 /* Unlock write access to accessory registers (cf datasheet). If this
348 * is going to be used in conjunction with the Cortex-M3, this sequence
349 * will have to be moved in register write
351 miic_reg_writel(miic, MIIC_PRCMD, 0x00A5);
352 miic_reg_writel(miic, MIIC_PRCMD, 0x0001);
353 miic_reg_writel(miic, MIIC_PRCMD, 0xFFFE);
354 miic_reg_writel(miic, MIIC_PRCMD, 0x0001);
356 miic_reg_writel(miic, MIIC_MODCTRL,
357 FIELD_PREP(MIIC_MODCTRL_SW_MODE, cfg_mode));
359 for (port = 0; port < MIIC_MAX_NR_PORTS; port++) {
360 miic_converter_enable(miic, port, 0);
361 /* Disable speed/duplex control from these registers, datasheet
362 * says switch registers should be used to setup switch port
365 miic_reg_writel(miic, MIIC_SWCTRL, 0x0);
366 miic_reg_writel(miic, MIIC_SWDUPC, 0x0);
372 static bool miic_modctrl_match(s8 table_val[MIIC_MODCTRL_CONF_CONV_NUM],
373 s8 dt_val[MIIC_MODCTRL_CONF_CONV_NUM])
377 for (i = 0; i < MIIC_MODCTRL_CONF_CONV_NUM; i++) {
378 if (dt_val[i] == MIIC_MODCTRL_CONF_NONE)
381 if (dt_val[i] != table_val[i])
388 static void miic_dump_conf(struct device *dev,
389 s8 conf[MIIC_MODCTRL_CONF_CONV_NUM])
391 const char *conf_name;
394 for (i = 0; i < MIIC_MODCTRL_CONF_CONV_NUM; i++) {
395 if (conf[i] != MIIC_MODCTRL_CONF_NONE)
396 conf_name = conf_to_string[conf[i]];
400 dev_err(dev, "%s: %s\n", index_to_string[i], conf_name);
404 static int miic_match_dt_conf(struct device *dev,
405 s8 dt_val[MIIC_MODCTRL_CONF_CONV_NUM],
408 struct modctrl_match *table_entry;
411 for (i = 0; i < ARRAY_SIZE(modctrl_match_table); i++) {
412 table_entry = &modctrl_match_table[i];
414 if (miic_modctrl_match(table_entry->conv, dt_val)) {
415 *mode_cfg = table_entry->mode_cfg;
420 dev_err(dev, "Failed to apply requested configuration\n");
421 miic_dump_conf(dev, dt_val);
426 static int miic_parse_dt(struct device *dev, u32 *mode_cfg)
428 s8 dt_val[MIIC_MODCTRL_CONF_CONV_NUM];
429 struct device_node *np = dev->of_node;
430 struct device_node *conv;
434 memset(dt_val, MIIC_MODCTRL_CONF_NONE, sizeof(dt_val));
436 if (of_property_read_u32(np, "renesas,miic-switch-portin", &conf) == 0)
439 for_each_child_of_node(np, conv) {
440 if (of_property_read_u32(conv, "reg", &port))
443 if (!of_device_is_available(conv))
446 if (of_property_read_u32(conv, "renesas,miic-input", &conf) == 0)
450 return miic_match_dt_conf(dev, dt_val, mode_cfg);
453 static int miic_probe(struct platform_device *pdev)
455 struct device *dev = &pdev->dev;
460 ret = miic_parse_dt(dev, &mode_cfg);
464 miic = devm_kzalloc(dev, sizeof(*miic), GFP_KERNEL);
468 spin_lock_init(&miic->lock);
470 miic->base = devm_platform_ioremap_resource(pdev, 0);
471 if (IS_ERR(miic->base))
472 return PTR_ERR(miic->base);
474 ret = devm_pm_runtime_enable(dev);
478 ret = pm_runtime_resume_and_get(dev);
482 ret = miic_init_hw(miic, mode_cfg);
484 goto disable_runtime_pm;
486 /* miic_create() relies on that fact that data are attached to the
487 * platform device to determine if the driver is ready so this needs to
488 * be the last thing to be done after everything is initialized
491 platform_set_drvdata(pdev, miic);
501 static int miic_remove(struct platform_device *pdev)
503 pm_runtime_put(&pdev->dev);
508 static const struct of_device_id miic_of_mtable[] = {
509 { .compatible = "renesas,rzn1-miic" },
512 MODULE_DEVICE_TABLE(of, miic_of_mtable);
514 static struct platform_driver miic_driver = {
517 .suppress_bind_attrs = true,
518 .of_match_table = miic_of_mtable,
521 .remove = miic_remove,
523 module_platform_driver(miic_driver);
525 MODULE_LICENSE("GPL");
526 MODULE_DESCRIPTION("Renesas MII converter PCS driver");
527 MODULE_AUTHOR("Clément Léger <clement.leger@bootlin.com>");