blob: e5edfbeaa66d503558ba10b1d157b9f0ca822b14 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Calvin Johnson781b8382018-03-08 15:30:25 +05302/*
3 * Copyright 2015-2016 Freescale Semiconductor, Inc.
4 * Copyright 2017 NXP
Calvin Johnson781b8382018-03-08 15:30:25 +05305 */
6
Simon Glass0f2af882020-05-10 11:40:05 -06007#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -07008#include <malloc.h>
Calvin Johnson781b8382018-03-08 15:30:25 +05309#include <net/pfe_eth/pfe_eth.h>
10#include <net/pfe_eth/pfe_firmware.h>
11
12static struct tx_desc_s *g_tx_desc;
13static struct rx_desc_s *g_rx_desc;
14
15/*
16 * HIF Rx interface function
17 * Reads the rx descriptor from the current location (rx_to_read).
18 * - If the descriptor has a valid data/pkt, then get the data pointer
19 * - check for the input rx phy number
20 * - increment the rx data pointer by pkt_head_room_size
21 * - decrement the data length by pkt_head_room_size
22 * - handover the packet to caller.
23 *
24 * @param[out] pkt_ptr - Pointer to store rx packet
25 * @param[out] phy_port - Pointer to store recv phy port
26 *
27 * @return -1 if no packet, else return length of packet.
28 */
29int pfe_recv(uchar **pkt_ptr, int *phy_port)
30{
31 struct rx_desc_s *rx_desc = g_rx_desc;
32 struct buf_desc *bd;
33 int len = 0;
34
35 struct hif_header_s *hif_header;
36
37 bd = rx_desc->rx_base + rx_desc->rx_to_read;
38
39 if (readl(&bd->ctrl) & BD_CTRL_DESC_EN)
40 return len; /* No pending Rx packet */
41
42 /* this len include hif_header(8 bytes) */
43 len = readl(&bd->ctrl) & 0xFFFF;
44
45 hif_header = (struct hif_header_s *)DDR_PFE_TO_VIRT(readl(&bd->data));
46
47 /* Get the receive port info from the packet */
48 debug("Pkt received:");
49 debug(" Pkt ptr(%p), len(%d), gemac_port(%d) status(%08x)\n",
50 hif_header, len, hif_header->port_no, readl(&bd->status));
51#ifdef DEBUG
52 {
53 int i;
54 unsigned char *p = (unsigned char *)hif_header;
55
56 for (i = 0; i < len; i++) {
57 if (!(i % 16))
58 printf("\n");
59 printf(" %02x", p[i]);
60 }
61 printf("\n");
62 }
63#endif
64
65 *pkt_ptr = (uchar *)(hif_header + 1);
66 *phy_port = hif_header->port_no;
67 len -= sizeof(struct hif_header_s);
68
69 return len;
70}
71
72/*
73 * HIF function to check the Rx done
74 * This function will check the rx done indication of the current rx_to_read
75 * locations
76 * if success, moves the rx_to_read to next location.
77 */
78int pfe_eth_free_pkt(struct udevice *dev, uchar *packet, int length)
79{
80 struct rx_desc_s *rx_desc = g_rx_desc;
81 struct buf_desc *bd;
82
83 debug("%s:rx_base: %p, rx_to_read: %d\n", __func__, rx_desc->rx_base,
84 rx_desc->rx_to_read);
85
86 bd = rx_desc->rx_base + rx_desc->rx_to_read;
87
88 /* reset the control field */
89 writel((MAX_FRAME_SIZE | BD_CTRL_LIFM | BD_CTRL_DESC_EN
90 | BD_CTRL_DIR), &bd->ctrl);
91 writel(0, &bd->status);
92
93 debug("Rx Done : status: %08x, ctrl: %08x\n", readl(&bd->status),
94 readl(&bd->ctrl));
95
96 /* Give START_STROBE to BDP to fetch the descriptor __NOW__,
97 * BDP need not wait for rx_poll_cycle time to fetch the descriptor,
98 * In idle state (ie., no rx pkt), BDP will not fetch
99 * the descriptor even if strobe is given.
100 */
101 writel((readl(HIF_RX_CTRL) | HIF_CTRL_BDP_CH_START_WSTB), HIF_RX_CTRL);
102
103 /* increment the rx_to_read index to next location */
104 rx_desc->rx_to_read = (rx_desc->rx_to_read + 1)
105 & (rx_desc->rx_ring_size - 1);
106
107 debug("Rx next pkt location: %d\n", rx_desc->rx_to_read);
108
109 return 0;
110}
111
112/*
113 * HIF Tx interface function
114 * This function sends a single packet to PFE from HIF interface.
115 * - No interrupt indication on tx completion.
116 * - Data is copied to tx buffers before tx descriptor is updated
117 * and TX DMA is enabled.
118 *
119 * @param[in] phy_port Phy port number to send out this packet
120 * @param[in] data Pointer to the data
121 * @param[in] length Length of the ethernet packet to be transferred.
122 *
123 * @return -1 if tx Q is full, else returns the tx location where the pkt is
124 * placed.
125 */
126int pfe_send(int phy_port, void *data, int length)
127{
128 struct tx_desc_s *tx_desc = g_tx_desc;
129 struct buf_desc *bd;
130 struct hif_header_s hif_header;
131 u8 *tx_buf_va;
132
133 debug("%s:pkt: %p, len: %d, tx_base: %p, tx_to_send: %d\n", __func__,
134 data, length, tx_desc->tx_base, tx_desc->tx_to_send);
135
136 bd = tx_desc->tx_base + tx_desc->tx_to_send;
137
138 /* check queue-full condition */
139 if (readl(&bd->ctrl) & BD_CTRL_DESC_EN)
140 return -1;
141
142 /* PFE checks for min pkt size */
143 if (length < MIN_PKT_SIZE)
144 length = MIN_PKT_SIZE;
145
146 tx_buf_va = (void *)DDR_PFE_TO_VIRT(readl(&bd->data));
147 debug("%s: tx_buf_va: %p, tx_buf_pa: %08x\n", __func__, tx_buf_va,
148 readl(&bd->data));
149
150 /* Fill the gemac/phy port number to send this packet out */
151 memset(&hif_header, 0, sizeof(struct hif_header_s));
152 hif_header.port_no = phy_port;
153
154 memcpy(tx_buf_va, (u8 *)&hif_header, sizeof(struct hif_header_s));
155 memcpy(tx_buf_va + sizeof(struct hif_header_s), data, length);
156 length += sizeof(struct hif_header_s);
157
158#ifdef DEBUG
159 {
160 int i;
161 unsigned char *p = (unsigned char *)tx_buf_va;
162
163 for (i = 0; i < length; i++) {
164 if (!(i % 16))
165 printf("\n");
166 printf("%02x ", p[i]);
167 }
168 }
169#endif
170
171 debug("Tx Done: status: %08x, ctrl: %08x\n", readl(&bd->status),
172 readl(&bd->ctrl));
173
174 /* fill the tx desc */
175 writel((u32)(BD_CTRL_DESC_EN | BD_CTRL_LIFM | (length & 0xFFFF)),
176 &bd->ctrl);
177 writel(0, &bd->status);
178
179 writel((HIF_CTRL_DMA_EN | HIF_CTRL_BDP_CH_START_WSTB), HIF_TX_CTRL);
180
181 udelay(100);
182
183 return tx_desc->tx_to_send;
184}
185
186/*
187 * HIF function to check the Tx done
188 * This function will check the tx done indication of the current tx_to_send
189 * locations
190 * if success, moves the tx_to_send to next location.
191 *
192 * @return -1 if TX ownership bit is not cleared by hw.
193 * else on success (tx done completion) return zero.
194 */
195int pfe_tx_done(void)
196{
197 struct tx_desc_s *tx_desc = g_tx_desc;
198 struct buf_desc *bd;
199
200 debug("%s:tx_base: %p, tx_to_send: %d\n", __func__, tx_desc->tx_base,
201 tx_desc->tx_to_send);
202
203 bd = tx_desc->tx_base + tx_desc->tx_to_send;
204
205 /* check queue-full condition */
206 if (readl(&bd->ctrl) & BD_CTRL_DESC_EN)
207 return -1;
208
209 /* reset the control field */
210 writel(0, &bd->ctrl);
211 writel(0, &bd->status);
212
213 debug("Tx Done : status: %08x, ctrl: %08x\n", readl(&bd->status),
214 readl(&bd->ctrl));
215
216 /* increment the txtosend index to next location */
217 tx_desc->tx_to_send = (tx_desc->tx_to_send + 1)
218 & (tx_desc->tx_ring_size - 1);
219
220 debug("Tx next pkt location: %d\n", tx_desc->tx_to_send);
221
222 return 0;
223}
224
225/*
226 * Helper function to dump Rx descriptors.
227 */
228static inline void hif_rx_desc_dump(void)
229{
230 struct buf_desc *bd_va;
231 int i;
232 struct rx_desc_s *rx_desc;
233
234 if (!g_rx_desc) {
235 printf("%s: HIF Rx desc no init\n", __func__);
236 return;
237 }
238
239 rx_desc = g_rx_desc;
240 bd_va = rx_desc->rx_base;
241
242 debug("HIF rx desc: base_va: %p, base_pa: %08x\n", rx_desc->rx_base,
243 rx_desc->rx_base_pa);
244 for (i = 0; i < rx_desc->rx_ring_size; i++) {
245 debug("status: %08x, ctrl: %08x, data: %08x, next: 0x%08x\n",
246 readl(&bd_va->status),
247 readl(&bd_va->ctrl),
248 readl(&bd_va->data),
249 readl(&bd_va->next));
250 bd_va++;
251 }
252}
253
254/*
255 * This function mark all Rx descriptors as LAST_BD.
256 */
257void hif_rx_desc_disable(void)
258{
259 int i;
260 struct rx_desc_s *rx_desc;
261 struct buf_desc *bd_va;
262
263 if (!g_rx_desc) {
264 printf("%s: HIF Rx desc not initialized\n", __func__);
265 return;
266 }
267
268 rx_desc = g_rx_desc;
269 bd_va = rx_desc->rx_base;
270
271 for (i = 0; i < rx_desc->rx_ring_size; i++) {
272 writel(readl(&bd_va->ctrl) | BD_CTRL_LAST_BD, &bd_va->ctrl);
273 bd_va++;
274 }
275}
276
277/*
278 * HIF Rx Desc initialization function.
279 */
280static int hif_rx_desc_init(struct pfe_ddr_address *pfe_addr)
281{
282 u32 ctrl;
283 struct buf_desc *bd_va;
284 struct buf_desc *bd_pa;
285 struct rx_desc_s *rx_desc;
286 u32 rx_buf_pa;
287 int i;
288
289 /* sanity check */
290 if (g_rx_desc) {
291 printf("%s: HIF Rx desc re-init request\n", __func__);
292 return 0;
293 }
294
295 rx_desc = (struct rx_desc_s *)malloc(sizeof(struct rx_desc_s));
296 if (!rx_desc) {
297 printf("%s: Memory allocation failure\n", __func__);
298 return -ENOMEM;
299 }
300 memset(rx_desc, 0, sizeof(struct rx_desc_s));
301
302 /* init: Rx ring buffer */
303 rx_desc->rx_ring_size = HIF_RX_DESC_NT;
304
305 /* NOTE: must be 64bit aligned */
306 bd_va = (struct buf_desc *)(pfe_addr->ddr_pfe_baseaddr
307 + RX_BD_BASEADDR);
308 bd_pa = (struct buf_desc *)(pfe_addr->ddr_pfe_phys_baseaddr
309 + RX_BD_BASEADDR);
310
311 rx_desc->rx_base = bd_va;
312 rx_desc->rx_base_pa = (unsigned long)bd_pa;
313
314 rx_buf_pa = pfe_addr->ddr_pfe_phys_baseaddr + HIF_RX_PKT_DDR_BASEADDR;
315
316 debug("%s: Rx desc base: %p, base_pa: %08x, desc_count: %d\n",
317 __func__, rx_desc->rx_base, rx_desc->rx_base_pa,
318 rx_desc->rx_ring_size);
319
320 memset(bd_va, 0, sizeof(struct buf_desc) * rx_desc->rx_ring_size);
321
322 ctrl = (MAX_FRAME_SIZE | BD_CTRL_DESC_EN | BD_CTRL_DIR | BD_CTRL_LIFM);
323
324 for (i = 0; i < rx_desc->rx_ring_size; i++) {
325 writel((unsigned long)(bd_pa + 1), &bd_va->next);
326 writel(ctrl, &bd_va->ctrl);
327 writel(rx_buf_pa + (i * MAX_FRAME_SIZE), &bd_va->data);
328 bd_va++;
329 bd_pa++;
330 }
331 --bd_va;
332 writel((u32)rx_desc->rx_base_pa, &bd_va->next);
333
334 writel(rx_desc->rx_base_pa, HIF_RX_BDP_ADDR);
335 writel((readl(HIF_RX_CTRL) | HIF_CTRL_BDP_CH_START_WSTB), HIF_RX_CTRL);
336
337 g_rx_desc = rx_desc;
338
339 return 0;
340}
341
342/*
343 * Helper function to dump Tx Descriptors.
344 */
345static inline void hif_tx_desc_dump(void)
346{
347 struct tx_desc_s *tx_desc;
348 int i;
349 struct buf_desc *bd_va;
350
351 if (!g_tx_desc) {
352 printf("%s: HIF Tx desc no init\n", __func__);
353 return;
354 }
355
356 tx_desc = g_tx_desc;
357 bd_va = tx_desc->tx_base;
358
359 debug("HIF tx desc: base_va: %p, base_pa: %08x\n", tx_desc->tx_base,
360 tx_desc->tx_base_pa);
361
362 for (i = 0; i < tx_desc->tx_ring_size; i++)
363 bd_va++;
364}
365
366/*
367 * HIF Tx descriptor initialization function.
368 */
369static int hif_tx_desc_init(struct pfe_ddr_address *pfe_addr)
370{
371 struct buf_desc *bd_va;
372 struct buf_desc *bd_pa;
373 int i;
374 struct tx_desc_s *tx_desc;
375 u32 tx_buf_pa;
376
377 /* sanity check */
378 if (g_tx_desc) {
379 printf("%s: HIF Tx desc re-init request\n", __func__);
380 return 0;
381 }
382
383 tx_desc = (struct tx_desc_s *)malloc(sizeof(struct tx_desc_s));
384 if (!tx_desc) {
385 printf("%s:%d:Memory allocation failure\n", __func__,
386 __LINE__);
387 return -ENOMEM;
388 }
389 memset(tx_desc, 0, sizeof(struct tx_desc_s));
390
391 /* init: Tx ring buffer */
392 tx_desc->tx_ring_size = HIF_TX_DESC_NT;
393
394 /* NOTE: must be 64bit aligned */
395 bd_va = (struct buf_desc *)(pfe_addr->ddr_pfe_baseaddr
396 + TX_BD_BASEADDR);
397 bd_pa = (struct buf_desc *)(pfe_addr->ddr_pfe_phys_baseaddr
398 + TX_BD_BASEADDR);
399
400 tx_desc->tx_base_pa = (unsigned long)bd_pa;
401 tx_desc->tx_base = bd_va;
402
403 debug("%s: Tx desc_base: %p, base_pa: %08x, desc_count: %d\n",
404 __func__, tx_desc->tx_base, tx_desc->tx_base_pa,
405 tx_desc->tx_ring_size);
406
407 memset(bd_va, 0, sizeof(struct buf_desc) * tx_desc->tx_ring_size);
408
409 tx_buf_pa = pfe_addr->ddr_pfe_phys_baseaddr + HIF_TX_PKT_DDR_BASEADDR;
410
411 for (i = 0; i < tx_desc->tx_ring_size; i++) {
412 writel((unsigned long)(bd_pa + 1), &bd_va->next);
413 writel(tx_buf_pa + (i * MAX_FRAME_SIZE), &bd_va->data);
414 bd_va++;
415 bd_pa++;
416 }
417 --bd_va;
418 writel((u32)tx_desc->tx_base_pa, &bd_va->next);
419
420 writel(tx_desc->tx_base_pa, HIF_TX_BDP_ADDR);
421
422 g_tx_desc = tx_desc;
423
424 return 0;
425}
426
427/*
428 * PFE/Class initialization.
429 */
430static void pfe_class_init(struct pfe_ddr_address *pfe_addr)
431{
432 struct class_cfg class_cfg = {
433 .route_table_baseaddr = pfe_addr->ddr_pfe_phys_baseaddr +
434 ROUTE_TABLE_BASEADDR,
435 .route_table_hash_bits = ROUTE_TABLE_HASH_BITS,
436 };
437
438 class_init(&class_cfg);
439
440 debug("class init complete\n");
441}
442
443/*
444 * PFE/TMU initialization.
445 */
446static void pfe_tmu_init(struct pfe_ddr_address *pfe_addr)
447{
448 struct tmu_cfg tmu_cfg = {
449 .llm_base_addr = pfe_addr->ddr_pfe_phys_baseaddr
450 + TMU_LLM_BASEADDR,
451 .llm_queue_len = TMU_LLM_QUEUE_LEN,
452 };
453
454 tmu_init(&tmu_cfg);
455
456 debug("tmu init complete\n");
457}
458
459/*
460 * PFE/BMU (both BMU1 & BMU2) initialization.
461 */
462static void pfe_bmu_init(struct pfe_ddr_address *pfe_addr)
463{
464 struct bmu_cfg bmu1_cfg = {
465 .baseaddr = CBUS_VIRT_TO_PFE(LMEM_BASE_ADDR +
466 BMU1_LMEM_BASEADDR),
467 .count = BMU1_BUF_COUNT,
468 .size = BMU1_BUF_SIZE,
469 };
470
471 struct bmu_cfg bmu2_cfg = {
472 .baseaddr = pfe_addr->ddr_pfe_phys_baseaddr + BMU2_DDR_BASEADDR,
473 .count = BMU2_BUF_COUNT,
474 .size = BMU2_BUF_SIZE,
475 };
476
477 bmu_init(BMU1_BASE_ADDR, &bmu1_cfg);
478 debug("bmu1 init: done\n");
479
480 bmu_init(BMU2_BASE_ADDR, &bmu2_cfg);
481 debug("bmu2 init: done\n");
482}
483
484/*
485 * PFE/GPI initialization function.
486 * - egpi1, egpi2, egpi3, hgpi
487 */
488static void pfe_gpi_init(struct pfe_ddr_address *pfe_addr)
489{
490 struct gpi_cfg egpi1_cfg = {
491 .lmem_rtry_cnt = EGPI1_LMEM_RTRY_CNT,
492 .tmlf_txthres = EGPI1_TMLF_TXTHRES,
493 .aseq_len = EGPI1_ASEQ_LEN,
494 };
495
496 struct gpi_cfg egpi2_cfg = {
497 .lmem_rtry_cnt = EGPI2_LMEM_RTRY_CNT,
498 .tmlf_txthres = EGPI2_TMLF_TXTHRES,
499 .aseq_len = EGPI2_ASEQ_LEN,
500 };
501
502 struct gpi_cfg hgpi_cfg = {
503 .lmem_rtry_cnt = HGPI_LMEM_RTRY_CNT,
504 .tmlf_txthres = HGPI_TMLF_TXTHRES,
505 .aseq_len = HGPI_ASEQ_LEN,
506 };
507
508 gpi_init(EGPI1_BASE_ADDR, &egpi1_cfg);
509 debug("GPI1 init complete\n");
510
511 gpi_init(EGPI2_BASE_ADDR, &egpi2_cfg);
512 debug("GPI2 init complete\n");
513
514 gpi_init(HGPI_BASE_ADDR, &hgpi_cfg);
515 debug("HGPI init complete\n");
516}
517
518/*
519 * PFE/HIF initialization function.
520 */
521static int pfe_hif_init(struct pfe_ddr_address *pfe_addr)
522{
523 int ret = 0;
524
525 hif_tx_disable();
526 hif_rx_disable();
527
528 ret = hif_tx_desc_init(pfe_addr);
529 if (ret)
530 return ret;
531 ret = hif_rx_desc_init(pfe_addr);
532 if (ret)
533 return ret;
534
535 hif_init();
536
537 hif_tx_enable();
538 hif_rx_enable();
539
540 hif_rx_desc_dump();
541 hif_tx_desc_dump();
542
543 debug("HIF init complete\n");
544 return ret;
545}
546
547/*
548 * PFE initialization
549 * - Firmware loading (CLASS-PE and TMU-PE)
550 * - BMU1 and BMU2 init
551 * - GEMAC init
552 * - GPI init
553 * - CLASS-PE init
554 * - TMU-PE init
555 * - HIF tx and rx descriptors init
556 *
557 * @param[in] edev Pointer to eth device structure.
558 *
559 * @return 0, on success.
560 */
561static int pfe_hw_init(struct pfe_ddr_address *pfe_addr)
562{
563 int ret = 0;
564
565 debug("%s: start\n", __func__);
566
567 writel(0x3, CLASS_PE_SYS_CLK_RATIO);
568 writel(0x3, TMU_PE_SYS_CLK_RATIO);
569 writel(0x3, UTIL_PE_SYS_CLK_RATIO);
570 udelay(10);
571
572 pfe_class_init(pfe_addr);
573
574 pfe_tmu_init(pfe_addr);
575
576 pfe_bmu_init(pfe_addr);
577
578 pfe_gpi_init(pfe_addr);
579
580 ret = pfe_hif_init(pfe_addr);
581 if (ret)
582 return ret;
583
584 bmu_enable(BMU1_BASE_ADDR);
585 debug("bmu1 enabled\n");
586
587 bmu_enable(BMU2_BASE_ADDR);
588 debug("bmu2 enabled\n");
589
590 debug("%s: done\n", __func__);
591
592 return ret;
593}
594
595/*
596 * PFE driver init function.
597 * - Initializes pfe_lib
598 * - pfe hw init
599 * - fw loading and enables PEs
600 * - should be executed once.
601 *
602 * @param[in] pfe Pointer the pfe control block
603 */
604int pfe_drv_init(struct pfe_ddr_address *pfe_addr)
605{
606 int ret = 0;
607
608 pfe_lib_init();
609
610 ret = pfe_hw_init(pfe_addr);
611 if (ret)
612 return ret;
613
614 /* Load the class,TM, Util fw.
615 * By now pfe is:
616 * - out of reset + disabled + configured.
617 * Fw loading should be done after pfe_hw_init()
618 */
619 /* It loads default inbuilt sbl firmware */
620 pfe_firmware_init();
621
622 return ret;
623}
624
625/*
626 * PFE remove function
627 * - stops PEs
628 * - frees tx/rx descriptor resources
629 * - should be called once.
630 *
631 * @param[in] pfe Pointer to pfe control block.
632 */
633int pfe_eth_remove(struct udevice *dev)
634{
635 if (g_tx_desc)
636 free(g_tx_desc);
637
638 if (g_rx_desc)
639 free(g_rx_desc);
640
641 pfe_firmware_exit();
642
643 return 0;
644}