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