drivers: clean-up prom.h implicit includes
[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/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>
5af50730 34#include <linux/of_address.h>
b219108c 35#include <linux/of_platform.h>
5b4b8454
VB
36
37#include <asm/pgtable.h>
38#include <asm/irq.h>
39#include <asm/uaccess.h>
652f6787 40#include <asm/mpc5xxx.h>
5b4b8454
VB
41
42#include "fs_enet.h"
43#include "fec.h"
44
45/* Make MII read/write commands for the FEC.
46*/
47#define mk_mii_read(REG) (0x60020000 | ((REG & 0x1f) << 18))
48#define mk_mii_write(REG, VAL) (0x50020000 | ((REG & 0x1f) << 18) | (VAL & 0xffff))
49#define mk_mii_end 0
50
51#define FEC_MII_LOOPS 10000
52
5b4b8454
VB
53static int fs_enet_fec_mii_read(struct mii_bus *bus , int phy_id, int location)
54{
55 struct fec_info* fec = bus->priv;
60ab4361 56 struct fec __iomem *fecp = fec->fecp;
5b4b8454
VB
57 int i, ret = -1;
58
0ee904c3 59 BUG_ON((in_be32(&fecp->fec_r_cntrl) & FEC_RCNTRL_MII_MODE) == 0);
5b4b8454
VB
60
61 /* Add PHY address to register command. */
62 out_be32(&fecp->fec_mii_data, (phy_id << 23) | mk_mii_read(location));
63
64 for (i = 0; i < FEC_MII_LOOPS; i++)
65 if ((in_be32(&fecp->fec_ievent) & FEC_ENET_MII) != 0)
66 break;
67
68 if (i < FEC_MII_LOOPS) {
69 out_be32(&fecp->fec_ievent, FEC_ENET_MII);
70 ret = in_be32(&fecp->fec_mii_data) & 0xffff;
71 }
72
73 return ret;
5b4b8454
VB
74}
75
76static int fs_enet_fec_mii_write(struct mii_bus *bus, int phy_id, int location, u16 val)
77{
78 struct fec_info* fec = bus->priv;
60ab4361 79 struct fec __iomem *fecp = fec->fecp;
5b4b8454
VB
80 int i;
81
82 /* this must never happen */
0ee904c3 83 BUG_ON((in_be32(&fecp->fec_r_cntrl) & FEC_RCNTRL_MII_MODE) == 0);
5b4b8454
VB
84
85 /* Add PHY address to register command. */
86 out_be32(&fecp->fec_mii_data, (phy_id << 23) | mk_mii_write(location, val));
87
88 for (i = 0; i < FEC_MII_LOOPS; i++)
89 if ((in_be32(&fecp->fec_ievent) & FEC_ENET_MII) != 0)
90 break;
91
92 if (i < FEC_MII_LOOPS)
93 out_be32(&fecp->fec_ievent, FEC_ENET_MII);
94
95 return 0;
96
97}
98
99static int fs_enet_fec_mii_reset(struct mii_bus *bus)
100{
101 /* nothing here - for now */
102 return 0;
103}
104
b1608d69 105static struct of_device_id fs_enet_mdio_fec_match[];
4f2c53ea 106static int fs_enet_mdio_probe(struct platform_device *ofdev)
976de6a8 107{
b1608d69 108 const struct of_device_id *match;
976de6a8
SW
109 struct resource res;
110 struct mii_bus *new_bus;
111 struct fec_info *fec;
74888760 112 int (*get_bus_freq)(struct device_node *);
652f6787 113 int ret = -ENOMEM, clock, speed;
976de6a8 114
b1608d69
GL
115 match = of_match_device(fs_enet_mdio_fec_match, &ofdev->dev);
116 if (!match)
74888760 117 return -EINVAL;
b1608d69 118 get_bus_freq = match->data;
74888760 119
298cf9be 120 new_bus = mdiobus_alloc();
976de6a8
SW
121 if (!new_bus)
122 goto out;
123
124 fec = kzalloc(sizeof(struct fec_info), GFP_KERNEL);
125 if (!fec)
126 goto out_mii;
127
128 new_bus->priv = fec;
129 new_bus->name = "FEC MII Bus";
130 new_bus->read = &fs_enet_fec_mii_read;
131 new_bus->write = &fs_enet_fec_mii_write;
132 new_bus->reset = &fs_enet_fec_mii_reset;
133
61c7a080 134 ret = of_address_to_resource(ofdev->dev.of_node, 0, &res);
976de6a8 135 if (ret)
a86e2cbe 136 goto out_res;
976de6a8 137
9d9326d3 138 snprintf(new_bus->id, MII_BUS_ID_SIZE, "%x", res.start);
976de6a8 139
28f65c11 140 fec->fecp = ioremap(res.start, resource_size(&res));
137bc99f
JL
141 if (!fec->fecp) {
142 ret = -ENOMEM;
976de6a8 143 goto out_fec;
137bc99f 144 }
976de6a8 145
652f6787 146 if (get_bus_freq) {
61c7a080 147 clock = get_bus_freq(ofdev->dev.of_node);
652f6787
WD
148 if (!clock) {
149 /* Use maximum divider if clock is unknown */
150 dev_warn(&ofdev->dev, "could not determine IPS clock\n");
151 clock = 0x3F * 5000000;
152 }
153 } else
154 clock = ppc_proc_freq;
155
156 /*
157 * Scale for a MII clock <= 2.5 MHz
158 * Note that only 6 bits (25:30) are available for MII speed.
159 */
160 speed = (clock + 4999999) / 5000000;
161 if (speed > 0x3F) {
162 speed = 0x3F;
163 dev_err(&ofdev->dev,
164 "MII clock (%d Hz) exceeds max (2.5 MHz)\n",
165 clock / speed);
166 }
167
168 fec->mii_speed = speed << 1;
976de6a8
SW
169
170 setbits32(&fec->fecp->fec_r_cntrl, FEC_RCNTRL_MII_MODE);
171 setbits32(&fec->fecp->fec_ecntrl, FEC_ECNTRL_PINMUX |
172 FEC_ECNTRL_ETHER_EN);
173 out_be32(&fec->fecp->fec_ievent, FEC_ENET_MII);
652f6787 174 clrsetbits_be32(&fec->fecp->fec_mii_speed, 0x7E, fec->mii_speed);
976de6a8
SW
175
176 new_bus->phy_mask = ~0;
177 new_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
137bc99f
JL
178 if (!new_bus->irq) {
179 ret = -ENOMEM;
976de6a8 180 goto out_unmap_regs;
137bc99f 181 }
976de6a8 182
18ee49dd 183 new_bus->parent = &ofdev->dev;
8513fbd8 184 platform_set_drvdata(ofdev, new_bus);
976de6a8 185
61c7a080 186 ret = of_mdiobus_register(new_bus, ofdev->dev.of_node);
976de6a8
SW
187 if (ret)
188 goto out_free_irqs;
189
190 return 0;
191
192out_free_irqs:
976de6a8
SW
193 kfree(new_bus->irq);
194out_unmap_regs:
195 iounmap(fec->fecp);
a86e2cbe 196out_res:
976de6a8
SW
197out_fec:
198 kfree(fec);
199out_mii:
298cf9be 200 mdiobus_free(new_bus);
976de6a8
SW
201out:
202 return ret;
203}
204
2dc11581 205static int fs_enet_mdio_remove(struct platform_device *ofdev)
976de6a8 206{
8513fbd8 207 struct mii_bus *bus = platform_get_drvdata(ofdev);
976de6a8
SW
208 struct fec_info *fec = bus->priv;
209
210 mdiobus_unregister(bus);
976de6a8
SW
211 kfree(bus->irq);
212 iounmap(fec->fecp);
213 kfree(fec);
298cf9be 214 mdiobus_free(bus);
976de6a8
SW
215
216 return 0;
217}
218
219static struct of_device_id fs_enet_mdio_fec_match[] = {
220 {
221 .compatible = "fsl,pq1-fec-mdio",
222 },
652f6787
WD
223#if defined(CONFIG_PPC_MPC512x)
224 {
225 .compatible = "fsl,mpc5121-fec-mdio",
226 .data = mpc5xxx_get_bus_frequency,
227 },
228#endif
976de6a8
SW
229 {},
230};
e72701ac 231MODULE_DEVICE_TABLE(of, fs_enet_mdio_fec_match);
976de6a8 232
74888760 233static struct platform_driver fs_enet_fec_mdio_driver = {
4018294b
GL
234 .driver = {
235 .name = "fsl-fec-mdio",
236 .owner = THIS_MODULE,
237 .of_match_table = fs_enet_mdio_fec_match,
238 },
976de6a8
SW
239 .probe = fs_enet_mdio_probe,
240 .remove = fs_enet_mdio_remove,
241};
242
db62f684 243module_platform_driver(fs_enet_fec_mdio_driver);
This page took 0.72736 seconds and 5 git commands to generate.