GNU Linux-libre 4.9.294-gnu1
[releases.git] / drivers / net / ethernet / stmicro / stmmac / descs_com.h
1 /*******************************************************************************
2   Header File to describe Normal/enhanced descriptor functions used for RING
3   and CHAINED modes.
4
5   Copyright(C) 2011  STMicroelectronics Ltd
6
7   It defines all the functions used to handle the normal/enhanced
8   descriptors in case of the DMA is configured to work in chained or
9   in ring mode.
10
11   This program is free software; you can redistribute it and/or modify it
12   under the terms and conditions of the GNU General Public License,
13   version 2, as published by the Free Software Foundation.
14
15   This program is distributed in the hope it will be useful, but WITHOUT
16   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
18   more details.
19
20   You should have received a copy of the GNU General Public License along with
21   this program; if not, write to the Free Software Foundation, Inc.,
22   51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
23
24   The full GNU General Public License is included in this distribution in
25   the file called "COPYING".
26
27   Author: Giuseppe Cavallaro <peppe.cavallaro@st.com>
28 *******************************************************************************/
29
30 #ifndef __DESC_COM_H__
31 #define __DESC_COM_H__
32
33 /* Specific functions used for Ring mode */
34
35 /* Enhanced descriptors */
36 static inline void ehn_desc_rx_set_on_ring(struct dma_desc *p, int end,
37                                            int bfsize)
38 {
39         if (bfsize == BUF_SIZE_16KiB)
40                 p->des1 |= cpu_to_le32((BUF_SIZE_8KiB
41                                 << ERDES1_BUFFER2_SIZE_SHIFT)
42                 & ERDES1_BUFFER2_SIZE_MASK);
43
44
45         if (end)
46                 p->des1 |= cpu_to_le32(ERDES1_END_RING);
47 }
48
49 static inline void enh_desc_end_tx_desc_on_ring(struct dma_desc *p, int end)
50 {
51         if (end)
52                 p->des0 |= cpu_to_le32(ETDES0_END_RING);
53         else
54                 p->des0 &= cpu_to_le32(~ETDES0_END_RING);
55 }
56
57 static inline void enh_set_tx_desc_len_on_ring(struct dma_desc *p, int len)
58 {
59         if (unlikely(len > BUF_SIZE_4KiB)) {
60                 p->des1 |= cpu_to_le32((((len - BUF_SIZE_4KiB)
61                                         << ETDES1_BUFFER2_SIZE_SHIFT)
62                             & ETDES1_BUFFER2_SIZE_MASK) | (BUF_SIZE_4KiB
63                             & ETDES1_BUFFER1_SIZE_MASK));
64         } else
65                 p->des1 |= cpu_to_le32((len & ETDES1_BUFFER1_SIZE_MASK));
66 }
67
68 /* Normal descriptors */
69 static inline void ndesc_rx_set_on_ring(struct dma_desc *p, int end, int bfsize)
70 {
71         if (bfsize >= BUF_SIZE_2KiB) {
72                 int bfsize2;
73
74                 bfsize2 = min(bfsize - BUF_SIZE_2KiB + 1, BUF_SIZE_2KiB - 1);
75                 p->des1 |= cpu_to_le32((bfsize2 << RDES1_BUFFER2_SIZE_SHIFT)
76                             & RDES1_BUFFER2_SIZE_MASK);
77         }
78
79         if (end)
80                 p->des1 |= cpu_to_le32(RDES1_END_RING);
81 }
82
83 static inline void ndesc_end_tx_desc_on_ring(struct dma_desc *p, int end)
84 {
85         if (end)
86                 p->des1 |= cpu_to_le32(TDES1_END_RING);
87         else
88                 p->des1 &= cpu_to_le32(~TDES1_END_RING);
89 }
90
91 static inline void norm_set_tx_desc_len_on_ring(struct dma_desc *p, int len)
92 {
93         if (unlikely(len > BUF_SIZE_2KiB)) {
94                 unsigned int buffer1 = (BUF_SIZE_2KiB - 1)
95                                         & TDES1_BUFFER1_SIZE_MASK;
96                 p->des1 |= cpu_to_le32((((len - buffer1)
97                                         << TDES1_BUFFER2_SIZE_SHIFT)
98                                 & TDES1_BUFFER2_SIZE_MASK) | buffer1);
99         } else
100                 p->des1 |= cpu_to_le32((len & TDES1_BUFFER1_SIZE_MASK));
101 }
102
103 /* Specific functions used for Chain mode */
104
105 /* Enhanced descriptors */
106 static inline void ehn_desc_rx_set_on_chain(struct dma_desc *p)
107 {
108         p->des1 |= cpu_to_le32(ERDES1_SECOND_ADDRESS_CHAINED);
109 }
110
111 static inline void enh_desc_end_tx_desc_on_chain(struct dma_desc *p)
112 {
113         p->des0 |= cpu_to_le32(ETDES0_SECOND_ADDRESS_CHAINED);
114 }
115
116 static inline void enh_set_tx_desc_len_on_chain(struct dma_desc *p, int len)
117 {
118         p->des1 |= cpu_to_le32(len & ETDES1_BUFFER1_SIZE_MASK);
119 }
120
121 /* Normal descriptors */
122 static inline void ndesc_rx_set_on_chain(struct dma_desc *p, int end)
123 {
124         p->des1 |= cpu_to_le32(RDES1_SECOND_ADDRESS_CHAINED);
125 }
126
127 static inline void ndesc_tx_set_on_chain(struct dma_desc *p)
128 {
129         p->des1 |= cpu_to_le32(TDES1_SECOND_ADDRESS_CHAINED);
130 }
131
132 static inline void norm_set_tx_desc_len_on_chain(struct dma_desc *p, int len)
133 {
134         p->des1 |= cpu_to_le32(len & TDES1_BUFFER1_SIZE_MASK);
135 }
136 #endif /* __DESC_COM_H__ */