Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 1 | /* |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 2 | * Fixed MDIO bus (MDIO bus emulation with fixed PHYs) |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 3 | * |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 4 | * Author: Vitaly Bordug <vbordug@ru.mvista.com> |
| 5 | * Anton Vorontsov <avorontsov@ru.mvista.com> |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 6 | * |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 7 | * Copyright (c) 2006-2007 MontaVista Software, Inc. |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify it |
| 10 | * under the terms of the GNU General Public License as published by the |
| 11 | * Free Software Foundation; either version 2 of the License, or (at your |
| 12 | * option) any later version. |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 13 | */ |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 14 | |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 15 | #include <linux/kernel.h> |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 16 | #include <linux/module.h> |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 17 | #include <linux/platform_device.h> |
| 18 | #include <linux/list.h> |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 19 | #include <linux/mii.h> |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 20 | #include <linux/phy.h> |
Vitaly Bordug | 7c32f47 | 2007-08-10 14:05:16 -0700 | [diff] [blame] | 21 | #include <linux/phy_fixed.h> |
Dan Carpenter | 57401d5 | 2009-04-11 01:52:29 -0700 | [diff] [blame] | 22 | #include <linux/err.h> |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 23 | |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 24 | #define MII_REGS_NUM 29 |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 25 | |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 26 | struct fixed_mdio_bus { |
| 27 | int irqs[PHY_MAX_ADDR]; |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 28 | struct mii_bus *mii_bus; |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 29 | struct list_head phys; |
| 30 | }; |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 31 | |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 32 | struct fixed_phy { |
| 33 | int id; |
| 34 | u16 regs[MII_REGS_NUM]; |
| 35 | struct phy_device *phydev; |
| 36 | struct fixed_phy_status status; |
| 37 | int (*link_update)(struct net_device *, struct fixed_phy_status *); |
| 38 | struct list_head node; |
| 39 | }; |
| 40 | |
| 41 | static struct platform_device *pdev; |
| 42 | static struct fixed_mdio_bus platform_fmb = { |
| 43 | .phys = LIST_HEAD_INIT(platform_fmb.phys), |
| 44 | }; |
| 45 | |
| 46 | static int fixed_phy_update_regs(struct fixed_phy *fp) |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 47 | { |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 48 | u16 bmsr = BMSR_ANEGCAPABLE; |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 49 | u16 bmcr = 0; |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 50 | u16 lpagb = 0; |
| 51 | u16 lpa = 0; |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 52 | |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 53 | if (fp->status.duplex) { |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 54 | bmcr |= BMCR_FULLDPLX; |
| 55 | |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 56 | switch (fp->status.speed) { |
| 57 | case 1000: |
| 58 | bmsr |= BMSR_ESTATEN; |
| 59 | bmcr |= BMCR_SPEED1000; |
| 60 | lpagb |= LPA_1000FULL; |
| 61 | break; |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 62 | case 100: |
| 63 | bmsr |= BMSR_100FULL; |
| 64 | bmcr |= BMCR_SPEED100; |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 65 | lpa |= LPA_100FULL; |
Vitaly Bordug | 7c32f47 | 2007-08-10 14:05:16 -0700 | [diff] [blame] | 66 | break; |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 67 | case 10: |
| 68 | bmsr |= BMSR_10FULL; |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 69 | lpa |= LPA_10FULL; |
Vitaly Bordug | 7c32f47 | 2007-08-10 14:05:16 -0700 | [diff] [blame] | 70 | break; |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 71 | default: |
| 72 | printk(KERN_WARNING "fixed phy: unknown speed\n"); |
| 73 | return -EINVAL; |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 74 | } |
| 75 | } else { |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 76 | switch (fp->status.speed) { |
| 77 | case 1000: |
| 78 | bmsr |= BMSR_ESTATEN; |
| 79 | bmcr |= BMCR_SPEED1000; |
| 80 | lpagb |= LPA_1000HALF; |
| 81 | break; |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 82 | case 100: |
| 83 | bmsr |= BMSR_100HALF; |
| 84 | bmcr |= BMCR_SPEED100; |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 85 | lpa |= LPA_100HALF; |
Vitaly Bordug | 7c32f47 | 2007-08-10 14:05:16 -0700 | [diff] [blame] | 86 | break; |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 87 | case 10: |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 88 | bmsr |= BMSR_10HALF; |
| 89 | lpa |= LPA_10HALF; |
Vitaly Bordug | 7c32f47 | 2007-08-10 14:05:16 -0700 | [diff] [blame] | 90 | break; |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 91 | default: |
| 92 | printk(KERN_WARNING "fixed phy: unknown speed\n"); |
| 93 | return -EINVAL; |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 97 | if (fp->status.link) |
| 98 | bmsr |= BMSR_LSTATUS | BMSR_ANEGCOMPLETE; |
| 99 | |
| 100 | if (fp->status.pause) |
| 101 | lpa |= LPA_PAUSE_CAP; |
| 102 | |
| 103 | if (fp->status.asym_pause) |
| 104 | lpa |= LPA_PAUSE_ASYM; |
| 105 | |
| 106 | fp->regs[MII_PHYSID1] = fp->id >> 16; |
| 107 | fp->regs[MII_PHYSID2] = fp->id; |
| 108 | |
| 109 | fp->regs[MII_BMSR] = bmsr; |
| 110 | fp->regs[MII_BMCR] = bmcr; |
| 111 | fp->regs[MII_LPA] = lpa; |
| 112 | fp->regs[MII_STAT1000] = lpagb; |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 117 | static int fixed_mdio_read(struct mii_bus *bus, int phy_id, int reg_num) |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 118 | { |
Lennert Buytenhek | ec2a565 | 2008-10-09 09:45:04 -0700 | [diff] [blame] | 119 | struct fixed_mdio_bus *fmb = bus->priv; |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 120 | struct fixed_phy *fp; |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 121 | |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 122 | if (reg_num >= MII_REGS_NUM) |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 123 | return -1; |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 124 | |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 125 | list_for_each_entry(fp, &fmb->phys, node) { |
| 126 | if (fp->id == phy_id) { |
| 127 | /* Issue callback if user registered it. */ |
| 128 | if (fp->link_update) { |
| 129 | fp->link_update(fp->phydev->attached_dev, |
| 130 | &fp->status); |
| 131 | fixed_phy_update_regs(fp); |
| 132 | } |
| 133 | return fp->regs[reg_num]; |
| 134 | } |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 137 | return 0xFFFF; |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 140 | static int fixed_mdio_write(struct mii_bus *bus, int phy_id, int reg_num, |
| 141 | u16 val) |
| 142 | { |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | /* |
| 147 | * If something weird is required to be done with link/speed, |
| 148 | * network driver is able to assign a function to implement this. |
| 149 | * May be useful for PHY's that need to be software-driven. |
| 150 | */ |
| 151 | int fixed_phy_set_link_update(struct phy_device *phydev, |
| 152 | int (*link_update)(struct net_device *, |
| 153 | struct fixed_phy_status *)) |
| 154 | { |
| 155 | struct fixed_mdio_bus *fmb = &platform_fmb; |
| 156 | struct fixed_phy *fp; |
| 157 | |
| 158 | if (!link_update || !phydev || !phydev->bus) |
| 159 | return -EINVAL; |
| 160 | |
| 161 | list_for_each_entry(fp, &fmb->phys, node) { |
| 162 | if (fp->id == phydev->phy_id) { |
| 163 | fp->link_update = link_update; |
| 164 | fp->phydev = phydev; |
| 165 | return 0; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | return -ENOENT; |
| 170 | } |
| 171 | EXPORT_SYMBOL_GPL(fixed_phy_set_link_update); |
| 172 | |
| 173 | int fixed_phy_add(unsigned int irq, int phy_id, |
| 174 | struct fixed_phy_status *status) |
| 175 | { |
| 176 | int ret; |
| 177 | struct fixed_mdio_bus *fmb = &platform_fmb; |
| 178 | struct fixed_phy *fp; |
| 179 | |
| 180 | fp = kzalloc(sizeof(*fp), GFP_KERNEL); |
| 181 | if (!fp) |
| 182 | return -ENOMEM; |
| 183 | |
| 184 | memset(fp->regs, 0xFF, sizeof(fp->regs[0]) * MII_REGS_NUM); |
| 185 | |
| 186 | fmb->irqs[phy_id] = irq; |
| 187 | |
| 188 | fp->id = phy_id; |
| 189 | fp->status = *status; |
| 190 | |
| 191 | ret = fixed_phy_update_regs(fp); |
| 192 | if (ret) |
| 193 | goto err_regs; |
| 194 | |
| 195 | list_add_tail(&fp->node, &fmb->phys); |
| 196 | |
| 197 | return 0; |
| 198 | |
| 199 | err_regs: |
| 200 | kfree(fp); |
| 201 | return ret; |
| 202 | } |
| 203 | EXPORT_SYMBOL_GPL(fixed_phy_add); |
| 204 | |
| 205 | static int __init fixed_mdio_bus_init(void) |
| 206 | { |
| 207 | struct fixed_mdio_bus *fmb = &platform_fmb; |
| 208 | int ret; |
| 209 | |
| 210 | pdev = platform_device_register_simple("Fixed MDIO bus", 0, NULL, 0); |
Dan Carpenter | 57401d5 | 2009-04-11 01:52:29 -0700 | [diff] [blame] | 211 | if (IS_ERR(pdev)) { |
| 212 | ret = PTR_ERR(pdev); |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 213 | goto err_pdev; |
| 214 | } |
| 215 | |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 216 | fmb->mii_bus = mdiobus_alloc(); |
| 217 | if (fmb->mii_bus == NULL) { |
| 218 | ret = -ENOMEM; |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 219 | goto err_mdiobus_reg; |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | snprintf(fmb->mii_bus->id, MII_BUS_ID_SIZE, "0"); |
| 223 | fmb->mii_bus->name = "Fixed MDIO Bus"; |
Lennert Buytenhek | ec2a565 | 2008-10-09 09:45:04 -0700 | [diff] [blame] | 224 | fmb->mii_bus->priv = fmb; |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 225 | fmb->mii_bus->parent = &pdev->dev; |
| 226 | fmb->mii_bus->read = &fixed_mdio_read; |
| 227 | fmb->mii_bus->write = &fixed_mdio_write; |
| 228 | fmb->mii_bus->irq = fmb->irqs; |
| 229 | |
| 230 | ret = mdiobus_register(fmb->mii_bus); |
| 231 | if (ret) |
| 232 | goto err_mdiobus_alloc; |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 233 | |
| 234 | return 0; |
| 235 | |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 236 | err_mdiobus_alloc: |
| 237 | mdiobus_free(fmb->mii_bus); |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 238 | err_mdiobus_reg: |
| 239 | platform_device_unregister(pdev); |
| 240 | err_pdev: |
| 241 | return ret; |
| 242 | } |
| 243 | module_init(fixed_mdio_bus_init); |
| 244 | |
| 245 | static void __exit fixed_mdio_bus_exit(void) |
| 246 | { |
| 247 | struct fixed_mdio_bus *fmb = &platform_fmb; |
Adrian Bunk | 651be3a | 2008-02-02 23:15:02 +0200 | [diff] [blame] | 248 | struct fixed_phy *fp, *tmp; |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 249 | |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 250 | mdiobus_unregister(fmb->mii_bus); |
| 251 | mdiobus_free(fmb->mii_bus); |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 252 | platform_device_unregister(pdev); |
| 253 | |
Adrian Bunk | 651be3a | 2008-02-02 23:15:02 +0200 | [diff] [blame] | 254 | list_for_each_entry_safe(fp, tmp, &fmb->phys, node) { |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 255 | list_del(&fp->node); |
| 256 | kfree(fp); |
| 257 | } |
| 258 | } |
| 259 | module_exit(fixed_mdio_bus_exit); |
| 260 | |
| 261 | MODULE_DESCRIPTION("Fixed MDIO bus (MDIO bus emulation with fixed PHYs)"); |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 262 | MODULE_AUTHOR("Vitaly Bordug"); |
| 263 | MODULE_LICENSE("GPL"); |