1 // SPDX-License-Identifier: GPL-2.0
3 * Driver for Marvell NETA network card for Armada XP and Armada 370 SoCs.
6 * Copyright (C) 2014-2015 Stefan Roese <sr@denx.de>
8 * Based on the Linux version which is:
9 * Copyright (C) 2012 Marvell
11 * Rami Rosen <rosenr@marvell.com>
12 * Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
23 #include <dm/device_compat.h>
24 #include <dm/devres.h>
25 #include <linux/errno.h>
29 #include <asm/arch/cpu.h>
30 #include <asm/arch/soc.h>
31 #include <linux/compat.h>
32 #include <linux/mbus.h>
33 #include <asm-generic/gpio.h>
35 DECLARE_GLOBAL_DATA_PTR;
37 #if !defined(CONFIG_PHYLIB)
38 # error Marvell mvneta requires PHYLIB
41 #define CONFIG_NR_CPUS 1
42 #define ETH_HLEN 14 /* Total octets in header */
44 /* 2(HW hdr) 14(MAC hdr) 4(CRC) 32(extra for cache prefetch) */
45 #define WRAP (2 + ETH_HLEN + 4 + 32)
47 #define RX_BUFFER_SIZE (ALIGN(MTU + WRAP, ARCH_DMA_MINALIGN))
49 #define MVNETA_SMI_TIMEOUT 10000
52 #define MVNETA_RXQ_CONFIG_REG(q) (0x1400 + ((q) << 2))
53 #define MVNETA_RXQ_HW_BUF_ALLOC BIT(1)
54 #define MVNETA_RXQ_PKT_OFFSET_ALL_MASK (0xf << 8)
55 #define MVNETA_RXQ_PKT_OFFSET_MASK(offs) ((offs) << 8)
56 #define MVNETA_RXQ_THRESHOLD_REG(q) (0x14c0 + ((q) << 2))
57 #define MVNETA_RXQ_NON_OCCUPIED(v) ((v) << 16)
58 #define MVNETA_RXQ_BASE_ADDR_REG(q) (0x1480 + ((q) << 2))
59 #define MVNETA_RXQ_SIZE_REG(q) (0x14a0 + ((q) << 2))
60 #define MVNETA_RXQ_BUF_SIZE_SHIFT 19
61 #define MVNETA_RXQ_BUF_SIZE_MASK (0x1fff << 19)
62 #define MVNETA_RXQ_STATUS_REG(q) (0x14e0 + ((q) << 2))
63 #define MVNETA_RXQ_OCCUPIED_ALL_MASK 0x3fff
64 #define MVNETA_RXQ_STATUS_UPDATE_REG(q) (0x1500 + ((q) << 2))
65 #define MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT 16
66 #define MVNETA_RXQ_ADD_NON_OCCUPIED_MAX 255
67 #define MVNETA_PORT_RX_RESET 0x1cc0
68 #define MVNETA_PORT_RX_DMA_RESET BIT(0)
69 #define MVNETA_PHY_ADDR 0x2000
70 #define MVNETA_PHY_ADDR_MASK 0x1f
71 #define MVNETA_SMI 0x2004
72 #define MVNETA_PHY_REG_MASK 0x1f
73 /* SMI register fields */
74 #define MVNETA_SMI_DATA_OFFS 0 /* Data */
75 #define MVNETA_SMI_DATA_MASK (0xffff << MVNETA_SMI_DATA_OFFS)
76 #define MVNETA_SMI_DEV_ADDR_OFFS 16 /* PHY device address */
77 #define MVNETA_SMI_REG_ADDR_OFFS 21 /* PHY device reg addr*/
78 #define MVNETA_SMI_OPCODE_OFFS 26 /* Write/Read opcode */
79 #define MVNETA_SMI_OPCODE_READ (1 << MVNETA_SMI_OPCODE_OFFS)
80 #define MVNETA_SMI_READ_VALID (1 << 27) /* Read Valid */
81 #define MVNETA_SMI_BUSY (1 << 28) /* Busy */
82 #define MVNETA_MBUS_RETRY 0x2010
83 #define MVNETA_UNIT_INTR_CAUSE 0x2080
84 #define MVNETA_UNIT_CONTROL 0x20B0
85 #define MVNETA_PHY_POLLING_ENABLE BIT(1)
86 #define MVNETA_WIN_BASE(w) (0x2200 + ((w) << 3))
87 #define MVNETA_WIN_SIZE(w) (0x2204 + ((w) << 3))
88 #define MVNETA_WIN_REMAP(w) (0x2280 + ((w) << 2))
89 #define MVNETA_WIN_SIZE_MASK (0xffff0000)
90 #define MVNETA_BASE_ADDR_ENABLE 0x2290
91 #define MVNETA_BASE_ADDR_ENABLE_BIT 0x1
92 #define MVNETA_PORT_ACCESS_PROTECT 0x2294
93 #define MVNETA_PORT_ACCESS_PROTECT_WIN0_RW 0x3
94 #define MVNETA_PORT_CONFIG 0x2400
95 #define MVNETA_UNI_PROMISC_MODE BIT(0)
96 #define MVNETA_DEF_RXQ(q) ((q) << 1)
97 #define MVNETA_DEF_RXQ_ARP(q) ((q) << 4)
98 #define MVNETA_TX_UNSET_ERR_SUM BIT(12)
99 #define MVNETA_DEF_RXQ_TCP(q) ((q) << 16)
100 #define MVNETA_DEF_RXQ_UDP(q) ((q) << 19)
101 #define MVNETA_DEF_RXQ_BPDU(q) ((q) << 22)
102 #define MVNETA_RX_CSUM_WITH_PSEUDO_HDR BIT(25)
103 #define MVNETA_PORT_CONFIG_DEFL_VALUE(q) (MVNETA_DEF_RXQ(q) | \
104 MVNETA_DEF_RXQ_ARP(q) | \
105 MVNETA_DEF_RXQ_TCP(q) | \
106 MVNETA_DEF_RXQ_UDP(q) | \
107 MVNETA_DEF_RXQ_BPDU(q) | \
108 MVNETA_TX_UNSET_ERR_SUM | \
109 MVNETA_RX_CSUM_WITH_PSEUDO_HDR)
110 #define MVNETA_PORT_CONFIG_EXTEND 0x2404
111 #define MVNETA_MAC_ADDR_LOW 0x2414
112 #define MVNETA_MAC_ADDR_HIGH 0x2418
113 #define MVNETA_SDMA_CONFIG 0x241c
114 #define MVNETA_SDMA_BRST_SIZE_16 4
115 #define MVNETA_RX_BRST_SZ_MASK(burst) ((burst) << 1)
116 #define MVNETA_RX_NO_DATA_SWAP BIT(4)
117 #define MVNETA_TX_NO_DATA_SWAP BIT(5)
118 #define MVNETA_DESC_SWAP BIT(6)
119 #define MVNETA_TX_BRST_SZ_MASK(burst) ((burst) << 22)
120 #define MVNETA_PORT_STATUS 0x2444
121 #define MVNETA_TX_IN_PRGRS BIT(1)
122 #define MVNETA_TX_FIFO_EMPTY BIT(8)
123 #define MVNETA_RX_MIN_FRAME_SIZE 0x247c
124 #define MVNETA_SERDES_CFG 0x24A0
125 #define MVNETA_SGMII_SERDES_PROTO 0x0cc7
126 #define MVNETA_QSGMII_SERDES_PROTO 0x0667
127 #define MVNETA_TYPE_PRIO 0x24bc
128 #define MVNETA_FORCE_UNI BIT(21)
129 #define MVNETA_TXQ_CMD_1 0x24e4
130 #define MVNETA_TXQ_CMD 0x2448
131 #define MVNETA_TXQ_DISABLE_SHIFT 8
132 #define MVNETA_TXQ_ENABLE_MASK 0x000000ff
133 #define MVNETA_ACC_MODE 0x2500
134 #define MVNETA_CPU_MAP(cpu) (0x2540 + ((cpu) << 2))
135 #define MVNETA_CPU_RXQ_ACCESS_ALL_MASK 0x000000ff
136 #define MVNETA_CPU_TXQ_ACCESS_ALL_MASK 0x0000ff00
137 #define MVNETA_RXQ_TIME_COAL_REG(q) (0x2580 + ((q) << 2))
139 /* Exception Interrupt Port/Queue Cause register */
141 #define MVNETA_INTR_NEW_CAUSE 0x25a0
142 #define MVNETA_INTR_NEW_MASK 0x25a4
144 /* bits 0..7 = TXQ SENT, one bit per queue.
145 * bits 8..15 = RXQ OCCUP, one bit per queue.
146 * bits 16..23 = RXQ FREE, one bit per queue.
147 * bit 29 = OLD_REG_SUM, see old reg ?
148 * bit 30 = TX_ERR_SUM, one bit for 4 ports
149 * bit 31 = MISC_SUM, one bit for 4 ports
151 #define MVNETA_TX_INTR_MASK(nr_txqs) (((1 << nr_txqs) - 1) << 0)
152 #define MVNETA_TX_INTR_MASK_ALL (0xff << 0)
153 #define MVNETA_RX_INTR_MASK(nr_rxqs) (((1 << nr_rxqs) - 1) << 8)
154 #define MVNETA_RX_INTR_MASK_ALL (0xff << 8)
156 #define MVNETA_INTR_OLD_CAUSE 0x25a8
157 #define MVNETA_INTR_OLD_MASK 0x25ac
159 /* Data Path Port/Queue Cause Register */
160 #define MVNETA_INTR_MISC_CAUSE 0x25b0
161 #define MVNETA_INTR_MISC_MASK 0x25b4
162 #define MVNETA_INTR_ENABLE 0x25b8
164 #define MVNETA_RXQ_CMD 0x2680
165 #define MVNETA_RXQ_DISABLE_SHIFT 8
166 #define MVNETA_RXQ_ENABLE_MASK 0x000000ff
167 #define MVETH_TXQ_TOKEN_COUNT_REG(q) (0x2700 + ((q) << 4))
168 #define MVETH_TXQ_TOKEN_CFG_REG(q) (0x2704 + ((q) << 4))
169 #define MVNETA_GMAC_CTRL_0 0x2c00
170 #define MVNETA_GMAC_MAX_RX_SIZE_SHIFT 2
171 #define MVNETA_GMAC_MAX_RX_SIZE_MASK 0x7ffc
172 #define MVNETA_GMAC0_PORT_ENABLE BIT(0)
173 #define MVNETA_GMAC_CTRL_2 0x2c08
174 #define MVNETA_GMAC2_PCS_ENABLE BIT(3)
175 #define MVNETA_GMAC2_PORT_RGMII BIT(4)
176 #define MVNETA_GMAC2_PORT_RESET BIT(6)
177 #define MVNETA_GMAC_STATUS 0x2c10
178 #define MVNETA_GMAC_LINK_UP BIT(0)
179 #define MVNETA_GMAC_SPEED_1000 BIT(1)
180 #define MVNETA_GMAC_SPEED_100 BIT(2)
181 #define MVNETA_GMAC_FULL_DUPLEX BIT(3)
182 #define MVNETA_GMAC_RX_FLOW_CTRL_ENABLE BIT(4)
183 #define MVNETA_GMAC_TX_FLOW_CTRL_ENABLE BIT(5)
184 #define MVNETA_GMAC_RX_FLOW_CTRL_ACTIVE BIT(6)
185 #define MVNETA_GMAC_TX_FLOW_CTRL_ACTIVE BIT(7)
186 #define MVNETA_GMAC_AUTONEG_CONFIG 0x2c0c
187 #define MVNETA_GMAC_FORCE_LINK_DOWN BIT(0)
188 #define MVNETA_GMAC_FORCE_LINK_PASS BIT(1)
189 #define MVNETA_GMAC_FORCE_LINK_UP (BIT(0) | BIT(1))
190 #define MVNETA_GMAC_IB_BYPASS_AN_EN BIT(3)
191 #define MVNETA_GMAC_CONFIG_MII_SPEED BIT(5)
192 #define MVNETA_GMAC_CONFIG_GMII_SPEED BIT(6)
193 #define MVNETA_GMAC_AN_SPEED_EN BIT(7)
194 #define MVNETA_GMAC_SET_FC_EN BIT(8)
195 #define MVNETA_GMAC_ADVERT_FC_EN BIT(9)
196 #define MVNETA_GMAC_CONFIG_FULL_DUPLEX BIT(12)
197 #define MVNETA_GMAC_AN_DUPLEX_EN BIT(13)
198 #define MVNETA_GMAC_SAMPLE_TX_CFG_EN BIT(15)
199 #define MVNETA_MIB_COUNTERS_BASE 0x3080
200 #define MVNETA_MIB_LATE_COLLISION 0x7c
201 #define MVNETA_DA_FILT_SPEC_MCAST 0x3400
202 #define MVNETA_DA_FILT_OTH_MCAST 0x3500
203 #define MVNETA_DA_FILT_UCAST_BASE 0x3600
204 #define MVNETA_TXQ_BASE_ADDR_REG(q) (0x3c00 + ((q) << 2))
205 #define MVNETA_TXQ_SIZE_REG(q) (0x3c20 + ((q) << 2))
206 #define MVNETA_TXQ_SENT_THRESH_ALL_MASK 0x3fff0000
207 #define MVNETA_TXQ_SENT_THRESH_MASK(coal) ((coal) << 16)
208 #define MVNETA_TXQ_UPDATE_REG(q) (0x3c60 + ((q) << 2))
209 #define MVNETA_TXQ_DEC_SENT_SHIFT 16
210 #define MVNETA_TXQ_STATUS_REG(q) (0x3c40 + ((q) << 2))
211 #define MVNETA_TXQ_SENT_DESC_SHIFT 16
212 #define MVNETA_TXQ_SENT_DESC_MASK 0x3fff0000
213 #define MVNETA_PORT_TX_RESET 0x3cf0
214 #define MVNETA_PORT_TX_DMA_RESET BIT(0)
215 #define MVNETA_TX_MTU 0x3e0c
216 #define MVNETA_TX_TOKEN_SIZE 0x3e14
217 #define MVNETA_TX_TOKEN_SIZE_MAX 0xffffffff
218 #define MVNETA_TXQ_TOKEN_SIZE_REG(q) (0x3e40 + ((q) << 2))
219 #define MVNETA_TXQ_TOKEN_SIZE_MAX 0x7fffffff
221 /* Descriptor ring Macros */
222 #define MVNETA_QUEUE_NEXT_DESC(q, index) \
223 (((index) < (q)->last_desc) ? ((index) + 1) : 0)
225 /* Various constants */
228 #define MVNETA_TXDONE_COAL_PKTS 16
229 #define MVNETA_RX_COAL_PKTS 32
230 #define MVNETA_RX_COAL_USEC 100
232 /* The two bytes Marvell header. Either contains a special value used
233 * by Marvell switches when a specific hardware mode is enabled (not
234 * supported by this driver) or is filled automatically by zeroes on
235 * the RX side. Those two bytes being at the front of the Ethernet
236 * header, they allow to have the IP header aligned on a 4 bytes
237 * boundary automatically: the hardware skips those two bytes on its
240 #define MVNETA_MH_SIZE 2
242 #define MVNETA_VLAN_TAG_LEN 4
244 #define MVNETA_CPU_D_CACHE_LINE_SIZE 32
245 #define MVNETA_TX_CSUM_MAX_SIZE 9800
246 #define MVNETA_ACC_MODE_EXT 1
248 /* Timeout constants */
249 #define MVNETA_TX_DISABLE_TIMEOUT_MSEC 1000
250 #define MVNETA_RX_DISABLE_TIMEOUT_MSEC 1000
251 #define MVNETA_TX_FIFO_EMPTY_TIMEOUT 10000
253 #define MVNETA_TX_MTU_MAX 0x3ffff
255 /* Max number of Rx descriptors */
256 #define MVNETA_MAX_RXD 16
258 /* Max number of Tx descriptors */
259 #define MVNETA_MAX_TXD 16
261 /* descriptor aligned size */
262 #define MVNETA_DESC_ALIGNED_SIZE 32
266 struct mvneta_rx_queue *rxqs;
267 struct mvneta_tx_queue *txqs;
273 phy_interface_t phy_interface;
280 struct phy_device *phydev;
281 #if CONFIG_IS_ENABLED(DM_GPIO)
282 struct gpio_desc phy_reset_gpio;
287 /* The mvneta_tx_desc and mvneta_rx_desc structures describe the
288 * layout of the transmit and reception DMA descriptors, and their
289 * layout is therefore defined by the hardware design
292 #define MVNETA_TX_L3_OFF_SHIFT 0
293 #define MVNETA_TX_IP_HLEN_SHIFT 8
294 #define MVNETA_TX_L4_UDP BIT(16)
295 #define MVNETA_TX_L3_IP6 BIT(17)
296 #define MVNETA_TXD_IP_CSUM BIT(18)
297 #define MVNETA_TXD_Z_PAD BIT(19)
298 #define MVNETA_TXD_L_DESC BIT(20)
299 #define MVNETA_TXD_F_DESC BIT(21)
300 #define MVNETA_TXD_FLZ_DESC (MVNETA_TXD_Z_PAD | \
301 MVNETA_TXD_L_DESC | \
303 #define MVNETA_TX_L4_CSUM_FULL BIT(30)
304 #define MVNETA_TX_L4_CSUM_NOT BIT(31)
306 #define MVNETA_RXD_ERR_CRC 0x0
307 #define MVNETA_RXD_ERR_SUMMARY BIT(16)
308 #define MVNETA_RXD_ERR_OVERRUN BIT(17)
309 #define MVNETA_RXD_ERR_LEN BIT(18)
310 #define MVNETA_RXD_ERR_RESOURCE (BIT(17) | BIT(18))
311 #define MVNETA_RXD_ERR_CODE_MASK (BIT(17) | BIT(18))
312 #define MVNETA_RXD_L3_IP4 BIT(25)
313 #define MVNETA_RXD_FIRST_LAST_DESC (BIT(26) | BIT(27))
314 #define MVNETA_RXD_L4_CSUM_OK BIT(30)
316 struct mvneta_tx_desc {
317 u32 command; /* Options used by HW for packet transmitting.*/
318 u16 reserverd1; /* csum_l4 (for future use) */
319 u16 data_size; /* Data size of transmitted packet in bytes */
320 u32 buf_phys_addr; /* Physical addr of transmitted buffer */
321 u32 reserved2; /* hw_cmd - (for future use, PMT) */
322 u32 reserved3[4]; /* Reserved - (for future use) */
325 struct mvneta_rx_desc {
326 u32 status; /* Info about received packet */
327 u16 reserved1; /* pnc_info - (for future use, PnC) */
328 u16 data_size; /* Size of received packet in bytes */
330 u32 buf_phys_addr; /* Physical address of the buffer */
331 u32 reserved2; /* pnc_flow_id (for future use, PnC) */
333 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
334 u16 reserved3; /* prefetch_cmd, for future use */
335 u16 reserved4; /* csum_l4 - (for future use, PnC) */
337 u32 reserved5; /* pnc_extra PnC (for future use, PnC) */
338 u32 reserved6; /* hw_cmd (for future use, PnC and HWF) */
341 struct mvneta_tx_queue {
342 /* Number of this TX queue, in the range 0-7 */
345 /* Number of TX DMA descriptors in the descriptor ring */
348 /* Index of last TX DMA descriptor that was inserted */
351 /* Index of the TX DMA descriptor to be cleaned up */
354 /* Virtual address of the TX DMA descriptors array */
355 struct mvneta_tx_desc *descs;
357 /* DMA address of the TX DMA descriptors array */
358 dma_addr_t descs_phys;
360 /* Index of the last TX DMA descriptor */
363 /* Index of the next TX DMA descriptor to process */
364 int next_desc_to_proc;
367 struct mvneta_rx_queue {
368 /* rx queue number, in the range 0-7 */
371 /* num of rx descriptors in the rx descriptor ring */
374 /* Virtual address of the RX DMA descriptors array */
375 struct mvneta_rx_desc *descs;
377 /* DMA address of the RX DMA descriptors array */
378 dma_addr_t descs_phys;
380 /* Index of the last RX DMA descriptor */
383 /* Index of the next RX DMA descriptor to process */
384 int next_desc_to_proc;
387 /* U-Boot doesn't use the queues, so set the number to 1 */
388 static int rxq_number = 1;
389 static int txq_number = 1;
392 struct buffer_location {
393 struct mvneta_tx_desc *tx_descs;
394 struct mvneta_rx_desc *rx_descs;
399 * All 4 interfaces use the same global buffer, since only one interface
400 * can be enabled at once
402 static struct buffer_location buffer_loc;
405 * Page table entries are set to 1MB, or multiples of 1MB
406 * (not < 1MB). driver uses less bd's so use 1MB bdspace.
408 #define BD_SPACE (1 << 20)
411 * Dummy implementation that can be overwritten by a board
414 __weak int board_network_enable(struct mii_dev *bus)
419 /* Utility/helper methods */
421 /* Write helper method */
422 static void mvreg_write(struct mvneta_port *pp, u32 offset, u32 data)
424 writel(data, pp->base + offset);
427 /* Read helper method */
428 static u32 mvreg_read(struct mvneta_port *pp, u32 offset)
430 return readl(pp->base + offset);
433 /* Clear all MIB counters */
434 static void mvneta_mib_counters_clear(struct mvneta_port *pp)
438 /* Perform dummy reads from MIB counters */
439 for (i = 0; i < MVNETA_MIB_LATE_COLLISION; i += 4)
440 mvreg_read(pp, (MVNETA_MIB_COUNTERS_BASE + i));
443 /* Rx descriptors helper methods */
445 /* Checks whether the RX descriptor having this status is both the first
446 * and the last descriptor for the RX packet. Each RX packet is currently
447 * received through a single RX descriptor, so not having each RX
448 * descriptor with its first and last bits set is an error
450 static int mvneta_rxq_desc_is_first_last(u32 status)
452 return (status & MVNETA_RXD_FIRST_LAST_DESC) ==
453 MVNETA_RXD_FIRST_LAST_DESC;
456 /* Add number of descriptors ready to receive new packets */
457 static void mvneta_rxq_non_occup_desc_add(struct mvneta_port *pp,
458 struct mvneta_rx_queue *rxq,
461 /* Only MVNETA_RXQ_ADD_NON_OCCUPIED_MAX (255) descriptors can
464 while (ndescs > MVNETA_RXQ_ADD_NON_OCCUPIED_MAX) {
465 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
466 (MVNETA_RXQ_ADD_NON_OCCUPIED_MAX <<
467 MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
468 ndescs -= MVNETA_RXQ_ADD_NON_OCCUPIED_MAX;
471 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
472 (ndescs << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
475 /* Get number of RX descriptors occupied by received packets */
476 static int mvneta_rxq_busy_desc_num_get(struct mvneta_port *pp,
477 struct mvneta_rx_queue *rxq)
481 val = mvreg_read(pp, MVNETA_RXQ_STATUS_REG(rxq->id));
482 return val & MVNETA_RXQ_OCCUPIED_ALL_MASK;
485 /* Update num of rx desc called upon return from rx path or
486 * from mvneta_rxq_drop_pkts().
488 static void mvneta_rxq_desc_num_update(struct mvneta_port *pp,
489 struct mvneta_rx_queue *rxq,
490 int rx_done, int rx_filled)
494 if ((rx_done <= 0xff) && (rx_filled <= 0xff)) {
496 (rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT);
497 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
501 /* Only 255 descriptors can be added at once */
502 while ((rx_done > 0) || (rx_filled > 0)) {
503 if (rx_done <= 0xff) {
510 if (rx_filled <= 0xff) {
511 val |= rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
514 val |= 0xff << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
517 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
521 /* Get pointer to next RX descriptor to be processed by SW */
522 static struct mvneta_rx_desc *
523 mvneta_rxq_next_desc_get(struct mvneta_rx_queue *rxq)
525 int rx_desc = rxq->next_desc_to_proc;
527 rxq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(rxq, rx_desc);
528 return rxq->descs + rx_desc;
531 /* Tx descriptors helper methods */
533 /* Update HW with number of TX descriptors to be sent */
534 static void mvneta_txq_pend_desc_add(struct mvneta_port *pp,
535 struct mvneta_tx_queue *txq,
540 /* Only 255 descriptors can be added at once ; Assume caller
541 * process TX descriptors in quanta less than 256
544 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
547 /* Get pointer to next TX descriptor to be processed (send) by HW */
548 static struct mvneta_tx_desc *
549 mvneta_txq_next_desc_get(struct mvneta_tx_queue *txq)
551 int tx_desc = txq->next_desc_to_proc;
553 txq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(txq, tx_desc);
554 return txq->descs + tx_desc;
557 /* Set rxq buf size */
558 static void mvneta_rxq_buf_size_set(struct mvneta_port *pp,
559 struct mvneta_rx_queue *rxq,
564 val = mvreg_read(pp, MVNETA_RXQ_SIZE_REG(rxq->id));
566 val &= ~MVNETA_RXQ_BUF_SIZE_MASK;
567 val |= ((buf_size >> 3) << MVNETA_RXQ_BUF_SIZE_SHIFT);
569 mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), val);
572 static int mvneta_port_is_fixed_link(struct mvneta_port *pp)
574 /* phy_addr is set to invalid value for fixed link */
575 return pp->phyaddr > PHY_MAX_ADDR;
579 /* Start the Ethernet port RX and TX activity */
580 static void mvneta_port_up(struct mvneta_port *pp)
585 /* Enable all initialized TXs. */
586 mvneta_mib_counters_clear(pp);
588 for (queue = 0; queue < txq_number; queue++) {
589 struct mvneta_tx_queue *txq = &pp->txqs[queue];
590 if (txq->descs != NULL)
591 q_map |= (1 << queue);
593 mvreg_write(pp, MVNETA_TXQ_CMD, q_map);
595 /* Enable all initialized RXQs. */
597 for (queue = 0; queue < rxq_number; queue++) {
598 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
599 if (rxq->descs != NULL)
600 q_map |= (1 << queue);
602 mvreg_write(pp, MVNETA_RXQ_CMD, q_map);
605 /* Stop the Ethernet port activity */
606 static void mvneta_port_down(struct mvneta_port *pp)
611 /* Stop Rx port activity. Check port Rx activity. */
612 val = mvreg_read(pp, MVNETA_RXQ_CMD) & MVNETA_RXQ_ENABLE_MASK;
614 /* Issue stop command for active channels only */
616 mvreg_write(pp, MVNETA_RXQ_CMD,
617 val << MVNETA_RXQ_DISABLE_SHIFT);
619 /* Wait for all Rx activity to terminate. */
622 if (count++ >= MVNETA_RX_DISABLE_TIMEOUT_MSEC) {
624 "TIMEOUT for RX stopped ! rx_queue_cmd: 0x08%x\n",
630 val = mvreg_read(pp, MVNETA_RXQ_CMD);
631 } while (val & 0xff);
633 /* Stop Tx port activity. Check port Tx activity. Issue stop
634 * command for active channels only
636 val = (mvreg_read(pp, MVNETA_TXQ_CMD)) & MVNETA_TXQ_ENABLE_MASK;
639 mvreg_write(pp, MVNETA_TXQ_CMD,
640 (val << MVNETA_TXQ_DISABLE_SHIFT));
642 /* Wait for all Tx activity to terminate. */
645 if (count++ >= MVNETA_TX_DISABLE_TIMEOUT_MSEC) {
647 "TIMEOUT for TX stopped status=0x%08x\n",
653 /* Check TX Command reg that all Txqs are stopped */
654 val = mvreg_read(pp, MVNETA_TXQ_CMD);
656 } while (val & 0xff);
658 /* Double check to verify that TX FIFO is empty */
661 if (count++ >= MVNETA_TX_FIFO_EMPTY_TIMEOUT) {
663 "TX FIFO empty timeout status=0x08%x\n",
669 val = mvreg_read(pp, MVNETA_PORT_STATUS);
670 } while (!(val & MVNETA_TX_FIFO_EMPTY) &&
671 (val & MVNETA_TX_IN_PRGRS));
676 /* Enable the port by setting the port enable bit of the MAC control register */
677 static void mvneta_port_enable(struct mvneta_port *pp)
682 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
683 val |= MVNETA_GMAC0_PORT_ENABLE;
684 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
687 /* Disable the port and wait for about 200 usec before retuning */
688 static void mvneta_port_disable(struct mvneta_port *pp)
692 /* Reset the Enable bit in the Serial Control Register */
693 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
694 val &= ~MVNETA_GMAC0_PORT_ENABLE;
695 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
700 /* Multicast tables methods */
702 /* Set all entries in Unicast MAC Table; queue==-1 means reject all */
703 static void mvneta_set_ucast_table(struct mvneta_port *pp, int queue)
711 val = 0x1 | (queue << 1);
712 val |= (val << 24) | (val << 16) | (val << 8);
715 for (offset = 0; offset <= 0xc; offset += 4)
716 mvreg_write(pp, MVNETA_DA_FILT_UCAST_BASE + offset, val);
719 /* Set all entries in Special Multicast MAC Table; queue==-1 means reject all */
720 static void mvneta_set_special_mcast_table(struct mvneta_port *pp, int queue)
728 val = 0x1 | (queue << 1);
729 val |= (val << 24) | (val << 16) | (val << 8);
732 for (offset = 0; offset <= 0xfc; offset += 4)
733 mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + offset, val);
736 /* Set all entries in Other Multicast MAC Table. queue==-1 means reject all */
737 static void mvneta_set_other_mcast_table(struct mvneta_port *pp, int queue)
743 memset(pp->mcast_count, 0, sizeof(pp->mcast_count));
746 memset(pp->mcast_count, 1, sizeof(pp->mcast_count));
747 val = 0x1 | (queue << 1);
748 val |= (val << 24) | (val << 16) | (val << 8);
751 for (offset = 0; offset <= 0xfc; offset += 4)
752 mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + offset, val);
755 /* This method sets defaults to the NETA port:
756 * Clears interrupt Cause and Mask registers.
757 * Clears all MAC tables.
758 * Sets defaults to all registers.
759 * Resets RX and TX descriptor rings.
761 * This method can be called after mvneta_port_down() to return the port
762 * settings to defaults.
764 static void mvneta_defaults_set(struct mvneta_port *pp)
770 /* Clear all Cause registers */
771 mvreg_write(pp, MVNETA_INTR_NEW_CAUSE, 0);
772 mvreg_write(pp, MVNETA_INTR_OLD_CAUSE, 0);
773 mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
775 /* Mask all interrupts */
776 mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
777 mvreg_write(pp, MVNETA_INTR_OLD_MASK, 0);
778 mvreg_write(pp, MVNETA_INTR_MISC_MASK, 0);
779 mvreg_write(pp, MVNETA_INTR_ENABLE, 0);
781 /* Enable MBUS Retry bit16 */
782 mvreg_write(pp, MVNETA_MBUS_RETRY, 0x20);
784 /* Set CPU queue access map - all CPUs have access to all RX
785 * queues and to all TX queues
787 for (cpu = 0; cpu < CONFIG_NR_CPUS; cpu++)
788 mvreg_write(pp, MVNETA_CPU_MAP(cpu),
789 (MVNETA_CPU_RXQ_ACCESS_ALL_MASK |
790 MVNETA_CPU_TXQ_ACCESS_ALL_MASK));
792 /* Reset RX and TX DMAs */
793 mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
794 mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
796 /* Disable Legacy WRR, Disable EJP, Release from reset */
797 mvreg_write(pp, MVNETA_TXQ_CMD_1, 0);
798 for (queue = 0; queue < txq_number; queue++) {
799 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(queue), 0);
800 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(queue), 0);
803 mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
804 mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
806 /* Set Port Acceleration Mode */
807 val = MVNETA_ACC_MODE_EXT;
808 mvreg_write(pp, MVNETA_ACC_MODE, val);
810 /* Update val of portCfg register accordingly with all RxQueue types */
811 val = MVNETA_PORT_CONFIG_DEFL_VALUE(rxq_def);
812 mvreg_write(pp, MVNETA_PORT_CONFIG, val);
815 mvreg_write(pp, MVNETA_PORT_CONFIG_EXTEND, val);
816 mvreg_write(pp, MVNETA_RX_MIN_FRAME_SIZE, 64);
818 /* Build PORT_SDMA_CONFIG_REG */
821 /* Default burst size */
822 val |= MVNETA_TX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
823 val |= MVNETA_RX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
824 val |= MVNETA_RX_NO_DATA_SWAP | MVNETA_TX_NO_DATA_SWAP;
826 /* Assign port SDMA configuration */
827 mvreg_write(pp, MVNETA_SDMA_CONFIG, val);
829 /* Enable PHY polling in hardware if not in fixed-link mode */
830 if (!mvneta_port_is_fixed_link(pp)) {
831 val = mvreg_read(pp, MVNETA_UNIT_CONTROL);
832 val |= MVNETA_PHY_POLLING_ENABLE;
833 mvreg_write(pp, MVNETA_UNIT_CONTROL, val);
836 mvneta_set_ucast_table(pp, -1);
837 mvneta_set_special_mcast_table(pp, -1);
838 mvneta_set_other_mcast_table(pp, -1);
841 /* Set unicast address */
842 static void mvneta_set_ucast_addr(struct mvneta_port *pp, u8 last_nibble,
845 unsigned int unicast_reg;
846 unsigned int tbl_offset;
847 unsigned int reg_offset;
849 /* Locate the Unicast table entry */
850 last_nibble = (0xf & last_nibble);
852 /* offset from unicast tbl base */
853 tbl_offset = (last_nibble / 4) * 4;
855 /* offset within the above reg */
856 reg_offset = last_nibble % 4;
858 unicast_reg = mvreg_read(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset));
861 /* Clear accepts frame bit at specified unicast DA tbl entry */
862 unicast_reg &= ~(0xff << (8 * reg_offset));
864 unicast_reg &= ~(0xff << (8 * reg_offset));
865 unicast_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
868 mvreg_write(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset), unicast_reg);
871 /* Set mac address */
872 static void mvneta_mac_addr_set(struct mvneta_port *pp, unsigned char *addr,
879 mac_l = (addr[4] << 8) | (addr[5]);
880 mac_h = (addr[0] << 24) | (addr[1] << 16) |
881 (addr[2] << 8) | (addr[3] << 0);
883 mvreg_write(pp, MVNETA_MAC_ADDR_LOW, mac_l);
884 mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, mac_h);
887 /* Accept frames of this address */
888 mvneta_set_ucast_addr(pp, addr[5], queue);
891 static int mvneta_write_hwaddr(struct udevice *dev)
893 mvneta_mac_addr_set(dev_get_priv(dev),
894 ((struct eth_pdata *)dev_get_platdata(dev))->enetaddr,
900 /* Handle rx descriptor fill by setting buf_cookie and buf_phys_addr */
901 static void mvneta_rx_desc_fill(struct mvneta_rx_desc *rx_desc,
902 u32 phys_addr, u32 cookie)
904 rx_desc->buf_cookie = cookie;
905 rx_desc->buf_phys_addr = phys_addr;
908 /* Decrement sent descriptors counter */
909 static void mvneta_txq_sent_desc_dec(struct mvneta_port *pp,
910 struct mvneta_tx_queue *txq,
915 /* Only 255 TX descriptors can be updated at once */
916 while (sent_desc > 0xff) {
917 val = 0xff << MVNETA_TXQ_DEC_SENT_SHIFT;
918 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
919 sent_desc = sent_desc - 0xff;
922 val = sent_desc << MVNETA_TXQ_DEC_SENT_SHIFT;
923 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
926 /* Get number of TX descriptors already sent by HW */
927 static int mvneta_txq_sent_desc_num_get(struct mvneta_port *pp,
928 struct mvneta_tx_queue *txq)
933 val = mvreg_read(pp, MVNETA_TXQ_STATUS_REG(txq->id));
934 sent_desc = (val & MVNETA_TXQ_SENT_DESC_MASK) >>
935 MVNETA_TXQ_SENT_DESC_SHIFT;
940 /* Display more error info */
941 static void mvneta_rx_error(struct mvneta_port *pp,
942 struct mvneta_rx_desc *rx_desc)
944 u32 status = rx_desc->status;
946 if (!mvneta_rxq_desc_is_first_last(status)) {
948 "bad rx status %08x (buffer oversize), size=%d\n",
949 status, rx_desc->data_size);
953 switch (status & MVNETA_RXD_ERR_CODE_MASK) {
954 case MVNETA_RXD_ERR_CRC:
955 netdev_err(pp->dev, "bad rx status %08x (crc error), size=%d\n",
956 status, rx_desc->data_size);
958 case MVNETA_RXD_ERR_OVERRUN:
959 netdev_err(pp->dev, "bad rx status %08x (overrun error), size=%d\n",
960 status, rx_desc->data_size);
962 case MVNETA_RXD_ERR_LEN:
963 netdev_err(pp->dev, "bad rx status %08x (max frame length error), size=%d\n",
964 status, rx_desc->data_size);
966 case MVNETA_RXD_ERR_RESOURCE:
967 netdev_err(pp->dev, "bad rx status %08x (resource error), size=%d\n",
968 status, rx_desc->data_size);
973 static struct mvneta_rx_queue *mvneta_rxq_handle_get(struct mvneta_port *pp,
976 return &pp->rxqs[rxq];
980 /* Drop packets received by the RXQ and free buffers */
981 static void mvneta_rxq_drop_pkts(struct mvneta_port *pp,
982 struct mvneta_rx_queue *rxq)
986 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
988 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
991 /* Handle rxq fill: allocates rxq skbs; called when initializing a port */
992 static int mvneta_rxq_fill(struct mvneta_port *pp, struct mvneta_rx_queue *rxq,
997 for (i = 0; i < num; i++) {
1000 /* U-Boot special: Fill in the rx buffer addresses */
1001 addr = buffer_loc.rx_buffers + (i * RX_BUFFER_SIZE);
1002 mvneta_rx_desc_fill(rxq->descs + i, addr, addr);
1005 /* Add this number of RX descriptors as non occupied (ready to
1008 mvneta_rxq_non_occup_desc_add(pp, rxq, i);
1013 /* Rx/Tx queue initialization/cleanup methods */
1015 /* Create a specified RX queue */
1016 static int mvneta_rxq_init(struct mvneta_port *pp,
1017 struct mvneta_rx_queue *rxq)
1020 rxq->size = pp->rx_ring_size;
1022 /* Allocate memory for RX descriptors */
1023 rxq->descs_phys = (dma_addr_t)rxq->descs;
1024 if (rxq->descs == NULL)
1027 WARN_ON(rxq->descs != PTR_ALIGN(rxq->descs, ARCH_DMA_MINALIGN));
1029 rxq->last_desc = rxq->size - 1;
1031 /* Set Rx descriptors queue starting address */
1032 mvreg_write(pp, MVNETA_RXQ_BASE_ADDR_REG(rxq->id), rxq->descs_phys);
1033 mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), rxq->size);
1035 /* Fill RXQ with buffers from RX pool */
1036 mvneta_rxq_buf_size_set(pp, rxq, RX_BUFFER_SIZE);
1037 mvneta_rxq_fill(pp, rxq, rxq->size);
1042 /* Cleanup Rx queue */
1043 static void mvneta_rxq_deinit(struct mvneta_port *pp,
1044 struct mvneta_rx_queue *rxq)
1046 mvneta_rxq_drop_pkts(pp, rxq);
1050 rxq->next_desc_to_proc = 0;
1051 rxq->descs_phys = 0;
1054 /* Create and initialize a tx queue */
1055 static int mvneta_txq_init(struct mvneta_port *pp,
1056 struct mvneta_tx_queue *txq)
1058 txq->size = pp->tx_ring_size;
1060 /* Allocate memory for TX descriptors */
1061 txq->descs_phys = (dma_addr_t)txq->descs;
1062 if (txq->descs == NULL)
1065 WARN_ON(txq->descs != PTR_ALIGN(txq->descs, ARCH_DMA_MINALIGN));
1067 txq->last_desc = txq->size - 1;
1069 /* Set maximum bandwidth for enabled TXQs */
1070 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0x03ffffff);
1071 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0x3fffffff);
1073 /* Set Tx descriptors queue starting address */
1074 mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), txq->descs_phys);
1075 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), txq->size);
1080 /* Free allocated resources when mvneta_txq_init() fails to allocate memory*/
1081 static void mvneta_txq_deinit(struct mvneta_port *pp,
1082 struct mvneta_tx_queue *txq)
1086 txq->next_desc_to_proc = 0;
1087 txq->descs_phys = 0;
1089 /* Set minimum bandwidth for disabled TXQs */
1090 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0);
1091 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0);
1093 /* Set Tx descriptors queue starting address and size */
1094 mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), 0);
1095 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), 0);
1098 /* Cleanup all Tx queues */
1099 static void mvneta_cleanup_txqs(struct mvneta_port *pp)
1103 for (queue = 0; queue < txq_number; queue++)
1104 mvneta_txq_deinit(pp, &pp->txqs[queue]);
1107 /* Cleanup all Rx queues */
1108 static void mvneta_cleanup_rxqs(struct mvneta_port *pp)
1112 for (queue = 0; queue < rxq_number; queue++)
1113 mvneta_rxq_deinit(pp, &pp->rxqs[queue]);
1117 /* Init all Rx queues */
1118 static int mvneta_setup_rxqs(struct mvneta_port *pp)
1122 for (queue = 0; queue < rxq_number; queue++) {
1123 int err = mvneta_rxq_init(pp, &pp->rxqs[queue]);
1125 netdev_err(pp->dev, "%s: can't create rxq=%d\n",
1127 mvneta_cleanup_rxqs(pp);
1135 /* Init all tx queues */
1136 static int mvneta_setup_txqs(struct mvneta_port *pp)
1140 for (queue = 0; queue < txq_number; queue++) {
1141 int err = mvneta_txq_init(pp, &pp->txqs[queue]);
1143 netdev_err(pp->dev, "%s: can't create txq=%d\n",
1145 mvneta_cleanup_txqs(pp);
1153 static void mvneta_start_dev(struct mvneta_port *pp)
1155 /* start the Rx/Tx activity */
1156 mvneta_port_enable(pp);
1159 static void mvneta_adjust_link(struct udevice *dev)
1161 struct mvneta_port *pp = dev_get_priv(dev);
1162 struct phy_device *phydev = pp->phydev;
1163 int status_change = 0;
1165 if (mvneta_port_is_fixed_link(pp)) {
1166 debug("Using fixed link, skip link adjust\n");
1171 if ((pp->speed != phydev->speed) ||
1172 (pp->duplex != phydev->duplex)) {
1175 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
1176 val &= ~(MVNETA_GMAC_CONFIG_MII_SPEED |
1177 MVNETA_GMAC_CONFIG_GMII_SPEED |
1178 MVNETA_GMAC_CONFIG_FULL_DUPLEX |
1179 MVNETA_GMAC_AN_SPEED_EN |
1180 MVNETA_GMAC_AN_DUPLEX_EN);
1183 val |= MVNETA_GMAC_CONFIG_FULL_DUPLEX;
1185 if (phydev->speed == SPEED_1000)
1186 val |= MVNETA_GMAC_CONFIG_GMII_SPEED;
1188 val |= MVNETA_GMAC_CONFIG_MII_SPEED;
1190 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
1192 pp->duplex = phydev->duplex;
1193 pp->speed = phydev->speed;
1197 if (phydev->link != pp->link) {
1198 if (!phydev->link) {
1203 pp->link = phydev->link;
1207 if (status_change) {
1209 u32 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
1210 val |= (MVNETA_GMAC_FORCE_LINK_PASS |
1211 MVNETA_GMAC_FORCE_LINK_DOWN);
1212 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
1215 mvneta_port_down(pp);
1220 static int mvneta_open(struct udevice *dev)
1222 struct mvneta_port *pp = dev_get_priv(dev);
1225 ret = mvneta_setup_rxqs(pp);
1229 ret = mvneta_setup_txqs(pp);
1233 mvneta_adjust_link(dev);
1235 mvneta_start_dev(pp);
1241 static int mvneta_init2(struct mvneta_port *pp)
1246 mvneta_port_disable(pp);
1248 /* Set port default values */
1249 mvneta_defaults_set(pp);
1251 pp->txqs = kzalloc(txq_number * sizeof(struct mvneta_tx_queue),
1256 /* U-Boot special: use preallocated area */
1257 pp->txqs[0].descs = buffer_loc.tx_descs;
1259 /* Initialize TX descriptor rings */
1260 for (queue = 0; queue < txq_number; queue++) {
1261 struct mvneta_tx_queue *txq = &pp->txqs[queue];
1263 txq->size = pp->tx_ring_size;
1266 pp->rxqs = kzalloc(rxq_number * sizeof(struct mvneta_rx_queue),
1273 /* U-Boot special: use preallocated area */
1274 pp->rxqs[0].descs = buffer_loc.rx_descs;
1276 /* Create Rx descriptor rings */
1277 for (queue = 0; queue < rxq_number; queue++) {
1278 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
1280 rxq->size = pp->rx_ring_size;
1286 /* platform glue : initialize decoding windows */
1289 * Not like A380, in Armada3700, there are two layers of decode windows for GBE:
1290 * First layer is: GbE Address window that resides inside the GBE unit,
1291 * Second layer is: Fabric address window which is located in the NIC400
1293 * To simplify the address decode configuration for Armada3700, we bypass the
1294 * first layer of GBE decode window by setting the first window to 4GB.
1296 static void mvneta_bypass_mbus_windows(struct mvneta_port *pp)
1299 * Set window size to 4GB, to bypass GBE address decode, leave the
1300 * work to MBUS decode window
1302 mvreg_write(pp, MVNETA_WIN_SIZE(0), MVNETA_WIN_SIZE_MASK);
1304 /* Enable GBE address decode window 0 by set bit 0 to 0 */
1305 clrbits_le32(pp->base + MVNETA_BASE_ADDR_ENABLE,
1306 MVNETA_BASE_ADDR_ENABLE_BIT);
1308 /* Set GBE address decode window 0 to full Access (read or write) */
1309 setbits_le32(pp->base + MVNETA_PORT_ACCESS_PROTECT,
1310 MVNETA_PORT_ACCESS_PROTECT_WIN0_RW);
1313 static void mvneta_conf_mbus_windows(struct mvneta_port *pp)
1315 const struct mbus_dram_target_info *dram;
1320 dram = mvebu_mbus_dram_info();
1321 for (i = 0; i < 6; i++) {
1322 mvreg_write(pp, MVNETA_WIN_BASE(i), 0);
1323 mvreg_write(pp, MVNETA_WIN_SIZE(i), 0);
1326 mvreg_write(pp, MVNETA_WIN_REMAP(i), 0);
1332 for (i = 0; i < dram->num_cs; i++) {
1333 const struct mbus_dram_window *cs = dram->cs + i;
1334 mvreg_write(pp, MVNETA_WIN_BASE(i), (cs->base & 0xffff0000) |
1335 (cs->mbus_attr << 8) | dram->mbus_dram_target_id);
1337 mvreg_write(pp, MVNETA_WIN_SIZE(i),
1338 (cs->size - 1) & 0xffff0000);
1340 win_enable &= ~(1 << i);
1341 win_protect |= 3 << (2 * i);
1344 mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable);
1347 /* Power up the port */
1348 static int mvneta_port_power_up(struct mvneta_port *pp, int phy_mode)
1352 /* MAC Cause register should be cleared */
1353 mvreg_write(pp, MVNETA_UNIT_INTR_CAUSE, 0);
1355 ctrl = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
1357 /* Even though it might look weird, when we're configured in
1358 * SGMII or QSGMII mode, the RGMII bit needs to be set.
1361 case PHY_INTERFACE_MODE_QSGMII:
1362 mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_QSGMII_SERDES_PROTO);
1363 ctrl |= MVNETA_GMAC2_PCS_ENABLE | MVNETA_GMAC2_PORT_RGMII;
1365 case PHY_INTERFACE_MODE_SGMII:
1366 mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_SGMII_SERDES_PROTO);
1367 ctrl |= MVNETA_GMAC2_PCS_ENABLE | MVNETA_GMAC2_PORT_RGMII;
1369 case PHY_INTERFACE_MODE_RGMII:
1370 case PHY_INTERFACE_MODE_RGMII_ID:
1371 ctrl |= MVNETA_GMAC2_PORT_RGMII;
1377 /* Cancel Port Reset */
1378 ctrl &= ~MVNETA_GMAC2_PORT_RESET;
1379 mvreg_write(pp, MVNETA_GMAC_CTRL_2, ctrl);
1381 while ((mvreg_read(pp, MVNETA_GMAC_CTRL_2) &
1382 MVNETA_GMAC2_PORT_RESET) != 0)
1388 /* Device initialization routine */
1389 static int mvneta_init(struct udevice *dev)
1391 struct eth_pdata *pdata = dev_get_platdata(dev);
1392 struct mvneta_port *pp = dev_get_priv(dev);
1395 pp->tx_ring_size = MVNETA_MAX_TXD;
1396 pp->rx_ring_size = MVNETA_MAX_RXD;
1398 err = mvneta_init2(pp);
1400 dev_err(&pdev->dev, "can't init eth hal\n");
1404 mvneta_mac_addr_set(pp, pdata->enetaddr, rxq_def);
1406 err = mvneta_port_power_up(pp, pp->phy_interface);
1408 dev_err(&pdev->dev, "can't power up port\n");
1412 /* Call open() now as it needs to be done before runing send() */
1418 /* U-Boot only functions follow here */
1420 /* SMI / MDIO functions */
1422 static int smi_wait_ready(struct mvneta_port *pp)
1424 u32 timeout = MVNETA_SMI_TIMEOUT;
1427 /* wait till the SMI is not busy */
1429 /* read smi register */
1430 smi_reg = mvreg_read(pp, MVNETA_SMI);
1431 if (timeout-- == 0) {
1432 printf("Error: SMI busy timeout\n");
1435 } while (smi_reg & MVNETA_SMI_BUSY);
1441 * mvneta_mdio_read - miiphy_read callback function.
1443 * Returns 16bit phy register value, or 0xffff on error
1445 static int mvneta_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
1447 struct mvneta_port *pp = bus->priv;
1451 /* check parameters */
1452 if (addr > MVNETA_PHY_ADDR_MASK) {
1453 printf("Error: Invalid PHY address %d\n", addr);
1457 if (reg > MVNETA_PHY_REG_MASK) {
1458 printf("Err: Invalid register offset %d\n", reg);
1462 /* wait till the SMI is not busy */
1463 if (smi_wait_ready(pp) < 0)
1466 /* fill the phy address and regiser offset and read opcode */
1467 smi_reg = (addr << MVNETA_SMI_DEV_ADDR_OFFS)
1468 | (reg << MVNETA_SMI_REG_ADDR_OFFS)
1469 | MVNETA_SMI_OPCODE_READ;
1471 /* write the smi register */
1472 mvreg_write(pp, MVNETA_SMI, smi_reg);
1474 /* wait till read value is ready */
1475 timeout = MVNETA_SMI_TIMEOUT;
1478 /* read smi register */
1479 smi_reg = mvreg_read(pp, MVNETA_SMI);
1480 if (timeout-- == 0) {
1481 printf("Err: SMI read ready timeout\n");
1484 } while (!(smi_reg & MVNETA_SMI_READ_VALID));
1486 /* Wait for the data to update in the SMI register */
1487 for (timeout = 0; timeout < MVNETA_SMI_TIMEOUT; timeout++)
1490 return mvreg_read(pp, MVNETA_SMI) & MVNETA_SMI_DATA_MASK;
1494 * mvneta_mdio_write - miiphy_write callback function.
1496 * Returns 0 if write succeed, -EINVAL on bad parameters
1499 static int mvneta_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
1502 struct mvneta_port *pp = bus->priv;
1505 /* check parameters */
1506 if (addr > MVNETA_PHY_ADDR_MASK) {
1507 printf("Error: Invalid PHY address %d\n", addr);
1511 if (reg > MVNETA_PHY_REG_MASK) {
1512 printf("Err: Invalid register offset %d\n", reg);
1516 /* wait till the SMI is not busy */
1517 if (smi_wait_ready(pp) < 0)
1520 /* fill the phy addr and reg offset and write opcode and data */
1521 smi_reg = value << MVNETA_SMI_DATA_OFFS;
1522 smi_reg |= (addr << MVNETA_SMI_DEV_ADDR_OFFS)
1523 | (reg << MVNETA_SMI_REG_ADDR_OFFS);
1524 smi_reg &= ~MVNETA_SMI_OPCODE_READ;
1526 /* write the smi register */
1527 mvreg_write(pp, MVNETA_SMI, smi_reg);
1532 static int mvneta_start(struct udevice *dev)
1534 struct mvneta_port *pp = dev_get_priv(dev);
1535 struct phy_device *phydev;
1537 mvneta_port_power_up(pp, pp->phy_interface);
1539 if (!pp->init || pp->link == 0) {
1540 if (mvneta_port_is_fixed_link(pp)) {
1547 val = MVNETA_GMAC_FORCE_LINK_UP |
1548 MVNETA_GMAC_IB_BYPASS_AN_EN |
1549 MVNETA_GMAC_SET_FC_EN |
1550 MVNETA_GMAC_ADVERT_FC_EN |
1551 MVNETA_GMAC_SAMPLE_TX_CFG_EN;
1554 val |= MVNETA_GMAC_CONFIG_FULL_DUPLEX;
1556 if (pp->speed == SPEED_1000)
1557 val |= MVNETA_GMAC_CONFIG_GMII_SPEED;
1558 else if (pp->speed == SPEED_100)
1559 val |= MVNETA_GMAC_CONFIG_MII_SPEED;
1561 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
1563 /* Set phy address of the port */
1564 mvreg_write(pp, MVNETA_PHY_ADDR, pp->phyaddr);
1566 phydev = phy_connect(pp->bus, pp->phyaddr, dev,
1569 printf("phy_connect failed\n");
1573 pp->phydev = phydev;
1575 phy_startup(phydev);
1576 if (!phydev->link) {
1577 printf("%s: No link.\n", phydev->dev->name);
1581 /* Full init on first call */
1588 /* Upon all following calls, this is enough */
1590 mvneta_port_enable(pp);
1595 static int mvneta_send(struct udevice *dev, void *packet, int length)
1597 struct mvneta_port *pp = dev_get_priv(dev);
1598 struct mvneta_tx_queue *txq = &pp->txqs[0];
1599 struct mvneta_tx_desc *tx_desc;
1603 /* Get a descriptor for the first part of the packet */
1604 tx_desc = mvneta_txq_next_desc_get(txq);
1606 tx_desc->buf_phys_addr = (u32)(uintptr_t)packet;
1607 tx_desc->data_size = length;
1608 flush_dcache_range((ulong)packet,
1609 (ulong)packet + ALIGN(length, PKTALIGN));
1611 /* First and Last descriptor */
1612 tx_desc->command = MVNETA_TX_L4_CSUM_NOT | MVNETA_TXD_FLZ_DESC;
1613 mvneta_txq_pend_desc_add(pp, txq, 1);
1615 /* Wait for packet to be sent (queue might help with speed here) */
1616 sent_desc = mvneta_txq_sent_desc_num_get(pp, txq);
1617 while (!sent_desc) {
1618 if (timeout++ > 10000) {
1619 printf("timeout: packet not sent\n");
1622 sent_desc = mvneta_txq_sent_desc_num_get(pp, txq);
1625 /* txDone has increased - hw sent packet */
1626 mvneta_txq_sent_desc_dec(pp, txq, sent_desc);
1631 static int mvneta_recv(struct udevice *dev, int flags, uchar **packetp)
1633 struct mvneta_port *pp = dev_get_priv(dev);
1635 struct mvneta_rx_queue *rxq;
1639 rxq = mvneta_rxq_handle_get(pp, rxq_def);
1640 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
1643 struct mvneta_rx_desc *rx_desc;
1644 unsigned char *data;
1648 * No cache invalidation needed here, since the desc's are
1649 * located in a uncached memory region
1651 rx_desc = mvneta_rxq_next_desc_get(rxq);
1653 rx_status = rx_desc->status;
1654 if (!mvneta_rxq_desc_is_first_last(rx_status) ||
1655 (rx_status & MVNETA_RXD_ERR_SUMMARY)) {
1656 mvneta_rx_error(pp, rx_desc);
1657 /* leave the descriptor untouched */
1661 /* 2 bytes for marvell header. 4 bytes for crc */
1662 rx_bytes = rx_desc->data_size - 6;
1664 /* give packet to stack - skip on first 2 bytes */
1665 data = (u8 *)(uintptr_t)rx_desc->buf_cookie + 2;
1667 * No cache invalidation needed here, since the rx_buffer's are
1668 * located in a uncached memory region
1673 * Only mark one descriptor as free
1674 * since only one was processed
1676 mvneta_rxq_desc_num_update(pp, rxq, 1, 1);
1682 static int mvneta_probe(struct udevice *dev)
1684 struct eth_pdata *pdata = dev_get_platdata(dev);
1685 struct mvneta_port *pp = dev_get_priv(dev);
1686 void *blob = (void *)gd->fdt_blob;
1687 int node = dev_of_offset(dev);
1688 struct mii_dev *bus;
1695 * Allocate buffer area for descs and rx_buffers. This is only
1696 * done once for all interfaces. As only one interface can
1697 * be active. Make this area DMA safe by disabling the D-cache
1699 if (!buffer_loc.tx_descs) {
1702 /* Align buffer area for descs and rx_buffers to 1MiB */
1703 bd_space = memalign(1 << MMU_SECTION_SHIFT, BD_SPACE);
1704 flush_dcache_range((ulong)bd_space, (ulong)bd_space + BD_SPACE);
1705 mmu_set_region_dcache_behaviour((phys_addr_t)bd_space, BD_SPACE,
1707 buffer_loc.tx_descs = (struct mvneta_tx_desc *)bd_space;
1708 size = roundup(MVNETA_MAX_TXD * sizeof(struct mvneta_tx_desc),
1710 memset(buffer_loc.tx_descs, 0, size);
1711 buffer_loc.rx_descs = (struct mvneta_rx_desc *)
1712 ((phys_addr_t)bd_space + size);
1713 size += roundup(MVNETA_MAX_RXD * sizeof(struct mvneta_rx_desc),
1715 buffer_loc.rx_buffers = (phys_addr_t)(bd_space + size);
1718 pp->base = (void __iomem *)pdata->iobase;
1720 /* Configure MBUS address windows */
1721 if (device_is_compatible(dev, "marvell,armada-3700-neta"))
1722 mvneta_bypass_mbus_windows(pp);
1724 mvneta_conf_mbus_windows(pp);
1726 /* PHY interface is already decoded in mvneta_ofdata_to_platdata() */
1727 pp->phy_interface = pdata->phy_interface;
1729 /* fetch 'fixed-link' property from 'neta' node */
1730 fl_node = fdt_subnode_offset(blob, node, "fixed-link");
1731 if (fl_node != -FDT_ERR_NOTFOUND) {
1732 /* set phy_addr to invalid value for fixed link */
1733 pp->phyaddr = PHY_MAX_ADDR + 1;
1734 pp->duplex = fdtdec_get_bool(blob, fl_node, "full-duplex");
1735 pp->speed = fdtdec_get_int(blob, fl_node, "speed", 0);
1737 /* Now read phyaddr from DT */
1738 addr = fdtdec_get_int(blob, node, "phy", 0);
1739 addr = fdt_node_offset_by_phandle(blob, addr);
1740 pp->phyaddr = fdtdec_get_int(blob, addr, "reg", 0);
1745 printf("Failed to allocate MDIO bus\n");
1749 bus->read = mvneta_mdio_read;
1750 bus->write = mvneta_mdio_write;
1751 snprintf(bus->name, sizeof(bus->name), dev->name);
1752 bus->priv = (void *)pp;
1755 ret = mdio_register(bus);
1759 #if CONFIG_IS_ENABLED(DM_GPIO)
1760 gpio_request_by_name(dev, "phy-reset-gpios", 0,
1761 &pp->phy_reset_gpio, GPIOD_IS_OUT);
1763 if (dm_gpio_is_valid(&pp->phy_reset_gpio)) {
1764 dm_gpio_set_value(&pp->phy_reset_gpio, 1);
1766 dm_gpio_set_value(&pp->phy_reset_gpio, 0);
1770 return board_network_enable(bus);
1773 static void mvneta_stop(struct udevice *dev)
1775 struct mvneta_port *pp = dev_get_priv(dev);
1777 mvneta_port_down(pp);
1778 mvneta_port_disable(pp);
1781 static const struct eth_ops mvneta_ops = {
1782 .start = mvneta_start,
1783 .send = mvneta_send,
1784 .recv = mvneta_recv,
1785 .stop = mvneta_stop,
1786 .write_hwaddr = mvneta_write_hwaddr,
1789 static int mvneta_ofdata_to_platdata(struct udevice *dev)
1791 struct eth_pdata *pdata = dev_get_platdata(dev);
1792 const char *phy_mode;
1794 pdata->iobase = devfdt_get_addr(dev);
1796 /* Get phy-mode / phy_interface from DT */
1797 pdata->phy_interface = -1;
1798 phy_mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy-mode",
1801 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
1802 if (pdata->phy_interface == -1) {
1803 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
1810 static const struct udevice_id mvneta_ids[] = {
1811 { .compatible = "marvell,armada-370-neta" },
1812 { .compatible = "marvell,armada-xp-neta" },
1813 { .compatible = "marvell,armada-3700-neta" },
1817 U_BOOT_DRIVER(mvneta) = {
1820 .of_match = mvneta_ids,
1821 .ofdata_to_platdata = mvneta_ofdata_to_platdata,
1822 .probe = mvneta_probe,
1824 .priv_auto_alloc_size = sizeof(struct mvneta_port),
1825 .platdata_auto_alloc_size = sizeof(struct eth_pdata),