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 {
38 	p->des1 |= cpu_to_le32(((BUF_SIZE_8KiB - 1)
39 			<< ERDES1_BUFFER2_SIZE_SHIFT)
40 		   & ERDES1_BUFFER2_SIZE_MASK);
41 
42 	if (end)
43 		p->des1 |= cpu_to_le32(ERDES1_END_RING);
44 }
45 
46 static inline void enh_desc_end_tx_desc_on_ring(struct dma_desc *p, int end)
47 {
48 	if (end)
49 		p->des0 |= cpu_to_le32(ETDES0_END_RING);
50 	else
51 		p->des0 &= cpu_to_le32(~ETDES0_END_RING);
52 }
53 
54 static inline void enh_set_tx_desc_len_on_ring(struct dma_desc *p, int len)
55 {
56 	if (unlikely(len > BUF_SIZE_4KiB)) {
57 		p->des1 |= cpu_to_le32((((len - BUF_SIZE_4KiB)
58 					<< ETDES1_BUFFER2_SIZE_SHIFT)
59 			    & ETDES1_BUFFER2_SIZE_MASK) | (BUF_SIZE_4KiB
60 			    & ETDES1_BUFFER1_SIZE_MASK));
61 	} else
62 		p->des1 |= cpu_to_le32((len & ETDES1_BUFFER1_SIZE_MASK));
63 }
64 
65 /* Normal descriptors */
66 static inline void ndesc_rx_set_on_ring(struct dma_desc *p, int end)
67 {
68 	p->des1 |= cpu_to_le32(((BUF_SIZE_2KiB - 1)
69 				<< RDES1_BUFFER2_SIZE_SHIFT)
70 		    & RDES1_BUFFER2_SIZE_MASK);
71 
72 	if (end)
73 		p->des1 |= cpu_to_le32(RDES1_END_RING);
74 }
75 
76 static inline void ndesc_end_tx_desc_on_ring(struct dma_desc *p, int end)
77 {
78 	if (end)
79 		p->des1 |= cpu_to_le32(TDES1_END_RING);
80 	else
81 		p->des1 &= cpu_to_le32(~TDES1_END_RING);
82 }
83 
84 static inline void norm_set_tx_desc_len_on_ring(struct dma_desc *p, int len)
85 {
86 	if (unlikely(len > BUF_SIZE_2KiB)) {
87 		unsigned int buffer1 = (BUF_SIZE_2KiB - 1)
88 					& TDES1_BUFFER1_SIZE_MASK;
89 		p->des1 |= cpu_to_le32((((len - buffer1)
90 					<< TDES1_BUFFER2_SIZE_SHIFT)
91 				& TDES1_BUFFER2_SIZE_MASK) | buffer1);
92 	} else
93 		p->des1 |= cpu_to_le32((len & TDES1_BUFFER1_SIZE_MASK));
94 }
95 
96 /* Specific functions used for Chain mode */
97 
98 /* Enhanced descriptors */
99 static inline void ehn_desc_rx_set_on_chain(struct dma_desc *p)
100 {
101 	p->des1 |= cpu_to_le32(ERDES1_SECOND_ADDRESS_CHAINED);
102 }
103 
104 static inline void enh_desc_end_tx_desc_on_chain(struct dma_desc *p)
105 {
106 	p->des0 |= cpu_to_le32(ETDES0_SECOND_ADDRESS_CHAINED);
107 }
108 
109 static inline void enh_set_tx_desc_len_on_chain(struct dma_desc *p, int len)
110 {
111 	p->des1 |= cpu_to_le32(len & ETDES1_BUFFER1_SIZE_MASK);
112 }
113 
114 /* Normal descriptors */
115 static inline void ndesc_rx_set_on_chain(struct dma_desc *p, int end)
116 {
117 	p->des1 |= cpu_to_le32(RDES1_SECOND_ADDRESS_CHAINED);
118 }
119 
120 static inline void ndesc_tx_set_on_chain(struct dma_desc *p)
121 {
122 	p->des1 |= cpu_to_le32(TDES1_SECOND_ADDRESS_CHAINED);
123 }
124 
125 static inline void norm_set_tx_desc_len_on_chain(struct dma_desc *p, int len)
126 {
127 	p->des1 |= cpu_to_le32(len & TDES1_BUFFER1_SIZE_MASK);
128 }
129 #endif /* __DESC_COM_H__ */
130