0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #include <linux/module.h>
0016 #include <linux/types.h>
0017 #include <linux/kernel.h>
0018 #include <linux/string.h>
0019 #include <linux/ptrace.h>
0020 #include <linux/errno.h>
0021 #include <linux/ioport.h>
0022 #include <linux/slab.h>
0023 #include <linux/interrupt.h>
0024 #include <linux/delay.h>
0025 #include <linux/netdevice.h>
0026 #include <linux/etherdevice.h>
0027 #include <linux/skbuff.h>
0028 #include <linux/spinlock.h>
0029 #include <linux/mii.h>
0030 #include <linux/ethtool.h>
0031 #include <linux/bitops.h>
0032 #include <linux/platform_device.h>
0033 #include <linux/of_address.h>
0034 #include <linux/of_platform.h>
0035 #include <linux/pgtable.h>
0036
0037 #include <asm/irq.h>
0038 #include <linux/uaccess.h>
0039 #include <asm/mpc5xxx.h>
0040
0041 #include "fs_enet.h"
0042 #include "fec.h"
0043
0044
0045
0046 #define mk_mii_read(REG) (0x60020000 | ((REG & 0x1f) << 18))
0047 #define mk_mii_write(REG, VAL) (0x50020000 | ((REG & 0x1f) << 18) | (VAL & 0xffff))
0048 #define mk_mii_end 0
0049
0050 #define FEC_MII_LOOPS 10000
0051
0052 static int fs_enet_fec_mii_read(struct mii_bus *bus , int phy_id, int location)
0053 {
0054 struct fec_info* fec = bus->priv;
0055 struct fec __iomem *fecp = fec->fecp;
0056 int i, ret = -1;
0057
0058 BUG_ON((in_be32(&fecp->fec_r_cntrl) & FEC_RCNTRL_MII_MODE) == 0);
0059
0060
0061 out_be32(&fecp->fec_mii_data, (phy_id << 23) | mk_mii_read(location));
0062
0063 for (i = 0; i < FEC_MII_LOOPS; i++)
0064 if ((in_be32(&fecp->fec_ievent) & FEC_ENET_MII) != 0)
0065 break;
0066
0067 if (i < FEC_MII_LOOPS) {
0068 out_be32(&fecp->fec_ievent, FEC_ENET_MII);
0069 ret = in_be32(&fecp->fec_mii_data) & 0xffff;
0070 }
0071
0072 return ret;
0073 }
0074
0075 static int fs_enet_fec_mii_write(struct mii_bus *bus, int phy_id, int location, u16 val)
0076 {
0077 struct fec_info* fec = bus->priv;
0078 struct fec __iomem *fecp = fec->fecp;
0079 int i;
0080
0081
0082 BUG_ON((in_be32(&fecp->fec_r_cntrl) & FEC_RCNTRL_MII_MODE) == 0);
0083
0084
0085 out_be32(&fecp->fec_mii_data, (phy_id << 23) | mk_mii_write(location, val));
0086
0087 for (i = 0; i < FEC_MII_LOOPS; i++)
0088 if ((in_be32(&fecp->fec_ievent) & FEC_ENET_MII) != 0)
0089 break;
0090
0091 if (i < FEC_MII_LOOPS)
0092 out_be32(&fecp->fec_ievent, FEC_ENET_MII);
0093
0094 return 0;
0095
0096 }
0097
0098 static const struct of_device_id fs_enet_mdio_fec_match[];
0099 static int fs_enet_mdio_probe(struct platform_device *ofdev)
0100 {
0101 const struct of_device_id *match;
0102 struct resource res;
0103 struct mii_bus *new_bus;
0104 struct fec_info *fec;
0105 int (*get_bus_freq)(struct device *);
0106 int ret = -ENOMEM, clock, speed;
0107
0108 match = of_match_device(fs_enet_mdio_fec_match, &ofdev->dev);
0109 if (!match)
0110 return -EINVAL;
0111 get_bus_freq = match->data;
0112
0113 new_bus = mdiobus_alloc();
0114 if (!new_bus)
0115 goto out;
0116
0117 fec = kzalloc(sizeof(struct fec_info), GFP_KERNEL);
0118 if (!fec)
0119 goto out_mii;
0120
0121 new_bus->priv = fec;
0122 new_bus->name = "FEC MII Bus";
0123 new_bus->read = &fs_enet_fec_mii_read;
0124 new_bus->write = &fs_enet_fec_mii_write;
0125
0126 ret = of_address_to_resource(ofdev->dev.of_node, 0, &res);
0127 if (ret)
0128 goto out_res;
0129
0130 snprintf(new_bus->id, MII_BUS_ID_SIZE, "%x", res.start);
0131
0132 fec->fecp = ioremap(res.start, resource_size(&res));
0133 if (!fec->fecp) {
0134 ret = -ENOMEM;
0135 goto out_fec;
0136 }
0137
0138 if (get_bus_freq) {
0139 clock = get_bus_freq(&ofdev->dev);
0140 if (!clock) {
0141
0142 dev_warn(&ofdev->dev, "could not determine IPS clock\n");
0143 clock = 0x3F * 5000000;
0144 }
0145 } else
0146 clock = ppc_proc_freq;
0147
0148
0149
0150
0151
0152 speed = (clock + 4999999) / 5000000;
0153 if (speed > 0x3F) {
0154 speed = 0x3F;
0155 dev_err(&ofdev->dev,
0156 "MII clock (%d Hz) exceeds max (2.5 MHz)\n",
0157 clock / speed);
0158 }
0159
0160 fec->mii_speed = speed << 1;
0161
0162 setbits32(&fec->fecp->fec_r_cntrl, FEC_RCNTRL_MII_MODE);
0163 setbits32(&fec->fecp->fec_ecntrl, FEC_ECNTRL_PINMUX |
0164 FEC_ECNTRL_ETHER_EN);
0165 out_be32(&fec->fecp->fec_ievent, FEC_ENET_MII);
0166 clrsetbits_be32(&fec->fecp->fec_mii_speed, 0x7E, fec->mii_speed);
0167
0168 new_bus->phy_mask = ~0;
0169
0170 new_bus->parent = &ofdev->dev;
0171 platform_set_drvdata(ofdev, new_bus);
0172
0173 ret = of_mdiobus_register(new_bus, ofdev->dev.of_node);
0174 if (ret)
0175 goto out_unmap_regs;
0176
0177 return 0;
0178
0179 out_unmap_regs:
0180 iounmap(fec->fecp);
0181 out_res:
0182 out_fec:
0183 kfree(fec);
0184 out_mii:
0185 mdiobus_free(new_bus);
0186 out:
0187 return ret;
0188 }
0189
0190 static int fs_enet_mdio_remove(struct platform_device *ofdev)
0191 {
0192 struct mii_bus *bus = platform_get_drvdata(ofdev);
0193 struct fec_info *fec = bus->priv;
0194
0195 mdiobus_unregister(bus);
0196 iounmap(fec->fecp);
0197 kfree(fec);
0198 mdiobus_free(bus);
0199
0200 return 0;
0201 }
0202
0203 static const struct of_device_id fs_enet_mdio_fec_match[] = {
0204 {
0205 .compatible = "fsl,pq1-fec-mdio",
0206 },
0207 #if defined(CONFIG_PPC_MPC512x)
0208 {
0209 .compatible = "fsl,mpc5121-fec-mdio",
0210 .data = mpc5xxx_get_bus_frequency,
0211 },
0212 #endif
0213 {},
0214 };
0215 MODULE_DEVICE_TABLE(of, fs_enet_mdio_fec_match);
0216
0217 static struct platform_driver fs_enet_fec_mdio_driver = {
0218 .driver = {
0219 .name = "fsl-fec-mdio",
0220 .of_match_table = fs_enet_mdio_fec_match,
0221 },
0222 .probe = fs_enet_mdio_probe,
0223 .remove = fs_enet_mdio_remove,
0224 };
0225
0226 module_platform_driver(fs_enet_fec_mdio_driver);
0227 MODULE_LICENSE("GPL");