blob: 24265360e643d4a428f3f7ebf9c84c33243c5266 [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>
Mugunthan V N9e556352016-04-28 15:36:07 +053022#include <fdt_support.h>
Mugunthan V N7ae228c2015-09-07 14:22:21 +053023
Grygorii Strashkoeae545a2018-10-31 16:21:44 -050024#include "cpsw_mdio.h"
25
Mugunthan V N7ae228c2015-09-07 14:22:21 +053026DECLARE_GLOBAL_DATA_PTR;
Cyril Chemparathy464adc82012-07-24 12:22:16 +000027
28#define BITMASK(bits) (BIT(bits) - 1)
Cyril Chemparathy464adc82012-07-24 12:22:16 +000029#define NUM_DESCS (PKTBUFSRX * 2)
30#define PKT_MIN 60
31#define PKT_MAX (1500 + 14 + 4 + 4)
32#define CLEAR_BIT 1
33#define GIGABITEN BIT(7)
34#define FULLDUPLEXEN BIT(0)
35#define MIIEN BIT(15)
Grygorii Strashko63bba7e2019-09-19 11:16:37 +030036#define CTL_EXT_EN BIT(18)
Cyril Chemparathy464adc82012-07-24 12:22:16 +000037/* DMA Registers */
38#define CPDMA_TXCONTROL 0x004
39#define CPDMA_RXCONTROL 0x014
40#define CPDMA_SOFTRESET 0x01c
41#define CPDMA_RXFREE 0x0e0
42#define CPDMA_TXHDP_VER1 0x100
43#define CPDMA_TXHDP_VER2 0x200
44#define CPDMA_RXHDP_VER1 0x120
45#define CPDMA_RXHDP_VER2 0x220
46#define CPDMA_TXCP_VER1 0x140
47#define CPDMA_TXCP_VER2 0x240
48#define CPDMA_RXCP_VER1 0x160
49#define CPDMA_RXCP_VER2 0x260
50
Cyril Chemparathy464adc82012-07-24 12:22:16 +000051/* Descriptor mode bits */
52#define CPDMA_DESC_SOP BIT(31)
53#define CPDMA_DESC_EOP BIT(30)
54#define CPDMA_DESC_OWNER BIT(29)
55#define CPDMA_DESC_EOQ BIT(28)
56
57/*
58 * This timeout definition is a worst-case ultra defensive measure against
59 * unexpected controller lock ups. Ideally, we should never ever hit this
60 * scenario in practice.
61 */
Cyril Chemparathy464adc82012-07-24 12:22:16 +000062#define CPDMA_TIMEOUT 100 /* msecs */
63
Cyril Chemparathy464adc82012-07-24 12:22:16 +000064struct cpsw_regs {
65 u32 id_ver;
66 u32 control;
67 u32 soft_reset;
68 u32 stat_port_en;
69 u32 ptype;
70};
71
72struct cpsw_slave_regs {
73 u32 max_blks;
74 u32 blk_cnt;
75 u32 flow_thresh;
76 u32 port_vlan;
77 u32 tx_pri_map;
Matt Porter1ef0e872013-03-20 05:38:12 +000078#ifdef CONFIG_AM33XX
Cyril Chemparathy464adc82012-07-24 12:22:16 +000079 u32 gap_thresh;
Matt Porter1ef0e872013-03-20 05:38:12 +000080#elif defined(CONFIG_TI814X)
81 u32 ts_ctl;
82 u32 ts_seq_ltype;
83 u32 ts_vlan;
84#endif
Cyril Chemparathy464adc82012-07-24 12:22:16 +000085 u32 sa_lo;
86 u32 sa_hi;
87};
88
89struct cpsw_host_regs {
90 u32 max_blks;
91 u32 blk_cnt;
92 u32 flow_thresh;
93 u32 port_vlan;
94 u32 tx_pri_map;
95 u32 cpdma_tx_pri_map;
96 u32 cpdma_rx_chan_map;
97};
98
99struct cpsw_sliver_regs {
100 u32 id_ver;
101 u32 mac_control;
102 u32 mac_status;
103 u32 soft_reset;
104 u32 rx_maxlen;
105 u32 __reserved_0;
106 u32 rx_pause;
107 u32 tx_pause;
108 u32 __reserved_1;
109 u32 rx_pri_map;
110};
111
112#define ALE_ENTRY_BITS 68
113#define ALE_ENTRY_WORDS DIV_ROUND_UP(ALE_ENTRY_BITS, 32)
114
115/* ALE Registers */
116#define ALE_CONTROL 0x08
117#define ALE_UNKNOWNVLAN 0x18
118#define ALE_TABLE_CONTROL 0x20
119#define ALE_TABLE 0x34
120#define ALE_PORTCTL 0x40
121
122#define ALE_TABLE_WRITE BIT(31)
123
124#define ALE_TYPE_FREE 0
125#define ALE_TYPE_ADDR 1
126#define ALE_TYPE_VLAN 2
127#define ALE_TYPE_VLAN_ADDR 3
128
129#define ALE_UCAST_PERSISTANT 0
130#define ALE_UCAST_UNTOUCHED 1
131#define ALE_UCAST_OUI 2
132#define ALE_UCAST_TOUCHED 3
133
134#define ALE_MCAST_FWD 0
135#define ALE_MCAST_BLOCK_LEARN_FWD 1
136#define ALE_MCAST_FWD_LEARN 2
137#define ALE_MCAST_FWD_2 3
138
139enum cpsw_ale_port_state {
140 ALE_PORT_STATE_DISABLE = 0x00,
141 ALE_PORT_STATE_BLOCK = 0x01,
142 ALE_PORT_STATE_LEARN = 0x02,
143 ALE_PORT_STATE_FORWARD = 0x03,
144};
145
146/* ALE unicast entry flags - passed into cpsw_ale_add_ucast() */
147#define ALE_SECURE 1
148#define ALE_BLOCKED 2
149
150struct cpsw_slave {
151 struct cpsw_slave_regs *regs;
152 struct cpsw_sliver_regs *sliver;
153 int slave_num;
154 u32 mac_control;
155 struct cpsw_slave_data *data;
156};
157
158struct cpdma_desc {
159 /* hardware fields */
160 u32 hw_next;
161 u32 hw_buffer;
162 u32 hw_len;
163 u32 hw_mode;
164 /* software fields */
165 u32 sw_buffer;
166 u32 sw_len;
167};
168
169struct cpdma_chan {
170 struct cpdma_desc *head, *tail;
171 void *hdp, *cp, *rxfree;
172};
173
Mugunthan V N4d5fdb62016-10-13 19:33:38 +0530174/* AM33xx SoC specific definitions for the CONTROL port */
175#define AM33XX_GMII_SEL_MODE_MII 0
176#define AM33XX_GMII_SEL_MODE_RMII 1
177#define AM33XX_GMII_SEL_MODE_RGMII 2
178
179#define AM33XX_GMII_SEL_RGMII1_IDMODE BIT(4)
180#define AM33XX_GMII_SEL_RGMII2_IDMODE BIT(5)
181#define AM33XX_GMII_SEL_RMII1_IO_CLK_EN BIT(6)
182#define AM33XX_GMII_SEL_RMII2_IO_CLK_EN BIT(7)
183
184#define GMII_SEL_MODE_MASK 0x3
185
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000186#define desc_write(desc, fld, val) __raw_writel((u32)(val), &(desc)->fld)
187#define desc_read(desc, fld) __raw_readl(&(desc)->fld)
188#define desc_read_ptr(desc, fld) ((void *)__raw_readl(&(desc)->fld))
189
190#define chan_write(chan, fld, val) __raw_writel((u32)(val), (chan)->fld)
191#define chan_read(chan, fld) __raw_readl((chan)->fld)
192#define chan_read_ptr(chan, fld) ((void *)__raw_readl((chan)->fld))
193
Mugunthan V N33e073e2014-05-22 14:37:10 +0530194#define for_active_slave(slave, priv) \
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530195 slave = (priv)->slaves + ((priv)->data)->active_slave; if (slave)
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000196#define for_each_slave(slave, priv) \
197 for (slave = (priv)->slaves; slave != (priv)->slaves + \
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530198 ((priv)->data)->slaves; slave++)
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000199
200struct cpsw_priv {
Mugunthan V N7ae228c2015-09-07 14:22:21 +0530201#ifdef CONFIG_DM_ETH
202 struct udevice *dev;
203#else
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000204 struct eth_device *dev;
Mugunthan V N7ae228c2015-09-07 14:22:21 +0530205#endif
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530206 struct cpsw_platform_data *data;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000207 int host_port;
208
209 struct cpsw_regs *regs;
210 void *dma_regs;
211 struct cpsw_host_regs *host_port_regs;
212 void *ale_regs;
213
214 struct cpdma_desc *descs;
215 struct cpdma_desc *desc_free;
216 struct cpdma_chan rx_chan, tx_chan;
217
218 struct cpsw_slave *slaves;
219 struct phy_device *phydev;
220 struct mii_dev *bus;
Mugunthan V Nc3fdab42013-02-19 21:34:44 +0000221
Mugunthan V Nc3fdab42013-02-19 21:34:44 +0000222 u32 phy_mask;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000223};
224
225static inline int cpsw_ale_get_field(u32 *ale_entry, u32 start, u32 bits)
226{
227 int idx;
228
229 idx = start / 32;
230 start -= idx * 32;
231 idx = 2 - idx; /* flip */
232 return (ale_entry[idx] >> start) & BITMASK(bits);
233}
234
235static inline void cpsw_ale_set_field(u32 *ale_entry, u32 start, u32 bits,
236 u32 value)
237{
238 int idx;
239
240 value &= BITMASK(bits);
241 idx = start / 32;
242 start -= idx * 32;
243 idx = 2 - idx; /* flip */
244 ale_entry[idx] &= ~(BITMASK(bits) << start);
245 ale_entry[idx] |= (value << start);
246}
247
248#define DEFINE_ALE_FIELD(name, start, bits) \
249static inline int cpsw_ale_get_##name(u32 *ale_entry) \
250{ \
251 return cpsw_ale_get_field(ale_entry, start, bits); \
252} \
253static inline void cpsw_ale_set_##name(u32 *ale_entry, u32 value) \
254{ \
255 cpsw_ale_set_field(ale_entry, start, bits, value); \
256}
257
258DEFINE_ALE_FIELD(entry_type, 60, 2)
259DEFINE_ALE_FIELD(mcast_state, 62, 2)
260DEFINE_ALE_FIELD(port_mask, 66, 3)
261DEFINE_ALE_FIELD(ucast_type, 62, 2)
262DEFINE_ALE_FIELD(port_num, 66, 2)
263DEFINE_ALE_FIELD(blocked, 65, 1)
264DEFINE_ALE_FIELD(secure, 64, 1)
265DEFINE_ALE_FIELD(mcast, 40, 1)
266
267/* The MAC address field in the ALE entry cannot be macroized as above */
268static inline void cpsw_ale_get_addr(u32 *ale_entry, u8 *addr)
269{
270 int i;
271
272 for (i = 0; i < 6; i++)
273 addr[i] = cpsw_ale_get_field(ale_entry, 40 - 8*i, 8);
274}
275
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500276static inline void cpsw_ale_set_addr(u32 *ale_entry, const u8 *addr)
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000277{
278 int i;
279
280 for (i = 0; i < 6; i++)
281 cpsw_ale_set_field(ale_entry, 40 - 8*i, 8, addr[i]);
282}
283
284static int cpsw_ale_read(struct cpsw_priv *priv, int idx, u32 *ale_entry)
285{
286 int i;
287
288 __raw_writel(idx, priv->ale_regs + ALE_TABLE_CONTROL);
289
290 for (i = 0; i < ALE_ENTRY_WORDS; i++)
291 ale_entry[i] = __raw_readl(priv->ale_regs + ALE_TABLE + 4 * i);
292
293 return idx;
294}
295
296static int cpsw_ale_write(struct cpsw_priv *priv, int idx, u32 *ale_entry)
297{
298 int i;
299
300 for (i = 0; i < ALE_ENTRY_WORDS; i++)
301 __raw_writel(ale_entry[i], priv->ale_regs + ALE_TABLE + 4 * i);
302
303 __raw_writel(idx | ALE_TABLE_WRITE, priv->ale_regs + ALE_TABLE_CONTROL);
304
305 return idx;
306}
307
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500308static int cpsw_ale_match_addr(struct cpsw_priv *priv, const u8 *addr)
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000309{
310 u32 ale_entry[ALE_ENTRY_WORDS];
311 int type, idx;
312
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530313 for (idx = 0; idx < priv->data->ale_entries; idx++) {
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000314 u8 entry_addr[6];
315
316 cpsw_ale_read(priv, idx, ale_entry);
317 type = cpsw_ale_get_entry_type(ale_entry);
318 if (type != ALE_TYPE_ADDR && type != ALE_TYPE_VLAN_ADDR)
319 continue;
320 cpsw_ale_get_addr(ale_entry, entry_addr);
321 if (memcmp(entry_addr, addr, 6) == 0)
322 return idx;
323 }
324 return -ENOENT;
325}
326
327static int cpsw_ale_match_free(struct cpsw_priv *priv)
328{
329 u32 ale_entry[ALE_ENTRY_WORDS];
330 int type, idx;
331
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530332 for (idx = 0; idx < priv->data->ale_entries; idx++) {
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000333 cpsw_ale_read(priv, idx, ale_entry);
334 type = cpsw_ale_get_entry_type(ale_entry);
335 if (type == ALE_TYPE_FREE)
336 return idx;
337 }
338 return -ENOENT;
339}
340
341static int cpsw_ale_find_ageable(struct cpsw_priv *priv)
342{
343 u32 ale_entry[ALE_ENTRY_WORDS];
344 int type, idx;
345
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530346 for (idx = 0; idx < priv->data->ale_entries; idx++) {
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000347 cpsw_ale_read(priv, idx, ale_entry);
348 type = cpsw_ale_get_entry_type(ale_entry);
349 if (type != ALE_TYPE_ADDR && type != ALE_TYPE_VLAN_ADDR)
350 continue;
351 if (cpsw_ale_get_mcast(ale_entry))
352 continue;
353 type = cpsw_ale_get_ucast_type(ale_entry);
354 if (type != ALE_UCAST_PERSISTANT &&
355 type != ALE_UCAST_OUI)
356 return idx;
357 }
358 return -ENOENT;
359}
360
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500361static int cpsw_ale_add_ucast(struct cpsw_priv *priv, const u8 *addr,
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000362 int port, int flags)
363{
364 u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
365 int idx;
366
367 cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_ADDR);
368 cpsw_ale_set_addr(ale_entry, addr);
369 cpsw_ale_set_ucast_type(ale_entry, ALE_UCAST_PERSISTANT);
370 cpsw_ale_set_secure(ale_entry, (flags & ALE_SECURE) ? 1 : 0);
371 cpsw_ale_set_blocked(ale_entry, (flags & ALE_BLOCKED) ? 1 : 0);
372 cpsw_ale_set_port_num(ale_entry, port);
373
374 idx = cpsw_ale_match_addr(priv, addr);
375 if (idx < 0)
376 idx = cpsw_ale_match_free(priv);
377 if (idx < 0)
378 idx = cpsw_ale_find_ageable(priv);
379 if (idx < 0)
380 return -ENOMEM;
381
382 cpsw_ale_write(priv, idx, ale_entry);
383 return 0;
384}
385
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500386static int cpsw_ale_add_mcast(struct cpsw_priv *priv, const u8 *addr,
387 int port_mask)
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000388{
389 u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
390 int idx, mask;
391
392 idx = cpsw_ale_match_addr(priv, addr);
393 if (idx >= 0)
394 cpsw_ale_read(priv, idx, ale_entry);
395
396 cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_ADDR);
397 cpsw_ale_set_addr(ale_entry, addr);
398 cpsw_ale_set_mcast_state(ale_entry, ALE_MCAST_FWD_2);
399
400 mask = cpsw_ale_get_port_mask(ale_entry);
401 port_mask |= mask;
402 cpsw_ale_set_port_mask(ale_entry, port_mask);
403
404 if (idx < 0)
405 idx = cpsw_ale_match_free(priv);
406 if (idx < 0)
407 idx = cpsw_ale_find_ageable(priv);
408 if (idx < 0)
409 return -ENOMEM;
410
411 cpsw_ale_write(priv, idx, ale_entry);
412 return 0;
413}
414
415static inline void cpsw_ale_control(struct cpsw_priv *priv, int bit, int val)
416{
417 u32 tmp, mask = BIT(bit);
418
419 tmp = __raw_readl(priv->ale_regs + ALE_CONTROL);
420 tmp &= ~mask;
421 tmp |= val ? mask : 0;
422 __raw_writel(tmp, priv->ale_regs + ALE_CONTROL);
423}
424
425#define cpsw_ale_enable(priv, val) cpsw_ale_control(priv, 31, val)
426#define cpsw_ale_clear(priv, val) cpsw_ale_control(priv, 30, val)
427#define cpsw_ale_vlan_aware(priv, val) cpsw_ale_control(priv, 2, val)
428
429static inline void cpsw_ale_port_state(struct cpsw_priv *priv, int port,
430 int val)
431{
432 int offset = ALE_PORTCTL + 4 * port;
433 u32 tmp, mask = 0x3;
434
435 tmp = __raw_readl(priv->ale_regs + offset);
436 tmp &= ~mask;
437 tmp |= val & mask;
438 __raw_writel(tmp, priv->ale_regs + offset);
439}
440
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000441/* Set a self-clearing bit in a register, and wait for it to clear */
442static inline void setbit_and_wait_for_clear32(void *addr)
443{
444 __raw_writel(CLEAR_BIT, addr);
445 while (__raw_readl(addr) & CLEAR_BIT)
446 ;
447}
448
449#define mac_hi(mac) (((mac)[0] << 0) | ((mac)[1] << 8) | \
450 ((mac)[2] << 16) | ((mac)[3] << 24))
451#define mac_lo(mac) (((mac)[4] << 0) | ((mac)[5] << 8))
452
453static void cpsw_set_slave_mac(struct cpsw_slave *slave,
454 struct cpsw_priv *priv)
455{
Mugunthan V N7ae228c2015-09-07 14:22:21 +0530456#ifdef CONFIG_DM_ETH
457 struct eth_pdata *pdata = dev_get_platdata(priv->dev);
458
459 writel(mac_hi(pdata->enetaddr), &slave->regs->sa_hi);
460 writel(mac_lo(pdata->enetaddr), &slave->regs->sa_lo);
461#else
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000462 __raw_writel(mac_hi(priv->dev->enetaddr), &slave->regs->sa_hi);
463 __raw_writel(mac_lo(priv->dev->enetaddr), &slave->regs->sa_lo);
Mugunthan V N7ae228c2015-09-07 14:22:21 +0530464#endif
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000465}
466
Sekhar Norib5988852017-05-08 20:49:56 +0530467static int cpsw_slave_update_link(struct cpsw_slave *slave,
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000468 struct cpsw_priv *priv, int *link)
469{
Heiko Schocheref660032013-09-05 11:50:41 +0200470 struct phy_device *phy;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000471 u32 mac_control = 0;
Sekhar Norib5988852017-05-08 20:49:56 +0530472 int ret = -ENODEV;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000473
Heiko Schocheref660032013-09-05 11:50:41 +0200474 phy = priv->phydev;
Heiko Schocheref660032013-09-05 11:50:41 +0200475 if (!phy)
Sekhar Norib5988852017-05-08 20:49:56 +0530476 goto out;
477
478 ret = phy_startup(phy);
479 if (ret)
480 goto out;
Heiko Schocheref660032013-09-05 11:50:41 +0200481
Sekhar Norib5988852017-05-08 20:49:56 +0530482 if (link)
483 *link = phy->link;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000484
Sekhar Norib5988852017-05-08 20:49:56 +0530485 if (phy->link) { /* link up */
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530486 mac_control = priv->data->mac_control;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000487 if (phy->speed == 1000)
488 mac_control |= GIGABITEN;
489 if (phy->duplex == DUPLEX_FULL)
490 mac_control |= FULLDUPLEXEN;
491 if (phy->speed == 100)
492 mac_control |= MIIEN;
Grygorii Strashko63bba7e2019-09-19 11:16:37 +0300493 if (phy->speed == 10 && phy_interface_is_rgmii(phy))
494 mac_control |= CTL_EXT_EN;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000495 }
496
497 if (mac_control == slave->mac_control)
Sekhar Norib5988852017-05-08 20:49:56 +0530498 goto out;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000499
500 if (mac_control) {
501 printf("link up on port %d, speed %d, %s duplex\n",
502 slave->slave_num, phy->speed,
503 (phy->duplex == DUPLEX_FULL) ? "full" : "half");
504 } else {
505 printf("link down on port %d\n", slave->slave_num);
506 }
507
508 __raw_writel(mac_control, &slave->sliver->mac_control);
509 slave->mac_control = mac_control;
Sekhar Norib5988852017-05-08 20:49:56 +0530510
511out:
512 return ret;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000513}
514
515static int cpsw_update_link(struct cpsw_priv *priv)
516{
Sekhar Norib5988852017-05-08 20:49:56 +0530517 int ret = -ENODEV;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000518 struct cpsw_slave *slave;
519
Mugunthan V N33e073e2014-05-22 14:37:10 +0530520 for_active_slave(slave, priv)
Sekhar Norib5988852017-05-08 20:49:56 +0530521 ret = cpsw_slave_update_link(slave, priv, NULL);
Mugunthan V Nc3fdab42013-02-19 21:34:44 +0000522
Sekhar Norib5988852017-05-08 20:49:56 +0530523 return ret;
Mugunthan V Nc3fdab42013-02-19 21:34:44 +0000524}
525
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000526static inline u32 cpsw_get_slave_port(struct cpsw_priv *priv, u32 slave_num)
527{
528 if (priv->host_port == 0)
529 return slave_num + 1;
530 else
531 return slave_num;
532}
533
534static void cpsw_slave_init(struct cpsw_slave *slave, struct cpsw_priv *priv)
535{
536 u32 slave_port;
537
538 setbit_and_wait_for_clear32(&slave->sliver->soft_reset);
539
540 /* setup priority mapping */
541 __raw_writel(0x76543210, &slave->sliver->rx_pri_map);
542 __raw_writel(0x33221100, &slave->regs->tx_pri_map);
543
544 /* setup max packet size, and mac address */
545 __raw_writel(PKT_MAX, &slave->sliver->rx_maxlen);
546 cpsw_set_slave_mac(slave, priv);
547
548 slave->mac_control = 0; /* no link yet */
549
550 /* enable forwarding */
551 slave_port = cpsw_get_slave_port(priv, slave->slave_num);
552 cpsw_ale_port_state(priv, slave_port, ALE_PORT_STATE_FORWARD);
553
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500554 cpsw_ale_add_mcast(priv, net_bcast_ethaddr, 1 << slave_port);
Mugunthan V Nc3fdab42013-02-19 21:34:44 +0000555
Mugunthan V N4944f372014-02-18 07:31:52 -0500556 priv->phy_mask |= 1 << slave->data->phy_addr;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000557}
558
559static struct cpdma_desc *cpdma_desc_alloc(struct cpsw_priv *priv)
560{
561 struct cpdma_desc *desc = priv->desc_free;
562
563 if (desc)
564 priv->desc_free = desc_read_ptr(desc, hw_next);
565 return desc;
566}
567
568static void cpdma_desc_free(struct cpsw_priv *priv, struct cpdma_desc *desc)
569{
570 if (desc) {
571 desc_write(desc, hw_next, priv->desc_free);
572 priv->desc_free = desc;
573 }
574}
575
576static int cpdma_submit(struct cpsw_priv *priv, struct cpdma_chan *chan,
577 void *buffer, int len)
578{
579 struct cpdma_desc *desc, *prev;
580 u32 mode;
581
582 desc = cpdma_desc_alloc(priv);
583 if (!desc)
584 return -ENOMEM;
585
586 if (len < PKT_MIN)
587 len = PKT_MIN;
588
589 mode = CPDMA_DESC_OWNER | CPDMA_DESC_SOP | CPDMA_DESC_EOP;
590
591 desc_write(desc, hw_next, 0);
592 desc_write(desc, hw_buffer, buffer);
593 desc_write(desc, hw_len, len);
594 desc_write(desc, hw_mode, mode | len);
595 desc_write(desc, sw_buffer, buffer);
596 desc_write(desc, sw_len, len);
597
598 if (!chan->head) {
599 /* simple case - first packet enqueued */
600 chan->head = desc;
601 chan->tail = desc;
602 chan_write(chan, hdp, desc);
603 goto done;
604 }
605
606 /* not the first packet - enqueue at the tail */
607 prev = chan->tail;
608 desc_write(prev, hw_next, desc);
609 chan->tail = desc;
610
611 /* next check if EOQ has been triggered already */
612 if (desc_read(prev, hw_mode) & CPDMA_DESC_EOQ)
613 chan_write(chan, hdp, desc);
614
615done:
616 if (chan->rxfree)
617 chan_write(chan, rxfree, 1);
618 return 0;
619}
620
621static int cpdma_process(struct cpsw_priv *priv, struct cpdma_chan *chan,
622 void **buffer, int *len)
623{
624 struct cpdma_desc *desc = chan->head;
625 u32 status;
626
627 if (!desc)
628 return -ENOENT;
629
630 status = desc_read(desc, hw_mode);
631
632 if (len)
633 *len = status & 0x7ff;
634
635 if (buffer)
636 *buffer = desc_read_ptr(desc, sw_buffer);
637
638 if (status & CPDMA_DESC_OWNER) {
639 if (chan_read(chan, hdp) == 0) {
640 if (desc_read(desc, hw_mode) & CPDMA_DESC_OWNER)
641 chan_write(chan, hdp, desc);
642 }
643
644 return -EBUSY;
645 }
646
647 chan->head = desc_read_ptr(desc, hw_next);
648 chan_write(chan, cp, desc);
649
650 cpdma_desc_free(priv, desc);
651 return 0;
652}
653
Mugunthan V N6af98c52015-09-07 14:22:20 +0530654static int _cpsw_init(struct cpsw_priv *priv, u8 *enetaddr)
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000655{
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000656 struct cpsw_slave *slave;
657 int i, ret;
658
659 /* soft reset the controller and initialize priv */
660 setbit_and_wait_for_clear32(&priv->regs->soft_reset);
661
662 /* initialize and reset the address lookup engine */
663 cpsw_ale_enable(priv, 1);
664 cpsw_ale_clear(priv, 1);
665 cpsw_ale_vlan_aware(priv, 0); /* vlan unaware mode */
666
667 /* setup host port priority mapping */
668 __raw_writel(0x76543210, &priv->host_port_regs->cpdma_tx_pri_map);
669 __raw_writel(0, &priv->host_port_regs->cpdma_rx_chan_map);
670
671 /* disable priority elevation and enable statistics on all ports */
672 __raw_writel(0, &priv->regs->ptype);
673
674 /* enable statistics collection only on the host port */
675 __raw_writel(BIT(priv->host_port), &priv->regs->stat_port_en);
Mugunthan V N2782f3e2013-07-08 16:04:38 +0530676 __raw_writel(0x7, &priv->regs->stat_port_en);
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000677
678 cpsw_ale_port_state(priv, priv->host_port, ALE_PORT_STATE_FORWARD);
679
Mugunthan V N6af98c52015-09-07 14:22:20 +0530680 cpsw_ale_add_ucast(priv, enetaddr, priv->host_port, ALE_SECURE);
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500681 cpsw_ale_add_mcast(priv, net_bcast_ethaddr, 1 << priv->host_port);
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000682
Mugunthan V N33e073e2014-05-22 14:37:10 +0530683 for_active_slave(slave, priv)
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000684 cpsw_slave_init(slave, priv);
685
Sekhar Norib5988852017-05-08 20:49:56 +0530686 ret = cpsw_update_link(priv);
687 if (ret)
688 goto out;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000689
690 /* init descriptor pool */
691 for (i = 0; i < NUM_DESCS; i++) {
692 desc_write(&priv->descs[i], hw_next,
693 (i == (NUM_DESCS - 1)) ? 0 : &priv->descs[i+1]);
694 }
695 priv->desc_free = &priv->descs[0];
696
697 /* initialize channels */
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530698 if (priv->data->version == CPSW_CTRL_VERSION_2) {
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000699 memset(&priv->rx_chan, 0, sizeof(struct cpdma_chan));
700 priv->rx_chan.hdp = priv->dma_regs + CPDMA_RXHDP_VER2;
701 priv->rx_chan.cp = priv->dma_regs + CPDMA_RXCP_VER2;
702 priv->rx_chan.rxfree = priv->dma_regs + CPDMA_RXFREE;
703
704 memset(&priv->tx_chan, 0, sizeof(struct cpdma_chan));
705 priv->tx_chan.hdp = priv->dma_regs + CPDMA_TXHDP_VER2;
706 priv->tx_chan.cp = priv->dma_regs + CPDMA_TXCP_VER2;
707 } else {
708 memset(&priv->rx_chan, 0, sizeof(struct cpdma_chan));
709 priv->rx_chan.hdp = priv->dma_regs + CPDMA_RXHDP_VER1;
710 priv->rx_chan.cp = priv->dma_regs + CPDMA_RXCP_VER1;
711 priv->rx_chan.rxfree = priv->dma_regs + CPDMA_RXFREE;
712
713 memset(&priv->tx_chan, 0, sizeof(struct cpdma_chan));
714 priv->tx_chan.hdp = priv->dma_regs + CPDMA_TXHDP_VER1;
715 priv->tx_chan.cp = priv->dma_regs + CPDMA_TXCP_VER1;
716 }
717
718 /* clear dma state */
719 setbit_and_wait_for_clear32(priv->dma_regs + CPDMA_SOFTRESET);
720
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530721 if (priv->data->version == CPSW_CTRL_VERSION_2) {
722 for (i = 0; i < priv->data->channels; i++) {
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000723 __raw_writel(0, priv->dma_regs + CPDMA_RXHDP_VER2 + 4
724 * i);
725 __raw_writel(0, priv->dma_regs + CPDMA_RXFREE + 4
726 * i);
727 __raw_writel(0, priv->dma_regs + CPDMA_RXCP_VER2 + 4
728 * i);
729 __raw_writel(0, priv->dma_regs + CPDMA_TXHDP_VER2 + 4
730 * i);
731 __raw_writel(0, priv->dma_regs + CPDMA_TXCP_VER2 + 4
732 * i);
733 }
734 } else {
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530735 for (i = 0; i < priv->data->channels; i++) {
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000736 __raw_writel(0, priv->dma_regs + CPDMA_RXHDP_VER1 + 4
737 * i);
738 __raw_writel(0, priv->dma_regs + CPDMA_RXFREE + 4
739 * i);
740 __raw_writel(0, priv->dma_regs + CPDMA_RXCP_VER1 + 4
741 * i);
742 __raw_writel(0, priv->dma_regs + CPDMA_TXHDP_VER1 + 4
743 * i);
744 __raw_writel(0, priv->dma_regs + CPDMA_TXCP_VER1 + 4
745 * i);
746
747 }
748 }
749
750 __raw_writel(1, priv->dma_regs + CPDMA_TXCONTROL);
751 __raw_writel(1, priv->dma_regs + CPDMA_RXCONTROL);
752
753 /* submit rx descs */
754 for (i = 0; i < PKTBUFSRX; i++) {
Joe Hershberger9f09a362015-04-08 01:41:06 -0500755 ret = cpdma_submit(priv, &priv->rx_chan, net_rx_packets[i],
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000756 PKTSIZE);
757 if (ret < 0) {
758 printf("error %d submitting rx desc\n", ret);
759 break;
760 }
761 }
762
Sekhar Norib5988852017-05-08 20:49:56 +0530763out:
764 return ret;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000765}
766
Alex Kiernane210e982018-05-12 07:30:02 +0000767static int cpsw_reap_completed_packets(struct cpsw_priv *priv)
768{
769 int timeout = CPDMA_TIMEOUT;
770
771 /* reap completed packets */
772 while (timeout-- &&
773 (cpdma_process(priv, &priv->tx_chan, NULL, NULL) >= 0))
774 ;
775
776 return timeout;
777}
778
Mugunthan V N6af98c52015-09-07 14:22:20 +0530779static void _cpsw_halt(struct cpsw_priv *priv)
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000780{
Alex Kiernane210e982018-05-12 07:30:02 +0000781 cpsw_reap_completed_packets(priv);
782
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000783 writel(0, priv->dma_regs + CPDMA_TXCONTROL);
784 writel(0, priv->dma_regs + CPDMA_RXCONTROL);
785
786 /* soft reset the controller and initialize priv */
787 setbit_and_wait_for_clear32(&priv->regs->soft_reset);
788
789 /* clear dma state */
790 setbit_and_wait_for_clear32(priv->dma_regs + CPDMA_SOFTRESET);
791
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000792}
793
Mugunthan V N6af98c52015-09-07 14:22:20 +0530794static int _cpsw_send(struct cpsw_priv *priv, void *packet, int length)
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000795{
Alex Kiernane210e982018-05-12 07:30:02 +0000796 int timeout;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000797
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000798 flush_dcache_range((unsigned long)packet,
Lokesh Vutlaf9f4ced2016-08-11 13:00:59 +0530799 (unsigned long)packet + ALIGN(length, PKTALIGN));
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000800
Alex Kiernane210e982018-05-12 07:30:02 +0000801 timeout = cpsw_reap_completed_packets(priv);
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000802 if (timeout == -1) {
803 printf("cpdma_process timeout\n");
804 return -ETIMEDOUT;
805 }
806
807 return cpdma_submit(priv, &priv->tx_chan, packet, length);
808}
809
Mugunthan V N6af98c52015-09-07 14:22:20 +0530810static int _cpsw_recv(struct cpsw_priv *priv, uchar **pkt)
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000811{
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000812 void *buffer;
813 int len;
Heinrich Schuchardt71bb8dd2018-03-18 11:24:38 +0100814 int ret;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000815
Mugunthan V N6af98c52015-09-07 14:22:20 +0530816 ret = cpdma_process(priv, &priv->rx_chan, &buffer, &len);
817 if (ret < 0)
818 return ret;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000819
Mugunthan V N6af98c52015-09-07 14:22:20 +0530820 invalidate_dcache_range((unsigned long)buffer,
821 (unsigned long)buffer + PKTSIZE_ALIGN);
822 *pkt = buffer;
823
824 return len;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000825}
826
827static void cpsw_slave_setup(struct cpsw_slave *slave, int slave_num,
828 struct cpsw_priv *priv)
829{
830 void *regs = priv->regs;
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530831 struct cpsw_slave_data *data = priv->data->slave_data + slave_num;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000832 slave->slave_num = slave_num;
833 slave->data = data;
834 slave->regs = regs + data->slave_reg_ofs;
835 slave->sliver = regs + data->sliver_reg_ofs;
836}
837
Mugunthan V N6af98c52015-09-07 14:22:20 +0530838static int cpsw_phy_init(struct cpsw_priv *priv, struct cpsw_slave *slave)
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000839{
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000840 struct phy_device *phydev;
Ilya Ledvicha1635f02014-03-12 11:26:30 +0200841 u32 supported = PHY_GBIT_FEATURES;
Grygorii Strashkoe32ed8c2019-09-19 11:16:39 +0300842 int ret;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000843
Yegor Yefremov062bbac2012-11-26 04:03:16 +0000844 phydev = phy_connect(priv->bus,
Mugunthan V N4944f372014-02-18 07:31:52 -0500845 slave->data->phy_addr,
Mugunthan V N6af98c52015-09-07 14:22:20 +0530846 priv->dev,
Yegor Yefremov062bbac2012-11-26 04:03:16 +0000847 slave->data->phy_if);
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000848
Heiko Schocheref660032013-09-05 11:50:41 +0200849 if (!phydev)
850 return -1;
851
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000852 phydev->supported &= supported;
Grygorii Strashkoe32ed8c2019-09-19 11:16:39 +0300853 if (slave->data->max_speed) {
854 ret = phy_set_supported(phydev, slave->data->max_speed);
855 if (ret)
856 return ret;
857 dev_dbg(priv->dev, "Port %u speed forced to %uMbit\n",
858 slave->slave_num + 1, slave->data->max_speed);
859 }
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000860 phydev->advertising = phydev->supported;
861
Dan Murphy4b7b24e2016-05-02 15:45:56 -0500862#ifdef CONFIG_DM_ETH
863 if (slave->data->phy_of_handle)
Grygorii Strashkoba1ef862018-07-05 12:02:51 -0500864 phydev->node = offset_to_ofnode(slave->data->phy_of_handle);
Dan Murphy4b7b24e2016-05-02 15:45:56 -0500865#endif
866
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000867 priv->phydev = phydev;
868 phy_config(phydev);
869
870 return 1;
871}
872
Sekhar Noricfc5cc82018-08-23 17:11:29 +0530873static void cpsw_phy_addr_update(struct cpsw_priv *priv)
874{
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530875 struct cpsw_platform_data *data = priv->data;
Grygorii Strashkoeae545a2018-10-31 16:21:44 -0500876 u16 alive = cpsw_mdio_get_alive(priv->bus);
Sekhar Noricfc5cc82018-08-23 17:11:29 +0530877 int active = data->active_slave;
878 int new_addr = ffs(alive) - 1;
879
880 /*
881 * If there is only one phy alive and its address does not match
882 * that of active slave, then phy address can safely be updated.
883 */
884 if (hweight16(alive) == 1 &&
885 data->slave_data[active].phy_addr != new_addr) {
886 printf("Updated phy address for CPSW#%d, old: %d, new: %d\n",
887 active, data->slave_data[active].phy_addr, new_addr);
888 data->slave_data[active].phy_addr = new_addr;
889 }
890}
891
Mugunthan V N6af98c52015-09-07 14:22:20 +0530892int _cpsw_register(struct cpsw_priv *priv)
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000893{
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000894 struct cpsw_slave *slave;
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530895 struct cpsw_platform_data *data = priv->data;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000896 void *regs = (void *)data->cpsw_base;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000897
898 priv->slaves = malloc(sizeof(struct cpsw_slave) * data->slaves);
899 if (!priv->slaves) {
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000900 return -ENOMEM;
901 }
902
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000903 priv->host_port = data->host_port_num;
904 priv->regs = regs;
905 priv->host_port_regs = regs + data->host_port_reg_ofs;
906 priv->dma_regs = regs + data->cpdma_reg_ofs;
907 priv->ale_regs = regs + data->ale_reg_ofs;
Mugunthan V Nff559872013-07-08 16:04:37 +0530908 priv->descs = (void *)regs + data->bd_ram_ofs;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000909
910 int idx = 0;
911
912 for_each_slave(slave, priv) {
913 cpsw_slave_setup(slave, idx, priv);
914 idx = idx + 1;
915 }
916
Grygorii Strashkoeae545a2018-10-31 16:21:44 -0500917 priv->bus = cpsw_mdio_init(priv->dev->name, data->mdio_base, 0, 0);
918 if (!priv->bus)
919 return -EFAULT;
Sekhar Noricfc5cc82018-08-23 17:11:29 +0530920
921 cpsw_phy_addr_update(priv);
922
Mugunthan V N6af98c52015-09-07 14:22:20 +0530923 for_active_slave(slave, priv)
924 cpsw_phy_init(priv, slave);
925
926 return 0;
927}
928
Mugunthan V N7ae228c2015-09-07 14:22:21 +0530929#ifndef CONFIG_DM_ETH
Mugunthan V N6af98c52015-09-07 14:22:20 +0530930static int cpsw_init(struct eth_device *dev, bd_t *bis)
931{
932 struct cpsw_priv *priv = dev->priv;
933
934 return _cpsw_init(priv, dev->enetaddr);
935}
936
937static void cpsw_halt(struct eth_device *dev)
938{
939 struct cpsw_priv *priv = dev->priv;
940
941 return _cpsw_halt(priv);
942}
943
944static int cpsw_send(struct eth_device *dev, void *packet, int length)
945{
946 struct cpsw_priv *priv = dev->priv;
947
948 return _cpsw_send(priv, packet, length);
949}
950
951static int cpsw_recv(struct eth_device *dev)
952{
953 struct cpsw_priv *priv = dev->priv;
954 uchar *pkt = NULL;
955 int len;
956
957 len = _cpsw_recv(priv, &pkt);
958
959 if (len > 0) {
960 net_process_received_packet(pkt, len);
961 cpdma_submit(priv, &priv->rx_chan, pkt, PKTSIZE);
962 }
963
964 return len;
965}
966
967int cpsw_register(struct cpsw_platform_data *data)
968{
969 struct cpsw_priv *priv;
970 struct eth_device *dev;
971 int ret;
972
973 dev = calloc(sizeof(*dev), 1);
974 if (!dev)
975 return -ENOMEM;
976
977 priv = calloc(sizeof(*priv), 1);
978 if (!priv) {
979 free(dev);
980 return -ENOMEM;
981 }
982
983 priv->dev = dev;
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530984 priv->data = data;
Mugunthan V N6af98c52015-09-07 14:22:20 +0530985
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000986 strcpy(dev->name, "cpsw");
987 dev->iobase = 0;
988 dev->init = cpsw_init;
989 dev->halt = cpsw_halt;
990 dev->send = cpsw_send;
991 dev->recv = cpsw_recv;
992 dev->priv = priv;
993
994 eth_register(dev);
995
Mugunthan V N6af98c52015-09-07 14:22:20 +0530996 ret = _cpsw_register(priv);
997 if (ret < 0) {
998 eth_unregister(dev);
999 free(dev);
1000 free(priv);
1001 return ret;
1002 }
Cyril Chemparathy464adc82012-07-24 12:22:16 +00001003
1004 return 1;
1005}
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301006#else
1007static int cpsw_eth_start(struct udevice *dev)
1008{
1009 struct eth_pdata *pdata = dev_get_platdata(dev);
1010 struct cpsw_priv *priv = dev_get_priv(dev);
1011
1012 return _cpsw_init(priv, pdata->enetaddr);
1013}
1014
1015static int cpsw_eth_send(struct udevice *dev, void *packet, int length)
1016{
1017 struct cpsw_priv *priv = dev_get_priv(dev);
1018
1019 return _cpsw_send(priv, packet, length);
1020}
1021
1022static int cpsw_eth_recv(struct udevice *dev, int flags, uchar **packetp)
1023{
1024 struct cpsw_priv *priv = dev_get_priv(dev);
1025
1026 return _cpsw_recv(priv, packetp);
1027}
1028
1029static int cpsw_eth_free_pkt(struct udevice *dev, uchar *packet,
1030 int length)
1031{
1032 struct cpsw_priv *priv = dev_get_priv(dev);
1033
1034 return cpdma_submit(priv, &priv->rx_chan, packet, PKTSIZE);
1035}
1036
1037static void cpsw_eth_stop(struct udevice *dev)
1038{
1039 struct cpsw_priv *priv = dev_get_priv(dev);
1040
1041 return _cpsw_halt(priv);
1042}
1043
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301044static const struct eth_ops cpsw_eth_ops = {
1045 .start = cpsw_eth_start,
1046 .send = cpsw_eth_send,
1047 .recv = cpsw_eth_recv,
1048 .free_pkt = cpsw_eth_free_pkt,
1049 .stop = cpsw_eth_stop,
1050};
1051
Mugunthan V N77f7bc72016-04-28 15:36:06 +05301052static inline fdt_addr_t cpsw_get_addr_by_node(const void *fdt, int node)
1053{
Stephen Warren7d30e102016-08-05 09:47:51 -06001054 return fdtdec_get_addr_size_auto_noparent(fdt, node, "reg", 0, NULL,
1055 false);
Mugunthan V N77f7bc72016-04-28 15:36:06 +05301056}
1057
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301058static void cpsw_gmii_sel_am3352(struct cpsw_priv *priv,
1059 phy_interface_t phy_mode)
1060{
1061 u32 reg;
1062 u32 mask;
1063 u32 mode = 0;
1064 bool rgmii_id = false;
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301065 int slave = priv->data->active_slave;
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301066
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301067 reg = readl(priv->data->gmii_sel);
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301068
1069 switch (phy_mode) {
1070 case PHY_INTERFACE_MODE_RMII:
1071 mode = AM33XX_GMII_SEL_MODE_RMII;
1072 break;
1073
1074 case PHY_INTERFACE_MODE_RGMII:
1075 mode = AM33XX_GMII_SEL_MODE_RGMII;
1076 break;
1077 case PHY_INTERFACE_MODE_RGMII_ID:
1078 case PHY_INTERFACE_MODE_RGMII_RXID:
1079 case PHY_INTERFACE_MODE_RGMII_TXID:
1080 mode = AM33XX_GMII_SEL_MODE_RGMII;
1081 rgmii_id = true;
1082 break;
1083
1084 case PHY_INTERFACE_MODE_MII:
1085 default:
1086 mode = AM33XX_GMII_SEL_MODE_MII;
1087 break;
1088 };
1089
1090 mask = GMII_SEL_MODE_MASK << (slave * 2) | BIT(slave + 6);
1091 mode <<= slave * 2;
1092
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301093 if (priv->data->rmii_clock_external) {
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301094 if (slave == 0)
1095 mode |= AM33XX_GMII_SEL_RMII1_IO_CLK_EN;
1096 else
1097 mode |= AM33XX_GMII_SEL_RMII2_IO_CLK_EN;
1098 }
1099
1100 if (rgmii_id) {
1101 if (slave == 0)
1102 mode |= AM33XX_GMII_SEL_RGMII1_IDMODE;
1103 else
1104 mode |= AM33XX_GMII_SEL_RGMII2_IDMODE;
1105 }
1106
1107 reg &= ~mask;
1108 reg |= mode;
1109
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301110 writel(reg, priv->data->gmii_sel);
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301111}
1112
1113static void cpsw_gmii_sel_dra7xx(struct cpsw_priv *priv,
1114 phy_interface_t phy_mode)
1115{
1116 u32 reg;
1117 u32 mask;
1118 u32 mode = 0;
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301119 int slave = priv->data->active_slave;
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301120
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301121 reg = readl(priv->data->gmii_sel);
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301122
1123 switch (phy_mode) {
1124 case PHY_INTERFACE_MODE_RMII:
1125 mode = AM33XX_GMII_SEL_MODE_RMII;
1126 break;
1127
1128 case PHY_INTERFACE_MODE_RGMII:
1129 case PHY_INTERFACE_MODE_RGMII_ID:
1130 case PHY_INTERFACE_MODE_RGMII_RXID:
1131 case PHY_INTERFACE_MODE_RGMII_TXID:
1132 mode = AM33XX_GMII_SEL_MODE_RGMII;
1133 break;
1134
1135 case PHY_INTERFACE_MODE_MII:
1136 default:
1137 mode = AM33XX_GMII_SEL_MODE_MII;
1138 break;
1139 };
1140
1141 switch (slave) {
1142 case 0:
1143 mask = GMII_SEL_MODE_MASK;
1144 break;
1145 case 1:
1146 mask = GMII_SEL_MODE_MASK << 4;
1147 mode <<= 4;
1148 break;
1149 default:
1150 dev_err(priv->dev, "invalid slave number...\n");
1151 return;
1152 }
1153
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301154 if (priv->data->rmii_clock_external)
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301155 dev_err(priv->dev, "RMII External clock is not supported\n");
1156
1157 reg &= ~mask;
1158 reg |= mode;
1159
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301160 writel(reg, priv->data->gmii_sel);
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301161}
1162
1163static void cpsw_phy_sel(struct cpsw_priv *priv, const char *compat,
1164 phy_interface_t phy_mode)
1165{
1166 if (!strcmp(compat, "ti,am3352-cpsw-phy-sel"))
1167 cpsw_gmii_sel_am3352(priv, phy_mode);
1168 if (!strcmp(compat, "ti,am43xx-cpsw-phy-sel"))
1169 cpsw_gmii_sel_am3352(priv, phy_mode);
1170 else if (!strcmp(compat, "ti,dra7xx-cpsw-phy-sel"))
1171 cpsw_gmii_sel_dra7xx(priv, phy_mode);
1172}
1173
Faiz Abbas5b5dc0f2019-03-18 13:54:32 +05301174static int cpsw_eth_probe(struct udevice *dev)
1175{
1176 struct cpsw_priv *priv = dev_get_priv(dev);
1177 struct eth_pdata *pdata = dev_get_platdata(dev);
1178
1179 priv->dev = dev;
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301180 priv->data = pdata->priv_pdata;
Faiz Abbas8ecdffe2019-03-18 13:54:34 +05301181 ti_cm_get_macid(dev, priv->data, pdata->enetaddr);
Faiz Abbas5b5dc0f2019-03-18 13:54:32 +05301182 /* Select phy interface in control module */
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301183 cpsw_phy_sel(priv, priv->data->phy_sel_compat,
Faiz Abbas5b5dc0f2019-03-18 13:54:32 +05301184 pdata->phy_interface);
1185
1186 return _cpsw_register(priv);
1187}
1188
Faiz Abbas687e80e2019-03-18 13:54:35 +05301189#if CONFIG_IS_ENABLED(OF_CONTROL)
Grygorii Strashko68e313e2019-09-19 11:16:38 +03001190static void cpsw_eth_of_parse_slave(struct cpsw_platform_data *data,
1191 int slave_index, int subnode)
1192{
1193 struct cpsw_slave_data *slave_data;
1194 const void *fdt = gd->fdt_blob;
1195 const char *phy_mode;
Grygorii Strashkoe32ed8c2019-09-19 11:16:39 +03001196 int max_speed = -1;
Grygorii Strashko68e313e2019-09-19 11:16:38 +03001197 u32 phy_id[2];
1198
1199 slave_data = &data->slave_data[slave_index];
1200
1201 phy_mode = fdt_getprop(fdt, subnode, "phy-mode", NULL);
1202 if (phy_mode)
1203 slave_data->phy_if =
1204 phy_get_interface_by_name(phy_mode);
1205
1206 slave_data->phy_of_handle = fdtdec_lookup_phandle(fdt, subnode,
1207 "phy-handle");
1208
1209 if (data->slave_data[slave_index].phy_of_handle >= 0) {
1210 slave_data->phy_addr =
1211 fdtdec_get_int(fdt, slave_data->phy_of_handle,
1212 "reg", -1);
1213 } else {
1214 fdtdec_get_int_array(fdt, subnode, "phy_id",
1215 phy_id, 2);
1216 slave_data->phy_addr = phy_id[1];
1217 }
Grygorii Strashkoe32ed8c2019-09-19 11:16:39 +03001218
1219 slave_data->max_speed = 0;
1220 max_speed = fdtdec_get_int(fdt, subnode,
1221 "max-speed", max_speed);
1222 if (max_speed > 0)
1223 slave_data->max_speed = max_speed;
Grygorii Strashko68e313e2019-09-19 11:16:38 +03001224}
1225
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301226static int cpsw_eth_ofdata_to_platdata(struct udevice *dev)
1227{
1228 struct eth_pdata *pdata = dev_get_platdata(dev);
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301229 struct cpsw_platform_data *data;
Vignesh R8e9473d2016-08-02 10:14:27 +05301230 struct gpio_desc *mode_gpios;
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301231 const void *fdt = gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -07001232 int node = dev_of_offset(dev);
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301233 int subnode;
1234 int slave_index = 0;
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301235 int active_slave;
Vignesh R8e9473d2016-08-02 10:14:27 +05301236 int num_mode_gpios;
Mugunthan V N9e556352016-04-28 15:36:07 +05301237 int ret;
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301238
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301239 data = calloc(1, sizeof(struct cpsw_platform_data));
1240 pdata->priv_pdata = data;
Simon Glassba1dea42017-05-17 17:18:05 -06001241 pdata->iobase = devfdt_get_addr(dev);
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301242 data->version = CPSW_CTRL_VERSION_2;
1243 data->bd_ram_ofs = CPSW_BD_OFFSET;
1244 data->ale_reg_ofs = CPSW_ALE_OFFSET;
1245 data->cpdma_reg_ofs = CPSW_CPDMA_OFFSET;
1246 data->mdio_div = CPSW_MDIO_DIV;
1247 data->host_port_reg_ofs = CPSW_HOST_PORT_OFFSET,
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301248
1249 pdata->phy_interface = -1;
1250
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301251 data->cpsw_base = pdata->iobase;
1252 data->channels = fdtdec_get_int(fdt, node, "cpdma_channels", -1);
1253 if (data->channels <= 0) {
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301254 printf("error: cpdma_channels not found in dt\n");
1255 return -ENOENT;
1256 }
1257
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301258 data->slaves = fdtdec_get_int(fdt, node, "slaves", -1);
1259 if (data->slaves <= 0) {
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301260 printf("error: slaves not found in dt\n");
1261 return -ENOENT;
1262 }
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301263 data->slave_data = malloc(sizeof(struct cpsw_slave_data) *
1264 data->slaves);
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301265
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301266 data->ale_entries = fdtdec_get_int(fdt, node, "ale_entries", -1);
1267 if (data->ale_entries <= 0) {
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301268 printf("error: ale_entries not found in dt\n");
1269 return -ENOENT;
1270 }
1271
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301272 data->bd_ram_ofs = fdtdec_get_int(fdt, node, "bd_ram_size", -1);
1273 if (data->bd_ram_ofs <= 0) {
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301274 printf("error: bd_ram_size not found in dt\n");
1275 return -ENOENT;
1276 }
1277
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301278 data->mac_control = fdtdec_get_int(fdt, node, "mac_control", -1);
1279 if (data->mac_control <= 0) {
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301280 printf("error: ale_entries not found in dt\n");
1281 return -ENOENT;
1282 }
1283
Vignesh R8e9473d2016-08-02 10:14:27 +05301284 num_mode_gpios = gpio_get_list_count(dev, "mode-gpios");
1285 if (num_mode_gpios > 0) {
1286 mode_gpios = malloc(sizeof(struct gpio_desc) *
1287 num_mode_gpios);
1288 gpio_request_list_by_name(dev, "mode-gpios", mode_gpios,
1289 num_mode_gpios, GPIOD_IS_OUT);
1290 free(mode_gpios);
1291 }
1292
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301293 active_slave = fdtdec_get_int(fdt, node, "active_slave", 0);
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301294 data->active_slave = active_slave;
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301295
Simon Glass499c29e2016-10-02 17:59:29 -06001296 fdt_for_each_subnode(subnode, fdt, node) {
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301297 int len;
1298 const char *name;
1299
1300 name = fdt_get_name(fdt, subnode, &len);
1301 if (!strncmp(name, "mdio", 4)) {
Mugunthan V N77f7bc72016-04-28 15:36:06 +05301302 u32 mdio_base;
1303
1304 mdio_base = cpsw_get_addr_by_node(fdt, subnode);
1305 if (mdio_base == FDT_ADDR_T_NONE) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001306 pr_err("Not able to get MDIO address space\n");
Mugunthan V N77f7bc72016-04-28 15:36:06 +05301307 return -ENOENT;
1308 }
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301309 data->mdio_base = mdio_base;
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301310 }
1311
1312 if (!strncmp(name, "slave", 5)) {
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301313 if (slave_index >= data->slaves)
Mugunthan V N13a27fd2016-04-28 15:36:04 +05301314 continue;
Dan Murphy4b7b24e2016-05-02 15:45:56 -05001315
Grygorii Strashko68e313e2019-09-19 11:16:38 +03001316 cpsw_eth_of_parse_slave(data, slave_index, subnode);
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301317 slave_index++;
1318 }
1319
1320 if (!strncmp(name, "cpsw-phy-sel", 12)) {
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301321 data->gmii_sel = cpsw_get_addr_by_node(fdt, subnode);
Mugunthan V N77f7bc72016-04-28 15:36:06 +05301322
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301323 if (data->gmii_sel == FDT_ADDR_T_NONE) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001324 pr_err("Not able to get gmii_sel reg address\n");
Mugunthan V N77f7bc72016-04-28 15:36:06 +05301325 return -ENOENT;
1326 }
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301327
1328 if (fdt_get_property(fdt, subnode, "rmii-clock-ext",
1329 NULL))
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301330 data->rmii_clock_external = true;
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301331
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301332 data->phy_sel_compat = fdt_getprop(fdt, subnode,
1333 "compatible", NULL);
1334 if (!data->phy_sel_compat) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001335 pr_err("Not able to get gmii_sel compatible\n");
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301336 return -ENOENT;
1337 }
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301338 }
1339 }
1340
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301341 data->slave_data[0].slave_reg_ofs = CPSW_SLAVE0_OFFSET;
1342 data->slave_data[0].sliver_reg_ofs = CPSW_SLIVER0_OFFSET;
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301343
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301344 if (data->slaves == 2) {
1345 data->slave_data[1].slave_reg_ofs = CPSW_SLAVE1_OFFSET;
1346 data->slave_data[1].sliver_reg_ofs = CPSW_SLIVER1_OFFSET;
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301347 }
1348
Faiz Abbas8ecdffe2019-03-18 13:54:34 +05301349 ret = ti_cm_get_macid_addr(dev, active_slave, data);
Mugunthan V N9e556352016-04-28 15:36:07 +05301350 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001351 pr_err("cpsw read efuse mac failed\n");
Mugunthan V N9e556352016-04-28 15:36:07 +05301352 return ret;
1353 }
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301354
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301355 pdata->phy_interface = data->slave_data[active_slave].phy_if;
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301356 if (pdata->phy_interface == -1) {
Grygorii Strashko68e313e2019-09-19 11:16:38 +03001357 debug("%s: Invalid PHY interface '%s'\n", __func__,
1358 phy_string_for_interface(pdata->phy_interface));
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301359 return -EINVAL;
1360 }
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301361
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301362 return 0;
1363}
1364
Faiz Abbas687e80e2019-03-18 13:54:35 +05301365static const struct udevice_id cpsw_eth_ids[] = {
1366 { .compatible = "ti,cpsw" },
1367 { .compatible = "ti,am335x-cpsw" },
1368 { }
1369};
1370#endif
1371
Sekhar Noricfc5cc82018-08-23 17:11:29 +05301372int cpsw_get_slave_phy_addr(struct udevice *dev, int slave)
1373{
1374 struct cpsw_priv *priv = dev_get_priv(dev);
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301375 struct cpsw_platform_data *data = priv->data;
Sekhar Noricfc5cc82018-08-23 17:11:29 +05301376
1377 return data->slave_data[slave].phy_addr;
1378}
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301379
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301380U_BOOT_DRIVER(eth_cpsw) = {
1381 .name = "eth_cpsw",
1382 .id = UCLASS_ETH,
Faiz Abbas687e80e2019-03-18 13:54:35 +05301383#if CONFIG_IS_ENABLED(OF_CONTROL)
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301384 .of_match = cpsw_eth_ids,
1385 .ofdata_to_platdata = cpsw_eth_ofdata_to_platdata,
Faiz Abbas687e80e2019-03-18 13:54:35 +05301386 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
1387#endif
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301388 .probe = cpsw_eth_probe,
1389 .ops = &cpsw_eth_ops,
1390 .priv_auto_alloc_size = sizeof(struct cpsw_priv),
Faiz Abbasd852b942019-03-18 13:54:36 +05301391 .flags = DM_FLAG_ALLOC_PRIV_DMA | DM_FLAG_PRE_RELOC,
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301392};
1393#endif /* CONFIG_DM_ETH */