Setting up repository
[linux-libre-firmware.git] / carl9170fw / carlfw / src / hostif.c
1 /*
2  * carl9170 firmware - used by the ar9170 wireless device
3  *
4  * Host interface routines
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-2012  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 "hostif.h"
28 #include "printf.h"
29 #include "wl.h"
30 #include "io.h"
31 #include "cam.h"
32 #include "rf.h"
33 #include "timer.h"
34 #include "wol.h"
35
36 static bool length_check(struct dma_desc *desc)
37 {
38         volatile struct carl9170_tx_superframe *super = __get_super(desc);
39
40         if (unlikely(desc->totalLen < sizeof(struct carl9170_tx_superdesc)))
41                 return false;
42
43         /*
44          * check if the DMA is complete, or clipped.
45          *
46          * NB: The hardware aligns the descriptor length to
47          * a 4 byte boundary. This makes the direct comparison
48          * difficult, or unnecessary complex for a hot-path.
49          */
50         if (unlikely(super->s.len > desc->totalLen))
51                 return false;
52
53         return true;
54 }
55
56 static void handle_download(void)
57 {
58         struct dma_desc *desc;
59
60         /*
61          * Under normal conditions, all completed descs should have
62          * the AR9170_OWN_BITS_SE status flag set.
63          * However there seems to be a undocumented case where the flag
64          * is _SW ( handle_download_exception )
65          */
66
67         for_each_desc_not_bits(desc, &fw.pta.down_queue, AR9170_OWN_BITS_HW) {
68                 if (unlikely((length_check(desc) == false))) {
69                         /*
70                          * There is no easy way of telling what was lost.
71                          *
72                          * Therefore we just reclaim the data.
73                          * The driver has to have some sort frame
74                          * timeout mechanism.
75                          */
76
77                         wlan_tx_complete(__get_super(desc), false);
78                         dma_reclaim(&fw.pta.down_queue, desc);
79                         down_trigger();
80                 } else {
81                         wlan_tx(desc);
82                 }
83         }
84
85 #ifdef CONFIG_CARL9170FW_DEBUG_LED_HEARTBEAT
86         xorl(AR9170_GPIO_REG_PORT_DATA, 2);
87 #endif /* CONFIG_CARL9170FW_DEBUG_LED_HEARTBEAT */
88 }
89
90 static void handle_upload(void)
91 {
92         struct dma_desc *desc;
93
94         for_each_desc_not_bits(desc, &fw.pta.up_queue, AR9170_OWN_BITS_HW) {
95                 /*
96                  * BIG FAT NOTE:
97                  *
98                  * DO NOT compare the descriptor addresses.
99                  */
100                 if (DESC_PAYLOAD(desc) == (void *) &dma_mem.reserved.rsp) {
101                         fw.usb.int_desc = desc;
102                         fw.usb.int_desc_available = 1;
103                 } else {
104                         dma_reclaim(&fw.wlan.rx_queue, desc);
105                         wlan_trigger(AR9170_DMA_TRIGGER_RXQ);
106                 }
107         }
108
109 #ifdef CONFIG_CARL9170FW_DEBUG_LED_HEARTBEAT
110         xorl(AR9170_GPIO_REG_PORT_DATA, 2);
111 #endif /* CONFIG_CARL9170FW_DEBUG_LED_HEARTBEAT */
112 }
113
114 static void handle_download_exception(void)
115 {
116         struct dma_desc *desc, *target;
117
118         /* actually, the queue should be stopped by now? */
119         usb_stop_down_queue();
120
121         target = (void *)((get(AR9170_PTA_REG_DN_CURR_ADDRH) << 16) |
122                   get(AR9170_PTA_REG_DN_CURR_ADDRL));
123
124         /*
125          * Put "forgotten" packets from the head of the queue, back
126          * to the current position
127          */
128         __while_desc_bits(desc, &fw.pta.down_queue, AR9170_OWN_BITS_HW) {
129                 if (desc == target)
130                         break;
131
132                 dma_reclaim(&fw.pta.down_queue,
133                     dma_unlink_head(&fw.pta.down_queue));
134         }
135
136         __for_each_desc_continue(desc, &fw.pta.down_queue) {
137                 if ((desc->status & AR9170_OWN_BITS) == AR9170_OWN_BITS_SW)
138                         dma_fix_downqueue(desc);
139         }
140
141
142         usb_start_down_queue();
143
144         down_trigger();
145 }
146
147 /* handle interrupts from DMA chip */
148 void handle_host_interface(void)
149 {
150         uint32_t pta_int;
151
152         pta_int = get(AR9170_PTA_REG_INT_FLAG);
153
154 #define HANDLER(intr, flag, func)                       \
155         do {                                            \
156                 if ((intr & flag) != 0) {               \
157                         func();                         \
158                 }                                       \
159         } while (0)
160
161         HANDLER(pta_int, AR9170_PTA_INT_FLAG_DN, handle_download);
162
163         HANDLER(pta_int, AR9170_PTA_INT_FLAG_UP, handle_upload);
164
165         /* This is just guesswork and MAGIC */
166         pta_int = get(AR9170_PTA_REG_DMA_STATUS);
167         HANDLER(pta_int, 0x1, handle_download_exception);
168
169 #undef HANDLER
170 }
171
172 void handle_cmd(struct carl9170_rsp *resp)
173 {
174         struct carl9170_cmd *cmd = &dma_mem.reserved.cmd.cmd;
175         unsigned int i;
176
177         /* copies cmd, len and extra fields */
178         resp->hdr.len = cmd->hdr.len;
179         resp->hdr.cmd = cmd->hdr.cmd;
180         resp->hdr.ext = cmd->hdr.ext;
181         resp->hdr.seq |= cmd->hdr.seq;
182
183         switch (cmd->hdr.cmd & ~CARL9170_CMD_ASYNC_FLAG) {
184         case CARL9170_CMD_RREG:
185                 for (i = 0; i < (cmd->hdr.len / 4); i++)
186                         resp->rreg_res.vals[i] = get(cmd->rreg.regs[i]);
187                 break;
188
189         case CARL9170_CMD_WREG:
190                 resp->hdr.len = 0;
191                 for (i = 0; i < (cmd->hdr.len / 8); i++)
192                         set(cmd->wreg.regs[i].addr, cmd->wreg.regs[i].val);
193                 break;
194
195         case CARL9170_CMD_ECHO:
196                 memcpy(resp->echo.vals, cmd->echo.vals, cmd->hdr.len);
197                 break;
198
199         case CARL9170_CMD_SWRST:
200 #ifdef CONFIG_CARL9170FW_FW_MAC_RESET
201                 /*
202                  * Command has no payload, so the response
203                  * has no payload either.
204                  * resp->hdr.len = 0;
205                  */
206                 fw.wlan.mac_reset = CARL9170_MAC_RESET_FORCE;
207 #endif /* CONFIG_CARL9170FW_FW_MAC_RESET */
208                 break;
209
210         case CARL9170_CMD_REBOOT:
211                 /*
212                  * resp->len = 0;
213                  */
214                 fw.reboot = 1;
215                 break;
216
217         case CARL9170_CMD_READ_TSF:
218                 resp->hdr.len = 8;
219                 read_tsf((uint32_t *)resp->tsf.tsf);
220                 break;
221
222         case CARL9170_CMD_RX_FILTER:
223                 resp->hdr.len = 0;
224                 fw.wlan.rx_filter = cmd->rx_filter.rx_filter;
225                 break;
226
227         case CARL9170_CMD_WOL:
228                 wol_cmd(&cmd->wol);
229                 break;
230
231         case CARL9170_CMD_TALLY:
232                 resp->hdr.len = sizeof(struct carl9170_tally_rsp);
233                 memcpy(&resp->tally, &fw.tally, sizeof(struct carl9170_tally_rsp));
234                 resp->tally.tick = fw.ticks_per_usec;
235                 memset(&fw.tally, 0, sizeof(struct carl9170_tally_rsp));
236                 break;
237
238         case CARL9170_CMD_BCN_CTRL:
239                 resp->hdr.len = 0;
240
241                 if (cmd->bcn_ctrl.mode & CARL9170_BCN_CTRL_CAB_TRIGGER) {
242                         wlan_modify_beacon(cmd->bcn_ctrl.vif_id,
243                                 cmd->bcn_ctrl.bcn_addr, cmd->bcn_ctrl.bcn_len);
244                         set(AR9170_MAC_REG_BCN_ADDR, cmd->bcn_ctrl.bcn_addr);
245                         set(AR9170_MAC_REG_BCN_LENGTH, cmd->bcn_ctrl.bcn_len);
246                         set(AR9170_MAC_REG_BCN_CTRL, AR9170_BCN_CTRL_READY);
247                 } else {
248                         wlan_cab_flush_queue(cmd->bcn_ctrl.vif_id);
249                         fw.wlan.cab_flush_trigger[cmd->bcn_ctrl.vif_id] = CARL9170_CAB_TRIGGER_EMPTY;
250                 }
251                 break;
252
253 #ifdef CONFIG_CARL9170FW_SECURITY_ENGINE
254         case CARL9170_CMD_EKEY:
255                 resp->hdr.len = 0;
256                 set_key(&cmd->setkey);
257                 break;
258
259         case CARL9170_CMD_DKEY:
260                 resp->hdr.len = 0;
261                 disable_key(&cmd->disablekey);
262                 break;
263 #endif /* CONFIG_CARL9170FW_SECURITY_ENGINE */
264
265 #ifdef CONFIG_CARL9170FW_RADIO_FUNCTIONS
266         case CARL9170_CMD_FREQUENCY:
267         case CARL9170_CMD_RF_INIT:
268                 rf_cmd(cmd, resp);
269                 break;
270
271         case CARL9170_CMD_FREQ_START:
272                 /*
273                  * resp->hdr.len = 0;
274                  */
275                 rf_notify_set_channel();
276                 break;
277
278         case CARL9170_CMD_PSM:
279                 resp->hdr.len = 0;
280                 fw.phy.psm.state = le32_to_cpu(cmd->psm.state);
281                 rf_psm();
282                 break;
283 #endif /* CONFIG_CARL9170FW_RADIO_FUNCTIONS */
284
285         default:
286                 BUG("Unknown command %x\n", cmd->hdr.cmd);
287                 break;
288         }
289 }