blob: a89267b94352a33c0749cc9786c0ef489061b873 [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/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>
Rob Herring5af50732013-09-17 14:28:33 -050033#include <linux/of_address.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
Fabian Frederick94e5a2a2015-03-17 19:37:34 +010098static const struct of_device_id fs_enet_mdio_fec_match[];
Bill Pemberton4f2c53e2012-12-03 09:23:08 -050099static int fs_enet_mdio_probe(struct platform_device *ofdev)
Scott Wood976de6a2007-10-02 10:55:58 -0500100{
Grant Likelyb1608d62011-05-18 11:19:24 -0600101 const struct of_device_id *match;
Scott Wood976de6a2007-10-02 10:55:58 -0500102 struct resource res;
103 struct mii_bus *new_bus;
104 struct fec_info *fec;
Grant Likely74888762011-02-22 21:05:51 -0700105 int (*get_bus_freq)(struct device_node *);
Wolfgang Denk652f6782009-07-17 02:27:07 +0000106 int ret = -ENOMEM, clock, speed;
Scott Wood976de6a2007-10-02 10:55:58 -0500107
Grant Likelyb1608d62011-05-18 11:19:24 -0600108 match = of_match_device(fs_enet_mdio_fec_match, &ofdev->dev);
109 if (!match)
Grant Likely74888762011-02-22 21:05:51 -0700110 return -EINVAL;
Grant Likelyb1608d62011-05-18 11:19:24 -0600111 get_bus_freq = match->data;
Grant Likely74888762011-02-22 21:05:51 -0700112
Lennert Buytenhek298cf9b2008-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;
Scott Wood976de6a2007-10-02 10:55:58 -0500125
Grant Likely61c7a082010-04-13 16:12:29 -0700126 ret = of_address_to_resource(ofdev->dev.of_node, 0, &res);
Scott Wood976de6a2007-10-02 10:55:58 -0500127 if (ret)
Scott Wooda86e2cb2008-05-02 13:42:41 -0500128 goto out_res;
Scott Wood976de6a2007-10-02 10:55:58 -0500129
Andy Fleming9d9326d32008-04-09 19:38:13 -0500130 snprintf(new_bus->id, MII_BUS_ID_SIZE, "%x", res.start);
Scott Wood976de6a2007-10-02 10:55:58 -0500131
Joe Perches28f65c112011-06-09 09:13:32 -0700132 fec->fecp = ioremap(res.start, resource_size(&res));
Julia Lawall137bc99f2012-08-14 02:58:33 +0000133 if (!fec->fecp) {
134 ret = -ENOMEM;
Scott Wood976de6a2007-10-02 10:55:58 -0500135 goto out_fec;
Julia Lawall137bc99f2012-08-14 02:58:33 +0000136 }
Scott Wood976de6a2007-10-02 10:55:58 -0500137
Wolfgang Denk652f6782009-07-17 02:27:07 +0000138 if (get_bus_freq) {
Grant Likely61c7a082010-04-13 16:12:29 -0700139 clock = get_bus_freq(ofdev->dev.of_node);
Wolfgang Denk652f6782009-07-17 02:27:07 +0000140 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;
Scott Wood976de6a2007-10-02 10:55:58 -0500161
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);
Wolfgang Denk652f6782009-07-17 02:27:07 +0000166 clrsetbits_be32(&fec->fecp->fec_mii_speed, 0x7E, fec->mii_speed);
Scott Wood976de6a2007-10-02 10:55:58 -0500167
168 new_bus->phy_mask = ~0;
Scott Wood976de6a2007-10-02 10:55:58 -0500169
Lennert Buytenhek18ee49d2008-10-01 15:41:33 +0000170 new_bus->parent = &ofdev->dev;
Jingoo Han8513fbd2013-05-23 00:52:31 +0000171 platform_set_drvdata(ofdev, new_bus);
Scott Wood976de6a2007-10-02 10:55:58 -0500172
Grant Likely61c7a082010-04-13 16:12:29 -0700173 ret = of_mdiobus_register(new_bus, ofdev->dev.of_node);
Scott Wood976de6a2007-10-02 10:55:58 -0500174 if (ret)
Andrew Lunne7f4dc32016-01-06 20:11:15 +0100175 goto out_unmap_regs;
Scott Wood976de6a2007-10-02 10:55:58 -0500176
177 return 0;
178
Scott Wood976de6a2007-10-02 10:55:58 -0500179out_unmap_regs:
180 iounmap(fec->fecp);
Scott Wooda86e2cb2008-05-02 13:42:41 -0500181out_res:
Scott Wood976de6a2007-10-02 10:55:58 -0500182out_fec:
183 kfree(fec);
184out_mii:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700185 mdiobus_free(new_bus);
Scott Wood976de6a2007-10-02 10:55:58 -0500186out:
187 return ret;
188}
189
Grant Likely2dc11582010-08-06 09:25:50 -0600190static int fs_enet_mdio_remove(struct platform_device *ofdev)
Scott Wood976de6a2007-10-02 10:55:58 -0500191{
Jingoo Han8513fbd2013-05-23 00:52:31 +0000192 struct mii_bus *bus = platform_get_drvdata(ofdev);
Scott Wood976de6a2007-10-02 10:55:58 -0500193 struct fec_info *fec = bus->priv;
194
195 mdiobus_unregister(bus);
Scott Wood976de6a2007-10-02 10:55:58 -0500196 iounmap(fec->fecp);
197 kfree(fec);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700198 mdiobus_free(bus);
Scott Wood976de6a2007-10-02 10:55:58 -0500199
200 return 0;
201}
202
Fabian Frederick94e5a2a2015-03-17 19:37:34 +0100203static const struct of_device_id fs_enet_mdio_fec_match[] = {
Scott Wood976de6a2007-10-02 10:55:58 -0500204 {
205 .compatible = "fsl,pq1-fec-mdio",
206 },
Wolfgang Denk652f6782009-07-17 02:27:07 +0000207#if defined(CONFIG_PPC_MPC512x)
208 {
209 .compatible = "fsl,mpc5121-fec-mdio",
210 .data = mpc5xxx_get_bus_frequency,
211 },
212#endif
Scott Wood976de6a2007-10-02 10:55:58 -0500213 {},
214};
Anton Vorontsove72701a2009-10-14 14:54:52 -0700215MODULE_DEVICE_TABLE(of, fs_enet_mdio_fec_match);
Scott Wood976de6a2007-10-02 10:55:58 -0500216
Grant Likely74888762011-02-22 21:05:51 -0700217static struct platform_driver fs_enet_fec_mdio_driver = {
Grant Likely40182942010-04-13 16:13:02 -0700218 .driver = {
219 .name = "fsl-fec-mdio",
Grant Likely40182942010-04-13 16:13:02 -0700220 .of_match_table = fs_enet_mdio_fec_match,
221 },
Scott Wood976de6a2007-10-02 10:55:58 -0500222 .probe = fs_enet_mdio_probe,
223 .remove = fs_enet_mdio_remove,
224};
225
Axel Lindb62f682011-11-27 16:44:17 +0000226module_platform_driver(fs_enet_fec_mdio_driver);