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_interface_mode_is_rgmii(phy_mode
) ||
56 phy_mode
== PHY_INTERFACE_MODE_GMII
||
57 phy_mode
== PHY_INTERFACE_MODE_MII
||
58 phy_mode
== PHY_INTERFACE_MODE_TBI
||
59 phy_mode
== PHY_INTERFACE_MODE_RTBI
;
62 static inline u32
rgmii_mode_mask(int mode
, int input
)
65 case PHY_INTERFACE_MODE_RGMII
:
66 case PHY_INTERFACE_MODE_RGMII_ID
:
67 case PHY_INTERFACE_MODE_RGMII_RXID
:
68 case PHY_INTERFACE_MODE_RGMII_TXID
:
69 return RGMII_FER_RGMII(input
);
70 case PHY_INTERFACE_MODE_TBI
:
71 return RGMII_FER_TBI(input
);
72 case PHY_INTERFACE_MODE_GMII
:
73 return RGMII_FER_GMII(input
);
74 case PHY_INTERFACE_MODE_MII
:
75 return RGMII_FER_MII(input
);
76 case PHY_INTERFACE_MODE_RTBI
:
77 return RGMII_FER_RTBI(input
);
83 int rgmii_attach(struct platform_device
*ofdev
, int input
, int mode
)
85 struct rgmii_instance
*dev
= platform_get_drvdata(ofdev
);
86 struct rgmii_regs __iomem
*p
= dev
->base
;
88 RGMII_DBG(dev
, "attach(%d)" NL
, input
);
90 /* Check if we need to attach to a RGMII */
91 if (input
< 0 || !rgmii_valid_mode(mode
)) {
92 printk(KERN_ERR
"%pOF: unsupported settings !\n",
97 mutex_lock(&dev
->lock
);
99 /* Enable this input */
100 out_be32(&p
->fer
, in_be32(&p
->fer
) | rgmii_mode_mask(mode
, input
));
102 printk(KERN_NOTICE
"%pOF: input %d in %s mode\n",
103 ofdev
->dev
.of_node
, input
, phy_modes(mode
));
107 mutex_unlock(&dev
->lock
);
112 void rgmii_set_speed(struct platform_device
*ofdev
, int input
, int speed
)
114 struct rgmii_instance
*dev
= platform_get_drvdata(ofdev
);
115 struct rgmii_regs __iomem
*p
= dev
->base
;
118 mutex_lock(&dev
->lock
);
120 ssr
= in_be32(&p
->ssr
) & ~RGMII_SSR_MASK(input
);
122 RGMII_DBG(dev
, "speed(%d, %d)" NL
, input
, speed
);
124 if (speed
== SPEED_1000
)
125 ssr
|= RGMII_SSR_1000(input
);
126 else if (speed
== SPEED_100
)
127 ssr
|= RGMII_SSR_100(input
);
128 else if (speed
== SPEED_10
)
129 ssr
|= RGMII_SSR_10(input
);
131 out_be32(&p
->ssr
, ssr
);
133 mutex_unlock(&dev
->lock
);
136 void rgmii_get_mdio(struct platform_device
*ofdev
, int input
)
138 struct rgmii_instance
*dev
= platform_get_drvdata(ofdev
);
139 struct rgmii_regs __iomem
*p
= dev
->base
;
142 RGMII_DBG2(dev
, "get_mdio(%d)" NL
, input
);
144 if (!(dev
->flags
& EMAC_RGMII_FLAG_HAS_MDIO
))
147 mutex_lock(&dev
->lock
);
149 fer
= in_be32(&p
->fer
);
150 fer
|= 0x00080000u
>> input
;
151 out_be32(&p
->fer
, fer
);
152 (void)in_be32(&p
->fer
);
154 DBG2(dev
, " fer = 0x%08x\n", fer
);
157 void rgmii_put_mdio(struct platform_device
*ofdev
, int input
)
159 struct rgmii_instance
*dev
= platform_get_drvdata(ofdev
);
160 struct rgmii_regs __iomem
*p
= dev
->base
;
163 RGMII_DBG2(dev
, "put_mdio(%d)" NL
, input
);
165 if (!(dev
->flags
& EMAC_RGMII_FLAG_HAS_MDIO
))
168 fer
= in_be32(&p
->fer
);
169 fer
&= ~(0x00080000u
>> input
);
170 out_be32(&p
->fer
, fer
);
171 (void)in_be32(&p
->fer
);
173 DBG2(dev
, " fer = 0x%08x\n", fer
);
175 mutex_unlock(&dev
->lock
);
178 void rgmii_detach(struct platform_device
*ofdev
, int input
)
180 struct rgmii_instance
*dev
= platform_get_drvdata(ofdev
);
181 struct rgmii_regs __iomem
*p
;
183 BUG_ON(!dev
|| dev
->users
== 0);
186 mutex_lock(&dev
->lock
);
188 RGMII_DBG(dev
, "detach(%d)" NL
, input
);
190 /* Disable this input */
191 out_be32(&p
->fer
, in_be32(&p
->fer
) & ~RGMII_FER_MASK(input
));
195 mutex_unlock(&dev
->lock
);
198 int rgmii_get_regs_len(struct platform_device
*ofdev
)
200 return sizeof(struct emac_ethtool_regs_subhdr
) +
201 sizeof(struct rgmii_regs
);
204 void *rgmii_dump_regs(struct platform_device
*ofdev
, void *buf
)
206 struct rgmii_instance
*dev
= platform_get_drvdata(ofdev
);
207 struct emac_ethtool_regs_subhdr
*hdr
= buf
;
208 struct rgmii_regs
*regs
= (struct rgmii_regs
*)(hdr
+ 1);
211 hdr
->index
= 0; /* for now, are there chips with more than one
212 * rgmii ? if yes, then we'll add a cell_index
213 * like we do for emac
215 memcpy_fromio(regs
, dev
->base
, sizeof(struct rgmii_regs
));
220 static int rgmii_probe(struct platform_device
*ofdev
)
222 struct device_node
*np
= ofdev
->dev
.of_node
;
223 struct rgmii_instance
*dev
;
224 struct resource regs
;
228 dev
= kzalloc(sizeof(struct rgmii_instance
), GFP_KERNEL
);
232 mutex_init(&dev
->lock
);
236 if (of_address_to_resource(np
, 0, ®s
)) {
237 printk(KERN_ERR
"%pOF: Can't get registers address\n", np
);
242 dev
->base
= (struct rgmii_regs __iomem
*)ioremap(regs
.start
,
243 sizeof(struct rgmii_regs
));
244 if (dev
->base
== NULL
) {
245 printk(KERN_ERR
"%pOF: Can't map device registers!\n", np
);
249 /* Check for RGMII flags */
250 if (of_get_property(ofdev
->dev
.of_node
, "has-mdio", NULL
))
251 dev
->flags
|= EMAC_RGMII_FLAG_HAS_MDIO
;
253 /* CAB lacks the right properties, fix this up */
254 if (of_device_is_compatible(ofdev
->dev
.of_node
, "ibm,rgmii-axon"))
255 dev
->flags
|= EMAC_RGMII_FLAG_HAS_MDIO
;
257 DBG2(dev
, " Boot FER = 0x%08x, SSR = 0x%08x\n",
258 in_be32(&dev
->base
->fer
), in_be32(&dev
->base
->ssr
));
260 /* Disable all inputs by default */
261 out_be32(&dev
->base
->fer
, 0);
264 "RGMII %pOF initialized with%s MDIO support\n",
266 (dev
->flags
& EMAC_RGMII_FLAG_HAS_MDIO
) ? "" : "out");
269 platform_set_drvdata(ofdev
, dev
);
279 static int rgmii_remove(struct platform_device
*ofdev
)
281 struct rgmii_instance
*dev
= platform_get_drvdata(ofdev
);
283 WARN_ON(dev
->users
!= 0);
291 static const struct of_device_id rgmii_match
[] =
294 .compatible
= "ibm,rgmii",
297 .type
= "emac-rgmii",
302 static struct platform_driver rgmii_driver
= {
304 .name
= "emac-rgmii",
305 .of_match_table
= rgmii_match
,
307 .probe
= rgmii_probe
,
308 .remove
= rgmii_remove
,
311 int __init
rgmii_init(void)
313 return platform_driver_register(&rgmii_driver
);
316 void rgmii_exit(void)
318 platform_driver_unregister(&rgmii_driver
);