ksz9477.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Microchip KSZ9477 switch driver main logic
  4. *
  5. * Copyright (C) 2017-2024 Microchip Technology Inc.
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/iopoll.h>
  10. #include <linux/platform_data/microchip-ksz.h>
  11. #include <linux/phy.h>
  12. #include <linux/if_bridge.h>
  13. #include <linux/if_vlan.h>
  14. #include <net/dsa.h>
  15. #include <net/switchdev.h>
  16. #include "ksz9477_reg.h"
  17. #include "ksz_common.h"
  18. #include "ksz9477.h"
  19. static void ksz_cfg(struct ksz_device *dev, u32 addr, u8 bits, bool set)
  20. {
  21. regmap_update_bits(ksz_regmap_8(dev), addr, bits, set ? bits : 0);
  22. }
  23. static void ksz_port_cfg(struct ksz_device *dev, int port, int offset, u8 bits,
  24. bool set)
  25. {
  26. regmap_update_bits(ksz_regmap_8(dev), PORT_CTRL_ADDR(port, offset),
  27. bits, set ? bits : 0);
  28. }
  29. static void ksz9477_cfg32(struct ksz_device *dev, u32 addr, u32 bits, bool set)
  30. {
  31. regmap_update_bits(ksz_regmap_32(dev), addr, bits, set ? bits : 0);
  32. }
  33. static void ksz9477_port_cfg32(struct ksz_device *dev, int port, int offset,
  34. u32 bits, bool set)
  35. {
  36. regmap_update_bits(ksz_regmap_32(dev), PORT_CTRL_ADDR(port, offset),
  37. bits, set ? bits : 0);
  38. }
  39. int ksz9477_change_mtu(struct ksz_device *dev, int port, int mtu)
  40. {
  41. u16 frame_size;
  42. if (!dsa_is_cpu_port(dev->ds, port))
  43. return 0;
  44. frame_size = mtu + VLAN_ETH_HLEN + ETH_FCS_LEN;
  45. return regmap_update_bits(ksz_regmap_16(dev), REG_SW_MTU__2,
  46. REG_SW_MTU_MASK, frame_size);
  47. }
  48. static int ksz9477_wait_vlan_ctrl_ready(struct ksz_device *dev)
  49. {
  50. unsigned int val;
  51. return regmap_read_poll_timeout(ksz_regmap_8(dev), REG_SW_VLAN_CTRL,
  52. val, !(val & VLAN_START), 10, 1000);
  53. }
  54. static int ksz9477_get_vlan_table(struct ksz_device *dev, u16 vid,
  55. u32 *vlan_table)
  56. {
  57. int ret;
  58. mutex_lock(&dev->vlan_mutex);
  59. ksz_write16(dev, REG_SW_VLAN_ENTRY_INDEX__2, vid & VLAN_INDEX_M);
  60. ksz_write8(dev, REG_SW_VLAN_CTRL, VLAN_READ | VLAN_START);
  61. /* wait to be cleared */
  62. ret = ksz9477_wait_vlan_ctrl_ready(dev);
  63. if (ret) {
  64. dev_dbg(dev->dev, "Failed to read vlan table\n");
  65. goto exit;
  66. }
  67. ksz_read32(dev, REG_SW_VLAN_ENTRY__4, &vlan_table[0]);
  68. ksz_read32(dev, REG_SW_VLAN_ENTRY_UNTAG__4, &vlan_table[1]);
  69. ksz_read32(dev, REG_SW_VLAN_ENTRY_PORTS__4, &vlan_table[2]);
  70. ksz_write8(dev, REG_SW_VLAN_CTRL, 0);
  71. exit:
  72. mutex_unlock(&dev->vlan_mutex);
  73. return ret;
  74. }
  75. static int ksz9477_set_vlan_table(struct ksz_device *dev, u16 vid,
  76. u32 *vlan_table)
  77. {
  78. int ret;
  79. mutex_lock(&dev->vlan_mutex);
  80. ksz_write32(dev, REG_SW_VLAN_ENTRY__4, vlan_table[0]);
  81. ksz_write32(dev, REG_SW_VLAN_ENTRY_UNTAG__4, vlan_table[1]);
  82. ksz_write32(dev, REG_SW_VLAN_ENTRY_PORTS__4, vlan_table[2]);
  83. ksz_write16(dev, REG_SW_VLAN_ENTRY_INDEX__2, vid & VLAN_INDEX_M);
  84. ksz_write8(dev, REG_SW_VLAN_CTRL, VLAN_START | VLAN_WRITE);
  85. /* wait to be cleared */
  86. ret = ksz9477_wait_vlan_ctrl_ready(dev);
  87. if (ret) {
  88. dev_dbg(dev->dev, "Failed to write vlan table\n");
  89. goto exit;
  90. }
  91. ksz_write8(dev, REG_SW_VLAN_CTRL, 0);
  92. /* update vlan cache table */
  93. dev->vlan_cache[vid].table[0] = vlan_table[0];
  94. dev->vlan_cache[vid].table[1] = vlan_table[1];
  95. dev->vlan_cache[vid].table[2] = vlan_table[2];
  96. exit:
  97. mutex_unlock(&dev->vlan_mutex);
  98. return ret;
  99. }
  100. static void ksz9477_read_table(struct ksz_device *dev, u32 *table)
  101. {
  102. ksz_read32(dev, REG_SW_ALU_VAL_A, &table[0]);
  103. ksz_read32(dev, REG_SW_ALU_VAL_B, &table[1]);
  104. ksz_read32(dev, REG_SW_ALU_VAL_C, &table[2]);
  105. ksz_read32(dev, REG_SW_ALU_VAL_D, &table[3]);
  106. }
  107. static void ksz9477_write_table(struct ksz_device *dev, u32 *table)
  108. {
  109. ksz_write32(dev, REG_SW_ALU_VAL_A, table[0]);
  110. ksz_write32(dev, REG_SW_ALU_VAL_B, table[1]);
  111. ksz_write32(dev, REG_SW_ALU_VAL_C, table[2]);
  112. ksz_write32(dev, REG_SW_ALU_VAL_D, table[3]);
  113. }
  114. static int ksz9477_wait_alu_ready(struct ksz_device *dev)
  115. {
  116. unsigned int val;
  117. return regmap_read_poll_timeout(ksz_regmap_32(dev), REG_SW_ALU_CTRL__4,
  118. val, !(val & ALU_START), 10, 1000);
  119. }
  120. static int ksz9477_wait_alu_sta_ready(struct ksz_device *dev)
  121. {
  122. unsigned int val;
  123. return regmap_read_poll_timeout(ksz_regmap_32(dev),
  124. REG_SW_ALU_STAT_CTRL__4,
  125. val, !(val & ALU_STAT_START),
  126. 10, 1000);
  127. }
  128. int ksz9477_reset_switch(struct ksz_device *dev)
  129. {
  130. u8 data8;
  131. u32 data32;
  132. /* reset switch */
  133. ksz_cfg(dev, REG_SW_OPERATION, SW_RESET, true);
  134. /* turn off SPI DO Edge select */
  135. regmap_update_bits(ksz_regmap_8(dev), REG_SW_GLOBAL_SERIAL_CTRL_0,
  136. SPI_AUTO_EDGE_DETECTION, 0);
  137. /* default configuration */
  138. ksz_write8(dev, REG_SW_LUE_CTRL_1,
  139. SW_AGING_ENABLE | SW_LINK_AUTO_AGING | SW_SRC_ADDR_FILTER);
  140. /* disable interrupts */
  141. ksz_write32(dev, REG_SW_INT_MASK__4, SWITCH_INT_MASK);
  142. ksz_write32(dev, REG_SW_PORT_INT_MASK__4, 0x7F);
  143. ksz_read32(dev, REG_SW_PORT_INT_STATUS__4, &data32);
  144. /* KSZ9893 compatible chips do not support refclk configuration */
  145. if (dev->chip_id == KSZ9893_CHIP_ID ||
  146. dev->chip_id == KSZ8563_CHIP_ID ||
  147. dev->chip_id == KSZ9563_CHIP_ID)
  148. return 0;
  149. data8 = SW_ENABLE_REFCLKO;
  150. if (dev->synclko_disable)
  151. data8 = 0;
  152. else if (dev->synclko_125)
  153. data8 = SW_ENABLE_REFCLKO | SW_REFCLKO_IS_125MHZ;
  154. ksz_write8(dev, REG_SW_GLOBAL_OUTPUT_CTRL__1, data8);
  155. return 0;
  156. }
  157. void ksz9477_r_mib_cnt(struct ksz_device *dev, int port, u16 addr, u64 *cnt)
  158. {
  159. struct ksz_port *p = &dev->ports[port];
  160. unsigned int val;
  161. u32 data;
  162. int ret;
  163. /* retain the flush/freeze bit */
  164. data = p->freeze ? MIB_COUNTER_FLUSH_FREEZE : 0;
  165. data |= MIB_COUNTER_READ;
  166. data |= (addr << MIB_COUNTER_INDEX_S);
  167. ksz_pwrite32(dev, port, REG_PORT_MIB_CTRL_STAT__4, data);
  168. ret = regmap_read_poll_timeout(ksz_regmap_32(dev),
  169. PORT_CTRL_ADDR(port, REG_PORT_MIB_CTRL_STAT__4),
  170. val, !(val & MIB_COUNTER_READ), 10, 1000);
  171. /* failed to read MIB. get out of loop */
  172. if (ret) {
  173. dev_dbg(dev->dev, "Failed to get MIB\n");
  174. return;
  175. }
  176. /* count resets upon read */
  177. ksz_pread32(dev, port, REG_PORT_MIB_DATA, &data);
  178. *cnt += data;
  179. }
  180. void ksz9477_r_mib_pkt(struct ksz_device *dev, int port, u16 addr,
  181. u64 *dropped, u64 *cnt)
  182. {
  183. addr = dev->info->mib_names[addr].index;
  184. ksz9477_r_mib_cnt(dev, port, addr, cnt);
  185. }
  186. void ksz9477_freeze_mib(struct ksz_device *dev, int port, bool freeze)
  187. {
  188. u32 val = freeze ? MIB_COUNTER_FLUSH_FREEZE : 0;
  189. struct ksz_port *p = &dev->ports[port];
  190. /* enable/disable the port for flush/freeze function */
  191. mutex_lock(&p->mib.cnt_mutex);
  192. ksz_pwrite32(dev, port, REG_PORT_MIB_CTRL_STAT__4, val);
  193. /* used by MIB counter reading code to know freeze is enabled */
  194. p->freeze = freeze;
  195. mutex_unlock(&p->mib.cnt_mutex);
  196. }
  197. static int ksz9477_half_duplex_monitor(struct ksz_device *dev, int port,
  198. u64 tx_late_col)
  199. {
  200. u8 lue_ctrl;
  201. u32 pmavbc;
  202. u16 pqm;
  203. int ret;
  204. /* Errata DS80000754 recommends monitoring potential faults in
  205. * half-duplex mode. The switch might not be able to communicate anymore
  206. * in these states. If you see this message, please read the
  207. * errata-sheet for more information:
  208. * https://ww1.microchip.com/downloads/aemDocuments/documents/UNG/ProductDocuments/Errata/KSZ9477S-Errata-DS80000754.pdf
  209. * To workaround this issue, half-duplex mode should be avoided.
  210. * A software reset could be implemented to recover from this state.
  211. */
  212. dev_warn_once(dev->dev,
  213. "Half-duplex detected on port %d, transmission halt may occur\n",
  214. port);
  215. if (tx_late_col != 0) {
  216. /* Transmission halt with late collisions */
  217. dev_crit_once(dev->dev,
  218. "TX late collisions detected, transmission may be halted on port %d\n",
  219. port);
  220. }
  221. ret = ksz_read8(dev, REG_SW_LUE_CTRL_0, &lue_ctrl);
  222. if (ret)
  223. return ret;
  224. if (lue_ctrl & SW_VLAN_ENABLE) {
  225. ret = ksz_pread16(dev, port, REG_PORT_QM_TX_CNT_0__4, &pqm);
  226. if (ret)
  227. return ret;
  228. ret = ksz_read32(dev, REG_PMAVBC, &pmavbc);
  229. if (ret)
  230. return ret;
  231. if ((FIELD_GET(PMAVBC_MASK, pmavbc) <= PMAVBC_MIN) ||
  232. (FIELD_GET(PORT_QM_TX_CNT_M, pqm) >= PORT_QM_TX_CNT_MAX)) {
  233. /* Transmission halt with Half-Duplex and VLAN */
  234. dev_crit_once(dev->dev,
  235. "resources out of limits, transmission may be halted\n");
  236. }
  237. }
  238. return ret;
  239. }
  240. int ksz9477_errata_monitor(struct ksz_device *dev, int port,
  241. u64 tx_late_col)
  242. {
  243. u8 status;
  244. int ret;
  245. ret = ksz_pread8(dev, port, REG_PORT_STATUS_0, &status);
  246. if (ret)
  247. return ret;
  248. if (!(FIELD_GET(PORT_INTF_SPEED_MASK, status)
  249. == PORT_INTF_SPEED_NONE) &&
  250. !(status & PORT_INTF_FULL_DUPLEX)) {
  251. ret = ksz9477_half_duplex_monitor(dev, port, tx_late_col);
  252. }
  253. return ret;
  254. }
  255. void ksz9477_port_init_cnt(struct ksz_device *dev, int port)
  256. {
  257. struct ksz_port_mib *mib = &dev->ports[port].mib;
  258. /* flush all enabled port MIB counters */
  259. mutex_lock(&mib->cnt_mutex);
  260. ksz_pwrite32(dev, port, REG_PORT_MIB_CTRL_STAT__4,
  261. MIB_COUNTER_FLUSH_FREEZE);
  262. ksz_write8(dev, REG_SW_MAC_CTRL_6, SW_MIB_COUNTER_FLUSH);
  263. ksz_pwrite32(dev, port, REG_PORT_MIB_CTRL_STAT__4, 0);
  264. mutex_unlock(&mib->cnt_mutex);
  265. }
  266. static void ksz9477_r_phy_quirks(struct ksz_device *dev, u16 addr, u16 reg,
  267. u16 *data)
  268. {
  269. /* KSZ8563R do not have extended registers but BMSR_ESTATEN and
  270. * BMSR_ERCAP bits are set.
  271. */
  272. if (dev->chip_id == KSZ8563_CHIP_ID && reg == MII_BMSR)
  273. *data &= ~(BMSR_ESTATEN | BMSR_ERCAP);
  274. }
  275. int ksz9477_r_phy(struct ksz_device *dev, u16 addr, u16 reg, u16 *data)
  276. {
  277. u16 val = 0xffff;
  278. int ret;
  279. /* No real PHY after this. Simulate the PHY.
  280. * A fixed PHY can be setup in the device tree, but this function is
  281. * still called for that port during initialization.
  282. * For RGMII PHY there is no way to access it so the fixed PHY should
  283. * be used. For SGMII PHY the supporting code will be added later.
  284. */
  285. if (!dev->info->internal_phy[addr]) {
  286. struct ksz_port *p = &dev->ports[addr];
  287. switch (reg) {
  288. case MII_BMCR:
  289. val = 0x1140;
  290. break;
  291. case MII_BMSR:
  292. val = 0x796d;
  293. break;
  294. case MII_PHYSID1:
  295. val = 0x0022;
  296. break;
  297. case MII_PHYSID2:
  298. val = 0x1631;
  299. break;
  300. case MII_ADVERTISE:
  301. val = 0x05e1;
  302. break;
  303. case MII_LPA:
  304. val = 0xc5e1;
  305. break;
  306. case MII_CTRL1000:
  307. val = 0x0700;
  308. break;
  309. case MII_STAT1000:
  310. if (p->phydev.speed == SPEED_1000)
  311. val = 0x3800;
  312. else
  313. val = 0;
  314. break;
  315. }
  316. } else {
  317. ret = ksz_pread16(dev, addr, 0x100 + (reg << 1), &val);
  318. if (ret)
  319. return ret;
  320. ksz9477_r_phy_quirks(dev, addr, reg, &val);
  321. }
  322. *data = val;
  323. return 0;
  324. }
  325. int ksz9477_w_phy(struct ksz_device *dev, u16 addr, u16 reg, u16 val)
  326. {
  327. u32 mask, val32;
  328. /* No real PHY after this. */
  329. if (!dev->info->internal_phy[addr])
  330. return 0;
  331. if (reg < 0x10)
  332. return ksz_pwrite16(dev, addr, 0x100 + (reg << 1), val);
  333. /* Errata: When using SPI, I2C, or in-band register access,
  334. * writes to certain PHY registers should be performed as
  335. * 32-bit writes instead of 16-bit writes.
  336. */
  337. val32 = val;
  338. mask = 0xffff;
  339. if ((reg & 1) == 0) {
  340. val32 <<= 16;
  341. mask <<= 16;
  342. }
  343. reg &= ~1;
  344. return ksz_prmw32(dev, addr, 0x100 + (reg << 1), mask, val32);
  345. }
  346. void ksz9477_cfg_port_member(struct ksz_device *dev, int port, u8 member)
  347. {
  348. ksz_pwrite32(dev, port, REG_PORT_VLAN_MEMBERSHIP__4, member);
  349. }
  350. void ksz9477_flush_dyn_mac_table(struct ksz_device *dev, int port)
  351. {
  352. const u16 *regs = dev->info->regs;
  353. u8 data;
  354. regmap_update_bits(ksz_regmap_8(dev), REG_SW_LUE_CTRL_2,
  355. SW_FLUSH_OPTION_M << SW_FLUSH_OPTION_S,
  356. SW_FLUSH_OPTION_DYN_MAC << SW_FLUSH_OPTION_S);
  357. if (port < dev->info->port_cnt) {
  358. /* flush individual port */
  359. ksz_pread8(dev, port, regs[P_STP_CTRL], &data);
  360. if (!(data & PORT_LEARN_DISABLE))
  361. ksz_pwrite8(dev, port, regs[P_STP_CTRL],
  362. data | PORT_LEARN_DISABLE);
  363. ksz_cfg(dev, S_FLUSH_TABLE_CTRL, SW_FLUSH_DYN_MAC_TABLE, true);
  364. ksz_pwrite8(dev, port, regs[P_STP_CTRL], data);
  365. } else {
  366. /* flush all */
  367. ksz_cfg(dev, S_FLUSH_TABLE_CTRL, SW_FLUSH_STP_TABLE, true);
  368. }
  369. }
  370. int ksz9477_port_vlan_filtering(struct ksz_device *dev, int port,
  371. bool flag, struct netlink_ext_ack *extack)
  372. {
  373. if (flag) {
  374. ksz_port_cfg(dev, port, REG_PORT_LUE_CTRL,
  375. PORT_VLAN_LOOKUP_VID_0, true);
  376. ksz_cfg(dev, REG_SW_LUE_CTRL_0, SW_VLAN_ENABLE, true);
  377. } else {
  378. ksz_cfg(dev, REG_SW_LUE_CTRL_0, SW_VLAN_ENABLE, false);
  379. ksz_port_cfg(dev, port, REG_PORT_LUE_CTRL,
  380. PORT_VLAN_LOOKUP_VID_0, false);
  381. }
  382. return 0;
  383. }
  384. int ksz9477_port_vlan_add(struct ksz_device *dev, int port,
  385. const struct switchdev_obj_port_vlan *vlan,
  386. struct netlink_ext_ack *extack)
  387. {
  388. u32 vlan_table[3];
  389. bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
  390. int err;
  391. err = ksz9477_get_vlan_table(dev, vlan->vid, vlan_table);
  392. if (err) {
  393. NL_SET_ERR_MSG_MOD(extack, "Failed to get vlan table");
  394. return err;
  395. }
  396. vlan_table[0] = VLAN_VALID | (vlan->vid & VLAN_FID_M);
  397. if (untagged)
  398. vlan_table[1] |= BIT(port);
  399. else
  400. vlan_table[1] &= ~BIT(port);
  401. vlan_table[1] &= ~(BIT(dev->cpu_port));
  402. vlan_table[2] |= BIT(port) | BIT(dev->cpu_port);
  403. err = ksz9477_set_vlan_table(dev, vlan->vid, vlan_table);
  404. if (err) {
  405. NL_SET_ERR_MSG_MOD(extack, "Failed to set vlan table");
  406. return err;
  407. }
  408. /* change PVID */
  409. if (vlan->flags & BRIDGE_VLAN_INFO_PVID)
  410. ksz_pwrite16(dev, port, REG_PORT_DEFAULT_VID, vlan->vid);
  411. return 0;
  412. }
  413. int ksz9477_port_vlan_del(struct ksz_device *dev, int port,
  414. const struct switchdev_obj_port_vlan *vlan)
  415. {
  416. bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
  417. u32 vlan_table[3];
  418. u16 pvid;
  419. ksz_pread16(dev, port, REG_PORT_DEFAULT_VID, &pvid);
  420. pvid = pvid & 0xFFF;
  421. if (ksz9477_get_vlan_table(dev, vlan->vid, vlan_table)) {
  422. dev_dbg(dev->dev, "Failed to get vlan table\n");
  423. return -ETIMEDOUT;
  424. }
  425. vlan_table[2] &= ~BIT(port);
  426. if (pvid == vlan->vid)
  427. pvid = 1;
  428. if (untagged)
  429. vlan_table[1] &= ~BIT(port);
  430. if (ksz9477_set_vlan_table(dev, vlan->vid, vlan_table)) {
  431. dev_dbg(dev->dev, "Failed to set vlan table\n");
  432. return -ETIMEDOUT;
  433. }
  434. ksz_pwrite16(dev, port, REG_PORT_DEFAULT_VID, pvid);
  435. return 0;
  436. }
  437. int ksz9477_fdb_add(struct ksz_device *dev, int port,
  438. const unsigned char *addr, u16 vid, struct dsa_db db)
  439. {
  440. u32 alu_table[4];
  441. u32 data;
  442. int ret = 0;
  443. mutex_lock(&dev->alu_mutex);
  444. /* find any entry with mac & vid */
  445. data = vid << ALU_FID_INDEX_S;
  446. data |= ((addr[0] << 8) | addr[1]);
  447. ksz_write32(dev, REG_SW_ALU_INDEX_0, data);
  448. data = ((addr[2] << 24) | (addr[3] << 16));
  449. data |= ((addr[4] << 8) | addr[5]);
  450. ksz_write32(dev, REG_SW_ALU_INDEX_1, data);
  451. /* start read operation */
  452. ksz_write32(dev, REG_SW_ALU_CTRL__4, ALU_READ | ALU_START);
  453. /* wait to be finished */
  454. ret = ksz9477_wait_alu_ready(dev);
  455. if (ret) {
  456. dev_dbg(dev->dev, "Failed to read ALU\n");
  457. goto exit;
  458. }
  459. /* read ALU entry */
  460. ksz9477_read_table(dev, alu_table);
  461. /* update ALU entry */
  462. alu_table[0] = ALU_V_STATIC_VALID;
  463. alu_table[1] |= BIT(port);
  464. if (vid)
  465. alu_table[1] |= ALU_V_USE_FID;
  466. alu_table[2] = (vid << ALU_V_FID_S);
  467. alu_table[2] |= ((addr[0] << 8) | addr[1]);
  468. alu_table[3] = ((addr[2] << 24) | (addr[3] << 16));
  469. alu_table[3] |= ((addr[4] << 8) | addr[5]);
  470. ksz9477_write_table(dev, alu_table);
  471. ksz_write32(dev, REG_SW_ALU_CTRL__4, ALU_WRITE | ALU_START);
  472. /* wait to be finished */
  473. ret = ksz9477_wait_alu_ready(dev);
  474. if (ret)
  475. dev_dbg(dev->dev, "Failed to write ALU\n");
  476. exit:
  477. mutex_unlock(&dev->alu_mutex);
  478. return ret;
  479. }
  480. int ksz9477_fdb_del(struct ksz_device *dev, int port,
  481. const unsigned char *addr, u16 vid, struct dsa_db db)
  482. {
  483. u32 alu_table[4];
  484. u32 data;
  485. int ret = 0;
  486. mutex_lock(&dev->alu_mutex);
  487. /* read any entry with mac & vid */
  488. data = vid << ALU_FID_INDEX_S;
  489. data |= ((addr[0] << 8) | addr[1]);
  490. ksz_write32(dev, REG_SW_ALU_INDEX_0, data);
  491. data = ((addr[2] << 24) | (addr[3] << 16));
  492. data |= ((addr[4] << 8) | addr[5]);
  493. ksz_write32(dev, REG_SW_ALU_INDEX_1, data);
  494. /* start read operation */
  495. ksz_write32(dev, REG_SW_ALU_CTRL__4, ALU_READ | ALU_START);
  496. /* wait to be finished */
  497. ret = ksz9477_wait_alu_ready(dev);
  498. if (ret) {
  499. dev_dbg(dev->dev, "Failed to read ALU\n");
  500. goto exit;
  501. }
  502. ksz_read32(dev, REG_SW_ALU_VAL_A, &alu_table[0]);
  503. if (alu_table[0] & ALU_V_STATIC_VALID) {
  504. ksz_read32(dev, REG_SW_ALU_VAL_B, &alu_table[1]);
  505. ksz_read32(dev, REG_SW_ALU_VAL_C, &alu_table[2]);
  506. ksz_read32(dev, REG_SW_ALU_VAL_D, &alu_table[3]);
  507. /* clear forwarding port */
  508. alu_table[1] &= ~BIT(port);
  509. /* if there is no port to forward, clear table */
  510. if ((alu_table[1] & ALU_V_PORT_MAP) == 0) {
  511. alu_table[0] = 0;
  512. alu_table[1] = 0;
  513. alu_table[2] = 0;
  514. alu_table[3] = 0;
  515. }
  516. } else {
  517. alu_table[0] = 0;
  518. alu_table[1] = 0;
  519. alu_table[2] = 0;
  520. alu_table[3] = 0;
  521. }
  522. ksz9477_write_table(dev, alu_table);
  523. ksz_write32(dev, REG_SW_ALU_CTRL__4, ALU_WRITE | ALU_START);
  524. /* wait to be finished */
  525. ret = ksz9477_wait_alu_ready(dev);
  526. if (ret)
  527. dev_dbg(dev->dev, "Failed to write ALU\n");
  528. exit:
  529. mutex_unlock(&dev->alu_mutex);
  530. return ret;
  531. }
  532. static void ksz9477_convert_alu(struct alu_struct *alu, u32 *alu_table)
  533. {
  534. alu->is_static = !!(alu_table[0] & ALU_V_STATIC_VALID);
  535. alu->is_src_filter = !!(alu_table[0] & ALU_V_SRC_FILTER);
  536. alu->is_dst_filter = !!(alu_table[0] & ALU_V_DST_FILTER);
  537. alu->prio_age = (alu_table[0] >> ALU_V_PRIO_AGE_CNT_S) &
  538. ALU_V_PRIO_AGE_CNT_M;
  539. alu->mstp = alu_table[0] & ALU_V_MSTP_M;
  540. alu->is_override = !!(alu_table[1] & ALU_V_OVERRIDE);
  541. alu->is_use_fid = !!(alu_table[1] & ALU_V_USE_FID);
  542. alu->port_forward = alu_table[1] & ALU_V_PORT_MAP;
  543. alu->fid = (alu_table[2] >> ALU_V_FID_S) & ALU_V_FID_M;
  544. alu->mac[0] = (alu_table[2] >> 8) & 0xFF;
  545. alu->mac[1] = alu_table[2] & 0xFF;
  546. alu->mac[2] = (alu_table[3] >> 24) & 0xFF;
  547. alu->mac[3] = (alu_table[3] >> 16) & 0xFF;
  548. alu->mac[4] = (alu_table[3] >> 8) & 0xFF;
  549. alu->mac[5] = alu_table[3] & 0xFF;
  550. }
  551. int ksz9477_fdb_dump(struct ksz_device *dev, int port,
  552. dsa_fdb_dump_cb_t *cb, void *data)
  553. {
  554. int ret = 0;
  555. u32 ksz_data;
  556. u32 alu_table[4];
  557. struct alu_struct alu;
  558. int timeout;
  559. mutex_lock(&dev->alu_mutex);
  560. /* start ALU search */
  561. ksz_write32(dev, REG_SW_ALU_CTRL__4, ALU_START | ALU_SEARCH);
  562. do {
  563. timeout = 1000;
  564. do {
  565. ksz_read32(dev, REG_SW_ALU_CTRL__4, &ksz_data);
  566. if ((ksz_data & ALU_VALID) || !(ksz_data & ALU_START))
  567. break;
  568. usleep_range(1, 10);
  569. } while (timeout-- > 0);
  570. if (!timeout) {
  571. dev_dbg(dev->dev, "Failed to search ALU\n");
  572. ret = -ETIMEDOUT;
  573. goto exit;
  574. }
  575. if (!(ksz_data & ALU_VALID))
  576. continue;
  577. /* read ALU table */
  578. ksz9477_read_table(dev, alu_table);
  579. ksz9477_convert_alu(&alu, alu_table);
  580. if (alu.port_forward & BIT(port)) {
  581. ret = cb(alu.mac, alu.fid, alu.is_static, data);
  582. if (ret)
  583. goto exit;
  584. }
  585. } while (ksz_data & ALU_START);
  586. exit:
  587. /* stop ALU search */
  588. ksz_write32(dev, REG_SW_ALU_CTRL__4, 0);
  589. mutex_unlock(&dev->alu_mutex);
  590. return ret;
  591. }
  592. int ksz9477_mdb_add(struct ksz_device *dev, int port,
  593. const struct switchdev_obj_port_mdb *mdb, struct dsa_db db)
  594. {
  595. u32 static_table[4];
  596. const u8 *shifts;
  597. const u32 *masks;
  598. u32 data;
  599. int index;
  600. u32 mac_hi, mac_lo;
  601. int err = 0;
  602. shifts = dev->info->shifts;
  603. masks = dev->info->masks;
  604. mac_hi = ((mdb->addr[0] << 8) | mdb->addr[1]);
  605. mac_lo = ((mdb->addr[2] << 24) | (mdb->addr[3] << 16));
  606. mac_lo |= ((mdb->addr[4] << 8) | mdb->addr[5]);
  607. mutex_lock(&dev->alu_mutex);
  608. for (index = 0; index < dev->info->num_statics; index++) {
  609. /* find empty slot first */
  610. data = (index << shifts[ALU_STAT_INDEX]) |
  611. masks[ALU_STAT_READ] | ALU_STAT_START;
  612. ksz_write32(dev, REG_SW_ALU_STAT_CTRL__4, data);
  613. /* wait to be finished */
  614. err = ksz9477_wait_alu_sta_ready(dev);
  615. if (err) {
  616. dev_dbg(dev->dev, "Failed to read ALU STATIC\n");
  617. goto exit;
  618. }
  619. /* read ALU static table */
  620. ksz9477_read_table(dev, static_table);
  621. if (static_table[0] & ALU_V_STATIC_VALID) {
  622. /* check this has same vid & mac address */
  623. if (((static_table[2] >> ALU_V_FID_S) == mdb->vid) &&
  624. ((static_table[2] & ALU_V_MAC_ADDR_HI) == mac_hi) &&
  625. static_table[3] == mac_lo) {
  626. /* found matching one */
  627. break;
  628. }
  629. } else {
  630. /* found empty one */
  631. break;
  632. }
  633. }
  634. /* no available entry */
  635. if (index == dev->info->num_statics) {
  636. err = -ENOSPC;
  637. goto exit;
  638. }
  639. /* add entry */
  640. static_table[0] = ALU_V_STATIC_VALID;
  641. static_table[1] |= BIT(port);
  642. if (mdb->vid)
  643. static_table[1] |= ALU_V_USE_FID;
  644. static_table[2] = (mdb->vid << ALU_V_FID_S);
  645. static_table[2] |= mac_hi;
  646. static_table[3] = mac_lo;
  647. ksz9477_write_table(dev, static_table);
  648. data = (index << shifts[ALU_STAT_INDEX]) | ALU_STAT_START;
  649. ksz_write32(dev, REG_SW_ALU_STAT_CTRL__4, data);
  650. /* wait to be finished */
  651. if (ksz9477_wait_alu_sta_ready(dev))
  652. dev_dbg(dev->dev, "Failed to read ALU STATIC\n");
  653. exit:
  654. mutex_unlock(&dev->alu_mutex);
  655. return err;
  656. }
  657. int ksz9477_mdb_del(struct ksz_device *dev, int port,
  658. const struct switchdev_obj_port_mdb *mdb, struct dsa_db db)
  659. {
  660. u32 static_table[4];
  661. const u8 *shifts;
  662. const u32 *masks;
  663. u32 data;
  664. int index;
  665. int ret = 0;
  666. u32 mac_hi, mac_lo;
  667. shifts = dev->info->shifts;
  668. masks = dev->info->masks;
  669. mac_hi = ((mdb->addr[0] << 8) | mdb->addr[1]);
  670. mac_lo = ((mdb->addr[2] << 24) | (mdb->addr[3] << 16));
  671. mac_lo |= ((mdb->addr[4] << 8) | mdb->addr[5]);
  672. mutex_lock(&dev->alu_mutex);
  673. for (index = 0; index < dev->info->num_statics; index++) {
  674. /* find empty slot first */
  675. data = (index << shifts[ALU_STAT_INDEX]) |
  676. masks[ALU_STAT_READ] | ALU_STAT_START;
  677. ksz_write32(dev, REG_SW_ALU_STAT_CTRL__4, data);
  678. /* wait to be finished */
  679. ret = ksz9477_wait_alu_sta_ready(dev);
  680. if (ret) {
  681. dev_dbg(dev->dev, "Failed to read ALU STATIC\n");
  682. goto exit;
  683. }
  684. /* read ALU static table */
  685. ksz9477_read_table(dev, static_table);
  686. if (static_table[0] & ALU_V_STATIC_VALID) {
  687. /* check this has same vid & mac address */
  688. if (((static_table[2] >> ALU_V_FID_S) == mdb->vid) &&
  689. ((static_table[2] & ALU_V_MAC_ADDR_HI) == mac_hi) &&
  690. static_table[3] == mac_lo) {
  691. /* found matching one */
  692. break;
  693. }
  694. }
  695. }
  696. /* no available entry */
  697. if (index == dev->info->num_statics)
  698. goto exit;
  699. /* clear port */
  700. static_table[1] &= ~BIT(port);
  701. if ((static_table[1] & ALU_V_PORT_MAP) == 0) {
  702. /* delete entry */
  703. static_table[0] = 0;
  704. static_table[1] = 0;
  705. static_table[2] = 0;
  706. static_table[3] = 0;
  707. }
  708. ksz9477_write_table(dev, static_table);
  709. data = (index << shifts[ALU_STAT_INDEX]) | ALU_STAT_START;
  710. ksz_write32(dev, REG_SW_ALU_STAT_CTRL__4, data);
  711. /* wait to be finished */
  712. ret = ksz9477_wait_alu_sta_ready(dev);
  713. if (ret)
  714. dev_dbg(dev->dev, "Failed to read ALU STATIC\n");
  715. exit:
  716. mutex_unlock(&dev->alu_mutex);
  717. return ret;
  718. }
  719. int ksz9477_port_mirror_add(struct ksz_device *dev, int port,
  720. struct dsa_mall_mirror_tc_entry *mirror,
  721. bool ingress, struct netlink_ext_ack *extack)
  722. {
  723. u8 data;
  724. int p;
  725. /* Limit to one sniffer port
  726. * Check if any of the port is already set for sniffing
  727. * If yes, instruct the user to remove the previous entry & exit
  728. */
  729. for (p = 0; p < dev->info->port_cnt; p++) {
  730. /* Skip the current sniffing port */
  731. if (p == mirror->to_local_port)
  732. continue;
  733. ksz_pread8(dev, p, P_MIRROR_CTRL, &data);
  734. if (data & PORT_MIRROR_SNIFFER) {
  735. NL_SET_ERR_MSG_MOD(extack,
  736. "Sniffer port is already configured, delete existing rules & retry");
  737. return -EBUSY;
  738. }
  739. }
  740. if (ingress)
  741. ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_RX, true);
  742. else
  743. ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_TX, true);
  744. /* configure mirror port */
  745. ksz_port_cfg(dev, mirror->to_local_port, P_MIRROR_CTRL,
  746. PORT_MIRROR_SNIFFER, true);
  747. ksz_cfg(dev, S_MIRROR_CTRL, SW_MIRROR_RX_TX, false);
  748. return 0;
  749. }
  750. void ksz9477_port_mirror_del(struct ksz_device *dev, int port,
  751. struct dsa_mall_mirror_tc_entry *mirror)
  752. {
  753. bool in_use = false;
  754. u8 data;
  755. int p;
  756. if (mirror->ingress)
  757. ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_RX, false);
  758. else
  759. ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_TX, false);
  760. /* Check if any of the port is still referring to sniffer port */
  761. for (p = 0; p < dev->info->port_cnt; p++) {
  762. ksz_pread8(dev, p, P_MIRROR_CTRL, &data);
  763. if ((data & (PORT_MIRROR_RX | PORT_MIRROR_TX))) {
  764. in_use = true;
  765. break;
  766. }
  767. }
  768. /* delete sniffing if there are no other mirroring rules */
  769. if (!in_use)
  770. ksz_port_cfg(dev, mirror->to_local_port, P_MIRROR_CTRL,
  771. PORT_MIRROR_SNIFFER, false);
  772. }
  773. static phy_interface_t ksz9477_get_interface(struct ksz_device *dev, int port)
  774. {
  775. phy_interface_t interface;
  776. bool gbit;
  777. if (dev->info->internal_phy[port])
  778. return PHY_INTERFACE_MODE_NA;
  779. gbit = ksz_get_gbit(dev, port);
  780. interface = ksz_get_xmii(dev, port, gbit);
  781. return interface;
  782. }
  783. void ksz9477_get_caps(struct ksz_device *dev, int port,
  784. struct phylink_config *config)
  785. {
  786. config->mac_capabilities = MAC_10 | MAC_100 | MAC_ASYM_PAUSE |
  787. MAC_SYM_PAUSE;
  788. if (dev->info->gbit_capable[port])
  789. config->mac_capabilities |= MAC_1000FD;
  790. }
  791. int ksz9477_set_ageing_time(struct ksz_device *dev, unsigned int msecs)
  792. {
  793. u32 secs = msecs / 1000;
  794. u8 data, mult, value;
  795. u32 max_val;
  796. int ret;
  797. #define MAX_TIMER_VAL ((1 << 8) - 1)
  798. /* The aging timer comprises a 3-bit multiplier and an 8-bit second
  799. * value. Either of them cannot be zero. The maximum timer is then
  800. * 7 * 255 = 1785 seconds.
  801. */
  802. if (!secs)
  803. secs = 1;
  804. /* Return error if too large. */
  805. else if (secs > 7 * MAX_TIMER_VAL)
  806. return -EINVAL;
  807. ret = ksz_read8(dev, REG_SW_LUE_CTRL_0, &value);
  808. if (ret < 0)
  809. return ret;
  810. /* Check whether there is need to update the multiplier. */
  811. mult = FIELD_GET(SW_AGE_CNT_M, value);
  812. max_val = MAX_TIMER_VAL;
  813. if (mult > 0) {
  814. /* Try to use the same multiplier already in the register as
  815. * the hardware default uses multiplier 4 and 75 seconds for
  816. * 300 seconds.
  817. */
  818. max_val = DIV_ROUND_UP(secs, mult);
  819. if (max_val > MAX_TIMER_VAL || max_val * mult != secs)
  820. max_val = MAX_TIMER_VAL;
  821. }
  822. data = DIV_ROUND_UP(secs, max_val);
  823. if (mult != data) {
  824. value &= ~SW_AGE_CNT_M;
  825. value |= FIELD_PREP(SW_AGE_CNT_M, data);
  826. ret = ksz_write8(dev, REG_SW_LUE_CTRL_0, value);
  827. if (ret < 0)
  828. return ret;
  829. }
  830. value = DIV_ROUND_UP(secs, data);
  831. return ksz_write8(dev, REG_SW_LUE_CTRL_3, value);
  832. }
  833. void ksz9477_port_queue_split(struct ksz_device *dev, int port)
  834. {
  835. u8 data;
  836. if (dev->info->num_tx_queues == 8)
  837. data = PORT_EIGHT_QUEUE;
  838. else if (dev->info->num_tx_queues == 4)
  839. data = PORT_FOUR_QUEUE;
  840. else if (dev->info->num_tx_queues == 2)
  841. data = PORT_TWO_QUEUE;
  842. else
  843. data = PORT_SINGLE_QUEUE;
  844. ksz_prmw8(dev, port, REG_PORT_CTRL_0, PORT_QUEUE_SPLIT_MASK, data);
  845. }
  846. void ksz9477_port_setup(struct ksz_device *dev, int port, bool cpu_port)
  847. {
  848. const u16 *regs = dev->info->regs;
  849. struct dsa_switch *ds = dev->ds;
  850. u16 data16;
  851. u8 member;
  852. /* enable tag tail for host port */
  853. if (cpu_port)
  854. ksz_port_cfg(dev, port, REG_PORT_CTRL_0, PORT_TAIL_TAG_ENABLE,
  855. true);
  856. ksz9477_port_queue_split(dev, port);
  857. ksz_port_cfg(dev, port, REG_PORT_CTRL_0, PORT_MAC_LOOPBACK, false);
  858. /* set back pressure */
  859. ksz_port_cfg(dev, port, REG_PORT_MAC_CTRL_1, PORT_BACK_PRESSURE, true);
  860. /* enable broadcast storm limit */
  861. ksz_port_cfg(dev, port, P_BCAST_STORM_CTRL, PORT_BROADCAST_STORM, true);
  862. /* replace priority */
  863. ksz_port_cfg(dev, port, REG_PORT_MRI_MAC_CTRL, PORT_USER_PRIO_CEILING,
  864. false);
  865. ksz9477_port_cfg32(dev, port, REG_PORT_MTI_QUEUE_CTRL_0__4,
  866. MTI_PVID_REPLACE, false);
  867. /* force flow control for non-PHY ports only */
  868. ksz_port_cfg(dev, port, REG_PORT_CTRL_0,
  869. PORT_FORCE_TX_FLOW_CTRL | PORT_FORCE_RX_FLOW_CTRL,
  870. !dev->info->internal_phy[port]);
  871. if (cpu_port)
  872. member = dsa_user_ports(ds);
  873. else
  874. member = BIT(dsa_upstream_port(ds, port));
  875. ksz9477_cfg_port_member(dev, port, member);
  876. /* clear pending interrupts */
  877. if (dev->info->internal_phy[port])
  878. ksz_pread16(dev, port, REG_PORT_PHY_INT_ENABLE, &data16);
  879. ksz9477_port_acl_init(dev, port);
  880. /* clear pending wake flags */
  881. ksz_handle_wake_reason(dev, port);
  882. /* Disable all WoL options by default. Otherwise
  883. * ksz_switch_macaddr_get/put logic will not work properly.
  884. */
  885. ksz_pwrite8(dev, port, regs[REG_PORT_PME_CTRL], 0);
  886. }
  887. void ksz9477_config_cpu_port(struct dsa_switch *ds)
  888. {
  889. struct ksz_device *dev = ds->priv;
  890. struct ksz_port *p;
  891. int i;
  892. for (i = 0; i < dev->info->port_cnt; i++) {
  893. if (dsa_is_cpu_port(ds, i) &&
  894. (dev->info->cpu_ports & (1 << i))) {
  895. phy_interface_t interface;
  896. const char *prev_msg;
  897. const char *prev_mode;
  898. dev->cpu_port = i;
  899. p = &dev->ports[i];
  900. /* Read from XMII register to determine host port
  901. * interface. If set specifically in device tree
  902. * note the difference to help debugging.
  903. */
  904. interface = ksz9477_get_interface(dev, i);
  905. if (!p->interface) {
  906. if (dev->compat_interface) {
  907. dev_warn(dev->dev,
  908. "Using legacy switch \"phy-mode\" property, because it is missing on port %d node. "
  909. "Please update your device tree.\n",
  910. i);
  911. p->interface = dev->compat_interface;
  912. } else {
  913. p->interface = interface;
  914. }
  915. }
  916. if (interface && interface != p->interface) {
  917. prev_msg = " instead of ";
  918. prev_mode = phy_modes(interface);
  919. } else {
  920. prev_msg = "";
  921. prev_mode = "";
  922. }
  923. dev_info(dev->dev,
  924. "Port%d: using phy mode %s%s%s\n",
  925. i,
  926. phy_modes(p->interface),
  927. prev_msg,
  928. prev_mode);
  929. /* enable cpu port */
  930. ksz9477_port_setup(dev, i, true);
  931. }
  932. }
  933. for (i = 0; i < dev->info->port_cnt; i++) {
  934. if (i == dev->cpu_port)
  935. continue;
  936. ksz_port_stp_state_set(ds, i, BR_STATE_DISABLED);
  937. }
  938. }
  939. int ksz9477_enable_stp_addr(struct ksz_device *dev)
  940. {
  941. const u32 *masks;
  942. u32 data;
  943. int ret;
  944. masks = dev->info->masks;
  945. /* Enable Reserved multicast table */
  946. ksz_cfg(dev, REG_SW_LUE_CTRL_0, SW_RESV_MCAST_ENABLE, true);
  947. /* Set the Override bit for forwarding BPDU packet to CPU */
  948. ret = ksz_write32(dev, REG_SW_ALU_VAL_B,
  949. ALU_V_OVERRIDE | BIT(dev->cpu_port));
  950. if (ret < 0)
  951. return ret;
  952. data = ALU_STAT_START | ALU_RESV_MCAST_ADDR | masks[ALU_STAT_WRITE];
  953. ret = ksz_write32(dev, REG_SW_ALU_STAT_CTRL__4, data);
  954. if (ret < 0)
  955. return ret;
  956. /* wait to be finished */
  957. ret = ksz9477_wait_alu_sta_ready(dev);
  958. if (ret < 0) {
  959. dev_err(dev->dev, "Failed to update Reserved Multicast table\n");
  960. return ret;
  961. }
  962. return 0;
  963. }
  964. int ksz9477_setup(struct dsa_switch *ds)
  965. {
  966. struct ksz_device *dev = ds->priv;
  967. const u16 *regs = dev->info->regs;
  968. int ret = 0;
  969. ds->mtu_enforcement_ingress = true;
  970. /* Required for port partitioning. */
  971. ksz9477_cfg32(dev, REG_SW_QM_CTRL__4, UNICAST_VLAN_BOUNDARY,
  972. true);
  973. /* Do not work correctly with tail tagging. */
  974. ksz_cfg(dev, REG_SW_MAC_CTRL_0, SW_CHECK_LENGTH, false);
  975. /* Enable REG_SW_MTU__2 reg by setting SW_JUMBO_PACKET */
  976. ksz_cfg(dev, REG_SW_MAC_CTRL_1, SW_JUMBO_PACKET, true);
  977. /* Use collision based back pressure mode. */
  978. ksz_cfg(dev, REG_SW_MAC_CTRL_1, SW_BACK_PRESSURE,
  979. SW_BACK_PRESSURE_COLLISION);
  980. /* Now we can configure default MTU value */
  981. ret = regmap_update_bits(ksz_regmap_16(dev), REG_SW_MTU__2, REG_SW_MTU_MASK,
  982. VLAN_ETH_FRAME_LEN + ETH_FCS_LEN);
  983. if (ret)
  984. return ret;
  985. /* queue based egress rate limit */
  986. ksz_cfg(dev, REG_SW_MAC_CTRL_5, SW_OUT_RATE_LIMIT_QUEUE_BASED, true);
  987. /* enable global MIB counter freeze function */
  988. ksz_cfg(dev, REG_SW_MAC_CTRL_6, SW_MIB_COUNTER_FREEZE, true);
  989. /* Make sure PME (WoL) is not enabled. If requested, it will
  990. * be enabled by ksz_wol_pre_shutdown(). Otherwise, some PMICs
  991. * do not like PME events changes before shutdown.
  992. */
  993. return ksz_write8(dev, regs[REG_SW_PME_CTRL], 0);
  994. }
  995. u32 ksz9477_get_port_addr(int port, int offset)
  996. {
  997. return PORT_CTRL_ADDR(port, offset);
  998. }
  999. int ksz9477_tc_cbs_set_cinc(struct ksz_device *dev, int port, u32 val)
  1000. {
  1001. val = val >> 8;
  1002. return ksz_pwrite16(dev, port, REG_PORT_MTI_CREDIT_INCREMENT, val);
  1003. }
  1004. /* The KSZ9477 provides following HW features to accelerate
  1005. * HSR frames handling:
  1006. *
  1007. * 1. TX PACKET DUPLICATION FROM HOST TO SWITCH
  1008. * 2. RX PACKET DUPLICATION DISCARDING
  1009. * 3. PREVENTING PACKET LOOP IN THE RING BY SELF-ADDRESS FILTERING
  1010. *
  1011. * Only one from point 1. has the NETIF_F* flag available.
  1012. *
  1013. * Ones from point 2 and 3 are "best effort" - i.e. those will
  1014. * work correctly most of the time, but it may happen that some
  1015. * frames will not be caught - to be more specific; there is a race
  1016. * condition in hardware such that, when duplicate packets are received
  1017. * on member ports very close in time to each other, the hardware fails
  1018. * to detect that they are duplicates.
  1019. *
  1020. * Hence, the SW needs to handle those special cases. However, the speed
  1021. * up gain is considerable when above features are used.
  1022. *
  1023. * Moreover, the NETIF_F_HW_HSR_FWD feature is also enabled, as HSR frames
  1024. * can be forwarded in the switch fabric between HSR ports.
  1025. */
  1026. #define KSZ9477_SUPPORTED_HSR_FEATURES (NETIF_F_HW_HSR_DUP | NETIF_F_HW_HSR_FWD)
  1027. void ksz9477_hsr_join(struct dsa_switch *ds, int port, struct net_device *hsr)
  1028. {
  1029. struct ksz_device *dev = ds->priv;
  1030. struct net_device *user;
  1031. struct dsa_port *hsr_dp;
  1032. u8 data, hsr_ports = 0;
  1033. /* Program which port(s) shall support HSR */
  1034. ksz_rmw32(dev, REG_HSR_PORT_MAP__4, BIT(port), BIT(port));
  1035. /* Forward frames between HSR ports (i.e. bridge together HSR ports) */
  1036. if (dev->hsr_ports) {
  1037. dsa_hsr_foreach_port(hsr_dp, ds, hsr)
  1038. hsr_ports |= BIT(hsr_dp->index);
  1039. hsr_ports |= BIT(dsa_upstream_port(ds, port));
  1040. dsa_hsr_foreach_port(hsr_dp, ds, hsr)
  1041. ksz9477_cfg_port_member(dev, hsr_dp->index, hsr_ports);
  1042. }
  1043. if (!dev->hsr_ports) {
  1044. /* Enable discarding of received HSR frames */
  1045. ksz_read8(dev, REG_HSR_ALU_CTRL_0__1, &data);
  1046. data |= HSR_DUPLICATE_DISCARD;
  1047. data &= ~HSR_NODE_UNICAST;
  1048. ksz_write8(dev, REG_HSR_ALU_CTRL_0__1, data);
  1049. }
  1050. /* Enable per port self-address filtering.
  1051. * The global self-address filtering has already been enabled in the
  1052. * ksz9477_reset_switch() function.
  1053. */
  1054. ksz_port_cfg(dev, port, REG_PORT_LUE_CTRL, PORT_SRC_ADDR_FILTER, true);
  1055. /* Setup HW supported features for lan HSR ports */
  1056. user = dsa_to_port(ds, port)->user;
  1057. user->features |= KSZ9477_SUPPORTED_HSR_FEATURES;
  1058. }
  1059. void ksz9477_hsr_leave(struct dsa_switch *ds, int port, struct net_device *hsr)
  1060. {
  1061. struct ksz_device *dev = ds->priv;
  1062. /* Clear port HSR support */
  1063. ksz_rmw32(dev, REG_HSR_PORT_MAP__4, BIT(port), 0);
  1064. /* Disable forwarding frames between HSR ports */
  1065. ksz9477_cfg_port_member(dev, port, BIT(dsa_upstream_port(ds, port)));
  1066. /* Disable per port self-address filtering */
  1067. ksz_port_cfg(dev, port, REG_PORT_LUE_CTRL, PORT_SRC_ADDR_FILTER, false);
  1068. }
  1069. int ksz9477_switch_init(struct ksz_device *dev)
  1070. {
  1071. u8 data8;
  1072. int ret;
  1073. dev->port_mask = (1 << dev->info->port_cnt) - 1;
  1074. /* turn off SPI DO Edge select */
  1075. ret = ksz_read8(dev, REG_SW_GLOBAL_SERIAL_CTRL_0, &data8);
  1076. if (ret)
  1077. return ret;
  1078. data8 &= ~SPI_AUTO_EDGE_DETECTION;
  1079. ret = ksz_write8(dev, REG_SW_GLOBAL_SERIAL_CTRL_0, data8);
  1080. if (ret)
  1081. return ret;
  1082. return 0;
  1083. }
  1084. void ksz9477_switch_exit(struct ksz_device *dev)
  1085. {
  1086. ksz9477_reset_switch(dev);
  1087. }
  1088. MODULE_AUTHOR("Woojung Huh <Woojung.Huh@microchip.com>");
  1089. MODULE_DESCRIPTION("Microchip KSZ9477 Series Switch DSA Driver");
  1090. MODULE_LICENSE("GPL");