blob: 1d5379de6900f5558e8130b0e4c84b5e3e85f818 [file] [log] [blame]
David Gibson1d3bb992007-08-23 13:56:01 +10001/*
2 * drivers/net/ibm_newemac/rgmii.c
3 *
4 * Driver for PowerPC 4xx on-chip ethernet controller, RGMII bridge support.
5 *
Benjamin Herrenschmidt17cf8032007-12-05 11:14:33 +11006 * Copyright 2007 Benjamin Herrenschmidt, IBM Corp.
7 * <benh@kernel.crashing.org>
8 *
9 * Based on the arch/ppc version of the driver:
10 *
David Gibson1d3bb992007-08-23 13:56:01 +100011 * Copyright (c) 2004, 2005 Zultys Technologies.
12 * Eugene Surovegin <eugene.surovegin@zultys.com> or <ebs@ebshome.net>
13 *
14 * Based on original work by
15 * Matt Porter <mporter@kernel.crashing.org>
16 * Copyright 2004 MontaVista Software, Inc.
17 *
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.
22 *
23 */
24#include <linux/kernel.h>
25#include <linux/ethtool.h>
26#include <asm/io.h>
27
28#include "emac.h"
29#include "debug.h"
30
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
35
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))
Grant Erickson004ea682008-07-09 01:03:06 +100042#define RGMII_FER_MII(idx) RGMII_FER_GMII(idx)
David Gibson1d3bb992007-08-23 13:56:01 +100043
44/* RGMIIx_SSR */
45#define RGMII_SSR_MASK(idx) (0x7 << ((idx) * 8))
46#define RGMII_SSR_100(idx) (0x2 << ((idx) * 8))
47#define RGMII_SSR_1000(idx) (0x4 << ((idx) * 8))
48
49/* RGMII bridge supports only GMII/TBI and RGMII/RTBI PHYs */
50static inline int rgmii_valid_mode(int phy_mode)
51{
52 return phy_mode == PHY_MODE_GMII ||
Grant Erickson004ea682008-07-09 01:03:06 +100053 phy_mode == PHY_MODE_MII ||
David Gibson1d3bb992007-08-23 13:56:01 +100054 phy_mode == PHY_MODE_RGMII ||
55 phy_mode == PHY_MODE_TBI ||
56 phy_mode == PHY_MODE_RTBI;
57}
58
59static inline const char *rgmii_mode_name(int mode)
60{
61 switch (mode) {
62 case PHY_MODE_RGMII:
63 return "RGMII";
64 case PHY_MODE_TBI:
65 return "TBI";
66 case PHY_MODE_GMII:
67 return "GMII";
Grant Erickson004ea682008-07-09 01:03:06 +100068 case PHY_MODE_MII:
69 return "MII";
David Gibson1d3bb992007-08-23 13:56:01 +100070 case PHY_MODE_RTBI:
71 return "RTBI";
72 default:
73 BUG();
74 }
75}
76
77static inline u32 rgmii_mode_mask(int mode, int input)
78{
79 switch (mode) {
80 case PHY_MODE_RGMII:
81 return RGMII_FER_RGMII(input);
82 case PHY_MODE_TBI:
83 return RGMII_FER_TBI(input);
84 case PHY_MODE_GMII:
85 return RGMII_FER_GMII(input);
Grant Erickson004ea682008-07-09 01:03:06 +100086 case PHY_MODE_MII:
87 return RGMII_FER_MII(input);
David Gibson1d3bb992007-08-23 13:56:01 +100088 case PHY_MODE_RTBI:
89 return RGMII_FER_RTBI(input);
90 default:
91 BUG();
92 }
93}
94
95int __devinit rgmii_attach(struct of_device *ofdev, int input, int mode)
96{
97 struct rgmii_instance *dev = dev_get_drvdata(&ofdev->dev);
Al Viroeb4d84f2007-10-14 19:36:10 +010098 struct rgmii_regs __iomem *p = dev->base;
David Gibson1d3bb992007-08-23 13:56:01 +100099
100 RGMII_DBG(dev, "attach(%d)" NL, input);
101
102 /* Check if we need to attach to a RGMII */
103 if (input < 0 || !rgmii_valid_mode(mode)) {
104 printk(KERN_ERR "%s: unsupported settings !\n",
105 ofdev->node->full_name);
106 return -ENODEV;
107 }
108
109 mutex_lock(&dev->lock);
110
111 /* Enable this input */
112 out_be32(&p->fer, in_be32(&p->fer) | rgmii_mode_mask(mode, input));
113
114 printk(KERN_NOTICE "%s: input %d in %s mode\n",
115 ofdev->node->full_name, input, rgmii_mode_name(mode));
116
117 ++dev->users;
118
119 mutex_unlock(&dev->lock);
120
121 return 0;
122}
123
124void rgmii_set_speed(struct of_device *ofdev, int input, int speed)
125{
126 struct rgmii_instance *dev = dev_get_drvdata(&ofdev->dev);
Al Viroeb4d84f2007-10-14 19:36:10 +0100127 struct rgmii_regs __iomem *p = dev->base;
David Gibson1d3bb992007-08-23 13:56:01 +1000128 u32 ssr;
129
130 mutex_lock(&dev->lock);
131
132 ssr = in_be32(&p->ssr) & ~RGMII_SSR_MASK(input);
133
134 RGMII_DBG(dev, "speed(%d, %d)" NL, input, speed);
135
136 if (speed == SPEED_1000)
137 ssr |= RGMII_SSR_1000(input);
138 else if (speed == SPEED_100)
139 ssr |= RGMII_SSR_100(input);
140
141 out_be32(&p->ssr, ssr);
142
143 mutex_unlock(&dev->lock);
144}
145
146void rgmii_get_mdio(struct of_device *ofdev, int input)
147{
148 struct rgmii_instance *dev = dev_get_drvdata(&ofdev->dev);
Al Viroeb4d84f2007-10-14 19:36:10 +0100149 struct rgmii_regs __iomem *p = dev->base;
David Gibson1d3bb992007-08-23 13:56:01 +1000150 u32 fer;
151
152 RGMII_DBG2(dev, "get_mdio(%d)" NL, input);
153
Benjamin Herrenschmidt1f578772007-12-05 11:14:28 +1100154 if (!(dev->flags & EMAC_RGMII_FLAG_HAS_MDIO))
David Gibson1d3bb992007-08-23 13:56:01 +1000155 return;
156
157 mutex_lock(&dev->lock);
158
159 fer = in_be32(&p->fer);
160 fer |= 0x00080000u >> input;
161 out_be32(&p->fer, fer);
162 (void)in_be32(&p->fer);
163
164 DBG2(dev, " fer = 0x%08x\n", fer);
165}
166
167void rgmii_put_mdio(struct of_device *ofdev, int input)
168{
169 struct rgmii_instance *dev = dev_get_drvdata(&ofdev->dev);
Al Viroeb4d84f2007-10-14 19:36:10 +0100170 struct rgmii_regs __iomem *p = dev->base;
David Gibson1d3bb992007-08-23 13:56:01 +1000171 u32 fer;
172
173 RGMII_DBG2(dev, "put_mdio(%d)" NL, input);
174
Benjamin Herrenschmidt1f578772007-12-05 11:14:28 +1100175 if (!(dev->flags & EMAC_RGMII_FLAG_HAS_MDIO))
David Gibson1d3bb992007-08-23 13:56:01 +1000176 return;
177
178 fer = in_be32(&p->fer);
179 fer &= ~(0x00080000u >> input);
180 out_be32(&p->fer, fer);
181 (void)in_be32(&p->fer);
182
183 DBG2(dev, " fer = 0x%08x\n", fer);
184
185 mutex_unlock(&dev->lock);
186}
187
Josh Boyer51d4a1c2008-04-22 10:46:43 +1000188void rgmii_detach(struct of_device *ofdev, int input)
David Gibson1d3bb992007-08-23 13:56:01 +1000189{
190 struct rgmii_instance *dev = dev_get_drvdata(&ofdev->dev);
Al Viroeb4d84f2007-10-14 19:36:10 +0100191 struct rgmii_regs __iomem *p = dev->base;
David Gibson1d3bb992007-08-23 13:56:01 +1000192
193 mutex_lock(&dev->lock);
194
195 BUG_ON(!dev || dev->users == 0);
196
197 RGMII_DBG(dev, "detach(%d)" NL, input);
198
199 /* Disable this input */
200 out_be32(&p->fer, in_be32(&p->fer) & ~RGMII_FER_MASK(input));
201
202 --dev->users;
203
204 mutex_unlock(&dev->lock);
205}
206
207int rgmii_get_regs_len(struct of_device *ofdev)
208{
209 return sizeof(struct emac_ethtool_regs_subhdr) +
210 sizeof(struct rgmii_regs);
211}
212
213void *rgmii_dump_regs(struct of_device *ofdev, void *buf)
214{
215 struct rgmii_instance *dev = dev_get_drvdata(&ofdev->dev);
216 struct emac_ethtool_regs_subhdr *hdr = buf;
217 struct rgmii_regs *regs = (struct rgmii_regs *)(hdr + 1);
218
219 hdr->version = 0;
220 hdr->index = 0; /* for now, are there chips with more than one
221 * rgmii ? if yes, then we'll add a cell_index
222 * like we do for emac
223 */
224 memcpy_fromio(regs, dev->base, sizeof(struct rgmii_regs));
225 return regs + 1;
226}
227
228
229static int __devinit rgmii_probe(struct of_device *ofdev,
230 const struct of_device_id *match)
231{
232 struct device_node *np = ofdev->node;
233 struct rgmii_instance *dev;
234 struct resource regs;
235 int rc;
236
237 rc = -ENOMEM;
238 dev = kzalloc(sizeof(struct rgmii_instance), GFP_KERNEL);
239 if (dev == NULL) {
240 printk(KERN_ERR "%s: could not allocate RGMII device!\n",
241 np->full_name);
242 goto err_gone;
243 }
244
245 mutex_init(&dev->lock);
246 dev->ofdev = ofdev;
247
248 rc = -ENXIO;
249 if (of_address_to_resource(np, 0, &regs)) {
250 printk(KERN_ERR "%s: Can't get registers address\n",
251 np->full_name);
252 goto err_free;
253 }
254
255 rc = -ENOMEM;
Al Viroeb4d84f2007-10-14 19:36:10 +0100256 dev->base = (struct rgmii_regs __iomem *)ioremap(regs.start,
David Gibson1d3bb992007-08-23 13:56:01 +1000257 sizeof(struct rgmii_regs));
258 if (dev->base == NULL) {
259 printk(KERN_ERR "%s: Can't map device registers!\n",
260 np->full_name);
261 goto err_free;
262 }
263
Benjamin Herrenschmidt1f578772007-12-05 11:14:28 +1100264 /* Check for RGMII flags */
265 if (of_get_property(ofdev->node, "has-mdio", NULL))
266 dev->flags |= EMAC_RGMII_FLAG_HAS_MDIO;
267
268 /* CAB lacks the right properties, fix this up */
Al Viro97be91c2007-10-14 05:50:12 +0100269 if (of_device_is_compatible(ofdev->node, "ibm,rgmii-axon"))
Benjamin Herrenschmidt1f578772007-12-05 11:14:28 +1100270 dev->flags |= EMAC_RGMII_FLAG_HAS_MDIO;
David Gibson1d3bb992007-08-23 13:56:01 +1000271
272 DBG2(dev, " Boot FER = 0x%08x, SSR = 0x%08x\n",
273 in_be32(&dev->base->fer), in_be32(&dev->base->ssr));
274
275 /* Disable all inputs by default */
276 out_be32(&dev->base->fer, 0);
277
278 printk(KERN_INFO
Benjamin Herrenschmidt1f578772007-12-05 11:14:28 +1100279 "RGMII %s initialized with%s MDIO support\n",
280 ofdev->node->full_name,
281 (dev->flags & EMAC_RGMII_FLAG_HAS_MDIO) ? "" : "out");
David Gibson1d3bb992007-08-23 13:56:01 +1000282
283 wmb();
284 dev_set_drvdata(&ofdev->dev, dev);
285
286 return 0;
287
288 err_free:
289 kfree(dev);
290 err_gone:
291 return rc;
292}
293
294static int __devexit rgmii_remove(struct of_device *ofdev)
295{
296 struct rgmii_instance *dev = dev_get_drvdata(&ofdev->dev);
297
298 dev_set_drvdata(&ofdev->dev, NULL);
299
300 WARN_ON(dev->users != 0);
301
302 iounmap(dev->base);
303 kfree(dev);
304
305 return 0;
306}
307
308static struct of_device_id rgmii_match[] =
309{
310 {
David Gibson1d3bb992007-08-23 13:56:01 +1000311 .compatible = "ibm,rgmii",
312 },
313 {
314 .type = "emac-rgmii",
315 },
316 {},
317};
318
319static struct of_platform_driver rgmii_driver = {
320 .name = "emac-rgmii",
321 .match_table = rgmii_match,
322
323 .probe = rgmii_probe,
324 .remove = rgmii_remove,
325};
326
327int __init rgmii_init(void)
328{
329 return of_register_platform_driver(&rgmii_driver);
330}
331
332void rgmii_exit(void)
333{
334 of_unregister_platform_driver(&rgmii_driver);
335}