| 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> | 
 | 18 | #include <linux/sched.h> | 
 | 19 | #include <linux/string.h> | 
 | 20 | #include <linux/ptrace.h> | 
 | 21 | #include <linux/errno.h> | 
 | 22 | #include <linux/ioport.h> | 
 | 23 | #include <linux/slab.h> | 
 | 24 | #include <linux/interrupt.h> | 
 | 25 | #include <linux/pci.h> | 
 | 26 | #include <linux/init.h> | 
 | 27 | #include <linux/delay.h> | 
 | 28 | #include <linux/netdevice.h> | 
 | 29 | #include <linux/etherdevice.h> | 
 | 30 | #include <linux/skbuff.h> | 
 | 31 | #include <linux/spinlock.h> | 
 | 32 | #include <linux/mii.h> | 
 | 33 | #include <linux/ethtool.h> | 
 | 34 | #include <linux/bitops.h> | 
 | 35 | #include <linux/platform_device.h> | 
 | 36 |  | 
 | 37 | #include <asm/pgtable.h> | 
 | 38 | #include <asm/irq.h> | 
 | 39 | #include <asm/uaccess.h> | 
 | 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 |  | 
 | 52 | static int match_has_phy (struct device *dev, void* data) | 
 | 53 | { | 
 | 54 | 	struct platform_device* pdev = container_of(dev, struct platform_device, dev); | 
 | 55 | 	struct fs_platform_info* fpi; | 
 | 56 | 	if(strcmp(pdev->name, (char*)data)) | 
 | 57 | 	{ | 
 | 58 | 	    return 0; | 
 | 59 | 	} | 
 | 60 |  | 
 | 61 | 	fpi = pdev->dev.platform_data; | 
 | 62 | 	if((fpi)&&(fpi->has_phy)) | 
 | 63 | 		return 1; | 
 | 64 | 	return 0; | 
 | 65 | } | 
 | 66 |  | 
 | 67 | static int fs_mii_fec_init(struct fec_info* fec, struct fs_mii_fec_platform_info *fmpi) | 
 | 68 | { | 
 | 69 | 	struct resource *r; | 
 | 70 | 	fec_t *fecp; | 
 | 71 | 	char* name = "fsl-cpm-fec"; | 
 | 72 |  | 
 | 73 | 	/* we need fec in order to be useful */ | 
 | 74 | 	struct platform_device *fec_pdev = | 
 | 75 | 		container_of(bus_find_device(&platform_bus_type, NULL, name, match_has_phy), | 
 | 76 | 				struct platform_device, dev); | 
 | 77 |  | 
 | 78 | 	if(fec_pdev == NULL) { | 
 | 79 | 		printk(KERN_ERR"Unable to find PHY for %s", name); | 
 | 80 | 		return -ENODEV; | 
 | 81 | 	} | 
 | 82 |  | 
 | 83 | 	r = platform_get_resource_byname(fec_pdev, IORESOURCE_MEM, "regs"); | 
 | 84 |  | 
 | 85 | 	fec->fecp = fecp = (fec_t*)ioremap(r->start,sizeof(fec_t)); | 
 | 86 | 	fec->mii_speed = fmpi->mii_speed; | 
 | 87 |  | 
 | 88 | 	setbits32(&fecp->fec_r_cntrl, FEC_RCNTRL_MII_MODE);	/* MII enable */ | 
 | 89 | 	setbits32(&fecp->fec_ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN); | 
 | 90 | 	out_be32(&fecp->fec_ievent, FEC_ENET_MII); | 
 | 91 | 	out_be32(&fecp->fec_mii_speed, fec->mii_speed); | 
 | 92 |  | 
 | 93 | 	return 0; | 
 | 94 | } | 
 | 95 |  | 
 | 96 | static int fs_enet_fec_mii_read(struct mii_bus *bus , int phy_id, int location) | 
 | 97 | { | 
 | 98 | 	struct fec_info* fec = bus->priv; | 
 | 99 | 	fec_t *fecp = fec->fecp; | 
 | 100 | 	int i, ret = -1; | 
 | 101 |  | 
 | 102 | 	if ((in_be32(&fecp->fec_r_cntrl) & FEC_RCNTRL_MII_MODE) == 0) | 
 | 103 | 		BUG(); | 
 | 104 |  | 
 | 105 | 	/* Add PHY address to register command.  */ | 
 | 106 | 	out_be32(&fecp->fec_mii_data, (phy_id << 23) | mk_mii_read(location)); | 
 | 107 |  | 
 | 108 | 	for (i = 0; i < FEC_MII_LOOPS; i++) | 
 | 109 | 		if ((in_be32(&fecp->fec_ievent) & FEC_ENET_MII) != 0) | 
 | 110 | 			break; | 
 | 111 |  | 
 | 112 | 	if (i < FEC_MII_LOOPS) { | 
 | 113 | 		out_be32(&fecp->fec_ievent, FEC_ENET_MII); | 
 | 114 | 		ret = in_be32(&fecp->fec_mii_data) & 0xffff; | 
 | 115 | 	} | 
 | 116 |  | 
 | 117 | 	return ret; | 
 | 118 |  | 
 | 119 | } | 
 | 120 |  | 
 | 121 | static int fs_enet_fec_mii_write(struct mii_bus *bus, int phy_id, int location, u16 val) | 
 | 122 | { | 
 | 123 | 	struct fec_info* fec = bus->priv; | 
 | 124 | 	fec_t *fecp = fec->fecp; | 
 | 125 | 	int i; | 
 | 126 |  | 
 | 127 | 	/* this must never happen */ | 
 | 128 | 	if ((in_be32(&fecp->fec_r_cntrl) & FEC_RCNTRL_MII_MODE) == 0) | 
 | 129 | 		BUG(); | 
 | 130 |  | 
 | 131 | 	/* Add PHY address to register command.  */ | 
 | 132 | 	out_be32(&fecp->fec_mii_data, (phy_id << 23) | mk_mii_write(location, val)); | 
 | 133 |  | 
 | 134 | 	for (i = 0; i < FEC_MII_LOOPS; i++) | 
 | 135 | 		if ((in_be32(&fecp->fec_ievent) & FEC_ENET_MII) != 0) | 
 | 136 | 			break; | 
 | 137 |  | 
 | 138 | 	if (i < FEC_MII_LOOPS) | 
 | 139 | 		out_be32(&fecp->fec_ievent, FEC_ENET_MII); | 
 | 140 |  | 
 | 141 | 	return 0; | 
 | 142 |  | 
 | 143 | } | 
 | 144 |  | 
 | 145 | static int fs_enet_fec_mii_reset(struct mii_bus *bus) | 
 | 146 | { | 
 | 147 | 	/* nothing here - for now */ | 
 | 148 | 	return 0; | 
 | 149 | } | 
 | 150 |  | 
 | 151 | static int __devinit fs_enet_fec_mdio_probe(struct device *dev) | 
 | 152 | { | 
 | 153 | 	struct platform_device *pdev = to_platform_device(dev); | 
 | 154 | 	struct fs_mii_fec_platform_info *pdata; | 
 | 155 | 	struct mii_bus *new_bus; | 
 | 156 | 	struct fec_info *fec; | 
 | 157 | 	int err = 0; | 
 | 158 | 	if (NULL == dev) | 
 | 159 | 		return -EINVAL; | 
 | 160 | 	new_bus = kzalloc(sizeof(struct mii_bus), GFP_KERNEL); | 
 | 161 |  | 
 | 162 | 	if (NULL == new_bus) | 
 | 163 | 		return -ENOMEM; | 
 | 164 |  | 
 | 165 | 	fec = kzalloc(sizeof(struct fec_info), GFP_KERNEL); | 
 | 166 |  | 
 | 167 | 	if (NULL == fec) | 
 | 168 | 		return -ENOMEM; | 
 | 169 |  | 
 | 170 | 	new_bus->name = "FEC MII Bus", | 
 | 171 | 	new_bus->read = &fs_enet_fec_mii_read, | 
 | 172 | 	new_bus->write = &fs_enet_fec_mii_write, | 
 | 173 | 	new_bus->reset = &fs_enet_fec_mii_reset, | 
 | 174 | 	new_bus->id = pdev->id; | 
 | 175 |  | 
 | 176 | 	pdata = (struct fs_mii_fec_platform_info *)pdev->dev.platform_data; | 
 | 177 |  | 
 | 178 | 	if (NULL == pdata) { | 
 | 179 | 		printk(KERN_ERR "fs_enet FEC mdio %d: Missing platform data!\n", pdev->id); | 
 | 180 | 		return -ENODEV; | 
 | 181 | 	} | 
 | 182 |  | 
 | 183 | 	/*set up workspace*/ | 
 | 184 |  | 
 | 185 | 	fs_mii_fec_init(fec, pdata); | 
 | 186 | 	new_bus->priv = fec; | 
 | 187 |  | 
 | 188 | 	new_bus->irq = pdata->irq; | 
 | 189 |  | 
 | 190 | 	new_bus->dev = dev; | 
 | 191 | 	dev_set_drvdata(dev, new_bus); | 
 | 192 |  | 
 | 193 | 	err = mdiobus_register(new_bus); | 
 | 194 |  | 
 | 195 | 	if (0 != err) { | 
 | 196 | 		printk (KERN_ERR "%s: Cannot register as MDIO bus\n", | 
 | 197 | 				new_bus->name); | 
 | 198 | 		goto bus_register_fail; | 
 | 199 | 	} | 
 | 200 |  | 
 | 201 | 	return 0; | 
 | 202 |  | 
 | 203 | bus_register_fail: | 
 | 204 | 	kfree(new_bus); | 
 | 205 |  | 
 | 206 | 	return err; | 
 | 207 | } | 
 | 208 |  | 
 | 209 |  | 
 | 210 | static int fs_enet_fec_mdio_remove(struct device *dev) | 
 | 211 | { | 
 | 212 | 	struct mii_bus *bus = dev_get_drvdata(dev); | 
 | 213 |  | 
 | 214 | 	mdiobus_unregister(bus); | 
 | 215 |  | 
 | 216 | 	dev_set_drvdata(dev, NULL); | 
 | 217 | 	kfree(bus->priv); | 
 | 218 |  | 
 | 219 | 	bus->priv = NULL; | 
 | 220 | 	kfree(bus); | 
 | 221 |  | 
 | 222 | 	return 0; | 
 | 223 | } | 
 | 224 |  | 
 | 225 | static struct device_driver fs_enet_fec_mdio_driver = { | 
 | 226 | 	.name = "fsl-cpm-fec-mdio", | 
 | 227 | 	.bus = &platform_bus_type, | 
 | 228 | 	.probe = fs_enet_fec_mdio_probe, | 
 | 229 | 	.remove = fs_enet_fec_mdio_remove, | 
 | 230 | }; | 
 | 231 |  | 
 | 232 | int fs_enet_mdio_fec_init(void) | 
 | 233 | { | 
 | 234 | 	return driver_register(&fs_enet_fec_mdio_driver); | 
 | 235 | } | 
 | 236 |  | 
 | 237 | void fs_enet_mdio_fec_exit(void) | 
 | 238 | { | 
 | 239 | 	driver_unregister(&fs_enet_fec_mdio_driver); | 
 | 240 | } | 
 | 241 |  |