2 * drivers/net/ethernet/ibm/emac/rgmii.c
4 * Driver for PowerPC 4xx on-chip ethernet controller, RGMII bridge support.
6 * Copyright 2007 Benjamin Herrenschmidt, IBM Corp.
7 * <benh@kernel.crashing.org>
9 * Based on the arch/ppc version of the driver:
11 * Copyright (c) 2004, 2005 Zultys Technologies.
12 * Eugene Surovegin <eugene.surovegin@zultys.com> or <ebs@ebshome.net>
14 * Based on original work by
15 * Matt Porter <mporter@kernel.crashing.org>
16 * Copyright 2004 MontaVista Software, Inc.
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.
24 #include <linux/slab.h>
25 #include <linux/kernel.h>
26 #include <linux/ethtool.h>
27 #include <linux/of_address.h>
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
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)
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))
52 /* RGMII bridge supports only GMII/TBI and RGMII/RTBI PHYs */
53 static inline int rgmii_valid_mode(int phy_mode
)
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
;
62 static inline const char *rgmii_mode_name(int mode
)
80 static inline u32
rgmii_mode_mask(int mode
, int input
)
84 return RGMII_FER_RGMII(input
);
86 return RGMII_FER_TBI(input
);
88 return RGMII_FER_GMII(input
);
90 return RGMII_FER_MII(input
);
92 return RGMII_FER_RTBI(input
);
98 int rgmii_attach(struct platform_device
*ofdev
, int input
, int mode
)
100 struct rgmii_instance
*dev
= platform_get_drvdata(ofdev
);
101 struct rgmii_regs __iomem
*p
= dev
->base
;
103 RGMII_DBG(dev
, "attach(%d)" NL
, input
);
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
);
112 mutex_lock(&dev
->lock
);
114 /* Enable this input */
115 out_be32(&p
->fer
, in_be32(&p
->fer
) | rgmii_mode_mask(mode
, input
));
117 printk(KERN_NOTICE
"%s: input %d in %s mode\n",
118 ofdev
->dev
.of_node
->full_name
, input
, rgmii_mode_name(mode
));
122 mutex_unlock(&dev
->lock
);
127 void rgmii_set_speed(struct platform_device
*ofdev
, int input
, int speed
)
129 struct rgmii_instance
*dev
= platform_get_drvdata(ofdev
);
130 struct rgmii_regs __iomem
*p
= dev
->base
;
133 mutex_lock(&dev
->lock
);
135 ssr
= in_be32(&p
->ssr
) & ~RGMII_SSR_MASK(input
);
137 RGMII_DBG(dev
, "speed(%d, %d)" NL
, input
, speed
);
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
);
146 out_be32(&p
->ssr
, ssr
);
148 mutex_unlock(&dev
->lock
);
151 void rgmii_get_mdio(struct platform_device
*ofdev
, int input
)
153 struct rgmii_instance
*dev
= platform_get_drvdata(ofdev
);
154 struct rgmii_regs __iomem
*p
= dev
->base
;
157 RGMII_DBG2(dev
, "get_mdio(%d)" NL
, input
);
159 if (!(dev
->flags
& EMAC_RGMII_FLAG_HAS_MDIO
))
162 mutex_lock(&dev
->lock
);
164 fer
= in_be32(&p
->fer
);
165 fer
|= 0x00080000u
>> input
;
166 out_be32(&p
->fer
, fer
);
167 (void)in_be32(&p
->fer
);
169 DBG2(dev
, " fer = 0x%08x\n", fer
);
172 void rgmii_put_mdio(struct platform_device
*ofdev
, int input
)
174 struct rgmii_instance
*dev
= platform_get_drvdata(ofdev
);
175 struct rgmii_regs __iomem
*p
= dev
->base
;
178 RGMII_DBG2(dev
, "put_mdio(%d)" NL
, input
);
180 if (!(dev
->flags
& EMAC_RGMII_FLAG_HAS_MDIO
))
183 fer
= in_be32(&p
->fer
);
184 fer
&= ~(0x00080000u
>> input
);
185 out_be32(&p
->fer
, fer
);
186 (void)in_be32(&p
->fer
);
188 DBG2(dev
, " fer = 0x%08x\n", fer
);
190 mutex_unlock(&dev
->lock
);
193 void rgmii_detach(struct platform_device
*ofdev
, int input
)
195 struct rgmii_instance
*dev
= platform_get_drvdata(ofdev
);
196 struct rgmii_regs __iomem
*p
;
198 BUG_ON(!dev
|| dev
->users
== 0);
201 mutex_lock(&dev
->lock
);
203 RGMII_DBG(dev
, "detach(%d)" NL
, input
);
205 /* Disable this input */
206 out_be32(&p
->fer
, in_be32(&p
->fer
) & ~RGMII_FER_MASK(input
));
210 mutex_unlock(&dev
->lock
);
213 int rgmii_get_regs_len(struct platform_device
*ofdev
)
215 return sizeof(struct emac_ethtool_regs_subhdr
) +
216 sizeof(struct rgmii_regs
);
219 void *rgmii_dump_regs(struct platform_device
*ofdev
, void *buf
)
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);
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
230 memcpy_fromio(regs
, dev
->base
, sizeof(struct rgmii_regs
));
235 static int rgmii_probe(struct platform_device
*ofdev
)
237 struct device_node
*np
= ofdev
->dev
.of_node
;
238 struct rgmii_instance
*dev
;
239 struct resource regs
;
243 dev
= kzalloc(sizeof(struct rgmii_instance
), GFP_KERNEL
);
247 mutex_init(&dev
->lock
);
251 if (of_address_to_resource(np
, 0, ®s
)) {
252 printk(KERN_ERR
"%s: Can't get registers address\n",
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",
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
;
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
;
274 DBG2(dev
, " Boot FER = 0x%08x, SSR = 0x%08x\n",
275 in_be32(&dev
->base
->fer
), in_be32(&dev
->base
->ssr
));
277 /* Disable all inputs by default */
278 out_be32(&dev
->base
->fer
, 0);
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");
286 platform_set_drvdata(ofdev
, dev
);
296 static int rgmii_remove(struct platform_device
*ofdev
)
298 struct rgmii_instance
*dev
= platform_get_drvdata(ofdev
);
300 WARN_ON(dev
->users
!= 0);
308 static const struct of_device_id rgmii_match
[] =
311 .compatible
= "ibm,rgmii",
314 .type
= "emac-rgmii",
319 static struct platform_driver rgmii_driver
= {
321 .name
= "emac-rgmii",
322 .of_match_table
= rgmii_match
,
324 .probe
= rgmii_probe
,
325 .remove
= rgmii_remove
,
328 int __init
rgmii_init(void)
330 return platform_driver_register(&rgmii_driver
);
333 void rgmii_exit(void)
335 platform_driver_unregister(&rgmii_driver
);