2 * drivers/net/ibm_newemac/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>
33 #define ZMII_FER_MDI(idx) (0x80000000 >> ((idx) * 4))
34 #define ZMII_FER_MDI_ALL (ZMII_FER_MDI(0) | ZMII_FER_MDI(1) | \
35 ZMII_FER_MDI(2) | ZMII_FER_MDI(3))
37 #define ZMII_FER_SMII(idx) (0x40000000 >> ((idx) * 4))
38 #define ZMII_FER_RMII(idx) (0x20000000 >> ((idx) * 4))
39 #define ZMII_FER_MII(idx) (0x10000000 >> ((idx) * 4))
42 #define ZMII_SSR_SCI(idx) (0x40000000 >> ((idx) * 4))
43 #define ZMII_SSR_FSS(idx) (0x20000000 >> ((idx) * 4))
44 #define ZMII_SSR_SP(idx) (0x10000000 >> ((idx) * 4))
46 /* ZMII only supports MII, RMII and SMII
47 * we also support autodetection for backward compatibility
49 static inline int zmii_valid_mode(int mode)
51 return mode == PHY_MODE_MII ||
52 mode == PHY_MODE_RMII ||
53 mode == PHY_MODE_SMII ||
57 static inline const char *zmii_mode_name(int mode)
71 static inline u32 zmii_mode_mask(int mode, int input)
75 return ZMII_FER_MII(input);
77 return ZMII_FER_RMII(input);
79 return ZMII_FER_SMII(input);
85 int __devinit zmii_attach(struct of_device *ofdev, int input, int *mode)
87 struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
88 struct zmii_regs __iomem *p = dev->base;
90 ZMII_DBG(dev, "init(%d, %d)" NL, input, *mode);
92 if (!zmii_valid_mode(*mode)) {
93 /* Probably an EMAC connected to RGMII,
94 * but it still may need ZMII for MDIO so
101 mutex_lock(&dev->lock);
103 /* Autodetect ZMII mode if not specified.
104 * This is only for backward compatibility with the old driver.
105 * Please, always specify PHY mode in your board port to avoid
108 if (dev->mode == PHY_MODE_NA) {
109 if (*mode == PHY_MODE_NA) {
110 u32 r = dev->fer_save;
112 ZMII_DBG(dev, "autodetecting mode, FER = 0x%08x" NL, r);
114 if (r & (ZMII_FER_MII(0) | ZMII_FER_MII(1)))
115 dev->mode = PHY_MODE_MII;
116 else if (r & (ZMII_FER_RMII(0) | ZMII_FER_RMII(1)))
117 dev->mode = PHY_MODE_RMII;
119 dev->mode = PHY_MODE_SMII;
123 printk(KERN_NOTICE "%s: bridge in %s mode\n",
124 ofdev->node->full_name, zmii_mode_name(dev->mode));
126 /* All inputs must use the same mode */
127 if (*mode != PHY_MODE_NA && *mode != dev->mode) {
129 "%s: invalid mode %d specified for input %d\n",
130 ofdev->node->full_name, *mode, input);
131 mutex_unlock(&dev->lock);
136 /* Report back correct PHY mode,
137 * it may be used during PHY initialization.
141 /* Enable this input */
142 out_be32(&p->fer, in_be32(&p->fer) | zmii_mode_mask(dev->mode, input));
145 mutex_unlock(&dev->lock);
150 void zmii_get_mdio(struct of_device *ofdev, int input)
152 struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
155 ZMII_DBG2(dev, "get_mdio(%d)" NL, input);
157 mutex_lock(&dev->lock);
159 fer = in_be32(&dev->base->fer) & ~ZMII_FER_MDI_ALL;
160 out_be32(&dev->base->fer, fer | ZMII_FER_MDI(input));
163 void zmii_put_mdio(struct of_device *ofdev, int input)
165 struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
167 ZMII_DBG2(dev, "put_mdio(%d)" NL, input);
168 mutex_unlock(&dev->lock);
172 void zmii_set_speed(struct of_device *ofdev, int input, int speed)
174 struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
177 mutex_lock(&dev->lock);
179 ssr = in_be32(&dev->base->ssr);
181 ZMII_DBG(dev, "speed(%d, %d)" NL, input, speed);
183 if (speed == SPEED_100)
184 ssr |= ZMII_SSR_SP(input);
186 ssr &= ~ZMII_SSR_SP(input);
188 out_be32(&dev->base->ssr, ssr);
190 mutex_unlock(&dev->lock);
193 void zmii_detach(struct of_device *ofdev, int input)
195 struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
197 BUG_ON(!dev || dev->users == 0);
199 mutex_lock(&dev->lock);
201 ZMII_DBG(dev, "detach(%d)" NL, input);
203 /* Disable this input */
204 out_be32(&dev->base->fer,
205 in_be32(&dev->base->fer) & ~zmii_mode_mask(dev->mode, input));
209 mutex_unlock(&dev->lock);
212 int zmii_get_regs_len(struct of_device *ofdev)
214 return sizeof(struct emac_ethtool_regs_subhdr) +
215 sizeof(struct zmii_regs);
218 void *zmii_dump_regs(struct of_device *ofdev, void *buf)
220 struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
221 struct emac_ethtool_regs_subhdr *hdr = buf;
222 struct zmii_regs *regs = (struct zmii_regs *)(hdr + 1);
225 hdr->index = 0; /* for now, are there chips with more than one
226 * zmii ? if yes, then we'll add a cell_index
227 * like we do for emac
229 memcpy_fromio(regs, dev->base, sizeof(struct zmii_regs));
233 static int __devinit zmii_probe(struct of_device *ofdev,
234 const struct of_device_id *match)
236 struct device_node *np = ofdev->node;
237 struct zmii_instance *dev;
238 struct resource regs;
242 dev = kzalloc(sizeof(struct zmii_instance), GFP_KERNEL);
244 printk(KERN_ERR "%s: could not allocate ZMII device!\n",
249 mutex_init(&dev->lock);
251 dev->mode = PHY_MODE_NA;
254 if (of_address_to_resource(np, 0, ®s)) {
255 printk(KERN_ERR "%s: Can't get registers address\n",
261 dev->base = (struct zmii_regs __iomem *)ioremap(regs.start,
262 sizeof(struct zmii_regs));
263 if (dev->base == NULL) {
264 printk(KERN_ERR "%s: Can't map device registers!\n",
269 /* We may need FER value for autodetection later */
270 dev->fer_save = in_be32(&dev->base->fer);
272 /* Disable all inputs by default */
273 out_be32(&dev->base->fer, 0);
276 "ZMII %s initialized\n", ofdev->node->full_name);
278 dev_set_drvdata(&ofdev->dev, dev);
288 static int __devexit zmii_remove(struct of_device *ofdev)
290 struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
292 dev_set_drvdata(&ofdev->dev, NULL);
294 WARN_ON(dev->users != 0);
302 static struct of_device_id zmii_match[] =
305 .compatible = "ibm,zmii",
307 /* For backward compat with old DT */
314 static struct of_platform_driver zmii_driver = {
316 .match_table = zmii_match,
319 .remove = zmii_remove,
322 int __init zmii_init(void)
324 return of_register_platform_driver(&zmii_driver);
329 of_unregister_platform_driver(&zmii_driver);