xref: /OK3568_Linux_fs/u-boot/drivers/net/xilinx_ll_temac_fifo.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Xilinx xps_ll_temac ethernet driver for u-boot
3  *
4  * FIFO sub-controller
5  *
6  * Copyright (C) 2011 - 2012 Stephan Linz <linz@li-pro.net>
7  * Copyright (C) 2008 - 2011 Michal Simek <monstr@monstr.eu>
8  * Copyright (C) 2008 - 2011 PetaLogix
9  *
10  * Based on Yoshio Kashiwagi kashiwagi@co-nss.co.jp driver
11  * Copyright (C) 2008 Nissin Systems Co.,Ltd.
12  * March 2008 created
13  *
14  * CREDITS: tsec driver
15  *
16  * SPDX-License-Identifier:	GPL-2.0+
17  *
18  * [0]: http://www.xilinx.com/support/documentation
19  *
20  * [F]:	[0]/ip_documentation/xps_ll_fifo.pdf
21  * [S]:	[0]/ip_documentation/xps_ll_temac.pdf
22  * [A]:	[0]/application_notes/xapp1041.pdf
23  */
24 
25 #include <config.h>
26 #include <common.h>
27 #include <net.h>
28 
29 #include <asm/types.h>
30 #include <asm/io.h>
31 
32 #include "xilinx_ll_temac.h"
33 #include "xilinx_ll_temac_fifo.h"
34 
ll_temac_reset_fifo(struct eth_device * dev)35 int ll_temac_reset_fifo(struct eth_device *dev)
36 {
37 	struct ll_temac *ll_temac = dev->priv;
38 	struct fifo_ctrl *fifo_ctrl = (void *)ll_temac->ctrladdr;
39 
40 	out_be32(&fifo_ctrl->tdfr, LL_FIFO_TDFR_KEY);
41 	out_be32(&fifo_ctrl->rdfr, LL_FIFO_RDFR_KEY);
42 	out_be32(&fifo_ctrl->isr, ~0UL);
43 	out_be32(&fifo_ctrl->ier, 0);
44 
45 	return 0;
46 }
47 
ll_temac_recv_fifo(struct eth_device * dev)48 int ll_temac_recv_fifo(struct eth_device *dev)
49 {
50 	int i, length = 0;
51 	u32 *buf = (u32 *)net_rx_packets[0];
52 	struct ll_temac *ll_temac = dev->priv;
53 	struct fifo_ctrl *fifo_ctrl = (void *)ll_temac->ctrladdr;
54 
55 	if (in_be32(&fifo_ctrl->isr) & LL_FIFO_ISR_RC) {
56 
57 		/* reset isr */
58 		out_be32(&fifo_ctrl->isr, ~0UL);
59 
60 		/*
61 		 * MAYBE here:
62 		 *   while (fifo_ctrl->isr);
63 		 */
64 
65 		/*
66 		 * The length is written (into RLR) by the XPS LL FIFO
67 		 * when the packet is received across the RX LocalLink
68 		 * interface and the receive data FIFO had enough
69 		 * locations that all of the packet data has been saved.
70 		 * The RLR should only be read when a receive packet is
71 		 * available for processing (the receive occupancy is
72 		 * not zero). Once the RLR is read, the receive packet
73 		 * data should be read from the receive data FIFO before
74 		 * the RLR is read again.
75 		 *
76 		 * [F] page 17, Receive Length Register (RLR)
77 		 */
78 		if (in_be32(&fifo_ctrl->rdfo) & LL_FIFO_RDFO_MASK) {
79 			length = in_be32(&fifo_ctrl->rlf) & LL_FIFO_RLF_MASK;
80 		} else {
81 			printf("%s: Got error, no receive occupancy\n",
82 					__func__);
83 			return -1;
84 		}
85 
86 		if (length > PKTSIZE_ALIGN) {
87 			printf("%s: Got error, receive package too big (%i)\n",
88 					__func__, length);
89 			ll_temac_reset_fifo(dev);
90 			return -1;
91 		}
92 
93 		for (i = 0; i < length; i += 4)
94 			*buf++ = in_be32(&fifo_ctrl->rdfd);
95 
96 		net_process_received_packet(net_rx_packets[0], length);
97 	}
98 
99 	return 0;
100 }
101 
ll_temac_send_fifo(struct eth_device * dev,void * packet,int length)102 int ll_temac_send_fifo(struct eth_device *dev, void *packet, int length)
103 {
104 	int i;
105 	u32 *buf = (u32 *)packet;
106 	struct ll_temac *ll_temac = dev->priv;
107 	struct fifo_ctrl *fifo_ctrl = (void *)ll_temac->ctrladdr;
108 
109 	if (length < LL_FIFO_TLF_MIN) {
110 		printf("%s: Got error, transmit package too small (%i)\n",
111 				__func__, length);
112 		return -1;
113 	}
114 
115 	if (length > LL_FIFO_TLF_MAX) {
116 		printf("%s: Got error, transmit package too big (%i)\n",
117 				__func__, length);
118 		return -1;
119 	}
120 
121 	for (i = 0; i < length; i += 4)
122 		out_be32(&fifo_ctrl->tdfd, *buf++);
123 
124 	/*
125 	 * Once the packet length is written to the TLR it is
126 	 * automatically moved to the transmit data FIFO with
127 	 * the packet data freeing up the TLR for another value.
128 	 * The packet length must be written to the TLR after
129 	 * the packet data is written to the transmit data FIFO.
130 	 * It is not valid to write data for multiple packets
131 	 * to the transmit data FIFO before writing the packet
132 	 * length values.
133 	 *
134 	 * [F] page 17, Transmit Length Register (TLR)
135 	 */
136 	out_be32(&fifo_ctrl->tlf, length);
137 
138 	return 0;
139 }
140