summaryrefslogtreecommitdiffstats
path: root/carl9170fw/carlfw/src/dma.c
blob: 9b83fcf9b4777ff2ff048a66af94695479d2fe37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
 * carl9170 firmware - used by the ar9170 wireless device
 *
 * DMA descriptor handling functions
 *
 * Copyright (c) 2000-2005 ZyDAS Technology Corporation
 * Copyright (c) 2007-2009 Atheros Communications, Inc.
 * Copyright	2009	Johannes Berg <johannes@sipsolutions.net>
 * Copyright 2009-2011	Christian Lamparter <chunkeey@googlemail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "carl9170.h"
#include "wl.h"
#include "printf.h"

struct ar9170_dma_memory dma_mem __section(sram);

static void copy_dma_desc(struct dma_desc *dst,
			  struct dma_desc *src)
{
	memcpy(dst, src, sizeof(struct dma_desc));
}

static void clear_descriptor(struct dma_desc *d)
{
	d->status = AR9170_OWN_BITS_SW;
	d->ctrl = 0;
	d->dataSize = 0;
	d->totalLen = 0;
	d->lastAddr = d;
	d->dataAddr = NULL;
	d->nextAddr = d;
}

static void fill_descriptor(struct dma_desc *d, uint16_t size, uint8_t *data)
{
	d->status = AR9170_OWN_BITS_SW;
	d->ctrl = 0;
	d->dataSize = size;
	d->totalLen = 0;
	d->lastAddr = d;
	d->dataAddr = data;
	d->nextAddr = NULL;
}

static void init_queue(struct dma_queue *q, struct dma_desc *d)
{
	q->head = q->terminator = d;
}

/*
 *  - Init up_queue, down_queue, tx_queue[5], rx_queue.
 *  - Setup descriptors and data buffer address.
 *  - Ring descriptors rx_queue and down_queue by dma_reclaim().
 *
 * NOTE: LastAddr tempary point (same) to nextAddr after initialize.
 *	 Because LastAddr is don't care in function dma_reclaim().
 */
void dma_init_descriptors(void)
{
	unsigned int i, j;

	for (i = 0; i < ARRAY_SIZE(dma_mem.terminator); i++)
		clear_descriptor(&dma_mem.terminator[i]);

	/* Assign terminators to DMA queues */
	i = 0;
	init_queue(&fw.pta.up_queue, &dma_mem.terminator[i++]);
	init_queue(&fw.pta.down_queue, &dma_mem.terminator[i++]);
	for (j = 0; j < __AR9170_NUM_TX_QUEUES; j++)
		init_queue(&fw.wlan.tx_queue[j], &dma_mem.terminator[i++]);
	init_queue(&fw.wlan.tx_retry, &dma_mem.terminator[i++]);
	init_queue(&fw.wlan.rx_queue, &dma_mem.terminator[i++]);
	fw.usb.int_desc = &dma_mem.terminator[i++];
	fw.wlan.fw_desc = &dma_mem.terminator[i++];

#ifdef CONFIG_CARL9170FW_CAB_QUEUE
	for (j = 0; j < CARL9170_INTF_NUM; j++)
		init_queue(&fw.wlan.cab_queue[j], &dma_mem.terminator[i++]);
#endif /* CONFIG_CARL9170FW_CAB_QUEUE */

	BUG_ON(AR9170_TERMINATOR_NUMBER != i);

	DBG("Blocks:%d [tx:%d, rx:%d] Terminators:%d/%d\n",
	    AR9170_BLOCK_NUMBER, AR9170_TX_BLOCK_NUMBER,
	    AR9170_RX_BLOCK_NUMBER, AR9170_TERMINATOR_NUMBER, i);

	/* Init descriptors and memory blocks */
	for (i = 0; i < AR9170_BLOCK_NUMBER; i++) {
		fill_descriptor(&dma_mem.block[i], AR9170_BLOCK_SIZE, dma_mem.data[i].data);

		if (i < AR9170_TX_BLOCK_NUMBER)
			dma_reclaim(&fw.pta.down_queue, &dma_mem.block[i]);
		else
			dma_reclaim(&fw.wlan.rx_queue, &dma_mem.block[i]);
	}

	/* Set DMA address registers */
	set(AR9170_PTA_REG_DN_DMA_ADDRH, (uint32_t) fw.pta.down_queue.head >> 16);
	set(AR9170_PTA_REG_DN_DMA_ADDRL, (uint32_t) fw.pta.down_queue.head & 0xffff);
	set(AR9170_PTA_REG_UP_DMA_ADDRH, (uint32_t) fw.pta.up_queue.head >> 16);
	set(AR9170_PTA_REG_UP_DMA_ADDRL, (uint32_t) fw.pta.up_queue.head & 0xffff);

	for (i = 0; i < __AR9170_NUM_TX_QUEUES; i++)
		set_wlan_txq_dma_addr(i, (uint32_t) fw.wlan.tx_queue[i].head);

	set(AR9170_MAC_REG_DMA_RXQ_ADDR, (uint32_t) fw.wlan.rx_queue.head);
	fw.usb.int_desc->dataSize = AR9170_BLOCK_SIZE;
	fw.usb.int_desc->dataAddr = (void *) &dma_mem.reserved.rsp;

	memset(DESC_PAYLOAD(fw.usb.int_desc), 0xff,
	       AR9170_INT_MAGIC_HEADER_SIZE);
	memset(DESC_PAYLOAD_OFF(fw.usb.int_desc, AR9170_INT_MAGIC_HEADER_SIZE),
	       0, AR9170_BLOCK_SIZE - AR9170_INT_MAGIC_HEADER_SIZE);

	/* rsp is now available for use */
	fw.usb.int_desc_available = 1;

	memset(DESC_PAYLOAD(fw.wlan.fw_desc), 0, 128);
	fw.wlan.fw_desc_available = 1;
}

