Merge branch 'for-davem' into for-next
[deliverable/linux.git] / drivers / net / ethernet / freescale / fs_enet / mii-fec.c
CommitLineData
5b4b8454
VB
1/*
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
5b4b8454
VB
15#include <linux/module.h>
16#include <linux/types.h>
17#include <linux/kernel.h>
5b4b8454
VB
18#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>
5b4b8454
VB
24#include <linux/delay.h>
25#include <linux/netdevice.h>
26#include <linux/etherdevice.h>
27#include <linux/skbuff.h>
28#include <linux/spinlock.h>
29#include <linux/mii.h>
30#include <linux/ethtool.h>
31#include <linux/bitops.h>
32#include <linux/platform_device.h>
5af50730 33#include <linux/of_address.h>
b219108c 34#include <linux/of_platform.h>
5b4b8454
VB
35
36#include <asm/pgtable.h>
37#include <asm/irq.h>
38#include <asm/uaccess.h>
652f6787 39#include <asm/mpc5xxx.h>
5b4b8454
VB
40
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
5b4b8454
VB
52static int fs_enet_fec_mii_read(struct mii_bus *bus , int phy_id, int location)
53{
54 struct fec_info* fec = bus->priv;
60ab4361 55 struct fec __iomem *fecp = fec->fecp;
5b4b8454
VB
56 int i, ret = -1;
57
0ee904c3 58 BUG_ON((in_be32(&fecp->fec_r_cntrl) & FEC_RCNTRL_MII_MODE) == 0);
5b4b8454
VB
59
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;
5b4b8454
VB
73}
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;
60ab4361 78 struct fec __iomem *fecp = fec->fecp;
5b4b8454
VB
79 int i;
80
81 /* this must never happen */
0ee904c3 82 BUG_ON((in_be32(&fecp->fec_r_cntrl) & FEC_RCNTRL_MII_MODE) == 0);
5b4b8454
VB
83
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
94e5a2a8 98static const struct of_device_id fs_enet_mdio_fec_match[];
4f2c53ea 99static int fs_enet_mdio_probe(struct platform_device *ofdev)
976de6a8 100{
b1608d69 101 const struct of_device_id *match;
976de6a8
SW
102 struct resource res;
103 struct mii_bus *new_bus;
104 struct fec_info *fec;
74888760 105 int (*get_bus_freq)(struct device_node *);
652f6787 106 int ret = -ENOMEM, clock, speed;
976de6a8 107
b1608d69
GL
108 match = of_match_device(fs_enet_mdio_fec_match, &ofdev->dev);
109 if (!match)
74888760 110 return -EINVAL;
b1608d69 111 get_bus_freq = match->data;
74888760 112
298cf9be 113 new_bus = mdiobus_alloc();
976de6a8
SW
114 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;
976de6a8 125
61c7a080 126 ret = of_address_to_resource(ofdev->dev.of_node, 0, &res);
976de6a8 127 if (ret)
a86e2cbe 128 goto out_res;
976de6a8 129
9d9326d3 130 snprintf(new_bus->id, MII_BUS_ID_SIZE, "%x", res.start);
976de6a8 131
28f65c11 132 fec->fecp = ioremap(res.start, resource_size(&res));
137bc99f
JL
133 if (!fec->fecp) {
134 ret = -ENOMEM;
976de6a8 135 goto out_fec;
137bc99f 136 }
976de6a8 137
652f6787 138 if (get_bus_freq) {
61c7a080 139 clock = get_bus_freq(ofdev->dev.of_node);
652f6787
WD
140 if (!clock) {
141 /* Use maximum divider if clock is unknown */
142 dev_warn(&ofdev->dev, "could not determine IPS clock\n");
143 clock = 0x3F * 5000000;
144 }
145 } else
146 clock = ppc_proc_freq;
147
148 /*
149 * Scale for a MII clock <= 2.5 MHz
150 * Note that only 6 bits (25:30) are available for MII speed.
151 */
152 speed = (clock + 4999999) / 5000000;
153 if (speed > 0x3F) {
154 speed = 0x3F;
155 dev_err(&ofdev->dev,
156 "MII clock (%d Hz) exceeds max (2.5 MHz)\n",
157 clock / speed);
158 }
159
160 fec->mii_speed = speed << 1;
976de6a8
SW
161
162 setbits32(&fec->fecp->fec_r_cntrl, FEC_RCNTRL_MII_MODE);
163 setbits32(&fec->fecp->fec_ecntrl, FEC_ECNTRL_PINMUX |
164 FEC_ECNTRL_ETHER_EN);
165 out_be32(&fec->fecp->fec_ievent, FEC_ENET_MII);
652f6787 166 clrsetbits_be32(&fec->fecp->fec_mii_speed, 0x7E, fec->mii_speed);
976de6a8
SW
167
168 new_bus->phy_mask = ~0;
169 new_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
137bc99f
JL
170 if (!new_bus->irq) {
171 ret = -ENOMEM;
976de6a8 172 goto out_unmap_regs;
137bc99f 173 }
976de6a8 174
18ee49dd 175 new_bus->parent = &ofdev->dev;
8513fbd8 176 platform_set_drvdata(ofdev, new_bus);
976de6a8 177
61c7a080 178 ret = of_mdiobus_register(new_bus, ofdev->dev.of_node);
976de6a8
SW
179 if (ret)
180 goto out_free_irqs;
181
182 return 0;
183
184out_free_irqs:
976de6a8
SW
185 kfree(new_bus->irq);
186out_unmap_regs:
187 iounmap(fec->fecp);
a86e2cbe 188out_res:
976de6a8
SW
189out_fec:
190 kfree(fec);
191out_mii:
298cf9be 192 mdiobus_free(new_bus);
976de6a8
SW
193out:
194 return ret;
195}
196
2dc11581 197static int fs_enet_mdio_remove(struct platform_device *ofdev)
976de6a8 198{
8513fbd8 199 struct mii_bus *bus = platform_get_drvdata(ofdev);
976de6a8
SW
200 struct fec_info *fec = bus->priv;
201
202 mdiobus_unregister(bus);
976de6a8
SW
203 kfree(bus->irq);
204 iounmap(fec->fecp);
205 kfree(fec);
298cf9be 206 mdiobus_free(bus);
976de6a8
SW
207
208 return 0;
209}
210
94e5a2a8 211static const struct of_device_id fs_enet_mdio_fec_match[] = {
976de6a8
SW
212 {
213 .compatible = "fsl,pq1-fec-mdio",
214 },
652f6787
WD
215#if defined(CONFIG_PPC_MPC512x)
216 {
217 .compatible = "fsl,mpc5121-fec-mdio",
218 .data = mpc5xxx_get_bus_frequency,
219 },
220#endif
976de6a8
SW
221 {},
222};
e72701ac 223MODULE_DEVICE_TABLE(of, fs_enet_mdio_fec_match);
976de6a8 224
74888760 225static struct platform_driver fs_enet_fec_mdio_driver = {
4018294b
GL
226 .driver = {
227 .name = "fsl-fec-mdio",
4018294b
GL
228 .of_match_table = fs_enet_mdio_fec_match,
229 },
976de6a8
SW
230 .probe = fs_enet_mdio_probe,
231 .remove = fs_enet_mdio_remove,
232};
233
db62f684 234module_platform_driver(fs_enet_fec_mdio_driver);
This page took 0.922658 seconds and 5 git commands to generate.