GNU Linux-libre 4.14.262-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   The full GNU General Public License is included in this distribution in
21   the file called "COPYING".
22
23   Author: Giuseppe Cavallaro <peppe.cavallaro@st.com>
24 *******************************************************************************/
25
26 #ifndef __DESC_COM_H__
27 #define __DESC_COM_H__
28
29 /* Specific functions used for Ring mode */
30
31 /* Enhanced descriptors */
32 static inline void ehn_desc_rx_set_on_ring(struct dma_desc *p, int end,
33                                            int bfsize)
34 {
35         if (bfsize == BUF_SIZE_16KiB)
36                 p->des1 |= cpu_to_le32((BUF_SIZE_8KiB
37                                 << ERDES1_BUFFER2_SIZE_SHIFT)
38                            & ERDES1_BUFFER2_SIZE_MASK);
39
40         if (end)
41                 p->des1 |= cpu_to_le32(ERDES1_END_RING);
42 }
43
44 static inline void enh_desc_end_tx_desc_on_ring(struct dma_desc *p, int end)
45 {
46         if (end)
47                 p->des0 |= cpu_to_le32(ETDES0_END_RING);
48         else
49                 p->des0 &= cpu_to_le32(~ETDES0_END_RING);
50 }
51
52 static inline void enh_set_tx_desc_len_on_ring(struct dma_desc *p, int len)
53 {
54         if (unlikely(len > BUF_SIZE_4KiB)) {
55                 p->des1 |= cpu_to_le32((((len - BUF_SIZE_4KiB)
56                                         << ETDES1_BUFFER2_SIZE_SHIFT)
57                             & ETDES1_BUFFER2_SIZE_MASK) | (BUF_SIZE_4KiB
58                             & ETDES1_BUFFER1_SIZE_MASK));
59         } else
60                 p->des1 |= cpu_to_le32((len & ETDES1_BUFFER1_SIZE_MASK));
61 }
62
63 /* Normal descriptors */
64 static inline void ndesc_rx_set_on_ring(struct dma_desc *p, int end, int bfsize)
65 {
66         if (bfsize >= BUF_SIZE_2KiB) {
67                 int bfsize2;
68
69                 bfsize2 = min(bfsize - BUF_SIZE_2KiB + 1, BUF_SIZE_2KiB - 1);
70                 p->des1 |= cpu_to_le32((bfsize2 << RDES1_BUFFER2_SIZE_SHIFT)
71                             & RDES1_BUFFER2_SIZE_MASK);
72         }
73
74         if (end)
75                 p->des1 |= cpu_to_le32(RDES1_END_RING);
76 }
77
78 static inline void ndesc_end_tx_desc_on_ring(struct dma_desc *p, int end)
79 {
80         if (end)
81                 p->des1 |= cpu_to_le32(TDES1_END_RING);
82         else
83                 p->des1 &= cpu_to_le32(~TDES1_END_RING);
84 }
85
86 static inline void norm_set_tx_desc_len_on_ring(struct dma_desc *p, int len)
87 {
88         if (unlikely(len > BUF_SIZE_2KiB)) {
89                 unsigned int buffer1 = (BUF_SIZE_2KiB - 1)
90                                         & TDES1_BUFFER1_SIZE_MASK;
91                 p->des1 |= cpu_to_le32((((len - buffer1)
92                                         << TDES1_BUFFER2_SIZE_SHIFT)
93                                 & TDES1_BUFFER2_SIZE_MASK) | buffer1);
94         } else
95                 p->des1 |= cpu_to_le32((len & TDES1_BUFFER1_SIZE_MASK));
96 }
97
98 /* Specific functions used for Chain mode */
99
100 /* Enhanced descriptors */
101 static inline void ehn_desc_rx_set_on_chain(struct dma_desc *p)
102 {
103         p->des1 |= cpu_to_le32(ERDES1_SECOND_ADDRESS_CHAINED);
104 }
105
106 static inline void enh_desc_end_tx_desc_on_chain(struct dma_desc *p)
107 {
108         p->des0 |= cpu_to_le32(ETDES0_SECOND_ADDRESS_CHAINED);
109 }
110
111 static inline void enh_set_tx_desc_len_on_chain(struct dma_desc *p, int len)
112 {
113         p->des1 |= cpu_to_le32(len & ETDES1_BUFFER1_SIZE_MASK);
114 }
115
116 /* Normal descriptors */
117 static inline void ndesc_rx_set_on_chain(struct dma_desc *p, int end)
118 {
119         p->des1 |= cpu_to_le32(RDES1_SECOND_ADDRESS_CHAINED);
120 }
121
122 static inline void ndesc_tx_set_on_chain(struct dma_desc *p)
123 {
124         p->des1 |= cpu_to_le32(TDES1_SECOND_ADDRESS_CHAINED);
125 }
126
127 static inline void norm_set_tx_desc_len_on_chain(struct dma_desc *p, int len)
128 {
129         p->des1 |= cpu_to_le32(len & TDES1_BUFFER1_SIZE_MASK);
130 }
131 #endif /* __DESC_COM_H__ */