2 * Combined Ethernet driver for Motorola MPC8xx and MPC82xx.
4 * Copyright (c) 2003 Intracom S.A.
5 * by Pantelis Antoniou <panto@intracom.gr>
7 * 2005 (c) MontaVista Software, Inc.
8 * Vitaly Bordug <vbordug@ru.mvista.com>
10 * This file is licensed under the terms of the GNU General Public License
11 * version 2. This program is licensed "as is" without any warranty of any
12 * kind, whether express or implied.
16 #include <linux/module.h>
17 #include <linux/types.h>
18 #include <linux/kernel.h>
19 #include <linux/sched.h>
20 #include <linux/string.h>
21 #include <linux/ptrace.h>
22 #include <linux/errno.h>
23 #include <linux/ioport.h>
24 #include <linux/slab.h>
25 #include <linux/interrupt.h>
26 #include <linux/pci.h>
27 #include <linux/init.h>
28 #include <linux/delay.h>
29 #include <linux/netdevice.h>
30 #include <linux/etherdevice.h>
31 #include <linux/skbuff.h>
32 #include <linux/spinlock.h>
33 #include <linux/mii.h>
34 #include <linux/ethtool.h>
35 #include <linux/bitops.h>
36 #include <linux/platform_device.h>
38 #include <asm/pgtable.h>
40 #include <asm/uaccess.h>
44 static int bitbang_prep_bit(u8 **datp, u8 *mskp,
45 struct fs_mii_bit *mii_bit)
51 dat = (void*) mii_bit->offset;
53 adv = mii_bit->bit >> 3;
54 dat = (char *)dat + adv;
56 msk = 1 << (7 - (mii_bit->bit & 7));
64 static inline void bb_set(u8 *p, u8 m)
66 out_8(p, in_8(p) | m);
69 static inline void bb_clr(u8 *p, u8 m)
71 out_8(p, in_8(p) & ~m);
74 static inline int bb_read(u8 *p, u8 m)
76 return (in_8(p) & m) != 0;
79 static inline void mdio_active(struct bb_info *bitbang)
81 bb_set(bitbang->mdio_dir, bitbang->mdio_dir_msk);
84 static inline void mdio_tristate(struct bb_info *bitbang )
86 bb_clr(bitbang->mdio_dir, bitbang->mdio_dir_msk);
89 static inline int mdio_read(struct bb_info *bitbang )
91 return bb_read(bitbang->mdio_dat, bitbang->mdio_dat_msk);
94 static inline void mdio(struct bb_info *bitbang , int what)
97 bb_set(bitbang->mdio_dat, bitbang->mdio_dat_msk);
99 bb_clr(bitbang->mdio_dat, bitbang->mdio_dat_msk);
102 static inline void mdc(struct bb_info *bitbang , int what)
105 bb_set(bitbang->mdc_dat, bitbang->mdc_msk);
107 bb_clr(bitbang->mdc_dat, bitbang->mdc_msk);
110 static inline void mii_delay(struct bb_info *bitbang )
112 udelay(bitbang->delay);
115 /* Utility to send the preamble, address, and register (common to read and write). */
116 static void bitbang_pre(struct bb_info *bitbang , int read, u8 addr, u8 reg)
121 * Send a 32 bit preamble ('1's) with an extra '1' bit for good measure.
122 * The IEEE spec says this is a PHY optional requirement. The AMD
123 * 79C874 requires one after power up and one after a MII communications
124 * error. This means that we are doing more preambles than we need,
125 * but it is safer and will be much more robust.
128 mdio_active(bitbang);
130 for (j = 0; j < 32; j++) {
137 /* send the start bit (01) and the read opcode (10) or write (10) */
154 mdio(bitbang, !read);
159 /* send the PHY address */
160 for (j = 0; j < 5; j++) {
162 mdio(bitbang, (addr & 0x10) != 0);
169 /* send the register address */
170 for (j = 0; j < 5; j++) {
172 mdio(bitbang, (reg & 0x10) != 0);
180 static int fs_enet_mii_bb_read(struct mii_bus *bus , int phy_id, int location)
184 u8 addr = phy_id & 0xff;
185 u8 reg = location & 0xff;
186 struct bb_info* bitbang = bus->priv;
188 bitbang_pre(bitbang, 1, addr, reg);
190 /* tri-state our MDIO I/O pin so we can read */
192 mdio_tristate(bitbang);
197 /* check the turnaround bit: the PHY should be driving it to zero */
198 if (mdio_read(bitbang) != 0) {
199 /* PHY didn't drive TA low */
200 for (j = 0; j < 32; j++) {
213 /* read 16 bits of register data, MSB first */
215 for (j = 0; j < 16; j++) {
219 rdreg |= mdio_read(bitbang);
236 static int fs_enet_mii_bb_write(struct mii_bus *bus, int phy_id, int location, u16 val)
239 struct bb_info* bitbang = bus->priv;
241 u8 addr = phy_id & 0xff;
242 u8 reg = location & 0xff;
243 u16 value = val & 0xffff;
245 bitbang_pre(bitbang, 0, addr, reg);
247 /* send the turnaround (10) */
259 /* write 16 bits of register data, MSB first */
260 for (j = 0; j < 16; j++) {
262 mdio(bitbang, (value & 0x8000) != 0);
270 * Tri-state the MDIO line.
272 mdio_tristate(bitbang);
280 static int fs_enet_mii_bb_reset(struct mii_bus *bus)
282 /*nothing here - dunno how to reset it*/
286 static int fs_mii_bitbang_init(struct bb_info *bitbang, struct fs_mii_bb_platform_info* fmpi)
290 bitbang->delay = fmpi->delay;
292 r = bitbang_prep_bit(&bitbang->mdio_dir,
293 &bitbang->mdio_dir_msk,
298 r = bitbang_prep_bit(&bitbang->mdio_dat,
299 &bitbang->mdio_dat_msk,
304 r = bitbang_prep_bit(&bitbang->mdc_dat,
314 static int __devinit fs_enet_mdio_probe(struct device *dev)
316 struct platform_device *pdev = to_platform_device(dev);
317 struct fs_mii_bb_platform_info *pdata;
318 struct mii_bus *new_bus;
319 struct bb_info *bitbang;
325 new_bus = kzalloc(sizeof(struct mii_bus), GFP_KERNEL);
330 bitbang = kzalloc(sizeof(struct bb_info), GFP_KERNEL);
335 new_bus->name = "BB MII Bus",
336 new_bus->read = &fs_enet_mii_bb_read,
337 new_bus->write = &fs_enet_mii_bb_write,
338 new_bus->reset = &fs_enet_mii_bb_reset,
339 new_bus->id = pdev->id;
341 new_bus->phy_mask = ~0x9;
342 pdata = (struct fs_mii_bb_platform_info *)pdev->dev.platform_data;
345 printk(KERN_ERR "gfar mdio %d: Missing platform data!\n", pdev->id);
350 fs_mii_bitbang_init(bitbang, pdata);
352 new_bus->priv = bitbang;
354 new_bus->irq = pdata->irq;
357 dev_set_drvdata(dev, new_bus);
359 err = mdiobus_register(new_bus);
362 printk (KERN_ERR "%s: Cannot register as MDIO bus\n",
364 goto bus_register_fail;
377 static int fs_enet_mdio_remove(struct device *dev)
379 struct mii_bus *bus = dev_get_drvdata(dev);
381 mdiobus_unregister(bus);
383 dev_set_drvdata(dev, NULL);
385 iounmap((void *) (&bus->priv));
392 static struct device_driver fs_enet_bb_mdio_driver = {
393 .name = "fsl-bb-mdio",
394 .bus = &platform_bus_type,
395 .probe = fs_enet_mdio_probe,
396 .remove = fs_enet_mdio_remove,
399 int fs_enet_mdio_bb_init(void)
401 return driver_register(&fs_enet_bb_mdio_driver);
404 void fs_enet_mdio_bb_exit(void)
406 driver_unregister(&fs_enet_bb_mdio_driver);