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