qca8k.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  1. /*
  2. * Copyright (C) 2009 Felix Fietkau <nbd@nbd.name>
  3. * Copyright (C) 2011-2012 Gabor Juhos <juhosg@openwrt.org>
  4. * Copyright (c) 2015, The Linux Foundation. All rights reserved.
  5. * Copyright (c) 2016 John Crispin <john@phrozen.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 and
  9. * only version 2 as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/module.h>
  17. #include <linux/phy.h>
  18. #include <linux/netdevice.h>
  19. #include <net/dsa.h>
  20. #include <net/switchdev.h>
  21. #include <linux/of_net.h>
  22. #include <linux/of_platform.h>
  23. #include <linux/if_bridge.h>
  24. #include <linux/mdio.h>
  25. #include <linux/etherdevice.h>
  26. #include "qca8k.h"
  27. #define MIB_DESC(_s, _o, _n) \
  28. { \
  29. .size = (_s), \
  30. .offset = (_o), \
  31. .name = (_n), \
  32. }
  33. static const struct qca8k_mib_desc ar8327_mib[] = {
  34. MIB_DESC(1, 0x00, "RxBroad"),
  35. MIB_DESC(1, 0x04, "RxPause"),
  36. MIB_DESC(1, 0x08, "RxMulti"),
  37. MIB_DESC(1, 0x0c, "RxFcsErr"),
  38. MIB_DESC(1, 0x10, "RxAlignErr"),
  39. MIB_DESC(1, 0x14, "RxRunt"),
  40. MIB_DESC(1, 0x18, "RxFragment"),
  41. MIB_DESC(1, 0x1c, "Rx64Byte"),
  42. MIB_DESC(1, 0x20, "Rx128Byte"),
  43. MIB_DESC(1, 0x24, "Rx256Byte"),
  44. MIB_DESC(1, 0x28, "Rx512Byte"),
  45. MIB_DESC(1, 0x2c, "Rx1024Byte"),
  46. MIB_DESC(1, 0x30, "Rx1518Byte"),
  47. MIB_DESC(1, 0x34, "RxMaxByte"),
  48. MIB_DESC(1, 0x38, "RxTooLong"),
  49. MIB_DESC(2, 0x3c, "RxGoodByte"),
  50. MIB_DESC(2, 0x44, "RxBadByte"),
  51. MIB_DESC(1, 0x4c, "RxOverFlow"),
  52. MIB_DESC(1, 0x50, "Filtered"),
  53. MIB_DESC(1, 0x54, "TxBroad"),
  54. MIB_DESC(1, 0x58, "TxPause"),
  55. MIB_DESC(1, 0x5c, "TxMulti"),
  56. MIB_DESC(1, 0x60, "TxUnderRun"),
  57. MIB_DESC(1, 0x64, "Tx64Byte"),
  58. MIB_DESC(1, 0x68, "Tx128Byte"),
  59. MIB_DESC(1, 0x6c, "Tx256Byte"),
  60. MIB_DESC(1, 0x70, "Tx512Byte"),
  61. MIB_DESC(1, 0x74, "Tx1024Byte"),
  62. MIB_DESC(1, 0x78, "Tx1518Byte"),
  63. MIB_DESC(1, 0x7c, "TxMaxByte"),
  64. MIB_DESC(1, 0x80, "TxOverSize"),
  65. MIB_DESC(2, 0x84, "TxByte"),
  66. MIB_DESC(1, 0x8c, "TxCollision"),
  67. MIB_DESC(1, 0x90, "TxAbortCol"),
  68. MIB_DESC(1, 0x94, "TxMultiCol"),
  69. MIB_DESC(1, 0x98, "TxSingleCol"),
  70. MIB_DESC(1, 0x9c, "TxExcDefer"),
  71. MIB_DESC(1, 0xa0, "TxDefer"),
  72. MIB_DESC(1, 0xa4, "TxLateCol"),
  73. };
  74. /* The 32bit switch registers are accessed indirectly. To achieve this we need
  75. * to set the page of the register. Track the last page that was set to reduce
  76. * mdio writes
  77. */
  78. static u16 qca8k_current_page = 0xffff;
  79. static void
  80. qca8k_split_addr(u32 regaddr, u16 *r1, u16 *r2, u16 *page)
  81. {
  82. regaddr >>= 1;
  83. *r1 = regaddr & 0x1e;
  84. regaddr >>= 5;
  85. *r2 = regaddr & 0x7;
  86. regaddr >>= 3;
  87. *page = regaddr & 0x3ff;
  88. }
  89. static u32
  90. qca8k_mii_read32(struct mii_bus *bus, int phy_id, u32 regnum)
  91. {
  92. u32 val;
  93. int ret;
  94. ret = bus->read(bus, phy_id, regnum);
  95. if (ret >= 0) {
  96. val = ret;
  97. ret = bus->read(bus, phy_id, regnum + 1);
  98. val |= ret << 16;
  99. }
  100. if (ret < 0) {
  101. dev_err_ratelimited(&bus->dev,
  102. "failed to read qca8k 32bit register\n");
  103. return ret;
  104. }
  105. return val;
  106. }
  107. static void
  108. qca8k_mii_write32(struct mii_bus *bus, int phy_id, u32 regnum, u32 val)
  109. {
  110. u16 lo, hi;
  111. int ret;
  112. lo = val & 0xffff;
  113. hi = (u16)(val >> 16);
  114. ret = bus->write(bus, phy_id, regnum, lo);
  115. if (ret >= 0)
  116. ret = bus->write(bus, phy_id, regnum + 1, hi);
  117. if (ret < 0)
  118. dev_err_ratelimited(&bus->dev,
  119. "failed to write qca8k 32bit register\n");
  120. }
  121. static void
  122. qca8k_set_page(struct mii_bus *bus, u16 page)
  123. {
  124. if (page == qca8k_current_page)
  125. return;
  126. if (bus->write(bus, 0x18, 0, page) < 0)
  127. dev_err_ratelimited(&bus->dev,
  128. "failed to set qca8k page\n");
  129. qca8k_current_page = page;
  130. }
  131. static u32
  132. qca8k_read(struct qca8k_priv *priv, u32 reg)
  133. {
  134. u16 r1, r2, page;
  135. u32 val;
  136. qca8k_split_addr(reg, &r1, &r2, &page);
  137. mutex_lock_nested(&priv->bus->mdio_lock, MDIO_MUTEX_NESTED);
  138. qca8k_set_page(priv->bus, page);
  139. val = qca8k_mii_read32(priv->bus, 0x10 | r2, r1);
  140. mutex_unlock(&priv->bus->mdio_lock);
  141. return val;
  142. }
  143. static void
  144. qca8k_write(struct qca8k_priv *priv, u32 reg, u32 val)
  145. {
  146. u16 r1, r2, page;
  147. qca8k_split_addr(reg, &r1, &r2, &page);
  148. mutex_lock_nested(&priv->bus->mdio_lock, MDIO_MUTEX_NESTED);
  149. qca8k_set_page(priv->bus, page);
  150. qca8k_mii_write32(priv->bus, 0x10 | r2, r1, val);
  151. mutex_unlock(&priv->bus->mdio_lock);
  152. }
  153. static u32
  154. qca8k_rmw(struct qca8k_priv *priv, u32 reg, u32 mask, u32 val)
  155. {
  156. u16 r1, r2, page;
  157. u32 ret;
  158. qca8k_split_addr(reg, &r1, &r2, &page);
  159. mutex_lock_nested(&priv->bus->mdio_lock, MDIO_MUTEX_NESTED);
  160. qca8k_set_page(priv->bus, page);
  161. ret = qca8k_mii_read32(priv->bus, 0x10 | r2, r1);
  162. ret &= ~mask;
  163. ret |= val;
  164. qca8k_mii_write32(priv->bus, 0x10 | r2, r1, ret);
  165. mutex_unlock(&priv->bus->mdio_lock);
  166. return ret;
  167. }
  168. static void
  169. qca8k_reg_set(struct qca8k_priv *priv, u32 reg, u32 val)
  170. {
  171. qca8k_rmw(priv, reg, 0, val);
  172. }
  173. static void
  174. qca8k_reg_clear(struct qca8k_priv *priv, u32 reg, u32 val)
  175. {
  176. qca8k_rmw(priv, reg, val, 0);
  177. }
  178. static int
  179. qca8k_regmap_read(void *ctx, uint32_t reg, uint32_t *val)
  180. {
  181. struct qca8k_priv *priv = (struct qca8k_priv *)ctx;
  182. *val = qca8k_read(priv, reg);
  183. return 0;
  184. }
  185. static int
  186. qca8k_regmap_write(void *ctx, uint32_t reg, uint32_t val)
  187. {
  188. struct qca8k_priv *priv = (struct qca8k_priv *)ctx;
  189. qca8k_write(priv, reg, val);
  190. return 0;
  191. }
  192. static const struct regmap_range qca8k_readable_ranges[] = {
  193. regmap_reg_range(0x0000, 0x00e4), /* Global control */
  194. regmap_reg_range(0x0100, 0x0168), /* EEE control */
  195. regmap_reg_range(0x0200, 0x0270), /* Parser control */
  196. regmap_reg_range(0x0400, 0x0454), /* ACL */
  197. regmap_reg_range(0x0600, 0x0718), /* Lookup */
  198. regmap_reg_range(0x0800, 0x0b70), /* QM */
  199. regmap_reg_range(0x0c00, 0x0c80), /* PKT */
  200. regmap_reg_range(0x0e00, 0x0e98), /* L3 */
  201. regmap_reg_range(0x1000, 0x10ac), /* MIB - Port0 */
  202. regmap_reg_range(0x1100, 0x11ac), /* MIB - Port1 */
  203. regmap_reg_range(0x1200, 0x12ac), /* MIB - Port2 */
  204. regmap_reg_range(0x1300, 0x13ac), /* MIB - Port3 */
  205. regmap_reg_range(0x1400, 0x14ac), /* MIB - Port4 */
  206. regmap_reg_range(0x1500, 0x15ac), /* MIB - Port5 */
  207. regmap_reg_range(0x1600, 0x16ac), /* MIB - Port6 */
  208. };
  209. static struct regmap_access_table qca8k_readable_table = {
  210. .yes_ranges = qca8k_readable_ranges,
  211. .n_yes_ranges = ARRAY_SIZE(qca8k_readable_ranges),
  212. };
  213. static struct regmap_config qca8k_regmap_config = {
  214. .reg_bits = 16,
  215. .val_bits = 32,
  216. .reg_stride = 4,
  217. .max_register = 0x16ac, /* end MIB - Port6 range */
  218. .reg_read = qca8k_regmap_read,
  219. .reg_write = qca8k_regmap_write,
  220. .rd_table = &qca8k_readable_table,
  221. };
  222. static int
  223. qca8k_busy_wait(struct qca8k_priv *priv, u32 reg, u32 mask)
  224. {
  225. unsigned long timeout;
  226. timeout = jiffies + msecs_to_jiffies(20);
  227. /* loop until the busy flag has cleared */
  228. do {
  229. u32 val = qca8k_read(priv, reg);
  230. int busy = val & mask;
  231. if (!busy)
  232. break;
  233. cond_resched();
  234. } while (!time_after_eq(jiffies, timeout));
  235. return time_after_eq(jiffies, timeout);
  236. }
  237. static void
  238. qca8k_fdb_read(struct qca8k_priv *priv, struct qca8k_fdb *fdb)
  239. {
  240. u32 reg[4];
  241. int i;
  242. /* load the ARL table into an array */
  243. for (i = 0; i < 4; i++)
  244. reg[i] = qca8k_read(priv, QCA8K_REG_ATU_DATA0 + (i * 4));
  245. /* vid - 83:72 */
  246. fdb->vid = (reg[2] >> QCA8K_ATU_VID_S) & QCA8K_ATU_VID_M;
  247. /* aging - 67:64 */
  248. fdb->aging = reg[2] & QCA8K_ATU_STATUS_M;
  249. /* portmask - 54:48 */
  250. fdb->port_mask = (reg[1] >> QCA8K_ATU_PORT_S) & QCA8K_ATU_PORT_M;
  251. /* mac - 47:0 */
  252. fdb->mac[0] = (reg[1] >> QCA8K_ATU_ADDR0_S) & 0xff;
  253. fdb->mac[1] = reg[1] & 0xff;
  254. fdb->mac[2] = (reg[0] >> QCA8K_ATU_ADDR2_S) & 0xff;
  255. fdb->mac[3] = (reg[0] >> QCA8K_ATU_ADDR3_S) & 0xff;
  256. fdb->mac[4] = (reg[0] >> QCA8K_ATU_ADDR4_S) & 0xff;
  257. fdb->mac[5] = reg[0] & 0xff;
  258. }
  259. static void
  260. qca8k_fdb_write(struct qca8k_priv *priv, u16 vid, u8 port_mask, const u8 *mac,
  261. u8 aging)
  262. {
  263. u32 reg[3] = { 0 };
  264. int i;
  265. /* vid - 83:72 */
  266. reg[2] = (vid & QCA8K_ATU_VID_M) << QCA8K_ATU_VID_S;
  267. /* aging - 67:64 */
  268. reg[2] |= aging & QCA8K_ATU_STATUS_M;
  269. /* portmask - 54:48 */
  270. reg[1] = (port_mask & QCA8K_ATU_PORT_M) << QCA8K_ATU_PORT_S;
  271. /* mac - 47:0 */
  272. reg[1] |= mac[0] << QCA8K_ATU_ADDR0_S;
  273. reg[1] |= mac[1];
  274. reg[0] |= mac[2] << QCA8K_ATU_ADDR2_S;
  275. reg[0] |= mac[3] << QCA8K_ATU_ADDR3_S;
  276. reg[0] |= mac[4] << QCA8K_ATU_ADDR4_S;
  277. reg[0] |= mac[5];
  278. /* load the array into the ARL table */
  279. for (i = 0; i < 3; i++)
  280. qca8k_write(priv, QCA8K_REG_ATU_DATA0 + (i * 4), reg[i]);
  281. }
  282. static int
  283. qca8k_fdb_access(struct qca8k_priv *priv, enum qca8k_fdb_cmd cmd, int port)
  284. {
  285. u32 reg;
  286. /* Set the command and FDB index */
  287. reg = QCA8K_ATU_FUNC_BUSY;
  288. reg |= cmd;
  289. if (port >= 0) {
  290. reg |= QCA8K_ATU_FUNC_PORT_EN;
  291. reg |= (port & QCA8K_ATU_FUNC_PORT_M) << QCA8K_ATU_FUNC_PORT_S;
  292. }
  293. /* Write the function register triggering the table access */
  294. qca8k_write(priv, QCA8K_REG_ATU_FUNC, reg);
  295. /* wait for completion */
  296. if (qca8k_busy_wait(priv, QCA8K_REG_ATU_FUNC, QCA8K_ATU_FUNC_BUSY))
  297. return -1;
  298. /* Check for table full violation when adding an entry */
  299. if (cmd == QCA8K_FDB_LOAD) {
  300. reg = qca8k_read(priv, QCA8K_REG_ATU_FUNC);
  301. if (reg & QCA8K_ATU_FUNC_FULL)
  302. return -1;
  303. }
  304. return 0;
  305. }
  306. static int
  307. qca8k_fdb_next(struct qca8k_priv *priv, struct qca8k_fdb *fdb, int port)
  308. {
  309. int ret;
  310. qca8k_fdb_write(priv, fdb->vid, fdb->port_mask, fdb->mac, fdb->aging);
  311. ret = qca8k_fdb_access(priv, QCA8K_FDB_NEXT, port);
  312. if (ret >= 0)
  313. qca8k_fdb_read(priv, fdb);
  314. return ret;
  315. }
  316. static int
  317. qca8k_fdb_add(struct qca8k_priv *priv, const u8 *mac, u16 port_mask,
  318. u16 vid, u8 aging)
  319. {
  320. int ret;
  321. mutex_lock(&priv->reg_mutex);
  322. qca8k_fdb_write(priv, vid, port_mask, mac, aging);
  323. ret = qca8k_fdb_access(priv, QCA8K_FDB_LOAD, -1);
  324. mutex_unlock(&priv->reg_mutex);
  325. return ret;
  326. }
  327. static int
  328. qca8k_fdb_del(struct qca8k_priv *priv, const u8 *mac, u16 port_mask, u16 vid)
  329. {
  330. int ret;
  331. mutex_lock(&priv->reg_mutex);
  332. qca8k_fdb_write(priv, vid, port_mask, mac, 0);
  333. ret = qca8k_fdb_access(priv, QCA8K_FDB_PURGE, -1);
  334. mutex_unlock(&priv->reg_mutex);
  335. return ret;
  336. }
  337. static void
  338. qca8k_fdb_flush(struct qca8k_priv *priv)
  339. {
  340. mutex_lock(&priv->reg_mutex);
  341. qca8k_fdb_access(priv, QCA8K_FDB_FLUSH, -1);
  342. mutex_unlock(&priv->reg_mutex);
  343. }
  344. static void
  345. qca8k_mib_init(struct qca8k_priv *priv)
  346. {
  347. mutex_lock(&priv->reg_mutex);
  348. qca8k_reg_set(priv, QCA8K_REG_MIB, QCA8K_MIB_FLUSH | QCA8K_MIB_BUSY);
  349. qca8k_busy_wait(priv, QCA8K_REG_MIB, QCA8K_MIB_BUSY);
  350. qca8k_reg_set(priv, QCA8K_REG_MIB, QCA8K_MIB_CPU_KEEP);
  351. qca8k_write(priv, QCA8K_REG_MODULE_EN, QCA8K_MODULE_EN_MIB);
  352. mutex_unlock(&priv->reg_mutex);
  353. }
  354. static int
  355. qca8k_set_pad_ctrl(struct qca8k_priv *priv, int port, int mode)
  356. {
  357. u32 reg;
  358. switch (port) {
  359. case 0:
  360. reg = QCA8K_REG_PORT0_PAD_CTRL;
  361. break;
  362. case 6:
  363. reg = QCA8K_REG_PORT6_PAD_CTRL;
  364. break;
  365. default:
  366. pr_err("Can't set PAD_CTRL on port %d\n", port);
  367. return -EINVAL;
  368. }
  369. /* Configure a port to be directly connected to an external
  370. * PHY or MAC.
  371. */
  372. switch (mode) {
  373. case PHY_INTERFACE_MODE_RGMII:
  374. qca8k_write(priv, reg,
  375. QCA8K_PORT_PAD_RGMII_EN |
  376. QCA8K_PORT_PAD_RGMII_TX_DELAY(3) |
  377. QCA8K_PORT_PAD_RGMII_RX_DELAY(3));
  378. /* According to the datasheet, RGMII delay is enabled through
  379. * PORT5_PAD_CTRL for all ports, rather than individual port
  380. * registers
  381. */
  382. qca8k_write(priv, QCA8K_REG_PORT5_PAD_CTRL,
  383. QCA8K_PORT_PAD_RGMII_RX_DELAY_EN);
  384. break;
  385. case PHY_INTERFACE_MODE_SGMII:
  386. qca8k_write(priv, reg, QCA8K_PORT_PAD_SGMII_EN);
  387. break;
  388. default:
  389. pr_err("xMII mode %d not supported\n", mode);
  390. return -EINVAL;
  391. }
  392. return 0;
  393. }
  394. static void
  395. qca8k_port_set_status(struct qca8k_priv *priv, int port, int enable)
  396. {
  397. u32 mask = QCA8K_PORT_STATUS_TXMAC | QCA8K_PORT_STATUS_RXMAC;
  398. /* Port 0 and 6 have no internal PHY */
  399. if ((port > 0) && (port < 6))
  400. mask |= QCA8K_PORT_STATUS_LINK_AUTO;
  401. if (enable)
  402. qca8k_reg_set(priv, QCA8K_REG_PORT_STATUS(port), mask);
  403. else
  404. qca8k_reg_clear(priv, QCA8K_REG_PORT_STATUS(port), mask);
  405. }
  406. static int
  407. qca8k_setup(struct dsa_switch *ds)
  408. {
  409. struct qca8k_priv *priv = (struct qca8k_priv *)ds->priv;
  410. int ret, i, phy_mode = -1;
  411. u32 mask;
  412. /* Make sure that port 0 is the cpu port */
  413. if (!dsa_is_cpu_port(ds, 0)) {
  414. pr_err("port 0 is not the CPU port\n");
  415. return -EINVAL;
  416. }
  417. mutex_init(&priv->reg_mutex);
  418. /* Start by setting up the register mapping */
  419. priv->regmap = devm_regmap_init(ds->dev, NULL, priv,
  420. &qca8k_regmap_config);
  421. if (IS_ERR(priv->regmap))
  422. pr_warn("regmap initialization failed");
  423. /* Initialize CPU port pad mode (xMII type, delays...) */
  424. phy_mode = of_get_phy_mode(ds->ports[ds->dst->cpu_port].dn);
  425. if (phy_mode < 0) {
  426. pr_err("Can't find phy-mode for master device\n");
  427. return phy_mode;
  428. }
  429. ret = qca8k_set_pad_ctrl(priv, QCA8K_CPU_PORT, phy_mode);
  430. if (ret < 0)
  431. return ret;
  432. /* Enable CPU Port, force it to maximum bandwidth and full-duplex */
  433. mask = QCA8K_PORT_STATUS_SPEED_1000 | QCA8K_PORT_STATUS_TXFLOW |
  434. QCA8K_PORT_STATUS_RXFLOW | QCA8K_PORT_STATUS_DUPLEX;
  435. qca8k_write(priv, QCA8K_REG_PORT_STATUS(QCA8K_CPU_PORT), mask);
  436. qca8k_reg_set(priv, QCA8K_REG_GLOBAL_FW_CTRL0,
  437. QCA8K_GLOBAL_FW_CTRL0_CPU_PORT_EN);
  438. qca8k_port_set_status(priv, QCA8K_CPU_PORT, 1);
  439. priv->port_sts[QCA8K_CPU_PORT].enabled = 1;
  440. /* Enable MIB counters */
  441. qca8k_mib_init(priv);
  442. /* Enable QCA header mode on the cpu port */
  443. qca8k_write(priv, QCA8K_REG_PORT_HDR_CTRL(QCA8K_CPU_PORT),
  444. QCA8K_PORT_HDR_CTRL_ALL << QCA8K_PORT_HDR_CTRL_TX_S |
  445. QCA8K_PORT_HDR_CTRL_ALL << QCA8K_PORT_HDR_CTRL_RX_S);
  446. /* Disable forwarding by default on all ports */
  447. for (i = 0; i < QCA8K_NUM_PORTS; i++)
  448. qca8k_rmw(priv, QCA8K_PORT_LOOKUP_CTRL(i),
  449. QCA8K_PORT_LOOKUP_MEMBER, 0);
  450. /* Disable MAC by default on all user ports */
  451. for (i = 1; i < QCA8K_NUM_PORTS; i++)
  452. if (ds->enabled_port_mask & BIT(i))
  453. qca8k_port_set_status(priv, i, 0);
  454. /* Forward all unknown frames to CPU port for Linux processing */
  455. qca8k_write(priv, QCA8K_REG_GLOBAL_FW_CTRL1,
  456. BIT(0) << QCA8K_GLOBAL_FW_CTRL1_IGMP_DP_S |
  457. BIT(0) << QCA8K_GLOBAL_FW_CTRL1_BC_DP_S |
  458. BIT(0) << QCA8K_GLOBAL_FW_CTRL1_MC_DP_S |
  459. BIT(0) << QCA8K_GLOBAL_FW_CTRL1_UC_DP_S);
  460. /* Setup connection between CPU port & user ports */
  461. for (i = 0; i < DSA_MAX_PORTS; i++) {
  462. /* CPU port gets connected to all user ports of the switch */
  463. if (dsa_is_cpu_port(ds, i)) {
  464. qca8k_rmw(priv, QCA8K_PORT_LOOKUP_CTRL(QCA8K_CPU_PORT),
  465. QCA8K_PORT_LOOKUP_MEMBER,
  466. ds->enabled_port_mask);
  467. }
  468. /* Invividual user ports get connected to CPU port only */
  469. if (ds->enabled_port_mask & BIT(i)) {
  470. int shift = 16 * (i % 2);
  471. qca8k_rmw(priv, QCA8K_PORT_LOOKUP_CTRL(i),
  472. QCA8K_PORT_LOOKUP_MEMBER,
  473. BIT(QCA8K_CPU_PORT));
  474. /* Enable ARP Auto-learning by default */
  475. qca8k_reg_set(priv, QCA8K_PORT_LOOKUP_CTRL(i),
  476. QCA8K_PORT_LOOKUP_LEARN);
  477. /* For port based vlans to work we need to set the
  478. * default egress vid
  479. */
  480. qca8k_rmw(priv, QCA8K_EGRESS_VLAN(i),
  481. 0xffff << shift, 1 << shift);
  482. qca8k_write(priv, QCA8K_REG_PORT_VLAN_CTRL0(i),
  483. QCA8K_PORT_VLAN_CVID(1) |
  484. QCA8K_PORT_VLAN_SVID(1));
  485. }
  486. }
  487. /* Flush the FDB table */
  488. qca8k_fdb_flush(priv);
  489. return 0;
  490. }
  491. static void
  492. qca8k_adjust_link(struct dsa_switch *ds, int port, struct phy_device *phy)
  493. {
  494. struct qca8k_priv *priv = ds->priv;
  495. u32 reg;
  496. /* Force fixed-link setting for CPU port, skip others. */
  497. if (!phy_is_pseudo_fixed_link(phy))
  498. return;
  499. /* Set port speed */
  500. switch (phy->speed) {
  501. case 10:
  502. reg = QCA8K_PORT_STATUS_SPEED_10;
  503. break;
  504. case 100:
  505. reg = QCA8K_PORT_STATUS_SPEED_100;
  506. break;
  507. case 1000:
  508. reg = QCA8K_PORT_STATUS_SPEED_1000;
  509. break;
  510. default:
  511. dev_dbg(priv->dev, "port%d link speed %dMbps not supported.\n",
  512. port, phy->speed);
  513. return;
  514. }
  515. /* Set duplex mode */
  516. if (phy->duplex == DUPLEX_FULL)
  517. reg |= QCA8K_PORT_STATUS_DUPLEX;
  518. /* Force flow control */
  519. if (dsa_is_cpu_port(ds, port))
  520. reg |= QCA8K_PORT_STATUS_RXFLOW | QCA8K_PORT_STATUS_TXFLOW;
  521. /* Force link down before changing MAC options */
  522. qca8k_port_set_status(priv, port, 0);
  523. qca8k_write(priv, QCA8K_REG_PORT_STATUS(port), reg);
  524. qca8k_port_set_status(priv, port, 1);
  525. }
  526. static int
  527. qca8k_phy_read(struct dsa_switch *ds, int phy, int regnum)
  528. {
  529. struct qca8k_priv *priv = (struct qca8k_priv *)ds->priv;
  530. return mdiobus_read(priv->bus, phy, regnum);
  531. }
  532. static int
  533. qca8k_phy_write(struct dsa_switch *ds, int phy, int regnum, u16 val)
  534. {
  535. struct qca8k_priv *priv = (struct qca8k_priv *)ds->priv;
  536. return mdiobus_write(priv->bus, phy, regnum, val);
  537. }
  538. static void
  539. qca8k_get_strings(struct dsa_switch *ds, int port, uint8_t *data)
  540. {
  541. int i;
  542. for (i = 0; i < ARRAY_SIZE(ar8327_mib); i++)
  543. strncpy(data + i * ETH_GSTRING_LEN, ar8327_mib[i].name,
  544. ETH_GSTRING_LEN);
  545. }
  546. static void
  547. qca8k_get_ethtool_stats(struct dsa_switch *ds, int port,
  548. uint64_t *data)
  549. {
  550. struct qca8k_priv *priv = (struct qca8k_priv *)ds->priv;
  551. const struct qca8k_mib_desc *mib;
  552. u32 reg, i;
  553. u64 hi;
  554. for (i = 0; i < ARRAY_SIZE(ar8327_mib); i++) {
  555. mib = &ar8327_mib[i];
  556. reg = QCA8K_PORT_MIB_COUNTER(port) + mib->offset;
  557. data[i] = qca8k_read(priv, reg);
  558. if (mib->size == 2) {
  559. hi = qca8k_read(priv, reg + 4);
  560. data[i] |= hi << 32;
  561. }
  562. }
  563. }
  564. static int
  565. qca8k_get_sset_count(struct dsa_switch *ds)
  566. {
  567. return ARRAY_SIZE(ar8327_mib);
  568. }
  569. static void
  570. qca8k_eee_enable_set(struct dsa_switch *ds, int port, bool enable)
  571. {
  572. struct qca8k_priv *priv = (struct qca8k_priv *)ds->priv;
  573. u32 lpi_en = QCA8K_REG_EEE_CTRL_LPI_EN(port);
  574. u32 reg;
  575. mutex_lock(&priv->reg_mutex);
  576. reg = qca8k_read(priv, QCA8K_REG_EEE_CTRL);
  577. if (enable)
  578. reg |= lpi_en;
  579. else
  580. reg &= ~lpi_en;
  581. qca8k_write(priv, QCA8K_REG_EEE_CTRL, reg);
  582. mutex_unlock(&priv->reg_mutex);
  583. }
  584. static int
  585. qca8k_eee_init(struct dsa_switch *ds, int port,
  586. struct phy_device *phy)
  587. {
  588. struct qca8k_priv *priv = (struct qca8k_priv *)ds->priv;
  589. struct ethtool_eee *p = &priv->port_sts[port].eee;
  590. int ret;
  591. p->supported = (SUPPORTED_1000baseT_Full | SUPPORTED_100baseT_Full);
  592. ret = phy_init_eee(phy, 0);
  593. if (ret)
  594. return ret;
  595. qca8k_eee_enable_set(ds, port, true);
  596. return 0;
  597. }
  598. static int
  599. qca8k_set_eee(struct dsa_switch *ds, int port,
  600. struct phy_device *phydev,
  601. struct ethtool_eee *e)
  602. {
  603. struct qca8k_priv *priv = (struct qca8k_priv *)ds->priv;
  604. struct ethtool_eee *p = &priv->port_sts[port].eee;
  605. int ret = 0;
  606. p->eee_enabled = e->eee_enabled;
  607. if (e->eee_enabled) {
  608. p->eee_enabled = qca8k_eee_init(ds, port, phydev);
  609. if (!p->eee_enabled)
  610. ret = -EOPNOTSUPP;
  611. }
  612. qca8k_eee_enable_set(ds, port, p->eee_enabled);
  613. return ret;
  614. }
  615. static int
  616. qca8k_get_eee(struct dsa_switch *ds, int port,
  617. struct ethtool_eee *e)
  618. {
  619. struct qca8k_priv *priv = (struct qca8k_priv *)ds->priv;
  620. struct ethtool_eee *p = &priv->port_sts[port].eee;
  621. struct net_device *netdev = ds->ports[port].netdev;
  622. int ret;
  623. ret = phy_ethtool_get_eee(netdev->phydev, p);
  624. if (!ret)
  625. e->eee_active =
  626. !!(p->supported & p->advertised & p->lp_advertised);
  627. else
  628. e->eee_active = 0;
  629. e->eee_enabled = p->eee_enabled;
  630. return ret;
  631. }
  632. static void
  633. qca8k_port_stp_state_set(struct dsa_switch *ds, int port, u8 state)
  634. {
  635. struct qca8k_priv *priv = (struct qca8k_priv *)ds->priv;
  636. u32 stp_state;
  637. switch (state) {
  638. case BR_STATE_DISABLED:
  639. stp_state = QCA8K_PORT_LOOKUP_STATE_DISABLED;
  640. break;
  641. case BR_STATE_BLOCKING:
  642. stp_state = QCA8K_PORT_LOOKUP_STATE_BLOCKING;
  643. break;
  644. case BR_STATE_LISTENING:
  645. stp_state = QCA8K_PORT_LOOKUP_STATE_LISTENING;
  646. break;
  647. case BR_STATE_LEARNING:
  648. stp_state = QCA8K_PORT_LOOKUP_STATE_LEARNING;
  649. break;
  650. case BR_STATE_FORWARDING:
  651. default:
  652. stp_state = QCA8K_PORT_LOOKUP_STATE_FORWARD;
  653. break;
  654. }
  655. qca8k_rmw(priv, QCA8K_PORT_LOOKUP_CTRL(port),
  656. QCA8K_PORT_LOOKUP_STATE_MASK, stp_state);
  657. }
  658. static int
  659. qca8k_port_bridge_join(struct dsa_switch *ds, int port,
  660. struct net_device *bridge)
  661. {
  662. struct qca8k_priv *priv = (struct qca8k_priv *)ds->priv;
  663. int port_mask = BIT(QCA8K_CPU_PORT);
  664. int i;
  665. priv->port_sts[port].bridge_dev = bridge;
  666. for (i = 1; i < QCA8K_NUM_PORTS; i++) {
  667. if (priv->port_sts[i].bridge_dev != bridge)
  668. continue;
  669. /* Add this port to the portvlan mask of the other ports
  670. * in the bridge
  671. */
  672. qca8k_reg_set(priv,
  673. QCA8K_PORT_LOOKUP_CTRL(i),
  674. BIT(port));
  675. if (i != port)
  676. port_mask |= BIT(i);
  677. }
  678. /* Add all other ports to this ports portvlan mask */
  679. qca8k_rmw(priv, QCA8K_PORT_LOOKUP_CTRL(port),
  680. QCA8K_PORT_LOOKUP_MEMBER, port_mask);
  681. return 0;
  682. }
  683. static void
  684. qca8k_port_bridge_leave(struct dsa_switch *ds, int port)
  685. {
  686. struct qca8k_priv *priv = (struct qca8k_priv *)ds->priv;
  687. int i;
  688. for (i = 1; i < QCA8K_NUM_PORTS; i++) {
  689. if (priv->port_sts[i].bridge_dev !=
  690. priv->port_sts[port].bridge_dev)
  691. continue;
  692. /* Remove this port to the portvlan mask of the other ports
  693. * in the bridge
  694. */
  695. qca8k_reg_clear(priv,
  696. QCA8K_PORT_LOOKUP_CTRL(i),
  697. BIT(port));
  698. }
  699. priv->port_sts[port].bridge_dev = NULL;
  700. /* Set the cpu port to be the only one in the portvlan mask of
  701. * this port
  702. */
  703. qca8k_rmw(priv, QCA8K_PORT_LOOKUP_CTRL(port),
  704. QCA8K_PORT_LOOKUP_MEMBER, BIT(QCA8K_CPU_PORT));
  705. }
  706. static int
  707. qca8k_port_enable(struct dsa_switch *ds, int port,
  708. struct phy_device *phy)
  709. {
  710. struct qca8k_priv *priv = (struct qca8k_priv *)ds->priv;
  711. qca8k_port_set_status(priv, port, 1);
  712. priv->port_sts[port].enabled = 1;
  713. return 0;
  714. }
  715. static void
  716. qca8k_port_disable(struct dsa_switch *ds, int port,
  717. struct phy_device *phy)
  718. {
  719. struct qca8k_priv *priv = (struct qca8k_priv *)ds->priv;
  720. qca8k_port_set_status(priv, port, 0);
  721. priv->port_sts[port].enabled = 0;
  722. }
  723. static int
  724. qca8k_port_fdb_insert(struct qca8k_priv *priv, const u8 *addr,
  725. u16 port_mask, u16 vid)
  726. {
  727. /* Set the vid to the port vlan id if no vid is set */
  728. if (!vid)
  729. vid = 1;
  730. return qca8k_fdb_add(priv, addr, port_mask, vid,
  731. QCA8K_ATU_STATUS_STATIC);
  732. }
  733. static int
  734. qca8k_port_fdb_prepare(struct dsa_switch *ds, int port,
  735. const struct switchdev_obj_port_fdb *fdb,
  736. struct switchdev_trans *trans)
  737. {
  738. struct qca8k_priv *priv = (struct qca8k_priv *)ds->priv;
  739. /* The FDB table for static and auto learned entries is the same. We
  740. * need to reserve an entry with no port_mask set to make sure that
  741. * when port_fdb_add is called an entry is still available. Otherwise
  742. * the last free entry might have been used up by auto learning
  743. */
  744. return qca8k_port_fdb_insert(priv, fdb->addr, 0, fdb->vid);
  745. }
  746. static void
  747. qca8k_port_fdb_add(struct dsa_switch *ds, int port,
  748. const struct switchdev_obj_port_fdb *fdb,
  749. struct switchdev_trans *trans)
  750. {
  751. struct qca8k_priv *priv = (struct qca8k_priv *)ds->priv;
  752. u16 port_mask = BIT(port);
  753. /* Update the FDB entry adding the port_mask */
  754. qca8k_port_fdb_insert(priv, fdb->addr, port_mask, fdb->vid);
  755. }
  756. static int
  757. qca8k_port_fdb_del(struct dsa_switch *ds, int port,
  758. const struct switchdev_obj_port_fdb *fdb)
  759. {
  760. struct qca8k_priv *priv = (struct qca8k_priv *)ds->priv;
  761. u16 port_mask = BIT(port);
  762. u16 vid = fdb->vid;
  763. if (!vid)
  764. vid = 1;
  765. return qca8k_fdb_del(priv, fdb->addr, port_mask, vid);
  766. }
  767. static int
  768. qca8k_port_fdb_dump(struct dsa_switch *ds, int port,
  769. struct switchdev_obj_port_fdb *fdb,
  770. int (*cb)(struct switchdev_obj *obj))
  771. {
  772. struct qca8k_priv *priv = (struct qca8k_priv *)ds->priv;
  773. struct qca8k_fdb _fdb = { 0 };
  774. int cnt = QCA8K_NUM_FDB_RECORDS;
  775. int ret = 0;
  776. mutex_lock(&priv->reg_mutex);
  777. while (cnt-- && !qca8k_fdb_next(priv, &_fdb, port)) {
  778. if (!_fdb.aging)
  779. break;
  780. ether_addr_copy(fdb->addr, _fdb.mac);
  781. fdb->vid = _fdb.vid;
  782. if (_fdb.aging == QCA8K_ATU_STATUS_STATIC)
  783. fdb->ndm_state = NUD_NOARP;
  784. else
  785. fdb->ndm_state = NUD_REACHABLE;
  786. ret = cb(&fdb->obj);
  787. if (ret)
  788. break;
  789. }
  790. mutex_unlock(&priv->reg_mutex);
  791. return 0;
  792. }
  793. static enum dsa_tag_protocol
  794. qca8k_get_tag_protocol(struct dsa_switch *ds)
  795. {
  796. return DSA_TAG_PROTO_QCA;
  797. }
  798. static struct dsa_switch_ops qca8k_switch_ops = {
  799. .get_tag_protocol = qca8k_get_tag_protocol,
  800. .setup = qca8k_setup,
  801. .adjust_link = qca8k_adjust_link,
  802. .get_strings = qca8k_get_strings,
  803. .phy_read = qca8k_phy_read,
  804. .phy_write = qca8k_phy_write,
  805. .get_ethtool_stats = qca8k_get_ethtool_stats,
  806. .get_sset_count = qca8k_get_sset_count,
  807. .get_eee = qca8k_get_eee,
  808. .set_eee = qca8k_set_eee,
  809. .port_enable = qca8k_port_enable,
  810. .port_disable = qca8k_port_disable,
  811. .port_stp_state_set = qca8k_port_stp_state_set,
  812. .port_bridge_join = qca8k_port_bridge_join,
  813. .port_bridge_leave = qca8k_port_bridge_leave,
  814. .port_fdb_prepare = qca8k_port_fdb_prepare,
  815. .port_fdb_add = qca8k_port_fdb_add,
  816. .port_fdb_del = qca8k_port_fdb_del,
  817. .port_fdb_dump = qca8k_port_fdb_dump,
  818. };
  819. static int
  820. qca8k_sw_probe(struct mdio_device *mdiodev)
  821. {
  822. struct qca8k_priv *priv;
  823. u32 id;
  824. /* allocate the private data struct so that we can probe the switches
  825. * ID register
  826. */
  827. priv = devm_kzalloc(&mdiodev->dev, sizeof(*priv), GFP_KERNEL);
  828. if (!priv)
  829. return -ENOMEM;
  830. priv->bus = mdiodev->bus;
  831. priv->dev = &mdiodev->dev;
  832. /* read the switches ID register */
  833. id = qca8k_read(priv, QCA8K_REG_MASK_CTRL);
  834. id >>= QCA8K_MASK_CTRL_ID_S;
  835. id &= QCA8K_MASK_CTRL_ID_M;
  836. if (id != QCA8K_ID_QCA8337)
  837. return -ENODEV;
  838. priv->ds = devm_kzalloc(&mdiodev->dev, sizeof(*priv->ds), GFP_KERNEL);
  839. if (!priv->ds)
  840. return -ENOMEM;
  841. priv->ds->priv = priv;
  842. priv->ds->dev = &mdiodev->dev;
  843. priv->ds->ops = &qca8k_switch_ops;
  844. mutex_init(&priv->reg_mutex);
  845. dev_set_drvdata(&mdiodev->dev, priv);
  846. return dsa_register_switch(priv->ds, priv->ds->dev->of_node);
  847. }
  848. static void
  849. qca8k_sw_remove(struct mdio_device *mdiodev)
  850. {
  851. struct qca8k_priv *priv = dev_get_drvdata(&mdiodev->dev);
  852. int i;
  853. for (i = 0; i < QCA8K_NUM_PORTS; i++)
  854. qca8k_port_set_status(priv, i, 0);
  855. dsa_unregister_switch(priv->ds);
  856. }
  857. #ifdef CONFIG_PM_SLEEP
  858. static void
  859. qca8k_set_pm(struct qca8k_priv *priv, int enable)
  860. {
  861. int i;
  862. for (i = 0; i < QCA8K_NUM_PORTS; i++) {
  863. if (!priv->port_sts[i].enabled)
  864. continue;
  865. qca8k_port_set_status(priv, i, enable);
  866. }
  867. }
  868. static int qca8k_suspend(struct device *dev)
  869. {
  870. struct platform_device *pdev = to_platform_device(dev);
  871. struct qca8k_priv *priv = platform_get_drvdata(pdev);
  872. qca8k_set_pm(priv, 0);
  873. return dsa_switch_suspend(priv->ds);
  874. }
  875. static int qca8k_resume(struct device *dev)
  876. {
  877. struct platform_device *pdev = to_platform_device(dev);
  878. struct qca8k_priv *priv = platform_get_drvdata(pdev);
  879. qca8k_set_pm(priv, 1);
  880. return dsa_switch_resume(priv->ds);
  881. }
  882. #endif /* CONFIG_PM_SLEEP */
  883. static SIMPLE_DEV_PM_OPS(qca8k_pm_ops,
  884. qca8k_suspend, qca8k_resume);
  885. static const struct of_device_id qca8k_of_match[] = {
  886. { .compatible = "qca,qca8334" },
  887. { .compatible = "qca,qca8337" },
  888. { /* sentinel */ },
  889. };
  890. static struct mdio_driver qca8kmdio_driver = {
  891. .probe = qca8k_sw_probe,
  892. .remove = qca8k_sw_remove,
  893. .mdiodrv.driver = {
  894. .name = "qca8k",
  895. .of_match_table = qca8k_of_match,
  896. .pm = &qca8k_pm_ops,
  897. },
  898. };
  899. mdio_module_driver(qca8kmdio_driver);
  900. MODULE_AUTHOR("Mathieu Olivari, John Crispin <john@phrozen.org>");
  901. MODULE_DESCRIPTION("Driver for QCA8K ethernet switch family");
  902. MODULE_LICENSE("GPL v2");
  903. MODULE_ALIAS("platform:qca8k");