Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / drivers / net / ibm_newemac / rgmii.c
blob5757788227be8d887537b21fbaf97dd7853f55c0
1 /*
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>
26 #include <asm/io.h>
28 #include "emac.h"
29 #include "debug.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
34 // AXON spec
36 /* RGMIIx_FER */
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))
43 /* RGMIIx_SSR */
44 #define RGMII_SSR_MASK(idx) (0x7 << ((idx) * 8))
45 #define RGMII_SSR_100(idx) (0x2 << ((idx) * 8))
46 #define RGMII_SSR_1000(idx) (0x4 << ((idx) * 8))
48 /* RGMII bridge supports only GMII/TBI and RGMII/RTBI PHYs */
49 static inline int rgmii_valid_mode(int phy_mode)
51 return phy_mode == PHY_MODE_GMII ||
52 phy_mode == PHY_MODE_RGMII ||
53 phy_mode == PHY_MODE_TBI ||
54 phy_mode == PHY_MODE_RTBI;
57 static inline const char *rgmii_mode_name(int mode)
59 switch (mode) {
60 case PHY_MODE_RGMII:
61 return "RGMII";
62 case PHY_MODE_TBI:
63 return "TBI";
64 case PHY_MODE_GMII:
65 return "GMII";
66 case PHY_MODE_RTBI:
67 return "RTBI";
68 default:
69 BUG();
73 static inline u32 rgmii_mode_mask(int mode, int input)
75 switch (mode) {
76 case PHY_MODE_RGMII:
77 return RGMII_FER_RGMII(input);
78 case PHY_MODE_TBI:
79 return RGMII_FER_TBI(input);
80 case PHY_MODE_GMII:
81 return RGMII_FER_GMII(input);
82 case PHY_MODE_RTBI:
83 return RGMII_FER_RTBI(input);
84 default:
85 BUG();
89 int __devinit rgmii_attach(struct of_device *ofdev, int input, int mode)
91 struct rgmii_instance *dev = dev_get_drvdata(&ofdev->dev);
92 struct rgmii_regs __iomem *p = dev->base;
94 RGMII_DBG(dev, "attach(%d)" NL, input);
96 /* Check if we need to attach to a RGMII */
97 if (input < 0 || !rgmii_valid_mode(mode)) {
98 printk(KERN_ERR "%s: unsupported settings !\n",
99 ofdev->node->full_name);
100 return -ENODEV;
103 mutex_lock(&dev->lock);
105 /* Enable this input */
106 out_be32(&p->fer, in_be32(&p->fer) | rgmii_mode_mask(mode, input));
108 printk(KERN_NOTICE "%s: input %d in %s mode\n",
109 ofdev->node->full_name, input, rgmii_mode_name(mode));
111 ++dev->users;
113 mutex_unlock(&dev->lock);
115 return 0;
118 void rgmii_set_speed(struct of_device *ofdev, int input, int speed)
120 struct rgmii_instance *dev = dev_get_drvdata(&ofdev->dev);
121 struct rgmii_regs __iomem *p = dev->base;
122 u32 ssr;
124 mutex_lock(&dev->lock);
126 ssr = in_be32(&p->ssr) & ~RGMII_SSR_MASK(input);
128 RGMII_DBG(dev, "speed(%d, %d)" NL, input, speed);
130 if (speed == SPEED_1000)
131 ssr |= RGMII_SSR_1000(input);
132 else if (speed == SPEED_100)
133 ssr |= RGMII_SSR_100(input);
135 out_be32(&p->ssr, ssr);
137 mutex_unlock(&dev->lock);
140 void rgmii_get_mdio(struct of_device *ofdev, int input)
142 struct rgmii_instance *dev = dev_get_drvdata(&ofdev->dev);
143 struct rgmii_regs __iomem *p = dev->base;
144 u32 fer;
146 RGMII_DBG2(dev, "get_mdio(%d)" NL, input);
148 if (!(dev->flags & EMAC_RGMII_FLAG_HAS_MDIO))
149 return;
151 mutex_lock(&dev->lock);
153 fer = in_be32(&p->fer);
154 fer |= 0x00080000u >> input;
155 out_be32(&p->fer, fer);
156 (void)in_be32(&p->fer);
158 DBG2(dev, " fer = 0x%08x\n", fer);
161 void rgmii_put_mdio(struct of_device *ofdev, int input)
163 struct rgmii_instance *dev = dev_get_drvdata(&ofdev->dev);
164 struct rgmii_regs __iomem *p = dev->base;
165 u32 fer;
167 RGMII_DBG2(dev, "put_mdio(%d)" NL, input);
169 if (!(dev->flags & EMAC_RGMII_FLAG_HAS_MDIO))
170 return;
172 fer = in_be32(&p->fer);
173 fer &= ~(0x00080000u >> input);
174 out_be32(&p->fer, fer);
175 (void)in_be32(&p->fer);
177 DBG2(dev, " fer = 0x%08x\n", fer);
179 mutex_unlock(&dev->lock);
182 void __devexit rgmii_detach(struct of_device *ofdev, int input)
184 struct rgmii_instance *dev = dev_get_drvdata(&ofdev->dev);
185 struct rgmii_regs __iomem *p = dev->base;
187 mutex_lock(&dev->lock);
189 BUG_ON(!dev || dev->users == 0);
191 RGMII_DBG(dev, "detach(%d)" NL, input);
193 /* Disable this input */
194 out_be32(&p->fer, in_be32(&p->fer) & ~RGMII_FER_MASK(input));
196 --dev->users;
198 mutex_unlock(&dev->lock);
201 int rgmii_get_regs_len(struct of_device *ofdev)
203 return sizeof(struct emac_ethtool_regs_subhdr) +
204 sizeof(struct rgmii_regs);
207 void *rgmii_dump_regs(struct of_device *ofdev, void *buf)
209 struct rgmii_instance *dev = dev_get_drvdata(&ofdev->dev);
210 struct emac_ethtool_regs_subhdr *hdr = buf;
211 struct rgmii_regs *regs = (struct rgmii_regs *)(hdr + 1);
213 hdr->version = 0;
214 hdr->index = 0; /* for now, are there chips with more than one
215 * rgmii ? if yes, then we'll add a cell_index
216 * like we do for emac
218 memcpy_fromio(regs, dev->base, sizeof(struct rgmii_regs));
219 return regs + 1;
223 static int __devinit rgmii_probe(struct of_device *ofdev,
224 const struct of_device_id *match)
226 struct device_node *np = ofdev->node;
227 struct rgmii_instance *dev;
228 struct resource regs;
229 int rc;
231 rc = -ENOMEM;
232 dev = kzalloc(sizeof(struct rgmii_instance), GFP_KERNEL);
233 if (dev == NULL) {
234 printk(KERN_ERR "%s: could not allocate RGMII device!\n",
235 np->full_name);
236 goto err_gone;
239 mutex_init(&dev->lock);
240 dev->ofdev = ofdev;
242 rc = -ENXIO;
243 if (of_address_to_resource(np, 0, &regs)) {
244 printk(KERN_ERR "%s: Can't get registers address\n",
245 np->full_name);
246 goto err_free;
249 rc = -ENOMEM;
250 dev->base = (struct rgmii_regs __iomem *)ioremap(regs.start,
251 sizeof(struct rgmii_regs));
252 if (dev->base == NULL) {
253 printk(KERN_ERR "%s: Can't map device registers!\n",
254 np->full_name);
255 goto err_free;
258 /* Check for RGMII flags */
259 if (of_get_property(ofdev->node, "has-mdio", NULL))
260 dev->flags |= EMAC_RGMII_FLAG_HAS_MDIO;
262 /* CAB lacks the right properties, fix this up */
263 if (of_device_is_compatible(ofdev->node, "ibm,rgmii-axon"))
264 dev->flags |= EMAC_RGMII_FLAG_HAS_MDIO;
266 DBG2(dev, " Boot FER = 0x%08x, SSR = 0x%08x\n",
267 in_be32(&dev->base->fer), in_be32(&dev->base->ssr));
269 /* Disable all inputs by default */
270 out_be32(&dev->base->fer, 0);
272 printk(KERN_INFO
273 "RGMII %s initialized with%s MDIO support\n",
274 ofdev->node->full_name,
275 (dev->flags & EMAC_RGMII_FLAG_HAS_MDIO) ? "" : "out");
277 wmb();
278 dev_set_drvdata(&ofdev->dev, dev);
280 return 0;
282 err_free:
283 kfree(dev);
284 err_gone:
285 return rc;
288 static int __devexit rgmii_remove(struct of_device *ofdev)
290 struct rgmii_instance *dev = dev_get_drvdata(&ofdev->dev);
292 dev_set_drvdata(&ofdev->dev, NULL);
294 WARN_ON(dev->users != 0);
296 iounmap(dev->base);
297 kfree(dev);
299 return 0;
302 static struct of_device_id rgmii_match[] =
305 .compatible = "ibm,rgmii",
308 .type = "emac-rgmii",
313 static struct of_platform_driver rgmii_driver = {
314 .name = "emac-rgmii",
315 .match_table = rgmii_match,
317 .probe = rgmii_probe,
318 .remove = rgmii_remove,
321 int __init rgmii_init(void)
323 return of_register_platform_driver(&rgmii_driver);
326 void rgmii_exit(void)
328 of_unregister_platform_driver(&rgmii_driver);