blob: 4901321d0cf2f8f46fc49294c0041e56706287c5 [file] [log] [blame]
Jassi Brar29c58b02021-06-04 18:44:38 +09001// SPDX-License-Identifier: GPL-2.0+
2/**
3 * netsec.c - Socionext Synquacer Netsec driver
4 * Copyright 2021 Linaro Ltd.
5 */
6
7#include <clk.h>
8#include <cpu_func.h>
9#include <dm.h>
10#include <fdt_support.h>
11#include <log.h>
12#include <malloc.h>
13#include <miiphy.h>
14#include <net.h>
15#include <regmap.h>
16#include <reset.h>
17#include <syscon.h>
18#include <asm/cache.h>
19#include <asm/global_data.h>
20#include <dm/device_compat.h>
21#include <linux/bitops.h>
22#include <linux/delay.h>
23#include <linux/err.h>
24#include <linux/io.h>
25#include <linux/iopoll.h>
26#include <spi.h>
27#include <spi_flash.h>
28
29#define NETSEC_REG_SOFT_RST 0x104
30#define NETSEC_REG_COM_INIT 0x120
31
32#define NETSEC_REG_TOP_STATUS 0x200
33#define NETSEC_IRQ_RX BIT(1)
34#define NETSEC_IRQ_TX BIT(0)
35
36#define NETSEC_REG_TOP_INTEN 0x204
37#define NETSEC_REG_INTEN_SET 0x234
38#define NETSEC_REG_INTEN_CLR 0x238
39
40#define NETSEC_REG_NRM_TX_STATUS 0x400
41#define NETSEC_REG_NRM_TX_INTEN 0x404
42#define NETSEC_REG_NRM_TX_INTEN_SET 0x428
43#define NETSEC_REG_NRM_TX_INTEN_CLR 0x42c
44#define NRM_TX_ST_NTOWNR BIT(17)
45#define NRM_TX_ST_TR_ERR BIT(16)
46#define NRM_TX_ST_TXDONE BIT(15)
47#define NRM_TX_ST_TMREXP BIT(14)
48
49#define NETSEC_REG_NRM_RX_STATUS 0x440
50#define NETSEC_REG_NRM_RX_INTEN 0x444
51#define NETSEC_REG_NRM_RX_INTEN_SET 0x468
52#define NETSEC_REG_NRM_RX_INTEN_CLR 0x46c
53#define NRM_RX_ST_RC_ERR BIT(16)
54#define NRM_RX_ST_PKTCNT BIT(15)
55#define NRM_RX_ST_TMREXP BIT(14)
56
57#define NETSEC_REG_PKT_CMD_BUF 0xd0
58
59#define NETSEC_REG_CLK_EN 0x100
60
61#define NETSEC_REG_PKT_CTRL 0x140
62
63#define NETSEC_REG_DMA_TMR_CTRL 0x20c
64#define NETSEC_REG_F_TAIKI_MC_VER 0x22c
65#define NETSEC_REG_F_TAIKI_VER 0x230
66#define NETSEC_REG_DMA_HM_CTRL 0x214
67#define NETSEC_REG_DMA_MH_CTRL 0x220
68#define NETSEC_REG_ADDR_DIS_CORE 0x218
69#define NETSEC_REG_DMAC_HM_CMD_BUF 0x210
70#define NETSEC_REG_DMAC_MH_CMD_BUF 0x21c
71
72#define NETSEC_REG_NRM_TX_PKTCNT 0x410
73
74#define NETSEC_REG_NRM_TX_DONE_PKTCNT 0x414
75#define NETSEC_REG_NRM_TX_DONE_TXINT_PKTCNT 0x418
76
77#define NETSEC_REG_NRM_TX_TMR 0x41c
78
79#define NETSEC_REG_NRM_RX_PKTCNT 0x454
80#define NETSEC_REG_NRM_RX_RXINT_PKTCNT 0x458
81#define NETSEC_REG_NRM_TX_TXINT_TMR 0x420
82#define NETSEC_REG_NRM_RX_RXINT_TMR 0x460
83
84#define NETSEC_REG_NRM_RX_TMR 0x45c
85
86#define NETSEC_REG_NRM_TX_DESC_START_UP 0x434
87#define NETSEC_REG_NRM_TX_DESC_START_LW 0x408
88#define NETSEC_REG_NRM_RX_DESC_START_UP 0x474
89#define NETSEC_REG_NRM_RX_DESC_START_LW 0x448
90
91#define NETSEC_REG_NRM_TX_CONFIG 0x430
92#define NETSEC_REG_NRM_RX_CONFIG 0x470
93
94#define MAC_REG_STATUS 0x1024
95#define MAC_REG_DATA 0x11c0
96#define MAC_REG_CMD 0x11c4
97#define MAC_REG_FLOW_TH 0x11cc
98#define MAC_REG_INTF_SEL 0x11d4
99#define MAC_REG_DESC_INIT 0x11fc
100#define MAC_REG_DESC_SOFT_RST 0x1204
101#define NETSEC_REG_MODE_TRANS_COMP_STATUS 0x500
102
103#define GMAC_REG_MCR 0x0000
104#define GMAC_REG_MFFR 0x0004
105#define GMAC_REG_GAR 0x0010
106#define GMAC_REG_GDR 0x0014
107#define GMAC_REG_FCR 0x0018
108#define GMAC_REG_BMR 0x1000
109#define GMAC_REG_RDLAR 0x100c
110#define GMAC_REG_TDLAR 0x1010
111#define GMAC_REG_OMR 0x1018
112
113#define MHZ(n) ((n) * 1000 * 1000)
114
115#define NETSEC_TX_SHIFT_OWN_FIELD 31
116#define NETSEC_TX_SHIFT_LD_FIELD 30
117#define NETSEC_TX_SHIFT_DRID_FIELD 24
118#define NETSEC_TX_SHIFT_PT_FIELD 21
119#define NETSEC_TX_SHIFT_TDRID_FIELD 16
120#define NETSEC_TX_SHIFT_CC_FIELD 15
121#define NETSEC_TX_SHIFT_FS_FIELD 9
122#define NETSEC_TX_LAST 8
123#define NETSEC_TX_SHIFT_CO 7
124#define NETSEC_TX_SHIFT_SO 6
125#define NETSEC_TX_SHIFT_TRS_FIELD 4
126
127#define NETSEC_RX_PKT_OWN_FIELD 31
128#define NETSEC_RX_PKT_LD_FIELD 30
129#define NETSEC_RX_PKT_SDRID_FIELD 24
130#define NETSEC_RX_PKT_FR_FIELD 23
131#define NETSEC_RX_PKT_ER_FIELD 21
132#define NETSEC_RX_PKT_ERR_FIELD 16
133#define NETSEC_RX_PKT_TDRID_FIELD 12
134#define NETSEC_RX_PKT_FS_FIELD 9
135#define NETSEC_RX_PKT_LS_FIELD 8
136#define NETSEC_RX_PKT_CO_FIELD 6
137
138#define NETSEC_RX_PKT_ERR_MASK 3
139
140#define NETSEC_MAX_TX_PKT_LEN 1518
141#define NETSEC_MAX_TX_JUMBO_PKT_LEN 9018
142
143#define NETSEC_RING_GMAC 15
144#define NETSEC_RING_MAX 2
145
146#define NETSEC_TCP_SEG_LEN_MAX 1460
147#define NETSEC_TCP_JUMBO_SEG_LEN_MAX 8960
148
149#define NETSEC_RX_CKSUM_NOTAVAIL 0
150#define NETSEC_RX_CKSUM_OK 1
151#define NETSEC_RX_CKSUM_NG 2
152
153#define NETSEC_TOP_IRQ_REG_ME_START BIT(20)
154#define NETSEC_IRQ_TRANSITION_COMPLETE BIT(4)
155
156#define NETSEC_MODE_TRANS_COMP_IRQ_N2T BIT(20)
157#define NETSEC_MODE_TRANS_COMP_IRQ_T2N BIT(19)
158
159#define NETSEC_INT_PKTCNT_MAX 2047
160
161#define NETSEC_FLOW_START_TH_MAX 95
162#define NETSEC_FLOW_STOP_TH_MAX 95
163#define NETSEC_FLOW_PAUSE_TIME_MIN 5
164
165#define NETSEC_CLK_EN_REG_DOM_ALL 0x3f
166
167#define NETSEC_PKT_CTRL_REG_MODE_NRM BIT(28)
168#define NETSEC_PKT_CTRL_REG_EN_JUMBO BIT(27)
169#define NETSEC_PKT_CTRL_REG_LOG_CHKSUM_ER BIT(3)
170#define NETSEC_PKT_CTRL_REG_LOG_HD_INCOMPLETE BIT(2)
171#define NETSEC_PKT_CTRL_REG_LOG_HD_ER BIT(1)
172#define NETSEC_PKT_CTRL_REG_DRP_NO_MATCH BIT(0)
173
174#define NETSEC_CLK_EN_REG_DOM_G BIT(5)
175#define NETSEC_CLK_EN_REG_DOM_C BIT(1)
176#define NETSEC_CLK_EN_REG_DOM_D BIT(0)
177
178#define NETSEC_COM_INIT_REG_DB BIT(2)
179#define NETSEC_COM_INIT_REG_CLS BIT(1)
180#define NETSEC_COM_INIT_REG_ALL (NETSEC_COM_INIT_REG_CLS | \
181 NETSEC_COM_INIT_REG_DB)
182
183#define NETSEC_SOFT_RST_REG_RESET 0
184#define NETSEC_SOFT_RST_REG_RUN BIT(31)
185
186#define NETSEC_DMA_CTRL_REG_STOP 1
187#define MH_CTRL__MODE_TRANS BIT(20)
188
189#define NETSEC_GMAC_CMD_ST_READ 0
190#define NETSEC_GMAC_CMD_ST_WRITE BIT(28)
191#define NETSEC_GMAC_CMD_ST_BUSY BIT(31)
192
193#define NETSEC_GMAC_BMR_REG_COMMON 0x00412080
194#define NETSEC_GMAC_BMR_REG_RESET 0x00020181
195#define NETSEC_GMAC_BMR_REG_SWR 0x00000001
196
197#define NETSEC_GMAC_OMR_REG_ST BIT(13)
198#define NETSEC_GMAC_OMR_REG_SR BIT(1)
199
200#define NETSEC_GMAC_MCR_REG_IBN BIT(30)
201#define NETSEC_GMAC_MCR_REG_CST BIT(25)
202#define NETSEC_GMAC_MCR_REG_JE BIT(20)
203#define NETSEC_MCR_PS BIT(15)
204#define NETSEC_GMAC_MCR_REG_FES BIT(14)
205#define NETSEC_GMAC_MCR_REG_FULL_DUPLEX_COMMON 0x0000280c
206#define NETSEC_GMAC_MCR_REG_HALF_DUPLEX_COMMON 0x0001a00c
207
208#define NETSEC_FCR_RFE BIT(2)
209#define NETSEC_FCR_TFE BIT(1)
210
211#define NETSEC_GMAC_GAR_REG_GW BIT(1)
212#define NETSEC_GMAC_GAR_REG_GB BIT(0)
213
214#define NETSEC_GMAC_GAR_REG_SHIFT_PA 11
215#define NETSEC_GMAC_GAR_REG_SHIFT_GR 6
216#define GMAC_REG_SHIFT_CR_GAR 2
217
218#define NETSEC_GMAC_GAR_REG_CR_25_35_MHZ 2
219#define NETSEC_GMAC_GAR_REG_CR_35_60_MHZ 3
220#define NETSEC_GMAC_GAR_REG_CR_60_100_MHZ 0
221#define NETSEC_GMAC_GAR_REG_CR_100_150_MHZ 1
222#define NETSEC_GMAC_GAR_REG_CR_150_250_MHZ 4
223#define NETSEC_GMAC_GAR_REG_CR_250_300_MHZ 5
224
225#define NETSEC_GMAC_RDLAR_REG_COMMON 0x18000
226#define NETSEC_GMAC_TDLAR_REG_COMMON 0x1c000
227
228#define NETSEC_REG_NETSEC_VER_F_TAIKI 0x50000
229
230#define NETSEC_REG_DESC_RING_CONFIG_CFG_UP BIT(31)
231#define NETSEC_REG_DESC_RING_CONFIG_CH_RST BIT(30)
232#define NETSEC_REG_DESC_TMR_MODE 4
233#define NETSEC_REG_DESC_ENDIAN 0
234
235#define NETSEC_MAC_DESC_SOFT_RST_SOFT_RST 1
236#define NETSEC_MAC_DESC_INIT_REG_INIT 1
237
238#define NETSEC_EEPROM_MAC_ADDRESS 0x00
239#define NETSEC_EEPROM_HM_ME_ADDRESS_H 0x08
240#define NETSEC_EEPROM_HM_ME_ADDRESS_L 0x0C
241#define NETSEC_EEPROM_HM_ME_SIZE 0x10
242#define NETSEC_EEPROM_MH_ME_ADDRESS_H 0x14
243#define NETSEC_EEPROM_MH_ME_ADDRESS_L 0x18
244#define NETSEC_EEPROM_MH_ME_SIZE 0x1C
245#define NETSEC_EEPROM_PKT_ME_ADDRESS 0x20
246#define NETSEC_EEPROM_PKT_ME_SIZE 0x24
247
248#define DESC_SZ sizeof(struct netsec_de)
249
250#define NETSEC_F_NETSEC_VER_MAJOR_NUM(x) ((x) & 0xffff0000)
251
252#define EERPROM_MAP_OFFSET 0x8000000
253#define NOR_BLOCK 1024
254
255struct netsec_de { /* Netsec Descriptor layout */
256 u32 attr;
257 u32 data_buf_addr_up;
258 u32 data_buf_addr_lw;
259 u32 buf_len_info;
260};
261
262struct netsec_priv {
263 struct netsec_de rxde[PKTBUFSRX];
264 struct netsec_de txde[1];
265 u16 rxat;
266
267 phys_addr_t eeprom_base;
268 phys_addr_t ioaddr;
269
270 struct mii_dev *bus;
271 struct phy_device *phydev;
272 u32 phy_addr, freq;
273 int phy_mode;
274 int max_speed;
275};
276
277struct netsec_tx_pkt_ctrl {
278 u16 tcp_seg_len;
279 bool tcp_seg_offload_flag;
280 bool cksum_offload_flag;
281};
282
283struct netsec_rx_pkt_info {
284 int rx_cksum_result;
285 int err_code;
286 bool err_flag;
287};
288
289static void netsec_write_reg(struct netsec_priv *priv, u32 reg_addr, u32 val)
290{
291 writel(val, priv->ioaddr + reg_addr);
292}
293
294static u32 netsec_read_reg(struct netsec_priv *priv, u32 reg_addr)
295{
296 return readl(priv->ioaddr + reg_addr);
297}
298
299/************* MDIO BUS OPS FOLLOW *************/
300
301#define TIMEOUT_SPINS_MAC 1000
302#define TIMEOUT_SECONDARY_MS_MAC 100
303
304static u32 netsec_clk_type(u32 freq)
305{
306 if (freq < MHZ(35))
307 return NETSEC_GMAC_GAR_REG_CR_25_35_MHZ;
308 if (freq < MHZ(60))
309 return NETSEC_GMAC_GAR_REG_CR_35_60_MHZ;
310 if (freq < MHZ(100))
311 return NETSEC_GMAC_GAR_REG_CR_60_100_MHZ;
312 if (freq < MHZ(150))
313 return NETSEC_GMAC_GAR_REG_CR_100_150_MHZ;
314 if (freq < MHZ(250))
315 return NETSEC_GMAC_GAR_REG_CR_150_250_MHZ;
316
317 return NETSEC_GMAC_GAR_REG_CR_250_300_MHZ;
318}
319
320static int netsec_wait_while_busy(struct netsec_priv *priv, u32 addr, u32 mask)
321{
322 u32 timeout = TIMEOUT_SPINS_MAC;
323
324 while (--timeout && netsec_read_reg(priv, addr) & mask)
325 cpu_relax();
326 if (timeout)
327 return 0;
328
329 timeout = TIMEOUT_SECONDARY_MS_MAC;
330 while (--timeout && netsec_read_reg(priv, addr) & mask)
331 udelay(2000);
332
333 if (timeout)
334 return 0;
335
336 pr_err("%s: timeout\n", __func__);
337
338 return -ETIMEDOUT;
339}
340
341static int netsec_set_mac_reg(struct netsec_priv *priv, u32 addr, u32 value)
342{
343 netsec_write_reg(priv, MAC_REG_DATA, value);
344 netsec_write_reg(priv, MAC_REG_CMD, addr | NETSEC_GMAC_CMD_ST_WRITE);
345 return netsec_wait_while_busy(priv,
346 MAC_REG_CMD, NETSEC_GMAC_CMD_ST_BUSY);
347}
348
349static int netsec_get_mac_reg(struct netsec_priv *priv, u32 addr, u32 *read)
350{
351 int ret;
352
353 netsec_write_reg(priv, MAC_REG_CMD, addr | NETSEC_GMAC_CMD_ST_READ);
354 ret = netsec_wait_while_busy(priv,
355 MAC_REG_CMD, NETSEC_GMAC_CMD_ST_BUSY);
356 if (ret)
357 return ret;
358
359 *read = netsec_read_reg(priv, MAC_REG_DATA);
360
361 return 0;
362}
363
364static int netsec_mac_wait_while_busy(struct netsec_priv *priv,
365 u32 addr, u32 mask)
366{
367 u32 timeout = TIMEOUT_SPINS_MAC;
368 u32 data;
369 int ret;
370
371 do {
372 ret = netsec_get_mac_reg(priv, addr, &data);
373 if (ret)
374 break;
375 udelay(1);
376 } while (--timeout && (data & mask));
377
378 if (timeout)
379 return 0;
380
381 timeout = TIMEOUT_SECONDARY_MS_MAC;
382 do {
383 udelay(2000);
384
385 ret = netsec_get_mac_reg(priv, addr, &data);
386 if (ret)
387 break;
388 cpu_relax();
389 } while (--timeout && (data & mask));
390
391 if (timeout && !ret)
392 return 0;
393
394 return -ETIMEDOUT;
395}
396
397static void netsec_cache_invalidate(uintptr_t vaddr, int len)
398{
399 invalidate_dcache_range(rounddown(vaddr, ARCH_DMA_MINALIGN),
400 roundup(vaddr + len, ARCH_DMA_MINALIGN));
401}
402
403static void netsec_cache_flush(uintptr_t vaddr, int len)
404{
405 flush_dcache_range(rounddown(vaddr, ARCH_DMA_MINALIGN),
406 roundup(vaddr + len, ARCH_DMA_MINALIGN));
407}
408
409static void netsec_set_rx_de(struct netsec_priv *priv, u16 idx, void *addr)
410{
411 struct netsec_de *de = &priv->rxde[idx];
412 u32 attr = (1 << NETSEC_RX_PKT_OWN_FIELD) |
413 (1 << NETSEC_RX_PKT_FS_FIELD) |
414 (1 << NETSEC_RX_PKT_LS_FIELD);
415
416 if (idx == PKTBUFSRX - 1)
417 attr |= (1 << NETSEC_RX_PKT_LD_FIELD);
418
419 de->data_buf_addr_up = upper_32_bits((dma_addr_t)addr);
420 de->data_buf_addr_lw = lower_32_bits((dma_addr_t)addr);
421 de->buf_len_info = PKTSIZE;
422 de->attr = attr;
423 dmb();
424 netsec_cache_flush((uintptr_t)de, sizeof(*de));
425}
426
427static void netsec_set_tx_de(struct netsec_priv *priv, void *addr, int len)
428{
429 struct netsec_de *de = &priv->txde[0];
430 u32 attr;
431
432 attr = (1 << NETSEC_TX_SHIFT_OWN_FIELD) |
433 (1 << NETSEC_TX_SHIFT_PT_FIELD) |
434 (NETSEC_RING_GMAC << NETSEC_TX_SHIFT_TDRID_FIELD) |
435 (1 << NETSEC_TX_SHIFT_FS_FIELD) |
436 (1 << NETSEC_TX_LAST) |
437 (1 << NETSEC_TX_SHIFT_TRS_FIELD) |
438 (1 << NETSEC_TX_SHIFT_LD_FIELD);
439
440 de->data_buf_addr_up = upper_32_bits((dma_addr_t)addr);
441 de->data_buf_addr_lw = lower_32_bits((dma_addr_t)addr);
442 de->buf_len_info = len;
443 de->attr = attr;
444 dmb();
445 netsec_cache_flush((uintptr_t)de, sizeof(*de));
446}
447
448static int netsec_get_phy_reg(struct netsec_priv *priv,
449 int phy_addr, int reg_addr)
450{
451 u32 data;
452 int ret;
453
454 if (phy_addr != 7)
455 return -EINVAL;
456
457 if (netsec_set_mac_reg(priv, GMAC_REG_GAR, NETSEC_GMAC_GAR_REG_GB |
458 phy_addr << NETSEC_GMAC_GAR_REG_SHIFT_PA |
459 reg_addr << NETSEC_GMAC_GAR_REG_SHIFT_GR |
460 (netsec_clk_type(priv->freq) <<
461 GMAC_REG_SHIFT_CR_GAR)))
462 return -ETIMEDOUT;
463
464 ret = netsec_mac_wait_while_busy(priv, GMAC_REG_GAR,
465 NETSEC_GMAC_GAR_REG_GB);
466 if (ret)
467 return ret;
468
469 ret = netsec_get_mac_reg(priv, GMAC_REG_GDR, &data);
470 if (ret)
471 return ret;
472
473 return data;
474}
475
476static int netsec_set_phy_reg(struct netsec_priv *priv,
477 int phy_addr, int reg_addr, u16 val)
478{
479 int ret;
480
481 if (phy_addr != 7)
482 return -EINVAL;
483 if (netsec_set_mac_reg(priv, GMAC_REG_GDR, val))
484 return -ETIMEDOUT;
485
486 if (netsec_set_mac_reg(priv, GMAC_REG_GAR,
487 phy_addr << NETSEC_GMAC_GAR_REG_SHIFT_PA |
488 reg_addr << NETSEC_GMAC_GAR_REG_SHIFT_GR |
489 NETSEC_GMAC_GAR_REG_GW | NETSEC_GMAC_GAR_REG_GB |
490 (netsec_clk_type(priv->freq) <<
491 GMAC_REG_SHIFT_CR_GAR)))
492 return -ETIMEDOUT;
493
494 ret = netsec_mac_wait_while_busy(priv, GMAC_REG_GAR,
495 NETSEC_GMAC_GAR_REG_GB);
496
497 /* Developerbox implements RTL8211E PHY and there is
498 * a compatibility problem with F_GMAC4.
499 * RTL8211E expects MDC clock must be kept toggling for several
500 * clock cycle with MDIO high before entering the IDLE state.
501 * To meet this requirement, netsec driver needs to issue dummy
502 * read(e.g. read PHYID1(offset 0x2) register) right after write.
503 */
504 netsec_get_phy_reg(priv, phy_addr, MII_PHYSID1);
505
506 return ret;
507}
508
509static int netsec_mac_update_to_phy_state(struct netsec_priv *priv)
510{
511 struct phy_device *phydev = priv->phydev;
512 u32 value = 0;
513
514 value = phydev->duplex ? NETSEC_GMAC_MCR_REG_FULL_DUPLEX_COMMON :
515 NETSEC_GMAC_MCR_REG_HALF_DUPLEX_COMMON;
516
517 if (phydev->speed != SPEED_1000)
518 value |= NETSEC_MCR_PS;
519
520 if (phydev->interface != PHY_INTERFACE_MODE_GMII &&
521 phydev->speed == SPEED_100)
522 value |= NETSEC_GMAC_MCR_REG_FES;
523
524 value |= NETSEC_GMAC_MCR_REG_CST | NETSEC_GMAC_MCR_REG_JE;
525
526 if (phy_interface_is_rgmii(phydev))
527 value |= NETSEC_GMAC_MCR_REG_IBN;
528
529 if (netsec_set_mac_reg(priv, GMAC_REG_MCR, value))
530 return -ETIMEDOUT;
531
532 return 0;
533}
534
535static int netsec_start_gmac(struct netsec_priv *priv)
536{
537 u32 value = 0;
538 int ret;
539
540 if (priv->max_speed != SPEED_1000)
541 value = (NETSEC_GMAC_MCR_REG_CST |
542 NETSEC_GMAC_MCR_REG_HALF_DUPLEX_COMMON);
543
544 if (netsec_set_mac_reg(priv, GMAC_REG_MCR, value))
545 return -ETIMEDOUT;
546
547 if (netsec_set_mac_reg(priv, GMAC_REG_BMR,
548 NETSEC_GMAC_BMR_REG_RESET))
549 return -ETIMEDOUT;
550
551 /* Wait soft reset */
552 mdelay(5);
553
554 ret = netsec_get_mac_reg(priv, GMAC_REG_BMR, &value);
555 if (ret)
556 return ret;
557
558 if (value & NETSEC_GMAC_BMR_REG_SWR)
559 return -EAGAIN;
560
561 netsec_write_reg(priv, MAC_REG_DESC_SOFT_RST, 1);
562 if (netsec_wait_while_busy(priv, MAC_REG_DESC_SOFT_RST, 1))
563 return -ETIMEDOUT;
564
565 netsec_write_reg(priv, MAC_REG_DESC_INIT, 1);
566 if (netsec_wait_while_busy(priv, MAC_REG_DESC_INIT, 1))
567 return -ETIMEDOUT;
568
569 if (netsec_set_mac_reg(priv, GMAC_REG_BMR,
570 NETSEC_GMAC_BMR_REG_COMMON))
571 return -ETIMEDOUT;
572
573 if (netsec_set_mac_reg(priv, GMAC_REG_RDLAR,
574 NETSEC_GMAC_RDLAR_REG_COMMON))
575 return -ETIMEDOUT;
576
577 if (netsec_set_mac_reg(priv, GMAC_REG_TDLAR,
578 NETSEC_GMAC_TDLAR_REG_COMMON))
579 return -ETIMEDOUT;
580
581 if (netsec_set_mac_reg(priv, GMAC_REG_MFFR, 0x80000001))
582 return -ETIMEDOUT;
583
584 ret = netsec_mac_update_to_phy_state(priv);
585 if (ret)
586 return ret;
587
588 ret = netsec_get_mac_reg(priv, GMAC_REG_OMR, &value);
589 if (ret)
590 return ret;
591
592 value |= NETSEC_GMAC_OMR_REG_SR;
593 value |= NETSEC_GMAC_OMR_REG_ST;
594
595 netsec_write_reg(priv, NETSEC_REG_NRM_RX_INTEN_CLR, ~0);
596 netsec_write_reg(priv, NETSEC_REG_NRM_TX_INTEN_CLR, ~0);
597
598 if (netsec_set_mac_reg(priv, GMAC_REG_OMR, value))
599 return -ETIMEDOUT;
600
601 return 0;
602}
603
604static int netsec_stop_gmac(struct netsec_priv *priv)
605{
606 u32 value;
607 int ret;
608
609 ret = netsec_get_mac_reg(priv, GMAC_REG_OMR, &value);
610 if (ret)
611 return ret;
612 value &= ~NETSEC_GMAC_OMR_REG_SR;
613 value &= ~NETSEC_GMAC_OMR_REG_ST;
614
615 /* disable all interrupts */
616 netsec_write_reg(priv, NETSEC_REG_NRM_RX_INTEN_CLR, ~0);
617 netsec_write_reg(priv, NETSEC_REG_NRM_TX_INTEN_CLR, ~0);
618
619 return netsec_set_mac_reg(priv, GMAC_REG_OMR, value);
620}
621
622static void netsec_spi_read(char *buf, loff_t len, loff_t offset)
623{
624 struct udevice *new;
625 struct spi_flash *flash;
626
627 spi_flash_probe_bus_cs(CONFIG_SF_DEFAULT_BUS, CONFIG_SF_DEFAULT_CS,
628 CONFIG_SF_DEFAULT_SPEED, CONFIG_SF_DEFAULT_MODE, &new);
629 flash = dev_get_uclass_priv(new);
630
631 spi_flash_read(flash, offset, len, buf);
632}
633
634static int netsec_read_rom_hwaddr(struct udevice *dev)
635{
636 struct netsec_priv *priv = dev_get_priv(dev);
637 struct eth_pdata *pdata = dev_get_plat(dev);
638 char macp[NOR_BLOCK];
639
640 netsec_spi_read(macp, sizeof(macp), priv->eeprom_base);
641
642 pdata->enetaddr[0] = readb(macp + 3);
643 pdata->enetaddr[1] = readb(macp + 2);
644 pdata->enetaddr[2] = readb(macp + 1);
645 pdata->enetaddr[3] = readb(macp + 0);
646 pdata->enetaddr[4] = readb(macp + 7);
647 pdata->enetaddr[5] = readb(macp + 6);
648 return 0;
649}
650
651static int netsec_send(struct udevice *dev, void *packet, int length)
652{
653 struct netsec_priv *priv = dev_get_priv(dev);
654 u32 val, tout;
655
656 val = netsec_read_reg(priv, NETSEC_REG_NRM_TX_STATUS);
657 netsec_cache_flush((uintptr_t)packet, length);
658 netsec_set_tx_de(priv, packet, length);
659 netsec_write_reg(priv, NETSEC_REG_NRM_TX_PKTCNT, 1); /* submit another tx */
660
661 val = netsec_read_reg(priv, NETSEC_REG_NRM_TX_PKTCNT);
662
663 tout = 10000;
664 do {
665 val = netsec_read_reg(priv, NETSEC_REG_NRM_TX_DONE_PKTCNT);
666 udelay(2);
667 } while (--tout && !val);
668
669 if (!tout) {
670 val = netsec_read_reg(priv, NETSEC_REG_NRM_TX_PKTCNT);
671 pr_err("%s: ETIMEDOUT: %dpackets\n", __func__, val);
672 return -ETIMEDOUT;
673 }
674
675 return 0;
676}
677
678static int netsec_free_packet(struct udevice *dev, uchar *packet, int length)
679{
680 struct netsec_priv *priv = dev_get_priv(dev);
681
682 netsec_set_rx_de(priv, priv->rxat, net_rx_packets[priv->rxat]);
683
684 priv->rxat++;
685 if (priv->rxat == PKTBUFSRX)
686 priv->rxat = 0;
687
688 return 0;
689}
690
691static int netsec_recv(struct udevice *dev, int flags, uchar **packetp)
692{
693 struct netsec_priv *priv = dev_get_priv(dev);
694 int idx = priv->rxat;
695 uchar *ptr = net_rx_packets[idx];
696 struct netsec_de *de = &priv->rxde[idx];
697 int length = 0;
698
699 netsec_cache_invalidate((uintptr_t)de, sizeof(*de));
700
701 if (de->attr & (1U << NETSEC_RX_PKT_OWN_FIELD))
702 return -EAGAIN;
703
704 length = de->buf_len_info >> 16;
705
706 /* invalidate after DMA is done */
707 netsec_cache_invalidate((uintptr_t)ptr, length);
708 *packetp = ptr;
709
710 return length;
711}
712
713static int _netsec_get_phy_reg(struct mii_dev *bus,
714 int phy_addr, int devad, int reg_addr)
715{
716 return netsec_get_phy_reg(bus->priv, phy_addr, reg_addr);
717}
718
719static int _netsec_set_phy_reg(struct mii_dev *bus,
720 int phy_addr, int devad, int reg_addr, u16 val)
721{
722 return netsec_set_phy_reg(bus->priv, phy_addr, reg_addr, val);
723}
724
725static int netsec_mdiobus_init(struct netsec_priv *priv, const char *name)
726{
727 struct mii_dev *bus = mdio_alloc();
728
729 if (!bus)
730 return -ENOMEM;
731
732 bus->read = _netsec_get_phy_reg;
733 bus->write = _netsec_set_phy_reg;
734 snprintf(bus->name, sizeof(bus->name), "%s", name);
735 bus->priv = priv;
736
737 return mdio_register(bus);
738}
739
740static int netsec_phy_init(struct netsec_priv *priv, void *dev)
741{
742 struct phy_device *phydev;
743 int ret;
744
745 phydev = phy_connect(priv->bus, priv->phy_addr, dev, priv->phy_mode);
746
747 phydev->supported &= PHY_GBIT_FEATURES;
748 if (priv->max_speed) {
749 ret = phy_set_supported(phydev, priv->max_speed);
750 if (ret)
751 return ret;
752 }
753 phydev->advertising = phydev->supported;
754
755 priv->phydev = phydev;
756 phy_config(phydev);
757
758 return 0;
759}
760
761static int netsec_netdev_load_ucode_region(struct netsec_priv *priv, u32 reg,
762 u32 addr_h, u32 addr_l, u32 size)
763{
764 u64 base = ((u64)addr_h << 32 | addr_l) - EERPROM_MAP_OFFSET;
765
766 while (size > 0) {
767 char buf[NOR_BLOCK];
768 u32 *ucode = (u32 *)buf;
769 u64 off;
770 int i;
771
772 off = base % NOR_BLOCK;
773 base -= off;
774 netsec_spi_read(buf, sizeof(buf), base);
775
776 for (i = off / 4; i < sizeof(buf) / 4 && size > 0; i++, size--)
777 netsec_write_reg(priv, reg, ucode[i]);
778 base += NOR_BLOCK;
779 }
780
781 return 0;
782}
783
784static int netsec_netdev_load_microcode(struct netsec_priv *priv)
785{
786 u32 addr_h, addr_l, size;
787 char buf[NOR_BLOCK];
788 u32 *ucinfo = (u32 *)buf;
789 int err;
790
791 netsec_spi_read(buf, sizeof(buf), priv->eeprom_base);
792
793 addr_h = ucinfo[NETSEC_EEPROM_HM_ME_ADDRESS_H >> 2];
794 addr_l = ucinfo[NETSEC_EEPROM_HM_ME_ADDRESS_L >> 2];
795 size = ucinfo[NETSEC_EEPROM_HM_ME_SIZE >> 2];
796
797 err = netsec_netdev_load_ucode_region(priv, NETSEC_REG_DMAC_HM_CMD_BUF,
798 addr_h, addr_l, size);
799 if (err)
800 return err;
801
802 addr_h = ucinfo[NETSEC_EEPROM_MH_ME_ADDRESS_H >> 2];
803 addr_l = ucinfo[NETSEC_EEPROM_MH_ME_ADDRESS_L >> 2];
804 size = ucinfo[NETSEC_EEPROM_MH_ME_SIZE >> 2];
805
806 err = netsec_netdev_load_ucode_region(priv, NETSEC_REG_DMAC_MH_CMD_BUF,
807 addr_h, addr_l, size);
808 if (err)
809 return err;
810
811 addr_h = 0;
812 addr_l = ucinfo[NETSEC_EEPROM_PKT_ME_ADDRESS >> 2];
813 size = ucinfo[NETSEC_EEPROM_PKT_ME_SIZE >> 2];
814
815 err = netsec_netdev_load_ucode_region(priv, NETSEC_REG_PKT_CMD_BUF,
816 addr_h, addr_l, size);
817 if (err)
818 return err;
819
820 return 0;
821}
822
823void netsec_pre_init_microengine(struct netsec_priv *priv)
824{
825 u32 data;
826
827 /* Remove dormant settings */
828 data = netsec_get_phy_reg(priv, priv->phy_addr, MII_BMCR);
829 data &= ~BMCR_PDOWN;
830 data |= BMCR_ISOLATE;
831 netsec_set_phy_reg(priv, priv->phy_addr, MII_BMCR, data);
832 mdelay(100);
833
834 /* Put phy in loopback mode to guarantee RXCLK input */
835 data |= BMCR_LOOPBACK;
836 netsec_set_phy_reg(priv, priv->phy_addr, MII_BMCR, data);
837 mdelay(100);
838}
839
840void netsec_post_init_microengine(struct netsec_priv *priv)
841{
842 u32 data;
843
844 /* Get phy back to normal operation */
845 data = netsec_get_phy_reg(priv, priv->phy_addr, MII_BMCR);
846 data &= ~BMCR_LOOPBACK;
847 netsec_set_phy_reg(priv, priv->phy_addr, MII_BMCR, data);
848 mdelay(100);
849
850 /* Apply software reset */
851 data |= BMCR_RESET;
852 netsec_set_phy_reg(priv, priv->phy_addr, MII_BMCR, data);
853 mdelay(100);
854}
855
856static int netsec_reset_hardware(struct netsec_priv *priv, bool load_ucode)
857{
858 u32 value;
859 int err;
860
861 netsec_write_reg(priv, NETSEC_REG_CLK_EN, 0x24);
862
863 /* stop DMA engines */
864 if (!netsec_read_reg(priv, NETSEC_REG_ADDR_DIS_CORE)) {
865 netsec_write_reg(priv, NETSEC_REG_DMA_HM_CTRL,
866 NETSEC_DMA_CTRL_REG_STOP);
867 netsec_write_reg(priv, NETSEC_REG_DMA_MH_CTRL,
868 NETSEC_DMA_CTRL_REG_STOP);
869
870 value = 100;
871 while (netsec_read_reg(priv, NETSEC_REG_DMA_HM_CTRL) &
872 NETSEC_DMA_CTRL_REG_STOP) {
873 udelay(1000);
874 if (--value == 0) {
875 pr_err("%s:%d timeout!\n", __func__, __LINE__);
876 break;
877 }
878 }
879
880 value = 100;
881 while (netsec_read_reg(priv, NETSEC_REG_DMA_MH_CTRL) &
882 NETSEC_DMA_CTRL_REG_STOP) {
883 udelay(1000);
884 if (--value == 0) {
885 pr_err("%s:%d timeout!\n", __func__, __LINE__);
886 break;
887 }
888 }
889 }
890
891 netsec_set_mac_reg(priv, GMAC_REG_BMR, NETSEC_GMAC_BMR_REG_RESET);
892
893 netsec_write_reg(priv, NETSEC_REG_SOFT_RST, NETSEC_SOFT_RST_REG_RESET);
894 netsec_write_reg(priv, NETSEC_REG_SOFT_RST, NETSEC_SOFT_RST_REG_RUN);
895 netsec_write_reg(priv, NETSEC_REG_COM_INIT, NETSEC_COM_INIT_REG_ALL);
896
897 value = 100;
898 while (netsec_read_reg(priv, NETSEC_REG_COM_INIT) != 0) {
899 udelay(1000);
900 if (--value == 0) {
901 pr_err("%s:%d COM_INIT timeout!\n", __func__, __LINE__);
902 break;
903 }
904 }
905
906 /* MAC desc init */
907 netsec_write_reg(priv, MAC_REG_DESC_INIT, 1);
908 netsec_wait_while_busy(priv, MAC_REG_DESC_INIT, 1);
909 /* set MAC_INTF_SEL */
910 netsec_write_reg(priv, MAC_REG_INTF_SEL, 1);
911
912 netsec_write_reg(priv, NETSEC_REG_CLK_EN, 1 << 5);
913
914 /* set desc_start addr */
915 netsec_write_reg(priv, NETSEC_REG_NRM_RX_DESC_START_UP,
916 upper_32_bits((dma_addr_t)priv->rxde));
917 netsec_write_reg(priv, NETSEC_REG_NRM_RX_DESC_START_LW,
918 lower_32_bits((dma_addr_t)priv->rxde));
919
920 netsec_write_reg(priv, NETSEC_REG_NRM_TX_DESC_START_UP,
921 upper_32_bits((dma_addr_t)priv->txde));
922 netsec_write_reg(priv, NETSEC_REG_NRM_TX_DESC_START_LW,
923 lower_32_bits((dma_addr_t)priv->txde));
924
925 /* set normal tx dring ring config */
926 netsec_write_reg(priv, NETSEC_REG_NRM_TX_CONFIG,
927 1 << NETSEC_REG_DESC_ENDIAN);
928 netsec_write_reg(priv, NETSEC_REG_NRM_RX_CONFIG,
929 1 << NETSEC_REG_DESC_ENDIAN);
930
931 if (load_ucode) {
932 err = netsec_netdev_load_microcode(priv);
933 if (err) {
934 pr_err("%s: failed to load microcode (%d)\n",
935 __func__, err);
936 return err;
937 }
938 }
939
940 /* set desc_start addr */
941 netsec_write_reg(priv, NETSEC_REG_NRM_RX_DESC_START_UP,
942 upper_32_bits((dma_addr_t)priv->rxde));
943 netsec_write_reg(priv, NETSEC_REG_NRM_RX_DESC_START_LW,
944 lower_32_bits((dma_addr_t)priv->rxde));
945
946 netsec_write_reg(priv, NETSEC_REG_NRM_TX_DESC_START_UP,
947 upper_32_bits((dma_addr_t)priv->txde));
948 netsec_write_reg(priv, NETSEC_REG_NRM_TX_DESC_START_LW,
949 lower_32_bits((dma_addr_t)priv->txde));
950
951 netsec_write_reg(priv, NETSEC_REG_CLK_EN, 1 << 5);
952
953 /* start DMA engines */
954 netsec_write_reg(priv, NETSEC_REG_DMA_TMR_CTRL, priv->freq / 1000000 - 1);
955
956 netsec_pre_init_microengine(priv);
957
958 netsec_write_reg(priv, NETSEC_REG_ADDR_DIS_CORE, 0);
959
960 mdelay(100);
961
962 if (!(netsec_read_reg(priv, NETSEC_REG_TOP_STATUS) &
963 NETSEC_TOP_IRQ_REG_ME_START)) {
964 pr_err("microengine start failed\n");
965 return -ENXIO;
966 }
967
968 netsec_post_init_microengine(priv);
969
970 /* clear microcode load end status */
971 netsec_write_reg(priv, NETSEC_REG_TOP_STATUS,
972 NETSEC_TOP_IRQ_REG_ME_START);
973
974 netsec_write_reg(priv, NETSEC_REG_CLK_EN, 1 << 5);
975
976 value = netsec_read_reg(priv, NETSEC_REG_PKT_CTRL);
977 value |= NETSEC_PKT_CTRL_REG_MODE_NRM;
978 /* change to normal mode */
979 netsec_write_reg(priv, NETSEC_REG_DMA_MH_CTRL, MH_CTRL__MODE_TRANS);
980 netsec_write_reg(priv, NETSEC_REG_PKT_CTRL, value);
981
982 value = 100;
983 while ((netsec_read_reg(priv, NETSEC_REG_MODE_TRANS_COMP_STATUS) &
984 NETSEC_MODE_TRANS_COMP_IRQ_T2N) == 0) {
985 udelay(1000);
986 if (--value == 0) {
987 value = netsec_read_reg(priv, NETSEC_REG_MODE_TRANS_COMP_STATUS);
988 pr_err("%s:%d timeout! val=%x\n", __func__, __LINE__, value);
989 break;
990 }
991 }
992
993 /* clear any pending EMPTY/ERR irq status */
994 netsec_write_reg(priv, NETSEC_REG_NRM_TX_STATUS, ~0);
995
996 /* Disable TX & RX intr */
997 netsec_write_reg(priv, NETSEC_REG_INTEN_CLR, ~0);
998
999 return 0;
1000}
1001
1002static void netsec_stop(struct udevice *dev)
1003{
1004 struct netsec_priv *priv = dev_get_priv(dev);
1005
1006 netsec_write_reg(priv, NETSEC_REG_ADDR_DIS_CORE, 7);
1007 netsec_stop_gmac(priv);
1008 phy_shutdown(priv->phydev);
1009 netsec_reset_hardware(priv, false);
1010}
1011
1012static int netsec_start(struct udevice *dev)
1013{
1014 struct netsec_priv *priv = dev_get_priv(dev);
1015 int i;
1016
1017 phy_startup(priv->phydev);
1018 netsec_start_gmac(priv);
1019
1020 priv->rxat = 0;
1021 for (i = 0; i < PKTBUFSRX; i++)
1022 netsec_set_rx_de(priv, i, net_rx_packets[i]);
1023
1024 return 0;
1025}
1026
1027static int netsec_of_to_plat(struct udevice *dev)
1028{
1029 struct eth_pdata *pdata = dev_get_plat(dev);
1030 struct netsec_priv *priv = dev_get_priv(dev);
1031 struct ofnode_phandle_args phandle_args;
1032 const char *phy_mode;
1033
1034 pdata->iobase = dev_read_addr_index(dev, 0);
1035 priv->eeprom_base = dev_read_addr_index(dev, 1) - EERPROM_MAP_OFFSET;
1036
1037 pdata->phy_interface = -1;
1038 phy_mode = dev_read_prop(dev, "phy-mode", NULL);
1039 if (phy_mode)
1040 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
1041 if (pdata->phy_interface == -1) {
1042 pr_err("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
1043 return -EINVAL;
1044 }
1045
1046 if (!dev_read_phandle_with_args(dev, "phy-handle", NULL, 0, 0,
1047 &phandle_args))
1048 priv->phy_addr = ofnode_read_u32_default(phandle_args.node, "reg", 7);
1049 else
1050 priv->phy_addr = 7;
1051
1052 pdata->max_speed = dev_read_u32_default(dev, "max-speed", SPEED_1000);
1053
1054 priv->ioaddr = pdata->iobase;
1055 priv->phy_mode = pdata->phy_interface;
1056 priv->max_speed = pdata->max_speed;
1057 priv->freq = 250000000UL;
1058
1059 return 0;
1060}
1061
Jassi Brar29c58b02021-06-04 18:44:38 +09001062static int netsec_probe(struct udevice *dev)
1063{
1064 struct netsec_priv *priv = dev_get_priv(dev);
1065 int ret;
1066
Jassi Brar29c58b02021-06-04 18:44:38 +09001067 netsec_reset_hardware(priv, true);
1068
1069 ret = netsec_mdiobus_init(priv, dev->name);
1070 if (ret) {
1071 pr_err("Failed to initialize mdiobus: %d\n", ret);
1072 return ret;
1073 }
1074
1075 priv->bus = miiphy_get_dev_by_name(dev->name);
1076
1077 ret = netsec_phy_init(priv, dev);
1078 if (ret) {
1079 pr_err("Failed to initialize phy: %d\n", ret);
1080 goto out_mdiobus_release;
1081 }
1082
1083 return 0;
1084out_mdiobus_release:
1085 mdio_unregister(priv->bus);
1086 mdio_free(priv->bus);
1087 return ret;
1088}
1089
1090static int netsec_remove(struct udevice *dev)
1091{
1092 struct netsec_priv *priv = dev_get_priv(dev);
1093
1094 free(priv->phydev);
1095 mdio_unregister(priv->bus);
1096 mdio_free(priv->bus);
1097
1098 return 0;
1099}
1100
1101static const struct eth_ops netsec_ops = {
1102 .start = netsec_start,
1103 .stop = netsec_stop,
1104 .send = netsec_send,
1105 .recv = netsec_recv,
1106 .free_pkt = netsec_free_packet,
1107 .read_rom_hwaddr = netsec_read_rom_hwaddr,
1108};
1109
1110static const struct udevice_id netsec_ids[] = {
1111 {
1112 .compatible = "socionext,synquacer-netsec",
1113 },
1114 {}
1115};
1116
1117U_BOOT_DRIVER(ave) = {
1118 .name = "synquacer_netsec",
1119 .id = UCLASS_ETH,
1120 .of_match = netsec_ids,
1121 .probe = netsec_probe,
1122 .remove = netsec_remove,
1123 .of_to_plat = netsec_of_to_plat,
1124 .ops = &netsec_ops,
1125 .priv_auto = sizeof(struct netsec_priv),
1126 .plat_auto = sizeof(struct eth_pdata),
1127};