blob: 456c3eb5b243ac280594d63289fd0f76c176dd32 [file] [log] [blame]
Keerthya00b95c2019-07-09 10:30:34 +05301// 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
9#include <common.h>
Simon Glass9bc15642020-02-03 07:36:16 -070010#include <malloc.h>
Simon Glass274e0b02020-05-10 11:39:56 -060011#include <asm/cache.h>
Keerthya00b95c2019-07-09 10:30:34 +053012#include <asm/io.h>
13#include <asm/processor.h>
14#include <clk.h>
15#include <dm.h>
Simon Glass9bc15642020-02-03 07:36:16 -070016#include <dm/device_compat.h>
Keerthya00b95c2019-07-09 10:30:34 +053017#include <dm/lists.h>
Maxime Ripard028849d2023-07-24 15:57:30 +020018#include <dm/pinctrl.h>
Keerthya00b95c2019-07-09 10:30:34 +053019#include <dma-uclass.h>
20#include <dm/of_access.h>
21#include <miiphy.h>
22#include <net.h>
23#include <phy.h>
24#include <power-domain.h>
Roger Quadroscb8f8ad2023-07-22 22:31:48 +030025#include <regmap.h>
Ravi Gunasekaran1eb61912022-09-22 15:21:24 +053026#include <soc.h>
Roger Quadroscb8f8ad2023-07-22 22:31:48 +030027#include <syscon.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060028#include <linux/bitops.h>
Keerthya00b95c2019-07-09 10:30:34 +053029#include <linux/soc/ti/ti-udma.h>
30
31#include "cpsw_mdio.h"
32
Vignesh Raghavendrac5a66132021-05-10 20:06:09 +053033#define AM65_CPSW_CPSWNU_MAX_PORTS 9
Keerthya00b95c2019-07-09 10:30:34 +053034
35#define AM65_CPSW_SS_BASE 0x0
36#define AM65_CPSW_SGMII_BASE 0x100
37#define AM65_CPSW_MDIO_BASE 0xf00
38#define AM65_CPSW_XGMII_BASE 0x2100
39#define AM65_CPSW_CPSW_NU_BASE 0x20000
40#define AM65_CPSW_CPSW_NU_ALE_BASE 0x1e000
41
42#define AM65_CPSW_CPSW_NU_PORTS_OFFSET 0x1000
43#define AM65_CPSW_CPSW_NU_PORT_MACSL_OFFSET 0x330
44
45#define AM65_CPSW_MDIO_BUS_FREQ_DEF 1000000
46
47#define AM65_CPSW_CTL_REG 0x4
48#define AM65_CPSW_STAT_PORT_EN_REG 0x14
49#define AM65_CPSW_PTYPE_REG 0x18
50
51#define AM65_CPSW_CTL_REG_P0_ENABLE BIT(2)
52#define AM65_CPSW_CTL_REG_P0_TX_CRC_REMOVE BIT(13)
53#define AM65_CPSW_CTL_REG_P0_RX_PAD BIT(14)
54
55#define AM65_CPSW_P0_FLOW_ID_REG 0x8
56#define AM65_CPSW_PN_RX_MAXLEN_REG 0x24
57#define AM65_CPSW_PN_REG_SA_L 0x308
58#define AM65_CPSW_PN_REG_SA_H 0x30c
59
Siddharth Vadapalli726fc0a2023-08-02 13:47:25 +053060#define AM65_CPSW_SGMII_CONTROL_REG 0x010
61#define AM65_CPSW_SGMII_MR_ADV_ABILITY_REG 0x018
62#define AM65_CPSW_SGMII_CONTROL_MR_AN_ENABLE BIT(0)
63
64#define ADVERTISE_SGMII 0x1
65
Keerthya00b95c2019-07-09 10:30:34 +053066#define AM65_CPSW_ALE_CTL_REG 0x8
67#define AM65_CPSW_ALE_CTL_REG_ENABLE BIT(31)
68#define AM65_CPSW_ALE_CTL_REG_RESET_TBL BIT(30)
69#define AM65_CPSW_ALE_CTL_REG_BYPASS BIT(4)
70#define AM65_CPSW_ALE_PN_CTL_REG(x) (0x40 + (x) * 4)
71#define AM65_CPSW_ALE_PN_CTL_REG_MODE_FORWARD 0x3
72#define AM65_CPSW_ALE_PN_CTL_REG_MAC_ONLY BIT(11)
73
Vignesh Raghavendra5cb8a0f2020-07-06 13:36:53 +053074#define AM65_CPSW_ALE_THREADMAPDEF_REG 0x134
75#define AM65_CPSW_ALE_DEFTHREAD_EN BIT(15)
76
Keerthya00b95c2019-07-09 10:30:34 +053077#define AM65_CPSW_MACSL_CTL_REG 0x0
78#define AM65_CPSW_MACSL_CTL_REG_IFCTL_A BIT(15)
Murali Karicheri6565e902020-04-17 11:12:09 -040079#define AM65_CPSW_MACSL_CTL_EXT_EN BIT(18)
Keerthya00b95c2019-07-09 10:30:34 +053080#define AM65_CPSW_MACSL_CTL_REG_GIG BIT(7)
81#define AM65_CPSW_MACSL_CTL_REG_GMII_EN BIT(5)
82#define AM65_CPSW_MACSL_CTL_REG_LOOPBACK BIT(1)
83#define AM65_CPSW_MACSL_CTL_REG_FULL_DUPLEX BIT(0)
84#define AM65_CPSW_MACSL_RESET_REG 0x8
85#define AM65_CPSW_MACSL_RESET_REG_RESET BIT(0)
86#define AM65_CPSW_MACSL_STATUS_REG 0x4
87#define AM65_CPSW_MACSL_RESET_REG_PN_IDLE BIT(31)
88#define AM65_CPSW_MACSL_RESET_REG_PN_E_IDLE BIT(30)
89#define AM65_CPSW_MACSL_RESET_REG_PN_P_IDLE BIT(29)
90#define AM65_CPSW_MACSL_RESET_REG_PN_TX_IDLE BIT(28)
91#define AM65_CPSW_MACSL_RESET_REG_IDLE_MASK \
92 (AM65_CPSW_MACSL_RESET_REG_PN_IDLE | \
93 AM65_CPSW_MACSL_RESET_REG_PN_E_IDLE | \
94 AM65_CPSW_MACSL_RESET_REG_PN_P_IDLE | \
95 AM65_CPSW_MACSL_RESET_REG_PN_TX_IDLE)
96
97#define AM65_CPSW_CPPI_PKT_TYPE 0x7
98
99struct am65_cpsw_port {
100 fdt_addr_t port_base;
Siddharth Vadapalli726fc0a2023-08-02 13:47:25 +0530101 fdt_addr_t port_sgmii_base;
Keerthya00b95c2019-07-09 10:30:34 +0530102 fdt_addr_t macsl_base;
103 bool disabled;
104 u32 mac_control;
105};
106
107struct am65_cpsw_common {
108 struct udevice *dev;
109 fdt_addr_t ss_base;
110 fdt_addr_t cpsw_base;
111 fdt_addr_t mdio_base;
112 fdt_addr_t ale_base;
Keerthya00b95c2019-07-09 10:30:34 +0530113
114 struct clk fclk;
115 struct power_domain pwrdmn;
116
117 u32 port_num;
118 struct am65_cpsw_port ports[AM65_CPSW_CPSWNU_MAX_PORTS];
Keerthya00b95c2019-07-09 10:30:34 +0530119
120 struct mii_dev *bus;
121 u32 bus_freq;
122
123 struct dma dma_tx;
124 struct dma dma_rx;
125 u32 rx_next;
126 u32 rx_pend;
127 bool started;
128};
129
130struct am65_cpsw_priv {
131 struct udevice *dev;
132 struct am65_cpsw_common *cpsw_common;
133 u32 port_id;
134
135 struct phy_device *phydev;
136 bool has_phy;
137 ofnode phy_node;
138 u32 phy_addr;
Ravi Gunasekaran1eb61912022-09-22 15:21:24 +0530139
140 bool mdio_manual_mode;
Keerthya00b95c2019-07-09 10:30:34 +0530141};
142
143#ifdef PKTSIZE_ALIGN
144#define UDMA_RX_BUF_SIZE PKTSIZE_ALIGN
145#else
146#define UDMA_RX_BUF_SIZE ALIGN(1522, ARCH_DMA_MINALIGN)
147#endif
148
149#ifdef PKTBUFSRX
150#define UDMA_RX_DESC_NUM PKTBUFSRX
151#else
152#define UDMA_RX_DESC_NUM 4
153#endif
154
155#define mac_hi(mac) (((mac)[0] << 0) | ((mac)[1] << 8) | \
156 ((mac)[2] << 16) | ((mac)[3] << 24))
157#define mac_lo(mac) (((mac)[4] << 0) | ((mac)[5] << 8))
158
159static void am65_cpsw_set_sl_mac(struct am65_cpsw_port *slave,
160 unsigned char *addr)
161{
162 writel(mac_hi(addr),
163 slave->port_base + AM65_CPSW_PN_REG_SA_H);
164 writel(mac_lo(addr),
165 slave->port_base + AM65_CPSW_PN_REG_SA_L);
166}
167
168int am65_cpsw_macsl_reset(struct am65_cpsw_port *slave)
169{
170 u32 i = 100;
171
172 /* Set the soft reset bit */
173 writel(AM65_CPSW_MACSL_RESET_REG_RESET,
174 slave->macsl_base + AM65_CPSW_MACSL_RESET_REG);
175
176 while ((readl(slave->macsl_base + AM65_CPSW_MACSL_RESET_REG) &
177 AM65_CPSW_MACSL_RESET_REG_RESET) && i--)
178 cpu_relax();
179
180 /* Timeout on the reset */
181 return i;
182}
183
184static int am65_cpsw_macsl_wait_for_idle(struct am65_cpsw_port *slave)
185{
186 u32 i = 100;
187
188 while ((readl(slave->macsl_base + AM65_CPSW_MACSL_STATUS_REG) &
189 AM65_CPSW_MACSL_RESET_REG_IDLE_MASK) && i--)
190 cpu_relax();
191
192 return i;
193}
194
195static int am65_cpsw_update_link(struct am65_cpsw_priv *priv)
196{
197 struct am65_cpsw_common *common = priv->cpsw_common;
198 struct am65_cpsw_port *port = &common->ports[priv->port_id];
199 struct phy_device *phy = priv->phydev;
200 u32 mac_control = 0;
201
202 if (phy->link) { /* link up */
203 mac_control = /*AM65_CPSW_MACSL_CTL_REG_LOOPBACK |*/
204 AM65_CPSW_MACSL_CTL_REG_GMII_EN;
205 if (phy->speed == 1000)
206 mac_control |= AM65_CPSW_MACSL_CTL_REG_GIG;
Murali Karicheri6565e902020-04-17 11:12:09 -0400207 if (phy->speed == 10 && phy_interface_is_rgmii(phy))
208 /* Can be used with in band mode only */
209 mac_control |= AM65_CPSW_MACSL_CTL_EXT_EN;
Keerthya00b95c2019-07-09 10:30:34 +0530210 if (phy->duplex == DUPLEX_FULL)
211 mac_control |= AM65_CPSW_MACSL_CTL_REG_FULL_DUPLEX;
212 if (phy->speed == 100)
213 mac_control |= AM65_CPSW_MACSL_CTL_REG_IFCTL_A;
Siddharth Vadapalli726fc0a2023-08-02 13:47:25 +0530214 if (phy->interface == PHY_INTERFACE_MODE_SGMII)
215 mac_control |= AM65_CPSW_MACSL_CTL_EXT_EN;
Keerthya00b95c2019-07-09 10:30:34 +0530216 }
217
218 if (mac_control == port->mac_control)
219 goto out;
220
221 if (mac_control) {
222 printf("link up on port %d, speed %d, %s duplex\n",
223 priv->port_id, phy->speed,
224 (phy->duplex == DUPLEX_FULL) ? "full" : "half");
225 } else {
226 printf("link down on port %d\n", priv->port_id);
227 }
228
229 writel(mac_control, port->macsl_base + AM65_CPSW_MACSL_CTL_REG);
230 port->mac_control = mac_control;
231
232out:
233 return phy->link;
234}
235
Andreas Dannenberg1dc2ee62023-06-14 17:28:53 -0500236#define AM65_GMII_SEL_PORT_OFFS(x) (0x4 * ((x) - 1))
237
Keerthya00b95c2019-07-09 10:30:34 +0530238#define AM65_GMII_SEL_MODE_MII 0
239#define AM65_GMII_SEL_MODE_RMII 1
240#define AM65_GMII_SEL_MODE_RGMII 2
Siddharth Vadapalli726fc0a2023-08-02 13:47:25 +0530241#define AM65_GMII_SEL_MODE_SGMII 3
Keerthya00b95c2019-07-09 10:30:34 +0530242
243#define AM65_GMII_SEL_RGMII_IDMODE BIT(4)
244
Roger Quadrosbe0619b2023-07-22 22:31:49 +0300245static int am65_cpsw_gmii_sel_k3(struct am65_cpsw_priv *priv,
246 phy_interface_t phy_mode)
Keerthya00b95c2019-07-09 10:30:34 +0530247{
Roger Quadrosbe0619b2023-07-22 22:31:49 +0300248 struct udevice *dev = priv->dev;
249 u32 offset, reg, phandle;
Keerthya00b95c2019-07-09 10:30:34 +0530250 bool rgmii_id = false;
Roger Quadrosbe0619b2023-07-22 22:31:49 +0300251 fdt_addr_t gmii_sel;
252 u32 mode = 0;
253 ofnode node;
254 int ret;
255
256 ret = ofnode_read_u32(dev_ofnode(dev), "phys", &phandle);
257 if (ret)
258 return ret;
259
260 ret = ofnode_read_u32_index(dev_ofnode(dev), "phys", 1, &offset);
261 if (ret)
262 return ret;
263
264 node = ofnode_get_by_phandle(phandle);
265 if (!ofnode_valid(node))
266 return -ENODEV;
267
268 gmii_sel = ofnode_get_addr(node);
269 if (gmii_sel == FDT_ADDR_T_NONE)
270 return -ENODEV;
Keerthya00b95c2019-07-09 10:30:34 +0530271
Roger Quadrosbe0619b2023-07-22 22:31:49 +0300272 gmii_sel += AM65_GMII_SEL_PORT_OFFS(offset);
Andreas Dannenberg1dc2ee62023-06-14 17:28:53 -0500273 reg = readl(gmii_sel);
Keerthya00b95c2019-07-09 10:30:34 +0530274
Roger Quadrosbe0619b2023-07-22 22:31:49 +0300275 dev_dbg(dev, "old gmii_sel: %08x\n", reg);
Keerthya00b95c2019-07-09 10:30:34 +0530276
277 switch (phy_mode) {
278 case PHY_INTERFACE_MODE_RMII:
279 mode = AM65_GMII_SEL_MODE_RMII;
280 break;
281
282 case PHY_INTERFACE_MODE_RGMII:
Grygorii Strashkobf45d9b2019-09-19 11:16:41 +0300283 case PHY_INTERFACE_MODE_RGMII_RXID:
Keerthya00b95c2019-07-09 10:30:34 +0530284 mode = AM65_GMII_SEL_MODE_RGMII;
285 break;
286
287 case PHY_INTERFACE_MODE_RGMII_ID:
Keerthya00b95c2019-07-09 10:30:34 +0530288 case PHY_INTERFACE_MODE_RGMII_TXID:
289 mode = AM65_GMII_SEL_MODE_RGMII;
290 rgmii_id = true;
291 break;
292
Siddharth Vadapalli726fc0a2023-08-02 13:47:25 +0530293 case PHY_INTERFACE_MODE_SGMII:
294 mode = AM65_GMII_SEL_MODE_SGMII;
295 break;
296
Keerthya00b95c2019-07-09 10:30:34 +0530297 default:
Roger Quadrosbe0619b2023-07-22 22:31:49 +0300298 dev_warn(dev,
Keerthya00b95c2019-07-09 10:30:34 +0530299 "Unsupported PHY mode: %u. Defaulting to MII.\n",
300 phy_mode);
301 /* fallthrough */
302 case PHY_INTERFACE_MODE_MII:
303 mode = AM65_GMII_SEL_MODE_MII;
304 break;
305 };
306
307 if (rgmii_id)
308 mode |= AM65_GMII_SEL_RGMII_IDMODE;
309
310 reg = mode;
Roger Quadrosbe0619b2023-07-22 22:31:49 +0300311 dev_dbg(dev, "gmii_sel PHY mode: %u, new gmii_sel: %08x\n",
Keerthya00b95c2019-07-09 10:30:34 +0530312 phy_mode, reg);
Andreas Dannenberg1dc2ee62023-06-14 17:28:53 -0500313 writel(reg, gmii_sel);
Keerthya00b95c2019-07-09 10:30:34 +0530314
Andreas Dannenberg1dc2ee62023-06-14 17:28:53 -0500315 reg = readl(gmii_sel);
Roger Quadrosbe0619b2023-07-22 22:31:49 +0300316 if (reg != mode) {
317 dev_err(dev,
Keerthya00b95c2019-07-09 10:30:34 +0530318 "gmii_sel PHY mode NOT SET!: requested: %08x, gmii_sel: %08x\n",
319 mode, reg);
Roger Quadrosbe0619b2023-07-22 22:31:49 +0300320 return 0;
321 }
322
323 return 0;
Keerthya00b95c2019-07-09 10:30:34 +0530324}
325
326static int am65_cpsw_start(struct udevice *dev)
327{
Simon Glassfa20e932020-12-03 16:55:20 -0700328 struct eth_pdata *pdata = dev_get_plat(dev);
Keerthya00b95c2019-07-09 10:30:34 +0530329 struct am65_cpsw_priv *priv = dev_get_priv(dev);
330 struct am65_cpsw_common *common = priv->cpsw_common;
331 struct am65_cpsw_port *port = &common->ports[priv->port_id];
332 struct am65_cpsw_port *port0 = &common->ports[0];
Vignesh Raghavendra462ff042019-12-04 22:17:22 +0530333 struct ti_udma_drv_chan_cfg_data *dma_rx_cfg_data;
Keerthya00b95c2019-07-09 10:30:34 +0530334 int ret, i;
335
336 ret = power_domain_on(&common->pwrdmn);
337 if (ret) {
338 dev_err(dev, "power_domain_on() failed %d\n", ret);
339 goto out;
340 }
341
342 ret = clk_enable(&common->fclk);
343 if (ret) {
344 dev_err(dev, "clk enabled failed %d\n", ret);
345 goto err_off_pwrdm;
346 }
347
348 common->rx_next = 0;
349 common->rx_pend = 0;
350 ret = dma_get_by_name(common->dev, "tx0", &common->dma_tx);
351 if (ret) {
352 dev_err(dev, "TX dma get failed %d\n", ret);
353 goto err_off_clk;
354 }
355 ret = dma_get_by_name(common->dev, "rx", &common->dma_rx);
356 if (ret) {
357 dev_err(dev, "RX dma get failed %d\n", ret);
358 goto err_free_tx;
359 }
360
361 for (i = 0; i < UDMA_RX_DESC_NUM; i++) {
362 ret = dma_prepare_rcv_buf(&common->dma_rx,
363 net_rx_packets[i],
364 UDMA_RX_BUF_SIZE);
365 if (ret) {
366 dev_err(dev, "RX dma add buf failed %d\n", ret);
367 goto err_free_tx;
368 }
369 }
370
371 ret = dma_enable(&common->dma_tx);
372 if (ret) {
373 dev_err(dev, "TX dma_enable failed %d\n", ret);
374 goto err_free_rx;
375 }
376 ret = dma_enable(&common->dma_rx);
377 if (ret) {
378 dev_err(dev, "RX dma_enable failed %d\n", ret);
379 goto err_dis_tx;
380 }
381
382 /* Control register */
383 writel(AM65_CPSW_CTL_REG_P0_ENABLE |
384 AM65_CPSW_CTL_REG_P0_TX_CRC_REMOVE |
385 AM65_CPSW_CTL_REG_P0_RX_PAD,
386 common->cpsw_base + AM65_CPSW_CTL_REG);
387
388 /* disable priority elevation */
389 writel(0, common->cpsw_base + AM65_CPSW_PTYPE_REG);
390
391 /* enable statistics */
392 writel(BIT(0) | BIT(priv->port_id),
393 common->cpsw_base + AM65_CPSW_STAT_PORT_EN_REG);
394
395 /* Port 0 length register */
396 writel(PKTSIZE_ALIGN, port0->port_base + AM65_CPSW_PN_RX_MAXLEN_REG);
397
398 /* set base flow_id */
Vignesh Raghavendra462ff042019-12-04 22:17:22 +0530399 dma_get_cfg(&common->dma_rx, 0, (void **)&dma_rx_cfg_data);
400 writel(dma_rx_cfg_data->flow_id_base,
Keerthya00b95c2019-07-09 10:30:34 +0530401 port0->port_base + AM65_CPSW_P0_FLOW_ID_REG);
Vignesh Raghavendra462ff042019-12-04 22:17:22 +0530402 dev_info(dev, "K3 CPSW: rflow_id_base: %u\n",
403 dma_rx_cfg_data->flow_id_base);
Keerthya00b95c2019-07-09 10:30:34 +0530404
405 /* Reset and enable the ALE */
406 writel(AM65_CPSW_ALE_CTL_REG_ENABLE | AM65_CPSW_ALE_CTL_REG_RESET_TBL |
407 AM65_CPSW_ALE_CTL_REG_BYPASS,
408 common->ale_base + AM65_CPSW_ALE_CTL_REG);
409
410 /* port 0 put into forward mode */
411 writel(AM65_CPSW_ALE_PN_CTL_REG_MODE_FORWARD,
412 common->ale_base + AM65_CPSW_ALE_PN_CTL_REG(0));
413
Vignesh Raghavendra5cb8a0f2020-07-06 13:36:53 +0530414 writel(AM65_CPSW_ALE_DEFTHREAD_EN,
415 common->ale_base + AM65_CPSW_ALE_THREADMAPDEF_REG);
416
Keerthya00b95c2019-07-09 10:30:34 +0530417 /* PORT x configuration */
418
419 /* Port x Max length register */
420 writel(PKTSIZE_ALIGN, port->port_base + AM65_CPSW_PN_RX_MAXLEN_REG);
421
422 /* Port x set mac */
423 am65_cpsw_set_sl_mac(port, pdata->enetaddr);
424
425 /* Port x ALE: mac_only, Forwarding */
426 writel(AM65_CPSW_ALE_PN_CTL_REG_MAC_ONLY |
427 AM65_CPSW_ALE_PN_CTL_REG_MODE_FORWARD,
428 common->ale_base + AM65_CPSW_ALE_PN_CTL_REG(priv->port_id));
429
430 port->mac_control = 0;
431 if (!am65_cpsw_macsl_reset(port)) {
432 dev_err(dev, "mac_sl reset failed\n");
433 ret = -EFAULT;
434 goto err_dis_rx;
435 }
436
Siddharth Vadapalli726fc0a2023-08-02 13:47:25 +0530437 if (priv->phydev->interface == PHY_INTERFACE_MODE_SGMII) {
438 writel(ADVERTISE_SGMII,
439 port->port_sgmii_base + AM65_CPSW_SGMII_MR_ADV_ABILITY_REG);
440 writel(AM65_CPSW_SGMII_CONTROL_MR_AN_ENABLE,
441 port->port_sgmii_base + AM65_CPSW_SGMII_CONTROL_REG);
442 }
443
Keerthya00b95c2019-07-09 10:30:34 +0530444 ret = phy_startup(priv->phydev);
445 if (ret) {
446 dev_err(dev, "phy_startup failed\n");
447 goto err_dis_rx;
448 }
449
450 ret = am65_cpsw_update_link(priv);
451 if (!ret) {
452 ret = -ENODEV;
453 goto err_phy_shutdown;
454 }
455
456 common->started = true;
457
458 return 0;
459
460err_phy_shutdown:
461 phy_shutdown(priv->phydev);
462err_dis_rx:
463 /* disable ports */
464 writel(0, common->ale_base + AM65_CPSW_ALE_PN_CTL_REG(priv->port_id));
465 writel(0, common->ale_base + AM65_CPSW_ALE_PN_CTL_REG(0));
466 if (!am65_cpsw_macsl_wait_for_idle(port))
467 dev_err(dev, "mac_sl idle timeout\n");
468 writel(0, port->macsl_base + AM65_CPSW_MACSL_CTL_REG);
469 writel(0, common->ale_base + AM65_CPSW_ALE_CTL_REG);
470 writel(0, common->cpsw_base + AM65_CPSW_CTL_REG);
471
472 dma_disable(&common->dma_rx);
473err_dis_tx:
474 dma_disable(&common->dma_tx);
475err_free_rx:
476 dma_free(&common->dma_rx);
477err_free_tx:
478 dma_free(&common->dma_tx);
479err_off_clk:
480 clk_disable(&common->fclk);
481err_off_pwrdm:
482 power_domain_off(&common->pwrdmn);
483out:
484 dev_err(dev, "%s end error\n", __func__);
485
486 return ret;
487}
488
489static int am65_cpsw_send(struct udevice *dev, void *packet, int length)
490{
491 struct am65_cpsw_priv *priv = dev_get_priv(dev);
492 struct am65_cpsw_common *common = priv->cpsw_common;
493 struct ti_udma_drv_packet_data packet_data;
494 int ret;
495
496 packet_data.pkt_type = AM65_CPSW_CPPI_PKT_TYPE;
497 packet_data.dest_tag = priv->port_id;
498 ret = dma_send(&common->dma_tx, packet, length, &packet_data);
499 if (ret) {
500 dev_err(dev, "TX dma_send failed %d\n", ret);
501 return ret;
502 }
503
504 return 0;
505}
506
507static int am65_cpsw_recv(struct udevice *dev, int flags, uchar **packetp)
508{
509 struct am65_cpsw_priv *priv = dev_get_priv(dev);
510 struct am65_cpsw_common *common = priv->cpsw_common;
511
512 /* try to receive a new packet */
513 return dma_receive(&common->dma_rx, (void **)packetp, NULL);
514}
515
516static int am65_cpsw_free_pkt(struct udevice *dev, uchar *packet, int length)
517{
518 struct am65_cpsw_priv *priv = dev_get_priv(dev);
519 struct am65_cpsw_common *common = priv->cpsw_common;
520 int ret;
521
522 if (length > 0) {
523 u32 pkt = common->rx_next % UDMA_RX_DESC_NUM;
524
525 ret = dma_prepare_rcv_buf(&common->dma_rx,
526 net_rx_packets[pkt],
527 UDMA_RX_BUF_SIZE);
528 if (ret)
529 dev_err(dev, "RX dma free_pkt failed %d\n", ret);
530 common->rx_next++;
531 }
532
533 return 0;
534}
535
536static void am65_cpsw_stop(struct udevice *dev)
537{
538 struct am65_cpsw_priv *priv = dev_get_priv(dev);
539 struct am65_cpsw_common *common = priv->cpsw_common;
540 struct am65_cpsw_port *port = &common->ports[priv->port_id];
541
542 if (!common->started)
543 return;
544
545 phy_shutdown(priv->phydev);
546
547 writel(0, common->ale_base + AM65_CPSW_ALE_PN_CTL_REG(priv->port_id));
548 writel(0, common->ale_base + AM65_CPSW_ALE_PN_CTL_REG(0));
549 if (!am65_cpsw_macsl_wait_for_idle(port))
550 dev_err(dev, "mac_sl idle timeout\n");
551 writel(0, port->macsl_base + AM65_CPSW_MACSL_CTL_REG);
552 writel(0, common->ale_base + AM65_CPSW_ALE_CTL_REG);
553 writel(0, common->cpsw_base + AM65_CPSW_CTL_REG);
554
555 dma_disable(&common->dma_tx);
556 dma_free(&common->dma_tx);
557
558 dma_disable(&common->dma_rx);
559 dma_free(&common->dma_rx);
560
561 common->started = false;
562}
563
Roger Quadroscb8f8ad2023-07-22 22:31:48 +0300564static int am65_cpsw_am654_get_efuse_macid(struct udevice *dev,
565 int slave, u8 *mac_addr)
566{
567 u32 mac_lo, mac_hi, offset;
568 struct regmap *syscon;
569 int ret;
570
571 syscon = syscon_regmap_lookup_by_phandle(dev, "ti,syscon-efuse");
572 if (IS_ERR(syscon)) {
573 if (PTR_ERR(syscon) == -ENODEV)
574 return 0;
575 return PTR_ERR(syscon);
576 }
577
578 ret = dev_read_u32_index(dev, "ti,syscon-efuse", 1, &offset);
579 if (ret)
580 return ret;
581
582 regmap_read(syscon, offset, &mac_lo);
583 regmap_read(syscon, offset + 4, &mac_hi);
584
585 mac_addr[0] = (mac_hi >> 8) & 0xff;
586 mac_addr[1] = mac_hi & 0xff;
587 mac_addr[2] = (mac_lo >> 24) & 0xff;
588 mac_addr[3] = (mac_lo >> 16) & 0xff;
589 mac_addr[4] = (mac_lo >> 8) & 0xff;
590 mac_addr[5] = mac_lo & 0xff;
591
592 return 0;
593}
594
Keerthya00b95c2019-07-09 10:30:34 +0530595static int am65_cpsw_read_rom_hwaddr(struct udevice *dev)
596{
597 struct am65_cpsw_priv *priv = dev_get_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700598 struct eth_pdata *pdata = dev_get_plat(dev);
Keerthya00b95c2019-07-09 10:30:34 +0530599
Roger Quadroscb8f8ad2023-07-22 22:31:48 +0300600 am65_cpsw_am654_get_efuse_macid(dev,
601 priv->port_id,
602 pdata->enetaddr);
Keerthya00b95c2019-07-09 10:30:34 +0530603
604 return 0;
605}
606
607static const struct eth_ops am65_cpsw_ops = {
608 .start = am65_cpsw_start,
609 .send = am65_cpsw_send,
610 .recv = am65_cpsw_recv,
611 .free_pkt = am65_cpsw_free_pkt,
612 .stop = am65_cpsw_stop,
613 .read_rom_hwaddr = am65_cpsw_read_rom_hwaddr,
614};
615
Ravi Gunasekaran1eb61912022-09-22 15:21:24 +0530616static const struct soc_attr k3_mdio_soc_data[] = {
617 { .family = "AM62X", .revision = "SR1.0" },
618 { .family = "AM64X", .revision = "SR1.0" },
619 { .family = "AM64X", .revision = "SR2.0" },
620 { .family = "AM65X", .revision = "SR1.0" },
621 { .family = "AM65X", .revision = "SR2.0" },
622 { .family = "J7200", .revision = "SR1.0" },
623 { .family = "J7200", .revision = "SR2.0" },
624 { .family = "J721E", .revision = "SR1.0" },
625 { .family = "J721E", .revision = "SR1.1" },
626 { .family = "J721S2", .revision = "SR1.0" },
627 { /* sentinel */ },
628};
629
Maxime Ripard028849d2023-07-24 15:57:30 +0200630static ofnode am65_cpsw_find_mdio(ofnode parent)
631{
632 ofnode node;
633
634 ofnode_for_each_subnode(node, parent)
635 if (ofnode_device_is_compatible(node, "ti,cpsw-mdio"))
636 return node;
637
638 return ofnode_null();
639}
640
641static int am65_cpsw_mdio_setup(struct udevice *dev)
642{
643 struct am65_cpsw_priv *priv = dev_get_priv(dev);
644 struct am65_cpsw_common *cpsw_common = priv->cpsw_common;
645 struct udevice *mdio_dev;
646 ofnode mdio;
647 int ret;
648
649 mdio = am65_cpsw_find_mdio(dev_ofnode(cpsw_common->dev));
650 if (!ofnode_valid(mdio))
651 return 0;
652
653 /*
654 * The MDIO controller is represented in the DT binding by a
655 * subnode of the MAC controller.
656 *
657 * We don't have a DM driver for the MDIO device yet, and thus any
658 * pinctrl setting on its node will be ignored.
659 *
660 * However, we do need to make sure the pins states tied to the
661 * MDIO node are configured properly. Fortunately, the core DM
662 * does that for use when we get a device, so we can work around
663 * that whole issue by just requesting a dummy MDIO driver to
664 * probe, and our pins will get muxed.
665 */
666 ret = uclass_get_device_by_ofnode(UCLASS_MDIO, mdio, &mdio_dev);
667 if (ret)
668 return ret;
669
670 return 0;
671}
672
Keerthya00b95c2019-07-09 10:30:34 +0530673static int am65_cpsw_mdio_init(struct udevice *dev)
674{
675 struct am65_cpsw_priv *priv = dev_get_priv(dev);
676 struct am65_cpsw_common *cpsw_common = priv->cpsw_common;
Maxime Ripard028849d2023-07-24 15:57:30 +0200677 int ret;
Keerthya00b95c2019-07-09 10:30:34 +0530678
679 if (!priv->has_phy || cpsw_common->bus)
680 return 0;
681
Maxime Ripard028849d2023-07-24 15:57:30 +0200682 ret = am65_cpsw_mdio_setup(dev);
683 if (ret)
684 return ret;
685
Keerthya00b95c2019-07-09 10:30:34 +0530686 cpsw_common->bus = cpsw_mdio_init(dev->name,
687 cpsw_common->mdio_base,
688 cpsw_common->bus_freq,
Ravi Gunasekaran40cea492022-09-22 15:21:23 +0530689 clk_get_rate(&cpsw_common->fclk),
Ravi Gunasekaran1eb61912022-09-22 15:21:24 +0530690 priv->mdio_manual_mode);
Keerthya00b95c2019-07-09 10:30:34 +0530691 if (!cpsw_common->bus)
692 return -EFAULT;
693
694 return 0;
695}
696
697static int am65_cpsw_phy_init(struct udevice *dev)
698{
699 struct am65_cpsw_priv *priv = dev_get_priv(dev);
700 struct am65_cpsw_common *cpsw_common = priv->cpsw_common;
Simon Glassfa20e932020-12-03 16:55:20 -0700701 struct eth_pdata *pdata = dev_get_plat(dev);
Keerthya00b95c2019-07-09 10:30:34 +0530702 struct phy_device *phydev;
703 u32 supported = PHY_GBIT_FEATURES;
704 int ret;
705
706 phydev = phy_connect(cpsw_common->bus,
707 priv->phy_addr,
708 priv->dev,
709 pdata->phy_interface);
710
711 if (!phydev) {
712 dev_err(dev, "phy_connect() failed\n");
713 return -ENODEV;
714 }
715
716 phydev->supported &= supported;
717 if (pdata->max_speed) {
718 ret = phy_set_supported(phydev, pdata->max_speed);
719 if (ret)
720 return ret;
721 }
722 phydev->advertising = phydev->supported;
723
724 if (ofnode_valid(priv->phy_node))
725 phydev->node = priv->phy_node;
726
727 priv->phydev = phydev;
728 ret = phy_config(phydev);
729 if (ret < 0)
730 pr_err("phy_config() failed: %d", ret);
731
732 return ret;
733}
734
Vignesh Raghavendrabbedbbb2021-12-24 12:55:30 +0530735static int am65_cpsw_ofdata_parse_phy(struct udevice *dev)
Keerthya00b95c2019-07-09 10:30:34 +0530736{
Simon Glassfa20e932020-12-03 16:55:20 -0700737 struct eth_pdata *pdata = dev_get_plat(dev);
Keerthya00b95c2019-07-09 10:30:34 +0530738 struct am65_cpsw_priv *priv = dev_get_priv(dev);
739 struct ofnode_phandle_args out_args;
Keerthya00b95c2019-07-09 10:30:34 +0530740 int ret = 0;
741
Vignesh Raghavendrabbedbbb2021-12-24 12:55:30 +0530742 dev_read_u32(dev, "reg", &priv->port_id);
743
Marek Behúnbc194772022-04-07 00:33:01 +0200744 pdata->phy_interface = dev_read_phy_mode(dev);
Marek Behún48631e42022-04-07 00:33:03 +0200745 if (pdata->phy_interface == PHY_INTERFACE_MODE_NA) {
Marek Behúnbc194772022-04-07 00:33:01 +0200746 dev_err(dev, "Invalid PHY mode, port %u\n", priv->port_id);
747 return -EINVAL;
Keerthya00b95c2019-07-09 10:30:34 +0530748 }
749
Vignesh Raghavendrabbedbbb2021-12-24 12:55:30 +0530750 dev_read_u32(dev, "max-speed", (u32 *)&pdata->max_speed);
Keerthya00b95c2019-07-09 10:30:34 +0530751 if (pdata->max_speed)
752 dev_err(dev, "Port %u speed froced to %uMbit\n",
753 priv->port_id, pdata->max_speed);
754
755 priv->has_phy = true;
Vignesh Raghavendrabbedbbb2021-12-24 12:55:30 +0530756 ret = ofnode_parse_phandle_with_args(dev_ofnode(dev), "phy-handle",
Keerthya00b95c2019-07-09 10:30:34 +0530757 NULL, 0, 0, &out_args);
758 if (ret) {
759 dev_err(dev, "can't parse phy-handle port %u (%d)\n",
760 priv->port_id, ret);
761 priv->has_phy = false;
762 ret = 0;
763 }
764
765 priv->phy_node = out_args.node;
766 if (priv->has_phy) {
767 ret = ofnode_read_u32(priv->phy_node, "reg", &priv->phy_addr);
768 if (ret) {
769 dev_err(dev, "failed to get phy_addr port %u (%d)\n",
770 priv->port_id, ret);
771 goto out;
772 }
773 }
774
775out:
776 return ret;
777}
778
Vignesh Raghavendrabbedbbb2021-12-24 12:55:30 +0530779static int am65_cpsw_port_probe(struct udevice *dev)
Keerthya00b95c2019-07-09 10:30:34 +0530780{
781 struct am65_cpsw_priv *priv = dev_get_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700782 struct eth_pdata *pdata = dev_get_plat(dev);
Keerthya00b95c2019-07-09 10:30:34 +0530783 struct am65_cpsw_common *cpsw_common;
Vignesh Raghavendrabbedbbb2021-12-24 12:55:30 +0530784 char portname[15];
785 int ret;
Keerthya00b95c2019-07-09 10:30:34 +0530786
787 priv->dev = dev;
788
Vignesh Raghavendrabbedbbb2021-12-24 12:55:30 +0530789 cpsw_common = dev_get_priv(dev->parent);
Keerthya00b95c2019-07-09 10:30:34 +0530790 priv->cpsw_common = cpsw_common;
791
Vignesh Raghavendrabbedbbb2021-12-24 12:55:30 +0530792 sprintf(portname, "%s%s", dev->parent->name, dev->name);
793 device_set_name(dev, portname);
794
Ravi Gunasekaran1eb61912022-09-22 15:21:24 +0530795 priv->mdio_manual_mode = false;
796 if (soc_device_match(k3_mdio_soc_data))
797 priv->mdio_manual_mode = true;
798
Vignesh Raghavendrabbedbbb2021-12-24 12:55:30 +0530799 ret = am65_cpsw_ofdata_parse_phy(dev);
800 if (ret)
801 goto out;
802
Roger Quadrosbe0619b2023-07-22 22:31:49 +0300803 ret = am65_cpsw_gmii_sel_k3(priv, pdata->phy_interface);
804 if (ret)
805 goto out;
Vignesh Raghavendrabbedbbb2021-12-24 12:55:30 +0530806
807 ret = am65_cpsw_mdio_init(dev);
808 if (ret)
809 goto out;
810
811 ret = am65_cpsw_phy_init(dev);
812 if (ret)
813 goto out;
814out:
815 return ret;
816}
817
818static int am65_cpsw_probe_nuss(struct udevice *dev)
819{
820 struct am65_cpsw_common *cpsw_common = dev_get_priv(dev);
821 ofnode ports_np, node;
822 int ret, i;
823 struct udevice *port_dev;
824
Keerthya00b95c2019-07-09 10:30:34 +0530825 cpsw_common->dev = dev;
826 cpsw_common->ss_base = dev_read_addr(dev);
827 if (cpsw_common->ss_base == FDT_ADDR_T_NONE)
828 return -EINVAL;
Keerthya00b95c2019-07-09 10:30:34 +0530829
830 ret = power_domain_get_by_index(dev, &cpsw_common->pwrdmn, 0);
831 if (ret) {
832 dev_err(dev, "failed to get pwrdmn: %d\n", ret);
833 return ret;
834 }
835
836 ret = clk_get_by_name(dev, "fck", &cpsw_common->fclk);
837 if (ret) {
838 power_domain_free(&cpsw_common->pwrdmn);
839 dev_err(dev, "failed to get clock %d\n", ret);
840 return ret;
841 }
842
843 cpsw_common->cpsw_base = cpsw_common->ss_base + AM65_CPSW_CPSW_NU_BASE;
844 cpsw_common->ale_base = cpsw_common->cpsw_base +
845 AM65_CPSW_CPSW_NU_ALE_BASE;
846 cpsw_common->mdio_base = cpsw_common->ss_base + AM65_CPSW_MDIO_BASE;
847
Vignesh Raghavendra2b834d02020-07-06 13:36:54 +0530848 ports_np = dev_read_subnode(dev, "ethernet-ports");
Keerthya00b95c2019-07-09 10:30:34 +0530849 if (!ofnode_valid(ports_np)) {
850 ret = -ENOENT;
851 goto out;
852 }
853
854 ofnode_for_each_subnode(node, ports_np) {
855 const char *node_name;
856 u32 port_id;
857 bool disabled;
858
859 node_name = ofnode_get_name(node);
860
Simon Glass2e4938b2022-09-06 20:27:17 -0600861 disabled = !ofnode_is_enabled(node);
Keerthya00b95c2019-07-09 10:30:34 +0530862
863 ret = ofnode_read_u32(node, "reg", &port_id);
864 if (ret) {
865 dev_err(dev, "%s: failed to get port_id (%d)\n",
866 node_name, ret);
867 goto out;
868 }
869
870 if (port_id >= AM65_CPSW_CPSWNU_MAX_PORTS) {
871 dev_err(dev, "%s: invalid port_id (%d)\n",
872 node_name, port_id);
873 ret = -EINVAL;
874 goto out;
875 }
876 cpsw_common->port_num++;
877
878 if (!port_id)
879 continue;
880
Keerthya00b95c2019-07-09 10:30:34 +0530881 cpsw_common->ports[port_id].disabled = disabled;
882 if (disabled)
883 continue;
884
Vignesh Raghavendrabbedbbb2021-12-24 12:55:30 +0530885 ret = device_bind_driver_to_node(dev, "am65_cpsw_nuss_port", ofnode_get_name(node), node, &port_dev);
Keerthya00b95c2019-07-09 10:30:34 +0530886 if (ret)
Vignesh Raghavendrad26ac2e2022-01-21 12:47:51 +0530887 dev_err(dev, "Failed to bind to %s node\n", ofnode_get_name(node));
Keerthya00b95c2019-07-09 10:30:34 +0530888 }
889
890 for (i = 0; i < AM65_CPSW_CPSWNU_MAX_PORTS; i++) {
891 struct am65_cpsw_port *port = &cpsw_common->ports[i];
892
893 port->port_base = cpsw_common->cpsw_base +
894 AM65_CPSW_CPSW_NU_PORTS_OFFSET +
895 (i * AM65_CPSW_CPSW_NU_PORTS_OFFSET);
Siddharth Vadapalli726fc0a2023-08-02 13:47:25 +0530896 port->port_sgmii_base = cpsw_common->ss_base +
897 (i * AM65_CPSW_SGMII_BASE);
Keerthya00b95c2019-07-09 10:30:34 +0530898 port->macsl_base = port->port_base +
899 AM65_CPSW_CPSW_NU_PORT_MACSL_OFFSET;
900 }
901
Keerthya00b95c2019-07-09 10:30:34 +0530902 cpsw_common->bus_freq =
903 dev_read_u32_default(dev, "bus_freq",
904 AM65_CPSW_MDIO_BUS_FREQ_DEF);
905
Vignesh Raghavendra462ff042019-12-04 22:17:22 +0530906 dev_info(dev, "K3 CPSW: nuss_ver: 0x%08X cpsw_ver: 0x%08X ale_ver: 0x%08X Ports:%u mdio_freq:%u\n",
Keerthya00b95c2019-07-09 10:30:34 +0530907 readl(cpsw_common->ss_base),
908 readl(cpsw_common->cpsw_base),
909 readl(cpsw_common->ale_base),
910 cpsw_common->port_num,
Keerthya00b95c2019-07-09 10:30:34 +0530911 cpsw_common->bus_freq);
912
913out:
914 clk_free(&cpsw_common->fclk);
915 power_domain_free(&cpsw_common->pwrdmn);
916 return ret;
917}
918
919static const struct udevice_id am65_cpsw_nuss_ids[] = {
920 { .compatible = "ti,am654-cpsw-nuss" },
Vignesh Raghavendra30bc6ea2019-12-04 22:17:23 +0530921 { .compatible = "ti,j721e-cpsw-nuss" },
Vignesh Raghavendra1cc35622021-05-10 20:06:11 +0530922 { .compatible = "ti,am642-cpsw-nuss" },
Keerthya00b95c2019-07-09 10:30:34 +0530923 { }
924};
925
Vignesh Raghavendrabbedbbb2021-12-24 12:55:30 +0530926U_BOOT_DRIVER(am65_cpsw_nuss) = {
927 .name = "am65_cpsw_nuss",
928 .id = UCLASS_MISC,
Keerthya00b95c2019-07-09 10:30:34 +0530929 .of_match = am65_cpsw_nuss_ids,
Vignesh Raghavendrabbedbbb2021-12-24 12:55:30 +0530930 .probe = am65_cpsw_probe_nuss,
931 .priv_auto = sizeof(struct am65_cpsw_common),
932};
933
934U_BOOT_DRIVER(am65_cpsw_nuss_port) = {
935 .name = "am65_cpsw_nuss_port",
936 .id = UCLASS_ETH,
937 .probe = am65_cpsw_port_probe,
Keerthya00b95c2019-07-09 10:30:34 +0530938 .ops = &am65_cpsw_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700939 .priv_auto = sizeof(struct am65_cpsw_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -0700940 .plat_auto = sizeof(struct eth_pdata),
Vignesh Raghavendra198bbb12022-01-28 11:21:19 +0530941 .flags = DM_FLAG_ALLOC_PRIV_DMA | DM_FLAG_OS_PREPARE,
Keerthya00b95c2019-07-09 10:30:34 +0530942};
Maxime Ripard028849d2023-07-24 15:57:30 +0200943
944static const struct udevice_id am65_cpsw_mdio_ids[] = {
945 { .compatible = "ti,cpsw-mdio" },
946 { }
947};
948
949U_BOOT_DRIVER(am65_cpsw_mdio) = {
950 .name = "am65_cpsw_mdio",
951 .id = UCLASS_MDIO,
952 .of_match = am65_cpsw_mdio_ids,
953};