2
0

mt7530.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  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 <[email protected]>
  13. * Copyright (C) 2016 Vitaly Chekryzhev <[email protected]>
  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. #include <linux/of.h>
  34. #include <asm/byteorder.h>
  35. #include "mt7530.h"
  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. #define MT7530_NUM_ARL_RECORDS 2048
  42. #define ARL_LINE_LENGTH 30
  43. #define MT7530_PORT_MIB_TXB_ID 2 /* TxGOC */
  44. #define MT7530_PORT_MIB_RXB_ID 6 /* RxGOC */
  45. /* registers */
  46. #define REG_ESW_WT_MAC_MFC 0x10
  47. #define REG_ESW_WT_MAC_MFC_MIRROR_ENABLE BIT(3)
  48. #define REG_ESW_WT_MAC_MFC_MIRROR_DEST_MASK 0x07
  49. #define REG_ESW_VLAN_VTCR 0x90
  50. #define REG_ESW_VLAN_VAWD1 0x94
  51. #define REG_ESW_VLAN_VAWD2 0x98
  52. #define REG_ESW_VLAN_VTIM(x) (0x100 + 4 * ((x) / 2))
  53. #define REG_ESW_WT_MAC_ATC 0x80
  54. #define REG_ESW_TABLE_ATRD 0x8C
  55. #define REG_ESW_TABLE_TSRA1 0x84
  56. #define REG_ESW_TABLE_TSRA2 0x88
  57. #define REG_MAC_ATC_START 0x8004
  58. #define REG_MAC_ATC_NEXT 0x8005
  59. #define REG_MAC_ATC_BUSY 0x8000U
  60. #define REG_MAC_ATC_SRCH_HIT 0x2000U
  61. #define REG_MAC_ATC_SRCH_END 0x4000U
  62. #define REG_ATRD_VALID 0xff000000U
  63. #define REG_ATRD_PORT_MASK 0xff0U
  64. #define REG_ESW_VLAN_VAWD1_IVL_MAC BIT(30)
  65. #define REG_ESW_VLAN_VAWD1_VTAG_EN BIT(28)
  66. #define REG_ESW_VLAN_VAWD1_VALID BIT(0)
  67. /* vlan egress mode */
  68. enum {
  69. ETAG_CTRL_UNTAG = 0,
  70. ETAG_CTRL_TAG = 2,
  71. ETAG_CTRL_SWAP = 1,
  72. ETAG_CTRL_STACK = 3,
  73. };
  74. #define REG_ESW_PORT_PCR(x) (0x2004 | ((x) << 8))
  75. #define REG_ESW_PORT_PVC(x) (0x2010 | ((x) << 8))
  76. #define REG_ESW_PORT_PPBV1(x) (0x2014 | ((x) << 8))
  77. #define REG_ESW_PORT_PCR_MIRROR_SRC_RX_BIT BIT(8)
  78. #define REG_ESW_PORT_PCR_MIRROR_SRC_TX_BIT BIT(9)
  79. #define REG_ESW_PORT_PCR_MIRROR_SRC_RX_MASK 0x0100
  80. #define REG_ESW_PORT_PCR_MIRROR_SRC_TX_MASK 0x0200
  81. #define REG_HWTRAP 0x7804
  82. #define MIB_DESC(_s , _o, _n) \
  83. { \
  84. .size = (_s), \
  85. .offset = (_o), \
  86. .name = (_n), \
  87. }
  88. struct mt7xxx_mib_desc {
  89. unsigned int size;
  90. unsigned int offset;
  91. const char *name;
  92. };
  93. static const struct mt7xxx_mib_desc mt7620_mibs[] = {
  94. MIB_DESC(1, MT7620_MIB_STATS_PPE_AC_BCNT0, "PPE_AC_BCNT0"),
  95. MIB_DESC(1, MT7620_MIB_STATS_PPE_AC_PCNT0, "PPE_AC_PCNT0"),
  96. MIB_DESC(1, MT7620_MIB_STATS_PPE_AC_BCNT63, "PPE_AC_BCNT63"),
  97. MIB_DESC(1, MT7620_MIB_STATS_PPE_AC_PCNT63, "PPE_AC_PCNT63"),
  98. MIB_DESC(1, MT7620_MIB_STATS_PPE_MTR_CNT0, "PPE_MTR_CNT0"),
  99. MIB_DESC(1, MT7620_MIB_STATS_PPE_MTR_CNT63, "PPE_MTR_CNT63"),
  100. MIB_DESC(1, MT7620_MIB_STATS_GDM1_TX_GBCNT, "GDM1_TX_GBCNT"),
  101. MIB_DESC(1, MT7620_MIB_STATS_GDM1_TX_GPCNT, "GDM1_TX_GPCNT"),
  102. MIB_DESC(1, MT7620_MIB_STATS_GDM1_TX_SKIPCNT, "GDM1_TX_SKIPCNT"),
  103. MIB_DESC(1, MT7620_MIB_STATS_GDM1_TX_COLCNT, "GDM1_TX_COLCNT"),
  104. MIB_DESC(1, MT7620_MIB_STATS_GDM1_RX_GBCNT1, "GDM1_RX_GBCNT1"),
  105. MIB_DESC(1, MT7620_MIB_STATS_GDM1_RX_GPCNT1, "GDM1_RX_GPCNT1"),
  106. MIB_DESC(1, MT7620_MIB_STATS_GDM1_RX_OERCNT, "GDM1_RX_OERCNT"),
  107. MIB_DESC(1, MT7620_MIB_STATS_GDM1_RX_FERCNT, "GDM1_RX_FERCNT"),
  108. MIB_DESC(1, MT7620_MIB_STATS_GDM1_RX_SERCNT, "GDM1_RX_SERCNT"),
  109. MIB_DESC(1, MT7620_MIB_STATS_GDM1_RX_LERCNT, "GDM1_RX_LERCNT"),
  110. MIB_DESC(1, MT7620_MIB_STATS_GDM1_RX_CERCNT, "GDM1_RX_CERCNT"),
  111. MIB_DESC(1, MT7620_MIB_STATS_GDM1_RX_FCCNT, "GDM1_RX_FCCNT"),
  112. MIB_DESC(1, MT7620_MIB_STATS_GDM2_TX_GBCNT, "GDM2_TX_GBCNT"),
  113. MIB_DESC(1, MT7620_MIB_STATS_GDM2_TX_GPCNT, "GDM2_TX_GPCNT"),
  114. MIB_DESC(1, MT7620_MIB_STATS_GDM2_TX_SKIPCNT, "GDM2_TX_SKIPCNT"),
  115. MIB_DESC(1, MT7620_MIB_STATS_GDM2_TX_COLCNT, "GDM2_TX_COLCNT"),
  116. MIB_DESC(1, MT7620_MIB_STATS_GDM2_RX_GBCNT, "GDM2_RX_GBCNT"),
  117. MIB_DESC(1, MT7620_MIB_STATS_GDM2_RX_GPCNT, "GDM2_RX_GPCNT"),
  118. MIB_DESC(1, MT7620_MIB_STATS_GDM2_RX_OERCNT, "GDM2_RX_OERCNT"),
  119. MIB_DESC(1, MT7620_MIB_STATS_GDM2_RX_FERCNT, "GDM2_RX_FERCNT"),
  120. MIB_DESC(1, MT7620_MIB_STATS_GDM2_RX_SERCNT, "GDM2_RX_SERCNT"),
  121. MIB_DESC(1, MT7620_MIB_STATS_GDM2_RX_LERCNT, "GDM2_RX_LERCNT"),
  122. MIB_DESC(1, MT7620_MIB_STATS_GDM2_RX_CERCNT, "GDM2_RX_CERCNT"),
  123. MIB_DESC(1, MT7620_MIB_STATS_GDM2_RX_FCCNT, "GDM2_RX_FCCNT")
  124. };
  125. static const struct mt7xxx_mib_desc mt7620_port_mibs[] = {
  126. MIB_DESC(1, MT7620_MIB_STATS_PORT_TGPCN, "TxGPC"),
  127. MIB_DESC(1, MT7620_MIB_STATS_PORT_TBOCN, "TxBOC"),
  128. MIB_DESC(1, MT7620_MIB_STATS_PORT_TGOCN, "TxGOC"),
  129. MIB_DESC(1, MT7620_MIB_STATS_PORT_TEPCN, "TxEPC"),
  130. MIB_DESC(1, MT7620_MIB_STATS_PORT_RGPCN, "RxGPC"),
  131. MIB_DESC(1, MT7620_MIB_STATS_PORT_RBOCN, "RxBOC"),
  132. MIB_DESC(1, MT7620_MIB_STATS_PORT_RGOCN, "RxGOC"),
  133. MIB_DESC(1, MT7620_MIB_STATS_PORT_REPC1N, "RxEPC1"),
  134. MIB_DESC(1, MT7620_MIB_STATS_PORT_REPC2N, "RxEPC2")
  135. };
  136. enum {
  137. /* Global attributes. */
  138. MT7530_ATTR_ENABLE_VLAN,
  139. };
  140. struct mt7530_port_entry {
  141. u16 pvid;
  142. bool mirror_rx;
  143. bool mirror_tx;
  144. };
  145. struct mt7530_vlan_entry {
  146. u16 vid;
  147. u8 member;
  148. u8 etags;
  149. };
  150. struct mt7530_priv {
  151. void __iomem *base;
  152. struct mii_bus *bus;
  153. struct switch_dev swdev;
  154. u8 mirror_dest_port;
  155. bool global_vlan_enable;
  156. struct mt7530_vlan_entry vlan_entries[MT7530_NUM_VLANS];
  157. struct mt7530_port_entry port_entries[MT7530_NUM_PORTS];
  158. char arl_buf[MT7530_NUM_ARL_RECORDS * ARL_LINE_LENGTH + 1];
  159. };
  160. struct mt7530_mapping {
  161. char *name;
  162. u16 pvids[MT7530_NUM_PORTS];
  163. u8 members[MT7530_NUM_VLANS];
  164. u8 etags[MT7530_NUM_VLANS];
  165. u16 vids[MT7530_NUM_VLANS];
  166. } mt7530_defaults[] = {
  167. {
  168. .name = "llllw",
  169. .pvids = { 1, 1, 1, 1, 2, 1, 1 },
  170. .members = { 0, 0x6f, 0x50 },
  171. .etags = { 0, 0x40, 0x40 },
  172. .vids = { 0, 1, 2 },
  173. }, {
  174. .name = "wllll",
  175. .pvids = { 2, 1, 1, 1, 1, 1, 1 },
  176. .members = { 0, 0x7e, 0x41 },
  177. .etags = { 0, 0x40, 0x40 },
  178. .vids = { 0, 1, 2 },
  179. }, {
  180. .name = "lwlll",
  181. .pvids = { 1, 2, 1, 1, 1, 1, 1 },
  182. .members = { 0, 0x7d, 0x42 },
  183. .etags = { 0, 0x40, 0x40 },
  184. .vids = { 0, 1, 2 },
  185. },
  186. };
  187. static struct mt7530_mapping*
  188. mt7530_find_mapping(struct device_node *np)
  189. {
  190. const char *map;
  191. int i;
  192. if (of_property_read_string(np, "mediatek,portmap", &map))
  193. return NULL;
  194. for (i = 0; i < ARRAY_SIZE(mt7530_defaults); i++)
  195. if (!strcmp(map, mt7530_defaults[i].name))
  196. return &mt7530_defaults[i];
  197. return NULL;
  198. }
  199. static void
  200. mt7530_apply_mapping(struct mt7530_priv *mt7530, struct mt7530_mapping *map)
  201. {
  202. int i = 0;
  203. for (i = 0; i < MT7530_NUM_PORTS; i++)
  204. mt7530->port_entries[i].pvid = map->pvids[i];
  205. for (i = 0; i < MT7530_NUM_VLANS; i++) {
  206. mt7530->vlan_entries[i].member = map->members[i];
  207. mt7530->vlan_entries[i].etags = map->etags[i];
  208. mt7530->vlan_entries[i].vid = map->vids[i];
  209. }
  210. }
  211. static int
  212. mt7530_reset_switch(struct switch_dev *dev)
  213. {
  214. struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
  215. int i;
  216. memset(priv->port_entries, 0, sizeof(priv->port_entries));
  217. memset(priv->vlan_entries, 0, sizeof(priv->vlan_entries));
  218. /* set default vid of each vlan to the same number of vlan, so the vid
  219. * won't need be set explicitly.
  220. */
  221. for (i = 0; i < MT7530_NUM_VLANS; i++) {
  222. priv->vlan_entries[i].vid = i;
  223. }
  224. return 0;
  225. }
  226. static int
  227. mt7530_get_vlan_enable(struct switch_dev *dev,
  228. const struct switch_attr *attr,
  229. struct switch_val *val)
  230. {
  231. struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
  232. val->value.i = priv->global_vlan_enable;
  233. return 0;
  234. }
  235. static int
  236. mt7530_set_vlan_enable(struct switch_dev *dev,
  237. const struct switch_attr *attr,
  238. struct switch_val *val)
  239. {
  240. struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
  241. priv->global_vlan_enable = val->value.i != 0;
  242. return 0;
  243. }
  244. static u32
  245. mt7530_r32(struct mt7530_priv *priv, u32 reg)
  246. {
  247. u32 val;
  248. if (priv->bus) {
  249. u16 high, low;
  250. mutex_lock(&priv->bus->mdio_lock);
  251. __mdiobus_write(priv->bus, 0x1f, 0x1f, (reg >> 6) & 0x3ff);
  252. low = __mdiobus_read(priv->bus, 0x1f, (reg >> 2) & 0xf);
  253. high = __mdiobus_read(priv->bus, 0x1f, 0x10);
  254. mutex_unlock(&priv->bus->mdio_lock);
  255. return (high << 16) | (low & 0xffff);
  256. }
  257. val = ioread32(priv->base + reg);
  258. pr_debug("MT7530 MDIO Read [%04x]=%08x\n", reg, val);
  259. return val;
  260. }
  261. static void
  262. mt7530_w32(struct mt7530_priv *priv, u32 reg, u32 val)
  263. {
  264. if (priv->bus) {
  265. mutex_lock(&priv->bus->mdio_lock);
  266. __mdiobus_write(priv->bus, 0x1f, 0x1f, (reg >> 6) & 0x3ff);
  267. __mdiobus_write(priv->bus, 0x1f, (reg >> 2) & 0xf, val & 0xffff);
  268. __mdiobus_write(priv->bus, 0x1f, 0x10, val >> 16);
  269. mutex_unlock(&priv->bus->mdio_lock);
  270. return;
  271. }
  272. pr_debug("MT7530 MDIO Write[%04x]=%08x\n", reg, val);
  273. iowrite32(val, priv->base + reg);
  274. }
  275. static void
  276. mt7530_vtcr(struct mt7530_priv *priv, u32 cmd, u32 val)
  277. {
  278. int i;
  279. mt7530_w32(priv, REG_ESW_VLAN_VTCR, BIT(31) | (cmd << 12) | val);
  280. for (i = 0; i < 20; i++) {
  281. u32 val = mt7530_r32(priv, REG_ESW_VLAN_VTCR);
  282. if ((val & BIT(31)) == 0)
  283. break;
  284. udelay(1000);
  285. }
  286. if (i == 20)
  287. printk("mt7530: vtcr timeout\n");
  288. }
  289. static int
  290. mt7530_get_port_pvid(struct switch_dev *dev, int port, int *val)
  291. {
  292. struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
  293. if (port >= MT7530_NUM_PORTS)
  294. return -EINVAL;
  295. *val = mt7530_r32(priv, REG_ESW_PORT_PPBV1(port));
  296. *val &= 0xfff;
  297. return 0;
  298. }
  299. static int
  300. mt7530_set_port_pvid(struct switch_dev *dev, int port, int pvid)
  301. {
  302. struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
  303. if (port >= MT7530_NUM_PORTS)
  304. return -EINVAL;
  305. if (pvid < MT7530_MIN_VID || pvid > MT7530_MAX_VID)
  306. return -EINVAL;
  307. priv->port_entries[port].pvid = pvid;
  308. return 0;
  309. }
  310. static int
  311. mt7530_get_vlan_ports(struct switch_dev *dev, struct switch_val *val)
  312. {
  313. struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
  314. u32 member;
  315. u32 etags;
  316. int i;
  317. val->len = 0;
  318. if (val->port_vlan < 0 || val->port_vlan >= MT7530_NUM_VLANS)
  319. return -EINVAL;
  320. mt7530_vtcr(priv, 0, val->port_vlan);
  321. member = mt7530_r32(priv, REG_ESW_VLAN_VAWD1);
  322. member >>= 16;
  323. member &= 0xff;
  324. etags = mt7530_r32(priv, REG_ESW_VLAN_VAWD2);
  325. for (i = 0; i < MT7530_NUM_PORTS; i++) {
  326. struct switch_port *p;
  327. int etag;
  328. if (!(member & BIT(i)))
  329. continue;
  330. p = &val->value.ports[val->len++];
  331. p->id = i;
  332. etag = (etags >> (i * 2)) & 0x3;
  333. if (etag == ETAG_CTRL_TAG)
  334. p->flags |= BIT(SWITCH_PORT_FLAG_TAGGED);
  335. else if (etag != ETAG_CTRL_UNTAG)
  336. printk("vlan %d port %d egress tag control neither untag nor tag: %d.\n",
  337. val->port_vlan, i, etag);
  338. }
  339. return 0;
  340. }
  341. static int
  342. mt7530_set_vlan_ports(struct switch_dev *dev, struct switch_val *val)
  343. {
  344. struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
  345. u8 member = 0;
  346. u8 etags = 0;
  347. int i;
  348. if (val->port_vlan < 0 || val->port_vlan >= MT7530_NUM_VLANS ||
  349. val->len > MT7530_NUM_PORTS)
  350. return -EINVAL;
  351. for (i = 0; i < val->len; i++) {
  352. struct switch_port *p = &val->value.ports[i];
  353. if (p->id >= MT7530_NUM_PORTS)
  354. return -EINVAL;
  355. member |= BIT(p->id);
  356. if (p->flags & BIT(SWITCH_PORT_FLAG_TAGGED))
  357. etags |= BIT(p->id);
  358. }
  359. priv->vlan_entries[val->port_vlan].member = member;
  360. priv->vlan_entries[val->port_vlan].etags = etags;
  361. return 0;
  362. }
  363. static int
  364. mt7530_set_vid(struct switch_dev *dev, const struct switch_attr *attr,
  365. struct switch_val *val)
  366. {
  367. struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
  368. int vlan;
  369. u16 vid;
  370. vlan = val->port_vlan;
  371. vid = (u16)val->value.i;
  372. if (vlan < 0 || vlan >= MT7530_NUM_VLANS)
  373. return -EINVAL;
  374. if (vid < MT7530_MIN_VID || vid > MT7530_MAX_VID)
  375. return -EINVAL;
  376. priv->vlan_entries[vlan].vid = vid;
  377. return 0;
  378. }
  379. static int
  380. mt7530_get_vid(struct switch_dev *dev, const struct switch_attr *attr,
  381. struct switch_val *val)
  382. {
  383. struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
  384. u32 vid;
  385. int vlan;
  386. vlan = val->port_vlan;
  387. vid = mt7530_r32(priv, REG_ESW_VLAN_VTIM(vlan));
  388. if (vlan & 1)
  389. vid = vid >> 12;
  390. vid &= 0xfff;
  391. val->value.i = vid;
  392. return 0;
  393. }
  394. static int
  395. mt7530_get_mirror_monitor_port(struct switch_dev *dev, const struct switch_attr *attr,
  396. struct switch_val *val)
  397. {
  398. struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
  399. val->value.i = priv->mirror_dest_port;
  400. return 0;
  401. }
  402. static int
  403. mt7530_set_mirror_monitor_port(struct switch_dev *dev, const struct switch_attr *attr,
  404. struct switch_val *val)
  405. {
  406. struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
  407. priv->mirror_dest_port = val->value.i;
  408. return 0;
  409. }
  410. static int
  411. mt7530_get_port_mirror_rx(struct switch_dev *dev, const struct switch_attr *attr,
  412. struct switch_val *val)
  413. {
  414. struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
  415. val->value.i = priv->port_entries[val->port_vlan].mirror_rx;
  416. return 0;
  417. }
  418. static int
  419. mt7530_set_port_mirror_rx(struct switch_dev *dev, const struct switch_attr *attr,
  420. struct switch_val *val)
  421. {
  422. struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
  423. priv->port_entries[val->port_vlan].mirror_rx = val->value.i;
  424. return 0;
  425. }
  426. static int
  427. mt7530_get_port_mirror_tx(struct switch_dev *dev, const struct switch_attr *attr,
  428. struct switch_val *val)
  429. {
  430. struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
  431. val->value.i = priv->port_entries[val->port_vlan].mirror_tx;
  432. return 0;
  433. }
  434. static int
  435. mt7530_set_port_mirror_tx(struct switch_dev *dev, const struct switch_attr *attr,
  436. struct switch_val *val)
  437. {
  438. struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
  439. priv->port_entries[val->port_vlan].mirror_tx = val->value.i;
  440. return 0;
  441. }
  442. static void
  443. mt7530_write_vlan_entry(struct mt7530_priv *priv, int vlan, u16 vid,
  444. u8 ports, u8 etags)
  445. {
  446. int port;
  447. u32 val;
  448. /* vid of vlan */
  449. val = mt7530_r32(priv, REG_ESW_VLAN_VTIM(vlan));
  450. if (vlan % 2 == 0) {
  451. val &= 0xfff000;
  452. val |= vid;
  453. } else {
  454. val &= 0xfff;
  455. val |= (vid << 12);
  456. }
  457. mt7530_w32(priv, REG_ESW_VLAN_VTIM(vlan), val);
  458. /* vlan port membership */
  459. if (ports)
  460. mt7530_w32(priv, REG_ESW_VLAN_VAWD1, REG_ESW_VLAN_VAWD1_IVL_MAC |
  461. REG_ESW_VLAN_VAWD1_VTAG_EN | (ports << 16) |
  462. REG_ESW_VLAN_VAWD1_VALID);
  463. else
  464. mt7530_w32(priv, REG_ESW_VLAN_VAWD1, 0);
  465. /* egress mode */
  466. val = 0;
  467. for (port = 0; port < MT7530_NUM_PORTS; port++) {
  468. if (etags & BIT(port))
  469. val |= ETAG_CTRL_TAG << (port * 2);
  470. else
  471. val |= ETAG_CTRL_UNTAG << (port * 2);
  472. }
  473. mt7530_w32(priv, REG_ESW_VLAN_VAWD2, val);
  474. /* write to vlan table */
  475. mt7530_vtcr(priv, 1, vlan);
  476. }
  477. static int
  478. mt7530_apply_config(struct switch_dev *dev)
  479. {
  480. struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
  481. int i, j;
  482. u8 tag_ports;
  483. u8 untag_ports;
  484. bool is_mirror = false;
  485. if (!priv->global_vlan_enable) {
  486. for (i = 0; i < MT7530_NUM_PORTS; i++)
  487. mt7530_w32(priv, REG_ESW_PORT_PCR(i), 0x00400000);
  488. mt7530_w32(priv, REG_ESW_PORT_PCR(MT7530_CPU_PORT), 0x00ff0000);
  489. for (i = 0; i < MT7530_NUM_PORTS; i++)
  490. mt7530_w32(priv, REG_ESW_PORT_PVC(i), 0x810000c0);
  491. return 0;
  492. }
  493. /* set all ports as security mode */
  494. for (i = 0; i < MT7530_NUM_PORTS; i++)
  495. mt7530_w32(priv, REG_ESW_PORT_PCR(i), 0x00ff0003);
  496. /* check if a port is used in tag/untag vlan egress mode */
  497. tag_ports = 0;
  498. untag_ports = 0;
  499. for (i = 0; i < MT7530_NUM_VLANS; i++) {
  500. u8 member = priv->vlan_entries[i].member;
  501. u8 etags = priv->vlan_entries[i].etags;
  502. if (!member)
  503. continue;
  504. for (j = 0; j < MT7530_NUM_PORTS; j++) {
  505. if (!(member & BIT(j)))
  506. continue;
  507. if (etags & BIT(j))
  508. tag_ports |= 1u << j;
  509. else
  510. untag_ports |= 1u << j;
  511. }
  512. }
  513. /* set all untag-only ports as transparent and the rest as user port */
  514. for (i = 0; i < MT7530_NUM_PORTS; i++) {
  515. u32 pvc_mode = 0x81000000;
  516. if (untag_ports & BIT(i) && !(tag_ports & BIT(i)))
  517. pvc_mode = 0x810000c0;
  518. mt7530_w32(priv, REG_ESW_PORT_PVC(i), pvc_mode);
  519. }
  520. /* first clear the swtich vlan table */
  521. for (i = 0; i < MT7530_NUM_VLANS; i++)
  522. mt7530_write_vlan_entry(priv, i, i, 0, 0);
  523. /* now program only vlans with members to avoid
  524. clobbering remapped entries in later iterations */
  525. for (i = 0; i < MT7530_NUM_VLANS; i++) {
  526. u16 vid = priv->vlan_entries[i].vid;
  527. u8 member = priv->vlan_entries[i].member;
  528. u8 etags = priv->vlan_entries[i].etags;
  529. if (member)
  530. mt7530_write_vlan_entry(priv, i, vid, member, etags);
  531. }
  532. /* Port Default PVID */
  533. for (i = 0; i < MT7530_NUM_PORTS; i++) {
  534. int vlan = priv->port_entries[i].pvid;
  535. u16 pvid = 0;
  536. u32 val;
  537. if (vlan < MT7530_NUM_VLANS && priv->vlan_entries[vlan].member)
  538. pvid = priv->vlan_entries[vlan].vid;
  539. val = mt7530_r32(priv, REG_ESW_PORT_PPBV1(i));
  540. val &= ~0xfff;
  541. val |= pvid;
  542. mt7530_w32(priv, REG_ESW_PORT_PPBV1(i), val);
  543. }
  544. /* set mirroring source port */
  545. for (i = 0; i < MT7530_NUM_PORTS; i++) {
  546. u32 val = mt7530_r32(priv, REG_ESW_PORT_PCR(i));
  547. if (priv->port_entries[i].mirror_rx) {
  548. val |= REG_ESW_PORT_PCR_MIRROR_SRC_RX_BIT;
  549. is_mirror = true;
  550. }
  551. if (priv->port_entries[i].mirror_tx) {
  552. val |= REG_ESW_PORT_PCR_MIRROR_SRC_TX_BIT;
  553. is_mirror = true;
  554. }
  555. mt7530_w32(priv, REG_ESW_PORT_PCR(i), val);
  556. }
  557. /* set mirroring monitor port */
  558. if (is_mirror) {
  559. u32 val = mt7530_r32(priv, REG_ESW_WT_MAC_MFC);
  560. val |= REG_ESW_WT_MAC_MFC_MIRROR_ENABLE;
  561. val &= ~REG_ESW_WT_MAC_MFC_MIRROR_DEST_MASK;
  562. val |= priv->mirror_dest_port;
  563. mt7530_w32(priv, REG_ESW_WT_MAC_MFC, val);
  564. }
  565. return 0;
  566. }
  567. static int
  568. mt7530_get_port_link(struct switch_dev *dev, int port,
  569. struct switch_port_link *link)
  570. {
  571. struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
  572. u32 speed, pmsr;
  573. if (port < 0 || port >= MT7530_NUM_PORTS)
  574. return -EINVAL;
  575. pmsr = mt7530_r32(priv, 0x3008 + (0x100 * port));
  576. link->link = pmsr & 1;
  577. link->duplex = (pmsr >> 1) & 1;
  578. speed = (pmsr >> 2) & 3;
  579. switch (speed) {
  580. case 0:
  581. link->speed = SWITCH_PORT_SPEED_10;
  582. break;
  583. case 1:
  584. link->speed = SWITCH_PORT_SPEED_100;
  585. break;
  586. case 2:
  587. case 3: /* forced gige speed can be 2 or 3 */
  588. link->speed = SWITCH_PORT_SPEED_1000;
  589. break;
  590. default:
  591. link->speed = SWITCH_PORT_SPEED_UNKNOWN;
  592. break;
  593. }
  594. return 0;
  595. }
  596. static u64 get_mib_counter_7620(struct mt7530_priv *priv, int i)
  597. {
  598. return mt7530_r32(priv, MT7620_MIB_COUNTER_BASE + mt7620_mibs[i].offset);
  599. }
  600. static u64 get_mib_counter_port_7620(struct mt7530_priv *priv, int i, int port)
  601. {
  602. return mt7530_r32(priv,
  603. MT7620_MIB_COUNTER_BASE_PORT +
  604. (MT7620_MIB_COUNTER_PORT_OFFSET * port) +
  605. mt7620_port_mibs[i].offset);
  606. }
  607. static int mt7530_sw_get_mib(struct switch_dev *dev,
  608. const struct switch_attr *attr,
  609. struct switch_val *val)
  610. {
  611. static char buf[4096];
  612. struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
  613. int i, len = 0;
  614. len += snprintf(buf + len, sizeof(buf) - len, "Switch MIB counters\n");
  615. for (i = 0; i < ARRAY_SIZE(mt7620_mibs); ++i) {
  616. u64 counter;
  617. len += snprintf(buf + len, sizeof(buf) - len,
  618. "%-11s: ", mt7620_mibs[i].name);
  619. counter = get_mib_counter_7620(priv, i);
  620. len += snprintf(buf + len, sizeof(buf) - len, "%llu\n",
  621. counter);
  622. }
  623. val->value.s = buf;
  624. val->len = len;
  625. return 0;
  626. }
  627. static char *mt7530_print_arl_table_row(u32 atrd,
  628. u32 mac1,
  629. u32 mac2,
  630. char *buf,
  631. size_t *size)
  632. {
  633. int ret;
  634. size_t port;
  635. size_t i;
  636. u8 port_map;
  637. u8 mac[ETH_ALEN];
  638. mac1 = ntohl(mac1);
  639. mac2 = ntohl(mac2);
  640. port_map = (u8)((atrd & REG_ATRD_PORT_MASK) >> 4);
  641. memcpy(mac, &mac1, sizeof(mac1));
  642. memcpy(mac + sizeof(mac1), &mac2, sizeof(mac) - sizeof(mac1));
  643. for (port = 0, i = 1; port < MT7530_NUM_PORTS; ++port, i <<= 1) {
  644. if (port_map & i) {
  645. ret = snprintf(buf, *size, "Port %d: MAC %pM\n", port, mac);
  646. if (ret >= *size || ret <= 0) {
  647. *buf = 0;
  648. buf = NULL;
  649. goto out;
  650. }
  651. buf += ret;
  652. *size = *size - ret;
  653. }
  654. }
  655. out:
  656. return buf;
  657. }
  658. static int mt7530_get_arl_table(struct switch_dev *dev,
  659. const struct switch_attr *attr,
  660. struct switch_val *val)
  661. {
  662. struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
  663. char *buf = priv->arl_buf;
  664. size_t size = sizeof(priv->arl_buf);
  665. size_t count = 0;
  666. size_t retry_times = 100;
  667. int ret;
  668. u32 atc;
  669. ret = snprintf(buf, size, "address resolution table\n");
  670. if (ret >= size || ret <= 0) {
  671. priv->arl_buf[0] = 0;
  672. goto out;
  673. }
  674. buf += ret;
  675. size = size - ret;
  676. mt7530_w32(priv, REG_ESW_WT_MAC_ATC, REG_MAC_ATC_START);
  677. do {
  678. atc = mt7530_r32(priv, REG_ESW_WT_MAC_ATC);
  679. if (atc & REG_MAC_ATC_SRCH_HIT && !(atc & REG_MAC_ATC_BUSY)) {
  680. u32 atrd;
  681. ++count;
  682. atrd = mt7530_r32(priv, REG_ESW_TABLE_ATRD);
  683. if (atrd & REG_ATRD_VALID) {
  684. u32 mac1;
  685. u32 mac2;
  686. mac1 = mt7530_r32(priv, REG_ESW_TABLE_TSRA1);
  687. mac2 = mt7530_r32(priv, REG_ESW_TABLE_TSRA2);
  688. if (!(atc & REG_MAC_ATC_SRCH_END))
  689. mt7530_w32(priv, REG_ESW_WT_MAC_ATC, REG_MAC_ATC_NEXT);
  690. buf = mt7530_print_arl_table_row(atrd, mac1, mac2, buf, &size);
  691. if (!buf) {
  692. pr_warn("%s: too many addresses\n", __func__);
  693. goto out;
  694. }
  695. } else if (!(atc & REG_MAC_ATC_SRCH_END)) {
  696. mt7530_w32(priv, REG_ESW_WT_MAC_ATC, REG_MAC_ATC_NEXT);
  697. }
  698. } else {
  699. --retry_times;
  700. usleep_range(1000, 5000);
  701. }
  702. } while (!(atc & REG_MAC_ATC_SRCH_END) &&
  703. count < MT7530_NUM_ARL_RECORDS &&
  704. retry_times > 0);
  705. out:
  706. val->value.s = priv->arl_buf;
  707. val->len = strlen(priv->arl_buf);
  708. return 0;
  709. }
  710. static int mt7530_sw_get_port_mib(struct switch_dev *dev,
  711. const struct switch_attr *attr,
  712. struct switch_val *val)
  713. {
  714. static char buf[4096];
  715. struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
  716. int i, len = 0;
  717. if (val->port_vlan >= MT7530_NUM_PORTS)
  718. return -EINVAL;
  719. len += snprintf(buf + len, sizeof(buf) - len,
  720. "Port %d MIB counters\n", val->port_vlan);
  721. for (i = 0; i < ARRAY_SIZE(mt7620_port_mibs); ++i) {
  722. u64 counter;
  723. len += snprintf(buf + len, sizeof(buf) - len,
  724. "%-11s: ", mt7620_port_mibs[i].name);
  725. counter = get_mib_counter_port_7620(priv, i, val->port_vlan);
  726. len += snprintf(buf + len, sizeof(buf) - len, "%llu\n",
  727. counter);
  728. }
  729. val->value.s = buf;
  730. val->len = len;
  731. return 0;
  732. }
  733. static int mt7530_get_port_stats(struct switch_dev *dev, int port,
  734. struct switch_port_stats *stats)
  735. {
  736. struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
  737. if (port < 0 || port >= MT7530_NUM_PORTS)
  738. return -EINVAL;
  739. stats->tx_bytes = get_mib_counter_port_7620(priv, MT7530_PORT_MIB_TXB_ID, port);
  740. stats->rx_bytes = get_mib_counter_port_7620(priv, MT7530_PORT_MIB_RXB_ID, port);
  741. return 0;
  742. }
  743. static const struct switch_attr mt7530_global[] = {
  744. {
  745. .type = SWITCH_TYPE_INT,
  746. .name = "enable_vlan",
  747. .description = "VLAN mode (1:enabled)",
  748. .max = 1,
  749. .id = MT7530_ATTR_ENABLE_VLAN,
  750. .get = mt7530_get_vlan_enable,
  751. .set = mt7530_set_vlan_enable,
  752. }, {
  753. .type = SWITCH_TYPE_STRING,
  754. .name = "mib",
  755. .description = "Get MIB counters for switch",
  756. .get = mt7530_sw_get_mib,
  757. .set = NULL,
  758. }, {
  759. .type = SWITCH_TYPE_INT,
  760. .name = "mirror_monitor_port",
  761. .description = "Mirror monitor port",
  762. .set = mt7530_set_mirror_monitor_port,
  763. .get = mt7530_get_mirror_monitor_port,
  764. .max = MT7530_NUM_PORTS - 1
  765. },
  766. {
  767. .type = SWITCH_TYPE_STRING,
  768. .name = "arl_table",
  769. .description = "Get ARL table",
  770. .set = NULL,
  771. .get = mt7530_get_arl_table,
  772. },
  773. };
  774. static const struct switch_attr mt7530_port[] = {
  775. {
  776. .type = SWITCH_TYPE_STRING,
  777. .name = "mib",
  778. .description = "Get MIB counters for port",
  779. .get = mt7530_sw_get_port_mib,
  780. .set = NULL,
  781. }, {
  782. .type = SWITCH_TYPE_INT,
  783. .name = "enable_mirror_rx",
  784. .description = "Enable mirroring of RX packets",
  785. .set = mt7530_set_port_mirror_rx,
  786. .get = mt7530_get_port_mirror_rx,
  787. .max = 1,
  788. }, {
  789. .type = SWITCH_TYPE_INT,
  790. .name = "enable_mirror_tx",
  791. .description = "Enable mirroring of TX packets",
  792. .set = mt7530_set_port_mirror_tx,
  793. .get = mt7530_get_port_mirror_tx,
  794. .max = 1,
  795. },
  796. };
  797. static const struct switch_attr mt7530_vlan[] = {
  798. {
  799. .type = SWITCH_TYPE_INT,
  800. .name = "vid",
  801. .description = "VLAN ID (0-4094)",
  802. .set = mt7530_set_vid,
  803. .get = mt7530_get_vid,
  804. .max = 4094,
  805. },
  806. };
  807. static const struct switch_dev_ops mt7530_ops = {
  808. .attr_global = {
  809. .attr = mt7530_global,
  810. .n_attr = ARRAY_SIZE(mt7530_global),
  811. },
  812. .attr_port = {
  813. .attr = mt7530_port,
  814. .n_attr = ARRAY_SIZE(mt7530_port),
  815. },
  816. .attr_vlan = {
  817. .attr = mt7530_vlan,
  818. .n_attr = ARRAY_SIZE(mt7530_vlan),
  819. },
  820. .get_vlan_ports = mt7530_get_vlan_ports,
  821. .set_vlan_ports = mt7530_set_vlan_ports,
  822. .get_port_pvid = mt7530_get_port_pvid,
  823. .set_port_pvid = mt7530_set_port_pvid,
  824. .get_port_link = mt7530_get_port_link,
  825. .get_port_stats = mt7530_get_port_stats,
  826. .apply_config = mt7530_apply_config,
  827. .reset_switch = mt7530_reset_switch,
  828. };
  829. int
  830. mt7530_probe(struct device *dev, void __iomem *base, struct mii_bus *bus, int vlan)
  831. {
  832. struct switch_dev *swdev;
  833. struct mt7530_priv *mt7530;
  834. struct mt7530_mapping *map;
  835. int ret;
  836. mt7530 = devm_kzalloc(dev, sizeof(struct mt7530_priv), GFP_KERNEL);
  837. if (!mt7530)
  838. return -ENOMEM;
  839. mt7530->base = base;
  840. mt7530->bus = bus;
  841. mt7530->global_vlan_enable = vlan;
  842. swdev = &mt7530->swdev;
  843. if (bus) {
  844. swdev->alias = "mt7530";
  845. swdev->name = "mt7530";
  846. } else {
  847. swdev->alias = "mt7620";
  848. swdev->name = "mt7620";
  849. }
  850. swdev->cpu_port = MT7530_CPU_PORT;
  851. swdev->ports = MT7530_NUM_PORTS;
  852. swdev->vlans = MT7530_NUM_VLANS;
  853. swdev->ops = &mt7530_ops;
  854. ret = register_switch(swdev, NULL);
  855. if (ret) {
  856. dev_err(dev, "failed to register mt7530\n");
  857. return ret;
  858. }
  859. map = mt7530_find_mapping(dev->of_node);
  860. if (map)
  861. mt7530_apply_mapping(mt7530, map);
  862. mt7530_apply_config(swdev);
  863. /* magic vodoo */
  864. if (bus && mt7530_r32(mt7530, REG_HWTRAP) != 0x1117edf) {
  865. dev_info(dev, "fixing up MHWTRAP register - bootloader probably played with it\n");
  866. mt7530_w32(mt7530, REG_HWTRAP, 0x1117edf);
  867. }
  868. dev_info(dev, "loaded %s driver\n", swdev->name);
  869. return 0;
  870. }