Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / net / ethernet / ibm / emac / zmii.c
blobfdcc734541feaa288d5bb1cc4ec5333c470d7658
1 /*
2 * drivers/net/ethernet/ibm/emac/zmii.c
4 * Driver for PowerPC 4xx on-chip ethernet controller, ZMII 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 * Armin Kuster <akuster@mvista.com>
16 * Copyright 2001 MontaVista Softare 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 "core.h"
33 /* ZMIIx_FER */
34 #define ZMII_FER_MDI(idx) (0x80000000 >> ((idx) * 4))
35 #define ZMII_FER_MDI_ALL (ZMII_FER_MDI(0) | ZMII_FER_MDI(1) | \
36 ZMII_FER_MDI(2) | ZMII_FER_MDI(3))
38 #define ZMII_FER_SMII(idx) (0x40000000 >> ((idx) * 4))
39 #define ZMII_FER_RMII(idx) (0x20000000 >> ((idx) * 4))
40 #define ZMII_FER_MII(idx) (0x10000000 >> ((idx) * 4))
42 /* ZMIIx_SSR */
43 #define ZMII_SSR_SCI(idx) (0x40000000 >> ((idx) * 4))
44 #define ZMII_SSR_FSS(idx) (0x20000000 >> ((idx) * 4))
45 #define ZMII_SSR_SP(idx) (0x10000000 >> ((idx) * 4))
47 /* ZMII only supports MII, RMII and SMII
48 * we also support autodetection for backward compatibility
50 static inline int zmii_valid_mode(int mode)
52 return mode == PHY_INTERFACE_MODE_MII ||
53 mode == PHY_INTERFACE_MODE_RMII ||
54 mode == PHY_INTERFACE_MODE_SMII ||
55 mode == PHY_INTERFACE_MODE_NA;
58 static inline const char *zmii_mode_name(int mode)
60 switch (mode) {
61 case PHY_INTERFACE_MODE_MII:
62 return "MII";
63 case PHY_INTERFACE_MODE_RMII:
64 return "RMII";
65 case PHY_INTERFACE_MODE_SMII:
66 return "SMII";
67 default:
68 BUG();
72 static inline u32 zmii_mode_mask(int mode, int input)
74 switch (mode) {
75 case PHY_INTERFACE_MODE_MII:
76 return ZMII_FER_MII(input);
77 case PHY_INTERFACE_MODE_RMII:
78 return ZMII_FER_RMII(input);
79 case PHY_INTERFACE_MODE_SMII:
80 return ZMII_FER_SMII(input);
81 default:
82 return 0;
86 int zmii_attach(struct platform_device *ofdev, int input, int *mode)
88 struct zmii_instance *dev = platform_get_drvdata(ofdev);
89 struct zmii_regs __iomem *p = dev->base;
91 ZMII_DBG(dev, "init(%d, %d)" NL, input, *mode);
93 if (!zmii_valid_mode(*mode)) {
94 /* Probably an EMAC connected to RGMII,
95 * but it still may need ZMII for MDIO so
96 * we don't fail here.
98 dev->users++;
99 return 0;
102 mutex_lock(&dev->lock);
104 /* Autodetect ZMII mode if not specified.
105 * This is only for backward compatibility with the old driver.
106 * Please, always specify PHY mode in your board port to avoid
107 * any surprises.
109 if (dev->mode == PHY_INTERFACE_MODE_NA) {
110 if (*mode == PHY_INTERFACE_MODE_NA) {
111 u32 r = dev->fer_save;
113 ZMII_DBG(dev, "autodetecting mode, FER = 0x%08x" NL, r);
115 if (r & (ZMII_FER_MII(0) | ZMII_FER_MII(1)))
116 dev->mode = PHY_INTERFACE_MODE_MII;
117 else if (r & (ZMII_FER_RMII(0) | ZMII_FER_RMII(1)))
118 dev->mode = PHY_INTERFACE_MODE_RMII;
119 else
120 dev->mode = PHY_INTERFACE_MODE_SMII;
121 } else {
122 dev->mode = *mode;
124 printk(KERN_NOTICE "%pOF: bridge in %s mode\n",
125 ofdev->dev.of_node,
126 zmii_mode_name(dev->mode));
127 } else {
128 /* All inputs must use the same mode */
129 if (*mode != PHY_INTERFACE_MODE_NA && *mode != dev->mode) {
130 printk(KERN_ERR
131 "%pOF: invalid mode %d specified for input %d\n",
132 ofdev->dev.of_node, *mode, input);
133 mutex_unlock(&dev->lock);
134 return -EINVAL;
138 /* Report back correct PHY mode,
139 * it may be used during PHY initialization.
141 *mode = dev->mode;
143 /* Enable this input */
144 out_be32(&p->fer, in_be32(&p->fer) | zmii_mode_mask(dev->mode, input));
145 ++dev->users;
147 mutex_unlock(&dev->lock);
149 return 0;
152 void zmii_get_mdio(struct platform_device *ofdev, int input)
154 struct zmii_instance *dev = platform_get_drvdata(ofdev);
155 u32 fer;
157 ZMII_DBG2(dev, "get_mdio(%d)" NL, input);
159 mutex_lock(&dev->lock);
161 fer = in_be32(&dev->base->fer) & ~ZMII_FER_MDI_ALL;
162 out_be32(&dev->base->fer, fer | ZMII_FER_MDI(input));
165 void zmii_put_mdio(struct platform_device *ofdev, int input)
167 struct zmii_instance *dev = platform_get_drvdata(ofdev);
169 ZMII_DBG2(dev, "put_mdio(%d)" NL, input);
170 mutex_unlock(&dev->lock);
174 void zmii_set_speed(struct platform_device *ofdev, int input, int speed)
176 struct zmii_instance *dev = platform_get_drvdata(ofdev);
177 u32 ssr;
179 mutex_lock(&dev->lock);
181 ssr = in_be32(&dev->base->ssr);
183 ZMII_DBG(dev, "speed(%d, %d)" NL, input, speed);
185 if (speed == SPEED_100)
186 ssr |= ZMII_SSR_SP(input);
187 else
188 ssr &= ~ZMII_SSR_SP(input);
190 out_be32(&dev->base->ssr, ssr);
192 mutex_unlock(&dev->lock);
195 void zmii_detach(struct platform_device *ofdev, int input)
197 struct zmii_instance *dev = platform_get_drvdata(ofdev);
199 BUG_ON(!dev || dev->users == 0);
201 mutex_lock(&dev->lock);
203 ZMII_DBG(dev, "detach(%d)" NL, input);
205 /* Disable this input */
206 out_be32(&dev->base->fer,
207 in_be32(&dev->base->fer) & ~zmii_mode_mask(dev->mode, input));
209 --dev->users;
211 mutex_unlock(&dev->lock);
214 int zmii_get_regs_len(struct platform_device *ofdev)
216 return sizeof(struct emac_ethtool_regs_subhdr) +
217 sizeof(struct zmii_regs);
220 void *zmii_dump_regs(struct platform_device *ofdev, void *buf)
222 struct zmii_instance *dev = platform_get_drvdata(ofdev);
223 struct emac_ethtool_regs_subhdr *hdr = buf;
224 struct zmii_regs *regs = (struct zmii_regs *)(hdr + 1);
226 hdr->version = 0;
227 hdr->index = 0; /* for now, are there chips with more than one
228 * zmii ? if yes, then we'll add a cell_index
229 * like we do for emac
231 memcpy_fromio(regs, dev->base, sizeof(struct zmii_regs));
232 return regs + 1;
235 static int zmii_probe(struct platform_device *ofdev)
237 struct device_node *np = ofdev->dev.of_node;
238 struct zmii_instance *dev;
239 struct resource regs;
240 int rc;
242 rc = -ENOMEM;
243 dev = kzalloc(sizeof(struct zmii_instance), GFP_KERNEL);
244 if (dev == NULL)
245 goto err_gone;
247 mutex_init(&dev->lock);
248 dev->ofdev = ofdev;
249 dev->mode = PHY_INTERFACE_MODE_NA;
251 rc = -ENXIO;
252 if (of_address_to_resource(np, 0, &regs)) {
253 printk(KERN_ERR "%pOF: Can't get registers address\n", np);
254 goto err_free;
257 rc = -ENOMEM;
258 dev->base = (struct zmii_regs __iomem *)ioremap(regs.start,
259 sizeof(struct zmii_regs));
260 if (dev->base == NULL) {
261 printk(KERN_ERR "%pOF: Can't map device registers!\n", np);
262 goto err_free;
265 /* We may need FER value for autodetection later */
266 dev->fer_save = in_be32(&dev->base->fer);
268 /* Disable all inputs by default */
269 out_be32(&dev->base->fer, 0);
271 printk(KERN_INFO "ZMII %pOF initialized\n", ofdev->dev.of_node);
272 wmb();
273 platform_set_drvdata(ofdev, dev);
275 return 0;
277 err_free:
278 kfree(dev);
279 err_gone:
280 return rc;
283 static int zmii_remove(struct platform_device *ofdev)
285 struct zmii_instance *dev = platform_get_drvdata(ofdev);
287 WARN_ON(dev->users != 0);
289 iounmap(dev->base);
290 kfree(dev);
292 return 0;
295 static const struct of_device_id zmii_match[] =
298 .compatible = "ibm,zmii",
300 /* For backward compat with old DT */
302 .type = "emac-zmii",
307 static struct platform_driver zmii_driver = {
308 .driver = {
309 .name = "emac-zmii",
310 .of_match_table = zmii_match,
312 .probe = zmii_probe,
313 .remove = zmii_remove,
316 int __init zmii_init(void)
318 return platform_driver_register(&zmii_driver);
321 void zmii_exit(void)
323 platform_driver_unregister(&zmii_driver);