blob: d6fefe530688d21ff98e3810e93a217b4cd39079 [file] [log] [blame]
Grygorii Strashko39b014a2018-10-31 16:21:42 -05001// SPDX-License-Identifier: GPL-2.0+
Cyril Chemparathy464adc82012-07-24 12:22:16 +00002/*
3 * CPSW Ethernet Switch Driver
4 *
Grygorii Strashko39b014a2018-10-31 16:21:42 -05005 * Copyright (C) 2010-2018 Texas Instruments Incorporated - http://www.ti.com/
Cyril Chemparathy464adc82012-07-24 12:22:16 +00006 */
7
8#include <common.h>
9#include <command.h>
Simon Glass63334482019-11-14 12:57:39 -070010#include <cpu_func.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Cyril Chemparathy464adc82012-07-24 12:22:16 +000012#include <net.h>
13#include <miiphy.h>
14#include <malloc.h>
15#include <net.h>
16#include <netdev.h>
17#include <cpsw.h>
Simon Glass9bc15642020-02-03 07:36:16 -070018#include <dm/device_compat.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060019#include <linux/bitops.h>
Tom Rinif4a09482020-06-04 16:05:32 -040020#include <linux/compiler.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090021#include <linux/errno.h>
Vignesh R8e9473d2016-08-02 10:14:27 +053022#include <asm/gpio.h>
Cyril Chemparathy464adc82012-07-24 12:22:16 +000023#include <asm/io.h>
24#include <phy.h>
Tom Rini8eb48ff2013-03-14 11:15:25 +000025#include <asm/arch/cpu.h>
Mugunthan V N7ae228c2015-09-07 14:22:21 +053026#include <dm.h>
27
Grygorii Strashkoeae545a2018-10-31 16:21:44 -050028#include "cpsw_mdio.h"
29
Cyril Chemparathy464adc82012-07-24 12:22:16 +000030#define BITMASK(bits) (BIT(bits) - 1)
Cyril Chemparathy464adc82012-07-24 12:22:16 +000031#define NUM_DESCS (PKTBUFSRX * 2)
32#define PKT_MIN 60
33#define PKT_MAX (1500 + 14 + 4 + 4)
34#define CLEAR_BIT 1
35#define GIGABITEN BIT(7)
36#define FULLDUPLEXEN BIT(0)
37#define MIIEN BIT(15)
Grygorii Strashko63bba7e2019-09-19 11:16:37 +030038#define CTL_EXT_EN BIT(18)
Cyril Chemparathy464adc82012-07-24 12:22:16 +000039/* DMA Registers */
40#define CPDMA_TXCONTROL 0x004
41#define CPDMA_RXCONTROL 0x014
42#define CPDMA_SOFTRESET 0x01c
43#define CPDMA_RXFREE 0x0e0
44#define CPDMA_TXHDP_VER1 0x100
45#define CPDMA_TXHDP_VER2 0x200
46#define CPDMA_RXHDP_VER1 0x120
47#define CPDMA_RXHDP_VER2 0x220
48#define CPDMA_TXCP_VER1 0x140
49#define CPDMA_TXCP_VER2 0x240
50#define CPDMA_RXCP_VER1 0x160
51#define CPDMA_RXCP_VER2 0x260
52
Cyril Chemparathy464adc82012-07-24 12:22:16 +000053/* Descriptor mode bits */
54#define CPDMA_DESC_SOP BIT(31)
55#define CPDMA_DESC_EOP BIT(30)
56#define CPDMA_DESC_OWNER BIT(29)
57#define CPDMA_DESC_EOQ BIT(28)
58
59/*
60 * This timeout definition is a worst-case ultra defensive measure against
61 * unexpected controller lock ups. Ideally, we should never ever hit this
62 * scenario in practice.
63 */
Cyril Chemparathy464adc82012-07-24 12:22:16 +000064#define CPDMA_TIMEOUT 100 /* msecs */
65
Cyril Chemparathy464adc82012-07-24 12:22:16 +000066struct cpsw_regs {
67 u32 id_ver;
68 u32 control;
69 u32 soft_reset;
70 u32 stat_port_en;
71 u32 ptype;
72};
73
74struct cpsw_slave_regs {
75 u32 max_blks;
76 u32 blk_cnt;
77 u32 flow_thresh;
78 u32 port_vlan;
79 u32 tx_pri_map;
Matt Porter1ef0e872013-03-20 05:38:12 +000080#ifdef CONFIG_AM33XX
Cyril Chemparathy464adc82012-07-24 12:22:16 +000081 u32 gap_thresh;
Matt Porter1ef0e872013-03-20 05:38:12 +000082#elif defined(CONFIG_TI814X)
83 u32 ts_ctl;
84 u32 ts_seq_ltype;
85 u32 ts_vlan;
86#endif
Cyril Chemparathy464adc82012-07-24 12:22:16 +000087 u32 sa_lo;
88 u32 sa_hi;
89};
90
91struct cpsw_host_regs {
92 u32 max_blks;
93 u32 blk_cnt;
94 u32 flow_thresh;
95 u32 port_vlan;
96 u32 tx_pri_map;
97 u32 cpdma_tx_pri_map;
98 u32 cpdma_rx_chan_map;
99};
100
101struct cpsw_sliver_regs {
102 u32 id_ver;
103 u32 mac_control;
104 u32 mac_status;
105 u32 soft_reset;
106 u32 rx_maxlen;
107 u32 __reserved_0;
108 u32 rx_pause;
109 u32 tx_pause;
110 u32 __reserved_1;
111 u32 rx_pri_map;
112};
113
114#define ALE_ENTRY_BITS 68
115#define ALE_ENTRY_WORDS DIV_ROUND_UP(ALE_ENTRY_BITS, 32)
116
117/* ALE Registers */
118#define ALE_CONTROL 0x08
119#define ALE_UNKNOWNVLAN 0x18
120#define ALE_TABLE_CONTROL 0x20
121#define ALE_TABLE 0x34
122#define ALE_PORTCTL 0x40
123
124#define ALE_TABLE_WRITE BIT(31)
125
126#define ALE_TYPE_FREE 0
127#define ALE_TYPE_ADDR 1
128#define ALE_TYPE_VLAN 2
129#define ALE_TYPE_VLAN_ADDR 3
130
131#define ALE_UCAST_PERSISTANT 0
132#define ALE_UCAST_UNTOUCHED 1
133#define ALE_UCAST_OUI 2
134#define ALE_UCAST_TOUCHED 3
135
136#define ALE_MCAST_FWD 0
137#define ALE_MCAST_BLOCK_LEARN_FWD 1
138#define ALE_MCAST_FWD_LEARN 2
139#define ALE_MCAST_FWD_2 3
140
141enum cpsw_ale_port_state {
142 ALE_PORT_STATE_DISABLE = 0x00,
143 ALE_PORT_STATE_BLOCK = 0x01,
144 ALE_PORT_STATE_LEARN = 0x02,
145 ALE_PORT_STATE_FORWARD = 0x03,
146};
147
148/* ALE unicast entry flags - passed into cpsw_ale_add_ucast() */
149#define ALE_SECURE 1
150#define ALE_BLOCKED 2
151
152struct cpsw_slave {
153 struct cpsw_slave_regs *regs;
154 struct cpsw_sliver_regs *sliver;
155 int slave_num;
156 u32 mac_control;
157 struct cpsw_slave_data *data;
158};
159
160struct cpdma_desc {
161 /* hardware fields */
162 u32 hw_next;
163 u32 hw_buffer;
164 u32 hw_len;
165 u32 hw_mode;
166 /* software fields */
167 u32 sw_buffer;
168 u32 sw_len;
169};
170
171struct cpdma_chan {
172 struct cpdma_desc *head, *tail;
173 void *hdp, *cp, *rxfree;
174};
175
Mugunthan V N4d5fdb62016-10-13 19:33:38 +0530176/* AM33xx SoC specific definitions for the CONTROL port */
177#define AM33XX_GMII_SEL_MODE_MII 0
178#define AM33XX_GMII_SEL_MODE_RMII 1
179#define AM33XX_GMII_SEL_MODE_RGMII 2
180
181#define AM33XX_GMII_SEL_RGMII1_IDMODE BIT(4)
182#define AM33XX_GMII_SEL_RGMII2_IDMODE BIT(5)
183#define AM33XX_GMII_SEL_RMII1_IO_CLK_EN BIT(6)
184#define AM33XX_GMII_SEL_RMII2_IO_CLK_EN BIT(7)
185
186#define GMII_SEL_MODE_MASK 0x3
187
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000188#define desc_write(desc, fld, val) __raw_writel((u32)(val), &(desc)->fld)
189#define desc_read(desc, fld) __raw_readl(&(desc)->fld)
190#define desc_read_ptr(desc, fld) ((void *)__raw_readl(&(desc)->fld))
191
192#define chan_write(chan, fld, val) __raw_writel((u32)(val), (chan)->fld)
193#define chan_read(chan, fld) __raw_readl((chan)->fld)
194#define chan_read_ptr(chan, fld) ((void *)__raw_readl((chan)->fld))
195
Mugunthan V N33e073e2014-05-22 14:37:10 +0530196#define for_active_slave(slave, priv) \
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530197 slave = (priv)->slaves + ((priv)->data)->active_slave; if (slave)
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000198#define for_each_slave(slave, priv) \
199 for (slave = (priv)->slaves; slave != (priv)->slaves + \
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530200 ((priv)->data)->slaves; slave++)
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000201
202struct cpsw_priv {
Mugunthan V N7ae228c2015-09-07 14:22:21 +0530203#ifdef CONFIG_DM_ETH
204 struct udevice *dev;
205#else
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000206 struct eth_device *dev;
Mugunthan V N7ae228c2015-09-07 14:22:21 +0530207#endif
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530208 struct cpsw_platform_data *data;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000209 int host_port;
210
211 struct cpsw_regs *regs;
212 void *dma_regs;
213 struct cpsw_host_regs *host_port_regs;
214 void *ale_regs;
215
216 struct cpdma_desc *descs;
217 struct cpdma_desc *desc_free;
218 struct cpdma_chan rx_chan, tx_chan;
219
220 struct cpsw_slave *slaves;
221 struct phy_device *phydev;
222 struct mii_dev *bus;
Mugunthan V Nc3fdab42013-02-19 21:34:44 +0000223
Mugunthan V Nc3fdab42013-02-19 21:34:44 +0000224 u32 phy_mask;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000225};
226
227static inline int cpsw_ale_get_field(u32 *ale_entry, u32 start, u32 bits)
228{
229 int idx;
230
231 idx = start / 32;
232 start -= idx * 32;
233 idx = 2 - idx; /* flip */
234 return (ale_entry[idx] >> start) & BITMASK(bits);
235}
236
237static inline void cpsw_ale_set_field(u32 *ale_entry, u32 start, u32 bits,
238 u32 value)
239{
240 int idx;
241
242 value &= BITMASK(bits);
243 idx = start / 32;
244 start -= idx * 32;
245 idx = 2 - idx; /* flip */
246 ale_entry[idx] &= ~(BITMASK(bits) << start);
247 ale_entry[idx] |= (value << start);
248}
249
250#define DEFINE_ALE_FIELD(name, start, bits) \
Tom Rinif4a09482020-06-04 16:05:32 -0400251static inline int __maybe_unused cpsw_ale_get_##name(u32 *ale_entry) \
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000252{ \
253 return cpsw_ale_get_field(ale_entry, start, bits); \
254} \
Tom Rinif4a09482020-06-04 16:05:32 -0400255static inline void __maybe_unused cpsw_ale_set_##name(u32 *ale_entry, u32 value) \
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000256{ \
257 cpsw_ale_set_field(ale_entry, start, bits, value); \
258}
259
260DEFINE_ALE_FIELD(entry_type, 60, 2)
261DEFINE_ALE_FIELD(mcast_state, 62, 2)
262DEFINE_ALE_FIELD(port_mask, 66, 3)
263DEFINE_ALE_FIELD(ucast_type, 62, 2)
264DEFINE_ALE_FIELD(port_num, 66, 2)
265DEFINE_ALE_FIELD(blocked, 65, 1)
266DEFINE_ALE_FIELD(secure, 64, 1)
267DEFINE_ALE_FIELD(mcast, 40, 1)
268
269/* The MAC address field in the ALE entry cannot be macroized as above */
270static inline void cpsw_ale_get_addr(u32 *ale_entry, u8 *addr)
271{
272 int i;
273
274 for (i = 0; i < 6; i++)
275 addr[i] = cpsw_ale_get_field(ale_entry, 40 - 8*i, 8);
276}
277
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500278static inline void cpsw_ale_set_addr(u32 *ale_entry, const u8 *addr)
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000279{
280 int i;
281
282 for (i = 0; i < 6; i++)
283 cpsw_ale_set_field(ale_entry, 40 - 8*i, 8, addr[i]);
284}
285
286static int cpsw_ale_read(struct cpsw_priv *priv, int idx, u32 *ale_entry)
287{
288 int i;
289
290 __raw_writel(idx, priv->ale_regs + ALE_TABLE_CONTROL);
291
292 for (i = 0; i < ALE_ENTRY_WORDS; i++)
293 ale_entry[i] = __raw_readl(priv->ale_regs + ALE_TABLE + 4 * i);
294
295 return idx;
296}
297
298static int cpsw_ale_write(struct cpsw_priv *priv, int idx, u32 *ale_entry)
299{
300 int i;
301
302 for (i = 0; i < ALE_ENTRY_WORDS; i++)
303 __raw_writel(ale_entry[i], priv->ale_regs + ALE_TABLE + 4 * i);
304
305 __raw_writel(idx | ALE_TABLE_WRITE, priv->ale_regs + ALE_TABLE_CONTROL);
306
307 return idx;
308}
309
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500310static int cpsw_ale_match_addr(struct cpsw_priv *priv, const u8 *addr)
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000311{
312 u32 ale_entry[ALE_ENTRY_WORDS];
313 int type, idx;
314
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530315 for (idx = 0; idx < priv->data->ale_entries; idx++) {
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000316 u8 entry_addr[6];
317
318 cpsw_ale_read(priv, idx, ale_entry);
319 type = cpsw_ale_get_entry_type(ale_entry);
320 if (type != ALE_TYPE_ADDR && type != ALE_TYPE_VLAN_ADDR)
321 continue;
322 cpsw_ale_get_addr(ale_entry, entry_addr);
323 if (memcmp(entry_addr, addr, 6) == 0)
324 return idx;
325 }
326 return -ENOENT;
327}
328
329static int cpsw_ale_match_free(struct cpsw_priv *priv)
330{
331 u32 ale_entry[ALE_ENTRY_WORDS];
332 int type, idx;
333
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530334 for (idx = 0; idx < priv->data->ale_entries; idx++) {
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000335 cpsw_ale_read(priv, idx, ale_entry);
336 type = cpsw_ale_get_entry_type(ale_entry);
337 if (type == ALE_TYPE_FREE)
338 return idx;
339 }
340 return -ENOENT;
341}
342
343static int cpsw_ale_find_ageable(struct cpsw_priv *priv)
344{
345 u32 ale_entry[ALE_ENTRY_WORDS];
346 int type, idx;
347
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530348 for (idx = 0; idx < priv->data->ale_entries; idx++) {
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000349 cpsw_ale_read(priv, idx, ale_entry);
350 type = cpsw_ale_get_entry_type(ale_entry);
351 if (type != ALE_TYPE_ADDR && type != ALE_TYPE_VLAN_ADDR)
352 continue;
353 if (cpsw_ale_get_mcast(ale_entry))
354 continue;
355 type = cpsw_ale_get_ucast_type(ale_entry);
356 if (type != ALE_UCAST_PERSISTANT &&
357 type != ALE_UCAST_OUI)
358 return idx;
359 }
360 return -ENOENT;
361}
362
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500363static int cpsw_ale_add_ucast(struct cpsw_priv *priv, const u8 *addr,
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000364 int port, int flags)
365{
366 u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
367 int idx;
368
369 cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_ADDR);
370 cpsw_ale_set_addr(ale_entry, addr);
371 cpsw_ale_set_ucast_type(ale_entry, ALE_UCAST_PERSISTANT);
372 cpsw_ale_set_secure(ale_entry, (flags & ALE_SECURE) ? 1 : 0);
373 cpsw_ale_set_blocked(ale_entry, (flags & ALE_BLOCKED) ? 1 : 0);
374 cpsw_ale_set_port_num(ale_entry, port);
375
376 idx = cpsw_ale_match_addr(priv, addr);
377 if (idx < 0)
378 idx = cpsw_ale_match_free(priv);
379 if (idx < 0)
380 idx = cpsw_ale_find_ageable(priv);
381 if (idx < 0)
382 return -ENOMEM;
383
384 cpsw_ale_write(priv, idx, ale_entry);
385 return 0;
386}
387
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500388static int cpsw_ale_add_mcast(struct cpsw_priv *priv, const u8 *addr,
389 int port_mask)
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000390{
391 u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
392 int idx, mask;
393
394 idx = cpsw_ale_match_addr(priv, addr);
395 if (idx >= 0)
396 cpsw_ale_read(priv, idx, ale_entry);
397
398 cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_ADDR);
399 cpsw_ale_set_addr(ale_entry, addr);
400 cpsw_ale_set_mcast_state(ale_entry, ALE_MCAST_FWD_2);
401
402 mask = cpsw_ale_get_port_mask(ale_entry);
403 port_mask |= mask;
404 cpsw_ale_set_port_mask(ale_entry, port_mask);
405
406 if (idx < 0)
407 idx = cpsw_ale_match_free(priv);
408 if (idx < 0)
409 idx = cpsw_ale_find_ageable(priv);
410 if (idx < 0)
411 return -ENOMEM;
412
413 cpsw_ale_write(priv, idx, ale_entry);
414 return 0;
415}
416
417static inline void cpsw_ale_control(struct cpsw_priv *priv, int bit, int val)
418{
419 u32 tmp, mask = BIT(bit);
420
421 tmp = __raw_readl(priv->ale_regs + ALE_CONTROL);
422 tmp &= ~mask;
423 tmp |= val ? mask : 0;
424 __raw_writel(tmp, priv->ale_regs + ALE_CONTROL);
425}
426
427#define cpsw_ale_enable(priv, val) cpsw_ale_control(priv, 31, val)
428#define cpsw_ale_clear(priv, val) cpsw_ale_control(priv, 30, val)
429#define cpsw_ale_vlan_aware(priv, val) cpsw_ale_control(priv, 2, val)
430
431static inline void cpsw_ale_port_state(struct cpsw_priv *priv, int port,
432 int val)
433{
434 int offset = ALE_PORTCTL + 4 * port;
435 u32 tmp, mask = 0x3;
436
437 tmp = __raw_readl(priv->ale_regs + offset);
438 tmp &= ~mask;
439 tmp |= val & mask;
440 __raw_writel(tmp, priv->ale_regs + offset);
441}
442
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000443/* Set a self-clearing bit in a register, and wait for it to clear */
444static inline void setbit_and_wait_for_clear32(void *addr)
445{
446 __raw_writel(CLEAR_BIT, addr);
447 while (__raw_readl(addr) & CLEAR_BIT)
448 ;
449}
450
451#define mac_hi(mac) (((mac)[0] << 0) | ((mac)[1] << 8) | \
452 ((mac)[2] << 16) | ((mac)[3] << 24))
453#define mac_lo(mac) (((mac)[4] << 0) | ((mac)[5] << 8))
454
455static void cpsw_set_slave_mac(struct cpsw_slave *slave,
456 struct cpsw_priv *priv)
457{
Mugunthan V N7ae228c2015-09-07 14:22:21 +0530458#ifdef CONFIG_DM_ETH
459 struct eth_pdata *pdata = dev_get_platdata(priv->dev);
460
461 writel(mac_hi(pdata->enetaddr), &slave->regs->sa_hi);
462 writel(mac_lo(pdata->enetaddr), &slave->regs->sa_lo);
463#else
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000464 __raw_writel(mac_hi(priv->dev->enetaddr), &slave->regs->sa_hi);
465 __raw_writel(mac_lo(priv->dev->enetaddr), &slave->regs->sa_lo);
Mugunthan V N7ae228c2015-09-07 14:22:21 +0530466#endif
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000467}
468
Sekhar Norib5988852017-05-08 20:49:56 +0530469static int cpsw_slave_update_link(struct cpsw_slave *slave,
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000470 struct cpsw_priv *priv, int *link)
471{
Heiko Schocheref660032013-09-05 11:50:41 +0200472 struct phy_device *phy;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000473 u32 mac_control = 0;
Sekhar Norib5988852017-05-08 20:49:56 +0530474 int ret = -ENODEV;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000475
Heiko Schocheref660032013-09-05 11:50:41 +0200476 phy = priv->phydev;
Heiko Schocheref660032013-09-05 11:50:41 +0200477 if (!phy)
Sekhar Norib5988852017-05-08 20:49:56 +0530478 goto out;
479
480 ret = phy_startup(phy);
481 if (ret)
482 goto out;
Heiko Schocheref660032013-09-05 11:50:41 +0200483
Sekhar Norib5988852017-05-08 20:49:56 +0530484 if (link)
485 *link = phy->link;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000486
Sekhar Norib5988852017-05-08 20:49:56 +0530487 if (phy->link) { /* link up */
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530488 mac_control = priv->data->mac_control;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000489 if (phy->speed == 1000)
490 mac_control |= GIGABITEN;
491 if (phy->duplex == DUPLEX_FULL)
492 mac_control |= FULLDUPLEXEN;
493 if (phy->speed == 100)
494 mac_control |= MIIEN;
Grygorii Strashko63bba7e2019-09-19 11:16:37 +0300495 if (phy->speed == 10 && phy_interface_is_rgmii(phy))
496 mac_control |= CTL_EXT_EN;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000497 }
498
499 if (mac_control == slave->mac_control)
Sekhar Norib5988852017-05-08 20:49:56 +0530500 goto out;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000501
502 if (mac_control) {
503 printf("link up on port %d, speed %d, %s duplex\n",
504 slave->slave_num, phy->speed,
505 (phy->duplex == DUPLEX_FULL) ? "full" : "half");
506 } else {
507 printf("link down on port %d\n", slave->slave_num);
508 }
509
510 __raw_writel(mac_control, &slave->sliver->mac_control);
511 slave->mac_control = mac_control;
Sekhar Norib5988852017-05-08 20:49:56 +0530512
513out:
514 return ret;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000515}
516
517static int cpsw_update_link(struct cpsw_priv *priv)
518{
Sekhar Norib5988852017-05-08 20:49:56 +0530519 int ret = -ENODEV;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000520 struct cpsw_slave *slave;
521
Mugunthan V N33e073e2014-05-22 14:37:10 +0530522 for_active_slave(slave, priv)
Sekhar Norib5988852017-05-08 20:49:56 +0530523 ret = cpsw_slave_update_link(slave, priv, NULL);
Mugunthan V Nc3fdab42013-02-19 21:34:44 +0000524
Sekhar Norib5988852017-05-08 20:49:56 +0530525 return ret;
Mugunthan V Nc3fdab42013-02-19 21:34:44 +0000526}
527
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000528static inline u32 cpsw_get_slave_port(struct cpsw_priv *priv, u32 slave_num)
529{
530 if (priv->host_port == 0)
531 return slave_num + 1;
532 else
533 return slave_num;
534}
535
536static void cpsw_slave_init(struct cpsw_slave *slave, struct cpsw_priv *priv)
537{
538 u32 slave_port;
539
540 setbit_and_wait_for_clear32(&slave->sliver->soft_reset);
541
542 /* setup priority mapping */
543 __raw_writel(0x76543210, &slave->sliver->rx_pri_map);
544 __raw_writel(0x33221100, &slave->regs->tx_pri_map);
545
546 /* setup max packet size, and mac address */
547 __raw_writel(PKT_MAX, &slave->sliver->rx_maxlen);
548 cpsw_set_slave_mac(slave, priv);
549
550 slave->mac_control = 0; /* no link yet */
551
552 /* enable forwarding */
553 slave_port = cpsw_get_slave_port(priv, slave->slave_num);
554 cpsw_ale_port_state(priv, slave_port, ALE_PORT_STATE_FORWARD);
555
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500556 cpsw_ale_add_mcast(priv, net_bcast_ethaddr, 1 << slave_port);
Mugunthan V Nc3fdab42013-02-19 21:34:44 +0000557
Mugunthan V N4944f372014-02-18 07:31:52 -0500558 priv->phy_mask |= 1 << slave->data->phy_addr;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000559}
560
561static struct cpdma_desc *cpdma_desc_alloc(struct cpsw_priv *priv)
562{
563 struct cpdma_desc *desc = priv->desc_free;
564
565 if (desc)
566 priv->desc_free = desc_read_ptr(desc, hw_next);
567 return desc;
568}
569
570static void cpdma_desc_free(struct cpsw_priv *priv, struct cpdma_desc *desc)
571{
572 if (desc) {
573 desc_write(desc, hw_next, priv->desc_free);
574 priv->desc_free = desc;
575 }
576}
577
578static int cpdma_submit(struct cpsw_priv *priv, struct cpdma_chan *chan,
579 void *buffer, int len)
580{
581 struct cpdma_desc *desc, *prev;
582 u32 mode;
583
584 desc = cpdma_desc_alloc(priv);
585 if (!desc)
586 return -ENOMEM;
587
588 if (len < PKT_MIN)
589 len = PKT_MIN;
590
591 mode = CPDMA_DESC_OWNER | CPDMA_DESC_SOP | CPDMA_DESC_EOP;
592
593 desc_write(desc, hw_next, 0);
594 desc_write(desc, hw_buffer, buffer);
595 desc_write(desc, hw_len, len);
596 desc_write(desc, hw_mode, mode | len);
597 desc_write(desc, sw_buffer, buffer);
598 desc_write(desc, sw_len, len);
599
600 if (!chan->head) {
601 /* simple case - first packet enqueued */
602 chan->head = desc;
603 chan->tail = desc;
604 chan_write(chan, hdp, desc);
605 goto done;
606 }
607
608 /* not the first packet - enqueue at the tail */
609 prev = chan->tail;
610 desc_write(prev, hw_next, desc);
611 chan->tail = desc;
612
613 /* next check if EOQ has been triggered already */
614 if (desc_read(prev, hw_mode) & CPDMA_DESC_EOQ)
615 chan_write(chan, hdp, desc);
616
617done:
618 if (chan->rxfree)
619 chan_write(chan, rxfree, 1);
620 return 0;
621}
622
623static int cpdma_process(struct cpsw_priv *priv, struct cpdma_chan *chan,
624 void **buffer, int *len)
625{
626 struct cpdma_desc *desc = chan->head;
627 u32 status;
628
629 if (!desc)
630 return -ENOENT;
631
632 status = desc_read(desc, hw_mode);
633
634 if (len)
635 *len = status & 0x7ff;
636
637 if (buffer)
638 *buffer = desc_read_ptr(desc, sw_buffer);
639
640 if (status & CPDMA_DESC_OWNER) {
641 if (chan_read(chan, hdp) == 0) {
642 if (desc_read(desc, hw_mode) & CPDMA_DESC_OWNER)
643 chan_write(chan, hdp, desc);
644 }
645
646 return -EBUSY;
647 }
648
649 chan->head = desc_read_ptr(desc, hw_next);
650 chan_write(chan, cp, desc);
651
652 cpdma_desc_free(priv, desc);
653 return 0;
654}
655
Mugunthan V N6af98c52015-09-07 14:22:20 +0530656static int _cpsw_init(struct cpsw_priv *priv, u8 *enetaddr)
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000657{
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000658 struct cpsw_slave *slave;
659 int i, ret;
660
661 /* soft reset the controller and initialize priv */
662 setbit_and_wait_for_clear32(&priv->regs->soft_reset);
663
664 /* initialize and reset the address lookup engine */
665 cpsw_ale_enable(priv, 1);
666 cpsw_ale_clear(priv, 1);
667 cpsw_ale_vlan_aware(priv, 0); /* vlan unaware mode */
668
669 /* setup host port priority mapping */
670 __raw_writel(0x76543210, &priv->host_port_regs->cpdma_tx_pri_map);
671 __raw_writel(0, &priv->host_port_regs->cpdma_rx_chan_map);
672
673 /* disable priority elevation and enable statistics on all ports */
674 __raw_writel(0, &priv->regs->ptype);
675
676 /* enable statistics collection only on the host port */
677 __raw_writel(BIT(priv->host_port), &priv->regs->stat_port_en);
Mugunthan V N2782f3e2013-07-08 16:04:38 +0530678 __raw_writel(0x7, &priv->regs->stat_port_en);
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000679
680 cpsw_ale_port_state(priv, priv->host_port, ALE_PORT_STATE_FORWARD);
681
Mugunthan V N6af98c52015-09-07 14:22:20 +0530682 cpsw_ale_add_ucast(priv, enetaddr, priv->host_port, ALE_SECURE);
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500683 cpsw_ale_add_mcast(priv, net_bcast_ethaddr, 1 << priv->host_port);
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000684
Mugunthan V N33e073e2014-05-22 14:37:10 +0530685 for_active_slave(slave, priv)
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000686 cpsw_slave_init(slave, priv);
687
Sekhar Norib5988852017-05-08 20:49:56 +0530688 ret = cpsw_update_link(priv);
689 if (ret)
690 goto out;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000691
692 /* init descriptor pool */
693 for (i = 0; i < NUM_DESCS; i++) {
694 desc_write(&priv->descs[i], hw_next,
695 (i == (NUM_DESCS - 1)) ? 0 : &priv->descs[i+1]);
696 }
697 priv->desc_free = &priv->descs[0];
698
699 /* initialize channels */
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530700 if (priv->data->version == CPSW_CTRL_VERSION_2) {
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000701 memset(&priv->rx_chan, 0, sizeof(struct cpdma_chan));
702 priv->rx_chan.hdp = priv->dma_regs + CPDMA_RXHDP_VER2;
703 priv->rx_chan.cp = priv->dma_regs + CPDMA_RXCP_VER2;
704 priv->rx_chan.rxfree = priv->dma_regs + CPDMA_RXFREE;
705
706 memset(&priv->tx_chan, 0, sizeof(struct cpdma_chan));
707 priv->tx_chan.hdp = priv->dma_regs + CPDMA_TXHDP_VER2;
708 priv->tx_chan.cp = priv->dma_regs + CPDMA_TXCP_VER2;
709 } else {
710 memset(&priv->rx_chan, 0, sizeof(struct cpdma_chan));
711 priv->rx_chan.hdp = priv->dma_regs + CPDMA_RXHDP_VER1;
712 priv->rx_chan.cp = priv->dma_regs + CPDMA_RXCP_VER1;
713 priv->rx_chan.rxfree = priv->dma_regs + CPDMA_RXFREE;
714
715 memset(&priv->tx_chan, 0, sizeof(struct cpdma_chan));
716 priv->tx_chan.hdp = priv->dma_regs + CPDMA_TXHDP_VER1;
717 priv->tx_chan.cp = priv->dma_regs + CPDMA_TXCP_VER1;
718 }
719
720 /* clear dma state */
721 setbit_and_wait_for_clear32(priv->dma_regs + CPDMA_SOFTRESET);
722
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530723 if (priv->data->version == CPSW_CTRL_VERSION_2) {
724 for (i = 0; i < priv->data->channels; i++) {
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000725 __raw_writel(0, priv->dma_regs + CPDMA_RXHDP_VER2 + 4
726 * i);
727 __raw_writel(0, priv->dma_regs + CPDMA_RXFREE + 4
728 * i);
729 __raw_writel(0, priv->dma_regs + CPDMA_RXCP_VER2 + 4
730 * i);
731 __raw_writel(0, priv->dma_regs + CPDMA_TXHDP_VER2 + 4
732 * i);
733 __raw_writel(0, priv->dma_regs + CPDMA_TXCP_VER2 + 4
734 * i);
735 }
736 } else {
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530737 for (i = 0; i < priv->data->channels; i++) {
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000738 __raw_writel(0, priv->dma_regs + CPDMA_RXHDP_VER1 + 4
739 * i);
740 __raw_writel(0, priv->dma_regs + CPDMA_RXFREE + 4
741 * i);
742 __raw_writel(0, priv->dma_regs + CPDMA_RXCP_VER1 + 4
743 * i);
744 __raw_writel(0, priv->dma_regs + CPDMA_TXHDP_VER1 + 4
745 * i);
746 __raw_writel(0, priv->dma_regs + CPDMA_TXCP_VER1 + 4
747 * i);
748
749 }
750 }
751
752 __raw_writel(1, priv->dma_regs + CPDMA_TXCONTROL);
753 __raw_writel(1, priv->dma_regs + CPDMA_RXCONTROL);
754
755 /* submit rx descs */
756 for (i = 0; i < PKTBUFSRX; i++) {
Joe Hershberger9f09a362015-04-08 01:41:06 -0500757 ret = cpdma_submit(priv, &priv->rx_chan, net_rx_packets[i],
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000758 PKTSIZE);
759 if (ret < 0) {
760 printf("error %d submitting rx desc\n", ret);
761 break;
762 }
763 }
764
Sekhar Norib5988852017-05-08 20:49:56 +0530765out:
766 return ret;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000767}
768
Alex Kiernane210e982018-05-12 07:30:02 +0000769static int cpsw_reap_completed_packets(struct cpsw_priv *priv)
770{
771 int timeout = CPDMA_TIMEOUT;
772
773 /* reap completed packets */
774 while (timeout-- &&
775 (cpdma_process(priv, &priv->tx_chan, NULL, NULL) >= 0))
776 ;
777
778 return timeout;
779}
780
Mugunthan V N6af98c52015-09-07 14:22:20 +0530781static void _cpsw_halt(struct cpsw_priv *priv)
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000782{
Alex Kiernane210e982018-05-12 07:30:02 +0000783 cpsw_reap_completed_packets(priv);
784
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000785 writel(0, priv->dma_regs + CPDMA_TXCONTROL);
786 writel(0, priv->dma_regs + CPDMA_RXCONTROL);
787
788 /* soft reset the controller and initialize priv */
789 setbit_and_wait_for_clear32(&priv->regs->soft_reset);
790
791 /* clear dma state */
792 setbit_and_wait_for_clear32(priv->dma_regs + CPDMA_SOFTRESET);
793
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000794}
795
Mugunthan V N6af98c52015-09-07 14:22:20 +0530796static int _cpsw_send(struct cpsw_priv *priv, void *packet, int length)
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000797{
Alex Kiernane210e982018-05-12 07:30:02 +0000798 int timeout;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000799
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000800 flush_dcache_range((unsigned long)packet,
Lokesh Vutlaf9f4ced2016-08-11 13:00:59 +0530801 (unsigned long)packet + ALIGN(length, PKTALIGN));
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000802
Alex Kiernane210e982018-05-12 07:30:02 +0000803 timeout = cpsw_reap_completed_packets(priv);
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000804 if (timeout == -1) {
805 printf("cpdma_process timeout\n");
806 return -ETIMEDOUT;
807 }
808
809 return cpdma_submit(priv, &priv->tx_chan, packet, length);
810}
811
Mugunthan V N6af98c52015-09-07 14:22:20 +0530812static int _cpsw_recv(struct cpsw_priv *priv, uchar **pkt)
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000813{
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000814 void *buffer;
815 int len;
Heinrich Schuchardt71bb8dd2018-03-18 11:24:38 +0100816 int ret;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000817
Mugunthan V N6af98c52015-09-07 14:22:20 +0530818 ret = cpdma_process(priv, &priv->rx_chan, &buffer, &len);
819 if (ret < 0)
820 return ret;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000821
Mugunthan V N6af98c52015-09-07 14:22:20 +0530822 invalidate_dcache_range((unsigned long)buffer,
823 (unsigned long)buffer + PKTSIZE_ALIGN);
824 *pkt = buffer;
825
826 return len;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000827}
828
829static void cpsw_slave_setup(struct cpsw_slave *slave, int slave_num,
830 struct cpsw_priv *priv)
831{
832 void *regs = priv->regs;
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530833 struct cpsw_slave_data *data = priv->data->slave_data + slave_num;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000834 slave->slave_num = slave_num;
835 slave->data = data;
836 slave->regs = regs + data->slave_reg_ofs;
837 slave->sliver = regs + data->sliver_reg_ofs;
838}
839
Mugunthan V N6af98c52015-09-07 14:22:20 +0530840static int cpsw_phy_init(struct cpsw_priv *priv, struct cpsw_slave *slave)
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000841{
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000842 struct phy_device *phydev;
Ilya Ledvicha1635f02014-03-12 11:26:30 +0200843 u32 supported = PHY_GBIT_FEATURES;
Grygorii Strashkoe32ed8c2019-09-19 11:16:39 +0300844 int ret;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000845
Yegor Yefremov062bbac2012-11-26 04:03:16 +0000846 phydev = phy_connect(priv->bus,
Mugunthan V N4944f372014-02-18 07:31:52 -0500847 slave->data->phy_addr,
Mugunthan V N6af98c52015-09-07 14:22:20 +0530848 priv->dev,
Yegor Yefremov062bbac2012-11-26 04:03:16 +0000849 slave->data->phy_if);
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000850
Heiko Schocheref660032013-09-05 11:50:41 +0200851 if (!phydev)
852 return -1;
853
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000854 phydev->supported &= supported;
Grygorii Strashkoe32ed8c2019-09-19 11:16:39 +0300855 if (slave->data->max_speed) {
856 ret = phy_set_supported(phydev, slave->data->max_speed);
857 if (ret)
858 return ret;
859 dev_dbg(priv->dev, "Port %u speed forced to %uMbit\n",
860 slave->slave_num + 1, slave->data->max_speed);
861 }
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000862 phydev->advertising = phydev->supported;
863
Dan Murphy4b7b24e2016-05-02 15:45:56 -0500864#ifdef CONFIG_DM_ETH
Grygorii Strashko133dd1c2019-09-19 11:16:42 +0300865 if (ofnode_valid(slave->data->phy_of_handle))
866 phydev->node = slave->data->phy_of_handle;
Dan Murphy4b7b24e2016-05-02 15:45:56 -0500867#endif
868
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000869 priv->phydev = phydev;
870 phy_config(phydev);
871
872 return 1;
873}
874
Sekhar Noricfc5cc82018-08-23 17:11:29 +0530875static void cpsw_phy_addr_update(struct cpsw_priv *priv)
876{
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530877 struct cpsw_platform_data *data = priv->data;
Grygorii Strashkoeae545a2018-10-31 16:21:44 -0500878 u16 alive = cpsw_mdio_get_alive(priv->bus);
Sekhar Noricfc5cc82018-08-23 17:11:29 +0530879 int active = data->active_slave;
880 int new_addr = ffs(alive) - 1;
881
882 /*
883 * If there is only one phy alive and its address does not match
884 * that of active slave, then phy address can safely be updated.
885 */
886 if (hweight16(alive) == 1 &&
887 data->slave_data[active].phy_addr != new_addr) {
888 printf("Updated phy address for CPSW#%d, old: %d, new: %d\n",
889 active, data->slave_data[active].phy_addr, new_addr);
890 data->slave_data[active].phy_addr = new_addr;
891 }
892}
893
Mugunthan V N6af98c52015-09-07 14:22:20 +0530894int _cpsw_register(struct cpsw_priv *priv)
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000895{
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000896 struct cpsw_slave *slave;
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530897 struct cpsw_platform_data *data = priv->data;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000898 void *regs = (void *)data->cpsw_base;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000899
900 priv->slaves = malloc(sizeof(struct cpsw_slave) * data->slaves);
901 if (!priv->slaves) {
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000902 return -ENOMEM;
903 }
904
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000905 priv->host_port = data->host_port_num;
906 priv->regs = regs;
907 priv->host_port_regs = regs + data->host_port_reg_ofs;
908 priv->dma_regs = regs + data->cpdma_reg_ofs;
909 priv->ale_regs = regs + data->ale_reg_ofs;
Mugunthan V Nff559872013-07-08 16:04:37 +0530910 priv->descs = (void *)regs + data->bd_ram_ofs;
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000911
912 int idx = 0;
913
914 for_each_slave(slave, priv) {
915 cpsw_slave_setup(slave, idx, priv);
916 idx = idx + 1;
917 }
918
Grygorii Strashkoeae545a2018-10-31 16:21:44 -0500919 priv->bus = cpsw_mdio_init(priv->dev->name, data->mdio_base, 0, 0);
920 if (!priv->bus)
921 return -EFAULT;
Sekhar Noricfc5cc82018-08-23 17:11:29 +0530922
923 cpsw_phy_addr_update(priv);
924
Mugunthan V N6af98c52015-09-07 14:22:20 +0530925 for_active_slave(slave, priv)
926 cpsw_phy_init(priv, slave);
927
928 return 0;
929}
930
Mugunthan V N7ae228c2015-09-07 14:22:21 +0530931#ifndef CONFIG_DM_ETH
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900932static int cpsw_init(struct eth_device *dev, struct bd_info *bis)
Mugunthan V N6af98c52015-09-07 14:22:20 +0530933{
934 struct cpsw_priv *priv = dev->priv;
935
936 return _cpsw_init(priv, dev->enetaddr);
937}
938
939static void cpsw_halt(struct eth_device *dev)
940{
941 struct cpsw_priv *priv = dev->priv;
942
943 return _cpsw_halt(priv);
944}
945
946static int cpsw_send(struct eth_device *dev, void *packet, int length)
947{
948 struct cpsw_priv *priv = dev->priv;
949
950 return _cpsw_send(priv, packet, length);
951}
952
953static int cpsw_recv(struct eth_device *dev)
954{
955 struct cpsw_priv *priv = dev->priv;
956 uchar *pkt = NULL;
957 int len;
958
959 len = _cpsw_recv(priv, &pkt);
960
961 if (len > 0) {
962 net_process_received_packet(pkt, len);
963 cpdma_submit(priv, &priv->rx_chan, pkt, PKTSIZE);
964 }
965
966 return len;
967}
968
969int cpsw_register(struct cpsw_platform_data *data)
970{
971 struct cpsw_priv *priv;
972 struct eth_device *dev;
973 int ret;
974
975 dev = calloc(sizeof(*dev), 1);
976 if (!dev)
977 return -ENOMEM;
978
979 priv = calloc(sizeof(*priv), 1);
980 if (!priv) {
981 free(dev);
982 return -ENOMEM;
983 }
984
985 priv->dev = dev;
Faiz Abbas5e3d77f2019-03-18 13:54:33 +0530986 priv->data = data;
Mugunthan V N6af98c52015-09-07 14:22:20 +0530987
Cyril Chemparathy464adc82012-07-24 12:22:16 +0000988 strcpy(dev->name, "cpsw");
989 dev->iobase = 0;
990 dev->init = cpsw_init;
991 dev->halt = cpsw_halt;
992 dev->send = cpsw_send;
993 dev->recv = cpsw_recv;
994 dev->priv = priv;
995
996 eth_register(dev);
997
Mugunthan V N6af98c52015-09-07 14:22:20 +0530998 ret = _cpsw_register(priv);
999 if (ret < 0) {
1000 eth_unregister(dev);
1001 free(dev);
1002 free(priv);
1003 return ret;
1004 }
Cyril Chemparathy464adc82012-07-24 12:22:16 +00001005
1006 return 1;
1007}
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301008#else
1009static int cpsw_eth_start(struct udevice *dev)
1010{
1011 struct eth_pdata *pdata = dev_get_platdata(dev);
1012 struct cpsw_priv *priv = dev_get_priv(dev);
1013
1014 return _cpsw_init(priv, pdata->enetaddr);
1015}
1016
1017static int cpsw_eth_send(struct udevice *dev, void *packet, int length)
1018{
1019 struct cpsw_priv *priv = dev_get_priv(dev);
1020
1021 return _cpsw_send(priv, packet, length);
1022}
1023
1024static int cpsw_eth_recv(struct udevice *dev, int flags, uchar **packetp)
1025{
1026 struct cpsw_priv *priv = dev_get_priv(dev);
1027
1028 return _cpsw_recv(priv, packetp);
1029}
1030
1031static int cpsw_eth_free_pkt(struct udevice *dev, uchar *packet,
1032 int length)
1033{
1034 struct cpsw_priv *priv = dev_get_priv(dev);
1035
1036 return cpdma_submit(priv, &priv->rx_chan, packet, PKTSIZE);
1037}
1038
1039static void cpsw_eth_stop(struct udevice *dev)
1040{
1041 struct cpsw_priv *priv = dev_get_priv(dev);
1042
1043 return _cpsw_halt(priv);
1044}
1045
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301046static const struct eth_ops cpsw_eth_ops = {
1047 .start = cpsw_eth_start,
1048 .send = cpsw_eth_send,
1049 .recv = cpsw_eth_recv,
1050 .free_pkt = cpsw_eth_free_pkt,
1051 .stop = cpsw_eth_stop,
1052};
1053
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301054static void cpsw_gmii_sel_am3352(struct cpsw_priv *priv,
1055 phy_interface_t phy_mode)
1056{
1057 u32 reg;
1058 u32 mask;
1059 u32 mode = 0;
1060 bool rgmii_id = false;
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301061 int slave = priv->data->active_slave;
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301062
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301063 reg = readl(priv->data->gmii_sel);
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301064
1065 switch (phy_mode) {
1066 case PHY_INTERFACE_MODE_RMII:
1067 mode = AM33XX_GMII_SEL_MODE_RMII;
1068 break;
1069
1070 case PHY_INTERFACE_MODE_RGMII:
Grygorii Strashko40b346d2019-09-19 11:16:40 +03001071 case PHY_INTERFACE_MODE_RGMII_RXID:
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301072 mode = AM33XX_GMII_SEL_MODE_RGMII;
1073 break;
1074 case PHY_INTERFACE_MODE_RGMII_ID:
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301075 case PHY_INTERFACE_MODE_RGMII_TXID:
1076 mode = AM33XX_GMII_SEL_MODE_RGMII;
1077 rgmii_id = true;
1078 break;
1079
1080 case PHY_INTERFACE_MODE_MII:
1081 default:
1082 mode = AM33XX_GMII_SEL_MODE_MII;
1083 break;
1084 };
1085
1086 mask = GMII_SEL_MODE_MASK << (slave * 2) | BIT(slave + 6);
1087 mode <<= slave * 2;
1088
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301089 if (priv->data->rmii_clock_external) {
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301090 if (slave == 0)
1091 mode |= AM33XX_GMII_SEL_RMII1_IO_CLK_EN;
1092 else
1093 mode |= AM33XX_GMII_SEL_RMII2_IO_CLK_EN;
1094 }
1095
1096 if (rgmii_id) {
1097 if (slave == 0)
1098 mode |= AM33XX_GMII_SEL_RGMII1_IDMODE;
1099 else
1100 mode |= AM33XX_GMII_SEL_RGMII2_IDMODE;
1101 }
1102
1103 reg &= ~mask;
1104 reg |= mode;
1105
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301106 writel(reg, priv->data->gmii_sel);
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301107}
1108
1109static void cpsw_gmii_sel_dra7xx(struct cpsw_priv *priv,
1110 phy_interface_t phy_mode)
1111{
1112 u32 reg;
1113 u32 mask;
1114 u32 mode = 0;
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301115 int slave = priv->data->active_slave;
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301116
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301117 reg = readl(priv->data->gmii_sel);
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301118
1119 switch (phy_mode) {
1120 case PHY_INTERFACE_MODE_RMII:
1121 mode = AM33XX_GMII_SEL_MODE_RMII;
1122 break;
1123
1124 case PHY_INTERFACE_MODE_RGMII:
1125 case PHY_INTERFACE_MODE_RGMII_ID:
1126 case PHY_INTERFACE_MODE_RGMII_RXID:
1127 case PHY_INTERFACE_MODE_RGMII_TXID:
1128 mode = AM33XX_GMII_SEL_MODE_RGMII;
1129 break;
1130
1131 case PHY_INTERFACE_MODE_MII:
1132 default:
1133 mode = AM33XX_GMII_SEL_MODE_MII;
1134 break;
1135 };
1136
1137 switch (slave) {
1138 case 0:
1139 mask = GMII_SEL_MODE_MASK;
1140 break;
1141 case 1:
1142 mask = GMII_SEL_MODE_MASK << 4;
1143 mode <<= 4;
1144 break;
1145 default:
1146 dev_err(priv->dev, "invalid slave number...\n");
1147 return;
1148 }
1149
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301150 if (priv->data->rmii_clock_external)
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301151 dev_err(priv->dev, "RMII External clock is not supported\n");
1152
1153 reg &= ~mask;
1154 reg |= mode;
1155
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301156 writel(reg, priv->data->gmii_sel);
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301157}
1158
1159static void cpsw_phy_sel(struct cpsw_priv *priv, const char *compat,
1160 phy_interface_t phy_mode)
1161{
1162 if (!strcmp(compat, "ti,am3352-cpsw-phy-sel"))
1163 cpsw_gmii_sel_am3352(priv, phy_mode);
1164 if (!strcmp(compat, "ti,am43xx-cpsw-phy-sel"))
1165 cpsw_gmii_sel_am3352(priv, phy_mode);
1166 else if (!strcmp(compat, "ti,dra7xx-cpsw-phy-sel"))
1167 cpsw_gmii_sel_dra7xx(priv, phy_mode);
1168}
1169
Faiz Abbas5b5dc0f2019-03-18 13:54:32 +05301170static int cpsw_eth_probe(struct udevice *dev)
1171{
1172 struct cpsw_priv *priv = dev_get_priv(dev);
1173 struct eth_pdata *pdata = dev_get_platdata(dev);
1174
1175 priv->dev = dev;
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301176 priv->data = pdata->priv_pdata;
Faiz Abbas8ecdffe2019-03-18 13:54:34 +05301177 ti_cm_get_macid(dev, priv->data, pdata->enetaddr);
Faiz Abbas5b5dc0f2019-03-18 13:54:32 +05301178 /* Select phy interface in control module */
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301179 cpsw_phy_sel(priv, priv->data->phy_sel_compat,
Faiz Abbas5b5dc0f2019-03-18 13:54:32 +05301180 pdata->phy_interface);
1181
1182 return _cpsw_register(priv);
1183}
1184
Faiz Abbas687e80e2019-03-18 13:54:35 +05301185#if CONFIG_IS_ENABLED(OF_CONTROL)
Grygorii Strashko68e313e2019-09-19 11:16:38 +03001186static void cpsw_eth_of_parse_slave(struct cpsw_platform_data *data,
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001187 int slave_index, ofnode subnode)
Grygorii Strashko68e313e2019-09-19 11:16:38 +03001188{
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001189 struct ofnode_phandle_args out_args;
1190 struct cpsw_slave_data *slave_data;
Grygorii Strashko68e313e2019-09-19 11:16:38 +03001191 const char *phy_mode;
1192 u32 phy_id[2];
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001193 int ret;
Grygorii Strashko68e313e2019-09-19 11:16:38 +03001194
1195 slave_data = &data->slave_data[slave_index];
1196
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001197 phy_mode = ofnode_read_string(subnode, "phy-mode");
Grygorii Strashko68e313e2019-09-19 11:16:38 +03001198 if (phy_mode)
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001199 slave_data->phy_if = phy_get_interface_by_name(phy_mode);
Grygorii Strashko68e313e2019-09-19 11:16:38 +03001200
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001201 ret = ofnode_parse_phandle_with_args(subnode, "phy-handle",
1202 NULL, 0, 0, &out_args);
1203 if (!ret) {
1204 slave_data->phy_of_handle = out_args.node;
Grygorii Strashko68e313e2019-09-19 11:16:38 +03001205
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001206 ret = ofnode_read_s32(slave_data->phy_of_handle, "reg",
1207 &slave_data->phy_addr);
1208 if (ret)
1209 printf("error: phy addr not found in dt\n");
Grygorii Strashko68e313e2019-09-19 11:16:38 +03001210 } else {
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001211 ret = ofnode_read_u32_array(subnode, "phy_id", phy_id, 2);
1212 if (ret)
1213 printf("error: phy_id read failed\n");
Grygorii Strashko68e313e2019-09-19 11:16:38 +03001214 }
Grygorii Strashkoe32ed8c2019-09-19 11:16:39 +03001215
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001216 slave_data->max_speed = ofnode_read_s32_default(subnode,
1217 "max-speed", 0);
Grygorii Strashko68e313e2019-09-19 11:16:38 +03001218}
1219
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301220static int cpsw_eth_ofdata_to_platdata(struct udevice *dev)
1221{
1222 struct eth_pdata *pdata = dev_get_platdata(dev);
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301223 struct cpsw_platform_data *data;
Vignesh R8e9473d2016-08-02 10:14:27 +05301224 struct gpio_desc *mode_gpios;
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301225 int slave_index = 0;
Vignesh R8e9473d2016-08-02 10:14:27 +05301226 int num_mode_gpios;
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001227 ofnode subnode;
Mugunthan V N9e556352016-04-28 15:36:07 +05301228 int ret;
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301229
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301230 data = calloc(1, sizeof(struct cpsw_platform_data));
Faiz Abbas9e0e1f32019-11-11 15:22:56 +05301231 if (!data)
1232 return -ENOMEM;
1233
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301234 pdata->priv_pdata = data;
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001235 pdata->iobase = dev_read_addr(dev);
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301236 data->version = CPSW_CTRL_VERSION_2;
1237 data->bd_ram_ofs = CPSW_BD_OFFSET;
1238 data->ale_reg_ofs = CPSW_ALE_OFFSET;
1239 data->cpdma_reg_ofs = CPSW_CPDMA_OFFSET;
1240 data->mdio_div = CPSW_MDIO_DIV;
1241 data->host_port_reg_ofs = CPSW_HOST_PORT_OFFSET,
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301242
1243 pdata->phy_interface = -1;
1244
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301245 data->cpsw_base = pdata->iobase;
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001246
1247 ret = dev_read_s32(dev, "cpdma_channels", &data->channels);
1248 if (ret) {
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301249 printf("error: cpdma_channels not found in dt\n");
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001250 return ret;
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301251 }
1252
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001253 ret = dev_read_s32(dev, "slaves", &data->slaves);
1254 if (ret) {
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301255 printf("error: slaves not found in dt\n");
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001256 return ret;
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301257 }
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301258 data->slave_data = malloc(sizeof(struct cpsw_slave_data) *
1259 data->slaves);
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301260
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001261 ret = dev_read_s32(dev, "ale_entries", &data->ale_entries);
1262 if (ret) {
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301263 printf("error: ale_entries not found in dt\n");
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001264 return ret;
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301265 }
1266
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001267 ret = dev_read_u32(dev, "bd_ram_size", &data->bd_ram_ofs);
1268 if (ret) {
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301269 printf("error: bd_ram_size not found in dt\n");
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001270 return ret;
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301271 }
1272
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001273 ret = dev_read_u32(dev, "mac_control", &data->mac_control);
1274 if (ret) {
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301275 printf("error: ale_entries not found in dt\n");
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001276 return ret;
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301277 }
1278
Vignesh R8e9473d2016-08-02 10:14:27 +05301279 num_mode_gpios = gpio_get_list_count(dev, "mode-gpios");
1280 if (num_mode_gpios > 0) {
1281 mode_gpios = malloc(sizeof(struct gpio_desc) *
1282 num_mode_gpios);
1283 gpio_request_list_by_name(dev, "mode-gpios", mode_gpios,
1284 num_mode_gpios, GPIOD_IS_OUT);
1285 free(mode_gpios);
1286 }
1287
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001288 data->active_slave = dev_read_u32_default(dev, "active_slave", 0);
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301289
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001290 ofnode_for_each_subnode(subnode, dev_ofnode(dev)) {
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301291 const char *name;
1292
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001293 name = ofnode_get_name(subnode);
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301294 if (!strncmp(name, "mdio", 4)) {
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001295 data->mdio_base = ofnode_get_addr(subnode);
1296 if (data->mdio_base == FDT_ADDR_T_NONE) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001297 pr_err("Not able to get MDIO address space\n");
Mugunthan V N77f7bc72016-04-28 15:36:06 +05301298 return -ENOENT;
1299 }
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301300 }
1301
1302 if (!strncmp(name, "slave", 5)) {
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301303 if (slave_index >= data->slaves)
Mugunthan V N13a27fd2016-04-28 15:36:04 +05301304 continue;
Dan Murphy4b7b24e2016-05-02 15:45:56 -05001305
Grygorii Strashko68e313e2019-09-19 11:16:38 +03001306 cpsw_eth_of_parse_slave(data, slave_index, subnode);
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301307 slave_index++;
1308 }
1309
1310 if (!strncmp(name, "cpsw-phy-sel", 12)) {
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001311 data->gmii_sel = ofnode_get_addr(subnode);
Mugunthan V N77f7bc72016-04-28 15:36:06 +05301312
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301313 if (data->gmii_sel == FDT_ADDR_T_NONE) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001314 pr_err("Not able to get gmii_sel reg address\n");
Mugunthan V N77f7bc72016-04-28 15:36:06 +05301315 return -ENOENT;
1316 }
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301317
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001318 if (ofnode_read_bool(subnode, "rmii-clock-ext"))
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301319 data->rmii_clock_external = true;
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301320
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001321 data->phy_sel_compat = ofnode_read_string(subnode,
1322 "compatible");
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301323 if (!data->phy_sel_compat) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001324 pr_err("Not able to get gmii_sel compatible\n");
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301325 return -ENOENT;
1326 }
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301327 }
1328 }
1329
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301330 data->slave_data[0].slave_reg_ofs = CPSW_SLAVE0_OFFSET;
1331 data->slave_data[0].sliver_reg_ofs = CPSW_SLIVER0_OFFSET;
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301332
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301333 if (data->slaves == 2) {
1334 data->slave_data[1].slave_reg_ofs = CPSW_SLAVE1_OFFSET;
1335 data->slave_data[1].sliver_reg_ofs = CPSW_SLIVER1_OFFSET;
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301336 }
1337
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001338 ret = ti_cm_get_macid_addr(dev, data->active_slave, data);
Mugunthan V N9e556352016-04-28 15:36:07 +05301339 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001340 pr_err("cpsw read efuse mac failed\n");
Mugunthan V N9e556352016-04-28 15:36:07 +05301341 return ret;
1342 }
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301343
Grygorii Strashko133dd1c2019-09-19 11:16:42 +03001344 pdata->phy_interface = data->slave_data[data->active_slave].phy_if;
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301345 if (pdata->phy_interface == -1) {
Grygorii Strashko68e313e2019-09-19 11:16:38 +03001346 debug("%s: Invalid PHY interface '%s'\n", __func__,
1347 phy_string_for_interface(pdata->phy_interface));
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301348 return -EINVAL;
1349 }
Mugunthan V N4d5fdb62016-10-13 19:33:38 +05301350
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301351 return 0;
1352}
1353
Faiz Abbas687e80e2019-03-18 13:54:35 +05301354static const struct udevice_id cpsw_eth_ids[] = {
1355 { .compatible = "ti,cpsw" },
1356 { .compatible = "ti,am335x-cpsw" },
1357 { }
1358};
1359#endif
1360
Sekhar Noricfc5cc82018-08-23 17:11:29 +05301361int cpsw_get_slave_phy_addr(struct udevice *dev, int slave)
1362{
1363 struct cpsw_priv *priv = dev_get_priv(dev);
Faiz Abbas5e3d77f2019-03-18 13:54:33 +05301364 struct cpsw_platform_data *data = priv->data;
Sekhar Noricfc5cc82018-08-23 17:11:29 +05301365
1366 return data->slave_data[slave].phy_addr;
1367}
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301368
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301369U_BOOT_DRIVER(eth_cpsw) = {
1370 .name = "eth_cpsw",
1371 .id = UCLASS_ETH,
Faiz Abbas687e80e2019-03-18 13:54:35 +05301372#if CONFIG_IS_ENABLED(OF_CONTROL)
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301373 .of_match = cpsw_eth_ids,
1374 .ofdata_to_platdata = cpsw_eth_ofdata_to_platdata,
Faiz Abbas687e80e2019-03-18 13:54:35 +05301375 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
1376#endif
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301377 .probe = cpsw_eth_probe,
1378 .ops = &cpsw_eth_ops,
1379 .priv_auto_alloc_size = sizeof(struct cpsw_priv),
Faiz Abbasd852b942019-03-18 13:54:36 +05301380 .flags = DM_FLAG_ALLOC_PRIV_DMA | DM_FLAG_PRE_RELOC,
Mugunthan V N7ae228c2015-09-07 14:22:21 +05301381};
1382#endif /* CONFIG_DM_ETH */