mxl862xx.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Driver for MaxLinear MxL862xx switch family
  4. *
  5. * Copyright (C) 2024 MaxLinear Inc.
  6. * Copyright (C) 2025 John Crispin <john@phrozen.org>
  7. * Copyright (C) 2025 Daniel Golle <daniel@makrotopia.org>
  8. */
  9. #include <linux/module.h>
  10. #include <linux/delay.h>
  11. #include <linux/of_device.h>
  12. #include <linux/of_mdio.h>
  13. #include <linux/phy.h>
  14. #include <linux/phylink.h>
  15. #include <net/dsa.h>
  16. #include "mxl862xx.h"
  17. #include "mxl862xx-api.h"
  18. #include "mxl862xx-cmd.h"
  19. #include "mxl862xx-host.h"
  20. #define MXL862XX_API_WRITE(dev, cmd, data) \
  21. mxl862xx_api_wrap(dev, cmd, &(data), sizeof((data)), false, false)
  22. #define MXL862XX_API_READ(dev, cmd, data) \
  23. mxl862xx_api_wrap(dev, cmd, &(data), sizeof((data)), true, false)
  24. #define MXL862XX_API_READ_QUIET(dev, cmd, data) \
  25. mxl862xx_api_wrap(dev, cmd, &(data), sizeof((data)), true, true)
  26. #define MXL862XX_SDMA_PCTRLP(p) (0xbc0 + ((p) * 0x6))
  27. #define MXL862XX_SDMA_PCTRL_EN BIT(0)
  28. #define MXL862XX_FDMA_PCTRLP(p) (0xa80 + ((p) * 0x6))
  29. #define MXL862XX_FDMA_PCTRL_EN BIT(0)
  30. #define MXL862XX_READY_TIMEOUT_MS 10000
  31. #define MXL862XX_READY_POLL_MS 100
  32. static enum dsa_tag_protocol mxl862xx_get_tag_protocol(struct dsa_switch *ds,
  33. int port,
  34. enum dsa_tag_protocol m)
  35. {
  36. return DSA_TAG_PROTO_MXL862;
  37. }
  38. /* PHY access via firmware relay */
  39. static int mxl862xx_phy_read_mmd(struct mxl862xx_priv *priv, int port,
  40. int devadd, int reg)
  41. {
  42. struct mdio_relay_data param = {
  43. .phy = port,
  44. .mmd = devadd,
  45. .reg = cpu_to_le16(reg),
  46. };
  47. int ret;
  48. ret = MXL862XX_API_READ(priv, INT_GPHY_READ, param);
  49. if (ret)
  50. return ret;
  51. return le16_to_cpu(param.data);
  52. }
  53. static int mxl862xx_phy_write_mmd(struct mxl862xx_priv *priv, int port,
  54. int devadd, int reg, u16 data)
  55. {
  56. struct mdio_relay_data param = {
  57. .phy = port,
  58. .mmd = devadd,
  59. .reg = cpu_to_le16(reg),
  60. .data = cpu_to_le16(data),
  61. };
  62. return MXL862XX_API_WRITE(priv, INT_GPHY_WRITE, param);
  63. }
  64. static int mxl862xx_phy_read_mii_bus(struct mii_bus *bus, int port, int regnum)
  65. {
  66. return mxl862xx_phy_read_mmd(bus->priv, port, 0, regnum);
  67. }
  68. static int mxl862xx_phy_write_mii_bus(struct mii_bus *bus, int port,
  69. int regnum, u16 val)
  70. {
  71. return mxl862xx_phy_write_mmd(bus->priv, port, 0, regnum, val);
  72. }
  73. static int mxl862xx_phy_read_c45_mii_bus(struct mii_bus *bus, int port,
  74. int devadd, int regnum)
  75. {
  76. return mxl862xx_phy_read_mmd(bus->priv, port, devadd, regnum);
  77. }
  78. static int mxl862xx_phy_write_c45_mii_bus(struct mii_bus *bus, int port,
  79. int devadd, int regnum, u16 val)
  80. {
  81. return mxl862xx_phy_write_mmd(bus->priv, port, devadd, regnum, val);
  82. }
  83. static int mxl862xx_wait_ready(struct dsa_switch *ds)
  84. {
  85. struct mxl862xx_sys_fw_image_version ver = {};
  86. unsigned long start = jiffies, timeout;
  87. struct mxl862xx_priv *priv = ds->priv;
  88. struct mxl862xx_cfg cfg = {};
  89. int ret;
  90. timeout = start + msecs_to_jiffies(MXL862XX_READY_TIMEOUT_MS);
  91. msleep(2000); /* it always takes at least 2 seconds */
  92. do {
  93. ret = MXL862XX_API_READ_QUIET(priv, SYS_MISC_FW_VERSION, ver);
  94. if (ret || !ver.iv_major)
  95. goto not_ready_yet;
  96. /* being able to perform CFGGET indicates that
  97. * the firmware is ready
  98. */
  99. ret = MXL862XX_API_READ_QUIET(priv,
  100. MXL862XX_COMMON_CFGGET,
  101. cfg);
  102. if (ret)
  103. goto not_ready_yet;
  104. dev_info(ds->dev, "switch ready after %ums, firmware %u.%u.%u (build %u)\n",
  105. jiffies_to_msecs(jiffies - start),
  106. ver.iv_major, ver.iv_minor,
  107. le16_to_cpu(ver.iv_revision),
  108. le32_to_cpu(ver.iv_build_num));
  109. return 0;
  110. not_ready_yet:
  111. msleep(MXL862XX_READY_POLL_MS);
  112. } while (time_before(jiffies, timeout));
  113. dev_err(ds->dev, "switch not responding after reset\n");
  114. return -ETIMEDOUT;
  115. }
  116. static int mxl862xx_setup_mdio(struct dsa_switch *ds)
  117. {
  118. struct mxl862xx_priv *priv = ds->priv;
  119. struct device *dev = ds->dev;
  120. struct device_node *mdio_np;
  121. struct mii_bus *bus;
  122. int ret;
  123. bus = devm_mdiobus_alloc(dev);
  124. if (!bus)
  125. return -ENOMEM;
  126. bus->priv = priv;
  127. bus->name = KBUILD_MODNAME "-mii";
  128. snprintf(bus->id, MII_BUS_ID_SIZE, "%s-mii", dev_name(dev));
  129. bus->read_c45 = mxl862xx_phy_read_c45_mii_bus;
  130. bus->write_c45 = mxl862xx_phy_write_c45_mii_bus;
  131. bus->read = mxl862xx_phy_read_mii_bus;
  132. bus->write = mxl862xx_phy_write_mii_bus;
  133. bus->parent = dev;
  134. bus->phy_mask = ~ds->phys_mii_mask;
  135. mdio_np = of_get_child_by_name(dev->of_node, "mdio");
  136. if (!mdio_np)
  137. return -ENODEV;
  138. ret = devm_of_mdiobus_register(dev, bus, mdio_np);
  139. of_node_put(mdio_np);
  140. return ret;
  141. }
  142. static int mxl862xx_setup(struct dsa_switch *ds)
  143. {
  144. struct mxl862xx_priv *priv = ds->priv;
  145. int ret;
  146. ret = mxl862xx_reset(priv);
  147. if (ret)
  148. return ret;
  149. ret = mxl862xx_wait_ready(ds);
  150. if (ret)
  151. return ret;
  152. return mxl862xx_setup_mdio(ds);
  153. }
  154. static int mxl862xx_port_state(struct dsa_switch *ds, int port, bool enable)
  155. {
  156. struct mxl862xx_register_mod sdma = {
  157. .addr = cpu_to_le16(MXL862XX_SDMA_PCTRLP(port)),
  158. .data = cpu_to_le16(enable ? MXL862XX_SDMA_PCTRL_EN : 0),
  159. .mask = cpu_to_le16(MXL862XX_SDMA_PCTRL_EN),
  160. };
  161. struct mxl862xx_register_mod fdma = {
  162. .addr = cpu_to_le16(MXL862XX_FDMA_PCTRLP(port)),
  163. .data = cpu_to_le16(enable ? MXL862XX_FDMA_PCTRL_EN : 0),
  164. .mask = cpu_to_le16(MXL862XX_FDMA_PCTRL_EN),
  165. };
  166. int ret;
  167. ret = MXL862XX_API_WRITE(ds->priv, MXL862XX_COMMON_REGISTERMOD, sdma);
  168. if (ret)
  169. return ret;
  170. return MXL862XX_API_WRITE(ds->priv, MXL862XX_COMMON_REGISTERMOD, fdma);
  171. }
  172. static int mxl862xx_port_enable(struct dsa_switch *ds, int port,
  173. struct phy_device *phydev)
  174. {
  175. return mxl862xx_port_state(ds, port, true);
  176. }
  177. static void mxl862xx_port_disable(struct dsa_switch *ds, int port)
  178. {
  179. if (mxl862xx_port_state(ds, port, false))
  180. dev_err(ds->dev, "failed to disable port %d\n", port);
  181. }
  182. static void mxl862xx_port_fast_age(struct dsa_switch *ds, int port)
  183. {
  184. struct mxl862xx_mac_table_clear param = {
  185. .type = MXL862XX_MAC_CLEAR_PHY_PORT,
  186. .port_id = port,
  187. };
  188. if (MXL862XX_API_WRITE(ds->priv, MXL862XX_MAC_TABLECLEARCOND, param))
  189. dev_err(ds->dev, "failed to clear fdb on port %d\n", port);
  190. }
  191. static int mxl862xx_configure_ctp_port(struct dsa_switch *ds, int port,
  192. u16 first_ctp_port_id,
  193. u16 number_of_ctp_ports)
  194. {
  195. struct mxl862xx_ctp_port_assignment ctp_assign = {
  196. .logical_port_id = port,
  197. .first_ctp_port_id = cpu_to_le16(first_ctp_port_id),
  198. .number_of_ctp_port = cpu_to_le16(number_of_ctp_ports),
  199. .mode = cpu_to_le32(MXL862XX_LOGICAL_PORT_ETHERNET),
  200. };
  201. return MXL862XX_API_WRITE(ds->priv, MXL862XX_CTP_PORTASSIGNMENTSET,
  202. ctp_assign);
  203. }
  204. static int mxl862xx_configure_sp_tag_proto(struct dsa_switch *ds, int port,
  205. bool enable)
  206. {
  207. struct mxl862xx_ss_sp_tag tag = {
  208. .pid = port,
  209. .mask = MXL862XX_SS_SP_TAG_MASK_RX | MXL862XX_SS_SP_TAG_MASK_TX,
  210. .rx = enable ? MXL862XX_SS_SP_TAG_RX_TAG_NO_INSERT :
  211. MXL862XX_SS_SP_TAG_RX_NO_TAG_INSERT,
  212. .tx = enable ? MXL862XX_SS_SP_TAG_TX_TAG_NO_REMOVE :
  213. MXL862XX_SS_SP_TAG_TX_TAG_REMOVE,
  214. };
  215. return MXL862XX_API_WRITE(ds->priv, MXL862XX_SS_SPTAG_SET, tag);
  216. }
  217. static int mxl862xx_setup_cpu_bridge(struct dsa_switch *ds, int port)
  218. {
  219. struct mxl862xx_bridge_port_config br_port_cfg = {};
  220. struct mxl862xx_priv *priv = ds->priv;
  221. u16 bridge_port_map = 0;
  222. struct dsa_port *dp;
  223. /* CPU port bridge setup */
  224. br_port_cfg.mask = cpu_to_le32(MXL862XX_BRIDGE_PORT_CONFIG_MASK_BRIDGE_PORT_MAP |
  225. MXL862XX_BRIDGE_PORT_CONFIG_MASK_MC_SRC_MAC_LEARNING |
  226. MXL862XX_BRIDGE_PORT_CONFIG_MASK_VLAN_BASED_MAC_LEARNING);
  227. br_port_cfg.bridge_port_id = cpu_to_le16(port);
  228. br_port_cfg.src_mac_learning_disable = false;
  229. br_port_cfg.vlan_src_mac_vid_enable = true;
  230. br_port_cfg.vlan_dst_mac_vid_enable = true;
  231. /* include all assigned user ports in the CPU portmap */
  232. dsa_switch_for_each_user_port(dp, ds) {
  233. /* it's safe to rely on cpu_dp being valid for user ports */
  234. if (dp->cpu_dp->index != port)
  235. continue;
  236. bridge_port_map |= BIT(dp->index);
  237. }
  238. br_port_cfg.bridge_port_map[0] |= cpu_to_le16(bridge_port_map);
  239. return MXL862XX_API_WRITE(priv, MXL862XX_BRIDGEPORT_CONFIGSET, br_port_cfg);
  240. }
  241. static int mxl862xx_add_single_port_bridge(struct dsa_switch *ds, int port)
  242. {
  243. struct mxl862xx_bridge_port_config br_port_cfg = {};
  244. struct dsa_port *dp = dsa_to_port(ds, port);
  245. struct mxl862xx_bridge_alloc br_alloc = {};
  246. int ret;
  247. ret = MXL862XX_API_READ(ds->priv, MXL862XX_BRIDGE_ALLOC, br_alloc);
  248. if (ret) {
  249. dev_err(ds->dev, "failed to allocate a bridge for port %d\n", port);
  250. return ret;
  251. }
  252. br_port_cfg.bridge_id = br_alloc.bridge_id;
  253. br_port_cfg.bridge_port_id = cpu_to_le16(port);
  254. br_port_cfg.mask = cpu_to_le32(MXL862XX_BRIDGE_PORT_CONFIG_MASK_BRIDGE_ID |
  255. MXL862XX_BRIDGE_PORT_CONFIG_MASK_BRIDGE_PORT_MAP |
  256. MXL862XX_BRIDGE_PORT_CONFIG_MASK_MC_SRC_MAC_LEARNING |
  257. MXL862XX_BRIDGE_PORT_CONFIG_MASK_VLAN_BASED_MAC_LEARNING);
  258. br_port_cfg.src_mac_learning_disable = true;
  259. br_port_cfg.vlan_src_mac_vid_enable = false;
  260. br_port_cfg.vlan_dst_mac_vid_enable = false;
  261. /* As this function is only called for user ports it is safe to rely on
  262. * cpu_dp being valid
  263. */
  264. br_port_cfg.bridge_port_map[0] = cpu_to_le16(BIT(dp->cpu_dp->index));
  265. return MXL862XX_API_WRITE(ds->priv, MXL862XX_BRIDGEPORT_CONFIGSET, br_port_cfg);
  266. }
  267. static int mxl862xx_port_setup(struct dsa_switch *ds, int port)
  268. {
  269. struct dsa_port *dp = dsa_to_port(ds, port);
  270. bool is_cpu_port = dsa_port_is_cpu(dp);
  271. int ret;
  272. /* disable port and flush MAC entries */
  273. ret = mxl862xx_port_state(ds, port, false);
  274. if (ret)
  275. return ret;
  276. mxl862xx_port_fast_age(ds, port);
  277. /* skip setup for unused and DSA ports */
  278. if (dsa_port_is_unused(dp) ||
  279. dsa_port_is_dsa(dp))
  280. return 0;
  281. /* configure tag protocol */
  282. ret = mxl862xx_configure_sp_tag_proto(ds, port, is_cpu_port);
  283. if (ret)
  284. return ret;
  285. /* assign CTP port IDs */
  286. ret = mxl862xx_configure_ctp_port(ds, port, port,
  287. is_cpu_port ? 32 - port : 1);
  288. if (ret)
  289. return ret;
  290. if (is_cpu_port)
  291. /* assign user ports to CPU port bridge */
  292. return mxl862xx_setup_cpu_bridge(ds, port);
  293. /* setup single-port bridge for user ports */
  294. return mxl862xx_add_single_port_bridge(ds, port);
  295. }
  296. static void mxl862xx_phylink_get_caps(struct dsa_switch *ds, int port,
  297. struct phylink_config *config)
  298. {
  299. config->mac_capabilities = MAC_ASYM_PAUSE | MAC_SYM_PAUSE | MAC_10 |
  300. MAC_100 | MAC_1000 | MAC_2500FD;
  301. __set_bit(PHY_INTERFACE_MODE_INTERNAL,
  302. config->supported_interfaces);
  303. }
  304. static const struct dsa_switch_ops mxl862xx_switch_ops = {
  305. .get_tag_protocol = mxl862xx_get_tag_protocol,
  306. .setup = mxl862xx_setup,
  307. .port_setup = mxl862xx_port_setup,
  308. .phylink_get_caps = mxl862xx_phylink_get_caps,
  309. .port_enable = mxl862xx_port_enable,
  310. .port_disable = mxl862xx_port_disable,
  311. .port_fast_age = mxl862xx_port_fast_age,
  312. };
  313. static void mxl862xx_phylink_mac_config(struct phylink_config *config,
  314. unsigned int mode,
  315. const struct phylink_link_state *state)
  316. {
  317. }
  318. static void mxl862xx_phylink_mac_link_down(struct phylink_config *config,
  319. unsigned int mode,
  320. phy_interface_t interface)
  321. {
  322. }
  323. static void mxl862xx_phylink_mac_link_up(struct phylink_config *config,
  324. struct phy_device *phydev,
  325. unsigned int mode,
  326. phy_interface_t interface,
  327. int speed, int duplex,
  328. bool tx_pause, bool rx_pause)
  329. {
  330. }
  331. static const struct phylink_mac_ops mxl862xx_phylink_mac_ops = {
  332. .mac_config = mxl862xx_phylink_mac_config,
  333. .mac_link_down = mxl862xx_phylink_mac_link_down,
  334. .mac_link_up = mxl862xx_phylink_mac_link_up,
  335. };
  336. static int mxl862xx_probe(struct mdio_device *mdiodev)
  337. {
  338. struct device *dev = &mdiodev->dev;
  339. struct mxl862xx_priv *priv;
  340. struct dsa_switch *ds;
  341. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  342. if (!priv)
  343. return -ENOMEM;
  344. priv->mdiodev = mdiodev;
  345. ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL);
  346. if (!ds)
  347. return -ENOMEM;
  348. priv->ds = ds;
  349. ds->dev = dev;
  350. ds->priv = priv;
  351. ds->ops = &mxl862xx_switch_ops;
  352. ds->phylink_mac_ops = &mxl862xx_phylink_mac_ops;
  353. ds->num_ports = MXL862XX_MAX_PORTS;
  354. dev_set_drvdata(dev, ds);
  355. return dsa_register_switch(ds);
  356. }
  357. static void mxl862xx_remove(struct mdio_device *mdiodev)
  358. {
  359. struct dsa_switch *ds = dev_get_drvdata(&mdiodev->dev);
  360. if (!ds)
  361. return;
  362. dsa_unregister_switch(ds);
  363. }
  364. static void mxl862xx_shutdown(struct mdio_device *mdiodev)
  365. {
  366. struct dsa_switch *ds = dev_get_drvdata(&mdiodev->dev);
  367. if (!ds)
  368. return;
  369. dsa_switch_shutdown(ds);
  370. dev_set_drvdata(&mdiodev->dev, NULL);
  371. }
  372. static const struct of_device_id mxl862xx_of_match[] = {
  373. { .compatible = "maxlinear,mxl86282" },
  374. { .compatible = "maxlinear,mxl86252" },
  375. { /* sentinel */ }
  376. };
  377. MODULE_DEVICE_TABLE(of, mxl862xx_of_match);
  378. static struct mdio_driver mxl862xx_driver = {
  379. .probe = mxl862xx_probe,
  380. .remove = mxl862xx_remove,
  381. .shutdown = mxl862xx_shutdown,
  382. .mdiodrv.driver = {
  383. .name = "mxl862xx",
  384. .of_match_table = mxl862xx_of_match,
  385. },
  386. };
  387. mdio_module_driver(mxl862xx_driver);
  388. MODULE_DESCRIPTION("Driver for MaxLinear MxL862xx switch family");
  389. MODULE_LICENSE("GPL");