0513-net-mediatek-add-swconfig-driver-for-gsw_mt762x.patch 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  1. From cf5a08f1f16913da8bb24a96afaa2969b29d0827 Mon Sep 17 00:00:00 2001
  2. From: John Crispin <blogic@openwrt.org>
  3. Date: Mon, 14 Dec 2015 22:25:57 +0100
  4. Subject: [PATCH 513/513] net: mediatek: add swconfig driver for gsw_mt762x
  5. Signed-off-by: John Crispin <blogic@openwrt.org>
  6. ---
  7. drivers/net/ethernet/mediatek/Makefile | 4 +-
  8. drivers/net/ethernet/mediatek/mt7530.c | 804 +++++++++++++++++++++++++++
  9. drivers/net/ethernet/mediatek/mt7530.h | 20 +
  10. drivers/net/ethernet/mediatek/mtk_eth_soc.c | 9 +-
  11. drivers/net/ethernet/mediatek/mtk_eth_soc.h | 1 +
  12. drivers/net/ethernet/mediatek/soc_mt7620.c | 1 +
  13. 6 files changed, 835 insertions(+), 4 deletions(-)
  14. create mode 100644 drivers/net/ethernet/mediatek/mt7530.c
  15. create mode 100644 drivers/net/ethernet/mediatek/mt7530.h
  16. --- a/drivers/net/ethernet/mediatek/Makefile
  17. +++ b/drivers/net/ethernet/mediatek/Makefile
  18. @@ -15,6 +15,6 @@ mtk-eth-soc-$(CONFIG_NET_MEDIATEK_MT7620
  19. mtk-eth-soc-$(CONFIG_NET_MEDIATEK_MT7621) += soc_mt7621.o
  20. obj-$(CONFIG_NET_MEDIATEK_ESW_RT3050) += esw_rt3050.o
  21. -obj-$(CONFIG_NET_MEDIATEK_GSW_MT7620) += gsw_mt7620.o
  22. -obj-$(CONFIG_NET_MEDIATEK_GSW_MT7621) += gsw_mt7621.o
  23. +obj-$(CONFIG_NET_MEDIATEK_GSW_MT7620) += gsw_mt7620.o mt7530.o
  24. +obj-$(CONFIG_NET_MEDIATEK_GSW_MT7621) += gsw_mt7621.o mt7530.o
  25. obj-$(CONFIG_NET_MEDIATEK_SOC) += mtk-eth-soc.o
  26. --- /dev/null
  27. +++ b/drivers/net/ethernet/mediatek/mt7530.c
  28. @@ -0,0 +1,804 @@
  29. +/*
  30. + * This program is free software; you can redistribute it and/or
  31. + * modify it under the terms of the GNU General Public License
  32. + * as published by the Free Software Foundation; either version 2
  33. + * of the License, or (at your option) any later version.
  34. + *
  35. + * This program is distributed in the hope that it will be useful,
  36. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  38. + * GNU General Public License for more details.
  39. + *
  40. + * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
  41. + */
  42. +
  43. +#include <linux/if.h>
  44. +#include <linux/module.h>
  45. +#include <linux/init.h>
  46. +#include <linux/list.h>
  47. +#include <linux/if_ether.h>
  48. +#include <linux/skbuff.h>
  49. +#include <linux/netdevice.h>
  50. +#include <linux/netlink.h>
  51. +#include <linux/bitops.h>
  52. +#include <net/genetlink.h>
  53. +#include <linux/switch.h>
  54. +#include <linux/delay.h>
  55. +#include <linux/phy.h>
  56. +#include <linux/netdevice.h>
  57. +#include <linux/etherdevice.h>
  58. +#include <linux/lockdep.h>
  59. +#include <linux/workqueue.h>
  60. +#include <linux/of_device.h>
  61. +
  62. +#include "mt7530.h"
  63. +
  64. +#define MT7530_CPU_PORT 6
  65. +#define MT7530_NUM_PORTS 8
  66. +#define MT7530_NUM_VLANS 16
  67. +#define MT7530_MAX_VID 4095
  68. +#define MT7530_MIN_VID 0
  69. +
  70. +/* registers */
  71. +#define REG_ESW_VLAN_VTCR 0x90
  72. +#define REG_ESW_VLAN_VAWD1 0x94
  73. +#define REG_ESW_VLAN_VAWD2 0x98
  74. +#define REG_ESW_VLAN_VTIM(x) (0x100 + 4 * ((x) / 2))
  75. +
  76. +#define REG_ESW_VLAN_VAWD1_IVL_MAC BIT(30)
  77. +#define REG_ESW_VLAN_VAWD1_VTAG_EN BIT(28)
  78. +#define REG_ESW_VLAN_VAWD1_VALID BIT(0)
  79. +
  80. +/* vlan egress mode */
  81. +enum {
  82. + ETAG_CTRL_UNTAG = 0,
  83. + ETAG_CTRL_TAG = 2,
  84. + ETAG_CTRL_SWAP = 1,
  85. + ETAG_CTRL_STACK = 3,
  86. +};
  87. +
  88. +#define REG_ESW_PORT_PCR(x) (0x2004 | ((x) << 8))
  89. +#define REG_ESW_PORT_PVC(x) (0x2010 | ((x) << 8))
  90. +#define REG_ESW_PORT_PPBV1(x) (0x2014 | ((x) << 8))
  91. +
  92. +#define REG_HWTRAP 0x7804
  93. +
  94. +#define MIB_DESC(_s , _o, _n) \
  95. + { \
  96. + .size = (_s), \
  97. + .offset = (_o), \
  98. + .name = (_n), \
  99. + }
  100. +
  101. +struct mt7xxx_mib_desc {
  102. + unsigned int size;
  103. + unsigned int offset;
  104. + const char *name;
  105. +};
  106. +
  107. +#define MT7621_MIB_COUNTER_BASE 0x4000
  108. +#define MT7621_MIB_COUNTER_PORT_OFFSET 0x100
  109. +#define MT7621_STATS_TDPC 0x00
  110. +#define MT7621_STATS_TCRC 0x04
  111. +#define MT7621_STATS_TUPC 0x08
  112. +#define MT7621_STATS_TMPC 0x0C
  113. +#define MT7621_STATS_TBPC 0x10
  114. +#define MT7621_STATS_TCEC 0x14
  115. +#define MT7621_STATS_TSCEC 0x18
  116. +#define MT7621_STATS_TMCEC 0x1C
  117. +#define MT7621_STATS_TDEC 0x20
  118. +#define MT7621_STATS_TLCEC 0x24
  119. +#define MT7621_STATS_TXCEC 0x28
  120. +#define MT7621_STATS_TPPC 0x2C
  121. +#define MT7621_STATS_TL64PC 0x30
  122. +#define MT7621_STATS_TL65PC 0x34
  123. +#define MT7621_STATS_TL128PC 0x38
  124. +#define MT7621_STATS_TL256PC 0x3C
  125. +#define MT7621_STATS_TL512PC 0x40
  126. +#define MT7621_STATS_TL1024PC 0x44
  127. +#define MT7621_STATS_TOC 0x48
  128. +#define MT7621_STATS_RDPC 0x60
  129. +#define MT7621_STATS_RFPC 0x64
  130. +#define MT7621_STATS_RUPC 0x68
  131. +#define MT7621_STATS_RMPC 0x6C
  132. +#define MT7621_STATS_RBPC 0x70
  133. +#define MT7621_STATS_RAEPC 0x74
  134. +#define MT7621_STATS_RCEPC 0x78
  135. +#define MT7621_STATS_RUSPC 0x7C
  136. +#define MT7621_STATS_RFEPC 0x80
  137. +#define MT7621_STATS_ROSPC 0x84
  138. +#define MT7621_STATS_RJEPC 0x88
  139. +#define MT7621_STATS_RPPC 0x8C
  140. +#define MT7621_STATS_RL64PC 0x90
  141. +#define MT7621_STATS_RL65PC 0x94
  142. +#define MT7621_STATS_RL128PC 0x98
  143. +#define MT7621_STATS_RL256PC 0x9C
  144. +#define MT7621_STATS_RL512PC 0xA0
  145. +#define MT7621_STATS_RL1024PC 0xA4
  146. +#define MT7621_STATS_ROC 0xA8
  147. +#define MT7621_STATS_RDPC_CTRL 0xB0
  148. +#define MT7621_STATS_RDPC_ING 0xB4
  149. +#define MT7621_STATS_RDPC_ARL 0xB8
  150. +
  151. +static const struct mt7xxx_mib_desc mt7621_mibs[] = {
  152. + MIB_DESC(1, MT7621_STATS_TDPC, "TxDrop"),
  153. + MIB_DESC(1, MT7621_STATS_TCRC, "TxCRC"),
  154. + MIB_DESC(1, MT7621_STATS_TUPC, "TxUni"),
  155. + MIB_DESC(1, MT7621_STATS_TMPC, "TxMulti"),
  156. + MIB_DESC(1, MT7621_STATS_TBPC, "TxBroad"),
  157. + MIB_DESC(1, MT7621_STATS_TCEC, "TxCollision"),
  158. + MIB_DESC(1, MT7621_STATS_TSCEC, "TxSingleCol"),
  159. + MIB_DESC(1, MT7621_STATS_TMCEC, "TxMultiCol"),
  160. + MIB_DESC(1, MT7621_STATS_TDEC, "TxDefer"),
  161. + MIB_DESC(1, MT7621_STATS_TLCEC, "TxLateCol"),
  162. + MIB_DESC(1, MT7621_STATS_TXCEC, "TxExcCol"),
  163. + MIB_DESC(1, MT7621_STATS_TPPC, "TxPause"),
  164. + MIB_DESC(1, MT7621_STATS_TL64PC, "Tx64Byte"),
  165. + MIB_DESC(1, MT7621_STATS_TL65PC, "Tx65Byte"),
  166. + MIB_DESC(1, MT7621_STATS_TL128PC, "Tx128Byte"),
  167. + MIB_DESC(1, MT7621_STATS_TL256PC, "Tx256Byte"),
  168. + MIB_DESC(1, MT7621_STATS_TL512PC, "Tx512Byte"),
  169. + MIB_DESC(1, MT7621_STATS_TL1024PC, "Tx1024Byte"),
  170. + MIB_DESC(2, MT7621_STATS_TOC, "TxByte"),
  171. + MIB_DESC(1, MT7621_STATS_RDPC, "RxDrop"),
  172. + MIB_DESC(1, MT7621_STATS_RFPC, "RxFiltered"),
  173. + MIB_DESC(1, MT7621_STATS_RUPC, "RxUni"),
  174. + MIB_DESC(1, MT7621_STATS_RMPC, "RxMulti"),
  175. + MIB_DESC(1, MT7621_STATS_RBPC, "RxBroad"),
  176. + MIB_DESC(1, MT7621_STATS_RAEPC, "RxAlignErr"),
  177. + MIB_DESC(1, MT7621_STATS_RCEPC, "RxCRC"),
  178. + MIB_DESC(1, MT7621_STATS_RUSPC, "RxUnderSize"),
  179. + MIB_DESC(1, MT7621_STATS_RFEPC, "RxFragment"),
  180. + MIB_DESC(1, MT7621_STATS_ROSPC, "RxOverSize"),
  181. + MIB_DESC(1, MT7621_STATS_RJEPC, "RxJabber"),
  182. + MIB_DESC(1, MT7621_STATS_RPPC, "RxPause"),
  183. + MIB_DESC(1, MT7621_STATS_RL64PC, "Rx64Byte"),
  184. + MIB_DESC(1, MT7621_STATS_RL65PC, "Rx65Byte"),
  185. + MIB_DESC(1, MT7621_STATS_RL128PC, "Rx128Byte"),
  186. + MIB_DESC(1, MT7621_STATS_RL256PC, "Rx256Byte"),
  187. + MIB_DESC(1, MT7621_STATS_RL512PC, "Rx512Byte"),
  188. + MIB_DESC(1, MT7621_STATS_RL1024PC, "Rx1024Byte"),
  189. + MIB_DESC(2, MT7621_STATS_ROC, "RxByte"),
  190. + MIB_DESC(1, MT7621_STATS_RDPC_CTRL, "RxCtrlDrop"),
  191. + MIB_DESC(1, MT7621_STATS_RDPC_ING, "RxIngDrop"),
  192. + MIB_DESC(1, MT7621_STATS_RDPC_ARL, "RxARLDrop")
  193. +};
  194. +
  195. +enum {
  196. + /* Global attributes. */
  197. + MT7530_ATTR_ENABLE_VLAN,
  198. +};
  199. +
  200. +struct mt7530_port_entry {
  201. + u16 pvid;
  202. +};
  203. +
  204. +struct mt7530_vlan_entry {
  205. + u16 vid;
  206. + u8 member;
  207. + u8 etags;
  208. +};
  209. +
  210. +struct mt7530_priv {
  211. + void __iomem *base;
  212. + struct mii_bus *bus;
  213. + struct switch_dev swdev;
  214. +
  215. + bool global_vlan_enable;
  216. + struct mt7530_vlan_entry vlan_entries[MT7530_NUM_VLANS];
  217. + struct mt7530_port_entry port_entries[MT7530_NUM_PORTS];
  218. +};
  219. +
  220. +struct mt7530_mapping {
  221. + char *name;
  222. + u16 pvids[MT7530_NUM_PORTS];
  223. + u8 members[MT7530_NUM_VLANS];
  224. + u8 etags[MT7530_NUM_VLANS];
  225. + u16 vids[MT7530_NUM_VLANS];
  226. +} mt7530_defaults[] = {
  227. + {
  228. + .name = "llllw",
  229. + .pvids = { 1, 1, 1, 1, 2, 1, 1 },
  230. + .members = { 0, 0x6f, 0x50 },
  231. + .etags = { 0, 0x40, 0x40 },
  232. + .vids = { 0, 1, 2 },
  233. + }, {
  234. + .name = "wllll",
  235. + .pvids = { 2, 1, 1, 1, 1, 1, 1 },
  236. + .members = { 0, 0x7e, 0x41 },
  237. + .etags = { 0, 0x40, 0x40 },
  238. + .vids = { 0, 1, 2 },
  239. + },
  240. +};
  241. +
  242. +struct mt7530_mapping*
  243. +mt7530_find_mapping(struct device_node *np)
  244. +{
  245. + const char *map;
  246. + int i;
  247. +
  248. + if (of_property_read_string(np, "mediatek,portmap", &map))
  249. + return NULL;
  250. +
  251. + for (i = 0; i < ARRAY_SIZE(mt7530_defaults); i++)
  252. + if (!strcmp(map, mt7530_defaults[i].name))
  253. + return &mt7530_defaults[i];
  254. +
  255. + return NULL;
  256. +}
  257. +
  258. +static void
  259. +mt7530_apply_mapping(struct mt7530_priv *mt7530, struct mt7530_mapping *map)
  260. +{
  261. + int i = 0;
  262. +
  263. + for (i = 0; i < MT7530_NUM_PORTS; i++)
  264. + mt7530->port_entries[i].pvid = map->pvids[i];
  265. +
  266. + for (i = 0; i < MT7530_NUM_VLANS; i++) {
  267. + mt7530->vlan_entries[i].member = map->members[i];
  268. + mt7530->vlan_entries[i].etags = map->etags[i];
  269. + mt7530->vlan_entries[i].vid = map->vids[i];
  270. + }
  271. +}
  272. +
  273. +static int
  274. +mt7530_reset_switch(struct switch_dev *dev)
  275. +{
  276. + struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
  277. + int i;
  278. +
  279. + memset(priv->port_entries, 0, sizeof(priv->port_entries));
  280. + memset(priv->vlan_entries, 0, sizeof(priv->vlan_entries));
  281. +
  282. + /* set default vid of each vlan to the same number of vlan, so the vid
  283. + * won't need be set explicitly.
  284. + */
  285. + for (i = 0; i < MT7530_NUM_VLANS; i++) {
  286. + priv->vlan_entries[i].vid = i;
  287. + }
  288. +
  289. + return 0;
  290. +}
  291. +
  292. +static int
  293. +mt7530_get_vlan_enable(struct switch_dev *dev,
  294. + const struct switch_attr *attr,
  295. + struct switch_val *val)
  296. +{
  297. + struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
  298. +
  299. + val->value.i = priv->global_vlan_enable;
  300. +
  301. + return 0;
  302. +}
  303. +
  304. +static int
  305. +mt7530_set_vlan_enable(struct switch_dev *dev,
  306. + const struct switch_attr *attr,
  307. + struct switch_val *val)
  308. +{
  309. + struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
  310. +
  311. + priv->global_vlan_enable = val->value.i != 0;
  312. +
  313. + return 0;
  314. +}
  315. +
  316. +static u32
  317. +mt7530_r32(struct mt7530_priv *priv, u32 reg)
  318. +{
  319. + u32 val;
  320. + if (priv->bus) {
  321. + u16 high, low;
  322. +
  323. + mdiobus_write(priv->bus, 0x1f, 0x1f, (reg >> 6) & 0x3ff);
  324. + low = mdiobus_read(priv->bus, 0x1f, (reg >> 2) & 0xf);
  325. + high = mdiobus_read(priv->bus, 0x1f, 0x10);
  326. +
  327. + return (high << 16) | (low & 0xffff);
  328. + }
  329. +
  330. + val = ioread32(priv->base + reg);
  331. + pr_debug("MT7530 MDIO Read [%04x]=%08x\n", reg, val);
  332. +
  333. + return val;
  334. +}
  335. +
  336. +static void
  337. +mt7530_w32(struct mt7530_priv *priv, u32 reg, u32 val)
  338. +{
  339. + if (priv->bus) {
  340. + mdiobus_write(priv->bus, 0x1f, 0x1f, (reg >> 6) & 0x3ff);
  341. + mdiobus_write(priv->bus, 0x1f, (reg >> 2) & 0xf, val & 0xffff);
  342. + mdiobus_write(priv->bus, 0x1f, 0x10, val >> 16);
  343. + return;
  344. + }
  345. +
  346. + pr_debug("MT7530 MDIO Write[%04x]=%08x\n", reg, val);
  347. + iowrite32(val, priv->base + reg);
  348. +}
  349. +
  350. +static void
  351. +mt7530_vtcr(struct mt7530_priv *priv, u32 cmd, u32 val)
  352. +{
  353. + int i;
  354. +
  355. + mt7530_w32(priv, REG_ESW_VLAN_VTCR, BIT(31) | (cmd << 12) | val);
  356. +
  357. + for (i = 0; i < 20; i++) {
  358. + u32 val = mt7530_r32(priv, REG_ESW_VLAN_VTCR);
  359. +
  360. + if ((val & BIT(31)) == 0)
  361. + break;
  362. +
  363. + udelay(1000);
  364. + }
  365. + if (i == 20)
  366. + printk("mt7530: vtcr timeout\n");
  367. +}
  368. +
  369. +static int
  370. +mt7530_get_port_pvid(struct switch_dev *dev, int port, int *val)
  371. +{
  372. + struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
  373. +
  374. + if (port >= MT7530_NUM_PORTS)
  375. + return -EINVAL;
  376. +
  377. + *val = mt7530_r32(priv, REG_ESW_PORT_PPBV1(port));
  378. + *val &= 0xfff;
  379. +
  380. + return 0;
  381. +}
  382. +
  383. +static int
  384. +mt7530_set_port_pvid(struct switch_dev *dev, int port, int pvid)
  385. +{
  386. + struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
  387. +
  388. + if (port >= MT7530_NUM_PORTS)
  389. + return -EINVAL;
  390. +
  391. + if (pvid < MT7530_MIN_VID || pvid > MT7530_MAX_VID)
  392. + return -EINVAL;
  393. +
  394. + priv->port_entries[port].pvid = pvid;
  395. +
  396. + return 0;
  397. +}
  398. +
  399. +static int
  400. +mt7530_get_vlan_ports(struct switch_dev *dev, struct switch_val *val)
  401. +{
  402. + struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
  403. + u32 member;
  404. + u32 etags;
  405. + int i;
  406. +
  407. + val->len = 0;
  408. +
  409. + if (val->port_vlan < 0 || val->port_vlan >= MT7530_NUM_VLANS)
  410. + return -EINVAL;
  411. +
  412. + mt7530_vtcr(priv, 0, val->port_vlan);
  413. +
  414. + member = mt7530_r32(priv, REG_ESW_VLAN_VAWD1);
  415. + member >>= 16;
  416. + member &= 0xff;
  417. +
  418. + etags = mt7530_r32(priv, REG_ESW_VLAN_VAWD2);
  419. +
  420. + for (i = 0; i < MT7530_NUM_PORTS; i++) {
  421. + struct switch_port *p;
  422. + int etag;
  423. +
  424. + if (!(member & BIT(i)))
  425. + continue;
  426. +
  427. + p = &val->value.ports[val->len++];
  428. + p->id = i;
  429. +
  430. + etag = (etags >> (i * 2)) & 0x3;
  431. +
  432. + if (etag == ETAG_CTRL_TAG)
  433. + p->flags |= BIT(SWITCH_PORT_FLAG_TAGGED);
  434. + else if (etag != ETAG_CTRL_UNTAG)
  435. + printk("vlan egress tag control neither untag nor tag.\n");
  436. + }
  437. +
  438. + return 0;
  439. +}
  440. +
  441. +static int
  442. +mt7530_set_vlan_ports(struct switch_dev *dev, struct switch_val *val)
  443. +{
  444. + struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
  445. + u8 member = 0;
  446. + u8 etags = 0;
  447. + int i;
  448. +
  449. + if (val->port_vlan < 0 || val->port_vlan >= MT7530_NUM_VLANS ||
  450. + val->len > MT7530_NUM_PORTS)
  451. + return -EINVAL;
  452. +
  453. + for (i = 0; i < val->len; i++) {
  454. + struct switch_port *p = &val->value.ports[i];
  455. +
  456. + if (p->id >= MT7530_NUM_PORTS)
  457. + return -EINVAL;
  458. +
  459. + member |= BIT(p->id);
  460. +
  461. + if (p->flags & BIT(SWITCH_PORT_FLAG_TAGGED))
  462. + etags |= BIT(p->id);
  463. + }
  464. + priv->vlan_entries[val->port_vlan].member = member;
  465. + priv->vlan_entries[val->port_vlan].etags = etags;
  466. +
  467. + return 0;
  468. +}
  469. +
  470. +static int
  471. +mt7530_set_vid(struct switch_dev *dev, const struct switch_attr *attr,
  472. + struct switch_val *val)
  473. +{
  474. + struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
  475. + int vlan;
  476. + u16 vid;
  477. +
  478. + vlan = val->port_vlan;
  479. + vid = (u16)val->value.i;
  480. +
  481. + if (vlan < 0 || vlan >= MT7530_NUM_VLANS)
  482. + return -EINVAL;
  483. +
  484. + if (vid < MT7530_MIN_VID || vid > MT7530_MAX_VID)
  485. + return -EINVAL;
  486. +
  487. + priv->vlan_entries[vlan].vid = vid;
  488. + return 0;
  489. +}
  490. +
  491. +static int
  492. +mt7530_get_vid(struct switch_dev *dev, const struct switch_attr *attr,
  493. + struct switch_val *val)
  494. +{
  495. + struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
  496. + u32 vid;
  497. + int vlan;
  498. +
  499. + vlan = val->port_vlan;
  500. +
  501. + vid = mt7530_r32(priv, REG_ESW_VLAN_VTIM(vlan));
  502. + if (vlan & 1)
  503. + vid = vid >> 12;
  504. + vid &= 0xfff;
  505. +
  506. + val->value.i = vid;
  507. + return 0;
  508. +}
  509. +
  510. +static int
  511. +mt7530_apply_config(struct switch_dev *dev)
  512. +{
  513. + struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
  514. + int i, j;
  515. + u8 tag_ports;
  516. + u8 untag_ports;
  517. +
  518. + if (!priv->global_vlan_enable) {
  519. + for (i = 0; i < MT7530_NUM_PORTS; i++)
  520. + mt7530_w32(priv, REG_ESW_PORT_PCR(i), 0x00ff0000);
  521. +
  522. + for (i = 0; i < MT7530_NUM_PORTS; i++)
  523. + mt7530_w32(priv, REG_ESW_PORT_PVC(i), 0x810000c0);
  524. +
  525. + return 0;
  526. + }
  527. +
  528. + /* set all ports as security mode */
  529. + for (i = 0; i < MT7530_NUM_PORTS; i++)
  530. + mt7530_w32(priv, REG_ESW_PORT_PCR(i), 0x00ff0003);
  531. +
  532. + /* check if a port is used in tag/untag vlan egress mode */
  533. + tag_ports = 0;
  534. + untag_ports = 0;
  535. +
  536. + for (i = 0; i < MT7530_NUM_VLANS; i++) {
  537. + u8 member = priv->vlan_entries[i].member;
  538. + u8 etags = priv->vlan_entries[i].etags;
  539. +
  540. + if (!member)
  541. + continue;
  542. +
  543. + for (j = 0; j < MT7530_NUM_PORTS; j++) {
  544. + if (!(member & BIT(j)))
  545. + continue;
  546. +
  547. + if (etags & BIT(j))
  548. + tag_ports |= 1u << j;
  549. + else
  550. + untag_ports |= 1u << j;
  551. + }
  552. + }
  553. +
  554. + /* set all untag-only ports as transparent and the rest as user port */
  555. + for (i = 0; i < MT7530_NUM_PORTS; i++) {
  556. + u32 pvc_mode = 0x81000000;
  557. +
  558. + if (untag_ports & BIT(i) && !(tag_ports & BIT(i)))
  559. + pvc_mode = 0x810000c0;
  560. +
  561. + mt7530_w32(priv, REG_ESW_PORT_PVC(i), pvc_mode);
  562. + }
  563. +
  564. + for (i = 0; i < MT7530_NUM_VLANS; i++) {
  565. + u16 vid = priv->vlan_entries[i].vid;
  566. + u8 member = priv->vlan_entries[i].member;
  567. + u8 etags = priv->vlan_entries[i].etags;
  568. + u32 val;
  569. +
  570. + /* vid of vlan */
  571. + val = mt7530_r32(priv, REG_ESW_VLAN_VTIM(i));
  572. + if (i % 2 == 0) {
  573. + val &= 0xfff000;
  574. + val |= vid;
  575. + } else {
  576. + val &= 0xfff;
  577. + val |= (vid << 12);
  578. + }
  579. + mt7530_w32(priv, REG_ESW_VLAN_VTIM(i), val);
  580. +
  581. + /* vlan port membership */
  582. + if (member)
  583. + mt7530_w32(priv, REG_ESW_VLAN_VAWD1, REG_ESW_VLAN_VAWD1_IVL_MAC |
  584. + REG_ESW_VLAN_VAWD1_VTAG_EN | (member << 16) |
  585. + REG_ESW_VLAN_VAWD1_VALID);
  586. + else
  587. + mt7530_w32(priv, REG_ESW_VLAN_VAWD1, 0);
  588. +
  589. + /* egress mode */
  590. + val = 0;
  591. + for (j = 0; j < MT7530_NUM_PORTS; j++) {
  592. + if (etags & BIT(j))
  593. + val |= ETAG_CTRL_TAG << (j * 2);
  594. + else
  595. + val |= ETAG_CTRL_UNTAG << (j * 2);
  596. + }
  597. + mt7530_w32(priv, REG_ESW_VLAN_VAWD2, val);
  598. +
  599. + /* write to vlan table */
  600. + mt7530_vtcr(priv, 1, i);
  601. + }
  602. +
  603. + /* Port Default PVID */
  604. + for (i = 0; i < MT7530_NUM_PORTS; i++) {
  605. + u32 val;
  606. + val = mt7530_r32(priv, REG_ESW_PORT_PPBV1(i));
  607. + val &= ~0xfff;
  608. + val |= priv->port_entries[i].pvid;
  609. + mt7530_w32(priv, REG_ESW_PORT_PPBV1(i), val);
  610. + }
  611. +
  612. + return 0;
  613. +}
  614. +
  615. +static int
  616. +mt7530_get_port_link(struct switch_dev *dev, int port,
  617. + struct switch_port_link *link)
  618. +{
  619. + struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
  620. + u32 speed, pmsr;
  621. +
  622. + if (port < 0 || port >= MT7530_NUM_PORTS)
  623. + return -EINVAL;
  624. +
  625. + pmsr = mt7530_r32(priv, 0x3008 + (0x100 * port));
  626. +
  627. + link->link = pmsr & 1;
  628. + link->duplex = (pmsr >> 1) & 1;
  629. + speed = (pmsr >> 2) & 3;
  630. +
  631. + switch (speed) {
  632. + case 0:
  633. + link->speed = SWITCH_PORT_SPEED_10;
  634. + break;
  635. + case 1:
  636. + link->speed = SWITCH_PORT_SPEED_100;
  637. + break;
  638. + case 2:
  639. + case 3: /* forced gige speed can be 2 or 3 */
  640. + link->speed = SWITCH_PORT_SPEED_1000;
  641. + break;
  642. + default:
  643. + link->speed = SWITCH_PORT_SPEED_UNKNOWN;
  644. + break;
  645. + }
  646. +
  647. + return 0;
  648. +}
  649. +
  650. +static const struct switch_attr mt7530_global[] = {
  651. + {
  652. + .type = SWITCH_TYPE_INT,
  653. + .name = "enable_vlan",
  654. + .description = "VLAN mode (1:enabled)",
  655. + .max = 1,
  656. + .id = MT7530_ATTR_ENABLE_VLAN,
  657. + .get = mt7530_get_vlan_enable,
  658. + .set = mt7530_set_vlan_enable,
  659. + },
  660. +};
  661. +
  662. +static u64 get_mib_counter(struct mt7530_priv *priv, int i, int port)
  663. +{
  664. + unsigned int port_base;
  665. + u64 t;
  666. +
  667. + port_base = MT7621_MIB_COUNTER_BASE +
  668. + MT7621_MIB_COUNTER_PORT_OFFSET * port;
  669. +
  670. + t = mt7530_r32(priv, port_base + mt7621_mibs[i].offset);
  671. + if (mt7621_mibs[i].size == 2) {
  672. + u64 hi;
  673. +
  674. + hi = mt7530_r32(priv, port_base + mt7621_mibs[i].offset + 4);
  675. + t |= hi << 32;
  676. + }
  677. +
  678. + return t;
  679. +}
  680. +
  681. +static int mt7621_sw_get_port_mib(struct switch_dev *dev,
  682. + const struct switch_attr *attr,
  683. + struct switch_val *val)
  684. +{
  685. + static char buf[4096];
  686. + struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
  687. + int i, len = 0;
  688. +
  689. + if (val->port_vlan >= MT7530_NUM_PORTS)
  690. + return -EINVAL;
  691. +
  692. + len += snprintf(buf + len, sizeof(buf) - len,
  693. + "Port %d MIB counters\n", val->port_vlan);
  694. +
  695. + for (i = 0; i < sizeof(mt7621_mibs) / sizeof(*mt7621_mibs); ++i) {
  696. + u64 counter;
  697. + len += snprintf(buf + len, sizeof(buf) - len,
  698. + "%-11s: ", mt7621_mibs[i].name);
  699. + counter = get_mib_counter(priv, i, val->port_vlan);
  700. + len += snprintf(buf + len, sizeof(buf) - len, "%llu\n",
  701. + counter);
  702. + }
  703. +
  704. + val->value.s = buf;
  705. + val->len = len;
  706. + return 0;
  707. +}
  708. +
  709. +static const struct switch_attr mt7621_port[] = {
  710. + {
  711. + .type = SWITCH_TYPE_STRING,
  712. + .name = "mib",
  713. + .description = "Get MIB counters for port",
  714. + .get = mt7621_sw_get_port_mib,
  715. + .set = NULL,
  716. + },
  717. +};
  718. +
  719. +static const struct switch_attr mt7530_port[] = {
  720. +};
  721. +
  722. +static const struct switch_attr mt7530_vlan[] = {
  723. + {
  724. + .type = SWITCH_TYPE_INT,
  725. + .name = "vid",
  726. + .description = "VLAN ID (0-4094)",
  727. + .set = mt7530_set_vid,
  728. + .get = mt7530_get_vid,
  729. + .max = 4094,
  730. + },
  731. +};
  732. +
  733. +static const struct switch_dev_ops mt7621_ops = {
  734. + .attr_global = {
  735. + .attr = mt7530_global,
  736. + .n_attr = ARRAY_SIZE(mt7530_global),
  737. + },
  738. + .attr_port = {
  739. + .attr = mt7621_port,
  740. + .n_attr = ARRAY_SIZE(mt7621_port),
  741. + },
  742. + .attr_vlan = {
  743. + .attr = mt7530_vlan,
  744. + .n_attr = ARRAY_SIZE(mt7530_vlan),
  745. + },
  746. + .get_vlan_ports = mt7530_get_vlan_ports,
  747. + .set_vlan_ports = mt7530_set_vlan_ports,
  748. + .get_port_pvid = mt7530_get_port_pvid,
  749. + .set_port_pvid = mt7530_set_port_pvid,
  750. + .get_port_link = mt7530_get_port_link,
  751. + .apply_config = mt7530_apply_config,
  752. + .reset_switch = mt7530_reset_switch,
  753. +};
  754. +
  755. +static const struct switch_dev_ops mt7530_ops = {
  756. + .attr_global = {
  757. + .attr = mt7530_global,
  758. + .n_attr = ARRAY_SIZE(mt7530_global),
  759. + },
  760. + .attr_port = {
  761. + .attr = mt7530_port,
  762. + .n_attr = ARRAY_SIZE(mt7530_port),
  763. + },
  764. + .attr_vlan = {
  765. + .attr = mt7530_vlan,
  766. + .n_attr = ARRAY_SIZE(mt7530_vlan),
  767. + },
  768. + .get_vlan_ports = mt7530_get_vlan_ports,
  769. + .set_vlan_ports = mt7530_set_vlan_ports,
  770. + .get_port_pvid = mt7530_get_port_pvid,
  771. + .set_port_pvid = mt7530_set_port_pvid,
  772. + .get_port_link = mt7530_get_port_link,
  773. + .apply_config = mt7530_apply_config,
  774. + .reset_switch = mt7530_reset_switch,
  775. +};
  776. +
  777. +int
  778. +mt7530_probe(struct device *dev, void __iomem *base, struct mii_bus *bus, int vlan)
  779. +{
  780. + struct switch_dev *swdev;
  781. + struct mt7530_priv *mt7530;
  782. + struct mt7530_mapping *map;
  783. + int ret;
  784. +
  785. + mt7530 = devm_kzalloc(dev, sizeof(struct mt7530_priv), GFP_KERNEL);
  786. + if (!mt7530)
  787. + return -ENOMEM;
  788. +
  789. + mt7530->base = base;
  790. + mt7530->bus = bus;
  791. + mt7530->global_vlan_enable = vlan;
  792. +
  793. + swdev = &mt7530->swdev;
  794. + if (bus) {
  795. + swdev->alias = "mt7530";
  796. + swdev->name = "mt7530";
  797. + } else if (IS_ENABLED(CONFIG_SOC_MT7621)) {
  798. + swdev->alias = "mt7621";
  799. + swdev->name = "mt7621";
  800. + } else {
  801. + swdev->alias = "mt7620";
  802. + swdev->name = "mt7620";
  803. + }
  804. + swdev->cpu_port = MT7530_CPU_PORT;
  805. + swdev->ports = MT7530_NUM_PORTS;
  806. + swdev->vlans = MT7530_NUM_VLANS;
  807. + if (IS_ENABLED(CONFIG_SOC_MT7621))
  808. + swdev->ops = &mt7621_ops;
  809. + else
  810. + swdev->ops = &mt7530_ops;
  811. +
  812. + ret = register_switch(swdev, NULL);
  813. + if (ret) {
  814. + dev_err(dev, "failed to register mt7530\n");
  815. + return ret;
  816. + }
  817. +
  818. +
  819. + map = mt7530_find_mapping(dev->of_node);
  820. + if (map)
  821. + mt7530_apply_mapping(mt7530, map);
  822. + mt7530_apply_config(swdev);
  823. +
  824. + /* magic vodoo */
  825. + if (!IS_ENABLED(CONFIG_SOC_MT7621) && bus && mt7530_r32(mt7530, REG_HWTRAP) != 0x1117edf) {
  826. + dev_info(dev, "fixing up MHWTRAP register - bootloader probably played with it\n");
  827. + mt7530_w32(mt7530, REG_HWTRAP, 0x1117edf);
  828. + }
  829. + dev_info(dev, "loaded %s driver\n", swdev->name);
  830. +
  831. + return 0;
  832. +}
  833. --- /dev/null
  834. +++ b/drivers/net/ethernet/mediatek/mt7530.h
  835. @@ -0,0 +1,20 @@
  836. +/*
  837. + * This program is free software; you can redistribute it and/or
  838. + * modify it under the terms of the GNU General Public License
  839. + * as published by the Free Software Foundation; either version 2
  840. + * of the License, or (at your option) any later version.
  841. + *
  842. + * This program is distributed in the hope that it will be useful,
  843. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  844. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  845. + * GNU General Public License for more details.
  846. + *
  847. + * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
  848. + */
  849. +
  850. +#ifndef _MT7530_H__
  851. +#define _MT7530_H__
  852. +
  853. +int mt7530_probe(struct device *dev, void __iomem *base, struct mii_bus *bus, int vlan);
  854. +
  855. +#endif
  856. --- a/drivers/net/ethernet/mediatek/mtk_eth_soc.c
  857. +++ b/drivers/net/ethernet/mediatek/mtk_eth_soc.c
  858. @@ -1291,8 +1291,13 @@ static int __init fe_init(struct net_dev
  859. }
  860. err = fe_hw_init(dev);
  861. - if (!err)
  862. - return 0;
  863. + if (err)
  864. + goto err_phy_disconnect;
  865. +
  866. + if ((priv->flags & FE_FLAG_HAS_SWITCH) && priv->soc->switch_config)
  867. + priv->soc->switch_config(priv);
  868. +
  869. + return 0;
  870. err_phy_disconnect:
  871. if (priv->phy)
  872. --- a/drivers/net/ethernet/mediatek/mtk_eth_soc.h
  873. +++ b/drivers/net/ethernet/mediatek/mtk_eth_soc.h
  874. @@ -383,6 +383,7 @@ struct fe_soc_data {
  875. int (*fwd_config)(struct fe_priv *priv);
  876. void (*tx_dma)(struct fe_tx_dma *txd);
  877. int (*switch_init)(struct fe_priv *priv);
  878. + int (*switch_config)(struct fe_priv *priv);
  879. void (*port_init)(struct fe_priv *priv, struct device_node *port);
  880. int (*has_carrier)(struct fe_priv *priv);
  881. int (*mdio_init)(struct fe_priv *priv);
  882. --- a/drivers/net/ethernet/mediatek/soc_mt7620.c
  883. +++ b/drivers/net/ethernet/mediatek/soc_mt7620.c
  884. @@ -313,6 +313,7 @@ static struct fe_soc_data mt7620_data =
  885. .fwd_config = mt7620_fwd_config,
  886. .tx_dma = mt7620_tx_dma,
  887. .switch_init = mtk_gsw_init,
  888. + .switch_config = mt7620_gsw_config,
  889. .port_init = mt7620_port_init,
  890. .reg_table = mt7620_reg_table,
  891. .pdma_glo_cfg = FE_PDMA_SIZE_16DWORDS,