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