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
;
193 BUG_ON(!dev
|| dev
->users
== 0);
196 mutex_lock(&dev
->lock
);
198 RGMII_DBG(dev
, "detach(%d)" NL
, input
);
200 /* Disable this input */
201 out_be32(&p
->fer
, in_be32(&p
->fer
) & ~RGMII_FER_MASK(input
));
205 mutex_unlock(&dev
->lock
);
208 int rgmii_get_regs_len(struct of_device
*ofdev
)
210 return sizeof(struct emac_ethtool_regs_subhdr
) +
211 sizeof(struct rgmii_regs
);
214 void *rgmii_dump_regs(struct of_device
*ofdev
, void *buf
)
216 struct rgmii_instance
*dev
= dev_get_drvdata(&ofdev
->dev
);
217 struct emac_ethtool_regs_subhdr
*hdr
= buf
;
218 struct rgmii_regs
*regs
= (struct rgmii_regs
*)(hdr
+ 1);
221 hdr
->index
= 0; /* for now, are there chips with more than one
222 * rgmii ? if yes, then we'll add a cell_index
223 * like we do for emac
225 memcpy_fromio(regs
, dev
->base
, sizeof(struct rgmii_regs
));
230 static int __devinit
rgmii_probe(struct of_device
*ofdev
,
231 const struct of_device_id
*match
)
233 struct device_node
*np
= ofdev
->node
;
234 struct rgmii_instance
*dev
;
235 struct resource regs
;
239 dev
= kzalloc(sizeof(struct rgmii_instance
), GFP_KERNEL
);
241 printk(KERN_ERR
"%s: could not allocate RGMII device!\n",
246 mutex_init(&dev
->lock
);
250 if (of_address_to_resource(np
, 0, ®s
)) {
251 printk(KERN_ERR
"%s: Can't get registers address\n",
257 dev
->base
= (struct rgmii_regs __iomem
*)ioremap(regs
.start
,
258 sizeof(struct rgmii_regs
));
259 if (dev
->base
== NULL
) {
260 printk(KERN_ERR
"%s: Can't map device registers!\n",
265 /* Check for RGMII flags */
266 if (of_get_property(ofdev
->node
, "has-mdio", NULL
))
267 dev
->flags
|= EMAC_RGMII_FLAG_HAS_MDIO
;
269 /* CAB lacks the right properties, fix this up */
270 if (of_device_is_compatible(ofdev
->node
, "ibm,rgmii-axon"))
271 dev
->flags
|= EMAC_RGMII_FLAG_HAS_MDIO
;
273 DBG2(dev
, " Boot FER = 0x%08x, SSR = 0x%08x\n",
274 in_be32(&dev
->base
->fer
), in_be32(&dev
->base
->ssr
));
276 /* Disable all inputs by default */
277 out_be32(&dev
->base
->fer
, 0);
280 "RGMII %s initialized with%s MDIO support\n",
281 ofdev
->node
->full_name
,
282 (dev
->flags
& EMAC_RGMII_FLAG_HAS_MDIO
) ? "" : "out");
285 dev_set_drvdata(&ofdev
->dev
, dev
);
295 static int __devexit
rgmii_remove(struct of_device
*ofdev
)
297 struct rgmii_instance
*dev
= dev_get_drvdata(&ofdev
->dev
);
299 dev_set_drvdata(&ofdev
->dev
, NULL
);
301 WARN_ON(dev
->users
!= 0);
309 static struct of_device_id rgmii_match
[] =
312 .compatible
= "ibm,rgmii",
315 .type
= "emac-rgmii",
320 static struct of_platform_driver rgmii_driver
= {
321 .name
= "emac-rgmii",
322 .match_table
= rgmii_match
,
324 .probe
= rgmii_probe
,
325 .remove
= rgmii_remove
,
328 int __init
rgmii_init(void)
330 return of_register_platform_driver(&rgmii_driver
);
333 void rgmii_exit(void)
335 of_unregister_platform_driver(&rgmii_driver
);