Linux 3.11-rc3
[cris-mirror.git] / drivers / net / ethernet / ibm / emac / rgmii.c
blobc47e23d6eeaa0b57752d4ca047567178e1d37026
1 /*
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 <asm/io.h>
29 #include "emac.h"
30 #include "debug.h"
32 // XXX FIXME: Axon seems to support a subset of the RGMII, we
33 // thus need to take that into account and possibly change some
34 // of the bit settings below that don't seem to quite match the
35 // AXON spec
37 /* RGMIIx_FER */
38 #define RGMII_FER_MASK(idx) (0x7 << ((idx) * 4))
39 #define RGMII_FER_RTBI(idx) (0x4 << ((idx) * 4))
40 #define RGMII_FER_RGMII(idx) (0x5 << ((idx) * 4))
41 #define RGMII_FER_TBI(idx) (0x6 << ((idx) * 4))
42 #define RGMII_FER_GMII(idx) (0x7 << ((idx) * 4))
43 #define RGMII_FER_MII(idx) RGMII_FER_GMII(idx)
45 /* RGMIIx_SSR */
46 #define RGMII_SSR_MASK(idx) (0x7 << ((idx) * 8))
47 #define RGMII_SSR_100(idx) (0x2 << ((idx) * 8))
48 #define RGMII_SSR_1000(idx) (0x4 << ((idx) * 8))
50 /* RGMII bridge supports only GMII/TBI and RGMII/RTBI PHYs */
51 static inline int rgmii_valid_mode(int phy_mode)
53 return phy_mode == PHY_MODE_GMII ||
54 phy_mode == PHY_MODE_MII ||
55 phy_mode == PHY_MODE_RGMII ||
56 phy_mode == PHY_MODE_TBI ||
57 phy_mode == PHY_MODE_RTBI;
60 static inline const char *rgmii_mode_name(int mode)
62 switch (mode) {
63 case PHY_MODE_RGMII:
64 return "RGMII";
65 case PHY_MODE_TBI:
66 return "TBI";
67 case PHY_MODE_GMII:
68 return "GMII";
69 case PHY_MODE_MII:
70 return "MII";
71 case PHY_MODE_RTBI:
72 return "RTBI";
73 default:
74 BUG();
78 static inline u32 rgmii_mode_mask(int mode, int input)
80 switch (mode) {
81 case PHY_MODE_RGMII:
82 return RGMII_FER_RGMII(input);
83 case PHY_MODE_TBI:
84 return RGMII_FER_TBI(input);
85 case PHY_MODE_GMII:
86 return RGMII_FER_GMII(input);
87 case PHY_MODE_MII:
88 return RGMII_FER_MII(input);
89 case PHY_MODE_RTBI:
90 return RGMII_FER_RTBI(input);
91 default:
92 BUG();
96 int rgmii_attach(struct platform_device *ofdev, int input, int mode)
98 struct rgmii_instance *dev = platform_get_drvdata(ofdev);
99 struct rgmii_regs __iomem *p = dev->base;
101 RGMII_DBG(dev, "attach(%d)" NL, input);
103 /* Check if we need to attach to a RGMII */
104 if (input < 0 || !rgmii_valid_mode(mode)) {
105 printk(KERN_ERR "%s: unsupported settings !\n",
106 ofdev->dev.of_node->full_name);
107 return -ENODEV;
110 mutex_lock(&dev->lock);
112 /* Enable this input */
113 out_be32(&p->fer, in_be32(&p->fer) | rgmii_mode_mask(mode, input));
115 printk(KERN_NOTICE "%s: input %d in %s mode\n",
116 ofdev->dev.of_node->full_name, input, rgmii_mode_name(mode));
118 ++dev->users;
120 mutex_unlock(&dev->lock);
122 return 0;
125 void rgmii_set_speed(struct platform_device *ofdev, int input, int speed)
127 struct rgmii_instance *dev = platform_get_drvdata(ofdev);
128 struct rgmii_regs __iomem *p = dev->base;
129 u32 ssr;
131 mutex_lock(&dev->lock);
133 ssr = in_be32(&p->ssr) & ~RGMII_SSR_MASK(input);
135 RGMII_DBG(dev, "speed(%d, %d)" NL, input, speed);
137 if (speed == SPEED_1000)
138 ssr |= RGMII_SSR_1000(input);
139 else if (speed == SPEED_100)
140 ssr |= RGMII_SSR_100(input);
142 out_be32(&p->ssr, ssr);
144 mutex_unlock(&dev->lock);
147 void rgmii_get_mdio(struct platform_device *ofdev, int input)
149 struct rgmii_instance *dev = platform_get_drvdata(ofdev);
150 struct rgmii_regs __iomem *p = dev->base;
151 u32 fer;
153 RGMII_DBG2(dev, "get_mdio(%d)" NL, input);
155 if (!(dev->flags & EMAC_RGMII_FLAG_HAS_MDIO))
156 return;
158 mutex_lock(&dev->lock);
160 fer = in_be32(&p->fer);
161 fer |= 0x00080000u >> input;
162 out_be32(&p->fer, fer);
163 (void)in_be32(&p->fer);
165 DBG2(dev, " fer = 0x%08x\n", fer);
168 void rgmii_put_mdio(struct platform_device *ofdev, int input)
170 struct rgmii_instance *dev = platform_get_drvdata(ofdev);
171 struct rgmii_regs __iomem *p = dev->base;
172 u32 fer;
174 RGMII_DBG2(dev, "put_mdio(%d)" NL, input);
176 if (!(dev->flags & EMAC_RGMII_FLAG_HAS_MDIO))
177 return;
179 fer = in_be32(&p->fer);
180 fer &= ~(0x00080000u >> input);
181 out_be32(&p->fer, fer);
182 (void)in_be32(&p->fer);
184 DBG2(dev, " fer = 0x%08x\n", fer);
186 mutex_unlock(&dev->lock);
189 void rgmii_detach(struct platform_device *ofdev, int input)
191 struct rgmii_instance *dev = platform_get_drvdata(ofdev);
192 struct rgmii_regs __iomem *p;
194 BUG_ON(!dev || dev->users == 0);
195 p = dev->base;
197 mutex_lock(&dev->lock);
199 RGMII_DBG(dev, "detach(%d)" NL, input);
201 /* Disable this input */
202 out_be32(&p->fer, in_be32(&p->fer) & ~RGMII_FER_MASK(input));
204 --dev->users;
206 mutex_unlock(&dev->lock);
209 int rgmii_get_regs_len(struct platform_device *ofdev)
211 return sizeof(struct emac_ethtool_regs_subhdr) +
212 sizeof(struct rgmii_regs);
215 void *rgmii_dump_regs(struct platform_device *ofdev, void *buf)
217 struct rgmii_instance *dev = platform_get_drvdata(ofdev);
218 struct emac_ethtool_regs_subhdr *hdr = buf;
219 struct rgmii_regs *regs = (struct rgmii_regs *)(hdr + 1);
221 hdr->version = 0;
222 hdr->index = 0; /* for now, are there chips with more than one
223 * rgmii ? if yes, then we'll add a cell_index
224 * like we do for emac
226 memcpy_fromio(regs, dev->base, sizeof(struct rgmii_regs));
227 return regs + 1;
231 static int rgmii_probe(struct platform_device *ofdev)
233 struct device_node *np = ofdev->dev.of_node;
234 struct rgmii_instance *dev;
235 struct resource regs;
236 int rc;
238 rc = -ENOMEM;
239 dev = kzalloc(sizeof(struct rgmii_instance), GFP_KERNEL);
240 if (dev == NULL)
241 goto err_gone;
243 mutex_init(&dev->lock);
244 dev->ofdev = ofdev;
246 rc = -ENXIO;
247 if (of_address_to_resource(np, 0, &regs)) {
248 printk(KERN_ERR "%s: Can't get registers address\n",
249 np->full_name);
250 goto err_free;
253 rc = -ENOMEM;
254 dev->base = (struct rgmii_regs __iomem *)ioremap(regs.start,
255 sizeof(struct rgmii_regs));
256 if (dev->base == NULL) {
257 printk(KERN_ERR "%s: Can't map device registers!\n",
258 np->full_name);
259 goto err_free;
262 /* Check for RGMII flags */
263 if (of_get_property(ofdev->dev.of_node, "has-mdio", NULL))
264 dev->flags |= EMAC_RGMII_FLAG_HAS_MDIO;
266 /* CAB lacks the right properties, fix this up */
267 if (of_device_is_compatible(ofdev->dev.of_node, "ibm,rgmii-axon"))
268 dev->flags |= EMAC_RGMII_FLAG_HAS_MDIO;
270 DBG2(dev, " Boot FER = 0x%08x, SSR = 0x%08x\n",
271 in_be32(&dev->base->fer), in_be32(&dev->base->ssr));
273 /* Disable all inputs by default */
274 out_be32(&dev->base->fer, 0);
276 printk(KERN_INFO
277 "RGMII %s initialized with%s MDIO support\n",
278 ofdev->dev.of_node->full_name,
279 (dev->flags & EMAC_RGMII_FLAG_HAS_MDIO) ? "" : "out");
281 wmb();
282 platform_set_drvdata(ofdev, dev);
284 return 0;
286 err_free:
287 kfree(dev);
288 err_gone:
289 return rc;
292 static int rgmii_remove(struct platform_device *ofdev)
294 struct rgmii_instance *dev = platform_get_drvdata(ofdev);
296 WARN_ON(dev->users != 0);
298 iounmap(dev->base);
299 kfree(dev);
301 return 0;
304 static struct of_device_id rgmii_match[] =
307 .compatible = "ibm,rgmii",
310 .type = "emac-rgmii",
315 static struct platform_driver rgmii_driver = {
316 .driver = {
317 .name = "emac-rgmii",
318 .owner = THIS_MODULE,
319 .of_match_table = rgmii_match,
321 .probe = rgmii_probe,
322 .remove = rgmii_remove,
325 int __init rgmii_init(void)
327 return platform_driver_register(&rgmii_driver);
330 void rgmii_exit(void)
332 platform_driver_unregister(&rgmii_driver);