carl9170 firmware: remove gcc workaround
[carl9170fw.git] / carlfw / src / dma.c
1 /*
2  * carl9170 firmware - used by the ar9170 wireless device
3  *
4  * DMA descriptor handling functions
5  *
6  * Copyright (c) 2000-2005 ZyDAS Technology Corporation
7  * Copyright (c) 2007-2009 Atheros Communications, Inc.
8  * Copyright    2009    Johannes Berg <johannes@sipsolutions.net>
9  * Copyright 2009-2011  Christian Lamparter <chunkeey@googlemail.com>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License along
22  * with this program; if not, write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25
26 #include "carl9170.h"
27 #include "wl.h"
28 #include "printf.h"
29
30 struct ar9170_dma_memory dma_mem __section(sram);
31
32 static void copy_dma_desc(struct dma_desc *dst,
33                           struct dma_desc *src)
34 {
35         memcpy(dst, src, sizeof(struct dma_desc));
36 }
37
38 static void clear_descriptor(struct dma_desc *d)
39 {
40         d->status = AR9170_OWN_BITS_SW;
41         d->ctrl = 0;
42         d->dataSize = 0;
43         d->totalLen = 0;
44         d->lastAddr = d;
45         d->dataAddr = NULL;
46         d->nextAddr = d;
47 }
48
49 static void fill_descriptor(struct dma_desc *d, uint16_t size, uint8_t *data)
50 {
51         d->status = AR9170_OWN_BITS_SW;
52         d->ctrl = 0;
53         d->dataSize = size;
54         d->totalLen = 0;
55         d->lastAddr = d;
56         d->dataAddr = data;
57         d->nextAddr = NULL;
58 }
59
60 /*
61  *  - Init up_queue, down_queue, tx_queue[5], rx_queue.
62  *  - Setup descriptors and data buffer address.
63  *  - Ring descriptors rx_queue and down_queue by dma_reclaim().
64  *
65  * NOTE: LastAddr tempary point (same) to nextAddr after initialize.
66  *       Because LastAddr is don't care in function dma_reclaim().
67  */
68 void dma_init_descriptors(void)
69 {
70         unsigned int i, j;
71
72         for (i = 0; i < ARRAY_SIZE(dma_mem.terminator); i++)
73                 clear_descriptor(&dma_mem.terminator[i]);
74
75         /* Assign terminators to DMA queues */
76         i = 0;
77         fw.pta.up_queue.head = fw.pta.up_queue.terminator = &dma_mem.terminator[i++];
78         fw.pta.down_queue.head = fw.pta.down_queue.terminator = &dma_mem.terminator[i++];
79         for (j = 0; j < __AR9170_NUM_TX_QUEUES; j++)
80                 fw.wlan.tx_queue[j].head = fw.wlan.tx_queue[j].terminator = &dma_mem.terminator[i++];
81         fw.wlan.tx_retry.head = fw.wlan.tx_retry.terminator = &dma_mem.terminator[i++];
82         fw.wlan.rx_queue.head = fw.wlan.rx_queue.terminator = &dma_mem.terminator[i++];
83         fw.usb.int_desc = &dma_mem.terminator[i++];
84         fw.wlan.fw_desc = &dma_mem.terminator[i++];
85
86 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
87         for (j = 0; j < CARL9170_INTF_NUM; j++)
88                 fw.wlan.cab_queue[j].head = fw.wlan.cab_queue[j].terminator = &dma_mem.terminator[i++];
89 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
90
91         BUILD_BUG_ON(AR9170_TERMINATOR_NUMBER != j);
92
93         DBG("Blocks:%d [tx:%d, rx:%d] Terminators:%d/%d\n",
94             AR9170_BLOCK_NUMBER, AR9170_TX_BLOCK_NUMBER,
95             AR9170_RX_BLOCK_NUMBER, AR9170_TERMINATOR_NUMBER, i);
96
97         /* Init descriptors and memory blocks */
98         for (i = 0; i < AR9170_BLOCK_NUMBER; i++) {
99                 fill_descriptor(&dma_mem.block[i], AR9170_BLOCK_SIZE, dma_mem.data[i].data);
100
101                 if (i < AR9170_TX_BLOCK_NUMBER)
102                         dma_reclaim(&fw.pta.down_queue, &dma_mem.block[i]);
103                 else
104                         dma_reclaim(&fw.wlan.rx_queue, &dma_mem.block[i]);
105         }
106
107         /* Set DMA address registers */
108         set(AR9170_PTA_REG_DN_DMA_ADDRH, (uint32_t) fw.pta.down_queue.head >> 16);
109         set(AR9170_PTA_REG_DN_DMA_ADDRL, (uint32_t) fw.pta.down_queue.head & 0xffff);
110         set(AR9170_PTA_REG_UP_DMA_ADDRH, (uint32_t) fw.pta.up_queue.head >> 16);
111         set(AR9170_PTA_REG_UP_DMA_ADDRL, (uint32_t) fw.pta.up_queue.head & 0xffff);
112
113         for (i = 0; i < __AR9170_NUM_TX_QUEUES; i++)
114                 set_wlan_txq_dma_addr(i, (uint32_t) fw.wlan.tx_queue[i].head);
115
116         set(AR9170_MAC_REG_DMA_RXQ_ADDR, (uint32_t) fw.wlan.rx_queue.head);
117         fw.usb.int_desc->dataSize = AR9170_BLOCK_SIZE;
118         fw.usb.int_desc->dataAddr = (void *) &dma_mem.reserved.rsp;
119
120         memset(DESC_PAYLOAD(fw.usb.int_desc), 0xff,
121                AR9170_INT_MAGIC_HEADER_SIZE);
122         memset(DESC_PAYLOAD_OFF(fw.usb.int_desc, AR9170_INT_MAGIC_HEADER_SIZE),
123                0, AR9170_BLOCK_SIZE - AR9170_INT_MAGIC_HEADER_SIZE);
124
125         /* rsp is now available for use */
126         fw.usb.int_desc_available = 1;
127
128         memset(DESC_PAYLOAD(fw.wlan.fw_desc), 0, 128);
129         fw.wlan.fw_desc_available = 1;
130 }
131
132 /*
133  * Free descriptor.
134  *
135  * Exchange the terminator and the first descriptor of the packet
136  * for hardware ascy...
137  */
138 void dma_reclaim(struct dma_queue *q, struct dma_desc *desc)
139 {
140         struct dma_desc *tmpDesc, *last;
141         struct dma_desc tdesc;
142
143         /* 1. Set OWN bit to HW for all TDs to be added, clear ctrl and size */
144         tmpDesc = desc;
145         last = desc->lastAddr;
146
147         while (1) {
148                 tmpDesc->status = AR9170_OWN_BITS_HW;
149                 tmpDesc->ctrl = 0;
150                 tmpDesc->totalLen = 0;
151                 tmpDesc->dataSize = AR9170_BLOCK_SIZE;
152
153                 /* TODO : Exception handle */
154
155                 tmpDesc->lastAddr = tmpDesc;
156
157                 if (tmpDesc == last)
158                         break;
159
160                 tmpDesc = tmpDesc->nextAddr;
161         }
162
163         /* 2. Next address of Last TD to be added = first TD */
164         tmpDesc->nextAddr = desc;
165
166         /* Link first TD to self */
167         desc->lastAddr = q->terminator;
168
169         /* 3. Copy first TD to be added to TTD */
170         copy_dma_desc(&tdesc, desc);
171
172         /* 4. Initialize new terminator */
173         clear_descriptor(desc);
174
175         /* 5. Copy TTD to last TD */
176         tdesc.status = 0;
177         copy_dma_desc((void *)q->terminator, (void *)&tdesc);
178         q->terminator->status |= AR9170_OWN_BITS_HW;
179
180         /* Update terminator pointer */
181         q->terminator = desc;
182 }
183
184 /*
185  * Put a complete packet into the tail of the Queue q.
186  * Exchange the terminator and the first descriptor of the packet
187  * for hardware ascy...
188  */
189 void dma_put(struct dma_queue *q, struct dma_desc *desc)
190 {
191         struct dma_desc *tmpDesc;
192         struct dma_desc tdesc;
193
194         tmpDesc = desc;
195
196         while (1) {
197                 /* update totalLen */
198                 tmpDesc->totalLen = desc->totalLen;
199
200                 /* 1. Set OWN bit to HW for all TDs to be added */
201                 tmpDesc->status = AR9170_OWN_BITS_HW;
202                 /* TODO : Exception handle */
203
204                 tmpDesc->lastAddr = desc->lastAddr;
205
206                 if (desc->lastAddr == tmpDesc)
207                         break;
208
209                 tmpDesc = tmpDesc->nextAddr;
210         }
211
212         /* 2. Next address of Last TD to be added = first TD */
213         desc->lastAddr->nextAddr = desc;
214
215         /* If there is only one descriptor, update pointer of last descriptor */
216         if (desc->lastAddr == desc)
217                 desc->lastAddr = q->terminator;
218
219         /* 3. Copy first TD to be added to TTD */
220         copy_dma_desc(&tdesc, desc);
221
222         /* 4. Initialize new terminator */
223         clear_descriptor(desc);
224
225         /* 5. Copy TTD to last TD */
226         tdesc.status &= (~AR9170_OWN_BITS);
227         copy_dma_desc((void *)q->terminator, (void *)&tdesc);
228         q->terminator->status |= AR9170_OWN_BITS_HW;
229
230         /* Update terminator pointer */
231         q->terminator = desc;
232 }
233
234 struct dma_desc *dma_unlink_head(struct dma_queue *queue)
235 {
236         struct dma_desc *desc;
237
238         if (queue_empty(queue))
239                 return NULL;
240
241         desc = queue->head;
242
243         queue->head = desc->lastAddr->nextAddr;
244
245         /* poison nextAddr address */
246         desc->lastAddr->nextAddr = desc->lastAddr;
247         desc->lastAddr->lastAddr = desc->lastAddr;
248
249         return desc;
250 }