mac.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0-or-later
  2. /*
  3. * Copyright 2008 - 2015 Freescale Semiconductor Inc.
  4. */
  5. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  6. #include <linux/init.h>
  7. #include <linux/module.h>
  8. #include <linux/of_address.h>
  9. #include <linux/of_platform.h>
  10. #include <linux/of_net.h>
  11. #include <linux/of_mdio.h>
  12. #include <linux/device.h>
  13. #include <linux/phy.h>
  14. #include <linux/netdevice.h>
  15. #include <linux/phy_fixed.h>
  16. #include <linux/etherdevice.h>
  17. #include <linux/libfdt_env.h>
  18. #include "mac.h"
  19. #include "fman_mac.h"
  20. #include "fman_dtsec.h"
  21. #include "fman_tgec.h"
  22. #include "fman_memac.h"
  23. MODULE_LICENSE("Dual BSD/GPL");
  24. MODULE_DESCRIPTION("FSL FMan MAC API based driver");
  25. struct mac_priv_s {
  26. u8 cell_index;
  27. struct fman *fman;
  28. /* List of multicast addresses */
  29. struct list_head mc_addr_list;
  30. struct platform_device *eth_dev;
  31. u16 speed;
  32. };
  33. struct mac_address {
  34. u8 addr[ETH_ALEN];
  35. struct list_head list;
  36. };
  37. static void mac_exception(struct mac_device *mac_dev,
  38. enum fman_mac_exceptions ex)
  39. {
  40. if (ex == FM_MAC_EX_10G_RX_FIFO_OVFL) {
  41. /* don't flag RX FIFO after the first */
  42. mac_dev->set_exception(mac_dev->fman_mac,
  43. FM_MAC_EX_10G_RX_FIFO_OVFL, false);
  44. dev_err(mac_dev->dev, "10G MAC got RX FIFO Error = %x\n", ex);
  45. }
  46. dev_dbg(mac_dev->dev, "%s:%s() -> %d\n", KBUILD_BASENAME ".c",
  47. __func__, ex);
  48. }
  49. int fman_set_multi(struct net_device *net_dev, struct mac_device *mac_dev)
  50. {
  51. struct mac_priv_s *priv;
  52. struct mac_address *old_addr, *tmp;
  53. struct netdev_hw_addr *ha;
  54. int err;
  55. enet_addr_t *addr;
  56. priv = mac_dev->priv;
  57. /* Clear previous address list */
  58. list_for_each_entry_safe(old_addr, tmp, &priv->mc_addr_list, list) {
  59. addr = (enet_addr_t *)old_addr->addr;
  60. err = mac_dev->remove_hash_mac_addr(mac_dev->fman_mac, addr);
  61. if (err < 0)
  62. return err;
  63. list_del(&old_addr->list);
  64. kfree(old_addr);
  65. }
  66. /* Add all the addresses from the new list */
  67. netdev_for_each_mc_addr(ha, net_dev) {
  68. addr = (enet_addr_t *)ha->addr;
  69. err = mac_dev->add_hash_mac_addr(mac_dev->fman_mac, addr);
  70. if (err < 0)
  71. return err;
  72. tmp = kmalloc(sizeof(*tmp), GFP_ATOMIC);
  73. if (!tmp)
  74. return -ENOMEM;
  75. ether_addr_copy(tmp->addr, ha->addr);
  76. list_add(&tmp->list, &priv->mc_addr_list);
  77. }
  78. return 0;
  79. }
  80. /**
  81. * fman_set_mac_active_pause
  82. * @mac_dev: A pointer to the MAC device
  83. * @rx: Pause frame setting for RX
  84. * @tx: Pause frame setting for TX
  85. *
  86. * Set the MAC RX/TX PAUSE frames settings
  87. *
  88. * Avoid redundant calls to FMD, if the MAC driver already contains the desired
  89. * active PAUSE settings. Otherwise, the new active settings should be reflected
  90. * in FMan.
  91. *
  92. * Return: 0 on success; Error code otherwise.
  93. */
  94. int fman_set_mac_active_pause(struct mac_device *mac_dev, bool rx, bool tx)
  95. {
  96. struct fman_mac *fman_mac = mac_dev->fman_mac;
  97. int err = 0;
  98. if (rx != mac_dev->rx_pause_active) {
  99. err = mac_dev->set_rx_pause(fman_mac, rx);
  100. if (likely(err == 0))
  101. mac_dev->rx_pause_active = rx;
  102. }
  103. if (tx != mac_dev->tx_pause_active) {
  104. u16 pause_time = (tx ? FSL_FM_PAUSE_TIME_ENABLE :
  105. FSL_FM_PAUSE_TIME_DISABLE);
  106. err = mac_dev->set_tx_pause(fman_mac, 0, pause_time, 0);
  107. if (likely(err == 0))
  108. mac_dev->tx_pause_active = tx;
  109. }
  110. return err;
  111. }
  112. EXPORT_SYMBOL(fman_set_mac_active_pause);
  113. /**
  114. * fman_get_pause_cfg
  115. * @mac_dev: A pointer to the MAC device
  116. * @rx_pause: Return value for RX setting
  117. * @tx_pause: Return value for TX setting
  118. *
  119. * Determine the MAC RX/TX PAUSE frames settings based on PHY
  120. * autonegotiation or values set by eththool.
  121. *
  122. * Return: Pointer to FMan device.
  123. */
  124. void fman_get_pause_cfg(struct mac_device *mac_dev, bool *rx_pause,
  125. bool *tx_pause)
  126. {
  127. struct phy_device *phy_dev = mac_dev->phy_dev;
  128. u16 lcl_adv, rmt_adv;
  129. u8 flowctrl;
  130. *rx_pause = *tx_pause = false;
  131. if (!phy_dev->duplex)
  132. return;
  133. /* If PAUSE autonegotiation is disabled, the TX/RX PAUSE settings
  134. * are those set by ethtool.
  135. */
  136. if (!mac_dev->autoneg_pause) {
  137. *rx_pause = mac_dev->rx_pause_req;
  138. *tx_pause = mac_dev->tx_pause_req;
  139. return;
  140. }
  141. /* Else if PAUSE autonegotiation is enabled, the TX/RX PAUSE
  142. * settings depend on the result of the link negotiation.
  143. */
  144. /* get local capabilities */
  145. lcl_adv = linkmode_adv_to_lcl_adv_t(phy_dev->advertising);
  146. /* get link partner capabilities */
  147. rmt_adv = 0;
  148. if (phy_dev->pause)
  149. rmt_adv |= LPA_PAUSE_CAP;
  150. if (phy_dev->asym_pause)
  151. rmt_adv |= LPA_PAUSE_ASYM;
  152. /* Calculate TX/RX settings based on local and peer advertised
  153. * symmetric/asymmetric PAUSE capabilities.
  154. */
  155. flowctrl = mii_resolve_flowctrl_fdx(lcl_adv, rmt_adv);
  156. if (flowctrl & FLOW_CTRL_RX)
  157. *rx_pause = true;
  158. if (flowctrl & FLOW_CTRL_TX)
  159. *tx_pause = true;
  160. }
  161. EXPORT_SYMBOL(fman_get_pause_cfg);
  162. #define DTSEC_SUPPORTED \
  163. (SUPPORTED_10baseT_Half \
  164. | SUPPORTED_10baseT_Full \
  165. | SUPPORTED_100baseT_Half \
  166. | SUPPORTED_100baseT_Full \
  167. | SUPPORTED_Autoneg \
  168. | SUPPORTED_Pause \
  169. | SUPPORTED_Asym_Pause \
  170. | SUPPORTED_FIBRE \
  171. | SUPPORTED_MII)
  172. static DEFINE_MUTEX(eth_lock);
  173. static const u16 phy2speed[] = {
  174. [PHY_INTERFACE_MODE_MII] = SPEED_100,
  175. [PHY_INTERFACE_MODE_GMII] = SPEED_1000,
  176. [PHY_INTERFACE_MODE_SGMII] = SPEED_1000,
  177. [PHY_INTERFACE_MODE_TBI] = SPEED_1000,
  178. [PHY_INTERFACE_MODE_RMII] = SPEED_100,
  179. [PHY_INTERFACE_MODE_RGMII] = SPEED_1000,
  180. [PHY_INTERFACE_MODE_RGMII_ID] = SPEED_1000,
  181. [PHY_INTERFACE_MODE_RGMII_RXID] = SPEED_1000,
  182. [PHY_INTERFACE_MODE_RGMII_TXID] = SPEED_1000,
  183. [PHY_INTERFACE_MODE_RTBI] = SPEED_1000,
  184. [PHY_INTERFACE_MODE_QSGMII] = SPEED_1000,
  185. [PHY_INTERFACE_MODE_XGMII] = SPEED_10000
  186. };
  187. static struct platform_device *dpaa_eth_add_device(int fman_id,
  188. struct mac_device *mac_dev)
  189. {
  190. struct platform_device *pdev;
  191. struct dpaa_eth_data data;
  192. struct mac_priv_s *priv;
  193. static int dpaa_eth_dev_cnt;
  194. int ret;
  195. priv = mac_dev->priv;
  196. data.mac_dev = mac_dev;
  197. data.mac_hw_id = priv->cell_index;
  198. data.fman_hw_id = fman_id;
  199. mutex_lock(&eth_lock);
  200. pdev = platform_device_alloc("dpaa-ethernet", dpaa_eth_dev_cnt);
  201. if (!pdev) {
  202. ret = -ENOMEM;
  203. goto no_mem;
  204. }
  205. pdev->dev.parent = mac_dev->dev;
  206. ret = platform_device_add_data(pdev, &data, sizeof(data));
  207. if (ret)
  208. goto err;
  209. ret = platform_device_add(pdev);
  210. if (ret)
  211. goto err;
  212. dpaa_eth_dev_cnt++;
  213. mutex_unlock(&eth_lock);
  214. return pdev;
  215. err:
  216. platform_device_put(pdev);
  217. no_mem:
  218. mutex_unlock(&eth_lock);
  219. return ERR_PTR(ret);
  220. }
  221. static const struct of_device_id mac_match[] = {
  222. { .compatible = "fsl,fman-dtsec", .data = dtsec_initialization },
  223. { .compatible = "fsl,fman-xgec", .data = tgec_initialization },
  224. { .compatible = "fsl,fman-memac", .data = memac_initialization },
  225. {}
  226. };
  227. MODULE_DEVICE_TABLE(of, mac_match);
  228. static int mac_probe(struct platform_device *_of_dev)
  229. {
  230. int err, i, nph;
  231. int (*init)(struct mac_device *mac_dev, struct device_node *mac_node,
  232. struct fman_mac_params *params);
  233. struct device *dev;
  234. struct device_node *mac_node, *dev_node;
  235. struct mac_device *mac_dev;
  236. struct platform_device *of_dev;
  237. struct mac_priv_s *priv;
  238. struct fman_mac_params params;
  239. u32 val;
  240. u8 fman_id;
  241. phy_interface_t phy_if;
  242. dev = &_of_dev->dev;
  243. mac_node = dev->of_node;
  244. init = of_device_get_match_data(dev);
  245. mac_dev = devm_kzalloc(dev, sizeof(*mac_dev), GFP_KERNEL);
  246. if (!mac_dev)
  247. return -ENOMEM;
  248. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  249. if (!priv)
  250. return -ENOMEM;
  251. /* Save private information */
  252. mac_dev->priv = priv;
  253. mac_dev->dev = dev;
  254. INIT_LIST_HEAD(&priv->mc_addr_list);
  255. /* Get the FM node */
  256. dev_node = of_get_parent(mac_node);
  257. if (!dev_node) {
  258. dev_err(dev, "of_get_parent(%pOF) failed\n",
  259. mac_node);
  260. return -EINVAL;
  261. }
  262. of_dev = of_find_device_by_node(dev_node);
  263. if (!of_dev) {
  264. dev_err(dev, "of_find_device_by_node(%pOF) failed\n", dev_node);
  265. err = -EINVAL;
  266. goto _return_of_node_put;
  267. }
  268. /* Get the FMan cell-index */
  269. err = of_property_read_u32(dev_node, "cell-index", &val);
  270. if (err) {
  271. dev_err(dev, "failed to read cell-index for %pOF\n", dev_node);
  272. err = -EINVAL;
  273. goto _return_of_node_put;
  274. }
  275. /* cell-index 0 => FMan id 1 */
  276. fman_id = (u8)(val + 1);
  277. priv->fman = fman_bind(&of_dev->dev);
  278. if (!priv->fman) {
  279. dev_err(dev, "fman_bind(%pOF) failed\n", dev_node);
  280. err = -ENODEV;
  281. goto _return_of_node_put;
  282. }
  283. of_node_put(dev_node);
  284. /* Get the address of the memory mapped registers */
  285. mac_dev->res = platform_get_mem_or_io(_of_dev, 0);
  286. if (!mac_dev->res) {
  287. dev_err(dev, "could not get registers\n");
  288. return -EINVAL;
  289. }
  290. err = devm_request_resource(dev, fman_get_mem_region(priv->fman),
  291. mac_dev->res);
  292. if (err) {
  293. dev_err_probe(dev, err, "could not request resource\n");
  294. return err;
  295. }
  296. mac_dev->vaddr = devm_ioremap(dev, mac_dev->res->start,
  297. resource_size(mac_dev->res));
  298. if (!mac_dev->vaddr) {
  299. dev_err(dev, "devm_ioremap() failed\n");
  300. return -EIO;
  301. }
  302. if (!of_device_is_available(mac_node))
  303. return -ENODEV;
  304. /* Get the cell-index */
  305. err = of_property_read_u32(mac_node, "cell-index", &val);
  306. if (err) {
  307. dev_err(dev, "failed to read cell-index for %pOF\n", mac_node);
  308. return -EINVAL;
  309. }
  310. priv->cell_index = (u8)val;
  311. /* Get the MAC address */
  312. err = of_get_mac_address(mac_node, mac_dev->addr);
  313. if (err)
  314. dev_warn(dev, "of_get_mac_address(%pOF) failed\n", mac_node);
  315. /* Get the port handles */
  316. nph = of_count_phandle_with_args(mac_node, "fsl,fman-ports", NULL);
  317. if (unlikely(nph < 0)) {
  318. dev_err(dev, "of_count_phandle_with_args(%pOF, fsl,fman-ports) failed\n",
  319. mac_node);
  320. return nph;
  321. }
  322. if (nph != ARRAY_SIZE(mac_dev->port)) {
  323. dev_err(dev, "Not supported number of fman-ports handles of mac node %pOF from device tree\n",
  324. mac_node);
  325. return -EINVAL;
  326. }
  327. for (i = 0; i < ARRAY_SIZE(mac_dev->port); i++) {
  328. /* Find the port node */
  329. dev_node = of_parse_phandle(mac_node, "fsl,fman-ports", i);
  330. if (!dev_node) {
  331. dev_err(dev, "of_parse_phandle(%pOF, fsl,fman-ports) failed\n",
  332. mac_node);
  333. return -EINVAL;
  334. }
  335. of_dev = of_find_device_by_node(dev_node);
  336. if (!of_dev) {
  337. dev_err(dev, "of_find_device_by_node(%pOF) failed\n",
  338. dev_node);
  339. err = -EINVAL;
  340. goto _return_of_node_put;
  341. }
  342. mac_dev->port[i] = fman_port_bind(&of_dev->dev);
  343. if (!mac_dev->port[i]) {
  344. dev_err(dev, "dev_get_drvdata(%pOF) failed\n",
  345. dev_node);
  346. err = -EINVAL;
  347. goto _return_of_node_put;
  348. }
  349. of_node_put(dev_node);
  350. }
  351. /* Get the PHY connection type */
  352. err = of_get_phy_mode(mac_node, &phy_if);
  353. if (err) {
  354. dev_warn(dev,
  355. "of_get_phy_mode() for %pOF failed. Defaulting to SGMII\n",
  356. mac_node);
  357. phy_if = PHY_INTERFACE_MODE_SGMII;
  358. }
  359. mac_dev->phy_if = phy_if;
  360. priv->speed = phy2speed[mac_dev->phy_if];
  361. params.max_speed = priv->speed;
  362. mac_dev->if_support = DTSEC_SUPPORTED;
  363. /* We don't support half-duplex in SGMII mode */
  364. if (mac_dev->phy_if == PHY_INTERFACE_MODE_SGMII)
  365. mac_dev->if_support &= ~(SUPPORTED_10baseT_Half |
  366. SUPPORTED_100baseT_Half);
  367. /* Gigabit support (no half-duplex) */
  368. if (params.max_speed == 1000)
  369. mac_dev->if_support |= SUPPORTED_1000baseT_Full;
  370. /* The 10G interface only supports one mode */
  371. if (mac_dev->phy_if == PHY_INTERFACE_MODE_XGMII)
  372. mac_dev->if_support = SUPPORTED_10000baseT_Full;
  373. /* Get the rest of the PHY information */
  374. mac_dev->phy_node = of_parse_phandle(mac_node, "phy-handle", 0);
  375. params.basex_if = false;
  376. params.mac_id = priv->cell_index;
  377. params.fm = (void *)priv->fman;
  378. params.exception_cb = mac_exception;
  379. params.event_cb = mac_exception;
  380. err = init(mac_dev, mac_node, &params);
  381. if (err < 0) {
  382. dev_err(dev, "mac_dev->init() = %d\n", err);
  383. of_node_put(mac_dev->phy_node);
  384. return err;
  385. }
  386. /* pause frame autonegotiation enabled */
  387. mac_dev->autoneg_pause = true;
  388. /* By intializing the values to false, force FMD to enable PAUSE frames
  389. * on RX and TX
  390. */
  391. mac_dev->rx_pause_req = true;
  392. mac_dev->tx_pause_req = true;
  393. mac_dev->rx_pause_active = false;
  394. mac_dev->tx_pause_active = false;
  395. err = fman_set_mac_active_pause(mac_dev, true, true);
  396. if (err < 0)
  397. dev_err(dev, "fman_set_mac_active_pause() = %d\n", err);
  398. if (!is_zero_ether_addr(mac_dev->addr))
  399. dev_info(dev, "FMan MAC address: %pM\n", mac_dev->addr);
  400. priv->eth_dev = dpaa_eth_add_device(fman_id, mac_dev);
  401. if (IS_ERR(priv->eth_dev)) {
  402. dev_err(dev, "failed to add Ethernet platform device for MAC %d\n",
  403. priv->cell_index);
  404. priv->eth_dev = NULL;
  405. }
  406. return err;
  407. _return_of_node_put:
  408. of_node_put(dev_node);
  409. return err;
  410. }
  411. static int mac_remove(struct platform_device *pdev)
  412. {
  413. struct mac_device *mac_dev = platform_get_drvdata(pdev);
  414. platform_device_unregister(mac_dev->priv->eth_dev);
  415. return 0;
  416. }
  417. static struct platform_driver mac_driver = {
  418. .driver = {
  419. .name = KBUILD_MODNAME,
  420. .of_match_table = mac_match,
  421. },
  422. .probe = mac_probe,
  423. .remove = mac_remove,
  424. };
  425. builtin_platform_driver(mac_driver);