blob: b0dd1b9bd35952efef98f7bb6feccccb210a2e8c [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>
Ravi Gunasekaran1eb61912022-09-22 15:21:24 +053025#include <soc.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060026#include <linux/bitops.h>
Keerthya00b95c2019-07-09 10:30:34 +053027#include <linux/soc/ti/ti-udma.h>
28
29#include "cpsw_mdio.h"
30
Vignesh Raghavendrac5a66132021-05-10 20:06:09 +053031#define AM65_CPSW_CPSWNU_MAX_PORTS 9
Keerthya00b95c2019-07-09 10:30:34 +053032
33#define AM65_CPSW_SS_BASE 0x0
34#define AM65_CPSW_SGMII_BASE 0x100
35#define AM65_CPSW_MDIO_BASE 0xf00
36#define AM65_CPSW_XGMII_BASE 0x2100
37#define AM65_CPSW_CPSW_NU_BASE 0x20000
38#define AM65_CPSW_CPSW_NU_ALE_BASE 0x1e000
39
40#define AM65_CPSW_CPSW_NU_PORTS_OFFSET 0x1000
41#define AM65_CPSW_CPSW_NU_PORT_MACSL_OFFSET 0x330
42
43#define AM65_CPSW_MDIO_BUS_FREQ_DEF 1000000
44
45#define AM65_CPSW_CTL_REG 0x4
46#define AM65_CPSW_STAT_PORT_EN_REG 0x14
47#define AM65_CPSW_PTYPE_REG 0x18
48
49#define AM65_CPSW_CTL_REG_P0_ENABLE BIT(2)
50#define AM65_CPSW_CTL_REG_P0_TX_CRC_REMOVE BIT(13)
51#define AM65_CPSW_CTL_REG_P0_RX_PAD BIT(14)
52
53#define AM65_CPSW_P0_FLOW_ID_REG 0x8
54#define AM65_CPSW_PN_RX_MAXLEN_REG 0x24
55#define AM65_CPSW_PN_REG_SA_L 0x308
56#define AM65_CPSW_PN_REG_SA_H 0x30c
57
58#define AM65_CPSW_ALE_CTL_REG 0x8
59#define AM65_CPSW_ALE_CTL_REG_ENABLE BIT(31)
60#define AM65_CPSW_ALE_CTL_REG_RESET_TBL BIT(30)
61#define AM65_CPSW_ALE_CTL_REG_BYPASS BIT(4)
62#define AM65_CPSW_ALE_PN_CTL_REG(x) (0x40 + (x) * 4)
63#define AM65_CPSW_ALE_PN_CTL_REG_MODE_FORWARD 0x3
64#define AM65_CPSW_ALE_PN_CTL_REG_MAC_ONLY BIT(11)
65
Vignesh Raghavendra5cb8a0f2020-07-06 13:36:53 +053066#define AM65_CPSW_ALE_THREADMAPDEF_REG 0x134
67#define AM65_CPSW_ALE_DEFTHREAD_EN BIT(15)
68
Keerthya00b95c2019-07-09 10:30:34 +053069#define AM65_CPSW_MACSL_CTL_REG 0x0
70#define AM65_CPSW_MACSL_CTL_REG_IFCTL_A BIT(15)
Murali Karicheri6565e902020-04-17 11:12:09 -040071#define AM65_CPSW_MACSL_CTL_EXT_EN BIT(18)
Keerthya00b95c2019-07-09 10:30:34 +053072#define AM65_CPSW_MACSL_CTL_REG_GIG BIT(7)
73#define AM65_CPSW_MACSL_CTL_REG_GMII_EN BIT(5)
74#define AM65_CPSW_MACSL_CTL_REG_LOOPBACK BIT(1)
75#define AM65_CPSW_MACSL_CTL_REG_FULL_DUPLEX BIT(0)
76#define AM65_CPSW_MACSL_RESET_REG 0x8
77#define AM65_CPSW_MACSL_RESET_REG_RESET BIT(0)
78#define AM65_CPSW_MACSL_STATUS_REG 0x4
79#define AM65_CPSW_MACSL_RESET_REG_PN_IDLE BIT(31)
80#define AM65_CPSW_MACSL_RESET_REG_PN_E_IDLE BIT(30)
81#define AM65_CPSW_MACSL_RESET_REG_PN_P_IDLE BIT(29)
82#define AM65_CPSW_MACSL_RESET_REG_PN_TX_IDLE BIT(28)
83#define AM65_CPSW_MACSL_RESET_REG_IDLE_MASK \
84 (AM65_CPSW_MACSL_RESET_REG_PN_IDLE | \
85 AM65_CPSW_MACSL_RESET_REG_PN_E_IDLE | \
86 AM65_CPSW_MACSL_RESET_REG_PN_P_IDLE | \
87 AM65_CPSW_MACSL_RESET_REG_PN_TX_IDLE)
88
89#define AM65_CPSW_CPPI_PKT_TYPE 0x7
90
91struct am65_cpsw_port {
92 fdt_addr_t port_base;
93 fdt_addr_t macsl_base;
94 bool disabled;
95 u32 mac_control;
96};
97
98struct am65_cpsw_common {
99 struct udevice *dev;
100 fdt_addr_t ss_base;
101 fdt_addr_t cpsw_base;
102 fdt_addr_t mdio_base;
103 fdt_addr_t ale_base;
104 fdt_addr_t gmii_sel;
105 fdt_addr_t mac_efuse;
106
107 struct clk fclk;
108 struct power_domain pwrdmn;
109
110 u32 port_num;
111 struct am65_cpsw_port ports[AM65_CPSW_CPSWNU_MAX_PORTS];
Keerthya00b95c2019-07-09 10:30:34 +0530112
113 struct mii_dev *bus;
114 u32 bus_freq;
115
116 struct dma dma_tx;
117 struct dma dma_rx;
118 u32 rx_next;
119 u32 rx_pend;
120 bool started;
121};
122
123struct am65_cpsw_priv {
124 struct udevice *dev;
125 struct am65_cpsw_common *cpsw_common;
126 u32 port_id;
127
128 struct phy_device *phydev;
129 bool has_phy;
130 ofnode phy_node;
131 u32 phy_addr;
Ravi Gunasekaran1eb61912022-09-22 15:21:24 +0530132
133 bool mdio_manual_mode;
Keerthya00b95c2019-07-09 10:30:34 +0530134};
135
136#ifdef PKTSIZE_ALIGN
137#define UDMA_RX_BUF_SIZE PKTSIZE_ALIGN
138#else
139#define UDMA_RX_BUF_SIZE ALIGN(1522, ARCH_DMA_MINALIGN)
140#endif
141
142#ifdef PKTBUFSRX
143#define UDMA_RX_DESC_NUM PKTBUFSRX
144#else
145#define UDMA_RX_DESC_NUM 4
146#endif
147
148#define mac_hi(mac) (((mac)[0] << 0) | ((mac)[1] << 8) | \
149 ((mac)[2] << 16) | ((mac)[3] << 24))
150#define mac_lo(mac) (((mac)[4] << 0) | ((mac)[5] << 8))
151
152static void am65_cpsw_set_sl_mac(struct am65_cpsw_port *slave,
153 unsigned char *addr)
154{
155 writel(mac_hi(addr),
156 slave->port_base + AM65_CPSW_PN_REG_SA_H);
157 writel(mac_lo(addr),
158 slave->port_base + AM65_CPSW_PN_REG_SA_L);
159}
160
161int am65_cpsw_macsl_reset(struct am65_cpsw_port *slave)
162{
163 u32 i = 100;
164
165 /* Set the soft reset bit */
166 writel(AM65_CPSW_MACSL_RESET_REG_RESET,
167 slave->macsl_base + AM65_CPSW_MACSL_RESET_REG);
168
169 while ((readl(slave->macsl_base + AM65_CPSW_MACSL_RESET_REG) &
170 AM65_CPSW_MACSL_RESET_REG_RESET) && i--)
171 cpu_relax();
172
173 /* Timeout on the reset */
174 return i;
175}
176
177static int am65_cpsw_macsl_wait_for_idle(struct am65_cpsw_port *slave)
178{
179 u32 i = 100;
180
181 while ((readl(slave->macsl_base + AM65_CPSW_MACSL_STATUS_REG) &
182 AM65_CPSW_MACSL_RESET_REG_IDLE_MASK) && i--)
183 cpu_relax();
184
185 return i;
186}
187
188static int am65_cpsw_update_link(struct am65_cpsw_priv *priv)
189{
190 struct am65_cpsw_common *common = priv->cpsw_common;
191 struct am65_cpsw_port *port = &common->ports[priv->port_id];
192 struct phy_device *phy = priv->phydev;
193 u32 mac_control = 0;
194
195 if (phy->link) { /* link up */
196 mac_control = /*AM65_CPSW_MACSL_CTL_REG_LOOPBACK |*/
197 AM65_CPSW_MACSL_CTL_REG_GMII_EN;
198 if (phy->speed == 1000)
199 mac_control |= AM65_CPSW_MACSL_CTL_REG_GIG;
Murali Karicheri6565e902020-04-17 11:12:09 -0400200 if (phy->speed == 10 && phy_interface_is_rgmii(phy))
201 /* Can be used with in band mode only */
202 mac_control |= AM65_CPSW_MACSL_CTL_EXT_EN;
Keerthya00b95c2019-07-09 10:30:34 +0530203 if (phy->duplex == DUPLEX_FULL)
204 mac_control |= AM65_CPSW_MACSL_CTL_REG_FULL_DUPLEX;
205 if (phy->speed == 100)
206 mac_control |= AM65_CPSW_MACSL_CTL_REG_IFCTL_A;
207 }
208
209 if (mac_control == port->mac_control)
210 goto out;
211
212 if (mac_control) {
213 printf("link up on port %d, speed %d, %s duplex\n",
214 priv->port_id, phy->speed,
215 (phy->duplex == DUPLEX_FULL) ? "full" : "half");
216 } else {
217 printf("link down on port %d\n", priv->port_id);
218 }
219
220 writel(mac_control, port->macsl_base + AM65_CPSW_MACSL_CTL_REG);
221 port->mac_control = mac_control;
222
223out:
224 return phy->link;
225}
226
Andreas Dannenberg1dc2ee62023-06-14 17:28:53 -0500227#define AM65_GMII_SEL_PORT_OFFS(x) (0x4 * ((x) - 1))
228
Keerthya00b95c2019-07-09 10:30:34 +0530229#define AM65_GMII_SEL_MODE_MII 0
230#define AM65_GMII_SEL_MODE_RMII 1
231#define AM65_GMII_SEL_MODE_RGMII 2
232
233#define AM65_GMII_SEL_RGMII_IDMODE BIT(4)
234
235static void am65_cpsw_gmii_sel_k3(struct am65_cpsw_priv *priv,
236 phy_interface_t phy_mode, int slave)
237{
238 struct am65_cpsw_common *common = priv->cpsw_common;
Andreas Dannenberg1dc2ee62023-06-14 17:28:53 -0500239 fdt_addr_t gmii_sel = common->gmii_sel + AM65_GMII_SEL_PORT_OFFS(slave);
Keerthya00b95c2019-07-09 10:30:34 +0530240 u32 reg;
241 u32 mode = 0;
242 bool rgmii_id = false;
243
Andreas Dannenberg1dc2ee62023-06-14 17:28:53 -0500244 reg = readl(gmii_sel);
Keerthya00b95c2019-07-09 10:30:34 +0530245
246 dev_dbg(common->dev, "old gmii_sel: %08x\n", reg);
247
248 switch (phy_mode) {
249 case PHY_INTERFACE_MODE_RMII:
250 mode = AM65_GMII_SEL_MODE_RMII;
251 break;
252
253 case PHY_INTERFACE_MODE_RGMII:
Grygorii Strashkobf45d9b2019-09-19 11:16:41 +0300254 case PHY_INTERFACE_MODE_RGMII_RXID:
Keerthya00b95c2019-07-09 10:30:34 +0530255 mode = AM65_GMII_SEL_MODE_RGMII;
256 break;
257
258 case PHY_INTERFACE_MODE_RGMII_ID:
Keerthya00b95c2019-07-09 10:30:34 +0530259 case PHY_INTERFACE_MODE_RGMII_TXID:
260 mode = AM65_GMII_SEL_MODE_RGMII;
261 rgmii_id = true;
262 break;
263
264 default:
265 dev_warn(common->dev,
266 "Unsupported PHY mode: %u. Defaulting to MII.\n",
267 phy_mode);
268 /* fallthrough */
269 case PHY_INTERFACE_MODE_MII:
270 mode = AM65_GMII_SEL_MODE_MII;
271 break;
272 };
273
274 if (rgmii_id)
275 mode |= AM65_GMII_SEL_RGMII_IDMODE;
276
277 reg = mode;
278 dev_dbg(common->dev, "gmii_sel PHY mode: %u, new gmii_sel: %08x\n",
279 phy_mode, reg);
Andreas Dannenberg1dc2ee62023-06-14 17:28:53 -0500280 writel(reg, gmii_sel);
Keerthya00b95c2019-07-09 10:30:34 +0530281
Andreas Dannenberg1dc2ee62023-06-14 17:28:53 -0500282 reg = readl(gmii_sel);
Keerthya00b95c2019-07-09 10:30:34 +0530283 if (reg != mode)
284 dev_err(common->dev,
285 "gmii_sel PHY mode NOT SET!: requested: %08x, gmii_sel: %08x\n",
286 mode, reg);
287}
288
289static int am65_cpsw_start(struct udevice *dev)
290{
Simon Glassfa20e932020-12-03 16:55:20 -0700291 struct eth_pdata *pdata = dev_get_plat(dev);
Keerthya00b95c2019-07-09 10:30:34 +0530292 struct am65_cpsw_priv *priv = dev_get_priv(dev);
293 struct am65_cpsw_common *common = priv->cpsw_common;
294 struct am65_cpsw_port *port = &common->ports[priv->port_id];
295 struct am65_cpsw_port *port0 = &common->ports[0];
Vignesh Raghavendra462ff042019-12-04 22:17:22 +0530296 struct ti_udma_drv_chan_cfg_data *dma_rx_cfg_data;
Keerthya00b95c2019-07-09 10:30:34 +0530297 int ret, i;
298
299 ret = power_domain_on(&common->pwrdmn);
300 if (ret) {
301 dev_err(dev, "power_domain_on() failed %d\n", ret);
302 goto out;
303 }
304
305 ret = clk_enable(&common->fclk);
306 if (ret) {
307 dev_err(dev, "clk enabled failed %d\n", ret);
308 goto err_off_pwrdm;
309 }
310
311 common->rx_next = 0;
312 common->rx_pend = 0;
313 ret = dma_get_by_name(common->dev, "tx0", &common->dma_tx);
314 if (ret) {
315 dev_err(dev, "TX dma get failed %d\n", ret);
316 goto err_off_clk;
317 }
318 ret = dma_get_by_name(common->dev, "rx", &common->dma_rx);
319 if (ret) {
320 dev_err(dev, "RX dma get failed %d\n", ret);
321 goto err_free_tx;
322 }
323
324 for (i = 0; i < UDMA_RX_DESC_NUM; i++) {
325 ret = dma_prepare_rcv_buf(&common->dma_rx,
326 net_rx_packets[i],
327 UDMA_RX_BUF_SIZE);
328 if (ret) {
329 dev_err(dev, "RX dma add buf failed %d\n", ret);
330 goto err_free_tx;
331 }
332 }
333
334 ret = dma_enable(&common->dma_tx);
335 if (ret) {
336 dev_err(dev, "TX dma_enable failed %d\n", ret);
337 goto err_free_rx;
338 }
339 ret = dma_enable(&common->dma_rx);
340 if (ret) {
341 dev_err(dev, "RX dma_enable failed %d\n", ret);
342 goto err_dis_tx;
343 }
344
345 /* Control register */
346 writel(AM65_CPSW_CTL_REG_P0_ENABLE |
347 AM65_CPSW_CTL_REG_P0_TX_CRC_REMOVE |
348 AM65_CPSW_CTL_REG_P0_RX_PAD,
349 common->cpsw_base + AM65_CPSW_CTL_REG);
350
351 /* disable priority elevation */
352 writel(0, common->cpsw_base + AM65_CPSW_PTYPE_REG);
353
354 /* enable statistics */
355 writel(BIT(0) | BIT(priv->port_id),
356 common->cpsw_base + AM65_CPSW_STAT_PORT_EN_REG);
357
358 /* Port 0 length register */
359 writel(PKTSIZE_ALIGN, port0->port_base + AM65_CPSW_PN_RX_MAXLEN_REG);
360
361 /* set base flow_id */
Vignesh Raghavendra462ff042019-12-04 22:17:22 +0530362 dma_get_cfg(&common->dma_rx, 0, (void **)&dma_rx_cfg_data);
363 writel(dma_rx_cfg_data->flow_id_base,
Keerthya00b95c2019-07-09 10:30:34 +0530364 port0->port_base + AM65_CPSW_P0_FLOW_ID_REG);
Vignesh Raghavendra462ff042019-12-04 22:17:22 +0530365 dev_info(dev, "K3 CPSW: rflow_id_base: %u\n",
366 dma_rx_cfg_data->flow_id_base);
Keerthya00b95c2019-07-09 10:30:34 +0530367
368 /* Reset and enable the ALE */
369 writel(AM65_CPSW_ALE_CTL_REG_ENABLE | AM65_CPSW_ALE_CTL_REG_RESET_TBL |
370 AM65_CPSW_ALE_CTL_REG_BYPASS,
371 common->ale_base + AM65_CPSW_ALE_CTL_REG);
372
373 /* port 0 put into forward mode */
374 writel(AM65_CPSW_ALE_PN_CTL_REG_MODE_FORWARD,
375 common->ale_base + AM65_CPSW_ALE_PN_CTL_REG(0));
376
Vignesh Raghavendra5cb8a0f2020-07-06 13:36:53 +0530377 writel(AM65_CPSW_ALE_DEFTHREAD_EN,
378 common->ale_base + AM65_CPSW_ALE_THREADMAPDEF_REG);
379
Keerthya00b95c2019-07-09 10:30:34 +0530380 /* PORT x configuration */
381
382 /* Port x Max length register */
383 writel(PKTSIZE_ALIGN, port->port_base + AM65_CPSW_PN_RX_MAXLEN_REG);
384
385 /* Port x set mac */
386 am65_cpsw_set_sl_mac(port, pdata->enetaddr);
387
388 /* Port x ALE: mac_only, Forwarding */
389 writel(AM65_CPSW_ALE_PN_CTL_REG_MAC_ONLY |
390 AM65_CPSW_ALE_PN_CTL_REG_MODE_FORWARD,
391 common->ale_base + AM65_CPSW_ALE_PN_CTL_REG(priv->port_id));
392
393 port->mac_control = 0;
394 if (!am65_cpsw_macsl_reset(port)) {
395 dev_err(dev, "mac_sl reset failed\n");
396 ret = -EFAULT;
397 goto err_dis_rx;
398 }
399
400 ret = phy_startup(priv->phydev);
401 if (ret) {
402 dev_err(dev, "phy_startup failed\n");
403 goto err_dis_rx;
404 }
405
406 ret = am65_cpsw_update_link(priv);
407 if (!ret) {
408 ret = -ENODEV;
409 goto err_phy_shutdown;
410 }
411
412 common->started = true;
413
414 return 0;
415
416err_phy_shutdown:
417 phy_shutdown(priv->phydev);
418err_dis_rx:
419 /* disable ports */
420 writel(0, common->ale_base + AM65_CPSW_ALE_PN_CTL_REG(priv->port_id));
421 writel(0, common->ale_base + AM65_CPSW_ALE_PN_CTL_REG(0));
422 if (!am65_cpsw_macsl_wait_for_idle(port))
423 dev_err(dev, "mac_sl idle timeout\n");
424 writel(0, port->macsl_base + AM65_CPSW_MACSL_CTL_REG);
425 writel(0, common->ale_base + AM65_CPSW_ALE_CTL_REG);
426 writel(0, common->cpsw_base + AM65_CPSW_CTL_REG);
427
428 dma_disable(&common->dma_rx);
429err_dis_tx:
430 dma_disable(&common->dma_tx);
431err_free_rx:
432 dma_free(&common->dma_rx);
433err_free_tx:
434 dma_free(&common->dma_tx);
435err_off_clk:
436 clk_disable(&common->fclk);
437err_off_pwrdm:
438 power_domain_off(&common->pwrdmn);
439out:
440 dev_err(dev, "%s end error\n", __func__);
441
442 return ret;
443}
444
445static int am65_cpsw_send(struct udevice *dev, void *packet, int length)
446{
447 struct am65_cpsw_priv *priv = dev_get_priv(dev);
448 struct am65_cpsw_common *common = priv->cpsw_common;
449 struct ti_udma_drv_packet_data packet_data;
450 int ret;
451
452 packet_data.pkt_type = AM65_CPSW_CPPI_PKT_TYPE;
453 packet_data.dest_tag = priv->port_id;
454 ret = dma_send(&common->dma_tx, packet, length, &packet_data);
455 if (ret) {
456 dev_err(dev, "TX dma_send failed %d\n", ret);
457 return ret;
458 }
459
460 return 0;
461}
462
463static int am65_cpsw_recv(struct udevice *dev, int flags, uchar **packetp)
464{
465 struct am65_cpsw_priv *priv = dev_get_priv(dev);
466 struct am65_cpsw_common *common = priv->cpsw_common;
467
468 /* try to receive a new packet */
469 return dma_receive(&common->dma_rx, (void **)packetp, NULL);
470}
471
472static int am65_cpsw_free_pkt(struct udevice *dev, uchar *packet, int length)
473{
474 struct am65_cpsw_priv *priv = dev_get_priv(dev);
475 struct am65_cpsw_common *common = priv->cpsw_common;
476 int ret;
477
478 if (length > 0) {
479 u32 pkt = common->rx_next % UDMA_RX_DESC_NUM;
480
481 ret = dma_prepare_rcv_buf(&common->dma_rx,
482 net_rx_packets[pkt],
483 UDMA_RX_BUF_SIZE);
484 if (ret)
485 dev_err(dev, "RX dma free_pkt failed %d\n", ret);
486 common->rx_next++;
487 }
488
489 return 0;
490}
491
492static void am65_cpsw_stop(struct udevice *dev)
493{
494 struct am65_cpsw_priv *priv = dev_get_priv(dev);
495 struct am65_cpsw_common *common = priv->cpsw_common;
496 struct am65_cpsw_port *port = &common->ports[priv->port_id];
497
498 if (!common->started)
499 return;
500
501 phy_shutdown(priv->phydev);
502
503 writel(0, common->ale_base + AM65_CPSW_ALE_PN_CTL_REG(priv->port_id));
504 writel(0, common->ale_base + AM65_CPSW_ALE_PN_CTL_REG(0));
505 if (!am65_cpsw_macsl_wait_for_idle(port))
506 dev_err(dev, "mac_sl idle timeout\n");
507 writel(0, port->macsl_base + AM65_CPSW_MACSL_CTL_REG);
508 writel(0, common->ale_base + AM65_CPSW_ALE_CTL_REG);
509 writel(0, common->cpsw_base + AM65_CPSW_CTL_REG);
510
511 dma_disable(&common->dma_tx);
512 dma_free(&common->dma_tx);
513
514 dma_disable(&common->dma_rx);
515 dma_free(&common->dma_rx);
516
517 common->started = false;
518}
519
520static int am65_cpsw_read_rom_hwaddr(struct udevice *dev)
521{
522 struct am65_cpsw_priv *priv = dev_get_priv(dev);
523 struct am65_cpsw_common *common = priv->cpsw_common;
Simon Glassfa20e932020-12-03 16:55:20 -0700524 struct eth_pdata *pdata = dev_get_plat(dev);
Keerthya00b95c2019-07-09 10:30:34 +0530525 u32 mac_hi, mac_lo;
526
527 if (common->mac_efuse == FDT_ADDR_T_NONE)
528 return -1;
529
530 mac_lo = readl(common->mac_efuse);
531 mac_hi = readl(common->mac_efuse + 4);
532 pdata->enetaddr[0] = (mac_hi >> 8) & 0xff;
533 pdata->enetaddr[1] = mac_hi & 0xff;
534 pdata->enetaddr[2] = (mac_lo >> 24) & 0xff;
535 pdata->enetaddr[3] = (mac_lo >> 16) & 0xff;
536 pdata->enetaddr[4] = (mac_lo >> 8) & 0xff;
537 pdata->enetaddr[5] = mac_lo & 0xff;
538
539 return 0;
540}
541
542static const struct eth_ops am65_cpsw_ops = {
543 .start = am65_cpsw_start,
544 .send = am65_cpsw_send,
545 .recv = am65_cpsw_recv,
546 .free_pkt = am65_cpsw_free_pkt,
547 .stop = am65_cpsw_stop,
548 .read_rom_hwaddr = am65_cpsw_read_rom_hwaddr,
549};
550
Ravi Gunasekaran1eb61912022-09-22 15:21:24 +0530551static const struct soc_attr k3_mdio_soc_data[] = {
552 { .family = "AM62X", .revision = "SR1.0" },
553 { .family = "AM64X", .revision = "SR1.0" },
554 { .family = "AM64X", .revision = "SR2.0" },
555 { .family = "AM65X", .revision = "SR1.0" },
556 { .family = "AM65X", .revision = "SR2.0" },
557 { .family = "J7200", .revision = "SR1.0" },
558 { .family = "J7200", .revision = "SR2.0" },
559 { .family = "J721E", .revision = "SR1.0" },
560 { .family = "J721E", .revision = "SR1.1" },
561 { .family = "J721S2", .revision = "SR1.0" },
562 { /* sentinel */ },
563};
564
Maxime Ripard028849d2023-07-24 15:57:30 +0200565static ofnode am65_cpsw_find_mdio(ofnode parent)
566{
567 ofnode node;
568
569 ofnode_for_each_subnode(node, parent)
570 if (ofnode_device_is_compatible(node, "ti,cpsw-mdio"))
571 return node;
572
573 return ofnode_null();
574}
575
576static int am65_cpsw_mdio_setup(struct udevice *dev)
577{
578 struct am65_cpsw_priv *priv = dev_get_priv(dev);
579 struct am65_cpsw_common *cpsw_common = priv->cpsw_common;
580 struct udevice *mdio_dev;
581 ofnode mdio;
582 int ret;
583
584 mdio = am65_cpsw_find_mdio(dev_ofnode(cpsw_common->dev));
585 if (!ofnode_valid(mdio))
586 return 0;
587
588 /*
589 * The MDIO controller is represented in the DT binding by a
590 * subnode of the MAC controller.
591 *
592 * We don't have a DM driver for the MDIO device yet, and thus any
593 * pinctrl setting on its node will be ignored.
594 *
595 * However, we do need to make sure the pins states tied to the
596 * MDIO node are configured properly. Fortunately, the core DM
597 * does that for use when we get a device, so we can work around
598 * that whole issue by just requesting a dummy MDIO driver to
599 * probe, and our pins will get muxed.
600 */
601 ret = uclass_get_device_by_ofnode(UCLASS_MDIO, mdio, &mdio_dev);
602 if (ret)
603 return ret;
604
605 return 0;
606}
607
Keerthya00b95c2019-07-09 10:30:34 +0530608static int am65_cpsw_mdio_init(struct udevice *dev)
609{
610 struct am65_cpsw_priv *priv = dev_get_priv(dev);
611 struct am65_cpsw_common *cpsw_common = priv->cpsw_common;
Maxime Ripard028849d2023-07-24 15:57:30 +0200612 int ret;
Keerthya00b95c2019-07-09 10:30:34 +0530613
614 if (!priv->has_phy || cpsw_common->bus)
615 return 0;
616
Maxime Ripard028849d2023-07-24 15:57:30 +0200617 ret = am65_cpsw_mdio_setup(dev);
618 if (ret)
619 return ret;
620
Keerthya00b95c2019-07-09 10:30:34 +0530621 cpsw_common->bus = cpsw_mdio_init(dev->name,
622 cpsw_common->mdio_base,
623 cpsw_common->bus_freq,
Ravi Gunasekaran40cea492022-09-22 15:21:23 +0530624 clk_get_rate(&cpsw_common->fclk),
Ravi Gunasekaran1eb61912022-09-22 15:21:24 +0530625 priv->mdio_manual_mode);
Keerthya00b95c2019-07-09 10:30:34 +0530626 if (!cpsw_common->bus)
627 return -EFAULT;
628
629 return 0;
630}
631
632static int am65_cpsw_phy_init(struct udevice *dev)
633{
634 struct am65_cpsw_priv *priv = dev_get_priv(dev);
635 struct am65_cpsw_common *cpsw_common = priv->cpsw_common;
Simon Glassfa20e932020-12-03 16:55:20 -0700636 struct eth_pdata *pdata = dev_get_plat(dev);
Keerthya00b95c2019-07-09 10:30:34 +0530637 struct phy_device *phydev;
638 u32 supported = PHY_GBIT_FEATURES;
639 int ret;
640
641 phydev = phy_connect(cpsw_common->bus,
642 priv->phy_addr,
643 priv->dev,
644 pdata->phy_interface);
645
646 if (!phydev) {
647 dev_err(dev, "phy_connect() failed\n");
648 return -ENODEV;
649 }
650
651 phydev->supported &= supported;
652 if (pdata->max_speed) {
653 ret = phy_set_supported(phydev, pdata->max_speed);
654 if (ret)
655 return ret;
656 }
657 phydev->advertising = phydev->supported;
658
659 if (ofnode_valid(priv->phy_node))
660 phydev->node = priv->phy_node;
661
662 priv->phydev = phydev;
663 ret = phy_config(phydev);
664 if (ret < 0)
665 pr_err("phy_config() failed: %d", ret);
666
667 return ret;
668}
669
Vignesh Raghavendrabbedbbb2021-12-24 12:55:30 +0530670static int am65_cpsw_ofdata_parse_phy(struct udevice *dev)
Keerthya00b95c2019-07-09 10:30:34 +0530671{
Simon Glassfa20e932020-12-03 16:55:20 -0700672 struct eth_pdata *pdata = dev_get_plat(dev);
Keerthya00b95c2019-07-09 10:30:34 +0530673 struct am65_cpsw_priv *priv = dev_get_priv(dev);
674 struct ofnode_phandle_args out_args;
Keerthya00b95c2019-07-09 10:30:34 +0530675 int ret = 0;
676
Vignesh Raghavendrabbedbbb2021-12-24 12:55:30 +0530677 dev_read_u32(dev, "reg", &priv->port_id);
678
Marek Behúnbc194772022-04-07 00:33:01 +0200679 pdata->phy_interface = dev_read_phy_mode(dev);
Marek Behún48631e42022-04-07 00:33:03 +0200680 if (pdata->phy_interface == PHY_INTERFACE_MODE_NA) {
Marek Behúnbc194772022-04-07 00:33:01 +0200681 dev_err(dev, "Invalid PHY mode, port %u\n", priv->port_id);
682 return -EINVAL;
Keerthya00b95c2019-07-09 10:30:34 +0530683 }
684
Vignesh Raghavendrabbedbbb2021-12-24 12:55:30 +0530685 dev_read_u32(dev, "max-speed", (u32 *)&pdata->max_speed);
Keerthya00b95c2019-07-09 10:30:34 +0530686 if (pdata->max_speed)
687 dev_err(dev, "Port %u speed froced to %uMbit\n",
688 priv->port_id, pdata->max_speed);
689
690 priv->has_phy = true;
Vignesh Raghavendrabbedbbb2021-12-24 12:55:30 +0530691 ret = ofnode_parse_phandle_with_args(dev_ofnode(dev), "phy-handle",
Keerthya00b95c2019-07-09 10:30:34 +0530692 NULL, 0, 0, &out_args);
693 if (ret) {
694 dev_err(dev, "can't parse phy-handle port %u (%d)\n",
695 priv->port_id, ret);
696 priv->has_phy = false;
697 ret = 0;
698 }
699
700 priv->phy_node = out_args.node;
701 if (priv->has_phy) {
702 ret = ofnode_read_u32(priv->phy_node, "reg", &priv->phy_addr);
703 if (ret) {
704 dev_err(dev, "failed to get phy_addr port %u (%d)\n",
705 priv->port_id, ret);
706 goto out;
707 }
708 }
709
710out:
711 return ret;
712}
713
Vignesh Raghavendrabbedbbb2021-12-24 12:55:30 +0530714static int am65_cpsw_port_probe(struct udevice *dev)
Keerthya00b95c2019-07-09 10:30:34 +0530715{
716 struct am65_cpsw_priv *priv = dev_get_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700717 struct eth_pdata *pdata = dev_get_plat(dev);
Keerthya00b95c2019-07-09 10:30:34 +0530718 struct am65_cpsw_common *cpsw_common;
Vignesh Raghavendrabbedbbb2021-12-24 12:55:30 +0530719 char portname[15];
720 int ret;
Keerthya00b95c2019-07-09 10:30:34 +0530721
722 priv->dev = dev;
723
Vignesh Raghavendrabbedbbb2021-12-24 12:55:30 +0530724 cpsw_common = dev_get_priv(dev->parent);
Keerthya00b95c2019-07-09 10:30:34 +0530725 priv->cpsw_common = cpsw_common;
726
Vignesh Raghavendrabbedbbb2021-12-24 12:55:30 +0530727 sprintf(portname, "%s%s", dev->parent->name, dev->name);
728 device_set_name(dev, portname);
729
Ravi Gunasekaran1eb61912022-09-22 15:21:24 +0530730 priv->mdio_manual_mode = false;
731 if (soc_device_match(k3_mdio_soc_data))
732 priv->mdio_manual_mode = true;
733
Vignesh Raghavendrabbedbbb2021-12-24 12:55:30 +0530734 ret = am65_cpsw_ofdata_parse_phy(dev);
735 if (ret)
736 goto out;
737
738 am65_cpsw_gmii_sel_k3(priv, pdata->phy_interface, priv->port_id);
739
740 ret = am65_cpsw_mdio_init(dev);
741 if (ret)
742 goto out;
743
744 ret = am65_cpsw_phy_init(dev);
745 if (ret)
746 goto out;
747out:
748 return ret;
749}
750
751static int am65_cpsw_probe_nuss(struct udevice *dev)
752{
753 struct am65_cpsw_common *cpsw_common = dev_get_priv(dev);
754 ofnode ports_np, node;
755 int ret, i;
756 struct udevice *port_dev;
757
Keerthya00b95c2019-07-09 10:30:34 +0530758 cpsw_common->dev = dev;
759 cpsw_common->ss_base = dev_read_addr(dev);
760 if (cpsw_common->ss_base == FDT_ADDR_T_NONE)
761 return -EINVAL;
762 cpsw_common->mac_efuse = devfdt_get_addr_name(dev, "mac_efuse");
763 /* no err check - optional */
764
765 ret = power_domain_get_by_index(dev, &cpsw_common->pwrdmn, 0);
766 if (ret) {
767 dev_err(dev, "failed to get pwrdmn: %d\n", ret);
768 return ret;
769 }
770
771 ret = clk_get_by_name(dev, "fck", &cpsw_common->fclk);
772 if (ret) {
773 power_domain_free(&cpsw_common->pwrdmn);
774 dev_err(dev, "failed to get clock %d\n", ret);
775 return ret;
776 }
777
778 cpsw_common->cpsw_base = cpsw_common->ss_base + AM65_CPSW_CPSW_NU_BASE;
779 cpsw_common->ale_base = cpsw_common->cpsw_base +
780 AM65_CPSW_CPSW_NU_ALE_BASE;
781 cpsw_common->mdio_base = cpsw_common->ss_base + AM65_CPSW_MDIO_BASE;
782
Vignesh Raghavendra2b834d02020-07-06 13:36:54 +0530783 ports_np = dev_read_subnode(dev, "ethernet-ports");
Keerthya00b95c2019-07-09 10:30:34 +0530784 if (!ofnode_valid(ports_np)) {
785 ret = -ENOENT;
786 goto out;
787 }
788
789 ofnode_for_each_subnode(node, ports_np) {
790 const char *node_name;
791 u32 port_id;
792 bool disabled;
793
794 node_name = ofnode_get_name(node);
795
Simon Glass2e4938b2022-09-06 20:27:17 -0600796 disabled = !ofnode_is_enabled(node);
Keerthya00b95c2019-07-09 10:30:34 +0530797
798 ret = ofnode_read_u32(node, "reg", &port_id);
799 if (ret) {
800 dev_err(dev, "%s: failed to get port_id (%d)\n",
801 node_name, ret);
802 goto out;
803 }
804
805 if (port_id >= AM65_CPSW_CPSWNU_MAX_PORTS) {
806 dev_err(dev, "%s: invalid port_id (%d)\n",
807 node_name, port_id);
808 ret = -EINVAL;
809 goto out;
810 }
811 cpsw_common->port_num++;
812
813 if (!port_id)
814 continue;
815
Keerthya00b95c2019-07-09 10:30:34 +0530816 cpsw_common->ports[port_id].disabled = disabled;
817 if (disabled)
818 continue;
819
Vignesh Raghavendrabbedbbb2021-12-24 12:55:30 +0530820 ret = device_bind_driver_to_node(dev, "am65_cpsw_nuss_port", ofnode_get_name(node), node, &port_dev);
Keerthya00b95c2019-07-09 10:30:34 +0530821 if (ret)
Vignesh Raghavendrad26ac2e2022-01-21 12:47:51 +0530822 dev_err(dev, "Failed to bind to %s node\n", ofnode_get_name(node));
Keerthya00b95c2019-07-09 10:30:34 +0530823 }
824
825 for (i = 0; i < AM65_CPSW_CPSWNU_MAX_PORTS; i++) {
826 struct am65_cpsw_port *port = &cpsw_common->ports[i];
827
828 port->port_base = cpsw_common->cpsw_base +
829 AM65_CPSW_CPSW_NU_PORTS_OFFSET +
830 (i * AM65_CPSW_CPSW_NU_PORTS_OFFSET);
831 port->macsl_base = port->port_base +
832 AM65_CPSW_CPSW_NU_PORT_MACSL_OFFSET;
833 }
834
835 node = dev_read_subnode(dev, "cpsw-phy-sel");
836 if (!ofnode_valid(node)) {
837 dev_err(dev, "can't find cpsw-phy-sel\n");
838 ret = -ENOENT;
839 goto out;
840 }
841
842 cpsw_common->gmii_sel = ofnode_get_addr(node);
843 if (cpsw_common->gmii_sel == FDT_ADDR_T_NONE) {
844 dev_err(dev, "failed to get gmii_sel base\n");
845 goto out;
846 }
847
Keerthya00b95c2019-07-09 10:30:34 +0530848 cpsw_common->bus_freq =
849 dev_read_u32_default(dev, "bus_freq",
850 AM65_CPSW_MDIO_BUS_FREQ_DEF);
851
Vignesh Raghavendra462ff042019-12-04 22:17:22 +0530852 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 +0530853 readl(cpsw_common->ss_base),
854 readl(cpsw_common->cpsw_base),
855 readl(cpsw_common->ale_base),
856 cpsw_common->port_num,
Keerthya00b95c2019-07-09 10:30:34 +0530857 cpsw_common->bus_freq);
858
859out:
860 clk_free(&cpsw_common->fclk);
861 power_domain_free(&cpsw_common->pwrdmn);
862 return ret;
863}
864
865static const struct udevice_id am65_cpsw_nuss_ids[] = {
866 { .compatible = "ti,am654-cpsw-nuss" },
Vignesh Raghavendra30bc6ea2019-12-04 22:17:23 +0530867 { .compatible = "ti,j721e-cpsw-nuss" },
Vignesh Raghavendra1cc35622021-05-10 20:06:11 +0530868 { .compatible = "ti,am642-cpsw-nuss" },
Keerthya00b95c2019-07-09 10:30:34 +0530869 { }
870};
871
Vignesh Raghavendrabbedbbb2021-12-24 12:55:30 +0530872U_BOOT_DRIVER(am65_cpsw_nuss) = {
873 .name = "am65_cpsw_nuss",
874 .id = UCLASS_MISC,
Keerthya00b95c2019-07-09 10:30:34 +0530875 .of_match = am65_cpsw_nuss_ids,
Vignesh Raghavendrabbedbbb2021-12-24 12:55:30 +0530876 .probe = am65_cpsw_probe_nuss,
877 .priv_auto = sizeof(struct am65_cpsw_common),
878};
879
880U_BOOT_DRIVER(am65_cpsw_nuss_port) = {
881 .name = "am65_cpsw_nuss_port",
882 .id = UCLASS_ETH,
883 .probe = am65_cpsw_port_probe,
Keerthya00b95c2019-07-09 10:30:34 +0530884 .ops = &am65_cpsw_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700885 .priv_auto = sizeof(struct am65_cpsw_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -0700886 .plat_auto = sizeof(struct eth_pdata),
Vignesh Raghavendra198bbb12022-01-28 11:21:19 +0530887 .flags = DM_FLAG_ALLOC_PRIV_DMA | DM_FLAG_OS_PREPARE,
Keerthya00b95c2019-07-09 10:30:34 +0530888};
Maxime Ripard028849d2023-07-24 15:57:30 +0200889
890static const struct udevice_id am65_cpsw_mdio_ids[] = {
891 { .compatible = "ti,cpsw-mdio" },
892 { }
893};
894
895U_BOOT_DRIVER(am65_cpsw_mdio) = {
896 .name = "am65_cpsw_mdio",
897 .id = UCLASS_MDIO,
898 .of_match = am65_cpsw_mdio_ids,
899};