blob: f0d008f0f56df3b433daf88c103d9032a49e6a64 [file] [log] [blame]
Grygorii Strashkocbec53b2018-10-31 16:21:42 -05001// SPDX-License-Identifier: GPL-2.0+
Cyril Chemparathy2b629972012-07-24 12:22:16 +00002/*
3 * CPSW Ethernet Switch Driver
4 *
Grygorii Strashkocbec53b2018-10-31 16:21:42 -05005 * Copyright (C) 2010-2018 Texas Instruments Incorporated - http://www.ti.com/
Cyril Chemparathy2b629972012-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 Yamada1221ce42016-09-21 11:28:55 +090016#include <linux/errno.h>
Vignesh R2e205ef2016-08-02 10:14:27 +053017#include <asm/gpio.h>
Cyril Chemparathy2b629972012-07-24 12:22:16 +000018#include <asm/io.h>
19#include <phy.h>
Tom Rini98f92002013-03-14 11:15:25 +000020#include <asm/arch/cpu.h>
Mugunthan V N4cc77892015-09-07 14:22:21 +053021#include <dm.h>
Mugunthan V Ne4310562016-04-28 15:36:07 +053022#include <fdt_support.h>
Mugunthan V N4cc77892015-09-07 14:22:21 +053023
Grygorii Strashko4f41cd92018-10-31 16:21:44 -050024#include "cpsw_mdio.h"
25
Mugunthan V N4cc77892015-09-07 14:22:21 +053026DECLARE_GLOBAL_DATA_PTR;
Cyril Chemparathy2b629972012-07-24 12:22:16 +000027
28#define BITMASK(bits) (BIT(bits) - 1)
Cyril Chemparathy2b629972012-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 Strashko60e81d02019-09-19 11:16:37 +030036#define CTL_EXT_EN BIT(18)
Cyril Chemparathy2b629972012-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 Chemparathy2b629972012-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 Chemparathy2b629972012-07-24 12:22:16 +000062#define CPDMA_TIMEOUT 100 /* msecs */
63
Cyril Chemparathy2b629972012-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 Porterf6f86a62013-03-20 05:38:12 +000078#ifdef CONFIG_AM33XX
Cyril Chemparathy2b629972012-07-24 12:22:16 +000079 u32 gap_thresh;
Matt Porterf6f86a62013-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 Chemparathy2b629972012-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 Nab971532016-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 Chemparathy2b629972012-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 N7a022752014-05-22 14:37:10 +0530194#define for_active_slave(slave, priv) \
Faiz Abbasf32a8162019-03-18 13:54:33 +0530195 slave = (priv)->slaves + ((priv)->data)->active_slave; if (slave)
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000196#define for_each_slave(slave, priv) \
197 for (slave = (priv)->slaves; slave != (priv)->slaves + \
Faiz Abbasf32a8162019-03-18 13:54:33 +0530198 ((priv)->data)->slaves; slave++)
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000199
200struct cpsw_priv {
Mugunthan V N4cc77892015-09-07 14:22:21 +0530201#ifdef CONFIG_DM_ETH
202 struct udevice *dev;
203#else
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000204 struct eth_device *dev;
Mugunthan V N4cc77892015-09-07 14:22:21 +0530205#endif
Faiz Abbasf32a8162019-03-18 13:54:33 +0530206 struct cpsw_platform_data *data;
Cyril Chemparathy2b629972012-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 N48ec5292013-02-19 21:34:44 +0000221
Mugunthan V N48ec5292013-02-19 21:34:44 +0000222 u32 phy_mask;
Cyril Chemparathy2b629972012-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 Hershberger0adb5b72015-04-08 01:41:04 -0500276static inline void cpsw_ale_set_addr(u32 *ale_entry, const u8 *addr)
Cyril Chemparathy2b629972012-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 Hershberger0adb5b72015-04-08 01:41:04 -0500308static int cpsw_ale_match_addr(struct cpsw_priv *priv, const u8 *addr)
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000309{
310 u32 ale_entry[ALE_ENTRY_WORDS];
311 int type, idx;
312
Faiz Abbasf32a8162019-03-18 13:54:33 +0530313 for (idx = 0; idx < priv->data->ale_entries; idx++) {
Cyril Chemparathy2b629972012-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 Abbasf32a8162019-03-18 13:54:33 +0530332 for (idx = 0; idx < priv->data->ale_entries; idx++) {
Cyril Chemparathy2b629972012-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 Abbasf32a8162019-03-18 13:54:33 +0530346 for (idx = 0; idx < priv->data->ale_entries; idx++) {
Cyril Chemparathy2b629972012-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 Hershberger0adb5b72015-04-08 01:41:04 -0500361static int cpsw_ale_add_ucast(struct cpsw_priv *priv, const u8 *addr,
Cyril Chemparathy2b629972012-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 Hershberger0adb5b72015-04-08 01:41:04 -0500386static int cpsw_ale_add_mcast(struct cpsw_priv *priv, const u8 *addr,
387 int port_mask)
Cyril Chemparathy2b629972012-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 Chemparathy2b629972012-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 N4cc77892015-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 Chemparathy2b629972012-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 N4cc77892015-09-07 14:22:21 +0530464#endif
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000465}
466
Sekhar Nori96d1d842017-05-08 20:49:56 +0530467static int cpsw_slave_update_link(struct cpsw_slave *slave,
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000468 struct cpsw_priv *priv, int *link)
469{
Heiko Schocher93ff2552013-09-05 11:50:41 +0200470 struct phy_device *phy;
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000471 u32 mac_control = 0;
Sekhar Nori96d1d842017-05-08 20:49:56 +0530472 int ret = -ENODEV;
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000473
Heiko Schocher93ff2552013-09-05 11:50:41 +0200474 phy = priv->phydev;
Heiko Schocher93ff2552013-09-05 11:50:41 +0200475 if (!phy)
Sekhar Nori96d1d842017-05-08 20:49:56 +0530476 goto out;
Heiko Schocher93ff2552013-09-05 11:50:41 +0200477
Sekhar Nori96d1d842017-05-08 20:49:56 +0530478 ret = phy_startup(phy);
479 if (ret)
480 goto out;
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000481
Sekhar Nori96d1d842017-05-08 20:49:56 +0530482 if (link)
483 *link = phy->link;
484
485 if (phy->link) { /* link up */
Faiz Abbasf32a8162019-03-18 13:54:33 +0530486 mac_control = priv->data->mac_control;
Cyril Chemparathy2b629972012-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 Strashko60e81d02019-09-19 11:16:37 +0300493 if (phy->speed == 10 && phy_interface_is_rgmii(phy))
494 mac_control |= CTL_EXT_EN;
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000495 }
496
497 if (mac_control == slave->mac_control)
Sekhar Nori96d1d842017-05-08 20:49:56 +0530498 goto out;
Cyril Chemparathy2b629972012-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 Nori96d1d842017-05-08 20:49:56 +0530510
511out:
512 return ret;
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000513}
514
515static int cpsw_update_link(struct cpsw_priv *priv)
516{
Sekhar Nori96d1d842017-05-08 20:49:56 +0530517 int ret = -ENODEV;
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000518 struct cpsw_slave *slave;
519
Mugunthan V N7a022752014-05-22 14:37:10 +0530520 for_active_slave(slave, priv)
Sekhar Nori96d1d842017-05-08 20:49:56 +0530521 ret = cpsw_slave_update_link(slave, priv, NULL);
Stefan Roese5a834c12014-08-25 11:26:19 +0200522
Sekhar Nori96d1d842017-05-08 20:49:56 +0530523 return ret;
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000524}
525
526static 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 Hershberger0adb5b72015-04-08 01:41:04 -0500554 cpsw_ale_add_mcast(priv, net_bcast_ethaddr, 1 << slave_port);
Mugunthan V N48ec5292013-02-19 21:34:44 +0000555
Mugunthan V N9c653aa2014-02-18 07:31:52 -0500556 priv->phy_mask |= 1 << slave->data->phy_addr;
Cyril Chemparathy2b629972012-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 Nbcd5eed2015-09-07 14:22:20 +0530654static int _cpsw_init(struct cpsw_priv *priv, u8 *enetaddr)
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000655{
Cyril Chemparathy2b629972012-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 N454ac632013-07-08 16:04:38 +0530676 __raw_writel(0x7, &priv->regs->stat_port_en);
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000677
678 cpsw_ale_port_state(priv, priv->host_port, ALE_PORT_STATE_FORWARD);
679
Mugunthan V Nbcd5eed2015-09-07 14:22:20 +0530680 cpsw_ale_add_ucast(priv, enetaddr, priv->host_port, ALE_SECURE);
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500681 cpsw_ale_add_mcast(priv, net_bcast_ethaddr, 1 << priv->host_port);
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000682
Mugunthan V N7a022752014-05-22 14:37:10 +0530683 for_active_slave(slave, priv)
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000684 cpsw_slave_init(slave, priv);
685
Sekhar Nori96d1d842017-05-08 20:49:56 +0530686 ret = cpsw_update_link(priv);
687 if (ret)
688 goto out;
Cyril Chemparathy2b629972012-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 Abbasf32a8162019-03-18 13:54:33 +0530698 if (priv->data->version == CPSW_CTRL_VERSION_2) {
Cyril Chemparathy2b629972012-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 Abbasf32a8162019-03-18 13:54:33 +0530721 if (priv->data->version == CPSW_CTRL_VERSION_2) {
722 for (i = 0; i < priv->data->channels; i++) {
Cyril Chemparathy2b629972012-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 Abbasf32a8162019-03-18 13:54:33 +0530735 for (i = 0; i < priv->data->channels; i++) {
Cyril Chemparathy2b629972012-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 Hershberger1fd92db2015-04-08 01:41:06 -0500755 ret = cpdma_submit(priv, &priv->rx_chan, net_rx_packets[i],
Cyril Chemparathy2b629972012-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 Nori96d1d842017-05-08 20:49:56 +0530763out:
764 return ret;
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000765}
766
Alex Kiernan286bea22018-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 Nbcd5eed2015-09-07 14:22:20 +0530779static void _cpsw_halt(struct cpsw_priv *priv)
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000780{
Alex Kiernan286bea22018-05-12 07:30:02 +0000781 cpsw_reap_completed_packets(priv);
782
Cyril Chemparathy2b629972012-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 Chemparathy2b629972012-07-24 12:22:16 +0000792}
793
Mugunthan V Nbcd5eed2015-09-07 14:22:20 +0530794static int _cpsw_send(struct cpsw_priv *priv, void *packet, int length)
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000795{
Alex Kiernan286bea22018-05-12 07:30:02 +0000796 int timeout;
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000797
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000798 flush_dcache_range((unsigned long)packet,
Lokesh Vutla1f019622016-08-11 13:00:59 +0530799 (unsigned long)packet + ALIGN(length, PKTALIGN));
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000800
Alex Kiernan286bea22018-05-12 07:30:02 +0000801 timeout = cpsw_reap_completed_packets(priv);
Cyril Chemparathy2b629972012-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 Nbcd5eed2015-09-07 14:22:20 +0530810static int _cpsw_recv(struct cpsw_priv *priv, uchar **pkt)
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000811{
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000812 void *buffer;
813 int len;
Heinrich Schuchardt4b23d3c82018-03-18 11:24:38 +0100814 int ret;
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000815
Mugunthan V Nbcd5eed2015-09-07 14:22:20 +0530816 ret = cpdma_process(priv, &priv->rx_chan, &buffer, &len);
817 if (ret < 0)
818 return ret;
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000819
Mugunthan V Nbcd5eed2015-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 Chemparathy2b629972012-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 Abbasf32a8162019-03-18 13:54:33 +0530831 struct cpsw_slave_data *data = priv->data->slave_data + slave_num;
Cyril Chemparathy2b629972012-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 Nbcd5eed2015-09-07 14:22:20 +0530838static int cpsw_phy_init(struct cpsw_priv *priv, struct cpsw_slave *slave)
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000839{
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000840 struct phy_device *phydev;
Ilya Ledvichef59bb72014-03-12 11:26:30 +0200841 u32 supported = PHY_GBIT_FEATURES;
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000842
Yegor Yefremovcdd07292012-11-26 04:03:16 +0000843 phydev = phy_connect(priv->bus,
Mugunthan V N9c653aa2014-02-18 07:31:52 -0500844 slave->data->phy_addr,
Mugunthan V Nbcd5eed2015-09-07 14:22:20 +0530845 priv->dev,
Yegor Yefremovcdd07292012-11-26 04:03:16 +0000846 slave->data->phy_if);
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000847
Heiko Schocher93ff2552013-09-05 11:50:41 +0200848 if (!phydev)
849 return -1;
850
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000851 phydev->supported &= supported;
852 phydev->advertising = phydev->supported;
853
Dan Murphycb386222016-05-02 15:45:56 -0500854#ifdef CONFIG_DM_ETH
855 if (slave->data->phy_of_handle)
Grygorii Strashkod4bb9812018-07-05 12:02:51 -0500856 phydev->node = offset_to_ofnode(slave->data->phy_of_handle);
Dan Murphycb386222016-05-02 15:45:56 -0500857#endif
858
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000859 priv->phydev = phydev;
860 phy_config(phydev);
861
862 return 1;
863}
864
Sekhar Norie2597be2018-08-23 17:11:29 +0530865static void cpsw_phy_addr_update(struct cpsw_priv *priv)
866{
Faiz Abbasf32a8162019-03-18 13:54:33 +0530867 struct cpsw_platform_data *data = priv->data;
Grygorii Strashko4f41cd92018-10-31 16:21:44 -0500868 u16 alive = cpsw_mdio_get_alive(priv->bus);
Sekhar Norie2597be2018-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 Nbcd5eed2015-09-07 14:22:20 +0530884int _cpsw_register(struct cpsw_priv *priv)
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000885{
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000886 struct cpsw_slave *slave;
Faiz Abbasf32a8162019-03-18 13:54:33 +0530887 struct cpsw_platform_data *data = priv->data;
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000888 void *regs = (void *)data->cpsw_base;
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000889
890 priv->slaves = malloc(sizeof(struct cpsw_slave) * data->slaves);
891 if (!priv->slaves) {
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000892 return -ENOMEM;
893 }
894
Cyril Chemparathy2b629972012-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 N2bf36ac2013-07-08 16:04:37 +0530900 priv->descs = (void *)regs + data->bd_ram_ofs;
Cyril Chemparathy2b629972012-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 Strashko4f41cd92018-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 Norie2597be2018-08-23 17:11:29 +0530912
913 cpsw_phy_addr_update(priv);
914
Mugunthan V Nbcd5eed2015-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 N4cc77892015-09-07 14:22:21 +0530921#ifndef CONFIG_DM_ETH
Mugunthan V Nbcd5eed2015-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 Abbasf32a8162019-03-18 13:54:33 +0530976 priv->data = data;
Mugunthan V Nbcd5eed2015-09-07 14:22:20 +0530977
Cyril Chemparathy2b629972012-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 Nbcd5eed2015-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 Chemparathy2b629972012-07-24 12:22:16 +0000995
996 return 1;
997}
Mugunthan V N4cc77892015-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 N4cc77892015-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 N66e740c2016-04-28 15:36:06 +05301044static inline fdt_addr_t cpsw_get_addr_by_node(const void *fdt, int node)
1045{
Stephen Warren6e06acb2016-08-05 09:47:51 -06001046 return fdtdec_get_addr_size_auto_noparent(fdt, node, "reg", 0, NULL,
1047 false);
Mugunthan V N66e740c2016-04-28 15:36:06 +05301048}
1049
Mugunthan V Nab971532016-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 Abbasf32a8162019-03-18 13:54:33 +05301057 int slave = priv->data->active_slave;
Mugunthan V Nab971532016-10-13 19:33:38 +05301058
Faiz Abbasf32a8162019-03-18 13:54:33 +05301059 reg = readl(priv->data->gmii_sel);
Mugunthan V Nab971532016-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 Abbasf32a8162019-03-18 13:54:33 +05301085 if (priv->data->rmii_clock_external) {
Mugunthan V Nab971532016-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 Abbasf32a8162019-03-18 13:54:33 +05301102 writel(reg, priv->data->gmii_sel);
Mugunthan V Nab971532016-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 Abbasf32a8162019-03-18 13:54:33 +05301111 int slave = priv->data->active_slave;
Mugunthan V Nab971532016-10-13 19:33:38 +05301112
Faiz Abbasf32a8162019-03-18 13:54:33 +05301113 reg = readl(priv->data->gmii_sel);
Mugunthan V Nab971532016-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 Abbasf32a8162019-03-18 13:54:33 +05301146 if (priv->data->rmii_clock_external)
Mugunthan V Nab971532016-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 Abbasf32a8162019-03-18 13:54:33 +05301152 writel(reg, priv->data->gmii_sel);
Mugunthan V Nab971532016-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 Abbase50f8782019-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 Abbasf32a8162019-03-18 13:54:33 +05301172 priv->data = pdata->priv_pdata;
Faiz Abbasa58d2222019-03-18 13:54:34 +05301173 ti_cm_get_macid(dev, priv->data, pdata->enetaddr);
Faiz Abbase50f8782019-03-18 13:54:32 +05301174 /* Select phy interface in control module */
Faiz Abbasf32a8162019-03-18 13:54:33 +05301175 cpsw_phy_sel(priv, priv->data->phy_sel_compat,
Faiz Abbase50f8782019-03-18 13:54:32 +05301176 pdata->phy_interface);
1177
1178 return _cpsw_register(priv);
1179}
1180
Faiz Abbasc3b460a2019-03-18 13:54:35 +05301181#if CONFIG_IS_ENABLED(OF_CONTROL)
Mugunthan V N4cc77892015-09-07 14:22:21 +05301182static int cpsw_eth_ofdata_to_platdata(struct udevice *dev)
1183{
1184 struct eth_pdata *pdata = dev_get_platdata(dev);
Faiz Abbasf32a8162019-03-18 13:54:33 +05301185 struct cpsw_platform_data *data;
Vignesh R2e205ef2016-08-02 10:14:27 +05301186 struct gpio_desc *mode_gpios;
Mugunthan V N4cc77892015-09-07 14:22:21 +05301187 const char *phy_mode;
1188 const void *fdt = gd->fdt_blob;
Simon Glasse160f7d2017-01-17 16:52:55 -07001189 int node = dev_of_offset(dev);
Mugunthan V N4cc77892015-09-07 14:22:21 +05301190 int subnode;
1191 int slave_index = 0;
Mugunthan V N4cc77892015-09-07 14:22:21 +05301192 int active_slave;
Vignesh R2e205ef2016-08-02 10:14:27 +05301193 int num_mode_gpios;
Mugunthan V Ne4310562016-04-28 15:36:07 +05301194 int ret;
Mugunthan V N4cc77892015-09-07 14:22:21 +05301195
Faiz Abbasf32a8162019-03-18 13:54:33 +05301196 data = calloc(1, sizeof(struct cpsw_platform_data));
1197 pdata->priv_pdata = data;
Simon Glassa821c4a2017-05-17 17:18:05 -06001198 pdata->iobase = devfdt_get_addr(dev);
Faiz Abbasf32a8162019-03-18 13:54:33 +05301199 data->version = CPSW_CTRL_VERSION_2;
1200 data->bd_ram_ofs = CPSW_BD_OFFSET;
1201 data->ale_reg_ofs = CPSW_ALE_OFFSET;
1202 data->cpdma_reg_ofs = CPSW_CPDMA_OFFSET;
1203 data->mdio_div = CPSW_MDIO_DIV;
1204 data->host_port_reg_ofs = CPSW_HOST_PORT_OFFSET,
Mugunthan V N4cc77892015-09-07 14:22:21 +05301205
1206 pdata->phy_interface = -1;
1207
Faiz Abbasf32a8162019-03-18 13:54:33 +05301208 data->cpsw_base = pdata->iobase;
1209 data->channels = fdtdec_get_int(fdt, node, "cpdma_channels", -1);
1210 if (data->channels <= 0) {
Mugunthan V N4cc77892015-09-07 14:22:21 +05301211 printf("error: cpdma_channels not found in dt\n");
1212 return -ENOENT;
1213 }
1214
Faiz Abbasf32a8162019-03-18 13:54:33 +05301215 data->slaves = fdtdec_get_int(fdt, node, "slaves", -1);
1216 if (data->slaves <= 0) {
Mugunthan V N4cc77892015-09-07 14:22:21 +05301217 printf("error: slaves not found in dt\n");
1218 return -ENOENT;
1219 }
Faiz Abbasf32a8162019-03-18 13:54:33 +05301220 data->slave_data = malloc(sizeof(struct cpsw_slave_data) *
1221 data->slaves);
Mugunthan V N4cc77892015-09-07 14:22:21 +05301222
Faiz Abbasf32a8162019-03-18 13:54:33 +05301223 data->ale_entries = fdtdec_get_int(fdt, node, "ale_entries", -1);
1224 if (data->ale_entries <= 0) {
Mugunthan V N4cc77892015-09-07 14:22:21 +05301225 printf("error: ale_entries not found in dt\n");
1226 return -ENOENT;
1227 }
1228
Faiz Abbasf32a8162019-03-18 13:54:33 +05301229 data->bd_ram_ofs = fdtdec_get_int(fdt, node, "bd_ram_size", -1);
1230 if (data->bd_ram_ofs <= 0) {
Mugunthan V N4cc77892015-09-07 14:22:21 +05301231 printf("error: bd_ram_size not found in dt\n");
1232 return -ENOENT;
1233 }
1234
Faiz Abbasf32a8162019-03-18 13:54:33 +05301235 data->mac_control = fdtdec_get_int(fdt, node, "mac_control", -1);
1236 if (data->mac_control <= 0) {
Mugunthan V N4cc77892015-09-07 14:22:21 +05301237 printf("error: ale_entries not found in dt\n");
1238 return -ENOENT;
1239 }
1240
Vignesh R2e205ef2016-08-02 10:14:27 +05301241 num_mode_gpios = gpio_get_list_count(dev, "mode-gpios");
1242 if (num_mode_gpios > 0) {
1243 mode_gpios = malloc(sizeof(struct gpio_desc) *
1244 num_mode_gpios);
1245 gpio_request_list_by_name(dev, "mode-gpios", mode_gpios,
1246 num_mode_gpios, GPIOD_IS_OUT);
1247 free(mode_gpios);
1248 }
1249
Mugunthan V N4cc77892015-09-07 14:22:21 +05301250 active_slave = fdtdec_get_int(fdt, node, "active_slave", 0);
Faiz Abbasf32a8162019-03-18 13:54:33 +05301251 data->active_slave = active_slave;
Mugunthan V N4cc77892015-09-07 14:22:21 +05301252
Simon Glassdf87e6b2016-10-02 17:59:29 -06001253 fdt_for_each_subnode(subnode, fdt, node) {
Mugunthan V N4cc77892015-09-07 14:22:21 +05301254 int len;
1255 const char *name;
1256
1257 name = fdt_get_name(fdt, subnode, &len);
1258 if (!strncmp(name, "mdio", 4)) {
Mugunthan V N66e740c2016-04-28 15:36:06 +05301259 u32 mdio_base;
1260
1261 mdio_base = cpsw_get_addr_by_node(fdt, subnode);
1262 if (mdio_base == FDT_ADDR_T_NONE) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +09001263 pr_err("Not able to get MDIO address space\n");
Mugunthan V N66e740c2016-04-28 15:36:06 +05301264 return -ENOENT;
1265 }
Faiz Abbasf32a8162019-03-18 13:54:33 +05301266 data->mdio_base = mdio_base;
Mugunthan V N4cc77892015-09-07 14:22:21 +05301267 }
1268
1269 if (!strncmp(name, "slave", 5)) {
1270 u32 phy_id[2];
1271
Faiz Abbasf32a8162019-03-18 13:54:33 +05301272 if (slave_index >= data->slaves)
Mugunthan V Nb2003c52016-04-28 15:36:04 +05301273 continue;
Mugunthan V N4cc77892015-09-07 14:22:21 +05301274 phy_mode = fdt_getprop(fdt, subnode, "phy-mode", NULL);
1275 if (phy_mode)
Faiz Abbasf32a8162019-03-18 13:54:33 +05301276 data->slave_data[slave_index].phy_if =
Mugunthan V N4cc77892015-09-07 14:22:21 +05301277 phy_get_interface_by_name(phy_mode);
Dan Murphycb386222016-05-02 15:45:56 -05001278
Faiz Abbasf32a8162019-03-18 13:54:33 +05301279 data->slave_data[slave_index].phy_of_handle =
Dan Murphycb386222016-05-02 15:45:56 -05001280 fdtdec_lookup_phandle(fdt, subnode,
1281 "phy-handle");
1282
Faiz Abbasf32a8162019-03-18 13:54:33 +05301283 if (data->slave_data[slave_index].phy_of_handle >= 0) {
1284 data->slave_data[slave_index].phy_addr =
Dan Murphycb386222016-05-02 15:45:56 -05001285 fdtdec_get_int(gd->fdt_blob,
Faiz Abbasf32a8162019-03-18 13:54:33 +05301286 data->slave_data[slave_index].phy_of_handle,
Dan Murphycb386222016-05-02 15:45:56 -05001287 "reg", -1);
1288 } else {
1289 fdtdec_get_int_array(fdt, subnode, "phy_id",
1290 phy_id, 2);
Faiz Abbasf32a8162019-03-18 13:54:33 +05301291 data->slave_data[slave_index].phy_addr =
Dan Murphycb386222016-05-02 15:45:56 -05001292 phy_id[1];
1293 }
Mugunthan V N4cc77892015-09-07 14:22:21 +05301294 slave_index++;
1295 }
1296
1297 if (!strncmp(name, "cpsw-phy-sel", 12)) {
Faiz Abbasf32a8162019-03-18 13:54:33 +05301298 data->gmii_sel = cpsw_get_addr_by_node(fdt, subnode);
Mugunthan V N66e740c2016-04-28 15:36:06 +05301299
Faiz Abbasf32a8162019-03-18 13:54:33 +05301300 if (data->gmii_sel == FDT_ADDR_T_NONE) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +09001301 pr_err("Not able to get gmii_sel reg address\n");
Mugunthan V N66e740c2016-04-28 15:36:06 +05301302 return -ENOENT;
1303 }
Mugunthan V Nab971532016-10-13 19:33:38 +05301304
1305 if (fdt_get_property(fdt, subnode, "rmii-clock-ext",
1306 NULL))
Faiz Abbasf32a8162019-03-18 13:54:33 +05301307 data->rmii_clock_external = true;
Mugunthan V Nab971532016-10-13 19:33:38 +05301308
Faiz Abbasf32a8162019-03-18 13:54:33 +05301309 data->phy_sel_compat = fdt_getprop(fdt, subnode,
1310 "compatible", NULL);
1311 if (!data->phy_sel_compat) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +09001312 pr_err("Not able to get gmii_sel compatible\n");
Mugunthan V Nab971532016-10-13 19:33:38 +05301313 return -ENOENT;
1314 }
Mugunthan V N4cc77892015-09-07 14:22:21 +05301315 }
1316 }
1317
Faiz Abbasf32a8162019-03-18 13:54:33 +05301318 data->slave_data[0].slave_reg_ofs = CPSW_SLAVE0_OFFSET;
1319 data->slave_data[0].sliver_reg_ofs = CPSW_SLIVER0_OFFSET;
Mugunthan V N4cc77892015-09-07 14:22:21 +05301320
Faiz Abbasf32a8162019-03-18 13:54:33 +05301321 if (data->slaves == 2) {
1322 data->slave_data[1].slave_reg_ofs = CPSW_SLAVE1_OFFSET;
1323 data->slave_data[1].sliver_reg_ofs = CPSW_SLIVER1_OFFSET;
Mugunthan V N4cc77892015-09-07 14:22:21 +05301324 }
1325
Faiz Abbasa58d2222019-03-18 13:54:34 +05301326 ret = ti_cm_get_macid_addr(dev, active_slave, data);
Mugunthan V Ne4310562016-04-28 15:36:07 +05301327 if (ret < 0) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +09001328 pr_err("cpsw read efuse mac failed\n");
Mugunthan V Ne4310562016-04-28 15:36:07 +05301329 return ret;
1330 }
Mugunthan V N4cc77892015-09-07 14:22:21 +05301331
Faiz Abbasf32a8162019-03-18 13:54:33 +05301332 pdata->phy_interface = data->slave_data[active_slave].phy_if;
Mugunthan V N4cc77892015-09-07 14:22:21 +05301333 if (pdata->phy_interface == -1) {
1334 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
1335 return -EINVAL;
1336 }
Mugunthan V Nab971532016-10-13 19:33:38 +05301337
Mugunthan V N4cc77892015-09-07 14:22:21 +05301338 return 0;
1339}
1340
Faiz Abbasc3b460a2019-03-18 13:54:35 +05301341static const struct udevice_id cpsw_eth_ids[] = {
1342 { .compatible = "ti,cpsw" },
1343 { .compatible = "ti,am335x-cpsw" },
1344 { }
1345};
1346#endif
1347
Sekhar Norie2597be2018-08-23 17:11:29 +05301348int cpsw_get_slave_phy_addr(struct udevice *dev, int slave)
1349{
1350 struct cpsw_priv *priv = dev_get_priv(dev);
Faiz Abbasf32a8162019-03-18 13:54:33 +05301351 struct cpsw_platform_data *data = priv->data;
Sekhar Norie2597be2018-08-23 17:11:29 +05301352
1353 return data->slave_data[slave].phy_addr;
1354}
Mugunthan V N4cc77892015-09-07 14:22:21 +05301355
Mugunthan V N4cc77892015-09-07 14:22:21 +05301356U_BOOT_DRIVER(eth_cpsw) = {
1357 .name = "eth_cpsw",
1358 .id = UCLASS_ETH,
Faiz Abbasc3b460a2019-03-18 13:54:35 +05301359#if CONFIG_IS_ENABLED(OF_CONTROL)
Mugunthan V N4cc77892015-09-07 14:22:21 +05301360 .of_match = cpsw_eth_ids,
1361 .ofdata_to_platdata = cpsw_eth_ofdata_to_platdata,
Faiz Abbasc3b460a2019-03-18 13:54:35 +05301362 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
1363#endif
Mugunthan V N4cc77892015-09-07 14:22:21 +05301364 .probe = cpsw_eth_probe,
1365 .ops = &cpsw_eth_ops,
1366 .priv_auto_alloc_size = sizeof(struct cpsw_priv),
Faiz Abbas8a616cc2019-03-18 13:54:36 +05301367 .flags = DM_FLAG_ALLOC_PRIV_DMA | DM_FLAG_PRE_RELOC,
Mugunthan V N4cc77892015-09-07 14:22:21 +05301368};
1369#endif /* CONFIG_DM_ETH */