3da9f8c328c1de3cbd66c478f764bfcc43fb38b9
[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, 2010 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.rx_queue.head = fw.wlan.rx_queue.terminator = &dma_mem.terminator[i++];
82         fw.usb.int_desc = &dma_mem.terminator[i++];
83
84 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
85         fw.wlan.cab_queue.head = fw.wlan.cab_queue.terminator = &dma_mem.terminator[i++];
86 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
87
88 #ifdef CONFIG_CARL9170FW_HANDLE_BACK_REQ
89         fw.wlan.ba_desc = &dma_mem.terminator[i++];
90 #endif /* CONFIG_CARL9170FW_HANDLE_BACK_REQ */
91
92 #ifdef CONFIG_CARL9170FW_DELAYED_TX
93         fw.wlan.tx_retry.head = fw.wlan.tx_retry.terminator = &dma_mem.terminator[i++];
94
95         for (j = 0; j < __AR9170_NUM_TX_QUEUES; j++)
96                 fw.wlan.tx_delay[j].head = fw.wlan.tx_delay[j].terminator = &dma_mem.terminator[i++];
97 #endif /* CONFIG_CARL9170FW_DELAYED_TX */
98
99         DBG("Blocks:%d [tx:%d, rx:%d] Terminators:%d/%d\n",
100             AR9170_BLOCK_NUMBER, AR9170_TX_BLOCK_NUMBER,
101             AR9170_RX_BLOCK_NUMBER, AR9170_TERMINATOR_NUMBER, i);
102
103         /* Init descriptors and memory blocks */
104         for (i = 0; i < AR9170_BLOCK_NUMBER; i++) {
105                 fill_descriptor(&dma_mem.block[i], AR9170_BLOCK_SIZE, dma_mem.data[i].data);
106
107                 if (i < AR9170_TX_BLOCK_NUMBER)
108                         dma_reclaim(&fw.pta.down_queue, &dma_mem.block[i]);
109                 else
110                         dma_reclaim(&fw.wlan.rx_queue, &dma_mem.block[i]);
111         }
112
113         /* Set DMA address registers */
114         set(AR9170_PTA_REG_DN_DMA_ADDRH, (uint32_t) fw.pta.down_queue.head >> 16);
115         set(AR9170_PTA_REG_DN_DMA_ADDRL, (uint32_t) fw.pta.down_queue.head & 0xffff);
116         set(AR9170_PTA_REG_UP_DMA_ADDRH, (uint32_t) fw.pta.up_queue.head >> 16);
117         set(AR9170_PTA_REG_UP_DMA_ADDRL, (uint32_t) fw.pta.up_queue.head & 0xffff);
118
119         for (i = 0; i < __AR9170_NUM_TX_QUEUES; i++)
120                 set_wlan_txq_dma_addr(i, (uint32_t) fw.wlan.tx_queue[i].head);
121
122         set(AR9170_MAC_REG_DMA_RXQ_ADDR, (uint32_t) fw.wlan.rx_queue.head);
123
124         fw.usb.int_desc->status = AR9170_OWN_BITS_SW;
125         fw.usb.int_desc->ctrl = (AR9170_CTRL_LS_BIT | AR9170_CTRL_FS_BIT);
126         fw.usb.int_desc->dataSize = AR9170_BLOCK_SIZE;
127         fw.usb.int_desc->totalLen = 0;
128         fw.usb.int_desc->lastAddr = fw.usb.int_desc;
129         fw.usb.int_desc->dataAddr = (void *) &dma_mem.reserved.rsp;
130         fw.usb.int_desc->nextAddr = (void *) 0;
131
132         memset(DESC_PAYLOAD(fw.usb.int_desc), 0xff,
133                AR9170_INT_MAGIC_HEADER_SIZE);
134         memset(DESC_PAYLOAD_OFF(fw.usb.int_desc, AR9170_INT_MAGIC_HEADER_SIZE),
135                0, AR9170_BLOCK_SIZE - AR9170_INT_MAGIC_HEADER_SIZE);
136
137         /* rsp is now available for use */
138         fw.usb.int_desc_available = 1;
139
140 #ifdef CONFIG_CARL9170FW_HANDLE_BACK_REQ
141         fw.wlan.ba_desc->status = AR9170_OWN_BITS_SW;
142         fw.wlan.ba_desc->ctrl = (AR9170_CTRL_LS_BIT | AR9170_CTRL_FS_BIT);
143         fw.wlan.ba_desc->dataSize = fw.wlan.ba_desc->totalLen =
144                 sizeof(struct carl9170_tx_superdesc) +
145                 sizeof(struct ar9170_tx_hwdesc) +
146                 sizeof(struct ieee80211_ba) + FCS_LEN;
147         fw.wlan.ba_desc->lastAddr = fw.wlan.ba_desc;
148         fw.wlan.ba_desc->nextAddr = fw.wlan.ba_desc;
149         fw.wlan.ba_desc->dataAddr = (void *) &dma_mem.reserved.ba;
150
151         memset(DESC_PAYLOAD(fw.wlan.ba_desc), 0, 128);
152
153         fw.wlan.ba_desc_available = 1;
154 #endif /* CONFIG_CARL9170FW_HANDLE_BACK_REQ */
155 }
156
157 /*
158  * Free descriptor.
159  *
160  * Exchange the terminator and the first descriptor of the packet
161  * for hardware ascy...
162  */
163 void dma_reclaim(struct dma_queue *q, struct dma_desc *desc)
164 {
165         struct dma_desc *tmpDesc;
166         struct dma_desc tdesc;
167
168         /* 1. Set OWN bit to HW for all TDs to be added, clear ctrl and size */
169         tmpDesc = desc;
170         while (1) {
171                 tmpDesc->status = AR9170_OWN_BITS_HW;
172                 tmpDesc->ctrl = 0;
173                 tmpDesc->totalLen = 0;
174                 tmpDesc->dataSize = AR9170_BLOCK_SIZE;
175
176                 /* TODO : Exception handle */
177
178                 if (desc->lastAddr == tmpDesc)
179                         break;
180
181                 tmpDesc->lastAddr = desc->lastAddr;
182                 tmpDesc = tmpDesc->nextAddr;
183         }
184
185         /* 2. Next address of Last TD to be added = first TD */
186         desc->lastAddr->nextAddr = desc;
187
188         /* 3. Copy first TD to be added to TTD */
189         copy_dma_desc(&tdesc, desc);
190
191         /* 4. set first TD OWN bit to SW */
192         desc->status = AR9170_OWN_BITS_SW;
193
194         /* 5. Copy TTD to last TD */
195         tdesc.status &= (~AR9170_OWN_BITS_MASK);
196         copy_dma_desc((void *)q->terminator, (void *)&tdesc);
197         q->terminator->status |= AR9170_OWN_BITS_HW;
198
199         /* Update terminator pointer */
200         q->terminator = desc;
201 }
202
203 /*
204  * Put a complete packet into the tail of the Queue q.
205  * Exchange the terminator and the first descriptor of the packet
206  * for hardware ascy...
207  */
208 void dma_put(struct dma_queue *q, struct dma_desc *desc)
209 {
210         struct dma_desc *tmpDesc;
211         struct dma_desc tdesc;
212
213         tmpDesc = desc;
214
215         /* force correct CTRL_BITS */
216         tmpDesc->ctrl = 0;
217         tmpDesc->ctrl |= AR9170_CTRL_FS_BIT;
218         while (1) {
219                 /* update totalLen */
220                 tmpDesc->totalLen = desc->totalLen;
221
222                 /* 1. Set OWN bit to HW for all TDs to be added */
223                 tmpDesc->status = AR9170_OWN_BITS_HW;
224                 /* TODO : Exception handle */
225
226                 tmpDesc->lastAddr = desc->lastAddr;
227
228                 if (desc->lastAddr == tmpDesc)
229                         break;
230
231                 tmpDesc = tmpDesc->nextAddr;
232                 tmpDesc->ctrl = 0;
233         }
234         tmpDesc->ctrl |= AR9170_CTRL_LS_BIT;
235
236         /* 2. Next address of Last TD to be added = first TD */
237         desc->lastAddr->nextAddr = desc;
238
239         /* If there is only one descriptor, update pointer of last descriptor */
240         if (desc->lastAddr == desc)
241                 desc->lastAddr = q->terminator;
242
243         /* 3. Copy first TD to be added to TTD */
244         copy_dma_desc(&tdesc, desc);
245
246         /* 4. set first TD OWN bit to SW */
247         desc->status = AR9170_OWN_BITS_SW;
248         desc->ctrl = 0;
249         desc->totalLen = 0;
250         desc->dataSize = 0;
251         desc->lastAddr = desc;
252         desc->nextAddr = desc;
253         desc->dataAddr = NULL;
254
255         /* 5. Copy TTD to last TD */
256         tdesc.status &= (~AR9170_OWN_BITS_MASK);
257         copy_dma_desc((void *)q->terminator, (void *)&tdesc);
258         q->terminator->status |= AR9170_OWN_BITS_HW;
259
260         /* Update terminator pointer */
261         q->terminator = desc;
262 }
263
264 struct dma_desc *dma_unlink_head(struct dma_queue *queue)
265 {
266         struct dma_desc *desc;
267
268         if (queue_empty(queue))
269                 return NULL;
270
271         desc = queue->head;
272
273         queue->head = desc->lastAddr->nextAddr;
274
275         /* poison nextAddr address */
276         desc->lastAddr->nextAddr = desc->lastAddr;
277         desc->lastAddr->lastAddr = desc->lastAddr;
278
279         return desc;
280 }