blob: ba55adfc7aaef00b7cfee22fe0fd2ff8eb67ffe8 [file] [log] [blame]
Vitaly Bordug11b0bac2006-08-14 23:00:29 -07001/*
Vitaly Borduga79d8e92007-12-07 01:51:22 +03002 * Fixed MDIO bus (MDIO bus emulation with fixed PHYs)
Vitaly Bordug11b0bac2006-08-14 23:00:29 -07003 *
Vitaly Borduga79d8e92007-12-07 01:51:22 +03004 * Author: Vitaly Bordug <vbordug@ru.mvista.com>
5 * Anton Vorontsov <avorontsov@ru.mvista.com>
Vitaly Bordug11b0bac2006-08-14 23:00:29 -07006 *
Vitaly Borduga79d8e92007-12-07 01:51:22 +03007 * Copyright (c) 2006-2007 MontaVista Software, Inc.
Vitaly Bordug11b0bac2006-08-14 23:00:29 -07008 *
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 Bordug11b0bac2006-08-14 23:00:29 -070013 */
Vitaly Borduga79d8e92007-12-07 01:51:22 +030014
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070015#include <linux/kernel.h>
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070016#include <linux/module.h>
Vitaly Borduga79d8e92007-12-07 01:51:22 +030017#include <linux/platform_device.h>
18#include <linux/list.h>
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070019#include <linux/mii.h>
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070020#include <linux/phy.h>
Vitaly Bordug7c32f472007-08-10 14:05:16 -070021#include <linux/phy_fixed.h>
Dan Carpenter57401d52009-04-11 01:52:29 -070022#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070024
Vitaly Borduga79d8e92007-12-07 01:51:22 +030025#define MII_REGS_NUM 29
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070026
Vitaly Borduga79d8e92007-12-07 01:51:22 +030027struct fixed_mdio_bus {
28 int irqs[PHY_MAX_ADDR];
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -070029 struct mii_bus *mii_bus;
Vitaly Borduga79d8e92007-12-07 01:51:22 +030030 struct list_head phys;
31};
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070032
Vitaly Borduga79d8e92007-12-07 01:51:22 +030033struct 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};
41
42static struct platform_device *pdev;
43static struct fixed_mdio_bus platform_fmb = {
44 .phys = LIST_HEAD_INIT(platform_fmb.phys),
45};
46
47static int fixed_phy_update_regs(struct fixed_phy *fp)
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070048{
Vitaly Borduga79d8e92007-12-07 01:51:22 +030049 u16 bmsr = BMSR_ANEGCAPABLE;
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070050 u16 bmcr = 0;
Vitaly Borduga79d8e92007-12-07 01:51:22 +030051 u16 lpagb = 0;
52 u16 lpa = 0;
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070053
Vitaly Borduga79d8e92007-12-07 01:51:22 +030054 if (fp->status.duplex) {
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070055 bmcr |= BMCR_FULLDPLX;
56
Vitaly Borduga79d8e92007-12-07 01:51:22 +030057 switch (fp->status.speed) {
58 case 1000:
59 bmsr |= BMSR_ESTATEN;
60 bmcr |= BMCR_SPEED1000;
61 lpagb |= LPA_1000FULL;
62 break;
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070063 case 100:
64 bmsr |= BMSR_100FULL;
65 bmcr |= BMCR_SPEED100;
Vitaly Borduga79d8e92007-12-07 01:51:22 +030066 lpa |= LPA_100FULL;
Vitaly Bordug7c32f472007-08-10 14:05:16 -070067 break;
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070068 case 10:
69 bmsr |= BMSR_10FULL;
Vitaly Borduga79d8e92007-12-07 01:51:22 +030070 lpa |= LPA_10FULL;
Vitaly Bordug7c32f472007-08-10 14:05:16 -070071 break;
Vitaly Borduga79d8e92007-12-07 01:51:22 +030072 default:
Joe Perches8d242482012-06-09 07:49:07 +000073 pr_warn("fixed phy: unknown speed\n");
Vitaly Borduga79d8e92007-12-07 01:51:22 +030074 return -EINVAL;
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070075 }
76 } else {
Vitaly Borduga79d8e92007-12-07 01:51:22 +030077 switch (fp->status.speed) {
78 case 1000:
79 bmsr |= BMSR_ESTATEN;
80 bmcr |= BMCR_SPEED1000;
81 lpagb |= LPA_1000HALF;
82 break;
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070083 case 100:
84 bmsr |= BMSR_100HALF;
85 bmcr |= BMCR_SPEED100;
Vitaly Borduga79d8e92007-12-07 01:51:22 +030086 lpa |= LPA_100HALF;
Vitaly Bordug7c32f472007-08-10 14:05:16 -070087 break;
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070088 case 10:
Vitaly Borduga79d8e92007-12-07 01:51:22 +030089 bmsr |= BMSR_10HALF;
90 lpa |= LPA_10HALF;
Vitaly Bordug7c32f472007-08-10 14:05:16 -070091 break;
Vitaly Borduga79d8e92007-12-07 01:51:22 +030092 default:
Joe Perches8d242482012-06-09 07:49:07 +000093 pr_warn("fixed phy: unknown speed\n");
Vitaly Borduga79d8e92007-12-07 01:51:22 +030094 return -EINVAL;
Vitaly Bordug11b0bac2006-08-14 23:00:29 -070095 }
96 }
97
Vitaly Borduga79d8e92007-12-07 01:51:22 +030098 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;
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700114
115 return 0;
116}
117
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300118static int fixed_mdio_read(struct mii_bus *bus, int phy_id, int reg_num)
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700119{
Lennert Buytenhekec2a5652008-10-09 09:45:04 -0700120 struct fixed_mdio_bus *fmb = bus->priv;
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300121 struct fixed_phy *fp;
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700122
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300123 if (reg_num >= MII_REGS_NUM)
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700124 return -1;
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700125
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300126 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);
133 }
134 return fp->regs[reg_num];
135 }
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700136 }
137
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300138 return 0xFFFF;
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700139}
140
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300141static int fixed_mdio_write(struct mii_bus *bus, int phy_id, int reg_num,
142 u16 val)
143{
144 return 0;
145}
146
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 */
152int fixed_phy_set_link_update(struct phy_device *phydev,
153 int (*link_update)(struct net_device *,
154 struct fixed_phy_status *))
155{
156 struct fixed_mdio_bus *fmb = &platform_fmb;
157 struct fixed_phy *fp;
158
159 if (!link_update || !phydev || !phydev->bus)
160 return -EINVAL;
161
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 }
169
170 return -ENOENT;
171}
172EXPORT_SYMBOL_GPL(fixed_phy_set_link_update);
173
174int fixed_phy_add(unsigned int irq, int phy_id,
175 struct fixed_phy_status *status)
176{
177 int ret;
178 struct fixed_mdio_bus *fmb = &platform_fmb;
179 struct fixed_phy *fp;
180
181 fp = kzalloc(sizeof(*fp), GFP_KERNEL);
182 if (!fp)
183 return -ENOMEM;
184
185 memset(fp->regs, 0xFF, sizeof(fp->regs[0]) * MII_REGS_NUM);
186
187 fmb->irqs[phy_id] = irq;
188
189 fp->id = phy_id;
190 fp->status = *status;
191
192 ret = fixed_phy_update_regs(fp);
193 if (ret)
194 goto err_regs;
195
196 list_add_tail(&fp->node, &fmb->phys);
197
198 return 0;
199
200err_regs:
201 kfree(fp);
202 return ret;
203}
204EXPORT_SYMBOL_GPL(fixed_phy_add);
205
206static int __init fixed_mdio_bus_init(void)
207{
208 struct fixed_mdio_bus *fmb = &platform_fmb;
209 int ret;
210
211 pdev = platform_device_register_simple("Fixed MDIO bus", 0, NULL, 0);
Dan Carpenter57401d52009-04-11 01:52:29 -0700212 if (IS_ERR(pdev)) {
213 ret = PTR_ERR(pdev);
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300214 goto err_pdev;
215 }
216
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700217 fmb->mii_bus = mdiobus_alloc();
218 if (fmb->mii_bus == NULL) {
219 ret = -ENOMEM;
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300220 goto err_mdiobus_reg;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700221 }
222
Florian Fainelli9e6c6432012-01-09 23:59:25 +0000223 snprintf(fmb->mii_bus->id, MII_BUS_ID_SIZE, "fixed-0");
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700224 fmb->mii_bus->name = "Fixed MDIO Bus";
Lennert Buytenhekec2a5652008-10-09 09:45:04 -0700225 fmb->mii_bus->priv = fmb;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700226 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);
232 if (ret)
233 goto err_mdiobus_alloc;
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300234
235 return 0;
236
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700237err_mdiobus_alloc:
238 mdiobus_free(fmb->mii_bus);
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300239err_mdiobus_reg:
240 platform_device_unregister(pdev);
241err_pdev:
242 return ret;
243}
244module_init(fixed_mdio_bus_init);
245
246static void __exit fixed_mdio_bus_exit(void)
247{
248 struct fixed_mdio_bus *fmb = &platform_fmb;
Adrian Bunk651be3a2008-02-02 23:15:02 +0200249 struct fixed_phy *fp, *tmp;
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300250
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700251 mdiobus_unregister(fmb->mii_bus);
252 mdiobus_free(fmb->mii_bus);
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300253 platform_device_unregister(pdev);
254
Adrian Bunk651be3a2008-02-02 23:15:02 +0200255 list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
Vitaly Borduga79d8e92007-12-07 01:51:22 +0300256 list_del(&fp->node);
257 kfree(fp);
258 }
259}
260module_exit(fixed_mdio_bus_exit);
261
262MODULE_DESCRIPTION("Fixed MDIO bus (MDIO bus emulation with fixed PHYs)");
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700263MODULE_AUTHOR("Vitaly Bordug");
264MODULE_LICENSE("GPL");