Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 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 | |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 15 | #include <linux/module.h> |
| 16 | #include <linux/types.h> |
| 17 | #include <linux/kernel.h> |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 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> |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 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> |
Kumar Gala | b219108 | 2008-06-12 08:32:13 -0500 | [diff] [blame] | 34 | #include <linux/of_platform.h> |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 35 | |
| 36 | #include <asm/pgtable.h> |
| 37 | #include <asm/irq.h> |
| 38 | #include <asm/uaccess.h> |
Wolfgang Denk | 652f678 | 2009-07-17 02:27:07 +0000 | [diff] [blame] | 39 | #include <asm/mpc5xxx.h> |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 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 | |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 52 | static int fs_enet_fec_mii_read(struct mii_bus *bus , int phy_id, int location) |
| 53 | { |
| 54 | struct fec_info* fec = bus->priv; |
Anatolij Gustschin | 60ab436 | 2010-02-26 12:00:48 +0000 | [diff] [blame] | 55 | struct fec __iomem *fecp = fec->fecp; |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 56 | int i, ret = -1; |
| 57 | |
Alexander Beregalov | 0ee904c | 2009-04-11 14:50:23 +0000 | [diff] [blame] | 58 | BUG_ON((in_be32(&fecp->fec_r_cntrl) & FEC_RCNTRL_MII_MODE) == 0); |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 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; |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | static 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 Gustschin | 60ab436 | 2010-02-26 12:00:48 +0000 | [diff] [blame] | 78 | struct fec __iomem *fecp = fec->fecp; |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 79 | int i; |
| 80 | |
| 81 | /* this must never happen */ |
Alexander Beregalov | 0ee904c | 2009-04-11 14:50:23 +0000 | [diff] [blame] | 82 | BUG_ON((in_be32(&fecp->fec_r_cntrl) & FEC_RCNTRL_MII_MODE) == 0); |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 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 | |
| 98 | static int fs_enet_fec_mii_reset(struct mii_bus *bus) |
| 99 | { |
| 100 | /* nothing here - for now */ |
| 101 | return 0; |
| 102 | } |
| 103 | |
Grant Likely | 7488876 | 2011-02-22 21:05:51 -0700 | [diff] [blame] | 104 | static int __devinit fs_enet_mdio_probe(struct platform_device *ofdev) |
Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 105 | { |
Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 106 | struct resource res; |
| 107 | struct mii_bus *new_bus; |
| 108 | struct fec_info *fec; |
Grant Likely | 7488876 | 2011-02-22 21:05:51 -0700 | [diff] [blame] | 109 | int (*get_bus_freq)(struct device_node *); |
Wolfgang Denk | 652f678 | 2009-07-17 02:27:07 +0000 | [diff] [blame] | 110 | int ret = -ENOMEM, clock, speed; |
Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 111 | |
Grant Likely | 7488876 | 2011-02-22 21:05:51 -0700 | [diff] [blame] | 112 | if (!ofdev->dev.of_match) |
| 113 | return -EINVAL; |
| 114 | get_bus_freq = ofdev->dev.of_match->data; |
| 115 | |
Lennert Buytenhek | 298cf9be | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 116 | new_bus = mdiobus_alloc(); |
Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 117 | if (!new_bus) |
| 118 | goto out; |
| 119 | |
| 120 | fec = kzalloc(sizeof(struct fec_info), GFP_KERNEL); |
| 121 | if (!fec) |
| 122 | goto out_mii; |
| 123 | |
| 124 | new_bus->priv = fec; |
| 125 | new_bus->name = "FEC MII Bus"; |
| 126 | new_bus->read = &fs_enet_fec_mii_read; |
| 127 | new_bus->write = &fs_enet_fec_mii_write; |
| 128 | new_bus->reset = &fs_enet_fec_mii_reset; |
| 129 | |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 130 | ret = of_address_to_resource(ofdev->dev.of_node, 0, &res); |
Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 131 | if (ret) |
Scott Wood | a86e2cb | 2008-05-02 13:42:41 -0500 | [diff] [blame] | 132 | goto out_res; |
Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 133 | |
Andy Fleming | 9d9326d | 2008-04-09 19:38:13 -0500 | [diff] [blame] | 134 | snprintf(new_bus->id, MII_BUS_ID_SIZE, "%x", res.start); |
Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 135 | |
| 136 | fec->fecp = ioremap(res.start, res.end - res.start + 1); |
| 137 | if (!fec->fecp) |
| 138 | goto out_fec; |
| 139 | |
Wolfgang Denk | 652f678 | 2009-07-17 02:27:07 +0000 | [diff] [blame] | 140 | if (get_bus_freq) { |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 141 | clock = get_bus_freq(ofdev->dev.of_node); |
Wolfgang Denk | 652f678 | 2009-07-17 02:27:07 +0000 | [diff] [blame] | 142 | if (!clock) { |
| 143 | /* Use maximum divider if clock is unknown */ |
| 144 | dev_warn(&ofdev->dev, "could not determine IPS clock\n"); |
| 145 | clock = 0x3F * 5000000; |
| 146 | } |
| 147 | } else |
| 148 | clock = ppc_proc_freq; |
| 149 | |
| 150 | /* |
| 151 | * Scale for a MII clock <= 2.5 MHz |
| 152 | * Note that only 6 bits (25:30) are available for MII speed. |
| 153 | */ |
| 154 | speed = (clock + 4999999) / 5000000; |
| 155 | if (speed > 0x3F) { |
| 156 | speed = 0x3F; |
| 157 | dev_err(&ofdev->dev, |
| 158 | "MII clock (%d Hz) exceeds max (2.5 MHz)\n", |
| 159 | clock / speed); |
| 160 | } |
| 161 | |
| 162 | fec->mii_speed = speed << 1; |
Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 163 | |
| 164 | setbits32(&fec->fecp->fec_r_cntrl, FEC_RCNTRL_MII_MODE); |
| 165 | setbits32(&fec->fecp->fec_ecntrl, FEC_ECNTRL_PINMUX | |
| 166 | FEC_ECNTRL_ETHER_EN); |
| 167 | out_be32(&fec->fecp->fec_ievent, FEC_ENET_MII); |
Wolfgang Denk | 652f678 | 2009-07-17 02:27:07 +0000 | [diff] [blame] | 168 | clrsetbits_be32(&fec->fecp->fec_mii_speed, 0x7E, fec->mii_speed); |
Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 169 | |
| 170 | new_bus->phy_mask = ~0; |
| 171 | new_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL); |
| 172 | if (!new_bus->irq) |
| 173 | goto out_unmap_regs; |
| 174 | |
Lennert Buytenhek | 18ee49d | 2008-10-01 15:41:33 +0000 | [diff] [blame] | 175 | new_bus->parent = &ofdev->dev; |
Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 176 | dev_set_drvdata(&ofdev->dev, new_bus); |
| 177 | |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 178 | ret = of_mdiobus_register(new_bus, ofdev->dev.of_node); |
Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 179 | if (ret) |
| 180 | goto out_free_irqs; |
| 181 | |
| 182 | return 0; |
| 183 | |
| 184 | out_free_irqs: |
| 185 | dev_set_drvdata(&ofdev->dev, NULL); |
| 186 | kfree(new_bus->irq); |
| 187 | out_unmap_regs: |
| 188 | iounmap(fec->fecp); |
Scott Wood | a86e2cb | 2008-05-02 13:42:41 -0500 | [diff] [blame] | 189 | out_res: |
Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 190 | out_fec: |
| 191 | kfree(fec); |
| 192 | out_mii: |
Lennert Buytenhek | 298cf9be | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 193 | mdiobus_free(new_bus); |
Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 194 | out: |
| 195 | return ret; |
| 196 | } |
| 197 | |
Grant Likely | 2dc1158 | 2010-08-06 09:25:50 -0600 | [diff] [blame] | 198 | static int fs_enet_mdio_remove(struct platform_device *ofdev) |
Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 199 | { |
| 200 | struct mii_bus *bus = dev_get_drvdata(&ofdev->dev); |
| 201 | struct fec_info *fec = bus->priv; |
| 202 | |
| 203 | mdiobus_unregister(bus); |
| 204 | dev_set_drvdata(&ofdev->dev, NULL); |
| 205 | kfree(bus->irq); |
| 206 | iounmap(fec->fecp); |
| 207 | kfree(fec); |
Lennert Buytenhek | 298cf9be | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 208 | mdiobus_free(bus); |
Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 209 | |
| 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | static struct of_device_id fs_enet_mdio_fec_match[] = { |
| 214 | { |
| 215 | .compatible = "fsl,pq1-fec-mdio", |
| 216 | }, |
Wolfgang Denk | 652f678 | 2009-07-17 02:27:07 +0000 | [diff] [blame] | 217 | #if defined(CONFIG_PPC_MPC512x) |
| 218 | { |
| 219 | .compatible = "fsl,mpc5121-fec-mdio", |
| 220 | .data = mpc5xxx_get_bus_frequency, |
| 221 | }, |
| 222 | #endif |
Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 223 | {}, |
| 224 | }; |
Anton Vorontsov | e72701a | 2009-10-14 14:54:52 -0700 | [diff] [blame] | 225 | MODULE_DEVICE_TABLE(of, fs_enet_mdio_fec_match); |
Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 226 | |
Grant Likely | 7488876 | 2011-02-22 21:05:51 -0700 | [diff] [blame] | 227 | static struct platform_driver fs_enet_fec_mdio_driver = { |
Grant Likely | 4018294 | 2010-04-13 16:13:02 -0700 | [diff] [blame] | 228 | .driver = { |
| 229 | .name = "fsl-fec-mdio", |
| 230 | .owner = THIS_MODULE, |
| 231 | .of_match_table = fs_enet_mdio_fec_match, |
| 232 | }, |
Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 233 | .probe = fs_enet_mdio_probe, |
| 234 | .remove = fs_enet_mdio_remove, |
| 235 | }; |
| 236 | |
| 237 | static int fs_enet_mdio_fec_init(void) |
| 238 | { |
Grant Likely | 7488876 | 2011-02-22 21:05:51 -0700 | [diff] [blame] | 239 | return platform_driver_register(&fs_enet_fec_mdio_driver); |
Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | static void fs_enet_mdio_fec_exit(void) |
| 243 | { |
Grant Likely | 7488876 | 2011-02-22 21:05:51 -0700 | [diff] [blame] | 244 | platform_driver_unregister(&fs_enet_fec_mdio_driver); |
Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | module_init(fs_enet_mdio_fec_init); |
| 248 | module_exit(fs_enet_mdio_fec_exit); |