2 * drivers/net/ibm_newemac/zmii.c
4 * Driver for PowerPC 4xx on-chip ethernet controller, ZMII bridge support.
6 * Copyright (c) 2004, 2005 Zultys Technologies.
7 * Eugene Surovegin <eugene.surovegin@zultys.com> or <ebs@ebshome.net>
9 * Based on original work by
10 * Armin Kuster <akuster@mvista.com>
11 * Copyright 2001 MontaVista Softare Inc.
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
19 #include <linux/kernel.h>
20 #include <linux/ethtool.h>
27 #define ZMII_FER_MDI(idx) (0x80000000 >> ((idx) * 4))
28 #define ZMII_FER_MDI_ALL (ZMII_FER_MDI(0) | ZMII_FER_MDI(1) | \
29 ZMII_FER_MDI(2) | ZMII_FER_MDI(3))
31 #define ZMII_FER_SMII(idx) (0x40000000 >> ((idx) * 4))
32 #define ZMII_FER_RMII(idx) (0x20000000 >> ((idx) * 4))
33 #define ZMII_FER_MII(idx) (0x10000000 >> ((idx) * 4))
36 #define ZMII_SSR_SCI(idx) (0x40000000 >> ((idx) * 4))
37 #define ZMII_SSR_FSS(idx) (0x20000000 >> ((idx) * 4))
38 #define ZMII_SSR_SP(idx) (0x10000000 >> ((idx) * 4))
40 /* ZMII only supports MII, RMII and SMII
41 * we also support autodetection for backward compatibility
43 static inline int zmii_valid_mode(int mode)
45 return mode == PHY_MODE_MII ||
46 mode == PHY_MODE_RMII ||
47 mode == PHY_MODE_SMII ||
51 static inline const char *zmii_mode_name(int mode)
65 static inline u32 zmii_mode_mask(int mode, int input)
69 return ZMII_FER_MII(input);
71 return ZMII_FER_RMII(input);
73 return ZMII_FER_SMII(input);
79 int __devinit zmii_attach(struct of_device *ofdev, int input, int *mode)
81 struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
82 struct zmii_regs __iomem *p = dev->base;
84 ZMII_DBG(dev, "init(%d, %d)" NL, input, *mode);
86 if (!zmii_valid_mode(*mode)) {
87 /* Probably an EMAC connected to RGMII,
88 * but it still may need ZMII for MDIO so
95 mutex_lock(&dev->lock);
97 /* Autodetect ZMII mode if not specified.
98 * This is only for backward compatibility with the old driver.
99 * Please, always specify PHY mode in your board port to avoid
102 if (dev->mode == PHY_MODE_NA) {
103 if (*mode == PHY_MODE_NA) {
104 u32 r = dev->fer_save;
106 ZMII_DBG(dev, "autodetecting mode, FER = 0x%08x" NL, r);
108 if (r & (ZMII_FER_MII(0) | ZMII_FER_MII(1)))
109 dev->mode = PHY_MODE_MII;
110 else if (r & (ZMII_FER_RMII(0) | ZMII_FER_RMII(1)))
111 dev->mode = PHY_MODE_RMII;
113 dev->mode = PHY_MODE_SMII;
117 printk(KERN_NOTICE "%s: bridge in %s mode\n",
118 ofdev->node->full_name, zmii_mode_name(dev->mode));
120 /* All inputs must use the same mode */
121 if (*mode != PHY_MODE_NA && *mode != dev->mode) {
123 "%s: invalid mode %d specified for input %d\n",
124 ofdev->node->full_name, *mode, input);
125 mutex_unlock(&dev->lock);
130 /* Report back correct PHY mode,
131 * it may be used during PHY initialization.
135 /* Enable this input */
136 out_be32(&p->fer, in_be32(&p->fer) | zmii_mode_mask(dev->mode, input));
139 mutex_unlock(&dev->lock);
144 void zmii_get_mdio(struct of_device *ofdev, int input)
146 struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
149 ZMII_DBG2(dev, "get_mdio(%d)" NL, input);
151 mutex_lock(&dev->lock);
153 fer = in_be32(&dev->base->fer) & ~ZMII_FER_MDI_ALL;
154 out_be32(&dev->base->fer, fer | ZMII_FER_MDI(input));
157 void zmii_put_mdio(struct of_device *ofdev, int input)
159 struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
161 ZMII_DBG2(dev, "put_mdio(%d)" NL, input);
162 mutex_unlock(&dev->lock);
166 void zmii_set_speed(struct of_device *ofdev, int input, int speed)
168 struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
171 mutex_lock(&dev->lock);
173 ssr = in_be32(&dev->base->ssr);
175 ZMII_DBG(dev, "speed(%d, %d)" NL, input, speed);
177 if (speed == SPEED_100)
178 ssr |= ZMII_SSR_SP(input);
180 ssr &= ~ZMII_SSR_SP(input);
182 out_be32(&dev->base->ssr, ssr);
184 mutex_unlock(&dev->lock);
187 void __devexit zmii_detach(struct of_device *ofdev, int input)
189 struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
191 BUG_ON(!dev || dev->users == 0);
193 mutex_lock(&dev->lock);
195 ZMII_DBG(dev, "detach(%d)" NL, input);
197 /* Disable this input */
198 out_be32(&dev->base->fer,
199 in_be32(&dev->base->fer) & ~zmii_mode_mask(dev->mode, input));
203 mutex_unlock(&dev->lock);
206 int zmii_get_regs_len(struct of_device *ofdev)
208 return sizeof(struct emac_ethtool_regs_subhdr) +
209 sizeof(struct zmii_regs);
212 void *zmii_dump_regs(struct of_device *ofdev, void *buf)
214 struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
215 struct emac_ethtool_regs_subhdr *hdr = buf;
216 struct zmii_regs *regs = (struct zmii_regs *)(hdr + 1);
219 hdr->index = 0; /* for now, are there chips with more than one
220 * zmii ? if yes, then we'll add a cell_index
221 * like we do for emac
223 memcpy_fromio(regs, dev->base, sizeof(struct zmii_regs));
227 static int __devinit zmii_probe(struct of_device *ofdev,
228 const struct of_device_id *match)
230 struct device_node *np = ofdev->node;
231 struct zmii_instance *dev;
232 struct resource regs;
236 dev = kzalloc(sizeof(struct zmii_instance), GFP_KERNEL);
238 printk(KERN_ERR "%s: could not allocate ZMII device!\n",
243 mutex_init(&dev->lock);
245 dev->mode = PHY_MODE_NA;
248 if (of_address_to_resource(np, 0, ®s)) {
249 printk(KERN_ERR "%s: Can't get registers address\n",
255 dev->base = (struct zmii_regs __iomem *)ioremap(regs.start,
256 sizeof(struct zmii_regs));
257 if (dev->base == NULL) {
258 printk(KERN_ERR "%s: Can't map device registers!\n",
263 /* We may need FER value for autodetection later */
264 dev->fer_save = in_be32(&dev->base->fer);
266 /* Disable all inputs by default */
267 out_be32(&dev->base->fer, 0);
270 "ZMII %s initialized\n", ofdev->node->full_name);
272 dev_set_drvdata(&ofdev->dev, dev);
282 static int __devexit zmii_remove(struct of_device *ofdev)
284 struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
286 dev_set_drvdata(&ofdev->dev, NULL);
288 WARN_ON(dev->users != 0);
296 static struct of_device_id zmii_match[] =
299 .compatible = "ibm,zmii",
301 /* For backward compat with old DT */
308 static struct of_platform_driver zmii_driver = {
310 .match_table = zmii_match,
313 .remove = zmii_remove,
316 int __init zmii_init(void)
318 return of_register_platform_driver(&zmii_driver);
323 of_unregister_platform_driver(&zmii_driver);