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
93 mutex_lock(&dev->lock);
95 /* Autodetect ZMII mode if not specified.
96 * This is only for backward compatibility with the old driver.
97 * Please, always specify PHY mode in your board port to avoid
100 if (dev->mode == PHY_MODE_NA) {
101 if (*mode == PHY_MODE_NA) {
102 u32 r = dev->fer_save;
104 ZMII_DBG(dev, "autodetecting mode, FER = 0x%08x" NL, r);
106 if (r & (ZMII_FER_MII(0) | ZMII_FER_MII(1)))
107 dev->mode = PHY_MODE_MII;
108 else if (r & (ZMII_FER_RMII(0) | ZMII_FER_RMII(1)))
109 dev->mode = PHY_MODE_RMII;
111 dev->mode = PHY_MODE_SMII;
115 printk(KERN_NOTICE "%s: bridge in %s mode\n",
116 ofdev->node->full_name, zmii_mode_name(dev->mode));
118 /* All inputs must use the same mode */
119 if (*mode != PHY_MODE_NA && *mode != dev->mode) {
121 "%s: invalid mode %d specified for input %d\n",
122 ofdev->node->full_name, *mode, input);
123 mutex_unlock(&dev->lock);
128 /* Report back correct PHY mode,
129 * it may be used during PHY initialization.
133 /* Enable this input */
134 out_be32(&p->fer, in_be32(&p->fer) | zmii_mode_mask(dev->mode, input));
137 mutex_unlock(&dev->lock);
142 void zmii_get_mdio(struct of_device *ofdev, int input)
144 struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
147 ZMII_DBG2(dev, "get_mdio(%d)" NL, input);
149 mutex_lock(&dev->lock);
151 fer = in_be32(&dev->base->fer) & ~ZMII_FER_MDI_ALL;
152 out_be32(&dev->base->fer, fer | ZMII_FER_MDI(input));
155 void zmii_put_mdio(struct of_device *ofdev, int input)
157 struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
159 ZMII_DBG2(dev, "put_mdio(%d)" NL, input);
160 mutex_unlock(&dev->lock);
164 void zmii_set_speed(struct of_device *ofdev, int input, int speed)
166 struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
169 mutex_lock(&dev->lock);
171 ssr = in_be32(&dev->base->ssr);
173 ZMII_DBG(dev, "speed(%d, %d)" NL, input, speed);
175 if (speed == SPEED_100)
176 ssr |= ZMII_SSR_SP(input);
178 ssr &= ~ZMII_SSR_SP(input);
180 out_be32(&dev->base->ssr, ssr);
182 mutex_unlock(&dev->lock);
185 void __devexit zmii_detach(struct of_device *ofdev, int input)
187 struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
189 BUG_ON(!dev || dev->users == 0);
191 mutex_lock(&dev->lock);
193 ZMII_DBG(dev, "detach(%d)" NL, input);
195 /* Disable this input */
196 out_be32(&dev->base->fer,
197 in_be32(&dev->base->fer) & ~zmii_mode_mask(dev->mode, input));
201 mutex_unlock(&dev->lock);
204 int zmii_get_regs_len(struct of_device *ofdev)
206 return sizeof(struct emac_ethtool_regs_subhdr) +
207 sizeof(struct zmii_regs);
210 void *zmii_dump_regs(struct of_device *ofdev, void *buf)
212 struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
213 struct emac_ethtool_regs_subhdr *hdr = buf;
214 struct zmii_regs *regs = (struct zmii_regs *)(hdr + 1);
217 hdr->index = 0; /* for now, are there chips with more than one
218 * zmii ? if yes, then we'll add a cell_index
219 * like we do for emac
221 memcpy_fromio(regs, dev->base, sizeof(struct zmii_regs));
225 static int __devinit zmii_probe(struct of_device *ofdev,
226 const struct of_device_id *match)
228 struct device_node *np = ofdev->node;
229 struct zmii_instance *dev;
230 struct resource regs;
234 dev = kzalloc(sizeof(struct zmii_instance), GFP_KERNEL);
236 printk(KERN_ERR "%s: could not allocate ZMII device!\n",
241 mutex_init(&dev->lock);
243 dev->mode = PHY_MODE_NA;
246 if (of_address_to_resource(np, 0, ®s)) {
247 printk(KERN_ERR "%s: Can't get registers address\n",
253 dev->base = (struct zmii_regs __iomem *)ioremap(regs.start,
254 sizeof(struct zmii_regs));
255 if (dev->base == NULL) {
256 printk(KERN_ERR "%s: Can't map device registers!\n",
261 /* We may need FER value for autodetection later */
262 dev->fer_save = in_be32(&dev->base->fer);
264 /* Disable all inputs by default */
265 out_be32(&dev->base->fer, 0);
268 "ZMII %s initialized\n", ofdev->node->full_name);
270 dev_set_drvdata(&ofdev->dev, dev);
280 static int __devexit zmii_remove(struct of_device *ofdev)
282 struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
284 dev_set_drvdata(&ofdev->dev, NULL);
286 WARN_ON(dev->users != 0);
294 static struct of_device_id zmii_match[] =
297 .compatible = "ibm,zmii",
299 /* For backward compat with old DT */
306 static struct of_platform_driver zmii_driver = {
308 .match_table = zmii_match,
311 .remove = zmii_remove,
314 int __init zmii_init(void)
316 return of_register_platform_driver(&zmii_driver);
321 of_unregister_platform_driver(&zmii_driver);