carl9170 firmware: streamline dma_put hot-path
[carl9170fw.git] / carlfw / usb / main.c
1 /*
2  * carl9170 firmware - used by the ar9170 wireless device
3  *
4  * Copyright (c) 2000-2005 ZyDAS Technology Corporation
5  * Copyright (c) 2007-2009 Atheros Communications, Inc.
6  * Copyright    2009    Johannes Berg <johannes@sipsolutions.net>
7  * Copyright    2009    Christian Lamparter <chunkeey@googlemail.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23
24 #include "carl9170.h"
25
26 #include "hostif.h"
27 #include "printf.h"
28 #include "timer.h"
29 #include "rom.h"
30 #include "gpio.h"
31 #include "shared/phy.h"
32
33 #ifdef CONFIG_CARL9170FW_DEBUG_USB
34 void usb_putc(const char c)
35 {
36         fw.usb.put_buffer[fw.usb.put_index++] = (uint8_t) c;
37
38         if (fw.usb.put_index == CARL9170_MAX_CMD_PAYLOAD_LEN || c == '\0') {
39                 fw.usb.put_buffer[fw.usb.put_index] = 0;
40
41                 send_cmd_to_host(__roundup(fw.usb.put_index, 4),
42                                  CARL9170_RSP_TEXT, fw.usb.put_index,
43                                  fw.usb.put_buffer);
44                 fw.usb.put_index = 0;
45         }
46 }
47
48 void usb_print_hex_dump(const void *buf, int len)
49 {
50         unsigned int offset = 0, block = 0;
51         while (len > 0) {
52                 block = min(__roundup(len, 4), CARL9170_MAX_CMD_PAYLOAD_LEN);
53
54                 send_cmd_to_host(block, CARL9170_RSP_HEXDUMP, len,
55                                  (const uint8_t *) buf + offset);
56
57                 offset += block;
58                 len -= block;
59         }
60 }
61 #endif /* CONFIG_CARL9170FW_DEBUG_USB */
62
63 /* grab a buffer from the interrupt in queue ring-buffer */
64 static struct carl9170_rsp *get_int_buf(void)
65 {
66         struct carl9170_rsp *tmp;
67
68         tmp = &fw.usb.int_buf[fw.usb.int_tail_index++];
69         fw.usb.int_tail_index %= CARL9170_INT_RQ_CACHES;
70         if (fw.usb.int_pending != CARL9170_INT_RQ_CACHES)
71                 fw.usb.int_pending++;
72
73         return tmp;
74 }
75
76 /* Pop up data from Interrupt IN Queue to USB Response buffer */
77 static struct carl9170_rsp *dequeue_int_buf(unsigned int space)
78 {
79         struct carl9170_rsp *tmp = NULL;
80
81         if (fw.usb.int_pending > 0) {
82                 tmp = &fw.usb.int_buf[fw.usb.int_head_index];
83
84                 if ((unsigned int)(tmp->hdr.len + 8) > space)
85                         return NULL;
86
87                 fw.usb.int_head_index++;
88                 fw.usb.int_head_index %= CARL9170_INT_RQ_CACHES;
89                 fw.usb.int_pending--;
90         }
91
92         return tmp;
93 }
94
95 static void usb_data_in(void)
96 {
97 }
98
99 static void usb_reg_out(void)
100 {
101         uint32_t *regaddr = (uint32_t *) &dma_mem.reserved.cmd;
102         uint16_t usbfifolen, i;
103
104         usb_reset_out();
105
106         usbfifolen = getb(AR9170_USB_REG_EP4_BYTE_COUNT_LOW) |
107                      getb(AR9170_USB_REG_EP4_BYTE_COUNT_HIGH) << 8;
108
109         if (usbfifolen & 0x3)
110                 usbfifolen = (usbfifolen >> 2) + 1;
111         else
112                 usbfifolen = usbfifolen >> 2;
113
114         for (i = 0; i < usbfifolen; i++)
115                 *regaddr++ = get(AR9170_USB_REG_EP4_DATA);
116
117         handle_cmd(get_int_buf());
118
119         usb_trigger_in();
120 }
121
122 static void usb_status_in(void)
123 {
124         struct carl9170_rsp *rsp;
125         unsigned int rem, tlen, elen;
126
127         if (!fw.usb.int_desc_available)
128                 return ;
129
130         fw.usb.int_desc_available = 0;
131
132         rem = AR9170_BLOCK_SIZE - AR9170_INT_MAGIC_HEADER_SIZE;
133         tlen = AR9170_INT_MAGIC_HEADER_SIZE;
134
135         usb_reset_in();
136
137         while (fw.usb.int_pending) {
138                 rsp = dequeue_int_buf(rem);
139                 if (!rsp)
140                         break;
141
142                 elen = rsp->hdr.len + 4;
143
144                 memcpy(DESC_PAYLOAD_OFF(fw.usb.int_desc, tlen), rsp, elen);
145
146                 rem -= elen;
147                 tlen += elen;
148         }
149
150         if (tlen == AR9170_INT_MAGIC_HEADER_SIZE) {
151                 DBG("attempted to send an empty int response!\n");
152                 goto reclaim;
153         }
154
155         fw.usb.int_desc->ctrl = AR9170_CTRL_FS_BIT | AR9170_CTRL_LS_BIT;
156         fw.usb.int_desc->totalLen = tlen;
157         fw.usb.int_desc->dataSize = tlen;
158
159         /* Put to UpQ */
160         dma_put(&fw.pta.up_queue, fw.usb.int_desc);
161
162         /* Trigger PTA UP DMA */
163         set(AR9170_PTA_REG_UP_DMA_TRIGGER, 1);
164         usb_trigger_out();
165
166         return ;
167
168 reclaim:
169         /* TODO: not sure what to do here */
170         fw.usb.int_desc_available = 1;
171 }
172
173 void send_cmd_to_host(const uint8_t len, const uint8_t type,
174                       const uint8_t ext, const uint8_t *body)
175 {
176         struct carl9170_cmd *resp;
177
178 #ifdef CONFIG_CARL9170FW_DEBUG
179         if (unlikely(len > sizeof(resp->data))) {
180                 DBG("CMD too long:%x %d\n", type, len);
181                 return ;
182         }
183
184         /* Element length must be a multiple of 4. */
185         if (unlikely(len & 0x3)) {
186                 DBG("CMD length not mult. of 4:%x %d\n", type, len);
187                 return ;
188         }
189 #endif /* CONFIG_CARL9170FW_DEBUG */
190
191         resp = (struct carl9170_cmd *) get_int_buf();
192         if (unlikely(resp == NULL)) {
193                 /* not very helpful for NON UART users */
194                 DBG("out of msg buffers\n");
195                 return ;
196         }
197
198         resp->hdr.len = len;
199         resp->hdr.cmd = type;
200         resp->hdr.ext = ext;
201
202         memcpy(resp->data, body, len);
203         usb_trigger_in();
204 }
205
206 /* Reset all the USB FIFO used for WLAN */
207 static void usb_reset_FIFO(void)
208 {
209         uint32_t val;
210
211         /*
212          * of course,
213          * simpley ORing AR9170_MAC_POWER_STATE_CTRL_RESET
214          * would be... I dunno, maybe: just to simple?
215          */
216
217         val = get(AR9170_MAC_REG_POWER_STATE_CTRL);
218         val |= AR9170_MAC_POWER_STATE_CTRL_RESET;
219         set(AR9170_MAC_REG_POWER_STATE_CTRL, val);
220
221         /* Reset USB FIFO */
222         set(AR9170_PWR_REG_ADDA_BB, AR9170_PWR_ADDA_BB_USB_FIFO_RESET);
223         set(AR9170_PWR_REG_ADDA_BB, 0x0);
224 }
225
226 /* Turn off ADDA/RF power, PLL */
227 static void turn_power_off(void)
228 {
229         set(AR9170_PHY_REG_ACTIVE, AR9170_PHY_ACTIVE_DIS);
230         set(AR9170_PHY_REG_ADC_CTL, 0xa0000000 |
231             AR9170_PHY_ADC_CTL_OFF_PWDADC | AR9170_PHY_ADC_CTL_OFF_PWDDAC);
232
233         set(AR9170_GPIO_REG_PORT_DATA, 0);
234         set(AR9170_GPIO_REG_PORT_TYPE, 0xf);
235
236         set(AR9170_PWR_REG_BASE, 0x40021);
237         set(AR9170_PWR_REG_ADDA_BB, 0);
238
239         clock_set(false, AHB_20_22MHZ);
240
241         set(AR9170_PWR_REG_PLL_ADDAC, 0x5163);  /* 0x502b; */
242         set(AR9170_PHY_REG_ADC_SERIAL_CTL, AR9170_PHY_ADC_SCTL_SEL_EXTERNAL_RADIO);
243         set(0x1c589c, 0);       /* 7-0 */
244         set(0x1c589c, 0);       /* 15-8 */
245         set(0x1c589c, 0);       /* 23-16 */
246         set(0x1c589c, 0);       /* 31- */
247         set(0x1c589c, 0);       /* 39- */
248         set(0x1c589c, 0);       /* 47- */
249         set(0x1c589c, 0);       /* 55- */
250         set(0x1c589c, 0xf8);    /* 63- */
251         set(0x1c589c, 0x27);    /* 0x24;        71-     modified */
252         set(0x1c589c, 0xf9);    /* 79- */
253         set(0x1c589c, 0x90);    /* 87- */
254         set(0x1c589c, 0x04);    /* 95- */
255         set(0x1c589c, 0x48);    /* 103- */
256         set(0x1c589c, 0x19);    /* 0;           111-    modified */
257         set(0x1c589c, 0);       /* 119- */
258         set(0x1c589c, 0);       /* 127- */
259         set(0x1c589c, 0);       /* 135- */
260         set(0x1c589c, 0);       /* 143- */
261         set(0x1c589c, 0);       /* 151- */
262         set(0x1c589c, 0x70);    /* 159- */
263         set(0x1c589c, 0x0c);    /* 167- */
264         set(0x1c589c, 0);       /* 175- */
265         set(0x1c589c, 0);       /* 183-176 */
266         set(0x1c589c, 0);       /* 191-184 */
267         set(0x1c589c, 0);       /* 199- */
268         set(0x1c589c, 0);       /* 207- */
269         set(0x1c589c, 0);       /* 215- */
270         set(0x1c589c, 0);       /* 223- */
271         set(0x1c589c, 0);       /* 231- */
272         set(0x1c58c4, 0);       /* 233- 232 */
273         set(AR9170_PHY_REG_ADC_SERIAL_CTL, AR9170_PHY_ADC_SCTL_SEL_INTERNAL_ADDAC);
274 }
275
276 void __attribute__((noreturn)) reboot(void)
277 {
278         /* turn off leds */
279         led_set(0);
280
281         /* write watchdog magic pattern for suspend  */
282         andl(AR9170_PWR_REG_WATCH_DOG_MAGIC, 0xffff);
283         orl(AR9170_PWR_REG_WATCH_DOG_MAGIC, 0x98760000);
284
285         /* Disable watchdog */
286         orl(AR9170_TIMER_REG_WATCH_DOG, 0xffff);
287
288         /* Reset USB FIFO */
289         usb_reset_FIFO();
290
291         /* Turn off power */
292         turn_power_off();
293
294         /* add by ygwei for work around USB PHY chirp sequence problem */
295         set(0x10f100, 0x12345678);
296
297         /* Jump to boot code */
298         jump_to_bootcode();
299 }
300
301 /* service USB events and re-enable USB interrupt */
302 static void usb_handler(uint8_t usb_interrupt_level1)
303 {
304         uint8_t usb_interrupt_level2;
305
306         if (usb_interrupt_level1 & BIT(5))
307                 usb_data_in();
308
309         if (usb_interrupt_level1 & BIT(4))
310                 usb_reg_out();
311
312         if (usb_interrupt_level1 & BIT(6))
313                 usb_status_in();
314
315         if (usb_interrupt_level1 & BIT(0)) {
316                 usb_interrupt_level2 = getb(AR9170_USB_REG_INTR_SOURCE_0);
317
318                 if (usb_interrupt_level2 & BIT(0))
319                         usb_ep0setup();
320
321                 if (usb_interrupt_level2 & BIT(1))
322                         usb_ep0tx();
323
324                 if (usb_interrupt_level2 & BIT(2))
325                         usb_ep0rx();
326
327                 if (usb_interrupt_level2 & BIT(7)) {
328                         /* Clear the command abort interrupt */
329                         andb(AR9170_USB_REG_INTR_SOURCE_0, 0x7f);
330                 }
331
332                 if (usb_interrupt_level2 & BIT(3) ||
333                     fw.usb.ep0_action & CARL9170_EP0_STALL) {
334                         /*
335                          * transmission failure.
336                          * stall ep 0
337                          */
338                         setb(AR9170_USB_REG_CX_CONFIG_STATUS, BIT(2));
339                         fw.usb.ep0_action &= ~CARL9170_EP0_STALL;
340                 }
341
342                 if (usb_interrupt_level2 & BIT(4) ||
343                     fw.usb.ep0_action & CARL9170_EP0_TRIGGER) {
344                         /*
345                          * transmission done.
346                          * set DONE bit.
347                          */
348                         setb(AR9170_USB_REG_CX_CONFIG_STATUS, BIT(0));
349                         fw.usb.ep0_action &= ~CARL9170_EP0_TRIGGER;
350                 }
351         }
352
353         if (usb_interrupt_level1 & BIT(7)) {
354                 usb_interrupt_level2 = getb(AR9170_USB_REG_INTR_SOURCE_7);
355
356                 if (usb_interrupt_level2 & BIT(7))
357                         usb_data_out0Byte();
358
359                 if (usb_interrupt_level2 & BIT(6))
360                         usb_data_in0Byte();
361
362                 if (usb_interrupt_level2 & BIT(1)) {
363                         usb_reset_ack();
364                         reboot();
365                 }
366
367                 if (usb_interrupt_level2 & BIT(2)) {
368                         /* ACK USB suspend interrupt */
369                         usb_suspend_ack();
370
371                         /* Set GO_TO_SUSPEND bit to USB main control register */
372                         setb(AR9170_USB_REG_MAIN_CTRL, BIT(3));
373
374                         /* add by ygwei for work around USB PHY chirp sequence problem */
375                         set(0x10f100, 0x12345678);
376
377                         reboot();
378                 }
379
380                 if (usb_interrupt_level2 & BIT(3))
381                         usb_resume_ack();
382         }
383 }
384
385 void handle_usb(void)
386 {
387         uint8_t usb_interrupt_level1;
388
389         usb_interrupt_level1 = getb(AR9170_USB_REG_INTR_GROUP);
390
391         if (usb_interrupt_level1)
392                 usb_handler(usb_interrupt_level1);
393
394         if (fw.usb.int_pending > 0)
395                 usb_trigger_in();
396 }
397
398 #ifdef CONFIG_CARL9170FW_USB_WATCHDOG
399 void usb_watchdog_timer(void)
400 {
401         if (fw.usb.watchdog.state == cpu_to_le32(CARL9170_USB_WATCHDOG_INACTIVE))
402                 return;
403
404         fw.usb.watchdog.state++;
405
406         if (le32_to_cpu(fw.usb.watchdog.state) >= CARL9170_USB_WATCHDOG_TRIGGER_THRESHOLD) {
407                 for (;;) {
408                         /*
409                          * Simply wait until the HW watchdog
410                          * timer has elapsed.
411                          */
412                 }
413         }
414
415         send_cmd_to_host(sizeof(fw.usb.watchdog), CARL9170_RSP_USB_WD,
416                          0x80, (uint8_t *) &fw.usb.watchdog);
417 }
418 #endif /* CONFIG_CARL9170FW_USB_WATCHDOG */
419