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 15 #include <linux/module.h> 16 #include <linux/types.h> 17 #include <linux/kernel.h> 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> 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> 34 #include <linux/of_address.h> 35 #include <linux/of_platform.h> 36 37 #include <asm/pgtable.h> 38 #include <asm/irq.h> 39 #include <asm/uaccess.h> 40 #include <asm/mpc5xxx.h> 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 53 static int fs_enet_fec_mii_read(struct mii_bus *bus , int phy_id, int location) 54 { 55 struct fec_info* fec = bus->priv; 56 struct fec __iomem *fecp = fec->fecp; 57 int i, ret = -1; 58 59 BUG_ON((in_be32(&fecp->fec_r_cntrl) & FEC_RCNTRL_MII_MODE) == 0); 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; 74 } 75 76 static 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; 79 struct fec __iomem *fecp = fec->fecp; 80 int i; 81 82 /* this must never happen */ 83 BUG_ON((in_be32(&fecp->fec_r_cntrl) & FEC_RCNTRL_MII_MODE) == 0); 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 99 static int fs_enet_fec_mii_reset(struct mii_bus *bus) 100 { 101 /* nothing here - for now */ 102 return 0; 103 } 104 105 static struct of_device_id fs_enet_mdio_fec_match[]; 106 static int fs_enet_mdio_probe(struct platform_device *ofdev) 107 { 108 const struct of_device_id *match; 109 struct resource res; 110 struct mii_bus *new_bus; 111 struct fec_info *fec; 112 int (*get_bus_freq)(struct device_node *); 113 int ret = -ENOMEM, clock, speed; 114 115 match = of_match_device(fs_enet_mdio_fec_match, &ofdev->dev); 116 if (!match) 117 return -EINVAL; 118 get_bus_freq = match->data; 119 120 new_bus = mdiobus_alloc(); 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 134 ret = of_address_to_resource(ofdev->dev.of_node, 0, &res); 135 if (ret) 136 goto out_res; 137 138 snprintf(new_bus->id, MII_BUS_ID_SIZE, "%x", res.start); 139 140 fec->fecp = ioremap(res.start, resource_size(&res)); 141 if (!fec->fecp) { 142 ret = -ENOMEM; 143 goto out_fec; 144 } 145 146 if (get_bus_freq) { 147 clock = get_bus_freq(ofdev->dev.of_node); 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; 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); 174 clrsetbits_be32(&fec->fecp->fec_mii_speed, 0x7E, fec->mii_speed); 175 176 new_bus->phy_mask = ~0; 177 new_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL); 178 if (!new_bus->irq) { 179 ret = -ENOMEM; 180 goto out_unmap_regs; 181 } 182 183 new_bus->parent = &ofdev->dev; 184 platform_set_drvdata(ofdev, new_bus); 185 186 ret = of_mdiobus_register(new_bus, ofdev->dev.of_node); 187 if (ret) 188 goto out_free_irqs; 189 190 return 0; 191 192 out_free_irqs: 193 kfree(new_bus->irq); 194 out_unmap_regs: 195 iounmap(fec->fecp); 196 out_res: 197 out_fec: 198 kfree(fec); 199 out_mii: 200 mdiobus_free(new_bus); 201 out: 202 return ret; 203 } 204 205 static int fs_enet_mdio_remove(struct platform_device *ofdev) 206 { 207 struct mii_bus *bus = platform_get_drvdata(ofdev); 208 struct fec_info *fec = bus->priv; 209 210 mdiobus_unregister(bus); 211 kfree(bus->irq); 212 iounmap(fec->fecp); 213 kfree(fec); 214 mdiobus_free(bus); 215 216 return 0; 217 } 218 219 static struct of_device_id fs_enet_mdio_fec_match[] = { 220 { 221 .compatible = "fsl,pq1-fec-mdio", 222 }, 223 #if defined(CONFIG_PPC_MPC512x) 224 { 225 .compatible = "fsl,mpc5121-fec-mdio", 226 .data = mpc5xxx_get_bus_frequency, 227 }, 228 #endif 229 {}, 230 }; 231 MODULE_DEVICE_TABLE(of, fs_enet_mdio_fec_match); 232 233 static struct platform_driver fs_enet_fec_mdio_driver = { 234 .driver = { 235 .name = "fsl-fec-mdio", 236 .owner = THIS_MODULE, 237 .of_match_table = fs_enet_mdio_fec_match, 238 }, 239 .probe = fs_enet_mdio_probe, 240 .remove = fs_enet_mdio_remove, 241 }; 242 243 module_platform_driver(fs_enet_fec_mdio_driver); 244