ar71xx: ag71xx: move link update function
[openwrt/svn-archive/archive.git] / target / linux / ar71xx / files / drivers / net / ag71xx / ag71xx_phy.c
1 /*
2 * Atheros AR71xx built-in ethernet mac driver
3 *
4 * Copyright (C) 2008-2010 Gabor Juhos <juhosg@openwrt.org>
5 * Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
6 *
7 * Based on Atheros' AG7100 driver
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 version 2 as published
11 * by the Free Software Foundation.
12 */
13
14 #include "ag71xx.h"
15
16 static void ag71xx_phy_link_adjust(struct net_device *dev)
17 {
18 struct ag71xx *ag = netdev_priv(dev);
19 struct phy_device *phydev = ag->phy_dev;
20 unsigned long flags;
21 int status_change = 0;
22
23 spin_lock_irqsave(&ag->lock, flags);
24
25 if (phydev->link) {
26 if (ag->duplex != phydev->duplex
27 || ag->speed != phydev->speed) {
28 status_change = 1;
29 }
30 }
31
32 if (phydev->link != ag->link)
33 status_change = 1;
34
35 ag->link = phydev->link;
36 ag->duplex = phydev->duplex;
37 ag->speed = phydev->speed;
38
39 if (status_change)
40 ag71xx_link_adjust(ag);
41
42 spin_unlock_irqrestore(&ag->lock, flags);
43 }
44
45 void ag71xx_phy_start(struct ag71xx *ag)
46 {
47 if (ag->phy_dev) {
48 phy_start(ag->phy_dev);
49 } else {
50 struct ag71xx_platform_data *pdata = ag71xx_get_pdata(ag);
51
52 ag->duplex = pdata->duplex;
53 ag->speed = pdata->speed;
54 ag->link = 1;
55 ag71xx_link_adjust(ag);
56 }
57 }
58
59 void ag71xx_phy_stop(struct ag71xx *ag)
60 {
61 if (ag->phy_dev) {
62 phy_stop(ag->phy_dev);
63 } else {
64 ag->duplex = -1;
65 ag->link = 0;
66 ag->speed = 0;
67 ag71xx_link_adjust(ag);
68 }
69 }
70
71 static int ag71xx_phy_connect_fixed(struct ag71xx *ag)
72 {
73 struct net_device *dev = ag->dev;
74 struct ag71xx_platform_data *pdata = ag71xx_get_pdata(ag);
75 int ret = 0;
76
77 /* use fixed settings */
78 switch (pdata->speed) {
79 case SPEED_10:
80 case SPEED_100:
81 case SPEED_1000:
82 break;
83 default:
84 printk(KERN_ERR "%s: invalid speed specified\n",
85 dev->name);
86 ret = -EINVAL;
87 break;
88 }
89
90 return ret;
91 }
92
93 static int ag71xx_phy_connect_multi(struct ag71xx *ag)
94 {
95 struct net_device *dev = ag->dev;
96 struct ag71xx_platform_data *pdata = ag71xx_get_pdata(ag);
97 struct phy_device *phydev = NULL;
98 int phy_count = 0;
99 int phy_addr;
100 int ret = 0;
101
102 for (phy_addr = 0; phy_addr < PHY_MAX_ADDR; phy_addr++) {
103 if (!(pdata->phy_mask & (1 << phy_addr)))
104 continue;
105
106 if (ag->mii_bus->phy_map[phy_addr] == NULL)
107 continue;
108
109 DBG("%s: PHY found at %s, uid=%08x\n",
110 dev->name,
111 dev_name(&ag->mii_bus->phy_map[phy_addr]->dev),
112 ag->mii_bus->phy_map[phy_addr]->phy_id);
113
114 if (phydev == NULL)
115 phydev = ag->mii_bus->phy_map[phy_addr];
116
117 phy_count++;
118 }
119
120 switch (phy_count) {
121 case 0:
122 printk(KERN_ERR "%s: no PHY found with phy_mask=%08x\n",
123 dev->name, pdata->phy_mask);
124 ret = -ENODEV;
125 break;
126 case 1:
127 ag->phy_dev = phy_connect(dev, dev_name(&phydev->dev),
128 &ag71xx_phy_link_adjust, 0, pdata->phy_if_mode);
129
130 if (IS_ERR(ag->phy_dev)) {
131 printk(KERN_ERR "%s: could not connect to PHY at %s\n",
132 dev->name, dev_name(&phydev->dev));
133 return PTR_ERR(ag->phy_dev);
134 }
135
136 /* mask with MAC supported features */
137 if (pdata->has_gbit)
138 phydev->supported &= PHY_GBIT_FEATURES;
139 else
140 phydev->supported &= PHY_BASIC_FEATURES;
141
142 phydev->advertising = phydev->supported;
143
144 printk(KERN_DEBUG "%s: connected to PHY at %s "
145 "[uid=%08x, driver=%s]\n",
146 dev->name, dev_name(&phydev->dev),
147 phydev->phy_id, phydev->drv->name);
148
149 ag->link = 0;
150 ag->speed = 0;
151 ag->duplex = -1;
152 break;
153
154 default:
155 printk(KERN_DEBUG "%s: connected to %d PHYs\n",
156 dev->name, phy_count);
157 ret = ag71xx_phy_connect_fixed(ag);
158 break;
159 }
160
161 return ret;
162 }
163
164 static int dev_is_class(struct device *dev, void *class)
165 {
166 if (dev->class != NULL && !strcmp(dev->class->name, class))
167 return 1;
168
169 return 0;
170 }
171
172 static struct device *dev_find_class(struct device *parent, char *class)
173 {
174 if (dev_is_class(parent, class)) {
175 get_device(parent);
176 return parent;
177 }
178
179 return device_find_child(parent, class, dev_is_class);
180 }
181
182 static struct mii_bus *dev_to_mii_bus(struct device *dev)
183 {
184 struct device *d;
185
186 d = dev_find_class(dev, "mdio_bus");
187 if (d != NULL) {
188 struct mii_bus *bus;
189
190 bus = to_mii_bus(d);
191 put_device(d);
192
193 return bus;
194 }
195
196 return NULL;
197 }
198
199 int ag71xx_phy_connect(struct ag71xx *ag)
200 {
201 struct ag71xx_platform_data *pdata = ag71xx_get_pdata(ag);
202
203 ag->mii_bus = dev_to_mii_bus(pdata->mii_bus_dev);
204 if (ag->mii_bus == NULL) {
205 printk(KERN_ERR "%s: unable to find MII bus on device '%s'\n",
206 ag->dev->name, dev_name(pdata->mii_bus_dev));
207 return -ENODEV;
208 }
209
210 /* Reset the mdio bus explicitly */
211 if (ag->mii_bus->reset) {
212 mutex_lock(&ag->mii_bus->mdio_lock);
213 ag->mii_bus->reset(ag->mii_bus);
214 mutex_unlock(&ag->mii_bus->mdio_lock);
215 }
216
217 if (pdata->phy_mask)
218 return ag71xx_phy_connect_multi(ag);
219
220 return ag71xx_phy_connect_fixed(ag);
221 }
222
223 void ag71xx_phy_disconnect(struct ag71xx *ag)
224 {
225 if (ag->phy_dev)
226 phy_disconnect(ag->phy_dev);
227 }