ar71xx: add profile and build image for EnGenius EPG5000.
[15.05/openwrt.git] / target / linux / ramips / files / drivers / net / ethernet / ralink / mt7530.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
13  */
14
15 #include <linux/if.h>
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/list.h>
19 #include <linux/if_ether.h>
20 #include <linux/skbuff.h>
21 #include <linux/netdevice.h>
22 #include <linux/netlink.h>
23 #include <linux/bitops.h>
24 #include <net/genetlink.h>
25 #include <linux/switch.h>
26 #include <linux/delay.h>
27 #include <linux/phy.h>
28 #include <linux/netdevice.h>
29 #include <linux/etherdevice.h>
30 #include <linux/lockdep.h>
31 #include <linux/workqueue.h>
32 #include <linux/of_device.h>
33
34 #include "mt7530.h"
35
36 #define MT7530_CPU_PORT         6
37 #define MT7530_NUM_PORTS        8
38 #define MT7530_NUM_VLANS        16
39 #define MT7530_MAX_VID          4095
40 #define MT7530_MIN_VID          0
41
42 /* registers */
43 #define REG_ESW_VLAN_VTCR               0x90
44 #define REG_ESW_VLAN_VAWD1              0x94
45 #define REG_ESW_VLAN_VAWD2              0x98
46 #define REG_ESW_VLAN_VTIM(x)    (0x100 + 4 * ((x) / 2))
47
48 #define REG_ESW_VLAN_VAWD1_IVL_MAC      BIT(30)
49 #define REG_ESW_VLAN_VAWD1_VTAG_EN      BIT(28)
50 #define REG_ESW_VLAN_VAWD1_VALID        BIT(0)
51
52 /* vlan egress mode */
53 enum {
54         ETAG_CTRL_UNTAG = 0,
55         ETAG_CTRL_TAG   = 2,
56         ETAG_CTRL_SWAP  = 1,
57         ETAG_CTRL_STACK = 3,
58 };
59
60 #define REG_ESW_PORT_PCR(x)     (0x2004 | ((x) << 8))
61 #define REG_ESW_PORT_PVC(x)     (0x2010 | ((x) << 8))
62 #define REG_ESW_PORT_PPBV1(x)   (0x2014 | ((x) << 8))
63
64 #define REG_HWTRAP              0x7804
65
66 enum {
67         /* Global attributes. */
68         MT7530_ATTR_ENABLE_VLAN,
69 };
70
71 struct mt7530_port_entry {
72         u16     pvid;
73 };
74
75 struct mt7530_vlan_entry {
76         u16     vid;
77         u8      member;
78         u8      etags;
79 };
80
81 struct mt7530_priv {
82         void __iomem            *base;
83         struct mii_bus          *bus;
84         struct switch_dev       swdev;
85
86         bool                    global_vlan_enable;
87         struct mt7530_vlan_entry        vlan_entries[MT7530_NUM_VLANS];
88         struct mt7530_port_entry        port_entries[MT7530_NUM_PORTS];
89 };
90
91 struct mt7530_mapping {
92         char    *name;
93         u16     pvids[MT7530_NUM_PORTS];
94         u8      members[MT7530_NUM_VLANS];
95         u8      etags[MT7530_NUM_VLANS];
96         u16     vids[MT7530_NUM_VLANS];
97 } mt7530_defaults[] = {
98         {
99                 .name = "llllw",
100                 .pvids = { 1, 1, 1, 1, 2, 1, 1 },
101                 .members = { 0, 0x6f, 0x50 },
102                 .etags = { 0, 0x40, 0x40 },
103                 .vids = { 0, 1, 2 },
104         }, {
105                 .name = "wllll",
106                 .pvids = { 2, 1, 1, 1, 1, 1, 1 },
107                 .members = { 0, 0x7e, 0x41 },
108                 .etags = { 0, 0x40, 0x40 },
109                 .vids = { 0, 1, 2 },
110         },
111 };
112
113 struct mt7530_mapping*
114 mt7530_find_mapping(struct device_node *np)
115 {
116         const char *map;
117         int i;
118
119         if (of_property_read_string(np, "ralink,port-map", &map))
120                 return NULL;
121
122         for (i = 0; i < ARRAY_SIZE(mt7530_defaults); i++)
123                 if (!strcmp(map, mt7530_defaults[i].name))
124                         return &mt7530_defaults[i];
125
126         return NULL;
127 }
128
129 static void
130 mt7530_apply_mapping(struct mt7530_priv *mt7530, struct mt7530_mapping *map)
131 {
132         int i = 0;
133
134         for (i = 0; i < MT7530_NUM_PORTS; i++)
135                 mt7530->port_entries[i].pvid = map->pvids[i];
136
137         for (i = 0; i < MT7530_NUM_VLANS; i++) {
138                 mt7530->vlan_entries[i].member = map->members[i];
139                 mt7530->vlan_entries[i].etags = map->etags[i];
140                 mt7530->vlan_entries[i].vid = map->vids[i];
141         }
142 }
143
144 static int
145 mt7530_reset_switch(struct switch_dev *dev)
146 {
147         struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
148         int i;
149
150         memset(priv->port_entries, 0, sizeof(priv->port_entries));
151         memset(priv->vlan_entries, 0, sizeof(priv->vlan_entries));
152
153         /* set default vid of each vlan to the same number of vlan, so the vid
154          * won't need be set explicitly.
155          */
156         for (i = 0; i < MT7530_NUM_VLANS; i++) {
157                 priv->vlan_entries[i].vid = i;
158         }
159
160         return 0;
161 }
162
163 static int
164 mt7530_get_vlan_enable(struct switch_dev *dev,
165                            const struct switch_attr *attr,
166                            struct switch_val *val)
167 {
168         struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
169
170         val->value.i = priv->global_vlan_enable;
171
172         return 0;
173 }
174
175 static int
176 mt7530_set_vlan_enable(struct switch_dev *dev,
177                            const struct switch_attr *attr,
178                            struct switch_val *val)
179 {
180         struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
181
182         priv->global_vlan_enable = val->value.i != 0;
183
184         return 0;
185 }
186
187 static u32
188 mt7530_r32(struct mt7530_priv *priv, u32 reg)
189 {
190         u32 val;
191         if (priv->bus) {
192                 u16 high, low;
193
194                 mdiobus_write(priv->bus, 0x1f, 0x1f, (reg >> 6) & 0x3ff);
195                 low = mdiobus_read(priv->bus, 0x1f, (reg >> 2) & 0xf);
196                 high = mdiobus_read(priv->bus, 0x1f, 0x10);
197
198                 return (high << 16) | (low & 0xffff);
199         }
200
201         val = ioread32(priv->base + reg);
202         pr_debug("MT7530 MDIO Read [%04x]=%08x\n", reg, val);
203
204         return val;
205 }
206
207 static void
208 mt7530_w32(struct mt7530_priv *priv, u32 reg, u32 val)
209 {
210         if (priv->bus) {
211                 mdiobus_write(priv->bus, 0x1f, 0x1f, (reg >> 6) & 0x3ff);
212                 mdiobus_write(priv->bus, 0x1f, (reg >> 2) & 0xf,  val & 0xffff);
213                 mdiobus_write(priv->bus, 0x1f, 0x10, val >> 16);
214                 return;
215         }
216
217         pr_debug("MT7530 MDIO Write[%04x]=%08x\n", reg, val);
218         iowrite32(val, priv->base + reg);
219 }
220
221 static void
222 mt7530_vtcr(struct mt7530_priv *priv, u32 cmd, u32 val)
223 {
224         int i;
225
226         mt7530_w32(priv, REG_ESW_VLAN_VTCR, BIT(31) | (cmd << 12) | val);
227
228         for (i = 0; i < 20; i++) {
229                 u32 val = mt7530_r32(priv, REG_ESW_VLAN_VTCR);
230
231                 if ((val & BIT(31)) == 0)
232                         break;
233
234                 udelay(1000);
235         }
236         if (i == 20)
237                 printk("mt7530: vtcr timeout\n");
238 }
239
240 static int
241 mt7530_get_port_pvid(struct switch_dev *dev, int port, int *val)
242 {
243         struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
244
245         if (port >= MT7530_NUM_PORTS)
246                 return -EINVAL;
247
248         *val = mt7530_r32(priv, REG_ESW_PORT_PPBV1(port));
249         *val &= 0xfff;
250
251         return 0;
252 }
253
254 static int
255 mt7530_set_port_pvid(struct switch_dev *dev, int port, int pvid)
256 {
257         struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
258
259         if (port >= MT7530_NUM_PORTS)
260                 return -EINVAL;
261
262         if (pvid < MT7530_MIN_VID || pvid > MT7530_MAX_VID)
263                 return -EINVAL;
264
265         priv->port_entries[port].pvid = pvid;
266
267         return 0;
268 }
269
270 static int
271 mt7530_get_vlan_ports(struct switch_dev *dev, struct switch_val *val)
272 {
273         struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
274         u32 member;
275         u32 etags;
276         int i;
277
278         val->len = 0;
279
280         if (val->port_vlan < 0 || val->port_vlan >= MT7530_NUM_VLANS)
281                 return -EINVAL;
282
283         mt7530_vtcr(priv, 0, val->port_vlan);
284
285         member = mt7530_r32(priv, REG_ESW_VLAN_VAWD1);
286         member >>= 16;
287         member &= 0xff;
288
289         etags = mt7530_r32(priv, REG_ESW_VLAN_VAWD2);
290
291         for (i = 0; i < MT7530_NUM_PORTS; i++) {
292                 struct switch_port *p;
293                 int etag;
294
295                 if (!(member & BIT(i)))
296                         continue;
297
298                 p = &val->value.ports[val->len++];
299                 p->id = i;
300
301                 etag = (etags >> (i * 2)) & 0x3;
302
303                 if (etag == ETAG_CTRL_TAG)
304                         p->flags |= BIT(SWITCH_PORT_FLAG_TAGGED);
305                 else if (etag != ETAG_CTRL_UNTAG)
306                         printk("vlan egress tag control neither untag nor tag.\n");
307         }
308
309         return 0;
310 }
311
312 static int
313 mt7530_set_vlan_ports(struct switch_dev *dev, struct switch_val *val)
314 {
315         struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
316         u8 member = 0;
317         u8 etags = 0;
318         int i;
319
320         if (val->port_vlan < 0 || val->port_vlan >= MT7530_NUM_VLANS ||
321                         val->len > MT7530_NUM_PORTS)
322                 return -EINVAL;
323
324         for (i = 0; i < val->len; i++) {
325                 struct switch_port *p = &val->value.ports[i];
326
327                 if (p->id >= MT7530_NUM_PORTS)
328                         return -EINVAL;
329
330                 member |= BIT(p->id);
331
332                 if (p->flags & BIT(SWITCH_PORT_FLAG_TAGGED))
333                         etags |= BIT(p->id);
334         }
335         priv->vlan_entries[val->port_vlan].member = member;
336         priv->vlan_entries[val->port_vlan].etags = etags;
337
338         return 0;
339 }
340
341 static int
342 mt7530_set_vid(struct switch_dev *dev, const struct switch_attr *attr,
343                 struct switch_val *val)
344 {
345         struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
346         int vlan;
347         u16 vid;
348
349         vlan = val->port_vlan;
350         vid = (u16)val->value.i;
351
352         if (vlan < 0 || vlan >= MT7530_NUM_VLANS)
353                 return -EINVAL;
354
355         if (vid < MT7530_MIN_VID || vid > MT7530_MAX_VID)
356                 return -EINVAL;
357
358         priv->vlan_entries[vlan].vid = vid;
359         return 0;
360 }
361
362 static int
363 mt7530_get_vid(struct switch_dev *dev, const struct switch_attr *attr,
364                 struct switch_val *val)
365 {
366         struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
367         u32 vid;
368         int vlan;
369
370         vlan = val->port_vlan;
371
372         vid = mt7530_r32(priv, REG_ESW_VLAN_VTIM(vlan));
373         if (vlan & 1)
374                 vid = vid >> 12;
375         vid &= 0xfff;
376
377         val->value.i = vid;
378         return 0;
379 }
380
381 static int
382 mt7530_apply_config(struct switch_dev *dev)
383 {
384         struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
385         int i, j;
386
387         if (!priv->global_vlan_enable) {
388                 for (i = 0; i < MT7530_NUM_PORTS; i++)
389                         mt7530_w32(priv, REG_ESW_PORT_PCR(i), 0x00ff0000);
390
391                 for (i = 0; i < MT7530_NUM_PORTS; i++)
392                         mt7530_w32(priv, REG_ESW_PORT_PVC(i), 0x810000c0);
393
394                 return 0;
395         }
396
397         /* set all ports as security mode */
398         for (i = 0; i < MT7530_NUM_PORTS; i++)
399                 mt7530_w32(priv, REG_ESW_PORT_PCR(i), 0x00ff0003);
400
401         /* set all ports as user port */
402         for (i = 0; i < MT7530_NUM_PORTS; i++)
403                 mt7530_w32(priv, REG_ESW_PORT_PVC(i), 0x81000000);
404
405         for (i = 0; i < MT7530_NUM_VLANS; i++) {
406                 u16 vid = priv->vlan_entries[i].vid;
407                 u8 member = priv->vlan_entries[i].member;
408                 u8 etags = priv->vlan_entries[i].etags;
409                 u32 val;
410
411                 /* vid of vlan */
412                 val = mt7530_r32(priv, REG_ESW_VLAN_VTIM(i));
413                 if (i % 2 == 0) {
414                         val &= 0xfff000;
415                         val |= vid;
416                 } else {
417                         val &= 0xfff;
418                         val |= (vid << 12);
419                 }
420                 mt7530_w32(priv, REG_ESW_VLAN_VTIM(i), val);
421
422                 /* vlan port membership */
423                 if (member)
424                         mt7530_w32(priv, REG_ESW_VLAN_VAWD1, REG_ESW_VLAN_VAWD1_IVL_MAC |
425                                 REG_ESW_VLAN_VAWD1_VTAG_EN | (member << 16) |
426                                 REG_ESW_VLAN_VAWD1_VALID);
427                 else
428                         mt7530_w32(priv, REG_ESW_VLAN_VAWD1, 0);
429
430                 /* egress mode */
431                 val = 0;
432                 for (j = 0; j < MT7530_NUM_PORTS; j++) {
433                         if (etags & BIT(j))
434                                 val |= ETAG_CTRL_TAG << (j * 2);
435                         else
436                                 val |= ETAG_CTRL_UNTAG << (j * 2);
437                 }
438                 mt7530_w32(priv, REG_ESW_VLAN_VAWD2, val);
439
440                 /* write to vlan table */
441                 mt7530_vtcr(priv, 1, i);
442         }
443
444         /* Port Default PVID */
445         for (i = 0; i < MT7530_NUM_PORTS; i++) {
446                 u32 val;
447                 val = mt7530_r32(priv, REG_ESW_PORT_PPBV1(i));
448                 val &= ~0xfff;
449                 val |= priv->port_entries[i].pvid;
450                 mt7530_w32(priv, REG_ESW_PORT_PPBV1(i), val);
451         }
452
453         return 0;
454 }
455
456 static int
457 mt7530_get_port_link(struct switch_dev *dev,  int port,
458                         struct switch_port_link *link)
459 {
460         struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
461         u32 speed, pmsr;
462
463         if (port < 0 || port >= MT7530_NUM_PORTS)
464                 return -EINVAL;
465
466         pmsr = mt7530_r32(priv, 0x3008 + (0x100 * port));
467
468         link->link = pmsr & 1;
469         link->duplex = (pmsr >> 1) & 1;
470         speed = (pmsr >> 2) & 3;
471
472         switch (speed) {
473         case 0:
474                 link->speed = SWITCH_PORT_SPEED_10;
475                 break;
476         case 1:
477                 link->speed = SWITCH_PORT_SPEED_100;
478                 break;
479         case 2:
480         case 3: /* forced gige speed can be 2 or 3 */
481                 link->speed = SWITCH_PORT_SPEED_1000;
482                 break;
483         default:
484                 link->speed = SWITCH_PORT_SPEED_UNKNOWN;
485                 break;
486         }
487
488         return 0;
489 }
490
491 static const struct switch_attr mt7530_global[] = {
492         {
493                 .type = SWITCH_TYPE_INT,
494                 .name = "enable_vlan",
495                 .description = "VLAN mode (1:enabled)",
496                 .max = 1,
497                 .id = MT7530_ATTR_ENABLE_VLAN,
498                 .get = mt7530_get_vlan_enable,
499                 .set = mt7530_set_vlan_enable,
500         },
501 };
502
503 static const struct switch_attr mt7530_port[] = {
504 };
505
506 static const struct switch_attr mt7530_vlan[] = {
507         {
508                 .type = SWITCH_TYPE_INT,
509                 .name = "vid",
510                 .description = "VLAN ID (0-4094)",
511                 .set = mt7530_set_vid,
512                 .get = mt7530_get_vid,
513                 .max = 4094,
514         },
515 };
516
517 static const struct switch_dev_ops mt7530_ops = {
518         .attr_global = {
519                 .attr = mt7530_global,
520                 .n_attr = ARRAY_SIZE(mt7530_global),
521         },
522         .attr_port = {
523                 .attr = mt7530_port,
524                 .n_attr = ARRAY_SIZE(mt7530_port),
525         },
526         .attr_vlan = {
527                 .attr = mt7530_vlan,
528                 .n_attr = ARRAY_SIZE(mt7530_vlan),
529         },
530         .get_vlan_ports = mt7530_get_vlan_ports,
531         .set_vlan_ports = mt7530_set_vlan_ports,
532         .get_port_pvid = mt7530_get_port_pvid,
533         .set_port_pvid = mt7530_set_port_pvid,
534         .get_port_link = mt7530_get_port_link,
535         .apply_config = mt7530_apply_config,
536         .reset_switch = mt7530_reset_switch,
537 };
538
539 int
540 mt7530_probe(struct device *dev, void __iomem *base, struct mii_bus *bus, int vlan)
541 {
542         struct switch_dev *swdev;
543         struct mt7530_priv *mt7530;
544         struct mt7530_mapping *map;
545         int ret;
546
547         mt7530 = devm_kzalloc(dev, sizeof(struct mt7530_priv), GFP_KERNEL);
548         if (!mt7530)
549                 return -ENOMEM;
550
551         mt7530->base = base;
552         mt7530->bus = bus;
553         mt7530->global_vlan_enable = vlan;
554
555         swdev = &mt7530->swdev;
556         if (bus) {
557                 swdev->alias = "mt7530";
558                 swdev->name = "mt7530";
559         } else {
560                 swdev->alias = "mt7620";
561                 swdev->name = "mt7620";
562         }
563         swdev->cpu_port = MT7530_CPU_PORT;
564         swdev->ports = MT7530_NUM_PORTS;
565         swdev->vlans = MT7530_NUM_VLANS;
566         swdev->ops = &mt7530_ops;
567
568         ret = register_switch(swdev, NULL);
569         if (ret) {
570                 dev_err(dev, "failed to register mt7530\n");
571                 return ret;
572         }
573
574
575         map = mt7530_find_mapping(dev->of_node);
576         if (map)
577                 mt7530_apply_mapping(mt7530, map);
578         mt7530_apply_config(swdev);
579
580         /* magic vodoo */
581         if (!IS_ENABLED(CONFIG_SOC_MT7621) && bus && mt7530_r32(mt7530, REG_HWTRAP) !=  0x1117edf) {
582                 dev_info(dev, "fixing up MHWTRAP register - bootloader probably played with it\n");
583                 mt7530_w32(mt7530, REG_HWTRAP, 0x1117edf);
584         }
585         dev_info(dev, "loaded %s driver\n", swdev->name);
586
587         return 0;
588 }