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