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_100(idx) (0x2 << ((idx) * 8))
49 #define RGMII_SSR_1000(idx) (0x4 << ((idx) * 8))
51 /* RGMII bridge supports only GMII/TBI and RGMII/RTBI PHYs */
52 static inline int rgmii_valid_mode(int phy_mode
)
54 return phy_mode
== PHY_MODE_GMII
||
55 phy_mode
== PHY_MODE_MII
||
56 phy_mode
== PHY_MODE_RGMII
||
57 phy_mode
== PHY_MODE_TBI
||
58 phy_mode
== PHY_MODE_RTBI
;
61 static inline const char *rgmii_mode_name(int mode
)
79 static inline u32
rgmii_mode_mask(int mode
, int input
)
83 return RGMII_FER_RGMII(input
);
85 return RGMII_FER_TBI(input
);
87 return RGMII_FER_GMII(input
);
89 return RGMII_FER_MII(input
);
91 return RGMII_FER_RTBI(input
);
97 int rgmii_attach(struct platform_device
*ofdev
, int input
, int mode
)
99 struct rgmii_instance
*dev
= platform_get_drvdata(ofdev
);
100 struct rgmii_regs __iomem
*p
= dev
->base
;
102 RGMII_DBG(dev
, "attach(%d)" NL
, input
);
104 /* Check if we need to attach to a RGMII */
105 if (input
< 0 || !rgmii_valid_mode(mode
)) {
106 printk(KERN_ERR
"%s: unsupported settings !\n",
107 ofdev
->dev
.of_node
->full_name
);
111 mutex_lock(&dev
->lock
);
113 /* Enable this input */
114 out_be32(&p
->fer
, in_be32(&p
->fer
) | rgmii_mode_mask(mode
, input
));
116 printk(KERN_NOTICE
"%s: input %d in %s mode\n",
117 ofdev
->dev
.of_node
->full_name
, input
, rgmii_mode_name(mode
));
121 mutex_unlock(&dev
->lock
);
126 void rgmii_set_speed(struct platform_device
*ofdev
, int input
, int speed
)
128 struct rgmii_instance
*dev
= platform_get_drvdata(ofdev
);
129 struct rgmii_regs __iomem
*p
= dev
->base
;
132 mutex_lock(&dev
->lock
);
134 ssr
= in_be32(&p
->ssr
) & ~RGMII_SSR_MASK(input
);
136 RGMII_DBG(dev
, "speed(%d, %d)" NL
, input
, speed
);
138 if (speed
== SPEED_1000
)
139 ssr
|= RGMII_SSR_1000(input
);
140 else if (speed
== SPEED_100
)
141 ssr
|= RGMII_SSR_100(input
);
143 out_be32(&p
->ssr
, ssr
);
145 mutex_unlock(&dev
->lock
);
148 void rgmii_get_mdio(struct platform_device
*ofdev
, int input
)
150 struct rgmii_instance
*dev
= platform_get_drvdata(ofdev
);
151 struct rgmii_regs __iomem
*p
= dev
->base
;
154 RGMII_DBG2(dev
, "get_mdio(%d)" NL
, input
);
156 if (!(dev
->flags
& EMAC_RGMII_FLAG_HAS_MDIO
))
159 mutex_lock(&dev
->lock
);
161 fer
= in_be32(&p
->fer
);
162 fer
|= 0x00080000u
>> input
;
163 out_be32(&p
->fer
, fer
);
164 (void)in_be32(&p
->fer
);
166 DBG2(dev
, " fer = 0x%08x\n", fer
);
169 void rgmii_put_mdio(struct platform_device
*ofdev
, int input
)
171 struct rgmii_instance
*dev
= platform_get_drvdata(ofdev
);
172 struct rgmii_regs __iomem
*p
= dev
->base
;
175 RGMII_DBG2(dev
, "put_mdio(%d)" NL
, input
);
177 if (!(dev
->flags
& EMAC_RGMII_FLAG_HAS_MDIO
))
180 fer
= in_be32(&p
->fer
);
181 fer
&= ~(0x00080000u
>> input
);
182 out_be32(&p
->fer
, fer
);
183 (void)in_be32(&p
->fer
);
185 DBG2(dev
, " fer = 0x%08x\n", fer
);
187 mutex_unlock(&dev
->lock
);
190 void rgmii_detach(struct platform_device
*ofdev
, int input
)
192 struct rgmii_instance
*dev
= platform_get_drvdata(ofdev
);
193 struct rgmii_regs __iomem
*p
;
195 BUG_ON(!dev
|| dev
->users
== 0);
198 mutex_lock(&dev
->lock
);
200 RGMII_DBG(dev
, "detach(%d)" NL
, input
);
202 /* Disable this input */
203 out_be32(&p
->fer
, in_be32(&p
->fer
) & ~RGMII_FER_MASK(input
));
207 mutex_unlock(&dev
->lock
);
210 int rgmii_get_regs_len(struct platform_device
*ofdev
)
212 return sizeof(struct emac_ethtool_regs_subhdr
) +
213 sizeof(struct rgmii_regs
);
216 void *rgmii_dump_regs(struct platform_device
*ofdev
, void *buf
)
218 struct rgmii_instance
*dev
= platform_get_drvdata(ofdev
);
219 struct emac_ethtool_regs_subhdr
*hdr
= buf
;
220 struct rgmii_regs
*regs
= (struct rgmii_regs
*)(hdr
+ 1);
223 hdr
->index
= 0; /* for now, are there chips with more than one
224 * rgmii ? if yes, then we'll add a cell_index
225 * like we do for emac
227 memcpy_fromio(regs
, dev
->base
, sizeof(struct rgmii_regs
));
232 static int rgmii_probe(struct platform_device
*ofdev
)
234 struct device_node
*np
= ofdev
->dev
.of_node
;
235 struct rgmii_instance
*dev
;
236 struct resource regs
;
240 dev
= kzalloc(sizeof(struct rgmii_instance
), GFP_KERNEL
);
244 mutex_init(&dev
->lock
);
248 if (of_address_to_resource(np
, 0, ®s
)) {
249 printk(KERN_ERR
"%s: Can't get registers address\n",
255 dev
->base
= (struct rgmii_regs __iomem
*)ioremap(regs
.start
,
256 sizeof(struct rgmii_regs
));
257 if (dev
->base
== NULL
) {
258 printk(KERN_ERR
"%s: Can't map device registers!\n",
263 /* Check for RGMII flags */
264 if (of_get_property(ofdev
->dev
.of_node
, "has-mdio", NULL
))
265 dev
->flags
|= EMAC_RGMII_FLAG_HAS_MDIO
;
267 /* CAB lacks the right properties, fix this up */
268 if (of_device_is_compatible(ofdev
->dev
.of_node
, "ibm,rgmii-axon"))
269 dev
->flags
|= EMAC_RGMII_FLAG_HAS_MDIO
;
271 DBG2(dev
, " Boot FER = 0x%08x, SSR = 0x%08x\n",
272 in_be32(&dev
->base
->fer
), in_be32(&dev
->base
->ssr
));
274 /* Disable all inputs by default */
275 out_be32(&dev
->base
->fer
, 0);
278 "RGMII %s initialized with%s MDIO support\n",
279 ofdev
->dev
.of_node
->full_name
,
280 (dev
->flags
& EMAC_RGMII_FLAG_HAS_MDIO
) ? "" : "out");
283 platform_set_drvdata(ofdev
, dev
);
293 static int rgmii_remove(struct platform_device
*ofdev
)
295 struct rgmii_instance
*dev
= platform_get_drvdata(ofdev
);
297 WARN_ON(dev
->users
!= 0);
305 static struct of_device_id rgmii_match
[] =
308 .compatible
= "ibm,rgmii",
311 .type
= "emac-rgmii",
316 static struct platform_driver rgmii_driver
= {
318 .name
= "emac-rgmii",
319 .owner
= THIS_MODULE
,
320 .of_match_table
= rgmii_match
,
322 .probe
= rgmii_probe
,
323 .remove
= rgmii_remove
,
326 int __init
rgmii_init(void)
328 return platform_driver_register(&rgmii_driver
);
331 void rgmii_exit(void)
333 platform_driver_unregister(&rgmii_driver
);