Setting up repository
[linux-libre-firmware.git] / carl9170fw / 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 static void init_queue(struct dma_queue *q, struct dma_desc *d)
61 {
62         q->head = q->terminator = d;
63 }
64
65 /*
66  *  - Init up_queue, down_queue, tx_queue[5], rx_queue.
67  *  - Setup descriptors and data buffer address.
68  *  - Ring descriptors rx_queue and down_queue by dma_reclaim().
69  *
70  * NOTE: LastAddr tempary point (same) to nextAddr after initialize.
71  *       Because LastAddr is don't care in function dma_reclaim().
72  */
73 void dma_init_descriptors(void)
74 {
75         unsigned int i, j;
76
77         for (i = 0; i < ARRAY_SIZE(dma_mem.terminator); i++)
78                 clear_descriptor(&dma_mem.terminator[i]);
79
80         /* Assign terminators to DMA queues */
81         i = 0;
82         init_queue(&fw.pta.up_queue, &dma_mem.terminator[i++]);
83         init_queue(&fw.pta.down_queue, &dma_mem.terminator[i++]);
84         for (j = 0; j < __AR9170_NUM_TX_QUEUES; j++)
85                 init_queue(&fw.wlan.tx_queue[j], &dma_mem.terminator[i++]);
86         init_queue(&fw.wlan.tx_retry, &dma_mem.terminator[i++]);
87         init_queue(&fw.wlan.rx_queue, &dma_mem.terminator[i++]);
88         fw.usb.int_desc = &dma_mem.terminator[i++];
89         fw.wlan.fw_desc = &dma_mem.terminator[i++];
90
91         for (j = 0; j < CARL9170_INTF_NUM; j++)
92                 init_queue(&fw.wlan.cab_queue[j], &dma_mem.terminator[i++]);
93
94         BUG_ON(AR9170_TERMINATOR_NUMBER != i);
95
96         DBG("Blocks:%d [tx:%d, rx:%d] Terminators:%d/%d\n",
97             AR9170_BLOCK_NUMBER, AR9170_TX_BLOCK_NUMBER,
98             AR9170_RX_BLOCK_NUMBER, AR9170_TERMINATOR_NUMBER, i);
99
100         /* Init descriptors and memory blocks */
101         for (i = 0; i < AR9170_BLOCK_NUMBER; i++) {
102                 fill_descriptor(&dma_mem.block[i], AR9170_BLOCK_SIZE, dma_mem.data[i].data);
103
104                 if (i < AR9170_TX_BLOCK_NUMBER)
105                         dma_reclaim(&fw.pta.down_queue, &dma_mem.block[i]);
106                 else
107                         dma_reclaim(&fw.wlan.rx_queue, &dma_mem.block[i]);
108         }
109
110         /* Set DMA address registers */
111         set(AR9170_PTA_REG_DN_DMA_ADDRH, (uint32_t) fw.pta.down_queue.head >> 16);
112         set(AR9170_PTA_REG_DN_DMA_ADDRL, (uint32_t) fw.pta.down_queue.head & 0xffff);
113         set(AR9170_PTA_REG_UP_DMA_ADDRH, (uint32_t) fw.pta.up_queue.head >> 16);
114         set(AR9170_PTA_REG_UP_DMA_ADDRL, (uint32_t) fw.pta.up_queue.head & 0xffff);
115
116         for (i = 0; i < __AR9170_NUM_TX_QUEUES; i++)
117                 set_wlan_txq_dma_addr(i, (uint32_t) fw.wlan.tx_queue[i].head);
118
119         set(AR9170_MAC_REG_DMA_RXQ_ADDR, (uint32_t) fw.wlan.rx_queue.head);
120         fw.usb.int_desc->dataSize = AR9170_BLOCK_SIZE;
121         fw.usb.int_desc->dataAddr = (void *) &dma_mem.reserved.rsp;
122
123         memset(DESC_PAYLOAD(fw.usb.int_desc), 0xff,
124                AR9170_INT_MAGIC_HEADER_SIZE);
125         memset(DESC_PAYLOAD_OFF(fw.usb.int_desc, AR9170_INT_MAGIC_HEADER_SIZE),
126                0, AR9170_BLOCK_SIZE - AR9170_INT_MAGIC_HEADER_SIZE);
127
128         /* rsp is now available for use */
129         fw.usb.int_desc_available = 1;
130
131         memset(DESC_PAYLOAD(fw.wlan.fw_desc), 0, 128);
132         fw.wlan.fw_desc_available = 1;
133 }
134
135 /*
136  * Free descriptor.
137  *
138  * Exchange the terminator and the first descriptor of the packet
139  * for hardware ascy...
140  */
141 void dma_reclaim(struct dma_queue *q, struct dma_desc *desc)
142 {
143         struct dma_desc *tmpDesc, *last;
144         struct dma_desc tdesc;
145
146         /* 1. Set OWN bit to HW for all TDs to be added, clear ctrl and size */
147         tmpDesc = desc;
148         last = desc->lastAddr;
149
150         while (1) {
151                 tmpDesc->status = AR9170_OWN_BITS_HW;
152                 tmpDesc->ctrl = 0;
153                 tmpDesc->totalLen = 0;
154                 tmpDesc->dataSize = AR9170_BLOCK_SIZE;
155
156                 /* TODO : Exception handle */
157
158                 tmpDesc->lastAddr = tmpDesc;
159
160                 if (tmpDesc == last)
161                         break;
162
163                 tmpDesc = tmpDesc->nextAddr;
164         }
165
166         /* 2. Next address of Last TD to be added = first TD */
167         tmpDesc->nextAddr = desc;
168
169         /* Link first TD to self */
170         desc->lastAddr = q->terminator;
171
172         /* 3. Copy first TD to be added to TTD */
173         copy_dma_desc(&tdesc, desc);
174
175         /* 4. Initialize new terminator */
176         clear_descriptor(desc);
177
178         /* 5. Copy TTD to last TD */
179         tdesc.status = 0;
180         copy_dma_desc((void *)q->terminator, (void *)&tdesc);
181         q->terminator->status |= AR9170_OWN_BITS_HW;
182
183         /* Update terminator pointer */
184         q->terminator = desc;
185 }
186
187 /*
188  * Put a complete packet into the tail of the Queue q.
189  * Exchange the terminator and the first descriptor of the packet
190  * for hardware ascy...
191  */
192 void dma_put(struct dma_queue *q, struct dma_desc *desc)
193 {
194         struct dma_desc *tmpDesc;
195         struct dma_desc tdesc;
196
197         tmpDesc = desc;
198
199         while (1) {
200                 /* update totalLen */
201                 tmpDesc->totalLen = desc->totalLen;
202
203                 /* 1. Set OWN bit to HW for all TDs to be added */
204                 tmpDesc->status = AR9170_OWN_BITS_HW;
205                 /* TODO : Exception handle */
206
207                 tmpDesc->lastAddr = desc->lastAddr;
208
209                 if (desc->lastAddr == tmpDesc)
210                         break;
211
212                 tmpDesc = tmpDesc->nextAddr;
213         }
214
215         /* 2. Next address of Last TD to be added = first TD */
216         desc->lastAddr->nextAddr = desc;
217
218         /* If there is only one descriptor, update pointer of last descriptor */
219         if (desc->lastAddr == desc)
220                 desc->lastAddr = q->terminator;
221
222         /* 3. Copy first TD to be added to TTD */
223         copy_dma_desc(&tdesc, desc);
224
225         /* 4. Initialize new terminator */
226         clear_descriptor(desc);
227
228         /* 5. Copy TTD to last TD */
229         tdesc.status &= (~AR9170_OWN_BITS);
230         copy_dma_desc((void *)q->terminator, (void *)&tdesc);
231         q->terminator->status |= AR9170_OWN_BITS_HW;
232
233         /* Update terminator pointer */
234         q->terminator = desc;
235 }
236
237 struct dma_desc *dma_unlink_head(struct dma_queue *queue)
238 {
239         struct dma_desc *desc;
240
241         if (queue_empty(queue))
242                 return NULL;
243
244         desc = queue->head;
245
246         queue->head = desc->lastAddr->nextAddr;
247
248         /* poison nextAddr address */
249         desc->lastAddr->nextAddr = desc->lastAddr;
250         desc->lastAddr->lastAddr = desc->lastAddr;
251
252         return desc;
253 }