xref: /openbmc/linux/drivers/net/ethernet/ibm/emac/rgmii.c (revision 455f9726)
1 /*
2  * drivers/net/ethernet/ibm/emac/rgmii.c
3  *
4  * Driver for PowerPC 4xx on-chip ethernet controller, RGMII bridge support.
5  *
6  * Copyright 2007 Benjamin Herrenschmidt, IBM Corp.
7  *                <benh@kernel.crashing.org>
8  *
9  * Based on the arch/ppc version of the driver:
10  *
11  * Copyright (c) 2004, 2005 Zultys Technologies.
12  * Eugene Surovegin <eugene.surovegin@zultys.com> or <ebs@ebshome.net>
13  *
14  * Based on original work by
15  * 	Matt Porter <mporter@kernel.crashing.org>
16  * 	Copyright 2004 MontaVista Software, Inc.
17  *
18  * This program is free software; you can redistribute  it and/or modify it
19  * under  the terms of  the GNU General  Public License as published by the
20  * Free Software Foundation;  either version 2 of the  License, or (at your
21  * option) any later version.
22  *
23  */
24 #include <linux/slab.h>
25 #include <linux/kernel.h>
26 #include <linux/ethtool.h>
27 #include <linux/of_address.h>
28 #include <asm/io.h>
29 
30 #include "emac.h"
31 #include "debug.h"
32 
33 // XXX FIXME: Axon seems to support a subset of the RGMII, we
34 // thus need to take that into account and possibly change some
35 // of the bit settings below that don't seem to quite match the
36 // AXON spec
37 
38 /* RGMIIx_FER */
39 #define RGMII_FER_MASK(idx)	(0x7 << ((idx) * 4))
40 #define RGMII_FER_RTBI(idx)	(0x4 << ((idx) * 4))
41 #define RGMII_FER_RGMII(idx)	(0x5 << ((idx) * 4))
42 #define RGMII_FER_TBI(idx)	(0x6 << ((idx) * 4))
43 #define RGMII_FER_GMII(idx)	(0x7 << ((idx) * 4))
44 #define RGMII_FER_MII(idx)	RGMII_FER_GMII(idx)
45 
46 /* RGMIIx_SSR */
47 #define RGMII_SSR_MASK(idx)	(0x7 << ((idx) * 8))
48 #define RGMII_SSR_10(idx)	(0x1 << ((idx) * 8))
49 #define RGMII_SSR_100(idx)	(0x2 << ((idx) * 8))
50 #define RGMII_SSR_1000(idx)	(0x4 << ((idx) * 8))
51 
52 /* RGMII bridge supports only GMII/TBI and RGMII/RTBI PHYs */
53 static inline int rgmii_valid_mode(int phy_mode)
54 {
55 	return  phy_mode == PHY_MODE_GMII ||
56 		phy_mode == PHY_MODE_MII ||
57 		phy_mode == PHY_MODE_RGMII ||
58 		phy_mode == PHY_MODE_TBI ||
59 		phy_mode == PHY_MODE_RTBI;
60 }
61 
62 static inline const char *rgmii_mode_name(int mode)
63 {
64 	switch (mode) {
65 	case PHY_MODE_RGMII:
66 		return "RGMII";
67 	case PHY_MODE_TBI:
68 		return "TBI";
69 	case PHY_MODE_GMII:
70 		return "GMII";
71 	case PHY_MODE_MII:
72 		return "MII";
73 	case PHY_MODE_RTBI:
74 		return "RTBI";
75 	default:
76 		BUG();
77 	}
78 }
79 
80 static inline u32 rgmii_mode_mask(int mode, int input)
81 {
82 	switch (mode) {
83 	case PHY_MODE_RGMII:
84 		return RGMII_FER_RGMII(input);
85 	case PHY_MODE_TBI:
86 		return RGMII_FER_TBI(input);
87 	case PHY_MODE_GMII:
88 		return RGMII_FER_GMII(input);
89 	case PHY_MODE_MII:
90 		return RGMII_FER_MII(input);
91 	case PHY_MODE_RTBI:
92 		return RGMII_FER_RTBI(input);
93 	default:
94 		BUG();
95 	}
96 }
97 
98 int rgmii_attach(struct platform_device *ofdev, int input, int mode)
99 {
100 	struct rgmii_instance *dev = platform_get_drvdata(ofdev);
101 	struct rgmii_regs __iomem *p = dev->base;
102 
103 	RGMII_DBG(dev, "attach(%d)" NL, input);
104 
105 	/* Check if we need to attach to a RGMII */
106 	if (input < 0 || !rgmii_valid_mode(mode)) {
107 		printk(KERN_ERR "%s: unsupported settings !\n",
108 		       ofdev->dev.of_node->full_name);
109 		return -ENODEV;
110 	}
111 
112 	mutex_lock(&dev->lock);
113 
114 	/* Enable this input */
115 	out_be32(&p->fer, in_be32(&p->fer) | rgmii_mode_mask(mode, input));
116 
117 	printk(KERN_NOTICE "%s: input %d in %s mode\n",
118 	       ofdev->dev.of_node->full_name, input, rgmii_mode_name(mode));
119 
120 	++dev->users;
121 
122 	mutex_unlock(&dev->lock);
123 
124 	return 0;
125 }
126 
127 void rgmii_set_speed(struct platform_device *ofdev, int input, int speed)
128 {
129 	struct rgmii_instance *dev = platform_get_drvdata(ofdev);
130 	struct rgmii_regs __iomem *p = dev->base;
131 	u32 ssr;
132 
133 	mutex_lock(&dev->lock);
134 
135 	ssr = in_be32(&p->ssr) & ~RGMII_SSR_MASK(input);
136 
137 	RGMII_DBG(dev, "speed(%d, %d)" NL, input, speed);
138 
139 	if (speed == SPEED_1000)
140 		ssr |= RGMII_SSR_1000(input);
141 	else if (speed == SPEED_100)
142 		ssr |= RGMII_SSR_100(input);
143 	else if (speed == SPEED_10)
144 		ssr |= RGMII_SSR_10(input);
145 
146 	out_be32(&p->ssr, ssr);
147 
148 	mutex_unlock(&dev->lock);
149 }
150 
151 void rgmii_get_mdio(struct platform_device *ofdev, int input)
152 {
153 	struct rgmii_instance *dev = platform_get_drvdata(ofdev);
154 	struct rgmii_regs __iomem *p = dev->base;
155 	u32 fer;
156 
157 	RGMII_DBG2(dev, "get_mdio(%d)" NL, input);
158 
159 	if (!(dev->flags & EMAC_RGMII_FLAG_HAS_MDIO))
160 		return;
161 
162 	mutex_lock(&dev->lock);
163 
164 	fer = in_be32(&p->fer);
165 	fer |= 0x00080000u >> input;
166 	out_be32(&p->fer, fer);
167 	(void)in_be32(&p->fer);
168 
169 	DBG2(dev, " fer = 0x%08x\n", fer);
170 }
171 
172 void rgmii_put_mdio(struct platform_device *ofdev, int input)
173 {
174 	struct rgmii_instance *dev = platform_get_drvdata(ofdev);
175 	struct rgmii_regs __iomem *p = dev->base;
176 	u32 fer;
177 
178 	RGMII_DBG2(dev, "put_mdio(%d)" NL, input);
179 
180 	if (!(dev->flags & EMAC_RGMII_FLAG_HAS_MDIO))
181 		return;
182 
183 	fer = in_be32(&p->fer);
184 	fer &= ~(0x00080000u >> input);
185 	out_be32(&p->fer, fer);
186 	(void)in_be32(&p->fer);
187 
188 	DBG2(dev, " fer = 0x%08x\n", fer);
189 
190 	mutex_unlock(&dev->lock);
191 }
192 
193 void rgmii_detach(struct platform_device *ofdev, int input)
194 {
195 	struct rgmii_instance *dev = platform_get_drvdata(ofdev);
196 	struct rgmii_regs __iomem *p;
197 
198 	BUG_ON(!dev || dev->users == 0);
199 	p = dev->base;
200 
201 	mutex_lock(&dev->lock);
202 
203 	RGMII_DBG(dev, "detach(%d)" NL, input);
204 
205 	/* Disable this input */
206 	out_be32(&p->fer, in_be32(&p->fer) & ~RGMII_FER_MASK(input));
207 
208 	--dev->users;
209 
210 	mutex_unlock(&dev->lock);
211 }
212 
213 int rgmii_get_regs_len(struct platform_device *ofdev)
214 {
215 	return sizeof(struct emac_ethtool_regs_subhdr) +
216 		sizeof(struct rgmii_regs);
217 }
218 
219 void *rgmii_dump_regs(struct platform_device *ofdev, void *buf)
220 {
221 	struct rgmii_instance *dev = platform_get_drvdata(ofdev);
222 	struct emac_ethtool_regs_subhdr *hdr = buf;
223 	struct rgmii_regs *regs = (struct rgmii_regs *)(hdr + 1);
224 
225 	hdr->version = 0;
226 	hdr->index = 0; /* for now, are there chips with more than one
227 			 * rgmii ? if yes, then we'll add a cell_index
228 			 * like we do for emac
229 			 */
230 	memcpy_fromio(regs, dev->base, sizeof(struct rgmii_regs));
231 	return regs + 1;
232 }
233 
234 
235 static int rgmii_probe(struct platform_device *ofdev)
236 {
237 	struct device_node *np = ofdev->dev.of_node;
238 	struct rgmii_instance *dev;
239 	struct resource regs;
240 	int rc;
241 
242 	rc = -ENOMEM;
243 	dev = kzalloc(sizeof(struct rgmii_instance), GFP_KERNEL);
244 	if (dev == NULL)
245 		goto err_gone;
246 
247 	mutex_init(&dev->lock);
248 	dev->ofdev = ofdev;
249 
250 	rc = -ENXIO;
251 	if (of_address_to_resource(np, 0, &regs)) {
252 		printk(KERN_ERR "%s: Can't get registers address\n",
253 		       np->full_name);
254 		goto err_free;
255 	}
256 
257 	rc = -ENOMEM;
258 	dev->base = (struct rgmii_regs __iomem *)ioremap(regs.start,
259 						 sizeof(struct rgmii_regs));
260 	if (dev->base == NULL) {
261 		printk(KERN_ERR "%s: Can't map device registers!\n",
262 		       np->full_name);
263 		goto err_free;
264 	}
265 
266 	/* Check for RGMII flags */
267 	if (of_get_property(ofdev->dev.of_node, "has-mdio", NULL))
268 		dev->flags |= EMAC_RGMII_FLAG_HAS_MDIO;
269 
270 	/* CAB lacks the right properties, fix this up */
271 	if (of_device_is_compatible(ofdev->dev.of_node, "ibm,rgmii-axon"))
272 		dev->flags |= EMAC_RGMII_FLAG_HAS_MDIO;
273 
274 	DBG2(dev, " Boot FER = 0x%08x, SSR = 0x%08x\n",
275 	     in_be32(&dev->base->fer), in_be32(&dev->base->ssr));
276 
277 	/* Disable all inputs by default */
278 	out_be32(&dev->base->fer, 0);
279 
280 	printk(KERN_INFO
281 	       "RGMII %s initialized with%s MDIO support\n",
282 	       ofdev->dev.of_node->full_name,
283 	       (dev->flags & EMAC_RGMII_FLAG_HAS_MDIO) ? "" : "out");
284 
285 	wmb();
286 	platform_set_drvdata(ofdev, dev);
287 
288 	return 0;
289 
290  err_free:
291 	kfree(dev);
292  err_gone:
293 	return rc;
294 }
295 
296 static int rgmii_remove(struct platform_device *ofdev)
297 {
298 	struct rgmii_instance *dev = platform_get_drvdata(ofdev);
299 
300 	WARN_ON(dev->users != 0);
301 
302 	iounmap(dev->base);
303 	kfree(dev);
304 
305 	return 0;
306 }
307 
308 static struct of_device_id rgmii_match[] =
309 {
310 	{
311 		.compatible	= "ibm,rgmii",
312 	},
313 	{
314 		.type		= "emac-rgmii",
315 	},
316 	{},
317 };
318 
319 static struct platform_driver rgmii_driver = {
320 	.driver = {
321 		.name = "emac-rgmii",
322 		.owner = THIS_MODULE,
323 		.of_match_table = rgmii_match,
324 	},
325 	.probe = rgmii_probe,
326 	.remove = rgmii_remove,
327 };
328 
329 int __init rgmii_init(void)
330 {
331 	return platform_driver_register(&rgmii_driver);
332 }
333 
334 void rgmii_exit(void)
335 {
336 	platform_driver_unregister(&rgmii_driver);
337 }
338