2 * Fixed MDIO bus (MDIO bus emulation with fixed PHYs)
4 * Author: Vitaly Bordug <vbordug@ru.mvista.com>
5 * Anton Vorontsov <avorontsov@ru.mvista.com>
7 * Copyright (c) 2006-2007 MontaVista Software, Inc.
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.
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/platform_device.h>
18 #include <linux/list.h>
19 #include <linux/mii.h>
20 #include <linux/phy.h>
21 #include <linux/phy_fixed.h>
22 #include <linux/err.h>
23 #include <linux/slab.h>
25 #include <linux/gpio.h>
27 #define MII_REGS_NUM 29
29 struct fixed_mdio_bus
{
30 int irqs
[PHY_MAX_ADDR
];
31 struct mii_bus
*mii_bus
;
32 struct list_head phys
;
37 u16 regs
[MII_REGS_NUM
];
38 struct phy_device
*phydev
;
39 struct fixed_phy_status status
;
40 int (*link_update
)(struct net_device
*, struct fixed_phy_status
*);
41 struct list_head node
;
45 static struct platform_device
*pdev
;
46 static struct fixed_mdio_bus platform_fmb
= {
47 .phys
= LIST_HEAD_INIT(platform_fmb
.phys
),
50 static int fixed_phy_update_regs(struct fixed_phy
*fp
)
52 u16 bmsr
= BMSR_ANEGCAPABLE
;
57 if (gpio_is_valid(fp
->link_gpio
))
58 fp
->status
.link
= !!gpio_get_value_cansleep(fp
->link_gpio
);
60 if (fp
->status
.duplex
) {
61 switch (fp
->status
.speed
) {
75 switch (fp
->status
.speed
) {
90 if (fp
->status
.link
) {
91 bmsr
|= BMSR_LSTATUS
| BMSR_ANEGCOMPLETE
;
93 if (fp
->status
.duplex
) {
94 bmcr
|= BMCR_FULLDPLX
;
96 switch (fp
->status
.speed
) {
98 bmcr
|= BMCR_SPEED1000
;
99 lpagb
|= LPA_1000FULL
;
102 bmcr
|= BMCR_SPEED100
;
109 pr_warn("fixed phy: unknown speed\n");
113 switch (fp
->status
.speed
) {
115 bmcr
|= BMCR_SPEED1000
;
116 lpagb
|= LPA_1000HALF
;
119 bmcr
|= BMCR_SPEED100
;
126 pr_warn("fixed phy: unknown speed\n");
131 if (fp
->status
.pause
)
132 lpa
|= LPA_PAUSE_CAP
;
134 if (fp
->status
.asym_pause
)
135 lpa
|= LPA_PAUSE_ASYM
;
138 fp
->regs
[MII_PHYSID1
] = 0;
139 fp
->regs
[MII_PHYSID2
] = 0;
141 fp
->regs
[MII_BMSR
] = bmsr
;
142 fp
->regs
[MII_BMCR
] = bmcr
;
143 fp
->regs
[MII_LPA
] = lpa
;
144 fp
->regs
[MII_STAT1000
] = lpagb
;
149 static int fixed_mdio_read(struct mii_bus
*bus
, int phy_addr
, int reg_num
)
151 struct fixed_mdio_bus
*fmb
= bus
->priv
;
152 struct fixed_phy
*fp
;
154 if (reg_num
>= MII_REGS_NUM
)
157 /* We do not support emulating Clause 45 over Clause 22 register reads
158 * return an error instead of bogus data.
168 list_for_each_entry(fp
, &fmb
->phys
, node
) {
169 if (fp
->addr
== phy_addr
) {
170 /* Issue callback if user registered it. */
171 if (fp
->link_update
) {
172 fp
->link_update(fp
->phydev
->attached_dev
,
174 fixed_phy_update_regs(fp
);
176 return fp
->regs
[reg_num
];
183 static int fixed_mdio_write(struct mii_bus
*bus
, int phy_addr
, int reg_num
,
190 * If something weird is required to be done with link/speed,
191 * network driver is able to assign a function to implement this.
192 * May be useful for PHY's that need to be software-driven.
194 int fixed_phy_set_link_update(struct phy_device
*phydev
,
195 int (*link_update
)(struct net_device
*,
196 struct fixed_phy_status
*))
198 struct fixed_mdio_bus
*fmb
= &platform_fmb
;
199 struct fixed_phy
*fp
;
201 if (!phydev
|| !phydev
->bus
)
204 list_for_each_entry(fp
, &fmb
->phys
, node
) {
205 if (fp
->addr
== phydev
->addr
) {
206 fp
->link_update
= link_update
;
214 EXPORT_SYMBOL_GPL(fixed_phy_set_link_update
);
216 int fixed_phy_update_state(struct phy_device
*phydev
,
217 const struct fixed_phy_status
*status
,
218 const struct fixed_phy_status
*changed
)
220 struct fixed_mdio_bus
*fmb
= &platform_fmb
;
221 struct fixed_phy
*fp
;
223 if (!phydev
|| phydev
->bus
!= fmb
->mii_bus
)
226 list_for_each_entry(fp
, &fmb
->phys
, node
) {
227 if (fp
->addr
== phydev
->addr
) {
228 #define _UPD(x) if (changed->x) \
229 fp->status.x = status->x
236 fixed_phy_update_regs(fp
);
243 EXPORT_SYMBOL(fixed_phy_update_state
);
245 int fixed_phy_add(unsigned int irq
, int phy_addr
,
246 struct fixed_phy_status
*status
,
250 struct fixed_mdio_bus
*fmb
= &platform_fmb
;
251 struct fixed_phy
*fp
;
253 fp
= kzalloc(sizeof(*fp
), GFP_KERNEL
);
257 memset(fp
->regs
, 0xFF, sizeof(fp
->regs
[0]) * MII_REGS_NUM
);
259 fmb
->irqs
[phy_addr
] = irq
;
262 fp
->status
= *status
;
263 fp
->link_gpio
= link_gpio
;
265 if (gpio_is_valid(fp
->link_gpio
)) {
266 ret
= gpio_request_one(fp
->link_gpio
, GPIOF_DIR_IN
,
267 "fixed-link-gpio-link");
272 ret
= fixed_phy_update_regs(fp
);
276 list_add_tail(&fp
->node
, &fmb
->phys
);
281 if (gpio_is_valid(fp
->link_gpio
))
282 gpio_free(fp
->link_gpio
);
287 EXPORT_SYMBOL_GPL(fixed_phy_add
);
289 void fixed_phy_del(int phy_addr
)
291 struct fixed_mdio_bus
*fmb
= &platform_fmb
;
292 struct fixed_phy
*fp
, *tmp
;
294 list_for_each_entry_safe(fp
, tmp
, &fmb
->phys
, node
) {
295 if (fp
->addr
== phy_addr
) {
297 if (gpio_is_valid(fp
->link_gpio
))
298 gpio_free(fp
->link_gpio
);
304 EXPORT_SYMBOL_GPL(fixed_phy_del
);
306 static int phy_fixed_addr
;
307 static DEFINE_SPINLOCK(phy_fixed_addr_lock
);
309 struct phy_device
*fixed_phy_register(unsigned int irq
,
310 struct fixed_phy_status
*status
,
312 struct device_node
*np
)
314 struct fixed_mdio_bus
*fmb
= &platform_fmb
;
315 struct phy_device
*phy
;
319 /* Get the next available PHY address, up to PHY_MAX_ADDR */
320 spin_lock(&phy_fixed_addr_lock
);
321 if (phy_fixed_addr
== PHY_MAX_ADDR
) {
322 spin_unlock(&phy_fixed_addr_lock
);
323 return ERR_PTR(-ENOSPC
);
325 phy_addr
= phy_fixed_addr
++;
326 spin_unlock(&phy_fixed_addr_lock
);
328 ret
= fixed_phy_add(irq
, phy_addr
, status
, link_gpio
);
332 phy
= get_phy_device(fmb
->mii_bus
, phy_addr
, false);
333 if (!phy
|| IS_ERR(phy
)) {
334 fixed_phy_del(phy_addr
);
335 return ERR_PTR(-EINVAL
);
338 /* propagate the fixed link values to struct phy_device */
339 phy
->link
= status
->link
;
341 phy
->speed
= status
->speed
;
342 phy
->duplex
= status
->duplex
;
343 phy
->pause
= status
->pause
;
344 phy
->asym_pause
= status
->asym_pause
;
348 phy
->dev
.of_node
= np
;
349 phy
->is_pseudo_fixed_link
= true;
351 switch (status
->speed
) {
353 phy
->supported
= PHY_1000BT_FEATURES
;
356 phy
->supported
= PHY_100BT_FEATURES
;
360 phy
->supported
= PHY_10BT_FEATURES
;
363 ret
= phy_device_register(phy
);
365 phy_device_free(phy
);
367 fixed_phy_del(phy_addr
);
373 EXPORT_SYMBOL_GPL(fixed_phy_register
);
375 static int __init
fixed_mdio_bus_init(void)
377 struct fixed_mdio_bus
*fmb
= &platform_fmb
;
380 pdev
= platform_device_register_simple("Fixed MDIO bus", 0, NULL
, 0);
386 fmb
->mii_bus
= mdiobus_alloc();
387 if (fmb
->mii_bus
== NULL
) {
389 goto err_mdiobus_reg
;
392 snprintf(fmb
->mii_bus
->id
, MII_BUS_ID_SIZE
, "fixed-0");
393 fmb
->mii_bus
->name
= "Fixed MDIO Bus";
394 fmb
->mii_bus
->priv
= fmb
;
395 fmb
->mii_bus
->parent
= &pdev
->dev
;
396 fmb
->mii_bus
->read
= &fixed_mdio_read
;
397 fmb
->mii_bus
->write
= &fixed_mdio_write
;
398 fmb
->mii_bus
->irq
= fmb
->irqs
;
400 ret
= mdiobus_register(fmb
->mii_bus
);
402 goto err_mdiobus_alloc
;
407 mdiobus_free(fmb
->mii_bus
);
409 platform_device_unregister(pdev
);
413 module_init(fixed_mdio_bus_init
);
415 static void __exit
fixed_mdio_bus_exit(void)
417 struct fixed_mdio_bus
*fmb
= &platform_fmb
;
418 struct fixed_phy
*fp
, *tmp
;
420 mdiobus_unregister(fmb
->mii_bus
);
421 mdiobus_free(fmb
->mii_bus
);
422 platform_device_unregister(pdev
);
424 list_for_each_entry_safe(fp
, tmp
, &fmb
->phys
, node
) {
429 module_exit(fixed_mdio_bus_exit
);
431 MODULE_DESCRIPTION("Fixed MDIO bus (MDIO bus emulation with fixed PHYs)");
432 MODULE_AUTHOR("Vitaly Bordug");
433 MODULE_LICENSE("GPL");