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/of_address.h>
29 #define ZMII_FER_MDI(idx) (0x80000000 >> ((idx) * 4))
30 #define ZMII_FER_MDI_ALL (ZMII_FER_MDI(0) | ZMII_FER_MDI(1) | \
31 ZMII_FER_MDI(2) | ZMII_FER_MDI(3))
33 #define ZMII_FER_SMII(idx) (0x40000000 >> ((idx) * 4))
34 #define ZMII_FER_RMII(idx) (0x20000000 >> ((idx) * 4))
35 #define ZMII_FER_MII(idx) (0x10000000 >> ((idx) * 4))
38 #define ZMII_SSR_SCI(idx) (0x40000000 >> ((idx) * 4))
39 #define ZMII_SSR_FSS(idx) (0x20000000 >> ((idx) * 4))
40 #define ZMII_SSR_SP(idx) (0x10000000 >> ((idx) * 4))
42 /* ZMII only supports MII, RMII and SMII
43 * we also support autodetection for backward compatibility
45 static inline int zmii_valid_mode(int mode
)
47 return mode
== PHY_INTERFACE_MODE_MII
||
48 mode
== PHY_INTERFACE_MODE_RMII
||
49 mode
== PHY_INTERFACE_MODE_SMII
||
50 mode
== PHY_INTERFACE_MODE_NA
;
53 static inline const char *zmii_mode_name(int mode
)
56 case PHY_INTERFACE_MODE_MII
:
58 case PHY_INTERFACE_MODE_RMII
:
60 case PHY_INTERFACE_MODE_SMII
:
67 static inline u32
zmii_mode_mask(int mode
, int input
)
70 case PHY_INTERFACE_MODE_MII
:
71 return ZMII_FER_MII(input
);
72 case PHY_INTERFACE_MODE_RMII
:
73 return ZMII_FER_RMII(input
);
74 case PHY_INTERFACE_MODE_SMII
:
75 return ZMII_FER_SMII(input
);
81 int zmii_attach(struct platform_device
*ofdev
, int input
,
82 phy_interface_t
*mode
)
84 struct zmii_instance
*dev
= platform_get_drvdata(ofdev
);
85 struct zmii_regs __iomem
*p
= dev
->base
;
87 ZMII_DBG(dev
, "init(%d, %d)" NL
, input
, *mode
);
89 if (!zmii_valid_mode(*mode
)) {
90 /* Probably an EMAC connected to RGMII,
91 * but it still may need ZMII for MDIO so
98 mutex_lock(&dev
->lock
);
100 /* Autodetect ZMII mode if not specified.
101 * This is only for backward compatibility with the old driver.
102 * Please, always specify PHY mode in your board port to avoid
105 if (dev
->mode
== PHY_INTERFACE_MODE_NA
) {
106 if (*mode
== PHY_INTERFACE_MODE_NA
) {
107 u32 r
= dev
->fer_save
;
109 ZMII_DBG(dev
, "autodetecting mode, FER = 0x%08x" NL
, r
);
111 if (r
& (ZMII_FER_MII(0) | ZMII_FER_MII(1)))
112 dev
->mode
= PHY_INTERFACE_MODE_MII
;
113 else if (r
& (ZMII_FER_RMII(0) | ZMII_FER_RMII(1)))
114 dev
->mode
= PHY_INTERFACE_MODE_RMII
;
116 dev
->mode
= PHY_INTERFACE_MODE_SMII
;
120 printk(KERN_NOTICE
"%pOF: bridge in %s mode\n",
122 zmii_mode_name(dev
->mode
));
124 /* All inputs must use the same mode */
125 if (*mode
!= PHY_INTERFACE_MODE_NA
&& *mode
!= dev
->mode
) {
127 "%pOF: invalid mode %d specified for input %d\n",
128 ofdev
->dev
.of_node
, *mode
, input
);
129 mutex_unlock(&dev
->lock
);
134 /* Report back correct PHY mode,
135 * it may be used during PHY initialization.
139 /* Enable this input */
140 out_be32(&p
->fer
, in_be32(&p
->fer
) | zmii_mode_mask(dev
->mode
, input
));
143 mutex_unlock(&dev
->lock
);
148 void zmii_get_mdio(struct platform_device
*ofdev
, int input
)
150 struct zmii_instance
*dev
= platform_get_drvdata(ofdev
);
153 ZMII_DBG2(dev
, "get_mdio(%d)" NL
, input
);
155 mutex_lock(&dev
->lock
);
157 fer
= in_be32(&dev
->base
->fer
) & ~ZMII_FER_MDI_ALL
;
158 out_be32(&dev
->base
->fer
, fer
| ZMII_FER_MDI(input
));
161 void zmii_put_mdio(struct platform_device
*ofdev
, int input
)
163 struct zmii_instance
*dev
= platform_get_drvdata(ofdev
);
165 ZMII_DBG2(dev
, "put_mdio(%d)" NL
, input
);
166 mutex_unlock(&dev
->lock
);
170 void zmii_set_speed(struct platform_device
*ofdev
, int input
, int speed
)
172 struct zmii_instance
*dev
= platform_get_drvdata(ofdev
);
175 mutex_lock(&dev
->lock
);
177 ssr
= in_be32(&dev
->base
->ssr
);
179 ZMII_DBG(dev
, "speed(%d, %d)" NL
, input
, speed
);
181 if (speed
== SPEED_100
)
182 ssr
|= ZMII_SSR_SP(input
);
184 ssr
&= ~ZMII_SSR_SP(input
);
186 out_be32(&dev
->base
->ssr
, ssr
);
188 mutex_unlock(&dev
->lock
);
191 void zmii_detach(struct platform_device
*ofdev
, int input
)
193 struct zmii_instance
*dev
= platform_get_drvdata(ofdev
);
195 BUG_ON(!dev
|| dev
->users
== 0);
197 mutex_lock(&dev
->lock
);
199 ZMII_DBG(dev
, "detach(%d)" NL
, input
);
201 /* Disable this input */
202 out_be32(&dev
->base
->fer
,
203 in_be32(&dev
->base
->fer
) & ~zmii_mode_mask(dev
->mode
, input
));
207 mutex_unlock(&dev
->lock
);
210 int zmii_get_regs_len(struct platform_device
*ofdev
)
212 return sizeof(struct emac_ethtool_regs_subhdr
) +
213 sizeof(struct zmii_regs
);
216 void *zmii_dump_regs(struct platform_device
*ofdev
, void *buf
)
218 struct zmii_instance
*dev
= platform_get_drvdata(ofdev
);
219 struct emac_ethtool_regs_subhdr
*hdr
= buf
;
220 struct zmii_regs
*regs
= (struct zmii_regs
*)(hdr
+ 1);
223 hdr
->index
= 0; /* for now, are there chips with more than one
224 * zmii ? if yes, then we'll add a cell_index
225 * like we do for emac
227 memcpy_fromio(regs
, dev
->base
, sizeof(struct zmii_regs
));
231 static int zmii_probe(struct platform_device
*ofdev
)
233 struct device_node
*np
= ofdev
->dev
.of_node
;
234 struct zmii_instance
*dev
;
235 struct resource regs
;
239 dev
= kzalloc(sizeof(struct zmii_instance
), GFP_KERNEL
);
243 mutex_init(&dev
->lock
);
245 dev
->mode
= PHY_INTERFACE_MODE_NA
;
248 if (of_address_to_resource(np
, 0, ®s
)) {
249 printk(KERN_ERR
"%pOF: Can't get registers address\n", np
);
254 dev
->base
= (struct zmii_regs __iomem
*)ioremap(regs
.start
,
255 sizeof(struct zmii_regs
));
256 if (dev
->base
== NULL
) {
257 printk(KERN_ERR
"%pOF: Can't map device registers!\n", np
);
261 /* We may need FER value for autodetection later */
262 dev
->fer_save
= in_be32(&dev
->base
->fer
);
264 /* Disable all inputs by default */
265 out_be32(&dev
->base
->fer
, 0);
267 printk(KERN_INFO
"ZMII %pOF initialized\n", ofdev
->dev
.of_node
);
269 platform_set_drvdata(ofdev
, dev
);
279 static int zmii_remove(struct platform_device
*ofdev
)
281 struct zmii_instance
*dev
= platform_get_drvdata(ofdev
);
283 WARN_ON(dev
->users
!= 0);
291 static const struct of_device_id zmii_match
[] =
294 .compatible
= "ibm,zmii",
296 /* For backward compat with old DT */
303 static struct platform_driver zmii_driver
= {
306 .of_match_table
= zmii_match
,
309 .remove
= zmii_remove
,
312 int __init
zmii_init(void)
314 return platform_driver_register(&zmii_driver
);
319 platform_driver_unregister(&zmii_driver
);