[kernel] swconfig: Check vlan/port indexes for validity.
[openwrt.git] / target / linux / generic-2.6 / files / drivers / net / phy / ip175c.c
1 /*
2 * swconfig-ip175c.c: Swconfig configuration for IC+ IP175C switch
3 *
4 * Copyright (C) 2008 Patrick Horn <patrick.horn@gmail.com>
5 * Copyright (C) 2008 Martin Mares <mj@ucw.cz>
6 * Copyright (C) 2009 Felix Fietkau <nbd@openwrt.org>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 */
18
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/list.h>
23 #include <linux/skbuff.h>
24 #include <linux/mii.h>
25 #include <linux/phy.h>
26 #include <linux/delay.h>
27 #include <linux/switch.h>
28
29 #define MAX_VLANS 16
30 #define MAX_PORTS 9
31
32 typedef struct ip175c_reg {
33 u16 p; // phy
34 u16 m; // mii
35 } reg;
36 typedef char bitnum;
37
38 #define NOTSUPPORTED {-1,-1}
39
40 #define REG_SUPP(x) (((x).m != ((u16)-1)) && ((x).p != (u16)-1))
41
42 /*********** CONSTANTS ***********/
43 struct register_mappings {
44 char *NAME;
45 u16 MODEL_NO; // compare to bits 4-9 of MII register 0,3.
46 bitnum NUM_PORTS;
47 bitnum CPU_PORT;
48
49 /* The default VLAN for each port.
50 Default: 0x0001 for Ports 0,1,2,3
51 0x0002 for Ports 4,5 */
52 reg VLAN_DEFAULT_TAG_REG[MAX_PORTS];
53
54 /* These ports are tagged.
55 Default: 0x00 */
56 reg ADD_TAG_REG;
57 reg REMOVE_TAG_REG;
58 bitnum ADD_TAG_BIT[MAX_PORTS];
59 /* These ports are untagged.
60 Default: 0x00 (i.e. do not alter any VLAN tags...)
61 Maybe set to 0 if user disables VLANs. */
62 bitnum REMOVE_TAG_BIT[MAX_PORTS];
63
64 /* Port M and Port N are on the same VLAN.
65 Default: All ports on all VLANs. */
66 // Use register {29, 19+N/2}
67 reg VLAN_LOOKUP_REG;
68 // Port 5 uses register {30, 18} but same as odd bits.
69 reg VLAN_LOOKUP_REG_5; // in a different register on IP175C.
70 bitnum VLAN_LOOKUP_EVEN_BIT[MAX_PORTS];
71 bitnum VLAN_LOOKUP_ODD_BIT[MAX_PORTS];
72
73 /* This VLAN corresponds to which ports.
74 Default: 0x2f,0x30,0x3f,0x3f... */
75 reg TAG_VLAN_MASK_REG;
76 bitnum TAG_VLAN_MASK_EVEN_BIT[MAX_PORTS];
77 bitnum TAG_VLAN_MASK_ODD_BIT[MAX_PORTS];
78
79 int RESET_VAL;
80 reg RESET_REG;
81
82 reg MODE_REG;
83 int MODE_VAL;
84
85 /* General flags */
86 reg ROUTER_CONTROL_REG;
87 reg VLAN_CONTROL_REG;
88 bitnum TAG_VLAN_BIT;
89 bitnum ROUTER_EN_BIT;
90 bitnum NUMLAN_GROUPS_MAX;
91 bitnum NUMLAN_GROUPS_BIT;
92
93 reg MII_REGISTER_EN;
94 bitnum MII_REGISTER_EN_BIT;
95
96 // set to 1 for 178C, 0 for 175C.
97 bitnum SIMPLE_VLAN_REGISTERS; // 175C has two vlans per register but 178C has only one.
98 };
99
100 static const struct register_mappings IP178C = {
101 .NAME = "IP178C",
102 .MODEL_NO = 0x18,
103 .VLAN_DEFAULT_TAG_REG = {
104 {30,3},{30,4},{30,5},{30,6},{30,7},{30,8},
105 {30,9},{30,10},{30,11},
106 },
107
108 .ADD_TAG_REG = {30,12},
109 .ADD_TAG_BIT = {0,1,2,3,4,5,6,7,8},
110 .REMOVE_TAG_REG = {30,13},
111 .REMOVE_TAG_BIT = {4,5,6,7,8,9,10,11,12},
112
113 .SIMPLE_VLAN_REGISTERS = 1,
114
115 .VLAN_LOOKUP_REG = {31,0},// +N
116 .VLAN_LOOKUP_REG_5 = NOTSUPPORTED, // not used with SIMPLE_VLAN_REGISTERS
117 .VLAN_LOOKUP_EVEN_BIT = {0,1,2,3,4,5,6,7,8},
118 .VLAN_LOOKUP_ODD_BIT = {0,1,2,3,4,5,6,7,8},
119
120 .TAG_VLAN_MASK_REG = {30,14}, // +N
121 .TAG_VLAN_MASK_EVEN_BIT = {0,1,2,3,4,5,6,7,8},
122 .TAG_VLAN_MASK_ODD_BIT = {0,1,2,3,4,5,6,7,8},
123
124 .RESET_VAL = 0x55AA,
125 .RESET_REG = {30,0},
126 .MODE_VAL = 0,
127 .MODE_REG = NOTSUPPORTED,
128
129 .ROUTER_CONTROL_REG = {30,30},
130 .ROUTER_EN_BIT = 11,
131 .NUMLAN_GROUPS_MAX = 8,
132 .NUMLAN_GROUPS_BIT = 8, // {0-2}
133
134 .VLAN_CONTROL_REG = {30,13},
135 .TAG_VLAN_BIT = 3,
136
137 .CPU_PORT = 8,
138 .NUM_PORTS = 9,
139
140 .MII_REGISTER_EN = NOTSUPPORTED,
141
142 };
143
144 static const struct register_mappings IP175C = {
145 .NAME = "IP175C",
146 .MODEL_NO = 0x18,
147 .VLAN_DEFAULT_TAG_REG = {
148 {29,24},{29,25},{29,26},{29,27},{29,28},{29,30},
149 NOTSUPPORTED,NOTSUPPORTED,NOTSUPPORTED
150 },
151
152 .ADD_TAG_REG = {29,23},
153 .REMOVE_TAG_REG = {29,23},
154 .ADD_TAG_BIT = {11,12,13,14,15,1,-1,-1,-1},
155 .REMOVE_TAG_BIT = {6,7,8,9,10,0,-1,-1,-1},
156
157 .SIMPLE_VLAN_REGISTERS = 0,
158
159 .VLAN_LOOKUP_REG = {29,19},// +N/2
160 .VLAN_LOOKUP_REG_5 = {30,18},
161 .VLAN_LOOKUP_EVEN_BIT = {8,9,10,11,12,15,-1,-1,-1},
162 .VLAN_LOOKUP_ODD_BIT = {0,1,2,3,4,7,-1,-1,-1},
163
164 .TAG_VLAN_MASK_REG = {30,1}, // +N/2
165 .TAG_VLAN_MASK_EVEN_BIT = {0,1,2,3,4,5,-1,-1,-1},
166 .TAG_VLAN_MASK_ODD_BIT = {8,9,10,11,12,13,-1,-1,-1},
167
168 .RESET_VAL = 0x175C,
169 .RESET_REG = {30,0},
170 .MODE_VAL = 0x175C,
171 .MODE_REG = {29,31},
172
173 .ROUTER_CONTROL_REG = {30,9},
174 .ROUTER_EN_BIT = 3,
175 .NUMLAN_GROUPS_MAX = 8,
176 .NUMLAN_GROUPS_BIT = 0, // {0-2}
177
178 .VLAN_CONTROL_REG = {30,9},
179 .TAG_VLAN_BIT = 7,
180
181 .NUM_PORTS = 6,
182 .CPU_PORT = 5,
183
184 .MII_REGISTER_EN = NOTSUPPORTED,
185
186 };
187
188 static const struct register_mappings IP175A = {
189 .NAME = "IP175A",
190 .MODEL_NO = 0x05,
191 .VLAN_DEFAULT_TAG_REG = {
192 {0,24},{0,25},{0,26},{0,27},{0,28},NOTSUPPORTED,
193 NOTSUPPORTED,NOTSUPPORTED,NOTSUPPORTED
194 },
195
196 .ADD_TAG_REG = {0,23},
197 .REMOVE_TAG_REG = {0,23},
198 .ADD_TAG_BIT = {11,12,13,14,15,-1,-1,-1,-1},
199 .REMOVE_TAG_BIT = {6,7,8,9,10,-1,-1,-1,-1},
200
201 .SIMPLE_VLAN_REGISTERS = 0,
202
203 // Register 19-21 documentation is missing/contradictory.
204 // For registers 19-21 ports need to be: even numbers to MSB, odd to LSB.
205 // This contradicts text for ROM registers, but follows logic of CoS bits.
206
207 .VLAN_LOOKUP_REG = {0,19},// +N/2
208 .VLAN_LOOKUP_REG_5 = NOTSUPPORTED,
209 .VLAN_LOOKUP_EVEN_BIT = {8,9,10,11,12,-1,-1,-1,-1},
210 .VLAN_LOOKUP_ODD_BIT = {0,1,2,3,4,-1,-1,-1,-1},
211
212 .TAG_VLAN_MASK_REG = NOTSUPPORTED, // +N/2,
213 .TAG_VLAN_MASK_EVEN_BIT = {-1,-1,-1,-1,-1,-1,-1,-1,-1},
214 .TAG_VLAN_MASK_ODD_BIT = {-1,-1,-1,-1,-1,-1,-1,-1,-1},
215
216 .RESET_VAL = -1,
217 .RESET_REG = NOTSUPPORTED,
218 .MODE_VAL = 0,
219 .MODE_REG = NOTSUPPORTED,
220
221 .ROUTER_CONTROL_REG = NOTSUPPORTED,
222 .VLAN_CONTROL_REG = NOTSUPPORTED,
223 .TAG_VLAN_BIT = -1,
224 .ROUTER_EN_BIT = -1,
225 .NUMLAN_GROUPS_MAX = -1,
226 .NUMLAN_GROUPS_BIT = -1, // {0-2}
227
228 .NUM_PORTS = 5,
229 .CPU_PORT = 4,
230
231 .MII_REGISTER_EN = {0, 18},
232 .MII_REGISTER_EN_BIT = 7,
233 };
234
235
236 struct ip175c_state {
237 struct switch_dev dev;
238 struct mii_bus *mii_bus;
239 bool registered;
240
241 int router_mode; // ROUTER_EN
242 int vlan_enabled; // TAG_VLAN_EN
243 struct port_state {
244 u16 pvid;
245 unsigned int shareports;
246 } ports[MAX_PORTS];
247 unsigned int add_tag;
248 unsigned int remove_tag;
249 int num_vlans;
250 unsigned int vlan_ports[MAX_VLANS];
251 const struct register_mappings *regs;
252 reg proc_mii; /*!< phy/reg for the low level register access via /proc */
253
254 char buf[80];
255 };
256
257 static int ip_phy_read(struct mii_bus *bus, int port, int reg)
258 {
259 int val;
260
261 mutex_lock(&bus->mdio_lock);
262 val = bus->read(bus, port, reg);
263 mutex_unlock(&bus->mdio_lock);
264
265 return val;
266 }
267
268
269 static int ip_phy_write(struct mii_bus *bus, int port, int reg, u16 val)
270 {
271 int err;
272
273 mutex_lock(&bus->mdio_lock);
274 err = bus->write(bus, port, reg, val);
275 mutex_unlock(&bus->mdio_lock);
276
277 return err;
278 }
279
280
281 static int getPhy (struct ip175c_state *state, reg mii)
282 {
283 struct mii_bus *bus = state->mii_bus;
284 int val;
285
286 if (!REG_SUPP(mii))
287 return -EFAULT;
288
289 val = ip_phy_read(bus, mii.p, mii.m);
290 if (val < 0)
291 pr_warning("IP175C: Unable to get MII register %d,%d: error %d\n", mii.p,mii.m,-val);
292
293 return val;
294 }
295
296 static int setPhy (struct ip175c_state *state, reg mii, u16 value)
297 {
298 struct mii_bus *bus = state->mii_bus;
299 int err;
300
301 if (!REG_SUPP(mii))
302 return -EFAULT;
303
304 err = ip_phy_write(bus, mii.p, mii.m, value);
305 if (err < 0) {
306 pr_warning("IP175C: Unable to set MII register %d,%d to %d: error %d\n", mii.p,mii.m,value,-err);
307 return err;
308 }
309 mdelay(2);
310 getPhy(state, mii);
311 return 0;
312 }
313
314 /**
315 * These two macros are to simplify the mapping of logical bits to the bits in hardware.
316 * NOTE: these macros will return if there is an error!
317 */
318
319 #define GET_PORT_BITS(state, bits, addr, bit_lookup) \
320 do { \
321 int i, val = getPhy((state), (addr)); \
322 if (val < 0) \
323 return val; \
324 (bits) = 0; \
325 for (i = 0; i < MAX_PORTS; i++) { \
326 if ((bit_lookup)[i] == -1) continue; \
327 if (val & (1<<(bit_lookup)[i])) \
328 (bits) |= (1<<i); \
329 } \
330 } while (0)
331
332 #define SET_PORT_BITS(state, bits, addr, bit_lookup) \
333 do { \
334 int i, val = getPhy((state), (addr)); \
335 if (val < 0) \
336 return val; \
337 for (i = 0; i < MAX_PORTS; i++) { \
338 unsigned int newmask = ((bits)&(1<<i)); \
339 if ((bit_lookup)[i] == -1) continue; \
340 val &= ~(1<<(bit_lookup)[i]); \
341 val |= ((newmask>>i)<<(bit_lookup)[i]); \
342 } \
343 val = setPhy((state), (addr), val); \
344 if (val < 0) \
345 return val; \
346 } while (0)
347
348 static int get_model(struct ip175c_state *state)
349 {
350 reg oui_id_reg = {0, 2};
351 int oui_id;
352 reg model_no_reg = {0, 3};
353 int model_no, model_no_orig;
354
355 // 175 and 178 have the same oui ID.
356 reg oui_id_reg_178c = {5, 2}; // returns error on IP175C.
357 int is_178c = 0;
358
359 oui_id = getPhy(state, oui_id_reg);
360 if (oui_id != 0x0243) {
361 // non
362 return -ENODEV; // Not a IC+ chip.
363 }
364 oui_id = getPhy(state, oui_id_reg_178c);
365 if (oui_id == 0x0243) {
366 is_178c = 1;
367 }
368
369 model_no_orig = getPhy(state, model_no_reg);
370 if (model_no_orig < 0) {
371 return -ENODEV;
372 }
373 model_no = model_no_orig >> 4; // shift out revision number.
374 model_no &= 0x3f; // only take the model number (low 6 bits).
375 if (model_no == IP175A.MODEL_NO) {
376 state->regs = &IP175A;
377 } else if (model_no == IP175C.MODEL_NO) {
378 if (is_178c) {
379 state->regs = &IP178C;
380 } else {
381 state->regs = &IP175C;
382 }
383 } else {
384 printk(KERN_WARNING "ip175c: Found an unknown IC+ switch with model number %02Xh.\n", model_no_orig);
385 return -EPERM;
386 }
387 return 0;
388 }
389
390 /** Get only the vlan and router flags on the router **/
391 static int get_flags(struct ip175c_state *state)
392 {
393 int val;
394
395 state->router_mode = 0;
396 state->vlan_enabled = -1; // hack
397 state->num_vlans = 0;
398
399 if (!REG_SUPP(state->regs->ROUTER_CONTROL_REG)) {
400 return 0; // not an error if it doesn't support enable vlan.
401 }
402
403 val = getPhy(state, state->regs->ROUTER_CONTROL_REG);
404 if (val < 0) {
405 return val;
406 }
407 if (state->regs->ROUTER_EN_BIT >= 0)
408 state->router_mode = ((val>>state->regs->ROUTER_EN_BIT) & 1);
409
410 if (state->regs->NUMLAN_GROUPS_BIT >= 0) {
411 state->num_vlans = (val >> state->regs->NUMLAN_GROUPS_BIT);
412 state->num_vlans &= (state->regs->NUMLAN_GROUPS_MAX-1);
413 state->num_vlans+=1; // does not include WAN.
414 }
415
416
417 val = getPhy(state, state->regs->VLAN_CONTROL_REG);
418 if (val < 0) {
419 return 0;
420 }
421 if (state->regs->TAG_VLAN_BIT >= 0)
422 state->vlan_enabled = ((val>>state->regs->TAG_VLAN_BIT) & 1);
423
424 return 0;
425 }
426 /** Get all state variables for VLAN mappings and port-based tagging. **/
427 static int get_state(struct ip175c_state *state)
428 {
429 int i, j;
430 int ret;
431 ret = get_flags(state);
432 if (ret < 0) {
433 return ret;
434 }
435 GET_PORT_BITS(state, state->remove_tag,
436 state->regs->REMOVE_TAG_REG, state->regs->REMOVE_TAG_BIT);
437 GET_PORT_BITS(state, state->add_tag,
438 state->regs->ADD_TAG_REG, state->regs->ADD_TAG_BIT);
439
440 if (state->vlan_enabled == -1) {
441 // not sure how to get this...
442 state->vlan_enabled = (state->remove_tag || state->add_tag);
443 }
444
445 if (REG_SUPP(state->regs->VLAN_LOOKUP_REG)) {
446 for (j=0; j<MAX_PORTS; j++) {
447 state->ports[j].shareports = 0; // initialize them in case.
448 }
449 for (j=0; j<state->regs->NUM_PORTS; j++) {
450 reg addr;
451 const bitnum *bit_lookup = (j%2==0)?
452 state->regs->VLAN_LOOKUP_EVEN_BIT:
453 state->regs->VLAN_LOOKUP_ODD_BIT;
454 addr = state->regs->VLAN_LOOKUP_REG;
455 if (state->regs->SIMPLE_VLAN_REGISTERS) {
456 addr.m += j;
457 } else {
458 switch (j) {
459 case 0:
460 case 1:
461 break;
462 case 2:
463 case 3:
464 addr.m+=1;
465 break;
466 case 4:
467 addr.m+=2;
468 break;
469 case 5:
470 addr = state->regs->VLAN_LOOKUP_REG_5;
471 break;
472 }
473 }
474
475 if (REG_SUPP(addr)) {
476 GET_PORT_BITS(state, state->ports[j].shareports, addr, bit_lookup);
477 }
478 }
479 } else {
480 for (j=0; j<MAX_PORTS; j++) {
481 state->ports[j].shareports = 0xff;
482 }
483 }
484
485 for (i=0; i<MAX_PORTS; i++) {
486 if (REG_SUPP(state->regs->VLAN_DEFAULT_TAG_REG[i])) {
487 int val = getPhy(state, state->regs->VLAN_DEFAULT_TAG_REG[i]);
488 if (val < 0) {
489 return val;
490 }
491 state->ports[i].pvid = val;
492 } else {
493 state->ports[i].pvid = 0;
494 }
495 }
496
497 if (REG_SUPP(state->regs->TAG_VLAN_MASK_REG)) {
498 for (j=0; j<MAX_VLANS; j++) {
499 reg addr = state->regs->TAG_VLAN_MASK_REG;
500 const bitnum *bit_lookup = (j%2==0)?
501 state->regs->TAG_VLAN_MASK_EVEN_BIT:
502 state->regs->TAG_VLAN_MASK_ODD_BIT;
503 if (state->regs->SIMPLE_VLAN_REGISTERS) {
504 addr.m += j;
505 } else {
506 addr.m += j/2;
507 }
508 GET_PORT_BITS(state, state->vlan_ports[j], addr, bit_lookup);
509 }
510 } else {
511 for (j=0; j<MAX_VLANS; j++) {
512 state->vlan_ports[j] = 0;
513 for (i=0; i<state->regs->NUM_PORTS; i++) {
514 if ((state->ports[i].pvid == j) ||
515 (state->ports[i].pvid == 0)) {
516 state->vlan_ports[j] |= (1<<i);
517 }
518 }
519 }
520 }
521
522 return 0;
523 }
524
525
526 /** Only update vlan and router flags in the switch **/
527 static int update_flags(struct ip175c_state *state)
528 {
529 int val;
530
531 if (!REG_SUPP(state->regs->ROUTER_CONTROL_REG)) {
532 return 0;
533 }
534
535 val = getPhy(state, state->regs->ROUTER_CONTROL_REG);
536 if (val < 0) {
537 return val;
538 }
539 if (state->regs->ROUTER_EN_BIT >= 0) {
540 if (state->router_mode) {
541 val |= (1<<state->regs->ROUTER_EN_BIT);
542 } else {
543 val &= (~(1<<state->regs->ROUTER_EN_BIT));
544 }
545 }
546 if (state->regs->TAG_VLAN_BIT >= 0) {
547 if (state->vlan_enabled) {
548 val |= (1<<state->regs->TAG_VLAN_BIT);
549 } else {
550 val &= (~(1<<state->regs->TAG_VLAN_BIT));
551 }
552 }
553 if (state->regs->NUMLAN_GROUPS_BIT >= 0) {
554 val &= (~((state->regs->NUMLAN_GROUPS_MAX-1)<<state->regs->NUMLAN_GROUPS_BIT));
555 if (state->num_vlans > state->regs->NUMLAN_GROUPS_MAX) {
556 val |= state->regs->NUMLAN_GROUPS_MAX << state->regs->NUMLAN_GROUPS_BIT;
557 } else if (state->num_vlans >= 1) {
558 val |= (state->num_vlans-1) << state->regs->NUMLAN_GROUPS_BIT;
559 }
560 }
561 return setPhy(state, state->regs->ROUTER_CONTROL_REG, val);
562 }
563
564 /** Update all VLAN and port state. Usually you should call "correct_vlan_state" first. **/
565 static int update_state(struct ip175c_state *state)
566 {
567 int j;
568 int i;
569 SET_PORT_BITS(state, state->add_tag,
570 state->regs->ADD_TAG_REG, state->regs->ADD_TAG_BIT);
571 SET_PORT_BITS(state, state->remove_tag,
572 state->regs->REMOVE_TAG_REG, state->regs->REMOVE_TAG_BIT);
573
574 if (REG_SUPP(state->regs->VLAN_LOOKUP_REG)) {
575 for (j=0; j<state->regs->NUM_PORTS; j++) {
576 reg addr;
577 const bitnum *bit_lookup = (j%2==0)?
578 state->regs->VLAN_LOOKUP_EVEN_BIT:
579 state->regs->VLAN_LOOKUP_ODD_BIT;
580
581 // duplicate code -- sorry
582 addr = state->regs->VLAN_LOOKUP_REG;
583 if (state->regs->SIMPLE_VLAN_REGISTERS) {
584 addr.m += j;
585 } else {
586 switch (j) {
587 case 0:
588 case 1:
589 break;
590 case 2:
591 case 3:
592 addr.m+=1;
593 break;
594 case 4:
595 addr.m+=2;
596 break;
597 case 5:
598 addr = state->regs->VLAN_LOOKUP_REG_5;
599 break;
600 default:
601 addr.m = -1; // shouldn't get here, but...
602 break;
603 }
604 }
605 //printf("shareports for %d is %02X\n",j,state->ports[j].shareports);
606 if (REG_SUPP(addr)) {
607 SET_PORT_BITS(state, state->ports[j].shareports, addr, bit_lookup);
608 }
609 }
610 }
611 if (REG_SUPP(state->regs->TAG_VLAN_MASK_REG)) {
612 for (j=0; j<MAX_VLANS; j++) {
613 reg addr = state->regs->TAG_VLAN_MASK_REG;
614 const bitnum *bit_lookup = (j%2==0)?
615 state->regs->TAG_VLAN_MASK_EVEN_BIT:
616 state->regs->TAG_VLAN_MASK_ODD_BIT;
617 unsigned int vlan_mask;
618 if (state->regs->SIMPLE_VLAN_REGISTERS) {
619 addr.m += j;
620 } else {
621 addr.m += j/2;
622 }
623 vlan_mask = state->vlan_ports[j];
624 SET_PORT_BITS(state, vlan_mask, addr, bit_lookup);
625 }
626 }
627
628 for (i=0; i<MAX_PORTS; i++) {
629 if (REG_SUPP(state->regs->VLAN_DEFAULT_TAG_REG[i])) {
630 int err = setPhy(state, state->regs->VLAN_DEFAULT_TAG_REG[i],
631 state->ports[i].pvid);
632 if (err < 0) {
633 return err;
634 }
635 }
636 }
637
638 return update_flags(state);
639
640 // software reset: 30.0 = 0x175C
641 // wait 2ms
642 // reset ports 0,1,2,3,4
643 }
644
645 /*
646 Uses only the VLAN port mask and the add tag mask to generate the other fields:
647 which ports are part of the same VLAN, removing vlan tags, and VLAN tag ids.
648 */
649 static void correct_vlan_state(struct ip175c_state *state)
650 {
651 int i, j;
652 state->num_vlans = 0;
653 for (i=0; i<MAX_VLANS; i++) {
654 if (state->vlan_ports[i] != 0) {
655 state->num_vlans = i+1; //hack -- we need to store the "set" vlans somewhere...
656 }
657 }
658
659
660
661 for (i=0; i<state->regs->NUM_PORTS; i++) {
662 unsigned int portmask = (1<<i);
663 if (!state->vlan_enabled) {
664 // share with everybody!
665 state->ports[i].shareports = (1<<state->regs->NUM_PORTS)-1;
666 continue;
667 }
668 state->ports[i].shareports = portmask;
669 for (j=0; j<MAX_VLANS; j++) {
670 if (state->vlan_ports[j] & portmask)
671 state->ports[i].shareports |= state->vlan_ports[j];
672 }
673 }
674 state->remove_tag = ((~state->add_tag) & ((1<<state->regs->NUM_PORTS)-1));
675 }
676
677 static int ip175c_get_enable_vlan(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
678 {
679 struct ip175c_state *state = dev->priv;
680 int err;
681
682 err = get_state(state); // may be set in get_state.
683 if (err < 0)
684 return err;
685 val->value.i = state->vlan_enabled;
686 return 0;
687 }
688
689 static int ip175c_set_enable_vlan(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
690 {
691 struct ip175c_state *state = dev->priv;
692 int err;
693 int enable;
694 int i;
695
696 err = get_state(state);
697 if (err < 0)
698 return err;
699 enable = val->value.i;
700
701 if (state->vlan_enabled == enable) {
702 // do not change any state.
703 return 0;
704 }
705 state->vlan_enabled = enable;
706
707 // Otherwise, if we are switching state, set fields to a known default.
708 state->remove_tag = 0x0000;
709 state->add_tag = 0x0000;
710 for (i = 0; i < MAX_PORTS; i++)
711 state->ports[i].shareports = 0xffff;
712
713 for (i = 0; i < MAX_VLANS; i++)
714 state->vlan_ports[i] = 0x0;
715
716 if (state->vlan_enabled) {
717 // updates other fields only based off vlan_ports and add_tag fields.
718 // Note that by default, no ports are in any vlans.
719 correct_vlan_state(state);
720 }
721 // ensure sane defaults?
722 return update_state(state);
723 }
724
725 static int ip175c_get_ports(struct switch_dev *dev, struct switch_val *val)
726 {
727 struct ip175c_state *state = dev->priv;
728 int err;
729 int b;
730 int ind;
731 unsigned int ports;
732
733 if (val->port_vlan >= dev->vlans || val->port_vlan < 0)
734 return -EINVAL;
735
736 err = get_state(state);
737 if (err<0)
738 return err;
739
740 ports = state->vlan_ports[val->port_vlan];
741 b = 0;
742 ind = 0;
743 while (b < MAX_PORTS) {
744 if (ports&1) {
745 int istagged = ((state->add_tag >> b) & 1);
746 val->value.ports[ind].id = b;
747 val->value.ports[ind].flags = (istagged << SWITCH_PORT_FLAG_TAGGED);
748 ind++;
749 }
750 b++;
751 ports >>= 1;
752 }
753 val->len = ind;
754
755 return 0;
756 }
757
758 static int ip175c_set_ports(struct switch_dev *dev, struct switch_val *val)
759 {
760 struct ip175c_state *state = dev->priv;
761 int i;
762 int err;
763
764 if (val->port_vlan >= dev->vlans || val->port_vlan < 0)
765 return -EINVAL;
766
767 err = get_state(state);
768 if (err < 0)
769 return err;
770
771 state->vlan_ports[val->port_vlan] = 0;
772 for (i = 0; i < val->len; i++) {
773 int bitmask = (1<<val->value.ports[i].id);
774 state->vlan_ports[val->port_vlan] |= bitmask;
775 if (val->value.ports[i].flags & (1<<SWITCH_PORT_FLAG_TAGGED)) {
776 state->add_tag |= bitmask;
777 } else {
778 state->add_tag &= (~bitmask);
779 }
780 }
781
782 correct_vlan_state(state);
783 err = update_state(state);
784
785 return err;
786 }
787
788 static int ip175c_apply(struct switch_dev *dev)
789 {
790 struct ip175c_state *state = dev->priv;
791 int err;
792
793 err = get_flags(state);
794 if (err < 0)
795 return err;
796
797 if (REG_SUPP(state->regs->MII_REGISTER_EN)){
798 int val = getPhy(state, state->regs->MII_REGISTER_EN);
799 if (val < 0) {
800 return val;
801 }
802 val |= (1<<state->regs->MII_REGISTER_EN_BIT);
803 return setPhy(state, state->regs->MII_REGISTER_EN, val);
804 }
805 return 0;
806 }
807
808 static int ip175c_reset(struct switch_dev *dev)
809 {
810 struct ip175c_state *state = dev->priv;
811 int i, err;
812
813 err = get_flags(state);
814 if (err < 0)
815 return err;
816
817 if (REG_SUPP(state->regs->RESET_REG)) {
818 err = setPhy(state, state->regs->RESET_REG, state->regs->RESET_VAL);
819 if (err < 0)
820 return err;
821 err = getPhy(state, state->regs->RESET_REG);
822
823 /* data sheet specifies reset period is 2 msec
824 (don't see any mention of the 2ms delay in the IP178C spec, only
825 in IP175C, but it can't hurt.) */
826 mdelay(2);
827 }
828
829 if (REG_SUPP(state->regs->MODE_REG)) {
830 err = setPhy(state, state->regs->MODE_REG, state->regs->RESET_VAL);
831 if (err < 0)
832 return err;
833 err = getPhy(state, state->regs->MODE_REG);
834 }
835
836 if (REG_SUPP(state->regs->RESET_REG)) {
837 /* reset external phy ports, except on IP175A */
838 for (i = 0; i < state->regs->NUM_PORTS-1; i++) {
839 err = state->mii_bus->write(state->mii_bus, i,
840 MII_BMCR, BMCR_RESET);
841 if (err < 0)
842 return err;
843 }
844 }
845
846 return 0;
847 }
848
849 /*! get the current register number */
850 static int ip175c_get_tagged(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
851 {
852 struct ip175c_state *state = dev->priv;
853 int err;
854
855 err = get_state(state);
856 if (err < 0)
857 return err;
858
859 if (state->add_tag & (1<<val->port_vlan)) {
860 if (state->remove_tag & (1<<val->port_vlan))
861 val->value.i = 3; // shouldn't ever happen.
862 else
863 val->value.i = 1;
864 } else {
865 if (state->remove_tag & (1<<val->port_vlan))
866 val->value.i = 0;
867 else
868 val->value.i = 2;
869 }
870 return 0;
871 }
872
873 /*! set a new register address for low level access to registers */
874 static int ip175c_set_tagged(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
875 {
876 struct ip175c_state *state = dev->priv;
877 int err;
878
879 err = get_state(state);
880 if (err < 0)
881 return err;
882
883 state->add_tag &= ~(1<<val->port_vlan);
884 state->remove_tag &= ~(1<<val->port_vlan);
885
886 if (val->value.i == 0)
887 state->remove_tag |= (1<<val->port_vlan);
888 if (val->value.i == 1)
889 state->add_tag |= (1<<val->port_vlan);
890
891 SET_PORT_BITS(state, state->add_tag,
892 state->regs->ADD_TAG_REG, state->regs->ADD_TAG_BIT);
893 SET_PORT_BITS(state, state->remove_tag,
894 state->regs->REMOVE_TAG_REG, state->regs->REMOVE_TAG_BIT);
895
896 return err;
897 }
898
899
900 /* low level /proc procedures */
901
902 /*! get the current phy address */
903 static int ip175c_get_phy(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
904 {
905 struct ip175c_state *state = dev->priv;
906
907 val->value.i = state->proc_mii.p;
908 return 0;
909 }
910
911 /*! set a new phy address for low level access to registers */
912 static int ip175c_set_phy(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
913 {
914 struct ip175c_state *state = dev->priv;
915 int new_reg = val->value.i;
916
917 if (new_reg < 0 || new_reg > 31)
918 state->proc_mii.p = (u16)-1;
919 else
920 state->proc_mii.p = (u16)new_reg;
921 return 0;
922 }
923
924 /*! get the current register number */
925 static int ip175c_get_reg(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
926 {
927 struct ip175c_state *state = dev->priv;
928
929 val->value.i = state->proc_mii.m;
930 return 0;
931 }
932
933 /*! set a new register address for low level access to registers */
934 static int ip175c_set_reg(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
935 {
936 struct ip175c_state *state = dev->priv;
937 int new_reg = val->value.i;
938
939 if (new_reg < 0 || new_reg > 31)
940 state->proc_mii.m = (u16)-1;
941 else
942 state->proc_mii.m = (u16)new_reg;
943 return 0;
944 }
945
946 /*! get the register content of state->proc_mii */
947 static int ip175c_get_val(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
948 {
949 struct ip175c_state *state = dev->priv;
950 int retval = -EINVAL;
951 if (REG_SUPP(state->proc_mii))
952 retval = getPhy(state, state->proc_mii);
953
954 if (retval < 0) {
955 return retval;
956 } else {
957 val->value.i = retval;
958 return 0;
959 }
960 }
961
962 /*! write a value to the register defined by phy/reg above */
963 static int ip175c_set_val(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
964 {
965 struct ip175c_state *state = dev->priv;
966 int myval, err = 0;
967
968 myval = val->value.i;
969 if (myval <= 0xffff && myval >= 0 && REG_SUPP(state->proc_mii)) {
970 err = setPhy(state, state->proc_mii, (u16)myval);
971 }
972 return err;
973 }
974
975 static int ip175c_read_name(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
976 {
977 struct ip175c_state *state = dev->priv;
978 val->value.s = state->regs->NAME; // just a const pointer, won't be freed by swconfig.
979 return 0;
980 }
981
982
983 static int ip175c_set_port_speed(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
984 {
985 struct ip175c_state *state = dev->priv;
986 struct mii_bus *bus = state->mii_bus;
987 int nr = val->port_vlan;
988 int ctrl;
989 int autoneg;
990 int speed;
991 if (val->value.i == 100) {
992 speed = 1;
993 autoneg = 0;
994 } else if (val->value.i == 10) {
995 speed = 0;
996 autoneg = 0;
997 } else {
998 autoneg = 1;
999 speed = 1;
1000 }
1001
1002 /* can't set speed for cpu port */
1003 if (nr == state->regs->CPU_PORT)
1004 return -EINVAL;
1005
1006 if (nr >= dev->ports || nr < 0)
1007 return -EINVAL;
1008
1009 ctrl = ip_phy_read(bus, nr, 0);
1010 if (ctrl < 0)
1011 return -EIO;
1012
1013 ctrl &= (~(1<<12));
1014 ctrl &= (~(1<<13));
1015 ctrl |= (autoneg<<12);
1016 ctrl |= (speed<<13);
1017
1018 return ip_phy_write(bus, nr, 0, ctrl);
1019 }
1020
1021 static int ip175c_get_port_speed(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
1022 {
1023 struct ip175c_state *state = dev->priv;
1024 struct mii_bus *bus = state->mii_bus;
1025 int nr = val->port_vlan;
1026 int speed, status;
1027
1028 if (nr == state->regs->CPU_PORT) {
1029 val->value.i = 100;
1030 return 0;
1031 }
1032
1033 if (nr >= dev->ports || nr < 0)
1034 return -EINVAL;
1035
1036 status = ip_phy_read(bus, nr, 1);
1037 speed = ip_phy_read(bus, nr, 18);
1038 if (status < 0 || speed < 0)
1039 return -EIO;
1040
1041 if (status & 4)
1042 val->value.i = ((speed & (1<<11)) ? 100 : 10);
1043 else
1044 val->value.i = 0;
1045
1046 return 0;
1047 }
1048
1049
1050 static int ip175c_get_port_status(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
1051 {
1052 struct ip175c_state *state = dev->priv;
1053 struct mii_bus *bus = state->mii_bus;
1054 int ctrl, speed, status;
1055 int nr = val->port_vlan;
1056 int len;
1057 char *buf = state->buf; // fixed-length at 80.
1058
1059 if (nr == state->regs->CPU_PORT) {
1060 sprintf(buf, "up, 100 Mbps, cpu port");
1061 val->value.s = buf;
1062 return 0;
1063 }
1064
1065 if (nr >= dev->ports || nr < 0)
1066 return -EINVAL;
1067
1068 ctrl = ip_phy_read(bus, nr, 0);
1069 status = ip_phy_read(bus, nr, 1);
1070 speed = ip_phy_read(bus, nr, 18);
1071 if (ctrl < 0 || status < 0 || speed < 0)
1072 return -EIO;
1073
1074 if (status & 4)
1075 len = sprintf(buf, "up, %d Mbps, %s duplex",
1076 ((speed & (1<<11)) ? 100 : 10),
1077 ((speed & (1<<10)) ? "full" : "half"));
1078 else
1079 len = sprintf(buf, "down");
1080
1081 if (ctrl & (1<<12)) {
1082 len += sprintf(buf+len, ", auto-negotiate");
1083 if (!(status & (1<<5)))
1084 len += sprintf(buf+len, " (in progress)");
1085 } else {
1086 len += sprintf(buf+len, ", fixed speed (%d)",
1087 ((ctrl & (1<<13)) ? 100 : 10));
1088 }
1089
1090 buf[len] = '\0';
1091 val->value.s = buf;
1092 return 0;
1093 }
1094
1095 static int ip175c_get_pvid(struct switch_dev *dev, int port, int *val)
1096 {
1097 struct ip175c_state *state = dev->priv;
1098
1099 *val = state->ports[port].pvid;
1100 return 0;
1101 }
1102
1103 static int ip175c_set_pvid(struct switch_dev *dev, int port, int val)
1104 {
1105 struct ip175c_state *state = dev->priv;
1106
1107 state->ports[port].pvid = val;
1108
1109 if (!REG_SUPP(state->regs->VLAN_DEFAULT_TAG_REG[port]))
1110 return 0;
1111
1112 return setPhy(state, state->regs->VLAN_DEFAULT_TAG_REG[port], val);
1113 }
1114
1115
1116 enum Ports {
1117 IP175C_PORT_STATUS,
1118 IP175C_PORT_LINK,
1119 IP175C_PORT_TAGGED,
1120 IP175C_PORT_PVID,
1121 };
1122
1123 enum Globals {
1124 IP175C_ENABLE_VLAN,
1125 IP175C_GET_NAME,
1126 IP175C_REGISTER_PHY,
1127 IP175C_REGISTER_MII,
1128 IP175C_REGISTER_VALUE,
1129 IP175C_REGISTER_ERRNO,
1130 };
1131
1132 static const struct switch_attr ip175c_global[] = {
1133 [IP175C_ENABLE_VLAN] = {
1134 .id = IP175C_ENABLE_VLAN,
1135 .type = SWITCH_TYPE_INT,
1136 .name = "enable_vlan",
1137 .description = "Flag to enable or disable VLANs and tagging",
1138 .get = ip175c_get_enable_vlan,
1139 .set = ip175c_set_enable_vlan,
1140 },
1141 [IP175C_GET_NAME] = {
1142 .id = IP175C_GET_NAME,
1143 .type = SWITCH_TYPE_STRING,
1144 .description = "Returns the type of IC+ chip.",
1145 .name = "name",
1146 .get = ip175c_read_name,
1147 .set = NULL,
1148 },
1149 /* jal: added for low level debugging etc. */
1150 [IP175C_REGISTER_PHY] = {
1151 .id = IP175C_REGISTER_PHY,
1152 .type = SWITCH_TYPE_INT,
1153 .description = "Direct register access: set phy (0-4, or 29,30,31)",
1154 .name = "phy",
1155 .get = ip175c_get_phy,
1156 .set = ip175c_set_phy,
1157 },
1158 [IP175C_REGISTER_MII] = {
1159 .id = IP175C_REGISTER_MII,
1160 .type = SWITCH_TYPE_INT,
1161 .description = "Direct register access: set mii number (0-31)",
1162 .name = "reg",
1163 .get = ip175c_get_reg,
1164 .set = ip175c_set_reg,
1165 },
1166 [IP175C_REGISTER_VALUE] = {
1167 .id = IP175C_REGISTER_VALUE,
1168 .type = SWITCH_TYPE_INT,
1169 .description = "Direct register access: read/write to register (0-65535)",
1170 .name = "val",
1171 .get = ip175c_get_val,
1172 .set = ip175c_set_val,
1173 },
1174 };
1175
1176 static const struct switch_attr ip175c_vlan[] = {
1177 };
1178
1179 static const struct switch_attr ip175c_port[] = {
1180 [IP175C_PORT_STATUS] = {
1181 .id = IP175C_PORT_STATUS,
1182 .type = SWITCH_TYPE_STRING,
1183 .description = "Returns Detailed port status",
1184 .name = "status",
1185 .get = ip175c_get_port_status,
1186 .set = NULL,
1187 },
1188 [IP175C_PORT_LINK] = {
1189 .id = IP175C_PORT_LINK,
1190 .type = SWITCH_TYPE_INT,
1191 .description = "Link speed. Can write 0 for auto-negotiate, or 10 or 100",
1192 .name = "link",
1193 .get = ip175c_get_port_speed,
1194 .set = ip175c_set_port_speed,
1195 },
1196 [IP175C_PORT_TAGGED] = {
1197 .id = IP175C_PORT_LINK,
1198 .type = SWITCH_TYPE_INT,
1199 .description = "0 = untag, 1 = add tags, 2 = do not alter (This value is reset if vlans are altered)",
1200 .name = "tagged",
1201 .get = ip175c_get_tagged,
1202 .set = ip175c_set_tagged,
1203 },
1204 };
1205
1206 static int ip175c_probe(struct phy_device *pdev)
1207 {
1208 struct ip175c_state *state;
1209 struct switch_dev *dev;
1210 int err;
1211
1212 /* we only attach to PHY 0, but use all available PHYs */
1213 if (pdev->addr != 0)
1214 return -ENODEV;
1215
1216 state = kzalloc(sizeof(*state), GFP_KERNEL);
1217 if (!state)
1218 return -ENOMEM;
1219
1220 dev = &state->dev;
1221 dev->attr_global.attr = ip175c_global;
1222 dev->attr_global.n_attr = ARRAY_SIZE(ip175c_global);
1223 dev->attr_port.attr = ip175c_port;
1224 dev->attr_port.n_attr = ARRAY_SIZE(ip175c_port);
1225 dev->attr_vlan.attr = ip175c_vlan;
1226 dev->attr_vlan.n_attr = ARRAY_SIZE(ip175c_vlan);
1227
1228 dev->get_port_pvid = ip175c_get_pvid;
1229 dev->set_port_pvid = ip175c_set_pvid;
1230 dev->get_vlan_ports = ip175c_get_ports;
1231 dev->set_vlan_ports = ip175c_set_ports;
1232 dev->apply_config = ip175c_apply;
1233 dev->reset_switch = ip175c_reset;
1234
1235 dev->priv = state;
1236 pdev->priv = state;
1237 state->mii_bus = pdev->bus;
1238
1239 err = get_model(state);
1240 if (err < 0)
1241 goto error;
1242
1243 dev->vlans = MAX_VLANS;
1244 dev->cpu_port = state->regs->CPU_PORT;
1245 dev->ports = state->regs->NUM_PORTS;
1246 dev->name = state->regs->NAME;
1247
1248 return 0;
1249
1250 error:
1251 kfree(state);
1252 return err;
1253 }
1254
1255 static int ip175c_config_init(struct phy_device *pdev)
1256 {
1257 struct ip175c_state *state = pdev->priv;
1258 struct net_device *dev = pdev->attached_dev;
1259 int err;
1260
1261 pdev->irq = PHY_IGNORE_INTERRUPT;
1262 err = register_switch(&state->dev, dev);
1263 if (err < 0)
1264 return err;
1265
1266 ip175c_reset(&state->dev);
1267
1268 state->registered = true;
1269 netif_carrier_on(pdev->attached_dev);
1270
1271 return 0;
1272 }
1273
1274 static void ip175c_remove(struct phy_device *pdev)
1275 {
1276 struct ip175c_state *state = pdev->priv;
1277
1278 if (state->registered)
1279 unregister_switch(&state->dev);
1280 kfree(state);
1281 }
1282
1283 static int ip175c_config_aneg(struct phy_device *pdev)
1284 {
1285 return 0;
1286 }
1287
1288 static int ip175c_read_status(struct phy_device *pdev)
1289 {
1290 pdev->speed = SPEED_100;
1291 pdev->duplex = DUPLEX_FULL;
1292 pdev->pause = pdev->asym_pause = 0;
1293 return 0;
1294 }
1295
1296 static struct phy_driver ip175c_driver = {
1297 .name = "IC+ IP175C",
1298 .phy_id = 0x02430d80,
1299 .phy_id_mask = 0x0ffffff0,
1300 .features = PHY_BASIC_FEATURES,
1301 .probe = ip175c_probe,
1302 .remove = ip175c_remove,
1303 .config_init = ip175c_config_init,
1304 .config_aneg = ip175c_config_aneg,
1305 .read_status = ip175c_read_status,
1306 .driver = { .owner = THIS_MODULE },
1307 };
1308
1309 static struct phy_driver ip175a_driver = {
1310 .name = "IC+ IP175A",
1311 .phy_id = 0x02430c50,
1312 .phy_id_mask = 0x0ffffff0,
1313 .features = PHY_BASIC_FEATURES,
1314 .probe = ip175c_probe,
1315 .remove = ip175c_remove,
1316 .config_init = ip175c_config_init,
1317 .config_aneg = ip175c_config_aneg,
1318 .read_status = ip175c_read_status,
1319 .driver = { .owner = THIS_MODULE },
1320 };
1321
1322
1323 int __init ip175c_init(void)
1324 {
1325 int ret;
1326
1327 ret = phy_driver_register(&ip175a_driver);
1328 if (ret < 0)
1329 return ret;
1330
1331 return phy_driver_register(&ip175c_driver);
1332 }
1333
1334 void __exit ip175c_exit(void)
1335 {
1336 phy_driver_unregister(&ip175c_driver);
1337 phy_driver_unregister(&ip175a_driver);
1338 }
1339
1340 MODULE_AUTHOR("Patrick Horn <patrick.horn@gmail.com>");
1341 MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>");
1342 MODULE_LICENSE("GPL");
1343
1344 module_init(ip175c_init);
1345 module_exit(ip175c_exit);
1346
This page took 0.14873 seconds and 5 git commands to generate.