1 // SPDX-License-Identifier: GPL-2.0+
3 * netsec.c - Socionext Synquacer Netsec driver
4 * Copyright 2021 Linaro Ltd.
10 #include <fdt_support.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>
25 #include <linux/iopoll.h>
27 #include <spi_flash.h>
29 #define NETSEC_REG_SOFT_RST 0x104
30 #define NETSEC_REG_COM_INIT 0x120
32 #define NETSEC_REG_TOP_STATUS 0x200
33 #define NETSEC_IRQ_RX BIT(1)
34 #define NETSEC_IRQ_TX BIT(0)
36 #define NETSEC_REG_TOP_INTEN 0x204
37 #define NETSEC_REG_INTEN_SET 0x234
38 #define NETSEC_REG_INTEN_CLR 0x238
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)
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)
57 #define NETSEC_REG_PKT_CMD_BUF 0xd0
59 #define NETSEC_REG_CLK_EN 0x100
61 #define NETSEC_REG_PKT_CTRL 0x140
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
72 #define NETSEC_REG_NRM_TX_PKTCNT 0x410
74 #define NETSEC_REG_NRM_TX_DONE_PKTCNT 0x414
75 #define NETSEC_REG_NRM_TX_DONE_TXINT_PKTCNT 0x418
77 #define NETSEC_REG_NRM_TX_TMR 0x41c
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
84 #define NETSEC_REG_NRM_RX_TMR 0x45c
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
91 #define NETSEC_REG_NRM_TX_CONFIG 0x430
92 #define NETSEC_REG_NRM_RX_CONFIG 0x470
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
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
113 #define MHZ(n) ((n) * 1000 * 1000)
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
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
138 #define NETSEC_RX_PKT_ERR_MASK 3
140 #define NETSEC_MAX_TX_PKT_LEN 1518
141 #define NETSEC_MAX_TX_JUMBO_PKT_LEN 9018
143 #define NETSEC_RING_GMAC 15
144 #define NETSEC_RING_MAX 2
146 #define NETSEC_TCP_SEG_LEN_MAX 1460
147 #define NETSEC_TCP_JUMBO_SEG_LEN_MAX 8960
149 #define NETSEC_RX_CKSUM_NOTAVAIL 0
150 #define NETSEC_RX_CKSUM_OK 1
151 #define NETSEC_RX_CKSUM_NG 2
153 #define NETSEC_TOP_IRQ_REG_ME_START BIT(20)
154 #define NETSEC_IRQ_TRANSITION_COMPLETE BIT(4)
156 #define NETSEC_MODE_TRANS_COMP_IRQ_N2T BIT(20)
157 #define NETSEC_MODE_TRANS_COMP_IRQ_T2N BIT(19)
159 #define NETSEC_INT_PKTCNT_MAX 2047
161 #define NETSEC_FLOW_START_TH_MAX 95
162 #define NETSEC_FLOW_STOP_TH_MAX 95
163 #define NETSEC_FLOW_PAUSE_TIME_MIN 5
165 #define NETSEC_CLK_EN_REG_DOM_ALL 0x3f
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)
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)
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)
183 #define NETSEC_SOFT_RST_REG_RESET 0
184 #define NETSEC_SOFT_RST_REG_RUN BIT(31)
186 #define NETSEC_DMA_CTRL_REG_STOP 1
187 #define MH_CTRL__MODE_TRANS BIT(20)
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)
193 #define NETSEC_GMAC_BMR_REG_COMMON 0x00412080
194 #define NETSEC_GMAC_BMR_REG_RESET 0x00020181
195 #define NETSEC_GMAC_BMR_REG_SWR 0x00000001
197 #define NETSEC_GMAC_OMR_REG_ST BIT(13)
198 #define NETSEC_GMAC_OMR_REG_SR BIT(1)
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
208 #define NETSEC_FCR_RFE BIT(2)
209 #define NETSEC_FCR_TFE BIT(1)
211 #define NETSEC_GMAC_GAR_REG_GW BIT(1)
212 #define NETSEC_GMAC_GAR_REG_GB BIT(0)
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
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
225 #define NETSEC_GMAC_RDLAR_REG_COMMON 0x18000
226 #define NETSEC_GMAC_TDLAR_REG_COMMON 0x1c000
228 #define NETSEC_REG_NETSEC_VER_F_TAIKI 0x50000
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
235 #define NETSEC_MAC_DESC_SOFT_RST_SOFT_RST 1
236 #define NETSEC_MAC_DESC_INIT_REG_INIT 1
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
248 #define DESC_SZ sizeof(struct netsec_de)
250 #define NETSEC_F_NETSEC_VER_MAJOR_NUM(x) ((x) & 0xffff0000)
252 #define EERPROM_MAP_OFFSET 0x8000000
253 #define NOR_BLOCK 1024
255 struct netsec_de { /* Netsec Descriptor layout */
257 u32 data_buf_addr_up;
258 u32 data_buf_addr_lw;
263 struct netsec_de rxde[PKTBUFSRX];
264 struct netsec_de txde[1];
267 phys_addr_t eeprom_base;
271 struct phy_device *phydev;
277 struct netsec_tx_pkt_ctrl {
279 bool tcp_seg_offload_flag;
280 bool cksum_offload_flag;
283 struct netsec_rx_pkt_info {
289 static void netsec_write_reg(struct netsec_priv *priv, u32 reg_addr, u32 val)
291 writel(val, priv->ioaddr + reg_addr);
294 static u32 netsec_read_reg(struct netsec_priv *priv, u32 reg_addr)
296 return readl(priv->ioaddr + reg_addr);
299 /************* MDIO BUS OPS FOLLOW *************/
301 #define TIMEOUT_SPINS_MAC 1000
302 #define TIMEOUT_SECONDARY_MS_MAC 100
304 static u32 netsec_clk_type(u32 freq)
307 return NETSEC_GMAC_GAR_REG_CR_25_35_MHZ;
309 return NETSEC_GMAC_GAR_REG_CR_35_60_MHZ;
311 return NETSEC_GMAC_GAR_REG_CR_60_100_MHZ;
313 return NETSEC_GMAC_GAR_REG_CR_100_150_MHZ;
315 return NETSEC_GMAC_GAR_REG_CR_150_250_MHZ;
317 return NETSEC_GMAC_GAR_REG_CR_250_300_MHZ;
320 static int netsec_wait_while_busy(struct netsec_priv *priv, u32 addr, u32 mask)
322 u32 timeout = TIMEOUT_SPINS_MAC;
324 while (--timeout && netsec_read_reg(priv, addr) & mask)
329 timeout = TIMEOUT_SECONDARY_MS_MAC;
330 while (--timeout && netsec_read_reg(priv, addr) & mask)
336 pr_err("%s: timeout\n", __func__);
341 static int netsec_set_mac_reg(struct netsec_priv *priv, u32 addr, u32 value)
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);
349 static int netsec_get_mac_reg(struct netsec_priv *priv, u32 addr, u32 *read)
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);
359 *read = netsec_read_reg(priv, MAC_REG_DATA);
364 static int netsec_mac_wait_while_busy(struct netsec_priv *priv,
367 u32 timeout = TIMEOUT_SPINS_MAC;
372 ret = netsec_get_mac_reg(priv, addr, &data);
376 } while (--timeout && (data & mask));
381 timeout = TIMEOUT_SECONDARY_MS_MAC;
385 ret = netsec_get_mac_reg(priv, addr, &data);
389 } while (--timeout && (data & mask));
397 static void netsec_cache_invalidate(uintptr_t vaddr, int len)
399 invalidate_dcache_range(rounddown(vaddr, ARCH_DMA_MINALIGN),
400 roundup(vaddr + len, ARCH_DMA_MINALIGN));
403 static void netsec_cache_flush(uintptr_t vaddr, int len)
405 flush_dcache_range(rounddown(vaddr, ARCH_DMA_MINALIGN),
406 roundup(vaddr + len, ARCH_DMA_MINALIGN));
409 static void netsec_set_rx_de(struct netsec_priv *priv, u16 idx, void *addr)
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);
416 if (idx == PKTBUFSRX - 1)
417 attr |= (1 << NETSEC_RX_PKT_LD_FIELD);
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;
424 netsec_cache_flush((uintptr_t)de, sizeof(*de));
427 static void netsec_set_tx_de(struct netsec_priv *priv, void *addr, int len)
429 struct netsec_de *de = &priv->txde[0];
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);
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;
445 netsec_cache_flush((uintptr_t)de, sizeof(*de));
448 static int netsec_get_phy_reg(struct netsec_priv *priv,
449 int phy_addr, int reg_addr)
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)))
464 ret = netsec_mac_wait_while_busy(priv, GMAC_REG_GAR,
465 NETSEC_GMAC_GAR_REG_GB);
469 ret = netsec_get_mac_reg(priv, GMAC_REG_GDR, &data);
476 static int netsec_set_phy_reg(struct netsec_priv *priv,
477 int phy_addr, int reg_addr, u16 val)
483 if (netsec_set_mac_reg(priv, GMAC_REG_GDR, val))
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)))
494 ret = netsec_mac_wait_while_busy(priv, GMAC_REG_GAR,
495 NETSEC_GMAC_GAR_REG_GB);
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.
504 netsec_get_phy_reg(priv, phy_addr, MII_PHYSID1);
509 static int netsec_mac_update_to_phy_state(struct netsec_priv *priv)
511 struct phy_device *phydev = priv->phydev;
514 value = phydev->duplex ? NETSEC_GMAC_MCR_REG_FULL_DUPLEX_COMMON :
515 NETSEC_GMAC_MCR_REG_HALF_DUPLEX_COMMON;
517 if (phydev->speed != SPEED_1000)
518 value |= NETSEC_MCR_PS;
520 if (phydev->interface != PHY_INTERFACE_MODE_GMII &&
521 phydev->speed == SPEED_100)
522 value |= NETSEC_GMAC_MCR_REG_FES;
524 value |= NETSEC_GMAC_MCR_REG_CST | NETSEC_GMAC_MCR_REG_JE;
526 if (phy_interface_is_rgmii(phydev))
527 value |= NETSEC_GMAC_MCR_REG_IBN;
529 if (netsec_set_mac_reg(priv, GMAC_REG_MCR, value))
535 static int netsec_start_gmac(struct netsec_priv *priv)
540 if (priv->max_speed != SPEED_1000)
541 value = (NETSEC_GMAC_MCR_REG_CST |
542 NETSEC_GMAC_MCR_REG_HALF_DUPLEX_COMMON);
544 if (netsec_set_mac_reg(priv, GMAC_REG_MCR, value))
547 if (netsec_set_mac_reg(priv, GMAC_REG_BMR,
548 NETSEC_GMAC_BMR_REG_RESET))
551 /* Wait soft reset */
554 ret = netsec_get_mac_reg(priv, GMAC_REG_BMR, &value);
558 if (value & NETSEC_GMAC_BMR_REG_SWR)
561 netsec_write_reg(priv, MAC_REG_DESC_SOFT_RST, 1);
562 if (netsec_wait_while_busy(priv, MAC_REG_DESC_SOFT_RST, 1))
565 netsec_write_reg(priv, MAC_REG_DESC_INIT, 1);
566 if (netsec_wait_while_busy(priv, MAC_REG_DESC_INIT, 1))
569 if (netsec_set_mac_reg(priv, GMAC_REG_BMR,
570 NETSEC_GMAC_BMR_REG_COMMON))
573 if (netsec_set_mac_reg(priv, GMAC_REG_RDLAR,
574 NETSEC_GMAC_RDLAR_REG_COMMON))
577 if (netsec_set_mac_reg(priv, GMAC_REG_TDLAR,
578 NETSEC_GMAC_TDLAR_REG_COMMON))
581 if (netsec_set_mac_reg(priv, GMAC_REG_MFFR, 0x80000001))
584 ret = netsec_mac_update_to_phy_state(priv);
588 ret = netsec_get_mac_reg(priv, GMAC_REG_OMR, &value);
592 value |= NETSEC_GMAC_OMR_REG_SR;
593 value |= NETSEC_GMAC_OMR_REG_ST;
595 netsec_write_reg(priv, NETSEC_REG_NRM_RX_INTEN_CLR, ~0);
596 netsec_write_reg(priv, NETSEC_REG_NRM_TX_INTEN_CLR, ~0);
598 if (netsec_set_mac_reg(priv, GMAC_REG_OMR, value))
604 static int netsec_stop_gmac(struct netsec_priv *priv)
609 ret = netsec_get_mac_reg(priv, GMAC_REG_OMR, &value);
612 value &= ~NETSEC_GMAC_OMR_REG_SR;
613 value &= ~NETSEC_GMAC_OMR_REG_ST;
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);
619 return netsec_set_mac_reg(priv, GMAC_REG_OMR, value);
622 static void netsec_spi_read(char *buf, loff_t len, loff_t offset)
624 struct spi_flash *flash;
626 flash = spi_flash_probe(CONFIG_SF_DEFAULT_BUS, CONFIG_SF_DEFAULT_CS,
627 CONFIG_SF_DEFAULT_SPEED, CONFIG_SF_DEFAULT_MODE);
629 spi_flash_read(flash, offset, len, buf);
632 static int netsec_read_rom_hwaddr(struct udevice *dev)
634 struct netsec_priv *priv = dev_get_priv(dev);
635 struct eth_pdata *pdata = dev_get_plat(dev);
636 char macp[NOR_BLOCK];
638 netsec_spi_read(macp, sizeof(macp), priv->eeprom_base);
640 pdata->enetaddr[0] = readb(macp + 3);
641 pdata->enetaddr[1] = readb(macp + 2);
642 pdata->enetaddr[2] = readb(macp + 1);
643 pdata->enetaddr[3] = readb(macp + 0);
644 pdata->enetaddr[4] = readb(macp + 7);
645 pdata->enetaddr[5] = readb(macp + 6);
649 static int netsec_send(struct udevice *dev, void *packet, int length)
651 struct netsec_priv *priv = dev_get_priv(dev);
654 val = netsec_read_reg(priv, NETSEC_REG_NRM_TX_STATUS);
655 netsec_cache_flush((uintptr_t)packet, length);
656 netsec_set_tx_de(priv, packet, length);
657 netsec_write_reg(priv, NETSEC_REG_NRM_TX_PKTCNT, 1); /* submit another tx */
659 val = netsec_read_reg(priv, NETSEC_REG_NRM_TX_PKTCNT);
663 val = netsec_read_reg(priv, NETSEC_REG_NRM_TX_DONE_PKTCNT);
665 } while (--tout && !val);
668 val = netsec_read_reg(priv, NETSEC_REG_NRM_TX_PKTCNT);
669 pr_err("%s: ETIMEDOUT: %dpackets\n", __func__, val);
676 static int netsec_free_packet(struct udevice *dev, uchar *packet, int length)
678 struct netsec_priv *priv = dev_get_priv(dev);
680 netsec_set_rx_de(priv, priv->rxat, net_rx_packets[priv->rxat]);
683 if (priv->rxat == PKTBUFSRX)
689 static int netsec_recv(struct udevice *dev, int flags, uchar **packetp)
691 struct netsec_priv *priv = dev_get_priv(dev);
692 int idx = priv->rxat;
693 uchar *ptr = net_rx_packets[idx];
694 struct netsec_de *de = &priv->rxde[idx];
697 netsec_cache_invalidate((uintptr_t)de, sizeof(*de));
699 if (de->attr & (1U << NETSEC_RX_PKT_OWN_FIELD))
702 length = de->buf_len_info >> 16;
704 /* invalidate after DMA is done */
705 netsec_cache_invalidate((uintptr_t)ptr, length);
711 static int _netsec_get_phy_reg(struct mii_dev *bus,
712 int phy_addr, int devad, int reg_addr)
714 return netsec_get_phy_reg(bus->priv, phy_addr, reg_addr);
717 static int _netsec_set_phy_reg(struct mii_dev *bus,
718 int phy_addr, int devad, int reg_addr, u16 val)
720 return netsec_set_phy_reg(bus->priv, phy_addr, reg_addr, val);
723 static int netsec_mdiobus_init(struct netsec_priv *priv, const char *name)
725 struct mii_dev *bus = mdio_alloc();
730 bus->read = _netsec_get_phy_reg;
731 bus->write = _netsec_set_phy_reg;
732 snprintf(bus->name, sizeof(bus->name), "%s", name);
735 return mdio_register(bus);
738 static int netsec_phy_init(struct netsec_priv *priv, void *dev)
740 struct phy_device *phydev;
743 phydev = phy_connect(priv->bus, priv->phy_addr, dev, priv->phy_mode);
745 phydev->supported &= PHY_GBIT_FEATURES;
746 if (priv->max_speed) {
747 ret = phy_set_supported(phydev, priv->max_speed);
751 phydev->advertising = phydev->supported;
753 priv->phydev = phydev;
759 static int netsec_netdev_load_ucode_region(struct netsec_priv *priv, u32 reg,
760 u32 addr_h, u32 addr_l, u32 size)
762 u64 base = ((u64)addr_h << 32 | addr_l) - EERPROM_MAP_OFFSET;
766 u32 *ucode = (u32 *)buf;
770 off = base % NOR_BLOCK;
772 netsec_spi_read(buf, sizeof(buf), base);
774 for (i = off / 4; i < sizeof(buf) / 4 && size > 0; i++, size--)
775 netsec_write_reg(priv, reg, ucode[i]);
782 static int netsec_netdev_load_microcode(struct netsec_priv *priv)
784 u32 addr_h, addr_l, size;
786 u32 *ucinfo = (u32 *)buf;
789 netsec_spi_read(buf, sizeof(buf), priv->eeprom_base);
791 addr_h = ucinfo[NETSEC_EEPROM_HM_ME_ADDRESS_H >> 2];
792 addr_l = ucinfo[NETSEC_EEPROM_HM_ME_ADDRESS_L >> 2];
793 size = ucinfo[NETSEC_EEPROM_HM_ME_SIZE >> 2];
795 err = netsec_netdev_load_ucode_region(priv, NETSEC_REG_DMAC_HM_CMD_BUF,
796 addr_h, addr_l, size);
800 addr_h = ucinfo[NETSEC_EEPROM_MH_ME_ADDRESS_H >> 2];
801 addr_l = ucinfo[NETSEC_EEPROM_MH_ME_ADDRESS_L >> 2];
802 size = ucinfo[NETSEC_EEPROM_MH_ME_SIZE >> 2];
804 err = netsec_netdev_load_ucode_region(priv, NETSEC_REG_DMAC_MH_CMD_BUF,
805 addr_h, addr_l, size);
810 addr_l = ucinfo[NETSEC_EEPROM_PKT_ME_ADDRESS >> 2];
811 size = ucinfo[NETSEC_EEPROM_PKT_ME_SIZE >> 2];
813 err = netsec_netdev_load_ucode_region(priv, NETSEC_REG_PKT_CMD_BUF,
814 addr_h, addr_l, size);
821 void netsec_pre_init_microengine(struct netsec_priv *priv)
825 /* Remove dormant settings */
826 data = netsec_get_phy_reg(priv, priv->phy_addr, MII_BMCR);
828 data |= BMCR_ISOLATE;
829 netsec_set_phy_reg(priv, priv->phy_addr, MII_BMCR, data);
832 /* Put phy in loopback mode to guarantee RXCLK input */
833 data |= BMCR_LOOPBACK;
834 netsec_set_phy_reg(priv, priv->phy_addr, MII_BMCR, data);
838 void netsec_post_init_microengine(struct netsec_priv *priv)
842 /* Get phy back to normal operation */
843 data = netsec_get_phy_reg(priv, priv->phy_addr, MII_BMCR);
844 data &= ~BMCR_LOOPBACK;
845 netsec_set_phy_reg(priv, priv->phy_addr, MII_BMCR, data);
848 /* Apply software reset */
850 netsec_set_phy_reg(priv, priv->phy_addr, MII_BMCR, data);
854 static int netsec_reset_hardware(struct netsec_priv *priv, bool load_ucode)
859 netsec_write_reg(priv, NETSEC_REG_CLK_EN, 0x24);
861 /* stop DMA engines */
862 if (!netsec_read_reg(priv, NETSEC_REG_ADDR_DIS_CORE)) {
863 netsec_write_reg(priv, NETSEC_REG_DMA_HM_CTRL,
864 NETSEC_DMA_CTRL_REG_STOP);
865 netsec_write_reg(priv, NETSEC_REG_DMA_MH_CTRL,
866 NETSEC_DMA_CTRL_REG_STOP);
869 while (netsec_read_reg(priv, NETSEC_REG_DMA_HM_CTRL) &
870 NETSEC_DMA_CTRL_REG_STOP) {
873 pr_err("%s:%d timeout!\n", __func__, __LINE__);
879 while (netsec_read_reg(priv, NETSEC_REG_DMA_MH_CTRL) &
880 NETSEC_DMA_CTRL_REG_STOP) {
883 pr_err("%s:%d timeout!\n", __func__, __LINE__);
889 netsec_set_mac_reg(priv, GMAC_REG_BMR, NETSEC_GMAC_BMR_REG_RESET);
891 netsec_write_reg(priv, NETSEC_REG_SOFT_RST, NETSEC_SOFT_RST_REG_RESET);
892 netsec_write_reg(priv, NETSEC_REG_SOFT_RST, NETSEC_SOFT_RST_REG_RUN);
893 netsec_write_reg(priv, NETSEC_REG_COM_INIT, NETSEC_COM_INIT_REG_ALL);
896 while (netsec_read_reg(priv, NETSEC_REG_COM_INIT) != 0) {
899 pr_err("%s:%d COM_INIT timeout!\n", __func__, __LINE__);
905 netsec_write_reg(priv, MAC_REG_DESC_INIT, 1);
906 netsec_wait_while_busy(priv, MAC_REG_DESC_INIT, 1);
907 /* set MAC_INTF_SEL */
908 netsec_write_reg(priv, MAC_REG_INTF_SEL, 1);
910 netsec_write_reg(priv, NETSEC_REG_CLK_EN, 1 << 5);
912 /* set desc_start addr */
913 netsec_write_reg(priv, NETSEC_REG_NRM_RX_DESC_START_UP,
914 upper_32_bits((dma_addr_t)priv->rxde));
915 netsec_write_reg(priv, NETSEC_REG_NRM_RX_DESC_START_LW,
916 lower_32_bits((dma_addr_t)priv->rxde));
918 netsec_write_reg(priv, NETSEC_REG_NRM_TX_DESC_START_UP,
919 upper_32_bits((dma_addr_t)priv->txde));
920 netsec_write_reg(priv, NETSEC_REG_NRM_TX_DESC_START_LW,
921 lower_32_bits((dma_addr_t)priv->txde));
923 /* set normal tx dring ring config */
924 netsec_write_reg(priv, NETSEC_REG_NRM_TX_CONFIG,
925 1 << NETSEC_REG_DESC_ENDIAN);
926 netsec_write_reg(priv, NETSEC_REG_NRM_RX_CONFIG,
927 1 << NETSEC_REG_DESC_ENDIAN);
930 err = netsec_netdev_load_microcode(priv);
932 pr_err("%s: failed to load microcode (%d)\n",
938 /* set desc_start addr */
939 netsec_write_reg(priv, NETSEC_REG_NRM_RX_DESC_START_UP,
940 upper_32_bits((dma_addr_t)priv->rxde));
941 netsec_write_reg(priv, NETSEC_REG_NRM_RX_DESC_START_LW,
942 lower_32_bits((dma_addr_t)priv->rxde));
944 netsec_write_reg(priv, NETSEC_REG_NRM_TX_DESC_START_UP,
945 upper_32_bits((dma_addr_t)priv->txde));
946 netsec_write_reg(priv, NETSEC_REG_NRM_TX_DESC_START_LW,
947 lower_32_bits((dma_addr_t)priv->txde));
949 netsec_write_reg(priv, NETSEC_REG_CLK_EN, 1 << 5);
951 /* start DMA engines */
952 netsec_write_reg(priv, NETSEC_REG_DMA_TMR_CTRL, priv->freq / 1000000 - 1);
954 netsec_pre_init_microengine(priv);
956 netsec_write_reg(priv, NETSEC_REG_ADDR_DIS_CORE, 0);
960 if (!(netsec_read_reg(priv, NETSEC_REG_TOP_STATUS) &
961 NETSEC_TOP_IRQ_REG_ME_START)) {
962 pr_err("microengine start failed\n");
966 netsec_post_init_microengine(priv);
968 /* clear microcode load end status */
969 netsec_write_reg(priv, NETSEC_REG_TOP_STATUS,
970 NETSEC_TOP_IRQ_REG_ME_START);
972 netsec_write_reg(priv, NETSEC_REG_CLK_EN, 1 << 5);
974 value = netsec_read_reg(priv, NETSEC_REG_PKT_CTRL);
975 value |= NETSEC_PKT_CTRL_REG_MODE_NRM;
976 /* change to normal mode */
977 netsec_write_reg(priv, NETSEC_REG_DMA_MH_CTRL, MH_CTRL__MODE_TRANS);
978 netsec_write_reg(priv, NETSEC_REG_PKT_CTRL, value);
981 while ((netsec_read_reg(priv, NETSEC_REG_MODE_TRANS_COMP_STATUS) &
982 NETSEC_MODE_TRANS_COMP_IRQ_T2N) == 0) {
985 value = netsec_read_reg(priv, NETSEC_REG_MODE_TRANS_COMP_STATUS);
986 pr_err("%s:%d timeout! val=%x\n", __func__, __LINE__, value);
991 /* clear any pending EMPTY/ERR irq status */
992 netsec_write_reg(priv, NETSEC_REG_NRM_TX_STATUS, ~0);
994 /* Disable TX & RX intr */
995 netsec_write_reg(priv, NETSEC_REG_INTEN_CLR, ~0);
1000 static void netsec_stop(struct udevice *dev)
1002 struct netsec_priv *priv = dev_get_priv(dev);
1004 netsec_write_reg(priv, NETSEC_REG_ADDR_DIS_CORE, 7);
1005 netsec_stop_gmac(priv);
1006 phy_shutdown(priv->phydev);
1007 netsec_reset_hardware(priv, false);
1010 static int netsec_start(struct udevice *dev)
1012 struct netsec_priv *priv = dev_get_priv(dev);
1015 phy_startup(priv->phydev);
1016 netsec_start_gmac(priv);
1019 for (i = 0; i < PKTBUFSRX; i++)
1020 netsec_set_rx_de(priv, i, net_rx_packets[i]);
1025 static int netsec_of_to_plat(struct udevice *dev)
1027 struct eth_pdata *pdata = dev_get_plat(dev);
1028 struct netsec_priv *priv = dev_get_priv(dev);
1029 struct ofnode_phandle_args phandle_args;
1031 pdata->iobase = dev_read_addr_index(dev, 0);
1032 priv->eeprom_base = dev_read_addr_index(dev, 1) - EERPROM_MAP_OFFSET;
1034 pdata->phy_interface = dev_read_phy_mode(dev);
1035 if (pdata->phy_interface == PHY_INTERFACE_MODE_NA)
1038 if (!dev_read_phandle_with_args(dev, "phy-handle", NULL, 0, 0,
1040 priv->phy_addr = ofnode_read_u32_default(phandle_args.node, "reg", 7);
1044 pdata->max_speed = dev_read_u32_default(dev, "max-speed", SPEED_1000);
1046 priv->ioaddr = pdata->iobase;
1047 priv->phy_mode = pdata->phy_interface;
1048 priv->max_speed = pdata->max_speed;
1049 priv->freq = 250000000UL;
1054 static int netsec_probe(struct udevice *dev)
1056 struct netsec_priv *priv = dev_get_priv(dev);
1059 netsec_reset_hardware(priv, true);
1061 ret = netsec_mdiobus_init(priv, dev->name);
1063 pr_err("Failed to initialize mdiobus: %d\n", ret);
1067 priv->bus = miiphy_get_dev_by_name(dev->name);
1069 ret = netsec_phy_init(priv, dev);
1071 pr_err("Failed to initialize phy: %d\n", ret);
1072 goto out_mdiobus_release;
1076 out_mdiobus_release:
1077 mdio_unregister(priv->bus);
1078 mdio_free(priv->bus);
1082 static int netsec_remove(struct udevice *dev)
1084 struct netsec_priv *priv = dev_get_priv(dev);
1087 mdio_unregister(priv->bus);
1088 mdio_free(priv->bus);
1093 static const struct eth_ops netsec_ops = {
1094 .start = netsec_start,
1095 .stop = netsec_stop,
1096 .send = netsec_send,
1097 .recv = netsec_recv,
1098 .free_pkt = netsec_free_packet,
1099 .read_rom_hwaddr = netsec_read_rom_hwaddr,
1102 static const struct udevice_id netsec_ids[] = {
1104 .compatible = "socionext,synquacer-netsec",
1109 U_BOOT_DRIVER(ave) = {
1110 .name = "synquacer_netsec",
1112 .of_match = netsec_ids,
1113 .probe = netsec_probe,
1114 .remove = netsec_remove,
1115 .of_to_plat = netsec_of_to_plat,
1117 .priv_auto = sizeof(struct netsec_priv),
1118 .plat_auto = sizeof(struct eth_pdata),