ksz_common.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /* Microchip switch driver common header
  3. *
  4. * Copyright (C) 2017-2024 Microchip Technology Inc.
  5. */
  6. #ifndef __KSZ_COMMON_H
  7. #define __KSZ_COMMON_H
  8. #include <linux/etherdevice.h>
  9. #include <linux/kernel.h>
  10. #include <linux/mutex.h>
  11. #include <linux/phy.h>
  12. #include <linux/regmap.h>
  13. #include <net/dsa.h>
  14. #include <linux/irq.h>
  15. #include <linux/platform_data/microchip-ksz.h>
  16. #include "ksz_ptp.h"
  17. #define KSZ_MAX_NUM_PORTS 8
  18. /* all KSZ switches count ports from 1 */
  19. #define KSZ_PORT_1 0
  20. #define KSZ_PORT_2 1
  21. #define KSZ_PORT_4 3
  22. struct ksz_device;
  23. struct ksz_port;
  24. struct phylink_mac_ops;
  25. enum ksz_regmap_width {
  26. KSZ_REGMAP_8,
  27. KSZ_REGMAP_16,
  28. KSZ_REGMAP_32,
  29. __KSZ_NUM_REGMAPS,
  30. };
  31. struct vlan_table {
  32. u32 table[3];
  33. };
  34. struct ksz_port_mib {
  35. struct mutex cnt_mutex; /* structure access */
  36. u8 cnt_ptr;
  37. u64 *counters;
  38. struct rtnl_link_stats64 stats64;
  39. struct ethtool_pause_stats pause_stats;
  40. struct spinlock stats64_lock;
  41. };
  42. struct ksz_mib_names {
  43. int index;
  44. char string[ETH_GSTRING_LEN];
  45. };
  46. struct ksz_chip_data {
  47. u32 chip_id;
  48. const char *dev_name;
  49. int num_vlans;
  50. int num_alus;
  51. int num_statics;
  52. int cpu_ports;
  53. int port_cnt;
  54. u8 port_nirqs;
  55. u8 num_tx_queues;
  56. u8 num_ipms; /* number of Internal Priority Maps */
  57. bool tc_cbs_supported;
  58. const struct ksz_dev_ops *ops;
  59. const struct phylink_mac_ops *phylink_mac_ops;
  60. bool phy_errata_9477;
  61. bool ksz87xx_eee_link_erratum;
  62. const struct ksz_mib_names *mib_names;
  63. int mib_cnt;
  64. u8 reg_mib_cnt;
  65. const u16 *regs;
  66. const u32 *masks;
  67. const u8 *shifts;
  68. const u8 *xmii_ctrl0;
  69. const u8 *xmii_ctrl1;
  70. int stp_ctrl_reg;
  71. int broadcast_ctrl_reg;
  72. int multicast_ctrl_reg;
  73. int start_ctrl_reg;
  74. bool supports_mii[KSZ_MAX_NUM_PORTS];
  75. bool supports_rmii[KSZ_MAX_NUM_PORTS];
  76. bool supports_rgmii[KSZ_MAX_NUM_PORTS];
  77. bool internal_phy[KSZ_MAX_NUM_PORTS];
  78. bool gbit_capable[KSZ_MAX_NUM_PORTS];
  79. const struct regmap_access_table *wr_table;
  80. const struct regmap_access_table *rd_table;
  81. };
  82. struct ksz_irq {
  83. u16 masked;
  84. u16 reg_mask;
  85. u16 reg_status;
  86. struct irq_domain *domain;
  87. int nirqs;
  88. int irq_num;
  89. char name[16];
  90. struct ksz_device *dev;
  91. };
  92. struct ksz_ptp_irq {
  93. struct ksz_port *port;
  94. u16 ts_reg;
  95. bool ts_en;
  96. char name[16];
  97. int num;
  98. };
  99. struct ksz_switch_macaddr {
  100. unsigned char addr[ETH_ALEN];
  101. refcount_t refcount;
  102. };
  103. struct ksz_port {
  104. bool remove_tag; /* Remove Tag flag set, for ksz8795 only */
  105. bool learning;
  106. bool isolated;
  107. int stp_state;
  108. struct phy_device phydev;
  109. u32 fiber:1; /* port is fiber */
  110. u32 force:1;
  111. u32 read:1; /* read MIB counters in background */
  112. u32 freeze:1; /* MIB counter freeze is enabled */
  113. struct ksz_port_mib mib;
  114. phy_interface_t interface;
  115. u32 rgmii_tx_val;
  116. u32 rgmii_rx_val;
  117. struct ksz_device *ksz_dev;
  118. void *acl_priv;
  119. struct ksz_irq pirq;
  120. u8 num;
  121. #if IS_ENABLED(CONFIG_NET_DSA_MICROCHIP_KSZ_PTP)
  122. struct hwtstamp_config tstamp_config;
  123. bool hwts_tx_en;
  124. bool hwts_rx_en;
  125. struct ksz_irq ptpirq;
  126. struct ksz_ptp_irq ptpmsg_irq[3];
  127. ktime_t tstamp_msg;
  128. struct completion tstamp_msg_comp;
  129. #endif
  130. bool manual_flow;
  131. };
  132. struct ksz_device {
  133. struct dsa_switch *ds;
  134. struct ksz_platform_data *pdata;
  135. const struct ksz_chip_data *info;
  136. struct mutex dev_mutex; /* device access */
  137. struct mutex regmap_mutex; /* regmap access */
  138. struct mutex alu_mutex; /* ALU access */
  139. struct mutex vlan_mutex; /* vlan access */
  140. const struct ksz_dev_ops *dev_ops;
  141. struct device *dev;
  142. struct regmap *regmap[__KSZ_NUM_REGMAPS];
  143. void *priv;
  144. int irq;
  145. struct gpio_desc *reset_gpio; /* Optional reset GPIO */
  146. /* chip specific data */
  147. u32 chip_id;
  148. u8 chip_rev;
  149. int cpu_port; /* port connected to CPU */
  150. int phy_port_cnt;
  151. phy_interface_t compat_interface;
  152. bool synclko_125;
  153. bool synclko_disable;
  154. bool wakeup_source;
  155. bool pme_active_high;
  156. struct vlan_table *vlan_cache;
  157. struct ksz_port *ports;
  158. struct delayed_work mib_read;
  159. unsigned long mib_read_interval;
  160. u16 mirror_rx;
  161. u16 mirror_tx;
  162. u16 port_mask;
  163. struct mutex lock_irq; /* IRQ Access */
  164. struct ksz_irq girq;
  165. struct ksz_ptp_data ptp_data;
  166. struct ksz_switch_macaddr *switch_macaddr;
  167. struct net_device *hsr_dev; /* HSR */
  168. u8 hsr_ports;
  169. };
  170. /* List of supported models */
  171. enum ksz_model {
  172. KSZ8563,
  173. KSZ8567,
  174. KSZ8795,
  175. KSZ8794,
  176. KSZ8765,
  177. KSZ88X3,
  178. KSZ8864,
  179. KSZ8895,
  180. KSZ9477,
  181. KSZ9896,
  182. KSZ9897,
  183. KSZ9893,
  184. KSZ9563,
  185. KSZ9567,
  186. LAN9370,
  187. LAN9371,
  188. LAN9372,
  189. LAN9373,
  190. LAN9374,
  191. };
  192. enum ksz_regs {
  193. REG_SW_MAC_ADDR,
  194. REG_IND_CTRL_0,
  195. REG_IND_DATA_8,
  196. REG_IND_DATA_CHECK,
  197. REG_IND_DATA_HI,
  198. REG_IND_DATA_LO,
  199. REG_IND_MIB_CHECK,
  200. REG_IND_BYTE,
  201. P_FORCE_CTRL,
  202. P_LINK_STATUS,
  203. P_LOCAL_CTRL,
  204. P_NEG_RESTART_CTRL,
  205. P_REMOTE_STATUS,
  206. P_SPEED_STATUS,
  207. S_TAIL_TAG_CTRL,
  208. P_STP_CTRL,
  209. S_START_CTRL,
  210. S_BROADCAST_CTRL,
  211. S_MULTICAST_CTRL,
  212. P_XMII_CTRL_0,
  213. P_XMII_CTRL_1,
  214. REG_SW_PME_CTRL,
  215. REG_PORT_PME_STATUS,
  216. REG_PORT_PME_CTRL,
  217. };
  218. enum ksz_masks {
  219. PORT_802_1P_REMAPPING,
  220. SW_TAIL_TAG_ENABLE,
  221. MIB_COUNTER_OVERFLOW,
  222. MIB_COUNTER_VALID,
  223. VLAN_TABLE_FID,
  224. VLAN_TABLE_MEMBERSHIP,
  225. VLAN_TABLE_VALID,
  226. STATIC_MAC_TABLE_VALID,
  227. STATIC_MAC_TABLE_USE_FID,
  228. STATIC_MAC_TABLE_FID,
  229. STATIC_MAC_TABLE_OVERRIDE,
  230. STATIC_MAC_TABLE_FWD_PORTS,
  231. DYNAMIC_MAC_TABLE_ENTRIES_H,
  232. DYNAMIC_MAC_TABLE_MAC_EMPTY,
  233. DYNAMIC_MAC_TABLE_NOT_READY,
  234. DYNAMIC_MAC_TABLE_ENTRIES,
  235. DYNAMIC_MAC_TABLE_FID,
  236. DYNAMIC_MAC_TABLE_SRC_PORT,
  237. DYNAMIC_MAC_TABLE_TIMESTAMP,
  238. ALU_STAT_WRITE,
  239. ALU_STAT_READ,
  240. P_MII_TX_FLOW_CTRL,
  241. P_MII_RX_FLOW_CTRL,
  242. };
  243. enum ksz_shifts {
  244. VLAN_TABLE_MEMBERSHIP_S,
  245. VLAN_TABLE,
  246. STATIC_MAC_FWD_PORTS,
  247. STATIC_MAC_FID,
  248. DYNAMIC_MAC_ENTRIES_H,
  249. DYNAMIC_MAC_ENTRIES,
  250. DYNAMIC_MAC_FID,
  251. DYNAMIC_MAC_TIMESTAMP,
  252. DYNAMIC_MAC_SRC_PORT,
  253. ALU_STAT_INDEX,
  254. };
  255. enum ksz_xmii_ctrl0 {
  256. P_MII_100MBIT,
  257. P_MII_10MBIT,
  258. P_MII_FULL_DUPLEX,
  259. P_MII_HALF_DUPLEX,
  260. };
  261. enum ksz_xmii_ctrl1 {
  262. P_RGMII_SEL,
  263. P_RMII_SEL,
  264. P_GMII_SEL,
  265. P_MII_SEL,
  266. P_GMII_1GBIT,
  267. P_GMII_NOT_1GBIT,
  268. };
  269. struct alu_struct {
  270. /* entry 1 */
  271. u8 is_static:1;
  272. u8 is_src_filter:1;
  273. u8 is_dst_filter:1;
  274. u8 prio_age:3;
  275. u32 _reserv_0_1:23;
  276. u8 mstp:3;
  277. /* entry 2 */
  278. u8 is_override:1;
  279. u8 is_use_fid:1;
  280. u32 _reserv_1_1:23;
  281. u8 port_forward:7;
  282. /* entry 3 & 4*/
  283. u32 _reserv_2_1:9;
  284. u8 fid:7;
  285. u8 mac[ETH_ALEN];
  286. };
  287. struct ksz_dev_ops {
  288. int (*setup)(struct dsa_switch *ds);
  289. void (*teardown)(struct dsa_switch *ds);
  290. u32 (*get_port_addr)(int port, int offset);
  291. void (*cfg_port_member)(struct ksz_device *dev, int port, u8 member);
  292. void (*flush_dyn_mac_table)(struct ksz_device *dev, int port);
  293. void (*port_cleanup)(struct ksz_device *dev, int port);
  294. void (*port_setup)(struct ksz_device *dev, int port, bool cpu_port);
  295. int (*set_ageing_time)(struct ksz_device *dev, unsigned int msecs);
  296. int (*r_phy)(struct ksz_device *dev, u16 phy, u16 reg, u16 *val);
  297. int (*w_phy)(struct ksz_device *dev, u16 phy, u16 reg, u16 val);
  298. void (*r_mib_cnt)(struct ksz_device *dev, int port, u16 addr,
  299. u64 *cnt);
  300. void (*r_mib_pkt)(struct ksz_device *dev, int port, u16 addr,
  301. u64 *dropped, u64 *cnt);
  302. void (*r_mib_stat64)(struct ksz_device *dev, int port);
  303. int (*vlan_filtering)(struct ksz_device *dev, int port,
  304. bool flag, struct netlink_ext_ack *extack);
  305. int (*vlan_add)(struct ksz_device *dev, int port,
  306. const struct switchdev_obj_port_vlan *vlan,
  307. struct netlink_ext_ack *extack);
  308. int (*vlan_del)(struct ksz_device *dev, int port,
  309. const struct switchdev_obj_port_vlan *vlan);
  310. int (*mirror_add)(struct ksz_device *dev, int port,
  311. struct dsa_mall_mirror_tc_entry *mirror,
  312. bool ingress, struct netlink_ext_ack *extack);
  313. void (*mirror_del)(struct ksz_device *dev, int port,
  314. struct dsa_mall_mirror_tc_entry *mirror);
  315. int (*fdb_add)(struct ksz_device *dev, int port,
  316. const unsigned char *addr, u16 vid, struct dsa_db db);
  317. int (*fdb_del)(struct ksz_device *dev, int port,
  318. const unsigned char *addr, u16 vid, struct dsa_db db);
  319. int (*fdb_dump)(struct ksz_device *dev, int port,
  320. dsa_fdb_dump_cb_t *cb, void *data);
  321. int (*mdb_add)(struct ksz_device *dev, int port,
  322. const struct switchdev_obj_port_mdb *mdb,
  323. struct dsa_db db);
  324. int (*mdb_del)(struct ksz_device *dev, int port,
  325. const struct switchdev_obj_port_mdb *mdb,
  326. struct dsa_db db);
  327. void (*get_caps)(struct ksz_device *dev, int port,
  328. struct phylink_config *config);
  329. int (*change_mtu)(struct ksz_device *dev, int port, int mtu);
  330. int (*pme_write8)(struct ksz_device *dev, u32 reg, u8 value);
  331. int (*pme_pread8)(struct ksz_device *dev, int port, int offset,
  332. u8 *data);
  333. int (*pme_pwrite8)(struct ksz_device *dev, int port, int offset,
  334. u8 data);
  335. void (*freeze_mib)(struct ksz_device *dev, int port, bool freeze);
  336. void (*port_init_cnt)(struct ksz_device *dev, int port);
  337. void (*phylink_mac_link_up)(struct ksz_device *dev, int port,
  338. unsigned int mode,
  339. phy_interface_t interface,
  340. struct phy_device *phydev, int speed,
  341. int duplex, bool tx_pause, bool rx_pause);
  342. void (*setup_rgmii_delay)(struct ksz_device *dev, int port);
  343. int (*tc_cbs_set_cinc)(struct ksz_device *dev, int port, u32 val);
  344. void (*config_cpu_port)(struct dsa_switch *ds);
  345. int (*enable_stp_addr)(struct ksz_device *dev);
  346. int (*reset)(struct ksz_device *dev);
  347. int (*init)(struct ksz_device *dev);
  348. void (*exit)(struct ksz_device *dev);
  349. };
  350. struct ksz_device *ksz_switch_alloc(struct device *base, void *priv);
  351. int ksz_switch_register(struct ksz_device *dev);
  352. void ksz_switch_remove(struct ksz_device *dev);
  353. void ksz_init_mib_timer(struct ksz_device *dev);
  354. bool ksz_is_port_mac_global_usable(struct dsa_switch *ds, int port);
  355. void ksz_r_mib_stats64(struct ksz_device *dev, int port);
  356. void ksz88xx_r_mib_stats64(struct ksz_device *dev, int port);
  357. void ksz_port_stp_state_set(struct dsa_switch *ds, int port, u8 state);
  358. bool ksz_get_gbit(struct ksz_device *dev, int port);
  359. phy_interface_t ksz_get_xmii(struct ksz_device *dev, int port, bool gbit);
  360. extern const struct ksz_chip_data ksz_switch_chips[];
  361. int ksz_switch_macaddr_get(struct dsa_switch *ds, int port,
  362. struct netlink_ext_ack *extack);
  363. void ksz_switch_macaddr_put(struct dsa_switch *ds);
  364. void ksz_switch_shutdown(struct ksz_device *dev);
  365. int ksz_handle_wake_reason(struct ksz_device *dev, int port);
  366. /* Common register access functions */
  367. static inline struct regmap *ksz_regmap_8(struct ksz_device *dev)
  368. {
  369. return dev->regmap[KSZ_REGMAP_8];
  370. }
  371. static inline struct regmap *ksz_regmap_16(struct ksz_device *dev)
  372. {
  373. return dev->regmap[KSZ_REGMAP_16];
  374. }
  375. static inline struct regmap *ksz_regmap_32(struct ksz_device *dev)
  376. {
  377. return dev->regmap[KSZ_REGMAP_32];
  378. }
  379. static inline int ksz_read8(struct ksz_device *dev, u32 reg, u8 *val)
  380. {
  381. unsigned int value;
  382. int ret = regmap_read(ksz_regmap_8(dev), reg, &value);
  383. if (ret)
  384. dev_err(dev->dev, "can't read 8bit reg: 0x%x %pe\n", reg,
  385. ERR_PTR(ret));
  386. *val = value;
  387. return ret;
  388. }
  389. static inline int ksz_read16(struct ksz_device *dev, u32 reg, u16 *val)
  390. {
  391. unsigned int value;
  392. int ret = regmap_read(ksz_regmap_16(dev), reg, &value);
  393. if (ret)
  394. dev_err(dev->dev, "can't read 16bit reg: 0x%x %pe\n", reg,
  395. ERR_PTR(ret));
  396. *val = value;
  397. return ret;
  398. }
  399. static inline int ksz_read32(struct ksz_device *dev, u32 reg, u32 *val)
  400. {
  401. unsigned int value;
  402. int ret = regmap_read(ksz_regmap_32(dev), reg, &value);
  403. if (ret)
  404. dev_err(dev->dev, "can't read 32bit reg: 0x%x %pe\n", reg,
  405. ERR_PTR(ret));
  406. *val = value;
  407. return ret;
  408. }
  409. static inline int ksz_read64(struct ksz_device *dev, u32 reg, u64 *val)
  410. {
  411. u32 value[2];
  412. int ret;
  413. ret = regmap_bulk_read(ksz_regmap_32(dev), reg, value, 2);
  414. if (ret)
  415. dev_err(dev->dev, "can't read 64bit reg: 0x%x %pe\n", reg,
  416. ERR_PTR(ret));
  417. else
  418. *val = (u64)value[0] << 32 | value[1];
  419. return ret;
  420. }
  421. static inline int ksz_write8(struct ksz_device *dev, u32 reg, u8 value)
  422. {
  423. int ret;
  424. ret = regmap_write(ksz_regmap_8(dev), reg, value);
  425. if (ret)
  426. dev_err(dev->dev, "can't write 8bit reg: 0x%x %pe\n", reg,
  427. ERR_PTR(ret));
  428. return ret;
  429. }
  430. static inline int ksz_write16(struct ksz_device *dev, u32 reg, u16 value)
  431. {
  432. int ret;
  433. ret = regmap_write(ksz_regmap_16(dev), reg, value);
  434. if (ret)
  435. dev_err(dev->dev, "can't write 16bit reg: 0x%x %pe\n", reg,
  436. ERR_PTR(ret));
  437. return ret;
  438. }
  439. static inline int ksz_write32(struct ksz_device *dev, u32 reg, u32 value)
  440. {
  441. int ret;
  442. ret = regmap_write(ksz_regmap_32(dev), reg, value);
  443. if (ret)
  444. dev_err(dev->dev, "can't write 32bit reg: 0x%x %pe\n", reg,
  445. ERR_PTR(ret));
  446. return ret;
  447. }
  448. static inline int ksz_rmw16(struct ksz_device *dev, u32 reg, u16 mask,
  449. u16 value)
  450. {
  451. int ret;
  452. ret = regmap_update_bits(ksz_regmap_16(dev), reg, mask, value);
  453. if (ret)
  454. dev_err(dev->dev, "can't rmw 16bit reg 0x%x: %pe\n", reg,
  455. ERR_PTR(ret));
  456. return ret;
  457. }
  458. static inline int ksz_rmw32(struct ksz_device *dev, u32 reg, u32 mask,
  459. u32 value)
  460. {
  461. int ret;
  462. ret = regmap_update_bits(ksz_regmap_32(dev), reg, mask, value);
  463. if (ret)
  464. dev_err(dev->dev, "can't rmw 32bit reg 0x%x: %pe\n", reg,
  465. ERR_PTR(ret));
  466. return ret;
  467. }
  468. static inline int ksz_write64(struct ksz_device *dev, u32 reg, u64 value)
  469. {
  470. u32 val[2];
  471. /* Ick! ToDo: Add 64bit R/W to regmap on 32bit systems */
  472. value = swab64(value);
  473. val[0] = swab32(value & 0xffffffffULL);
  474. val[1] = swab32(value >> 32ULL);
  475. return regmap_bulk_write(ksz_regmap_32(dev), reg, val, 2);
  476. }
  477. static inline int ksz_rmw8(struct ksz_device *dev, int offset, u8 mask, u8 val)
  478. {
  479. int ret;
  480. ret = regmap_update_bits(ksz_regmap_8(dev), offset, mask, val);
  481. if (ret)
  482. dev_err(dev->dev, "can't rmw 8bit reg 0x%x: %pe\n", offset,
  483. ERR_PTR(ret));
  484. return ret;
  485. }
  486. static inline int ksz_pread8(struct ksz_device *dev, int port, int offset,
  487. u8 *data)
  488. {
  489. return ksz_read8(dev, dev->dev_ops->get_port_addr(port, offset), data);
  490. }
  491. static inline int ksz_pread16(struct ksz_device *dev, int port, int offset,
  492. u16 *data)
  493. {
  494. return ksz_read16(dev, dev->dev_ops->get_port_addr(port, offset), data);
  495. }
  496. static inline int ksz_pread32(struct ksz_device *dev, int port, int offset,
  497. u32 *data)
  498. {
  499. return ksz_read32(dev, dev->dev_ops->get_port_addr(port, offset), data);
  500. }
  501. static inline int ksz_pwrite8(struct ksz_device *dev, int port, int offset,
  502. u8 data)
  503. {
  504. return ksz_write8(dev, dev->dev_ops->get_port_addr(port, offset), data);
  505. }
  506. static inline int ksz_pwrite16(struct ksz_device *dev, int port, int offset,
  507. u16 data)
  508. {
  509. return ksz_write16(dev, dev->dev_ops->get_port_addr(port, offset),
  510. data);
  511. }
  512. static inline int ksz_pwrite32(struct ksz_device *dev, int port, int offset,
  513. u32 data)
  514. {
  515. return ksz_write32(dev, dev->dev_ops->get_port_addr(port, offset),
  516. data);
  517. }
  518. static inline int ksz_prmw8(struct ksz_device *dev, int port, int offset,
  519. u8 mask, u8 val)
  520. {
  521. return ksz_rmw8(dev, dev->dev_ops->get_port_addr(port, offset),
  522. mask, val);
  523. }
  524. static inline int ksz_prmw32(struct ksz_device *dev, int port, int offset,
  525. u32 mask, u32 val)
  526. {
  527. return ksz_rmw32(dev, dev->dev_ops->get_port_addr(port, offset),
  528. mask, val);
  529. }
  530. static inline void ksz_regmap_lock(void *__mtx)
  531. {
  532. struct mutex *mtx = __mtx;
  533. mutex_lock(mtx);
  534. }
  535. static inline void ksz_regmap_unlock(void *__mtx)
  536. {
  537. struct mutex *mtx = __mtx;
  538. mutex_unlock(mtx);
  539. }
  540. static inline bool ksz_is_ksz87xx(struct ksz_device *dev)
  541. {
  542. return dev->chip_id == KSZ8795_CHIP_ID ||
  543. dev->chip_id == KSZ8794_CHIP_ID ||
  544. dev->chip_id == KSZ8765_CHIP_ID;
  545. }
  546. static inline bool ksz_is_ksz88x3(struct ksz_device *dev)
  547. {
  548. return dev->chip_id == KSZ88X3_CHIP_ID;
  549. }
  550. static inline bool ksz_is_8895_family(struct ksz_device *dev)
  551. {
  552. return dev->chip_id == KSZ8895_CHIP_ID ||
  553. dev->chip_id == KSZ8864_CHIP_ID;
  554. }
  555. static inline bool is_ksz8(struct ksz_device *dev)
  556. {
  557. return ksz_is_ksz87xx(dev) || ksz_is_ksz88x3(dev) ||
  558. ksz_is_8895_family(dev);
  559. }
  560. static inline bool is_ksz88xx(struct ksz_device *dev)
  561. {
  562. return ksz_is_ksz88x3(dev) || ksz_is_8895_family(dev);
  563. }
  564. static inline bool is_ksz9477(struct ksz_device *dev)
  565. {
  566. return dev->chip_id == KSZ9477_CHIP_ID;
  567. }
  568. static inline int is_lan937x(struct ksz_device *dev)
  569. {
  570. return dev->chip_id == LAN9370_CHIP_ID ||
  571. dev->chip_id == LAN9371_CHIP_ID ||
  572. dev->chip_id == LAN9372_CHIP_ID ||
  573. dev->chip_id == LAN9373_CHIP_ID ||
  574. dev->chip_id == LAN9374_CHIP_ID;
  575. }
  576. static inline bool is_lan937x_tx_phy(struct ksz_device *dev, int port)
  577. {
  578. return (dev->chip_id == LAN9371_CHIP_ID ||
  579. dev->chip_id == LAN9372_CHIP_ID) && port == KSZ_PORT_4;
  580. }
  581. /* STP State Defines */
  582. #define PORT_TX_ENABLE BIT(2)
  583. #define PORT_RX_ENABLE BIT(1)
  584. #define PORT_LEARN_DISABLE BIT(0)
  585. /* Switch ID Defines */
  586. #define REG_CHIP_ID0 0x00
  587. #define SW_FAMILY_ID_M GENMASK(15, 8)
  588. #define KSZ87_FAMILY_ID 0x87
  589. #define KSZ88_FAMILY_ID 0x88
  590. #define KSZ8895_FAMILY_ID 0x95
  591. #define KSZ8_PORT_STATUS_0 0x08
  592. #define KSZ8_PORT_FIBER_MODE BIT(7)
  593. #define SW_CHIP_ID_M GENMASK(7, 4)
  594. #define KSZ87_CHIP_ID_94 0x6
  595. #define KSZ87_CHIP_ID_95 0x9
  596. #define KSZ88_CHIP_ID_63 0x3
  597. #define KSZ8895_CHIP_ID_95 0x4
  598. #define KSZ8895_CHIP_ID_95R 0x6
  599. /* KSZ8895 specific register */
  600. #define REG_KSZ8864_CHIP_ID 0xFE
  601. #define SW_KSZ8864 BIT(7)
  602. #define SW_REV_ID_M GENMASK(7, 4)
  603. /* KSZ9893, KSZ9563, KSZ8563 specific register */
  604. #define REG_CHIP_ID4 0x0f
  605. #define SKU_ID_KSZ8563 0x3c
  606. #define SKU_ID_KSZ9563 0x1c
  607. /* Driver set switch broadcast storm protection at 10% rate. */
  608. #define BROADCAST_STORM_PROT_RATE 10
  609. /* 148,800 frames * 67 ms / 100 */
  610. #define BROADCAST_STORM_VALUE 9969
  611. #define BROADCAST_STORM_RATE_HI 0x07
  612. #define BROADCAST_STORM_RATE_LO 0xFF
  613. #define BROADCAST_STORM_RATE 0x07FF
  614. #define MULTICAST_STORM_DISABLE BIT(6)
  615. #define SW_START 0x01
  616. /* xMII configuration */
  617. #define P_MII_DUPLEX_M BIT(6)
  618. #define P_MII_100MBIT_M BIT(4)
  619. #define P_GMII_1GBIT_M BIT(6)
  620. #define P_RGMII_ID_IG_ENABLE BIT(4)
  621. #define P_RGMII_ID_EG_ENABLE BIT(3)
  622. #define P_MII_MAC_MODE BIT(2)
  623. #define P_MII_SEL_M 0x3
  624. /* KSZ9477, KSZ87xx Wake-on-LAN (WoL) masks */
  625. #define PME_WOL_MAGICPKT BIT(2)
  626. #define PME_WOL_LINKUP BIT(1)
  627. #define PME_WOL_ENERGY BIT(0)
  628. #define PME_ENABLE BIT(1)
  629. #define PME_POLARITY BIT(0)
  630. #define KSZ87XX_REG_INT_EN 0x7D
  631. #define KSZ87XX_INT_PME_MASK BIT(4)
  632. /* Interrupt */
  633. #define REG_SW_PORT_INT_STATUS__1 0x001B
  634. #define REG_SW_PORT_INT_MASK__1 0x001F
  635. #define REG_PORT_INT_STATUS 0x001B
  636. #define REG_PORT_INT_MASK 0x001F
  637. #define PORT_SRC_PHY_INT 1
  638. #define PORT_SRC_PTP_INT 2
  639. #define KSZ8795_HUGE_PACKET_SIZE 2000
  640. #define KSZ8863_HUGE_PACKET_SIZE 1916
  641. #define KSZ8863_NORMAL_PACKET_SIZE 1536
  642. #define KSZ8_LEGAL_PACKET_SIZE 1518
  643. #define KSZ9477_MAX_FRAME_SIZE 9000
  644. #define KSZ8873_REG_GLOBAL_CTRL_12 0x0e
  645. /* Drive Strength of I/O Pad
  646. * 0: 8mA, 1: 16mA
  647. */
  648. #define KSZ8873_DRIVE_STRENGTH_16MA BIT(6)
  649. #define KSZ8795_REG_SW_CTRL_20 0xa3
  650. #define KSZ9477_REG_SW_IO_STRENGTH 0x010d
  651. #define SW_DRIVE_STRENGTH_M 0x7
  652. #define SW_DRIVE_STRENGTH_2MA 0
  653. #define SW_DRIVE_STRENGTH_4MA 1
  654. #define SW_DRIVE_STRENGTH_8MA 2
  655. #define SW_DRIVE_STRENGTH_12MA 3
  656. #define SW_DRIVE_STRENGTH_16MA 4
  657. #define SW_DRIVE_STRENGTH_20MA 5
  658. #define SW_DRIVE_STRENGTH_24MA 6
  659. #define SW_DRIVE_STRENGTH_28MA 7
  660. #define SW_HI_SPEED_DRIVE_STRENGTH_S 4
  661. #define SW_LO_SPEED_DRIVE_STRENGTH_S 0
  662. #define KSZ9477_REG_PORT_OUT_RATE_0 0x0420
  663. #define KSZ9477_OUT_RATE_NO_LIMIT 0
  664. #define KSZ9477_PORT_MRI_TC_MAP__4 0x0808
  665. #define KSZ9477_PORT_TC_MAP_S 4
  666. /* CBS related registers */
  667. #define REG_PORT_MTI_QUEUE_INDEX__4 0x0900
  668. #define REG_PORT_MTI_QUEUE_CTRL_0 0x0914
  669. #define MTI_SCHEDULE_MODE_M GENMASK(7, 6)
  670. #define MTI_SCHEDULE_STRICT_PRIO 0
  671. #define MTI_SCHEDULE_WRR 2
  672. #define MTI_SHAPING_M GENMASK(5, 4)
  673. #define MTI_SHAPING_OFF 0
  674. #define MTI_SHAPING_SRP 1
  675. #define MTI_SHAPING_TIME_AWARE 2
  676. #define KSZ9477_PORT_MTI_QUEUE_CTRL_1 0x0915
  677. #define KSZ9477_DEFAULT_WRR_WEIGHT 1
  678. #define REG_PORT_MTI_HI_WATER_MARK 0x0916
  679. #define REG_PORT_MTI_LO_WATER_MARK 0x0918
  680. /* Regmap tables generation */
  681. #define KSZ_SPI_OP_RD 3
  682. #define KSZ_SPI_OP_WR 2
  683. #define swabnot_used(x) 0
  684. #define KSZ_SPI_OP_FLAG_MASK(opcode, swp, regbits, regpad) \
  685. swab##swp((opcode) << ((regbits) + (regpad)))
  686. #define KSZ_REGMAP_ENTRY(width, swp, regbits, regpad, regalign) \
  687. { \
  688. .name = #width, \
  689. .val_bits = (width), \
  690. .reg_stride = 1, \
  691. .reg_bits = (regbits) + (regalign), \
  692. .pad_bits = (regpad), \
  693. .max_register = BIT(regbits) - 1, \
  694. .cache_type = REGCACHE_NONE, \
  695. .read_flag_mask = \
  696. KSZ_SPI_OP_FLAG_MASK(KSZ_SPI_OP_RD, swp, \
  697. regbits, regpad), \
  698. .write_flag_mask = \
  699. KSZ_SPI_OP_FLAG_MASK(KSZ_SPI_OP_WR, swp, \
  700. regbits, regpad), \
  701. .lock = ksz_regmap_lock, \
  702. .unlock = ksz_regmap_unlock, \
  703. .reg_format_endian = REGMAP_ENDIAN_BIG, \
  704. .val_format_endian = REGMAP_ENDIAN_BIG \
  705. }
  706. #define KSZ_REGMAP_TABLE(ksz, swp, regbits, regpad, regalign) \
  707. static const struct regmap_config ksz##_regmap_config[] = { \
  708. [KSZ_REGMAP_8] = KSZ_REGMAP_ENTRY(8, swp, (regbits), (regpad), (regalign)), \
  709. [KSZ_REGMAP_16] = KSZ_REGMAP_ENTRY(16, swp, (regbits), (regpad), (regalign)), \
  710. [KSZ_REGMAP_32] = KSZ_REGMAP_ENTRY(32, swp, (regbits), (regpad), (regalign)), \
  711. }
  712. #endif