| 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> | 
 | 39 |  | 
 | 40 | #include "fs_enet.h" | 
 | 41 | #include "fec.h" | 
 | 42 |  | 
 | 43 | /* Make MII read/write commands for the FEC. | 
 | 44 | */ | 
 | 45 | #define mk_mii_read(REG)	(0x60020000 | ((REG & 0x1f) << 18)) | 
 | 46 | #define mk_mii_write(REG, VAL)	(0x50020000 | ((REG & 0x1f) << 18) | (VAL & 0xffff)) | 
 | 47 | #define mk_mii_end		0 | 
 | 48 |  | 
 | 49 | #define FEC_MII_LOOPS	10000 | 
 | 50 |  | 
| Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 51 | static int fs_enet_fec_mii_read(struct mii_bus *bus , int phy_id, int location) | 
 | 52 | { | 
 | 53 | 	struct fec_info* fec = bus->priv; | 
| Scott Wood | 31a5bb0 | 2007-10-01 14:20:58 -0500 | [diff] [blame] | 54 | 	fec_t __iomem *fecp = fec->fecp; | 
| Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 55 | 	int i, ret = -1; | 
 | 56 |  | 
| Alexander Beregalov | 0ee904c | 2009-04-11 14:50:23 +0000 | [diff] [blame] | 57 | 	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] | 58 |  | 
 | 59 | 	/* Add PHY address to register command.  */ | 
 | 60 | 	out_be32(&fecp->fec_mii_data, (phy_id << 23) | mk_mii_read(location)); | 
 | 61 |  | 
 | 62 | 	for (i = 0; i < FEC_MII_LOOPS; i++) | 
 | 63 | 		if ((in_be32(&fecp->fec_ievent) & FEC_ENET_MII) != 0) | 
 | 64 | 			break; | 
 | 65 |  | 
 | 66 | 	if (i < FEC_MII_LOOPS) { | 
 | 67 | 		out_be32(&fecp->fec_ievent, FEC_ENET_MII); | 
 | 68 | 		ret = in_be32(&fecp->fec_mii_data) & 0xffff; | 
 | 69 | 	} | 
 | 70 |  | 
 | 71 | 	return ret; | 
| Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 72 | } | 
 | 73 |  | 
 | 74 | static int fs_enet_fec_mii_write(struct mii_bus *bus, int phy_id, int location, u16 val) | 
 | 75 | { | 
 | 76 | 	struct fec_info* fec = bus->priv; | 
| Scott Wood | 31a5bb0 | 2007-10-01 14:20:58 -0500 | [diff] [blame] | 77 | 	fec_t __iomem *fecp = fec->fecp; | 
| Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 78 | 	int i; | 
 | 79 |  | 
 | 80 | 	/* this must never happen */ | 
| Alexander Beregalov | 0ee904c | 2009-04-11 14:50:23 +0000 | [diff] [blame] | 81 | 	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] | 82 |  | 
 | 83 | 	/* Add PHY address to register command.  */ | 
 | 84 | 	out_be32(&fecp->fec_mii_data, (phy_id << 23) | mk_mii_write(location, val)); | 
 | 85 |  | 
 | 86 | 	for (i = 0; i < FEC_MII_LOOPS; i++) | 
 | 87 | 		if ((in_be32(&fecp->fec_ievent) & FEC_ENET_MII) != 0) | 
 | 88 | 			break; | 
 | 89 |  | 
 | 90 | 	if (i < FEC_MII_LOOPS) | 
 | 91 | 		out_be32(&fecp->fec_ievent, FEC_ENET_MII); | 
 | 92 |  | 
 | 93 | 	return 0; | 
 | 94 |  | 
 | 95 | } | 
 | 96 |  | 
 | 97 | static int fs_enet_fec_mii_reset(struct mii_bus *bus) | 
 | 98 | { | 
 | 99 | 	/* nothing here - for now */ | 
 | 100 | 	return 0; | 
 | 101 | } | 
 | 102 |  | 
| Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 103 | static int __devinit fs_enet_mdio_probe(struct of_device *ofdev, | 
 | 104 |                                         const struct of_device_id *match) | 
 | 105 | { | 
 | 106 | 	struct device_node *np = NULL; | 
 | 107 | 	struct resource res; | 
 | 108 | 	struct mii_bus *new_bus; | 
 | 109 | 	struct fec_info *fec; | 
 | 110 | 	int ret = -ENOMEM, i; | 
 | 111 |  | 
| Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 112 | 	new_bus = mdiobus_alloc(); | 
| Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 113 | 	if (!new_bus) | 
 | 114 | 		goto out; | 
 | 115 |  | 
 | 116 | 	fec = kzalloc(sizeof(struct fec_info), GFP_KERNEL); | 
 | 117 | 	if (!fec) | 
 | 118 | 		goto out_mii; | 
 | 119 |  | 
 | 120 | 	new_bus->priv = fec; | 
 | 121 | 	new_bus->name = "FEC MII Bus"; | 
 | 122 | 	new_bus->read = &fs_enet_fec_mii_read; | 
 | 123 | 	new_bus->write = &fs_enet_fec_mii_write; | 
 | 124 | 	new_bus->reset = &fs_enet_fec_mii_reset; | 
 | 125 |  | 
 | 126 | 	ret = of_address_to_resource(ofdev->node, 0, &res); | 
 | 127 | 	if (ret) | 
| Scott Wood | a86e2cb | 2008-05-02 13:42:41 -0500 | [diff] [blame] | 128 | 		goto out_res; | 
| Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 129 |  | 
| Andy Fleming | 9d9326d | 2008-04-09 19:38:13 -0500 | [diff] [blame] | 130 | 	snprintf(new_bus->id, MII_BUS_ID_SIZE, "%x", res.start); | 
| Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 131 |  | 
 | 132 | 	fec->fecp = ioremap(res.start, res.end - res.start + 1); | 
 | 133 | 	if (!fec->fecp) | 
 | 134 | 		goto out_fec; | 
 | 135 |  | 
 | 136 | 	fec->mii_speed = ((ppc_proc_freq + 4999999) / 5000000) << 1; | 
 | 137 |  | 
 | 138 | 	setbits32(&fec->fecp->fec_r_cntrl, FEC_RCNTRL_MII_MODE); | 
 | 139 | 	setbits32(&fec->fecp->fec_ecntrl, FEC_ECNTRL_PINMUX | | 
 | 140 | 	                                  FEC_ECNTRL_ETHER_EN); | 
 | 141 | 	out_be32(&fec->fecp->fec_ievent, FEC_ENET_MII); | 
 | 142 | 	out_be32(&fec->fecp->fec_mii_speed, fec->mii_speed); | 
 | 143 |  | 
 | 144 | 	new_bus->phy_mask = ~0; | 
 | 145 | 	new_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL); | 
 | 146 | 	if (!new_bus->irq) | 
 | 147 | 		goto out_unmap_regs; | 
 | 148 |  | 
| Lennert Buytenhek | 18ee49d | 2008-10-01 15:41:33 +0000 | [diff] [blame] | 149 | 	new_bus->parent = &ofdev->dev; | 
| Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 150 | 	dev_set_drvdata(&ofdev->dev, new_bus); | 
 | 151 |  | 
| Grant Likely | aa73832 | 2009-04-25 12:53:33 +0000 | [diff] [blame] | 152 | 	ret = of_mdiobus_register(new_bus, ofdev->node); | 
| Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 153 | 	if (ret) | 
 | 154 | 		goto out_free_irqs; | 
 | 155 |  | 
 | 156 | 	return 0; | 
 | 157 |  | 
 | 158 | out_free_irqs: | 
 | 159 | 	dev_set_drvdata(&ofdev->dev, NULL); | 
 | 160 | 	kfree(new_bus->irq); | 
 | 161 | out_unmap_regs: | 
 | 162 | 	iounmap(fec->fecp); | 
| Scott Wood | a86e2cb | 2008-05-02 13:42:41 -0500 | [diff] [blame] | 163 | out_res: | 
| Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 164 | out_fec: | 
 | 165 | 	kfree(fec); | 
 | 166 | out_mii: | 
| Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 167 | 	mdiobus_free(new_bus); | 
| Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 168 | out: | 
 | 169 | 	return ret; | 
 | 170 | } | 
 | 171 |  | 
 | 172 | static int fs_enet_mdio_remove(struct of_device *ofdev) | 
 | 173 | { | 
 | 174 | 	struct mii_bus *bus = dev_get_drvdata(&ofdev->dev); | 
 | 175 | 	struct fec_info *fec = bus->priv; | 
 | 176 |  | 
 | 177 | 	mdiobus_unregister(bus); | 
 | 178 | 	dev_set_drvdata(&ofdev->dev, NULL); | 
 | 179 | 	kfree(bus->irq); | 
 | 180 | 	iounmap(fec->fecp); | 
 | 181 | 	kfree(fec); | 
| Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 182 | 	mdiobus_free(bus); | 
| Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 183 |  | 
 | 184 | 	return 0; | 
 | 185 | } | 
 | 186 |  | 
 | 187 | static struct of_device_id fs_enet_mdio_fec_match[] = { | 
 | 188 | 	{ | 
 | 189 | 		.compatible = "fsl,pq1-fec-mdio", | 
 | 190 | 	}, | 
 | 191 | 	{}, | 
 | 192 | }; | 
 | 193 |  | 
 | 194 | static struct of_platform_driver fs_enet_fec_mdio_driver = { | 
 | 195 | 	.name = "fsl-fec-mdio", | 
 | 196 | 	.match_table = fs_enet_mdio_fec_match, | 
 | 197 | 	.probe = fs_enet_mdio_probe, | 
 | 198 | 	.remove = fs_enet_mdio_remove, | 
 | 199 | }; | 
 | 200 |  | 
 | 201 | static int fs_enet_mdio_fec_init(void) | 
 | 202 | { | 
 | 203 | 	return of_register_platform_driver(&fs_enet_fec_mdio_driver); | 
 | 204 | } | 
 | 205 |  | 
 | 206 | static void fs_enet_mdio_fec_exit(void) | 
 | 207 | { | 
 | 208 | 	of_unregister_platform_driver(&fs_enet_fec_mdio_driver); | 
 | 209 | } | 
 | 210 |  | 
 | 211 | module_init(fs_enet_mdio_fec_init); | 
 | 212 | module_exit(fs_enet_mdio_fec_exit); |