ar71xx: ag71xx: Prevent kernel oops for board def
[openwrt/staging/mkresin.git] / target / linux / ar71xx / files / drivers / net / ethernet / atheros / 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 struct ag71xx_platform_data *pdata = ag71xx_get_pdata(ag);
48
49 if (ag->phy_dev) {
50 phy_start(ag->phy_dev);
51 } else if (pdata->mii_bus_dev && pdata->switch_data) {
52 ag71xx_ar7240_start(ag);
53 } else {
54 ag->link = 1;
55 ag71xx_link_adjust(ag);
56 }
57 }
58
59 void ag71xx_phy_stop(struct ag71xx *ag)
60 {
61 struct ag71xx_platform_data *pdata = ag71xx_get_pdata(ag);
62 unsigned long flags;
63
64 if (ag->phy_dev)
65 phy_stop(ag->phy_dev);
66 else if (pdata->mii_bus_dev && pdata->switch_data)
67 ag71xx_ar7240_stop(ag);
68
69 spin_lock_irqsave(&ag->lock, flags);
70 if (ag->link) {
71 ag->link = 0;
72 ag71xx_link_adjust(ag);
73 }
74 spin_unlock_irqrestore(&ag->lock, flags);
75 }
76
77 static int ag71xx_phy_connect_fixed(struct ag71xx *ag)
78 {
79 struct platform_device *pdev = ag->pdev;
80 struct device *dev = NULL;
81 struct ag71xx_platform_data *pdata = ag71xx_get_pdata(ag);
82 int ret = 0;
83
84 if (!pdev)
85 return -ENODEV;
86
87 dev = &pdev->dev;
88
89 if (!dev)
90 return -ENODEV;
91
92 if (!ag->phy_dev) {
93 pr_err("Missing PHY for %s", dev_name(dev));
94 return -ENODEV;
95 }
96
97 /* use fixed settings */
98 switch (pdata->speed) {
99 case SPEED_10:
100 case SPEED_100:
101 case SPEED_1000:
102 break;
103 default:
104 dev_err(dev, "invalid speed specified\n");
105 ret = -EINVAL;
106 break;
107 }
108
109 dev_dbg(dev, "using fixed link parameters\n");
110
111 ag->duplex = pdata->duplex;
112 ag->speed = pdata->speed;
113
114 if (!ret) {
115 dev_info(dev, "connected to fixed PHY at %s [uid=%08x, driver=%s]\n",
116 phydev_name(ag->phy_dev),
117 ag->phy_dev->phy_id, ag->phy_dev->drv->name);
118 } else {
119 pr_err("Failed to connect to fixed PHY\n");
120 }
121 return ret;
122 }
123
124 static int ag71xx_phy_connect_multi(struct ag71xx *ag)
125 {
126 struct device *dev = &ag->pdev->dev;
127 struct ag71xx_platform_data *pdata = ag71xx_get_pdata(ag);
128 struct phy_device *phydev = NULL;
129 int phy_addr;
130 int ret = 0;
131
132 for (phy_addr = 0; phy_addr < PHY_MAX_ADDR; phy_addr++) {
133 if (!(pdata->phy_mask & (1 << phy_addr)))
134 continue;
135
136 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,5,0)
137 if (ag->mii_bus->phy_map[phy_addr] == NULL)
138 continue;
139
140 DBG("%s: PHY found at %s, uid=%08x\n",
141 dev_name(dev),
142 dev_name(&ag->mii_bus->phy_map[phy_addr]->dev),
143 &ag->mii_bus->phy_map[phy_addr]->phy_id),
144 &ag->mii_bus->phy_map[phy_addr]->phy_id : 0);
145
146 if (phydev == NULL)
147 phydev = ag->mii_bus->phy_map[phy_addr];
148 #else
149 if (ag->mii_bus->mdio_map[phy_addr] == NULL)
150 continue;
151
152 DBG("%s: PHY found at %s, uid=%08x\n",
153 dev_name(dev),
154 dev_name(&ag->mii_bus->mdio_map[phy_addr]->dev),
155 mdiobus_get_phy(ag->mii_bus, phy_addr) ?
156 mdiobus_get_phy(ag->mii_bus, phy_addr)->phy_id : 0);
157
158 if (phydev == NULL)
159 phydev = mdiobus_get_phy(ag->mii_bus, phy_addr);
160 #endif
161 }
162
163 if (!phydev) {
164 dev_err(dev, "no PHY found with phy_mask=%08x\n",
165 pdata->phy_mask);
166 return -ENODEV;
167 }
168
169 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,5,0)
170 ag->phy_dev = phy_connect(ag->dev, dev_name(&phydev->dev),
171 #else
172 ag->phy_dev = phy_connect(ag->dev, phydev_name(phydev),
173 #endif
174 &ag71xx_phy_link_adjust,
175 pdata->phy_if_mode);
176
177 if (IS_ERR(ag->phy_dev)) {
178 dev_err(dev, "could not connect to PHY at %s\n",
179 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,5,0)
180 dev_name(&phydev->dev));
181 #else
182 phydev_name(phydev));
183 #endif
184 return PTR_ERR(ag->phy_dev);
185 }
186
187 /* mask with MAC supported features */
188 if (pdata->has_gbit)
189 phydev->supported &= PHY_GBIT_FEATURES;
190 else
191 phydev->supported &= PHY_BASIC_FEATURES;
192
193 phydev->advertising = phydev->supported;
194
195 dev_info(dev, "connected to PHY at %s [uid=%08x, driver=%s]\n",
196 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,5,0)
197 dev_name(&phydev->dev),
198 #else
199 phydev_name(phydev),
200 #endif
201 phydev->phy_id, phydev->drv->name);
202
203 ag->link = 0;
204 ag->speed = 0;
205 ag->duplex = -1;
206
207 return ret;
208 }
209
210 static int dev_is_class(struct device *dev, void *class)
211 {
212 if (dev->class != NULL && !strcmp(dev->class->name, class))
213 return 1;
214
215 return 0;
216 }
217
218 static struct device *dev_find_class(struct device *parent, char *class)
219 {
220 if (dev_is_class(parent, class)) {
221 get_device(parent);
222 return parent;
223 }
224
225 return device_find_child(parent, class, dev_is_class);
226 }
227
228 static struct mii_bus *dev_to_mii_bus(struct device *dev)
229 {
230 struct device *d;
231
232 d = dev_find_class(dev, "mdio_bus");
233 if (d != NULL) {
234 struct mii_bus *bus;
235
236 bus = to_mii_bus(d);
237 put_device(d);
238
239 return bus;
240 }
241
242 return NULL;
243 }
244
245 int ag71xx_phy_connect(struct ag71xx *ag)
246 {
247 struct ag71xx_platform_data *pdata = ag71xx_get_pdata(ag);
248
249 if (pdata->mii_bus_dev == NULL ||
250 pdata->mii_bus_dev->bus == NULL )
251 return ag71xx_phy_connect_fixed(ag);
252
253 ag->mii_bus = dev_to_mii_bus(pdata->mii_bus_dev);
254 if (ag->mii_bus == NULL) {
255 dev_err(&ag->pdev->dev, "unable to find MII bus on device '%s'\n",
256 dev_name(pdata->mii_bus_dev));
257 return -ENODEV;
258 }
259
260 /* Reset the mdio bus explicitly */
261 if (ag->mii_bus->reset) {
262 mutex_lock(&ag->mii_bus->mdio_lock);
263 ag->mii_bus->reset(ag->mii_bus);
264 mutex_unlock(&ag->mii_bus->mdio_lock);
265 }
266
267 if (pdata->switch_data)
268 return ag71xx_ar7240_init(ag);
269
270 if (pdata->phy_mask)
271 return ag71xx_phy_connect_multi(ag);
272
273 return ag71xx_phy_connect_fixed(ag);
274 }
275
276 void ag71xx_phy_disconnect(struct ag71xx *ag)
277 {
278 struct ag71xx_platform_data *pdata = ag71xx_get_pdata(ag);
279
280 if (pdata->switch_data)
281 ag71xx_ar7240_cleanup(ag);
282 else if (ag->phy_dev)
283 phy_disconnect(ag->phy_dev);
284 }