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