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