]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/net/gianfar_mii.c
Merge Paulus' tree
[karo-tx-linux.git] / drivers / net / gianfar_mii.c
1 /* 
2  * drivers/net/gianfar_mii.c
3  *
4  * Gianfar Ethernet Driver -- MIIM bus implementation
5  * Provides Bus interface for MIIM regs
6  *
7  * Author: Andy Fleming
8  * Maintainer: Kumar Gala (kumar.gala@freescale.com)
9  *
10  * Copyright (c) 2002-2004 Freescale Semiconductor, Inc.
11  *
12  * This program is free software; you can redistribute  it and/or modify it
13  * under  the terms of  the GNU General  Public License as published by the
14  * Free Software Foundation;  either version 2 of the  License, or (at your
15  * option) any later version.
16  *
17  */
18
19 #include <linux/config.h>
20 #include <linux/kernel.h>
21 #include <linux/sched.h>
22 #include <linux/string.h>
23 #include <linux/errno.h>
24 #include <linux/unistd.h>
25 #include <linux/slab.h>
26 #include <linux/interrupt.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/mm.h>
34 #include <linux/module.h>
35 #include <linux/version.h>
36 #include <linux/platform_device.h>
37 #include <asm/ocp.h>
38 #include <linux/crc32.h>
39 #include <linux/mii.h>
40 #include <linux/phy.h>
41
42 #include <asm/io.h>
43 #include <asm/irq.h>
44 #include <asm/uaccess.h>
45
46 #include "gianfar.h"
47 #include "gianfar_mii.h"
48
49 /* Write value to the PHY at mii_id at register regnum,
50  * on the bus, waiting until the write is done before returning.
51  * All PHY configuration is done through the TSEC1 MIIM regs */
52 int gfar_mdio_write(struct mii_bus *bus, int mii_id, int regnum, u16 value)
53 {
54         struct gfar_mii *regs = bus->priv;
55
56         /* Set the PHY address and the register address we want to write */
57         gfar_write(&regs->miimadd, (mii_id << 8) | regnum);
58
59         /* Write out the value we want */
60         gfar_write(&regs->miimcon, value);
61
62         /* Wait for the transaction to finish */
63         while (gfar_read(&regs->miimind) & MIIMIND_BUSY)
64                 cpu_relax();
65
66         return 0;
67 }
68
69 /* Read the bus for PHY at addr mii_id, register regnum, and
70  * return the value.  Clears miimcom first.  All PHY
71  * configuration has to be done through the TSEC1 MIIM regs */
72 int gfar_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
73 {
74         struct gfar_mii *regs = bus->priv;
75         u16 value;
76
77         /* Set the PHY address and the register address we want to read */
78         gfar_write(&regs->miimadd, (mii_id << 8) | regnum);
79
80         /* Clear miimcom, and then initiate a read */
81         gfar_write(&regs->miimcom, 0);
82         gfar_write(&regs->miimcom, MII_READ_COMMAND);
83
84         /* Wait for the transaction to finish */
85         while (gfar_read(&regs->miimind) & (MIIMIND_NOTVALID | MIIMIND_BUSY))
86                 cpu_relax();
87
88         /* Grab the value of the register from miimstat */
89         value = gfar_read(&regs->miimstat);
90
91         return value;
92 }
93
94
95 /* Reset the MIIM registers, and wait for the bus to free */
96 int gfar_mdio_reset(struct mii_bus *bus)
97 {
98         struct gfar_mii *regs = bus->priv;
99         unsigned int timeout = PHY_INIT_TIMEOUT;
100
101         spin_lock_bh(&bus->mdio_lock);
102
103         /* Reset the management interface */
104         gfar_write(&regs->miimcfg, MIIMCFG_RESET);
105
106         /* Setup the MII Mgmt clock speed */
107         gfar_write(&regs->miimcfg, MIIMCFG_INIT_VALUE);
108
109         /* Wait until the bus is free */
110         while ((gfar_read(&regs->miimind) & MIIMIND_BUSY) &&
111                         timeout--)
112                 cpu_relax();
113
114         spin_unlock_bh(&bus->mdio_lock);
115
116         if(timeout <= 0) {
117                 printk(KERN_ERR "%s: The MII Bus is stuck!\n",
118                                 bus->name);
119                 return -EBUSY;
120         }
121
122         return 0;
123 }
124
125
126 int gfar_mdio_probe(struct device *dev)
127 {
128         struct platform_device *pdev = to_platform_device(dev);
129         struct gianfar_mdio_data *pdata;
130         struct gfar_mii *regs;
131         struct mii_bus *new_bus;
132         int err = 0;
133
134         if (NULL == dev)
135                 return -EINVAL;
136
137         new_bus = kmalloc(sizeof(struct mii_bus), GFP_KERNEL);
138
139         if (NULL == new_bus)
140                 return -ENOMEM;
141
142         new_bus->name = "Gianfar MII Bus",
143         new_bus->read = &gfar_mdio_read,
144         new_bus->write = &gfar_mdio_write,
145         new_bus->reset = &gfar_mdio_reset,
146         new_bus->id = pdev->id;
147
148         pdata = (struct gianfar_mdio_data *)pdev->dev.platform_data;
149
150         if (NULL == pdata) {
151                 printk(KERN_ERR "gfar mdio %d: Missing platform data!\n", pdev->id);
152                 return -ENODEV;
153         }
154
155         /* Set the PHY base address */
156         regs = (struct gfar_mii *) ioremap(pdata->paddr, 
157                         sizeof (struct gfar_mii));
158
159         if (NULL == regs) {
160                 err = -ENOMEM;
161                 goto reg_map_fail;
162         }
163
164         new_bus->priv = regs;
165
166         new_bus->irq = pdata->irq;
167
168         new_bus->dev = dev;
169         dev_set_drvdata(dev, new_bus);
170
171         err = mdiobus_register(new_bus);
172
173         if (0 != err) {
174                 printk (KERN_ERR "%s: Cannot register as MDIO bus\n", 
175                                 new_bus->name);
176                 goto bus_register_fail;
177         }
178
179         return 0;
180
181 bus_register_fail:
182         iounmap((void *) regs);
183 reg_map_fail:
184         kfree(new_bus);
185
186         return err;
187 }
188
189
190 int gfar_mdio_remove(struct device *dev)
191 {
192         struct mii_bus *bus = dev_get_drvdata(dev);
193
194         mdiobus_unregister(bus);
195
196         dev_set_drvdata(dev, NULL);
197
198         iounmap((void *) (&bus->priv));
199         bus->priv = NULL;
200         kfree(bus);
201
202         return 0;
203 }
204
205 static struct device_driver gianfar_mdio_driver = {
206         .name = "fsl-gianfar_mdio",
207         .bus = &platform_bus_type,
208         .probe = gfar_mdio_probe,
209         .remove = gfar_mdio_remove,
210 };
211
212 int __init gfar_mdio_init(void)
213 {
214         return driver_register(&gianfar_mdio_driver);
215 }
216
217 void __exit gfar_mdio_exit(void)
218 {
219         driver_unregister(&gianfar_mdio_driver);
220 }