ksz8.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Microchip KSZ8XXX series switch driver
  4. *
  5. * It supports the following switches:
  6. * - KSZ8863, KSZ8873 aka KSZ88X3
  7. * - KSZ8895, KSZ8864 aka KSZ8895 family
  8. * - KSZ8794, KSZ8795, KSZ8765 aka KSZ87XX
  9. * Note that it does NOT support:
  10. * - KSZ8563, KSZ8567 - see KSZ9477 driver
  11. *
  12. * Copyright (C) 2017 Microchip Technology Inc.
  13. * Tristram Ha <Tristram.Ha@microchip.com>
  14. */
  15. #include <linux/bitfield.h>
  16. #include <linux/delay.h>
  17. #include <linux/export.h>
  18. #include <linux/gpio.h>
  19. #include <linux/if_vlan.h>
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/platform_data/microchip-ksz.h>
  23. #include <linux/phy.h>
  24. #include <linux/etherdevice.h>
  25. #include <linux/if_bridge.h>
  26. #include <linux/micrel_phy.h>
  27. #include <net/dsa.h>
  28. #include <net/switchdev.h>
  29. #include <linux/phylink.h>
  30. #include "ksz_common.h"
  31. #include "ksz8_reg.h"
  32. #include "ksz8.h"
  33. static void ksz_cfg(struct ksz_device *dev, u32 addr, u8 bits, bool set)
  34. {
  35. regmap_update_bits(ksz_regmap_8(dev), addr, bits, set ? bits : 0);
  36. }
  37. static void ksz_port_cfg(struct ksz_device *dev, int port, int offset, u8 bits,
  38. bool set)
  39. {
  40. regmap_update_bits(ksz_regmap_8(dev), PORT_CTRL_ADDR(port, offset),
  41. bits, set ? bits : 0);
  42. }
  43. /**
  44. * ksz8_ind_write8 - EEE/ACL/PME indirect register write
  45. * @dev: The device structure.
  46. * @table: Function & table select, register 110.
  47. * @addr: Indirect access control, register 111.
  48. * @data: The data to be written.
  49. *
  50. * This function performs an indirect register write for EEE, ACL or
  51. * PME switch functionalities. Both 8-bit registers 110 and 111 are
  52. * written at once with ksz_write16, using the serial multiple write
  53. * functionality.
  54. *
  55. * Return: 0 on success, or an error code on failure.
  56. */
  57. static int ksz8_ind_write8(struct ksz_device *dev, u8 table, u16 addr, u8 data)
  58. {
  59. const u16 *regs;
  60. u16 ctrl_addr;
  61. int ret = 0;
  62. regs = dev->info->regs;
  63. mutex_lock(&dev->alu_mutex);
  64. ctrl_addr = IND_ACC_TABLE(table) | addr;
  65. ret = ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr);
  66. if (!ret)
  67. ret = ksz_write8(dev, regs[REG_IND_BYTE], data);
  68. mutex_unlock(&dev->alu_mutex);
  69. return ret;
  70. }
  71. /**
  72. * ksz8_ind_read8 - EEE/ACL/PME indirect register read
  73. * @dev: The device structure.
  74. * @table: Function & table select, register 110.
  75. * @addr: Indirect access control, register 111.
  76. * @val: The value read.
  77. *
  78. * This function performs an indirect register read for EEE, ACL or
  79. * PME switch functionalities. Both 8-bit registers 110 and 111 are
  80. * written at once with ksz_write16, using the serial multiple write
  81. * functionality.
  82. *
  83. * Return: 0 on success, or an error code on failure.
  84. */
  85. static int ksz8_ind_read8(struct ksz_device *dev, u8 table, u16 addr, u8 *val)
  86. {
  87. const u16 *regs;
  88. u16 ctrl_addr;
  89. int ret = 0;
  90. regs = dev->info->regs;
  91. mutex_lock(&dev->alu_mutex);
  92. ctrl_addr = IND_ACC_TABLE(table | TABLE_READ) | addr;
  93. ret = ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr);
  94. if (!ret)
  95. ret = ksz_read8(dev, regs[REG_IND_BYTE], val);
  96. mutex_unlock(&dev->alu_mutex);
  97. return ret;
  98. }
  99. int ksz8_pme_write8(struct ksz_device *dev, u32 reg, u8 value)
  100. {
  101. return ksz8_ind_write8(dev, (u8)(reg >> 8), (u8)(reg), value);
  102. }
  103. int ksz8_pme_pread8(struct ksz_device *dev, int port, int offset, u8 *data)
  104. {
  105. u8 table = (u8)(offset >> 8 | (port + 1));
  106. return ksz8_ind_read8(dev, table, (u8)(offset), data);
  107. }
  108. int ksz8_pme_pwrite8(struct ksz_device *dev, int port, int offset, u8 data)
  109. {
  110. u8 table = (u8)(offset >> 8 | (port + 1));
  111. return ksz8_ind_write8(dev, table, (u8)(offset), data);
  112. }
  113. int ksz8_reset_switch(struct ksz_device *dev)
  114. {
  115. if (ksz_is_ksz88x3(dev)) {
  116. /* reset switch */
  117. ksz_cfg(dev, KSZ8863_REG_SW_RESET,
  118. KSZ8863_GLOBAL_SOFTWARE_RESET | KSZ8863_PCS_RESET, true);
  119. ksz_cfg(dev, KSZ8863_REG_SW_RESET,
  120. KSZ8863_GLOBAL_SOFTWARE_RESET | KSZ8863_PCS_RESET, false);
  121. } else {
  122. /* reset switch */
  123. ksz_write8(dev, REG_POWER_MANAGEMENT_1,
  124. SW_SOFTWARE_POWER_DOWN << SW_POWER_MANAGEMENT_MODE_S);
  125. ksz_write8(dev, REG_POWER_MANAGEMENT_1, 0);
  126. }
  127. return 0;
  128. }
  129. static int ksz8863_change_mtu(struct ksz_device *dev, int frame_size)
  130. {
  131. u8 ctrl2 = 0;
  132. if (frame_size <= KSZ8_LEGAL_PACKET_SIZE)
  133. ctrl2 |= KSZ8863_LEGAL_PACKET_ENABLE;
  134. else if (frame_size > KSZ8863_NORMAL_PACKET_SIZE)
  135. ctrl2 |= KSZ8863_HUGE_PACKET_ENABLE;
  136. return ksz_rmw8(dev, REG_SW_CTRL_2, KSZ8863_LEGAL_PACKET_ENABLE |
  137. KSZ8863_HUGE_PACKET_ENABLE, ctrl2);
  138. }
  139. static int ksz8795_change_mtu(struct ksz_device *dev, int frame_size)
  140. {
  141. u8 ctrl1 = 0, ctrl2 = 0;
  142. int ret;
  143. if (frame_size > KSZ8_LEGAL_PACKET_SIZE)
  144. ctrl2 |= SW_LEGAL_PACKET_DISABLE;
  145. if (frame_size > KSZ8863_NORMAL_PACKET_SIZE)
  146. ctrl1 |= SW_HUGE_PACKET;
  147. ret = ksz_rmw8(dev, REG_SW_CTRL_1, SW_HUGE_PACKET, ctrl1);
  148. if (ret)
  149. return ret;
  150. return ksz_rmw8(dev, REG_SW_CTRL_2, SW_LEGAL_PACKET_DISABLE, ctrl2);
  151. }
  152. int ksz8_change_mtu(struct ksz_device *dev, int port, int mtu)
  153. {
  154. u16 frame_size;
  155. if (!dsa_is_cpu_port(dev->ds, port))
  156. return 0;
  157. frame_size = mtu + VLAN_ETH_HLEN + ETH_FCS_LEN;
  158. switch (dev->chip_id) {
  159. case KSZ8795_CHIP_ID:
  160. case KSZ8794_CHIP_ID:
  161. case KSZ8765_CHIP_ID:
  162. return ksz8795_change_mtu(dev, frame_size);
  163. case KSZ88X3_CHIP_ID:
  164. case KSZ8864_CHIP_ID:
  165. case KSZ8895_CHIP_ID:
  166. return ksz8863_change_mtu(dev, frame_size);
  167. }
  168. return -EOPNOTSUPP;
  169. }
  170. static int ksz8_port_queue_split(struct ksz_device *dev, int port, int queues)
  171. {
  172. u8 mask_4q, mask_2q;
  173. u8 reg_4q, reg_2q;
  174. u8 data_4q = 0;
  175. u8 data_2q = 0;
  176. int ret;
  177. if (ksz_is_ksz88x3(dev)) {
  178. mask_4q = KSZ8873_PORT_4QUEUE_SPLIT_EN;
  179. mask_2q = KSZ8873_PORT_2QUEUE_SPLIT_EN;
  180. reg_4q = REG_PORT_CTRL_0;
  181. reg_2q = REG_PORT_CTRL_2;
  182. /* KSZ8795 family switches have Weighted Fair Queueing (WFQ)
  183. * enabled by default. Enable it for KSZ8873 family switches
  184. * too. Default value for KSZ8873 family is strict priority,
  185. * which should be enabled by using TC_SETUP_QDISC_ETS, not
  186. * by default.
  187. */
  188. ret = ksz_rmw8(dev, REG_SW_CTRL_3, WEIGHTED_FAIR_QUEUE_ENABLE,
  189. WEIGHTED_FAIR_QUEUE_ENABLE);
  190. if (ret)
  191. return ret;
  192. } else {
  193. mask_4q = KSZ8795_PORT_4QUEUE_SPLIT_EN;
  194. mask_2q = KSZ8795_PORT_2QUEUE_SPLIT_EN;
  195. reg_4q = REG_PORT_CTRL_13;
  196. reg_2q = REG_PORT_CTRL_0;
  197. /* TODO: this is legacy from initial KSZ8795 driver, should be
  198. * moved to appropriate place in the future.
  199. */
  200. ret = ksz_rmw8(dev, REG_SW_CTRL_19,
  201. SW_OUT_RATE_LIMIT_QUEUE_BASED,
  202. SW_OUT_RATE_LIMIT_QUEUE_BASED);
  203. if (ret)
  204. return ret;
  205. }
  206. if (queues == 4)
  207. data_4q = mask_4q;
  208. else if (queues == 2)
  209. data_2q = mask_2q;
  210. ret = ksz_prmw8(dev, port, reg_4q, mask_4q, data_4q);
  211. if (ret)
  212. return ret;
  213. return ksz_prmw8(dev, port, reg_2q, mask_2q, data_2q);
  214. }
  215. int ksz8_all_queues_split(struct ksz_device *dev, int queues)
  216. {
  217. struct dsa_switch *ds = dev->ds;
  218. const struct dsa_port *dp;
  219. dsa_switch_for_each_port(dp, ds) {
  220. int ret = ksz8_port_queue_split(dev, dp->index, queues);
  221. if (ret)
  222. return ret;
  223. }
  224. return 0;
  225. }
  226. void ksz8_r_mib_cnt(struct ksz_device *dev, int port, u16 addr, u64 *cnt)
  227. {
  228. const u32 *masks;
  229. const u16 *regs;
  230. u16 ctrl_addr;
  231. u32 data;
  232. u8 check;
  233. int loop;
  234. masks = dev->info->masks;
  235. regs = dev->info->regs;
  236. ctrl_addr = addr + dev->info->reg_mib_cnt * port;
  237. ctrl_addr |= IND_ACC_TABLE(TABLE_MIB | TABLE_READ);
  238. mutex_lock(&dev->alu_mutex);
  239. ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr);
  240. /* It is almost guaranteed to always read the valid bit because of
  241. * slow SPI speed.
  242. */
  243. for (loop = 2; loop > 0; loop--) {
  244. ksz_read8(dev, regs[REG_IND_MIB_CHECK], &check);
  245. if (check & masks[MIB_COUNTER_VALID]) {
  246. ksz_read32(dev, regs[REG_IND_DATA_LO], &data);
  247. if (check & masks[MIB_COUNTER_OVERFLOW])
  248. *cnt += MIB_COUNTER_VALUE + 1;
  249. *cnt += data & MIB_COUNTER_VALUE;
  250. break;
  251. }
  252. }
  253. mutex_unlock(&dev->alu_mutex);
  254. }
  255. static void ksz8795_r_mib_pkt(struct ksz_device *dev, int port, u16 addr,
  256. u64 *dropped, u64 *cnt)
  257. {
  258. const u32 *masks;
  259. const u16 *regs;
  260. u16 ctrl_addr;
  261. u32 data;
  262. u8 check;
  263. int loop;
  264. masks = dev->info->masks;
  265. regs = dev->info->regs;
  266. addr -= dev->info->reg_mib_cnt;
  267. ctrl_addr = (KSZ8795_MIB_TOTAL_RX_1 - KSZ8795_MIB_TOTAL_RX_0) * port;
  268. ctrl_addr += addr + KSZ8795_MIB_TOTAL_RX_0;
  269. ctrl_addr |= IND_ACC_TABLE(TABLE_MIB | TABLE_READ);
  270. mutex_lock(&dev->alu_mutex);
  271. ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr);
  272. /* It is almost guaranteed to always read the valid bit because of
  273. * slow SPI speed.
  274. */
  275. for (loop = 2; loop > 0; loop--) {
  276. ksz_read8(dev, regs[REG_IND_MIB_CHECK], &check);
  277. if (check & masks[MIB_COUNTER_VALID]) {
  278. ksz_read32(dev, regs[REG_IND_DATA_LO], &data);
  279. if (addr < 2) {
  280. u64 total;
  281. total = check & MIB_TOTAL_BYTES_H;
  282. total <<= 32;
  283. *cnt += total;
  284. *cnt += data;
  285. if (check & masks[MIB_COUNTER_OVERFLOW]) {
  286. total = MIB_TOTAL_BYTES_H + 1;
  287. total <<= 32;
  288. *cnt += total;
  289. }
  290. } else {
  291. if (check & masks[MIB_COUNTER_OVERFLOW])
  292. *cnt += MIB_PACKET_DROPPED + 1;
  293. *cnt += data & MIB_PACKET_DROPPED;
  294. }
  295. break;
  296. }
  297. }
  298. mutex_unlock(&dev->alu_mutex);
  299. }
  300. static void ksz8863_r_mib_pkt(struct ksz_device *dev, int port, u16 addr,
  301. u64 *dropped, u64 *cnt)
  302. {
  303. u32 *last = (u32 *)dropped;
  304. const u16 *regs;
  305. u16 ctrl_addr;
  306. u32 data;
  307. u32 cur;
  308. regs = dev->info->regs;
  309. addr -= dev->info->reg_mib_cnt;
  310. ctrl_addr = addr ? KSZ8863_MIB_PACKET_DROPPED_TX_0 :
  311. KSZ8863_MIB_PACKET_DROPPED_RX_0;
  312. if (ksz_is_8895_family(dev) &&
  313. ctrl_addr == KSZ8863_MIB_PACKET_DROPPED_RX_0)
  314. ctrl_addr = KSZ8895_MIB_PACKET_DROPPED_RX_0;
  315. ctrl_addr += port;
  316. ctrl_addr |= IND_ACC_TABLE(TABLE_MIB | TABLE_READ);
  317. mutex_lock(&dev->alu_mutex);
  318. ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr);
  319. ksz_read32(dev, regs[REG_IND_DATA_LO], &data);
  320. mutex_unlock(&dev->alu_mutex);
  321. data &= MIB_PACKET_DROPPED;
  322. cur = last[addr];
  323. if (data != cur) {
  324. last[addr] = data;
  325. if (data < cur)
  326. data += MIB_PACKET_DROPPED + 1;
  327. data -= cur;
  328. *cnt += data;
  329. }
  330. }
  331. void ksz8_r_mib_pkt(struct ksz_device *dev, int port, u16 addr,
  332. u64 *dropped, u64 *cnt)
  333. {
  334. if (is_ksz88xx(dev))
  335. ksz8863_r_mib_pkt(dev, port, addr, dropped, cnt);
  336. else
  337. ksz8795_r_mib_pkt(dev, port, addr, dropped, cnt);
  338. }
  339. void ksz8_freeze_mib(struct ksz_device *dev, int port, bool freeze)
  340. {
  341. if (is_ksz88xx(dev))
  342. return;
  343. /* enable the port for flush/freeze function */
  344. if (freeze)
  345. ksz_cfg(dev, REG_SW_CTRL_6, BIT(port), true);
  346. ksz_cfg(dev, REG_SW_CTRL_6, SW_MIB_COUNTER_FREEZE, freeze);
  347. /* disable the port after freeze is done */
  348. if (!freeze)
  349. ksz_cfg(dev, REG_SW_CTRL_6, BIT(port), false);
  350. }
  351. void ksz8_port_init_cnt(struct ksz_device *dev, int port)
  352. {
  353. struct ksz_port_mib *mib = &dev->ports[port].mib;
  354. u64 *dropped;
  355. /* For KSZ8795 family. */
  356. if (ksz_is_ksz87xx(dev)) {
  357. /* flush all enabled port MIB counters */
  358. ksz_cfg(dev, REG_SW_CTRL_6, BIT(port), true);
  359. ksz_cfg(dev, REG_SW_CTRL_6, SW_MIB_COUNTER_FLUSH, true);
  360. ksz_cfg(dev, REG_SW_CTRL_6, BIT(port), false);
  361. }
  362. mib->cnt_ptr = 0;
  363. /* Some ports may not have MIB counters before SWITCH_COUNTER_NUM. */
  364. while (mib->cnt_ptr < dev->info->reg_mib_cnt) {
  365. dev->dev_ops->r_mib_cnt(dev, port, mib->cnt_ptr,
  366. &mib->counters[mib->cnt_ptr]);
  367. ++mib->cnt_ptr;
  368. }
  369. /* last one in storage */
  370. dropped = &mib->counters[dev->info->mib_cnt];
  371. /* Some ports may not have MIB counters after SWITCH_COUNTER_NUM. */
  372. while (mib->cnt_ptr < dev->info->mib_cnt) {
  373. dev->dev_ops->r_mib_pkt(dev, port, mib->cnt_ptr,
  374. dropped, &mib->counters[mib->cnt_ptr]);
  375. ++mib->cnt_ptr;
  376. }
  377. }
  378. static int ksz8_r_table(struct ksz_device *dev, int table, u16 addr, u64 *data)
  379. {
  380. const u16 *regs;
  381. u16 ctrl_addr;
  382. int ret;
  383. regs = dev->info->regs;
  384. ctrl_addr = IND_ACC_TABLE(table | TABLE_READ) | addr;
  385. mutex_lock(&dev->alu_mutex);
  386. ret = ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr);
  387. if (ret)
  388. goto unlock_alu;
  389. ret = ksz_read64(dev, regs[REG_IND_DATA_HI], data);
  390. unlock_alu:
  391. mutex_unlock(&dev->alu_mutex);
  392. return ret;
  393. }
  394. static int ksz8_w_table(struct ksz_device *dev, int table, u16 addr, u64 data)
  395. {
  396. const u16 *regs;
  397. u16 ctrl_addr;
  398. int ret;
  399. regs = dev->info->regs;
  400. ctrl_addr = IND_ACC_TABLE(table) | addr;
  401. mutex_lock(&dev->alu_mutex);
  402. ret = ksz_write64(dev, regs[REG_IND_DATA_HI], data);
  403. if (ret)
  404. goto unlock_alu;
  405. ret = ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr);
  406. unlock_alu:
  407. mutex_unlock(&dev->alu_mutex);
  408. return ret;
  409. }
  410. static int ksz8_valid_dyn_entry(struct ksz_device *dev, u8 *data)
  411. {
  412. int timeout = 100;
  413. const u32 *masks;
  414. const u16 *regs;
  415. int ret;
  416. masks = dev->info->masks;
  417. regs = dev->info->regs;
  418. do {
  419. ret = ksz_read8(dev, regs[REG_IND_DATA_CHECK], data);
  420. if (ret)
  421. return ret;
  422. timeout--;
  423. } while ((*data & masks[DYNAMIC_MAC_TABLE_NOT_READY]) && timeout);
  424. /* Entry is not ready for accessing. */
  425. if (*data & masks[DYNAMIC_MAC_TABLE_NOT_READY])
  426. return -ETIMEDOUT;
  427. /* Entry is ready for accessing. */
  428. return ksz_read8(dev, regs[REG_IND_DATA_8], data);
  429. }
  430. static int ksz8_r_dyn_mac_table(struct ksz_device *dev, u16 addr, u8 *mac_addr,
  431. u8 *fid, u8 *src_port, u16 *entries)
  432. {
  433. u32 data_hi, data_lo;
  434. const u8 *shifts;
  435. const u32 *masks;
  436. const u16 *regs;
  437. u16 ctrl_addr;
  438. u64 buf = 0;
  439. u8 data;
  440. int cnt;
  441. int ret;
  442. shifts = dev->info->shifts;
  443. masks = dev->info->masks;
  444. regs = dev->info->regs;
  445. ctrl_addr = IND_ACC_TABLE(TABLE_DYNAMIC_MAC | TABLE_READ) | addr;
  446. mutex_lock(&dev->alu_mutex);
  447. ret = ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr);
  448. if (ret)
  449. goto unlock_alu;
  450. ret = ksz8_valid_dyn_entry(dev, &data);
  451. if (ret)
  452. goto unlock_alu;
  453. if (data & masks[DYNAMIC_MAC_TABLE_MAC_EMPTY]) {
  454. *entries = 0;
  455. goto unlock_alu;
  456. }
  457. ret = ksz_read64(dev, regs[REG_IND_DATA_HI], &buf);
  458. if (ret)
  459. goto unlock_alu;
  460. data_hi = (u32)(buf >> 32);
  461. data_lo = (u32)buf;
  462. /* Check out how many valid entry in the table. */
  463. cnt = data & masks[DYNAMIC_MAC_TABLE_ENTRIES_H];
  464. cnt <<= shifts[DYNAMIC_MAC_ENTRIES_H];
  465. cnt |= (data_hi & masks[DYNAMIC_MAC_TABLE_ENTRIES]) >>
  466. shifts[DYNAMIC_MAC_ENTRIES];
  467. *entries = cnt + 1;
  468. *fid = (data_hi & masks[DYNAMIC_MAC_TABLE_FID]) >>
  469. shifts[DYNAMIC_MAC_FID];
  470. *src_port = (data_hi & masks[DYNAMIC_MAC_TABLE_SRC_PORT]) >>
  471. shifts[DYNAMIC_MAC_SRC_PORT];
  472. mac_addr[5] = (u8)data_lo;
  473. mac_addr[4] = (u8)(data_lo >> 8);
  474. mac_addr[3] = (u8)(data_lo >> 16);
  475. mac_addr[2] = (u8)(data_lo >> 24);
  476. mac_addr[1] = (u8)data_hi;
  477. mac_addr[0] = (u8)(data_hi >> 8);
  478. unlock_alu:
  479. mutex_unlock(&dev->alu_mutex);
  480. return ret;
  481. }
  482. static int ksz8_r_sta_mac_table(struct ksz_device *dev, u16 addr,
  483. struct alu_struct *alu, bool *valid)
  484. {
  485. u32 data_hi, data_lo;
  486. const u8 *shifts;
  487. const u32 *masks;
  488. u64 data;
  489. int ret;
  490. shifts = dev->info->shifts;
  491. masks = dev->info->masks;
  492. ret = ksz8_r_table(dev, TABLE_STATIC_MAC, addr, &data);
  493. if (ret)
  494. return ret;
  495. data_hi = data >> 32;
  496. data_lo = (u32)data;
  497. if (!(data_hi & (masks[STATIC_MAC_TABLE_VALID] |
  498. masks[STATIC_MAC_TABLE_OVERRIDE]))) {
  499. *valid = false;
  500. return 0;
  501. }
  502. alu->mac[5] = (u8)data_lo;
  503. alu->mac[4] = (u8)(data_lo >> 8);
  504. alu->mac[3] = (u8)(data_lo >> 16);
  505. alu->mac[2] = (u8)(data_lo >> 24);
  506. alu->mac[1] = (u8)data_hi;
  507. alu->mac[0] = (u8)(data_hi >> 8);
  508. alu->port_forward =
  509. (data_hi & masks[STATIC_MAC_TABLE_FWD_PORTS]) >>
  510. shifts[STATIC_MAC_FWD_PORTS];
  511. alu->is_override = (data_hi & masks[STATIC_MAC_TABLE_OVERRIDE]) ? 1 : 0;
  512. /* KSZ8795/KSZ8895 family switches have STATIC_MAC_TABLE_USE_FID and
  513. * STATIC_MAC_TABLE_FID definitions off by 1 when doing read on the
  514. * static MAC table compared to doing write.
  515. */
  516. if (ksz_is_ksz87xx(dev) || ksz_is_8895_family(dev))
  517. data_hi >>= 1;
  518. alu->is_static = true;
  519. alu->is_use_fid = (data_hi & masks[STATIC_MAC_TABLE_USE_FID]) ? 1 : 0;
  520. alu->fid = (data_hi & masks[STATIC_MAC_TABLE_FID]) >>
  521. shifts[STATIC_MAC_FID];
  522. *valid = true;
  523. return 0;
  524. }
  525. static int ksz8_w_sta_mac_table(struct ksz_device *dev, u16 addr,
  526. struct alu_struct *alu)
  527. {
  528. u32 data_hi, data_lo;
  529. const u8 *shifts;
  530. const u32 *masks;
  531. u64 data;
  532. shifts = dev->info->shifts;
  533. masks = dev->info->masks;
  534. data_lo = ((u32)alu->mac[2] << 24) |
  535. ((u32)alu->mac[3] << 16) |
  536. ((u32)alu->mac[4] << 8) | alu->mac[5];
  537. data_hi = ((u32)alu->mac[0] << 8) | alu->mac[1];
  538. data_hi |= (u32)alu->port_forward << shifts[STATIC_MAC_FWD_PORTS];
  539. if (alu->is_override)
  540. data_hi |= masks[STATIC_MAC_TABLE_OVERRIDE];
  541. if (alu->is_use_fid) {
  542. data_hi |= masks[STATIC_MAC_TABLE_USE_FID];
  543. data_hi |= (u32)alu->fid << shifts[STATIC_MAC_FID];
  544. }
  545. if (alu->is_static)
  546. data_hi |= masks[STATIC_MAC_TABLE_VALID];
  547. else
  548. data_hi &= ~masks[STATIC_MAC_TABLE_OVERRIDE];
  549. data = (u64)data_hi << 32 | data_lo;
  550. return ksz8_w_table(dev, TABLE_STATIC_MAC, addr, data);
  551. }
  552. static void ksz8_from_vlan(struct ksz_device *dev, u32 vlan, u8 *fid,
  553. u8 *member, u8 *valid)
  554. {
  555. const u8 *shifts;
  556. const u32 *masks;
  557. shifts = dev->info->shifts;
  558. masks = dev->info->masks;
  559. *fid = vlan & masks[VLAN_TABLE_FID];
  560. *member = (vlan & masks[VLAN_TABLE_MEMBERSHIP]) >>
  561. shifts[VLAN_TABLE_MEMBERSHIP_S];
  562. *valid = !!(vlan & masks[VLAN_TABLE_VALID]);
  563. }
  564. static void ksz8_to_vlan(struct ksz_device *dev, u8 fid, u8 member, u8 valid,
  565. u16 *vlan)
  566. {
  567. const u8 *shifts;
  568. const u32 *masks;
  569. shifts = dev->info->shifts;
  570. masks = dev->info->masks;
  571. *vlan = fid;
  572. *vlan |= (u16)member << shifts[VLAN_TABLE_MEMBERSHIP_S];
  573. if (valid)
  574. *vlan |= masks[VLAN_TABLE_VALID];
  575. }
  576. static void ksz8_r_vlan_entries(struct ksz_device *dev, u16 addr)
  577. {
  578. const u8 *shifts;
  579. u64 data;
  580. int i;
  581. shifts = dev->info->shifts;
  582. ksz8_r_table(dev, TABLE_VLAN, addr, &data);
  583. addr *= 4;
  584. for (i = 0; i < 4; i++) {
  585. dev->vlan_cache[addr + i].table[0] = (u16)data;
  586. data >>= shifts[VLAN_TABLE];
  587. }
  588. }
  589. static void ksz8_r_vlan_table(struct ksz_device *dev, u16 vid, u16 *vlan)
  590. {
  591. int index;
  592. u16 *data;
  593. u16 addr;
  594. u64 buf;
  595. data = (u16 *)&buf;
  596. addr = vid / 4;
  597. index = vid & 3;
  598. ksz8_r_table(dev, TABLE_VLAN, addr, &buf);
  599. *vlan = data[index];
  600. }
  601. static void ksz8_w_vlan_table(struct ksz_device *dev, u16 vid, u16 vlan)
  602. {
  603. int index;
  604. u16 *data;
  605. u16 addr;
  606. u64 buf;
  607. data = (u16 *)&buf;
  608. addr = vid / 4;
  609. index = vid & 3;
  610. ksz8_r_table(dev, TABLE_VLAN, addr, &buf);
  611. data[index] = vlan;
  612. dev->vlan_cache[vid].table[0] = vlan;
  613. ksz8_w_table(dev, TABLE_VLAN, addr, buf);
  614. }
  615. /**
  616. * ksz879x_get_loopback - KSZ879x specific function to get loopback
  617. * configuration status for a specific port
  618. * @dev: Pointer to the device structure
  619. * @port: Port number to query
  620. * @val: Pointer to store the result
  621. *
  622. * This function reads the SMI registers to determine whether loopback mode
  623. * is enabled for a specific port.
  624. *
  625. * Return: 0 on success, error code on failure.
  626. */
  627. static int ksz879x_get_loopback(struct ksz_device *dev, u16 port,
  628. u16 *val)
  629. {
  630. u8 stat3;
  631. int ret;
  632. ret = ksz_pread8(dev, port, REG_PORT_STATUS_3, &stat3);
  633. if (ret)
  634. return ret;
  635. if (stat3 & PORT_PHY_LOOPBACK)
  636. *val |= BMCR_LOOPBACK;
  637. return 0;
  638. }
  639. /**
  640. * ksz879x_set_loopback - KSZ879x specific function to set loopback mode for
  641. * a specific port
  642. * @dev: Pointer to the device structure.
  643. * @port: Port number to modify.
  644. * @val: Value indicating whether to enable or disable loopback mode.
  645. *
  646. * This function translates loopback bit of the BMCR register into the
  647. * corresponding hardware register bit value and writes it to the SMI interface.
  648. *
  649. * Return: 0 on success, error code on failure.
  650. */
  651. static int ksz879x_set_loopback(struct ksz_device *dev, u16 port, u16 val)
  652. {
  653. u8 stat3 = 0;
  654. if (val & BMCR_LOOPBACK)
  655. stat3 |= PORT_PHY_LOOPBACK;
  656. return ksz_prmw8(dev, port, REG_PORT_STATUS_3, PORT_PHY_LOOPBACK,
  657. stat3);
  658. }
  659. /**
  660. * ksz8_r_phy_ctrl - Translates and reads from the SMI interface to a MIIM PHY
  661. * Control register (Reg. 31).
  662. * @dev: The KSZ device instance.
  663. * @port: The port number to be read.
  664. * @val: The value read from the SMI interface.
  665. *
  666. * This function reads the SMI interface and translates the hardware register
  667. * bit values into their corresponding control settings for a MIIM PHY Control
  668. * register.
  669. *
  670. * Return: 0 on success, error code on failure.
  671. */
  672. static int ksz8_r_phy_ctrl(struct ksz_device *dev, int port, u16 *val)
  673. {
  674. const u16 *regs = dev->info->regs;
  675. u8 reg_val;
  676. int ret;
  677. *val = 0;
  678. ret = ksz_pread8(dev, port, regs[P_LINK_STATUS], &reg_val);
  679. if (ret < 0)
  680. return ret;
  681. if (reg_val & PORT_MDIX_STATUS)
  682. *val |= KSZ886X_CTRL_MDIX_STAT;
  683. ret = ksz_pread8(dev, port, REG_PORT_LINK_MD_CTRL, &reg_val);
  684. if (ret < 0)
  685. return ret;
  686. if (reg_val & PORT_FORCE_LINK)
  687. *val |= KSZ886X_CTRL_FORCE_LINK;
  688. if (reg_val & PORT_POWER_SAVING)
  689. *val |= KSZ886X_CTRL_PWRSAVE;
  690. if (reg_val & PORT_PHY_REMOTE_LOOPBACK)
  691. *val |= KSZ886X_CTRL_REMOTE_LOOPBACK;
  692. return 0;
  693. }
  694. /**
  695. * ksz8_r_phy_bmcr - Translates and reads from the SMI interface to a MIIM PHY
  696. * Basic mode control register (Reg. 0).
  697. * @dev: The KSZ device instance.
  698. * @port: The port number to be read.
  699. * @val: The value read from the SMI interface.
  700. *
  701. * This function reads the SMI interface and translates the hardware register
  702. * bit values into their corresponding control settings for a MIIM PHY Basic
  703. * mode control register.
  704. *
  705. * MIIM Bit Mapping Comparison between KSZ8794 and KSZ8873
  706. * -------------------------------------------------------------------
  707. * MIIM Bit | KSZ8794 Reg/Bit | KSZ8873 Reg/Bit
  708. * ----------------------------+-----------------------------+----------------
  709. * Bit 15 - Soft Reset | 0xF/4 | Not supported
  710. * Bit 14 - Loopback | 0xD/0 (MAC), 0xF/7 (PHY) ~ 0xD/0 (PHY)
  711. * Bit 13 - Force 100 | 0xC/6 = 0xC/6
  712. * Bit 12 - AN Enable | 0xC/7 (reverse logic) ~ 0xC/7
  713. * Bit 11 - Power Down | 0xD/3 = 0xD/3
  714. * Bit 10 - PHY Isolate | 0xF/5 | Not supported
  715. * Bit 9 - Restart AN | 0xD/5 = 0xD/5
  716. * Bit 8 - Force Full-Duplex | 0xC/5 = 0xC/5
  717. * Bit 7 - Collision Test/Res. | Not supported | Not supported
  718. * Bit 6 - Reserved | Not supported | Not supported
  719. * Bit 5 - Hp_mdix | 0x9/7 ~ 0xF/7
  720. * Bit 4 - Force MDI | 0xD/1 = 0xD/1
  721. * Bit 3 - Disable MDIX | 0xD/2 = 0xD/2
  722. * Bit 2 - Disable Far-End F. | ???? | 0xD/4
  723. * Bit 1 - Disable Transmit | 0xD/6 = 0xD/6
  724. * Bit 0 - Disable LED | 0xD/7 = 0xD/7
  725. * -------------------------------------------------------------------
  726. *
  727. * Return: 0 on success, error code on failure.
  728. */
  729. static int ksz8_r_phy_bmcr(struct ksz_device *dev, u16 port, u16 *val)
  730. {
  731. const u16 *regs = dev->info->regs;
  732. u8 restart, speed, ctrl;
  733. int ret;
  734. *val = 0;
  735. ret = ksz_pread8(dev, port, regs[P_NEG_RESTART_CTRL], &restart);
  736. if (ret)
  737. return ret;
  738. ret = ksz_pread8(dev, port, regs[P_SPEED_STATUS], &speed);
  739. if (ret)
  740. return ret;
  741. ret = ksz_pread8(dev, port, regs[P_FORCE_CTRL], &ctrl);
  742. if (ret)
  743. return ret;
  744. if (ctrl & PORT_FORCE_100_MBIT)
  745. *val |= BMCR_SPEED100;
  746. if (ksz_is_ksz88x3(dev)) {
  747. if (restart & KSZ8873_PORT_PHY_LOOPBACK)
  748. *val |= BMCR_LOOPBACK;
  749. if ((ctrl & PORT_AUTO_NEG_ENABLE))
  750. *val |= BMCR_ANENABLE;
  751. } else {
  752. ret = ksz879x_get_loopback(dev, port, val);
  753. if (ret)
  754. return ret;
  755. if (!(ctrl & PORT_AUTO_NEG_DISABLE))
  756. *val |= BMCR_ANENABLE;
  757. }
  758. if (restart & PORT_POWER_DOWN)
  759. *val |= BMCR_PDOWN;
  760. if (restart & PORT_AUTO_NEG_RESTART)
  761. *val |= BMCR_ANRESTART;
  762. if (ctrl & PORT_FORCE_FULL_DUPLEX)
  763. *val |= BMCR_FULLDPLX;
  764. if (speed & PORT_HP_MDIX)
  765. *val |= KSZ886X_BMCR_HP_MDIX;
  766. if (restart & PORT_FORCE_MDIX)
  767. *val |= KSZ886X_BMCR_FORCE_MDI;
  768. if (restart & PORT_AUTO_MDIX_DISABLE)
  769. *val |= KSZ886X_BMCR_DISABLE_AUTO_MDIX;
  770. if (restart & PORT_TX_DISABLE)
  771. *val |= KSZ886X_BMCR_DISABLE_TRANSMIT;
  772. if (restart & PORT_LED_OFF)
  773. *val |= KSZ886X_BMCR_DISABLE_LED;
  774. return 0;
  775. }
  776. int ksz8_r_phy(struct ksz_device *dev, u16 phy, u16 reg, u16 *val)
  777. {
  778. u8 ctrl, link, val1, val2;
  779. int processed = true;
  780. const u16 *regs;
  781. u16 data = 0;
  782. u16 p = phy;
  783. int ret;
  784. regs = dev->info->regs;
  785. switch (reg) {
  786. case MII_BMCR:
  787. ret = ksz8_r_phy_bmcr(dev, p, &data);
  788. if (ret)
  789. return ret;
  790. break;
  791. case MII_BMSR:
  792. ret = ksz_pread8(dev, p, regs[P_LINK_STATUS], &link);
  793. if (ret)
  794. return ret;
  795. data = BMSR_100FULL |
  796. BMSR_100HALF |
  797. BMSR_10FULL |
  798. BMSR_10HALF |
  799. BMSR_ANEGCAPABLE;
  800. if (link & PORT_AUTO_NEG_COMPLETE)
  801. data |= BMSR_ANEGCOMPLETE;
  802. if (link & PORT_STAT_LINK_GOOD)
  803. data |= BMSR_LSTATUS;
  804. break;
  805. case MII_PHYSID1:
  806. data = KSZ8795_ID_HI;
  807. break;
  808. case MII_PHYSID2:
  809. if (ksz_is_ksz88x3(dev))
  810. data = KSZ8863_ID_LO;
  811. else
  812. data = KSZ8795_ID_LO;
  813. break;
  814. case MII_ADVERTISE:
  815. ret = ksz_pread8(dev, p, regs[P_LOCAL_CTRL], &ctrl);
  816. if (ret)
  817. return ret;
  818. data = ADVERTISE_CSMA;
  819. if (ctrl & PORT_AUTO_NEG_SYM_PAUSE)
  820. data |= ADVERTISE_PAUSE_CAP;
  821. if (ctrl & PORT_AUTO_NEG_100BTX_FD)
  822. data |= ADVERTISE_100FULL;
  823. if (ctrl & PORT_AUTO_NEG_100BTX)
  824. data |= ADVERTISE_100HALF;
  825. if (ctrl & PORT_AUTO_NEG_10BT_FD)
  826. data |= ADVERTISE_10FULL;
  827. if (ctrl & PORT_AUTO_NEG_10BT)
  828. data |= ADVERTISE_10HALF;
  829. break;
  830. case MII_LPA:
  831. ret = ksz_pread8(dev, p, regs[P_REMOTE_STATUS], &link);
  832. if (ret)
  833. return ret;
  834. data = LPA_SLCT;
  835. if (link & PORT_REMOTE_SYM_PAUSE)
  836. data |= LPA_PAUSE_CAP;
  837. if (link & PORT_REMOTE_100BTX_FD)
  838. data |= LPA_100FULL;
  839. if (link & PORT_REMOTE_100BTX)
  840. data |= LPA_100HALF;
  841. if (link & PORT_REMOTE_10BT_FD)
  842. data |= LPA_10FULL;
  843. if (link & PORT_REMOTE_10BT)
  844. data |= LPA_10HALF;
  845. if (data & ~LPA_SLCT)
  846. data |= LPA_LPACK;
  847. break;
  848. case PHY_REG_LINK_MD:
  849. ret = ksz_pread8(dev, p, REG_PORT_LINK_MD_CTRL, &val1);
  850. if (ret)
  851. return ret;
  852. ret = ksz_pread8(dev, p, REG_PORT_LINK_MD_RESULT, &val2);
  853. if (ret)
  854. return ret;
  855. if (val1 & PORT_START_CABLE_DIAG)
  856. data |= PHY_START_CABLE_DIAG;
  857. if (val1 & PORT_CABLE_10M_SHORT)
  858. data |= PHY_CABLE_10M_SHORT;
  859. data |= FIELD_PREP(PHY_CABLE_DIAG_RESULT_M,
  860. FIELD_GET(PORT_CABLE_DIAG_RESULT_M, val1));
  861. data |= FIELD_PREP(PHY_CABLE_FAULT_COUNTER_M,
  862. (FIELD_GET(PORT_CABLE_FAULT_COUNTER_H, val1) << 8) |
  863. FIELD_GET(PORT_CABLE_FAULT_COUNTER_L, val2));
  864. break;
  865. case PHY_REG_PHY_CTRL:
  866. ret = ksz8_r_phy_ctrl(dev, p, &data);
  867. if (ret)
  868. return ret;
  869. break;
  870. default:
  871. processed = false;
  872. break;
  873. }
  874. if (processed)
  875. *val = data;
  876. return 0;
  877. }
  878. /**
  879. * ksz8_w_phy_ctrl - Translates and writes to the SMI interface from a MIIM PHY
  880. * Control register (Reg. 31).
  881. * @dev: The KSZ device instance.
  882. * @port: The port number to be configured.
  883. * @val: The register value to be written.
  884. *
  885. * This function translates control settings from a MIIM PHY Control register
  886. * into their corresponding hardware register bit values for the SMI
  887. * interface.
  888. *
  889. * Return: 0 on success, error code on failure.
  890. */
  891. static int ksz8_w_phy_ctrl(struct ksz_device *dev, int port, u16 val)
  892. {
  893. u8 reg_val = 0;
  894. int ret;
  895. if (val & KSZ886X_CTRL_FORCE_LINK)
  896. reg_val |= PORT_FORCE_LINK;
  897. if (val & KSZ886X_CTRL_PWRSAVE)
  898. reg_val |= PORT_POWER_SAVING;
  899. if (val & KSZ886X_CTRL_REMOTE_LOOPBACK)
  900. reg_val |= PORT_PHY_REMOTE_LOOPBACK;
  901. ret = ksz_prmw8(dev, port, REG_PORT_LINK_MD_CTRL, PORT_FORCE_LINK |
  902. PORT_POWER_SAVING | PORT_PHY_REMOTE_LOOPBACK, reg_val);
  903. return ret;
  904. }
  905. /**
  906. * ksz8_w_phy_bmcr - Translates and writes to the SMI interface from a MIIM PHY
  907. * Basic mode control register (Reg. 0).
  908. * @dev: The KSZ device instance.
  909. * @port: The port number to be configured.
  910. * @val: The register value to be written.
  911. *
  912. * This function translates control settings from a MIIM PHY Basic mode control
  913. * register into their corresponding hardware register bit values for the SMI
  914. * interface.
  915. *
  916. * MIIM Bit Mapping Comparison between KSZ8794 and KSZ8873
  917. * -------------------------------------------------------------------
  918. * MIIM Bit | KSZ8794 Reg/Bit | KSZ8873 Reg/Bit
  919. * ----------------------------+-----------------------------+----------------
  920. * Bit 15 - Soft Reset | 0xF/4 | Not supported
  921. * Bit 14 - Loopback | 0xD/0 (MAC), 0xF/7 (PHY) ~ 0xD/0 (PHY)
  922. * Bit 13 - Force 100 | 0xC/6 = 0xC/6
  923. * Bit 12 - AN Enable | 0xC/7 (reverse logic) ~ 0xC/7
  924. * Bit 11 - Power Down | 0xD/3 = 0xD/3
  925. * Bit 10 - PHY Isolate | 0xF/5 | Not supported
  926. * Bit 9 - Restart AN | 0xD/5 = 0xD/5
  927. * Bit 8 - Force Full-Duplex | 0xC/5 = 0xC/5
  928. * Bit 7 - Collision Test/Res. | Not supported | Not supported
  929. * Bit 6 - Reserved | Not supported | Not supported
  930. * Bit 5 - Hp_mdix | 0x9/7 ~ 0xF/7
  931. * Bit 4 - Force MDI | 0xD/1 = 0xD/1
  932. * Bit 3 - Disable MDIX | 0xD/2 = 0xD/2
  933. * Bit 2 - Disable Far-End F. | ???? | 0xD/4
  934. * Bit 1 - Disable Transmit | 0xD/6 = 0xD/6
  935. * Bit 0 - Disable LED | 0xD/7 = 0xD/7
  936. * -------------------------------------------------------------------
  937. *
  938. * Return: 0 on success, error code on failure.
  939. */
  940. static int ksz8_w_phy_bmcr(struct ksz_device *dev, u16 port, u16 val)
  941. {
  942. u8 restart, speed, ctrl, restart_mask;
  943. const u16 *regs = dev->info->regs;
  944. int ret;
  945. /* Do not support PHY reset function. */
  946. if (val & BMCR_RESET)
  947. return 0;
  948. speed = 0;
  949. if (val & KSZ886X_BMCR_HP_MDIX)
  950. speed |= PORT_HP_MDIX;
  951. ret = ksz_prmw8(dev, port, regs[P_SPEED_STATUS], PORT_HP_MDIX, speed);
  952. if (ret)
  953. return ret;
  954. ctrl = 0;
  955. if (ksz_is_ksz88x3(dev)) {
  956. if ((val & BMCR_ANENABLE))
  957. ctrl |= PORT_AUTO_NEG_ENABLE;
  958. } else {
  959. if (!(val & BMCR_ANENABLE))
  960. ctrl |= PORT_AUTO_NEG_DISABLE;
  961. /* Fiber port does not support auto-negotiation. */
  962. if (dev->ports[port].fiber)
  963. ctrl |= PORT_AUTO_NEG_DISABLE;
  964. }
  965. if (val & BMCR_SPEED100)
  966. ctrl |= PORT_FORCE_100_MBIT;
  967. if (val & BMCR_FULLDPLX)
  968. ctrl |= PORT_FORCE_FULL_DUPLEX;
  969. ret = ksz_prmw8(dev, port, regs[P_FORCE_CTRL], PORT_FORCE_100_MBIT |
  970. /* PORT_AUTO_NEG_ENABLE and PORT_AUTO_NEG_DISABLE are the same
  971. * bits
  972. */
  973. PORT_FORCE_FULL_DUPLEX | PORT_AUTO_NEG_ENABLE, ctrl);
  974. if (ret)
  975. return ret;
  976. restart = 0;
  977. restart_mask = PORT_LED_OFF | PORT_TX_DISABLE | PORT_AUTO_NEG_RESTART |
  978. PORT_POWER_DOWN | PORT_AUTO_MDIX_DISABLE | PORT_FORCE_MDIX;
  979. if (val & KSZ886X_BMCR_DISABLE_LED)
  980. restart |= PORT_LED_OFF;
  981. if (val & KSZ886X_BMCR_DISABLE_TRANSMIT)
  982. restart |= PORT_TX_DISABLE;
  983. if (val & BMCR_ANRESTART)
  984. restart |= PORT_AUTO_NEG_RESTART;
  985. if (val & BMCR_PDOWN)
  986. restart |= PORT_POWER_DOWN;
  987. if (val & KSZ886X_BMCR_DISABLE_AUTO_MDIX)
  988. restart |= PORT_AUTO_MDIX_DISABLE;
  989. if (val & KSZ886X_BMCR_FORCE_MDI)
  990. restart |= PORT_FORCE_MDIX;
  991. if (ksz_is_ksz88x3(dev)) {
  992. restart_mask |= KSZ8873_PORT_PHY_LOOPBACK;
  993. if (val & BMCR_LOOPBACK)
  994. restart |= KSZ8873_PORT_PHY_LOOPBACK;
  995. } else {
  996. ret = ksz879x_set_loopback(dev, port, val);
  997. if (ret)
  998. return ret;
  999. }
  1000. return ksz_prmw8(dev, port, regs[P_NEG_RESTART_CTRL], restart_mask,
  1001. restart);
  1002. }
  1003. int ksz8_w_phy(struct ksz_device *dev, u16 phy, u16 reg, u16 val)
  1004. {
  1005. const u16 *regs;
  1006. u8 ctrl, data;
  1007. u16 p = phy;
  1008. int ret;
  1009. regs = dev->info->regs;
  1010. switch (reg) {
  1011. case MII_BMCR:
  1012. ret = ksz8_w_phy_bmcr(dev, p, val);
  1013. if (ret)
  1014. return ret;
  1015. break;
  1016. case MII_ADVERTISE:
  1017. ret = ksz_pread8(dev, p, regs[P_LOCAL_CTRL], &ctrl);
  1018. if (ret)
  1019. return ret;
  1020. data = ctrl;
  1021. data &= ~(PORT_AUTO_NEG_SYM_PAUSE |
  1022. PORT_AUTO_NEG_100BTX_FD |
  1023. PORT_AUTO_NEG_100BTX |
  1024. PORT_AUTO_NEG_10BT_FD |
  1025. PORT_AUTO_NEG_10BT);
  1026. if (val & ADVERTISE_PAUSE_CAP)
  1027. data |= PORT_AUTO_NEG_SYM_PAUSE;
  1028. if (val & ADVERTISE_100FULL)
  1029. data |= PORT_AUTO_NEG_100BTX_FD;
  1030. if (val & ADVERTISE_100HALF)
  1031. data |= PORT_AUTO_NEG_100BTX;
  1032. if (val & ADVERTISE_10FULL)
  1033. data |= PORT_AUTO_NEG_10BT_FD;
  1034. if (val & ADVERTISE_10HALF)
  1035. data |= PORT_AUTO_NEG_10BT;
  1036. if (data != ctrl) {
  1037. ret = ksz_pwrite8(dev, p, regs[P_LOCAL_CTRL], data);
  1038. if (ret)
  1039. return ret;
  1040. }
  1041. break;
  1042. case PHY_REG_LINK_MD:
  1043. if (val & PHY_START_CABLE_DIAG)
  1044. ksz_port_cfg(dev, p, REG_PORT_LINK_MD_CTRL, PORT_START_CABLE_DIAG, true);
  1045. break;
  1046. case PHY_REG_PHY_CTRL:
  1047. ret = ksz8_w_phy_ctrl(dev, p, val);
  1048. if (ret)
  1049. return ret;
  1050. break;
  1051. default:
  1052. break;
  1053. }
  1054. return 0;
  1055. }
  1056. void ksz8_cfg_port_member(struct ksz_device *dev, int port, u8 member)
  1057. {
  1058. u8 data;
  1059. ksz_pread8(dev, port, P_MIRROR_CTRL, &data);
  1060. data &= ~PORT_VLAN_MEMBERSHIP;
  1061. data |= (member & dev->port_mask);
  1062. ksz_pwrite8(dev, port, P_MIRROR_CTRL, data);
  1063. }
  1064. void ksz8_flush_dyn_mac_table(struct ksz_device *dev, int port)
  1065. {
  1066. u8 learn[DSA_MAX_PORTS];
  1067. int first, index, cnt;
  1068. const u16 *regs;
  1069. regs = dev->info->regs;
  1070. if ((uint)port < dev->info->port_cnt) {
  1071. first = port;
  1072. cnt = port + 1;
  1073. } else {
  1074. /* Flush all ports. */
  1075. first = 0;
  1076. cnt = dev->info->port_cnt;
  1077. }
  1078. for (index = first; index < cnt; index++) {
  1079. ksz_pread8(dev, index, regs[P_STP_CTRL], &learn[index]);
  1080. if (!(learn[index] & PORT_LEARN_DISABLE))
  1081. ksz_pwrite8(dev, index, regs[P_STP_CTRL],
  1082. learn[index] | PORT_LEARN_DISABLE);
  1083. }
  1084. ksz_cfg(dev, S_FLUSH_TABLE_CTRL, SW_FLUSH_DYN_MAC_TABLE, true);
  1085. for (index = first; index < cnt; index++) {
  1086. if (!(learn[index] & PORT_LEARN_DISABLE))
  1087. ksz_pwrite8(dev, index, regs[P_STP_CTRL], learn[index]);
  1088. }
  1089. }
  1090. int ksz8_fdb_dump(struct ksz_device *dev, int port,
  1091. dsa_fdb_dump_cb_t *cb, void *data)
  1092. {
  1093. u8 mac[ETH_ALEN];
  1094. u8 src_port, fid;
  1095. u16 entries = 0;
  1096. int ret, i;
  1097. for (i = 0; i < KSZ8_DYN_MAC_ENTRIES; i++) {
  1098. ret = ksz8_r_dyn_mac_table(dev, i, mac, &fid, &src_port,
  1099. &entries);
  1100. if (ret)
  1101. return ret;
  1102. if (i >= entries)
  1103. return 0;
  1104. if (port == src_port) {
  1105. ret = cb(mac, fid, false, data);
  1106. if (ret)
  1107. return ret;
  1108. }
  1109. }
  1110. return 0;
  1111. }
  1112. static int ksz8_add_sta_mac(struct ksz_device *dev, int port,
  1113. const unsigned char *addr, u16 vid)
  1114. {
  1115. struct alu_struct alu;
  1116. int index, ret;
  1117. int empty = 0;
  1118. alu.port_forward = 0;
  1119. for (index = 0; index < dev->info->num_statics; index++) {
  1120. bool valid;
  1121. ret = ksz8_r_sta_mac_table(dev, index, &alu, &valid);
  1122. if (ret)
  1123. return ret;
  1124. if (!valid) {
  1125. /* Remember the first empty entry. */
  1126. if (!empty)
  1127. empty = index + 1;
  1128. continue;
  1129. }
  1130. if (!memcmp(alu.mac, addr, ETH_ALEN) && alu.fid == vid)
  1131. break;
  1132. }
  1133. /* no available entry */
  1134. if (index == dev->info->num_statics && !empty)
  1135. return -ENOSPC;
  1136. /* add entry */
  1137. if (index == dev->info->num_statics) {
  1138. index = empty - 1;
  1139. memset(&alu, 0, sizeof(alu));
  1140. memcpy(alu.mac, addr, ETH_ALEN);
  1141. alu.is_static = true;
  1142. }
  1143. alu.port_forward |= BIT(port);
  1144. if (vid) {
  1145. alu.is_use_fid = true;
  1146. /* Need a way to map VID to FID. */
  1147. alu.fid = vid;
  1148. }
  1149. return ksz8_w_sta_mac_table(dev, index, &alu);
  1150. }
  1151. static int ksz8_del_sta_mac(struct ksz_device *dev, int port,
  1152. const unsigned char *addr, u16 vid)
  1153. {
  1154. struct alu_struct alu;
  1155. int index, ret;
  1156. for (index = 0; index < dev->info->num_statics; index++) {
  1157. bool valid;
  1158. ret = ksz8_r_sta_mac_table(dev, index, &alu, &valid);
  1159. if (ret)
  1160. return ret;
  1161. if (!valid)
  1162. continue;
  1163. if (!memcmp(alu.mac, addr, ETH_ALEN) && alu.fid == vid)
  1164. break;
  1165. }
  1166. /* no available entry */
  1167. if (index == dev->info->num_statics)
  1168. return 0;
  1169. /* clear port */
  1170. alu.port_forward &= ~BIT(port);
  1171. if (!alu.port_forward)
  1172. alu.is_static = false;
  1173. return ksz8_w_sta_mac_table(dev, index, &alu);
  1174. }
  1175. int ksz8_mdb_add(struct ksz_device *dev, int port,
  1176. const struct switchdev_obj_port_mdb *mdb, struct dsa_db db)
  1177. {
  1178. return ksz8_add_sta_mac(dev, port, mdb->addr, mdb->vid);
  1179. }
  1180. int ksz8_mdb_del(struct ksz_device *dev, int port,
  1181. const struct switchdev_obj_port_mdb *mdb, struct dsa_db db)
  1182. {
  1183. return ksz8_del_sta_mac(dev, port, mdb->addr, mdb->vid);
  1184. }
  1185. int ksz8_fdb_add(struct ksz_device *dev, int port, const unsigned char *addr,
  1186. u16 vid, struct dsa_db db)
  1187. {
  1188. return ksz8_add_sta_mac(dev, port, addr, vid);
  1189. }
  1190. int ksz8_fdb_del(struct ksz_device *dev, int port, const unsigned char *addr,
  1191. u16 vid, struct dsa_db db)
  1192. {
  1193. return ksz8_del_sta_mac(dev, port, addr, vid);
  1194. }
  1195. int ksz8_port_vlan_filtering(struct ksz_device *dev, int port, bool flag,
  1196. struct netlink_ext_ack *extack)
  1197. {
  1198. if (ksz_is_ksz88x3(dev))
  1199. return -ENOTSUPP;
  1200. /* Discard packets with VID not enabled on the switch */
  1201. ksz_cfg(dev, S_MIRROR_CTRL, SW_VLAN_ENABLE, flag);
  1202. /* Discard packets with VID not enabled on the ingress port */
  1203. for (port = 0; port < dev->phy_port_cnt; ++port)
  1204. ksz_port_cfg(dev, port, REG_PORT_CTRL_2, PORT_INGRESS_FILTER,
  1205. flag);
  1206. return 0;
  1207. }
  1208. static void ksz8_port_enable_pvid(struct ksz_device *dev, int port, bool state)
  1209. {
  1210. if (ksz_is_ksz88x3(dev)) {
  1211. ksz_cfg(dev, REG_SW_INSERT_SRC_PVID,
  1212. 0x03 << (4 - 2 * port), state);
  1213. } else {
  1214. ksz_pwrite8(dev, port, REG_PORT_CTRL_12, state ? 0x0f : 0x00);
  1215. }
  1216. }
  1217. int ksz8_port_vlan_add(struct ksz_device *dev, int port,
  1218. const struct switchdev_obj_port_vlan *vlan,
  1219. struct netlink_ext_ack *extack)
  1220. {
  1221. bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
  1222. struct ksz_port *p = &dev->ports[port];
  1223. u16 data, new_pvid = 0;
  1224. u8 fid, member, valid;
  1225. if (ksz_is_ksz88x3(dev))
  1226. return -ENOTSUPP;
  1227. /* If a VLAN is added with untagged flag different from the
  1228. * port's Remove Tag flag, we need to change the latter.
  1229. * Ignore VID 0, which is always untagged.
  1230. * Ignore CPU port, which will always be tagged.
  1231. */
  1232. if (untagged != p->remove_tag && vlan->vid != 0 &&
  1233. port != dev->cpu_port) {
  1234. unsigned int vid;
  1235. /* Reject attempts to add a VLAN that requires the
  1236. * Remove Tag flag to be changed, unless there are no
  1237. * other VLANs currently configured.
  1238. */
  1239. for (vid = 1; vid < dev->info->num_vlans; ++vid) {
  1240. /* Skip the VID we are going to add or reconfigure */
  1241. if (vid == vlan->vid)
  1242. continue;
  1243. ksz8_from_vlan(dev, dev->vlan_cache[vid].table[0],
  1244. &fid, &member, &valid);
  1245. if (valid && (member & BIT(port)))
  1246. return -EINVAL;
  1247. }
  1248. ksz_port_cfg(dev, port, P_TAG_CTRL, PORT_REMOVE_TAG, untagged);
  1249. p->remove_tag = untagged;
  1250. }
  1251. ksz8_r_vlan_table(dev, vlan->vid, &data);
  1252. ksz8_from_vlan(dev, data, &fid, &member, &valid);
  1253. /* First time to setup the VLAN entry. */
  1254. if (!valid) {
  1255. /* Need to find a way to map VID to FID. */
  1256. fid = 1;
  1257. valid = 1;
  1258. }
  1259. member |= BIT(port);
  1260. ksz8_to_vlan(dev, fid, member, valid, &data);
  1261. ksz8_w_vlan_table(dev, vlan->vid, data);
  1262. /* change PVID */
  1263. if (vlan->flags & BRIDGE_VLAN_INFO_PVID)
  1264. new_pvid = vlan->vid;
  1265. if (new_pvid) {
  1266. u16 vid;
  1267. ksz_pread16(dev, port, REG_PORT_CTRL_VID, &vid);
  1268. vid &= ~VLAN_VID_MASK;
  1269. vid |= new_pvid;
  1270. ksz_pwrite16(dev, port, REG_PORT_CTRL_VID, vid);
  1271. ksz8_port_enable_pvid(dev, port, true);
  1272. }
  1273. return 0;
  1274. }
  1275. int ksz8_port_vlan_del(struct ksz_device *dev, int port,
  1276. const struct switchdev_obj_port_vlan *vlan)
  1277. {
  1278. u16 data, pvid;
  1279. u8 fid, member, valid;
  1280. if (ksz_is_ksz88x3(dev))
  1281. return -ENOTSUPP;
  1282. ksz_pread16(dev, port, REG_PORT_CTRL_VID, &pvid);
  1283. pvid = pvid & 0xFFF;
  1284. ksz8_r_vlan_table(dev, vlan->vid, &data);
  1285. ksz8_from_vlan(dev, data, &fid, &member, &valid);
  1286. member &= ~BIT(port);
  1287. /* Invalidate the entry if no more member. */
  1288. if (!member) {
  1289. fid = 0;
  1290. valid = 0;
  1291. }
  1292. ksz8_to_vlan(dev, fid, member, valid, &data);
  1293. ksz8_w_vlan_table(dev, vlan->vid, data);
  1294. if (pvid == vlan->vid)
  1295. ksz8_port_enable_pvid(dev, port, false);
  1296. return 0;
  1297. }
  1298. int ksz8_port_mirror_add(struct ksz_device *dev, int port,
  1299. struct dsa_mall_mirror_tc_entry *mirror,
  1300. bool ingress, struct netlink_ext_ack *extack)
  1301. {
  1302. if (ingress) {
  1303. ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_RX, true);
  1304. dev->mirror_rx |= BIT(port);
  1305. } else {
  1306. ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_TX, true);
  1307. dev->mirror_tx |= BIT(port);
  1308. }
  1309. ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_SNIFFER, false);
  1310. /* configure mirror port */
  1311. if (dev->mirror_rx || dev->mirror_tx)
  1312. ksz_port_cfg(dev, mirror->to_local_port, P_MIRROR_CTRL,
  1313. PORT_MIRROR_SNIFFER, true);
  1314. return 0;
  1315. }
  1316. void ksz8_port_mirror_del(struct ksz_device *dev, int port,
  1317. struct dsa_mall_mirror_tc_entry *mirror)
  1318. {
  1319. u8 data;
  1320. if (mirror->ingress) {
  1321. ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_RX, false);
  1322. dev->mirror_rx &= ~BIT(port);
  1323. } else {
  1324. ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_TX, false);
  1325. dev->mirror_tx &= ~BIT(port);
  1326. }
  1327. ksz_pread8(dev, port, P_MIRROR_CTRL, &data);
  1328. if (!dev->mirror_rx && !dev->mirror_tx)
  1329. ksz_port_cfg(dev, mirror->to_local_port, P_MIRROR_CTRL,
  1330. PORT_MIRROR_SNIFFER, false);
  1331. }
  1332. static void ksz8795_cpu_interface_select(struct ksz_device *dev, int port)
  1333. {
  1334. struct ksz_port *p = &dev->ports[port];
  1335. if (!ksz_is_ksz87xx(dev))
  1336. return;
  1337. if (!p->interface && dev->compat_interface) {
  1338. dev_warn(dev->dev,
  1339. "Using legacy switch \"phy-mode\" property, because it is missing on port %d node. "
  1340. "Please update your device tree.\n",
  1341. port);
  1342. p->interface = dev->compat_interface;
  1343. }
  1344. }
  1345. void ksz8_port_setup(struct ksz_device *dev, int port, bool cpu_port)
  1346. {
  1347. const u16 *regs = dev->info->regs;
  1348. struct dsa_switch *ds = dev->ds;
  1349. const u32 *masks;
  1350. int queues;
  1351. u8 member;
  1352. masks = dev->info->masks;
  1353. /* enable broadcast storm limit */
  1354. ksz_port_cfg(dev, port, P_BCAST_STORM_CTRL, PORT_BROADCAST_STORM, true);
  1355. /* For KSZ88x3 enable only one queue by default, otherwise we won't
  1356. * be able to get rid of PCP prios on Port 2.
  1357. */
  1358. if (ksz_is_ksz88x3(dev))
  1359. queues = 1;
  1360. else
  1361. queues = dev->info->num_tx_queues;
  1362. ksz8_port_queue_split(dev, port, queues);
  1363. /* replace priority */
  1364. ksz_port_cfg(dev, port, P_802_1P_CTRL,
  1365. masks[PORT_802_1P_REMAPPING], false);
  1366. if (cpu_port)
  1367. member = dsa_user_ports(ds);
  1368. else
  1369. member = BIT(dsa_upstream_port(ds, port));
  1370. ksz8_cfg_port_member(dev, port, member);
  1371. /* Disable all WoL options by default. Otherwise
  1372. * ksz_switch_macaddr_get/put logic will not work properly.
  1373. * CPU port 4 has no WoL functionality.
  1374. */
  1375. if (ksz_is_ksz87xx(dev) && !cpu_port)
  1376. ksz8_pme_pwrite8(dev, port, regs[REG_PORT_PME_CTRL], 0);
  1377. }
  1378. static void ksz88x3_config_rmii_clk(struct ksz_device *dev)
  1379. {
  1380. struct dsa_port *cpu_dp = dsa_to_port(dev->ds, dev->cpu_port);
  1381. bool rmii_clk_internal;
  1382. if (!ksz_is_ksz88x3(dev))
  1383. return;
  1384. rmii_clk_internal = of_property_read_bool(cpu_dp->dn,
  1385. "microchip,rmii-clk-internal");
  1386. ksz_cfg(dev, KSZ88X3_REG_FVID_AND_HOST_MODE,
  1387. KSZ88X3_PORT3_RMII_CLK_INTERNAL, rmii_clk_internal);
  1388. }
  1389. void ksz8_config_cpu_port(struct dsa_switch *ds)
  1390. {
  1391. struct ksz_device *dev = ds->priv;
  1392. struct ksz_port *p;
  1393. const u32 *masks;
  1394. const u16 *regs;
  1395. u8 remote;
  1396. int i;
  1397. masks = dev->info->masks;
  1398. regs = dev->info->regs;
  1399. ksz_cfg(dev, regs[S_TAIL_TAG_CTRL], masks[SW_TAIL_TAG_ENABLE], true);
  1400. ksz8_port_setup(dev, dev->cpu_port, true);
  1401. ksz8795_cpu_interface_select(dev, dev->cpu_port);
  1402. ksz88x3_config_rmii_clk(dev);
  1403. for (i = 0; i < dev->phy_port_cnt; i++) {
  1404. ksz_port_stp_state_set(ds, i, BR_STATE_DISABLED);
  1405. }
  1406. for (i = 0; i < dev->phy_port_cnt; i++) {
  1407. p = &dev->ports[i];
  1408. /* For KSZ8795 family. */
  1409. if (ksz_is_ksz87xx(dev)) {
  1410. ksz_pread8(dev, i, regs[P_REMOTE_STATUS], &remote);
  1411. if (remote & KSZ8_PORT_FIBER_MODE)
  1412. p->fiber = 1;
  1413. }
  1414. if (p->fiber)
  1415. ksz_port_cfg(dev, i, regs[P_STP_CTRL],
  1416. PORT_FORCE_FLOW_CTRL, true);
  1417. else
  1418. ksz_port_cfg(dev, i, regs[P_STP_CTRL],
  1419. PORT_FORCE_FLOW_CTRL, false);
  1420. }
  1421. }
  1422. /**
  1423. * ksz8_phy_port_link_up - Configures ports with integrated PHYs
  1424. * @dev: The KSZ device instance.
  1425. * @port: The port number to configure.
  1426. * @duplex: The desired duplex mode.
  1427. * @tx_pause: If true, enables transmit pause.
  1428. * @rx_pause: If true, enables receive pause.
  1429. *
  1430. * Description:
  1431. * The function configures flow control settings for a given port based on the
  1432. * desired settings and current duplex mode.
  1433. *
  1434. * According to the KSZ8873 datasheet, the PORT_FORCE_FLOW_CTRL bit in the
  1435. * Port Control 2 register (0x1A for Port 1, 0x22 for Port 2, 0x32 for Port 3)
  1436. * determines how flow control is handled on the port:
  1437. * "1 = will always enable full-duplex flow control on the port, regardless
  1438. * of AN result.
  1439. * 0 = full-duplex flow control is enabled based on AN result."
  1440. *
  1441. * This means that the flow control behavior depends on the state of this bit:
  1442. * - If PORT_FORCE_FLOW_CTRL is set to 1, the switch will ignore AN results and
  1443. * force flow control on the port.
  1444. * - If PORT_FORCE_FLOW_CTRL is set to 0, the switch will enable or disable
  1445. * flow control based on the AN results.
  1446. *
  1447. * However, there is a potential limitation in this configuration. It is
  1448. * currently not possible to force disable flow control on a port if we still
  1449. * advertise pause support. While such a configuration is not currently
  1450. * supported by Linux, and may not make practical sense, it's important to be
  1451. * aware of this limitation when working with the KSZ8873 and similar devices.
  1452. */
  1453. static void ksz8_phy_port_link_up(struct ksz_device *dev, int port, int duplex,
  1454. bool tx_pause, bool rx_pause)
  1455. {
  1456. const u16 *regs = dev->info->regs;
  1457. u8 sctrl = 0;
  1458. /* The KSZ8795 switch differs from the KSZ8873 by supporting
  1459. * asymmetric pause control. However, since a single bit is used to
  1460. * control both RX and TX pause, we can't enforce asymmetric pause
  1461. * control - both TX and RX pause will be either enabled or disabled
  1462. * together.
  1463. *
  1464. * If auto-negotiation is enabled, we usually allow the flow control to
  1465. * be determined by the auto-negotiation process based on the
  1466. * capabilities of both link partners. However, for KSZ8873, the
  1467. * PORT_FORCE_FLOW_CTRL bit may be set by the hardware bootstrap,
  1468. * ignoring the auto-negotiation result. Thus, even in auto-negotiation
  1469. * mode, we need to ensure that the PORT_FORCE_FLOW_CTRL bit is
  1470. * properly cleared.
  1471. *
  1472. * In the absence of pause auto-negotiation, we will enforce symmetric
  1473. * pause control for both variants of switches - KSZ8873 and KSZ8795.
  1474. *
  1475. * Autoneg Pause Autoneg rx,tx PORT_FORCE_FLOW_CTRL
  1476. * 1 1 x 0
  1477. * 0 1 x 0 (flow control probably disabled)
  1478. * x 0 1 1 (flow control force enabled)
  1479. * 1 0 0 0 (flow control still depends on
  1480. * aneg result due to hardware)
  1481. * 0 0 0 0 (flow control probably disabled)
  1482. */
  1483. if (dev->ports[port].manual_flow && tx_pause)
  1484. sctrl |= PORT_FORCE_FLOW_CTRL;
  1485. ksz_prmw8(dev, port, regs[P_STP_CTRL], PORT_FORCE_FLOW_CTRL, sctrl);
  1486. }
  1487. /**
  1488. * ksz8_cpu_port_link_up - Configures the CPU port of the switch.
  1489. * @dev: The KSZ device instance.
  1490. * @speed: The desired link speed.
  1491. * @duplex: The desired duplex mode.
  1492. * @tx_pause: If true, enables transmit pause.
  1493. * @rx_pause: If true, enables receive pause.
  1494. *
  1495. * Description:
  1496. * The function configures flow control and speed settings for the CPU
  1497. * port of the switch based on the desired settings, current duplex mode, and
  1498. * speed.
  1499. */
  1500. static void ksz8_cpu_port_link_up(struct ksz_device *dev, int speed, int duplex,
  1501. bool tx_pause, bool rx_pause)
  1502. {
  1503. const u16 *regs = dev->info->regs;
  1504. u8 ctrl = 0;
  1505. /* SW_FLOW_CTRL, SW_HALF_DUPLEX, and SW_10_MBIT bits are bootstrappable
  1506. * at least on KSZ8873. They can have different values depending on your
  1507. * board setup.
  1508. */
  1509. if (tx_pause || rx_pause)
  1510. ctrl |= SW_FLOW_CTRL;
  1511. if (duplex == DUPLEX_HALF)
  1512. ctrl |= SW_HALF_DUPLEX;
  1513. /* This hardware only supports SPEED_10 and SPEED_100. For SPEED_10
  1514. * we need to set the SW_10_MBIT bit. Otherwise, we can leave it 0.
  1515. */
  1516. if (speed == SPEED_10)
  1517. ctrl |= SW_10_MBIT;
  1518. ksz_rmw8(dev, regs[S_BROADCAST_CTRL], SW_HALF_DUPLEX | SW_FLOW_CTRL |
  1519. SW_10_MBIT, ctrl);
  1520. }
  1521. void ksz8_phylink_mac_link_up(struct phylink_config *config,
  1522. struct phy_device *phydev, unsigned int mode,
  1523. phy_interface_t interface, int speed, int duplex,
  1524. bool tx_pause, bool rx_pause)
  1525. {
  1526. struct dsa_port *dp = dsa_phylink_to_port(config);
  1527. struct ksz_device *dev = dp->ds->priv;
  1528. int port = dp->index;
  1529. /* If the port is the CPU port, apply special handling. Only the CPU
  1530. * port is configured via global registers.
  1531. */
  1532. if (dev->cpu_port == port)
  1533. ksz8_cpu_port_link_up(dev, speed, duplex, tx_pause, rx_pause);
  1534. else if (dev->info->internal_phy[port])
  1535. ksz8_phy_port_link_up(dev, port, duplex, tx_pause, rx_pause);
  1536. }
  1537. static int ksz8_handle_global_errata(struct dsa_switch *ds)
  1538. {
  1539. struct ksz_device *dev = ds->priv;
  1540. int ret = 0;
  1541. /* KSZ87xx Errata DS80000687C.
  1542. * Module 2: Link drops with some EEE link partners.
  1543. * An issue with the EEE next page exchange between the
  1544. * KSZ879x/KSZ877x/KSZ876x and some EEE link partners may result in
  1545. * the link dropping.
  1546. */
  1547. if (dev->info->ksz87xx_eee_link_erratum)
  1548. ret = ksz8_ind_write8(dev, TABLE_EEE, REG_IND_EEE_GLOB2_HI, 0);
  1549. return ret;
  1550. }
  1551. int ksz8_enable_stp_addr(struct ksz_device *dev)
  1552. {
  1553. struct alu_struct alu;
  1554. /* Setup STP address for STP operation. */
  1555. memset(&alu, 0, sizeof(alu));
  1556. ether_addr_copy(alu.mac, eth_stp_addr);
  1557. alu.is_static = true;
  1558. alu.is_override = true;
  1559. alu.port_forward = dev->info->cpu_ports;
  1560. return ksz8_w_sta_mac_table(dev, 0, &alu);
  1561. }
  1562. int ksz8_setup(struct dsa_switch *ds)
  1563. {
  1564. struct ksz_device *dev = ds->priv;
  1565. const u16 *regs = dev->info->regs;
  1566. int i, ret = 0;
  1567. ds->mtu_enforcement_ingress = true;
  1568. /* We rely on software untagging on the CPU port, so that we
  1569. * can support both tagged and untagged VLANs
  1570. */
  1571. ds->untag_bridge_pvid = true;
  1572. /* VLAN filtering is partly controlled by the global VLAN
  1573. * Enable flag
  1574. */
  1575. ds->vlan_filtering_is_global = true;
  1576. /* Enable automatic fast aging when link changed detected. */
  1577. ksz_cfg(dev, S_LINK_AGING_CTRL, SW_LINK_AUTO_AGING, true);
  1578. /* Enable aggressive back off algorithm in half duplex mode. */
  1579. regmap_update_bits(ksz_regmap_8(dev), REG_SW_CTRL_1,
  1580. SW_AGGR_BACKOFF, SW_AGGR_BACKOFF);
  1581. /*
  1582. * Make sure unicast VLAN boundary is set as default and
  1583. * enable no excessive collision drop.
  1584. */
  1585. regmap_update_bits(ksz_regmap_8(dev), REG_SW_CTRL_2,
  1586. UNICAST_VLAN_BOUNDARY | NO_EXC_COLLISION_DROP,
  1587. UNICAST_VLAN_BOUNDARY | NO_EXC_COLLISION_DROP);
  1588. ksz_cfg(dev, S_REPLACE_VID_CTRL, SW_REPLACE_VID, false);
  1589. ksz_cfg(dev, S_MIRROR_CTRL, SW_MIRROR_RX_TX, false);
  1590. if (!ksz_is_ksz88x3(dev))
  1591. ksz_cfg(dev, REG_SW_CTRL_19, SW_INS_TAG_ENABLE, true);
  1592. for (i = 0; i < (dev->info->num_vlans / 4); i++)
  1593. ksz8_r_vlan_entries(dev, i);
  1594. /* Make sure PME (WoL) is not enabled. If requested, it will
  1595. * be enabled by ksz_wol_pre_shutdown(). Otherwise, some PMICs
  1596. * do not like PME events changes before shutdown. PME only
  1597. * available on KSZ87xx family.
  1598. */
  1599. if (ksz_is_ksz87xx(dev)) {
  1600. ret = ksz8_pme_write8(dev, regs[REG_SW_PME_CTRL], 0);
  1601. if (!ret)
  1602. ret = ksz_rmw8(dev, REG_INT_ENABLE, INT_PME, 0);
  1603. }
  1604. if (!ret)
  1605. return ksz8_handle_global_errata(ds);
  1606. else
  1607. return ret;
  1608. }
  1609. void ksz8_get_caps(struct ksz_device *dev, int port,
  1610. struct phylink_config *config)
  1611. {
  1612. config->mac_capabilities = MAC_10 | MAC_100;
  1613. /* Silicon Errata Sheet (DS80000830A):
  1614. * "Port 1 does not respond to received flow control PAUSE frames"
  1615. * So, disable Pause support on "Port 1" (port == 0) for all ksz88x3
  1616. * switches.
  1617. */
  1618. if (!ksz_is_ksz88x3(dev) || port)
  1619. config->mac_capabilities |= MAC_SYM_PAUSE;
  1620. /* Asym pause is not supported on KSZ8863 and KSZ8873 */
  1621. if (!ksz_is_ksz88x3(dev))
  1622. config->mac_capabilities |= MAC_ASYM_PAUSE;
  1623. }
  1624. u32 ksz8_get_port_addr(int port, int offset)
  1625. {
  1626. return PORT_CTRL_ADDR(port, offset);
  1627. }
  1628. int ksz8_switch_init(struct ksz_device *dev)
  1629. {
  1630. dev->cpu_port = fls(dev->info->cpu_ports) - 1;
  1631. dev->phy_port_cnt = dev->info->port_cnt - 1;
  1632. dev->port_mask = (BIT(dev->phy_port_cnt) - 1) | dev->info->cpu_ports;
  1633. return 0;
  1634. }
  1635. void ksz8_switch_exit(struct ksz_device *dev)
  1636. {
  1637. ksz8_reset_switch(dev);
  1638. }
  1639. MODULE_AUTHOR("Tristram Ha <Tristram.Ha@microchip.com>");
  1640. MODULE_DESCRIPTION("Microchip KSZ8795 Series Switch DSA Driver");
  1641. MODULE_LICENSE("GPL");