2 * carl9170 firmware - used by the ar9170 wireless device
4 * DMA descriptor handling functions
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>
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.
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.
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.
30 struct ar9170_dma_memory dma_mem __section(sram);
32 static void copy_dma_desc(struct dma_desc *dst,
35 memcpy(dst, src, sizeof(struct dma_desc));
38 static void clear_descriptor(struct dma_desc *d)
40 d->status = AR9170_OWN_BITS_SW;
49 static void fill_descriptor(struct dma_desc *d, uint16_t size, uint8_t *data)
51 d->status = AR9170_OWN_BITS_SW;
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().
65 * NOTE: LastAddr tempary point (same) to nextAddr after initialize.
66 * Because LastAddr is don't care in function dma_reclaim().
68 void dma_init_descriptors(void)
72 for (i = 0; i < ARRAY_SIZE(dma_mem.terminator); i++)
73 clear_descriptor(&dma_mem.terminator[i]);
75 /* Assign terminators to DMA queues */
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++];
84 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
86 # if (CARL9170_INTF_NUM != 2)
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++];
90 fw.wlan.cab_queue[0].head = fw.wlan.cab_queue[0].terminator = &dma_mem.terminator[i++];
91 fw.wlan.cab_queue[1].head = fw.wlan.cab_queue[1].terminator = &dma_mem.terminator[i++];
93 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
95 #ifdef CONFIG_CARL9170FW_HANDLE_BACK_REQ
96 fw.wlan.ba_desc = &dma_mem.terminator[i++];
97 #endif /* CONFIG_CARL9170FW_HANDLE_BACK_REQ */
99 #ifdef CONFIG_CARL9170FW_DELAYED_TX
100 fw.wlan.tx_retry.head = fw.wlan.tx_retry.terminator = &dma_mem.terminator[i++];
102 for (j = 0; j < __AR9170_NUM_TX_QUEUES; j++)
103 fw.wlan.tx_delay[j].head = fw.wlan.tx_delay[j].terminator = &dma_mem.terminator[i++];
104 #endif /* CONFIG_CARL9170FW_DELAYED_TX */
106 BUILD_BUG_ON(AR9170_TERMINATOR_NUMBER != j);
108 DBG("Blocks:%d [tx:%d, rx:%d] Terminators:%d/%d\n",
109 AR9170_BLOCK_NUMBER, AR9170_TX_BLOCK_NUMBER,
110 AR9170_RX_BLOCK_NUMBER, AR9170_TERMINATOR_NUMBER, i);
112 /* Init descriptors and memory blocks */
113 for (i = 0; i < AR9170_BLOCK_NUMBER; i++) {
114 fill_descriptor(&dma_mem.block[i], AR9170_BLOCK_SIZE, dma_mem.data[i].data);
116 if (i < AR9170_TX_BLOCK_NUMBER)
117 dma_reclaim(&fw.pta.down_queue, &dma_mem.block[i]);
119 dma_reclaim(&fw.wlan.rx_queue, &dma_mem.block[i]);
122 /* Set DMA address registers */
123 set(AR9170_PTA_REG_DN_DMA_ADDRH, (uint32_t) fw.pta.down_queue.head >> 16);
124 set(AR9170_PTA_REG_DN_DMA_ADDRL, (uint32_t) fw.pta.down_queue.head & 0xffff);
125 set(AR9170_PTA_REG_UP_DMA_ADDRH, (uint32_t) fw.pta.up_queue.head >> 16);
126 set(AR9170_PTA_REG_UP_DMA_ADDRL, (uint32_t) fw.pta.up_queue.head & 0xffff);
128 for (i = 0; i < __AR9170_NUM_TX_QUEUES; i++)
129 set_wlan_txq_dma_addr(i, (uint32_t) fw.wlan.tx_queue[i].head);
131 set(AR9170_MAC_REG_DMA_RXQ_ADDR, (uint32_t) fw.wlan.rx_queue.head);
133 fw.usb.int_desc->status = AR9170_OWN_BITS_SW;
134 fw.usb.int_desc->ctrl = (AR9170_CTRL_LS_BIT | AR9170_CTRL_FS_BIT);
135 fw.usb.int_desc->dataSize = AR9170_BLOCK_SIZE;
136 fw.usb.int_desc->totalLen = 0;
137 fw.usb.int_desc->lastAddr = fw.usb.int_desc;
138 fw.usb.int_desc->dataAddr = (void *) &dma_mem.reserved.rsp;
139 fw.usb.int_desc->nextAddr = (void *) 0;
141 memset(DESC_PAYLOAD(fw.usb.int_desc), 0xff,
142 AR9170_INT_MAGIC_HEADER_SIZE);
143 memset(DESC_PAYLOAD_OFF(fw.usb.int_desc, AR9170_INT_MAGIC_HEADER_SIZE),
144 0, AR9170_BLOCK_SIZE - AR9170_INT_MAGIC_HEADER_SIZE);
146 /* rsp is now available for use */
147 fw.usb.int_desc_available = 1;
149 #ifdef CONFIG_CARL9170FW_HANDLE_BACK_REQ
150 fw.wlan.ba_desc->status = AR9170_OWN_BITS_SW;
151 fw.wlan.ba_desc->ctrl = (AR9170_CTRL_LS_BIT | AR9170_CTRL_FS_BIT);
152 fw.wlan.ba_desc->dataSize = fw.wlan.ba_desc->totalLen =
153 sizeof(struct carl9170_tx_superdesc) +
154 sizeof(struct ar9170_tx_hwdesc) +
155 sizeof(struct ieee80211_ba) + FCS_LEN;
156 fw.wlan.ba_desc->lastAddr = fw.wlan.ba_desc;
157 fw.wlan.ba_desc->nextAddr = fw.wlan.ba_desc;
158 fw.wlan.ba_desc->dataAddr = (void *) &dma_mem.reserved.ba;
160 memset(DESC_PAYLOAD(fw.wlan.ba_desc), 0, 128);
162 fw.wlan.ba_desc_available = 1;
163 #endif /* CONFIG_CARL9170FW_HANDLE_BACK_REQ */
169 * Exchange the terminator and the first descriptor of the packet
170 * for hardware ascy...
172 void dma_reclaim(struct dma_queue *q, struct dma_desc *desc)
174 struct dma_desc *tmpDesc;
175 struct dma_desc tdesc;
177 /* 1. Set OWN bit to HW for all TDs to be added, clear ctrl and size */
180 tmpDesc->status = AR9170_OWN_BITS_HW;
182 tmpDesc->totalLen = 0;
183 tmpDesc->dataSize = AR9170_BLOCK_SIZE;
185 /* TODO : Exception handle */
187 if (desc->lastAddr == tmpDesc)
190 tmpDesc->lastAddr = desc->lastAddr;
191 tmpDesc = tmpDesc->nextAddr;
194 /* 2. Next address of Last TD to be added = first TD */
195 desc->lastAddr->nextAddr = desc;
197 /* 3. Copy first TD to be added to TTD */
198 copy_dma_desc(&tdesc, desc);
200 /* 4. set first TD OWN bit to SW */
201 desc->status = AR9170_OWN_BITS_SW;
203 /* 5. Copy TTD to last TD */
204 tdesc.status &= (~AR9170_OWN_BITS);
205 copy_dma_desc((void *)q->terminator, (void *)&tdesc);
206 q->terminator->status |= AR9170_OWN_BITS_HW;
208 /* Update terminator pointer */
209 q->terminator = desc;
213 * Put a complete packet into the tail of the Queue q.
214 * Exchange the terminator and the first descriptor of the packet
215 * for hardware ascy...
217 void dma_put(struct dma_queue *q, struct dma_desc *desc)
219 struct dma_desc *tmpDesc;
220 struct dma_desc tdesc;
225 /* update totalLen */
226 tmpDesc->totalLen = desc->totalLen;
228 /* 1. Set OWN bit to HW for all TDs to be added */
229 tmpDesc->status = AR9170_OWN_BITS_HW;
230 /* TODO : Exception handle */
232 tmpDesc->lastAddr = desc->lastAddr;
234 if (desc->lastAddr == tmpDesc)
237 tmpDesc = tmpDesc->nextAddr;
240 /* 2. Next address of Last TD to be added = first TD */
241 desc->lastAddr->nextAddr = desc;
243 /* If there is only one descriptor, update pointer of last descriptor */
244 if (desc->lastAddr == desc)
245 desc->lastAddr = q->terminator;
247 /* 3. Copy first TD to be added to TTD */
248 copy_dma_desc(&tdesc, desc);
250 /* 4. set first TD OWN bit to SW */
251 desc->status = AR9170_OWN_BITS_SW;
255 desc->lastAddr = desc;
256 desc->nextAddr = desc;
257 desc->dataAddr = NULL;
259 /* 5. Copy TTD to last TD */
260 tdesc.status &= (~AR9170_OWN_BITS);
261 copy_dma_desc((void *)q->terminator, (void *)&tdesc);
262 q->terminator->status |= AR9170_OWN_BITS_HW;
264 /* Update terminator pointer */
265 q->terminator = desc;
268 struct dma_desc *dma_unlink_head(struct dma_queue *queue)
270 struct dma_desc *desc;
272 if (queue_empty(queue))
277 queue->head = desc->lastAddr->nextAddr;
279 /* poison nextAddr address */
280 desc->lastAddr->nextAddr = desc->lastAddr;
281 desc->lastAddr->lastAddr = desc->lastAddr;