blob: ac5d447ff8c43c7ae045165117765510f0f49d13 [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>
Rob Herring5af50732013-09-17 14:28:33 -050034#include <linux/of_address.h>
Kumar Galab2191082008-06-12 08:32:13 -050035#include <linux/of_platform.h>
Vitaly Bordug5b4b8452006-08-14 23:00:30 -070036
37#include <asm/pgtable.h>
38#include <asm/irq.h>
39#include <asm/uaccess.h>
Wolfgang Denk652f6782009-07-17 02:27:07 +000040#include <asm/mpc5xxx.h>
Vitaly Bordug5b4b8452006-08-14 23:00:30 -070041
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
Vitaly Bordug5b4b8452006-08-14 23:00:30 -070053static int fs_enet_fec_mii_read(struct mii_bus *bus , int phy_id, int location)
54{
55 struct fec_info* fec = bus->priv;
Anatolij Gustschin60ab4362010-02-26 12:00:48 +000056 struct fec __iomem *fecp = fec->fecp;
Vitaly Bordug5b4b8452006-08-14 23:00:30 -070057 int i, ret = -1;
58
Alexander Beregalov0ee904c2009-04-11 14:50:23 +000059 BUG_ON((in_be32(&fecp->fec_r_cntrl) & FEC_RCNTRL_MII_MODE) == 0);
Vitaly Bordug5b4b8452006-08-14 23:00:30 -070060
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;
Vitaly Bordug5b4b8452006-08-14 23:00:30 -070074}
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;
Anatolij Gustschin60ab4362010-02-26 12:00:48 +000079 struct fec __iomem *fecp = fec->fecp;
Vitaly Bordug5b4b8452006-08-14 23:00:30 -070080 int i;
81
82 /* this must never happen */
Alexander Beregalov0ee904c2009-04-11 14:50:23 +000083 BUG_ON((in_be32(&fecp->fec_r_cntrl) & FEC_RCNTRL_MII_MODE) == 0);
Vitaly Bordug5b4b8452006-08-14 23:00:30 -070084
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
Grant Likelyb1608d62011-05-18 11:19:24 -0600105static struct of_device_id fs_enet_mdio_fec_match[];
Bill Pemberton4f2c53e2012-12-03 09:23:08 -0500106static int fs_enet_mdio_probe(struct platform_device *ofdev)
Scott Wood976de6a2007-10-02 10:55:58 -0500107{
Grant Likelyb1608d62011-05-18 11:19:24 -0600108 const struct of_device_id *match;
Scott Wood976de6a2007-10-02 10:55:58 -0500109 struct resource res;
110 struct mii_bus *new_bus;
111 struct fec_info *fec;
Grant Likely74888762011-02-22 21:05:51 -0700112 int (*get_bus_freq)(struct device_node *);
Wolfgang Denk652f6782009-07-17 02:27:07 +0000113 int ret = -ENOMEM, clock, speed;
Scott Wood976de6a2007-10-02 10:55:58 -0500114
Grant Likelyb1608d62011-05-18 11:19:24 -0600115 match = of_match_device(fs_enet_mdio_fec_match, &ofdev->dev);
116 if (!match)
Grant Likely74888762011-02-22 21:05:51 -0700117 return -EINVAL;
Grant Likelyb1608d62011-05-18 11:19:24 -0600118 get_bus_freq = match->data;
Grant Likely74888762011-02-22 21:05:51 -0700119
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700120 new_bus = mdiobus_alloc();
Scott Wood976de6a2007-10-02 10:55:58 -0500121 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
Grant Likely61c7a082010-04-13 16:12:29 -0700134 ret = of_address_to_resource(ofdev->dev.of_node, 0, &res);
Scott Wood976de6a2007-10-02 10:55:58 -0500135 if (ret)
Scott Wooda86e2cb2008-05-02 13:42:41 -0500136 goto out_res;
Scott Wood976de6a2007-10-02 10:55:58 -0500137
Andy Fleming9d9326d2008-04-09 19:38:13 -0500138 snprintf(new_bus->id, MII_BUS_ID_SIZE, "%x", res.start);
Scott Wood976de6a2007-10-02 10:55:58 -0500139
Joe Perches28f65c112011-06-09 09:13:32 -0700140 fec->fecp = ioremap(res.start, resource_size(&res));
Julia Lawall137bc99f2012-08-14 02:58:33 +0000141 if (!fec->fecp) {
142 ret = -ENOMEM;
Scott Wood976de6a2007-10-02 10:55:58 -0500143 goto out_fec;
Julia Lawall137bc99f2012-08-14 02:58:33 +0000144 }
Scott Wood976de6a2007-10-02 10:55:58 -0500145
Wolfgang Denk652f6782009-07-17 02:27:07 +0000146 if (get_bus_freq) {
Grant Likely61c7a082010-04-13 16:12:29 -0700147 clock = get_bus_freq(ofdev->dev.of_node);
Wolfgang Denk652f6782009-07-17 02:27:07 +0000148 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;
Scott Wood976de6a2007-10-02 10:55:58 -0500169
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);
Wolfgang Denk652f6782009-07-17 02:27:07 +0000174 clrsetbits_be32(&fec->fecp->fec_mii_speed, 0x7E, fec->mii_speed);
Scott Wood976de6a2007-10-02 10:55:58 -0500175
176 new_bus->phy_mask = ~0;
177 new_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
Julia Lawall137bc99f2012-08-14 02:58:33 +0000178 if (!new_bus->irq) {
179 ret = -ENOMEM;
Scott Wood976de6a2007-10-02 10:55:58 -0500180 goto out_unmap_regs;
Julia Lawall137bc99f2012-08-14 02:58:33 +0000181 }
Scott Wood976de6a2007-10-02 10:55:58 -0500182
Lennert Buytenhek18ee49d2008-10-01 15:41:33 +0000183 new_bus->parent = &ofdev->dev;
Jingoo Han8513fbd2013-05-23 00:52:31 +0000184 platform_set_drvdata(ofdev, new_bus);
Scott Wood976de6a2007-10-02 10:55:58 -0500185
Grant Likely61c7a082010-04-13 16:12:29 -0700186 ret = of_mdiobus_register(new_bus, ofdev->dev.of_node);
Scott Wood976de6a2007-10-02 10:55:58 -0500187 if (ret)
188 goto out_free_irqs;
189
190 return 0;
191
192out_free_irqs:
Scott Wood976de6a2007-10-02 10:55:58 -0500193 kfree(new_bus->irq);
194out_unmap_regs:
195 iounmap(fec->fecp);
Scott Wooda86e2cb2008-05-02 13:42:41 -0500196out_res:
Scott Wood976de6a2007-10-02 10:55:58 -0500197out_fec:
198 kfree(fec);
199out_mii:
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700200 mdiobus_free(new_bus);
Scott Wood976de6a2007-10-02 10:55:58 -0500201out:
202 return ret;
203}
204
Grant Likely2dc11582010-08-06 09:25:50 -0600205static int fs_enet_mdio_remove(struct platform_device *ofdev)
Scott Wood976de6a2007-10-02 10:55:58 -0500206{
Jingoo Han8513fbd2013-05-23 00:52:31 +0000207 struct mii_bus *bus = platform_get_drvdata(ofdev);
Scott Wood976de6a2007-10-02 10:55:58 -0500208 struct fec_info *fec = bus->priv;
209
210 mdiobus_unregister(bus);
Scott Wood976de6a2007-10-02 10:55:58 -0500211 kfree(bus->irq);
212 iounmap(fec->fecp);
213 kfree(fec);
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700214 mdiobus_free(bus);
Scott Wood976de6a2007-10-02 10:55:58 -0500215
216 return 0;
217}
218
219static struct of_device_id fs_enet_mdio_fec_match[] = {
220 {
221 .compatible = "fsl,pq1-fec-mdio",
222 },
Wolfgang Denk652f6782009-07-17 02:27:07 +0000223#if defined(CONFIG_PPC_MPC512x)
224 {
225 .compatible = "fsl,mpc5121-fec-mdio",
226 .data = mpc5xxx_get_bus_frequency,
227 },
228#endif
Scott Wood976de6a2007-10-02 10:55:58 -0500229 {},
230};
Anton Vorontsove72701a2009-10-14 14:54:52 -0700231MODULE_DEVICE_TABLE(of, fs_enet_mdio_fec_match);
Scott Wood976de6a2007-10-02 10:55:58 -0500232
Grant Likely74888762011-02-22 21:05:51 -0700233static struct platform_driver fs_enet_fec_mdio_driver = {
Grant Likely40182942010-04-13 16:13:02 -0700234 .driver = {
235 .name = "fsl-fec-mdio",
236 .owner = THIS_MODULE,
237 .of_match_table = fs_enet_mdio_fec_match,
238 },
Scott Wood976de6a2007-10-02 10:55:58 -0500239 .probe = fs_enet_mdio_probe,
240 .remove = fs_enet_mdio_remove,
241};
242
Axel Lindb62f682011-11-27 16:44:17 +0000243module_platform_driver(fs_enet_fec_mdio_driver);