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