xref: /openbmc/linux/drivers/net/ethernet/ibm/emac/zmii.c (revision fb8d6c8d)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * drivers/net/ethernet/ibm/emac/zmii.c
4  *
5  * Driver for PowerPC 4xx on-chip ethernet controller, ZMII bridge support.
6  *
7  * Copyright 2007 Benjamin Herrenschmidt, IBM Corp.
8  *                <benh@kernel.crashing.org>
9  *
10  * Based on the arch/ppc version of the driver:
11  *
12  * Copyright (c) 2004, 2005 Zultys Technologies.
13  * Eugene Surovegin <eugene.surovegin@zultys.com> or <ebs@ebshome.net>
14  *
15  * Based on original work by
16  *      Armin Kuster <akuster@mvista.com>
17  * 	Copyright 2001 MontaVista Softare Inc.
18  */
19 #include <linux/slab.h>
20 #include <linux/kernel.h>
21 #include <linux/ethtool.h>
22 #include <linux/of_address.h>
23 #include <asm/io.h>
24 
25 #include "emac.h"
26 #include "core.h"
27 
28 /* ZMIIx_FER */
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))
32 
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))
36 
37 /* ZMIIx_SSR */
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))
41 
42 /* ZMII only supports MII, RMII and SMII
43  * we also support autodetection for backward compatibility
44  */
45 static inline int zmii_valid_mode(int mode)
46 {
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;
51 }
52 
53 static inline const char *zmii_mode_name(int mode)
54 {
55 	switch (mode) {
56 	case PHY_INTERFACE_MODE_MII:
57 		return "MII";
58 	case PHY_INTERFACE_MODE_RMII:
59 		return "RMII";
60 	case PHY_INTERFACE_MODE_SMII:
61 		return "SMII";
62 	default:
63 		BUG();
64 	}
65 }
66 
67 static inline u32 zmii_mode_mask(int mode, int input)
68 {
69 	switch (mode) {
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);
76 	default:
77 		return 0;
78 	}
79 }
80 
81 int zmii_attach(struct platform_device *ofdev, int input, int *mode)
82 {
83 	struct zmii_instance *dev = platform_get_drvdata(ofdev);
84 	struct zmii_regs __iomem *p = dev->base;
85 
86 	ZMII_DBG(dev, "init(%d, %d)" NL, input, *mode);
87 
88 	if (!zmii_valid_mode(*mode)) {
89 		/* Probably an EMAC connected to RGMII,
90 		 * but it still may need ZMII for MDIO so
91 		 * we don't fail here.
92 		 */
93 		dev->users++;
94 		return 0;
95 	}
96 
97 	mutex_lock(&dev->lock);
98 
99 	/* Autodetect ZMII mode if not specified.
100 	 * This is only for backward compatibility with the old driver.
101 	 * Please, always specify PHY mode in your board port to avoid
102 	 * any surprises.
103 	 */
104 	if (dev->mode == PHY_INTERFACE_MODE_NA) {
105 		if (*mode == PHY_INTERFACE_MODE_NA) {
106 			u32 r = dev->fer_save;
107 
108 			ZMII_DBG(dev, "autodetecting mode, FER = 0x%08x" NL, r);
109 
110 			if (r & (ZMII_FER_MII(0) | ZMII_FER_MII(1)))
111 				dev->mode = PHY_INTERFACE_MODE_MII;
112 			else if (r & (ZMII_FER_RMII(0) | ZMII_FER_RMII(1)))
113 				dev->mode = PHY_INTERFACE_MODE_RMII;
114 			else
115 				dev->mode = PHY_INTERFACE_MODE_SMII;
116 		} else {
117 			dev->mode = *mode;
118 		}
119 		printk(KERN_NOTICE "%pOF: bridge in %s mode\n",
120 		       ofdev->dev.of_node,
121 		       zmii_mode_name(dev->mode));
122 	} else {
123 		/* All inputs must use the same mode */
124 		if (*mode != PHY_INTERFACE_MODE_NA && *mode != dev->mode) {
125 			printk(KERN_ERR
126 			       "%pOF: invalid mode %d specified for input %d\n",
127 			       ofdev->dev.of_node, *mode, input);
128 			mutex_unlock(&dev->lock);
129 			return -EINVAL;
130 		}
131 	}
132 
133 	/* Report back correct PHY mode,
134 	 * it may be used during PHY initialization.
135 	 */
136 	*mode = dev->mode;
137 
138 	/* Enable this input */
139 	out_be32(&p->fer, in_be32(&p->fer) | zmii_mode_mask(dev->mode, input));
140 	++dev->users;
141 
142 	mutex_unlock(&dev->lock);
143 
144 	return 0;
145 }
146 
147 void zmii_get_mdio(struct platform_device *ofdev, int input)
148 {
149 	struct zmii_instance *dev = platform_get_drvdata(ofdev);
150 	u32 fer;
151 
152 	ZMII_DBG2(dev, "get_mdio(%d)" NL, input);
153 
154 	mutex_lock(&dev->lock);
155 
156 	fer = in_be32(&dev->base->fer) & ~ZMII_FER_MDI_ALL;
157 	out_be32(&dev->base->fer, fer | ZMII_FER_MDI(input));
158 }
159 
160 void zmii_put_mdio(struct platform_device *ofdev, int input)
161 {
162 	struct zmii_instance *dev = platform_get_drvdata(ofdev);
163 
164 	ZMII_DBG2(dev, "put_mdio(%d)" NL, input);
165 	mutex_unlock(&dev->lock);
166 }
167 
168 
169 void zmii_set_speed(struct platform_device *ofdev, int input, int speed)
170 {
171 	struct zmii_instance *dev = platform_get_drvdata(ofdev);
172 	u32 ssr;
173 
174 	mutex_lock(&dev->lock);
175 
176 	ssr = in_be32(&dev->base->ssr);
177 
178 	ZMII_DBG(dev, "speed(%d, %d)" NL, input, speed);
179 
180 	if (speed == SPEED_100)
181 		ssr |= ZMII_SSR_SP(input);
182 	else
183 		ssr &= ~ZMII_SSR_SP(input);
184 
185 	out_be32(&dev->base->ssr, ssr);
186 
187 	mutex_unlock(&dev->lock);
188 }
189 
190 void zmii_detach(struct platform_device *ofdev, int input)
191 {
192 	struct zmii_instance *dev = platform_get_drvdata(ofdev);
193 
194 	BUG_ON(!dev || dev->users == 0);
195 
196 	mutex_lock(&dev->lock);
197 
198 	ZMII_DBG(dev, "detach(%d)" NL, input);
199 
200 	/* Disable this input */
201 	out_be32(&dev->base->fer,
202 		 in_be32(&dev->base->fer) & ~zmii_mode_mask(dev->mode, input));
203 
204 	--dev->users;
205 
206 	mutex_unlock(&dev->lock);
207 }
208 
209 int zmii_get_regs_len(struct platform_device *ofdev)
210 {
211 	return sizeof(struct emac_ethtool_regs_subhdr) +
212 		sizeof(struct zmii_regs);
213 }
214 
215 void *zmii_dump_regs(struct platform_device *ofdev, void *buf)
216 {
217 	struct zmii_instance *dev = platform_get_drvdata(ofdev);
218 	struct emac_ethtool_regs_subhdr *hdr = buf;
219 	struct zmii_regs *regs = (struct zmii_regs *)(hdr + 1);
220 
221 	hdr->version = 0;
222 	hdr->index = 0; /* for now, are there chips with more than one
223 			 * zmii ? if yes, then we'll add a cell_index
224 			 * like we do for emac
225 			 */
226 	memcpy_fromio(regs, dev->base, sizeof(struct zmii_regs));
227 	return regs + 1;
228 }
229 
230 static int zmii_probe(struct platform_device *ofdev)
231 {
232 	struct device_node *np = ofdev->dev.of_node;
233 	struct zmii_instance *dev;
234 	struct resource regs;
235 	int rc;
236 
237 	rc = -ENOMEM;
238 	dev = kzalloc(sizeof(struct zmii_instance), GFP_KERNEL);
239 	if (dev == NULL)
240 		goto err_gone;
241 
242 	mutex_init(&dev->lock);
243 	dev->ofdev = ofdev;
244 	dev->mode = PHY_INTERFACE_MODE_NA;
245 
246 	rc = -ENXIO;
247 	if (of_address_to_resource(np, 0, &regs)) {
248 		printk(KERN_ERR "%pOF: Can't get registers address\n", np);
249 		goto err_free;
250 	}
251 
252 	rc = -ENOMEM;
253 	dev->base = (struct zmii_regs __iomem *)ioremap(regs.start,
254 						sizeof(struct zmii_regs));
255 	if (dev->base == NULL) {
256 		printk(KERN_ERR "%pOF: Can't map device registers!\n", np);
257 		goto err_free;
258 	}
259 
260 	/* We may need FER value for autodetection later */
261 	dev->fer_save = in_be32(&dev->base->fer);
262 
263 	/* Disable all inputs by default */
264 	out_be32(&dev->base->fer, 0);
265 
266 	printk(KERN_INFO "ZMII %pOF initialized\n", ofdev->dev.of_node);
267 	wmb();
268 	platform_set_drvdata(ofdev, dev);
269 
270 	return 0;
271 
272  err_free:
273 	kfree(dev);
274  err_gone:
275 	return rc;
276 }
277 
278 static int zmii_remove(struct platform_device *ofdev)
279 {
280 	struct zmii_instance *dev = platform_get_drvdata(ofdev);
281 
282 	WARN_ON(dev->users != 0);
283 
284 	iounmap(dev->base);
285 	kfree(dev);
286 
287 	return 0;
288 }
289 
290 static const struct of_device_id zmii_match[] =
291 {
292 	{
293 		.compatible	= "ibm,zmii",
294 	},
295 	/* For backward compat with old DT */
296 	{
297 		.type		= "emac-zmii",
298 	},
299 	{},
300 };
301 
302 static struct platform_driver zmii_driver = {
303 	.driver = {
304 		.name = "emac-zmii",
305 		.of_match_table = zmii_match,
306 	},
307 	.probe = zmii_probe,
308 	.remove = zmii_remove,
309 };
310 
311 int __init zmii_init(void)
312 {
313 	return platform_driver_register(&zmii_driver);
314 }
315 
316 void zmii_exit(void)
317 {
318 	platform_driver_unregister(&zmii_driver);
319 }
320