Revert "ar71xx: ag71xx: Add connect message: fixed phy"
[openwrt/openwrt.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 return ret;
115 }
116
117 static int ag71xx_phy_connect_multi(struct ag71xx *ag)
118 {
119 struct device *dev = &ag->pdev->dev;
120 struct ag71xx_platform_data *pdata = ag71xx_get_pdata(ag);
121 struct phy_device *phydev = NULL;
122 int phy_addr;
123 int ret = 0;
124
125 for (phy_addr = 0; phy_addr < PHY_MAX_ADDR; phy_addr++) {
126 if (!(pdata->phy_mask & (1 << phy_addr)))
127 continue;
128
129 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,5,0)
130 if (ag->mii_bus->phy_map[phy_addr] == NULL)
131 continue;
132
133 DBG("%s: PHY found at %s, uid=%08x\n",
134 dev_name(dev),
135 dev_name(&ag->mii_bus->phy_map[phy_addr]->dev),
136 ag->mii_bus->phy_map[phy_addr]->phy_id);
137
138 if (phydev == NULL)
139 phydev = ag->mii_bus->phy_map[phy_addr];
140 #else
141 if (ag->mii_bus->mdio_map[phy_addr] == NULL)
142 continue;
143
144 DBG("%s: PHY found at %s, uid=%08x\n",
145 dev_name(dev),
146 dev_name(&ag->mii_bus->mdio_map[phy_addr]->dev),
147 ag->mii_bus->mdio_map[phy_addr]->phy_id);
148
149 if (phydev == NULL)
150 phydev = mdiobus_get_phy(ag->mii_bus, phy_addr);
151 #endif
152 }
153
154 if (!phydev) {
155 dev_err(dev, "no PHY found with phy_mask=%08x\n",
156 pdata->phy_mask);
157 return -ENODEV;
158 }
159
160 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,5,0)
161 ag->phy_dev = phy_connect(ag->dev, dev_name(&phydev->dev),
162 #else
163 ag->phy_dev = phy_connect(ag->dev, phydev_name(phydev),
164 #endif
165 &ag71xx_phy_link_adjust,
166 pdata->phy_if_mode);
167
168 if (IS_ERR(ag->phy_dev)) {
169 dev_err(dev, "could not connect to PHY at %s\n",
170 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,5,0)
171 dev_name(&phydev->dev));
172 #else
173 phydev_name(phydev));
174 #endif
175 return PTR_ERR(ag->phy_dev);
176 }
177
178 /* mask with MAC supported features */
179 if (pdata->has_gbit)
180 phydev->supported &= PHY_GBIT_FEATURES;
181 else
182 phydev->supported &= PHY_BASIC_FEATURES;
183
184 phydev->advertising = phydev->supported;
185
186 dev_info(dev, "connected to PHY at %s [uid=%08x, driver=%s]\n",
187 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,5,0)
188 dev_name(&phydev->dev),
189 #else
190 phydev_name(phydev),
191 #endif
192 phydev->phy_id, phydev->drv->name);
193
194 ag->link = 0;
195 ag->speed = 0;
196 ag->duplex = -1;
197
198 return ret;
199 }
200
201 static int dev_is_class(struct device *dev, void *class)
202 {
203 if (dev->class != NULL && !strcmp(dev->class->name, class))
204 return 1;
205
206 return 0;
207 }
208
209 static struct device *dev_find_class(struct device *parent, char *class)
210 {
211 if (dev_is_class(parent, class)) {
212 get_device(parent);
213 return parent;
214 }
215
216 return device_find_child(parent, class, dev_is_class);
217 }
218
219 static struct mii_bus *dev_to_mii_bus(struct device *dev)
220 {
221 struct device *d;
222
223 d = dev_find_class(dev, "mdio_bus");
224 if (d != NULL) {
225 struct mii_bus *bus;
226
227 bus = to_mii_bus(d);
228 put_device(d);
229
230 return bus;
231 }
232
233 return NULL;
234 }
235
236 int ag71xx_phy_connect(struct ag71xx *ag)
237 {
238 struct ag71xx_platform_data *pdata = ag71xx_get_pdata(ag);
239
240 if (pdata->mii_bus_dev == NULL ||
241 pdata->mii_bus_dev->bus == NULL )
242 return ag71xx_phy_connect_fixed(ag);
243
244 ag->mii_bus = dev_to_mii_bus(pdata->mii_bus_dev);
245 if (ag->mii_bus == NULL) {
246 dev_err(&ag->pdev->dev, "unable to find MII bus on device '%s'\n",
247 dev_name(pdata->mii_bus_dev));
248 return -ENODEV;
249 }
250
251 /* Reset the mdio bus explicitly */
252 if (ag->mii_bus->reset) {
253 mutex_lock(&ag->mii_bus->mdio_lock);
254 ag->mii_bus->reset(ag->mii_bus);
255 mutex_unlock(&ag->mii_bus->mdio_lock);
256 }
257
258 if (pdata->switch_data)
259 return ag71xx_ar7240_init(ag);
260
261 if (pdata->phy_mask)
262 return ag71xx_phy_connect_multi(ag);
263
264 return ag71xx_phy_connect_fixed(ag);
265 }
266
267 void ag71xx_phy_disconnect(struct ag71xx *ag)
268 {
269 struct ag71xx_platform_data *pdata = ag71xx_get_pdata(ag);
270
271 if (pdata->switch_data)
272 ag71xx_ar7240_cleanup(ag);
273 else if (ag->phy_dev)
274 phy_disconnect(ag->phy_dev);
275 }