ar71xx: preliminary Ubiquiti Bullet M support
[openwrt.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-2009 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 unsigned char *ag71xx_speed_str(struct ag71xx *ag)
17 {
18 switch (ag->speed) {
19 case SPEED_1000:
20 return "1000";
21 case SPEED_100:
22 return "100";
23 case SPEED_10:
24 return "10";
25 }
26
27 return "?";
28 }
29
30 static void ag71xx_phy_link_update(struct ag71xx *ag)
31 {
32 struct ag71xx_platform_data *pdata = ag71xx_get_pdata(ag);
33 u32 cfg2;
34 u32 ifctl;
35 u32 fifo5;
36 u32 mii_speed;
37
38 if (!ag->link) {
39 netif_carrier_off(ag->dev);
40 if (netif_msg_link(ag))
41 printk(KERN_INFO "%s: link down\n", ag->dev->name);
42 return;
43 }
44
45 cfg2 = ag71xx_rr(ag, AG71XX_REG_MAC_CFG2);
46 cfg2 &= ~(MAC_CFG2_IF_1000 | MAC_CFG2_IF_10_100 | MAC_CFG2_FDX);
47 cfg2 |= (ag->duplex) ? MAC_CFG2_FDX : 0;
48
49 ifctl = ag71xx_rr(ag, AG71XX_REG_MAC_IFCTL);
50 ifctl &= ~(MAC_IFCTL_SPEED);
51
52 fifo5 = ag71xx_rr(ag, AG71XX_REG_FIFO_CFG5);
53 fifo5 &= ~FIFO_CFG5_BM;
54
55 switch (ag->speed) {
56 case SPEED_1000:
57 mii_speed = MII_CTRL_SPEED_1000;
58 cfg2 |= MAC_CFG2_IF_1000;
59 fifo5 |= FIFO_CFG5_BM;
60 break;
61 case SPEED_100:
62 mii_speed = MII_CTRL_SPEED_100;
63 cfg2 |= MAC_CFG2_IF_10_100;
64 ifctl |= MAC_IFCTL_SPEED;
65 break;
66 case SPEED_10:
67 mii_speed = MII_CTRL_SPEED_10;
68 cfg2 |= MAC_CFG2_IF_10_100;
69 break;
70 default:
71 BUG();
72 return;
73 }
74
75 ag71xx_wr(ag, AG71XX_REG_FIFO_CFG3,
76 pdata->is_ar91xx ? 0x780fff : 0x008001ff);
77
78 if (pdata->set_pll)
79 pdata->set_pll(ag->speed);
80
81 ag71xx_mii_ctrl_set_speed(ag, mii_speed);
82
83 ag71xx_wr(ag, AG71XX_REG_MAC_CFG2, cfg2);
84 ag71xx_wr(ag, AG71XX_REG_FIFO_CFG5, fifo5);
85 ag71xx_wr(ag, AG71XX_REG_MAC_IFCTL, ifctl);
86
87 netif_carrier_on(ag->dev);
88 if (netif_msg_link(ag))
89 printk(KERN_INFO "%s: link up (%sMbps/%s duplex)\n",
90 ag->dev->name,
91 ag71xx_speed_str(ag),
92 (DUPLEX_FULL == ag->duplex) ? "Full" : "Half");
93
94 DBG("%s: fifo_cfg0=%#x, fifo_cfg1=%#x, fifo_cfg2=%#x\n",
95 ag->dev->name,
96 ag71xx_rr(ag, AG71XX_REG_FIFO_CFG0),
97 ag71xx_rr(ag, AG71XX_REG_FIFO_CFG1),
98 ag71xx_rr(ag, AG71XX_REG_FIFO_CFG2));
99
100 DBG("%s: fifo_cfg3=%#x, fifo_cfg4=%#x, fifo_cfg5=%#x\n",
101 ag->dev->name,
102 ag71xx_rr(ag, AG71XX_REG_FIFO_CFG3),
103 ag71xx_rr(ag, AG71XX_REG_FIFO_CFG4),
104 ag71xx_rr(ag, AG71XX_REG_FIFO_CFG5));
105
106 DBG("%s: mac_cfg2=%#x, mac_ifctl=%#x, mii_ctrl=%#x\n",
107 ag->dev->name,
108 ag71xx_rr(ag, AG71XX_REG_MAC_CFG2),
109 ag71xx_rr(ag, AG71XX_REG_MAC_IFCTL),
110 ag71xx_mii_ctrl_rr(ag));
111 }
112
113 static void ag71xx_phy_link_adjust(struct net_device *dev)
114 {
115 struct ag71xx *ag = netdev_priv(dev);
116 struct phy_device *phydev = ag->phy_dev;
117 unsigned long flags;
118 int status_change = 0;
119
120 spin_lock_irqsave(&ag->lock, flags);
121
122 if (phydev->link) {
123 if (ag->duplex != phydev->duplex
124 || ag->speed != phydev->speed) {
125 status_change = 1;
126 }
127 }
128
129 if (phydev->link != ag->link)
130 status_change = 1;
131
132 ag->link = phydev->link;
133 ag->duplex = phydev->duplex;
134 ag->speed = phydev->speed;
135
136 if (status_change)
137 ag71xx_phy_link_update(ag);
138
139 spin_unlock_irqrestore(&ag->lock, flags);
140 }
141
142 void ag71xx_phy_start(struct ag71xx *ag)
143 {
144 if (ag->phy_dev) {
145 phy_start(ag->phy_dev);
146 } else {
147 struct ag71xx_platform_data *pdata = ag71xx_get_pdata(ag);
148
149 ag->duplex = pdata->duplex;
150 ag->speed = pdata->speed;
151 ag->link = 1;
152 ag71xx_phy_link_update(ag);
153 }
154 }
155
156 void ag71xx_phy_stop(struct ag71xx *ag)
157 {
158 if (ag->phy_dev) {
159 phy_stop(ag->phy_dev);
160 } else {
161 ag->duplex = -1;
162 ag->link = 0;
163 ag->speed = 0;
164 ag71xx_phy_link_update(ag);
165 }
166 }
167
168 static int ag71xx_phy_connect_fixed(struct ag71xx *ag)
169 {
170 struct net_device *dev = ag->dev;
171 struct ag71xx_platform_data *pdata = ag71xx_get_pdata(ag);
172 int ret = 0;
173
174 /* use fixed settings */
175 switch (pdata->speed) {
176 case SPEED_10:
177 case SPEED_100:
178 case SPEED_1000:
179 break;
180 default:
181 printk(KERN_ERR "%s: invalid speed specified\n",
182 dev->name);
183 ret = -EINVAL;
184 break;
185 }
186
187 return ret;
188 }
189
190 static int ag71xx_phy_connect_multi(struct ag71xx *ag)
191 {
192 struct net_device *dev = ag->dev;
193 struct ag71xx_platform_data *pdata = ag71xx_get_pdata(ag);
194 struct phy_device *phydev = NULL;
195 int phy_count = 0;
196 int phy_addr;
197 int ret = 0;
198
199 for (phy_addr = 0; phy_addr < PHY_MAX_ADDR; phy_addr++) {
200 if (!(pdata->phy_mask & (1 << phy_addr)))
201 continue;
202
203 if (ag->mii_bus->phy_map[phy_addr] == NULL)
204 continue;
205
206 DBG("%s: PHY found at %s, uid=%08x\n",
207 dev->name,
208 dev_name(&ag->mii_bus->phy_map[phy_addr]->dev),
209 ag->mii_bus->phy_map[phy_addr]->phy_id);
210
211 if (phydev == NULL)
212 phydev = ag->mii_bus->phy_map[phy_addr];
213
214 phy_count++;
215 }
216
217 switch (phy_count) {
218 case 0:
219 printk(KERN_ERR "%s: no PHY found with phy_mask=%08x\n",
220 dev->name, pdata->phy_mask);
221 ret = -ENODEV;
222 break;
223 case 1:
224 ag->phy_dev = phy_connect(dev, dev_name(&phydev->dev),
225 &ag71xx_phy_link_adjust, 0, pdata->phy_if_mode);
226
227 if (IS_ERR(ag->phy_dev)) {
228 printk(KERN_ERR "%s: could not connect to PHY at %s\n",
229 dev->name, dev_name(&phydev->dev));
230 return PTR_ERR(ag->phy_dev);
231 }
232
233 /* mask with MAC supported features */
234 if (pdata->has_gbit)
235 phydev->supported &= PHY_GBIT_FEATURES;
236 else
237 phydev->supported &= PHY_BASIC_FEATURES;
238
239 phydev->advertising = phydev->supported;
240
241 printk(KERN_DEBUG "%s: connected to PHY at %s "
242 "[uid=%08x, driver=%s]\n",
243 dev->name, dev_name(&phydev->dev),
244 phydev->phy_id, phydev->drv->name);
245
246 ag->link = 0;
247 ag->speed = 0;
248 ag->duplex = -1;
249 break;
250
251 default:
252 printk(KERN_DEBUG "%s: connected to %d PHYs\n",
253 dev->name, phy_count);
254 ret = ag71xx_phy_connect_fixed(ag);
255 break;
256 }
257
258 return ret;
259 }
260
261 int ag71xx_phy_connect(struct ag71xx *ag)
262 {
263 struct ag71xx_platform_data *pdata = ag71xx_get_pdata(ag);
264
265 if (pdata->phy_mask)
266 return ag71xx_phy_connect_multi(ag);
267
268 return ag71xx_phy_connect_fixed(ag);
269 }
270
271 void ag71xx_phy_disconnect(struct ag71xx *ag)
272 {
273 if (ag->phy_dev)
274 phy_disconnect(ag->phy_dev);
275 }
This page took 0.052882 seconds and 5 git commands to generate.