blob: fe43d14a1d0b4d64b882bf6911a2c924e04fff4a [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
24DECLARE_GLOBAL_DATA_PTR;
Cyril Chemparathy2b629972012-07-24 12:22:16 +000025
26#define BITMASK(bits) (BIT(bits) - 1)
27#define PHY_REG_MASK 0x1f
28#define PHY_ID_MASK 0x1f
29#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)
36
Mugunthan V N4cc77892015-09-07 14:22:21 +053037/* reg offset */
38#define CPSW_HOST_PORT_OFFSET 0x108
39#define CPSW_SLAVE0_OFFSET 0x208
40#define CPSW_SLAVE1_OFFSET 0x308
41#define CPSW_SLAVE_SIZE 0x100
42#define CPSW_CPDMA_OFFSET 0x800
43#define CPSW_HW_STATS 0x900
44#define CPSW_STATERAM_OFFSET 0xa00
45#define CPSW_CPTS_OFFSET 0xc00
46#define CPSW_ALE_OFFSET 0xd00
47#define CPSW_SLIVER0_OFFSET 0xd80
48#define CPSW_SLIVER1_OFFSET 0xdc0
49#define CPSW_BD_OFFSET 0x2000
50#define CPSW_MDIO_DIV 0xff
51
52#define AM335X_GMII_SEL_OFFSET 0x630
53
Cyril Chemparathy2b629972012-07-24 12:22:16 +000054/* DMA Registers */
55#define CPDMA_TXCONTROL 0x004
56#define CPDMA_RXCONTROL 0x014
57#define CPDMA_SOFTRESET 0x01c
58#define CPDMA_RXFREE 0x0e0
59#define CPDMA_TXHDP_VER1 0x100
60#define CPDMA_TXHDP_VER2 0x200
61#define CPDMA_RXHDP_VER1 0x120
62#define CPDMA_RXHDP_VER2 0x220
63#define CPDMA_TXCP_VER1 0x140
64#define CPDMA_TXCP_VER2 0x240
65#define CPDMA_RXCP_VER1 0x160
66#define CPDMA_RXCP_VER2 0x260
67
Cyril Chemparathy2b629972012-07-24 12:22:16 +000068/* Descriptor mode bits */
69#define CPDMA_DESC_SOP BIT(31)
70#define CPDMA_DESC_EOP BIT(30)
71#define CPDMA_DESC_OWNER BIT(29)
72#define CPDMA_DESC_EOQ BIT(28)
73
74/*
75 * This timeout definition is a worst-case ultra defensive measure against
76 * unexpected controller lock ups. Ideally, we should never ever hit this
77 * scenario in practice.
78 */
79#define MDIO_TIMEOUT 100 /* msecs */
80#define CPDMA_TIMEOUT 100 /* msecs */
81
82struct cpsw_mdio_regs {
83 u32 version;
84 u32 control;
85#define CONTROL_IDLE BIT(31)
86#define CONTROL_ENABLE BIT(30)
87
88 u32 alive;
89 u32 link;
90 u32 linkintraw;
91 u32 linkintmasked;
92 u32 __reserved_0[2];
93 u32 userintraw;
94 u32 userintmasked;
95 u32 userintmaskset;
96 u32 userintmaskclr;
97 u32 __reserved_1[20];
98
99 struct {
100 u32 access;
101 u32 physel;
102#define USERACCESS_GO BIT(31)
103#define USERACCESS_WRITE BIT(30)
104#define USERACCESS_ACK BIT(29)
105#define USERACCESS_READ (0)
106#define USERACCESS_DATA (0xffff)
107 } user[0];
108};
109
110struct cpsw_regs {
111 u32 id_ver;
112 u32 control;
113 u32 soft_reset;
114 u32 stat_port_en;
115 u32 ptype;
116};
117
118struct cpsw_slave_regs {
119 u32 max_blks;
120 u32 blk_cnt;
121 u32 flow_thresh;
122 u32 port_vlan;
123 u32 tx_pri_map;
Matt Porterf6f86a62013-03-20 05:38:12 +0000124#ifdef CONFIG_AM33XX
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000125 u32 gap_thresh;
Matt Porterf6f86a62013-03-20 05:38:12 +0000126#elif defined(CONFIG_TI814X)
127 u32 ts_ctl;
128 u32 ts_seq_ltype;
129 u32 ts_vlan;
130#endif
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000131 u32 sa_lo;
132 u32 sa_hi;
133};
134
135struct cpsw_host_regs {
136 u32 max_blks;
137 u32 blk_cnt;
138 u32 flow_thresh;
139 u32 port_vlan;
140 u32 tx_pri_map;
141 u32 cpdma_tx_pri_map;
142 u32 cpdma_rx_chan_map;
143};
144
145struct cpsw_sliver_regs {
146 u32 id_ver;
147 u32 mac_control;
148 u32 mac_status;
149 u32 soft_reset;
150 u32 rx_maxlen;
151 u32 __reserved_0;
152 u32 rx_pause;
153 u32 tx_pause;
154 u32 __reserved_1;
155 u32 rx_pri_map;
156};
157
158#define ALE_ENTRY_BITS 68
159#define ALE_ENTRY_WORDS DIV_ROUND_UP(ALE_ENTRY_BITS, 32)
160
161/* ALE Registers */
162#define ALE_CONTROL 0x08
163#define ALE_UNKNOWNVLAN 0x18
164#define ALE_TABLE_CONTROL 0x20
165#define ALE_TABLE 0x34
166#define ALE_PORTCTL 0x40
167
168#define ALE_TABLE_WRITE BIT(31)
169
170#define ALE_TYPE_FREE 0
171#define ALE_TYPE_ADDR 1
172#define ALE_TYPE_VLAN 2
173#define ALE_TYPE_VLAN_ADDR 3
174
175#define ALE_UCAST_PERSISTANT 0
176#define ALE_UCAST_UNTOUCHED 1
177#define ALE_UCAST_OUI 2
178#define ALE_UCAST_TOUCHED 3
179
180#define ALE_MCAST_FWD 0
181#define ALE_MCAST_BLOCK_LEARN_FWD 1
182#define ALE_MCAST_FWD_LEARN 2
183#define ALE_MCAST_FWD_2 3
184
185enum cpsw_ale_port_state {
186 ALE_PORT_STATE_DISABLE = 0x00,
187 ALE_PORT_STATE_BLOCK = 0x01,
188 ALE_PORT_STATE_LEARN = 0x02,
189 ALE_PORT_STATE_FORWARD = 0x03,
190};
191
192/* ALE unicast entry flags - passed into cpsw_ale_add_ucast() */
193#define ALE_SECURE 1
194#define ALE_BLOCKED 2
195
196struct cpsw_slave {
197 struct cpsw_slave_regs *regs;
198 struct cpsw_sliver_regs *sliver;
199 int slave_num;
200 u32 mac_control;
201 struct cpsw_slave_data *data;
202};
203
204struct cpdma_desc {
205 /* hardware fields */
206 u32 hw_next;
207 u32 hw_buffer;
208 u32 hw_len;
209 u32 hw_mode;
210 /* software fields */
211 u32 sw_buffer;
212 u32 sw_len;
213};
214
215struct cpdma_chan {
216 struct cpdma_desc *head, *tail;
217 void *hdp, *cp, *rxfree;
218};
219
Mugunthan V Nab971532016-10-13 19:33:38 +0530220/* AM33xx SoC specific definitions for the CONTROL port */
221#define AM33XX_GMII_SEL_MODE_MII 0
222#define AM33XX_GMII_SEL_MODE_RMII 1
223#define AM33XX_GMII_SEL_MODE_RGMII 2
224
225#define AM33XX_GMII_SEL_RGMII1_IDMODE BIT(4)
226#define AM33XX_GMII_SEL_RGMII2_IDMODE BIT(5)
227#define AM33XX_GMII_SEL_RMII1_IO_CLK_EN BIT(6)
228#define AM33XX_GMII_SEL_RMII2_IO_CLK_EN BIT(7)
229
230#define GMII_SEL_MODE_MASK 0x3
231
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000232#define desc_write(desc, fld, val) __raw_writel((u32)(val), &(desc)->fld)
233#define desc_read(desc, fld) __raw_readl(&(desc)->fld)
234#define desc_read_ptr(desc, fld) ((void *)__raw_readl(&(desc)->fld))
235
236#define chan_write(chan, fld, val) __raw_writel((u32)(val), (chan)->fld)
237#define chan_read(chan, fld) __raw_readl((chan)->fld)
238#define chan_read_ptr(chan, fld) ((void *)__raw_readl((chan)->fld))
239
Mugunthan V N7a022752014-05-22 14:37:10 +0530240#define for_active_slave(slave, priv) \
241 slave = (priv)->slaves + (priv)->data.active_slave; if (slave)
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000242#define for_each_slave(slave, priv) \
243 for (slave = (priv)->slaves; slave != (priv)->slaves + \
244 (priv)->data.slaves; slave++)
245
246struct cpsw_priv {
Mugunthan V N4cc77892015-09-07 14:22:21 +0530247#ifdef CONFIG_DM_ETH
248 struct udevice *dev;
249#else
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000250 struct eth_device *dev;
Mugunthan V N4cc77892015-09-07 14:22:21 +0530251#endif
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000252 struct cpsw_platform_data data;
253 int host_port;
254
255 struct cpsw_regs *regs;
256 void *dma_regs;
257 struct cpsw_host_regs *host_port_regs;
258 void *ale_regs;
259
260 struct cpdma_desc *descs;
261 struct cpdma_desc *desc_free;
262 struct cpdma_chan rx_chan, tx_chan;
263
264 struct cpsw_slave *slaves;
265 struct phy_device *phydev;
266 struct mii_dev *bus;
Mugunthan V N48ec5292013-02-19 21:34:44 +0000267
Mugunthan V N48ec5292013-02-19 21:34:44 +0000268 u32 phy_mask;
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000269};
270
271static inline int cpsw_ale_get_field(u32 *ale_entry, u32 start, u32 bits)
272{
273 int idx;
274
275 idx = start / 32;
276 start -= idx * 32;
277 idx = 2 - idx; /* flip */
278 return (ale_entry[idx] >> start) & BITMASK(bits);
279}
280
281static inline void cpsw_ale_set_field(u32 *ale_entry, u32 start, u32 bits,
282 u32 value)
283{
284 int idx;
285
286 value &= BITMASK(bits);
287 idx = start / 32;
288 start -= idx * 32;
289 idx = 2 - idx; /* flip */
290 ale_entry[idx] &= ~(BITMASK(bits) << start);
291 ale_entry[idx] |= (value << start);
292}
293
294#define DEFINE_ALE_FIELD(name, start, bits) \
295static inline int cpsw_ale_get_##name(u32 *ale_entry) \
296{ \
297 return cpsw_ale_get_field(ale_entry, start, bits); \
298} \
299static inline void cpsw_ale_set_##name(u32 *ale_entry, u32 value) \
300{ \
301 cpsw_ale_set_field(ale_entry, start, bits, value); \
302}
303
304DEFINE_ALE_FIELD(entry_type, 60, 2)
305DEFINE_ALE_FIELD(mcast_state, 62, 2)
306DEFINE_ALE_FIELD(port_mask, 66, 3)
307DEFINE_ALE_FIELD(ucast_type, 62, 2)
308DEFINE_ALE_FIELD(port_num, 66, 2)
309DEFINE_ALE_FIELD(blocked, 65, 1)
310DEFINE_ALE_FIELD(secure, 64, 1)
311DEFINE_ALE_FIELD(mcast, 40, 1)
312
313/* The MAC address field in the ALE entry cannot be macroized as above */
314static inline void cpsw_ale_get_addr(u32 *ale_entry, u8 *addr)
315{
316 int i;
317
318 for (i = 0; i < 6; i++)
319 addr[i] = cpsw_ale_get_field(ale_entry, 40 - 8*i, 8);
320}
321
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500322static inline void cpsw_ale_set_addr(u32 *ale_entry, const u8 *addr)
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000323{
324 int i;
325
326 for (i = 0; i < 6; i++)
327 cpsw_ale_set_field(ale_entry, 40 - 8*i, 8, addr[i]);
328}
329
330static int cpsw_ale_read(struct cpsw_priv *priv, int idx, u32 *ale_entry)
331{
332 int i;
333
334 __raw_writel(idx, priv->ale_regs + ALE_TABLE_CONTROL);
335
336 for (i = 0; i < ALE_ENTRY_WORDS; i++)
337 ale_entry[i] = __raw_readl(priv->ale_regs + ALE_TABLE + 4 * i);
338
339 return idx;
340}
341
342static int cpsw_ale_write(struct cpsw_priv *priv, int idx, u32 *ale_entry)
343{
344 int i;
345
346 for (i = 0; i < ALE_ENTRY_WORDS; i++)
347 __raw_writel(ale_entry[i], priv->ale_regs + ALE_TABLE + 4 * i);
348
349 __raw_writel(idx | ALE_TABLE_WRITE, priv->ale_regs + ALE_TABLE_CONTROL);
350
351 return idx;
352}
353
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500354static int cpsw_ale_match_addr(struct cpsw_priv *priv, const u8 *addr)
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000355{
356 u32 ale_entry[ALE_ENTRY_WORDS];
357 int type, idx;
358
359 for (idx = 0; idx < priv->data.ale_entries; idx++) {
360 u8 entry_addr[6];
361
362 cpsw_ale_read(priv, idx, ale_entry);
363 type = cpsw_ale_get_entry_type(ale_entry);
364 if (type != ALE_TYPE_ADDR && type != ALE_TYPE_VLAN_ADDR)
365 continue;
366 cpsw_ale_get_addr(ale_entry, entry_addr);
367 if (memcmp(entry_addr, addr, 6) == 0)
368 return idx;
369 }
370 return -ENOENT;
371}
372
373static int cpsw_ale_match_free(struct cpsw_priv *priv)
374{
375 u32 ale_entry[ALE_ENTRY_WORDS];
376 int type, idx;
377
378 for (idx = 0; idx < priv->data.ale_entries; idx++) {
379 cpsw_ale_read(priv, idx, ale_entry);
380 type = cpsw_ale_get_entry_type(ale_entry);
381 if (type == ALE_TYPE_FREE)
382 return idx;
383 }
384 return -ENOENT;
385}
386
387static int cpsw_ale_find_ageable(struct cpsw_priv *priv)
388{
389 u32 ale_entry[ALE_ENTRY_WORDS];
390 int type, idx;
391
392 for (idx = 0; idx < priv->data.ale_entries; idx++) {
393 cpsw_ale_read(priv, idx, ale_entry);
394 type = cpsw_ale_get_entry_type(ale_entry);
395 if (type != ALE_TYPE_ADDR && type != ALE_TYPE_VLAN_ADDR)
396 continue;
397 if (cpsw_ale_get_mcast(ale_entry))
398 continue;
399 type = cpsw_ale_get_ucast_type(ale_entry);
400 if (type != ALE_UCAST_PERSISTANT &&
401 type != ALE_UCAST_OUI)
402 return idx;
403 }
404 return -ENOENT;
405}
406
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500407static int cpsw_ale_add_ucast(struct cpsw_priv *priv, const u8 *addr,
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000408 int port, int flags)
409{
410 u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
411 int idx;
412
413 cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_ADDR);
414 cpsw_ale_set_addr(ale_entry, addr);
415 cpsw_ale_set_ucast_type(ale_entry, ALE_UCAST_PERSISTANT);
416 cpsw_ale_set_secure(ale_entry, (flags & ALE_SECURE) ? 1 : 0);
417 cpsw_ale_set_blocked(ale_entry, (flags & ALE_BLOCKED) ? 1 : 0);
418 cpsw_ale_set_port_num(ale_entry, port);
419
420 idx = cpsw_ale_match_addr(priv, addr);
421 if (idx < 0)
422 idx = cpsw_ale_match_free(priv);
423 if (idx < 0)
424 idx = cpsw_ale_find_ageable(priv);
425 if (idx < 0)
426 return -ENOMEM;
427
428 cpsw_ale_write(priv, idx, ale_entry);
429 return 0;
430}
431
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500432static int cpsw_ale_add_mcast(struct cpsw_priv *priv, const u8 *addr,
433 int port_mask)
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000434{
435 u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
436 int idx, mask;
437
438 idx = cpsw_ale_match_addr(priv, addr);
439 if (idx >= 0)
440 cpsw_ale_read(priv, idx, ale_entry);
441
442 cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_ADDR);
443 cpsw_ale_set_addr(ale_entry, addr);
444 cpsw_ale_set_mcast_state(ale_entry, ALE_MCAST_FWD_2);
445
446 mask = cpsw_ale_get_port_mask(ale_entry);
447 port_mask |= mask;
448 cpsw_ale_set_port_mask(ale_entry, port_mask);
449
450 if (idx < 0)
451 idx = cpsw_ale_match_free(priv);
452 if (idx < 0)
453 idx = cpsw_ale_find_ageable(priv);
454 if (idx < 0)
455 return -ENOMEM;
456
457 cpsw_ale_write(priv, idx, ale_entry);
458 return 0;
459}
460
461static inline void cpsw_ale_control(struct cpsw_priv *priv, int bit, int val)
462{
463 u32 tmp, mask = BIT(bit);
464
465 tmp = __raw_readl(priv->ale_regs + ALE_CONTROL);
466 tmp &= ~mask;
467 tmp |= val ? mask : 0;
468 __raw_writel(tmp, priv->ale_regs + ALE_CONTROL);
469}
470
471#define cpsw_ale_enable(priv, val) cpsw_ale_control(priv, 31, val)
472#define cpsw_ale_clear(priv, val) cpsw_ale_control(priv, 30, val)
473#define cpsw_ale_vlan_aware(priv, val) cpsw_ale_control(priv, 2, val)
474
475static inline void cpsw_ale_port_state(struct cpsw_priv *priv, int port,
476 int val)
477{
478 int offset = ALE_PORTCTL + 4 * port;
479 u32 tmp, mask = 0x3;
480
481 tmp = __raw_readl(priv->ale_regs + offset);
482 tmp &= ~mask;
483 tmp |= val & mask;
484 __raw_writel(tmp, priv->ale_regs + offset);
485}
486
487static struct cpsw_mdio_regs *mdio_regs;
488
489/* wait until hardware is ready for another user access */
490static inline u32 wait_for_user_access(void)
491{
492 u32 reg = 0;
493 int timeout = MDIO_TIMEOUT;
494
495 while (timeout-- &&
496 ((reg = __raw_readl(&mdio_regs->user[0].access)) & USERACCESS_GO))
497 udelay(10);
498
499 if (timeout == -1) {
500 printf("wait_for_user_access Timeout\n");
501 return -ETIMEDOUT;
502 }
503 return reg;
504}
505
506/* wait until hardware state machine is idle */
507static inline void wait_for_idle(void)
508{
509 int timeout = MDIO_TIMEOUT;
510
511 while (timeout-- &&
512 ((__raw_readl(&mdio_regs->control) & CONTROL_IDLE) == 0))
513 udelay(10);
514
515 if (timeout == -1)
516 printf("wait_for_idle Timeout\n");
517}
518
519static int cpsw_mdio_read(struct mii_dev *bus, int phy_id,
520 int dev_addr, int phy_reg)
521{
Heiko Schocherf6d1f6e2013-07-23 15:32:36 +0200522 int data;
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000523 u32 reg;
524
525 if (phy_reg & ~PHY_REG_MASK || phy_id & ~PHY_ID_MASK)
526 return -EINVAL;
527
528 wait_for_user_access();
529 reg = (USERACCESS_GO | USERACCESS_READ | (phy_reg << 21) |
530 (phy_id << 16));
531 __raw_writel(reg, &mdio_regs->user[0].access);
532 reg = wait_for_user_access();
533
534 data = (reg & USERACCESS_ACK) ? (reg & USERACCESS_DATA) : -1;
535 return data;
536}
537
538static int cpsw_mdio_write(struct mii_dev *bus, int phy_id, int dev_addr,
539 int phy_reg, u16 data)
540{
541 u32 reg;
542
543 if (phy_reg & ~PHY_REG_MASK || phy_id & ~PHY_ID_MASK)
544 return -EINVAL;
545
546 wait_for_user_access();
547 reg = (USERACCESS_GO | USERACCESS_WRITE | (phy_reg << 21) |
548 (phy_id << 16) | (data & USERACCESS_DATA));
549 __raw_writel(reg, &mdio_regs->user[0].access);
550 wait_for_user_access();
551
552 return 0;
553}
554
Mugunthan V N4cc77892015-09-07 14:22:21 +0530555static void cpsw_mdio_init(const char *name, u32 mdio_base, u32 div)
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000556{
557 struct mii_dev *bus = mdio_alloc();
558
559 mdio_regs = (struct cpsw_mdio_regs *)mdio_base;
560
561 /* set enable and clock divider */
562 __raw_writel(div | CONTROL_ENABLE, &mdio_regs->control);
563
564 /*
565 * wait for scan logic to settle:
566 * the scan time consists of (a) a large fixed component, and (b) a
567 * small component that varies with the mii bus frequency. These
568 * were estimated using measurements at 1.1 and 2.2 MHz on tnetv107x
569 * silicon. Since the effect of (b) was found to be largely
570 * negligible, we keep things simple here.
571 */
572 udelay(1000);
573
574 bus->read = cpsw_mdio_read;
575 bus->write = cpsw_mdio_write;
Ben Whitten192bc692015-12-30 13:05:58 +0000576 strcpy(bus->name, name);
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000577
578 mdio_register(bus);
579}
580
581/* Set a self-clearing bit in a register, and wait for it to clear */
582static inline void setbit_and_wait_for_clear32(void *addr)
583{
584 __raw_writel(CLEAR_BIT, addr);
585 while (__raw_readl(addr) & CLEAR_BIT)
586 ;
587}
588
589#define mac_hi(mac) (((mac)[0] << 0) | ((mac)[1] << 8) | \
590 ((mac)[2] << 16) | ((mac)[3] << 24))
591#define mac_lo(mac) (((mac)[4] << 0) | ((mac)[5] << 8))
592
593static void cpsw_set_slave_mac(struct cpsw_slave *slave,
594 struct cpsw_priv *priv)
595{
Mugunthan V N4cc77892015-09-07 14:22:21 +0530596#ifdef CONFIG_DM_ETH
597 struct eth_pdata *pdata = dev_get_platdata(priv->dev);
598
599 writel(mac_hi(pdata->enetaddr), &slave->regs->sa_hi);
600 writel(mac_lo(pdata->enetaddr), &slave->regs->sa_lo);
601#else
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000602 __raw_writel(mac_hi(priv->dev->enetaddr), &slave->regs->sa_hi);
603 __raw_writel(mac_lo(priv->dev->enetaddr), &slave->regs->sa_lo);
Mugunthan V N4cc77892015-09-07 14:22:21 +0530604#endif
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000605}
606
Sekhar Nori96d1d842017-05-08 20:49:56 +0530607static int cpsw_slave_update_link(struct cpsw_slave *slave,
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000608 struct cpsw_priv *priv, int *link)
609{
Heiko Schocher93ff2552013-09-05 11:50:41 +0200610 struct phy_device *phy;
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000611 u32 mac_control = 0;
Sekhar Nori96d1d842017-05-08 20:49:56 +0530612 int ret = -ENODEV;
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000613
Heiko Schocher93ff2552013-09-05 11:50:41 +0200614 phy = priv->phydev;
Heiko Schocher93ff2552013-09-05 11:50:41 +0200615 if (!phy)
Sekhar Nori96d1d842017-05-08 20:49:56 +0530616 goto out;
Heiko Schocher93ff2552013-09-05 11:50:41 +0200617
Sekhar Nori96d1d842017-05-08 20:49:56 +0530618 ret = phy_startup(phy);
619 if (ret)
620 goto out;
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000621
Sekhar Nori96d1d842017-05-08 20:49:56 +0530622 if (link)
623 *link = phy->link;
624
625 if (phy->link) { /* link up */
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000626 mac_control = priv->data.mac_control;
627 if (phy->speed == 1000)
628 mac_control |= GIGABITEN;
629 if (phy->duplex == DUPLEX_FULL)
630 mac_control |= FULLDUPLEXEN;
631 if (phy->speed == 100)
632 mac_control |= MIIEN;
633 }
634
635 if (mac_control == slave->mac_control)
Sekhar Nori96d1d842017-05-08 20:49:56 +0530636 goto out;
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000637
638 if (mac_control) {
639 printf("link up on port %d, speed %d, %s duplex\n",
640 slave->slave_num, phy->speed,
641 (phy->duplex == DUPLEX_FULL) ? "full" : "half");
642 } else {
643 printf("link down on port %d\n", slave->slave_num);
644 }
645
646 __raw_writel(mac_control, &slave->sliver->mac_control);
647 slave->mac_control = mac_control;
Sekhar Nori96d1d842017-05-08 20:49:56 +0530648
649out:
650 return ret;
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000651}
652
653static int cpsw_update_link(struct cpsw_priv *priv)
654{
Sekhar Nori96d1d842017-05-08 20:49:56 +0530655 int ret = -ENODEV;
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000656 struct cpsw_slave *slave;
657
Mugunthan V N7a022752014-05-22 14:37:10 +0530658 for_active_slave(slave, priv)
Sekhar Nori96d1d842017-05-08 20:49:56 +0530659 ret = cpsw_slave_update_link(slave, priv, NULL);
Stefan Roese5a834c12014-08-25 11:26:19 +0200660
Sekhar Nori96d1d842017-05-08 20:49:56 +0530661 return ret;
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000662}
663
664static inline u32 cpsw_get_slave_port(struct cpsw_priv *priv, u32 slave_num)
665{
666 if (priv->host_port == 0)
667 return slave_num + 1;
668 else
669 return slave_num;
670}
671
672static void cpsw_slave_init(struct cpsw_slave *slave, struct cpsw_priv *priv)
673{
674 u32 slave_port;
675
676 setbit_and_wait_for_clear32(&slave->sliver->soft_reset);
677
678 /* setup priority mapping */
679 __raw_writel(0x76543210, &slave->sliver->rx_pri_map);
680 __raw_writel(0x33221100, &slave->regs->tx_pri_map);
681
682 /* setup max packet size, and mac address */
683 __raw_writel(PKT_MAX, &slave->sliver->rx_maxlen);
684 cpsw_set_slave_mac(slave, priv);
685
686 slave->mac_control = 0; /* no link yet */
687
688 /* enable forwarding */
689 slave_port = cpsw_get_slave_port(priv, slave->slave_num);
690 cpsw_ale_port_state(priv, slave_port, ALE_PORT_STATE_FORWARD);
691
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500692 cpsw_ale_add_mcast(priv, net_bcast_ethaddr, 1 << slave_port);
Mugunthan V N48ec5292013-02-19 21:34:44 +0000693
Mugunthan V N9c653aa2014-02-18 07:31:52 -0500694 priv->phy_mask |= 1 << slave->data->phy_addr;
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000695}
696
697static struct cpdma_desc *cpdma_desc_alloc(struct cpsw_priv *priv)
698{
699 struct cpdma_desc *desc = priv->desc_free;
700
701 if (desc)
702 priv->desc_free = desc_read_ptr(desc, hw_next);
703 return desc;
704}
705
706static void cpdma_desc_free(struct cpsw_priv *priv, struct cpdma_desc *desc)
707{
708 if (desc) {
709 desc_write(desc, hw_next, priv->desc_free);
710 priv->desc_free = desc;
711 }
712}
713
714static int cpdma_submit(struct cpsw_priv *priv, struct cpdma_chan *chan,
715 void *buffer, int len)
716{
717 struct cpdma_desc *desc, *prev;
718 u32 mode;
719
720 desc = cpdma_desc_alloc(priv);
721 if (!desc)
722 return -ENOMEM;
723
724 if (len < PKT_MIN)
725 len = PKT_MIN;
726
727 mode = CPDMA_DESC_OWNER | CPDMA_DESC_SOP | CPDMA_DESC_EOP;
728
729 desc_write(desc, hw_next, 0);
730 desc_write(desc, hw_buffer, buffer);
731 desc_write(desc, hw_len, len);
732 desc_write(desc, hw_mode, mode | len);
733 desc_write(desc, sw_buffer, buffer);
734 desc_write(desc, sw_len, len);
735
736 if (!chan->head) {
737 /* simple case - first packet enqueued */
738 chan->head = desc;
739 chan->tail = desc;
740 chan_write(chan, hdp, desc);
741 goto done;
742 }
743
744 /* not the first packet - enqueue at the tail */
745 prev = chan->tail;
746 desc_write(prev, hw_next, desc);
747 chan->tail = desc;
748
749 /* next check if EOQ has been triggered already */
750 if (desc_read(prev, hw_mode) & CPDMA_DESC_EOQ)
751 chan_write(chan, hdp, desc);
752
753done:
754 if (chan->rxfree)
755 chan_write(chan, rxfree, 1);
756 return 0;
757}
758
759static int cpdma_process(struct cpsw_priv *priv, struct cpdma_chan *chan,
760 void **buffer, int *len)
761{
762 struct cpdma_desc *desc = chan->head;
763 u32 status;
764
765 if (!desc)
766 return -ENOENT;
767
768 status = desc_read(desc, hw_mode);
769
770 if (len)
771 *len = status & 0x7ff;
772
773 if (buffer)
774 *buffer = desc_read_ptr(desc, sw_buffer);
775
776 if (status & CPDMA_DESC_OWNER) {
777 if (chan_read(chan, hdp) == 0) {
778 if (desc_read(desc, hw_mode) & CPDMA_DESC_OWNER)
779 chan_write(chan, hdp, desc);
780 }
781
782 return -EBUSY;
783 }
784
785 chan->head = desc_read_ptr(desc, hw_next);
786 chan_write(chan, cp, desc);
787
788 cpdma_desc_free(priv, desc);
789 return 0;
790}
791
Mugunthan V Nbcd5eed2015-09-07 14:22:20 +0530792static int _cpsw_init(struct cpsw_priv *priv, u8 *enetaddr)
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000793{
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000794 struct cpsw_slave *slave;
795 int i, ret;
796
797 /* soft reset the controller and initialize priv */
798 setbit_and_wait_for_clear32(&priv->regs->soft_reset);
799
800 /* initialize and reset the address lookup engine */
801 cpsw_ale_enable(priv, 1);
802 cpsw_ale_clear(priv, 1);
803 cpsw_ale_vlan_aware(priv, 0); /* vlan unaware mode */
804
805 /* setup host port priority mapping */
806 __raw_writel(0x76543210, &priv->host_port_regs->cpdma_tx_pri_map);
807 __raw_writel(0, &priv->host_port_regs->cpdma_rx_chan_map);
808
809 /* disable priority elevation and enable statistics on all ports */
810 __raw_writel(0, &priv->regs->ptype);
811
812 /* enable statistics collection only on the host port */
813 __raw_writel(BIT(priv->host_port), &priv->regs->stat_port_en);
Mugunthan V N454ac632013-07-08 16:04:38 +0530814 __raw_writel(0x7, &priv->regs->stat_port_en);
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000815
816 cpsw_ale_port_state(priv, priv->host_port, ALE_PORT_STATE_FORWARD);
817
Mugunthan V Nbcd5eed2015-09-07 14:22:20 +0530818 cpsw_ale_add_ucast(priv, enetaddr, priv->host_port, ALE_SECURE);
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500819 cpsw_ale_add_mcast(priv, net_bcast_ethaddr, 1 << priv->host_port);
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000820
Mugunthan V N7a022752014-05-22 14:37:10 +0530821 for_active_slave(slave, priv)
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000822 cpsw_slave_init(slave, priv);
823
Sekhar Nori96d1d842017-05-08 20:49:56 +0530824 ret = cpsw_update_link(priv);
825 if (ret)
826 goto out;
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000827
828 /* init descriptor pool */
829 for (i = 0; i < NUM_DESCS; i++) {
830 desc_write(&priv->descs[i], hw_next,
831 (i == (NUM_DESCS - 1)) ? 0 : &priv->descs[i+1]);
832 }
833 priv->desc_free = &priv->descs[0];
834
835 /* initialize channels */
836 if (priv->data.version == CPSW_CTRL_VERSION_2) {
837 memset(&priv->rx_chan, 0, sizeof(struct cpdma_chan));
838 priv->rx_chan.hdp = priv->dma_regs + CPDMA_RXHDP_VER2;
839 priv->rx_chan.cp = priv->dma_regs + CPDMA_RXCP_VER2;
840 priv->rx_chan.rxfree = priv->dma_regs + CPDMA_RXFREE;
841
842 memset(&priv->tx_chan, 0, sizeof(struct cpdma_chan));
843 priv->tx_chan.hdp = priv->dma_regs + CPDMA_TXHDP_VER2;
844 priv->tx_chan.cp = priv->dma_regs + CPDMA_TXCP_VER2;
845 } else {
846 memset(&priv->rx_chan, 0, sizeof(struct cpdma_chan));
847 priv->rx_chan.hdp = priv->dma_regs + CPDMA_RXHDP_VER1;
848 priv->rx_chan.cp = priv->dma_regs + CPDMA_RXCP_VER1;
849 priv->rx_chan.rxfree = priv->dma_regs + CPDMA_RXFREE;
850
851 memset(&priv->tx_chan, 0, sizeof(struct cpdma_chan));
852 priv->tx_chan.hdp = priv->dma_regs + CPDMA_TXHDP_VER1;
853 priv->tx_chan.cp = priv->dma_regs + CPDMA_TXCP_VER1;
854 }
855
856 /* clear dma state */
857 setbit_and_wait_for_clear32(priv->dma_regs + CPDMA_SOFTRESET);
858
859 if (priv->data.version == CPSW_CTRL_VERSION_2) {
860 for (i = 0; i < priv->data.channels; i++) {
861 __raw_writel(0, priv->dma_regs + CPDMA_RXHDP_VER2 + 4
862 * i);
863 __raw_writel(0, priv->dma_regs + CPDMA_RXFREE + 4
864 * i);
865 __raw_writel(0, priv->dma_regs + CPDMA_RXCP_VER2 + 4
866 * i);
867 __raw_writel(0, priv->dma_regs + CPDMA_TXHDP_VER2 + 4
868 * i);
869 __raw_writel(0, priv->dma_regs + CPDMA_TXCP_VER2 + 4
870 * i);
871 }
872 } else {
873 for (i = 0; i < priv->data.channels; i++) {
874 __raw_writel(0, priv->dma_regs + CPDMA_RXHDP_VER1 + 4
875 * i);
876 __raw_writel(0, priv->dma_regs + CPDMA_RXFREE + 4
877 * i);
878 __raw_writel(0, priv->dma_regs + CPDMA_RXCP_VER1 + 4
879 * i);
880 __raw_writel(0, priv->dma_regs + CPDMA_TXHDP_VER1 + 4
881 * i);
882 __raw_writel(0, priv->dma_regs + CPDMA_TXCP_VER1 + 4
883 * i);
884
885 }
886 }
887
888 __raw_writel(1, priv->dma_regs + CPDMA_TXCONTROL);
889 __raw_writel(1, priv->dma_regs + CPDMA_RXCONTROL);
890
891 /* submit rx descs */
892 for (i = 0; i < PKTBUFSRX; i++) {
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500893 ret = cpdma_submit(priv, &priv->rx_chan, net_rx_packets[i],
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000894 PKTSIZE);
895 if (ret < 0) {
896 printf("error %d submitting rx desc\n", ret);
897 break;
898 }
899 }
900
Sekhar Nori96d1d842017-05-08 20:49:56 +0530901out:
902 return ret;
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000903}
904
Alex Kiernan286bea22018-05-12 07:30:02 +0000905static int cpsw_reap_completed_packets(struct cpsw_priv *priv)
906{
907 int timeout = CPDMA_TIMEOUT;
908
909 /* reap completed packets */
910 while (timeout-- &&
911 (cpdma_process(priv, &priv->tx_chan, NULL, NULL) >= 0))
912 ;
913
914 return timeout;
915}
916
Mugunthan V Nbcd5eed2015-09-07 14:22:20 +0530917static void _cpsw_halt(struct cpsw_priv *priv)
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000918{
Alex Kiernan286bea22018-05-12 07:30:02 +0000919 cpsw_reap_completed_packets(priv);
920
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000921 writel(0, priv->dma_regs + CPDMA_TXCONTROL);
922 writel(0, priv->dma_regs + CPDMA_RXCONTROL);
923
924 /* soft reset the controller and initialize priv */
925 setbit_and_wait_for_clear32(&priv->regs->soft_reset);
926
927 /* clear dma state */
928 setbit_and_wait_for_clear32(priv->dma_regs + CPDMA_SOFTRESET);
929
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000930}
931
Mugunthan V Nbcd5eed2015-09-07 14:22:20 +0530932static int _cpsw_send(struct cpsw_priv *priv, void *packet, int length)
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000933{
Alex Kiernan286bea22018-05-12 07:30:02 +0000934 int timeout;
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000935
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000936 flush_dcache_range((unsigned long)packet,
Lokesh Vutla1f019622016-08-11 13:00:59 +0530937 (unsigned long)packet + ALIGN(length, PKTALIGN));
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000938
Alex Kiernan286bea22018-05-12 07:30:02 +0000939 timeout = cpsw_reap_completed_packets(priv);
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000940 if (timeout == -1) {
941 printf("cpdma_process timeout\n");
942 return -ETIMEDOUT;
943 }
944
945 return cpdma_submit(priv, &priv->tx_chan, packet, length);
946}
947
Mugunthan V Nbcd5eed2015-09-07 14:22:20 +0530948static int _cpsw_recv(struct cpsw_priv *priv, uchar **pkt)
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000949{
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000950 void *buffer;
951 int len;
Heinrich Schuchardt4b23d3c82018-03-18 11:24:38 +0100952 int ret;
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000953
Mugunthan V Nbcd5eed2015-09-07 14:22:20 +0530954 ret = cpdma_process(priv, &priv->rx_chan, &buffer, &len);
955 if (ret < 0)
956 return ret;
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000957
Mugunthan V Nbcd5eed2015-09-07 14:22:20 +0530958 invalidate_dcache_range((unsigned long)buffer,
959 (unsigned long)buffer + PKTSIZE_ALIGN);
960 *pkt = buffer;
961
962 return len;
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000963}
964
965static void cpsw_slave_setup(struct cpsw_slave *slave, int slave_num,
966 struct cpsw_priv *priv)
967{
968 void *regs = priv->regs;
969 struct cpsw_slave_data *data = priv->data.slave_data + slave_num;
970 slave->slave_num = slave_num;
971 slave->data = data;
972 slave->regs = regs + data->slave_reg_ofs;
973 slave->sliver = regs + data->sliver_reg_ofs;
974}
975
Mugunthan V Nbcd5eed2015-09-07 14:22:20 +0530976static int cpsw_phy_init(struct cpsw_priv *priv, struct cpsw_slave *slave)
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000977{
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000978 struct phy_device *phydev;
Ilya Ledvichef59bb72014-03-12 11:26:30 +0200979 u32 supported = PHY_GBIT_FEATURES;
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000980
Yegor Yefremovcdd07292012-11-26 04:03:16 +0000981 phydev = phy_connect(priv->bus,
Mugunthan V N9c653aa2014-02-18 07:31:52 -0500982 slave->data->phy_addr,
Mugunthan V Nbcd5eed2015-09-07 14:22:20 +0530983 priv->dev,
Yegor Yefremovcdd07292012-11-26 04:03:16 +0000984 slave->data->phy_if);
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000985
Heiko Schocher93ff2552013-09-05 11:50:41 +0200986 if (!phydev)
987 return -1;
988
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000989 phydev->supported &= supported;
990 phydev->advertising = phydev->supported;
991
Dan Murphycb386222016-05-02 15:45:56 -0500992#ifdef CONFIG_DM_ETH
993 if (slave->data->phy_of_handle)
Grygorii Strashkod4bb9812018-07-05 12:02:51 -0500994 phydev->node = offset_to_ofnode(slave->data->phy_of_handle);
Dan Murphycb386222016-05-02 15:45:56 -0500995#endif
996
Cyril Chemparathy2b629972012-07-24 12:22:16 +0000997 priv->phydev = phydev;
998 phy_config(phydev);
999
1000 return 1;
1001}
1002
Sekhar Norie2597be2018-08-23 17:11:29 +05301003static void cpsw_phy_addr_update(struct cpsw_priv *priv)
1004{
1005 struct cpsw_platform_data *data = &priv->data;
1006 u16 alive = mdio_regs->alive & GENMASK(15, 0);
1007 int active = data->active_slave;
1008 int new_addr = ffs(alive) - 1;
1009
1010 /*
1011 * If there is only one phy alive and its address does not match
1012 * that of active slave, then phy address can safely be updated.
1013 */
1014 if (hweight16(alive) == 1 &&
1015 data->slave_data[active].phy_addr != new_addr) {
1016 printf("Updated phy address for CPSW#%d, old: %d, new: %d\n",
1017 active, data->slave_data[active].phy_addr, new_addr);
1018 data->slave_data[active].phy_addr = new_addr;
1019 }
1020}
1021
Mugunthan V Nbcd5eed2015-09-07 14:22:20 +05301022int _cpsw_register(struct cpsw_priv *priv)
Cyril Chemparathy2b629972012-07-24 12:22:16 +00001023{
Cyril Chemparathy2b629972012-07-24 12:22:16 +00001024 struct cpsw_slave *slave;
Mugunthan V Nbcd5eed2015-09-07 14:22:20 +05301025 struct cpsw_platform_data *data = &priv->data;
Cyril Chemparathy2b629972012-07-24 12:22:16 +00001026 void *regs = (void *)data->cpsw_base;
Cyril Chemparathy2b629972012-07-24 12:22:16 +00001027
1028 priv->slaves = malloc(sizeof(struct cpsw_slave) * data->slaves);
1029 if (!priv->slaves) {
Cyril Chemparathy2b629972012-07-24 12:22:16 +00001030 return -ENOMEM;
1031 }
1032
Cyril Chemparathy2b629972012-07-24 12:22:16 +00001033 priv->host_port = data->host_port_num;
1034 priv->regs = regs;
1035 priv->host_port_regs = regs + data->host_port_reg_ofs;
1036 priv->dma_regs = regs + data->cpdma_reg_ofs;
1037 priv->ale_regs = regs + data->ale_reg_ofs;
Mugunthan V N2bf36ac2013-07-08 16:04:37 +05301038 priv->descs = (void *)regs + data->bd_ram_ofs;
Cyril Chemparathy2b629972012-07-24 12:22:16 +00001039
1040 int idx = 0;
1041
1042 for_each_slave(slave, priv) {
1043 cpsw_slave_setup(slave, idx, priv);
1044 idx = idx + 1;
1045 }
1046
Mugunthan V Nbcd5eed2015-09-07 14:22:20 +05301047 cpsw_mdio_init(priv->dev->name, data->mdio_base, data->mdio_div);
Sekhar Norie2597be2018-08-23 17:11:29 +05301048
1049 cpsw_phy_addr_update(priv);
1050
Mugunthan V Nbcd5eed2015-09-07 14:22:20 +05301051 priv->bus = miiphy_get_dev_by_name(priv->dev->name);
1052 for_active_slave(slave, priv)
1053 cpsw_phy_init(priv, slave);
1054
1055 return 0;
1056}
1057
Mugunthan V N4cc77892015-09-07 14:22:21 +05301058#ifndef CONFIG_DM_ETH
Mugunthan V Nbcd5eed2015-09-07 14:22:20 +05301059static int cpsw_init(struct eth_device *dev, bd_t *bis)
1060{
1061 struct cpsw_priv *priv = dev->priv;
1062
1063 return _cpsw_init(priv, dev->enetaddr);
1064}
1065
1066static void cpsw_halt(struct eth_device *dev)
1067{
1068 struct cpsw_priv *priv = dev->priv;
1069
1070 return _cpsw_halt(priv);
1071}
1072
1073static int cpsw_send(struct eth_device *dev, void *packet, int length)
1074{
1075 struct cpsw_priv *priv = dev->priv;
1076
1077 return _cpsw_send(priv, packet, length);
1078}
1079
1080static int cpsw_recv(struct eth_device *dev)
1081{
1082 struct cpsw_priv *priv = dev->priv;
1083 uchar *pkt = NULL;
1084 int len;
1085
1086 len = _cpsw_recv(priv, &pkt);
1087
1088 if (len > 0) {
1089 net_process_received_packet(pkt, len);
1090 cpdma_submit(priv, &priv->rx_chan, pkt, PKTSIZE);
1091 }
1092
1093 return len;
1094}
1095
1096int cpsw_register(struct cpsw_platform_data *data)
1097{
1098 struct cpsw_priv *priv;
1099 struct eth_device *dev;
1100 int ret;
1101
1102 dev = calloc(sizeof(*dev), 1);
1103 if (!dev)
1104 return -ENOMEM;
1105
1106 priv = calloc(sizeof(*priv), 1);
1107 if (!priv) {
1108 free(dev);
1109 return -ENOMEM;
1110 }
1111
1112 priv->dev = dev;
1113 priv->data = *data;
1114
Cyril Chemparathy2b629972012-07-24 12:22:16 +00001115 strcpy(dev->name, "cpsw");
1116 dev->iobase = 0;
1117 dev->init = cpsw_init;
1118 dev->halt = cpsw_halt;
1119 dev->send = cpsw_send;
1120 dev->recv = cpsw_recv;
1121 dev->priv = priv;
1122
1123 eth_register(dev);
1124
Mugunthan V Nbcd5eed2015-09-07 14:22:20 +05301125 ret = _cpsw_register(priv);
1126 if (ret < 0) {
1127 eth_unregister(dev);
1128 free(dev);
1129 free(priv);
1130 return ret;
1131 }
Cyril Chemparathy2b629972012-07-24 12:22:16 +00001132
1133 return 1;
1134}
Mugunthan V N4cc77892015-09-07 14:22:21 +05301135#else
1136static int cpsw_eth_start(struct udevice *dev)
1137{
1138 struct eth_pdata *pdata = dev_get_platdata(dev);
1139 struct cpsw_priv *priv = dev_get_priv(dev);
1140
1141 return _cpsw_init(priv, pdata->enetaddr);
1142}
1143
1144static int cpsw_eth_send(struct udevice *dev, void *packet, int length)
1145{
1146 struct cpsw_priv *priv = dev_get_priv(dev);
1147
1148 return _cpsw_send(priv, packet, length);
1149}
1150
1151static int cpsw_eth_recv(struct udevice *dev, int flags, uchar **packetp)
1152{
1153 struct cpsw_priv *priv = dev_get_priv(dev);
1154
1155 return _cpsw_recv(priv, packetp);
1156}
1157
1158static int cpsw_eth_free_pkt(struct udevice *dev, uchar *packet,
1159 int length)
1160{
1161 struct cpsw_priv *priv = dev_get_priv(dev);
1162
1163 return cpdma_submit(priv, &priv->rx_chan, packet, PKTSIZE);
1164}
1165
1166static void cpsw_eth_stop(struct udevice *dev)
1167{
1168 struct cpsw_priv *priv = dev_get_priv(dev);
1169
1170 return _cpsw_halt(priv);
1171}
1172
1173
1174static int cpsw_eth_probe(struct udevice *dev)
1175{
1176 struct cpsw_priv *priv = dev_get_priv(dev);
1177
1178 priv->dev = dev;
1179
1180 return _cpsw_register(priv);
1181}
1182
1183static const struct eth_ops cpsw_eth_ops = {
1184 .start = cpsw_eth_start,
1185 .send = cpsw_eth_send,
1186 .recv = cpsw_eth_recv,
1187 .free_pkt = cpsw_eth_free_pkt,
1188 .stop = cpsw_eth_stop,
1189};
1190
Mugunthan V N66e740c2016-04-28 15:36:06 +05301191static inline fdt_addr_t cpsw_get_addr_by_node(const void *fdt, int node)
1192{
Stephen Warren6e06acb2016-08-05 09:47:51 -06001193 return fdtdec_get_addr_size_auto_noparent(fdt, node, "reg", 0, NULL,
1194 false);
Mugunthan V N66e740c2016-04-28 15:36:06 +05301195}
1196
Mugunthan V Nab971532016-10-13 19:33:38 +05301197static void cpsw_gmii_sel_am3352(struct cpsw_priv *priv,
1198 phy_interface_t phy_mode)
1199{
1200 u32 reg;
1201 u32 mask;
1202 u32 mode = 0;
1203 bool rgmii_id = false;
1204 int slave = priv->data.active_slave;
1205
1206 reg = readl(priv->data.gmii_sel);
1207
1208 switch (phy_mode) {
1209 case PHY_INTERFACE_MODE_RMII:
1210 mode = AM33XX_GMII_SEL_MODE_RMII;
1211 break;
1212
1213 case PHY_INTERFACE_MODE_RGMII:
1214 mode = AM33XX_GMII_SEL_MODE_RGMII;
1215 break;
1216 case PHY_INTERFACE_MODE_RGMII_ID:
1217 case PHY_INTERFACE_MODE_RGMII_RXID:
1218 case PHY_INTERFACE_MODE_RGMII_TXID:
1219 mode = AM33XX_GMII_SEL_MODE_RGMII;
1220 rgmii_id = true;
1221 break;
1222
1223 case PHY_INTERFACE_MODE_MII:
1224 default:
1225 mode = AM33XX_GMII_SEL_MODE_MII;
1226 break;
1227 };
1228
1229 mask = GMII_SEL_MODE_MASK << (slave * 2) | BIT(slave + 6);
1230 mode <<= slave * 2;
1231
1232 if (priv->data.rmii_clock_external) {
1233 if (slave == 0)
1234 mode |= AM33XX_GMII_SEL_RMII1_IO_CLK_EN;
1235 else
1236 mode |= AM33XX_GMII_SEL_RMII2_IO_CLK_EN;
1237 }
1238
1239 if (rgmii_id) {
1240 if (slave == 0)
1241 mode |= AM33XX_GMII_SEL_RGMII1_IDMODE;
1242 else
1243 mode |= AM33XX_GMII_SEL_RGMII2_IDMODE;
1244 }
1245
1246 reg &= ~mask;
1247 reg |= mode;
1248
1249 writel(reg, priv->data.gmii_sel);
1250}
1251
1252static void cpsw_gmii_sel_dra7xx(struct cpsw_priv *priv,
1253 phy_interface_t phy_mode)
1254{
1255 u32 reg;
1256 u32 mask;
1257 u32 mode = 0;
1258 int slave = priv->data.active_slave;
1259
1260 reg = readl(priv->data.gmii_sel);
1261
1262 switch (phy_mode) {
1263 case PHY_INTERFACE_MODE_RMII:
1264 mode = AM33XX_GMII_SEL_MODE_RMII;
1265 break;
1266
1267 case PHY_INTERFACE_MODE_RGMII:
1268 case PHY_INTERFACE_MODE_RGMII_ID:
1269 case PHY_INTERFACE_MODE_RGMII_RXID:
1270 case PHY_INTERFACE_MODE_RGMII_TXID:
1271 mode = AM33XX_GMII_SEL_MODE_RGMII;
1272 break;
1273
1274 case PHY_INTERFACE_MODE_MII:
1275 default:
1276 mode = AM33XX_GMII_SEL_MODE_MII;
1277 break;
1278 };
1279
1280 switch (slave) {
1281 case 0:
1282 mask = GMII_SEL_MODE_MASK;
1283 break;
1284 case 1:
1285 mask = GMII_SEL_MODE_MASK << 4;
1286 mode <<= 4;
1287 break;
1288 default:
1289 dev_err(priv->dev, "invalid slave number...\n");
1290 return;
1291 }
1292
1293 if (priv->data.rmii_clock_external)
1294 dev_err(priv->dev, "RMII External clock is not supported\n");
1295
1296 reg &= ~mask;
1297 reg |= mode;
1298
1299 writel(reg, priv->data.gmii_sel);
1300}
1301
1302static void cpsw_phy_sel(struct cpsw_priv *priv, const char *compat,
1303 phy_interface_t phy_mode)
1304{
1305 if (!strcmp(compat, "ti,am3352-cpsw-phy-sel"))
1306 cpsw_gmii_sel_am3352(priv, phy_mode);
1307 if (!strcmp(compat, "ti,am43xx-cpsw-phy-sel"))
1308 cpsw_gmii_sel_am3352(priv, phy_mode);
1309 else if (!strcmp(compat, "ti,dra7xx-cpsw-phy-sel"))
1310 cpsw_gmii_sel_dra7xx(priv, phy_mode);
1311}
1312
Mugunthan V N4cc77892015-09-07 14:22:21 +05301313static int cpsw_eth_ofdata_to_platdata(struct udevice *dev)
1314{
1315 struct eth_pdata *pdata = dev_get_platdata(dev);
1316 struct cpsw_priv *priv = dev_get_priv(dev);
Vignesh R2e205ef2016-08-02 10:14:27 +05301317 struct gpio_desc *mode_gpios;
Mugunthan V N4cc77892015-09-07 14:22:21 +05301318 const char *phy_mode;
Mugunthan V Nab971532016-10-13 19:33:38 +05301319 const char *phy_sel_compat = NULL;
Mugunthan V N4cc77892015-09-07 14:22:21 +05301320 const void *fdt = gd->fdt_blob;
Simon Glasse160f7d2017-01-17 16:52:55 -07001321 int node = dev_of_offset(dev);
Mugunthan V N4cc77892015-09-07 14:22:21 +05301322 int subnode;
1323 int slave_index = 0;
Mugunthan V N4cc77892015-09-07 14:22:21 +05301324 int active_slave;
Vignesh R2e205ef2016-08-02 10:14:27 +05301325 int num_mode_gpios;
Mugunthan V Ne4310562016-04-28 15:36:07 +05301326 int ret;
Mugunthan V N4cc77892015-09-07 14:22:21 +05301327
Simon Glassa821c4a2017-05-17 17:18:05 -06001328 pdata->iobase = devfdt_get_addr(dev);
Mugunthan V N4cc77892015-09-07 14:22:21 +05301329 priv->data.version = CPSW_CTRL_VERSION_2;
1330 priv->data.bd_ram_ofs = CPSW_BD_OFFSET;
1331 priv->data.ale_reg_ofs = CPSW_ALE_OFFSET;
1332 priv->data.cpdma_reg_ofs = CPSW_CPDMA_OFFSET;
1333 priv->data.mdio_div = CPSW_MDIO_DIV;
1334 priv->data.host_port_reg_ofs = CPSW_HOST_PORT_OFFSET,
1335
1336 pdata->phy_interface = -1;
1337
1338 priv->data.cpsw_base = pdata->iobase;
1339 priv->data.channels = fdtdec_get_int(fdt, node, "cpdma_channels", -1);
1340 if (priv->data.channels <= 0) {
1341 printf("error: cpdma_channels not found in dt\n");
1342 return -ENOENT;
1343 }
1344
1345 priv->data.slaves = fdtdec_get_int(fdt, node, "slaves", -1);
1346 if (priv->data.slaves <= 0) {
1347 printf("error: slaves not found in dt\n");
1348 return -ENOENT;
1349 }
1350 priv->data.slave_data = malloc(sizeof(struct cpsw_slave_data) *
1351 priv->data.slaves);
1352
1353 priv->data.ale_entries = fdtdec_get_int(fdt, node, "ale_entries", -1);
1354 if (priv->data.ale_entries <= 0) {
1355 printf("error: ale_entries not found in dt\n");
1356 return -ENOENT;
1357 }
1358
1359 priv->data.bd_ram_ofs = fdtdec_get_int(fdt, node, "bd_ram_size", -1);
1360 if (priv->data.bd_ram_ofs <= 0) {
1361 printf("error: bd_ram_size not found in dt\n");
1362 return -ENOENT;
1363 }
1364
1365 priv->data.mac_control = fdtdec_get_int(fdt, node, "mac_control", -1);
1366 if (priv->data.mac_control <= 0) {
1367 printf("error: ale_entries not found in dt\n");
1368 return -ENOENT;
1369 }
1370
Vignesh R2e205ef2016-08-02 10:14:27 +05301371 num_mode_gpios = gpio_get_list_count(dev, "mode-gpios");
1372 if (num_mode_gpios > 0) {
1373 mode_gpios = malloc(sizeof(struct gpio_desc) *
1374 num_mode_gpios);
1375 gpio_request_list_by_name(dev, "mode-gpios", mode_gpios,
1376 num_mode_gpios, GPIOD_IS_OUT);
1377 free(mode_gpios);
1378 }
1379
Mugunthan V N4cc77892015-09-07 14:22:21 +05301380 active_slave = fdtdec_get_int(fdt, node, "active_slave", 0);
1381 priv->data.active_slave = active_slave;
1382
Simon Glassdf87e6b2016-10-02 17:59:29 -06001383 fdt_for_each_subnode(subnode, fdt, node) {
Mugunthan V N4cc77892015-09-07 14:22:21 +05301384 int len;
1385 const char *name;
1386
1387 name = fdt_get_name(fdt, subnode, &len);
1388 if (!strncmp(name, "mdio", 4)) {
Mugunthan V N66e740c2016-04-28 15:36:06 +05301389 u32 mdio_base;
1390
1391 mdio_base = cpsw_get_addr_by_node(fdt, subnode);
1392 if (mdio_base == FDT_ADDR_T_NONE) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +09001393 pr_err("Not able to get MDIO address space\n");
Mugunthan V N66e740c2016-04-28 15:36:06 +05301394 return -ENOENT;
1395 }
1396 priv->data.mdio_base = mdio_base;
Mugunthan V N4cc77892015-09-07 14:22:21 +05301397 }
1398
1399 if (!strncmp(name, "slave", 5)) {
1400 u32 phy_id[2];
1401
Mugunthan V Nb2003c52016-04-28 15:36:04 +05301402 if (slave_index >= priv->data.slaves)
1403 continue;
Mugunthan V N4cc77892015-09-07 14:22:21 +05301404 phy_mode = fdt_getprop(fdt, subnode, "phy-mode", NULL);
1405 if (phy_mode)
1406 priv->data.slave_data[slave_index].phy_if =
1407 phy_get_interface_by_name(phy_mode);
Dan Murphycb386222016-05-02 15:45:56 -05001408
1409 priv->data.slave_data[slave_index].phy_of_handle =
1410 fdtdec_lookup_phandle(fdt, subnode,
1411 "phy-handle");
1412
1413 if (priv->data.slave_data[slave_index].phy_of_handle >= 0) {
1414 priv->data.slave_data[slave_index].phy_addr =
1415 fdtdec_get_int(gd->fdt_blob,
1416 priv->data.slave_data[slave_index].phy_of_handle,
1417 "reg", -1);
1418 } else {
1419 fdtdec_get_int_array(fdt, subnode, "phy_id",
1420 phy_id, 2);
1421 priv->data.slave_data[slave_index].phy_addr =
1422 phy_id[1];
1423 }
Mugunthan V N4cc77892015-09-07 14:22:21 +05301424 slave_index++;
1425 }
1426
1427 if (!strncmp(name, "cpsw-phy-sel", 12)) {
Mugunthan V N66e740c2016-04-28 15:36:06 +05301428 priv->data.gmii_sel = cpsw_get_addr_by_node(fdt,
1429 subnode);
1430
1431 if (priv->data.gmii_sel == FDT_ADDR_T_NONE) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +09001432 pr_err("Not able to get gmii_sel reg address\n");
Mugunthan V N66e740c2016-04-28 15:36:06 +05301433 return -ENOENT;
1434 }
Mugunthan V Nab971532016-10-13 19:33:38 +05301435
1436 if (fdt_get_property(fdt, subnode, "rmii-clock-ext",
1437 NULL))
1438 priv->data.rmii_clock_external = true;
1439
1440 phy_sel_compat = fdt_getprop(fdt, subnode, "compatible",
1441 NULL);
1442 if (!phy_sel_compat) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +09001443 pr_err("Not able to get gmii_sel compatible\n");
Mugunthan V Nab971532016-10-13 19:33:38 +05301444 return -ENOENT;
1445 }
Mugunthan V N4cc77892015-09-07 14:22:21 +05301446 }
1447 }
1448
1449 priv->data.slave_data[0].slave_reg_ofs = CPSW_SLAVE0_OFFSET;
1450 priv->data.slave_data[0].sliver_reg_ofs = CPSW_SLIVER0_OFFSET;
1451
1452 if (priv->data.slaves == 2) {
1453 priv->data.slave_data[1].slave_reg_ofs = CPSW_SLAVE1_OFFSET;
1454 priv->data.slave_data[1].sliver_reg_ofs = CPSW_SLIVER1_OFFSET;
1455 }
1456
Mugunthan V Ne4310562016-04-28 15:36:07 +05301457 ret = ti_cm_get_macid(dev, active_slave, pdata->enetaddr);
1458 if (ret < 0) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +09001459 pr_err("cpsw read efuse mac failed\n");
Mugunthan V Ne4310562016-04-28 15:36:07 +05301460 return ret;
1461 }
Mugunthan V N4cc77892015-09-07 14:22:21 +05301462
1463 pdata->phy_interface = priv->data.slave_data[active_slave].phy_if;
1464 if (pdata->phy_interface == -1) {
1465 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
1466 return -EINVAL;
1467 }
Mugunthan V Nab971532016-10-13 19:33:38 +05301468
1469 /* Select phy interface in control module */
1470 cpsw_phy_sel(priv, phy_sel_compat, pdata->phy_interface);
Mugunthan V Ne4310562016-04-28 15:36:07 +05301471
Mugunthan V N4cc77892015-09-07 14:22:21 +05301472 return 0;
1473}
1474
Sekhar Norie2597be2018-08-23 17:11:29 +05301475int cpsw_get_slave_phy_addr(struct udevice *dev, int slave)
1476{
1477 struct cpsw_priv *priv = dev_get_priv(dev);
1478 struct cpsw_platform_data *data = &priv->data;
1479
1480 return data->slave_data[slave].phy_addr;
1481}
Mugunthan V N4cc77892015-09-07 14:22:21 +05301482
1483static const struct udevice_id cpsw_eth_ids[] = {
1484 { .compatible = "ti,cpsw" },
1485 { .compatible = "ti,am335x-cpsw" },
1486 { }
1487};
1488
1489U_BOOT_DRIVER(eth_cpsw) = {
1490 .name = "eth_cpsw",
1491 .id = UCLASS_ETH,
1492 .of_match = cpsw_eth_ids,
1493 .ofdata_to_platdata = cpsw_eth_ofdata_to_platdata,
1494 .probe = cpsw_eth_probe,
1495 .ops = &cpsw_eth_ops,
1496 .priv_auto_alloc_size = sizeof(struct cpsw_priv),
1497 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
1498 .flags = DM_FLAG_ALLOC_PRIV_DMA,
1499};
1500#endif /* CONFIG_DM_ETH */