/*
 * Free descriptor.
 *
 * Exchange the terminator and the first descriptor of the packet
 * for hardware ascy...
 */
void dma_reclaim(struct dma_queue *q, struct dma_desc *desc)
{
	struct dma_desc *tmpDesc, *last;
	struct dma_desc tdesc;

	/* 1. Set OWN bit to HW for all TDs to be added, clear ctrl and size */
	tmpDesc = desc;
	last = desc->lastAddr;

	while (1) {
		tmpDesc->status = AR9170_OWN_BITS_HW;
		tmpDesc->ctrl = 0;
		tmpDesc->totalLen = 0;
		tmpDesc->dataSize = AR9170_BLOCK_SIZE;

		/* TODO : Exception handle */

		tmpDesc->lastAddr = tmpDesc;

		if (tmpDesc == last)
			break;

		tmpDesc = tmpDesc->nextAddr;
	}

	/* 2. Next address of Last TD to be added = first TD */
	tmpDesc->nextAddr = desc;

	/* Link first TD to self */
	desc->lastAddr = q->terminator;

	/* 3. Copy first TD to be added to TTD */
	copy_dma_desc(&tdesc, desc);

	/* 4. Initialize new terminator */
	clear_descriptor(desc);

	/* 5. Copy TTD to last TD */
	tdesc.status = 0;
	copy_dma_desc((void *)q->terminator, (void *)&tdesc);
	q->terminator->status |= AR9170_OWN_BITS_HW;

	/* Update terminator pointer */
	q->terminator = desc;
}

/*
 * Put a complete packet into the tail of the Queue q.
 * Exchange the terminator and the first descriptor of the packet
 * for hardware ascy...
 */
void dma_put(struct dma_queue *q, struct dma_desc *desc)
{
	struct dma_desc *tmpDesc;
	struct dma_desc tdesc;

	tmpDesc = desc;

	while (1) {
		/* update totalLen */
		tmpDesc->totalLen = desc->totalLen;

		/* 1. Set OWN bit to HW for all TDs to be added */
		tmpDesc->status = AR9170_OWN_BITS_HW;
		/* TODO : Exception handle */

		tmpDesc->lastAddr = desc->lastAddr;

		if (desc->lastAddr == tmpDesc)
			break;

		tmpDesc = tmpDesc->nextAddr;
	}

	/* 2. Next address of Last TD to be added = first TD */
	desc->lastAddr->nextAddr = desc;

	/* If there is only one descriptor, update pointer of last descriptor */
	if (desc->lastAddr == desc)
		desc->lastAddr = q->terminator;

	/* 3. Copy first TD to be added to TTD */
	copy_dma_desc(&tdesc, desc);

	/* 4. Initialize new terminator */
	clear_descriptor(desc);

	/* 5. Copy TTD to last TD */
	tdesc.status &= (~AR9170_OWN_BITS);
	copy_dma_desc((void *)q->terminator, (void *)&tdesc);
	q->terminator->status |= AR9170_OWN_BITS_HW;

	/* Update terminator pointer */
	q->terminator = desc;
}

struct dma_desc *dma_unlink_head(struct dma_queue *queue)
{
	struct dma_desc *desc;

	if (queue_empty(queue))
		return NULL;

	desc = queue->head;

	queue->head = desc->lastAddr->nextAddr;

	/* poison nextAddr address */
	desc->lastAddr->nextAddr = desc->lastAddr;
	desc->lastAddr->lastAddr = desc->lastAddr;

	return desc;
}