1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include <common.h>
26#include <netdev.h>
27#include "mv88e61xx.h"
28
29#ifdef CONFIG_MV88E61XX_MULTICHIP_ADRMODE
30
31
32
33
34
35
36
37
38
39static int mv88e61xx_busychk_multic(char *name, u32 devaddr)
40{
41 u16 reg = 0;
42 u32 timeout = MV88E61XX_PHY_TIMEOUT;
43
44
45 do {
46 miiphy_read(name, devaddr, 0x0, ®);
47 if (timeout-- == 0) {
48 printf("SMI busy timeout\n");
49 return -1;
50 }
51 } while (reg & (1 << 15));
52 return 0;
53}
54
55static void mv88e61xx_wr_phy(char *name, u32 phy_adr, u32 reg_ofs, u16 data)
56{
57 u16 mii_dev_addr;
58
59
60 if (miiphy_read(name, 0xEE, 0xEE, &mii_dev_addr)) {
61 printf("Error..could not read PHY dev address\n");
62 return;
63 }
64 mv88e61xx_busychk_multic(name, mii_dev_addr);
65
66 miiphy_write(name, mii_dev_addr, 0x1, data);
67
68 miiphy_write(name, mii_dev_addr, 0x0,
69 reg_ofs | (phy_adr << 5) | (1 << 10) | (1 << 12) | (1 <<
70 15));
71}
72
73static void mv88e61xx_rd_phy(char *name, u32 phy_adr, u32 reg_ofs, u16 * data)
74{
75 u16 mii_dev_addr;
76
77
78 if (miiphy_read(name, 0xEE, 0xEE, &mii_dev_addr)) {
79 printf("Error..could not read PHY dev address\n");
80 return;
81 }
82 mv88e61xx_busychk_multic(name, mii_dev_addr);
83
84 miiphy_write(name, mii_dev_addr, 0x0,
85 reg_ofs | (phy_adr << 5) | (1 << 11) | (1 << 12) | (1 <<
86 15));
87 mv88e61xx_busychk_multic(name, mii_dev_addr);
88
89 miiphy_read(name, mii_dev_addr, 0x1, data);
90}
91#endif
92
93static void mv88e61xx_port_vlan_config(struct mv88e61xx_config *swconfig,
94 u32 max_prtnum, u32 ports_ofs)
95{
96 u32 prt;
97 u16 reg;
98 char *name = swconfig->name;
99 u32 cpu_port = swconfig->cpuport;
100 u32 port_mask = swconfig->ports_enabled;
101 enum mv88e61xx_cfg_vlan vlancfg = swconfig->vlancfg;
102
103
104 for (prt = 0; prt < max_prtnum; prt++) {
105 RD_PHY(name, ports_ofs + prt, MV88E61XX_PRT_CTRL_REG, ®);
106 reg &= ~0x3;
107 WR_PHY(name, ports_ofs + prt, MV88E61XX_PRT_CTRL_REG, reg);
108
109 if (!(cpu_port & (1 << prt)))
110 continue;
111
112 RD_PHY(name, (ports_ofs + prt), MV88E61XX_PRT_VID_REG, ®);
113 reg &= ~0xfff;
114 reg |= 0x1;
115 WR_PHY(name, (ports_ofs + prt), MV88E61XX_PRT_VID_REG, reg);
116 }
117
118
119 for (prt = 0; prt < max_prtnum; prt++) {
120 RD_PHY(name, ports_ofs + prt, MV88E61XX_PRT_VID_REG, ®);
121 reg &= ~0xc000;
122 WR_PHY(name, ports_ofs + prt, MV88E61XX_PRT_VID_REG, reg);
123 }
124
125 for (prt = 0; prt < max_prtnum; prt++) {
126
127 if ((1 << prt) & port_mask) {
128
129 if ((1 << prt) & cpu_port) {
130
131
132
133
134 RD_PHY(name, (ports_ofs + prt),
135 MV88E61XX_PRT_VMAP_REG, ®);
136 reg &= ~((1 << max_prtnum) - 1);
137 reg |= port_mask & ~(1 << prt);
138 WR_PHY(name, (ports_ofs + prt),
139 MV88E61XX_PRT_VMAP_REG, reg);
140 } else {
141
142
143
144
145
146 RD_PHY(name, ports_ofs + prt,
147 MV88E61XX_PRT_VID_REG, ®);
148 reg &= ~0x0fff;
149 reg |= (prt + 1);
150 WR_PHY(name, ports_ofs + prt,
151 MV88E61XX_PRT_VID_REG, reg);
152
153 RD_PHY(name, ports_ofs + prt,
154 MV88E61XX_PRT_VMAP_REG, ®);
155 if (vlancfg == MV88E61XX_VLANCFG_DEFAULT) {
156
157
158
159
160 reg |= 0x03f;
161 reg &= ~(1 << prt);
162 } else if (vlancfg == MV88E61XX_VLANCFG_ROUTER) {
163
164
165
166
167 reg &= ~((1 << max_prtnum) - 1);
168 reg |= cpu_port;
169 }
170 WR_PHY(name, ports_ofs + prt,
171 MV88E61XX_PRT_VMAP_REG, reg);
172 }
173 }
174 }
175
176
177
178
179
180 for (prt = 0; prt < max_prtnum; prt++) {
181 if ((1 << prt) & port_mask) {
182 RD_PHY(name, ports_ofs + prt,
183 MV88E61XX_PRT_CTRL_REG, ®);
184 reg |= 0x3;
185 WR_PHY(name, ports_ofs + prt,
186 MV88E61XX_PRT_CTRL_REG, reg);
187 } else {
188
189 RD_PHY(name, ports_ofs + prt,
190 MV88E61XX_PRT_CTRL_REG, ®);
191 reg &= ~0x3;
192 WR_PHY(name, ports_ofs + prt,
193 MV88E61XX_PRT_CTRL_REG, reg);
194 }
195 }
196}
197
198
199
200
201
202static int mv88e61xx_busychk(char *name)
203{
204 u16 reg = 0;
205 u32 timeout = MV88E61XX_PHY_TIMEOUT;
206 do {
207 RD_PHY(name, MV88E61XX_GLB2REG_DEVADR,
208 MV88E61XX_PHY_CMD, ®);
209 if (timeout-- == 0) {
210 printf("SMI busy timeout\n");
211 return -1;
212 }
213 } while (reg & 1 << 15);
214 return 0;
215}
216
217
218
219
220static int mv88361xx_powerup(struct mv88e61xx_config *swconfig, u32 prt)
221{
222 char *name = swconfig->name;
223
224
225
226
227
228
229 WR_PHY(name, MV88E61XX_GLB2REG_DEVADR, MV88E61XX_PHY_DATA, 0x3360);
230 WR_PHY(name, MV88E61XX_GLB2REG_DEVADR,
231 MV88E61XX_PHY_CMD, (0x9410 | (prt << 5)));
232
233 if (mv88e61xx_busychk(name))
234 return -1;
235
236
237
238
239
240 WR_PHY(name, MV88E61XX_GLB2REG_DEVADR, MV88E61XX_PHY_DATA, 0x1140);
241 WR_PHY(name, MV88E61XX_GLB2REG_DEVADR,
242 MV88E61XX_PHY_CMD, (0x9400 | (prt << 5)));
243
244 if (mv88e61xx_busychk(name))
245 return -1;
246
247 return 0;
248}
249
250
251
252
253
254
255
256
257
258
259static int mv88361xx_led_init(struct mv88e61xx_config *swconfig, u32 prt)
260{
261 char *name = swconfig->name;
262 u16 reg;
263
264 if (swconfig->led_init != MV88E61XX_LED_INIT_EN)
265 return 0;
266
267
268 reg = 3;
269 WR_PHY(name, MV88E61XX_GLB2REG_DEVADR, MV88E61XX_PHY_DATA, reg);
270 WR_PHY(name, MV88E61XX_GLB2REG_DEVADR,
271 MV88E61XX_PHY_CMD, (1 << MV88E61XX_BUSY_OFST |
272 1 << MV88E61XX_MODE_OFST |
273 1 << MV88E61XX_OP_OFST |
274 prt << MV88E61XX_ADDR_OFST | 22));
275
276 if (mv88e61xx_busychk(name))
277 return -1;
278
279
280 reg = 1;
281 WR_PHY(name, MV88E61XX_GLB2REG_DEVADR, MV88E61XX_PHY_DATA, reg);
282 WR_PHY(name, MV88E61XX_GLB2REG_DEVADR,
283 MV88E61XX_PHY_CMD, (1 << MV88E61XX_BUSY_OFST |
284 1 << MV88E61XX_MODE_OFST |
285 1 << MV88E61XX_OP_OFST |
286 prt << MV88E61XX_ADDR_OFST | 16));
287
288 if (mv88e61xx_busychk(name))
289 return -1;
290
291
292 reg = 0;
293 WR_PHY(name, MV88E61XX_GLB2REG_DEVADR, MV88E61XX_PHY_DATA, reg);
294 WR_PHY(name, MV88E61XX_GLB2REG_DEVADR,
295 MV88E61XX_PHY_CMD, (1 << MV88E61XX_BUSY_OFST |
296 1 << MV88E61XX_MODE_OFST |
297 1 << MV88E61XX_OP_OFST |
298 prt << MV88E61XX_ADDR_OFST | 22));
299
300 if (mv88e61xx_busychk(name))
301 return -1;
302
303 return 0;
304}
305
306
307
308
309
310
311
312
313
314
315static int mv88361xx_reverse_mdipn(struct mv88e61xx_config *swconfig, u32 prt)
316{
317 char *name = swconfig->name;
318 u16 reg;
319
320 if (swconfig->mdip != MV88E61XX_MDIP_REVERSE)
321 return 0;
322
323 reg = 0x0f;
324 WR_PHY(name, MV88E61XX_GLB2REG_DEVADR, MV88E61XX_PHY_DATA, reg);
325 WR_PHY(name, MV88E61XX_GLB2REG_DEVADR,
326 MV88E61XX_PHY_CMD, (1 << MV88E61XX_BUSY_OFST |
327 1 << MV88E61XX_MODE_OFST |
328 1 << MV88E61XX_OP_OFST |
329 prt << MV88E61XX_ADDR_OFST | 20));
330
331 if (mv88e61xx_busychk(name))
332 return -1;
333
334 return 0;
335}
336
337
338
339
340int mv88e61xx_switch_initialize(struct mv88e61xx_config *swconfig)
341{
342 u32 prt;
343 u16 reg;
344 char *idstr;
345 char *name = swconfig->name;
346
347 if (miiphy_set_current_dev(name)) {
348 printf("%s failed\n", __FUNCTION__);
349 return -1;
350 }
351
352 if (!(swconfig->cpuport & ((1 << 4) | (1 << 5)))) {
353 swconfig->cpuport = (1 << 5);
354 printf("Invalid cpu port config, using default port5\n");
355 }
356
357 RD_PHY(name, MV88E61XX_PRT_OFST, PHY_PHYIDR2, ®);
358 switch (reg &= 0xfff0) {
359 case 0x1610:
360 idstr = "88E6161";
361 break;
362 case 0x1650:
363 idstr = "88E6165";
364 break;
365 case 0x1210:
366 idstr = "88E6123";
367
368 swconfig->ports_enabled &= 0x023;
369 break;
370 default:
371
372 idstr = "88E61??";
373 break;
374 }
375
376
377 if ((swconfig->vlancfg == MV88E61XX_VLANCFG_DEFAULT)
378 || (swconfig->vlancfg == MV88E61XX_VLANCFG_ROUTER))
379 mv88e61xx_port_vlan_config(swconfig, MV88E61XX_MAX_PORTS_NUM,
380 MV88E61XX_PRT_OFST);
381 else {
382 printf("Unsupported mode %s failed\n", __FUNCTION__);
383 return -1;
384 }
385
386 if (swconfig->rgmii_delay == MV88E61XX_RGMII_DELAY_EN) {
387
388
389
390
391 WR_PHY(name, MV88E61XX_PRT_OFST + 5,
392 MV88E61XX_RGMII_TIMECTRL_REG, 0x18);
393 WR_PHY(name, MV88E61XX_PRT_OFST + 4,
394 MV88E61XX_RGMII_TIMECTRL_REG, 0xc1e7);
395 }
396
397 for (prt = 0; prt < MV88E61XX_MAX_PORTS_NUM; prt++) {
398 if (!((1 << prt) & swconfig->cpuport)) {
399
400 if (mv88361xx_led_init(swconfig, prt))
401 return -1;
402 if (mv88361xx_reverse_mdipn(swconfig, prt))
403 return -1;
404 if (mv88361xx_powerup(swconfig, prt))
405 return -1;
406 }
407
408
409 RD_PHY(name, MV88E61XX_PRT_OFST + prt,
410 MV88E61XX_PRT_CTRL_REG, ®);
411 WR_PHY(name, MV88E61XX_PRT_OFST + prt,
412 MV88E61XX_PRT_CTRL_REG,
413 reg | (swconfig->portstate & 0x03));
414 }
415
416 printf("%s Initialized on %s\n", idstr, name);
417 return 0;
418}
419