1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * drivers/net/ethernet/ibm/emac/zmii.c
5 * Driver for PowerPC 4xx on-chip ethernet controller, ZMII bridge support.
7 * Copyright 2007 Benjamin Herrenschmidt, IBM Corp.
8 * <benh@kernel.crashing.org>
10 * Based on the arch/ppc version of the driver:
12 * Copyright (c) 2004, 2005 Zultys Technologies.
13 * Eugene Surovegin <eugene.surovegin@zultys.com> or <ebs@ebshome.net>
15 * Based on original work by
16 * Armin Kuster <akuster@mvista.com>
17 * Copyright 2001 MontaVista Softare Inc.
19 #include <linux/slab.h>
20 #include <linux/kernel.h>
21 #include <linux/ethtool.h>
22 #include <linux/mod_devicetable.h>
23 #include <linux/of_address.h>
24 #include <linux/platform_device.h>
31 #define ZMII_FER_MDI(idx) (0x80000000 >> ((idx) * 4))
32 #define ZMII_FER_MDI_ALL (ZMII_FER_MDI(0) | ZMII_FER_MDI(1) | \
33 ZMII_FER_MDI(2) | ZMII_FER_MDI(3))
35 #define ZMII_FER_SMII(idx) (0x40000000 >> ((idx) * 4))
36 #define ZMII_FER_RMII(idx) (0x20000000 >> ((idx) * 4))
37 #define ZMII_FER_MII(idx) (0x10000000 >> ((idx) * 4))
40 #define ZMII_SSR_SCI(idx) (0x40000000 >> ((idx) * 4))
41 #define ZMII_SSR_FSS(idx) (0x20000000 >> ((idx) * 4))
42 #define ZMII_SSR_SP(idx) (0x10000000 >> ((idx) * 4))
44 /* ZMII only supports MII, RMII and SMII
45 * we also support autodetection for backward compatibility
47 static inline int zmii_valid_mode(int mode
)
49 return mode
== PHY_INTERFACE_MODE_MII
||
50 mode
== PHY_INTERFACE_MODE_RMII
||
51 mode
== PHY_INTERFACE_MODE_SMII
||
52 mode
== PHY_INTERFACE_MODE_NA
;
55 static inline const char *zmii_mode_name(int mode
)
58 case PHY_INTERFACE_MODE_MII
:
60 case PHY_INTERFACE_MODE_RMII
:
62 case PHY_INTERFACE_MODE_SMII
:
69 static inline u32
zmii_mode_mask(int mode
, int input
)
72 case PHY_INTERFACE_MODE_MII
:
73 return ZMII_FER_MII(input
);
74 case PHY_INTERFACE_MODE_RMII
:
75 return ZMII_FER_RMII(input
);
76 case PHY_INTERFACE_MODE_SMII
:
77 return ZMII_FER_SMII(input
);
83 int zmii_attach(struct platform_device
*ofdev
, int input
,
84 phy_interface_t
*mode
)
86 struct zmii_instance
*dev
= platform_get_drvdata(ofdev
);
87 struct zmii_regs __iomem
*p
= dev
->base
;
89 ZMII_DBG(dev
, "init(%d, %d)" NL
, input
, *mode
);
91 if (!zmii_valid_mode(*mode
)) {
92 /* Probably an EMAC connected to RGMII,
93 * but it still may need ZMII for MDIO so
100 mutex_lock(&dev
->lock
);
102 /* Autodetect ZMII mode if not specified.
103 * This is only for backward compatibility with the old driver.
104 * Please, always specify PHY mode in your board port to avoid
107 if (dev
->mode
== PHY_INTERFACE_MODE_NA
) {
108 if (*mode
== PHY_INTERFACE_MODE_NA
) {
109 u32 r
= dev
->fer_save
;
111 ZMII_DBG(dev
, "autodetecting mode, FER = 0x%08x" NL
, r
);
113 if (r
& (ZMII_FER_MII(0) | ZMII_FER_MII(1)))
114 dev
->mode
= PHY_INTERFACE_MODE_MII
;
115 else if (r
& (ZMII_FER_RMII(0) | ZMII_FER_RMII(1)))
116 dev
->mode
= PHY_INTERFACE_MODE_RMII
;
118 dev
->mode
= PHY_INTERFACE_MODE_SMII
;
122 printk(KERN_NOTICE
"%pOF: bridge in %s mode\n",
124 zmii_mode_name(dev
->mode
));
126 /* All inputs must use the same mode */
127 if (*mode
!= PHY_INTERFACE_MODE_NA
&& *mode
!= dev
->mode
) {
129 "%pOF: invalid mode %d specified for input %d\n",
130 ofdev
->dev
.of_node
, *mode
, input
);
131 mutex_unlock(&dev
->lock
);
136 /* Report back correct PHY mode,
137 * it may be used during PHY initialization.
141 /* Enable this input */
142 out_be32(&p
->fer
, in_be32(&p
->fer
) | zmii_mode_mask(dev
->mode
, input
));
145 mutex_unlock(&dev
->lock
);
150 void zmii_get_mdio(struct platform_device
*ofdev
, int input
)
152 struct zmii_instance
*dev
= platform_get_drvdata(ofdev
);
155 ZMII_DBG2(dev
, "get_mdio(%d)" NL
, input
);
157 mutex_lock(&dev
->lock
);
159 fer
= in_be32(&dev
->base
->fer
) & ~ZMII_FER_MDI_ALL
;
160 out_be32(&dev
->base
->fer
, fer
| ZMII_FER_MDI(input
));
163 void zmii_put_mdio(struct platform_device
*ofdev
, int input
)
165 struct zmii_instance
*dev
= platform_get_drvdata(ofdev
);
167 ZMII_DBG2(dev
, "put_mdio(%d)" NL
, input
);
168 mutex_unlock(&dev
->lock
);
172 void zmii_set_speed(struct platform_device
*ofdev
, int input
, int speed
)
174 struct zmii_instance
*dev
= platform_get_drvdata(ofdev
);
177 mutex_lock(&dev
->lock
);
179 ssr
= in_be32(&dev
->base
->ssr
);
181 ZMII_DBG(dev
, "speed(%d, %d)" NL
, input
, speed
);
183 if (speed
== SPEED_100
)
184 ssr
|= ZMII_SSR_SP(input
);
186 ssr
&= ~ZMII_SSR_SP(input
);
188 out_be32(&dev
->base
->ssr
, ssr
);
190 mutex_unlock(&dev
->lock
);
193 void zmii_detach(struct platform_device
*ofdev
, int input
)
195 struct zmii_instance
*dev
= platform_get_drvdata(ofdev
);
197 BUG_ON(!dev
|| dev
->users
== 0);
199 mutex_lock(&dev
->lock
);
201 ZMII_DBG(dev
, "detach(%d)" NL
, input
);
203 /* Disable this input */
204 out_be32(&dev
->base
->fer
,
205 in_be32(&dev
->base
->fer
) & ~zmii_mode_mask(dev
->mode
, input
));
209 mutex_unlock(&dev
->lock
);
212 int zmii_get_regs_len(struct platform_device
*ofdev
)
214 return sizeof(struct emac_ethtool_regs_subhdr
) +
215 sizeof(struct zmii_regs
);
218 void *zmii_dump_regs(struct platform_device
*ofdev
, void *buf
)
220 struct zmii_instance
*dev
= platform_get_drvdata(ofdev
);
221 struct emac_ethtool_regs_subhdr
*hdr
= buf
;
222 struct zmii_regs
*regs
= (struct zmii_regs
*)(hdr
+ 1);
225 hdr
->index
= 0; /* for now, are there chips with more than one
226 * zmii ? if yes, then we'll add a cell_index
227 * like we do for emac
229 memcpy_fromio(regs
, dev
->base
, sizeof(struct zmii_regs
));
233 static int zmii_probe(struct platform_device
*ofdev
)
235 struct zmii_instance
*dev
;
238 dev
= devm_kzalloc(&ofdev
->dev
, sizeof(struct zmii_instance
),
243 err
= devm_mutex_init(&ofdev
->dev
, &dev
->lock
);
248 dev
->mode
= PHY_INTERFACE_MODE_NA
;
250 dev
->base
= devm_platform_ioremap_resource(ofdev
, 0);
251 if (IS_ERR(dev
->base
)) {
252 dev_err(&ofdev
->dev
, "can't map device registers");
253 return PTR_ERR(dev
->base
);
256 /* We may need FER value for autodetection later */
257 dev
->fer_save
= in_be32(&dev
->base
->fer
);
259 /* Disable all inputs by default */
260 out_be32(&dev
->base
->fer
, 0);
262 printk(KERN_INFO
"ZMII %pOF initialized\n", ofdev
->dev
.of_node
);
264 platform_set_drvdata(ofdev
, dev
);
269 static const struct of_device_id zmii_match
[] =
272 .compatible
= "ibm,zmii",
274 /* For backward compat with old DT */
281 static struct platform_driver zmii_driver
= {
284 .of_match_table
= zmii_match
,
289 int __init
zmii_init(void)
291 return platform_driver_register(&zmii_driver
);
296 platform_driver_unregister(&zmii_driver
);