iwlwifi: don't include iwl-dev.h from iwl-devtrace.h
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / net / phy / fixed.c
CommitLineData
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>
11b0bacd 23
a79d8e93 24#define MII_REGS_NUM 29
11b0bacd 25
a79d8e93
VB
26struct fixed_mdio_bus {
27 int irqs[PHY_MAX_ADDR];
298cf9be 28 struct mii_bus *mii_bus;
a79d8e93
VB
29 struct list_head phys;
30};
11b0bacd 31
a79d8e93
VB
32struct fixed_phy {
33 int id;
34 u16 regs[MII_REGS_NUM];
35 struct phy_device *phydev;
36 struct fixed_phy_status status;
37 int (*link_update)(struct net_device *, struct fixed_phy_status *);
38 struct list_head node;
39};
7c32f470 40
a79d8e93
VB
41static struct platform_device *pdev;
42static struct fixed_mdio_bus platform_fmb = {
43 .phys = LIST_HEAD_INIT(platform_fmb.phys),
44};
7c32f470 45
a79d8e93 46static int fixed_phy_update_regs(struct fixed_phy *fp)
11b0bacd 47{
a79d8e93 48 u16 bmsr = BMSR_ANEGCAPABLE;
11b0bacd 49 u16 bmcr = 0;
a79d8e93
VB
50 u16 lpagb = 0;
51 u16 lpa = 0;
11b0bacd 52
a79d8e93 53 if (fp->status.duplex) {
11b0bacd
VB
54 bmcr |= BMCR_FULLDPLX;
55
a79d8e93
VB
56 switch (fp->status.speed) {
57 case 1000:
58 bmsr |= BMSR_ESTATEN;
59 bmcr |= BMCR_SPEED1000;
60 lpagb |= LPA_1000FULL;
61 break;
11b0bacd
VB
62 case 100:
63 bmsr |= BMSR_100FULL;
64 bmcr |= BMCR_SPEED100;
a79d8e93 65 lpa |= LPA_100FULL;
7c32f470 66 break;
11b0bacd
VB
67 case 10:
68 bmsr |= BMSR_10FULL;
a79d8e93 69 lpa |= LPA_10FULL;
7c32f470 70 break;
a79d8e93
VB
71 default:
72 printk(KERN_WARNING "fixed phy: unknown speed\n");
73 return -EINVAL;
11b0bacd
VB
74 }
75 } else {
a79d8e93
VB
76 switch (fp->status.speed) {
77 case 1000:
78 bmsr |= BMSR_ESTATEN;
79 bmcr |= BMCR_SPEED1000;
80 lpagb |= LPA_1000HALF;
81 break;
11b0bacd
VB
82 case 100:
83 bmsr |= BMSR_100HALF;
84 bmcr |= BMCR_SPEED100;
a79d8e93 85 lpa |= LPA_100HALF;
7c32f470 86 break;
11b0bacd 87 case 10:
a79d8e93
VB
88 bmsr |= BMSR_10HALF;
89 lpa |= LPA_10HALF;
7c32f470 90 break;
a79d8e93
VB
91 default:
92 printk(KERN_WARNING "fixed phy: unknown speed\n");
93 return -EINVAL;
11b0bacd
VB
94 }
95 }
96
a79d8e93
VB
97 if (fp->status.link)
98 bmsr |= BMSR_LSTATUS | BMSR_ANEGCOMPLETE;
99
100 if (fp->status.pause)
101 lpa |= LPA_PAUSE_CAP;
102
103 if (fp->status.asym_pause)
104 lpa |= LPA_PAUSE_ASYM;
105
106 fp->regs[MII_PHYSID1] = fp->id >> 16;
107 fp->regs[MII_PHYSID2] = fp->id;
108
109 fp->regs[MII_BMSR] = bmsr;
110 fp->regs[MII_BMCR] = bmcr;
111 fp->regs[MII_LPA] = lpa;
112 fp->regs[MII_STAT1000] = lpagb;
11b0bacd
VB
113
114 return 0;
115}
116
a79d8e93 117static int fixed_mdio_read(struct mii_bus *bus, int phy_id, int reg_num)
11b0bacd 118{
ec2a5652 119 struct fixed_mdio_bus *fmb = bus->priv;
a79d8e93
VB
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
140static 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 */
151int 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 171EXPORT_SYMBOL_GPL(fixed_phy_set_link_update);
7c32f470 172
a79d8e93
VB
173int 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
199err_regs:
200 kfree(fp);
201 return ret;
202}
203EXPORT_SYMBOL_GPL(fixed_phy_add);
11b0bacd 204
a79d8e93
VB
205static int __init fixed_mdio_bus_init(void)
206{
207 struct fixed_mdio_bus *fmb = &platform_fmb;
208 int ret;
11b0bacd 209
a79d8e93 210 pdev = platform_device_register_simple("Fixed MDIO bus", 0, NULL, 0);
57401d5e
DC
211 if (IS_ERR(pdev)) {
212 ret = PTR_ERR(pdev);
a79d8e93
VB
213 goto err_pdev;
214 }
11b0bacd 215
298cf9be
LB
216 fmb->mii_bus = mdiobus_alloc();
217 if (fmb->mii_bus == NULL) {
218 ret = -ENOMEM;
219 goto err_mdiobus_reg;
220 }
11b0bacd 221
298cf9be
LB
222 snprintf(fmb->mii_bus->id, MII_BUS_ID_SIZE, "0");
223 fmb->mii_bus->name = "Fixed MDIO Bus";
ec2a5652 224 fmb->mii_bus->priv = fmb;
298cf9be
LB
225 fmb->mii_bus->parent = &pdev->dev;
226 fmb->mii_bus->read = &fixed_mdio_read;
227 fmb->mii_bus->write = &fixed_mdio_write;
228 fmb->mii_bus->irq = fmb->irqs;
229
230 ret = mdiobus_register(fmb->mii_bus);
a79d8e93 231 if (ret)
298cf9be 232 goto err_mdiobus_alloc;
11b0bacd 233
a79d8e93 234 return 0;
11b0bacd 235
298cf9be
LB
236err_mdiobus_alloc:
237 mdiobus_free(fmb->mii_bus);
a79d8e93
VB
238err_mdiobus_reg:
239 platform_device_unregister(pdev);
240err_pdev:
241 return ret;
242}
243module_init(fixed_mdio_bus_init);
11b0bacd 244
a79d8e93
VB
245static void __exit fixed_mdio_bus_exit(void)
246{
247 struct fixed_mdio_bus *fmb = &platform_fmb;
651be3a2 248 struct fixed_phy *fp, *tmp;
11b0bacd 249
298cf9be
LB
250 mdiobus_unregister(fmb->mii_bus);
251 mdiobus_free(fmb->mii_bus);
a79d8e93 252 platform_device_unregister(pdev);
11b0bacd 253
651be3a2 254 list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
a79d8e93
VB
255 list_del(&fp->node);
256 kfree(fp);
7c32f470 257 }
11b0bacd 258}
a79d8e93 259module_exit(fixed_mdio_bus_exit);
11b0bacd 260
a79d8e93 261MODULE_DESCRIPTION("Fixed MDIO bus (MDIO bus emulation with fixed PHYs)");
11b0bacd
VB
262MODULE_AUTHOR("Vitaly Bordug");
263MODULE_LICENSE("GPL");