am65-cpsw-nuss.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Texas Instruments K3 AM65 Ethernet Switch SubSystem Driver
  4. *
  5. * Copyright (C) 2019, Texas Instruments, Incorporated
  6. *
  7. */
  8. #include <common.h>
  9. #include <malloc.h>
  10. #include <asm/cache.h>
  11. #include <asm/io.h>
  12. #include <asm/processor.h>
  13. #include <clk.h>
  14. #include <dm.h>
  15. #include <dm/device_compat.h>
  16. #include <dm/lists.h>
  17. #include <dm/pinctrl.h>
  18. #include <dma-uclass.h>
  19. #include <dm/of_access.h>
  20. #include <miiphy.h>
  21. #include <net.h>
  22. #include <phy.h>
  23. #include <power-domain.h>
  24. #include <regmap.h>
  25. #include <soc.h>
  26. #include <syscon.h>
  27. #include <linux/bitops.h>
  28. #include <linux/soc/ti/ti-udma.h>
  29. #include "cpsw_mdio.h"
  30. #define AM65_CPSW_CPSWNU_MAX_PORTS 9
  31. #define AM65_CPSW_SS_BASE 0x0
  32. #define AM65_CPSW_SGMII_BASE 0x100
  33. #define AM65_CPSW_MDIO_BASE 0xf00
  34. #define AM65_CPSW_XGMII_BASE 0x2100
  35. #define AM65_CPSW_CPSW_NU_BASE 0x20000
  36. #define AM65_CPSW_CPSW_NU_ALE_BASE 0x1e000
  37. #define AM65_CPSW_CPSW_NU_PORTS_OFFSET 0x1000
  38. #define AM65_CPSW_CPSW_NU_PORT_MACSL_OFFSET 0x330
  39. #define AM65_CPSW_MDIO_BUS_FREQ_DEF 1000000
  40. #define AM65_CPSW_CTL_REG 0x4
  41. #define AM65_CPSW_STAT_PORT_EN_REG 0x14
  42. #define AM65_CPSW_PTYPE_REG 0x18
  43. #define AM65_CPSW_CTL_REG_P0_ENABLE BIT(2)
  44. #define AM65_CPSW_CTL_REG_P0_TX_CRC_REMOVE BIT(13)
  45. #define AM65_CPSW_CTL_REG_P0_RX_PAD BIT(14)
  46. #define AM65_CPSW_P0_FLOW_ID_REG 0x8
  47. #define AM65_CPSW_PN_RX_MAXLEN_REG 0x24
  48. #define AM65_CPSW_PN_REG_SA_L 0x308
  49. #define AM65_CPSW_PN_REG_SA_H 0x30c
  50. #define AM65_CPSW_ALE_CTL_REG 0x8
  51. #define AM65_CPSW_ALE_CTL_REG_ENABLE BIT(31)
  52. #define AM65_CPSW_ALE_CTL_REG_RESET_TBL BIT(30)
  53. #define AM65_CPSW_ALE_CTL_REG_BYPASS BIT(4)
  54. #define AM65_CPSW_ALE_PN_CTL_REG(x) (0x40 + (x) * 4)
  55. #define AM65_CPSW_ALE_PN_CTL_REG_MODE_FORWARD 0x3
  56. #define AM65_CPSW_ALE_PN_CTL_REG_MAC_ONLY BIT(11)
  57. #define AM65_CPSW_ALE_THREADMAPDEF_REG 0x134
  58. #define AM65_CPSW_ALE_DEFTHREAD_EN BIT(15)
  59. #define AM65_CPSW_MACSL_CTL_REG 0x0
  60. #define AM65_CPSW_MACSL_CTL_REG_IFCTL_A BIT(15)
  61. #define AM65_CPSW_MACSL_CTL_EXT_EN BIT(18)
  62. #define AM65_CPSW_MACSL_CTL_REG_GIG BIT(7)
  63. #define AM65_CPSW_MACSL_CTL_REG_GMII_EN BIT(5)
  64. #define AM65_CPSW_MACSL_CTL_REG_LOOPBACK BIT(1)
  65. #define AM65_CPSW_MACSL_CTL_REG_FULL_DUPLEX BIT(0)
  66. #define AM65_CPSW_MACSL_RESET_REG 0x8
  67. #define AM65_CPSW_MACSL_RESET_REG_RESET BIT(0)
  68. #define AM65_CPSW_MACSL_STATUS_REG 0x4
  69. #define AM65_CPSW_MACSL_RESET_REG_PN_IDLE BIT(31)
  70. #define AM65_CPSW_MACSL_RESET_REG_PN_E_IDLE BIT(30)
  71. #define AM65_CPSW_MACSL_RESET_REG_PN_P_IDLE BIT(29)
  72. #define AM65_CPSW_MACSL_RESET_REG_PN_TX_IDLE BIT(28)
  73. #define AM65_CPSW_MACSL_RESET_REG_IDLE_MASK \
  74. (AM65_CPSW_MACSL_RESET_REG_PN_IDLE | \
  75. AM65_CPSW_MACSL_RESET_REG_PN_E_IDLE | \
  76. AM65_CPSW_MACSL_RESET_REG_PN_P_IDLE | \
  77. AM65_CPSW_MACSL_RESET_REG_PN_TX_IDLE)
  78. #define AM65_CPSW_CPPI_PKT_TYPE 0x7
  79. struct am65_cpsw_port {
  80. fdt_addr_t port_base;
  81. fdt_addr_t macsl_base;
  82. bool disabled;
  83. u32 mac_control;
  84. };
  85. struct am65_cpsw_common {
  86. struct udevice *dev;
  87. fdt_addr_t ss_base;
  88. fdt_addr_t cpsw_base;
  89. fdt_addr_t mdio_base;
  90. fdt_addr_t ale_base;
  91. struct clk fclk;
  92. struct power_domain pwrdmn;
  93. u32 port_num;
  94. struct am65_cpsw_port ports[AM65_CPSW_CPSWNU_MAX_PORTS];
  95. struct mii_dev *bus;
  96. u32 bus_freq;
  97. struct dma dma_tx;
  98. struct dma dma_rx;
  99. u32 rx_next;
  100. u32 rx_pend;
  101. bool started;
  102. };
  103. struct am65_cpsw_priv {
  104. struct udevice *dev;
  105. struct am65_cpsw_common *cpsw_common;
  106. u32 port_id;
  107. struct phy_device *phydev;
  108. bool has_phy;
  109. ofnode phy_node;
  110. u32 phy_addr;
  111. bool mdio_manual_mode;
  112. };
  113. #ifdef PKTSIZE_ALIGN
  114. #define UDMA_RX_BUF_SIZE PKTSIZE_ALIGN
  115. #else
  116. #define UDMA_RX_BUF_SIZE ALIGN(1522, ARCH_DMA_MINALIGN)
  117. #endif
  118. #ifdef PKTBUFSRX
  119. #define UDMA_RX_DESC_NUM PKTBUFSRX
  120. #else
  121. #define UDMA_RX_DESC_NUM 4
  122. #endif
  123. #define mac_hi(mac) (((mac)[0] << 0) | ((mac)[1] << 8) | \
  124. ((mac)[2] << 16) | ((mac)[3] << 24))
  125. #define mac_lo(mac) (((mac)[4] << 0) | ((mac)[5] << 8))
  126. static void am65_cpsw_set_sl_mac(struct am65_cpsw_port *slave,
  127. unsigned char *addr)
  128. {
  129. writel(mac_hi(addr),
  130. slave->port_base + AM65_CPSW_PN_REG_SA_H);
  131. writel(mac_lo(addr),
  132. slave->port_base + AM65_CPSW_PN_REG_SA_L);
  133. }
  134. int am65_cpsw_macsl_reset(struct am65_cpsw_port *slave)
  135. {
  136. u32 i = 100;
  137. /* Set the soft reset bit */
  138. writel(AM65_CPSW_MACSL_RESET_REG_RESET,
  139. slave->macsl_base + AM65_CPSW_MACSL_RESET_REG);
  140. while ((readl(slave->macsl_base + AM65_CPSW_MACSL_RESET_REG) &
  141. AM65_CPSW_MACSL_RESET_REG_RESET) && i--)
  142. cpu_relax();
  143. /* Timeout on the reset */
  144. return i;
  145. }
  146. static int am65_cpsw_macsl_wait_for_idle(struct am65_cpsw_port *slave)
  147. {
  148. u32 i = 100;
  149. while ((readl(slave->macsl_base + AM65_CPSW_MACSL_STATUS_REG) &
  150. AM65_CPSW_MACSL_RESET_REG_IDLE_MASK) && i--)
  151. cpu_relax();
  152. return i;
  153. }
  154. static int am65_cpsw_update_link(struct am65_cpsw_priv *priv)
  155. {
  156. struct am65_cpsw_common *common = priv->cpsw_common;
  157. struct am65_cpsw_port *port = &common->ports[priv->port_id];
  158. struct phy_device *phy = priv->phydev;
  159. u32 mac_control = 0;
  160. if (phy->link) { /* link up */
  161. mac_control = /*AM65_CPSW_MACSL_CTL_REG_LOOPBACK |*/
  162. AM65_CPSW_MACSL_CTL_REG_GMII_EN;
  163. if (phy->speed == 1000)
  164. mac_control |= AM65_CPSW_MACSL_CTL_REG_GIG;
  165. if (phy->speed == 10 && phy_interface_is_rgmii(phy))
  166. /* Can be used with in band mode only */
  167. mac_control |= AM65_CPSW_MACSL_CTL_EXT_EN;
  168. if (phy->duplex == DUPLEX_FULL)
  169. mac_control |= AM65_CPSW_MACSL_CTL_REG_FULL_DUPLEX;
  170. if (phy->speed == 100)
  171. mac_control |= AM65_CPSW_MACSL_CTL_REG_IFCTL_A;
  172. }
  173. if (mac_control == port->mac_control)
  174. goto out;
  175. if (mac_control) {
  176. printf("link up on port %d, speed %d, %s duplex\n",
  177. priv->port_id, phy->speed,
  178. (phy->duplex == DUPLEX_FULL) ? "full" : "half");
  179. } else {
  180. printf("link down on port %d\n", priv->port_id);
  181. }
  182. writel(mac_control, port->macsl_base + AM65_CPSW_MACSL_CTL_REG);
  183. port->mac_control = mac_control;
  184. out:
  185. return phy->link;
  186. }
  187. #define AM65_GMII_SEL_PORT_OFFS(x) (0x4 * ((x) - 1))
  188. #define AM65_GMII_SEL_MODE_MII 0
  189. #define AM65_GMII_SEL_MODE_RMII 1
  190. #define AM65_GMII_SEL_MODE_RGMII 2
  191. #define AM65_GMII_SEL_RGMII_IDMODE BIT(4)
  192. static int am65_cpsw_gmii_sel_k3(struct am65_cpsw_priv *priv,
  193. phy_interface_t phy_mode)
  194. {
  195. struct udevice *dev = priv->dev;
  196. u32 offset, reg, phandle;
  197. bool rgmii_id = false;
  198. fdt_addr_t gmii_sel;
  199. u32 mode = 0;
  200. ofnode node;
  201. int ret;
  202. ret = ofnode_read_u32(dev_ofnode(dev), "phys", &phandle);
  203. if (ret)
  204. return ret;
  205. ret = ofnode_read_u32_index(dev_ofnode(dev), "phys", 1, &offset);
  206. if (ret)
  207. return ret;
  208. node = ofnode_get_by_phandle(phandle);
  209. if (!ofnode_valid(node))
  210. return -ENODEV;
  211. gmii_sel = ofnode_get_addr(node);
  212. if (gmii_sel == FDT_ADDR_T_NONE)
  213. return -ENODEV;
  214. gmii_sel += AM65_GMII_SEL_PORT_OFFS(offset);
  215. reg = readl(gmii_sel);
  216. dev_dbg(dev, "old gmii_sel: %08x\n", reg);
  217. switch (phy_mode) {
  218. case PHY_INTERFACE_MODE_RMII:
  219. mode = AM65_GMII_SEL_MODE_RMII;
  220. break;
  221. case PHY_INTERFACE_MODE_RGMII:
  222. case PHY_INTERFACE_MODE_RGMII_RXID:
  223. mode = AM65_GMII_SEL_MODE_RGMII;
  224. break;
  225. case PHY_INTERFACE_MODE_RGMII_ID:
  226. case PHY_INTERFACE_MODE_RGMII_TXID:
  227. mode = AM65_GMII_SEL_MODE_RGMII;
  228. rgmii_id = true;
  229. break;
  230. default:
  231. dev_warn(dev,
  232. "Unsupported PHY mode: %u. Defaulting to MII.\n",
  233. phy_mode);
  234. /* fallthrough */
  235. case PHY_INTERFACE_MODE_MII:
  236. mode = AM65_GMII_SEL_MODE_MII;
  237. break;
  238. };
  239. if (rgmii_id)
  240. mode |= AM65_GMII_SEL_RGMII_IDMODE;
  241. reg = mode;
  242. dev_dbg(dev, "gmii_sel PHY mode: %u, new gmii_sel: %08x\n",
  243. phy_mode, reg);
  244. writel(reg, gmii_sel);
  245. reg = readl(gmii_sel);
  246. if (reg != mode) {
  247. dev_err(dev,
  248. "gmii_sel PHY mode NOT SET!: requested: %08x, gmii_sel: %08x\n",
  249. mode, reg);
  250. return 0;
  251. }
  252. return 0;
  253. }
  254. static int am65_cpsw_start(struct udevice *dev)
  255. {
  256. struct eth_pdata *pdata = dev_get_plat(dev);
  257. struct am65_cpsw_priv *priv = dev_get_priv(dev);
  258. struct am65_cpsw_common *common = priv->cpsw_common;
  259. struct am65_cpsw_port *port = &common->ports[priv->port_id];
  260. struct am65_cpsw_port *port0 = &common->ports[0];
  261. struct ti_udma_drv_chan_cfg_data *dma_rx_cfg_data;
  262. int ret, i;
  263. ret = power_domain_on(&common->pwrdmn);
  264. if (ret) {
  265. dev_err(dev, "power_domain_on() failed %d\n", ret);
  266. goto out;
  267. }
  268. ret = clk_enable(&common->fclk);
  269. if (ret) {
  270. dev_err(dev, "clk enabled failed %d\n", ret);
  271. goto err_off_pwrdm;
  272. }
  273. common->rx_next = 0;
  274. common->rx_pend = 0;
  275. ret = dma_get_by_name(common->dev, "tx0", &common->dma_tx);
  276. if (ret) {
  277. dev_err(dev, "TX dma get failed %d\n", ret);
  278. goto err_off_clk;
  279. }
  280. ret = dma_get_by_name(common->dev, "rx", &common->dma_rx);
  281. if (ret) {
  282. dev_err(dev, "RX dma get failed %d\n", ret);
  283. goto err_free_tx;
  284. }
  285. for (i = 0; i < UDMA_RX_DESC_NUM; i++) {
  286. ret = dma_prepare_rcv_buf(&common->dma_rx,
  287. net_rx_packets[i],
  288. UDMA_RX_BUF_SIZE);
  289. if (ret) {
  290. dev_err(dev, "RX dma add buf failed %d\n", ret);
  291. goto err_free_tx;
  292. }
  293. }
  294. ret = dma_enable(&common->dma_tx);
  295. if (ret) {
  296. dev_err(dev, "TX dma_enable failed %d\n", ret);
  297. goto err_free_rx;
  298. }
  299. ret = dma_enable(&common->dma_rx);
  300. if (ret) {
  301. dev_err(dev, "RX dma_enable failed %d\n", ret);
  302. goto err_dis_tx;
  303. }
  304. /* Control register */
  305. writel(AM65_CPSW_CTL_REG_P0_ENABLE |
  306. AM65_CPSW_CTL_REG_P0_TX_CRC_REMOVE |
  307. AM65_CPSW_CTL_REG_P0_RX_PAD,
  308. common->cpsw_base + AM65_CPSW_CTL_REG);
  309. /* disable priority elevation */
  310. writel(0, common->cpsw_base + AM65_CPSW_PTYPE_REG);
  311. /* enable statistics */
  312. writel(BIT(0) | BIT(priv->port_id),
  313. common->cpsw_base + AM65_CPSW_STAT_PORT_EN_REG);
  314. /* Port 0 length register */
  315. writel(PKTSIZE_ALIGN, port0->port_base + AM65_CPSW_PN_RX_MAXLEN_REG);
  316. /* set base flow_id */
  317. dma_get_cfg(&common->dma_rx, 0, (void **)&dma_rx_cfg_data);
  318. writel(dma_rx_cfg_data->flow_id_base,
  319. port0->port_base + AM65_CPSW_P0_FLOW_ID_REG);
  320. dev_info(dev, "K3 CPSW: rflow_id_base: %u\n",
  321. dma_rx_cfg_data->flow_id_base);
  322. /* Reset and enable the ALE */
  323. writel(AM65_CPSW_ALE_CTL_REG_ENABLE | AM65_CPSW_ALE_CTL_REG_RESET_TBL |
  324. AM65_CPSW_ALE_CTL_REG_BYPASS,
  325. common->ale_base + AM65_CPSW_ALE_CTL_REG);
  326. /* port 0 put into forward mode */
  327. writel(AM65_CPSW_ALE_PN_CTL_REG_MODE_FORWARD,
  328. common->ale_base + AM65_CPSW_ALE_PN_CTL_REG(0));
  329. writel(AM65_CPSW_ALE_DEFTHREAD_EN,
  330. common->ale_base + AM65_CPSW_ALE_THREADMAPDEF_REG);
  331. /* PORT x configuration */
  332. /* Port x Max length register */
  333. writel(PKTSIZE_ALIGN, port->port_base + AM65_CPSW_PN_RX_MAXLEN_REG);
  334. /* Port x set mac */
  335. am65_cpsw_set_sl_mac(port, pdata->enetaddr);
  336. /* Port x ALE: mac_only, Forwarding */
  337. writel(AM65_CPSW_ALE_PN_CTL_REG_MAC_ONLY |
  338. AM65_CPSW_ALE_PN_CTL_REG_MODE_FORWARD,
  339. common->ale_base + AM65_CPSW_ALE_PN_CTL_REG(priv->port_id));
  340. port->mac_control = 0;
  341. if (!am65_cpsw_macsl_reset(port)) {
  342. dev_err(dev, "mac_sl reset failed\n");
  343. ret = -EFAULT;
  344. goto err_dis_rx;
  345. }
  346. ret = phy_startup(priv->phydev);
  347. if (ret) {
  348. dev_err(dev, "phy_startup failed\n");
  349. goto err_dis_rx;
  350. }
  351. ret = am65_cpsw_update_link(priv);
  352. if (!ret) {
  353. ret = -ENODEV;
  354. goto err_phy_shutdown;
  355. }
  356. common->started = true;
  357. return 0;
  358. err_phy_shutdown:
  359. phy_shutdown(priv->phydev);
  360. err_dis_rx:
  361. /* disable ports */
  362. writel(0, common->ale_base + AM65_CPSW_ALE_PN_CTL_REG(priv->port_id));
  363. writel(0, common->ale_base + AM65_CPSW_ALE_PN_CTL_REG(0));
  364. if (!am65_cpsw_macsl_wait_for_idle(port))
  365. dev_err(dev, "mac_sl idle timeout\n");
  366. writel(0, port->macsl_base + AM65_CPSW_MACSL_CTL_REG);
  367. writel(0, common->ale_base + AM65_CPSW_ALE_CTL_REG);
  368. writel(0, common->cpsw_base + AM65_CPSW_CTL_REG);
  369. dma_disable(&common->dma_rx);
  370. err_dis_tx:
  371. dma_disable(&common->dma_tx);
  372. err_free_rx:
  373. dma_free(&common->dma_rx);
  374. err_free_tx:
  375. dma_free(&common->dma_tx);
  376. err_off_clk:
  377. clk_disable(&common->fclk);
  378. err_off_pwrdm:
  379. power_domain_off(&common->pwrdmn);
  380. out:
  381. dev_err(dev, "%s end error\n", __func__);
  382. return ret;
  383. }
  384. static int am65_cpsw_send(struct udevice *dev, void *packet, int length)
  385. {
  386. struct am65_cpsw_priv *priv = dev_get_priv(dev);
  387. struct am65_cpsw_common *common = priv->cpsw_common;
  388. struct ti_udma_drv_packet_data packet_data;
  389. int ret;
  390. packet_data.pkt_type = AM65_CPSW_CPPI_PKT_TYPE;
  391. packet_data.dest_tag = priv->port_id;
  392. ret = dma_send(&common->dma_tx, packet, length, &packet_data);
  393. if (ret) {
  394. dev_err(dev, "TX dma_send failed %d\n", ret);
  395. return ret;
  396. }
  397. return 0;
  398. }
  399. static int am65_cpsw_recv(struct udevice *dev, int flags, uchar **packetp)
  400. {
  401. struct am65_cpsw_priv *priv = dev_get_priv(dev);
  402. struct am65_cpsw_common *common = priv->cpsw_common;
  403. /* try to receive a new packet */
  404. return dma_receive(&common->dma_rx, (void **)packetp, NULL);
  405. }
  406. static int am65_cpsw_free_pkt(struct udevice *dev, uchar *packet, int length)
  407. {
  408. struct am65_cpsw_priv *priv = dev_get_priv(dev);
  409. struct am65_cpsw_common *common = priv->cpsw_common;
  410. int ret;
  411. if (length > 0) {
  412. u32 pkt = common->rx_next % UDMA_RX_DESC_NUM;
  413. ret = dma_prepare_rcv_buf(&common->dma_rx,
  414. net_rx_packets[pkt],
  415. UDMA_RX_BUF_SIZE);
  416. if (ret)
  417. dev_err(dev, "RX dma free_pkt failed %d\n", ret);
  418. common->rx_next++;
  419. }
  420. return 0;
  421. }
  422. static void am65_cpsw_stop(struct udevice *dev)
  423. {
  424. struct am65_cpsw_priv *priv = dev_get_priv(dev);
  425. struct am65_cpsw_common *common = priv->cpsw_common;
  426. struct am65_cpsw_port *port = &common->ports[priv->port_id];
  427. if (!common->started)
  428. return;
  429. phy_shutdown(priv->phydev);
  430. writel(0, common->ale_base + AM65_CPSW_ALE_PN_CTL_REG(priv->port_id));
  431. writel(0, common->ale_base + AM65_CPSW_ALE_PN_CTL_REG(0));
  432. if (!am65_cpsw_macsl_wait_for_idle(port))
  433. dev_err(dev, "mac_sl idle timeout\n");
  434. writel(0, port->macsl_base + AM65_CPSW_MACSL_CTL_REG);
  435. writel(0, common->ale_base + AM65_CPSW_ALE_CTL_REG);
  436. writel(0, common->cpsw_base + AM65_CPSW_CTL_REG);
  437. dma_disable(&common->dma_tx);
  438. dma_free(&common->dma_tx);
  439. dma_disable(&common->dma_rx);
  440. dma_free(&common->dma_rx);
  441. common->started = false;
  442. }
  443. static int am65_cpsw_am654_get_efuse_macid(struct udevice *dev,
  444. int slave, u8 *mac_addr)
  445. {
  446. u32 mac_lo, mac_hi, offset;
  447. struct regmap *syscon;
  448. int ret;
  449. syscon = syscon_regmap_lookup_by_phandle(dev, "ti,syscon-efuse");
  450. if (IS_ERR(syscon)) {
  451. if (PTR_ERR(syscon) == -ENODEV)
  452. return 0;
  453. return PTR_ERR(syscon);
  454. }
  455. ret = dev_read_u32_index(dev, "ti,syscon-efuse", 1, &offset);
  456. if (ret)
  457. return ret;
  458. regmap_read(syscon, offset, &mac_lo);
  459. regmap_read(syscon, offset + 4, &mac_hi);
  460. mac_addr[0] = (mac_hi >> 8) & 0xff;
  461. mac_addr[1] = mac_hi & 0xff;
  462. mac_addr[2] = (mac_lo >> 24) & 0xff;
  463. mac_addr[3] = (mac_lo >> 16) & 0xff;
  464. mac_addr[4] = (mac_lo >> 8) & 0xff;
  465. mac_addr[5] = mac_lo & 0xff;
  466. return 0;
  467. }
  468. static int am65_cpsw_read_rom_hwaddr(struct udevice *dev)
  469. {
  470. struct am65_cpsw_priv *priv = dev_get_priv(dev);
  471. struct eth_pdata *pdata = dev_get_plat(dev);
  472. am65_cpsw_am654_get_efuse_macid(dev,
  473. priv->port_id,
  474. pdata->enetaddr);
  475. return 0;
  476. }
  477. static const struct eth_ops am65_cpsw_ops = {
  478. .start = am65_cpsw_start,
  479. .send = am65_cpsw_send,
  480. .recv = am65_cpsw_recv,
  481. .free_pkt = am65_cpsw_free_pkt,
  482. .stop = am65_cpsw_stop,
  483. .read_rom_hwaddr = am65_cpsw_read_rom_hwaddr,
  484. };
  485. static const struct soc_attr k3_mdio_soc_data[] = {
  486. { .family = "AM62X", .revision = "SR1.0" },
  487. { .family = "AM64X", .revision = "SR1.0" },
  488. { .family = "AM64X", .revision = "SR2.0" },
  489. { .family = "AM65X", .revision = "SR1.0" },
  490. { .family = "AM65X", .revision = "SR2.0" },
  491. { .family = "J7200", .revision = "SR1.0" },
  492. { .family = "J7200", .revision = "SR2.0" },
  493. { .family = "J721E", .revision = "SR1.0" },
  494. { .family = "J721E", .revision = "SR1.1" },
  495. { .family = "J721S2", .revision = "SR1.0" },
  496. { /* sentinel */ },
  497. };
  498. static ofnode am65_cpsw_find_mdio(ofnode parent)
  499. {
  500. ofnode node;
  501. ofnode_for_each_subnode(node, parent)
  502. if (ofnode_device_is_compatible(node, "ti,cpsw-mdio"))
  503. return node;
  504. return ofnode_null();
  505. }
  506. static int am65_cpsw_mdio_setup(struct udevice *dev)
  507. {
  508. struct am65_cpsw_priv *priv = dev_get_priv(dev);
  509. struct am65_cpsw_common *cpsw_common = priv->cpsw_common;
  510. struct udevice *mdio_dev;
  511. ofnode mdio;
  512. int ret;
  513. mdio = am65_cpsw_find_mdio(dev_ofnode(cpsw_common->dev));
  514. if (!ofnode_valid(mdio))
  515. return 0;
  516. /*
  517. * The MDIO controller is represented in the DT binding by a
  518. * subnode of the MAC controller.
  519. *
  520. * We don't have a DM driver for the MDIO device yet, and thus any
  521. * pinctrl setting on its node will be ignored.
  522. *
  523. * However, we do need to make sure the pins states tied to the
  524. * MDIO node are configured properly. Fortunately, the core DM
  525. * does that for use when we get a device, so we can work around
  526. * that whole issue by just requesting a dummy MDIO driver to
  527. * probe, and our pins will get muxed.
  528. */
  529. ret = uclass_get_device_by_ofnode(UCLASS_MDIO, mdio, &mdio_dev);
  530. if (ret)
  531. return ret;
  532. return 0;
  533. }
  534. static int am65_cpsw_mdio_init(struct udevice *dev)
  535. {
  536. struct am65_cpsw_priv *priv = dev_get_priv(dev);
  537. struct am65_cpsw_common *cpsw_common = priv->cpsw_common;
  538. int ret;
  539. if (!priv->has_phy || cpsw_common->bus)
  540. return 0;
  541. ret = am65_cpsw_mdio_setup(dev);
  542. if (ret)
  543. return ret;
  544. cpsw_common->bus = cpsw_mdio_init(dev->name,
  545. cpsw_common->mdio_base,
  546. cpsw_common->bus_freq,
  547. clk_get_rate(&cpsw_common->fclk),
  548. priv->mdio_manual_mode);
  549. if (!cpsw_common->bus)
  550. return -EFAULT;
  551. return 0;
  552. }
  553. static int am65_cpsw_phy_init(struct udevice *dev)
  554. {
  555. struct am65_cpsw_priv *priv = dev_get_priv(dev);
  556. struct am65_cpsw_common *cpsw_common = priv->cpsw_common;
  557. struct eth_pdata *pdata = dev_get_plat(dev);
  558. struct phy_device *phydev;
  559. u32 supported = PHY_GBIT_FEATURES;
  560. int ret;
  561. phydev = phy_connect(cpsw_common->bus,
  562. priv->phy_addr,
  563. priv->dev,
  564. pdata->phy_interface);
  565. if (!phydev) {
  566. dev_err(dev, "phy_connect() failed\n");
  567. return -ENODEV;
  568. }
  569. phydev->supported &= supported;
  570. if (pdata->max_speed) {
  571. ret = phy_set_supported(phydev, pdata->max_speed);
  572. if (ret)
  573. return ret;
  574. }
  575. phydev->advertising = phydev->supported;
  576. if (ofnode_valid(priv->phy_node))
  577. phydev->node = priv->phy_node;
  578. priv->phydev = phydev;
  579. ret = phy_config(phydev);
  580. if (ret < 0)
  581. pr_err("phy_config() failed: %d", ret);
  582. return ret;
  583. }
  584. static int am65_cpsw_ofdata_parse_phy(struct udevice *dev)
  585. {
  586. struct eth_pdata *pdata = dev_get_plat(dev);
  587. struct am65_cpsw_priv *priv = dev_get_priv(dev);
  588. struct ofnode_phandle_args out_args;
  589. int ret = 0;
  590. dev_read_u32(dev, "reg", &priv->port_id);
  591. pdata->phy_interface = dev_read_phy_mode(dev);
  592. if (pdata->phy_interface == PHY_INTERFACE_MODE_NA) {
  593. dev_err(dev, "Invalid PHY mode, port %u\n", priv->port_id);
  594. return -EINVAL;
  595. }
  596. dev_read_u32(dev, "max-speed", (u32 *)&pdata->max_speed);
  597. if (pdata->max_speed)
  598. dev_err(dev, "Port %u speed froced to %uMbit\n",
  599. priv->port_id, pdata->max_speed);
  600. priv->has_phy = true;
  601. ret = ofnode_parse_phandle_with_args(dev_ofnode(dev), "phy-handle",
  602. NULL, 0, 0, &out_args);
  603. if (ret) {
  604. dev_err(dev, "can't parse phy-handle port %u (%d)\n",
  605. priv->port_id, ret);
  606. priv->has_phy = false;
  607. ret = 0;
  608. }
  609. priv->phy_node = out_args.node;
  610. if (priv->has_phy) {
  611. ret = ofnode_read_u32(priv->phy_node, "reg", &priv->phy_addr);
  612. if (ret) {
  613. dev_err(dev, "failed to get phy_addr port %u (%d)\n",
  614. priv->port_id, ret);
  615. goto out;
  616. }
  617. }
  618. out:
  619. return ret;
  620. }
  621. static int am65_cpsw_port_probe(struct udevice *dev)
  622. {
  623. struct am65_cpsw_priv *priv = dev_get_priv(dev);
  624. struct eth_pdata *pdata = dev_get_plat(dev);
  625. struct am65_cpsw_common *cpsw_common;
  626. char portname[15];
  627. int ret;
  628. priv->dev = dev;
  629. cpsw_common = dev_get_priv(dev->parent);
  630. priv->cpsw_common = cpsw_common;
  631. sprintf(portname, "%s%s", dev->parent->name, dev->name);
  632. device_set_name(dev, portname);
  633. priv->mdio_manual_mode = false;
  634. if (soc_device_match(k3_mdio_soc_data))
  635. priv->mdio_manual_mode = true;
  636. ret = am65_cpsw_ofdata_parse_phy(dev);
  637. if (ret)
  638. goto out;
  639. ret = am65_cpsw_gmii_sel_k3(priv, pdata->phy_interface);
  640. if (ret)
  641. goto out;
  642. ret = am65_cpsw_mdio_init(dev);
  643. if (ret)
  644. goto out;
  645. ret = am65_cpsw_phy_init(dev);
  646. if (ret)
  647. goto out;
  648. out:
  649. return ret;
  650. }
  651. static int am65_cpsw_probe_nuss(struct udevice *dev)
  652. {
  653. struct am65_cpsw_common *cpsw_common = dev_get_priv(dev);
  654. ofnode ports_np, node;
  655. int ret, i;
  656. struct udevice *port_dev;
  657. cpsw_common->dev = dev;
  658. cpsw_common->ss_base = dev_read_addr(dev);
  659. if (cpsw_common->ss_base == FDT_ADDR_T_NONE)
  660. return -EINVAL;
  661. ret = power_domain_get_by_index(dev, &cpsw_common->pwrdmn, 0);
  662. if (ret) {
  663. dev_err(dev, "failed to get pwrdmn: %d\n", ret);
  664. return ret;
  665. }
  666. ret = clk_get_by_name(dev, "fck", &cpsw_common->fclk);
  667. if (ret) {
  668. power_domain_free(&cpsw_common->pwrdmn);
  669. dev_err(dev, "failed to get clock %d\n", ret);
  670. return ret;
  671. }
  672. cpsw_common->cpsw_base = cpsw_common->ss_base + AM65_CPSW_CPSW_NU_BASE;
  673. cpsw_common->ale_base = cpsw_common->cpsw_base +
  674. AM65_CPSW_CPSW_NU_ALE_BASE;
  675. cpsw_common->mdio_base = cpsw_common->ss_base + AM65_CPSW_MDIO_BASE;
  676. ports_np = dev_read_subnode(dev, "ethernet-ports");
  677. if (!ofnode_valid(ports_np)) {
  678. ret = -ENOENT;
  679. goto out;
  680. }
  681. ofnode_for_each_subnode(node, ports_np) {
  682. const char *node_name;
  683. u32 port_id;
  684. bool disabled;
  685. node_name = ofnode_get_name(node);
  686. disabled = !ofnode_is_enabled(node);
  687. ret = ofnode_read_u32(node, "reg", &port_id);
  688. if (ret) {
  689. dev_err(dev, "%s: failed to get port_id (%d)\n",
  690. node_name, ret);
  691. goto out;
  692. }
  693. if (port_id >= AM65_CPSW_CPSWNU_MAX_PORTS) {
  694. dev_err(dev, "%s: invalid port_id (%d)\n",
  695. node_name, port_id);
  696. ret = -EINVAL;
  697. goto out;
  698. }
  699. cpsw_common->port_num++;
  700. if (!port_id)
  701. continue;
  702. cpsw_common->ports[port_id].disabled = disabled;
  703. if (disabled)
  704. continue;
  705. ret = device_bind_driver_to_node(dev, "am65_cpsw_nuss_port", ofnode_get_name(node), node, &port_dev);
  706. if (ret)
  707. dev_err(dev, "Failed to bind to %s node\n", ofnode_get_name(node));
  708. }
  709. for (i = 0; i < AM65_CPSW_CPSWNU_MAX_PORTS; i++) {
  710. struct am65_cpsw_port *port = &cpsw_common->ports[i];
  711. port->port_base = cpsw_common->cpsw_base +
  712. AM65_CPSW_CPSW_NU_PORTS_OFFSET +
  713. (i * AM65_CPSW_CPSW_NU_PORTS_OFFSET);
  714. port->macsl_base = port->port_base +
  715. AM65_CPSW_CPSW_NU_PORT_MACSL_OFFSET;
  716. }
  717. cpsw_common->bus_freq =
  718. dev_read_u32_default(dev, "bus_freq",
  719. AM65_CPSW_MDIO_BUS_FREQ_DEF);
  720. dev_info(dev, "K3 CPSW: nuss_ver: 0x%08X cpsw_ver: 0x%08X ale_ver: 0x%08X Ports:%u mdio_freq:%u\n",
  721. readl(cpsw_common->ss_base),
  722. readl(cpsw_common->cpsw_base),
  723. readl(cpsw_common->ale_base),
  724. cpsw_common->port_num,
  725. cpsw_common->bus_freq);
  726. out:
  727. clk_free(&cpsw_common->fclk);
  728. power_domain_free(&cpsw_common->pwrdmn);
  729. return ret;
  730. }
  731. static const struct udevice_id am65_cpsw_nuss_ids[] = {
  732. { .compatible = "ti,am654-cpsw-nuss" },
  733. { .compatible = "ti,j721e-cpsw-nuss" },
  734. { .compatible = "ti,am642-cpsw-nuss" },
  735. { }
  736. };
  737. U_BOOT_DRIVER(am65_cpsw_nuss) = {
  738. .name = "am65_cpsw_nuss",
  739. .id = UCLASS_MISC,
  740. .of_match = am65_cpsw_nuss_ids,
  741. .probe = am65_cpsw_probe_nuss,
  742. .priv_auto = sizeof(struct am65_cpsw_common),
  743. };
  744. U_BOOT_DRIVER(am65_cpsw_nuss_port) = {
  745. .name = "am65_cpsw_nuss_port",
  746. .id = UCLASS_ETH,
  747. .probe = am65_cpsw_port_probe,
  748. .ops = &am65_cpsw_ops,
  749. .priv_auto = sizeof(struct am65_cpsw_priv),
  750. .plat_auto = sizeof(struct eth_pdata),
  751. .flags = DM_FLAG_ALLOC_PRIV_DMA | DM_FLAG_OS_PREPARE,
  752. };
  753. static const struct udevice_id am65_cpsw_mdio_ids[] = {
  754. { .compatible = "ti,cpsw-mdio" },
  755. { }
  756. };
  757. U_BOOT_DRIVER(am65_cpsw_mdio) = {
  758. .name = "am65_cpsw_mdio",
  759. .id = UCLASS_MDIO,
  760. .of_match = am65_cpsw_mdio_ids,
  761. };