| Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 1 | /* | 
|  | 2 | * Combined Ethernet driver for Motorola MPC8xx and MPC82xx. | 
|  | 3 | * | 
| Vitaly Bordug | 9b8ee8e | 2007-09-18 20:05:35 +0400 | [diff] [blame] | 4 | * Copyright (c) 2003 Intracom S.A. | 
| Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 5 | *  by Pantelis Antoniou <panto@intracom.gr> | 
| Vitaly Bordug | 9b8ee8e | 2007-09-18 20:05:35 +0400 | [diff] [blame] | 6 | * | 
|  | 7 | * 2005 (c) MontaVista Software, Inc. | 
| Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 8 | * Vitaly Bordug <vbordug@ru.mvista.com> | 
|  | 9 | * | 
| Vitaly Bordug | 9b8ee8e | 2007-09-18 20:05:35 +0400 | [diff] [blame] | 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 | 
| Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 12 | * kind, whether express or implied. | 
|  | 13 | */ | 
|  | 14 |  | 
| Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 15 | #include <linux/module.h> | 
| Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 16 | #include <linux/ioport.h> | 
|  | 17 | #include <linux/slab.h> | 
| Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 18 | #include <linux/init.h> | 
| Scott Wood | 2b5b3a6 | 2007-10-01 14:20:57 -0500 | [diff] [blame] | 19 | #include <linux/interrupt.h> | 
| Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 20 | #include <linux/netdevice.h> | 
|  | 21 | #include <linux/etherdevice.h> | 
| Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 22 | #include <linux/mii.h> | 
| Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 23 | #include <linux/platform_device.h> | 
| Scott Wood | 2b5b3a6 | 2007-10-01 14:20:57 -0500 | [diff] [blame] | 24 | #include <linux/mdio-bitbang.h> | 
| Grant Likely | aa73832 | 2009-04-25 12:53:33 +0000 | [diff] [blame] | 25 | #include <linux/of_mdio.h> | 
| Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 26 | #include <linux/of_platform.h> | 
| Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 27 |  | 
|  | 28 | #include "fs_enet.h" | 
|  | 29 |  | 
| Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 30 | struct bb_info { | 
| Scott Wood | 2b5b3a6 | 2007-10-01 14:20:57 -0500 | [diff] [blame] | 31 | struct mdiobb_ctrl ctrl; | 
| Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 32 | __be32 __iomem *dir; | 
|  | 33 | __be32 __iomem *dat; | 
|  | 34 | u32 mdio_msk; | 
|  | 35 | u32 mdc_msk; | 
| Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 36 | }; | 
|  | 37 |  | 
|  | 38 | /* FIXME: If any other users of GPIO crop up, then these will have to | 
|  | 39 | * have some sort of global synchronization to avoid races with other | 
|  | 40 | * pins on the same port.  The ideal solution would probably be to | 
|  | 41 | * bind the ports to a GPIO driver, and have this be a client of it. | 
|  | 42 | */ | 
|  | 43 | static inline void bb_set(u32 __iomem *p, u32 m) | 
| Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 44 | { | 
| Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 45 | out_be32(p, in_be32(p) | m); | 
| Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 46 | } | 
| Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 47 |  | 
| Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 48 | static inline void bb_clr(u32 __iomem *p, u32 m) | 
| Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 49 | { | 
| Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 50 | out_be32(p, in_be32(p) & ~m); | 
| Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 51 | } | 
|  | 52 |  | 
| Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 53 | static inline int bb_read(u32 __iomem *p, u32 m) | 
| Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 54 | { | 
| Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 55 | return (in_be32(p) & m) != 0; | 
| Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 56 | } | 
|  | 57 |  | 
| Scott Wood | 2b5b3a6 | 2007-10-01 14:20:57 -0500 | [diff] [blame] | 58 | static inline void mdio_dir(struct mdiobb_ctrl *ctrl, int dir) | 
| Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 59 | { | 
| Scott Wood | 2b5b3a6 | 2007-10-01 14:20:57 -0500 | [diff] [blame] | 60 | struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl); | 
|  | 61 |  | 
|  | 62 | if (dir) | 
|  | 63 | bb_set(bitbang->dir, bitbang->mdio_msk); | 
|  | 64 | else | 
|  | 65 | bb_clr(bitbang->dir, bitbang->mdio_msk); | 
|  | 66 |  | 
|  | 67 | /* Read back to flush the write. */ | 
|  | 68 | in_be32(bitbang->dir); | 
| Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 69 | } | 
|  | 70 |  | 
| Scott Wood | 2b5b3a6 | 2007-10-01 14:20:57 -0500 | [diff] [blame] | 71 | static inline int mdio_read(struct mdiobb_ctrl *ctrl) | 
| Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 72 | { | 
| Scott Wood | 2b5b3a6 | 2007-10-01 14:20:57 -0500 | [diff] [blame] | 73 | struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl); | 
| Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 74 | return bb_read(bitbang->dat, bitbang->mdio_msk); | 
| Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 75 | } | 
|  | 76 |  | 
| Scott Wood | 2b5b3a6 | 2007-10-01 14:20:57 -0500 | [diff] [blame] | 77 | static inline void mdio(struct mdiobb_ctrl *ctrl, int what) | 
| Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 78 | { | 
| Scott Wood | 2b5b3a6 | 2007-10-01 14:20:57 -0500 | [diff] [blame] | 79 | struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl); | 
|  | 80 |  | 
| Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 81 | if (what) | 
| Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 82 | bb_set(bitbang->dat, bitbang->mdio_msk); | 
| Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 83 | else | 
| Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 84 | bb_clr(bitbang->dat, bitbang->mdio_msk); | 
| Scott Wood | 2b5b3a6 | 2007-10-01 14:20:57 -0500 | [diff] [blame] | 85 |  | 
|  | 86 | /* Read back to flush the write. */ | 
|  | 87 | in_be32(bitbang->dat); | 
| Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 88 | } | 
|  | 89 |  | 
| Scott Wood | 2b5b3a6 | 2007-10-01 14:20:57 -0500 | [diff] [blame] | 90 | static inline void mdc(struct mdiobb_ctrl *ctrl, int what) | 
| Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 91 | { | 
| Scott Wood | 2b5b3a6 | 2007-10-01 14:20:57 -0500 | [diff] [blame] | 92 | struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl); | 
|  | 93 |  | 
| Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 94 | if (what) | 
| Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 95 | bb_set(bitbang->dat, bitbang->mdc_msk); | 
| Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 96 | else | 
| Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 97 | bb_clr(bitbang->dat, bitbang->mdc_msk); | 
| Scott Wood | 2b5b3a6 | 2007-10-01 14:20:57 -0500 | [diff] [blame] | 98 |  | 
|  | 99 | /* Read back to flush the write. */ | 
|  | 100 | in_be32(bitbang->dat); | 
| Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 101 | } | 
|  | 102 |  | 
| Scott Wood | 2b5b3a6 | 2007-10-01 14:20:57 -0500 | [diff] [blame] | 103 | static struct mdiobb_ops bb_ops = { | 
|  | 104 | .owner = THIS_MODULE, | 
|  | 105 | .set_mdc = mdc, | 
|  | 106 | .set_mdio_dir = mdio_dir, | 
|  | 107 | .set_mdio_data = mdio, | 
|  | 108 | .get_mdio_data = mdio_read, | 
|  | 109 | }; | 
| Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 110 |  | 
| Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 111 | static int __devinit fs_mii_bitbang_init(struct mii_bus *bus, | 
|  | 112 | struct device_node *np) | 
| Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 113 | { | 
| Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 114 | struct resource res; | 
|  | 115 | const u32 *data; | 
|  | 116 | int mdio_pin, mdc_pin, len; | 
|  | 117 | struct bb_info *bitbang = bus->priv; | 
| Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 118 |  | 
| Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 119 | int ret = of_address_to_resource(np, 0, &res); | 
|  | 120 | if (ret) | 
|  | 121 | return ret; | 
|  | 122 |  | 
|  | 123 | if (res.end - res.start < 13) | 
|  | 124 | return -ENODEV; | 
|  | 125 |  | 
|  | 126 | /* This should really encode the pin number as well, but all | 
|  | 127 | * we get is an int, and the odds of multiple bitbang mdio buses | 
|  | 128 | * is low enough that it's not worth going too crazy. | 
|  | 129 | */ | 
| Andy Fleming | 9d9326d | 2008-04-09 19:38:13 -0500 | [diff] [blame] | 130 | snprintf(bus->id, MII_BUS_ID_SIZE, "%x", res.start); | 
| Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 131 |  | 
|  | 132 | data = of_get_property(np, "fsl,mdio-pin", &len); | 
|  | 133 | if (!data || len != 4) | 
|  | 134 | return -ENODEV; | 
|  | 135 | mdio_pin = *data; | 
|  | 136 |  | 
|  | 137 | data = of_get_property(np, "fsl,mdc-pin", &len); | 
|  | 138 | if (!data || len != 4) | 
|  | 139 | return -ENODEV; | 
|  | 140 | mdc_pin = *data; | 
|  | 141 |  | 
|  | 142 | bitbang->dir = ioremap(res.start, res.end - res.start + 1); | 
|  | 143 | if (!bitbang->dir) | 
|  | 144 | return -ENOMEM; | 
|  | 145 |  | 
|  | 146 | bitbang->dat = bitbang->dir + 4; | 
|  | 147 | bitbang->mdio_msk = 1 << (31 - mdio_pin); | 
|  | 148 | bitbang->mdc_msk = 1 << (31 - mdc_pin); | 
| Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 149 |  | 
|  | 150 | return 0; | 
|  | 151 | } | 
|  | 152 |  | 
| Grant Likely | 7488876 | 2011-02-22 21:05:51 -0700 | [diff] [blame] | 153 | static int __devinit fs_enet_mdio_probe(struct platform_device *ofdev) | 
| Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 154 | { | 
| Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 155 | struct mii_bus *new_bus; | 
|  | 156 | struct bb_info *bitbang; | 
|  | 157 | int ret = -ENOMEM; | 
| Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 158 |  | 
| Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 159 | bitbang = kzalloc(sizeof(struct bb_info), GFP_KERNEL); | 
|  | 160 | if (!bitbang) | 
| Scott Wood | 2b5b3a6 | 2007-10-01 14:20:57 -0500 | [diff] [blame] | 161 | goto out; | 
| Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 162 |  | 
| Scott Wood | 2b5b3a6 | 2007-10-01 14:20:57 -0500 | [diff] [blame] | 163 | bitbang->ctrl.ops = &bb_ops; | 
|  | 164 |  | 
|  | 165 | new_bus = alloc_mdio_bitbang(&bitbang->ctrl); | 
|  | 166 | if (!new_bus) | 
|  | 167 | goto out_free_priv; | 
|  | 168 |  | 
| Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 169 | new_bus->name = "CPM2 Bitbanged MII", | 
| Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 170 |  | 
| Anatolij Gustschin | 4eecb178 | 2010-06-03 03:06:54 +0200 | [diff] [blame] | 171 | ret = fs_mii_bitbang_init(new_bus, ofdev->dev.of_node); | 
| Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 172 | if (ret) | 
| Scott Wood | 2b5b3a6 | 2007-10-01 14:20:57 -0500 | [diff] [blame] | 173 | goto out_free_bus; | 
| Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 174 |  | 
|  | 175 | new_bus->phy_mask = ~0; | 
|  | 176 | new_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL); | 
|  | 177 | if (!new_bus->irq) | 
|  | 178 | goto out_unmap_regs; | 
|  | 179 |  | 
| Lennert Buytenhek | 18ee49d | 2008-10-01 15:41:33 +0000 | [diff] [blame] | 180 | new_bus->parent = &ofdev->dev; | 
| Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 181 | dev_set_drvdata(&ofdev->dev, new_bus); | 
|  | 182 |  | 
| Anatolij Gustschin | 4eecb178 | 2010-06-03 03:06:54 +0200 | [diff] [blame] | 183 | ret = of_mdiobus_register(new_bus, ofdev->dev.of_node); | 
| Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 184 | if (ret) | 
|  | 185 | goto out_free_irqs; | 
|  | 186 |  | 
|  | 187 | return 0; | 
|  | 188 |  | 
|  | 189 | out_free_irqs: | 
|  | 190 | dev_set_drvdata(&ofdev->dev, NULL); | 
|  | 191 | kfree(new_bus->irq); | 
|  | 192 | out_unmap_regs: | 
|  | 193 | iounmap(bitbang->dir); | 
| Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 194 | out_free_bus: | 
| Scott Wood | 2b5b3a6 | 2007-10-01 14:20:57 -0500 | [diff] [blame] | 195 | free_mdio_bitbang(new_bus); | 
| Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 196 | out_free_priv: | 
|  | 197 | kfree(bitbang); | 
| Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 198 | out: | 
|  | 199 | return ret; | 
|  | 200 | } | 
|  | 201 |  | 
| Grant Likely | 2dc1158 | 2010-08-06 09:25:50 -0600 | [diff] [blame] | 202 | static int fs_enet_mdio_remove(struct platform_device *ofdev) | 
| Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 203 | { | 
|  | 204 | struct mii_bus *bus = dev_get_drvdata(&ofdev->dev); | 
|  | 205 | struct bb_info *bitbang = bus->priv; | 
|  | 206 |  | 
|  | 207 | mdiobus_unregister(bus); | 
|  | 208 | dev_set_drvdata(&ofdev->dev, NULL); | 
|  | 209 | kfree(bus->irq); | 
| Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 210 | free_mdio_bitbang(bus); | 
| Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 211 | iounmap(bitbang->dir); | 
|  | 212 | kfree(bitbang); | 
| Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 213 |  | 
|  | 214 | return 0; | 
|  | 215 | } | 
|  | 216 |  | 
|  | 217 | static struct of_device_id fs_enet_mdio_bb_match[] = { | 
|  | 218 | { | 
|  | 219 | .compatible = "fsl,cpm2-mdio-bitbang", | 
|  | 220 | }, | 
|  | 221 | {}, | 
|  | 222 | }; | 
| Anton Vorontsov | e72701a | 2009-10-14 14:54:52 -0700 | [diff] [blame] | 223 | MODULE_DEVICE_TABLE(of, fs_enet_mdio_bb_match); | 
| Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 224 |  | 
| Grant Likely | 7488876 | 2011-02-22 21:05:51 -0700 | [diff] [blame] | 225 | static struct platform_driver fs_enet_bb_mdio_driver = { | 
| Grant Likely | 4018294 | 2010-04-13 16:13:02 -0700 | [diff] [blame] | 226 | .driver = { | 
|  | 227 | .name = "fsl-bb-mdio", | 
|  | 228 | .owner = THIS_MODULE, | 
|  | 229 | .of_match_table = fs_enet_mdio_bb_match, | 
|  | 230 | }, | 
| Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 231 | .probe = fs_enet_mdio_probe, | 
|  | 232 | .remove = fs_enet_mdio_remove, | 
|  | 233 | }; | 
|  | 234 |  | 
| Scott Wood | 2b5b3a6 | 2007-10-01 14:20:57 -0500 | [diff] [blame] | 235 | static int fs_enet_mdio_bb_init(void) | 
| Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 236 | { | 
| Grant Likely | 7488876 | 2011-02-22 21:05:51 -0700 | [diff] [blame] | 237 | return platform_driver_register(&fs_enet_bb_mdio_driver); | 
| Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 238 | } | 
|  | 239 |  | 
| Scott Wood | 2b5b3a6 | 2007-10-01 14:20:57 -0500 | [diff] [blame] | 240 | static void fs_enet_mdio_bb_exit(void) | 
| Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 241 | { | 
| Grant Likely | 7488876 | 2011-02-22 21:05:51 -0700 | [diff] [blame] | 242 | platform_driver_unregister(&fs_enet_bb_mdio_driver); | 
| Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 243 | } | 
|  | 244 |  | 
|  | 245 | module_init(fs_enet_mdio_bb_init); | 
|  | 246 | module_exit(fs_enet_mdio_bb_exit); |