blob: 93d53612b53cffa5db1bb1b38b9df3a49fe31127 [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>
Simon Glass1eb69ae2019-11-14 12:57:39 -070010#include <cpu_func.h>
Cyril Chemparathy2b629972012-07-24 12:22:16 +000011#include <net.h>
12#include <miiphy.h>
13#include <malloc.h>
14#include <net.h>
15#include <netdev.h>
16#include <cpsw.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090017#include <linux/errno.h>
Vignesh R2e205ef2016-08-02 10:14:27 +053018#include <asm/gpio.h>
Cyril Chemparathy2b629972012-07-24 12:22:16 +000019#include <asm/io.h>
20#include <phy.h>
Tom Rini98f92002013-03-14 11:15:25 +000021#include <asm/arch/cpu.h>
Mugunthan V N4cc77892015-09-07 14:22:21 +053022#include <dm.h>
23
Grygorii Strashko4f41cd92018-10-31 16:21:44 -050024#include "cpsw_mdio.h"
25
Cyril Chemparathy2b629972012-07-24 12:22:16 +000026#define BITMASK(bits) (BIT(bits) - 1)
Cyril Chemparathy2b629972012-07-24 12:22:16 +000027#define NUM_DESCS (PKTBUFSRX * 2)
28#define PKT_MIN 60
29#define PKT_MAX (1500 + 14 + 4 + 4)
30#define CLEAR_BIT 1
31#define GIGABITEN BIT(7)
32#define FULLDUPLEXEN BIT(0)
33#define MIIEN BIT(15)
Grygorii Strashko60e81d02019-09-19 11:16:37 +030034#define CTL_EXT_EN BIT(18)
Cyril Chemparathy2b629972012-07-24 12:22:16 +000035/* DMA Registers */
36#define CPDMA_TXCONTROL 0x004
37#define CPDMA_RXCONTROL 0x014
38#define CPDMA_SOFTRESET 0x01c
39#define CPDMA_RXFREE 0x0e0
40#define CPDMA_TXHDP_VER1 0x100
41#define CPDMA_TXHDP_VER2 0x200
42#define CPDMA_RXHDP_VER1 0x120
43#define CPDMA_RXHDP_VER2 0x220
44#define CPDMA_TXCP_VER1 0x140
45#define CPDMA_TXCP_VER2 0x240
46#define CPDMA_RXCP_VER1 0x160
47#define CPDMA_RXCP_VER2 0x260
48
Cyril Chemparathy2b629972012-07-24 12:22:16 +000049/* Descriptor mode bits */
50#define CPDMA_DESC_SOP BIT(31)
51#define CPDMA_DESC_EOP BIT(30)
52#define CPDMA_DESC_OWNER BIT(29)
53#define CPDMA_DESC_EOQ BIT(28)
54
55/*
56 * This timeout definition is a worst-case ultra defensive measure against
57 * unexpected controller lock ups. Ideally, we should never ever hit this
58 * scenario in practice.
59 */
Cyril Chemparathy2b629972012-07-24 12:22:16 +000060#define CPDMA_TIMEOUT 100 /* msecs */
61
Cyril Chemparathy2b629972012-07-24 12:22:16 +000062struct cpsw_regs {
63 u32 id_ver;
64 u32 control;
65 u32 soft_reset;
66 u32 stat_port_en;
67 u32 ptype;
68};
69
70struct cpsw_slave_regs {
71 u32 max_blks;
72 u32 blk_cnt;
73 u32 flow_thresh;
74 u32 port_vlan;
75 u32 tx_pri_map;
Matt Porterf6f86a62013-03-20 05:38:12 +000076#ifdef CONFIG_AM33XX
Cyril Chemparathy2b629972012-07-24 12:22:16 +000077 u32 gap_thresh;
Matt Porterf6f86a62013-03-20 05:38:12 +000078#elif defined(CONFIG_TI814X)
79 u32 ts_ctl;
80 u32 ts_seq_ltype;
81 u32 ts_vlan;
82#endif
Cyril Chemparathy2b629972012-07-24 12:22:16 +000083 u32 sa_lo;
84 u32 sa_hi;
85};
86
87struct cpsw_host_regs {
88 u32 max_blks;
89 u32 blk_cnt;
90 u32 flow_thresh;
91 u32 port_vlan;
92 u32 tx_pri_map;
93 u32 cpdma_tx_pri_map;
94 u32 cpdma_rx_chan_map;
95};
96
97struct cpsw_sliver_regs {
98 u32 id_ver;
99 u32 mac_control;
100 u32 mac_status;
101 u32 soft_reset;
102 u32 rx_maxlen;
103 u32 __reserved_0;
104 u32 rx_pause;
105 u32 tx_pause;
106 u32 __reserved_1;
107 u32 rx_pri_map;
108};
109
110#define ALE_ENTRY_BITS 68
111#define ALE_ENTRY_WORDS DIV_ROUND_UP(ALE_ENTRY_BITS, 32)
112
113/* ALE Registers */
114#define ALE_CONTROL 0x08
115#define ALE_UNKNOWNVLAN 0x18
116#define ALE_TABLE_CONTROL 0x20
117#define ALE_TABLE 0x34
118#define ALE_PORTCTL 0x40
119
120#define ALE_TABLE_WRITE BIT(31)
121
122#define ALE_TYPE_FREE 0
123#define ALE_TYPE_ADDR 1
124#define ALE_TYPE_VLAN 2
125#define ALE_TYPE_VLAN_ADDR 3
126
127#define ALE_UCAST_PERSISTANT 0
128#define ALE_UCAST_UNTOUCHED 1
129#define ALE_UCAST_OUI 2
130#define ALE_UCAST_TOUCHED 3
131
132#define ALE_MCAST_FWD 0
133#define ALE_MCAST_BLOCK_LEARN_FWD 1
134#define ALE_MCAST_FWD_LEARN 2
135#define ALE_MCAST_FWD_2 3
136
137enum cpsw_ale_port_state {
138 ALE_PORT_STATE_DISABLE = 0x00,
139 ALE_PORT_STATE_BLOCK = 0x01,
140 ALE_PORT_STATE_LEARN = 0x02,
141 ALE_PORT_STATE_FORWARD = 0x03,
142};
143
144/* ALE unicast entry flags - passed into cpsw_ale_add_ucast() */
145#define ALE_SECURE 1
146#define ALE_BLOCKED 2
147
148struct cpsw_slave {
149 struct cpsw_slave_regs *regs;
150 struct cpsw_sliver_regs *sliver;
151 int slave_num;
152 u32 mac_control;
153 struct cpsw_slave_data *data;
154};
155
156struct cpdma_desc {
157 /* hardware fields */
158 u32 hw_next;
159 u32 hw_buffer;
160 u32 hw_len;
161 u32 hw_mode;
162 /* software fields */
163 u32 sw_buffer;
164 u32 sw_len;
165};
166
167struct cpdma_chan {
168 struct cpdma_desc *head, *tail;
169 void *hdp, *cp, *rxfree;
170};
171
Mugunthan V Nab971532016-10-13 19:33:38 +0530172/* AM33xx SoC specific definitions for the CONTROL port */
173#define AM33XX_GMII_SEL_MODE_MII 0
174#define AM33XX_GMII_SEL_MODE_RMII 1
175#define AM33XX_GMII_SEL_MODE_RGMII 2
176
177#define AM33XX_GMII_SEL_RGMII1_IDMODE BIT(4)
178#define AM33XX_GMII_SEL_RGMII2_IDMODE BIT(5)
179#define AM33XX_GMII_SEL_RMII1_IO_CLK_EN BIT(6)
180#define AM33XX_GMII_SEL_RMII2_IO_CLK_EN BIT(7)
181
182#define GMII_SEL_MODE_MASK 0x3
183
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000184#define desc_write(desc, fld, val) __raw_writel((u32)(val), &(desc)->fld)
185#define desc_read(desc, fld) __raw_readl(&(desc)->fld)
186#define desc_read_ptr(desc, fld) ((void *)__raw_readl(&(desc)->fld))
187
188#define chan_write(chan, fld, val) __raw_writel((u32)(val), (chan)->fld)
189#define chan_read(chan, fld) __raw_readl((chan)->fld)
190#define chan_read_ptr(chan, fld) ((void *)__raw_readl((chan)->fld))
191
Mugunthan V N7a022752014-05-22 14:37:10 +0530192#define for_active_slave(slave, priv) \
Faiz Abbasf32a8162019-03-18 13:54:33 +0530193 slave = (priv)->slaves + ((priv)->data)->active_slave; if (slave)
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000194#define for_each_slave(slave, priv) \
195 for (slave = (priv)->slaves; slave != (priv)->slaves + \
Faiz Abbasf32a8162019-03-18 13:54:33 +0530196 ((priv)->data)->slaves; slave++)
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000197
198struct cpsw_priv {
Mugunthan V N4cc77892015-09-07 14:22:21 +0530199#ifdef CONFIG_DM_ETH
200 struct udevice *dev;
201#else
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000202 struct eth_device *dev;
Mugunthan V N4cc77892015-09-07 14:22:21 +0530203#endif
Faiz Abbasf32a8162019-03-18 13:54:33 +0530204 struct cpsw_platform_data *data;
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000205 int host_port;
206
207 struct cpsw_regs *regs;
208 void *dma_regs;
209 struct cpsw_host_regs *host_port_regs;
210 void *ale_regs;
211
212 struct cpdma_desc *descs;
213 struct cpdma_desc *desc_free;
214 struct cpdma_chan rx_chan, tx_chan;
215
216 struct cpsw_slave *slaves;
217 struct phy_device *phydev;
218 struct mii_dev *bus;
Mugunthan V N48ec5292013-02-19 21:34:44 +0000219
Mugunthan V N48ec5292013-02-19 21:34:44 +0000220 u32 phy_mask;
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000221};
222
223static inline int cpsw_ale_get_field(u32 *ale_entry, u32 start, u32 bits)
224{
225 int idx;
226
227 idx = start / 32;
228 start -= idx * 32;
229 idx = 2 - idx; /* flip */
230 return (ale_entry[idx] >> start) & BITMASK(bits);
231}
232
233static inline void cpsw_ale_set_field(u32 *ale_entry, u32 start, u32 bits,
234 u32 value)
235{
236 int idx;
237
238 value &= BITMASK(bits);
239 idx = start / 32;
240 start -= idx * 32;
241 idx = 2 - idx; /* flip */
242 ale_entry[idx] &= ~(BITMASK(bits) << start);
243 ale_entry[idx] |= (value << start);
244}
245
246#define DEFINE_ALE_FIELD(name, start, bits) \
247static inline int cpsw_ale_get_##name(u32 *ale_entry) \
248{ \
249 return cpsw_ale_get_field(ale_entry, start, bits); \
250} \
251static inline void cpsw_ale_set_##name(u32 *ale_entry, u32 value) \
252{ \
253 cpsw_ale_set_field(ale_entry, start, bits, value); \
254}
255
256DEFINE_ALE_FIELD(entry_type, 60, 2)
257DEFINE_ALE_FIELD(mcast_state, 62, 2)
258DEFINE_ALE_FIELD(port_mask, 66, 3)
259DEFINE_ALE_FIELD(ucast_type, 62, 2)
260DEFINE_ALE_FIELD(port_num, 66, 2)
261DEFINE_ALE_FIELD(blocked, 65, 1)
262DEFINE_ALE_FIELD(secure, 64, 1)
263DEFINE_ALE_FIELD(mcast, 40, 1)
264
265/* The MAC address field in the ALE entry cannot be macroized as above */
266static inline void cpsw_ale_get_addr(u32 *ale_entry, u8 *addr)
267{
268 int i;
269
270 for (i = 0; i < 6; i++)
271 addr[i] = cpsw_ale_get_field(ale_entry, 40 - 8*i, 8);
272}
273
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500274static inline void cpsw_ale_set_addr(u32 *ale_entry, const u8 *addr)
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000275{
276 int i;
277
278 for (i = 0; i < 6; i++)
279 cpsw_ale_set_field(ale_entry, 40 - 8*i, 8, addr[i]);
280}
281
282static int cpsw_ale_read(struct cpsw_priv *priv, int idx, u32 *ale_entry)
283{
284 int i;
285
286 __raw_writel(idx, priv->ale_regs + ALE_TABLE_CONTROL);
287
288 for (i = 0; i < ALE_ENTRY_WORDS; i++)
289 ale_entry[i] = __raw_readl(priv->ale_regs + ALE_TABLE + 4 * i);
290
291 return idx;
292}
293
294static int cpsw_ale_write(struct cpsw_priv *priv, int idx, u32 *ale_entry)
295{
296 int i;
297
298 for (i = 0; i < ALE_ENTRY_WORDS; i++)
299 __raw_writel(ale_entry[i], priv->ale_regs + ALE_TABLE + 4 * i);
300
301 __raw_writel(idx | ALE_TABLE_WRITE, priv->ale_regs + ALE_TABLE_CONTROL);
302
303 return idx;
304}
305
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500306static int cpsw_ale_match_addr(struct cpsw_priv *priv, const u8 *addr)
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000307{
308 u32 ale_entry[ALE_ENTRY_WORDS];
309 int type, idx;
310
Faiz Abbasf32a8162019-03-18 13:54:33 +0530311 for (idx = 0; idx < priv->data->ale_entries; idx++) {
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000312 u8 entry_addr[6];
313
314 cpsw_ale_read(priv, idx, ale_entry);
315 type = cpsw_ale_get_entry_type(ale_entry);
316 if (type != ALE_TYPE_ADDR && type != ALE_TYPE_VLAN_ADDR)
317 continue;
318 cpsw_ale_get_addr(ale_entry, entry_addr);
319 if (memcmp(entry_addr, addr, 6) == 0)
320 return idx;
321 }
322 return -ENOENT;
323}
324
325static int cpsw_ale_match_free(struct cpsw_priv *priv)
326{
327 u32 ale_entry[ALE_ENTRY_WORDS];
328 int type, idx;
329
Faiz Abbasf32a8162019-03-18 13:54:33 +0530330 for (idx = 0; idx < priv->data->ale_entries; idx++) {
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000331 cpsw_ale_read(priv, idx, ale_entry);
332 type = cpsw_ale_get_entry_type(ale_entry);
333 if (type == ALE_TYPE_FREE)
334 return idx;
335 }
336 return -ENOENT;
337}
338
339static int cpsw_ale_find_ageable(struct cpsw_priv *priv)
340{
341 u32 ale_entry[ALE_ENTRY_WORDS];
342 int type, idx;
343
Faiz Abbasf32a8162019-03-18 13:54:33 +0530344 for (idx = 0; idx < priv->data->ale_entries; idx++) {
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000345 cpsw_ale_read(priv, idx, ale_entry);
346 type = cpsw_ale_get_entry_type(ale_entry);
347 if (type != ALE_TYPE_ADDR && type != ALE_TYPE_VLAN_ADDR)
348 continue;
349 if (cpsw_ale_get_mcast(ale_entry))
350 continue;
351 type = cpsw_ale_get_ucast_type(ale_entry);
352 if (type != ALE_UCAST_PERSISTANT &&
353 type != ALE_UCAST_OUI)
354 return idx;
355 }
356 return -ENOENT;
357}
358
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500359static int cpsw_ale_add_ucast(struct cpsw_priv *priv, const u8 *addr,
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000360 int port, int flags)
361{
362 u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
363 int idx;
364
365 cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_ADDR);
366 cpsw_ale_set_addr(ale_entry, addr);
367 cpsw_ale_set_ucast_type(ale_entry, ALE_UCAST_PERSISTANT);
368 cpsw_ale_set_secure(ale_entry, (flags & ALE_SECURE) ? 1 : 0);
369 cpsw_ale_set_blocked(ale_entry, (flags & ALE_BLOCKED) ? 1 : 0);
370 cpsw_ale_set_port_num(ale_entry, port);
371
372 idx = cpsw_ale_match_addr(priv, addr);
373 if (idx < 0)
374 idx = cpsw_ale_match_free(priv);
375 if (idx < 0)
376 idx = cpsw_ale_find_ageable(priv);
377 if (idx < 0)
378 return -ENOMEM;
379
380 cpsw_ale_write(priv, idx, ale_entry);
381 return 0;
382}
383
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500384static int cpsw_ale_add_mcast(struct cpsw_priv *priv, const u8 *addr,
385 int port_mask)
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000386{
387 u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
388 int idx, mask;
389
390 idx = cpsw_ale_match_addr(priv, addr);
391 if (idx >= 0)
392 cpsw_ale_read(priv, idx, ale_entry);
393
394 cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_ADDR);
395 cpsw_ale_set_addr(ale_entry, addr);
396 cpsw_ale_set_mcast_state(ale_entry, ALE_MCAST_FWD_2);
397
398 mask = cpsw_ale_get_port_mask(ale_entry);
399 port_mask |= mask;
400 cpsw_ale_set_port_mask(ale_entry, port_mask);
401
402 if (idx < 0)
403 idx = cpsw_ale_match_free(priv);
404 if (idx < 0)
405 idx = cpsw_ale_find_ageable(priv);
406 if (idx < 0)
407 return -ENOMEM;
408
409 cpsw_ale_write(priv, idx, ale_entry);
410 return 0;
411}
412
413static inline void cpsw_ale_control(struct cpsw_priv *priv, int bit, int val)
414{
415 u32 tmp, mask = BIT(bit);
416
417 tmp = __raw_readl(priv->ale_regs + ALE_CONTROL);
418 tmp &= ~mask;
419 tmp |= val ? mask : 0;
420 __raw_writel(tmp, priv->ale_regs + ALE_CONTROL);
421}
422
423#define cpsw_ale_enable(priv, val) cpsw_ale_control(priv, 31, val)
424#define cpsw_ale_clear(priv, val) cpsw_ale_control(priv, 30, val)
425#define cpsw_ale_vlan_aware(priv, val) cpsw_ale_control(priv, 2, val)
426
427static inline void cpsw_ale_port_state(struct cpsw_priv *priv, int port,
428 int val)
429{
430 int offset = ALE_PORTCTL + 4 * port;
431 u32 tmp, mask = 0x3;
432
433 tmp = __raw_readl(priv->ale_regs + offset);
434 tmp &= ~mask;
435 tmp |= val & mask;
436 __raw_writel(tmp, priv->ale_regs + offset);
437}
438
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000439/* Set a self-clearing bit in a register, and wait for it to clear */
440static inline void setbit_and_wait_for_clear32(void *addr)
441{
442 __raw_writel(CLEAR_BIT, addr);
443 while (__raw_readl(addr) & CLEAR_BIT)
444 ;
445}
446
447#define mac_hi(mac) (((mac)[0] << 0) | ((mac)[1] << 8) | \
448 ((mac)[2] << 16) | ((mac)[3] << 24))
449#define mac_lo(mac) (((mac)[4] << 0) | ((mac)[5] << 8))
450
451static void cpsw_set_slave_mac(struct cpsw_slave *slave,
452 struct cpsw_priv *priv)
453{
Mugunthan V N4cc77892015-09-07 14:22:21 +0530454#ifdef CONFIG_DM_ETH
455 struct eth_pdata *pdata = dev_get_platdata(priv->dev);
456
457 writel(mac_hi(pdata->enetaddr), &slave->regs->sa_hi);
458 writel(mac_lo(pdata->enetaddr), &slave->regs->sa_lo);
459#else
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000460 __raw_writel(mac_hi(priv->dev->enetaddr), &slave->regs->sa_hi);
461 __raw_writel(mac_lo(priv->dev->enetaddr), &slave->regs->sa_lo);
Mugunthan V N4cc77892015-09-07 14:22:21 +0530462#endif
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000463}
464
Sekhar Nori96d1d842017-05-08 20:49:56 +0530465static int cpsw_slave_update_link(struct cpsw_slave *slave,
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000466 struct cpsw_priv *priv, int *link)
467{
Heiko Schocher93ff2552013-09-05 11:50:41 +0200468 struct phy_device *phy;
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000469 u32 mac_control = 0;
Sekhar Nori96d1d842017-05-08 20:49:56 +0530470 int ret = -ENODEV;
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000471
Heiko Schocher93ff2552013-09-05 11:50:41 +0200472 phy = priv->phydev;
Heiko Schocher93ff2552013-09-05 11:50:41 +0200473 if (!phy)
Sekhar Nori96d1d842017-05-08 20:49:56 +0530474 goto out;
Heiko Schocher93ff2552013-09-05 11:50:41 +0200475
Sekhar Nori96d1d842017-05-08 20:49:56 +0530476 ret = phy_startup(phy);
477 if (ret)
478 goto out;
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000479
Sekhar Nori96d1d842017-05-08 20:49:56 +0530480 if (link)
481 *link = phy->link;
482
483 if (phy->link) { /* link up */
Faiz Abbasf32a8162019-03-18 13:54:33 +0530484 mac_control = priv->data->mac_control;
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000485 if (phy->speed == 1000)
486 mac_control |= GIGABITEN;
487 if (phy->duplex == DUPLEX_FULL)
488 mac_control |= FULLDUPLEXEN;
489 if (phy->speed == 100)
490 mac_control |= MIIEN;
Grygorii Strashko60e81d02019-09-19 11:16:37 +0300491 if (phy->speed == 10 && phy_interface_is_rgmii(phy))
492 mac_control |= CTL_EXT_EN;
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000493 }
494
495 if (mac_control == slave->mac_control)
Sekhar Nori96d1d842017-05-08 20:49:56 +0530496 goto out;
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000497
498 if (mac_control) {
499 printf("link up on port %d, speed %d, %s duplex\n",
500 slave->slave_num, phy->speed,
501 (phy->duplex == DUPLEX_FULL) ? "full" : "half");
502 } else {
503 printf("link down on port %d\n", slave->slave_num);
504 }
505
506 __raw_writel(mac_control, &slave->sliver->mac_control);
507 slave->mac_control = mac_control;
Sekhar Nori96d1d842017-05-08 20:49:56 +0530508
509out:
510 return ret;
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000511}
512
513static int cpsw_update_link(struct cpsw_priv *priv)
514{
Sekhar Nori96d1d842017-05-08 20:49:56 +0530515 int ret = -ENODEV;
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000516 struct cpsw_slave *slave;
517
Mugunthan V N7a022752014-05-22 14:37:10 +0530518 for_active_slave(slave, priv)
Sekhar Nori96d1d842017-05-08 20:49:56 +0530519 ret = cpsw_slave_update_link(slave, priv, NULL);
Stefan Roese5a834c12014-08-25 11:26:19 +0200520
Sekhar Nori96d1d842017-05-08 20:49:56 +0530521 return ret;
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000522}
523
524static inline u32 cpsw_get_slave_port(struct cpsw_priv *priv, u32 slave_num)
525{
526 if (priv->host_port == 0)
527 return slave_num + 1;
528 else
529 return slave_num;
530}
531
532static void cpsw_slave_init(struct cpsw_slave *slave, struct cpsw_priv *priv)
533{
534 u32 slave_port;
535
536 setbit_and_wait_for_clear32(&slave->sliver->soft_reset);
537
538 /* setup priority mapping */
539 __raw_writel(0x76543210, &slave->sliver->rx_pri_map);
540 __raw_writel(0x33221100, &slave->regs->tx_pri_map);
541
542 /* setup max packet size, and mac address */
543 __raw_writel(PKT_MAX, &slave->sliver->rx_maxlen);
544 cpsw_set_slave_mac(slave, priv);
545
546 slave->mac_control = 0; /* no link yet */
547
548 /* enable forwarding */
549 slave_port = cpsw_get_slave_port(priv, slave->slave_num);
550 cpsw_ale_port_state(priv, slave_port, ALE_PORT_STATE_FORWARD);
551
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500552 cpsw_ale_add_mcast(priv, net_bcast_ethaddr, 1 << slave_port);
Mugunthan V N48ec5292013-02-19 21:34:44 +0000553
Mugunthan V N9c653aa2014-02-18 07:31:52 -0500554 priv->phy_mask |= 1 << slave->data->phy_addr;
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000555}
556
557static struct cpdma_desc *cpdma_desc_alloc(struct cpsw_priv *priv)
558{
559 struct cpdma_desc *desc = priv->desc_free;
560
561 if (desc)
562 priv->desc_free = desc_read_ptr(desc, hw_next);
563 return desc;
564}
565
566static void cpdma_desc_free(struct cpsw_priv *priv, struct cpdma_desc *desc)
567{
568 if (desc) {
569 desc_write(desc, hw_next, priv->desc_free);
570 priv->desc_free = desc;
571 }
572}
573
574static int cpdma_submit(struct cpsw_priv *priv, struct cpdma_chan *chan,
575 void *buffer, int len)
576{
577 struct cpdma_desc *desc, *prev;
578 u32 mode;
579
580 desc = cpdma_desc_alloc(priv);
581 if (!desc)
582 return -ENOMEM;
583
584 if (len < PKT_MIN)
585 len = PKT_MIN;
586
587 mode = CPDMA_DESC_OWNER | CPDMA_DESC_SOP | CPDMA_DESC_EOP;
588
589 desc_write(desc, hw_next, 0);
590 desc_write(desc, hw_buffer, buffer);
591 desc_write(desc, hw_len, len);
592 desc_write(desc, hw_mode, mode | len);
593 desc_write(desc, sw_buffer, buffer);
594 desc_write(desc, sw_len, len);
595
596 if (!chan->head) {
597 /* simple case - first packet enqueued */
598 chan->head = desc;
599 chan->tail = desc;
600 chan_write(chan, hdp, desc);
601 goto done;
602 }
603
604 /* not the first packet - enqueue at the tail */
605 prev = chan->tail;
606 desc_write(prev, hw_next, desc);
607 chan->tail = desc;
608
609 /* next check if EOQ has been triggered already */
610 if (desc_read(prev, hw_mode) & CPDMA_DESC_EOQ)
611 chan_write(chan, hdp, desc);
612
613done:
614 if (chan->rxfree)
615 chan_write(chan, rxfree, 1);
616 return 0;
617}
618
619static int cpdma_process(struct cpsw_priv *priv, struct cpdma_chan *chan,
620 void **buffer, int *len)
621{
622 struct cpdma_desc *desc = chan->head;
623 u32 status;
624
625 if (!desc)
626 return -ENOENT;
627
628 status = desc_read(desc, hw_mode);
629
630 if (len)
631 *len = status & 0x7ff;
632
633 if (buffer)
634 *buffer = desc_read_ptr(desc, sw_buffer);
635
636 if (status & CPDMA_DESC_OWNER) {
637 if (chan_read(chan, hdp) == 0) {
638 if (desc_read(desc, hw_mode) & CPDMA_DESC_OWNER)
639 chan_write(chan, hdp, desc);
640 }
641
642 return -EBUSY;
643 }
644
645 chan->head = desc_read_ptr(desc, hw_next);
646 chan_write(chan, cp, desc);
647
648 cpdma_desc_free(priv, desc);
649 return 0;
650}
651
Mugunthan V Nbcd5eed2015-09-07 14:22:20 +0530652static int _cpsw_init(struct cpsw_priv *priv, u8 *enetaddr)
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000653{
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000654 struct cpsw_slave *slave;
655 int i, ret;
656
657 /* soft reset the controller and initialize priv */
658 setbit_and_wait_for_clear32(&priv->regs->soft_reset);
659
660 /* initialize and reset the address lookup engine */
661 cpsw_ale_enable(priv, 1);
662 cpsw_ale_clear(priv, 1);
663 cpsw_ale_vlan_aware(priv, 0); /* vlan unaware mode */
664
665 /* setup host port priority mapping */
666 __raw_writel(0x76543210, &priv->host_port_regs->cpdma_tx_pri_map);
667 __raw_writel(0, &priv->host_port_regs->cpdma_rx_chan_map);
668
669 /* disable priority elevation and enable statistics on all ports */
670 __raw_writel(0, &priv->regs->ptype);
671
672 /* enable statistics collection only on the host port */
673 __raw_writel(BIT(priv->host_port), &priv->regs->stat_port_en);
Mugunthan V N454ac632013-07-08 16:04:38 +0530674 __raw_writel(0x7, &priv->regs->stat_port_en);
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000675
676 cpsw_ale_port_state(priv, priv->host_port, ALE_PORT_STATE_FORWARD);
677
Mugunthan V Nbcd5eed2015-09-07 14:22:20 +0530678 cpsw_ale_add_ucast(priv, enetaddr, priv->host_port, ALE_SECURE);
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500679 cpsw_ale_add_mcast(priv, net_bcast_ethaddr, 1 << priv->host_port);
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000680
Mugunthan V N7a022752014-05-22 14:37:10 +0530681 for_active_slave(slave, priv)
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000682 cpsw_slave_init(slave, priv);
683
Sekhar Nori96d1d842017-05-08 20:49:56 +0530684 ret = cpsw_update_link(priv);
685 if (ret)
686 goto out;
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000687
688 /* init descriptor pool */
689 for (i = 0; i < NUM_DESCS; i++) {
690 desc_write(&priv->descs[i], hw_next,
691 (i == (NUM_DESCS - 1)) ? 0 : &priv->descs[i+1]);
692 }
693 priv->desc_free = &priv->descs[0];
694
695 /* initialize channels */
Faiz Abbasf32a8162019-03-18 13:54:33 +0530696 if (priv->data->version == CPSW_CTRL_VERSION_2) {
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000697 memset(&priv->rx_chan, 0, sizeof(struct cpdma_chan));
698 priv->rx_chan.hdp = priv->dma_regs + CPDMA_RXHDP_VER2;
699 priv->rx_chan.cp = priv->dma_regs + CPDMA_RXCP_VER2;
700 priv->rx_chan.rxfree = priv->dma_regs + CPDMA_RXFREE;
701
702 memset(&priv->tx_chan, 0, sizeof(struct cpdma_chan));
703 priv->tx_chan.hdp = priv->dma_regs + CPDMA_TXHDP_VER2;
704 priv->tx_chan.cp = priv->dma_regs + CPDMA_TXCP_VER2;
705 } else {
706 memset(&priv->rx_chan, 0, sizeof(struct cpdma_chan));
707 priv->rx_chan.hdp = priv->dma_regs + CPDMA_RXHDP_VER1;
708 priv->rx_chan.cp = priv->dma_regs + CPDMA_RXCP_VER1;
709 priv->rx_chan.rxfree = priv->dma_regs + CPDMA_RXFREE;
710
711 memset(&priv->tx_chan, 0, sizeof(struct cpdma_chan));
712 priv->tx_chan.hdp = priv->dma_regs + CPDMA_TXHDP_VER1;
713 priv->tx_chan.cp = priv->dma_regs + CPDMA_TXCP_VER1;
714 }
715
716 /* clear dma state */
717 setbit_and_wait_for_clear32(priv->dma_regs + CPDMA_SOFTRESET);
718
Faiz Abbasf32a8162019-03-18 13:54:33 +0530719 if (priv->data->version == CPSW_CTRL_VERSION_2) {
720 for (i = 0; i < priv->data->channels; i++) {
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000721 __raw_writel(0, priv->dma_regs + CPDMA_RXHDP_VER2 + 4
722 * i);
723 __raw_writel(0, priv->dma_regs + CPDMA_RXFREE + 4
724 * i);
725 __raw_writel(0, priv->dma_regs + CPDMA_RXCP_VER2 + 4
726 * i);
727 __raw_writel(0, priv->dma_regs + CPDMA_TXHDP_VER2 + 4
728 * i);
729 __raw_writel(0, priv->dma_regs + CPDMA_TXCP_VER2 + 4
730 * i);
731 }
732 } else {
Faiz Abbasf32a8162019-03-18 13:54:33 +0530733 for (i = 0; i < priv->data->channels; i++) {
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000734 __raw_writel(0, priv->dma_regs + CPDMA_RXHDP_VER1 + 4
735 * i);
736 __raw_writel(0, priv->dma_regs + CPDMA_RXFREE + 4
737 * i);
738 __raw_writel(0, priv->dma_regs + CPDMA_RXCP_VER1 + 4
739 * i);
740 __raw_writel(0, priv->dma_regs + CPDMA_TXHDP_VER1 + 4
741 * i);
742 __raw_writel(0, priv->dma_regs + CPDMA_TXCP_VER1 + 4
743 * i);
744
745 }
746 }
747
748 __raw_writel(1, priv->dma_regs + CPDMA_TXCONTROL);
749 __raw_writel(1, priv->dma_regs + CPDMA_RXCONTROL);
750
751 /* submit rx descs */
752 for (i = 0; i < PKTBUFSRX; i++) {
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500753 ret = cpdma_submit(priv, &priv->rx_chan, net_rx_packets[i],
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000754 PKTSIZE);
755 if (ret < 0) {
756 printf("error %d submitting rx desc\n", ret);
757 break;
758 }
759 }
760
Sekhar Nori96d1d842017-05-08 20:49:56 +0530761out:
762 return ret;
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000763}
764
Alex Kiernan286bea22018-05-12 07:30:02 +0000765static int cpsw_reap_completed_packets(struct cpsw_priv *priv)
766{
767 int timeout = CPDMA_TIMEOUT;
768
769 /* reap completed packets */
770 while (timeout-- &&
771 (cpdma_process(priv, &priv->tx_chan, NULL, NULL) >= 0))
772 ;
773
774 return timeout;
775}
776
Mugunthan V Nbcd5eed2015-09-07 14:22:20 +0530777static void _cpsw_halt(struct cpsw_priv *priv)
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000778{
Alex Kiernan286bea22018-05-12 07:30:02 +0000779 cpsw_reap_completed_packets(priv);
780
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000781 writel(0, priv->dma_regs + CPDMA_TXCONTROL);
782 writel(0, priv->dma_regs + CPDMA_RXCONTROL);
783
784 /* soft reset the controller and initialize priv */
785 setbit_and_wait_for_clear32(&priv->regs->soft_reset);
786
787 /* clear dma state */
788 setbit_and_wait_for_clear32(priv->dma_regs + CPDMA_SOFTRESET);
789
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000790}
791
Mugunthan V Nbcd5eed2015-09-07 14:22:20 +0530792static int _cpsw_send(struct cpsw_priv *priv, void *packet, int length)
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000793{
Alex Kiernan286bea22018-05-12 07:30:02 +0000794 int timeout;
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000795
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000796 flush_dcache_range((unsigned long)packet,
Lokesh Vutla1f019622016-08-11 13:00:59 +0530797 (unsigned long)packet + ALIGN(length, PKTALIGN));
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000798
Alex Kiernan286bea22018-05-12 07:30:02 +0000799 timeout = cpsw_reap_completed_packets(priv);
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000800 if (timeout == -1) {
801 printf("cpdma_process timeout\n");
802 return -ETIMEDOUT;
803 }
804
805 return cpdma_submit(priv, &priv->tx_chan, packet, length);
806}
807
Mugunthan V Nbcd5eed2015-09-07 14:22:20 +0530808static int _cpsw_recv(struct cpsw_priv *priv, uchar **pkt)
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000809{
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000810 void *buffer;
811 int len;
Heinrich Schuchardt4b23d3c82018-03-18 11:24:38 +0100812 int ret;
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000813
Mugunthan V Nbcd5eed2015-09-07 14:22:20 +0530814 ret = cpdma_process(priv, &priv->rx_chan, &buffer, &len);
815 if (ret < 0)
816 return ret;
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000817
Mugunthan V Nbcd5eed2015-09-07 14:22:20 +0530818 invalidate_dcache_range((unsigned long)buffer,
819 (unsigned long)buffer + PKTSIZE_ALIGN);
820 *pkt = buffer;
821
822 return len;
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000823}
824
825static void cpsw_slave_setup(struct cpsw_slave *slave, int slave_num,
826 struct cpsw_priv *priv)
827{
828 void *regs = priv->regs;
Faiz Abbasf32a8162019-03-18 13:54:33 +0530829 struct cpsw_slave_data *data = priv->data->slave_data + slave_num;
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000830 slave->slave_num = slave_num;
831 slave->data = data;
832 slave->regs = regs + data->slave_reg_ofs;
833 slave->sliver = regs + data->sliver_reg_ofs;
834}
835
Mugunthan V Nbcd5eed2015-09-07 14:22:20 +0530836static int cpsw_phy_init(struct cpsw_priv *priv, struct cpsw_slave *slave)
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000837{
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000838 struct phy_device *phydev;
Ilya Ledvichef59bb72014-03-12 11:26:30 +0200839 u32 supported = PHY_GBIT_FEATURES;
Grygorii Strashko3c57b622019-09-19 11:16:39 +0300840 int ret;
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000841
Yegor Yefremovcdd07292012-11-26 04:03:16 +0000842 phydev = phy_connect(priv->bus,
Mugunthan V N9c653aa2014-02-18 07:31:52 -0500843 slave->data->phy_addr,
Mugunthan V Nbcd5eed2015-09-07 14:22:20 +0530844 priv->dev,
Yegor Yefremovcdd07292012-11-26 04:03:16 +0000845 slave->data->phy_if);
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000846
Heiko Schocher93ff2552013-09-05 11:50:41 +0200847 if (!phydev)
848 return -1;
849
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000850 phydev->supported &= supported;
Grygorii Strashko3c57b622019-09-19 11:16:39 +0300851 if (slave->data->max_speed) {
852 ret = phy_set_supported(phydev, slave->data->max_speed);
853 if (ret)
854 return ret;
855 dev_dbg(priv->dev, "Port %u speed forced to %uMbit\n",
856 slave->slave_num + 1, slave->data->max_speed);
857 }
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000858 phydev->advertising = phydev->supported;
859
Dan Murphycb386222016-05-02 15:45:56 -0500860#ifdef CONFIG_DM_ETH
Grygorii Strashko62f8e842019-09-19 11:16:42 +0300861 if (ofnode_valid(slave->data->phy_of_handle))
862 phydev->node = slave->data->phy_of_handle;
Dan Murphycb386222016-05-02 15:45:56 -0500863#endif
864
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000865 priv->phydev = phydev;
866 phy_config(phydev);
867
868 return 1;
869}
870
Sekhar Norie2597be2018-08-23 17:11:29 +0530871static void cpsw_phy_addr_update(struct cpsw_priv *priv)
872{
Faiz Abbasf32a8162019-03-18 13:54:33 +0530873 struct cpsw_platform_data *data = priv->data;
Grygorii Strashko4f41cd92018-10-31 16:21:44 -0500874 u16 alive = cpsw_mdio_get_alive(priv->bus);
Sekhar Norie2597be2018-08-23 17:11:29 +0530875 int active = data->active_slave;
876 int new_addr = ffs(alive) - 1;
877
878 /*
879 * If there is only one phy alive and its address does not match
880 * that of active slave, then phy address can safely be updated.
881 */
882 if (hweight16(alive) == 1 &&
883 data->slave_data[active].phy_addr != new_addr) {
884 printf("Updated phy address for CPSW#%d, old: %d, new: %d\n",
885 active, data->slave_data[active].phy_addr, new_addr);
886 data->slave_data[active].phy_addr = new_addr;
887 }
888}
889
Mugunthan V Nbcd5eed2015-09-07 14:22:20 +0530890int _cpsw_register(struct cpsw_priv *priv)
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000891{
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000892 struct cpsw_slave *slave;
Faiz Abbasf32a8162019-03-18 13:54:33 +0530893 struct cpsw_platform_data *data = priv->data;
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000894 void *regs = (void *)data->cpsw_base;
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000895
896 priv->slaves = malloc(sizeof(struct cpsw_slave) * data->slaves);
897 if (!priv->slaves) {
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000898 return -ENOMEM;
899 }
900
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000901 priv->host_port = data->host_port_num;
902 priv->regs = regs;
903 priv->host_port_regs = regs + data->host_port_reg_ofs;
904 priv->dma_regs = regs + data->cpdma_reg_ofs;
905 priv->ale_regs = regs + data->ale_reg_ofs;
Mugunthan V N2bf36ac2013-07-08 16:04:37 +0530906 priv->descs = (void *)regs + data->bd_ram_ofs;
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000907
908 int idx = 0;
909
910 for_each_slave(slave, priv) {
911 cpsw_slave_setup(slave, idx, priv);
912 idx = idx + 1;
913 }
914
Grygorii Strashko4f41cd92018-10-31 16:21:44 -0500915 priv->bus = cpsw_mdio_init(priv->dev->name, data->mdio_base, 0, 0);
916 if (!priv->bus)
917 return -EFAULT;
Sekhar Norie2597be2018-08-23 17:11:29 +0530918
919 cpsw_phy_addr_update(priv);
920
Mugunthan V Nbcd5eed2015-09-07 14:22:20 +0530921 for_active_slave(slave, priv)
922 cpsw_phy_init(priv, slave);
923
924 return 0;
925}
926
Mugunthan V N4cc77892015-09-07 14:22:21 +0530927#ifndef CONFIG_DM_ETH
Mugunthan V Nbcd5eed2015-09-07 14:22:20 +0530928static int cpsw_init(struct eth_device *dev, bd_t *bis)
929{
930 struct cpsw_priv *priv = dev->priv;
931
932 return _cpsw_init(priv, dev->enetaddr);
933}
934
935static void cpsw_halt(struct eth_device *dev)
936{
937 struct cpsw_priv *priv = dev->priv;
938
939 return _cpsw_halt(priv);
940}
941
942static int cpsw_send(struct eth_device *dev, void *packet, int length)
943{
944 struct cpsw_priv *priv = dev->priv;
945
946 return _cpsw_send(priv, packet, length);
947}
948
949static int cpsw_recv(struct eth_device *dev)
950{
951 struct cpsw_priv *priv = dev->priv;
952 uchar *pkt = NULL;
953 int len;
954
955 len = _cpsw_recv(priv, &pkt);
956
957 if (len > 0) {
958 net_process_received_packet(pkt, len);
959 cpdma_submit(priv, &priv->rx_chan, pkt, PKTSIZE);
960 }
961
962 return len;
963}
964
965int cpsw_register(struct cpsw_platform_data *data)
966{
967 struct cpsw_priv *priv;
968 struct eth_device *dev;
969 int ret;
970
971 dev = calloc(sizeof(*dev), 1);
972 if (!dev)
973 return -ENOMEM;
974
975 priv = calloc(sizeof(*priv), 1);
976 if (!priv) {
977 free(dev);
978 return -ENOMEM;
979 }
980
981 priv->dev = dev;
Faiz Abbasf32a8162019-03-18 13:54:33 +0530982 priv->data = data;
Mugunthan V Nbcd5eed2015-09-07 14:22:20 +0530983
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000984 strcpy(dev->name, "cpsw");
985 dev->iobase = 0;
986 dev->init = cpsw_init;
987 dev->halt = cpsw_halt;
988 dev->send = cpsw_send;
989 dev->recv = cpsw_recv;
990 dev->priv = priv;
991
992 eth_register(dev);
993
Mugunthan V Nbcd5eed2015-09-07 14:22:20 +0530994 ret = _cpsw_register(priv);
995 if (ret < 0) {
996 eth_unregister(dev);
997 free(dev);
998 free(priv);
999 return ret;
1000 }
Cyril Chemparathy2b629972012-07-24 12:22:16 +00001001
1002 return 1;
1003}
Mugunthan V N4cc77892015-09-07 14:22:21 +05301004#else
1005static int cpsw_eth_start(struct udevice *dev)
1006{
1007 struct eth_pdata *pdata = dev_get_platdata(dev);
1008 struct cpsw_priv *priv = dev_get_priv(dev);
1009
1010 return _cpsw_init(priv, pdata->enetaddr);
1011}
1012
1013static int cpsw_eth_send(struct udevice *dev, void *packet, int length)
1014{
1015 struct cpsw_priv *priv = dev_get_priv(dev);
1016
1017 return _cpsw_send(priv, packet, length);
1018}
1019
1020static int cpsw_eth_recv(struct udevice *dev, int flags, uchar **packetp)
1021{
1022 struct cpsw_priv *priv = dev_get_priv(dev);
1023
1024 return _cpsw_recv(priv, packetp);
1025}
1026
1027static int cpsw_eth_free_pkt(struct udevice *dev, uchar *packet,
1028 int length)
1029{
1030 struct cpsw_priv *priv = dev_get_priv(dev);
1031
1032 return cpdma_submit(priv, &priv->rx_chan, packet, PKTSIZE);
1033}
1034
1035static void cpsw_eth_stop(struct udevice *dev)
1036{
1037 struct cpsw_priv *priv = dev_get_priv(dev);
1038
1039 return _cpsw_halt(priv);
1040}
1041
Mugunthan V N4cc77892015-09-07 14:22:21 +05301042static const struct eth_ops cpsw_eth_ops = {
1043 .start = cpsw_eth_start,
1044 .send = cpsw_eth_send,
1045 .recv = cpsw_eth_recv,
1046 .free_pkt = cpsw_eth_free_pkt,
1047 .stop = cpsw_eth_stop,
1048};
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:
Grygorii Strashkoa3c867a2019-09-19 11:16:40 +03001067 case PHY_INTERFACE_MODE_RGMII_RXID:
Mugunthan V Nab971532016-10-13 19:33:38 +05301068 mode = AM33XX_GMII_SEL_MODE_RGMII;
1069 break;
1070 case PHY_INTERFACE_MODE_RGMII_ID:
Mugunthan V Nab971532016-10-13 19:33:38 +05301071 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)
Grygorii Strashko40401482019-09-19 11:16:38 +03001182static void cpsw_eth_of_parse_slave(struct cpsw_platform_data *data,
Grygorii Strashko62f8e842019-09-19 11:16:42 +03001183 int slave_index, ofnode subnode)
Grygorii Strashko40401482019-09-19 11:16:38 +03001184{
Grygorii Strashko62f8e842019-09-19 11:16:42 +03001185 struct ofnode_phandle_args out_args;
1186 struct cpsw_slave_data *slave_data;
Grygorii Strashko40401482019-09-19 11:16:38 +03001187 const char *phy_mode;
1188 u32 phy_id[2];
Grygorii Strashko62f8e842019-09-19 11:16:42 +03001189 int ret;
Grygorii Strashko40401482019-09-19 11:16:38 +03001190
1191 slave_data = &data->slave_data[slave_index];
1192
Grygorii Strashko62f8e842019-09-19 11:16:42 +03001193 phy_mode = ofnode_read_string(subnode, "phy-mode");
Grygorii Strashko40401482019-09-19 11:16:38 +03001194 if (phy_mode)
Grygorii Strashko62f8e842019-09-19 11:16:42 +03001195 slave_data->phy_if = phy_get_interface_by_name(phy_mode);
Grygorii Strashko40401482019-09-19 11:16:38 +03001196
Grygorii Strashko62f8e842019-09-19 11:16:42 +03001197 ret = ofnode_parse_phandle_with_args(subnode, "phy-handle",
1198 NULL, 0, 0, &out_args);
1199 if (!ret) {
1200 slave_data->phy_of_handle = out_args.node;
Grygorii Strashko40401482019-09-19 11:16:38 +03001201
Grygorii Strashko62f8e842019-09-19 11:16:42 +03001202 ret = ofnode_read_s32(slave_data->phy_of_handle, "reg",
1203 &slave_data->phy_addr);
1204 if (ret)
1205 printf("error: phy addr not found in dt\n");
Grygorii Strashko40401482019-09-19 11:16:38 +03001206 } else {
Grygorii Strashko62f8e842019-09-19 11:16:42 +03001207 ret = ofnode_read_u32_array(subnode, "phy_id", phy_id, 2);
1208 if (ret)
1209 printf("error: phy_id read failed\n");
Grygorii Strashko40401482019-09-19 11:16:38 +03001210 }
Grygorii Strashko3c57b622019-09-19 11:16:39 +03001211
Grygorii Strashko62f8e842019-09-19 11:16:42 +03001212 slave_data->max_speed = ofnode_read_s32_default(subnode,
1213 "max-speed", 0);
Grygorii Strashko40401482019-09-19 11:16:38 +03001214}
1215
Mugunthan V N4cc77892015-09-07 14:22:21 +05301216static int cpsw_eth_ofdata_to_platdata(struct udevice *dev)
1217{
1218 struct eth_pdata *pdata = dev_get_platdata(dev);
Faiz Abbasf32a8162019-03-18 13:54:33 +05301219 struct cpsw_platform_data *data;
Vignesh R2e205ef2016-08-02 10:14:27 +05301220 struct gpio_desc *mode_gpios;
Mugunthan V N4cc77892015-09-07 14:22:21 +05301221 int slave_index = 0;
Vignesh R2e205ef2016-08-02 10:14:27 +05301222 int num_mode_gpios;
Grygorii Strashko62f8e842019-09-19 11:16:42 +03001223 ofnode subnode;
Mugunthan V Ne4310562016-04-28 15:36:07 +05301224 int ret;
Mugunthan V N4cc77892015-09-07 14:22:21 +05301225
Faiz Abbasf32a8162019-03-18 13:54:33 +05301226 data = calloc(1, sizeof(struct cpsw_platform_data));
1227 pdata->priv_pdata = data;
Grygorii Strashko62f8e842019-09-19 11:16:42 +03001228 pdata->iobase = dev_read_addr(dev);
Faiz Abbasf32a8162019-03-18 13:54:33 +05301229 data->version = CPSW_CTRL_VERSION_2;
1230 data->bd_ram_ofs = CPSW_BD_OFFSET;
1231 data->ale_reg_ofs = CPSW_ALE_OFFSET;
1232 data->cpdma_reg_ofs = CPSW_CPDMA_OFFSET;
1233 data->mdio_div = CPSW_MDIO_DIV;
1234 data->host_port_reg_ofs = CPSW_HOST_PORT_OFFSET,
Mugunthan V N4cc77892015-09-07 14:22:21 +05301235
1236 pdata->phy_interface = -1;
1237
Faiz Abbasf32a8162019-03-18 13:54:33 +05301238 data->cpsw_base = pdata->iobase;
Grygorii Strashko62f8e842019-09-19 11:16:42 +03001239
1240 ret = dev_read_s32(dev, "cpdma_channels", &data->channels);
1241 if (ret) {
Mugunthan V N4cc77892015-09-07 14:22:21 +05301242 printf("error: cpdma_channels not found in dt\n");
Grygorii Strashko62f8e842019-09-19 11:16:42 +03001243 return ret;
Mugunthan V N4cc77892015-09-07 14:22:21 +05301244 }
1245
Grygorii Strashko62f8e842019-09-19 11:16:42 +03001246 ret = dev_read_s32(dev, "slaves", &data->slaves);
1247 if (ret) {
Mugunthan V N4cc77892015-09-07 14:22:21 +05301248 printf("error: slaves not found in dt\n");
Grygorii Strashko62f8e842019-09-19 11:16:42 +03001249 return ret;
Mugunthan V N4cc77892015-09-07 14:22:21 +05301250 }
Faiz Abbasf32a8162019-03-18 13:54:33 +05301251 data->slave_data = malloc(sizeof(struct cpsw_slave_data) *
1252 data->slaves);
Mugunthan V N4cc77892015-09-07 14:22:21 +05301253
Grygorii Strashko62f8e842019-09-19 11:16:42 +03001254 ret = dev_read_s32(dev, "ale_entries", &data->ale_entries);
1255 if (ret) {
Mugunthan V N4cc77892015-09-07 14:22:21 +05301256 printf("error: ale_entries not found in dt\n");
Grygorii Strashko62f8e842019-09-19 11:16:42 +03001257 return ret;
Mugunthan V N4cc77892015-09-07 14:22:21 +05301258 }
1259
Grygorii Strashko62f8e842019-09-19 11:16:42 +03001260 ret = dev_read_u32(dev, "bd_ram_size", &data->bd_ram_ofs);
1261 if (ret) {
Mugunthan V N4cc77892015-09-07 14:22:21 +05301262 printf("error: bd_ram_size not found in dt\n");
Grygorii Strashko62f8e842019-09-19 11:16:42 +03001263 return ret;
Mugunthan V N4cc77892015-09-07 14:22:21 +05301264 }
1265
Grygorii Strashko62f8e842019-09-19 11:16:42 +03001266 ret = dev_read_u32(dev, "mac_control", &data->mac_control);
1267 if (ret) {
Mugunthan V N4cc77892015-09-07 14:22:21 +05301268 printf("error: ale_entries not found in dt\n");
Grygorii Strashko62f8e842019-09-19 11:16:42 +03001269 return ret;
Mugunthan V N4cc77892015-09-07 14:22:21 +05301270 }
1271
Vignesh R2e205ef2016-08-02 10:14:27 +05301272 num_mode_gpios = gpio_get_list_count(dev, "mode-gpios");
1273 if (num_mode_gpios > 0) {
1274 mode_gpios = malloc(sizeof(struct gpio_desc) *
1275 num_mode_gpios);
1276 gpio_request_list_by_name(dev, "mode-gpios", mode_gpios,
1277 num_mode_gpios, GPIOD_IS_OUT);
1278 free(mode_gpios);
1279 }
1280
Grygorii Strashko62f8e842019-09-19 11:16:42 +03001281 data->active_slave = dev_read_u32_default(dev, "active_slave", 0);
Mugunthan V N4cc77892015-09-07 14:22:21 +05301282
Grygorii Strashko62f8e842019-09-19 11:16:42 +03001283 ofnode_for_each_subnode(subnode, dev_ofnode(dev)) {
Mugunthan V N4cc77892015-09-07 14:22:21 +05301284 const char *name;
1285
Grygorii Strashko62f8e842019-09-19 11:16:42 +03001286 name = ofnode_get_name(subnode);
Mugunthan V N4cc77892015-09-07 14:22:21 +05301287 if (!strncmp(name, "mdio", 4)) {
Grygorii Strashko62f8e842019-09-19 11:16:42 +03001288 data->mdio_base = ofnode_get_addr(subnode);
1289 if (data->mdio_base == FDT_ADDR_T_NONE) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +09001290 pr_err("Not able to get MDIO address space\n");
Mugunthan V N66e740c2016-04-28 15:36:06 +05301291 return -ENOENT;
1292 }
Mugunthan V N4cc77892015-09-07 14:22:21 +05301293 }
1294
1295 if (!strncmp(name, "slave", 5)) {
Faiz Abbasf32a8162019-03-18 13:54:33 +05301296 if (slave_index >= data->slaves)
Mugunthan V Nb2003c52016-04-28 15:36:04 +05301297 continue;
Dan Murphycb386222016-05-02 15:45:56 -05001298
Grygorii Strashko40401482019-09-19 11:16:38 +03001299 cpsw_eth_of_parse_slave(data, slave_index, subnode);
Mugunthan V N4cc77892015-09-07 14:22:21 +05301300 slave_index++;
1301 }
1302
1303 if (!strncmp(name, "cpsw-phy-sel", 12)) {
Grygorii Strashko62f8e842019-09-19 11:16:42 +03001304 data->gmii_sel = ofnode_get_addr(subnode);
Mugunthan V N66e740c2016-04-28 15:36:06 +05301305
Faiz Abbasf32a8162019-03-18 13:54:33 +05301306 if (data->gmii_sel == FDT_ADDR_T_NONE) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +09001307 pr_err("Not able to get gmii_sel reg address\n");
Mugunthan V N66e740c2016-04-28 15:36:06 +05301308 return -ENOENT;
1309 }
Mugunthan V Nab971532016-10-13 19:33:38 +05301310
Grygorii Strashko62f8e842019-09-19 11:16:42 +03001311 if (ofnode_read_bool(subnode, "rmii-clock-ext"))
Faiz Abbasf32a8162019-03-18 13:54:33 +05301312 data->rmii_clock_external = true;
Mugunthan V Nab971532016-10-13 19:33:38 +05301313
Grygorii Strashko62f8e842019-09-19 11:16:42 +03001314 data->phy_sel_compat = ofnode_read_string(subnode,
1315 "compatible");
Faiz Abbasf32a8162019-03-18 13:54:33 +05301316 if (!data->phy_sel_compat) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +09001317 pr_err("Not able to get gmii_sel compatible\n");
Mugunthan V Nab971532016-10-13 19:33:38 +05301318 return -ENOENT;
1319 }
Mugunthan V N4cc77892015-09-07 14:22:21 +05301320 }
1321 }
1322
Faiz Abbasf32a8162019-03-18 13:54:33 +05301323 data->slave_data[0].slave_reg_ofs = CPSW_SLAVE0_OFFSET;
1324 data->slave_data[0].sliver_reg_ofs = CPSW_SLIVER0_OFFSET;
Mugunthan V N4cc77892015-09-07 14:22:21 +05301325
Faiz Abbasf32a8162019-03-18 13:54:33 +05301326 if (data->slaves == 2) {
1327 data->slave_data[1].slave_reg_ofs = CPSW_SLAVE1_OFFSET;
1328 data->slave_data[1].sliver_reg_ofs = CPSW_SLIVER1_OFFSET;
Mugunthan V N4cc77892015-09-07 14:22:21 +05301329 }
1330
Grygorii Strashko62f8e842019-09-19 11:16:42 +03001331 ret = ti_cm_get_macid_addr(dev, data->active_slave, data);
Mugunthan V Ne4310562016-04-28 15:36:07 +05301332 if (ret < 0) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +09001333 pr_err("cpsw read efuse mac failed\n");
Mugunthan V Ne4310562016-04-28 15:36:07 +05301334 return ret;
1335 }
Mugunthan V N4cc77892015-09-07 14:22:21 +05301336
Grygorii Strashko62f8e842019-09-19 11:16:42 +03001337 pdata->phy_interface = data->slave_data[data->active_slave].phy_if;
Mugunthan V N4cc77892015-09-07 14:22:21 +05301338 if (pdata->phy_interface == -1) {
Grygorii Strashko40401482019-09-19 11:16:38 +03001339 debug("%s: Invalid PHY interface '%s'\n", __func__,
1340 phy_string_for_interface(pdata->phy_interface));
Mugunthan V N4cc77892015-09-07 14:22:21 +05301341 return -EINVAL;
1342 }
Mugunthan V Nab971532016-10-13 19:33:38 +05301343
Mugunthan V N4cc77892015-09-07 14:22:21 +05301344 return 0;
1345}
1346
Faiz Abbasc3b460a2019-03-18 13:54:35 +05301347static const struct udevice_id cpsw_eth_ids[] = {
1348 { .compatible = "ti,cpsw" },
1349 { .compatible = "ti,am335x-cpsw" },
1350 { }
1351};
1352#endif
1353
Sekhar Norie2597be2018-08-23 17:11:29 +05301354int cpsw_get_slave_phy_addr(struct udevice *dev, int slave)
1355{
1356 struct cpsw_priv *priv = dev_get_priv(dev);
Faiz Abbasf32a8162019-03-18 13:54:33 +05301357 struct cpsw_platform_data *data = priv->data;
Sekhar Norie2597be2018-08-23 17:11:29 +05301358
1359 return data->slave_data[slave].phy_addr;
1360}
Mugunthan V N4cc77892015-09-07 14:22:21 +05301361
Mugunthan V N4cc77892015-09-07 14:22:21 +05301362U_BOOT_DRIVER(eth_cpsw) = {
1363 .name = "eth_cpsw",
1364 .id = UCLASS_ETH,
Faiz Abbasc3b460a2019-03-18 13:54:35 +05301365#if CONFIG_IS_ENABLED(OF_CONTROL)
Mugunthan V N4cc77892015-09-07 14:22:21 +05301366 .of_match = cpsw_eth_ids,
1367 .ofdata_to_platdata = cpsw_eth_ofdata_to_platdata,
Faiz Abbasc3b460a2019-03-18 13:54:35 +05301368 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
1369#endif
Mugunthan V N4cc77892015-09-07 14:22:21 +05301370 .probe = cpsw_eth_probe,
1371 .ops = &cpsw_eth_ops,
1372 .priv_auto_alloc_size = sizeof(struct cpsw_priv),
Faiz Abbas8a616cc2019-03-18 13:54:36 +05301373 .flags = DM_FLAG_ALLOC_PRIV_DMA | DM_FLAG_PRE_RELOC,
Mugunthan V N4cc77892015-09-07 14:22:21 +05301374};
1375#endif /* CONFIG_DM_ETH */