1 // SPDX-License-Identifier: GPL-2.0
3 * Driver for Marvell NETA network card for Armada XP and Armada 370 SoCs.
8 * Based on the Linux version which is:
9 * Copyright (C) 2012 Marvell
23 #include <linux/errno.h>
27 #include <asm/arch/cpu.h>
28 #include <asm/arch/soc.h>
29 #include <linux/compat.h>
30 #include <linux/mbus.h>
31 #include <asm-generic/gpio.h>
33 DECLARE_GLOBAL_DATA_PTR;
35 #if !defined(CONFIG_PHYLIB)
36 # error Marvell mvneta requires PHYLIB
39 #define CONFIG_NR_CPUS 1
40 #define ETH_HLEN 14 /* Total octets in header */
42 /* 2(HW hdr) 14(MAC hdr) 4(CRC) 32(extra for cache prefetch) */
43 #define WRAP (2 + ETH_HLEN + 4 + 32)
45 #define RX_BUFFER_SIZE (ALIGN(MTU + WRAP, ARCH_DMA_MINALIGN))
47 #define MVNETA_SMI_TIMEOUT 10000
50 #define MVNETA_RXQ_CONFIG_REG(q) (0x1400 + ((q) << 2))
51 #define MVNETA_RXQ_HW_BUF_ALLOC BIT(1)
52 #define MVNETA_RXQ_PKT_OFFSET_ALL_MASK (0xf << 8)
53 #define MVNETA_RXQ_PKT_OFFSET_MASK(offs) ((offs) << 8)
54 #define MVNETA_RXQ_THRESHOLD_REG(q) (0x14c0 + ((q) << 2))
55 #define MVNETA_RXQ_NON_OCCUPIED(v) ((v) << 16)
56 #define MVNETA_RXQ_BASE_ADDR_REG(q) (0x1480 + ((q) << 2))
57 #define MVNETA_RXQ_SIZE_REG(q) (0x14a0 + ((q) << 2))
58 #define MVNETA_RXQ_BUF_SIZE_SHIFT 19
59 #define MVNETA_RXQ_BUF_SIZE_MASK (0x1fff << 19)
60 #define MVNETA_RXQ_STATUS_REG(q) (0x14e0 + ((q) << 2))
61 #define MVNETA_RXQ_OCCUPIED_ALL_MASK 0x3fff
62 #define MVNETA_RXQ_STATUS_UPDATE_REG(q) (0x1500 + ((q) << 2))
63 #define MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT 16
64 #define MVNETA_RXQ_ADD_NON_OCCUPIED_MAX 255
65 #define MVNETA_PORT_RX_RESET 0x1cc0
66 #define MVNETA_PORT_RX_DMA_RESET BIT(0)
67 #define MVNETA_PHY_ADDR 0x2000
68 #define MVNETA_PHY_ADDR_MASK 0x1f
69 #define MVNETA_SMI 0x2004
70 #define MVNETA_PHY_REG_MASK 0x1f
71 /* SMI register fields */
72 #define MVNETA_SMI_DATA_OFFS 0 /* Data */
73 #define MVNETA_SMI_DATA_MASK (0xffff << MVNETA_SMI_DATA_OFFS)
74 #define MVNETA_SMI_DEV_ADDR_OFFS 16 /* PHY device address */
75 #define MVNETA_SMI_REG_ADDR_OFFS 21 /* PHY device reg addr*/
76 #define MVNETA_SMI_OPCODE_OFFS 26 /* Write/Read opcode */
77 #define MVNETA_SMI_OPCODE_READ (1 << MVNETA_SMI_OPCODE_OFFS)
78 #define MVNETA_SMI_READ_VALID (1 << 27) /* Read Valid */
79 #define MVNETA_SMI_BUSY (1 << 28) /* Busy */
80 #define MVNETA_MBUS_RETRY 0x2010
81 #define MVNETA_UNIT_INTR_CAUSE 0x2080
82 #define MVNETA_UNIT_CONTROL 0x20B0
83 #define MVNETA_PHY_POLLING_ENABLE BIT(1)
84 #define MVNETA_WIN_BASE(w) (0x2200 + ((w) << 3))
85 #define MVNETA_WIN_SIZE(w) (0x2204 + ((w) << 3))
86 #define MVNETA_WIN_REMAP(w) (0x2280 + ((w) << 2))
87 #define MVNETA_WIN_SIZE_MASK (0xffff0000)
88 #define MVNETA_BASE_ADDR_ENABLE 0x2290
89 #define MVNETA_BASE_ADDR_ENABLE_BIT 0x1
90 #define MVNETA_PORT_ACCESS_PROTECT 0x2294
91 #define MVNETA_PORT_ACCESS_PROTECT_WIN0_RW 0x3
92 #define MVNETA_PORT_CONFIG 0x2400
93 #define MVNETA_UNI_PROMISC_MODE BIT(0)
94 #define MVNETA_DEF_RXQ(q) ((q) << 1)
95 #define MVNETA_DEF_RXQ_ARP(q) ((q) << 4)
96 #define MVNETA_TX_UNSET_ERR_SUM BIT(12)
97 #define MVNETA_DEF_RXQ_TCP(q) ((q) << 16)
98 #define MVNETA_DEF_RXQ_UDP(q) ((q) << 19)
99 #define MVNETA_DEF_RXQ_BPDU(q) ((q) << 22)
100 #define MVNETA_RX_CSUM_WITH_PSEUDO_HDR BIT(25)
101 #define MVNETA_PORT_CONFIG_DEFL_VALUE(q) (MVNETA_DEF_RXQ(q) | \
102 MVNETA_DEF_RXQ_ARP(q) | \
103 MVNETA_DEF_RXQ_TCP(q) | \
104 MVNETA_DEF_RXQ_UDP(q) | \
105 MVNETA_DEF_RXQ_BPDU(q) | \
106 MVNETA_TX_UNSET_ERR_SUM | \
107 MVNETA_RX_CSUM_WITH_PSEUDO_HDR)
108 #define MVNETA_PORT_CONFIG_EXTEND 0x2404
109 #define MVNETA_MAC_ADDR_LOW 0x2414
110 #define MVNETA_MAC_ADDR_HIGH 0x2418
111 #define MVNETA_SDMA_CONFIG 0x241c
112 #define MVNETA_SDMA_BRST_SIZE_16 4
113 #define MVNETA_RX_BRST_SZ_MASK(burst) ((burst) << 1)
114 #define MVNETA_RX_NO_DATA_SWAP BIT(4)
115 #define MVNETA_TX_NO_DATA_SWAP BIT(5)
116 #define MVNETA_DESC_SWAP BIT(6)
117 #define MVNETA_TX_BRST_SZ_MASK(burst) ((burst) << 22)
118 #define MVNETA_PORT_STATUS 0x2444
119 #define MVNETA_TX_IN_PRGRS BIT(1)
120 #define MVNETA_TX_FIFO_EMPTY BIT(8)
121 #define MVNETA_RX_MIN_FRAME_SIZE 0x247c
122 #define MVNETA_SERDES_CFG 0x24A0
123 #define MVNETA_SGMII_SERDES_PROTO 0x0cc7
124 #define MVNETA_QSGMII_SERDES_PROTO 0x0667
125 #define MVNETA_TYPE_PRIO 0x24bc
126 #define MVNETA_FORCE_UNI BIT(21)
127 #define MVNETA_TXQ_CMD_1 0x24e4
128 #define MVNETA_TXQ_CMD 0x2448
129 #define MVNETA_TXQ_DISABLE_SHIFT 8
130 #define MVNETA_TXQ_ENABLE_MASK 0x000000ff
131 #define MVNETA_ACC_MODE 0x2500
132 #define MVNETA_CPU_MAP(cpu) (0x2540 + ((cpu) << 2))
133 #define MVNETA_CPU_RXQ_ACCESS_ALL_MASK 0x000000ff
134 #define MVNETA_CPU_TXQ_ACCESS_ALL_MASK 0x0000ff00
135 #define MVNETA_RXQ_TIME_COAL_REG(q) (0x2580 + ((q) << 2))
137 /* Exception Interrupt Port/Queue Cause register */
139 #define MVNETA_INTR_NEW_CAUSE 0x25a0
140 #define MVNETA_INTR_NEW_MASK 0x25a4
142 /* bits 0..7 = TXQ SENT, one bit per queue.
143 * bits 8..15 = RXQ OCCUP, one bit per queue.
144 * bits 16..23 = RXQ FREE, one bit per queue.
145 * bit 29 = OLD_REG_SUM, see old reg ?
146 * bit 30 = TX_ERR_SUM, one bit for 4 ports
147 * bit 31 = MISC_SUM, one bit for 4 ports
149 #define MVNETA_TX_INTR_MASK(nr_txqs) (((1 << nr_txqs) - 1) << 0)
150 #define MVNETA_TX_INTR_MASK_ALL (0xff << 0)
151 #define MVNETA_RX_INTR_MASK(nr_rxqs) (((1 << nr_rxqs) - 1) << 8)
152 #define MVNETA_RX_INTR_MASK_ALL (0xff << 8)
154 #define MVNETA_INTR_OLD_CAUSE 0x25a8
155 #define MVNETA_INTR_OLD_MASK 0x25ac
157 /* Data Path Port/Queue Cause Register */
158 #define MVNETA_INTR_MISC_CAUSE 0x25b0
159 #define MVNETA_INTR_MISC_MASK 0x25b4
160 #define MVNETA_INTR_ENABLE 0x25b8
162 #define MVNETA_RXQ_CMD 0x2680
163 #define MVNETA_RXQ_DISABLE_SHIFT 8
164 #define MVNETA_RXQ_ENABLE_MASK 0x000000ff
165 #define MVETH_TXQ_TOKEN_COUNT_REG(q) (0x2700 + ((q) << 4))
166 #define MVETH_TXQ_TOKEN_CFG_REG(q) (0x2704 + ((q) << 4))
167 #define MVNETA_GMAC_CTRL_0 0x2c00
168 #define MVNETA_GMAC_MAX_RX_SIZE_SHIFT 2
169 #define MVNETA_GMAC_MAX_RX_SIZE_MASK 0x7ffc
170 #define MVNETA_GMAC0_PORT_ENABLE BIT(0)
171 #define MVNETA_GMAC_CTRL_2 0x2c08
172 #define MVNETA_GMAC2_PCS_ENABLE BIT(3)
173 #define MVNETA_GMAC2_PORT_RGMII BIT(4)
174 #define MVNETA_GMAC2_PORT_RESET BIT(6)
175 #define MVNETA_GMAC_STATUS 0x2c10
176 #define MVNETA_GMAC_LINK_UP BIT(0)
177 #define MVNETA_GMAC_SPEED_1000 BIT(1)
178 #define MVNETA_GMAC_SPEED_100 BIT(2)
179 #define MVNETA_GMAC_FULL_DUPLEX BIT(3)
180 #define MVNETA_GMAC_RX_FLOW_CTRL_ENABLE BIT(4)
181 #define MVNETA_GMAC_TX_FLOW_CTRL_ENABLE BIT(5)
182 #define MVNETA_GMAC_RX_FLOW_CTRL_ACTIVE BIT(6)
183 #define MVNETA_GMAC_TX_FLOW_CTRL_ACTIVE BIT(7)
184 #define MVNETA_GMAC_AUTONEG_CONFIG 0x2c0c
185 #define MVNETA_GMAC_FORCE_LINK_DOWN BIT(0)
186 #define MVNETA_GMAC_FORCE_LINK_PASS BIT(1)
187 #define MVNETA_GMAC_FORCE_LINK_UP (BIT(0) | BIT(1))
188 #define MVNETA_GMAC_IB_BYPASS_AN_EN BIT(3)
189 #define MVNETA_GMAC_CONFIG_MII_SPEED BIT(5)
190 #define MVNETA_GMAC_CONFIG_GMII_SPEED BIT(6)
191 #define MVNETA_GMAC_AN_SPEED_EN BIT(7)
192 #define MVNETA_GMAC_SET_FC_EN BIT(8)
193 #define MVNETA_GMAC_ADVERT_FC_EN BIT(9)
194 #define MVNETA_GMAC_CONFIG_FULL_DUPLEX BIT(12)
195 #define MVNETA_GMAC_AN_DUPLEX_EN BIT(13)
196 #define MVNETA_GMAC_SAMPLE_TX_CFG_EN BIT(15)
197 #define MVNETA_MIB_COUNTERS_BASE 0x3080
198 #define MVNETA_MIB_LATE_COLLISION 0x7c
199 #define MVNETA_DA_FILT_SPEC_MCAST 0x3400
200 #define MVNETA_DA_FILT_OTH_MCAST 0x3500
201 #define MVNETA_DA_FILT_UCAST_BASE 0x3600
202 #define MVNETA_TXQ_BASE_ADDR_REG(q) (0x3c00 + ((q) << 2))
203 #define MVNETA_TXQ_SIZE_REG(q) (0x3c20 + ((q) << 2))
204 #define MVNETA_TXQ_SENT_THRESH_ALL_MASK 0x3fff0000
205 #define MVNETA_TXQ_SENT_THRESH_MASK(coal) ((coal) << 16)
206 #define MVNETA_TXQ_UPDATE_REG(q) (0x3c60 + ((q) << 2))
207 #define MVNETA_TXQ_DEC_SENT_SHIFT 16
208 #define MVNETA_TXQ_STATUS_REG(q) (0x3c40 + ((q) << 2))
209 #define MVNETA_TXQ_SENT_DESC_SHIFT 16
210 #define MVNETA_TXQ_SENT_DESC_MASK 0x3fff0000
211 #define MVNETA_PORT_TX_RESET 0x3cf0
212 #define MVNETA_PORT_TX_DMA_RESET BIT(0)
213 #define MVNETA_TX_MTU 0x3e0c
214 #define MVNETA_TX_TOKEN_SIZE 0x3e14
215 #define MVNETA_TX_TOKEN_SIZE_MAX 0xffffffff
216 #define MVNETA_TXQ_TOKEN_SIZE_REG(q) (0x3e40 + ((q) << 2))
217 #define MVNETA_TXQ_TOKEN_SIZE_MAX 0x7fffffff
219 /* Descriptor ring Macros */
220 #define MVNETA_QUEUE_NEXT_DESC(q, index) \
221 (((index) < (q)->last_desc) ? ((index) + 1) : 0)
223 /* Various constants */
226 #define MVNETA_TXDONE_COAL_PKTS 16
227 #define MVNETA_RX_COAL_PKTS 32
228 #define MVNETA_RX_COAL_USEC 100
230 /* The two bytes Marvell header. Either contains a special value used
231 * by Marvell switches when a specific hardware mode is enabled (not
232 * supported by this driver) or is filled automatically by zeroes on
233 * the RX side. Those two bytes being at the front of the Ethernet
234 * header, they allow to have the IP header aligned on a 4 bytes
235 * boundary automatically: the hardware skips those two bytes on its
238 #define MVNETA_MH_SIZE 2
240 #define MVNETA_VLAN_TAG_LEN 4
242 #define MVNETA_CPU_D_CACHE_LINE_SIZE 32
243 #define MVNETA_TX_CSUM_MAX_SIZE 9800
244 #define MVNETA_ACC_MODE_EXT 1
246 /* Timeout constants */
247 #define MVNETA_TX_DISABLE_TIMEOUT_MSEC 1000
248 #define MVNETA_RX_DISABLE_TIMEOUT_MSEC 1000
249 #define MVNETA_TX_FIFO_EMPTY_TIMEOUT 10000
251 #define MVNETA_TX_MTU_MAX 0x3ffff
253 /* Max number of Rx descriptors */
254 #define MVNETA_MAX_RXD 16
256 /* Max number of Tx descriptors */
257 #define MVNETA_MAX_TXD 16
259 /* descriptor aligned size */
260 #define MVNETA_DESC_ALIGNED_SIZE 32
264 struct mvneta_rx_queue *rxqs;
265 struct mvneta_tx_queue *txqs;
271 phy_interface_t phy_interface;
278 struct phy_device *phydev;
279 #if CONFIG_IS_ENABLED(DM_GPIO)
280 struct gpio_desc phy_reset_gpio;
285 /* The mvneta_tx_desc and mvneta_rx_desc structures describe the
286 * layout of the transmit and reception DMA descriptors, and their
287 * layout is therefore defined by the hardware design
290 #define MVNETA_TX_L3_OFF_SHIFT 0
291 #define MVNETA_TX_IP_HLEN_SHIFT 8
292 #define MVNETA_TX_L4_UDP BIT(16)
293 #define MVNETA_TX_L3_IP6 BIT(17)
294 #define MVNETA_TXD_IP_CSUM BIT(18)
295 #define MVNETA_TXD_Z_PAD BIT(19)
296 #define MVNETA_TXD_L_DESC BIT(20)
297 #define MVNETA_TXD_F_DESC BIT(21)
298 #define MVNETA_TXD_FLZ_DESC (MVNETA_TXD_Z_PAD | \
299 MVNETA_TXD_L_DESC | \
301 #define MVNETA_TX_L4_CSUM_FULL BIT(30)
302 #define MVNETA_TX_L4_CSUM_NOT BIT(31)
304 #define MVNETA_RXD_ERR_CRC 0x0
305 #define MVNETA_RXD_ERR_SUMMARY BIT(16)
306 #define MVNETA_RXD_ERR_OVERRUN BIT(17)
307 #define MVNETA_RXD_ERR_LEN BIT(18)
308 #define MVNETA_RXD_ERR_RESOURCE (BIT(17) | BIT(18))
309 #define MVNETA_RXD_ERR_CODE_MASK (BIT(17) | BIT(18))
310 #define MVNETA_RXD_L3_IP4 BIT(25)
311 #define MVNETA_RXD_FIRST_LAST_DESC (BIT(26) | BIT(27))
312 #define MVNETA_RXD_L4_CSUM_OK BIT(30)
314 struct mvneta_tx_desc {
315 u32 command; /* Options used by HW for packet transmitting.*/
316 u16 reserverd1; /* csum_l4 (for future use) */
317 u16 data_size; /* Data size of transmitted packet in bytes */
318 u32 buf_phys_addr; /* Physical addr of transmitted buffer */
319 u32 reserved2; /* hw_cmd - (for future use, PMT) */
320 u32 reserved3[4]; /* Reserved - (for future use) */
323 struct mvneta_rx_desc {
324 u32 status; /* Info about received packet */
325 u16 reserved1; /* pnc_info - (for future use, PnC) */
326 u16 data_size; /* Size of received packet in bytes */
328 u32 buf_phys_addr; /* Physical address of the buffer */
329 u32 reserved2; /* pnc_flow_id (for future use, PnC) */
331 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
332 u16 reserved3; /* prefetch_cmd, for future use */
333 u16 reserved4; /* csum_l4 - (for future use, PnC) */
335 u32 reserved5; /* pnc_extra PnC (for future use, PnC) */
336 u32 reserved6; /* hw_cmd (for future use, PnC and HWF) */
339 struct mvneta_tx_queue {
340 /* Number of this TX queue, in the range 0-7 */
343 /* Number of TX DMA descriptors in the descriptor ring */
346 /* Index of last TX DMA descriptor that was inserted */
349 /* Index of the TX DMA descriptor to be cleaned up */
352 /* Virtual address of the TX DMA descriptors array */
353 struct mvneta_tx_desc *descs;
355 /* DMA address of the TX DMA descriptors array */
356 dma_addr_t descs_phys;
358 /* Index of the last TX DMA descriptor */
361 /* Index of the next TX DMA descriptor to process */
362 int next_desc_to_proc;
365 struct mvneta_rx_queue {
366 /* rx queue number, in the range 0-7 */
369 /* num of rx descriptors in the rx descriptor ring */
372 /* Virtual address of the RX DMA descriptors array */
373 struct mvneta_rx_desc *descs;
375 /* DMA address of the RX DMA descriptors array */
376 dma_addr_t descs_phys;
378 /* Index of the last RX DMA descriptor */
381 /* Index of the next RX DMA descriptor to process */
382 int next_desc_to_proc;
385 /* U-Boot doesn't use the queues, so set the number to 1 */
386 static int rxq_number = 1;
387 static int txq_number = 1;
390 struct buffer_location {
391 struct mvneta_tx_desc *tx_descs;
392 struct mvneta_rx_desc *rx_descs;
397 * All 4 interfaces use the same global buffer, since only one interface
398 * can be enabled at once
400 static struct buffer_location buffer_loc;
403 * Page table entries are set to 1MB, or multiples of 1MB
404 * (not < 1MB). driver uses less bd's so use 1MB bdspace.
406 #define BD_SPACE (1 << 20)
409 * Dummy implementation that can be overwritten by a board
412 __weak int board_network_enable(struct mii_dev *bus)
417 /* Utility/helper methods */
419 /* Write helper method */
420 static void mvreg_write(struct mvneta_port *pp, u32 offset, u32 data)
422 writel(data, pp->base + offset);
425 /* Read helper method */
426 static u32 mvreg_read(struct mvneta_port *pp, u32 offset)
428 return readl(pp->base + offset);
431 /* Clear all MIB counters */
432 static void mvneta_mib_counters_clear(struct mvneta_port *pp)
436 /* Perform dummy reads from MIB counters */
437 for (i = 0; i < MVNETA_MIB_LATE_COLLISION; i += 4)
438 mvreg_read(pp, (MVNETA_MIB_COUNTERS_BASE + i));
441 /* Rx descriptors helper methods */
443 /* Checks whether the RX descriptor having this status is both the first
444 * and the last descriptor for the RX packet. Each RX packet is currently
445 * received through a single RX descriptor, so not having each RX
446 * descriptor with its first and last bits set is an error
448 static int mvneta_rxq_desc_is_first_last(u32 status)
450 return (status & MVNETA_RXD_FIRST_LAST_DESC) ==
451 MVNETA_RXD_FIRST_LAST_DESC;
454 /* Add number of descriptors ready to receive new packets */
455 static void mvneta_rxq_non_occup_desc_add(struct mvneta_port *pp,
456 struct mvneta_rx_queue *rxq,
459 /* Only MVNETA_RXQ_ADD_NON_OCCUPIED_MAX (255) descriptors can
462 while (ndescs > MVNETA_RXQ_ADD_NON_OCCUPIED_MAX) {
463 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
464 (MVNETA_RXQ_ADD_NON_OCCUPIED_MAX <<
465 MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
466 ndescs -= MVNETA_RXQ_ADD_NON_OCCUPIED_MAX;
469 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
470 (ndescs << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
473 /* Get number of RX descriptors occupied by received packets */
474 static int mvneta_rxq_busy_desc_num_get(struct mvneta_port *pp,
475 struct mvneta_rx_queue *rxq)
479 val = mvreg_read(pp, MVNETA_RXQ_STATUS_REG(rxq->id));
480 return val & MVNETA_RXQ_OCCUPIED_ALL_MASK;
483 /* Update num of rx desc called upon return from rx path or
484 * from mvneta_rxq_drop_pkts().
486 static void mvneta_rxq_desc_num_update(struct mvneta_port *pp,
487 struct mvneta_rx_queue *rxq,
488 int rx_done, int rx_filled)
492 if ((rx_done <= 0xff) && (rx_filled <= 0xff)) {
494 (rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT);
495 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
499 /* Only 255 descriptors can be added at once */
500 while ((rx_done > 0) || (rx_filled > 0)) {
501 if (rx_done <= 0xff) {
508 if (rx_filled <= 0xff) {
509 val |= rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
512 val |= 0xff << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
515 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
519 /* Get pointer to next RX descriptor to be processed by SW */
520 static struct mvneta_rx_desc *
521 mvneta_rxq_next_desc_get(struct mvneta_rx_queue *rxq)
523 int rx_desc = rxq->next_desc_to_proc;
525 rxq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(rxq, rx_desc);
526 return rxq->descs + rx_desc;
529 /* Tx descriptors helper methods */
531 /* Update HW with number of TX descriptors to be sent */
532 static void mvneta_txq_pend_desc_add(struct mvneta_port *pp,
533 struct mvneta_tx_queue *txq,
538 /* Only 255 descriptors can be added at once ; Assume caller
539 * process TX descriptors in quanta less than 256
542 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
545 /* Get pointer to next TX descriptor to be processed (send) by HW */
546 static struct mvneta_tx_desc *
547 mvneta_txq_next_desc_get(struct mvneta_tx_queue *txq)
549 int tx_desc = txq->next_desc_to_proc;
551 txq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(txq, tx_desc);
552 return txq->descs + tx_desc;
555 /* Set rxq buf size */
556 static void mvneta_rxq_buf_size_set(struct mvneta_port *pp,
557 struct mvneta_rx_queue *rxq,
562 val = mvreg_read(pp, MVNETA_RXQ_SIZE_REG(rxq->id));
564 val &= ~MVNETA_RXQ_BUF_SIZE_MASK;
565 val |= ((buf_size >> 3) << MVNETA_RXQ_BUF_SIZE_SHIFT);
567 mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), val);
570 static int mvneta_port_is_fixed_link(struct mvneta_port *pp)
572 /* phy_addr is set to invalid value for fixed link */
573 return pp->phyaddr > PHY_MAX_ADDR;
577 /* Start the Ethernet port RX and TX activity */
578 static void mvneta_port_up(struct mvneta_port *pp)
583 /* Enable all initialized TXs. */
584 mvneta_mib_counters_clear(pp);
586 for (queue = 0; queue < txq_number; queue++) {
587 struct mvneta_tx_queue *txq = &pp->txqs[queue];
588 if (txq->descs != NULL)
589 q_map |= (1 << queue);
591 mvreg_write(pp, MVNETA_TXQ_CMD, q_map);
593 /* Enable all initialized RXQs. */
595 for (queue = 0; queue < rxq_number; queue++) {
596 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
597 if (rxq->descs != NULL)
598 q_map |= (1 << queue);
600 mvreg_write(pp, MVNETA_RXQ_CMD, q_map);
603 /* Stop the Ethernet port activity */
604 static void mvneta_port_down(struct mvneta_port *pp)
609 /* Stop Rx port activity. Check port Rx activity. */
610 val = mvreg_read(pp, MVNETA_RXQ_CMD) & MVNETA_RXQ_ENABLE_MASK;
612 /* Issue stop command for active channels only */
614 mvreg_write(pp, MVNETA_RXQ_CMD,
615 val << MVNETA_RXQ_DISABLE_SHIFT);
617 /* Wait for all Rx activity to terminate. */
620 if (count++ >= MVNETA_RX_DISABLE_TIMEOUT_MSEC) {
622 "TIMEOUT for RX stopped ! rx_queue_cmd: 0x08%x\n",
628 val = mvreg_read(pp, MVNETA_RXQ_CMD);
629 } while (val & 0xff);
631 /* Stop Tx port activity. Check port Tx activity. Issue stop
632 * command for active channels only
634 val = (mvreg_read(pp, MVNETA_TXQ_CMD)) & MVNETA_TXQ_ENABLE_MASK;
637 mvreg_write(pp, MVNETA_TXQ_CMD,
638 (val << MVNETA_TXQ_DISABLE_SHIFT));
640 /* Wait for all Tx activity to terminate. */
643 if (count++ >= MVNETA_TX_DISABLE_TIMEOUT_MSEC) {
645 "TIMEOUT for TX stopped status=0x%08x\n",
651 /* Check TX Command reg that all Txqs are stopped */
652 val = mvreg_read(pp, MVNETA_TXQ_CMD);
654 } while (val & 0xff);
656 /* Double check to verify that TX FIFO is empty */
659 if (count++ >= MVNETA_TX_FIFO_EMPTY_TIMEOUT) {
661 "TX FIFO empty timeout status=0x08%x\n",
667 val = mvreg_read(pp, MVNETA_PORT_STATUS);
668 } while (!(val & MVNETA_TX_FIFO_EMPTY) &&
669 (val & MVNETA_TX_IN_PRGRS));
674 /* Enable the port by setting the port enable bit of the MAC control register */
675 static void mvneta_port_enable(struct mvneta_port *pp)
680 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
681 val |= MVNETA_GMAC0_PORT_ENABLE;
682 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
685 /* Disable the port and wait for about 200 usec before retuning */
686 static void mvneta_port_disable(struct mvneta_port *pp)
690 /* Reset the Enable bit in the Serial Control Register */
691 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
692 val &= ~MVNETA_GMAC0_PORT_ENABLE;
693 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
698 /* Multicast tables methods */
700 /* Set all entries in Unicast MAC Table; queue==-1 means reject all */
701 static void mvneta_set_ucast_table(struct mvneta_port *pp, int queue)
709 val = 0x1 | (queue << 1);
710 val |= (val << 24) | (val << 16) | (val << 8);
713 for (offset = 0; offset <= 0xc; offset += 4)
714 mvreg_write(pp, MVNETA_DA_FILT_UCAST_BASE + offset, val);
717 /* Set all entries in Special Multicast MAC Table; queue==-1 means reject all */
718 static void mvneta_set_special_mcast_table(struct mvneta_port *pp, int queue)
726 val = 0x1 | (queue << 1);
727 val |= (val << 24) | (val << 16) | (val << 8);
730 for (offset = 0; offset <= 0xfc; offset += 4)
731 mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + offset, val);
734 /* Set all entries in Other Multicast MAC Table. queue==-1 means reject all */
735 static void mvneta_set_other_mcast_table(struct mvneta_port *pp, int queue)
741 memset(pp->mcast_count, 0, sizeof(pp->mcast_count));
744 memset(pp->mcast_count, 1, sizeof(pp->mcast_count));
745 val = 0x1 | (queue << 1);
746 val |= (val << 24) | (val << 16) | (val << 8);
749 for (offset = 0; offset <= 0xfc; offset += 4)
750 mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + offset, val);
753 /* This method sets defaults to the NETA port:
754 * Clears interrupt Cause and Mask registers.
755 * Clears all MAC tables.
756 * Sets defaults to all registers.
757 * Resets RX and TX descriptor rings.
759 * This method can be called after mvneta_port_down() to return the port
760 * settings to defaults.
762 static void mvneta_defaults_set(struct mvneta_port *pp)
768 /* Clear all Cause registers */
769 mvreg_write(pp, MVNETA_INTR_NEW_CAUSE, 0);
770 mvreg_write(pp, MVNETA_INTR_OLD_CAUSE, 0);
771 mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
773 /* Mask all interrupts */
774 mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
775 mvreg_write(pp, MVNETA_INTR_OLD_MASK, 0);
776 mvreg_write(pp, MVNETA_INTR_MISC_MASK, 0);
777 mvreg_write(pp, MVNETA_INTR_ENABLE, 0);
779 /* Enable MBUS Retry bit16 */
780 mvreg_write(pp, MVNETA_MBUS_RETRY, 0x20);
782 /* Set CPU queue access map - all CPUs have access to all RX
783 * queues and to all TX queues
785 for (cpu = 0; cpu < CONFIG_NR_CPUS; cpu++)
786 mvreg_write(pp, MVNETA_CPU_MAP(cpu),
787 (MVNETA_CPU_RXQ_ACCESS_ALL_MASK |
788 MVNETA_CPU_TXQ_ACCESS_ALL_MASK));
790 /* Reset RX and TX DMAs */
791 mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
792 mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
794 /* Disable Legacy WRR, Disable EJP, Release from reset */
795 mvreg_write(pp, MVNETA_TXQ_CMD_1, 0);
796 for (queue = 0; queue < txq_number; queue++) {
797 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(queue), 0);
798 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(queue), 0);
801 mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
802 mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
804 /* Set Port Acceleration Mode */
805 val = MVNETA_ACC_MODE_EXT;
806 mvreg_write(pp, MVNETA_ACC_MODE, val);
808 /* Update val of portCfg register accordingly with all RxQueue types */
809 val = MVNETA_PORT_CONFIG_DEFL_VALUE(rxq_def);
810 mvreg_write(pp, MVNETA_PORT_CONFIG, val);
813 mvreg_write(pp, MVNETA_PORT_CONFIG_EXTEND, val);
814 mvreg_write(pp, MVNETA_RX_MIN_FRAME_SIZE, 64);
816 /* Build PORT_SDMA_CONFIG_REG */
819 /* Default burst size */
820 val |= MVNETA_TX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
821 val |= MVNETA_RX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
822 val |= MVNETA_RX_NO_DATA_SWAP | MVNETA_TX_NO_DATA_SWAP;
824 /* Assign port SDMA configuration */
825 mvreg_write(pp, MVNETA_SDMA_CONFIG, val);
827 /* Enable PHY polling in hardware if not in fixed-link mode */
828 if (!mvneta_port_is_fixed_link(pp)) {
829 val = mvreg_read(pp, MVNETA_UNIT_CONTROL);
830 val |= MVNETA_PHY_POLLING_ENABLE;
831 mvreg_write(pp, MVNETA_UNIT_CONTROL, val);
834 mvneta_set_ucast_table(pp, -1);
835 mvneta_set_special_mcast_table(pp, -1);
836 mvneta_set_other_mcast_table(pp, -1);
839 /* Set unicast address */
840 static void mvneta_set_ucast_addr(struct mvneta_port *pp, u8 last_nibble,
843 unsigned int unicast_reg;
844 unsigned int tbl_offset;
845 unsigned int reg_offset;
847 /* Locate the Unicast table entry */
848 last_nibble = (0xf & last_nibble);
850 /* offset from unicast tbl base */
851 tbl_offset = (last_nibble / 4) * 4;
853 /* offset within the above reg */
854 reg_offset = last_nibble % 4;
856 unicast_reg = mvreg_read(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset));
859 /* Clear accepts frame bit at specified unicast DA tbl entry */
860 unicast_reg &= ~(0xff << (8 * reg_offset));
862 unicast_reg &= ~(0xff << (8 * reg_offset));
863 unicast_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
866 mvreg_write(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset), unicast_reg);
869 /* Set mac address */
870 static void mvneta_mac_addr_set(struct mvneta_port *pp, unsigned char *addr,
877 mac_l = (addr[4] << 8) | (addr[5]);
878 mac_h = (addr[0] << 24) | (addr[1] << 16) |
879 (addr[2] << 8) | (addr[3] << 0);
881 mvreg_write(pp, MVNETA_MAC_ADDR_LOW, mac_l);
882 mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, mac_h);
885 /* Accept frames of this address */
886 mvneta_set_ucast_addr(pp, addr[5], queue);
889 static int mvneta_write_hwaddr(struct udevice *dev)
891 mvneta_mac_addr_set(dev_get_priv(dev),
892 ((struct eth_pdata *)dev_get_platdata(dev))->enetaddr,
898 /* Handle rx descriptor fill by setting buf_cookie and buf_phys_addr */
899 static void mvneta_rx_desc_fill(struct mvneta_rx_desc *rx_desc,
900 u32 phys_addr, u32 cookie)
902 rx_desc->buf_cookie = cookie;
903 rx_desc->buf_phys_addr = phys_addr;
906 /* Decrement sent descriptors counter */
907 static void mvneta_txq_sent_desc_dec(struct mvneta_port *pp,
908 struct mvneta_tx_queue *txq,
913 /* Only 255 TX descriptors can be updated at once */
914 while (sent_desc > 0xff) {
915 val = 0xff << MVNETA_TXQ_DEC_SENT_SHIFT;
916 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
917 sent_desc = sent_desc - 0xff;
920 val = sent_desc << MVNETA_TXQ_DEC_SENT_SHIFT;
921 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
924 /* Get number of TX descriptors already sent by HW */
925 static int mvneta_txq_sent_desc_num_get(struct mvneta_port *pp,
926 struct mvneta_tx_queue *txq)
931 val = mvreg_read(pp, MVNETA_TXQ_STATUS_REG(txq->id));
932 sent_desc = (val & MVNETA_TXQ_SENT_DESC_MASK) >>
933 MVNETA_TXQ_SENT_DESC_SHIFT;
938 /* Display more error info */
939 static void mvneta_rx_error(struct mvneta_port *pp,
940 struct mvneta_rx_desc *rx_desc)
942 u32 status = rx_desc->status;
944 if (!mvneta_rxq_desc_is_first_last(status)) {
946 "bad rx status %08x (buffer oversize), size=%d\n",
947 status, rx_desc->data_size);
951 switch (status & MVNETA_RXD_ERR_CODE_MASK) {
952 case MVNETA_RXD_ERR_CRC:
953 netdev_err(pp->dev, "bad rx status %08x (crc error), size=%d\n",
954 status, rx_desc->data_size);
956 case MVNETA_RXD_ERR_OVERRUN:
957 netdev_err(pp->dev, "bad rx status %08x (overrun error), size=%d\n",
958 status, rx_desc->data_size);
960 case MVNETA_RXD_ERR_LEN:
961 netdev_err(pp->dev, "bad rx status %08x (max frame length error), size=%d\n",
962 status, rx_desc->data_size);
964 case MVNETA_RXD_ERR_RESOURCE:
965 netdev_err(pp->dev, "bad rx status %08x (resource error), size=%d\n",
966 status, rx_desc->data_size);
971 static struct mvneta_rx_queue *mvneta_rxq_handle_get(struct mvneta_port *pp,
974 return &pp->rxqs[rxq];
978 /* Drop packets received by the RXQ and free buffers */
979 static void mvneta_rxq_drop_pkts(struct mvneta_port *pp,
980 struct mvneta_rx_queue *rxq)
984 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
986 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
989 /* Handle rxq fill: allocates rxq skbs; called when initializing a port */
990 static int mvneta_rxq_fill(struct mvneta_port *pp, struct mvneta_rx_queue *rxq,
995 for (i = 0; i < num; i++) {
998 /* U-Boot special: Fill in the rx buffer addresses */
999 addr = buffer_loc.rx_buffers + (i * RX_BUFFER_SIZE);
1000 mvneta_rx_desc_fill(rxq->descs + i, addr, addr);
1003 /* Add this number of RX descriptors as non occupied (ready to
1006 mvneta_rxq_non_occup_desc_add(pp, rxq, i);
1011 /* Rx/Tx queue initialization/cleanup methods */
1013 /* Create a specified RX queue */
1014 static int mvneta_rxq_init(struct mvneta_port *pp,
1015 struct mvneta_rx_queue *rxq)
1018 rxq->size = pp->rx_ring_size;
1020 /* Allocate memory for RX descriptors */
1021 rxq->descs_phys = (dma_addr_t)rxq->descs;
1022 if (rxq->descs == NULL)
1025 WARN_ON(rxq->descs != PTR_ALIGN(rxq->descs, ARCH_DMA_MINALIGN));
1027 rxq->last_desc = rxq->size - 1;
1029 /* Set Rx descriptors queue starting address */
1030 mvreg_write(pp, MVNETA_RXQ_BASE_ADDR_REG(rxq->id), rxq->descs_phys);
1031 mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), rxq->size);
1033 /* Fill RXQ with buffers from RX pool */
1034 mvneta_rxq_buf_size_set(pp, rxq, RX_BUFFER_SIZE);
1035 mvneta_rxq_fill(pp, rxq, rxq->size);
1040 /* Cleanup Rx queue */
1041 static void mvneta_rxq_deinit(struct mvneta_port *pp,
1042 struct mvneta_rx_queue *rxq)
1044 mvneta_rxq_drop_pkts(pp, rxq);
1048 rxq->next_desc_to_proc = 0;
1049 rxq->descs_phys = 0;
1052 /* Create and initialize a tx queue */
1053 static int mvneta_txq_init(struct mvneta_port *pp,
1054 struct mvneta_tx_queue *txq)
1056 txq->size = pp->tx_ring_size;
1058 /* Allocate memory for TX descriptors */
1059 txq->descs_phys = (dma_addr_t)txq->descs;
1060 if (txq->descs == NULL)
1063 WARN_ON(txq->descs != PTR_ALIGN(txq->descs, ARCH_DMA_MINALIGN));
1065 txq->last_desc = txq->size - 1;
1067 /* Set maximum bandwidth for enabled TXQs */
1068 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0x03ffffff);
1069 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0x3fffffff);
1071 /* Set Tx descriptors queue starting address */
1072 mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), txq->descs_phys);
1073 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), txq->size);
1078 /* Free allocated resources when mvneta_txq_init() fails to allocate memory*/
1079 static void mvneta_txq_deinit(struct mvneta_port *pp,
1080 struct mvneta_tx_queue *txq)
1084 txq->next_desc_to_proc = 0;
1085 txq->descs_phys = 0;
1087 /* Set minimum bandwidth for disabled TXQs */
1088 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0);
1089 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0);
1091 /* Set Tx descriptors queue starting address and size */
1092 mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), 0);
1093 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), 0);
1096 /* Cleanup all Tx queues */
1097 static void mvneta_cleanup_txqs(struct mvneta_port *pp)
1101 for (queue = 0; queue < txq_number; queue++)
1102 mvneta_txq_deinit(pp, &pp->txqs[queue]);
1105 /* Cleanup all Rx queues */
1106 static void mvneta_cleanup_rxqs(struct mvneta_port *pp)
1110 for (queue = 0; queue < rxq_number; queue++)
1111 mvneta_rxq_deinit(pp, &pp->rxqs[queue]);
1115 /* Init all Rx queues */
1116 static int mvneta_setup_rxqs(struct mvneta_port *pp)
1120 for (queue = 0; queue < rxq_number; queue++) {
1121 int err = mvneta_rxq_init(pp, &pp->rxqs[queue]);
1123 netdev_err(pp->dev, "%s: can't create rxq=%d\n",
1125 mvneta_cleanup_rxqs(pp);
1133 /* Init all tx queues */
1134 static int mvneta_setup_txqs(struct mvneta_port *pp)
1138 for (queue = 0; queue < txq_number; queue++) {
1139 int err = mvneta_txq_init(pp, &pp->txqs[queue]);
1141 netdev_err(pp->dev, "%s: can't create txq=%d\n",
1143 mvneta_cleanup_txqs(pp);
1151 static void mvneta_start_dev(struct mvneta_port *pp)
1153 /* start the Rx/Tx activity */
1154 mvneta_port_enable(pp);
1157 static void mvneta_adjust_link(struct udevice *dev)
1159 struct mvneta_port *pp = dev_get_priv(dev);
1160 struct phy_device *phydev = pp->phydev;
1161 int status_change = 0;
1163 if (mvneta_port_is_fixed_link(pp)) {
1164 debug("Using fixed link, skip link adjust\n");
1169 if ((pp->speed != phydev->speed) ||
1170 (pp->duplex != phydev->duplex)) {
1173 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
1174 val &= ~(MVNETA_GMAC_CONFIG_MII_SPEED |
1175 MVNETA_GMAC_CONFIG_GMII_SPEED |
1176 MVNETA_GMAC_CONFIG_FULL_DUPLEX |
1177 MVNETA_GMAC_AN_SPEED_EN |
1178 MVNETA_GMAC_AN_DUPLEX_EN);
1181 val |= MVNETA_GMAC_CONFIG_FULL_DUPLEX;
1183 if (phydev->speed == SPEED_1000)
1184 val |= MVNETA_GMAC_CONFIG_GMII_SPEED;
1186 val |= MVNETA_GMAC_CONFIG_MII_SPEED;
1188 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
1190 pp->duplex = phydev->duplex;
1191 pp->speed = phydev->speed;
1195 if (phydev->link != pp->link) {
1196 if (!phydev->link) {
1201 pp->link = phydev->link;
1205 if (status_change) {
1207 u32 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
1208 val |= (MVNETA_GMAC_FORCE_LINK_PASS |
1209 MVNETA_GMAC_FORCE_LINK_DOWN);
1210 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
1213 mvneta_port_down(pp);
1218 static int mvneta_open(struct udevice *dev)
1220 struct mvneta_port *pp = dev_get_priv(dev);
1223 ret = mvneta_setup_rxqs(pp);
1227 ret = mvneta_setup_txqs(pp);
1231 mvneta_adjust_link(dev);
1233 mvneta_start_dev(pp);
1239 static int mvneta_init2(struct mvneta_port *pp)
1244 mvneta_port_disable(pp);
1246 /* Set port default values */
1247 mvneta_defaults_set(pp);
1249 pp->txqs = kzalloc(txq_number * sizeof(struct mvneta_tx_queue),
1254 /* U-Boot special: use preallocated area */
1255 pp->txqs[0].descs = buffer_loc.tx_descs;
1257 /* Initialize TX descriptor rings */
1258 for (queue = 0; queue < txq_number; queue++) {
1259 struct mvneta_tx_queue *txq = &pp->txqs[queue];
1261 txq->size = pp->tx_ring_size;
1264 pp->rxqs = kzalloc(rxq_number * sizeof(struct mvneta_rx_queue),
1271 /* U-Boot special: use preallocated area */
1272 pp->rxqs[0].descs = buffer_loc.rx_descs;
1274 /* Create Rx descriptor rings */
1275 for (queue = 0; queue < rxq_number; queue++) {
1276 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
1278 rxq->size = pp->rx_ring_size;
1284 /* platform glue : initialize decoding windows */
1287 * Not like A380, in Armada3700, there are two layers of decode windows for GBE:
1288 * First layer is: GbE Address window that resides inside the GBE unit,
1289 * Second layer is: Fabric address window which is located in the NIC400
1291 * To simplify the address decode configuration for Armada3700, we bypass the
1292 * first layer of GBE decode window by setting the first window to 4GB.
1294 static void mvneta_bypass_mbus_windows(struct mvneta_port *pp)
1297 * Set window size to 4GB, to bypass GBE address decode, leave the
1298 * work to MBUS decode window
1300 mvreg_write(pp, MVNETA_WIN_SIZE(0), MVNETA_WIN_SIZE_MASK);
1302 /* Enable GBE address decode window 0 by set bit 0 to 0 */
1303 clrbits_le32(pp->base + MVNETA_BASE_ADDR_ENABLE,
1304 MVNETA_BASE_ADDR_ENABLE_BIT);
1306 /* Set GBE address decode window 0 to full Access (read or write) */
1307 setbits_le32(pp->base + MVNETA_PORT_ACCESS_PROTECT,
1308 MVNETA_PORT_ACCESS_PROTECT_WIN0_RW);
1311 static void mvneta_conf_mbus_windows(struct mvneta_port *pp)
1313 const struct mbus_dram_target_info *dram;
1318 dram = mvebu_mbus_dram_info();
1319 for (i = 0; i < 6; i++) {
1320 mvreg_write(pp, MVNETA_WIN_BASE(i), 0);
1321 mvreg_write(pp, MVNETA_WIN_SIZE(i), 0);
1324 mvreg_write(pp, MVNETA_WIN_REMAP(i), 0);
1330 for (i = 0; i < dram->num_cs; i++) {
1331 const struct mbus_dram_window *cs = dram->cs + i;
1332 mvreg_write(pp, MVNETA_WIN_BASE(i), (cs->base & 0xffff0000) |
1333 (cs->mbus_attr << 8) | dram->mbus_dram_target_id);
1335 mvreg_write(pp, MVNETA_WIN_SIZE(i),
1336 (cs->size - 1) & 0xffff0000);
1338 win_enable &= ~(1 << i);
1339 win_protect |= 3 << (2 * i);
1342 mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable);
1345 /* Power up the port */
1346 static int mvneta_port_power_up(struct mvneta_port *pp, int phy_mode)
1350 /* MAC Cause register should be cleared */
1351 mvreg_write(pp, MVNETA_UNIT_INTR_CAUSE, 0);
1353 ctrl = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
1355 /* Even though it might look weird, when we're configured in
1356 * SGMII or QSGMII mode, the RGMII bit needs to be set.
1359 case PHY_INTERFACE_MODE_QSGMII:
1360 mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_QSGMII_SERDES_PROTO);
1361 ctrl |= MVNETA_GMAC2_PCS_ENABLE | MVNETA_GMAC2_PORT_RGMII;
1363 case PHY_INTERFACE_MODE_SGMII:
1364 mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_SGMII_SERDES_PROTO);
1365 ctrl |= MVNETA_GMAC2_PCS_ENABLE | MVNETA_GMAC2_PORT_RGMII;
1367 case PHY_INTERFACE_MODE_RGMII:
1368 case PHY_INTERFACE_MODE_RGMII_ID:
1369 ctrl |= MVNETA_GMAC2_PORT_RGMII;
1375 /* Cancel Port Reset */
1376 ctrl &= ~MVNETA_GMAC2_PORT_RESET;
1377 mvreg_write(pp, MVNETA_GMAC_CTRL_2, ctrl);
1379 while ((mvreg_read(pp, MVNETA_GMAC_CTRL_2) &
1380 MVNETA_GMAC2_PORT_RESET) != 0)
1386 /* Device initialization routine */
1387 static int mvneta_init(struct udevice *dev)
1389 struct eth_pdata *pdata = dev_get_platdata(dev);
1390 struct mvneta_port *pp = dev_get_priv(dev);
1393 pp->tx_ring_size = MVNETA_MAX_TXD;
1394 pp->rx_ring_size = MVNETA_MAX_RXD;
1396 err = mvneta_init2(pp);
1398 dev_err(&pdev->dev, "can't init eth hal\n");
1402 mvneta_mac_addr_set(pp, pdata->enetaddr, rxq_def);
1404 err = mvneta_port_power_up(pp, pp->phy_interface);
1406 dev_err(&pdev->dev, "can't power up port\n");
1410 /* Call open() now as it needs to be done before runing send() */
1416 /* U-Boot only functions follow here */
1418 /* SMI / MDIO functions */
1420 static int smi_wait_ready(struct mvneta_port *pp)
1422 u32 timeout = MVNETA_SMI_TIMEOUT;
1425 /* wait till the SMI is not busy */
1427 /* read smi register */
1428 smi_reg = mvreg_read(pp, MVNETA_SMI);
1429 if (timeout-- == 0) {
1430 printf("Error: SMI busy timeout\n");
1433 } while (smi_reg & MVNETA_SMI_BUSY);
1439 * mvneta_mdio_read - miiphy_read callback function.
1441 * Returns 16bit phy register value, or 0xffff on error
1443 static int mvneta_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
1445 struct mvneta_port *pp = bus->priv;
1449 /* check parameters */
1450 if (addr > MVNETA_PHY_ADDR_MASK) {
1451 printf("Error: Invalid PHY address %d\n", addr);
1455 if (reg > MVNETA_PHY_REG_MASK) {
1456 printf("Err: Invalid register offset %d\n", reg);
1460 /* wait till the SMI is not busy */
1461 if (smi_wait_ready(pp) < 0)
1464 /* fill the phy address and regiser offset and read opcode */
1465 smi_reg = (addr << MVNETA_SMI_DEV_ADDR_OFFS)
1466 | (reg << MVNETA_SMI_REG_ADDR_OFFS)
1467 | MVNETA_SMI_OPCODE_READ;
1469 /* write the smi register */
1470 mvreg_write(pp, MVNETA_SMI, smi_reg);
1472 /* wait till read value is ready */
1473 timeout = MVNETA_SMI_TIMEOUT;
1476 /* read smi register */
1477 smi_reg = mvreg_read(pp, MVNETA_SMI);
1478 if (timeout-- == 0) {
1479 printf("Err: SMI read ready timeout\n");
1482 } while (!(smi_reg & MVNETA_SMI_READ_VALID));
1484 /* Wait for the data to update in the SMI register */
1485 for (timeout = 0; timeout < MVNETA_SMI_TIMEOUT; timeout++)
1488 return mvreg_read(pp, MVNETA_SMI) & MVNETA_SMI_DATA_MASK;
1492 * mvneta_mdio_write - miiphy_write callback function.
1494 * Returns 0 if write succeed, -EINVAL on bad parameters
1497 static int mvneta_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
1500 struct mvneta_port *pp = bus->priv;
1503 /* check parameters */
1504 if (addr > MVNETA_PHY_ADDR_MASK) {
1505 printf("Error: Invalid PHY address %d\n", addr);
1509 if (reg > MVNETA_PHY_REG_MASK) {
1510 printf("Err: Invalid register offset %d\n", reg);
1514 /* wait till the SMI is not busy */
1515 if (smi_wait_ready(pp) < 0)
1518 /* fill the phy addr and reg offset and write opcode and data */
1519 smi_reg = value << MVNETA_SMI_DATA_OFFS;
1520 smi_reg |= (addr << MVNETA_SMI_DEV_ADDR_OFFS)
1521 | (reg << MVNETA_SMI_REG_ADDR_OFFS);
1522 smi_reg &= ~MVNETA_SMI_OPCODE_READ;
1524 /* write the smi register */
1525 mvreg_write(pp, MVNETA_SMI, smi_reg);
1530 static int mvneta_start(struct udevice *dev)
1532 struct mvneta_port *pp = dev_get_priv(dev);
1533 struct phy_device *phydev;
1535 mvneta_port_power_up(pp, pp->phy_interface);
1537 if (!pp->init || pp->link == 0) {
1538 if (mvneta_port_is_fixed_link(pp)) {
1545 val = MVNETA_GMAC_FORCE_LINK_UP |
1546 MVNETA_GMAC_IB_BYPASS_AN_EN |
1547 MVNETA_GMAC_SET_FC_EN |
1548 MVNETA_GMAC_ADVERT_FC_EN |
1549 MVNETA_GMAC_SAMPLE_TX_CFG_EN;
1552 val |= MVNETA_GMAC_CONFIG_FULL_DUPLEX;
1554 if (pp->speed == SPEED_1000)
1555 val |= MVNETA_GMAC_CONFIG_GMII_SPEED;
1556 else if (pp->speed == SPEED_100)
1557 val |= MVNETA_GMAC_CONFIG_MII_SPEED;
1559 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
1561 /* Set phy address of the port */
1562 mvreg_write(pp, MVNETA_PHY_ADDR, pp->phyaddr);
1564 phydev = phy_connect(pp->bus, pp->phyaddr, dev,
1567 printf("phy_connect failed\n");
1571 pp->phydev = phydev;
1573 phy_startup(phydev);
1574 if (!phydev->link) {
1575 printf("%s: No link.\n", phydev->dev->name);
1579 /* Full init on first call */
1586 /* Upon all following calls, this is enough */
1588 mvneta_port_enable(pp);
1593 static int mvneta_send(struct udevice *dev, void *packet, int length)
1595 struct mvneta_port *pp = dev_get_priv(dev);
1596 struct mvneta_tx_queue *txq = &pp->txqs[0];
1597 struct mvneta_tx_desc *tx_desc;
1601 /* Get a descriptor for the first part of the packet */
1602 tx_desc = mvneta_txq_next_desc_get(txq);
1604 tx_desc->buf_phys_addr = (u32)(uintptr_t)packet;
1605 tx_desc->data_size = length;
1606 flush_dcache_range((ulong)packet,
1607 (ulong)packet + ALIGN(length, PKTALIGN));
1609 /* First and Last descriptor */
1610 tx_desc->command = MVNETA_TX_L4_CSUM_NOT | MVNETA_TXD_FLZ_DESC;
1611 mvneta_txq_pend_desc_add(pp, txq, 1);
1613 /* Wait for packet to be sent (queue might help with speed here) */
1614 sent_desc = mvneta_txq_sent_desc_num_get(pp, txq);
1615 while (!sent_desc) {
1616 if (timeout++ > 10000) {
1617 printf("timeout: packet not sent\n");
1620 sent_desc = mvneta_txq_sent_desc_num_get(pp, txq);
1623 /* txDone has increased - hw sent packet */
1624 mvneta_txq_sent_desc_dec(pp, txq, sent_desc);
1629 static int mvneta_recv(struct udevice *dev, int flags, uchar **packetp)
1631 struct mvneta_port *pp = dev_get_priv(dev);
1633 struct mvneta_rx_queue *rxq;
1637 rxq = mvneta_rxq_handle_get(pp, rxq_def);
1638 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
1641 struct mvneta_rx_desc *rx_desc;
1642 unsigned char *data;
1646 * No cache invalidation needed here, since the desc's are
1647 * located in a uncached memory region
1649 rx_desc = mvneta_rxq_next_desc_get(rxq);
1651 rx_status = rx_desc->status;
1652 if (!mvneta_rxq_desc_is_first_last(rx_status) ||
1653 (rx_status & MVNETA_RXD_ERR_SUMMARY)) {
1654 mvneta_rx_error(pp, rx_desc);
1655 /* leave the descriptor untouched */
1659 /* 2 bytes for marvell header. 4 bytes for crc */
1660 rx_bytes = rx_desc->data_size - 6;
1662 /* give packet to stack - skip on first 2 bytes */
1663 data = (u8 *)(uintptr_t)rx_desc->buf_cookie + 2;
1665 * No cache invalidation needed here, since the rx_buffer's are
1666 * located in a uncached memory region
1671 * Only mark one descriptor as free
1672 * since only one was processed
1674 mvneta_rxq_desc_num_update(pp, rxq, 1, 1);
1680 static int mvneta_probe(struct udevice *dev)
1682 struct eth_pdata *pdata = dev_get_platdata(dev);
1683 struct mvneta_port *pp = dev_get_priv(dev);
1684 void *blob = (void *)gd->fdt_blob;
1685 int node = dev_of_offset(dev);
1686 struct mii_dev *bus;
1693 * Allocate buffer area for descs and rx_buffers. This is only
1694 * done once for all interfaces. As only one interface can
1695 * be active. Make this area DMA safe by disabling the D-cache
1697 if (!buffer_loc.tx_descs) {
1700 /* Align buffer area for descs and rx_buffers to 1MiB */
1701 bd_space = memalign(1 << MMU_SECTION_SHIFT, BD_SPACE);
1702 flush_dcache_range((ulong)bd_space, (ulong)bd_space + BD_SPACE);
1703 mmu_set_region_dcache_behaviour((phys_addr_t)bd_space, BD_SPACE,
1705 buffer_loc.tx_descs = (struct mvneta_tx_desc *)bd_space;
1706 size = roundup(MVNETA_MAX_TXD * sizeof(struct mvneta_tx_desc),
1708 memset(buffer_loc.tx_descs, 0, size);
1709 buffer_loc.rx_descs = (struct mvneta_rx_desc *)
1710 ((phys_addr_t)bd_space + size);
1711 size += roundup(MVNETA_MAX_RXD * sizeof(struct mvneta_rx_desc),
1713 buffer_loc.rx_buffers = (phys_addr_t)(bd_space + size);
1716 pp->base = (void __iomem *)pdata->iobase;
1718 /* Configure MBUS address windows */
1719 if (device_is_compatible(dev, "marvell,armada-3700-neta"))
1720 mvneta_bypass_mbus_windows(pp);
1722 mvneta_conf_mbus_windows(pp);
1724 /* PHY interface is already decoded in mvneta_ofdata_to_platdata() */
1725 pp->phy_interface = pdata->phy_interface;
1727 /* fetch 'fixed-link' property from 'neta' node */
1728 fl_node = fdt_subnode_offset(blob, node, "fixed-link");
1729 if (fl_node != -FDT_ERR_NOTFOUND) {
1730 /* set phy_addr to invalid value for fixed link */
1731 pp->phyaddr = PHY_MAX_ADDR + 1;
1732 pp->duplex = fdtdec_get_bool(blob, fl_node, "full-duplex");
1733 pp->speed = fdtdec_get_int(blob, fl_node, "speed", 0);
1735 /* Now read phyaddr from DT */
1736 addr = fdtdec_get_int(blob, node, "phy", 0);
1737 addr = fdt_node_offset_by_phandle(blob, addr);
1738 pp->phyaddr = fdtdec_get_int(blob, addr, "reg", 0);
1743 printf("Failed to allocate MDIO bus\n");
1747 bus->read = mvneta_mdio_read;
1748 bus->write = mvneta_mdio_write;
1749 snprintf(bus->name, sizeof(bus->name), dev->name);
1750 bus->priv = (void *)pp;
1753 ret = mdio_register(bus);
1757 #if CONFIG_IS_ENABLED(DM_GPIO)
1758 gpio_request_by_name(dev, "phy-reset-gpios", 0,
1759 &pp->phy_reset_gpio, GPIOD_IS_OUT);
1761 if (dm_gpio_is_valid(&pp->phy_reset_gpio)) {
1762 dm_gpio_set_value(&pp->phy_reset_gpio, 1);
1764 dm_gpio_set_value(&pp->phy_reset_gpio, 0);
1768 return board_network_enable(bus);
1771 static void mvneta_stop(struct udevice *dev)
1773 struct mvneta_port *pp = dev_get_priv(dev);
1775 mvneta_port_down(pp);
1776 mvneta_port_disable(pp);
1779 static const struct eth_ops mvneta_ops = {
1780 .start = mvneta_start,
1781 .send = mvneta_send,
1782 .recv = mvneta_recv,
1783 .stop = mvneta_stop,
1784 .write_hwaddr = mvneta_write_hwaddr,
1787 static int mvneta_ofdata_to_platdata(struct udevice *dev)
1789 struct eth_pdata *pdata = dev_get_platdata(dev);
1790 const char *phy_mode;
1792 pdata->iobase = devfdt_get_addr(dev);
1794 /* Get phy-mode / phy_interface from DT */
1795 pdata->phy_interface = -1;
1796 phy_mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy-mode",
1799 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
1800 if (pdata->phy_interface == -1) {
1801 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
1808 static const struct udevice_id mvneta_ids[] = {
1809 { .compatible = "marvell,armada-370-neta" },
1810 { .compatible = "marvell,armada-xp-neta" },
1811 { .compatible = "marvell,armada-3700-neta" },
1815 U_BOOT_DRIVER(mvneta) = {
1818 .of_match = mvneta_ids,
1819 .ofdata_to_platdata = mvneta_ofdata_to_platdata,
1820 .probe = mvneta_probe,
1822 .priv_auto_alloc_size = sizeof(struct mvneta_port),
1823 .platdata_auto_alloc_size = sizeof(struct eth_pdata),