blob: 4a990be93e999ec81ea0343e7d04eee18312c479 [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>
10#include <net.h>
11#include <miiphy.h>
12#include <malloc.h>
13#include <net.h>
14#include <netdev.h>
15#include <cpsw.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090016#include <linux/errno.h>
Vignesh R8e9473d2016-08-02 10:14:27 +053017#include <asm/gpio.h>
Cyril Chemparathy464adc82012-07-24 12:22:16 +000018#include <asm/io.h>
19#include <phy.h>
Tom Rini8eb48ff2013-03-14 11:15:25 +000020#include <asm/arch/cpu.h>
Mugunthan V N7ae228c2015-09-07 14:22:21 +053021#include <dm.h>
22
Grygorii Strashkoeae545a2018-10-31 16:21:44 -050023#include "cpsw_mdio.h"
24
Cyril Chemparathy464adc82012-07-24 12:22:16 +000025#define BITMASK(bits) (BIT(bits) - 1)
Cyril Chemparathy464adc82012-07-24 12:22:16 +000026#define NUM_DESCS (PKTBUFSRX * 2)
27#define PKT_MIN 60
28#define PKT_MAX (1500 + 14 + 4 + 4)
29#define CLEAR_BIT 1
30#define GIGABITEN BIT(7)
31#define FULLDUPLEXEN BIT(0)
32#define MIIEN BIT(15)
Grygorii Strashko63bba7e2019-09-19 11:16:37 +030033#define CTL_EXT_EN BIT(18)
Cyril Chemparathy464adc82012-07-24 12:22:16 +000034/* DMA Registers */
35#define CPDMA_TXCONTROL 0x004
36#define CPDMA_RXCONTROL 0x014
37#define CPDMA_SOFTRESET 0x01c
38#define CPDMA_RXFREE 0x0e0
39#define CPDMA_TXHDP_VER1 0x100
40#define CPDMA_TXHDP_VER2 0x200
41#define CPDMA_RXHDP_VER1 0x120
42#define CPDMA_RXHDP_VER2 0x220
43#define CPDMA_TXCP_VER1 0x140
44#define CPDMA_TXCP_VER2 0x240
45#define CPDMA_RXCP_VER1 0x160
46#define CPDMA_RXCP_VER2 0x260
47
Cyril Chemparathy464adc82012-07-24 12:22:16 +000048/* Descriptor mode bits */
49#define CPDMA_DESC_SOP BIT(31)
50#define CPDMA_DESC_EOP BIT(30)
51#define CPDMA_DESC_OWNER BIT(29)
52#define CPDMA_DESC_EOQ BIT(28)
53
54/*
55 * This timeout definition is a worst-case ultra defensive measure against
56 * unexpected controller lock ups. Ideally, we should never ever hit this
57 * scenario in practice.
58 */
Cyril Chemparathy464adc82012-07-24 12:22:16 +000059#define CPDMA_TIMEOUT 100 /* msecs */
60
Cyril Chemparathy464adc82012-07-24 12:22:16 +000061struct cpsw_regs {
62 u32 id_ver;
63 u32 control;
64 u32 soft_reset;
65 u32 stat_port_en;
66 u32 ptype;
67};
68
69struct cpsw_slave_regs {
70 u32 max_blks;
71 u32 blk_cnt;
72 u32 flow_thresh;
73 u32 port_vlan;
74 u32 tx_pri_map;
Matt Porter1ef0e872013-03-20 05:38:12 +000075#ifdef CONFIG_AM33XX
Cyril Chemparathy464adc82012-07-24 12:22:16 +000076 u32 gap_thresh;
Matt Porter1ef0e872013-03-20 05:38:12 +000077#elif defined(CONFIG_TI814X)
78 u32 ts_ctl;
79 u32 ts_seq_ltype;
80 u32 ts_vlan;
81#endif
Cyril Chemparathy464adc82012-07-24 12:22:16 +000082 u32 sa_lo;
83 u32 sa_hi;
84};
85
86struct cpsw_host_regs {
87 u32 max_blks;
88 u32 blk_cnt;
89 u32 flow_thresh;
90 u32 port_vlan;
91 u32 tx_pri_map;
92 u32 cpdma_tx_pri_map;
93 u32 cpdma_rx_chan_map;
94};
95
96struct cpsw_sliver_regs {
97 u32 id_ver;
98 u32 mac_control;
99 u32 mac_status;
100 u32 soft_reset;
101 u32 rx_maxlen;
102 u32 __reserved_0;
103 u32 rx_pause;
104 u32 tx_pause;
105 u32 __reserved_1;
106 u32 rx_pri_map;
107};
108
109#define ALE_ENTRY_BITS 68
110#define ALE_ENTRY_WORDS DIV_ROUND_UP(ALE_ENTRY_BITS, 32)
111
112/* ALE Registers */
113#define ALE_CONTROL 0x08
114#define ALE_UNKNOWNVLAN 0x18
115#define ALE_TABLE_CONTROL 0x20
116#define ALE_TABLE 0x34
117#define ALE_PORTCTL 0x40
118
119#define ALE_TABLE_WRITE BIT(31)
120
121#define ALE_TYPE_FREE 0
122#define ALE_TYPE_ADDR 1
123#define ALE_TYPE_VLAN 2
124#define ALE_TYPE_VLAN_ADDR 3
125
126#define ALE_UCAST_PERSISTANT 0
127#define ALE_UCAST_UNTOUCHED 1
128#define ALE_UCAST_OUI 2
129#define ALE_UCAST_TOUCHED 3
130
131#define ALE_MCAST_FWD 0
132#define ALE_MCAST_BLOCK_LEARN_FWD 1
133#define ALE_MCAST_FWD_LEARN 2
134#define ALE_MCAST_FWD_2 3
135
136enum cpsw_ale_port_state {
137 ALE_PORT_STATE_DISABLE = 0x00,
138 ALE_PORT_STATE_BLOCK = 0x01,
139 ALE_PORT_STATE_LEARN = 0x02,
140 ALE_PORT_STATE_FORWARD = 0x03,
141};
142
143/* ALE unicast entry flags - passed into cpsw_ale_add_ucast() */
144#define ALE_SECURE 1
145#define ALE_BLOCKED 2
146
147struct cpsw_slave {
148 struct cpsw_slave_regs *regs;
149 struct cpsw_sliver_regs *sliver;
150 int slave_num;
151 u32 mac_control;
152 struct cpsw_slave_data *data;
153};
154
155struct cpdma_desc {
156 /* hardware fields */
157 u32 hw_next;
158 u32 hw_buffer;
159 u32 hw_len;
160 u32 hw_mode;
161 /* software fields */
162 u32 sw_buffer;
163 u32 sw_len;
164};
165
166struct cpdma_chan {
167 struct cpdma_desc *head, *tail;
168 void *hdp, *cp, *rxfree;
169};
170
Mugunthan V N4d5fdb62016-10-13 19:33:38 +0530171/* AM33xx SoC specific definitions for the CONTROL port */
172#define AM33XX_GMII_SEL_MODE_MII 0
173#define AM33XX_GMII_SEL_MODE_RMII 1
174#define AM33XX_GMII_SEL_MODE_RGMII 2
175
176#define AM33XX_GMII_SEL_RGMII1_IDMODE BIT(4)
177#define AM33XX_GMII_SEL_RGMII2_IDMODE BIT(5)
178#define AM33XX_GMII_SEL_RMII1_IO_CLK_EN BIT(6)
179#define AM33XX_GMII_SEL_RMII2_IO_CLK_EN BIT(7)
180
181#define GMII_SEL_MODE_MASK 0x3
182
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000183#define desc_write(desc, fld, val) __raw_writel((u32)(val), &(desc)->fld)
184#define desc_read(desc, fld) __raw_readl(&(desc)->fld)
185#define desc_read_ptr(desc, fld) ((void *)__raw_readl(&(desc)->fld))
186
187#define chan_write(chan, fld, val) __raw_writel((u32)(val), (chan)->fld)
188#define chan_read(chan, fld) __raw_readl((chan)->fld)
189#define chan_read_ptr(chan, fld) ((void *)__raw_readl((chan)->fld))
190
Mugunthan V N33e073e2014-05-22 14:37:10 +0530191#define for_active_slave(slave, priv) \
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530192 slave = (priv)->slaves + ((priv)->data)->active_slave; if (slave)
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000193#define for_each_slave(slave, priv) \
194 for (slave = (priv)->slaves; slave != (priv)->slaves + \
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530195 ((priv)->data)->slaves; slave++)
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000196
197struct cpsw_priv {
Mugunthan V N7ae228c2015-09-07 14:22:21 +0530198#ifdef CONFIG_DM_ETH
199 struct udevice *dev;
200#else
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000201 struct eth_device *dev;
Mugunthan V N7ae228c2015-09-07 14:22:21 +0530202#endif
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530203 struct cpsw_platform_data *data;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000204 int host_port;
205
206 struct cpsw_regs *regs;
207 void *dma_regs;
208 struct cpsw_host_regs *host_port_regs;
209 void *ale_regs;
210
211 struct cpdma_desc *descs;
212 struct cpdma_desc *desc_free;
213 struct cpdma_chan rx_chan, tx_chan;
214
215 struct cpsw_slave *slaves;
216 struct phy_device *phydev;
217 struct mii_dev *bus;
Mugunthan V Nc3fdab42013-02-19 21:34:44 +0000218
Mugunthan V Nc3fdab42013-02-19 21:34:44 +0000219 u32 phy_mask;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000220};
221
222static inline int cpsw_ale_get_field(u32 *ale_entry, u32 start, u32 bits)
223{
224 int idx;
225
226 idx = start / 32;
227 start -= idx * 32;
228 idx = 2 - idx; /* flip */
229 return (ale_entry[idx] >> start) & BITMASK(bits);
230}
231
232static inline void cpsw_ale_set_field(u32 *ale_entry, u32 start, u32 bits,
233 u32 value)
234{
235 int idx;
236
237 value &= BITMASK(bits);
238 idx = start / 32;
239 start -= idx * 32;
240 idx = 2 - idx; /* flip */
241 ale_entry[idx] &= ~(BITMASK(bits) << start);
242 ale_entry[idx] |= (value << start);
243}
244
245#define DEFINE_ALE_FIELD(name, start, bits) \
246static inline int cpsw_ale_get_##name(u32 *ale_entry) \
247{ \
248 return cpsw_ale_get_field(ale_entry, start, bits); \
249} \
250static inline void cpsw_ale_set_##name(u32 *ale_entry, u32 value) \
251{ \
252 cpsw_ale_set_field(ale_entry, start, bits, value); \
253}
254
255DEFINE_ALE_FIELD(entry_type, 60, 2)
256DEFINE_ALE_FIELD(mcast_state, 62, 2)
257DEFINE_ALE_FIELD(port_mask, 66, 3)
258DEFINE_ALE_FIELD(ucast_type, 62, 2)
259DEFINE_ALE_FIELD(port_num, 66, 2)
260DEFINE_ALE_FIELD(blocked, 65, 1)
261DEFINE_ALE_FIELD(secure, 64, 1)
262DEFINE_ALE_FIELD(mcast, 40, 1)
263
264/* The MAC address field in the ALE entry cannot be macroized as above */
265static inline void cpsw_ale_get_addr(u32 *ale_entry, u8 *addr)
266{
267 int i;
268
269 for (i = 0; i < 6; i++)
270 addr[i] = cpsw_ale_get_field(ale_entry, 40 - 8*i, 8);
271}
272
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500273static inline void cpsw_ale_set_addr(u32 *ale_entry, const u8 *addr)
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000274{
275 int i;
276
277 for (i = 0; i < 6; i++)
278 cpsw_ale_set_field(ale_entry, 40 - 8*i, 8, addr[i]);
279}
280
281static int cpsw_ale_read(struct cpsw_priv *priv, int idx, u32 *ale_entry)
282{
283 int i;
284
285 __raw_writel(idx, priv->ale_regs + ALE_TABLE_CONTROL);
286
287 for (i = 0; i < ALE_ENTRY_WORDS; i++)
288 ale_entry[i] = __raw_readl(priv->ale_regs + ALE_TABLE + 4 * i);
289
290 return idx;
291}
292
293static int cpsw_ale_write(struct cpsw_priv *priv, int idx, u32 *ale_entry)
294{
295 int i;
296
297 for (i = 0; i < ALE_ENTRY_WORDS; i++)
298 __raw_writel(ale_entry[i], priv->ale_regs + ALE_TABLE + 4 * i);
299
300 __raw_writel(idx | ALE_TABLE_WRITE, priv->ale_regs + ALE_TABLE_CONTROL);
301
302 return idx;
303}
304
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500305static int cpsw_ale_match_addr(struct cpsw_priv *priv, const u8 *addr)
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000306{
307 u32 ale_entry[ALE_ENTRY_WORDS];
308 int type, idx;
309
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530310 for (idx = 0; idx < priv->data->ale_entries; idx++) {
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000311 u8 entry_addr[6];
312
313 cpsw_ale_read(priv, idx, ale_entry);
314 type = cpsw_ale_get_entry_type(ale_entry);
315 if (type != ALE_TYPE_ADDR && type != ALE_TYPE_VLAN_ADDR)
316 continue;
317 cpsw_ale_get_addr(ale_entry, entry_addr);
318 if (memcmp(entry_addr, addr, 6) == 0)
319 return idx;
320 }
321 return -ENOENT;
322}
323
324static int cpsw_ale_match_free(struct cpsw_priv *priv)
325{
326 u32 ale_entry[ALE_ENTRY_WORDS];
327 int type, idx;
328
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530329 for (idx = 0; idx < priv->data->ale_entries; idx++) {
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000330 cpsw_ale_read(priv, idx, ale_entry);
331 type = cpsw_ale_get_entry_type(ale_entry);
332 if (type == ALE_TYPE_FREE)
333 return idx;
334 }
335 return -ENOENT;
336}
337
338static int cpsw_ale_find_ageable(struct cpsw_priv *priv)
339{
340 u32 ale_entry[ALE_ENTRY_WORDS];
341 int type, idx;
342
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530343 for (idx = 0; idx < priv->data->ale_entries; idx++) {
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000344 cpsw_ale_read(priv, idx, ale_entry);
345 type = cpsw_ale_get_entry_type(ale_entry);
346 if (type != ALE_TYPE_ADDR && type != ALE_TYPE_VLAN_ADDR)
347 continue;
348 if (cpsw_ale_get_mcast(ale_entry))
349 continue;
350 type = cpsw_ale_get_ucast_type(ale_entry);
351 if (type != ALE_UCAST_PERSISTANT &&
352 type != ALE_UCAST_OUI)
353 return idx;
354 }
355 return -ENOENT;
356}
357
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500358static int cpsw_ale_add_ucast(struct cpsw_priv *priv, const u8 *addr,
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000359 int port, int flags)
360{
361 u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
362 int idx;
363
364 cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_ADDR);
365 cpsw_ale_set_addr(ale_entry, addr);
366 cpsw_ale_set_ucast_type(ale_entry, ALE_UCAST_PERSISTANT);
367 cpsw_ale_set_secure(ale_entry, (flags & ALE_SECURE) ? 1 : 0);
368 cpsw_ale_set_blocked(ale_entry, (flags & ALE_BLOCKED) ? 1 : 0);
369 cpsw_ale_set_port_num(ale_entry, port);
370
371 idx = cpsw_ale_match_addr(priv, addr);
372 if (idx < 0)
373 idx = cpsw_ale_match_free(priv);
374 if (idx < 0)
375 idx = cpsw_ale_find_ageable(priv);
376 if (idx < 0)
377 return -ENOMEM;
378
379 cpsw_ale_write(priv, idx, ale_entry);
380 return 0;
381}
382
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500383static int cpsw_ale_add_mcast(struct cpsw_priv *priv, const u8 *addr,
384 int port_mask)
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000385{
386 u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
387 int idx, mask;
388
389 idx = cpsw_ale_match_addr(priv, addr);
390 if (idx >= 0)
391 cpsw_ale_read(priv, idx, ale_entry);
392
393 cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_ADDR);
394 cpsw_ale_set_addr(ale_entry, addr);
395 cpsw_ale_set_mcast_state(ale_entry, ALE_MCAST_FWD_2);
396
397 mask = cpsw_ale_get_port_mask(ale_entry);
398 port_mask |= mask;
399 cpsw_ale_set_port_mask(ale_entry, port_mask);
400
401 if (idx < 0)
402 idx = cpsw_ale_match_free(priv);
403 if (idx < 0)
404 idx = cpsw_ale_find_ageable(priv);
405 if (idx < 0)
406 return -ENOMEM;
407
408 cpsw_ale_write(priv, idx, ale_entry);
409 return 0;
410}
411
412static inline void cpsw_ale_control(struct cpsw_priv *priv, int bit, int val)
413{
414 u32 tmp, mask = BIT(bit);
415
416 tmp = __raw_readl(priv->ale_regs + ALE_CONTROL);
417 tmp &= ~mask;
418 tmp |= val ? mask : 0;
419 __raw_writel(tmp, priv->ale_regs + ALE_CONTROL);
420}
421
422#define cpsw_ale_enable(priv, val) cpsw_ale_control(priv, 31, val)
423#define cpsw_ale_clear(priv, val) cpsw_ale_control(priv, 30, val)
424#define cpsw_ale_vlan_aware(priv, val) cpsw_ale_control(priv, 2, val)
425
426static inline void cpsw_ale_port_state(struct cpsw_priv *priv, int port,
427 int val)
428{
429 int offset = ALE_PORTCTL + 4 * port;
430 u32 tmp, mask = 0x3;
431
432 tmp = __raw_readl(priv->ale_regs + offset);
433 tmp &= ~mask;
434 tmp |= val & mask;
435 __raw_writel(tmp, priv->ale_regs + offset);
436}
437
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000438/* Set a self-clearing bit in a register, and wait for it to clear */
439static inline void setbit_and_wait_for_clear32(void *addr)
440{
441 __raw_writel(CLEAR_BIT, addr);
442 while (__raw_readl(addr) & CLEAR_BIT)
443 ;
444}
445
446#define mac_hi(mac) (((mac)[0] << 0) | ((mac)[1] << 8) | \
447 ((mac)[2] << 16) | ((mac)[3] << 24))
448#define mac_lo(mac) (((mac)[4] << 0) | ((mac)[5] << 8))
449
450static void cpsw_set_slave_mac(struct cpsw_slave *slave,
451 struct cpsw_priv *priv)
452{
Mugunthan V N7ae228c2015-09-07 14:22:21 +0530453#ifdef CONFIG_DM_ETH
454 struct eth_pdata *pdata = dev_get_platdata(priv->dev);
455
456 writel(mac_hi(pdata->enetaddr), &slave->regs->sa_hi);
457 writel(mac_lo(pdata->enetaddr), &slave->regs->sa_lo);
458#else
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000459 __raw_writel(mac_hi(priv->dev->enetaddr), &slave->regs->sa_hi);
460 __raw_writel(mac_lo(priv->dev->enetaddr), &slave->regs->sa_lo);
Mugunthan V N7ae228c2015-09-07 14:22:21 +0530461#endif
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000462}
463
Sekhar Norib5988852017-05-08 20:49:56 +0530464static int cpsw_slave_update_link(struct cpsw_slave *slave,
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000465 struct cpsw_priv *priv, int *link)
466{
Heiko Schocheref660032013-09-05 11:50:41 +0200467 struct phy_device *phy;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000468 u32 mac_control = 0;
Sekhar Norib5988852017-05-08 20:49:56 +0530469 int ret = -ENODEV;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000470
Heiko Schocheref660032013-09-05 11:50:41 +0200471 phy = priv->phydev;
Heiko Schocheref660032013-09-05 11:50:41 +0200472 if (!phy)
Sekhar Norib5988852017-05-08 20:49:56 +0530473 goto out;
474
475 ret = phy_startup(phy);
476 if (ret)
477 goto out;
Heiko Schocheref660032013-09-05 11:50:41 +0200478
Sekhar Norib5988852017-05-08 20:49:56 +0530479 if (link)
480 *link = phy->link;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000481
Sekhar Norib5988852017-05-08 20:49:56 +0530482 if (phy->link) { /* link up */
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530483 mac_control = priv->data->mac_control;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000484 if (phy->speed == 1000)
485 mac_control |= GIGABITEN;
486 if (phy->duplex == DUPLEX_FULL)
487 mac_control |= FULLDUPLEXEN;
488 if (phy->speed == 100)
489 mac_control |= MIIEN;
Grygorii Strashko63bba7e2019-09-19 11:16:37 +0300490 if (phy->speed == 10 && phy_interface_is_rgmii(phy))
491 mac_control |= CTL_EXT_EN;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000492 }
493
494 if (mac_control == slave->mac_control)
Sekhar Norib5988852017-05-08 20:49:56 +0530495 goto out;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000496
497 if (mac_control) {
498 printf("link up on port %d, speed %d, %s duplex\n",
499 slave->slave_num, phy->speed,
500 (phy->duplex == DUPLEX_FULL) ? "full" : "half");
501 } else {
502 printf("link down on port %d\n", slave->slave_num);
503 }
504
505 __raw_writel(mac_control, &slave->sliver->mac_control);
506 slave->mac_control = mac_control;
Sekhar Norib5988852017-05-08 20:49:56 +0530507
508out:
509 return ret;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000510}
511
512static int cpsw_update_link(struct cpsw_priv *priv)
513{
Sekhar Norib5988852017-05-08 20:49:56 +0530514 int ret = -ENODEV;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000515 struct cpsw_slave *slave;
516
Mugunthan V N33e073e2014-05-22 14:37:10 +0530517 for_active_slave(slave, priv)
Sekhar Norib5988852017-05-08 20:49:56 +0530518 ret = cpsw_slave_update_link(slave, priv, NULL);
Mugunthan V Nc3fdab42013-02-19 21:34:44 +0000519
Sekhar Norib5988852017-05-08 20:49:56 +0530520 return ret;
Mugunthan V Nc3fdab42013-02-19 21:34:44 +0000521}
522
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000523static inline u32 cpsw_get_slave_port(struct cpsw_priv *priv, u32 slave_num)
524{
525 if (priv->host_port == 0)
526 return slave_num + 1;
527 else
528 return slave_num;
529}
530
531static void cpsw_slave_init(struct cpsw_slave *slave, struct cpsw_priv *priv)
532{
533 u32 slave_port;
534
535 setbit_and_wait_for_clear32(&slave->sliver->soft_reset);
536
537 /* setup priority mapping */
538 __raw_writel(0x76543210, &slave->sliver->rx_pri_map);
539 __raw_writel(0x33221100, &slave->regs->tx_pri_map);
540
541 /* setup max packet size, and mac address */
542 __raw_writel(PKT_MAX, &slave->sliver->rx_maxlen);
543 cpsw_set_slave_mac(slave, priv);
544
545 slave->mac_control = 0; /* no link yet */
546
547 /* enable forwarding */
548 slave_port = cpsw_get_slave_port(priv, slave->slave_num);
549 cpsw_ale_port_state(priv, slave_port, ALE_PORT_STATE_FORWARD);
550
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500551 cpsw_ale_add_mcast(priv, net_bcast_ethaddr, 1 << slave_port);
Mugunthan V Nc3fdab42013-02-19 21:34:44 +0000552
Mugunthan V N4944f372014-02-18 07:31:52 -0500553 priv->phy_mask |= 1 << slave->data->phy_addr;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000554}
555
556static struct cpdma_desc *cpdma_desc_alloc(struct cpsw_priv *priv)
557{
558 struct cpdma_desc *desc = priv->desc_free;
559
560 if (desc)
561 priv->desc_free = desc_read_ptr(desc, hw_next);
562 return desc;
563}
564
565static void cpdma_desc_free(struct cpsw_priv *priv, struct cpdma_desc *desc)
566{
567 if (desc) {
568 desc_write(desc, hw_next, priv->desc_free);
569 priv->desc_free = desc;
570 }
571}
572
573static int cpdma_submit(struct cpsw_priv *priv, struct cpdma_chan *chan,
574 void *buffer, int len)
575{
576 struct cpdma_desc *desc, *prev;
577 u32 mode;
578
579 desc = cpdma_desc_alloc(priv);
580 if (!desc)
581 return -ENOMEM;
582
583 if (len < PKT_MIN)
584 len = PKT_MIN;
585
586 mode = CPDMA_DESC_OWNER | CPDMA_DESC_SOP | CPDMA_DESC_EOP;
587
588 desc_write(desc, hw_next, 0);
589 desc_write(desc, hw_buffer, buffer);
590 desc_write(desc, hw_len, len);
591 desc_write(desc, hw_mode, mode | len);
592 desc_write(desc, sw_buffer, buffer);
593 desc_write(desc, sw_len, len);
594
595 if (!chan->head) {
596 /* simple case - first packet enqueued */
597 chan->head = desc;
598 chan->tail = desc;
599 chan_write(chan, hdp, desc);
600 goto done;
601 }
602
603 /* not the first packet - enqueue at the tail */
604 prev = chan->tail;
605 desc_write(prev, hw_next, desc);
606 chan->tail = desc;
607
608 /* next check if EOQ has been triggered already */
609 if (desc_read(prev, hw_mode) & CPDMA_DESC_EOQ)
610 chan_write(chan, hdp, desc);
611
612done:
613 if (chan->rxfree)
614 chan_write(chan, rxfree, 1);
615 return 0;
616}
617
618static int cpdma_process(struct cpsw_priv *priv, struct cpdma_chan *chan,
619 void **buffer, int *len)
620{
621 struct cpdma_desc *desc = chan->head;
622 u32 status;
623
624 if (!desc)
625 return -ENOENT;
626
627 status = desc_read(desc, hw_mode);
628
629 if (len)
630 *len = status & 0x7ff;
631
632 if (buffer)
633 *buffer = desc_read_ptr(desc, sw_buffer);
634
635 if (status & CPDMA_DESC_OWNER) {
636 if (chan_read(chan, hdp) == 0) {
637 if (desc_read(desc, hw_mode) & CPDMA_DESC_OWNER)
638 chan_write(chan, hdp, desc);
639 }
640
641 return -EBUSY;
642 }
643
644 chan->head = desc_read_ptr(desc, hw_next);
645 chan_write(chan, cp, desc);
646
647 cpdma_desc_free(priv, desc);
648 return 0;
649}
650
Mugunthan V N6af98c52015-09-07 14:22:20 +0530651static int _cpsw_init(struct cpsw_priv *priv, u8 *enetaddr)
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000652{
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000653 struct cpsw_slave *slave;
654 int i, ret;
655
656 /* soft reset the controller and initialize priv */
657 setbit_and_wait_for_clear32(&priv->regs->soft_reset);
658
659 /* initialize and reset the address lookup engine */
660 cpsw_ale_enable(priv, 1);
661 cpsw_ale_clear(priv, 1);
662 cpsw_ale_vlan_aware(priv, 0); /* vlan unaware mode */
663
664 /* setup host port priority mapping */
665 __raw_writel(0x76543210, &priv->host_port_regs->cpdma_tx_pri_map);
666 __raw_writel(0, &priv->host_port_regs->cpdma_rx_chan_map);
667
668 /* disable priority elevation and enable statistics on all ports */
669 __raw_writel(0, &priv->regs->ptype);
670
671 /* enable statistics collection only on the host port */
672 __raw_writel(BIT(priv->host_port), &priv->regs->stat_port_en);
Mugunthan V N2782f3e2013-07-08 16:04:38 +0530673 __raw_writel(0x7, &priv->regs->stat_port_en);
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000674
675 cpsw_ale_port_state(priv, priv->host_port, ALE_PORT_STATE_FORWARD);
676
Mugunthan V N6af98c52015-09-07 14:22:20 +0530677 cpsw_ale_add_ucast(priv, enetaddr, priv->host_port, ALE_SECURE);
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500678 cpsw_ale_add_mcast(priv, net_bcast_ethaddr, 1 << priv->host_port);
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000679
Mugunthan V N33e073e2014-05-22 14:37:10 +0530680 for_active_slave(slave, priv)
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000681 cpsw_slave_init(slave, priv);
682
Sekhar Norib5988852017-05-08 20:49:56 +0530683 ret = cpsw_update_link(priv);
684 if (ret)
685 goto out;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000686
687 /* init descriptor pool */
688 for (i = 0; i < NUM_DESCS; i++) {
689 desc_write(&priv->descs[i], hw_next,
690 (i == (NUM_DESCS - 1)) ? 0 : &priv->descs[i+1]);
691 }
692 priv->desc_free = &priv->descs[0];
693
694 /* initialize channels */
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530695 if (priv->data->version == CPSW_CTRL_VERSION_2) {
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000696 memset(&priv->rx_chan, 0, sizeof(struct cpdma_chan));
697 priv->rx_chan.hdp = priv->dma_regs + CPDMA_RXHDP_VER2;
698 priv->rx_chan.cp = priv->dma_regs + CPDMA_RXCP_VER2;
699 priv->rx_chan.rxfree = priv->dma_regs + CPDMA_RXFREE;
700
701 memset(&priv->tx_chan, 0, sizeof(struct cpdma_chan));
702 priv->tx_chan.hdp = priv->dma_regs + CPDMA_TXHDP_VER2;
703 priv->tx_chan.cp = priv->dma_regs + CPDMA_TXCP_VER2;
704 } else {
705 memset(&priv->rx_chan, 0, sizeof(struct cpdma_chan));
706 priv->rx_chan.hdp = priv->dma_regs + CPDMA_RXHDP_VER1;
707 priv->rx_chan.cp = priv->dma_regs + CPDMA_RXCP_VER1;
708 priv->rx_chan.rxfree = priv->dma_regs + CPDMA_RXFREE;
709
710 memset(&priv->tx_chan, 0, sizeof(struct cpdma_chan));
711 priv->tx_chan.hdp = priv->dma_regs + CPDMA_TXHDP_VER1;
712 priv->tx_chan.cp = priv->dma_regs + CPDMA_TXCP_VER1;
713 }
714
715 /* clear dma state */
716 setbit_and_wait_for_clear32(priv->dma_regs + CPDMA_SOFTRESET);
717
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530718 if (priv->data->version == CPSW_CTRL_VERSION_2) {
719 for (i = 0; i < priv->data->channels; i++) {
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000720 __raw_writel(0, priv->dma_regs + CPDMA_RXHDP_VER2 + 4
721 * i);
722 __raw_writel(0, priv->dma_regs + CPDMA_RXFREE + 4
723 * i);
724 __raw_writel(0, priv->dma_regs + CPDMA_RXCP_VER2 + 4
725 * i);
726 __raw_writel(0, priv->dma_regs + CPDMA_TXHDP_VER2 + 4
727 * i);
728 __raw_writel(0, priv->dma_regs + CPDMA_TXCP_VER2 + 4
729 * i);
730 }
731 } else {
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530732 for (i = 0; i < priv->data->channels; i++) {
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000733 __raw_writel(0, priv->dma_regs + CPDMA_RXHDP_VER1 + 4
734 * i);
735 __raw_writel(0, priv->dma_regs + CPDMA_RXFREE + 4
736 * i);
737 __raw_writel(0, priv->dma_regs + CPDMA_RXCP_VER1 + 4
738 * i);
739 __raw_writel(0, priv->dma_regs + CPDMA_TXHDP_VER1 + 4
740 * i);
741 __raw_writel(0, priv->dma_regs + CPDMA_TXCP_VER1 + 4
742 * i);
743
744 }
745 }
746
747 __raw_writel(1, priv->dma_regs + CPDMA_TXCONTROL);
748 __raw_writel(1, priv->dma_regs + CPDMA_RXCONTROL);
749
750 /* submit rx descs */
751 for (i = 0; i < PKTBUFSRX; i++) {
Joe Hershberger9f09a362015-04-08 01:41:06 -0500752 ret = cpdma_submit(priv, &priv->rx_chan, net_rx_packets[i],
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000753 PKTSIZE);
754 if (ret < 0) {
755 printf("error %d submitting rx desc\n", ret);
756 break;
757 }
758 }
759
Sekhar Norib5988852017-05-08 20:49:56 +0530760out:
761 return ret;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000762}
763
Alex Kiernane210e982018-05-12 07:30:02 +0000764static int cpsw_reap_completed_packets(struct cpsw_priv *priv)
765{
766 int timeout = CPDMA_TIMEOUT;
767
768 /* reap completed packets */
769 while (timeout-- &&
770 (cpdma_process(priv, &priv->tx_chan, NULL, NULL) >= 0))
771 ;
772
773 return timeout;
774}
775
Mugunthan V N6af98c52015-09-07 14:22:20 +0530776static void _cpsw_halt(struct cpsw_priv *priv)
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000777{
Alex Kiernane210e982018-05-12 07:30:02 +0000778 cpsw_reap_completed_packets(priv);
779
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000780 writel(0, priv->dma_regs + CPDMA_TXCONTROL);
781 writel(0, priv->dma_regs + CPDMA_RXCONTROL);
782
783 /* soft reset the controller and initialize priv */
784 setbit_and_wait_for_clear32(&priv->regs->soft_reset);
785
786 /* clear dma state */
787 setbit_and_wait_for_clear32(priv->dma_regs + CPDMA_SOFTRESET);
788
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000789}
790
Mugunthan V N6af98c52015-09-07 14:22:20 +0530791static int _cpsw_send(struct cpsw_priv *priv, void *packet, int length)
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000792{
Alex Kiernane210e982018-05-12 07:30:02 +0000793 int timeout;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000794
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000795 flush_dcache_range((unsigned long)packet,
Lokesh Vutlaf9f4ced2016-08-11 13:00:59 +0530796 (unsigned long)packet + ALIGN(length, PKTALIGN));
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000797
Alex Kiernane210e982018-05-12 07:30:02 +0000798 timeout = cpsw_reap_completed_packets(priv);
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000799 if (timeout == -1) {
800 printf("cpdma_process timeout\n");
801 return -ETIMEDOUT;
802 }
803
804 return cpdma_submit(priv, &priv->tx_chan, packet, length);
805}
806
Mugunthan V N6af98c52015-09-07 14:22:20 +0530807static int _cpsw_recv(struct cpsw_priv *priv, uchar **pkt)
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000808{
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000809 void *buffer;
810 int len;
Heinrich Schuchardt71bb8dd2018-03-18 11:24:38 +0100811 int ret;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000812
Mugunthan V N6af98c52015-09-07 14:22:20 +0530813 ret = cpdma_process(priv, &priv->rx_chan, &buffer, &len);
814 if (ret < 0)
815 return ret;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000816
Mugunthan V N6af98c52015-09-07 14:22:20 +0530817 invalidate_dcache_range((unsigned long)buffer,
818 (unsigned long)buffer + PKTSIZE_ALIGN);
819 *pkt = buffer;
820
821 return len;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000822}
823
824static void cpsw_slave_setup(struct cpsw_slave *slave, int slave_num,
825 struct cpsw_priv *priv)
826{
827 void *regs = priv->regs;
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530828 struct cpsw_slave_data *data = priv->data->slave_data + slave_num;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000829 slave->slave_num = slave_num;
830 slave->data = data;
831 slave->regs = regs + data->slave_reg_ofs;
832 slave->sliver = regs + data->sliver_reg_ofs;
833}
834
Mugunthan V N6af98c52015-09-07 14:22:20 +0530835static int cpsw_phy_init(struct cpsw_priv *priv, struct cpsw_slave *slave)
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000836{
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000837 struct phy_device *phydev;
Ilya Ledvicha1635f02014-03-12 11:26:30 +0200838 u32 supported = PHY_GBIT_FEATURES;
Grygorii Strashkoe32ed8c2019-09-19 11:16:39 +0300839 int ret;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000840
Yegor Yefremov062bbac2012-11-26 04:03:16 +0000841 phydev = phy_connect(priv->bus,
Mugunthan V N4944f372014-02-18 07:31:52 -0500842 slave->data->phy_addr,
Mugunthan V N6af98c52015-09-07 14:22:20 +0530843 priv->dev,
Yegor Yefremov062bbac2012-11-26 04:03:16 +0000844 slave->data->phy_if);
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000845
Heiko Schocheref660032013-09-05 11:50:41 +0200846 if (!phydev)
847 return -1;
848
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000849 phydev->supported &= supported;
Grygorii Strashkoe32ed8c2019-09-19 11:16:39 +0300850 if (slave->data->max_speed) {
851 ret = phy_set_supported(phydev, slave->data->max_speed);
852 if (ret)
853 return ret;
854 dev_dbg(priv->dev, "Port %u speed forced to %uMbit\n",
855 slave->slave_num + 1, slave->data->max_speed);
856 }
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000857 phydev->advertising = phydev->supported;
858
Dan Murphy4b7b24e2016-05-02 15:45:56 -0500859#ifdef CONFIG_DM_ETH
Grygorii Strashko133dd1c2019-09-19 11:16:42 +0300860 if (ofnode_valid(slave->data->phy_of_handle))
861 phydev->node = slave->data->phy_of_handle;
Dan Murphy4b7b24e2016-05-02 15:45:56 -0500862#endif
863
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000864 priv->phydev = phydev;
865 phy_config(phydev);
866
867 return 1;
868}
869
Sekhar Noricfc5cc82018-08-23 17:11:29 +0530870static void cpsw_phy_addr_update(struct cpsw_priv *priv)
871{
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530872 struct cpsw_platform_data *data = priv->data;
Grygorii Strashkoeae545a2018-10-31 16:21:44 -0500873 u16 alive = cpsw_mdio_get_alive(priv->bus);
Sekhar Noricfc5cc82018-08-23 17:11:29 +0530874 int active = data->active_slave;
875 int new_addr = ffs(alive) - 1;
876
877 /*
878 * If there is only one phy alive and its address does not match
879 * that of active slave, then phy address can safely be updated.
880 */
881 if (hweight16(alive) == 1 &&
882 data->slave_data[active].phy_addr != new_addr) {
883 printf("Updated phy address for CPSW#%d, old: %d, new: %d\n",
884 active, data->slave_data[active].phy_addr, new_addr);
885 data->slave_data[active].phy_addr = new_addr;
886 }
887}
888
Mugunthan V N6af98c52015-09-07 14:22:20 +0530889int _cpsw_register(struct cpsw_priv *priv)
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000890{
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000891 struct cpsw_slave *slave;
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530892 struct cpsw_platform_data *data = priv->data;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000893 void *regs = (void *)data->cpsw_base;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000894
895 priv->slaves = malloc(sizeof(struct cpsw_slave) * data->slaves);
896 if (!priv->slaves) {
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000897 return -ENOMEM;
898 }
899
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000900 priv->host_port = data->host_port_num;
901 priv->regs = regs;
902 priv->host_port_regs = regs + data->host_port_reg_ofs;
903 priv->dma_regs = regs + data->cpdma_reg_ofs;
904 priv->ale_regs = regs + data->ale_reg_ofs;
Mugunthan V Nff559872013-07-08 16:04:37 +0530905 priv->descs = (void *)regs + data->bd_ram_ofs;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000906
907 int idx = 0;
908
909 for_each_slave(slave, priv) {
910 cpsw_slave_setup(slave, idx, priv);
911 idx = idx + 1;
912 }
913
Grygorii Strashkoeae545a2018-10-31 16:21:44 -0500914 priv->bus = cpsw_mdio_init(priv->dev->name, data->mdio_base, 0, 0);
915 if (!priv->bus)
916 return -EFAULT;
Sekhar Noricfc5cc82018-08-23 17:11:29 +0530917
918 cpsw_phy_addr_update(priv);
919
Mugunthan V N6af98c52015-09-07 14:22:20 +0530920 for_active_slave(slave, priv)
921 cpsw_phy_init(priv, slave);
922
923 return 0;
924}
925
Mugunthan V N7ae228c2015-09-07 14:22:21 +0530926#ifndef CONFIG_DM_ETH
Mugunthan V N6af98c52015-09-07 14:22:20 +0530927static int cpsw_init(struct eth_device *dev, bd_t *bis)
928{
929 struct cpsw_priv *priv = dev->priv;
930
931 return _cpsw_init(priv, dev->enetaddr);
932}
933
934static void cpsw_halt(struct eth_device *dev)
935{
936 struct cpsw_priv *priv = dev->priv;
937
938 return _cpsw_halt(priv);
939}
940
941static int cpsw_send(struct eth_device *dev, void *packet, int length)
942{
943 struct cpsw_priv *priv = dev->priv;
944
945 return _cpsw_send(priv, packet, length);
946}
947
948static int cpsw_recv(struct eth_device *dev)
949{
950 struct cpsw_priv *priv = dev->priv;
951 uchar *pkt = NULL;
952 int len;
953
954 len = _cpsw_recv(priv, &pkt);
955
956 if (len > 0) {
957 net_process_received_packet(pkt, len);
958 cpdma_submit(priv, &priv->rx_chan, pkt, PKTSIZE);
959 }
960
961 return len;
962}
963
964int cpsw_register(struct cpsw_platform_data *data)
965{
966 struct cpsw_priv *priv;
967 struct eth_device *dev;
968 int ret;
969
970 dev = calloc(sizeof(*dev), 1);
971 if (!dev)
972 return -ENOMEM;
973
974 priv = calloc(sizeof(*priv), 1);
975 if (!priv) {
976 free(dev);
977 return -ENOMEM;
978 }
979
980 priv->dev = dev;
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530981 priv->data = data;
Mugunthan V N6af98c52015-09-07 14:22:20 +0530982
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000983 strcpy(dev->name, "cpsw");
984 dev->iobase = 0;
985 dev->init = cpsw_init;
986 dev->halt = cpsw_halt;
987 dev->send = cpsw_send;
988 dev->recv = cpsw_recv;
989 dev->priv = priv;
990
991 eth_register(dev);
992
Mugunthan V N6af98c52015-09-07 14:22:20 +0530993 ret = _cpsw_register(priv);
994 if (ret < 0) {
995 eth_unregister(dev);
996 free(dev);
997 free(priv);
998 return ret;
999 }
Cyril Chemparathy464adc82012-07-24 12:22:16 +00001000
1001 return 1;
1002}
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301003#else
1004static int cpsw_eth_start(struct udevice *dev)
1005{
1006 struct eth_pdata *pdata = dev_get_platdata(dev);
1007 struct cpsw_priv *priv = dev_get_priv(dev);
1008
1009 return _cpsw_init(priv, pdata->enetaddr);
1010}
1011
1012static int cpsw_eth_send(struct udevice *dev, void *packet, int length)
1013{
1014 struct cpsw_priv *priv = dev_get_priv(dev);
1015
1016 return _cpsw_send(priv, packet, length);
1017}
1018
1019static int cpsw_eth_recv(struct udevice *dev, int flags, uchar **packetp)
1020{
1021 struct cpsw_priv *priv = dev_get_priv(dev);
1022
1023 return _cpsw_recv(priv, packetp);
1024}
1025
1026static int cpsw_eth_free_pkt(struct udevice *dev, uchar *packet,
1027 int length)
1028{
1029 struct cpsw_priv *priv = dev_get_priv(dev);
1030
1031 return cpdma_submit(priv, &priv->rx_chan, packet, PKTSIZE);
1032}
1033
1034static void cpsw_eth_stop(struct udevice *dev)
1035{
1036 struct cpsw_priv *priv = dev_get_priv(dev);
1037
1038 return _cpsw_halt(priv);
1039}
1040
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301041static const struct eth_ops cpsw_eth_ops = {
1042 .start = cpsw_eth_start,
1043 .send = cpsw_eth_send,
1044 .recv = cpsw_eth_recv,
1045 .free_pkt = cpsw_eth_free_pkt,
1046 .stop = cpsw_eth_stop,
1047};
1048
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301049static void cpsw_gmii_sel_am3352(struct cpsw_priv *priv,
1050 phy_interface_t phy_mode)
1051{
1052 u32 reg;
1053 u32 mask;
1054 u32 mode = 0;
1055 bool rgmii_id = false;
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301056 int slave = priv->data->active_slave;
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301057
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301058 reg = readl(priv->data->gmii_sel);
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301059
1060 switch (phy_mode) {
1061 case PHY_INTERFACE_MODE_RMII:
1062 mode = AM33XX_GMII_SEL_MODE_RMII;
1063 break;
1064
1065 case PHY_INTERFACE_MODE_RGMII:
Grygorii Strashko40b346d2019-09-19 11:16:40 +03001066 case PHY_INTERFACE_MODE_RGMII_RXID:
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301067 mode = AM33XX_GMII_SEL_MODE_RGMII;
1068 break;
1069 case PHY_INTERFACE_MODE_RGMII_ID:
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301070 case PHY_INTERFACE_MODE_RGMII_TXID:
1071 mode = AM33XX_GMII_SEL_MODE_RGMII;
1072 rgmii_id = true;
1073 break;
1074
1075 case PHY_INTERFACE_MODE_MII:
1076 default:
1077 mode = AM33XX_GMII_SEL_MODE_MII;
1078 break;
1079 };
1080
1081 mask = GMII_SEL_MODE_MASK << (slave * 2) | BIT(slave + 6);
1082 mode <<= slave * 2;
1083
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301084 if (priv->data->rmii_clock_external) {
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301085 if (slave == 0)
1086 mode |= AM33XX_GMII_SEL_RMII1_IO_CLK_EN;
1087 else
1088 mode |= AM33XX_GMII_SEL_RMII2_IO_CLK_EN;
1089 }
1090
1091 if (rgmii_id) {
1092 if (slave == 0)
1093 mode |= AM33XX_GMII_SEL_RGMII1_IDMODE;
1094 else
1095 mode |= AM33XX_GMII_SEL_RGMII2_IDMODE;
1096 }
1097
1098 reg &= ~mask;
1099 reg |= mode;
1100
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301101 writel(reg, priv->data->gmii_sel);
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301102}
1103
1104static void cpsw_gmii_sel_dra7xx(struct cpsw_priv *priv,
1105 phy_interface_t phy_mode)
1106{
1107 u32 reg;
1108 u32 mask;
1109 u32 mode = 0;
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301110 int slave = priv->data->active_slave;
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301111
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301112 reg = readl(priv->data->gmii_sel);
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301113
1114 switch (phy_mode) {
1115 case PHY_INTERFACE_MODE_RMII:
1116 mode = AM33XX_GMII_SEL_MODE_RMII;
1117 break;
1118
1119 case PHY_INTERFACE_MODE_RGMII:
1120 case PHY_INTERFACE_MODE_RGMII_ID:
1121 case PHY_INTERFACE_MODE_RGMII_RXID:
1122 case PHY_INTERFACE_MODE_RGMII_TXID:
1123 mode = AM33XX_GMII_SEL_MODE_RGMII;
1124 break;
1125
1126 case PHY_INTERFACE_MODE_MII:
1127 default:
1128 mode = AM33XX_GMII_SEL_MODE_MII;
1129 break;
1130 };
1131
1132 switch (slave) {
1133 case 0:
1134 mask = GMII_SEL_MODE_MASK;
1135 break;
1136 case 1:
1137 mask = GMII_SEL_MODE_MASK << 4;
1138 mode <<= 4;
1139 break;
1140 default:
1141 dev_err(priv->dev, "invalid slave number...\n");
1142 return;
1143 }
1144
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301145 if (priv->data->rmii_clock_external)
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301146 dev_err(priv->dev, "RMII External clock is not supported\n");
1147
1148 reg &= ~mask;
1149 reg |= mode;
1150
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301151 writel(reg, priv->data->gmii_sel);
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301152}
1153
1154static void cpsw_phy_sel(struct cpsw_priv *priv, const char *compat,
1155 phy_interface_t phy_mode)
1156{
1157 if (!strcmp(compat, "ti,am3352-cpsw-phy-sel"))
1158 cpsw_gmii_sel_am3352(priv, phy_mode);
1159 if (!strcmp(compat, "ti,am43xx-cpsw-phy-sel"))
1160 cpsw_gmii_sel_am3352(priv, phy_mode);
1161 else if (!strcmp(compat, "ti,dra7xx-cpsw-phy-sel"))
1162 cpsw_gmii_sel_dra7xx(priv, phy_mode);
1163}
1164
Faiz Abbas5b5dc0f2019-03-18 13:54:32 +05301165static int cpsw_eth_probe(struct udevice *dev)
1166{
1167 struct cpsw_priv *priv = dev_get_priv(dev);
1168 struct eth_pdata *pdata = dev_get_platdata(dev);
1169
1170 priv->dev = dev;
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301171 priv->data = pdata->priv_pdata;
Faiz Abbas8ecdffe2019-03-18 13:54:34 +05301172 ti_cm_get_macid(dev, priv->data, pdata->enetaddr);
Faiz Abbas5b5dc0f2019-03-18 13:54:32 +05301173 /* Select phy interface in control module */
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301174 cpsw_phy_sel(priv, priv->data->phy_sel_compat,
Faiz Abbas5b5dc0f2019-03-18 13:54:32 +05301175 pdata->phy_interface);
1176
1177 return _cpsw_register(priv);
1178}
1179
Faiz Abbas687e80e2019-03-18 13:54:35 +05301180#if CONFIG_IS_ENABLED(OF_CONTROL)
Grygorii Strashko68e313e2019-09-19 11:16:38 +03001181static void cpsw_eth_of_parse_slave(struct cpsw_platform_data *data,
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001182 int slave_index, ofnode subnode)
Grygorii Strashko68e313e2019-09-19 11:16:38 +03001183{
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001184 struct ofnode_phandle_args out_args;
1185 struct cpsw_slave_data *slave_data;
Grygorii Strashko68e313e2019-09-19 11:16:38 +03001186 const char *phy_mode;
1187 u32 phy_id[2];
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001188 int ret;
Grygorii Strashko68e313e2019-09-19 11:16:38 +03001189
1190 slave_data = &data->slave_data[slave_index];
1191
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001192 phy_mode = ofnode_read_string(subnode, "phy-mode");
Grygorii Strashko68e313e2019-09-19 11:16:38 +03001193 if (phy_mode)
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001194 slave_data->phy_if = phy_get_interface_by_name(phy_mode);
Grygorii Strashko68e313e2019-09-19 11:16:38 +03001195
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001196 ret = ofnode_parse_phandle_with_args(subnode, "phy-handle",
1197 NULL, 0, 0, &out_args);
1198 if (!ret) {
1199 slave_data->phy_of_handle = out_args.node;
Grygorii Strashko68e313e2019-09-19 11:16:38 +03001200
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001201 ret = ofnode_read_s32(slave_data->phy_of_handle, "reg",
1202 &slave_data->phy_addr);
1203 if (ret)
1204 printf("error: phy addr not found in dt\n");
Grygorii Strashko68e313e2019-09-19 11:16:38 +03001205 } else {
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001206 ret = ofnode_read_u32_array(subnode, "phy_id", phy_id, 2);
1207 if (ret)
1208 printf("error: phy_id read failed\n");
Grygorii Strashko68e313e2019-09-19 11:16:38 +03001209 }
Grygorii Strashkoe32ed8c2019-09-19 11:16:39 +03001210
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001211 slave_data->max_speed = ofnode_read_s32_default(subnode,
1212 "max-speed", 0);
Grygorii Strashko68e313e2019-09-19 11:16:38 +03001213}
1214
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301215static int cpsw_eth_ofdata_to_platdata(struct udevice *dev)
1216{
1217 struct eth_pdata *pdata = dev_get_platdata(dev);
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301218 struct cpsw_platform_data *data;
Vignesh R8e9473d2016-08-02 10:14:27 +05301219 struct gpio_desc *mode_gpios;
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301220 int slave_index = 0;
Vignesh R8e9473d2016-08-02 10:14:27 +05301221 int num_mode_gpios;
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001222 ofnode subnode;
Mugunthan V N9e556352016-04-28 15:36:07 +05301223 int ret;
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301224
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301225 data = calloc(1, sizeof(struct cpsw_platform_data));
1226 pdata->priv_pdata = data;
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001227 pdata->iobase = dev_read_addr(dev);
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301228 data->version = CPSW_CTRL_VERSION_2;
1229 data->bd_ram_ofs = CPSW_BD_OFFSET;
1230 data->ale_reg_ofs = CPSW_ALE_OFFSET;
1231 data->cpdma_reg_ofs = CPSW_CPDMA_OFFSET;
1232 data->mdio_div = CPSW_MDIO_DIV;
1233 data->host_port_reg_ofs = CPSW_HOST_PORT_OFFSET,
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301234
1235 pdata->phy_interface = -1;
1236
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301237 data->cpsw_base = pdata->iobase;
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001238
1239 ret = dev_read_s32(dev, "cpdma_channels", &data->channels);
1240 if (ret) {
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301241 printf("error: cpdma_channels not found in dt\n");
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001242 return ret;
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301243 }
1244
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001245 ret = dev_read_s32(dev, "slaves", &data->slaves);
1246 if (ret) {
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301247 printf("error: slaves not found in dt\n");
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001248 return ret;
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301249 }
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301250 data->slave_data = malloc(sizeof(struct cpsw_slave_data) *
1251 data->slaves);
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301252
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001253 ret = dev_read_s32(dev, "ale_entries", &data->ale_entries);
1254 if (ret) {
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301255 printf("error: ale_entries not found in dt\n");
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001256 return ret;
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301257 }
1258
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001259 ret = dev_read_u32(dev, "bd_ram_size", &data->bd_ram_ofs);
1260 if (ret) {
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301261 printf("error: bd_ram_size not found in dt\n");
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001262 return ret;
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301263 }
1264
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001265 ret = dev_read_u32(dev, "mac_control", &data->mac_control);
1266 if (ret) {
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301267 printf("error: ale_entries not found in dt\n");
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001268 return ret;
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301269 }
1270
Vignesh R8e9473d2016-08-02 10:14:27 +05301271 num_mode_gpios = gpio_get_list_count(dev, "mode-gpios");
1272 if (num_mode_gpios > 0) {
1273 mode_gpios = malloc(sizeof(struct gpio_desc) *
1274 num_mode_gpios);
1275 gpio_request_list_by_name(dev, "mode-gpios", mode_gpios,
1276 num_mode_gpios, GPIOD_IS_OUT);
1277 free(mode_gpios);
1278 }
1279
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001280 data->active_slave = dev_read_u32_default(dev, "active_slave", 0);
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301281
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001282 ofnode_for_each_subnode(subnode, dev_ofnode(dev)) {
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301283 const char *name;
1284
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001285 name = ofnode_get_name(subnode);
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301286 if (!strncmp(name, "mdio", 4)) {
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001287 data->mdio_base = ofnode_get_addr(subnode);
1288 if (data->mdio_base == FDT_ADDR_T_NONE) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001289 pr_err("Not able to get MDIO address space\n");
Mugunthan V N77f7bc72016-04-28 15:36:06 +05301290 return -ENOENT;
1291 }
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301292 }
1293
1294 if (!strncmp(name, "slave", 5)) {
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301295 if (slave_index >= data->slaves)
Mugunthan V N13a27fd2016-04-28 15:36:04 +05301296 continue;
Dan Murphy4b7b24e2016-05-02 15:45:56 -05001297
Grygorii Strashko68e313e2019-09-19 11:16:38 +03001298 cpsw_eth_of_parse_slave(data, slave_index, subnode);
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301299 slave_index++;
1300 }
1301
1302 if (!strncmp(name, "cpsw-phy-sel", 12)) {
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001303 data->gmii_sel = ofnode_get_addr(subnode);
Mugunthan V N77f7bc72016-04-28 15:36:06 +05301304
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301305 if (data->gmii_sel == FDT_ADDR_T_NONE) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001306 pr_err("Not able to get gmii_sel reg address\n");
Mugunthan V N77f7bc72016-04-28 15:36:06 +05301307 return -ENOENT;
1308 }
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301309
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001310 if (ofnode_read_bool(subnode, "rmii-clock-ext"))
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301311 data->rmii_clock_external = true;
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301312
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001313 data->phy_sel_compat = ofnode_read_string(subnode,
1314 "compatible");
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301315 if (!data->phy_sel_compat) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001316 pr_err("Not able to get gmii_sel compatible\n");
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301317 return -ENOENT;
1318 }
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301319 }
1320 }
1321
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301322 data->slave_data[0].slave_reg_ofs = CPSW_SLAVE0_OFFSET;
1323 data->slave_data[0].sliver_reg_ofs = CPSW_SLIVER0_OFFSET;
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301324
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301325 if (data->slaves == 2) {
1326 data->slave_data[1].slave_reg_ofs = CPSW_SLAVE1_OFFSET;
1327 data->slave_data[1].sliver_reg_ofs = CPSW_SLIVER1_OFFSET;
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301328 }
1329
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001330 ret = ti_cm_get_macid_addr(dev, data->active_slave, data);
Mugunthan V N9e556352016-04-28 15:36:07 +05301331 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001332 pr_err("cpsw read efuse mac failed\n");
Mugunthan V N9e556352016-04-28 15:36:07 +05301333 return ret;
1334 }
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301335
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001336 pdata->phy_interface = data->slave_data[data->active_slave].phy_if;
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301337 if (pdata->phy_interface == -1) {
Grygorii Strashko68e313e2019-09-19 11:16:38 +03001338 debug("%s: Invalid PHY interface '%s'\n", __func__,
1339 phy_string_for_interface(pdata->phy_interface));
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301340 return -EINVAL;
1341 }
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301342
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301343 return 0;
1344}
1345
Faiz Abbas687e80e2019-03-18 13:54:35 +05301346static const struct udevice_id cpsw_eth_ids[] = {
1347 { .compatible = "ti,cpsw" },
1348 { .compatible = "ti,am335x-cpsw" },
1349 { }
1350};
1351#endif
1352
Sekhar Noricfc5cc82018-08-23 17:11:29 +05301353int cpsw_get_slave_phy_addr(struct udevice *dev, int slave)
1354{
1355 struct cpsw_priv *priv = dev_get_priv(dev);
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301356 struct cpsw_platform_data *data = priv->data;
Sekhar Noricfc5cc82018-08-23 17:11:29 +05301357
1358 return data->slave_data[slave].phy_addr;
1359}
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301360
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301361U_BOOT_DRIVER(eth_cpsw) = {
1362 .name = "eth_cpsw",
1363 .id = UCLASS_ETH,
Faiz Abbas687e80e2019-03-18 13:54:35 +05301364#if CONFIG_IS_ENABLED(OF_CONTROL)
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301365 .of_match = cpsw_eth_ids,
1366 .ofdata_to_platdata = cpsw_eth_ofdata_to_platdata,
Faiz Abbas687e80e2019-03-18 13:54:35 +05301367 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
1368#endif
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301369 .probe = cpsw_eth_probe,
1370 .ops = &cpsw_eth_ops,
1371 .priv_auto_alloc_size = sizeof(struct cpsw_priv),
Faiz Abbasd852b942019-03-18 13:54:36 +05301372 .flags = DM_FLAG_ALLOC_PRIV_DMA | DM_FLAG_PRE_RELOC,
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301373};
1374#endif /* CONFIG_DM_ETH */