blob: 04b01a81298f578abae507768ac50cc9d1e8396e [file] [log] [blame]
Grygorii Strashko39b014a2018-10-31 16:21:42 -05001// SPDX-License-Identifier: GPL-2.0+
Cyril Chemparathy464adc82012-07-24 12:22:16 +00002/*
3 * CPSW Ethernet Switch Driver
4 *
Grygorii Strashko39b014a2018-10-31 16:21:42 -05005 * Copyright (C) 2010-2018 Texas Instruments Incorporated - http://www.ti.com/
Cyril Chemparathy464adc82012-07-24 12:22:16 +00006 */
7
8#include <common.h>
9#include <command.h>
Simon Glass63334482019-11-14 12:57:39 -070010#include <cpu_func.h>
Cyril Chemparathy464adc82012-07-24 12:22:16 +000011#include <net.h>
12#include <miiphy.h>
13#include <malloc.h>
14#include <net.h>
15#include <netdev.h>
16#include <cpsw.h>
Simon Glass9bc15642020-02-03 07:36:16 -070017#include <dm/device_compat.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090018#include <linux/errno.h>
Vignesh R8e9473d2016-08-02 10:14:27 +053019#include <asm/gpio.h>
Cyril Chemparathy464adc82012-07-24 12:22:16 +000020#include <asm/io.h>
21#include <phy.h>
Tom Rini8eb48ff2013-03-14 11:15:25 +000022#include <asm/arch/cpu.h>
Mugunthan V N7ae228c2015-09-07 14:22:21 +053023#include <dm.h>
24
Grygorii Strashkoeae545a2018-10-31 16:21:44 -050025#include "cpsw_mdio.h"
26
Cyril Chemparathy464adc82012-07-24 12:22:16 +000027#define BITMASK(bits) (BIT(bits) - 1)
Cyril Chemparathy464adc82012-07-24 12:22:16 +000028#define NUM_DESCS (PKTBUFSRX * 2)
29#define PKT_MIN 60
30#define PKT_MAX (1500 + 14 + 4 + 4)
31#define CLEAR_BIT 1
32#define GIGABITEN BIT(7)
33#define FULLDUPLEXEN BIT(0)
34#define MIIEN BIT(15)
Grygorii Strashko63bba7e2019-09-19 11:16:37 +030035#define CTL_EXT_EN BIT(18)
Cyril Chemparathy464adc82012-07-24 12:22:16 +000036/* DMA Registers */
37#define CPDMA_TXCONTROL 0x004
38#define CPDMA_RXCONTROL 0x014
39#define CPDMA_SOFTRESET 0x01c
40#define CPDMA_RXFREE 0x0e0
41#define CPDMA_TXHDP_VER1 0x100
42#define CPDMA_TXHDP_VER2 0x200
43#define CPDMA_RXHDP_VER1 0x120
44#define CPDMA_RXHDP_VER2 0x220
45#define CPDMA_TXCP_VER1 0x140
46#define CPDMA_TXCP_VER2 0x240
47#define CPDMA_RXCP_VER1 0x160
48#define CPDMA_RXCP_VER2 0x260
49
Cyril Chemparathy464adc82012-07-24 12:22:16 +000050/* Descriptor mode bits */
51#define CPDMA_DESC_SOP BIT(31)
52#define CPDMA_DESC_EOP BIT(30)
53#define CPDMA_DESC_OWNER BIT(29)
54#define CPDMA_DESC_EOQ BIT(28)
55
56/*
57 * This timeout definition is a worst-case ultra defensive measure against
58 * unexpected controller lock ups. Ideally, we should never ever hit this
59 * scenario in practice.
60 */
Cyril Chemparathy464adc82012-07-24 12:22:16 +000061#define CPDMA_TIMEOUT 100 /* msecs */
62
Cyril Chemparathy464adc82012-07-24 12:22:16 +000063struct cpsw_regs {
64 u32 id_ver;
65 u32 control;
66 u32 soft_reset;
67 u32 stat_port_en;
68 u32 ptype;
69};
70
71struct cpsw_slave_regs {
72 u32 max_blks;
73 u32 blk_cnt;
74 u32 flow_thresh;
75 u32 port_vlan;
76 u32 tx_pri_map;
Matt Porter1ef0e872013-03-20 05:38:12 +000077#ifdef CONFIG_AM33XX
Cyril Chemparathy464adc82012-07-24 12:22:16 +000078 u32 gap_thresh;
Matt Porter1ef0e872013-03-20 05:38:12 +000079#elif defined(CONFIG_TI814X)
80 u32 ts_ctl;
81 u32 ts_seq_ltype;
82 u32 ts_vlan;
83#endif
Cyril Chemparathy464adc82012-07-24 12:22:16 +000084 u32 sa_lo;
85 u32 sa_hi;
86};
87
88struct cpsw_host_regs {
89 u32 max_blks;
90 u32 blk_cnt;
91 u32 flow_thresh;
92 u32 port_vlan;
93 u32 tx_pri_map;
94 u32 cpdma_tx_pri_map;
95 u32 cpdma_rx_chan_map;
96};
97
98struct cpsw_sliver_regs {
99 u32 id_ver;
100 u32 mac_control;
101 u32 mac_status;
102 u32 soft_reset;
103 u32 rx_maxlen;
104 u32 __reserved_0;
105 u32 rx_pause;
106 u32 tx_pause;
107 u32 __reserved_1;
108 u32 rx_pri_map;
109};
110
111#define ALE_ENTRY_BITS 68
112#define ALE_ENTRY_WORDS DIV_ROUND_UP(ALE_ENTRY_BITS, 32)
113
114/* ALE Registers */
115#define ALE_CONTROL 0x08
116#define ALE_UNKNOWNVLAN 0x18
117#define ALE_TABLE_CONTROL 0x20
118#define ALE_TABLE 0x34
119#define ALE_PORTCTL 0x40
120
121#define ALE_TABLE_WRITE BIT(31)
122
123#define ALE_TYPE_FREE 0
124#define ALE_TYPE_ADDR 1
125#define ALE_TYPE_VLAN 2
126#define ALE_TYPE_VLAN_ADDR 3
127
128#define ALE_UCAST_PERSISTANT 0
129#define ALE_UCAST_UNTOUCHED 1
130#define ALE_UCAST_OUI 2
131#define ALE_UCAST_TOUCHED 3
132
133#define ALE_MCAST_FWD 0
134#define ALE_MCAST_BLOCK_LEARN_FWD 1
135#define ALE_MCAST_FWD_LEARN 2
136#define ALE_MCAST_FWD_2 3
137
138enum cpsw_ale_port_state {
139 ALE_PORT_STATE_DISABLE = 0x00,
140 ALE_PORT_STATE_BLOCK = 0x01,
141 ALE_PORT_STATE_LEARN = 0x02,
142 ALE_PORT_STATE_FORWARD = 0x03,
143};
144
145/* ALE unicast entry flags - passed into cpsw_ale_add_ucast() */
146#define ALE_SECURE 1
147#define ALE_BLOCKED 2
148
149struct cpsw_slave {
150 struct cpsw_slave_regs *regs;
151 struct cpsw_sliver_regs *sliver;
152 int slave_num;
153 u32 mac_control;
154 struct cpsw_slave_data *data;
155};
156
157struct cpdma_desc {
158 /* hardware fields */
159 u32 hw_next;
160 u32 hw_buffer;
161 u32 hw_len;
162 u32 hw_mode;
163 /* software fields */
164 u32 sw_buffer;
165 u32 sw_len;
166};
167
168struct cpdma_chan {
169 struct cpdma_desc *head, *tail;
170 void *hdp, *cp, *rxfree;
171};
172
Mugunthan V N4d5fdb62016-10-13 19:33:38 +0530173/* AM33xx SoC specific definitions for the CONTROL port */
174#define AM33XX_GMII_SEL_MODE_MII 0
175#define AM33XX_GMII_SEL_MODE_RMII 1
176#define AM33XX_GMII_SEL_MODE_RGMII 2
177
178#define AM33XX_GMII_SEL_RGMII1_IDMODE BIT(4)
179#define AM33XX_GMII_SEL_RGMII2_IDMODE BIT(5)
180#define AM33XX_GMII_SEL_RMII1_IO_CLK_EN BIT(6)
181#define AM33XX_GMII_SEL_RMII2_IO_CLK_EN BIT(7)
182
183#define GMII_SEL_MODE_MASK 0x3
184
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000185#define desc_write(desc, fld, val) __raw_writel((u32)(val), &(desc)->fld)
186#define desc_read(desc, fld) __raw_readl(&(desc)->fld)
187#define desc_read_ptr(desc, fld) ((void *)__raw_readl(&(desc)->fld))
188
189#define chan_write(chan, fld, val) __raw_writel((u32)(val), (chan)->fld)
190#define chan_read(chan, fld) __raw_readl((chan)->fld)
191#define chan_read_ptr(chan, fld) ((void *)__raw_readl((chan)->fld))
192
Mugunthan V N33e073e2014-05-22 14:37:10 +0530193#define for_active_slave(slave, priv) \
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530194 slave = (priv)->slaves + ((priv)->data)->active_slave; if (slave)
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000195#define for_each_slave(slave, priv) \
196 for (slave = (priv)->slaves; slave != (priv)->slaves + \
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530197 ((priv)->data)->slaves; slave++)
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000198
199struct cpsw_priv {
Mugunthan V N7ae228c2015-09-07 14:22:21 +0530200#ifdef CONFIG_DM_ETH
201 struct udevice *dev;
202#else
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000203 struct eth_device *dev;
Mugunthan V N7ae228c2015-09-07 14:22:21 +0530204#endif
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530205 struct cpsw_platform_data *data;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000206 int host_port;
207
208 struct cpsw_regs *regs;
209 void *dma_regs;
210 struct cpsw_host_regs *host_port_regs;
211 void *ale_regs;
212
213 struct cpdma_desc *descs;
214 struct cpdma_desc *desc_free;
215 struct cpdma_chan rx_chan, tx_chan;
216
217 struct cpsw_slave *slaves;
218 struct phy_device *phydev;
219 struct mii_dev *bus;
Mugunthan V Nc3fdab42013-02-19 21:34:44 +0000220
Mugunthan V Nc3fdab42013-02-19 21:34:44 +0000221 u32 phy_mask;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000222};
223
224static inline int cpsw_ale_get_field(u32 *ale_entry, u32 start, u32 bits)
225{
226 int idx;
227
228 idx = start / 32;
229 start -= idx * 32;
230 idx = 2 - idx; /* flip */
231 return (ale_entry[idx] >> start) & BITMASK(bits);
232}
233
234static inline void cpsw_ale_set_field(u32 *ale_entry, u32 start, u32 bits,
235 u32 value)
236{
237 int idx;
238
239 value &= BITMASK(bits);
240 idx = start / 32;
241 start -= idx * 32;
242 idx = 2 - idx; /* flip */
243 ale_entry[idx] &= ~(BITMASK(bits) << start);
244 ale_entry[idx] |= (value << start);
245}
246
247#define DEFINE_ALE_FIELD(name, start, bits) \
248static inline int cpsw_ale_get_##name(u32 *ale_entry) \
249{ \
250 return cpsw_ale_get_field(ale_entry, start, bits); \
251} \
252static inline void cpsw_ale_set_##name(u32 *ale_entry, u32 value) \
253{ \
254 cpsw_ale_set_field(ale_entry, start, bits, value); \
255}
256
257DEFINE_ALE_FIELD(entry_type, 60, 2)
258DEFINE_ALE_FIELD(mcast_state, 62, 2)
259DEFINE_ALE_FIELD(port_mask, 66, 3)
260DEFINE_ALE_FIELD(ucast_type, 62, 2)
261DEFINE_ALE_FIELD(port_num, 66, 2)
262DEFINE_ALE_FIELD(blocked, 65, 1)
263DEFINE_ALE_FIELD(secure, 64, 1)
264DEFINE_ALE_FIELD(mcast, 40, 1)
265
266/* The MAC address field in the ALE entry cannot be macroized as above */
267static inline void cpsw_ale_get_addr(u32 *ale_entry, u8 *addr)
268{
269 int i;
270
271 for (i = 0; i < 6; i++)
272 addr[i] = cpsw_ale_get_field(ale_entry, 40 - 8*i, 8);
273}
274
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500275static inline void cpsw_ale_set_addr(u32 *ale_entry, const u8 *addr)
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000276{
277 int i;
278
279 for (i = 0; i < 6; i++)
280 cpsw_ale_set_field(ale_entry, 40 - 8*i, 8, addr[i]);
281}
282
283static int cpsw_ale_read(struct cpsw_priv *priv, int idx, u32 *ale_entry)
284{
285 int i;
286
287 __raw_writel(idx, priv->ale_regs + ALE_TABLE_CONTROL);
288
289 for (i = 0; i < ALE_ENTRY_WORDS; i++)
290 ale_entry[i] = __raw_readl(priv->ale_regs + ALE_TABLE + 4 * i);
291
292 return idx;
293}
294
295static int cpsw_ale_write(struct cpsw_priv *priv, int idx, u32 *ale_entry)
296{
297 int i;
298
299 for (i = 0; i < ALE_ENTRY_WORDS; i++)
300 __raw_writel(ale_entry[i], priv->ale_regs + ALE_TABLE + 4 * i);
301
302 __raw_writel(idx | ALE_TABLE_WRITE, priv->ale_regs + ALE_TABLE_CONTROL);
303
304 return idx;
305}
306
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500307static int cpsw_ale_match_addr(struct cpsw_priv *priv, const u8 *addr)
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000308{
309 u32 ale_entry[ALE_ENTRY_WORDS];
310 int type, idx;
311
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530312 for (idx = 0; idx < priv->data->ale_entries; idx++) {
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000313 u8 entry_addr[6];
314
315 cpsw_ale_read(priv, idx, ale_entry);
316 type = cpsw_ale_get_entry_type(ale_entry);
317 if (type != ALE_TYPE_ADDR && type != ALE_TYPE_VLAN_ADDR)
318 continue;
319 cpsw_ale_get_addr(ale_entry, entry_addr);
320 if (memcmp(entry_addr, addr, 6) == 0)
321 return idx;
322 }
323 return -ENOENT;
324}
325
326static int cpsw_ale_match_free(struct cpsw_priv *priv)
327{
328 u32 ale_entry[ALE_ENTRY_WORDS];
329 int type, idx;
330
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530331 for (idx = 0; idx < priv->data->ale_entries; idx++) {
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000332 cpsw_ale_read(priv, idx, ale_entry);
333 type = cpsw_ale_get_entry_type(ale_entry);
334 if (type == ALE_TYPE_FREE)
335 return idx;
336 }
337 return -ENOENT;
338}
339
340static int cpsw_ale_find_ageable(struct cpsw_priv *priv)
341{
342 u32 ale_entry[ALE_ENTRY_WORDS];
343 int type, idx;
344
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530345 for (idx = 0; idx < priv->data->ale_entries; idx++) {
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000346 cpsw_ale_read(priv, idx, ale_entry);
347 type = cpsw_ale_get_entry_type(ale_entry);
348 if (type != ALE_TYPE_ADDR && type != ALE_TYPE_VLAN_ADDR)
349 continue;
350 if (cpsw_ale_get_mcast(ale_entry))
351 continue;
352 type = cpsw_ale_get_ucast_type(ale_entry);
353 if (type != ALE_UCAST_PERSISTANT &&
354 type != ALE_UCAST_OUI)
355 return idx;
356 }
357 return -ENOENT;
358}
359
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500360static int cpsw_ale_add_ucast(struct cpsw_priv *priv, const u8 *addr,
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000361 int port, int flags)
362{
363 u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
364 int idx;
365
366 cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_ADDR);
367 cpsw_ale_set_addr(ale_entry, addr);
368 cpsw_ale_set_ucast_type(ale_entry, ALE_UCAST_PERSISTANT);
369 cpsw_ale_set_secure(ale_entry, (flags & ALE_SECURE) ? 1 : 0);
370 cpsw_ale_set_blocked(ale_entry, (flags & ALE_BLOCKED) ? 1 : 0);
371 cpsw_ale_set_port_num(ale_entry, port);
372
373 idx = cpsw_ale_match_addr(priv, addr);
374 if (idx < 0)
375 idx = cpsw_ale_match_free(priv);
376 if (idx < 0)
377 idx = cpsw_ale_find_ageable(priv);
378 if (idx < 0)
379 return -ENOMEM;
380
381 cpsw_ale_write(priv, idx, ale_entry);
382 return 0;
383}
384
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500385static int cpsw_ale_add_mcast(struct cpsw_priv *priv, const u8 *addr,
386 int port_mask)
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000387{
388 u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
389 int idx, mask;
390
391 idx = cpsw_ale_match_addr(priv, addr);
392 if (idx >= 0)
393 cpsw_ale_read(priv, idx, ale_entry);
394
395 cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_ADDR);
396 cpsw_ale_set_addr(ale_entry, addr);
397 cpsw_ale_set_mcast_state(ale_entry, ALE_MCAST_FWD_2);
398
399 mask = cpsw_ale_get_port_mask(ale_entry);
400 port_mask |= mask;
401 cpsw_ale_set_port_mask(ale_entry, port_mask);
402
403 if (idx < 0)
404 idx = cpsw_ale_match_free(priv);
405 if (idx < 0)
406 idx = cpsw_ale_find_ageable(priv);
407 if (idx < 0)
408 return -ENOMEM;
409
410 cpsw_ale_write(priv, idx, ale_entry);
411 return 0;
412}
413
414static inline void cpsw_ale_control(struct cpsw_priv *priv, int bit, int val)
415{
416 u32 tmp, mask = BIT(bit);
417
418 tmp = __raw_readl(priv->ale_regs + ALE_CONTROL);
419 tmp &= ~mask;
420 tmp |= val ? mask : 0;
421 __raw_writel(tmp, priv->ale_regs + ALE_CONTROL);
422}
423
424#define cpsw_ale_enable(priv, val) cpsw_ale_control(priv, 31, val)
425#define cpsw_ale_clear(priv, val) cpsw_ale_control(priv, 30, val)
426#define cpsw_ale_vlan_aware(priv, val) cpsw_ale_control(priv, 2, val)
427
428static inline void cpsw_ale_port_state(struct cpsw_priv *priv, int port,
429 int val)
430{
431 int offset = ALE_PORTCTL + 4 * port;
432 u32 tmp, mask = 0x3;
433
434 tmp = __raw_readl(priv->ale_regs + offset);
435 tmp &= ~mask;
436 tmp |= val & mask;
437 __raw_writel(tmp, priv->ale_regs + offset);
438}
439
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000440/* Set a self-clearing bit in a register, and wait for it to clear */
441static inline void setbit_and_wait_for_clear32(void *addr)
442{
443 __raw_writel(CLEAR_BIT, addr);
444 while (__raw_readl(addr) & CLEAR_BIT)
445 ;
446}
447
448#define mac_hi(mac) (((mac)[0] << 0) | ((mac)[1] << 8) | \
449 ((mac)[2] << 16) | ((mac)[3] << 24))
450#define mac_lo(mac) (((mac)[4] << 0) | ((mac)[5] << 8))
451
452static void cpsw_set_slave_mac(struct cpsw_slave *slave,
453 struct cpsw_priv *priv)
454{
Mugunthan V N7ae228c2015-09-07 14:22:21 +0530455#ifdef CONFIG_DM_ETH
456 struct eth_pdata *pdata = dev_get_platdata(priv->dev);
457
458 writel(mac_hi(pdata->enetaddr), &slave->regs->sa_hi);
459 writel(mac_lo(pdata->enetaddr), &slave->regs->sa_lo);
460#else
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000461 __raw_writel(mac_hi(priv->dev->enetaddr), &slave->regs->sa_hi);
462 __raw_writel(mac_lo(priv->dev->enetaddr), &slave->regs->sa_lo);
Mugunthan V N7ae228c2015-09-07 14:22:21 +0530463#endif
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000464}
465
Sekhar Norib5988852017-05-08 20:49:56 +0530466static int cpsw_slave_update_link(struct cpsw_slave *slave,
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000467 struct cpsw_priv *priv, int *link)
468{
Heiko Schocheref660032013-09-05 11:50:41 +0200469 struct phy_device *phy;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000470 u32 mac_control = 0;
Sekhar Norib5988852017-05-08 20:49:56 +0530471 int ret = -ENODEV;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000472
Heiko Schocheref660032013-09-05 11:50:41 +0200473 phy = priv->phydev;
Heiko Schocheref660032013-09-05 11:50:41 +0200474 if (!phy)
Sekhar Norib5988852017-05-08 20:49:56 +0530475 goto out;
476
477 ret = phy_startup(phy);
478 if (ret)
479 goto out;
Heiko Schocheref660032013-09-05 11:50:41 +0200480
Sekhar Norib5988852017-05-08 20:49:56 +0530481 if (link)
482 *link = phy->link;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000483
Sekhar Norib5988852017-05-08 20:49:56 +0530484 if (phy->link) { /* link up */
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530485 mac_control = priv->data->mac_control;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000486 if (phy->speed == 1000)
487 mac_control |= GIGABITEN;
488 if (phy->duplex == DUPLEX_FULL)
489 mac_control |= FULLDUPLEXEN;
490 if (phy->speed == 100)
491 mac_control |= MIIEN;
Grygorii Strashko63bba7e2019-09-19 11:16:37 +0300492 if (phy->speed == 10 && phy_interface_is_rgmii(phy))
493 mac_control |= CTL_EXT_EN;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000494 }
495
496 if (mac_control == slave->mac_control)
Sekhar Norib5988852017-05-08 20:49:56 +0530497 goto out;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000498
499 if (mac_control) {
500 printf("link up on port %d, speed %d, %s duplex\n",
501 slave->slave_num, phy->speed,
502 (phy->duplex == DUPLEX_FULL) ? "full" : "half");
503 } else {
504 printf("link down on port %d\n", slave->slave_num);
505 }
506
507 __raw_writel(mac_control, &slave->sliver->mac_control);
508 slave->mac_control = mac_control;
Sekhar Norib5988852017-05-08 20:49:56 +0530509
510out:
511 return ret;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000512}
513
514static int cpsw_update_link(struct cpsw_priv *priv)
515{
Sekhar Norib5988852017-05-08 20:49:56 +0530516 int ret = -ENODEV;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000517 struct cpsw_slave *slave;
518
Mugunthan V N33e073e2014-05-22 14:37:10 +0530519 for_active_slave(slave, priv)
Sekhar Norib5988852017-05-08 20:49:56 +0530520 ret = cpsw_slave_update_link(slave, priv, NULL);
Mugunthan V Nc3fdab42013-02-19 21:34:44 +0000521
Sekhar Norib5988852017-05-08 20:49:56 +0530522 return ret;
Mugunthan V Nc3fdab42013-02-19 21:34:44 +0000523}
524
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000525static inline u32 cpsw_get_slave_port(struct cpsw_priv *priv, u32 slave_num)
526{
527 if (priv->host_port == 0)
528 return slave_num + 1;
529 else
530 return slave_num;
531}
532
533static void cpsw_slave_init(struct cpsw_slave *slave, struct cpsw_priv *priv)
534{
535 u32 slave_port;
536
537 setbit_and_wait_for_clear32(&slave->sliver->soft_reset);
538
539 /* setup priority mapping */
540 __raw_writel(0x76543210, &slave->sliver->rx_pri_map);
541 __raw_writel(0x33221100, &slave->regs->tx_pri_map);
542
543 /* setup max packet size, and mac address */
544 __raw_writel(PKT_MAX, &slave->sliver->rx_maxlen);
545 cpsw_set_slave_mac(slave, priv);
546
547 slave->mac_control = 0; /* no link yet */
548
549 /* enable forwarding */
550 slave_port = cpsw_get_slave_port(priv, slave->slave_num);
551 cpsw_ale_port_state(priv, slave_port, ALE_PORT_STATE_FORWARD);
552
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500553 cpsw_ale_add_mcast(priv, net_bcast_ethaddr, 1 << slave_port);
Mugunthan V Nc3fdab42013-02-19 21:34:44 +0000554
Mugunthan V N4944f372014-02-18 07:31:52 -0500555 priv->phy_mask |= 1 << slave->data->phy_addr;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000556}
557
558static struct cpdma_desc *cpdma_desc_alloc(struct cpsw_priv *priv)
559{
560 struct cpdma_desc *desc = priv->desc_free;
561
562 if (desc)
563 priv->desc_free = desc_read_ptr(desc, hw_next);
564 return desc;
565}
566
567static void cpdma_desc_free(struct cpsw_priv *priv, struct cpdma_desc *desc)
568{
569 if (desc) {
570 desc_write(desc, hw_next, priv->desc_free);
571 priv->desc_free = desc;
572 }
573}
574
575static int cpdma_submit(struct cpsw_priv *priv, struct cpdma_chan *chan,
576 void *buffer, int len)
577{
578 struct cpdma_desc *desc, *prev;
579 u32 mode;
580
581 desc = cpdma_desc_alloc(priv);
582 if (!desc)
583 return -ENOMEM;
584
585 if (len < PKT_MIN)
586 len = PKT_MIN;
587
588 mode = CPDMA_DESC_OWNER | CPDMA_DESC_SOP | CPDMA_DESC_EOP;
589
590 desc_write(desc, hw_next, 0);
591 desc_write(desc, hw_buffer, buffer);
592 desc_write(desc, hw_len, len);
593 desc_write(desc, hw_mode, mode | len);
594 desc_write(desc, sw_buffer, buffer);
595 desc_write(desc, sw_len, len);
596
597 if (!chan->head) {
598 /* simple case - first packet enqueued */
599 chan->head = desc;
600 chan->tail = desc;
601 chan_write(chan, hdp, desc);
602 goto done;
603 }
604
605 /* not the first packet - enqueue at the tail */
606 prev = chan->tail;
607 desc_write(prev, hw_next, desc);
608 chan->tail = desc;
609
610 /* next check if EOQ has been triggered already */
611 if (desc_read(prev, hw_mode) & CPDMA_DESC_EOQ)
612 chan_write(chan, hdp, desc);
613
614done:
615 if (chan->rxfree)
616 chan_write(chan, rxfree, 1);
617 return 0;
618}
619
620static int cpdma_process(struct cpsw_priv *priv, struct cpdma_chan *chan,
621 void **buffer, int *len)
622{
623 struct cpdma_desc *desc = chan->head;
624 u32 status;
625
626 if (!desc)
627 return -ENOENT;
628
629 status = desc_read(desc, hw_mode);
630
631 if (len)
632 *len = status & 0x7ff;
633
634 if (buffer)
635 *buffer = desc_read_ptr(desc, sw_buffer);
636
637 if (status & CPDMA_DESC_OWNER) {
638 if (chan_read(chan, hdp) == 0) {
639 if (desc_read(desc, hw_mode) & CPDMA_DESC_OWNER)
640 chan_write(chan, hdp, desc);
641 }
642
643 return -EBUSY;
644 }
645
646 chan->head = desc_read_ptr(desc, hw_next);
647 chan_write(chan, cp, desc);
648
649 cpdma_desc_free(priv, desc);
650 return 0;
651}
652
Mugunthan V N6af98c52015-09-07 14:22:20 +0530653static int _cpsw_init(struct cpsw_priv *priv, u8 *enetaddr)
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000654{
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000655 struct cpsw_slave *slave;
656 int i, ret;
657
658 /* soft reset the controller and initialize priv */
659 setbit_and_wait_for_clear32(&priv->regs->soft_reset);
660
661 /* initialize and reset the address lookup engine */
662 cpsw_ale_enable(priv, 1);
663 cpsw_ale_clear(priv, 1);
664 cpsw_ale_vlan_aware(priv, 0); /* vlan unaware mode */
665
666 /* setup host port priority mapping */
667 __raw_writel(0x76543210, &priv->host_port_regs->cpdma_tx_pri_map);
668 __raw_writel(0, &priv->host_port_regs->cpdma_rx_chan_map);
669
670 /* disable priority elevation and enable statistics on all ports */
671 __raw_writel(0, &priv->regs->ptype);
672
673 /* enable statistics collection only on the host port */
674 __raw_writel(BIT(priv->host_port), &priv->regs->stat_port_en);
Mugunthan V N2782f3e2013-07-08 16:04:38 +0530675 __raw_writel(0x7, &priv->regs->stat_port_en);
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000676
677 cpsw_ale_port_state(priv, priv->host_port, ALE_PORT_STATE_FORWARD);
678
Mugunthan V N6af98c52015-09-07 14:22:20 +0530679 cpsw_ale_add_ucast(priv, enetaddr, priv->host_port, ALE_SECURE);
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500680 cpsw_ale_add_mcast(priv, net_bcast_ethaddr, 1 << priv->host_port);
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000681
Mugunthan V N33e073e2014-05-22 14:37:10 +0530682 for_active_slave(slave, priv)
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000683 cpsw_slave_init(slave, priv);
684
Sekhar Norib5988852017-05-08 20:49:56 +0530685 ret = cpsw_update_link(priv);
686 if (ret)
687 goto out;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000688
689 /* init descriptor pool */
690 for (i = 0; i < NUM_DESCS; i++) {
691 desc_write(&priv->descs[i], hw_next,
692 (i == (NUM_DESCS - 1)) ? 0 : &priv->descs[i+1]);
693 }
694 priv->desc_free = &priv->descs[0];
695
696 /* initialize channels */
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530697 if (priv->data->version == CPSW_CTRL_VERSION_2) {
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000698 memset(&priv->rx_chan, 0, sizeof(struct cpdma_chan));
699 priv->rx_chan.hdp = priv->dma_regs + CPDMA_RXHDP_VER2;
700 priv->rx_chan.cp = priv->dma_regs + CPDMA_RXCP_VER2;
701 priv->rx_chan.rxfree = priv->dma_regs + CPDMA_RXFREE;
702
703 memset(&priv->tx_chan, 0, sizeof(struct cpdma_chan));
704 priv->tx_chan.hdp = priv->dma_regs + CPDMA_TXHDP_VER2;
705 priv->tx_chan.cp = priv->dma_regs + CPDMA_TXCP_VER2;
706 } else {
707 memset(&priv->rx_chan, 0, sizeof(struct cpdma_chan));
708 priv->rx_chan.hdp = priv->dma_regs + CPDMA_RXHDP_VER1;
709 priv->rx_chan.cp = priv->dma_regs + CPDMA_RXCP_VER1;
710 priv->rx_chan.rxfree = priv->dma_regs + CPDMA_RXFREE;
711
712 memset(&priv->tx_chan, 0, sizeof(struct cpdma_chan));
713 priv->tx_chan.hdp = priv->dma_regs + CPDMA_TXHDP_VER1;
714 priv->tx_chan.cp = priv->dma_regs + CPDMA_TXCP_VER1;
715 }
716
717 /* clear dma state */
718 setbit_and_wait_for_clear32(priv->dma_regs + CPDMA_SOFTRESET);
719
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530720 if (priv->data->version == CPSW_CTRL_VERSION_2) {
721 for (i = 0; i < priv->data->channels; i++) {
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000722 __raw_writel(0, priv->dma_regs + CPDMA_RXHDP_VER2 + 4
723 * i);
724 __raw_writel(0, priv->dma_regs + CPDMA_RXFREE + 4
725 * i);
726 __raw_writel(0, priv->dma_regs + CPDMA_RXCP_VER2 + 4
727 * i);
728 __raw_writel(0, priv->dma_regs + CPDMA_TXHDP_VER2 + 4
729 * i);
730 __raw_writel(0, priv->dma_regs + CPDMA_TXCP_VER2 + 4
731 * i);
732 }
733 } else {
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530734 for (i = 0; i < priv->data->channels; i++) {
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000735 __raw_writel(0, priv->dma_regs + CPDMA_RXHDP_VER1 + 4
736 * i);
737 __raw_writel(0, priv->dma_regs + CPDMA_RXFREE + 4
738 * i);
739 __raw_writel(0, priv->dma_regs + CPDMA_RXCP_VER1 + 4
740 * i);
741 __raw_writel(0, priv->dma_regs + CPDMA_TXHDP_VER1 + 4
742 * i);
743 __raw_writel(0, priv->dma_regs + CPDMA_TXCP_VER1 + 4
744 * i);
745
746 }
747 }
748
749 __raw_writel(1, priv->dma_regs + CPDMA_TXCONTROL);
750 __raw_writel(1, priv->dma_regs + CPDMA_RXCONTROL);
751
752 /* submit rx descs */
753 for (i = 0; i < PKTBUFSRX; i++) {
Joe Hershberger9f09a362015-04-08 01:41:06 -0500754 ret = cpdma_submit(priv, &priv->rx_chan, net_rx_packets[i],
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000755 PKTSIZE);
756 if (ret < 0) {
757 printf("error %d submitting rx desc\n", ret);
758 break;
759 }
760 }
761
Sekhar Norib5988852017-05-08 20:49:56 +0530762out:
763 return ret;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000764}
765
Alex Kiernane210e982018-05-12 07:30:02 +0000766static int cpsw_reap_completed_packets(struct cpsw_priv *priv)
767{
768 int timeout = CPDMA_TIMEOUT;
769
770 /* reap completed packets */
771 while (timeout-- &&
772 (cpdma_process(priv, &priv->tx_chan, NULL, NULL) >= 0))
773 ;
774
775 return timeout;
776}
777
Mugunthan V N6af98c52015-09-07 14:22:20 +0530778static void _cpsw_halt(struct cpsw_priv *priv)
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000779{
Alex Kiernane210e982018-05-12 07:30:02 +0000780 cpsw_reap_completed_packets(priv);
781
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000782 writel(0, priv->dma_regs + CPDMA_TXCONTROL);
783 writel(0, priv->dma_regs + CPDMA_RXCONTROL);
784
785 /* soft reset the controller and initialize priv */
786 setbit_and_wait_for_clear32(&priv->regs->soft_reset);
787
788 /* clear dma state */
789 setbit_and_wait_for_clear32(priv->dma_regs + CPDMA_SOFTRESET);
790
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000791}
792
Mugunthan V N6af98c52015-09-07 14:22:20 +0530793static int _cpsw_send(struct cpsw_priv *priv, void *packet, int length)
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000794{
Alex Kiernane210e982018-05-12 07:30:02 +0000795 int timeout;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000796
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000797 flush_dcache_range((unsigned long)packet,
Lokesh Vutlaf9f4ced2016-08-11 13:00:59 +0530798 (unsigned long)packet + ALIGN(length, PKTALIGN));
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000799
Alex Kiernane210e982018-05-12 07:30:02 +0000800 timeout = cpsw_reap_completed_packets(priv);
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000801 if (timeout == -1) {
802 printf("cpdma_process timeout\n");
803 return -ETIMEDOUT;
804 }
805
806 return cpdma_submit(priv, &priv->tx_chan, packet, length);
807}
808
Mugunthan V N6af98c52015-09-07 14:22:20 +0530809static int _cpsw_recv(struct cpsw_priv *priv, uchar **pkt)
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000810{
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000811 void *buffer;
812 int len;
Heinrich Schuchardt71bb8dd2018-03-18 11:24:38 +0100813 int ret;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000814
Mugunthan V N6af98c52015-09-07 14:22:20 +0530815 ret = cpdma_process(priv, &priv->rx_chan, &buffer, &len);
816 if (ret < 0)
817 return ret;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000818
Mugunthan V N6af98c52015-09-07 14:22:20 +0530819 invalidate_dcache_range((unsigned long)buffer,
820 (unsigned long)buffer + PKTSIZE_ALIGN);
821 *pkt = buffer;
822
823 return len;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000824}
825
826static void cpsw_slave_setup(struct cpsw_slave *slave, int slave_num,
827 struct cpsw_priv *priv)
828{
829 void *regs = priv->regs;
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530830 struct cpsw_slave_data *data = priv->data->slave_data + slave_num;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000831 slave->slave_num = slave_num;
832 slave->data = data;
833 slave->regs = regs + data->slave_reg_ofs;
834 slave->sliver = regs + data->sliver_reg_ofs;
835}
836
Mugunthan V N6af98c52015-09-07 14:22:20 +0530837static int cpsw_phy_init(struct cpsw_priv *priv, struct cpsw_slave *slave)
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000838{
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000839 struct phy_device *phydev;
Ilya Ledvicha1635f02014-03-12 11:26:30 +0200840 u32 supported = PHY_GBIT_FEATURES;
Grygorii Strashkoe32ed8c2019-09-19 11:16:39 +0300841 int ret;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000842
Yegor Yefremov062bbac2012-11-26 04:03:16 +0000843 phydev = phy_connect(priv->bus,
Mugunthan V N4944f372014-02-18 07:31:52 -0500844 slave->data->phy_addr,
Mugunthan V N6af98c52015-09-07 14:22:20 +0530845 priv->dev,
Yegor Yefremov062bbac2012-11-26 04:03:16 +0000846 slave->data->phy_if);
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000847
Heiko Schocheref660032013-09-05 11:50:41 +0200848 if (!phydev)
849 return -1;
850
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000851 phydev->supported &= supported;
Grygorii Strashkoe32ed8c2019-09-19 11:16:39 +0300852 if (slave->data->max_speed) {
853 ret = phy_set_supported(phydev, slave->data->max_speed);
854 if (ret)
855 return ret;
856 dev_dbg(priv->dev, "Port %u speed forced to %uMbit\n",
857 slave->slave_num + 1, slave->data->max_speed);
858 }
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000859 phydev->advertising = phydev->supported;
860
Dan Murphy4b7b24e2016-05-02 15:45:56 -0500861#ifdef CONFIG_DM_ETH
Grygorii Strashko133dd1c2019-09-19 11:16:42 +0300862 if (ofnode_valid(slave->data->phy_of_handle))
863 phydev->node = slave->data->phy_of_handle;
Dan Murphy4b7b24e2016-05-02 15:45:56 -0500864#endif
865
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000866 priv->phydev = phydev;
867 phy_config(phydev);
868
869 return 1;
870}
871
Sekhar Noricfc5cc82018-08-23 17:11:29 +0530872static void cpsw_phy_addr_update(struct cpsw_priv *priv)
873{
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530874 struct cpsw_platform_data *data = priv->data;
Grygorii Strashkoeae545a2018-10-31 16:21:44 -0500875 u16 alive = cpsw_mdio_get_alive(priv->bus);
Sekhar Noricfc5cc82018-08-23 17:11:29 +0530876 int active = data->active_slave;
877 int new_addr = ffs(alive) - 1;
878
879 /*
880 * If there is only one phy alive and its address does not match
881 * that of active slave, then phy address can safely be updated.
882 */
883 if (hweight16(alive) == 1 &&
884 data->slave_data[active].phy_addr != new_addr) {
885 printf("Updated phy address for CPSW#%d, old: %d, new: %d\n",
886 active, data->slave_data[active].phy_addr, new_addr);
887 data->slave_data[active].phy_addr = new_addr;
888 }
889}
890
Mugunthan V N6af98c52015-09-07 14:22:20 +0530891int _cpsw_register(struct cpsw_priv *priv)
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000892{
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000893 struct cpsw_slave *slave;
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530894 struct cpsw_platform_data *data = priv->data;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000895 void *regs = (void *)data->cpsw_base;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000896
897 priv->slaves = malloc(sizeof(struct cpsw_slave) * data->slaves);
898 if (!priv->slaves) {
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000899 return -ENOMEM;
900 }
901
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000902 priv->host_port = data->host_port_num;
903 priv->regs = regs;
904 priv->host_port_regs = regs + data->host_port_reg_ofs;
905 priv->dma_regs = regs + data->cpdma_reg_ofs;
906 priv->ale_regs = regs + data->ale_reg_ofs;
Mugunthan V Nff559872013-07-08 16:04:37 +0530907 priv->descs = (void *)regs + data->bd_ram_ofs;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000908
909 int idx = 0;
910
911 for_each_slave(slave, priv) {
912 cpsw_slave_setup(slave, idx, priv);
913 idx = idx + 1;
914 }
915
Grygorii Strashkoeae545a2018-10-31 16:21:44 -0500916 priv->bus = cpsw_mdio_init(priv->dev->name, data->mdio_base, 0, 0);
917 if (!priv->bus)
918 return -EFAULT;
Sekhar Noricfc5cc82018-08-23 17:11:29 +0530919
920 cpsw_phy_addr_update(priv);
921
Mugunthan V N6af98c52015-09-07 14:22:20 +0530922 for_active_slave(slave, priv)
923 cpsw_phy_init(priv, slave);
924
925 return 0;
926}
927
Mugunthan V N7ae228c2015-09-07 14:22:21 +0530928#ifndef CONFIG_DM_ETH
Mugunthan V N6af98c52015-09-07 14:22:20 +0530929static int cpsw_init(struct eth_device *dev, bd_t *bis)
930{
931 struct cpsw_priv *priv = dev->priv;
932
933 return _cpsw_init(priv, dev->enetaddr);
934}
935
936static void cpsw_halt(struct eth_device *dev)
937{
938 struct cpsw_priv *priv = dev->priv;
939
940 return _cpsw_halt(priv);
941}
942
943static int cpsw_send(struct eth_device *dev, void *packet, int length)
944{
945 struct cpsw_priv *priv = dev->priv;
946
947 return _cpsw_send(priv, packet, length);
948}
949
950static int cpsw_recv(struct eth_device *dev)
951{
952 struct cpsw_priv *priv = dev->priv;
953 uchar *pkt = NULL;
954 int len;
955
956 len = _cpsw_recv(priv, &pkt);
957
958 if (len > 0) {
959 net_process_received_packet(pkt, len);
960 cpdma_submit(priv, &priv->rx_chan, pkt, PKTSIZE);
961 }
962
963 return len;
964}
965
966int cpsw_register(struct cpsw_platform_data *data)
967{
968 struct cpsw_priv *priv;
969 struct eth_device *dev;
970 int ret;
971
972 dev = calloc(sizeof(*dev), 1);
973 if (!dev)
974 return -ENOMEM;
975
976 priv = calloc(sizeof(*priv), 1);
977 if (!priv) {
978 free(dev);
979 return -ENOMEM;
980 }
981
982 priv->dev = dev;
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530983 priv->data = data;
Mugunthan V N6af98c52015-09-07 14:22:20 +0530984
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000985 strcpy(dev->name, "cpsw");
986 dev->iobase = 0;
987 dev->init = cpsw_init;
988 dev->halt = cpsw_halt;
989 dev->send = cpsw_send;
990 dev->recv = cpsw_recv;
991 dev->priv = priv;
992
993 eth_register(dev);
994
Mugunthan V N6af98c52015-09-07 14:22:20 +0530995 ret = _cpsw_register(priv);
996 if (ret < 0) {
997 eth_unregister(dev);
998 free(dev);
999 free(priv);
1000 return ret;
1001 }
Cyril Chemparathy464adc82012-07-24 12:22:16 +00001002
1003 return 1;
1004}
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301005#else
1006static int cpsw_eth_start(struct udevice *dev)
1007{
1008 struct eth_pdata *pdata = dev_get_platdata(dev);
1009 struct cpsw_priv *priv = dev_get_priv(dev);
1010
1011 return _cpsw_init(priv, pdata->enetaddr);
1012}
1013
1014static int cpsw_eth_send(struct udevice *dev, void *packet, int length)
1015{
1016 struct cpsw_priv *priv = dev_get_priv(dev);
1017
1018 return _cpsw_send(priv, packet, length);
1019}
1020
1021static int cpsw_eth_recv(struct udevice *dev, int flags, uchar **packetp)
1022{
1023 struct cpsw_priv *priv = dev_get_priv(dev);
1024
1025 return _cpsw_recv(priv, packetp);
1026}
1027
1028static int cpsw_eth_free_pkt(struct udevice *dev, uchar *packet,
1029 int length)
1030{
1031 struct cpsw_priv *priv = dev_get_priv(dev);
1032
1033 return cpdma_submit(priv, &priv->rx_chan, packet, PKTSIZE);
1034}
1035
1036static void cpsw_eth_stop(struct udevice *dev)
1037{
1038 struct cpsw_priv *priv = dev_get_priv(dev);
1039
1040 return _cpsw_halt(priv);
1041}
1042
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301043static const struct eth_ops cpsw_eth_ops = {
1044 .start = cpsw_eth_start,
1045 .send = cpsw_eth_send,
1046 .recv = cpsw_eth_recv,
1047 .free_pkt = cpsw_eth_free_pkt,
1048 .stop = cpsw_eth_stop,
1049};
1050
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301051static void cpsw_gmii_sel_am3352(struct cpsw_priv *priv,
1052 phy_interface_t phy_mode)
1053{
1054 u32 reg;
1055 u32 mask;
1056 u32 mode = 0;
1057 bool rgmii_id = false;
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301058 int slave = priv->data->active_slave;
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301059
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301060 reg = readl(priv->data->gmii_sel);
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301061
1062 switch (phy_mode) {
1063 case PHY_INTERFACE_MODE_RMII:
1064 mode = AM33XX_GMII_SEL_MODE_RMII;
1065 break;
1066
1067 case PHY_INTERFACE_MODE_RGMII:
Grygorii Strashko40b346d2019-09-19 11:16:40 +03001068 case PHY_INTERFACE_MODE_RGMII_RXID:
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301069 mode = AM33XX_GMII_SEL_MODE_RGMII;
1070 break;
1071 case PHY_INTERFACE_MODE_RGMII_ID:
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301072 case PHY_INTERFACE_MODE_RGMII_TXID:
1073 mode = AM33XX_GMII_SEL_MODE_RGMII;
1074 rgmii_id = true;
1075 break;
1076
1077 case PHY_INTERFACE_MODE_MII:
1078 default:
1079 mode = AM33XX_GMII_SEL_MODE_MII;
1080 break;
1081 };
1082
1083 mask = GMII_SEL_MODE_MASK << (slave * 2) | BIT(slave + 6);
1084 mode <<= slave * 2;
1085
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301086 if (priv->data->rmii_clock_external) {
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301087 if (slave == 0)
1088 mode |= AM33XX_GMII_SEL_RMII1_IO_CLK_EN;
1089 else
1090 mode |= AM33XX_GMII_SEL_RMII2_IO_CLK_EN;
1091 }
1092
1093 if (rgmii_id) {
1094 if (slave == 0)
1095 mode |= AM33XX_GMII_SEL_RGMII1_IDMODE;
1096 else
1097 mode |= AM33XX_GMII_SEL_RGMII2_IDMODE;
1098 }
1099
1100 reg &= ~mask;
1101 reg |= mode;
1102
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301103 writel(reg, priv->data->gmii_sel);
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301104}
1105
1106static void cpsw_gmii_sel_dra7xx(struct cpsw_priv *priv,
1107 phy_interface_t phy_mode)
1108{
1109 u32 reg;
1110 u32 mask;
1111 u32 mode = 0;
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301112 int slave = priv->data->active_slave;
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301113
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301114 reg = readl(priv->data->gmii_sel);
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301115
1116 switch (phy_mode) {
1117 case PHY_INTERFACE_MODE_RMII:
1118 mode = AM33XX_GMII_SEL_MODE_RMII;
1119 break;
1120
1121 case PHY_INTERFACE_MODE_RGMII:
1122 case PHY_INTERFACE_MODE_RGMII_ID:
1123 case PHY_INTERFACE_MODE_RGMII_RXID:
1124 case PHY_INTERFACE_MODE_RGMII_TXID:
1125 mode = AM33XX_GMII_SEL_MODE_RGMII;
1126 break;
1127
1128 case PHY_INTERFACE_MODE_MII:
1129 default:
1130 mode = AM33XX_GMII_SEL_MODE_MII;
1131 break;
1132 };
1133
1134 switch (slave) {
1135 case 0:
1136 mask = GMII_SEL_MODE_MASK;
1137 break;
1138 case 1:
1139 mask = GMII_SEL_MODE_MASK << 4;
1140 mode <<= 4;
1141 break;
1142 default:
1143 dev_err(priv->dev, "invalid slave number...\n");
1144 return;
1145 }
1146
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301147 if (priv->data->rmii_clock_external)
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301148 dev_err(priv->dev, "RMII External clock is not supported\n");
1149
1150 reg &= ~mask;
1151 reg |= mode;
1152
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301153 writel(reg, priv->data->gmii_sel);
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301154}
1155
1156static void cpsw_phy_sel(struct cpsw_priv *priv, const char *compat,
1157 phy_interface_t phy_mode)
1158{
1159 if (!strcmp(compat, "ti,am3352-cpsw-phy-sel"))
1160 cpsw_gmii_sel_am3352(priv, phy_mode);
1161 if (!strcmp(compat, "ti,am43xx-cpsw-phy-sel"))
1162 cpsw_gmii_sel_am3352(priv, phy_mode);
1163 else if (!strcmp(compat, "ti,dra7xx-cpsw-phy-sel"))
1164 cpsw_gmii_sel_dra7xx(priv, phy_mode);
1165}
1166
Faiz Abbas5b5dc0f2019-03-18 13:54:32 +05301167static int cpsw_eth_probe(struct udevice *dev)
1168{
1169 struct cpsw_priv *priv = dev_get_priv(dev);
1170 struct eth_pdata *pdata = dev_get_platdata(dev);
1171
1172 priv->dev = dev;
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301173 priv->data = pdata->priv_pdata;
Faiz Abbas8ecdffe2019-03-18 13:54:34 +05301174 ti_cm_get_macid(dev, priv->data, pdata->enetaddr);
Faiz Abbas5b5dc0f2019-03-18 13:54:32 +05301175 /* Select phy interface in control module */
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301176 cpsw_phy_sel(priv, priv->data->phy_sel_compat,
Faiz Abbas5b5dc0f2019-03-18 13:54:32 +05301177 pdata->phy_interface);
1178
1179 return _cpsw_register(priv);
1180}
1181
Faiz Abbas687e80e2019-03-18 13:54:35 +05301182#if CONFIG_IS_ENABLED(OF_CONTROL)
Grygorii Strashko68e313e2019-09-19 11:16:38 +03001183static void cpsw_eth_of_parse_slave(struct cpsw_platform_data *data,
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001184 int slave_index, ofnode subnode)
Grygorii Strashko68e313e2019-09-19 11:16:38 +03001185{
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001186 struct ofnode_phandle_args out_args;
1187 struct cpsw_slave_data *slave_data;
Grygorii Strashko68e313e2019-09-19 11:16:38 +03001188 const char *phy_mode;
1189 u32 phy_id[2];
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001190 int ret;
Grygorii Strashko68e313e2019-09-19 11:16:38 +03001191
1192 slave_data = &data->slave_data[slave_index];
1193
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001194 phy_mode = ofnode_read_string(subnode, "phy-mode");
Grygorii Strashko68e313e2019-09-19 11:16:38 +03001195 if (phy_mode)
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001196 slave_data->phy_if = phy_get_interface_by_name(phy_mode);
Grygorii Strashko68e313e2019-09-19 11:16:38 +03001197
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001198 ret = ofnode_parse_phandle_with_args(subnode, "phy-handle",
1199 NULL, 0, 0, &out_args);
1200 if (!ret) {
1201 slave_data->phy_of_handle = out_args.node;
Grygorii Strashko68e313e2019-09-19 11:16:38 +03001202
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001203 ret = ofnode_read_s32(slave_data->phy_of_handle, "reg",
1204 &slave_data->phy_addr);
1205 if (ret)
1206 printf("error: phy addr not found in dt\n");
Grygorii Strashko68e313e2019-09-19 11:16:38 +03001207 } else {
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001208 ret = ofnode_read_u32_array(subnode, "phy_id", phy_id, 2);
1209 if (ret)
1210 printf("error: phy_id read failed\n");
Grygorii Strashko68e313e2019-09-19 11:16:38 +03001211 }
Grygorii Strashkoe32ed8c2019-09-19 11:16:39 +03001212
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001213 slave_data->max_speed = ofnode_read_s32_default(subnode,
1214 "max-speed", 0);
Grygorii Strashko68e313e2019-09-19 11:16:38 +03001215}
1216
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301217static int cpsw_eth_ofdata_to_platdata(struct udevice *dev)
1218{
1219 struct eth_pdata *pdata = dev_get_platdata(dev);
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301220 struct cpsw_platform_data *data;
Vignesh R8e9473d2016-08-02 10:14:27 +05301221 struct gpio_desc *mode_gpios;
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301222 int slave_index = 0;
Vignesh R8e9473d2016-08-02 10:14:27 +05301223 int num_mode_gpios;
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001224 ofnode subnode;
Mugunthan V N9e556352016-04-28 15:36:07 +05301225 int ret;
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301226
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301227 data = calloc(1, sizeof(struct cpsw_platform_data));
Faiz Abbas9e0e1f32019-11-11 15:22:56 +05301228 if (!data)
1229 return -ENOMEM;
1230
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301231 pdata->priv_pdata = data;
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001232 pdata->iobase = dev_read_addr(dev);
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301233 data->version = CPSW_CTRL_VERSION_2;
1234 data->bd_ram_ofs = CPSW_BD_OFFSET;
1235 data->ale_reg_ofs = CPSW_ALE_OFFSET;
1236 data->cpdma_reg_ofs = CPSW_CPDMA_OFFSET;
1237 data->mdio_div = CPSW_MDIO_DIV;
1238 data->host_port_reg_ofs = CPSW_HOST_PORT_OFFSET,
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301239
1240 pdata->phy_interface = -1;
1241
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301242 data->cpsw_base = pdata->iobase;
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001243
1244 ret = dev_read_s32(dev, "cpdma_channels", &data->channels);
1245 if (ret) {
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301246 printf("error: cpdma_channels not found in dt\n");
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001247 return ret;
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301248 }
1249
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001250 ret = dev_read_s32(dev, "slaves", &data->slaves);
1251 if (ret) {
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301252 printf("error: slaves not found in dt\n");
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001253 return ret;
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301254 }
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301255 data->slave_data = malloc(sizeof(struct cpsw_slave_data) *
1256 data->slaves);
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301257
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001258 ret = dev_read_s32(dev, "ale_entries", &data->ale_entries);
1259 if (ret) {
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301260 printf("error: ale_entries not found in dt\n");
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001261 return ret;
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301262 }
1263
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001264 ret = dev_read_u32(dev, "bd_ram_size", &data->bd_ram_ofs);
1265 if (ret) {
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301266 printf("error: bd_ram_size not found in dt\n");
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001267 return ret;
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301268 }
1269
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001270 ret = dev_read_u32(dev, "mac_control", &data->mac_control);
1271 if (ret) {
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301272 printf("error: ale_entries not found in dt\n");
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001273 return ret;
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301274 }
1275
Vignesh R8e9473d2016-08-02 10:14:27 +05301276 num_mode_gpios = gpio_get_list_count(dev, "mode-gpios");
1277 if (num_mode_gpios > 0) {
1278 mode_gpios = malloc(sizeof(struct gpio_desc) *
1279 num_mode_gpios);
1280 gpio_request_list_by_name(dev, "mode-gpios", mode_gpios,
1281 num_mode_gpios, GPIOD_IS_OUT);
1282 free(mode_gpios);
1283 }
1284
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001285 data->active_slave = dev_read_u32_default(dev, "active_slave", 0);
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301286
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001287 ofnode_for_each_subnode(subnode, dev_ofnode(dev)) {
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301288 const char *name;
1289
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001290 name = ofnode_get_name(subnode);
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301291 if (!strncmp(name, "mdio", 4)) {
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001292 data->mdio_base = ofnode_get_addr(subnode);
1293 if (data->mdio_base == FDT_ADDR_T_NONE) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001294 pr_err("Not able to get MDIO address space\n");
Mugunthan V N77f7bc72016-04-28 15:36:06 +05301295 return -ENOENT;
1296 }
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301297 }
1298
1299 if (!strncmp(name, "slave", 5)) {
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301300 if (slave_index >= data->slaves)
Mugunthan V N13a27fd2016-04-28 15:36:04 +05301301 continue;
Dan Murphy4b7b24e2016-05-02 15:45:56 -05001302
Grygorii Strashko68e313e2019-09-19 11:16:38 +03001303 cpsw_eth_of_parse_slave(data, slave_index, subnode);
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301304 slave_index++;
1305 }
1306
1307 if (!strncmp(name, "cpsw-phy-sel", 12)) {
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001308 data->gmii_sel = ofnode_get_addr(subnode);
Mugunthan V N77f7bc72016-04-28 15:36:06 +05301309
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301310 if (data->gmii_sel == FDT_ADDR_T_NONE) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001311 pr_err("Not able to get gmii_sel reg address\n");
Mugunthan V N77f7bc72016-04-28 15:36:06 +05301312 return -ENOENT;
1313 }
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301314
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001315 if (ofnode_read_bool(subnode, "rmii-clock-ext"))
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301316 data->rmii_clock_external = true;
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301317
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001318 data->phy_sel_compat = ofnode_read_string(subnode,
1319 "compatible");
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301320 if (!data->phy_sel_compat) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001321 pr_err("Not able to get gmii_sel compatible\n");
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301322 return -ENOENT;
1323 }
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301324 }
1325 }
1326
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301327 data->slave_data[0].slave_reg_ofs = CPSW_SLAVE0_OFFSET;
1328 data->slave_data[0].sliver_reg_ofs = CPSW_SLIVER0_OFFSET;
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301329
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301330 if (data->slaves == 2) {
1331 data->slave_data[1].slave_reg_ofs = CPSW_SLAVE1_OFFSET;
1332 data->slave_data[1].sliver_reg_ofs = CPSW_SLIVER1_OFFSET;
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301333 }
1334
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001335 ret = ti_cm_get_macid_addr(dev, data->active_slave, data);
Mugunthan V N9e556352016-04-28 15:36:07 +05301336 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001337 pr_err("cpsw read efuse mac failed\n");
Mugunthan V N9e556352016-04-28 15:36:07 +05301338 return ret;
1339 }
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301340
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001341 pdata->phy_interface = data->slave_data[data->active_slave].phy_if;
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301342 if (pdata->phy_interface == -1) {
Grygorii Strashko68e313e2019-09-19 11:16:38 +03001343 debug("%s: Invalid PHY interface '%s'\n", __func__,
1344 phy_string_for_interface(pdata->phy_interface));
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301345 return -EINVAL;
1346 }
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301347
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301348 return 0;
1349}
1350
Faiz Abbas687e80e2019-03-18 13:54:35 +05301351static const struct udevice_id cpsw_eth_ids[] = {
1352 { .compatible = "ti,cpsw" },
1353 { .compatible = "ti,am335x-cpsw" },
1354 { }
1355};
1356#endif
1357
Sekhar Noricfc5cc82018-08-23 17:11:29 +05301358int cpsw_get_slave_phy_addr(struct udevice *dev, int slave)
1359{
1360 struct cpsw_priv *priv = dev_get_priv(dev);
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301361 struct cpsw_platform_data *data = priv->data;
Sekhar Noricfc5cc82018-08-23 17:11:29 +05301362
1363 return data->slave_data[slave].phy_addr;
1364}
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301365
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301366U_BOOT_DRIVER(eth_cpsw) = {
1367 .name = "eth_cpsw",
1368 .id = UCLASS_ETH,
Faiz Abbas687e80e2019-03-18 13:54:35 +05301369#if CONFIG_IS_ENABLED(OF_CONTROL)
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301370 .of_match = cpsw_eth_ids,
1371 .ofdata_to_platdata = cpsw_eth_ofdata_to_platdata,
Faiz Abbas687e80e2019-03-18 13:54:35 +05301372 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
1373#endif
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301374 .probe = cpsw_eth_probe,
1375 .ops = &cpsw_eth_ops,
1376 .priv_auto_alloc_size = sizeof(struct cpsw_priv),
Faiz Abbasd852b942019-03-18 13:54:36 +05301377 .flags = DM_FLAG_ALLOC_PRIV_DMA | DM_FLAG_PRE_RELOC,
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301378};
1379#endif /* CONFIG_DM_ETH */