PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / net / ethernet / ibm / emac / rgmii.c
blob4fb2f96da23bb32f4709baddbff77cc7880e49d4
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 <linux/of_address.h>
28 #include <asm/io.h>
30 #include "emac.h"
31 #include "debug.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
36 // AXON spec
38 /* RGMIIx_FER */
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)
46 /* RGMIIx_SSR */
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)
63 switch (mode) {
64 case PHY_MODE_RGMII:
65 return "RGMII";
66 case PHY_MODE_TBI:
67 return "TBI";
68 case PHY_MODE_GMII:
69 return "GMII";
70 case PHY_MODE_MII:
71 return "MII";
72 case PHY_MODE_RTBI:
73 return "RTBI";
74 default:
75 BUG();
79 static inline u32 rgmii_mode_mask(int mode, int input)
81 switch (mode) {
82 case PHY_MODE_RGMII:
83 return RGMII_FER_RGMII(input);
84 case PHY_MODE_TBI:
85 return RGMII_FER_TBI(input);
86 case PHY_MODE_GMII:
87 return RGMII_FER_GMII(input);
88 case PHY_MODE_MII:
89 return RGMII_FER_MII(input);
90 case PHY_MODE_RTBI:
91 return RGMII_FER_RTBI(input);
92 default:
93 BUG();
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);
108 return -ENODEV;
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));
119 ++dev->users;
121 mutex_unlock(&dev->lock);
123 return 0;
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;
130 u32 ssr;
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;
152 u32 fer;
154 RGMII_DBG2(dev, "get_mdio(%d)" NL, input);
156 if (!(dev->flags & EMAC_RGMII_FLAG_HAS_MDIO))
157 return;
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;
173 u32 fer;
175 RGMII_DBG2(dev, "put_mdio(%d)" NL, input);
177 if (!(dev->flags & EMAC_RGMII_FLAG_HAS_MDIO))
178 return;
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);
196 p = dev->base;
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));
205 --dev->users;
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);
222 hdr->version = 0;
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));
228 return regs + 1;
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;
237 int rc;
239 rc = -ENOMEM;
240 dev = kzalloc(sizeof(struct rgmii_instance), GFP_KERNEL);
241 if (dev == NULL)
242 goto err_gone;
244 mutex_init(&dev->lock);
245 dev->ofdev = ofdev;
247 rc = -ENXIO;
248 if (of_address_to_resource(np, 0, &regs)) {
249 printk(KERN_ERR "%s: Can't get registers address\n",
250 np->full_name);
251 goto err_free;
254 rc = -ENOMEM;
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",
259 np->full_name);
260 goto err_free;
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);
277 printk(KERN_INFO
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");
282 wmb();
283 platform_set_drvdata(ofdev, dev);
285 return 0;
287 err_free:
288 kfree(dev);
289 err_gone:
290 return rc;
293 static int rgmii_remove(struct platform_device *ofdev)
295 struct rgmii_instance *dev = platform_get_drvdata(ofdev);
297 WARN_ON(dev->users != 0);
299 iounmap(dev->base);
300 kfree(dev);
302 return 0;
305 static struct of_device_id rgmii_match[] =
308 .compatible = "ibm,rgmii",
311 .type = "emac-rgmii",
316 static struct platform_driver rgmii_driver = {
317 .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);