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> |
11b0bacd | 22 | |
a79d8e93 | 23 | #define MII_REGS_NUM 29 |
11b0bacd | 24 | |
a79d8e93 VB |
25 | struct fixed_mdio_bus { |
26 | int irqs[PHY_MAX_ADDR]; | |
27 | struct mii_bus mii_bus; | |
28 | struct list_head phys; | |
29 | }; | |
11b0bacd | 30 | |
a79d8e93 VB |
31 | struct fixed_phy { |
32 | int id; | |
33 | u16 regs[MII_REGS_NUM]; | |
34 | struct phy_device *phydev; | |
35 | struct fixed_phy_status status; | |
36 | int (*link_update)(struct net_device *, struct fixed_phy_status *); | |
37 | struct list_head node; | |
38 | }; | |
7c32f470 | 39 | |
a79d8e93 VB |
40 | static struct platform_device *pdev; |
41 | static struct fixed_mdio_bus platform_fmb = { | |
42 | .phys = LIST_HEAD_INIT(platform_fmb.phys), | |
43 | }; | |
7c32f470 | 44 | |
a79d8e93 | 45 | static int fixed_phy_update_regs(struct fixed_phy *fp) |
11b0bacd | 46 | { |
a79d8e93 | 47 | u16 bmsr = BMSR_ANEGCAPABLE; |
11b0bacd | 48 | u16 bmcr = 0; |
a79d8e93 VB |
49 | u16 lpagb = 0; |
50 | u16 lpa = 0; | |
11b0bacd | 51 | |
a79d8e93 | 52 | if (fp->status.duplex) { |
11b0bacd VB |
53 | bmcr |= BMCR_FULLDPLX; |
54 | ||
a79d8e93 VB |
55 | switch (fp->status.speed) { |
56 | case 1000: | |
57 | bmsr |= BMSR_ESTATEN; | |
58 | bmcr |= BMCR_SPEED1000; | |
59 | lpagb |= LPA_1000FULL; | |
60 | break; | |
11b0bacd VB |
61 | case 100: |
62 | bmsr |= BMSR_100FULL; | |
63 | bmcr |= BMCR_SPEED100; | |
a79d8e93 | 64 | lpa |= LPA_100FULL; |
7c32f470 | 65 | break; |
11b0bacd VB |
66 | case 10: |
67 | bmsr |= BMSR_10FULL; | |
a79d8e93 | 68 | lpa |= LPA_10FULL; |
7c32f470 | 69 | break; |
a79d8e93 VB |
70 | default: |
71 | printk(KERN_WARNING "fixed phy: unknown speed\n"); | |
72 | return -EINVAL; | |
11b0bacd VB |
73 | } |
74 | } else { | |
a79d8e93 VB |
75 | switch (fp->status.speed) { |
76 | case 1000: | |
77 | bmsr |= BMSR_ESTATEN; | |
78 | bmcr |= BMCR_SPEED1000; | |
79 | lpagb |= LPA_1000HALF; | |
80 | break; | |
11b0bacd VB |
81 | case 100: |
82 | bmsr |= BMSR_100HALF; | |
83 | bmcr |= BMCR_SPEED100; | |
a79d8e93 | 84 | lpa |= LPA_100HALF; |
7c32f470 | 85 | break; |
11b0bacd | 86 | case 10: |
a79d8e93 VB |
87 | bmsr |= BMSR_10HALF; |
88 | lpa |= LPA_10HALF; | |
7c32f470 | 89 | break; |
a79d8e93 VB |
90 | default: |
91 | printk(KERN_WARNING "fixed phy: unknown speed\n"); | |
92 | return -EINVAL; | |
11b0bacd VB |
93 | } |
94 | } | |
95 | ||
a79d8e93 VB |
96 | if (fp->status.link) |
97 | bmsr |= BMSR_LSTATUS | BMSR_ANEGCOMPLETE; | |
98 | ||
99 | if (fp->status.pause) | |
100 | lpa |= LPA_PAUSE_CAP; | |
101 | ||
102 | if (fp->status.asym_pause) | |
103 | lpa |= LPA_PAUSE_ASYM; | |
104 | ||
105 | fp->regs[MII_PHYSID1] = fp->id >> 16; | |
106 | fp->regs[MII_PHYSID2] = fp->id; | |
107 | ||
108 | fp->regs[MII_BMSR] = bmsr; | |
109 | fp->regs[MII_BMCR] = bmcr; | |
110 | fp->regs[MII_LPA] = lpa; | |
111 | fp->regs[MII_STAT1000] = lpagb; | |
11b0bacd VB |
112 | |
113 | return 0; | |
114 | } | |
115 | ||
a79d8e93 | 116 | static int fixed_mdio_read(struct mii_bus *bus, int phy_id, int reg_num) |
11b0bacd | 117 | { |
a79d8e93 VB |
118 | struct fixed_mdio_bus *fmb = container_of(bus, struct fixed_mdio_bus, |
119 | mii_bus); | |
120 | struct fixed_phy *fp; | |
121 | ||
122 | if (reg_num >= MII_REGS_NUM) | |
123 | return -1; | |
124 | ||
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); | |
11b0bacd | 132 | } |
a79d8e93 | 133 | return fp->regs[reg_num]; |
7c32f470 | 134 | } |
a79d8e93 | 135 | } |
11b0bacd | 136 | |
a79d8e93 | 137 | return 0xFFFF; |
11b0bacd VB |
138 | } |
139 | ||
a79d8e93 VB |
140 | static int fixed_mdio_write(struct mii_bus *bus, int phy_id, int reg_num, |
141 | u16 val) | |
11b0bacd | 142 | { |
11b0bacd VB |
143 | return 0; |
144 | } | |
145 | ||
a79d8e93 VB |
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 *)) | |
11b0bacd | 154 | { |
a79d8e93 VB |
155 | struct fixed_mdio_bus *fmb = &platform_fmb; |
156 | struct fixed_phy *fp; | |
11b0bacd | 157 | |
a79d8e93 VB |
158 | if (!link_update || !phydev || !phydev->bus) |
159 | return -EINVAL; | |
11b0bacd | 160 | |
a79d8e93 VB |
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 | } | |
11b0bacd | 168 | |
a79d8e93 | 169 | return -ENOENT; |
7c32f470 | 170 | } |
a79d8e93 | 171 | EXPORT_SYMBOL_GPL(fixed_phy_set_link_update); |
7c32f470 | 172 | |
a79d8e93 VB |
173 | int fixed_phy_add(unsigned int irq, int phy_id, |
174 | struct fixed_phy_status *status) | |
11b0bacd | 175 | { |
a79d8e93 VB |
176 | int ret; |
177 | struct fixed_mdio_bus *fmb = &platform_fmb; | |
178 | struct fixed_phy *fp; | |
11b0bacd | 179 | |
a79d8e93 VB |
180 | fp = kzalloc(sizeof(*fp), GFP_KERNEL); |
181 | if (!fp) | |
182 | return -ENOMEM; | |
11b0bacd | 183 | |
a79d8e93 | 184 | memset(fp->regs, 0xFF, sizeof(fp->regs[0]) * MII_REGS_NUM); |
11b0bacd | 185 | |
a79d8e93 | 186 | fmb->irqs[phy_id] = irq; |
11b0bacd | 187 | |
a79d8e93 VB |
188 | fp->id = phy_id; |
189 | fp->status = *status; | |
7c32f470 | 190 | |
a79d8e93 VB |
191 | ret = fixed_phy_update_regs(fp); |
192 | if (ret) | |
193 | goto err_regs; | |
11b0bacd | 194 | |
a79d8e93 | 195 | list_add_tail(&fp->node, &fmb->phys); |
7c32f470 | 196 | |
a79d8e93 | 197 | return 0; |
11b0bacd | 198 | |
a79d8e93 VB |
199 | err_regs: |
200 | kfree(fp); | |
201 | return ret; | |
202 | } | |
203 | EXPORT_SYMBOL_GPL(fixed_phy_add); | |
11b0bacd | 204 | |
a79d8e93 VB |
205 | static int __init fixed_mdio_bus_init(void) |
206 | { | |
207 | struct fixed_mdio_bus *fmb = &platform_fmb; | |
208 | int ret; | |
11b0bacd | 209 | |
a79d8e93 VB |
210 | pdev = platform_device_register_simple("Fixed MDIO bus", 0, NULL, 0); |
211 | if (!pdev) { | |
212 | ret = -ENOMEM; | |
213 | goto err_pdev; | |
214 | } | |
11b0bacd | 215 | |
a79d8e93 VB |
216 | fmb->mii_bus.id = 0; |
217 | fmb->mii_bus.name = "Fixed MDIO Bus"; | |
218 | fmb->mii_bus.dev = &pdev->dev; | |
219 | fmb->mii_bus.read = &fixed_mdio_read; | |
220 | fmb->mii_bus.write = &fixed_mdio_write; | |
221 | fmb->mii_bus.irq = fmb->irqs; | |
11b0bacd | 222 | |
a79d8e93 VB |
223 | ret = mdiobus_register(&fmb->mii_bus); |
224 | if (ret) | |
225 | goto err_mdiobus_reg; | |
11b0bacd | 226 | |
a79d8e93 | 227 | return 0; |
11b0bacd | 228 | |
a79d8e93 VB |
229 | err_mdiobus_reg: |
230 | platform_device_unregister(pdev); | |
231 | err_pdev: | |
232 | return ret; | |
233 | } | |
234 | module_init(fixed_mdio_bus_init); | |
11b0bacd | 235 | |
a79d8e93 VB |
236 | static void __exit fixed_mdio_bus_exit(void) |
237 | { | |
238 | struct fixed_mdio_bus *fmb = &platform_fmb; | |
239 | struct fixed_phy *fp; | |
11b0bacd | 240 | |
a79d8e93 VB |
241 | mdiobus_unregister(&fmb->mii_bus); |
242 | platform_device_unregister(pdev); | |
11b0bacd | 243 | |
a79d8e93 VB |
244 | list_for_each_entry(fp, &fmb->phys, node) { |
245 | list_del(&fp->node); | |
246 | kfree(fp); | |
7c32f470 | 247 | } |
11b0bacd | 248 | } |
a79d8e93 | 249 | module_exit(fixed_mdio_bus_exit); |
11b0bacd | 250 | |
a79d8e93 | 251 | MODULE_DESCRIPTION("Fixed MDIO bus (MDIO bus emulation with fixed PHYs)"); |
11b0bacd VB |
252 | MODULE_AUTHOR("Vitaly Bordug"); |
253 | MODULE_LICENSE("GPL"); |