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