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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 23 | #include <linux/slab.h> |
Thomas Petazzoni | a759512 | 2014-05-16 16:14:04 +0200 | [diff] [blame] | 24 | #include <linux/of.h> |
Andrew Lunn | a559700 | 2015-08-31 15:56:53 +0200 | [diff] [blame] | 25 | #include <linux/gpio.h> |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 26 | |
Russell King | 5ae68b0 | 2016-06-23 14:50:05 +0100 | [diff] [blame] | 27 | #include "swphy.h" |
| 28 | |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 29 | #define MII_REGS_NUM 29 |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 30 | |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 31 | struct fixed_mdio_bus { |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 32 | struct mii_bus *mii_bus; |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 33 | struct list_head phys; |
| 34 | }; |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 35 | |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 36 | struct fixed_phy { |
Thomas Petazzoni | 9b74494 | 2014-05-16 16:14:03 +0200 | [diff] [blame] | 37 | int addr; |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 38 | u16 regs[MII_REGS_NUM]; |
| 39 | struct phy_device *phydev; |
| 40 | struct fixed_phy_status status; |
| 41 | int (*link_update)(struct net_device *, struct fixed_phy_status *); |
| 42 | struct list_head node; |
Andrew Lunn | a559700 | 2015-08-31 15:56:53 +0200 | [diff] [blame] | 43 | int link_gpio; |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | static struct platform_device *pdev; |
| 47 | static struct fixed_mdio_bus platform_fmb = { |
| 48 | .phys = LIST_HEAD_INIT(platform_fmb.phys), |
| 49 | }; |
| 50 | |
Russell King | 68888ce | 2016-06-23 14:50:15 +0100 | [diff] [blame^] | 51 | static void fixed_phy_update_regs(struct fixed_phy *fp) |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 52 | { |
Andrew Lunn | a559700 | 2015-08-31 15:56:53 +0200 | [diff] [blame] | 53 | if (gpio_is_valid(fp->link_gpio)) |
| 54 | fp->status.link = !!gpio_get_value_cansleep(fp->link_gpio); |
| 55 | |
Russell King | 68888ce | 2016-06-23 14:50:15 +0100 | [diff] [blame^] | 56 | swphy_update_regs(fp->regs, &fp->status); |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Thomas Petazzoni | 9b74494 | 2014-05-16 16:14:03 +0200 | [diff] [blame] | 59 | static int fixed_mdio_read(struct mii_bus *bus, int phy_addr, int reg_num) |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 60 | { |
Lennert Buytenhek | ec2a565 | 2008-10-09 09:45:04 -0700 | [diff] [blame] | 61 | struct fixed_mdio_bus *fmb = bus->priv; |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 62 | struct fixed_phy *fp; |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 63 | |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 64 | if (reg_num >= MII_REGS_NUM) |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 65 | return -1; |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 66 | |
Florian Fainelli | a2dbba7 | 2014-08-22 18:55:42 -0700 | [diff] [blame] | 67 | /* We do not support emulating Clause 45 over Clause 22 register reads |
| 68 | * return an error instead of bogus data. |
| 69 | */ |
| 70 | switch (reg_num) { |
| 71 | case MII_MMD_CTRL: |
| 72 | case MII_MMD_DATA: |
| 73 | return -1; |
| 74 | default: |
| 75 | break; |
| 76 | } |
| 77 | |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 78 | list_for_each_entry(fp, &fmb->phys, node) { |
Thomas Petazzoni | 9b74494 | 2014-05-16 16:14:03 +0200 | [diff] [blame] | 79 | if (fp->addr == phy_addr) { |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 80 | /* Issue callback if user registered it. */ |
| 81 | if (fp->link_update) { |
| 82 | fp->link_update(fp->phydev->attached_dev, |
| 83 | &fp->status); |
| 84 | fixed_phy_update_regs(fp); |
| 85 | } |
| 86 | return fp->regs[reg_num]; |
| 87 | } |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 90 | return 0xFFFF; |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Thomas Petazzoni | 9b74494 | 2014-05-16 16:14:03 +0200 | [diff] [blame] | 93 | static int fixed_mdio_write(struct mii_bus *bus, int phy_addr, int reg_num, |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 94 | u16 val) |
| 95 | { |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | /* |
| 100 | * If something weird is required to be done with link/speed, |
| 101 | * network driver is able to assign a function to implement this. |
| 102 | * May be useful for PHY's that need to be software-driven. |
| 103 | */ |
| 104 | int fixed_phy_set_link_update(struct phy_device *phydev, |
| 105 | int (*link_update)(struct net_device *, |
| 106 | struct fixed_phy_status *)) |
| 107 | { |
| 108 | struct fixed_mdio_bus *fmb = &platform_fmb; |
| 109 | struct fixed_phy *fp; |
| 110 | |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 111 | if (!phydev || !phydev->mdio.bus) |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 112 | return -EINVAL; |
| 113 | |
| 114 | list_for_each_entry(fp, &fmb->phys, node) { |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 115 | if (fp->addr == phydev->mdio.addr) { |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 116 | fp->link_update = link_update; |
| 117 | fp->phydev = phydev; |
| 118 | return 0; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | return -ENOENT; |
| 123 | } |
| 124 | EXPORT_SYMBOL_GPL(fixed_phy_set_link_update); |
| 125 | |
Stas Sergeev | a3bebdc | 2015-04-01 20:30:31 +0300 | [diff] [blame] | 126 | int fixed_phy_update_state(struct phy_device *phydev, |
| 127 | const struct fixed_phy_status *status, |
| 128 | const struct fixed_phy_status *changed) |
| 129 | { |
| 130 | struct fixed_mdio_bus *fmb = &platform_fmb; |
| 131 | struct fixed_phy *fp; |
| 132 | |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 133 | if (!phydev || phydev->mdio.bus != fmb->mii_bus) |
Stas Sergeev | a3bebdc | 2015-04-01 20:30:31 +0300 | [diff] [blame] | 134 | return -EINVAL; |
| 135 | |
| 136 | list_for_each_entry(fp, &fmb->phys, node) { |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 137 | if (fp->addr == phydev->mdio.addr) { |
Stas Sergeev | a3bebdc | 2015-04-01 20:30:31 +0300 | [diff] [blame] | 138 | #define _UPD(x) if (changed->x) \ |
| 139 | fp->status.x = status->x |
| 140 | _UPD(link); |
| 141 | _UPD(speed); |
| 142 | _UPD(duplex); |
| 143 | _UPD(pause); |
| 144 | _UPD(asym_pause); |
| 145 | #undef _UPD |
| 146 | fixed_phy_update_regs(fp); |
| 147 | return 0; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | return -ENOENT; |
| 152 | } |
| 153 | EXPORT_SYMBOL(fixed_phy_update_state); |
| 154 | |
Thomas Petazzoni | 9b74494 | 2014-05-16 16:14:03 +0200 | [diff] [blame] | 155 | int fixed_phy_add(unsigned int irq, int phy_addr, |
Andrew Lunn | a559700 | 2015-08-31 15:56:53 +0200 | [diff] [blame] | 156 | struct fixed_phy_status *status, |
| 157 | int link_gpio) |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 158 | { |
| 159 | int ret; |
| 160 | struct fixed_mdio_bus *fmb = &platform_fmb; |
| 161 | struct fixed_phy *fp; |
| 162 | |
Russell King | 68888ce | 2016-06-23 14:50:15 +0100 | [diff] [blame^] | 163 | ret = swphy_validate_state(status); |
| 164 | if (ret < 0) |
| 165 | return ret; |
| 166 | |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 167 | fp = kzalloc(sizeof(*fp), GFP_KERNEL); |
| 168 | if (!fp) |
| 169 | return -ENOMEM; |
| 170 | |
| 171 | memset(fp->regs, 0xFF, sizeof(fp->regs[0]) * MII_REGS_NUM); |
| 172 | |
Rabin Vincent | 185be5a | 2016-05-18 12:47:31 +0200 | [diff] [blame] | 173 | if (irq != PHY_POLL) |
| 174 | fmb->mii_bus->irq[phy_addr] = irq; |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 175 | |
Thomas Petazzoni | 9b74494 | 2014-05-16 16:14:03 +0200 | [diff] [blame] | 176 | fp->addr = phy_addr; |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 177 | fp->status = *status; |
Andrew Lunn | a559700 | 2015-08-31 15:56:53 +0200 | [diff] [blame] | 178 | fp->link_gpio = link_gpio; |
| 179 | |
| 180 | if (gpio_is_valid(fp->link_gpio)) { |
| 181 | ret = gpio_request_one(fp->link_gpio, GPIOF_DIR_IN, |
| 182 | "fixed-link-gpio-link"); |
| 183 | if (ret) |
| 184 | goto err_regs; |
| 185 | } |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 186 | |
Russell King | 68888ce | 2016-06-23 14:50:15 +0100 | [diff] [blame^] | 187 | fixed_phy_update_regs(fp); |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 188 | |
| 189 | list_add_tail(&fp->node, &fmb->phys); |
| 190 | |
| 191 | return 0; |
| 192 | |
| 193 | err_regs: |
| 194 | kfree(fp); |
| 195 | return ret; |
| 196 | } |
| 197 | EXPORT_SYMBOL_GPL(fixed_phy_add); |
| 198 | |
Andrew Lunn | 5bcbe0f | 2016-03-12 00:01:40 +0100 | [diff] [blame] | 199 | static void fixed_phy_del(int phy_addr) |
Thomas Petazzoni | a759512 | 2014-05-16 16:14:04 +0200 | [diff] [blame] | 200 | { |
| 201 | struct fixed_mdio_bus *fmb = &platform_fmb; |
| 202 | struct fixed_phy *fp, *tmp; |
| 203 | |
| 204 | list_for_each_entry_safe(fp, tmp, &fmb->phys, node) { |
| 205 | if (fp->addr == phy_addr) { |
| 206 | list_del(&fp->node); |
Andrew Lunn | a559700 | 2015-08-31 15:56:53 +0200 | [diff] [blame] | 207 | if (gpio_is_valid(fp->link_gpio)) |
| 208 | gpio_free(fp->link_gpio); |
Thomas Petazzoni | a759512 | 2014-05-16 16:14:04 +0200 | [diff] [blame] | 209 | kfree(fp); |
| 210 | return; |
| 211 | } |
| 212 | } |
| 213 | } |
Thomas Petazzoni | a759512 | 2014-05-16 16:14:04 +0200 | [diff] [blame] | 214 | |
| 215 | static int phy_fixed_addr; |
| 216 | static DEFINE_SPINLOCK(phy_fixed_addr_lock); |
| 217 | |
Petri Gynther | fd2ef0b | 2014-10-06 11:38:30 -0700 | [diff] [blame] | 218 | struct phy_device *fixed_phy_register(unsigned int irq, |
| 219 | struct fixed_phy_status *status, |
Andrew Lunn | a559700 | 2015-08-31 15:56:53 +0200 | [diff] [blame] | 220 | int link_gpio, |
Petri Gynther | fd2ef0b | 2014-10-06 11:38:30 -0700 | [diff] [blame] | 221 | struct device_node *np) |
Thomas Petazzoni | a759512 | 2014-05-16 16:14:04 +0200 | [diff] [blame] | 222 | { |
| 223 | struct fixed_mdio_bus *fmb = &platform_fmb; |
| 224 | struct phy_device *phy; |
| 225 | int phy_addr; |
| 226 | int ret; |
| 227 | |
Rabin Vincent | 185be5a | 2016-05-18 12:47:31 +0200 | [diff] [blame] | 228 | if (!fmb->mii_bus || fmb->mii_bus->state != MDIOBUS_REGISTERED) |
| 229 | return ERR_PTR(-EPROBE_DEFER); |
| 230 | |
Thomas Petazzoni | a759512 | 2014-05-16 16:14:04 +0200 | [diff] [blame] | 231 | /* Get the next available PHY address, up to PHY_MAX_ADDR */ |
| 232 | spin_lock(&phy_fixed_addr_lock); |
| 233 | if (phy_fixed_addr == PHY_MAX_ADDR) { |
| 234 | spin_unlock(&phy_fixed_addr_lock); |
Petri Gynther | fd2ef0b | 2014-10-06 11:38:30 -0700 | [diff] [blame] | 235 | return ERR_PTR(-ENOSPC); |
Thomas Petazzoni | a759512 | 2014-05-16 16:14:04 +0200 | [diff] [blame] | 236 | } |
| 237 | phy_addr = phy_fixed_addr++; |
| 238 | spin_unlock(&phy_fixed_addr_lock); |
| 239 | |
Sergei Shtylyov | bd1a05e | 2015-09-03 23:22:16 +0300 | [diff] [blame] | 240 | ret = fixed_phy_add(irq, phy_addr, status, link_gpio); |
Thomas Petazzoni | a759512 | 2014-05-16 16:14:04 +0200 | [diff] [blame] | 241 | if (ret < 0) |
Petri Gynther | fd2ef0b | 2014-10-06 11:38:30 -0700 | [diff] [blame] | 242 | return ERR_PTR(ret); |
Thomas Petazzoni | a759512 | 2014-05-16 16:14:04 +0200 | [diff] [blame] | 243 | |
| 244 | phy = get_phy_device(fmb->mii_bus, phy_addr, false); |
Sergei Shtylyov | 4914a58 | 2016-04-24 20:29:23 +0300 | [diff] [blame] | 245 | if (IS_ERR(phy)) { |
Thomas Petazzoni | a759512 | 2014-05-16 16:14:04 +0200 | [diff] [blame] | 246 | fixed_phy_del(phy_addr); |
Petri Gynther | fd2ef0b | 2014-10-06 11:38:30 -0700 | [diff] [blame] | 247 | return ERR_PTR(-EINVAL); |
Thomas Petazzoni | a759512 | 2014-05-16 16:14:04 +0200 | [diff] [blame] | 248 | } |
| 249 | |
Madalin Bucur | 4b19536 | 2015-08-26 17:58:47 +0300 | [diff] [blame] | 250 | /* propagate the fixed link values to struct phy_device */ |
| 251 | phy->link = status->link; |
| 252 | if (status->link) { |
| 253 | phy->speed = status->speed; |
| 254 | phy->duplex = status->duplex; |
| 255 | phy->pause = status->pause; |
| 256 | phy->asym_pause = status->asym_pause; |
| 257 | } |
| 258 | |
Thomas Petazzoni | a759512 | 2014-05-16 16:14:04 +0200 | [diff] [blame] | 259 | of_node_get(np); |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 260 | phy->mdio.dev.of_node = np; |
Florian Fainelli | 5a11dd7 | 2015-08-31 15:56:46 +0200 | [diff] [blame] | 261 | phy->is_pseudo_fixed_link = true; |
Thomas Petazzoni | a759512 | 2014-05-16 16:14:04 +0200 | [diff] [blame] | 262 | |
Andrew Lunn | 34b31da4 | 2015-08-31 15:56:48 +0200 | [diff] [blame] | 263 | switch (status->speed) { |
| 264 | case SPEED_1000: |
| 265 | phy->supported = PHY_1000BT_FEATURES; |
| 266 | break; |
| 267 | case SPEED_100: |
| 268 | phy->supported = PHY_100BT_FEATURES; |
| 269 | break; |
| 270 | case SPEED_10: |
| 271 | default: |
| 272 | phy->supported = PHY_10BT_FEATURES; |
| 273 | } |
| 274 | |
Thomas Petazzoni | a759512 | 2014-05-16 16:14:04 +0200 | [diff] [blame] | 275 | ret = phy_device_register(phy); |
| 276 | if (ret) { |
| 277 | phy_device_free(phy); |
| 278 | of_node_put(np); |
| 279 | fixed_phy_del(phy_addr); |
Petri Gynther | fd2ef0b | 2014-10-06 11:38:30 -0700 | [diff] [blame] | 280 | return ERR_PTR(ret); |
Thomas Petazzoni | a759512 | 2014-05-16 16:14:04 +0200 | [diff] [blame] | 281 | } |
| 282 | |
Petri Gynther | fd2ef0b | 2014-10-06 11:38:30 -0700 | [diff] [blame] | 283 | return phy; |
Thomas Petazzoni | a759512 | 2014-05-16 16:14:04 +0200 | [diff] [blame] | 284 | } |
Mark Salter | 37e9a69 | 2014-12-11 23:03:26 -0500 | [diff] [blame] | 285 | EXPORT_SYMBOL_GPL(fixed_phy_register); |
Thomas Petazzoni | a759512 | 2014-05-16 16:14:04 +0200 | [diff] [blame] | 286 | |
Andrew Lunn | 5bcbe0f | 2016-03-12 00:01:40 +0100 | [diff] [blame] | 287 | void fixed_phy_unregister(struct phy_device *phy) |
| 288 | { |
| 289 | phy_device_remove(phy); |
| 290 | |
| 291 | fixed_phy_del(phy->mdio.addr); |
| 292 | } |
| 293 | EXPORT_SYMBOL_GPL(fixed_phy_unregister); |
| 294 | |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 295 | static int __init fixed_mdio_bus_init(void) |
| 296 | { |
| 297 | struct fixed_mdio_bus *fmb = &platform_fmb; |
| 298 | int ret; |
| 299 | |
| 300 | pdev = platform_device_register_simple("Fixed MDIO bus", 0, NULL, 0); |
Dan Carpenter | 57401d5 | 2009-04-11 01:52:29 -0700 | [diff] [blame] | 301 | if (IS_ERR(pdev)) { |
| 302 | ret = PTR_ERR(pdev); |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 303 | goto err_pdev; |
| 304 | } |
| 305 | |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 306 | fmb->mii_bus = mdiobus_alloc(); |
| 307 | if (fmb->mii_bus == NULL) { |
| 308 | ret = -ENOMEM; |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 309 | goto err_mdiobus_reg; |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Florian Fainelli | 9e6c643 | 2012-01-09 23:59:25 +0000 | [diff] [blame] | 312 | snprintf(fmb->mii_bus->id, MII_BUS_ID_SIZE, "fixed-0"); |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 313 | fmb->mii_bus->name = "Fixed MDIO Bus"; |
Lennert Buytenhek | ec2a565 | 2008-10-09 09:45:04 -0700 | [diff] [blame] | 314 | fmb->mii_bus->priv = fmb; |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 315 | fmb->mii_bus->parent = &pdev->dev; |
| 316 | fmb->mii_bus->read = &fixed_mdio_read; |
| 317 | fmb->mii_bus->write = &fixed_mdio_write; |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 318 | |
| 319 | ret = mdiobus_register(fmb->mii_bus); |
| 320 | if (ret) |
| 321 | goto err_mdiobus_alloc; |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 322 | |
| 323 | return 0; |
| 324 | |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 325 | err_mdiobus_alloc: |
| 326 | mdiobus_free(fmb->mii_bus); |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 327 | err_mdiobus_reg: |
| 328 | platform_device_unregister(pdev); |
| 329 | err_pdev: |
| 330 | return ret; |
| 331 | } |
| 332 | module_init(fixed_mdio_bus_init); |
| 333 | |
| 334 | static void __exit fixed_mdio_bus_exit(void) |
| 335 | { |
| 336 | struct fixed_mdio_bus *fmb = &platform_fmb; |
Adrian Bunk | 651be3a | 2008-02-02 23:15:02 +0200 | [diff] [blame] | 337 | struct fixed_phy *fp, *tmp; |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 338 | |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 339 | mdiobus_unregister(fmb->mii_bus); |
| 340 | mdiobus_free(fmb->mii_bus); |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 341 | platform_device_unregister(pdev); |
| 342 | |
Adrian Bunk | 651be3a | 2008-02-02 23:15:02 +0200 | [diff] [blame] | 343 | list_for_each_entry_safe(fp, tmp, &fmb->phys, node) { |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 344 | list_del(&fp->node); |
| 345 | kfree(fp); |
| 346 | } |
| 347 | } |
| 348 | module_exit(fixed_mdio_bus_exit); |
| 349 | |
| 350 | MODULE_DESCRIPTION("Fixed MDIO bus (MDIO bus emulation with fixed PHYs)"); |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 351 | MODULE_AUTHOR("Vitaly Bordug"); |
| 352 | MODULE_LICENSE("GPL"); |