Setting up repository
[linux-libre-firmware.git] / ath9k_htc / sboot / magpie_1_1 / sboot / vdesc / src / vdesc.c
1 /*
2  * Copyright (c) 2013 Qualcomm Atheros, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted (subject to the limitations in the
7  * disclaimer below) provided that the following conditions are met:
8  *
9  *  * Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  *  * Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the
15  *    distribution.
16  *
17  *  * Neither the name of Qualcomm Atheros nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific prior written permission.
20  *
21  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
22  * GRANTED BY THIS LICENSE.  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
23  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
24  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
32  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
33  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 /*
36  * @File: vdesc.c
37  * 
38  * @Abstract: 
39  * 
40  * @Notes:
41  */
42
43
44 #include <osapi.h>
45 #include <vdesc_api.h>
46 #include <Magpie_api.h>
47
48 #include "vdesc.h"
49
50 #define VDESC_SIZE              sizeof(VDESC)
51 //#define VDESC_TX_SIZE           sizeof(VDESC_TX)
52 //#define VDESC_RX_SIZE           sizeof(VDESC_RX)
53
54 static VDESC* alloc_rx_desc(void);
55 static VDESC* alloc_tx_desc(void);
56 void _vdesc_init(int nDesc);
57 A_UINT8* _vdesc_get_hw_desc(VDESC *desc);
58 VDESC* _vdesc_alloc_desc();
59 void _vdesc_swap_vdesc(VDESC *dest, VDESC *src);
60
61 struct VDESC_CONTEXT g_vdescCtx;
62
63 void _vdesc_init(int nDesc) 
64 {
65     int i;
66     VDESC *vdesc;  
67     //int nextAddr = dataAddr;
68     //int nDesc = nTxDesc + nRxDesc;
69     
70     // Initialize VDESC_TX for nTxDesc number
71     //vdesc = (VDESC*)dataAddr;
72     vdesc = (VDESC *)A_ALLOCRAM(sizeof(VDESC));
73     vdesc->next_desc = NULL;
74     vdesc->control = 0;
75
76 #if 1
77     g_vdescCtx.free_vdesc_head = vdesc;  
78     
79     for(i=1; i<nDesc; i++)
80     {
81         //nextAddr = dataAddr + i*VDESC_SIZE;
82         //vdesc = (VDESC*)nextAddr;
83         vdesc = (VDESC *)A_ALLOCRAM(sizeof(VDESC));
84         
85         vdesc->control = 0;
86         
87         vdesc->next_desc = g_vdescCtx.free_vdesc_head;
88         g_vdescCtx.free_vdesc_head = vdesc;
89     }    
90     
91     //nextAddr = nextAddr + VDESC_SIZE;
92     //return nextAddr;
93     return;
94 #else                    
95     g_vdescCtx.free_vdesc_tx_head = vdesc;    
96     for(i=1; i<nTxDesc; i++)
97     {
98         nextAddr = dataAddr + i*VDESC_TX_SIZE;
99         vdesc = (VDESC*)nextAddr;
100         vdesc->control = 0;
101         
102         vdesc->next_desc = g_vdescCtx.free_vdesc_tx_head;
103         g_vdescCtx.free_vdesc_tx_head = vdesc;
104     }    
105     
106     nextAddr = nextAddr + VDESC_TX_SIZE;
107     
108     // Initialize VDESC_RX for nTxDesc number
109     vdesc = (VDESC*)nextAddr;
110     vdesc->next_desc = NULL;
111     vdesc->control = 0;
112     
113     g_vdescCtx.free_vdesc_rx_head = vdesc;  
114     for(i=1; i<nRxDesc; i++)
115     {
116         //nextAddr = nextAddr + i*VDESC_RX_SIZE;
117         vdesc = (VDESC*)(nextAddr + i*VDESC_RX_SIZE);
118         vdesc->control = 0;
119         
120         vdesc->next_desc = g_vdescCtx.free_vdesc_rx_head;
121         g_vdescCtx.free_vdesc_rx_head = vdesc;
122     }  
123
124     return (nextAddr + nRxDesc*VDESC_RX_SIZE);   
125 #endif                
126 }
127
128 #if 0
129 static VDESC* alloc_tx_desc(void)
130 {
131     VDESC *allocDesc = NULL;
132     
133     if ( g_vdescCtx.free_vdesc_tx_head != NULL )
134     {
135         allocDesc = g_vdescCtx.free_vdesc_tx_head;
136         
137         //g_vbufCtx.nVbufNum--;
138         
139         g_vdescCtx.free_vdesc_tx_head = allocDesc->next_desc;
140         allocDesc->next_desc = NULL;        
141     }    
142     
143     return allocDesc;
144 }
145
146 static VDESC* alloc_rx_desc(void)
147 {
148     VDESC *allocDesc = NULL;
149     
150     if ( g_vdescCtx.free_vdesc_rx_head != NULL )
151     {
152         allocDesc = g_vdescCtx.free_vdesc_rx_head;
153         //g_vbufCtx.nVbufNum--;
154         
155         g_vdescCtx.free_vdesc_rx_head = allocDesc->next_desc;
156         allocDesc->next_desc = NULL;        
157     }    
158     
159     return allocDesc;    
160 }
161 #endif
162
163 VDESC* _vdesc_alloc_desc()
164 {
165 #if 1
166     VDESC *allocDesc = NULL;
167     
168     if ( g_vdescCtx.free_vdesc_head != NULL )
169     {
170         allocDesc = g_vdescCtx.free_vdesc_head;
171         //g_vbufCtx.nVbufNum--;
172         
173         g_vdescCtx.free_vdesc_head = allocDesc->next_desc;
174         allocDesc->next_desc = NULL;        
175     }    
176     
177     return allocDesc;    
178 #else    
179     if ( type == VDESC_TYPE_RX )
180     {
181         return alloc_rx_desc();
182     }
183     else
184     {
185         return alloc_tx_desc();
186     }
187 #endif    
188 }
189
190 A_UINT8* _vdesc_get_hw_desc(VDESC *desc)
191 {
192 #if 1
193     return desc->hw_desc_buf;
194 #else    
195     if ( type == VDESC_TYPE_RX )
196     {
197         return ((VDESC_RX*)desc)->hw_desc_buf;
198     }
199     else
200     {
201         return ((VDESC_TX*)desc)->hw_desc_buf;
202     }
203 #endif    
204
205
206 void _vdesc_swap_vdesc(VDESC *dest, VDESC *src)
207 {
208     A_UINT32 tmp;
209     A_UINT8 *tmpAddr;
210         
211     tmp = dest->buf_size;
212     dest->buf_size = src->buf_size;
213     src->buf_size = tmp;
214     
215     tmp = dest->data_offset;       
216     dest->data_offset = src->data_offset;
217     src->data_offset = tmp;
218     
219     tmp = dest->data_size;       
220     dest->data_size = src->data_size;
221     src->data_size = tmp;
222         
223     tmp = dest->control;
224     dest->control = src->control;
225     src->control = tmp;
226     
227     tmpAddr = dest->buf_addr;
228     dest->buf_addr = src->buf_addr;
229     src->buf_addr = tmpAddr;     
230 }
231
232 /* the exported entry point into this module. All apis are accessed through
233  * function pointers */
234 void vdesc_module_install(struct vdesc_api *apis)
235 {    
236         /* hook in APIs */
237     apis->_init = _vdesc_init;
238     apis->_alloc_vdesc = _vdesc_alloc_desc;
239     apis->_get_hw_desc = _vdesc_get_hw_desc;
240     apis->_swap_vdesc  = _vdesc_swap_vdesc;
241     
242     //apis->_free_vbuf = _vbuf_free_vbuf;
243     
244         /* save ptr to the ptr to the context for external code to inspect/modify internal module state */
245     //apis->pReserved = &g_pMboxHWContext;
246 }
247  
248