mt7530.c 25 KB

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