kernel: adm6996: add support for ADM6996L and GPIO interface
[openwrt.git] / target / linux / generic / files / drivers / net / phy / adm6996.c
1 /*
2  * ADM6996 switch driver
3  *
4  * swconfig interface based on ar8216.c
5  *
6  * Copyright (c) 2008 Felix Fietkau <nbd@openwrt.org>
7  * VLAN support Copyright (c) 2010, 2011 Peter Lebbing <peter@digitalbrains.com>
8  * Copyright (c) 2013 Hauke Mehrtens <hauke@hauke-m.de>
9  *
10  * This program is free software; you can redistribute  it and/or modify it
11  * under  the terms of the GNU General Public License v2 as published by the
12  * Free Software Foundation
13  */
14
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16
17 /*#define DEBUG 1*/
18 #include <linux/kernel.h>
19 #include <linux/string.h>
20 #include <linux/errno.h>
21 #include <linux/unistd.h>
22 #include <linux/slab.h>
23 #include <linux/interrupt.h>
24 #include <linux/init.h>
25 #include <linux/delay.h>
26 #include <linux/gpio.h>
27 #include <linux/netdevice.h>
28 #include <linux/etherdevice.h>
29 #include <linux/skbuff.h>
30 #include <linux/spinlock.h>
31 #include <linux/mm.h>
32 #include <linux/module.h>
33 #include <linux/mii.h>
34 #include <linux/platform_device.h>
35 #include <linux/platform_data/adm6996-gpio.h>
36 #include <linux/ethtool.h>
37 #include <linux/phy.h>
38 #include <linux/switch.h>
39
40 #include <asm/io.h>
41 #include <asm/irq.h>
42 #include <asm/uaccess.h>
43 #include "adm6996.h"
44
45 MODULE_DESCRIPTION("Infineon ADM6996 Switch");
46 MODULE_AUTHOR("Felix Fietkau, Peter Lebbing <peter@digitalbrains.com>");
47 MODULE_LICENSE("GPL");
48
49 static const char * const adm6996_model_name[] =
50 {
51         NULL,
52         "ADM6996FC",
53         "ADM6996M",
54         "ADM6996L"
55 };
56
57 struct adm6996_priv {
58         struct switch_dev dev;
59         void *priv;
60
61         u8 eecs;
62         u8 eesk;
63         u8 eedi;
64         u8 eerc;
65
66         enum adm6996_model model;
67
68         bool enable_vlan;
69         bool vlan_enabled;      /* Current hardware state */
70
71 #ifdef DEBUG
72         u16 addr;               /* Debugging: register address to operate on */
73 #endif
74
75         u16 pvid[ADM_NUM_PORTS];        /* Primary VLAN ID */
76         u8 tagged_ports;
77
78         u16 vlan_id[ADM_NUM_VLANS];
79         u8 vlan_table[ADM_NUM_VLANS];   /* bitmap, 1 = port is member */
80         u8 vlan_tagged[ADM_NUM_VLANS];  /* bitmap, 1 = tagged member */
81
82         struct mutex reg_mutex;
83
84         /* use abstraction for regops, we want to add gpio support in the future */
85         u16 (*read)(struct adm6996_priv *priv, enum admreg reg);
86         void (*write)(struct adm6996_priv *priv, enum admreg reg, u16 val);
87 };
88
89 #define to_adm(_dev) container_of(_dev, struct adm6996_priv, dev)
90 #define phy_to_adm(_phy) ((struct adm6996_priv *) (_phy)->priv)
91
92 static inline u16
93 r16(struct adm6996_priv *priv, enum admreg reg)
94 {
95         return priv->read(priv, reg);
96 }
97
98 static inline void
99 w16(struct adm6996_priv *priv, enum admreg reg, u16 val)
100 {
101         priv->write(priv, reg, val);
102 }
103
104 /* Minimum timing constants */
105 #define EECK_EDGE_TIME  3   /* 3us - max(adm 2.5us, 93c 1us) */
106 #define EEDI_SETUP_TIME 1   /* 1us - max(adm 10ns, 93c 400ns) */
107 #define EECS_SETUP_TIME 1   /* 1us - max(adm no, 93c 200ns) */
108
109 static void adm6996_gpio_write(struct adm6996_priv *priv, int cs, char *buf, unsigned int bits)
110 {
111         int i, len = (bits + 7) / 8;
112         u8 mask;
113
114         gpio_set_value(priv->eecs, cs);
115         udelay(EECK_EDGE_TIME);
116
117         /* Byte assemble from MSB to LSB */
118         for (i = 0; i < len; i++) {
119                 /* Bit bang from MSB to LSB */
120                 for (mask = 0x80; mask && bits > 0; mask >>= 1, bits --) {
121                         /* Clock low */
122                         gpio_set_value(priv->eesk, 0);
123                         udelay(EECK_EDGE_TIME);
124
125                         /* Output on rising edge */
126                         gpio_set_value(priv->eedi, (mask & buf[i]));
127                         udelay(EEDI_SETUP_TIME);
128
129                         /* Clock high */
130                         gpio_set_value(priv->eesk, 1);
131                         udelay(EECK_EDGE_TIME);
132                 }
133         }
134
135         /* Clock low */
136         gpio_set_value(priv->eesk, 0);
137         udelay(EECK_EDGE_TIME);
138
139         if (cs)
140                 gpio_set_value(priv->eecs, 0);
141 }
142
143 static void adm6996_gpio_read(struct adm6996_priv *priv, int cs, char *buf, unsigned int bits)
144 {
145         int i, len = (bits + 7) / 8;
146         u8 mask;
147
148         gpio_set_value(priv->eecs, cs);
149         udelay(EECK_EDGE_TIME);
150
151         /* Byte assemble from MSB to LSB */
152         for (i = 0; i < len; i++) {
153                 u8 byte;
154
155                 /* Bit bang from MSB to LSB */
156                 for (mask = 0x80, byte = 0; mask && bits > 0; mask >>= 1, bits --) {
157                         u8 gp;
158
159                         /* Clock low */
160                         gpio_set_value(priv->eesk, 0);
161                         udelay(EECK_EDGE_TIME);
162
163                         /* Input on rising edge */
164                         gp = gpio_get_value(priv->eedi);
165                         if (gp)
166                                 byte |= mask;
167
168                         /* Clock high */
169                         gpio_set_value(priv->eesk, 1);
170                         udelay(EECK_EDGE_TIME);
171                 }
172
173                 *buf++ = byte;
174         }
175
176         /* Clock low */
177         gpio_set_value(priv->eesk, 0);
178         udelay(EECK_EDGE_TIME);
179
180         if (cs)
181                 gpio_set_value(priv->eecs, 0);
182 }
183
184 /* Advance clock(s) */
185 static void adm6996_gpio_adclk(struct adm6996_priv *priv, int clocks)
186 {
187         int i;
188         for (i = 0; i < clocks; i++) {
189                 /* Clock high */
190                 gpio_set_value(priv->eesk, 1);
191                 udelay(EECK_EDGE_TIME);
192
193                 /* Clock low */
194                 gpio_set_value(priv->eesk, 0);
195                 udelay(EECK_EDGE_TIME);
196         }
197 }
198
199 static u16
200 adm6996_read_gpio_reg(struct adm6996_priv *priv, enum admreg reg)
201 {
202         /* cmd: 01 10 T DD R RRRRRR */
203         u8 bits[6] = {
204                 0xFF, 0xFF, 0xFF, 0xFF,
205                 (0x06 << 4) | ((0 & 0x01) << 3 | (reg&64)>>6),
206                 ((reg&63)<<2)
207         };
208
209         u8 rbits[4];
210
211         /* Enable GPIO outputs with all pins to 0 */
212         gpio_direction_output(priv->eecs, 0);
213         gpio_direction_output(priv->eesk, 0);
214         gpio_direction_output(priv->eedi, 0);
215
216         adm6996_gpio_write(priv, 0, bits, 46);
217         gpio_direction_input(priv->eedi);
218         adm6996_gpio_adclk(priv, 2);
219         adm6996_gpio_read(priv, 0, rbits, 32);
220
221         /* Extra clock(s) required per datasheet */
222         adm6996_gpio_adclk(priv, 2);
223
224         /* Disable GPIO outputs */
225         gpio_direction_input(priv->eecs);
226         gpio_direction_input(priv->eesk);
227
228          /* EEPROM has 16-bit registers, but pumps out two registers in one request */
229         return (reg & 0x01 ?  (rbits[0]<<8) | rbits[1] : (rbits[2]<<8) | (rbits[3]));
230 }
231
232 /* Write chip configuration register */
233 /* Follow 93c66 timing and chip's min EEPROM timing requirement */
234 static void
235 adm6996_write_gpio_reg(struct adm6996_priv *priv, enum admreg reg, u16 val)
236 {
237         /* cmd(27bits): sb(1) + opc(01) + addr(bbbbbbbb) + data(bbbbbbbbbbbbbbbb) */
238         u8 bits[4] = {
239                 (0x05 << 5) | (reg >> 3),
240                 (reg << 5) | (u8)(val >> 11),
241                 (u8)(val >> 3),
242                 (u8)(val << 5)
243         };
244
245         /* Enable GPIO outputs with all pins to 0 */
246         gpio_direction_output(priv->eecs, 0);
247         gpio_direction_output(priv->eesk, 0);
248         gpio_direction_output(priv->eedi, 0);
249
250         /* Write cmd. Total 27 bits */
251         adm6996_gpio_write(priv, 1, bits, 27);
252
253         /* Extra clock(s) required per datasheet */
254         adm6996_gpio_adclk(priv, 2);
255
256         /* Disable GPIO outputs */
257         gpio_direction_input(priv->eecs);
258         gpio_direction_input(priv->eesk);
259         gpio_direction_input(priv->eedi);
260 }
261
262 static u16
263 adm6996_read_mii_reg(struct adm6996_priv *priv, enum admreg reg)
264 {
265         struct phy_device *phydev = priv->priv;
266         struct mii_bus *bus = phydev->bus;
267
268         return bus->read(bus, PHYADDR(reg));
269 }
270
271 static void
272 adm6996_write_mii_reg(struct adm6996_priv *priv, enum admreg reg, u16 val)
273 {
274         struct phy_device *phydev = priv->priv;
275         struct mii_bus *bus = phydev->bus;
276
277         bus->write(bus, PHYADDR(reg), val);
278 }
279
280 static int
281 adm6996_set_enable_vlan(struct switch_dev *dev, const struct switch_attr *attr,
282                         struct switch_val *val)
283 {
284         struct adm6996_priv *priv = to_adm(dev);
285
286         if (val->value.i > 1)
287                 return -EINVAL;
288
289         priv->enable_vlan = val->value.i;
290
291         return 0;
292 };
293
294 static int
295 adm6996_get_enable_vlan(struct switch_dev *dev, const struct switch_attr *attr,
296                         struct switch_val *val)
297 {
298         struct adm6996_priv *priv = to_adm(dev);
299
300         val->value.i = priv->enable_vlan;
301
302         return 0;
303 };
304
305 #ifdef DEBUG
306
307 static int
308 adm6996_set_addr(struct switch_dev *dev, const struct switch_attr *attr,
309                  struct switch_val *val)
310 {
311         struct adm6996_priv *priv = to_adm(dev);
312
313         if (val->value.i > 1023)
314                 return -EINVAL;
315
316         priv->addr = val->value.i;
317
318         return 0;
319 };
320
321 static int
322 adm6996_get_addr(struct switch_dev *dev, const struct switch_attr *attr,
323                  struct switch_val *val)
324 {
325         struct adm6996_priv *priv = to_adm(dev);
326
327         val->value.i = priv->addr;
328
329         return 0;
330 };
331
332 static int
333 adm6996_set_data(struct switch_dev *dev, const struct switch_attr *attr,
334                  struct switch_val *val)
335 {
336         struct adm6996_priv *priv = to_adm(dev);
337
338         if (val->value.i > 65535)
339                 return -EINVAL;
340
341         w16(priv, priv->addr, val->value.i);
342
343         return 0;
344 };
345
346 static int
347 adm6996_get_data(struct switch_dev *dev, const struct switch_attr *attr,
348                  struct switch_val *val)
349 {
350         struct adm6996_priv *priv = to_adm(dev);
351
352         val->value.i = r16(priv, priv->addr);
353
354         return 0;
355 };
356
357 #endif /* def DEBUG */
358
359 static int
360 adm6996_set_pvid(struct switch_dev *dev, int port, int vlan)
361 {
362         struct adm6996_priv *priv = to_adm(dev);
363
364         pr_devel("set_pvid port %d vlan %d\n", port, vlan);
365
366         if (vlan > ADM_VLAN_MAX_ID)
367                 return -EINVAL;
368
369         priv->pvid[port] = vlan;
370
371         return 0;
372 }
373
374 static int
375 adm6996_get_pvid(struct switch_dev *dev, int port, int *vlan)
376 {
377         struct adm6996_priv *priv = to_adm(dev);
378
379         pr_devel("get_pvid port %d\n", port);
380         *vlan = priv->pvid[port];
381
382         return 0;
383 }
384
385 static int
386 adm6996_set_vid(struct switch_dev *dev, const struct switch_attr *attr,
387                 struct switch_val *val)
388 {
389         struct adm6996_priv *priv = to_adm(dev);
390
391         pr_devel("set_vid port %d vid %d\n", val->port_vlan, val->value.i);
392
393         if (val->value.i > ADM_VLAN_MAX_ID)
394                 return -EINVAL;
395
396         priv->vlan_id[val->port_vlan] = val->value.i;
397
398         return 0;
399 };
400
401 static int
402 adm6996_get_vid(struct switch_dev *dev, const struct switch_attr *attr,
403                 struct switch_val *val)
404 {
405         struct adm6996_priv *priv = to_adm(dev);
406
407         pr_devel("get_vid port %d\n", val->port_vlan);
408
409         val->value.i = priv->vlan_id[val->port_vlan];
410
411         return 0;
412 };
413
414 static int
415 adm6996_get_ports(struct switch_dev *dev, struct switch_val *val)
416 {
417         struct adm6996_priv *priv = to_adm(dev);
418         u8 ports = priv->vlan_table[val->port_vlan];
419         u8 tagged = priv->vlan_tagged[val->port_vlan];
420         int i;
421
422         pr_devel("get_ports port_vlan %d\n", val->port_vlan);
423
424         val->len = 0;
425
426         for (i = 0; i < ADM_NUM_PORTS; i++) {
427                 struct switch_port *p;
428
429                 if (!(ports & (1 << i)))
430                         continue;
431
432                 p = &val->value.ports[val->len++];
433                 p->id = i;
434                 if (tagged & (1 << i))
435                         p->flags = (1 << SWITCH_PORT_FLAG_TAGGED);
436                 else
437                         p->flags = 0;
438         }
439
440         return 0;
441 };
442
443 static int
444 adm6996_set_ports(struct switch_dev *dev, struct switch_val *val)
445 {
446         struct adm6996_priv *priv = to_adm(dev);
447         u8 *ports = &priv->vlan_table[val->port_vlan];
448         u8 *tagged = &priv->vlan_tagged[val->port_vlan];
449         int i;
450
451         pr_devel("set_ports port_vlan %d ports", val->port_vlan);
452
453         *ports = 0;
454         *tagged = 0;
455
456         for (i = 0; i < val->len; i++) {
457                 struct switch_port *p = &val->value.ports[i];
458
459 #ifdef DEBUG
460                 pr_cont(" %d%s", p->id,
461                        ((p->flags & (1 << SWITCH_PORT_FLAG_TAGGED)) ? "T" :
462                         ""));
463 #endif
464
465                 if (p->flags & (1 << SWITCH_PORT_FLAG_TAGGED)) {
466                         *tagged |= (1 << p->id);
467                         priv->tagged_ports |= (1 << p->id);
468                 }
469
470                 *ports |= (1 << p->id);
471         }
472
473 #ifdef DEBUG
474         pr_cont("\n");
475 #endif
476
477         return 0;
478 };
479
480 /*
481  * Precondition: reg_mutex must be held
482  */
483 static void
484 adm6996_enable_vlan(struct adm6996_priv *priv)
485 {
486         u16 reg;
487
488         reg = r16(priv, ADM_OTBE_P2_PVID);
489         reg &= ~(ADM_OTBE_MASK);
490         w16(priv, ADM_OTBE_P2_PVID, reg);
491         reg = r16(priv, ADM_IFNTE);
492         reg &= ~(ADM_IFNTE_MASK);
493         w16(priv, ADM_IFNTE, reg);
494         reg = r16(priv, ADM_VID_CHECK);
495         reg |= ADM_VID_CHECK_MASK;
496         w16(priv, ADM_VID_CHECK, reg);
497         reg = r16(priv, ADM_SYSC0);
498         reg |= ADM_NTTE;
499         reg &= ~(ADM_RVID1);
500         w16(priv, ADM_SYSC0, reg);
501         reg = r16(priv, ADM_SYSC3);
502         reg |= ADM_TBV;
503         w16(priv, ADM_SYSC3, reg);
504 }
505
506 static void
507 adm6996_enable_vlan_6996l(struct adm6996_priv *priv)
508 {
509         u16 reg;
510
511         reg = r16(priv, ADM_SYSC3);
512         reg |= ADM_TBV;
513         reg |= ADM_MAC_CLONE;
514         w16(priv, ADM_SYSC3, reg);
515 }
516
517 /*
518  * Disable VLANs
519  *
520  * Sets VLAN mapping for port-based VLAN with all ports connected to
521  * eachother (this is also the power-on default).
522  *
523  * Precondition: reg_mutex must be held
524  */
525 static void
526 adm6996_disable_vlan(struct adm6996_priv *priv)
527 {
528         u16 reg;
529         int i;
530
531         for (i = 0; i < ADM_NUM_VLANS; i++) {
532                 reg = ADM_VLAN_FILT_MEMBER_MASK;
533                 w16(priv, ADM_VLAN_FILT_L(i), reg);
534                 reg = ADM_VLAN_FILT_VALID | ADM_VLAN_FILT_VID(1);
535                 w16(priv, ADM_VLAN_FILT_H(i), reg);
536         }
537
538         reg = r16(priv, ADM_OTBE_P2_PVID);
539         reg |= ADM_OTBE_MASK;
540         w16(priv, ADM_OTBE_P2_PVID, reg);
541         reg = r16(priv, ADM_IFNTE);
542         reg |= ADM_IFNTE_MASK;
543         w16(priv, ADM_IFNTE, reg);
544         reg = r16(priv, ADM_VID_CHECK);
545         reg &= ~(ADM_VID_CHECK_MASK);
546         w16(priv, ADM_VID_CHECK, reg);
547         reg = r16(priv, ADM_SYSC0);
548         reg &= ~(ADM_NTTE);
549         reg |= ADM_RVID1;
550         w16(priv, ADM_SYSC0, reg);
551         reg = r16(priv, ADM_SYSC3);
552         reg &= ~(ADM_TBV);
553         w16(priv, ADM_SYSC3, reg);
554 }
555
556 /*
557  * Disable VLANs
558  *
559  * Sets VLAN mapping for port-based VLAN with all ports connected to
560  * eachother (this is also the power-on default).
561  *
562  * Precondition: reg_mutex must be held
563  */
564 static void
565 adm6996_disable_vlan_6996l(struct adm6996_priv *priv)
566 {
567         u16 reg;
568         int i;
569
570         for (i = 0; i < ADM_NUM_VLANS; i++) {
571                 w16(priv, ADM_VLAN_MAP(i), 0);
572         }
573
574         reg = r16(priv, ADM_SYSC3);
575         reg &= ~(ADM_TBV);
576         reg &= ~(ADM_MAC_CLONE);
577         w16(priv, ADM_SYSC3, reg);
578 }
579
580 /*
581  * Precondition: reg_mutex must be held
582  */
583 static void
584 adm6996_apply_port_pvids(struct adm6996_priv *priv)
585 {
586         u16 reg;
587         int i;
588
589         for (i = 0; i < ADM_NUM_PORTS; i++) {
590                 reg = r16(priv, adm_portcfg[i]);
591                 reg &= ~(ADM_PORTCFG_PVID_MASK);
592                 reg |= ADM_PORTCFG_PVID(priv->pvid[i]);
593                 if (priv->model == ADM6996L) {
594                         if (priv->tagged_ports & (1 << i))
595                                 reg |= (1 << 4);
596                         else
597                                 reg &= ~(1 << 4);
598                 }
599                 w16(priv, adm_portcfg[i], reg);
600         }
601
602         w16(priv, ADM_P0_PVID, ADM_P0_PVID_VAL(priv->pvid[0]));
603         w16(priv, ADM_P1_PVID, ADM_P1_PVID_VAL(priv->pvid[1]));
604         reg = r16(priv, ADM_OTBE_P2_PVID);
605         reg &= ~(ADM_P2_PVID_MASK);
606         reg |= ADM_P2_PVID_VAL(priv->pvid[2]);
607         w16(priv, ADM_OTBE_P2_PVID, reg);
608         reg = ADM_P3_PVID_VAL(priv->pvid[3]);
609         reg |= ADM_P4_PVID_VAL(priv->pvid[4]);
610         w16(priv, ADM_P3_P4_PVID, reg);
611         reg = r16(priv, ADM_P5_PVID);
612         reg &= ~(ADM_P2_PVID_MASK);
613         reg |= ADM_P5_PVID_VAL(priv->pvid[5]);
614         w16(priv, ADM_P5_PVID, reg);
615 }
616
617 /*
618  * Precondition: reg_mutex must be held
619  */
620 static void
621 adm6996_apply_vlan_filters(struct adm6996_priv *priv)
622 {
623         u8 ports, tagged;
624         u16 vid, reg;
625         int i;
626
627         for (i = 0; i < ADM_NUM_VLANS; i++) {
628                 vid = priv->vlan_id[i];
629                 ports = priv->vlan_table[i];
630                 tagged = priv->vlan_tagged[i];
631
632                 if (ports == 0) {
633                         /* Disable VLAN entry */
634                         w16(priv, ADM_VLAN_FILT_H(i), 0);
635                         w16(priv, ADM_VLAN_FILT_L(i), 0);
636                         continue;
637                 }
638
639                 reg = ADM_VLAN_FILT_MEMBER(ports);
640                 reg |= ADM_VLAN_FILT_TAGGED(tagged);
641                 w16(priv, ADM_VLAN_FILT_L(i), reg);
642                 reg = ADM_VLAN_FILT_VALID | ADM_VLAN_FILT_VID(vid);
643                 w16(priv, ADM_VLAN_FILT_H(i), reg);
644         }
645 }
646
647 static void
648 adm6996_apply_vlan_filters_6996l(struct adm6996_priv *priv)
649 {
650         u8 ports;
651         u16 reg;
652         int i;
653
654         for (i = 0; i < ADM_NUM_VLANS; i++) {
655                 ports = priv->vlan_table[i];
656
657                 if (ports == 0) {
658                         /* Disable VLAN entry */
659                         w16(priv, ADM_VLAN_MAP(i), 0);
660                         continue;
661                 } else {
662                         reg = ADM_VLAN_FILT(ports);
663                         w16(priv, ADM_VLAN_MAP(i), reg);
664                 }
665         }
666 }
667
668 static int
669 adm6996_hw_apply(struct switch_dev *dev)
670 {
671         struct adm6996_priv *priv = to_adm(dev);
672
673         pr_devel("hw_apply\n");
674
675         mutex_lock(&priv->reg_mutex);
676
677         if (!priv->enable_vlan) {
678                 if (priv->vlan_enabled) {
679                         if (priv->model == ADM6996L)
680                                 adm6996_disable_vlan_6996l(priv);
681                         else
682                                 adm6996_disable_vlan(priv);
683                         priv->vlan_enabled = 0;
684                 }
685                 goto out;
686         }
687
688         if (!priv->vlan_enabled) {
689                 if (priv->model == ADM6996L)
690                         adm6996_enable_vlan_6996l(priv);
691                 else
692                         adm6996_enable_vlan(priv);
693                 priv->vlan_enabled = 1;
694         }
695
696         adm6996_apply_port_pvids(priv);
697         if (priv->model == ADM6996L)
698                 adm6996_apply_vlan_filters_6996l(priv);
699         else
700                 adm6996_apply_vlan_filters(priv);
701
702 out:
703         mutex_unlock(&priv->reg_mutex);
704
705         return 0;
706 }
707
708 /*
709  * Reset the switch
710  *
711  * The ADM6996 can't do a software-initiated reset, so we just initialise the
712  * registers we support in this driver.
713  *
714  * Precondition: reg_mutex must be held
715  */
716 static void
717 adm6996_perform_reset (struct adm6996_priv *priv)
718 {
719         int i;
720
721         /* initialize port and vlan settings */
722         for (i = 0; i < ADM_NUM_PORTS - 1; i++) {
723                 w16(priv, adm_portcfg[i], ADM_PORTCFG_INIT |
724                         ADM_PORTCFG_PVID(0));
725         }
726         w16(priv, adm_portcfg[5], ADM_PORTCFG_CPU);
727
728         if (priv->model == ADM6996M || priv->model == ADM6996FC) {
729                 /* reset all PHY ports */
730                 for (i = 0; i < ADM_PHY_PORTS; i++) {
731                         w16(priv, ADM_PHY_PORT(i), ADM_PHYCFG_INIT);
732                 }
733         }
734
735         priv->enable_vlan = 0;
736         priv->vlan_enabled = 0;
737
738         for (i = 0; i < ADM_NUM_PORTS; i++) {
739                 priv->pvid[i] = 0;
740         }
741
742         for (i = 0; i < ADM_NUM_VLANS; i++) {
743                 priv->vlan_id[i] = i;
744                 priv->vlan_table[i] = 0;
745                 priv->vlan_tagged[i] = 0;
746         }
747
748         if (priv->model == ADM6996M) {
749                 /* Clear VLAN priority map so prio's are unused */
750                 w16 (priv, ADM_VLAN_PRIOMAP, 0);
751
752                 adm6996_disable_vlan(priv);
753                 adm6996_apply_port_pvids(priv);
754         } else if (priv->model == ADM6996L) {
755                 /* Clear VLAN priority map so prio's are unused */
756                 w16 (priv, ADM_VLAN_PRIOMAP, 0);
757
758                 adm6996_disable_vlan_6996l(priv);
759                 adm6996_apply_port_pvids(priv);
760         }
761 }
762
763 static int
764 adm6996_reset_switch(struct switch_dev *dev)
765 {
766         struct adm6996_priv *priv = to_adm(dev);
767
768         pr_devel("reset\n");
769
770         mutex_lock(&priv->reg_mutex);
771         adm6996_perform_reset (priv);
772         mutex_unlock(&priv->reg_mutex);
773         return 0;
774 }
775
776 static struct switch_attr adm6996_globals[] = {
777         {
778          .type = SWITCH_TYPE_INT,
779          .name = "enable_vlan",
780          .description = "Enable VLANs",
781          .set = adm6996_set_enable_vlan,
782          .get = adm6996_get_enable_vlan,
783         },
784 #ifdef DEBUG
785         {
786          .type = SWITCH_TYPE_INT,
787          .name = "addr",
788          .description =
789          "Direct register access: set register address (0 - 1023)",
790          .set = adm6996_set_addr,
791          .get = adm6996_get_addr,
792          },
793         {
794          .type = SWITCH_TYPE_INT,
795          .name = "data",
796          .description =
797          "Direct register access: read/write to register (0 - 65535)",
798          .set = adm6996_set_data,
799          .get = adm6996_get_data,
800          },
801 #endif /* def DEBUG */
802 };
803
804 static struct switch_attr adm6996_port[] = {
805 };
806
807 static struct switch_attr adm6996_vlan[] = {
808         {
809          .type = SWITCH_TYPE_INT,
810          .name = "vid",
811          .description = "VLAN ID",
812          .set = adm6996_set_vid,
813          .get = adm6996_get_vid,
814          },
815 };
816
817 static const struct switch_dev_ops adm6996_ops = {
818         .attr_global = {
819                         .attr = adm6996_globals,
820                         .n_attr = ARRAY_SIZE(adm6996_globals),
821                         },
822         .attr_port = {
823                       .attr = adm6996_port,
824                       .n_attr = ARRAY_SIZE(adm6996_port),
825                       },
826         .attr_vlan = {
827                       .attr = adm6996_vlan,
828                       .n_attr = ARRAY_SIZE(adm6996_vlan),
829                       },
830         .get_port_pvid = adm6996_get_pvid,
831         .set_port_pvid = adm6996_set_pvid,
832         .get_vlan_ports = adm6996_get_ports,
833         .set_vlan_ports = adm6996_set_ports,
834         .apply_config = adm6996_hw_apply,
835         .reset_switch = adm6996_reset_switch,
836 };
837
838 static int adm6996_switch_init(struct adm6996_priv *priv, const char *alias, struct net_device *netdev)
839 {
840         struct switch_dev *swdev;
841         u16 test, old;
842
843         if (!priv->model) {
844                 /* Detect type of chip */
845                 old = r16(priv, ADM_VID_CHECK);
846                 test = old ^ (1 << 12);
847                 w16(priv, ADM_VID_CHECK, test);
848                 test ^= r16(priv, ADM_VID_CHECK);
849                 if (test & (1 << 12)) {
850                         /* 
851                          * Bit 12 of this register is read-only. 
852                          * This is the FC model. 
853                          */
854                         priv->model = ADM6996FC;
855                 } else {
856                         /* Bit 12 is read-write. This is the M model. */
857                         priv->model = ADM6996M;
858                         w16(priv, ADM_VID_CHECK, old);
859                 }
860         }
861
862         swdev = &priv->dev;
863         swdev->name = (adm6996_model_name[priv->model]);
864         swdev->cpu_port = ADM_CPU_PORT;
865         swdev->ports = ADM_NUM_PORTS;
866         swdev->vlans = ADM_NUM_VLANS;
867         swdev->ops = &adm6996_ops;
868         swdev->alias = alias;
869
870         pr_info ("%s: %s model PHY found.\n", alias, swdev->name);
871
872         mutex_lock(&priv->reg_mutex);
873         adm6996_perform_reset (priv);
874         mutex_unlock(&priv->reg_mutex);
875
876         if (priv->model == ADM6996M || priv->model == ADM6996L) {
877                 return register_switch(swdev, netdev);
878         }
879
880         return -ENODEV;
881 }
882
883 static int adm6996_config_init(struct phy_device *pdev)
884 {
885         struct adm6996_priv *priv;
886         int ret;
887
888         pdev->supported = ADVERTISED_100baseT_Full;
889         pdev->advertising = ADVERTISED_100baseT_Full;
890
891         if (pdev->addr != 0) {
892                 pr_info ("%s: PHY overlaps ADM6996, providing fixed PHY 0x%x.\n"
893                                 , pdev->attached_dev->name, pdev->addr);
894                 return 0;
895         }
896
897         priv = devm_kzalloc(&pdev->dev, sizeof(struct adm6996_priv), GFP_KERNEL);
898         if (!priv)
899                 return -ENOMEM;
900
901         mutex_init(&priv->reg_mutex);
902         priv->priv = pdev;
903         priv->read = adm6996_read_mii_reg;
904         priv->write = adm6996_write_mii_reg;
905
906         ret = adm6996_switch_init(priv, pdev->attached_dev->name, pdev->attached_dev);
907         if (ret < 0)
908                 return ret;
909
910         pdev->priv = priv;
911
912         return 0;
913 }
914
915 /*
916  * Warning: phydev->priv is NULL if phydev->addr != 0
917  */
918 static int adm6996_read_status(struct phy_device *phydev)
919 {
920         phydev->speed = SPEED_100;
921         phydev->duplex = DUPLEX_FULL;
922         phydev->link = 1;
923         return 0;
924 }
925
926 /*
927  * Warning: phydev->priv is NULL if phydev->addr != 0
928  */
929 static int adm6996_config_aneg(struct phy_device *phydev)
930 {
931         return 0;
932 }
933
934 static int adm6996_fixup(struct phy_device *dev)
935 {
936         struct mii_bus *bus = dev->bus;
937         u16 reg;
938
939         /* Our custom registers are at PHY addresses 0-10. Claim those. */
940         if (dev->addr > 10)
941                 return 0;
942
943         /* look for the switch on the bus */
944         reg = bus->read(bus, PHYADDR(ADM_SIG0)) & ADM_SIG0_MASK;
945         if (reg != ADM_SIG0_VAL)
946                 return 0;
947
948         reg = bus->read(bus, PHYADDR(ADM_SIG1)) & ADM_SIG1_MASK;
949         if (reg != ADM_SIG1_VAL)
950                 return 0;
951
952         dev->phy_id = (ADM_SIG0_VAL << 16) | ADM_SIG1_VAL;
953
954         return 0;
955 }
956
957 static int adm6996_probe(struct phy_device *pdev)
958 {
959         return 0;
960 }
961
962 static void adm6996_remove(struct phy_device *pdev)
963 {
964         struct adm6996_priv *priv = phy_to_adm(pdev);
965
966         if (priv && (priv->model == ADM6996M || priv->model == ADM6996L))
967                 unregister_switch(&priv->dev);
968 }
969
970
971 static struct phy_driver adm6996_phy_driver = {
972         .name           = "Infineon ADM6996",
973         .phy_id         = (ADM_SIG0_VAL << 16) | ADM_SIG1_VAL,
974         .phy_id_mask    = 0xffffffff,
975         .features       = PHY_BASIC_FEATURES,
976         .probe          = adm6996_probe,
977         .remove         = adm6996_remove,
978         .config_init    = &adm6996_config_init,
979         .config_aneg    = &adm6996_config_aneg,
980         .read_status    = &adm6996_read_status,
981         .driver         = { .owner = THIS_MODULE,},
982 };
983
984 static int adm6996_gpio_probe(struct platform_device *pdev)
985 {
986         struct adm6996_gpio_platform_data *pdata = pdev->dev.platform_data;
987         struct adm6996_priv *priv;
988         int ret;
989
990         if (!pdata)
991                 return -EINVAL;
992                                   
993         priv = devm_kzalloc(&pdev->dev, sizeof(struct adm6996_priv), GFP_KERNEL);
994         if (!priv)
995                 return -ENOMEM;
996
997         mutex_init(&priv->reg_mutex);
998
999         priv->eecs = pdata->eecs;
1000         priv->eedi = pdata->eedi;
1001         priv->eerc = pdata->eerc;
1002         priv->eesk = pdata->eesk;
1003
1004         priv->model = pdata->model;
1005         priv->read = adm6996_read_gpio_reg;
1006         priv->write = adm6996_write_gpio_reg;
1007
1008         ret = devm_gpio_request(&pdev->dev, priv->eecs, "adm_eecs");
1009         if (ret)
1010                 return ret;
1011         ret = devm_gpio_request(&pdev->dev, priv->eedi, "adm_eedi");
1012         if (ret)
1013                 return ret;
1014         ret = devm_gpio_request(&pdev->dev, priv->eerc, "adm_eerc");
1015         if (ret)
1016                 return ret;
1017         ret = devm_gpio_request(&pdev->dev, priv->eesk, "adm_eesk");
1018         if (ret)
1019                 return ret;
1020
1021         ret = adm6996_switch_init(priv, dev_name(&pdev->dev), NULL);
1022         if (ret < 0)
1023                 return ret;
1024
1025         platform_set_drvdata(pdev, priv);
1026
1027         return 0;
1028 }
1029
1030 static int adm6996_gpio_remove(struct platform_device *pdev)
1031 {
1032         struct adm6996_priv *priv = platform_get_drvdata(pdev);
1033
1034         if (priv && (priv->model == ADM6996M || priv->model == ADM6996L))
1035                 unregister_switch(&priv->dev);
1036
1037         return 0;
1038 }
1039
1040 static struct platform_driver adm6996_gpio_driver = {
1041         .probe = adm6996_gpio_probe,
1042         .remove = adm6996_gpio_remove,
1043         .driver = {
1044                 .name = "adm6996_gpio",
1045         },
1046 };
1047
1048 static int __init adm6996_init(void)
1049 {
1050         int err;
1051
1052         phy_register_fixup_for_id(PHY_ANY_ID, adm6996_fixup);
1053         err =  phy_driver_register(&adm6996_phy_driver);
1054         if (err)
1055                 return err;
1056
1057         err = platform_driver_register(&adm6996_gpio_driver);
1058         if (err)
1059                 phy_driver_unregister(&adm6996_phy_driver);
1060
1061         return err;
1062 }
1063
1064 static void __exit adm6996_exit(void)
1065 {
1066         platform_driver_unregister(&adm6996_gpio_driver);
1067         phy_driver_unregister(&adm6996_phy_driver);
1068 }
1069
1070 module_init(adm6996_init);
1071 module_exit(adm6996_exit);