blob: 533c1679950c694e458f861d9ccf2fbdc2363a09 [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;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000842
Yegor Yefremov062bbac2012-11-26 04:03:16 +0000843 phydev = phy_connect(priv->bus,
Mugunthan V N4944f372014-02-18 07:31:52 -0500844 slave->data->phy_addr,
Mugunthan V N6af98c52015-09-07 14:22:20 +0530845 priv->dev,
Yegor Yefremov062bbac2012-11-26 04:03:16 +0000846 slave->data->phy_if);
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000847
Heiko Schocheref660032013-09-05 11:50:41 +0200848 if (!phydev)
849 return -1;
850
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000851 phydev->supported &= supported;
852 phydev->advertising = phydev->supported;
853
Dan Murphy4b7b24e2016-05-02 15:45:56 -0500854#ifdef CONFIG_DM_ETH
855 if (slave->data->phy_of_handle)
Grygorii Strashkoba1ef862018-07-05 12:02:51 -0500856 phydev->node = offset_to_ofnode(slave->data->phy_of_handle);
Dan Murphy4b7b24e2016-05-02 15:45:56 -0500857#endif
858
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000859 priv->phydev = phydev;
860 phy_config(phydev);
861
862 return 1;
863}
864
Sekhar Noricfc5cc82018-08-23 17:11:29 +0530865static void cpsw_phy_addr_update(struct cpsw_priv *priv)
866{
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530867 struct cpsw_platform_data *data = priv->data;
Grygorii Strashkoeae545a2018-10-31 16:21:44 -0500868 u16 alive = cpsw_mdio_get_alive(priv->bus);
Sekhar Noricfc5cc82018-08-23 17:11:29 +0530869 int active = data->active_slave;
870 int new_addr = ffs(alive) - 1;
871
872 /*
873 * If there is only one phy alive and its address does not match
874 * that of active slave, then phy address can safely be updated.
875 */
876 if (hweight16(alive) == 1 &&
877 data->slave_data[active].phy_addr != new_addr) {
878 printf("Updated phy address for CPSW#%d, old: %d, new: %d\n",
879 active, data->slave_data[active].phy_addr, new_addr);
880 data->slave_data[active].phy_addr = new_addr;
881 }
882}
883
Mugunthan V N6af98c52015-09-07 14:22:20 +0530884int _cpsw_register(struct cpsw_priv *priv)
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000885{
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000886 struct cpsw_slave *slave;
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530887 struct cpsw_platform_data *data = priv->data;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000888 void *regs = (void *)data->cpsw_base;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000889
890 priv->slaves = malloc(sizeof(struct cpsw_slave) * data->slaves);
891 if (!priv->slaves) {
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000892 return -ENOMEM;
893 }
894
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000895 priv->host_port = data->host_port_num;
896 priv->regs = regs;
897 priv->host_port_regs = regs + data->host_port_reg_ofs;
898 priv->dma_regs = regs + data->cpdma_reg_ofs;
899 priv->ale_regs = regs + data->ale_reg_ofs;
Mugunthan V Nff559872013-07-08 16:04:37 +0530900 priv->descs = (void *)regs + data->bd_ram_ofs;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000901
902 int idx = 0;
903
904 for_each_slave(slave, priv) {
905 cpsw_slave_setup(slave, idx, priv);
906 idx = idx + 1;
907 }
908
Grygorii Strashkoeae545a2018-10-31 16:21:44 -0500909 priv->bus = cpsw_mdio_init(priv->dev->name, data->mdio_base, 0, 0);
910 if (!priv->bus)
911 return -EFAULT;
Sekhar Noricfc5cc82018-08-23 17:11:29 +0530912
913 cpsw_phy_addr_update(priv);
914
Mugunthan V N6af98c52015-09-07 14:22:20 +0530915 for_active_slave(slave, priv)
916 cpsw_phy_init(priv, slave);
917
918 return 0;
919}
920
Mugunthan V N7ae228c2015-09-07 14:22:21 +0530921#ifndef CONFIG_DM_ETH
Mugunthan V N6af98c52015-09-07 14:22:20 +0530922static int cpsw_init(struct eth_device *dev, bd_t *bis)
923{
924 struct cpsw_priv *priv = dev->priv;
925
926 return _cpsw_init(priv, dev->enetaddr);
927}
928
929static void cpsw_halt(struct eth_device *dev)
930{
931 struct cpsw_priv *priv = dev->priv;
932
933 return _cpsw_halt(priv);
934}
935
936static int cpsw_send(struct eth_device *dev, void *packet, int length)
937{
938 struct cpsw_priv *priv = dev->priv;
939
940 return _cpsw_send(priv, packet, length);
941}
942
943static int cpsw_recv(struct eth_device *dev)
944{
945 struct cpsw_priv *priv = dev->priv;
946 uchar *pkt = NULL;
947 int len;
948
949 len = _cpsw_recv(priv, &pkt);
950
951 if (len > 0) {
952 net_process_received_packet(pkt, len);
953 cpdma_submit(priv, &priv->rx_chan, pkt, PKTSIZE);
954 }
955
956 return len;
957}
958
959int cpsw_register(struct cpsw_platform_data *data)
960{
961 struct cpsw_priv *priv;
962 struct eth_device *dev;
963 int ret;
964
965 dev = calloc(sizeof(*dev), 1);
966 if (!dev)
967 return -ENOMEM;
968
969 priv = calloc(sizeof(*priv), 1);
970 if (!priv) {
971 free(dev);
972 return -ENOMEM;
973 }
974
975 priv->dev = dev;
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530976 priv->data = data;
Mugunthan V N6af98c52015-09-07 14:22:20 +0530977
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000978 strcpy(dev->name, "cpsw");
979 dev->iobase = 0;
980 dev->init = cpsw_init;
981 dev->halt = cpsw_halt;
982 dev->send = cpsw_send;
983 dev->recv = cpsw_recv;
984 dev->priv = priv;
985
986 eth_register(dev);
987
Mugunthan V N6af98c52015-09-07 14:22:20 +0530988 ret = _cpsw_register(priv);
989 if (ret < 0) {
990 eth_unregister(dev);
991 free(dev);
992 free(priv);
993 return ret;
994 }
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000995
996 return 1;
997}
Mugunthan V N7ae228c2015-09-07 14:22:21 +0530998#else
999static int cpsw_eth_start(struct udevice *dev)
1000{
1001 struct eth_pdata *pdata = dev_get_platdata(dev);
1002 struct cpsw_priv *priv = dev_get_priv(dev);
1003
1004 return _cpsw_init(priv, pdata->enetaddr);
1005}
1006
1007static int cpsw_eth_send(struct udevice *dev, void *packet, int length)
1008{
1009 struct cpsw_priv *priv = dev_get_priv(dev);
1010
1011 return _cpsw_send(priv, packet, length);
1012}
1013
1014static int cpsw_eth_recv(struct udevice *dev, int flags, uchar **packetp)
1015{
1016 struct cpsw_priv *priv = dev_get_priv(dev);
1017
1018 return _cpsw_recv(priv, packetp);
1019}
1020
1021static int cpsw_eth_free_pkt(struct udevice *dev, uchar *packet,
1022 int length)
1023{
1024 struct cpsw_priv *priv = dev_get_priv(dev);
1025
1026 return cpdma_submit(priv, &priv->rx_chan, packet, PKTSIZE);
1027}
1028
1029static void cpsw_eth_stop(struct udevice *dev)
1030{
1031 struct cpsw_priv *priv = dev_get_priv(dev);
1032
1033 return _cpsw_halt(priv);
1034}
1035
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301036static const struct eth_ops cpsw_eth_ops = {
1037 .start = cpsw_eth_start,
1038 .send = cpsw_eth_send,
1039 .recv = cpsw_eth_recv,
1040 .free_pkt = cpsw_eth_free_pkt,
1041 .stop = cpsw_eth_stop,
1042};
1043
Mugunthan V N77f7bc72016-04-28 15:36:06 +05301044static inline fdt_addr_t cpsw_get_addr_by_node(const void *fdt, int node)
1045{
Stephen Warren7d30e102016-08-05 09:47:51 -06001046 return fdtdec_get_addr_size_auto_noparent(fdt, node, "reg", 0, NULL,
1047 false);
Mugunthan V N77f7bc72016-04-28 15:36:06 +05301048}
1049
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301050static void cpsw_gmii_sel_am3352(struct cpsw_priv *priv,
1051 phy_interface_t phy_mode)
1052{
1053 u32 reg;
1054 u32 mask;
1055 u32 mode = 0;
1056 bool rgmii_id = false;
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301057 int slave = priv->data->active_slave;
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301058
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301059 reg = readl(priv->data->gmii_sel);
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301060
1061 switch (phy_mode) {
1062 case PHY_INTERFACE_MODE_RMII:
1063 mode = AM33XX_GMII_SEL_MODE_RMII;
1064 break;
1065
1066 case PHY_INTERFACE_MODE_RGMII:
1067 mode = AM33XX_GMII_SEL_MODE_RGMII;
1068 break;
1069 case PHY_INTERFACE_MODE_RGMII_ID:
1070 case PHY_INTERFACE_MODE_RGMII_RXID:
1071 case PHY_INTERFACE_MODE_RGMII_TXID:
1072 mode = AM33XX_GMII_SEL_MODE_RGMII;
1073 rgmii_id = true;
1074 break;
1075
1076 case PHY_INTERFACE_MODE_MII:
1077 default:
1078 mode = AM33XX_GMII_SEL_MODE_MII;
1079 break;
1080 };
1081
1082 mask = GMII_SEL_MODE_MASK << (slave * 2) | BIT(slave + 6);
1083 mode <<= slave * 2;
1084
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301085 if (priv->data->rmii_clock_external) {
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301086 if (slave == 0)
1087 mode |= AM33XX_GMII_SEL_RMII1_IO_CLK_EN;
1088 else
1089 mode |= AM33XX_GMII_SEL_RMII2_IO_CLK_EN;
1090 }
1091
1092 if (rgmii_id) {
1093 if (slave == 0)
1094 mode |= AM33XX_GMII_SEL_RGMII1_IDMODE;
1095 else
1096 mode |= AM33XX_GMII_SEL_RGMII2_IDMODE;
1097 }
1098
1099 reg &= ~mask;
1100 reg |= mode;
1101
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301102 writel(reg, priv->data->gmii_sel);
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301103}
1104
1105static void cpsw_gmii_sel_dra7xx(struct cpsw_priv *priv,
1106 phy_interface_t phy_mode)
1107{
1108 u32 reg;
1109 u32 mask;
1110 u32 mode = 0;
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301111 int slave = priv->data->active_slave;
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301112
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301113 reg = readl(priv->data->gmii_sel);
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301114
1115 switch (phy_mode) {
1116 case PHY_INTERFACE_MODE_RMII:
1117 mode = AM33XX_GMII_SEL_MODE_RMII;
1118 break;
1119
1120 case PHY_INTERFACE_MODE_RGMII:
1121 case PHY_INTERFACE_MODE_RGMII_ID:
1122 case PHY_INTERFACE_MODE_RGMII_RXID:
1123 case PHY_INTERFACE_MODE_RGMII_TXID:
1124 mode = AM33XX_GMII_SEL_MODE_RGMII;
1125 break;
1126
1127 case PHY_INTERFACE_MODE_MII:
1128 default:
1129 mode = AM33XX_GMII_SEL_MODE_MII;
1130 break;
1131 };
1132
1133 switch (slave) {
1134 case 0:
1135 mask = GMII_SEL_MODE_MASK;
1136 break;
1137 case 1:
1138 mask = GMII_SEL_MODE_MASK << 4;
1139 mode <<= 4;
1140 break;
1141 default:
1142 dev_err(priv->dev, "invalid slave number...\n");
1143 return;
1144 }
1145
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301146 if (priv->data->rmii_clock_external)
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301147 dev_err(priv->dev, "RMII External clock is not supported\n");
1148
1149 reg &= ~mask;
1150 reg |= mode;
1151
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301152 writel(reg, priv->data->gmii_sel);
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301153}
1154
1155static void cpsw_phy_sel(struct cpsw_priv *priv, const char *compat,
1156 phy_interface_t phy_mode)
1157{
1158 if (!strcmp(compat, "ti,am3352-cpsw-phy-sel"))
1159 cpsw_gmii_sel_am3352(priv, phy_mode);
1160 if (!strcmp(compat, "ti,am43xx-cpsw-phy-sel"))
1161 cpsw_gmii_sel_am3352(priv, phy_mode);
1162 else if (!strcmp(compat, "ti,dra7xx-cpsw-phy-sel"))
1163 cpsw_gmii_sel_dra7xx(priv, phy_mode);
1164}
1165
Faiz Abbas5b5dc0f2019-03-18 13:54:32 +05301166static int cpsw_eth_probe(struct udevice *dev)
1167{
1168 struct cpsw_priv *priv = dev_get_priv(dev);
1169 struct eth_pdata *pdata = dev_get_platdata(dev);
1170
1171 priv->dev = dev;
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301172 priv->data = pdata->priv_pdata;
Faiz Abbas8ecdffe2019-03-18 13:54:34 +05301173 ti_cm_get_macid(dev, priv->data, pdata->enetaddr);
Faiz Abbas5b5dc0f2019-03-18 13:54:32 +05301174 /* Select phy interface in control module */
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301175 cpsw_phy_sel(priv, priv->data->phy_sel_compat,
Faiz Abbas5b5dc0f2019-03-18 13:54:32 +05301176 pdata->phy_interface);
1177
1178 return _cpsw_register(priv);
1179}
1180
Faiz Abbas687e80e2019-03-18 13:54:35 +05301181#if CONFIG_IS_ENABLED(OF_CONTROL)
Grygorii Strashko68e313e2019-09-19 11:16:38 +03001182static void cpsw_eth_of_parse_slave(struct cpsw_platform_data *data,
1183 int slave_index, int subnode)
1184{
1185 struct cpsw_slave_data *slave_data;
1186 const void *fdt = gd->fdt_blob;
1187 const char *phy_mode;
1188 u32 phy_id[2];
1189
1190 slave_data = &data->slave_data[slave_index];
1191
1192 phy_mode = fdt_getprop(fdt, subnode, "phy-mode", NULL);
1193 if (phy_mode)
1194 slave_data->phy_if =
1195 phy_get_interface_by_name(phy_mode);
1196
1197 slave_data->phy_of_handle = fdtdec_lookup_phandle(fdt, subnode,
1198 "phy-handle");
1199
1200 if (data->slave_data[slave_index].phy_of_handle >= 0) {
1201 slave_data->phy_addr =
1202 fdtdec_get_int(fdt, slave_data->phy_of_handle,
1203 "reg", -1);
1204 } else {
1205 fdtdec_get_int_array(fdt, subnode, "phy_id",
1206 phy_id, 2);
1207 slave_data->phy_addr = phy_id[1];
1208 }
1209}
1210
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301211static int cpsw_eth_ofdata_to_platdata(struct udevice *dev)
1212{
1213 struct eth_pdata *pdata = dev_get_platdata(dev);
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301214 struct cpsw_platform_data *data;
Vignesh R8e9473d2016-08-02 10:14:27 +05301215 struct gpio_desc *mode_gpios;
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301216 const void *fdt = gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -07001217 int node = dev_of_offset(dev);
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301218 int subnode;
1219 int slave_index = 0;
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301220 int active_slave;
Vignesh R8e9473d2016-08-02 10:14:27 +05301221 int num_mode_gpios;
Mugunthan V N9e556352016-04-28 15:36:07 +05301222 int ret;
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301223
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301224 data = calloc(1, sizeof(struct cpsw_platform_data));
1225 pdata->priv_pdata = data;
Simon Glassba1dea42017-05-17 17:18:05 -06001226 pdata->iobase = devfdt_get_addr(dev);
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301227 data->version = CPSW_CTRL_VERSION_2;
1228 data->bd_ram_ofs = CPSW_BD_OFFSET;
1229 data->ale_reg_ofs = CPSW_ALE_OFFSET;
1230 data->cpdma_reg_ofs = CPSW_CPDMA_OFFSET;
1231 data->mdio_div = CPSW_MDIO_DIV;
1232 data->host_port_reg_ofs = CPSW_HOST_PORT_OFFSET,
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301233
1234 pdata->phy_interface = -1;
1235
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301236 data->cpsw_base = pdata->iobase;
1237 data->channels = fdtdec_get_int(fdt, node, "cpdma_channels", -1);
1238 if (data->channels <= 0) {
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301239 printf("error: cpdma_channels not found in dt\n");
1240 return -ENOENT;
1241 }
1242
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301243 data->slaves = fdtdec_get_int(fdt, node, "slaves", -1);
1244 if (data->slaves <= 0) {
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301245 printf("error: slaves not found in dt\n");
1246 return -ENOENT;
1247 }
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301248 data->slave_data = malloc(sizeof(struct cpsw_slave_data) *
1249 data->slaves);
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301250
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301251 data->ale_entries = fdtdec_get_int(fdt, node, "ale_entries", -1);
1252 if (data->ale_entries <= 0) {
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301253 printf("error: ale_entries not found in dt\n");
1254 return -ENOENT;
1255 }
1256
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301257 data->bd_ram_ofs = fdtdec_get_int(fdt, node, "bd_ram_size", -1);
1258 if (data->bd_ram_ofs <= 0) {
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301259 printf("error: bd_ram_size not found in dt\n");
1260 return -ENOENT;
1261 }
1262
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301263 data->mac_control = fdtdec_get_int(fdt, node, "mac_control", -1);
1264 if (data->mac_control <= 0) {
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301265 printf("error: ale_entries not found in dt\n");
1266 return -ENOENT;
1267 }
1268
Vignesh R8e9473d2016-08-02 10:14:27 +05301269 num_mode_gpios = gpio_get_list_count(dev, "mode-gpios");
1270 if (num_mode_gpios > 0) {
1271 mode_gpios = malloc(sizeof(struct gpio_desc) *
1272 num_mode_gpios);
1273 gpio_request_list_by_name(dev, "mode-gpios", mode_gpios,
1274 num_mode_gpios, GPIOD_IS_OUT);
1275 free(mode_gpios);
1276 }
1277
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301278 active_slave = fdtdec_get_int(fdt, node, "active_slave", 0);
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301279 data->active_slave = active_slave;
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301280
Simon Glass499c29e2016-10-02 17:59:29 -06001281 fdt_for_each_subnode(subnode, fdt, node) {
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301282 int len;
1283 const char *name;
1284
1285 name = fdt_get_name(fdt, subnode, &len);
1286 if (!strncmp(name, "mdio", 4)) {
Mugunthan V N77f7bc72016-04-28 15:36:06 +05301287 u32 mdio_base;
1288
1289 mdio_base = cpsw_get_addr_by_node(fdt, subnode);
1290 if (mdio_base == FDT_ADDR_T_NONE) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001291 pr_err("Not able to get MDIO address space\n");
Mugunthan V N77f7bc72016-04-28 15:36:06 +05301292 return -ENOENT;
1293 }
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301294 data->mdio_base = mdio_base;
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301295 }
1296
1297 if (!strncmp(name, "slave", 5)) {
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301298 if (slave_index >= data->slaves)
Mugunthan V N13a27fd2016-04-28 15:36:04 +05301299 continue;
Dan Murphy4b7b24e2016-05-02 15:45:56 -05001300
Grygorii Strashko68e313e2019-09-19 11:16:38 +03001301 cpsw_eth_of_parse_slave(data, slave_index, subnode);
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301302 slave_index++;
1303 }
1304
1305 if (!strncmp(name, "cpsw-phy-sel", 12)) {
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301306 data->gmii_sel = cpsw_get_addr_by_node(fdt, subnode);
Mugunthan V N77f7bc72016-04-28 15:36:06 +05301307
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301308 if (data->gmii_sel == FDT_ADDR_T_NONE) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001309 pr_err("Not able to get gmii_sel reg address\n");
Mugunthan V N77f7bc72016-04-28 15:36:06 +05301310 return -ENOENT;
1311 }
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301312
1313 if (fdt_get_property(fdt, subnode, "rmii-clock-ext",
1314 NULL))
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301315 data->rmii_clock_external = true;
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301316
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301317 data->phy_sel_compat = fdt_getprop(fdt, subnode,
1318 "compatible", NULL);
1319 if (!data->phy_sel_compat) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001320 pr_err("Not able to get gmii_sel compatible\n");
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301321 return -ENOENT;
1322 }
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301323 }
1324 }
1325
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301326 data->slave_data[0].slave_reg_ofs = CPSW_SLAVE0_OFFSET;
1327 data->slave_data[0].sliver_reg_ofs = CPSW_SLIVER0_OFFSET;
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301328
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301329 if (data->slaves == 2) {
1330 data->slave_data[1].slave_reg_ofs = CPSW_SLAVE1_OFFSET;
1331 data->slave_data[1].sliver_reg_ofs = CPSW_SLIVER1_OFFSET;
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301332 }
1333
Faiz Abbas8ecdffe2019-03-18 13:54:34 +05301334 ret = ti_cm_get_macid_addr(dev, active_slave, data);
Mugunthan V N9e556352016-04-28 15:36:07 +05301335 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001336 pr_err("cpsw read efuse mac failed\n");
Mugunthan V N9e556352016-04-28 15:36:07 +05301337 return ret;
1338 }
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301339
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301340 pdata->phy_interface = data->slave_data[active_slave].phy_if;
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301341 if (pdata->phy_interface == -1) {
Grygorii Strashko68e313e2019-09-19 11:16:38 +03001342 debug("%s: Invalid PHY interface '%s'\n", __func__,
1343 phy_string_for_interface(pdata->phy_interface));
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301344 return -EINVAL;
1345 }
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301346
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301347 return 0;
1348}
1349
Faiz Abbas687e80e2019-03-18 13:54:35 +05301350static const struct udevice_id cpsw_eth_ids[] = {
1351 { .compatible = "ti,cpsw" },
1352 { .compatible = "ti,am335x-cpsw" },
1353 { }
1354};
1355#endif
1356
Sekhar Noricfc5cc82018-08-23 17:11:29 +05301357int cpsw_get_slave_phy_addr(struct udevice *dev, int slave)
1358{
1359 struct cpsw_priv *priv = dev_get_priv(dev);
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301360 struct cpsw_platform_data *data = priv->data;
Sekhar Noricfc5cc82018-08-23 17:11:29 +05301361
1362 return data->slave_data[slave].phy_addr;
1363}
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301364
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301365U_BOOT_DRIVER(eth_cpsw) = {
1366 .name = "eth_cpsw",
1367 .id = UCLASS_ETH,
Faiz Abbas687e80e2019-03-18 13:54:35 +05301368#if CONFIG_IS_ENABLED(OF_CONTROL)
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301369 .of_match = cpsw_eth_ids,
1370 .ofdata_to_platdata = cpsw_eth_ofdata_to_platdata,
Faiz Abbas687e80e2019-03-18 13:54:35 +05301371 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
1372#endif
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301373 .probe = cpsw_eth_probe,
1374 .ops = &cpsw_eth_ops,
1375 .priv_auto_alloc_size = sizeof(struct cpsw_priv),
Faiz Abbasd852b942019-03-18 13:54:36 +05301376 .flags = DM_FLAG_ALLOC_PRIV_DMA | DM_FLAG_PRE_RELOC,
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301377};
1378#endif /* CONFIG_DM_ETH */