ocelot_vsc7514.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. // SPDX-License-Identifier: (GPL-2.0 OR MIT)
  2. /*
  3. * Microsemi Ocelot Switch driver
  4. *
  5. * Copyright (c) 2017 Microsemi Corporation
  6. */
  7. #include <linux/dsa/ocelot.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/iopoll.h>
  10. #include <linux/module.h>
  11. #include <linux/of_net.h>
  12. #include <linux/netdevice.h>
  13. #include <linux/phylink.h>
  14. #include <linux/of_mdio.h>
  15. #include <linux/of_platform.h>
  16. #include <linux/mfd/syscon.h>
  17. #include <linux/skbuff.h>
  18. #include <net/switchdev.h>
  19. #include <soc/mscc/ocelot_vcap.h>
  20. #include <soc/mscc/ocelot_hsio.h>
  21. #include <soc/mscc/vsc7514_regs.h>
  22. #include "ocelot_fdma.h"
  23. #include "ocelot.h"
  24. #define VSC7514_VCAP_POLICER_BASE 128
  25. #define VSC7514_VCAP_POLICER_MAX 191
  26. #define MEM_INIT_SLEEP_US 1000
  27. #define MEM_INIT_TIMEOUT_US 100000
  28. static const u32 *ocelot_regmap[TARGET_MAX] = {
  29. [ANA] = vsc7514_ana_regmap,
  30. [QS] = vsc7514_qs_regmap,
  31. [QSYS] = vsc7514_qsys_regmap,
  32. [REW] = vsc7514_rew_regmap,
  33. [SYS] = vsc7514_sys_regmap,
  34. [S0] = vsc7514_vcap_regmap,
  35. [S1] = vsc7514_vcap_regmap,
  36. [S2] = vsc7514_vcap_regmap,
  37. [PTP] = vsc7514_ptp_regmap,
  38. [DEV_GMII] = vsc7514_dev_gmii_regmap,
  39. };
  40. static const struct reg_field ocelot_regfields[REGFIELD_MAX] = {
  41. [ANA_ADVLEARN_VLAN_CHK] = REG_FIELD(ANA_ADVLEARN, 11, 11),
  42. [ANA_ADVLEARN_LEARN_MIRROR] = REG_FIELD(ANA_ADVLEARN, 0, 10),
  43. [ANA_ANEVENTS_MSTI_DROP] = REG_FIELD(ANA_ANEVENTS, 27, 27),
  44. [ANA_ANEVENTS_ACLKILL] = REG_FIELD(ANA_ANEVENTS, 26, 26),
  45. [ANA_ANEVENTS_ACLUSED] = REG_FIELD(ANA_ANEVENTS, 25, 25),
  46. [ANA_ANEVENTS_AUTOAGE] = REG_FIELD(ANA_ANEVENTS, 24, 24),
  47. [ANA_ANEVENTS_VS2TTL1] = REG_FIELD(ANA_ANEVENTS, 23, 23),
  48. [ANA_ANEVENTS_STORM_DROP] = REG_FIELD(ANA_ANEVENTS, 22, 22),
  49. [ANA_ANEVENTS_LEARN_DROP] = REG_FIELD(ANA_ANEVENTS, 21, 21),
  50. [ANA_ANEVENTS_AGED_ENTRY] = REG_FIELD(ANA_ANEVENTS, 20, 20),
  51. [ANA_ANEVENTS_CPU_LEARN_FAILED] = REG_FIELD(ANA_ANEVENTS, 19, 19),
  52. [ANA_ANEVENTS_AUTO_LEARN_FAILED] = REG_FIELD(ANA_ANEVENTS, 18, 18),
  53. [ANA_ANEVENTS_LEARN_REMOVE] = REG_FIELD(ANA_ANEVENTS, 17, 17),
  54. [ANA_ANEVENTS_AUTO_LEARNED] = REG_FIELD(ANA_ANEVENTS, 16, 16),
  55. [ANA_ANEVENTS_AUTO_MOVED] = REG_FIELD(ANA_ANEVENTS, 15, 15),
  56. [ANA_ANEVENTS_DROPPED] = REG_FIELD(ANA_ANEVENTS, 14, 14),
  57. [ANA_ANEVENTS_CLASSIFIED_DROP] = REG_FIELD(ANA_ANEVENTS, 13, 13),
  58. [ANA_ANEVENTS_CLASSIFIED_COPY] = REG_FIELD(ANA_ANEVENTS, 12, 12),
  59. [ANA_ANEVENTS_VLAN_DISCARD] = REG_FIELD(ANA_ANEVENTS, 11, 11),
  60. [ANA_ANEVENTS_FWD_DISCARD] = REG_FIELD(ANA_ANEVENTS, 10, 10),
  61. [ANA_ANEVENTS_MULTICAST_FLOOD] = REG_FIELD(ANA_ANEVENTS, 9, 9),
  62. [ANA_ANEVENTS_UNICAST_FLOOD] = REG_FIELD(ANA_ANEVENTS, 8, 8),
  63. [ANA_ANEVENTS_DEST_KNOWN] = REG_FIELD(ANA_ANEVENTS, 7, 7),
  64. [ANA_ANEVENTS_BUCKET3_MATCH] = REG_FIELD(ANA_ANEVENTS, 6, 6),
  65. [ANA_ANEVENTS_BUCKET2_MATCH] = REG_FIELD(ANA_ANEVENTS, 5, 5),
  66. [ANA_ANEVENTS_BUCKET1_MATCH] = REG_FIELD(ANA_ANEVENTS, 4, 4),
  67. [ANA_ANEVENTS_BUCKET0_MATCH] = REG_FIELD(ANA_ANEVENTS, 3, 3),
  68. [ANA_ANEVENTS_CPU_OPERATION] = REG_FIELD(ANA_ANEVENTS, 2, 2),
  69. [ANA_ANEVENTS_DMAC_LOOKUP] = REG_FIELD(ANA_ANEVENTS, 1, 1),
  70. [ANA_ANEVENTS_SMAC_LOOKUP] = REG_FIELD(ANA_ANEVENTS, 0, 0),
  71. [ANA_TABLES_MACACCESS_B_DOM] = REG_FIELD(ANA_TABLES_MACACCESS, 18, 18),
  72. [ANA_TABLES_MACTINDX_BUCKET] = REG_FIELD(ANA_TABLES_MACTINDX, 10, 11),
  73. [ANA_TABLES_MACTINDX_M_INDEX] = REG_FIELD(ANA_TABLES_MACTINDX, 0, 9),
  74. [QSYS_TIMED_FRAME_ENTRY_TFRM_VLD] = REG_FIELD(QSYS_TIMED_FRAME_ENTRY, 20, 20),
  75. [QSYS_TIMED_FRAME_ENTRY_TFRM_FP] = REG_FIELD(QSYS_TIMED_FRAME_ENTRY, 8, 19),
  76. [QSYS_TIMED_FRAME_ENTRY_TFRM_PORTNO] = REG_FIELD(QSYS_TIMED_FRAME_ENTRY, 4, 7),
  77. [QSYS_TIMED_FRAME_ENTRY_TFRM_TM_SEL] = REG_FIELD(QSYS_TIMED_FRAME_ENTRY, 1, 3),
  78. [QSYS_TIMED_FRAME_ENTRY_TFRM_TM_T] = REG_FIELD(QSYS_TIMED_FRAME_ENTRY, 0, 0),
  79. [SYS_RESET_CFG_CORE_ENA] = REG_FIELD(SYS_RESET_CFG, 2, 2),
  80. [SYS_RESET_CFG_MEM_ENA] = REG_FIELD(SYS_RESET_CFG, 1, 1),
  81. [SYS_RESET_CFG_MEM_INIT] = REG_FIELD(SYS_RESET_CFG, 0, 0),
  82. /* Replicated per number of ports (12), register size 4 per port */
  83. [QSYS_SWITCH_PORT_MODE_PORT_ENA] = REG_FIELD_ID(QSYS_SWITCH_PORT_MODE, 14, 14, 12, 4),
  84. [QSYS_SWITCH_PORT_MODE_SCH_NEXT_CFG] = REG_FIELD_ID(QSYS_SWITCH_PORT_MODE, 11, 13, 12, 4),
  85. [QSYS_SWITCH_PORT_MODE_YEL_RSRVD] = REG_FIELD_ID(QSYS_SWITCH_PORT_MODE, 10, 10, 12, 4),
  86. [QSYS_SWITCH_PORT_MODE_INGRESS_DROP_MODE] = REG_FIELD_ID(QSYS_SWITCH_PORT_MODE, 9, 9, 12, 4),
  87. [QSYS_SWITCH_PORT_MODE_TX_PFC_ENA] = REG_FIELD_ID(QSYS_SWITCH_PORT_MODE, 1, 8, 12, 4),
  88. [QSYS_SWITCH_PORT_MODE_TX_PFC_MODE] = REG_FIELD_ID(QSYS_SWITCH_PORT_MODE, 0, 0, 12, 4),
  89. [SYS_PORT_MODE_DATA_WO_TS] = REG_FIELD_ID(SYS_PORT_MODE, 5, 6, 12, 4),
  90. [SYS_PORT_MODE_INCL_INJ_HDR] = REG_FIELD_ID(SYS_PORT_MODE, 3, 4, 12, 4),
  91. [SYS_PORT_MODE_INCL_XTR_HDR] = REG_FIELD_ID(SYS_PORT_MODE, 1, 2, 12, 4),
  92. [SYS_PORT_MODE_INCL_HDR_ERR] = REG_FIELD_ID(SYS_PORT_MODE, 0, 0, 12, 4),
  93. [SYS_PAUSE_CFG_PAUSE_START] = REG_FIELD_ID(SYS_PAUSE_CFG, 10, 18, 12, 4),
  94. [SYS_PAUSE_CFG_PAUSE_STOP] = REG_FIELD_ID(SYS_PAUSE_CFG, 1, 9, 12, 4),
  95. [SYS_PAUSE_CFG_PAUSE_ENA] = REG_FIELD_ID(SYS_PAUSE_CFG, 0, 1, 12, 4),
  96. };
  97. static const struct ocelot_stat_layout ocelot_stats_layout[OCELOT_NUM_STATS] = {
  98. OCELOT_COMMON_STATS,
  99. };
  100. static void ocelot_pll5_init(struct ocelot *ocelot)
  101. {
  102. /* Configure PLL5. This will need a proper CCF driver
  103. * The values are coming from the VTSS API for Ocelot
  104. */
  105. regmap_write(ocelot->targets[HSIO], HSIO_PLL5G_CFG4,
  106. HSIO_PLL5G_CFG4_IB_CTRL(0x7600) |
  107. HSIO_PLL5G_CFG4_IB_BIAS_CTRL(0x8));
  108. regmap_write(ocelot->targets[HSIO], HSIO_PLL5G_CFG0,
  109. HSIO_PLL5G_CFG0_CORE_CLK_DIV(0x11) |
  110. HSIO_PLL5G_CFG0_CPU_CLK_DIV(2) |
  111. HSIO_PLL5G_CFG0_ENA_BIAS |
  112. HSIO_PLL5G_CFG0_ENA_VCO_BUF |
  113. HSIO_PLL5G_CFG0_ENA_CP1 |
  114. HSIO_PLL5G_CFG0_SELCPI(2) |
  115. HSIO_PLL5G_CFG0_LOOP_BW_RES(0xe) |
  116. HSIO_PLL5G_CFG0_SELBGV820(4) |
  117. HSIO_PLL5G_CFG0_DIV4 |
  118. HSIO_PLL5G_CFG0_ENA_CLKTREE |
  119. HSIO_PLL5G_CFG0_ENA_LANE);
  120. regmap_write(ocelot->targets[HSIO], HSIO_PLL5G_CFG2,
  121. HSIO_PLL5G_CFG2_EN_RESET_FRQ_DET |
  122. HSIO_PLL5G_CFG2_EN_RESET_OVERRUN |
  123. HSIO_PLL5G_CFG2_GAIN_TEST(0x8) |
  124. HSIO_PLL5G_CFG2_ENA_AMPCTRL |
  125. HSIO_PLL5G_CFG2_PWD_AMPCTRL_N |
  126. HSIO_PLL5G_CFG2_AMPC_SEL(0x10));
  127. }
  128. static int ocelot_chip_init(struct ocelot *ocelot, const struct ocelot_ops *ops)
  129. {
  130. int ret;
  131. ocelot->map = ocelot_regmap;
  132. ocelot->stats_layout = ocelot_stats_layout;
  133. ocelot->num_mact_rows = 1024;
  134. ocelot->ops = ops;
  135. ret = ocelot_regfields_init(ocelot, ocelot_regfields);
  136. if (ret)
  137. return ret;
  138. ocelot_pll5_init(ocelot);
  139. eth_random_addr(ocelot->base_mac);
  140. ocelot->base_mac[5] &= 0xf0;
  141. return 0;
  142. }
  143. static irqreturn_t ocelot_xtr_irq_handler(int irq, void *arg)
  144. {
  145. struct ocelot *ocelot = arg;
  146. int grp = 0, err;
  147. while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp)) {
  148. struct sk_buff *skb;
  149. err = ocelot_xtr_poll_frame(ocelot, grp, &skb);
  150. if (err)
  151. goto out;
  152. skb->dev->stats.rx_bytes += skb->len;
  153. skb->dev->stats.rx_packets++;
  154. if (!skb_defer_rx_timestamp(skb))
  155. netif_rx(skb);
  156. }
  157. out:
  158. if (err < 0)
  159. ocelot_drain_cpu_queue(ocelot, 0);
  160. return IRQ_HANDLED;
  161. }
  162. static irqreturn_t ocelot_ptp_rdy_irq_handler(int irq, void *arg)
  163. {
  164. struct ocelot *ocelot = arg;
  165. ocelot_get_txtstamp(ocelot);
  166. return IRQ_HANDLED;
  167. }
  168. static const struct of_device_id mscc_ocelot_match[] = {
  169. { .compatible = "mscc,vsc7514-switch" },
  170. { }
  171. };
  172. MODULE_DEVICE_TABLE(of, mscc_ocelot_match);
  173. static int ocelot_mem_init_status(struct ocelot *ocelot)
  174. {
  175. unsigned int val;
  176. int err;
  177. err = regmap_field_read(ocelot->regfields[SYS_RESET_CFG_MEM_INIT],
  178. &val);
  179. return err ?: val;
  180. }
  181. static int ocelot_reset(struct ocelot *ocelot)
  182. {
  183. int err;
  184. u32 val;
  185. err = regmap_field_write(ocelot->regfields[SYS_RESET_CFG_MEM_INIT], 1);
  186. if (err)
  187. return err;
  188. err = regmap_field_write(ocelot->regfields[SYS_RESET_CFG_MEM_ENA], 1);
  189. if (err)
  190. return err;
  191. /* MEM_INIT is a self-clearing bit. Wait for it to be cleared (should be
  192. * 100us) before enabling the switch core.
  193. */
  194. err = readx_poll_timeout(ocelot_mem_init_status, ocelot, val, !val,
  195. MEM_INIT_SLEEP_US, MEM_INIT_TIMEOUT_US);
  196. if (err)
  197. return err;
  198. err = regmap_field_write(ocelot->regfields[SYS_RESET_CFG_MEM_ENA], 1);
  199. if (err)
  200. return err;
  201. return regmap_field_write(ocelot->regfields[SYS_RESET_CFG_CORE_ENA], 1);
  202. }
  203. /* Watermark encode
  204. * Bit 8: Unit; 0:1, 1:16
  205. * Bit 7-0: Value to be multiplied with unit
  206. */
  207. static u16 ocelot_wm_enc(u16 value)
  208. {
  209. WARN_ON(value >= 16 * BIT(8));
  210. if (value >= BIT(8))
  211. return BIT(8) | (value / 16);
  212. return value;
  213. }
  214. static u16 ocelot_wm_dec(u16 wm)
  215. {
  216. if (wm & BIT(8))
  217. return (wm & GENMASK(7, 0)) * 16;
  218. return wm;
  219. }
  220. static void ocelot_wm_stat(u32 val, u32 *inuse, u32 *maxuse)
  221. {
  222. *inuse = (val & GENMASK(23, 12)) >> 12;
  223. *maxuse = val & GENMASK(11, 0);
  224. }
  225. static const struct ocelot_ops ocelot_ops = {
  226. .reset = ocelot_reset,
  227. .wm_enc = ocelot_wm_enc,
  228. .wm_dec = ocelot_wm_dec,
  229. .wm_stat = ocelot_wm_stat,
  230. .port_to_netdev = ocelot_port_to_netdev,
  231. .netdev_to_port = ocelot_netdev_to_port,
  232. };
  233. static struct vcap_props vsc7514_vcap_props[] = {
  234. [VCAP_ES0] = {
  235. .action_type_width = 0,
  236. .action_table = {
  237. [ES0_ACTION_TYPE_NORMAL] = {
  238. .width = 73, /* HIT_STICKY not included */
  239. .count = 1,
  240. },
  241. },
  242. .target = S0,
  243. .keys = vsc7514_vcap_es0_keys,
  244. .actions = vsc7514_vcap_es0_actions,
  245. },
  246. [VCAP_IS1] = {
  247. .action_type_width = 0,
  248. .action_table = {
  249. [IS1_ACTION_TYPE_NORMAL] = {
  250. .width = 78, /* HIT_STICKY not included */
  251. .count = 4,
  252. },
  253. },
  254. .target = S1,
  255. .keys = vsc7514_vcap_is1_keys,
  256. .actions = vsc7514_vcap_is1_actions,
  257. },
  258. [VCAP_IS2] = {
  259. .action_type_width = 1,
  260. .action_table = {
  261. [IS2_ACTION_TYPE_NORMAL] = {
  262. .width = 49,
  263. .count = 2
  264. },
  265. [IS2_ACTION_TYPE_SMAC_SIP] = {
  266. .width = 6,
  267. .count = 4
  268. },
  269. },
  270. .target = S2,
  271. .keys = vsc7514_vcap_is2_keys,
  272. .actions = vsc7514_vcap_is2_actions,
  273. },
  274. };
  275. static struct ptp_clock_info ocelot_ptp_clock_info = {
  276. .owner = THIS_MODULE,
  277. .name = "ocelot ptp",
  278. .max_adj = 0x7fffffff,
  279. .n_alarm = 0,
  280. .n_ext_ts = 0,
  281. .n_per_out = OCELOT_PTP_PINS_NUM,
  282. .n_pins = OCELOT_PTP_PINS_NUM,
  283. .pps = 0,
  284. .gettime64 = ocelot_ptp_gettime64,
  285. .settime64 = ocelot_ptp_settime64,
  286. .adjtime = ocelot_ptp_adjtime,
  287. .adjfine = ocelot_ptp_adjfine,
  288. .verify = ocelot_ptp_verify,
  289. .enable = ocelot_ptp_enable,
  290. };
  291. static void mscc_ocelot_teardown_devlink_ports(struct ocelot *ocelot)
  292. {
  293. int port;
  294. for (port = 0; port < ocelot->num_phys_ports; port++)
  295. ocelot_port_devlink_teardown(ocelot, port);
  296. }
  297. static void mscc_ocelot_release_ports(struct ocelot *ocelot)
  298. {
  299. int port;
  300. for (port = 0; port < ocelot->num_phys_ports; port++) {
  301. struct ocelot_port *ocelot_port;
  302. ocelot_port = ocelot->ports[port];
  303. if (!ocelot_port)
  304. continue;
  305. ocelot_deinit_port(ocelot, port);
  306. ocelot_release_port(ocelot_port);
  307. }
  308. }
  309. static int mscc_ocelot_init_ports(struct platform_device *pdev,
  310. struct device_node *ports)
  311. {
  312. struct ocelot *ocelot = platform_get_drvdata(pdev);
  313. u32 devlink_ports_registered = 0;
  314. struct device_node *portnp;
  315. int port, err;
  316. u32 reg;
  317. ocelot->ports = devm_kcalloc(ocelot->dev, ocelot->num_phys_ports,
  318. sizeof(struct ocelot_port *), GFP_KERNEL);
  319. if (!ocelot->ports)
  320. return -ENOMEM;
  321. ocelot->devlink_ports = devm_kcalloc(ocelot->dev,
  322. ocelot->num_phys_ports,
  323. sizeof(*ocelot->devlink_ports),
  324. GFP_KERNEL);
  325. if (!ocelot->devlink_ports)
  326. return -ENOMEM;
  327. for_each_available_child_of_node(ports, portnp) {
  328. struct ocelot_port_private *priv;
  329. struct ocelot_port *ocelot_port;
  330. struct devlink_port *dlp;
  331. struct regmap *target;
  332. struct resource *res;
  333. char res_name[8];
  334. if (of_property_read_u32(portnp, "reg", &reg))
  335. continue;
  336. port = reg;
  337. if (port < 0 || port >= ocelot->num_phys_ports) {
  338. dev_err(ocelot->dev,
  339. "invalid port number: %d >= %d\n", port,
  340. ocelot->num_phys_ports);
  341. continue;
  342. }
  343. snprintf(res_name, sizeof(res_name), "port%d", port);
  344. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  345. res_name);
  346. target = ocelot_regmap_init(ocelot, res);
  347. if (IS_ERR(target)) {
  348. err = PTR_ERR(target);
  349. of_node_put(portnp);
  350. goto out_teardown;
  351. }
  352. err = ocelot_port_devlink_init(ocelot, port,
  353. DEVLINK_PORT_FLAVOUR_PHYSICAL);
  354. if (err) {
  355. of_node_put(portnp);
  356. goto out_teardown;
  357. }
  358. err = ocelot_probe_port(ocelot, port, target, portnp);
  359. if (err) {
  360. ocelot_port_devlink_teardown(ocelot, port);
  361. continue;
  362. }
  363. devlink_ports_registered |= BIT(port);
  364. ocelot_port = ocelot->ports[port];
  365. priv = container_of(ocelot_port, struct ocelot_port_private,
  366. port);
  367. dlp = &ocelot->devlink_ports[port];
  368. devlink_port_type_eth_set(dlp, priv->dev);
  369. }
  370. /* Initialize unused devlink ports at the end */
  371. for (port = 0; port < ocelot->num_phys_ports; port++) {
  372. if (devlink_ports_registered & BIT(port))
  373. continue;
  374. err = ocelot_port_devlink_init(ocelot, port,
  375. DEVLINK_PORT_FLAVOUR_UNUSED);
  376. if (err)
  377. goto out_teardown;
  378. devlink_ports_registered |= BIT(port);
  379. }
  380. return 0;
  381. out_teardown:
  382. /* Unregister the network interfaces */
  383. mscc_ocelot_release_ports(ocelot);
  384. /* Tear down devlink ports for the registered network interfaces */
  385. for (port = 0; port < ocelot->num_phys_ports; port++) {
  386. if (devlink_ports_registered & BIT(port))
  387. ocelot_port_devlink_teardown(ocelot, port);
  388. }
  389. return err;
  390. }
  391. static int mscc_ocelot_probe(struct platform_device *pdev)
  392. {
  393. struct device_node *np = pdev->dev.of_node;
  394. int err, irq_xtr, irq_ptp_rdy;
  395. struct device_node *ports;
  396. struct devlink *devlink;
  397. struct ocelot *ocelot;
  398. struct regmap *hsio;
  399. unsigned int i;
  400. struct {
  401. enum ocelot_target id;
  402. char *name;
  403. u8 optional:1;
  404. } io_target[] = {
  405. { SYS, "sys" },
  406. { REW, "rew" },
  407. { QSYS, "qsys" },
  408. { ANA, "ana" },
  409. { QS, "qs" },
  410. { S0, "s0" },
  411. { S1, "s1" },
  412. { S2, "s2" },
  413. { PTP, "ptp", 1 },
  414. { FDMA, "fdma", 1 },
  415. };
  416. if (!np && !pdev->dev.platform_data)
  417. return -ENODEV;
  418. devlink =
  419. devlink_alloc(&ocelot_devlink_ops, sizeof(*ocelot), &pdev->dev);
  420. if (!devlink)
  421. return -ENOMEM;
  422. ocelot = devlink_priv(devlink);
  423. ocelot->devlink = priv_to_devlink(ocelot);
  424. platform_set_drvdata(pdev, ocelot);
  425. ocelot->dev = &pdev->dev;
  426. for (i = 0; i < ARRAY_SIZE(io_target); i++) {
  427. struct regmap *target;
  428. struct resource *res;
  429. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  430. io_target[i].name);
  431. target = ocelot_regmap_init(ocelot, res);
  432. if (IS_ERR(target)) {
  433. if (io_target[i].optional) {
  434. ocelot->targets[io_target[i].id] = NULL;
  435. continue;
  436. }
  437. err = PTR_ERR(target);
  438. goto out_free_devlink;
  439. }
  440. ocelot->targets[io_target[i].id] = target;
  441. }
  442. if (ocelot->targets[FDMA])
  443. ocelot_fdma_init(pdev, ocelot);
  444. hsio = syscon_regmap_lookup_by_compatible("mscc,ocelot-hsio");
  445. if (IS_ERR(hsio)) {
  446. dev_err(&pdev->dev, "missing hsio syscon\n");
  447. err = PTR_ERR(hsio);
  448. goto out_free_devlink;
  449. }
  450. ocelot->targets[HSIO] = hsio;
  451. err = ocelot_chip_init(ocelot, &ocelot_ops);
  452. if (err)
  453. goto out_free_devlink;
  454. irq_xtr = platform_get_irq_byname(pdev, "xtr");
  455. if (irq_xtr < 0) {
  456. err = irq_xtr;
  457. goto out_free_devlink;
  458. }
  459. err = devm_request_threaded_irq(&pdev->dev, irq_xtr, NULL,
  460. ocelot_xtr_irq_handler, IRQF_ONESHOT,
  461. "frame extraction", ocelot);
  462. if (err)
  463. goto out_free_devlink;
  464. irq_ptp_rdy = platform_get_irq_byname(pdev, "ptp_rdy");
  465. if (irq_ptp_rdy > 0 && ocelot->targets[PTP]) {
  466. err = devm_request_threaded_irq(&pdev->dev, irq_ptp_rdy, NULL,
  467. ocelot_ptp_rdy_irq_handler,
  468. IRQF_ONESHOT, "ptp ready",
  469. ocelot);
  470. if (err)
  471. goto out_free_devlink;
  472. /* Both the PTP interrupt and the PTP bank are available */
  473. ocelot->ptp = 1;
  474. }
  475. ports = of_get_child_by_name(np, "ethernet-ports");
  476. if (!ports) {
  477. dev_err(ocelot->dev, "no ethernet-ports child node found\n");
  478. err = -ENODEV;
  479. goto out_free_devlink;
  480. }
  481. ocelot->num_phys_ports = of_get_child_count(ports);
  482. ocelot->num_flooding_pgids = 1;
  483. ocelot->vcap = vsc7514_vcap_props;
  484. ocelot->vcap_pol.base = VSC7514_VCAP_POLICER_BASE;
  485. ocelot->vcap_pol.max = VSC7514_VCAP_POLICER_MAX;
  486. ocelot->npi = -1;
  487. err = ocelot_init(ocelot);
  488. if (err)
  489. goto out_put_ports;
  490. err = mscc_ocelot_init_ports(pdev, ports);
  491. if (err)
  492. goto out_ocelot_devlink_unregister;
  493. if (ocelot->fdma)
  494. ocelot_fdma_start(ocelot);
  495. err = ocelot_devlink_sb_register(ocelot);
  496. if (err)
  497. goto out_ocelot_release_ports;
  498. if (ocelot->ptp) {
  499. err = ocelot_init_timestamp(ocelot, &ocelot_ptp_clock_info);
  500. if (err) {
  501. dev_err(ocelot->dev,
  502. "Timestamp initialization failed\n");
  503. ocelot->ptp = 0;
  504. }
  505. }
  506. register_netdevice_notifier(&ocelot_netdevice_nb);
  507. register_switchdev_notifier(&ocelot_switchdev_nb);
  508. register_switchdev_blocking_notifier(&ocelot_switchdev_blocking_nb);
  509. of_node_put(ports);
  510. devlink_register(devlink);
  511. dev_info(&pdev->dev, "Ocelot switch probed\n");
  512. return 0;
  513. out_ocelot_release_ports:
  514. mscc_ocelot_release_ports(ocelot);
  515. mscc_ocelot_teardown_devlink_ports(ocelot);
  516. out_ocelot_devlink_unregister:
  517. ocelot_deinit(ocelot);
  518. out_put_ports:
  519. of_node_put(ports);
  520. out_free_devlink:
  521. devlink_free(devlink);
  522. return err;
  523. }
  524. static int mscc_ocelot_remove(struct platform_device *pdev)
  525. {
  526. struct ocelot *ocelot = platform_get_drvdata(pdev);
  527. if (ocelot->fdma)
  528. ocelot_fdma_deinit(ocelot);
  529. devlink_unregister(ocelot->devlink);
  530. ocelot_deinit_timestamp(ocelot);
  531. ocelot_devlink_sb_unregister(ocelot);
  532. mscc_ocelot_release_ports(ocelot);
  533. mscc_ocelot_teardown_devlink_ports(ocelot);
  534. ocelot_deinit(ocelot);
  535. unregister_switchdev_blocking_notifier(&ocelot_switchdev_blocking_nb);
  536. unregister_switchdev_notifier(&ocelot_switchdev_nb);
  537. unregister_netdevice_notifier(&ocelot_netdevice_nb);
  538. devlink_free(ocelot->devlink);
  539. return 0;
  540. }
  541. static struct platform_driver mscc_ocelot_driver = {
  542. .probe = mscc_ocelot_probe,
  543. .remove = mscc_ocelot_remove,
  544. .driver = {
  545. .name = "ocelot-switch",
  546. .of_match_table = mscc_ocelot_match,
  547. },
  548. };
  549. module_platform_driver(mscc_ocelot_driver);
  550. MODULE_DESCRIPTION("Microsemi Ocelot switch driver");
  551. MODULE_AUTHOR("Alexandre Belloni <[email protected]>");
  552. MODULE_LICENSE("Dual MIT/GPL");