blob: 5944b65082cb2a11ef13b16f2cb12c853b9b65c3 [file] [log] [blame]
Vitaly Bordug5b4b8452006-08-14 23:00:30 -07001/*
2 * Combined Ethernet driver for Motorola MPC8xx and MPC82xx.
3 *
4 * Copyright (c) 2003 Intracom S.A.
5 * by Pantelis Antoniou <panto@intracom.gr>
6 *
7 * 2005 (c) MontaVista Software, Inc.
8 * Vitaly Bordug <vbordug@ru.mvista.com>
9 *
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.
13 */
14
Vitaly Bordug5b4b8452006-08-14 23:00:30 -070015#include <linux/module.h>
16#include <linux/types.h>
17#include <linux/kernel.h>
Vitaly Bordug5b4b8452006-08-14 23:00:30 -070018#include <linux/string.h>
19#include <linux/ptrace.h>
20#include <linux/errno.h>
21#include <linux/ioport.h>
22#include <linux/slab.h>
23#include <linux/interrupt.h>
Vitaly Bordug5b4b8452006-08-14 23:00:30 -070024#include <linux/init.h>
25#include <linux/delay.h>
26#include <linux/netdevice.h>
27#include <linux/etherdevice.h>
28#include <linux/skbuff.h>
29#include <linux/spinlock.h>
30#include <linux/mii.h>
31#include <linux/ethtool.h>
32#include <linux/bitops.h>
33#include <linux/platform_device.h>
Kumar Galab2191082008-06-12 08:32:13 -050034#include <linux/of_platform.h>
Vitaly Bordug5b4b8452006-08-14 23:00:30 -070035
36#include <asm/pgtable.h>
37#include <asm/irq.h>
38#include <asm/uaccess.h>
Wolfgang Denk652f6782009-07-17 02:27:07 +000039#include <asm/mpc5xxx.h>
Vitaly Bordug5b4b8452006-08-14 23:00:30 -070040
41#include "fs_enet.h"
42#include "fec.h"
43
44/* Make MII read/write commands for the FEC.
45*/
46#define mk_mii_read(REG) (0x60020000 | ((REG & 0x1f) << 18))
47#define mk_mii_write(REG, VAL) (0x50020000 | ((REG & 0x1f) << 18) | (VAL & 0xffff))
48#define mk_mii_end 0
49
50#define FEC_MII_LOOPS 10000
51
Vitaly Bordug5b4b8452006-08-14 23:00:30 -070052static int fs_enet_fec_mii_read(struct mii_bus *bus , int phy_id, int location)
53{
54 struct fec_info* fec = bus->priv;
Anatolij Gustschin60ab4362010-02-26 12:00:48 +000055 struct fec __iomem *fecp = fec->fecp;
Vitaly Bordug5b4b8452006-08-14 23:00:30 -070056 int i, ret = -1;
57
Alexander Beregalov0ee904c2009-04-11 14:50:23 +000058 BUG_ON((in_be32(&fecp->fec_r_cntrl) & FEC_RCNTRL_MII_MODE) == 0);
Vitaly Bordug5b4b8452006-08-14 23:00:30 -070059
60 /* Add PHY address to register command. */
61 out_be32(&fecp->fec_mii_data, (phy_id << 23) | mk_mii_read(location));
62
63 for (i = 0; i < FEC_MII_LOOPS; i++)
64 if ((in_be32(&fecp->fec_ievent) & FEC_ENET_MII) != 0)
65 break;
66
67 if (i < FEC_MII_LOOPS) {
68 out_be32(&fecp->fec_ievent, FEC_ENET_MII);
69 ret = in_be32(&fecp->fec_mii_data) & 0xffff;
70 }
71
72 return ret;
Vitaly Bordug5b4b8452006-08-14 23:00:30 -070073}
74
75static int fs_enet_fec_mii_write(struct mii_bus *bus, int phy_id, int location, u16 val)
76{
77 struct fec_info* fec = bus->priv;
Anatolij Gustschin60ab4362010-02-26 12:00:48 +000078 struct fec __iomem *fecp = fec->fecp;
Vitaly Bordug5b4b8452006-08-14 23:00:30 -070079 int i;
80
81 /* this must never happen */
Alexander Beregalov0ee904c2009-04-11 14:50:23 +000082 BUG_ON((in_be32(&fecp->fec_r_cntrl) & FEC_RCNTRL_MII_MODE) == 0);
Vitaly Bordug5b4b8452006-08-14 23:00:30 -070083
84 /* Add PHY address to register command. */
85 out_be32(&fecp->fec_mii_data, (phy_id << 23) | mk_mii_write(location, val));
86
87 for (i = 0; i < FEC_MII_LOOPS; i++)
88 if ((in_be32(&fecp->fec_ievent) & FEC_ENET_MII) != 0)
89 break;
90
91 if (i < FEC_MII_LOOPS)
92 out_be32(&fecp->fec_ievent, FEC_ENET_MII);
93
94 return 0;
95
96}
97
98static int fs_enet_fec_mii_reset(struct mii_bus *bus)
99{
100 /* nothing here - for now */
101 return 0;
102}
103
Scott Wood976de6a2007-10-02 10:55:58 -0500104static int __devinit fs_enet_mdio_probe(struct of_device *ofdev,
105 const struct of_device_id *match)
106{
Scott Wood976de6a2007-10-02 10:55:58 -0500107 struct resource res;
108 struct mii_bus *new_bus;
109 struct fec_info *fec;
Wolfgang Denk652f6782009-07-17 02:27:07 +0000110 int (*get_bus_freq)(struct device_node *) = match->data;
111 int ret = -ENOMEM, clock, speed;
Scott Wood976de6a2007-10-02 10:55:58 -0500112
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700113 new_bus = mdiobus_alloc();
Scott Wood976de6a2007-10-02 10:55:58 -0500114 if (!new_bus)
115 goto out;
116
117 fec = kzalloc(sizeof(struct fec_info), GFP_KERNEL);
118 if (!fec)
119 goto out_mii;
120
121 new_bus->priv = fec;
122 new_bus->name = "FEC MII Bus";
123 new_bus->read = &fs_enet_fec_mii_read;
124 new_bus->write = &fs_enet_fec_mii_write;
125 new_bus->reset = &fs_enet_fec_mii_reset;
126
127 ret = of_address_to_resource(ofdev->node, 0, &res);
128 if (ret)
Scott Wooda86e2cb2008-05-02 13:42:41 -0500129 goto out_res;
Scott Wood976de6a2007-10-02 10:55:58 -0500130
Andy Fleming9d9326d2008-04-09 19:38:13 -0500131 snprintf(new_bus->id, MII_BUS_ID_SIZE, "%x", res.start);
Scott Wood976de6a2007-10-02 10:55:58 -0500132
133 fec->fecp = ioremap(res.start, res.end - res.start + 1);
134 if (!fec->fecp)
135 goto out_fec;
136
Wolfgang Denk652f6782009-07-17 02:27:07 +0000137 if (get_bus_freq) {
138 clock = get_bus_freq(ofdev->node);
139 if (!clock) {
140 /* Use maximum divider if clock is unknown */
141 dev_warn(&ofdev->dev, "could not determine IPS clock\n");
142 clock = 0x3F * 5000000;
143 }
144 } else
145 clock = ppc_proc_freq;
146
147 /*
148 * Scale for a MII clock <= 2.5 MHz
149 * Note that only 6 bits (25:30) are available for MII speed.
150 */
151 speed = (clock + 4999999) / 5000000;
152 if (speed > 0x3F) {
153 speed = 0x3F;
154 dev_err(&ofdev->dev,
155 "MII clock (%d Hz) exceeds max (2.5 MHz)\n",
156 clock / speed);
157 }
158
159 fec->mii_speed = speed << 1;
Scott Wood976de6a2007-10-02 10:55:58 -0500160
161 setbits32(&fec->fecp->fec_r_cntrl, FEC_RCNTRL_MII_MODE);
162 setbits32(&fec->fecp->fec_ecntrl, FEC_ECNTRL_PINMUX |
163 FEC_ECNTRL_ETHER_EN);
164 out_be32(&fec->fecp->fec_ievent, FEC_ENET_MII);
Wolfgang Denk652f6782009-07-17 02:27:07 +0000165 clrsetbits_be32(&fec->fecp->fec_mii_speed, 0x7E, fec->mii_speed);
Scott Wood976de6a2007-10-02 10:55:58 -0500166
167 new_bus->phy_mask = ~0;
168 new_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
169 if (!new_bus->irq)
170 goto out_unmap_regs;
171
Lennert Buytenhek18ee49d2008-10-01 15:41:33 +0000172 new_bus->parent = &ofdev->dev;
Scott Wood976de6a2007-10-02 10:55:58 -0500173 dev_set_drvdata(&ofdev->dev, new_bus);
174
Grant Likelyaa738322009-04-25 12:53:33 +0000175 ret = of_mdiobus_register(new_bus, ofdev->node);
Scott Wood976de6a2007-10-02 10:55:58 -0500176 if (ret)
177 goto out_free_irqs;
178
179 return 0;
180
181out_free_irqs:
182 dev_set_drvdata(&ofdev->dev, NULL);
183 kfree(new_bus->irq);
184out_unmap_regs:
185 iounmap(fec->fecp);
Scott Wooda86e2cb2008-05-02 13:42:41 -0500186out_res:
Scott Wood976de6a2007-10-02 10:55:58 -0500187out_fec:
188 kfree(fec);
189out_mii:
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700190 mdiobus_free(new_bus);
Scott Wood976de6a2007-10-02 10:55:58 -0500191out:
192 return ret;
193}
194
195static int fs_enet_mdio_remove(struct of_device *ofdev)
196{
197 struct mii_bus *bus = dev_get_drvdata(&ofdev->dev);
198 struct fec_info *fec = bus->priv;
199
200 mdiobus_unregister(bus);
201 dev_set_drvdata(&ofdev->dev, NULL);
202 kfree(bus->irq);
203 iounmap(fec->fecp);
204 kfree(fec);
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700205 mdiobus_free(bus);
Scott Wood976de6a2007-10-02 10:55:58 -0500206
207 return 0;
208}
209
210static struct of_device_id fs_enet_mdio_fec_match[] = {
211 {
212 .compatible = "fsl,pq1-fec-mdio",
213 },
Wolfgang Denk652f6782009-07-17 02:27:07 +0000214#if defined(CONFIG_PPC_MPC512x)
215 {
216 .compatible = "fsl,mpc5121-fec-mdio",
217 .data = mpc5xxx_get_bus_frequency,
218 },
219#endif
Scott Wood976de6a2007-10-02 10:55:58 -0500220 {},
221};
Anton Vorontsove72701a2009-10-14 14:54:52 -0700222MODULE_DEVICE_TABLE(of, fs_enet_mdio_fec_match);
Scott Wood976de6a2007-10-02 10:55:58 -0500223
224static struct of_platform_driver fs_enet_fec_mdio_driver = {
225 .name = "fsl-fec-mdio",
226 .match_table = fs_enet_mdio_fec_match,
227 .probe = fs_enet_mdio_probe,
228 .remove = fs_enet_mdio_remove,
229};
230
231static int fs_enet_mdio_fec_init(void)
232{
233 return of_register_platform_driver(&fs_enet_fec_mdio_driver);
234}
235
236static void fs_enet_mdio_fec_exit(void)
237{
238 of_unregister_platform_driver(&fs_enet_fec_mdio_driver);
239}
240
241module_init(fs_enet_mdio_fec_init);
242module_exit(fs_enet_mdio_fec_exit);