blob: 5bc6fa08366b5ef05c36c8d97e7c3231dd62637b [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
60#define AM65_CPSW_ALE_CTL_REG 0x8
61#define AM65_CPSW_ALE_CTL_REG_ENABLE BIT(31)
62#define AM65_CPSW_ALE_CTL_REG_RESET_TBL BIT(30)
63#define AM65_CPSW_ALE_CTL_REG_BYPASS BIT(4)
64#define AM65_CPSW_ALE_PN_CTL_REG(x) (0x40 + (x) * 4)
65#define AM65_CPSW_ALE_PN_CTL_REG_MODE_FORWARD 0x3
66#define AM65_CPSW_ALE_PN_CTL_REG_MAC_ONLY BIT(11)
67
Vignesh Raghavendra5cb8a0f2020-07-06 13:36:53 +053068#define AM65_CPSW_ALE_THREADMAPDEF_REG 0x134
69#define AM65_CPSW_ALE_DEFTHREAD_EN BIT(15)
70
Keerthya00b95c2019-07-09 10:30:34 +053071#define AM65_CPSW_MACSL_CTL_REG 0x0
72#define AM65_CPSW_MACSL_CTL_REG_IFCTL_A BIT(15)
Murali Karicheri6565e902020-04-17 11:12:09 -040073#define AM65_CPSW_MACSL_CTL_EXT_EN BIT(18)
Keerthya00b95c2019-07-09 10:30:34 +053074#define AM65_CPSW_MACSL_CTL_REG_GIG BIT(7)
75#define AM65_CPSW_MACSL_CTL_REG_GMII_EN BIT(5)
76#define AM65_CPSW_MACSL_CTL_REG_LOOPBACK BIT(1)
77#define AM65_CPSW_MACSL_CTL_REG_FULL_DUPLEX BIT(0)
78#define AM65_CPSW_MACSL_RESET_REG 0x8
79#define AM65_CPSW_MACSL_RESET_REG_RESET BIT(0)
80#define AM65_CPSW_MACSL_STATUS_REG 0x4
81#define AM65_CPSW_MACSL_RESET_REG_PN_IDLE BIT(31)
82#define AM65_CPSW_MACSL_RESET_REG_PN_E_IDLE BIT(30)
83#define AM65_CPSW_MACSL_RESET_REG_PN_P_IDLE BIT(29)
84#define AM65_CPSW_MACSL_RESET_REG_PN_TX_IDLE BIT(28)
85#define AM65_CPSW_MACSL_RESET_REG_IDLE_MASK \
86 (AM65_CPSW_MACSL_RESET_REG_PN_IDLE | \
87 AM65_CPSW_MACSL_RESET_REG_PN_E_IDLE | \
88 AM65_CPSW_MACSL_RESET_REG_PN_P_IDLE | \
89 AM65_CPSW_MACSL_RESET_REG_PN_TX_IDLE)
90
91#define AM65_CPSW_CPPI_PKT_TYPE 0x7
92
93struct am65_cpsw_port {
94 fdt_addr_t port_base;
95 fdt_addr_t macsl_base;
96 bool disabled;
97 u32 mac_control;
98};
99
100struct am65_cpsw_common {
101 struct udevice *dev;
102 fdt_addr_t ss_base;
103 fdt_addr_t cpsw_base;
104 fdt_addr_t mdio_base;
105 fdt_addr_t ale_base;
106 fdt_addr_t gmii_sel;
Keerthya00b95c2019-07-09 10:30:34 +0530107
108 struct clk fclk;
109 struct power_domain pwrdmn;
110
111 u32 port_num;
112 struct am65_cpsw_port ports[AM65_CPSW_CPSWNU_MAX_PORTS];
Keerthya00b95c2019-07-09 10:30:34 +0530113
114 struct mii_dev *bus;
115 u32 bus_freq;
116
117 struct dma dma_tx;
118 struct dma dma_rx;
119 u32 rx_next;
120 u32 rx_pend;
121 bool started;
122};
123
124struct am65_cpsw_priv {
125 struct udevice *dev;
126 struct am65_cpsw_common *cpsw_common;
127 u32 port_id;
128
129 struct phy_device *phydev;
130 bool has_phy;
131 ofnode phy_node;
132 u32 phy_addr;
Ravi Gunasekaran1eb61912022-09-22 15:21:24 +0530133
134 bool mdio_manual_mode;
Keerthya00b95c2019-07-09 10:30:34 +0530135};
136
137#ifdef PKTSIZE_ALIGN
138#define UDMA_RX_BUF_SIZE PKTSIZE_ALIGN
139#else
140#define UDMA_RX_BUF_SIZE ALIGN(1522, ARCH_DMA_MINALIGN)
141#endif
142
143#ifdef PKTBUFSRX
144#define UDMA_RX_DESC_NUM PKTBUFSRX
145#else
146#define UDMA_RX_DESC_NUM 4
147#endif
148
149#define mac_hi(mac) (((mac)[0] << 0) | ((mac)[1] << 8) | \
150 ((mac)[2] << 16) | ((mac)[3] << 24))
151#define mac_lo(mac) (((mac)[4] << 0) | ((mac)[5] << 8))
152
153static void am65_cpsw_set_sl_mac(struct am65_cpsw_port *slave,
154 unsigned char *addr)
155{
156 writel(mac_hi(addr),
157 slave->port_base + AM65_CPSW_PN_REG_SA_H);
158 writel(mac_lo(addr),
159 slave->port_base + AM65_CPSW_PN_REG_SA_L);
160}
161
162int am65_cpsw_macsl_reset(struct am65_cpsw_port *slave)
163{
164 u32 i = 100;
165
166 /* Set the soft reset bit */
167 writel(AM65_CPSW_MACSL_RESET_REG_RESET,
168 slave->macsl_base + AM65_CPSW_MACSL_RESET_REG);
169
170 while ((readl(slave->macsl_base + AM65_CPSW_MACSL_RESET_REG) &
171 AM65_CPSW_MACSL_RESET_REG_RESET) && i--)
172 cpu_relax();
173
174 /* Timeout on the reset */
175 return i;
176}
177
178static int am65_cpsw_macsl_wait_for_idle(struct am65_cpsw_port *slave)
179{
180 u32 i = 100;
181
182 while ((readl(slave->macsl_base + AM65_CPSW_MACSL_STATUS_REG) &
183 AM65_CPSW_MACSL_RESET_REG_IDLE_MASK) && i--)
184 cpu_relax();
185
186 return i;
187}
188
189static int am65_cpsw_update_link(struct am65_cpsw_priv *priv)
190{
191 struct am65_cpsw_common *common = priv->cpsw_common;
192 struct am65_cpsw_port *port = &common->ports[priv->port_id];
193 struct phy_device *phy = priv->phydev;
194 u32 mac_control = 0;
195
196 if (phy->link) { /* link up */
197 mac_control = /*AM65_CPSW_MACSL_CTL_REG_LOOPBACK |*/
198 AM65_CPSW_MACSL_CTL_REG_GMII_EN;
199 if (phy->speed == 1000)
200 mac_control |= AM65_CPSW_MACSL_CTL_REG_GIG;
Murali Karicheri6565e902020-04-17 11:12:09 -0400201 if (phy->speed == 10 && phy_interface_is_rgmii(phy))
202 /* Can be used with in band mode only */
203 mac_control |= AM65_CPSW_MACSL_CTL_EXT_EN;
Keerthya00b95c2019-07-09 10:30:34 +0530204 if (phy->duplex == DUPLEX_FULL)
205 mac_control |= AM65_CPSW_MACSL_CTL_REG_FULL_DUPLEX;
206 if (phy->speed == 100)
207 mac_control |= AM65_CPSW_MACSL_CTL_REG_IFCTL_A;
208 }
209
210 if (mac_control == port->mac_control)
211 goto out;
212
213 if (mac_control) {
214 printf("link up on port %d, speed %d, %s duplex\n",
215 priv->port_id, phy->speed,
216 (phy->duplex == DUPLEX_FULL) ? "full" : "half");
217 } else {
218 printf("link down on port %d\n", priv->port_id);
219 }
220
221 writel(mac_control, port->macsl_base + AM65_CPSW_MACSL_CTL_REG);
222 port->mac_control = mac_control;
223
224out:
225 return phy->link;
226}
227
Andreas Dannenberg1dc2ee62023-06-14 17:28:53 -0500228#define AM65_GMII_SEL_PORT_OFFS(x) (0x4 * ((x) - 1))
229
Keerthya00b95c2019-07-09 10:30:34 +0530230#define AM65_GMII_SEL_MODE_MII 0
231#define AM65_GMII_SEL_MODE_RMII 1
232#define AM65_GMII_SEL_MODE_RGMII 2
233
234#define AM65_GMII_SEL_RGMII_IDMODE BIT(4)
235
236static void am65_cpsw_gmii_sel_k3(struct am65_cpsw_priv *priv,
237 phy_interface_t phy_mode, int slave)
238{
239 struct am65_cpsw_common *common = priv->cpsw_common;
Andreas Dannenberg1dc2ee62023-06-14 17:28:53 -0500240 fdt_addr_t gmii_sel = common->gmii_sel + AM65_GMII_SEL_PORT_OFFS(slave);
Keerthya00b95c2019-07-09 10:30:34 +0530241 u32 reg;
242 u32 mode = 0;
243 bool rgmii_id = false;
244
Andreas Dannenberg1dc2ee62023-06-14 17:28:53 -0500245 reg = readl(gmii_sel);
Keerthya00b95c2019-07-09 10:30:34 +0530246
247 dev_dbg(common->dev, "old gmii_sel: %08x\n", reg);
248
249 switch (phy_mode) {
250 case PHY_INTERFACE_MODE_RMII:
251 mode = AM65_GMII_SEL_MODE_RMII;
252 break;
253
254 case PHY_INTERFACE_MODE_RGMII:
Grygorii Strashkobf45d9b2019-09-19 11:16:41 +0300255 case PHY_INTERFACE_MODE_RGMII_RXID:
Keerthya00b95c2019-07-09 10:30:34 +0530256 mode = AM65_GMII_SEL_MODE_RGMII;
257 break;
258
259 case PHY_INTERFACE_MODE_RGMII_ID:
Keerthya00b95c2019-07-09 10:30:34 +0530260 case PHY_INTERFACE_MODE_RGMII_TXID:
261 mode = AM65_GMII_SEL_MODE_RGMII;
262 rgmii_id = true;
263 break;
264
265 default:
266 dev_warn(common->dev,
267 "Unsupported PHY mode: %u. Defaulting to MII.\n",
268 phy_mode);
269 /* fallthrough */
270 case PHY_INTERFACE_MODE_MII:
271 mode = AM65_GMII_SEL_MODE_MII;
272 break;
273 };
274
275 if (rgmii_id)
276 mode |= AM65_GMII_SEL_RGMII_IDMODE;
277
278 reg = mode;
279 dev_dbg(common->dev, "gmii_sel PHY mode: %u, new gmii_sel: %08x\n",
280 phy_mode, reg);
Andreas Dannenberg1dc2ee62023-06-14 17:28:53 -0500281 writel(reg, gmii_sel);
Keerthya00b95c2019-07-09 10:30:34 +0530282
Andreas Dannenberg1dc2ee62023-06-14 17:28:53 -0500283 reg = readl(gmii_sel);
Keerthya00b95c2019-07-09 10:30:34 +0530284 if (reg != mode)
285 dev_err(common->dev,
286 "gmii_sel PHY mode NOT SET!: requested: %08x, gmii_sel: %08x\n",
287 mode, reg);
288}
289
290static int am65_cpsw_start(struct udevice *dev)
291{
Simon Glassfa20e932020-12-03 16:55:20 -0700292 struct eth_pdata *pdata = dev_get_plat(dev);
Keerthya00b95c2019-07-09 10:30:34 +0530293 struct am65_cpsw_priv *priv = dev_get_priv(dev);
294 struct am65_cpsw_common *common = priv->cpsw_common;
295 struct am65_cpsw_port *port = &common->ports[priv->port_id];
296 struct am65_cpsw_port *port0 = &common->ports[0];
Vignesh Raghavendra462ff042019-12-04 22:17:22 +0530297 struct ti_udma_drv_chan_cfg_data *dma_rx_cfg_data;
Keerthya00b95c2019-07-09 10:30:34 +0530298 int ret, i;
299
300 ret = power_domain_on(&common->pwrdmn);
301 if (ret) {
302 dev_err(dev, "power_domain_on() failed %d\n", ret);
303 goto out;
304 }
305
306 ret = clk_enable(&common->fclk);
307 if (ret) {
308 dev_err(dev, "clk enabled failed %d\n", ret);
309 goto err_off_pwrdm;
310 }
311
312 common->rx_next = 0;
313 common->rx_pend = 0;
314 ret = dma_get_by_name(common->dev, "tx0", &common->dma_tx);
315 if (ret) {
316 dev_err(dev, "TX dma get failed %d\n", ret);
317 goto err_off_clk;
318 }
319 ret = dma_get_by_name(common->dev, "rx", &common->dma_rx);
320 if (ret) {
321 dev_err(dev, "RX dma get failed %d\n", ret);
322 goto err_free_tx;
323 }
324
325 for (i = 0; i < UDMA_RX_DESC_NUM; i++) {
326 ret = dma_prepare_rcv_buf(&common->dma_rx,
327 net_rx_packets[i],
328 UDMA_RX_BUF_SIZE);
329 if (ret) {
330 dev_err(dev, "RX dma add buf failed %d\n", ret);
331 goto err_free_tx;
332 }
333 }
334
335 ret = dma_enable(&common->dma_tx);
336 if (ret) {
337 dev_err(dev, "TX dma_enable failed %d\n", ret);
338 goto err_free_rx;
339 }
340 ret = dma_enable(&common->dma_rx);
341 if (ret) {
342 dev_err(dev, "RX dma_enable failed %d\n", ret);
343 goto err_dis_tx;
344 }
345
346 /* Control register */
347 writel(AM65_CPSW_CTL_REG_P0_ENABLE |
348 AM65_CPSW_CTL_REG_P0_TX_CRC_REMOVE |
349 AM65_CPSW_CTL_REG_P0_RX_PAD,
350 common->cpsw_base + AM65_CPSW_CTL_REG);
351
352 /* disable priority elevation */
353 writel(0, common->cpsw_base + AM65_CPSW_PTYPE_REG);
354
355 /* enable statistics */
356 writel(BIT(0) | BIT(priv->port_id),
357 common->cpsw_base + AM65_CPSW_STAT_PORT_EN_REG);
358
359 /* Port 0 length register */
360 writel(PKTSIZE_ALIGN, port0->port_base + AM65_CPSW_PN_RX_MAXLEN_REG);
361
362 /* set base flow_id */
Vignesh Raghavendra462ff042019-12-04 22:17:22 +0530363 dma_get_cfg(&common->dma_rx, 0, (void **)&dma_rx_cfg_data);
364 writel(dma_rx_cfg_data->flow_id_base,
Keerthya00b95c2019-07-09 10:30:34 +0530365 port0->port_base + AM65_CPSW_P0_FLOW_ID_REG);
Vignesh Raghavendra462ff042019-12-04 22:17:22 +0530366 dev_info(dev, "K3 CPSW: rflow_id_base: %u\n",
367 dma_rx_cfg_data->flow_id_base);
Keerthya00b95c2019-07-09 10:30:34 +0530368
369 /* Reset and enable the ALE */
370 writel(AM65_CPSW_ALE_CTL_REG_ENABLE | AM65_CPSW_ALE_CTL_REG_RESET_TBL |
371 AM65_CPSW_ALE_CTL_REG_BYPASS,
372 common->ale_base + AM65_CPSW_ALE_CTL_REG);
373
374 /* port 0 put into forward mode */
375 writel(AM65_CPSW_ALE_PN_CTL_REG_MODE_FORWARD,
376 common->ale_base + AM65_CPSW_ALE_PN_CTL_REG(0));
377
Vignesh Raghavendra5cb8a0f2020-07-06 13:36:53 +0530378 writel(AM65_CPSW_ALE_DEFTHREAD_EN,
379 common->ale_base + AM65_CPSW_ALE_THREADMAPDEF_REG);
380
Keerthya00b95c2019-07-09 10:30:34 +0530381 /* PORT x configuration */
382
383 /* Port x Max length register */
384 writel(PKTSIZE_ALIGN, port->port_base + AM65_CPSW_PN_RX_MAXLEN_REG);
385
386 /* Port x set mac */
387 am65_cpsw_set_sl_mac(port, pdata->enetaddr);
388
389 /* Port x ALE: mac_only, Forwarding */
390 writel(AM65_CPSW_ALE_PN_CTL_REG_MAC_ONLY |
391 AM65_CPSW_ALE_PN_CTL_REG_MODE_FORWARD,
392 common->ale_base + AM65_CPSW_ALE_PN_CTL_REG(priv->port_id));
393
394 port->mac_control = 0;
395 if (!am65_cpsw_macsl_reset(port)) {
396 dev_err(dev, "mac_sl reset failed\n");
397 ret = -EFAULT;
398 goto err_dis_rx;
399 }
400
401 ret = phy_startup(priv->phydev);
402 if (ret) {
403 dev_err(dev, "phy_startup failed\n");
404 goto err_dis_rx;
405 }
406
407 ret = am65_cpsw_update_link(priv);
408 if (!ret) {
409 ret = -ENODEV;
410 goto err_phy_shutdown;
411 }
412
413 common->started = true;
414
415 return 0;
416
417err_phy_shutdown:
418 phy_shutdown(priv->phydev);
419err_dis_rx:
420 /* disable ports */
421 writel(0, common->ale_base + AM65_CPSW_ALE_PN_CTL_REG(priv->port_id));
422 writel(0, common->ale_base + AM65_CPSW_ALE_PN_CTL_REG(0));
423 if (!am65_cpsw_macsl_wait_for_idle(port))
424 dev_err(dev, "mac_sl idle timeout\n");
425 writel(0, port->macsl_base + AM65_CPSW_MACSL_CTL_REG);
426 writel(0, common->ale_base + AM65_CPSW_ALE_CTL_REG);
427 writel(0, common->cpsw_base + AM65_CPSW_CTL_REG);
428
429 dma_disable(&common->dma_rx);
430err_dis_tx:
431 dma_disable(&common->dma_tx);
432err_free_rx:
433 dma_free(&common->dma_rx);
434err_free_tx:
435 dma_free(&common->dma_tx);
436err_off_clk:
437 clk_disable(&common->fclk);
438err_off_pwrdm:
439 power_domain_off(&common->pwrdmn);
440out:
441 dev_err(dev, "%s end error\n", __func__);
442
443 return ret;
444}
445
446static int am65_cpsw_send(struct udevice *dev, void *packet, int length)
447{
448 struct am65_cpsw_priv *priv = dev_get_priv(dev);
449 struct am65_cpsw_common *common = priv->cpsw_common;
450 struct ti_udma_drv_packet_data packet_data;
451 int ret;
452
453 packet_data.pkt_type = AM65_CPSW_CPPI_PKT_TYPE;
454 packet_data.dest_tag = priv->port_id;
455 ret = dma_send(&common->dma_tx, packet, length, &packet_data);
456 if (ret) {
457 dev_err(dev, "TX dma_send failed %d\n", ret);
458 return ret;
459 }
460
461 return 0;
462}
463
464static int am65_cpsw_recv(struct udevice *dev, int flags, uchar **packetp)
465{
466 struct am65_cpsw_priv *priv = dev_get_priv(dev);
467 struct am65_cpsw_common *common = priv->cpsw_common;
468
469 /* try to receive a new packet */
470 return dma_receive(&common->dma_rx, (void **)packetp, NULL);
471}
472
473static int am65_cpsw_free_pkt(struct udevice *dev, uchar *packet, int length)
474{
475 struct am65_cpsw_priv *priv = dev_get_priv(dev);
476 struct am65_cpsw_common *common = priv->cpsw_common;
477 int ret;
478
479 if (length > 0) {
480 u32 pkt = common->rx_next % UDMA_RX_DESC_NUM;
481
482 ret = dma_prepare_rcv_buf(&common->dma_rx,
483 net_rx_packets[pkt],
484 UDMA_RX_BUF_SIZE);
485 if (ret)
486 dev_err(dev, "RX dma free_pkt failed %d\n", ret);
487 common->rx_next++;
488 }
489
490 return 0;
491}
492
493static void am65_cpsw_stop(struct udevice *dev)
494{
495 struct am65_cpsw_priv *priv = dev_get_priv(dev);
496 struct am65_cpsw_common *common = priv->cpsw_common;
497 struct am65_cpsw_port *port = &common->ports[priv->port_id];
498
499 if (!common->started)
500 return;
501
502 phy_shutdown(priv->phydev);
503
504 writel(0, common->ale_base + AM65_CPSW_ALE_PN_CTL_REG(priv->port_id));
505 writel(0, common->ale_base + AM65_CPSW_ALE_PN_CTL_REG(0));
506 if (!am65_cpsw_macsl_wait_for_idle(port))
507 dev_err(dev, "mac_sl idle timeout\n");
508 writel(0, port->macsl_base + AM65_CPSW_MACSL_CTL_REG);
509 writel(0, common->ale_base + AM65_CPSW_ALE_CTL_REG);
510 writel(0, common->cpsw_base + AM65_CPSW_CTL_REG);
511
512 dma_disable(&common->dma_tx);
513 dma_free(&common->dma_tx);
514
515 dma_disable(&common->dma_rx);
516 dma_free(&common->dma_rx);
517
518 common->started = false;
519}
520
Roger Quadroscb8f8ad2023-07-22 22:31:48 +0300521static int am65_cpsw_am654_get_efuse_macid(struct udevice *dev,
522 int slave, u8 *mac_addr)
523{
524 u32 mac_lo, mac_hi, offset;
525 struct regmap *syscon;
526 int ret;
527
528 syscon = syscon_regmap_lookup_by_phandle(dev, "ti,syscon-efuse");
529 if (IS_ERR(syscon)) {
530 if (PTR_ERR(syscon) == -ENODEV)
531 return 0;
532 return PTR_ERR(syscon);
533 }
534
535 ret = dev_read_u32_index(dev, "ti,syscon-efuse", 1, &offset);
536 if (ret)
537 return ret;
538
539 regmap_read(syscon, offset, &mac_lo);
540 regmap_read(syscon, offset + 4, &mac_hi);
541
542 mac_addr[0] = (mac_hi >> 8) & 0xff;
543 mac_addr[1] = mac_hi & 0xff;
544 mac_addr[2] = (mac_lo >> 24) & 0xff;
545 mac_addr[3] = (mac_lo >> 16) & 0xff;
546 mac_addr[4] = (mac_lo >> 8) & 0xff;
547 mac_addr[5] = mac_lo & 0xff;
548
549 return 0;
550}
551
Keerthya00b95c2019-07-09 10:30:34 +0530552static int am65_cpsw_read_rom_hwaddr(struct udevice *dev)
553{
554 struct am65_cpsw_priv *priv = dev_get_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700555 struct eth_pdata *pdata = dev_get_plat(dev);
Keerthya00b95c2019-07-09 10:30:34 +0530556
Roger Quadroscb8f8ad2023-07-22 22:31:48 +0300557 am65_cpsw_am654_get_efuse_macid(dev,
558 priv->port_id,
559 pdata->enetaddr);
Keerthya00b95c2019-07-09 10:30:34 +0530560
561 return 0;
562}
563
564static const struct eth_ops am65_cpsw_ops = {
565 .start = am65_cpsw_start,
566 .send = am65_cpsw_send,
567 .recv = am65_cpsw_recv,
568 .free_pkt = am65_cpsw_free_pkt,
569 .stop = am65_cpsw_stop,
570 .read_rom_hwaddr = am65_cpsw_read_rom_hwaddr,
571};
572
Ravi Gunasekaran1eb61912022-09-22 15:21:24 +0530573static const struct soc_attr k3_mdio_soc_data[] = {
574 { .family = "AM62X", .revision = "SR1.0" },
575 { .family = "AM64X", .revision = "SR1.0" },
576 { .family = "AM64X", .revision = "SR2.0" },
577 { .family = "AM65X", .revision = "SR1.0" },
578 { .family = "AM65X", .revision = "SR2.0" },
579 { .family = "J7200", .revision = "SR1.0" },
580 { .family = "J7200", .revision = "SR2.0" },
581 { .family = "J721E", .revision = "SR1.0" },
582 { .family = "J721E", .revision = "SR1.1" },
583 { .family = "J721S2", .revision = "SR1.0" },
584 { /* sentinel */ },
585};
586
Maxime Ripard028849d2023-07-24 15:57:30 +0200587static ofnode am65_cpsw_find_mdio(ofnode parent)
588{
589 ofnode node;
590
591 ofnode_for_each_subnode(node, parent)
592 if (ofnode_device_is_compatible(node, "ti,cpsw-mdio"))
593 return node;
594
595 return ofnode_null();
596}
597
598static int am65_cpsw_mdio_setup(struct udevice *dev)
599{
600 struct am65_cpsw_priv *priv = dev_get_priv(dev);
601 struct am65_cpsw_common *cpsw_common = priv->cpsw_common;
602 struct udevice *mdio_dev;
603 ofnode mdio;
604 int ret;
605
606 mdio = am65_cpsw_find_mdio(dev_ofnode(cpsw_common->dev));
607 if (!ofnode_valid(mdio))
608 return 0;
609
610 /*
611 * The MDIO controller is represented in the DT binding by a
612 * subnode of the MAC controller.
613 *
614 * We don't have a DM driver for the MDIO device yet, and thus any
615 * pinctrl setting on its node will be ignored.
616 *
617 * However, we do need to make sure the pins states tied to the
618 * MDIO node are configured properly. Fortunately, the core DM
619 * does that for use when we get a device, so we can work around
620 * that whole issue by just requesting a dummy MDIO driver to
621 * probe, and our pins will get muxed.
622 */
623 ret = uclass_get_device_by_ofnode(UCLASS_MDIO, mdio, &mdio_dev);
624 if (ret)
625 return ret;
626
627 return 0;
628}
629
Keerthya00b95c2019-07-09 10:30:34 +0530630static int am65_cpsw_mdio_init(struct udevice *dev)
631{
632 struct am65_cpsw_priv *priv = dev_get_priv(dev);
633 struct am65_cpsw_common *cpsw_common = priv->cpsw_common;
Maxime Ripard028849d2023-07-24 15:57:30 +0200634 int ret;
Keerthya00b95c2019-07-09 10:30:34 +0530635
636 if (!priv->has_phy || cpsw_common->bus)
637 return 0;
638
Maxime Ripard028849d2023-07-24 15:57:30 +0200639 ret = am65_cpsw_mdio_setup(dev);
640 if (ret)
641 return ret;
642
Keerthya00b95c2019-07-09 10:30:34 +0530643 cpsw_common->bus = cpsw_mdio_init(dev->name,
644 cpsw_common->mdio_base,
645 cpsw_common->bus_freq,
Ravi Gunasekaran40cea492022-09-22 15:21:23 +0530646 clk_get_rate(&cpsw_common->fclk),
Ravi Gunasekaran1eb61912022-09-22 15:21:24 +0530647 priv->mdio_manual_mode);
Keerthya00b95c2019-07-09 10:30:34 +0530648 if (!cpsw_common->bus)
649 return -EFAULT;
650
651 return 0;
652}
653
654static int am65_cpsw_phy_init(struct udevice *dev)
655{
656 struct am65_cpsw_priv *priv = dev_get_priv(dev);
657 struct am65_cpsw_common *cpsw_common = priv->cpsw_common;
Simon Glassfa20e932020-12-03 16:55:20 -0700658 struct eth_pdata *pdata = dev_get_plat(dev);
Keerthya00b95c2019-07-09 10:30:34 +0530659 struct phy_device *phydev;
660 u32 supported = PHY_GBIT_FEATURES;
661 int ret;
662
663 phydev = phy_connect(cpsw_common->bus,
664 priv->phy_addr,
665 priv->dev,
666 pdata->phy_interface);
667
668 if (!phydev) {
669 dev_err(dev, "phy_connect() failed\n");
670 return -ENODEV;
671 }
672
673 phydev->supported &= supported;
674 if (pdata->max_speed) {
675 ret = phy_set_supported(phydev, pdata->max_speed);
676 if (ret)
677 return ret;
678 }
679 phydev->advertising = phydev->supported;
680
681 if (ofnode_valid(priv->phy_node))
682 phydev->node = priv->phy_node;
683
684 priv->phydev = phydev;
685 ret = phy_config(phydev);
686 if (ret < 0)
687 pr_err("phy_config() failed: %d", ret);
688
689 return ret;
690}
691
Vignesh Raghavendrabbedbbb2021-12-24 12:55:30 +0530692static int am65_cpsw_ofdata_parse_phy(struct udevice *dev)
Keerthya00b95c2019-07-09 10:30:34 +0530693{
Simon Glassfa20e932020-12-03 16:55:20 -0700694 struct eth_pdata *pdata = dev_get_plat(dev);
Keerthya00b95c2019-07-09 10:30:34 +0530695 struct am65_cpsw_priv *priv = dev_get_priv(dev);
696 struct ofnode_phandle_args out_args;
Keerthya00b95c2019-07-09 10:30:34 +0530697 int ret = 0;
698
Vignesh Raghavendrabbedbbb2021-12-24 12:55:30 +0530699 dev_read_u32(dev, "reg", &priv->port_id);
700
Marek Behúnbc194772022-04-07 00:33:01 +0200701 pdata->phy_interface = dev_read_phy_mode(dev);
Marek Behún48631e42022-04-07 00:33:03 +0200702 if (pdata->phy_interface == PHY_INTERFACE_MODE_NA) {
Marek Behúnbc194772022-04-07 00:33:01 +0200703 dev_err(dev, "Invalid PHY mode, port %u\n", priv->port_id);
704 return -EINVAL;
Keerthya00b95c2019-07-09 10:30:34 +0530705 }
706
Vignesh Raghavendrabbedbbb2021-12-24 12:55:30 +0530707 dev_read_u32(dev, "max-speed", (u32 *)&pdata->max_speed);
Keerthya00b95c2019-07-09 10:30:34 +0530708 if (pdata->max_speed)
709 dev_err(dev, "Port %u speed froced to %uMbit\n",
710 priv->port_id, pdata->max_speed);
711
712 priv->has_phy = true;
Vignesh Raghavendrabbedbbb2021-12-24 12:55:30 +0530713 ret = ofnode_parse_phandle_with_args(dev_ofnode(dev), "phy-handle",
Keerthya00b95c2019-07-09 10:30:34 +0530714 NULL, 0, 0, &out_args);
715 if (ret) {
716 dev_err(dev, "can't parse phy-handle port %u (%d)\n",
717 priv->port_id, ret);
718 priv->has_phy = false;
719 ret = 0;
720 }
721
722 priv->phy_node = out_args.node;
723 if (priv->has_phy) {
724 ret = ofnode_read_u32(priv->phy_node, "reg", &priv->phy_addr);
725 if (ret) {
726 dev_err(dev, "failed to get phy_addr port %u (%d)\n",
727 priv->port_id, ret);
728 goto out;
729 }
730 }
731
732out:
733 return ret;
734}
735
Vignesh Raghavendrabbedbbb2021-12-24 12:55:30 +0530736static int am65_cpsw_port_probe(struct udevice *dev)
Keerthya00b95c2019-07-09 10:30:34 +0530737{
738 struct am65_cpsw_priv *priv = dev_get_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700739 struct eth_pdata *pdata = dev_get_plat(dev);
Keerthya00b95c2019-07-09 10:30:34 +0530740 struct am65_cpsw_common *cpsw_common;
Vignesh Raghavendrabbedbbb2021-12-24 12:55:30 +0530741 char portname[15];
742 int ret;
Keerthya00b95c2019-07-09 10:30:34 +0530743
744 priv->dev = dev;
745
Vignesh Raghavendrabbedbbb2021-12-24 12:55:30 +0530746 cpsw_common = dev_get_priv(dev->parent);
Keerthya00b95c2019-07-09 10:30:34 +0530747 priv->cpsw_common = cpsw_common;
748
Vignesh Raghavendrabbedbbb2021-12-24 12:55:30 +0530749 sprintf(portname, "%s%s", dev->parent->name, dev->name);
750 device_set_name(dev, portname);
751
Ravi Gunasekaran1eb61912022-09-22 15:21:24 +0530752 priv->mdio_manual_mode = false;
753 if (soc_device_match(k3_mdio_soc_data))
754 priv->mdio_manual_mode = true;
755
Vignesh Raghavendrabbedbbb2021-12-24 12:55:30 +0530756 ret = am65_cpsw_ofdata_parse_phy(dev);
757 if (ret)
758 goto out;
759
760 am65_cpsw_gmii_sel_k3(priv, pdata->phy_interface, priv->port_id);
761
762 ret = am65_cpsw_mdio_init(dev);
763 if (ret)
764 goto out;
765
766 ret = am65_cpsw_phy_init(dev);
767 if (ret)
768 goto out;
769out:
770 return ret;
771}
772
773static int am65_cpsw_probe_nuss(struct udevice *dev)
774{
775 struct am65_cpsw_common *cpsw_common = dev_get_priv(dev);
776 ofnode ports_np, node;
777 int ret, i;
778 struct udevice *port_dev;
779
Keerthya00b95c2019-07-09 10:30:34 +0530780 cpsw_common->dev = dev;
781 cpsw_common->ss_base = dev_read_addr(dev);
782 if (cpsw_common->ss_base == FDT_ADDR_T_NONE)
783 return -EINVAL;
Keerthya00b95c2019-07-09 10:30:34 +0530784
785 ret = power_domain_get_by_index(dev, &cpsw_common->pwrdmn, 0);
786 if (ret) {
787 dev_err(dev, "failed to get pwrdmn: %d\n", ret);
788 return ret;
789 }
790
791 ret = clk_get_by_name(dev, "fck", &cpsw_common->fclk);
792 if (ret) {
793 power_domain_free(&cpsw_common->pwrdmn);
794 dev_err(dev, "failed to get clock %d\n", ret);
795 return ret;
796 }
797
798 cpsw_common->cpsw_base = cpsw_common->ss_base + AM65_CPSW_CPSW_NU_BASE;
799 cpsw_common->ale_base = cpsw_common->cpsw_base +
800 AM65_CPSW_CPSW_NU_ALE_BASE;
801 cpsw_common->mdio_base = cpsw_common->ss_base + AM65_CPSW_MDIO_BASE;
802
Vignesh Raghavendra2b834d02020-07-06 13:36:54 +0530803 ports_np = dev_read_subnode(dev, "ethernet-ports");
Keerthya00b95c2019-07-09 10:30:34 +0530804 if (!ofnode_valid(ports_np)) {
805 ret = -ENOENT;
806 goto out;
807 }
808
809 ofnode_for_each_subnode(node, ports_np) {
810 const char *node_name;
811 u32 port_id;
812 bool disabled;
813
814 node_name = ofnode_get_name(node);
815
Simon Glass2e4938b2022-09-06 20:27:17 -0600816 disabled = !ofnode_is_enabled(node);
Keerthya00b95c2019-07-09 10:30:34 +0530817
818 ret = ofnode_read_u32(node, "reg", &port_id);
819 if (ret) {
820 dev_err(dev, "%s: failed to get port_id (%d)\n",
821 node_name, ret);
822 goto out;
823 }
824
825 if (port_id >= AM65_CPSW_CPSWNU_MAX_PORTS) {
826 dev_err(dev, "%s: invalid port_id (%d)\n",
827 node_name, port_id);
828 ret = -EINVAL;
829 goto out;
830 }
831 cpsw_common->port_num++;
832
833 if (!port_id)
834 continue;
835
Keerthya00b95c2019-07-09 10:30:34 +0530836 cpsw_common->ports[port_id].disabled = disabled;
837 if (disabled)
838 continue;
839
Vignesh Raghavendrabbedbbb2021-12-24 12:55:30 +0530840 ret = device_bind_driver_to_node(dev, "am65_cpsw_nuss_port", ofnode_get_name(node), node, &port_dev);
Keerthya00b95c2019-07-09 10:30:34 +0530841 if (ret)
Vignesh Raghavendrad26ac2e2022-01-21 12:47:51 +0530842 dev_err(dev, "Failed to bind to %s node\n", ofnode_get_name(node));
Keerthya00b95c2019-07-09 10:30:34 +0530843 }
844
845 for (i = 0; i < AM65_CPSW_CPSWNU_MAX_PORTS; i++) {
846 struct am65_cpsw_port *port = &cpsw_common->ports[i];
847
848 port->port_base = cpsw_common->cpsw_base +
849 AM65_CPSW_CPSW_NU_PORTS_OFFSET +
850 (i * AM65_CPSW_CPSW_NU_PORTS_OFFSET);
851 port->macsl_base = port->port_base +
852 AM65_CPSW_CPSW_NU_PORT_MACSL_OFFSET;
853 }
854
855 node = dev_read_subnode(dev, "cpsw-phy-sel");
856 if (!ofnode_valid(node)) {
857 dev_err(dev, "can't find cpsw-phy-sel\n");
858 ret = -ENOENT;
859 goto out;
860 }
861
862 cpsw_common->gmii_sel = ofnode_get_addr(node);
863 if (cpsw_common->gmii_sel == FDT_ADDR_T_NONE) {
864 dev_err(dev, "failed to get gmii_sel base\n");
865 goto out;
866 }
867
Keerthya00b95c2019-07-09 10:30:34 +0530868 cpsw_common->bus_freq =
869 dev_read_u32_default(dev, "bus_freq",
870 AM65_CPSW_MDIO_BUS_FREQ_DEF);
871
Vignesh Raghavendra462ff042019-12-04 22:17:22 +0530872 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 +0530873 readl(cpsw_common->ss_base),
874 readl(cpsw_common->cpsw_base),
875 readl(cpsw_common->ale_base),
876 cpsw_common->port_num,
Keerthya00b95c2019-07-09 10:30:34 +0530877 cpsw_common->bus_freq);
878
879out:
880 clk_free(&cpsw_common->fclk);
881 power_domain_free(&cpsw_common->pwrdmn);
882 return ret;
883}
884
885static const struct udevice_id am65_cpsw_nuss_ids[] = {
886 { .compatible = "ti,am654-cpsw-nuss" },
Vignesh Raghavendra30bc6ea2019-12-04 22:17:23 +0530887 { .compatible = "ti,j721e-cpsw-nuss" },
Vignesh Raghavendra1cc35622021-05-10 20:06:11 +0530888 { .compatible = "ti,am642-cpsw-nuss" },
Keerthya00b95c2019-07-09 10:30:34 +0530889 { }
890};
891
Vignesh Raghavendrabbedbbb2021-12-24 12:55:30 +0530892U_BOOT_DRIVER(am65_cpsw_nuss) = {
893 .name = "am65_cpsw_nuss",
894 .id = UCLASS_MISC,
Keerthya00b95c2019-07-09 10:30:34 +0530895 .of_match = am65_cpsw_nuss_ids,
Vignesh Raghavendrabbedbbb2021-12-24 12:55:30 +0530896 .probe = am65_cpsw_probe_nuss,
897 .priv_auto = sizeof(struct am65_cpsw_common),
898};
899
900U_BOOT_DRIVER(am65_cpsw_nuss_port) = {
901 .name = "am65_cpsw_nuss_port",
902 .id = UCLASS_ETH,
903 .probe = am65_cpsw_port_probe,
Keerthya00b95c2019-07-09 10:30:34 +0530904 .ops = &am65_cpsw_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700905 .priv_auto = sizeof(struct am65_cpsw_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -0700906 .plat_auto = sizeof(struct eth_pdata),
Vignesh Raghavendra198bbb12022-01-28 11:21:19 +0530907 .flags = DM_FLAG_ALLOC_PRIV_DMA | DM_FLAG_OS_PREPARE,
Keerthya00b95c2019-07-09 10:30:34 +0530908};
Maxime Ripard028849d2023-07-24 15:57:30 +0200909
910static const struct udevice_id am65_cpsw_mdio_ids[] = {
911 { .compatible = "ti,cpsw-mdio" },
912 { }
913};
914
915U_BOOT_DRIVER(am65_cpsw_mdio) = {
916 .name = "am65_cpsw_mdio",
917 .id = UCLASS_MDIO,
918 .of_match = am65_cpsw_mdio_ids,
919};