2 * drivers/net/ibm_newemac/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/kernel.h>
25 #include <linux/ethtool.h>
31 // XXX FIXME: Axon seems to support a subset of the RGMII, we
32 // thus need to take that into account and possibly change some
33 // of the bit settings below that don't seem to quite match the
37 #define RGMII_FER_MASK(idx) (0x7 << ((idx) * 4))
38 #define RGMII_FER_RTBI(idx) (0x4 << ((idx) * 4))
39 #define RGMII_FER_RGMII(idx) (0x5 << ((idx) * 4))
40 #define RGMII_FER_TBI(idx) (0x6 << ((idx) * 4))
41 #define RGMII_FER_GMII(idx) (0x7 << ((idx) * 4))
42 #define RGMII_FER_MII(idx) RGMII_FER_GMII(idx)
45 #define RGMII_SSR_MASK(idx) (0x7 << ((idx) * 8))
46 #define RGMII_SSR_100(idx) (0x2 << ((idx) * 8))
47 #define RGMII_SSR_1000(idx) (0x4 << ((idx) * 8))
49 /* RGMII bridge supports only GMII/TBI and RGMII/RTBI PHYs */
50 static inline int rgmii_valid_mode(int phy_mode
)
52 return phy_mode
== PHY_MODE_GMII
||
53 phy_mode
== PHY_MODE_MII
||
54 phy_mode
== PHY_MODE_RGMII
||
55 phy_mode
== PHY_MODE_TBI
||
56 phy_mode
== PHY_MODE_RTBI
;
59 static inline const char *rgmii_mode_name(int mode
)
77 static inline u32
rgmii_mode_mask(int mode
, int input
)
81 return RGMII_FER_RGMII(input
);
83 return RGMII_FER_TBI(input
);
85 return RGMII_FER_GMII(input
);
87 return RGMII_FER_MII(input
);
89 return RGMII_FER_RTBI(input
);
95 int __devinit
rgmii_attach(struct of_device
*ofdev
, int input
, int mode
)
97 struct rgmii_instance
*dev
= dev_get_drvdata(&ofdev
->dev
);
98 struct rgmii_regs __iomem
*p
= dev
->base
;
100 RGMII_DBG(dev
, "attach(%d)" NL
, input
);
102 /* Check if we need to attach to a RGMII */
103 if (input
< 0 || !rgmii_valid_mode(mode
)) {
104 printk(KERN_ERR
"%s: unsupported settings !\n",
105 ofdev
->node
->full_name
);
109 mutex_lock(&dev
->lock
);
111 /* Enable this input */
112 out_be32(&p
->fer
, in_be32(&p
->fer
) | rgmii_mode_mask(mode
, input
));
114 printk(KERN_NOTICE
"%s: input %d in %s mode\n",
115 ofdev
->node
->full_name
, input
, rgmii_mode_name(mode
));
119 mutex_unlock(&dev
->lock
);
124 void rgmii_set_speed(struct of_device
*ofdev
, int input
, int speed
)
126 struct rgmii_instance
*dev
= dev_get_drvdata(&ofdev
->dev
);
127 struct rgmii_regs __iomem
*p
= dev
->base
;
130 mutex_lock(&dev
->lock
);
132 ssr
= in_be32(&p
->ssr
) & ~RGMII_SSR_MASK(input
);
134 RGMII_DBG(dev
, "speed(%d, %d)" NL
, input
, speed
);
136 if (speed
== SPEED_1000
)
137 ssr
|= RGMII_SSR_1000(input
);
138 else if (speed
== SPEED_100
)
139 ssr
|= RGMII_SSR_100(input
);
141 out_be32(&p
->ssr
, ssr
);
143 mutex_unlock(&dev
->lock
);
146 void rgmii_get_mdio(struct of_device
*ofdev
, int input
)
148 struct rgmii_instance
*dev
= dev_get_drvdata(&ofdev
->dev
);
149 struct rgmii_regs __iomem
*p
= dev
->base
;
152 RGMII_DBG2(dev
, "get_mdio(%d)" NL
, input
);
154 if (!(dev
->flags
& EMAC_RGMII_FLAG_HAS_MDIO
))
157 mutex_lock(&dev
->lock
);
159 fer
= in_be32(&p
->fer
);
160 fer
|= 0x00080000u
>> input
;
161 out_be32(&p
->fer
, fer
);
162 (void)in_be32(&p
->fer
);
164 DBG2(dev
, " fer = 0x%08x\n", fer
);
167 void rgmii_put_mdio(struct of_device
*ofdev
, int input
)
169 struct rgmii_instance
*dev
= dev_get_drvdata(&ofdev
->dev
);
170 struct rgmii_regs __iomem
*p
= dev
->base
;
173 RGMII_DBG2(dev
, "put_mdio(%d)" NL
, input
);
175 if (!(dev
->flags
& EMAC_RGMII_FLAG_HAS_MDIO
))
178 fer
= in_be32(&p
->fer
);
179 fer
&= ~(0x00080000u
>> input
);
180 out_be32(&p
->fer
, fer
);
181 (void)in_be32(&p
->fer
);
183 DBG2(dev
, " fer = 0x%08x\n", fer
);
185 mutex_unlock(&dev
->lock
);
188 void rgmii_detach(struct of_device
*ofdev
, int input
)
190 struct rgmii_instance
*dev
= dev_get_drvdata(&ofdev
->dev
);
191 struct rgmii_regs __iomem
*p
= dev
->base
;
193 mutex_lock(&dev
->lock
);
195 BUG_ON(!dev
|| dev
->users
== 0);
197 RGMII_DBG(dev
, "detach(%d)" NL
, input
);
199 /* Disable this input */
200 out_be32(&p
->fer
, in_be32(&p
->fer
) & ~RGMII_FER_MASK(input
));
204 mutex_unlock(&dev
->lock
);
207 int rgmii_get_regs_len(struct of_device
*ofdev
)
209 return sizeof(struct emac_ethtool_regs_subhdr
) +
210 sizeof(struct rgmii_regs
);
213 void *rgmii_dump_regs(struct of_device
*ofdev
, void *buf
)
215 struct rgmii_instance
*dev
= dev_get_drvdata(&ofdev
->dev
);
216 struct emac_ethtool_regs_subhdr
*hdr
= buf
;
217 struct rgmii_regs
*regs
= (struct rgmii_regs
*)(hdr
+ 1);
220 hdr
->index
= 0; /* for now, are there chips with more than one
221 * rgmii ? if yes, then we'll add a cell_index
222 * like we do for emac
224 memcpy_fromio(regs
, dev
->base
, sizeof(struct rgmii_regs
));
229 static int __devinit
rgmii_probe(struct of_device
*ofdev
,
230 const struct of_device_id
*match
)
232 struct device_node
*np
= ofdev
->node
;
233 struct rgmii_instance
*dev
;
234 struct resource regs
;
238 dev
= kzalloc(sizeof(struct rgmii_instance
), GFP_KERNEL
);
240 printk(KERN_ERR
"%s: could not allocate RGMII device!\n",
245 mutex_init(&dev
->lock
);
249 if (of_address_to_resource(np
, 0, ®s
)) {
250 printk(KERN_ERR
"%s: Can't get registers address\n",
256 dev
->base
= (struct rgmii_regs __iomem
*)ioremap(regs
.start
,
257 sizeof(struct rgmii_regs
));
258 if (dev
->base
== NULL
) {
259 printk(KERN_ERR
"%s: Can't map device registers!\n",
264 /* Check for RGMII flags */
265 if (of_get_property(ofdev
->node
, "has-mdio", NULL
))
266 dev
->flags
|= EMAC_RGMII_FLAG_HAS_MDIO
;
268 /* CAB lacks the right properties, fix this up */
269 if (of_device_is_compatible(ofdev
->node
, "ibm,rgmii-axon"))
270 dev
->flags
|= EMAC_RGMII_FLAG_HAS_MDIO
;
272 DBG2(dev
, " Boot FER = 0x%08x, SSR = 0x%08x\n",
273 in_be32(&dev
->base
->fer
), in_be32(&dev
->base
->ssr
));
275 /* Disable all inputs by default */
276 out_be32(&dev
->base
->fer
, 0);
279 "RGMII %s initialized with%s MDIO support\n",
280 ofdev
->node
->full_name
,
281 (dev
->flags
& EMAC_RGMII_FLAG_HAS_MDIO
) ? "" : "out");
284 dev_set_drvdata(&ofdev
->dev
, dev
);
294 static int __devexit
rgmii_remove(struct of_device
*ofdev
)
296 struct rgmii_instance
*dev
= dev_get_drvdata(&ofdev
->dev
);
298 dev_set_drvdata(&ofdev
->dev
, NULL
);
300 WARN_ON(dev
->users
!= 0);
308 static struct of_device_id rgmii_match
[] =
311 .compatible
= "ibm,rgmii",
314 .type
= "emac-rgmii",
319 static struct of_platform_driver rgmii_driver
= {
320 .name
= "emac-rgmii",
321 .match_table
= rgmii_match
,
323 .probe
= rgmii_probe
,
324 .remove
= rgmii_remove
,
327 int __init
rgmii_init(void)
329 return of_register_platform_driver(&rgmii_driver
);
332 void rgmii_exit(void)
334 of_unregister_platform_driver(&rgmii_driver
);