ocelot_board.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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/interrupt.h>
  8. #include <linux/module.h>
  9. #include <linux/netdevice.h>
  10. #include <linux/of_mdio.h>
  11. #include <linux/of_platform.h>
  12. #include <linux/skbuff.h>
  13. #include "ocelot.h"
  14. static int ocelot_parse_ifh(u32 *ifh, struct frame_info *info)
  15. {
  16. int i;
  17. u8 llen, wlen;
  18. /* The IFH is in network order, switch to CPU order */
  19. for (i = 0; i < IFH_LEN; i++)
  20. ifh[i] = ntohl((__force __be32)ifh[i]);
  21. wlen = (ifh[1] >> 7) & 0xff;
  22. llen = (ifh[1] >> 15) & 0x3f;
  23. info->len = OCELOT_BUFFER_CELL_SZ * wlen + llen - 80;
  24. info->port = (ifh[2] & GENMASK(14, 11)) >> 11;
  25. info->cpuq = (ifh[3] & GENMASK(27, 20)) >> 20;
  26. info->tag_type = (ifh[3] & BIT(16)) >> 16;
  27. info->vid = ifh[3] & GENMASK(11, 0);
  28. return 0;
  29. }
  30. static int ocelot_rx_frame_word(struct ocelot *ocelot, u8 grp, bool ifh,
  31. u32 *rval)
  32. {
  33. u32 val;
  34. u32 bytes_valid;
  35. val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
  36. if (val == XTR_NOT_READY) {
  37. if (ifh)
  38. return -EIO;
  39. do {
  40. val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
  41. } while (val == XTR_NOT_READY);
  42. }
  43. switch (val) {
  44. case XTR_ABORT:
  45. return -EIO;
  46. case XTR_EOF_0:
  47. case XTR_EOF_1:
  48. case XTR_EOF_2:
  49. case XTR_EOF_3:
  50. case XTR_PRUNED:
  51. bytes_valid = XTR_VALID_BYTES(val);
  52. val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
  53. if (val == XTR_ESCAPE)
  54. *rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
  55. else
  56. *rval = val;
  57. return bytes_valid;
  58. case XTR_ESCAPE:
  59. *rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
  60. return 4;
  61. default:
  62. *rval = val;
  63. return 4;
  64. }
  65. }
  66. static irqreturn_t ocelot_xtr_irq_handler(int irq, void *arg)
  67. {
  68. struct ocelot *ocelot = arg;
  69. int i = 0, grp = 0;
  70. int err = 0;
  71. if (!(ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp)))
  72. return IRQ_NONE;
  73. do {
  74. struct sk_buff *skb;
  75. struct net_device *dev;
  76. u32 *buf;
  77. int sz, len, buf_len;
  78. u32 ifh[4];
  79. u32 val;
  80. struct frame_info info;
  81. for (i = 0; i < IFH_LEN; i++) {
  82. err = ocelot_rx_frame_word(ocelot, grp, true, &ifh[i]);
  83. if (err != 4)
  84. break;
  85. }
  86. if (err != 4)
  87. break;
  88. /* At this point the IFH was read correctly, so it is safe to
  89. * presume that there is no error. The err needs to be reset
  90. * otherwise a frame could come in CPU queue between the while
  91. * condition and the check for error later on. And in that case
  92. * the new frame is just removed and not processed.
  93. */
  94. err = 0;
  95. ocelot_parse_ifh(ifh, &info);
  96. dev = ocelot->ports[info.port]->dev;
  97. skb = netdev_alloc_skb(dev, info.len);
  98. if (unlikely(!skb)) {
  99. netdev_err(dev, "Unable to allocate sk_buff\n");
  100. err = -ENOMEM;
  101. break;
  102. }
  103. buf_len = info.len - ETH_FCS_LEN;
  104. buf = (u32 *)skb_put(skb, buf_len);
  105. len = 0;
  106. do {
  107. sz = ocelot_rx_frame_word(ocelot, grp, false, &val);
  108. *buf++ = val;
  109. len += sz;
  110. } while (len < buf_len);
  111. /* Read the FCS and discard it */
  112. sz = ocelot_rx_frame_word(ocelot, grp, false, &val);
  113. /* Update the statistics if part of the FCS was read before */
  114. len -= ETH_FCS_LEN - sz;
  115. if (sz < 0) {
  116. err = sz;
  117. break;
  118. }
  119. /* Everything we see on an interface that is in the HW bridge
  120. * has already been forwarded.
  121. */
  122. if (ocelot->bridge_mask & BIT(info.port))
  123. skb->offload_fwd_mark = 1;
  124. skb->protocol = eth_type_trans(skb, dev);
  125. netif_rx(skb);
  126. dev->stats.rx_bytes += len;
  127. dev->stats.rx_packets++;
  128. } while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp));
  129. if (err)
  130. while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp))
  131. ocelot_read_rix(ocelot, QS_XTR_RD, grp);
  132. return IRQ_HANDLED;
  133. }
  134. static const struct of_device_id mscc_ocelot_match[] = {
  135. { .compatible = "mscc,vsc7514-switch" },
  136. { }
  137. };
  138. MODULE_DEVICE_TABLE(of, mscc_ocelot_match);
  139. static int mscc_ocelot_probe(struct platform_device *pdev)
  140. {
  141. int err, irq;
  142. unsigned int i;
  143. struct device_node *np = pdev->dev.of_node;
  144. struct device_node *ports, *portnp;
  145. struct ocelot *ocelot;
  146. u32 val;
  147. struct {
  148. enum ocelot_target id;
  149. char *name;
  150. } res[] = {
  151. { SYS, "sys" },
  152. { REW, "rew" },
  153. { QSYS, "qsys" },
  154. { ANA, "ana" },
  155. { QS, "qs" },
  156. { HSIO, "hsio" },
  157. };
  158. if (!np && !pdev->dev.platform_data)
  159. return -ENODEV;
  160. ocelot = devm_kzalloc(&pdev->dev, sizeof(*ocelot), GFP_KERNEL);
  161. if (!ocelot)
  162. return -ENOMEM;
  163. platform_set_drvdata(pdev, ocelot);
  164. ocelot->dev = &pdev->dev;
  165. for (i = 0; i < ARRAY_SIZE(res); i++) {
  166. struct regmap *target;
  167. target = ocelot_io_platform_init(ocelot, pdev, res[i].name);
  168. if (IS_ERR(target))
  169. return PTR_ERR(target);
  170. ocelot->targets[res[i].id] = target;
  171. }
  172. err = ocelot_chip_init(ocelot);
  173. if (err)
  174. return err;
  175. irq = platform_get_irq_byname(pdev, "xtr");
  176. if (irq < 0)
  177. return -ENODEV;
  178. err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
  179. ocelot_xtr_irq_handler, IRQF_ONESHOT,
  180. "frame extraction", ocelot);
  181. if (err)
  182. return err;
  183. regmap_field_write(ocelot->regfields[SYS_RESET_CFG_MEM_INIT], 1);
  184. regmap_field_write(ocelot->regfields[SYS_RESET_CFG_MEM_ENA], 1);
  185. do {
  186. msleep(1);
  187. regmap_field_read(ocelot->regfields[SYS_RESET_CFG_MEM_INIT],
  188. &val);
  189. } while (val);
  190. regmap_field_write(ocelot->regfields[SYS_RESET_CFG_MEM_ENA], 1);
  191. regmap_field_write(ocelot->regfields[SYS_RESET_CFG_CORE_ENA], 1);
  192. ocelot->num_cpu_ports = 1; /* 1 port on the switch, two groups */
  193. ports = of_get_child_by_name(np, "ethernet-ports");
  194. if (!ports) {
  195. dev_err(&pdev->dev, "no ethernet-ports child node found\n");
  196. return -ENODEV;
  197. }
  198. ocelot->num_phys_ports = of_get_child_count(ports);
  199. ocelot->ports = devm_kcalloc(&pdev->dev, ocelot->num_phys_ports,
  200. sizeof(struct ocelot_port *), GFP_KERNEL);
  201. INIT_LIST_HEAD(&ocelot->multicast);
  202. ocelot_init(ocelot);
  203. ocelot_rmw(ocelot, HSIO_HW_CFG_DEV1G_4_MODE |
  204. HSIO_HW_CFG_DEV1G_6_MODE |
  205. HSIO_HW_CFG_DEV1G_9_MODE,
  206. HSIO_HW_CFG_DEV1G_4_MODE |
  207. HSIO_HW_CFG_DEV1G_6_MODE |
  208. HSIO_HW_CFG_DEV1G_9_MODE,
  209. HSIO_HW_CFG);
  210. for_each_available_child_of_node(ports, portnp) {
  211. struct device_node *phy_node;
  212. struct phy_device *phy;
  213. struct resource *res;
  214. void __iomem *regs;
  215. char res_name[8];
  216. u32 port;
  217. if (of_property_read_u32(portnp, "reg", &port))
  218. continue;
  219. snprintf(res_name, sizeof(res_name), "port%d", port);
  220. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  221. res_name);
  222. regs = devm_ioremap_resource(&pdev->dev, res);
  223. if (IS_ERR(regs))
  224. continue;
  225. phy_node = of_parse_phandle(portnp, "phy-handle", 0);
  226. if (!phy_node)
  227. continue;
  228. phy = of_phy_find_device(phy_node);
  229. if (!phy)
  230. continue;
  231. err = ocelot_probe_port(ocelot, port, regs, phy);
  232. if (err) {
  233. dev_err(&pdev->dev, "failed to probe ports\n");
  234. goto err_probe_ports;
  235. }
  236. }
  237. register_netdevice_notifier(&ocelot_netdevice_nb);
  238. dev_info(&pdev->dev, "Ocelot switch probed\n");
  239. return 0;
  240. err_probe_ports:
  241. return err;
  242. }
  243. static int mscc_ocelot_remove(struct platform_device *pdev)
  244. {
  245. struct ocelot *ocelot = platform_get_drvdata(pdev);
  246. ocelot_deinit(ocelot);
  247. unregister_netdevice_notifier(&ocelot_netdevice_nb);
  248. return 0;
  249. }
  250. static struct platform_driver mscc_ocelot_driver = {
  251. .probe = mscc_ocelot_probe,
  252. .remove = mscc_ocelot_remove,
  253. .driver = {
  254. .name = "ocelot-switch",
  255. .of_match_table = mscc_ocelot_match,
  256. },
  257. };
  258. module_platform_driver(mscc_ocelot_driver);
  259. MODULE_DESCRIPTION("Microsemi Ocelot switch driver");
  260. MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@bootlin.com>");
  261. MODULE_LICENSE("Dual MIT/GPL");