Setting up repository
[linux-libre-firmware.git] / ath9k_htc / sboot / magpie_1_1 / sboot / dma_engine / src / desc.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 /*  Copyright (c) 2013 Qualcomm Atheros, All Rights Reserved.           */
37 /*                                                                      */
38 /*  Module Name : desc.c                                                */
39 /*                                                                      */
40 /*  Abstract                                                            */
41 /*      This module contains DMA descriptors handle functions.          */
42 /*                                                                      */
43 /*  ROUTINES                                                            */
44 /*                                                                      */
45 /*      zfDmaInitDescriptor                                             */
46 /*      zfDmaGetPacket                                                  */
47 /*      zfDmaReclaimPacket                                              */
48 /*      zfDmaPutPacket                                                  */
49 /*                                                                      */
50 /*  NOTES                                                               */
51 /*      None                                                            */
52 /*                                                                      */
53 /************************************************************************/
54 #include "dt_defs.h"
55 #include "string.h"
56 //#include "gv_extr.h"
57 #include "reg_defs.h"
58 //#include "uart_extr.h"
59 #include <osapi.h>
60 //#include <HIF_api.h>
61 //#include "HIF_usb.h"
62 #include <Magpie_api.h>
63 #include <vdesc_api.h>
64 #include "desc.h"
65
66 /* Function prototypes */
67 //void zfDmaInitDescriptor(void);
68 struct zsDmaDesc* zfDmaGetPacket(struct zsDmaQueue* q);
69 void zfDmaReclaimPacket(struct zsDmaQueue* q, struct zsDmaDesc* desc);
70 void zfDmaPutPacket(struct zsDmaQueue* q, struct zsDmaDesc* desc);
71
72 /************************************************************************/
73 /*                                                                      */
74 /*    FUNCTION DESCRIPTION                  zfDmaGetPacket              */
75 /*      Get a completed packet with # descriptors. Return the first     */
76 /*      descriptor and pointer the head directly by lastAddr->nextAddr  */
77 /*                                                                      */
78 /*    ROUTINES CALLED                                                   */
79 /*                                                                      */
80 /*    INPUTS                                                            */
81 /*      struct zsDmaQueue* q                                            */
82 /*                                                                      */
83 /*    OUTPUTS                                                           */
84 /*      struct zsDmaDesc* desc                                          */
85 /*                                                                      */
86 /*    AUTHOR                                                            */
87 /*      Stephen Chen   ZyDAS Communication Corporation        2005.10   */
88 /*                                                                      */
89 /*    NOTES                                                             */
90 /*                                                                      */
91 /************************************************************************/
92 struct zsDmaDesc* zfDmaGetPacket(struct zsDmaQueue* q)
93 {
94     struct zsDmaDesc* desc = NULL;
95     
96     if(q->head == q->terminator)
97        return NULL;
98
99     if (((q->head->status & ZM_OWN_BITS_MASK) == ZM_OWN_BITS_SW)
100             || ((q->head->status & ZM_OWN_BITS_MASK) == ZM_OWN_BITS_SE))
101         
102     //if ( (((q->head->status & ZM_OWN_BITS_MASK) == ZM_OWN_BITS_SW) && ((u32_t)q != (u32_t)&zgDnQ))
103     //        || (((q->head->status & ZM_OWN_BITS_MASK) == ZM_OWN_BITS_SE) && ((u32_t)q == (u32_t)&zgDnQ)) )
104         
105     {
106         desc = q->head;
107         
108         q->head = desc->lastAddr->nextAddr;
109     }
110     return desc;
111 }
112
113 /************************************************************************/
114 /*                                                                      */
115 /*    FUNCTION DESCRIPTION                  zfDmaReclaimPacket          */
116 /*      Free descriptor.                                                */
117 /*      Exchange the terminator and the first descriptor of the packet  */
118 /*      for hardware ascy... .                                          */
119 /*                                                                      */
120 /*    ROUTINES CALLED                                                   */
121 /*                                                                      */
122 /*    INPUTS                                                            */
123 /*      struct zsDmaQueue* q                                            */
124 /*      struct zsDmaDesc* desc                                          */
125 /*                                                                      */
126 /*    OUTPUTS                                                           */
127 /*                                                                      */
128 /*    AUTHOR                                                            */
129 /*      Stephen Chen   ZyDAS Communication Corporation        2005.10   */
130 /*                                                                      */
131 /*    NOTES                                                             */
132 /*                                                                      */
133 /************************************************************************/
134 void zfDmaReclaimPacket(struct zsDmaQueue* q, struct zsDmaDesc* desc)
135 {
136     struct zsDmaDesc* tmpDesc;
137     struct zsDmaDesc tdesc;
138     VDESC *vdesc;
139     VDESC *vtermdesc;
140     //int tmp;
141     //u8_t *tmpAddr;
142     
143     /* 1. Set OWN bit to 1 for all TDs to be added, clear ctrl and size */
144     tmpDesc = desc;
145     while (1)
146     {
147         tmpDesc->status = ZM_OWN_BITS_HW;
148         tmpDesc->ctrl = 0;
149         tmpDesc->totalLen = 0;
150    
151 #if ZM_FM_LOOPBACK == 1  
152         vdesc = VDESC_HW_TO_VDESC(tmpDesc);
153         tmpDesc->dataSize = vdesc->buf_size;
154 #endif            
155         
156         //A_PRINTF("tmpDesc->dataSize = %d\n", (u32_t)tmpDesc->dataSize);   
157                 
158         /* TODO : Exception handle */        
159         if (desc->lastAddr == tmpDesc)
160         {
161             break;
162         }
163         tmpDesc = tmpDesc->nextAddr;
164     } 
165     
166     /* 3. Next address of Last TD to be added = first TD */
167     desc->lastAddr->nextAddr = desc;
168     
169     /* 2. Copy first TD to be added to TTD */
170     //zfMemoryCopyInWord(&tdesc, desc, sizeof(struct zsDmaDesc));
171     A_MEMCPY(&tdesc, desc, sizeof(struct zsDmaDesc));
172     
173     /* 4. set first TD OWN bit to 0 */
174     desc->status &= (~ZM_OWN_BITS_MASK);
175     
176     /* 5. Copy TTD to last TD */
177     tdesc.status &= (~ZM_OWN_BITS_MASK);
178       
179     vdesc = VDESC_HW_TO_VDESC(desc);
180     vtermdesc = VDESC_HW_TO_VDESC(q->terminator);
181
182     VDESC_swap_vdesc(vtermdesc, vdesc);     
183     
184     //zfMemoryCopyInWord((void*)q->terminator, (void*)&tdesc, sizeof(struct zsDmaDesc));
185     A_MEMCPY((void*)q->terminator, (void*)&tdesc, sizeof(struct zsDmaDesc));
186     
187     //desc->dataSize = 0;
188     //desc->dataAddr = 0;
189     q->terminator->status |= ZM_OWN_BITS_HW;
190     
191     /* Update terminator pointer */
192     q->terminator = desc; 
193 }
194
195 /************************************************************************/
196 /*                                                                      */
197 /*    FUNCTION DESCRIPTION                  zfDmaPutPacket              */
198 /*      Put a complete packet into the tail of the Queue q.             */
199 /*      Exchange the terminator and the first descriptor of the packet  */
200 /*      for hardware ascy... .                                          */
201 /*                                                                      */
202 /*    ROUTINES CALLED                                                   */
203 /*                                                                      */
204 /*    INPUTS                                                            */
205 /*      struct zsDmaQueue* q                                            */
206 /*      struct zsDmaDesc* desc                                          */
207 /*                                                                      */
208 /*    OUTPUTS                                                           */
209 /*                                                                      */
210 /*    AUTHOR                                                            */
211 /*      Stephen Chen   ZyDAS Communication Corporation        2005.10   */
212 /*                                                                      */
213 /*    NOTES                                                             */
214 /*                                                                      */
215 /************************************************************************/
216 void zfDmaPutPacket(struct zsDmaQueue* q, struct zsDmaDesc* desc)
217 {
218     struct zsDmaDesc* tmpDesc;
219     struct zsDmaDesc tdesc;
220     VDESC *vdesc;
221     VDESC *vtermdesc;
222     //u32_t tmp;
223     //u8_t *tmpAddr;
224                 
225     /* 1. Set OWN bit to 1 for all TDs to be added */
226     tmpDesc = desc;
227     while (1)
228     {
229         tmpDesc->status =
230                 ((tmpDesc->status & (~ZM_OWN_BITS_MASK)) | ZM_OWN_BITS_HW);
231         /* TODO : Exception handle */
232
233         if (desc->lastAddr == tmpDesc)
234         {
235             break;
236         }
237         tmpDesc = tmpDesc->nextAddr;
238     }
239
240     /* 3. Next address of Last TD to be added = first TD */
241     desc->lastAddr->nextAddr = desc;
242     
243     /* If there is only one descriptor, update pointer of last descriptor */
244     if (desc->lastAddr == desc)
245     {
246         desc->lastAddr = q->terminator;
247     }
248     
249     /* 2. Copy first TD to be added to TTD */
250     A_MEMCPY(&tdesc, desc, sizeof(struct zsDmaDesc));
251     //tdesc.dataSize = 0;
252     //tdesc.dataAddr = 0;
253         
254     /* 4. set first TD OWN bit to 0 */
255     desc->status &= (~ZM_OWN_BITS_MASK);
256     
257     /* 5. Copy TTD to last TD */
258     tdesc.status &= (~ZM_OWN_BITS_MASK);
259  
260     vdesc = VDESC_HW_TO_VDESC(desc);    
261     vtermdesc = VDESC_HW_TO_VDESC(q->terminator);
262
263     VDESC_swap_vdesc(vtermdesc, vdesc);    
264     
265     A_MEMCPY((void*)q->terminator, (void*)&tdesc, sizeof(struct zsDmaDesc));
266     q->terminator->status |= ZM_OWN_BITS_HW;
267     /* Update terminator pointer */
268     q->terminator = desc;
269 }
270