Revert "carl9170 firmware: add workaround for carl vs. mac BAR problem"
[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-2011  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 "shared/phy.h"
27 #include "hostif.h"
28 #include "printf.h"
29 #include "timer.h"
30 #include "rom.h"
31 #include "wl.h"
32 #include "wol.h"
33
34 #ifdef CONFIG_CARL9170FW_DEBUG_USB
35 void usb_putc(const char c)
36 {
37         fw.usb.put_buffer[fw.usb.put_index++] = (uint8_t) c;
38
39         if (fw.usb.put_index == CARL9170_MAX_CMD_PAYLOAD_LEN || c == '\0') {
40                 fw.usb.put_buffer[fw.usb.put_index] = 0;
41
42                 send_cmd_to_host(__roundup(fw.usb.put_index, 4),
43                                  CARL9170_RSP_TEXT, fw.usb.put_index,
44                                  fw.usb.put_buffer);
45                 fw.usb.put_index = 0;
46         }
47 }
48
49 void usb_print_hex_dump(const void *buf, int len)
50 {
51         unsigned int offset = 0, block = 0;
52         while (len > 0) {
53                 block = min(__roundup(len, 4), CARL9170_MAX_CMD_PAYLOAD_LEN);
54
55                 send_cmd_to_host(block, CARL9170_RSP_HEXDUMP, len,
56                                  (const uint8_t *) buf + offset);
57
58                 offset += block;
59                 len -= block;
60         }
61 }
62 #endif /* CONFIG_CARL9170FW_DEBUG_USB */
63
64 /* grab a buffer from the interrupt in queue ring-buffer */
65 static struct carl9170_rsp *get_int_buf(void)
66 {
67         struct carl9170_rsp *tmp;
68
69         /* fetch the _oldest_ buffer from the ring */
70         tmp = &fw.usb.int_buf[fw.usb.int_tail_index];
71
72         /* assign a unique sequence for every response/trap */
73         tmp->hdr.seq = fw.usb.int_tail_index;
74
75         fw.usb.int_tail_index++;
76
77         fw.usb.int_tail_index %= CARL9170_INT_RQ_CACHES;
78         if (fw.usb.int_pending != CARL9170_INT_RQ_CACHES)
79                 fw.usb.int_pending++;
80
81         return tmp;
82 }
83
84 /* Pop up data from Interrupt IN Queue to USB Response buffer */
85 static struct carl9170_rsp *dequeue_int_buf(unsigned int space)
86 {
87         struct carl9170_rsp *tmp = NULL;
88
89         if (fw.usb.int_pending > 0) {
90                 tmp = &fw.usb.int_buf[fw.usb.int_head_index];
91
92                 if ((unsigned int)(tmp->hdr.len + 8) > space)
93                         return NULL;
94
95                 fw.usb.int_head_index++;
96                 fw.usb.int_head_index %= CARL9170_INT_RQ_CACHES;
97                 fw.usb.int_pending--;
98         }
99
100         return tmp;
101 }
102
103 static void usb_data_in(void)
104 {
105 }
106
107 static void usb_reg_out(void)
108 {
109         uint32_t *regaddr = (uint32_t *) &dma_mem.reserved.cmd;
110         uint16_t usbfifolen, i;
111
112         usb_reset_out();
113
114         usbfifolen = getb(AR9170_USB_REG_EP4_BYTE_COUNT_LOW) |
115                      getb(AR9170_USB_REG_EP4_BYTE_COUNT_HIGH) << 8;
116
117         if (usbfifolen & 0x3)
118                 usbfifolen = (usbfifolen >> 2) + 1;
119         else
120                 usbfifolen = usbfifolen >> 2;
121
122         for (i = 0; i < usbfifolen; i++)
123                 *regaddr++ = get(AR9170_USB_REG_EP4_DATA);
124
125         handle_cmd(get_int_buf());
126
127         usb_trigger_in();
128 }
129
130 static void usb_status_in(void)
131 {
132         struct carl9170_rsp *rsp;
133         unsigned int rem, tlen, elen;
134
135         if (!fw.usb.int_desc_available)
136                 return ;
137
138         fw.usb.int_desc_available = 0;
139
140         rem = AR9170_BLOCK_SIZE - AR9170_INT_MAGIC_HEADER_SIZE;
141         tlen = AR9170_INT_MAGIC_HEADER_SIZE;
142
143         usb_reset_in();
144
145         while (fw.usb.int_pending) {
146                 rsp = dequeue_int_buf(rem);
147                 if (!rsp)
148                         break;
149
150                 elen = rsp->hdr.len + 4;
151
152                 memcpy(DESC_PAYLOAD_OFF(fw.usb.int_desc, tlen), rsp, elen);
153
154                 rem -= elen;
155                 tlen += elen;
156         }
157
158         if (tlen == AR9170_INT_MAGIC_HEADER_SIZE) {
159                 DBG("attempted to send an empty int response!\n");
160                 goto reclaim;
161         }
162
163         fw.usb.int_desc->ctrl = AR9170_CTRL_FS_BIT | AR9170_CTRL_LS_BIT;
164         fw.usb.int_desc->totalLen = tlen;
165         fw.usb.int_desc->dataSize = tlen;
166
167         /* Put to UpQ */
168         dma_put(&fw.pta.up_queue, fw.usb.int_desc);
169
170         /* Trigger PTA UP DMA */
171         set(AR9170_PTA_REG_UP_DMA_TRIGGER, 1);
172         usb_trigger_out();
173
174         return ;
175
176 reclaim:
177         /* TODO: not sure what to do here */
178         fw.usb.int_desc_available = 1;
179 }
180
181 void send_cmd_to_host(const uint8_t len, const uint8_t type,
182                       const uint8_t ext, const uint8_t *body)
183 {
184         struct carl9170_cmd *resp;
185
186 #ifdef CONFIG_CARL9170FW_DEBUG
187         if (unlikely(len > sizeof(resp->data))) {
188                 DBG("CMD too long:%x %d\n", type, len);
189                 return ;
190         }
191
192         /* Element length must be a multiple of 4. */
193         if (unlikely(len & 0x3)) {
194                 DBG("CMD length not mult. of 4:%x %d\n", type, len);
195                 return ;
196         }
197 #endif /* CONFIG_CARL9170FW_DEBUG */
198
199         resp = (struct carl9170_cmd *) get_int_buf();
200         if (unlikely(resp == NULL)) {
201                 /* not very helpful for NON UART users */
202                 DBG("out of msg buffers\n");
203                 return ;
204         }
205
206         resp->hdr.len = len;
207         resp->hdr.cmd = type;
208         resp->hdr.ext = ext;
209
210         memcpy(resp->data, body, len);
211         usb_trigger_in();
212 }
213
214 /* Turn off ADDA/RF power, PLL */
215 static void turn_power_off(void)
216 {
217         set(AR9170_PHY_REG_ACTIVE, AR9170_PHY_ACTIVE_DIS);
218         set(AR9170_PHY_REG_ADC_CTL, 0xa0000000 |
219             AR9170_PHY_ADC_CTL_OFF_PWDADC | AR9170_PHY_ADC_CTL_OFF_PWDDAC);
220
221         /* This will also turn-off the LEDs */
222         set(AR9170_GPIO_REG_PORT_DATA, 0);
223         set(AR9170_GPIO_REG_PORT_TYPE, 0xf);
224
225         set(AR9170_PWR_REG_BASE, 0x40021);
226
227         set(AR9170_MAC_REG_DMA_TRIGGER, 0);
228
229         andl(AR9170_USB_REG_DMA_CTL, ~(AR9170_USB_DMA_CTL_ENABLE_TO_DEVICE |
230                                        AR9170_USB_DMA_CTL_ENABLE_FROM_DEVICE |
231                                        AR9170_USB_DMA_CTL_UP_PACKET_MODE |
232                                        AR9170_USB_DMA_CTL_DOWN_STREAM));
233
234         /* Do a software reset to PTA component */
235         orl(AR9170_PTA_REG_DMA_MODE_CTRL, AR9170_PTA_DMA_MODE_CTRL_RESET);
236         andl(AR9170_PTA_REG_DMA_MODE_CTRL, ~AR9170_PTA_DMA_MODE_CTRL_RESET);
237
238         orl(AR9170_PTA_REG_DMA_MODE_CTRL, AR9170_PTA_DMA_MODE_CTRL_DISABLE_USB);
239
240         set(AR9170_MAC_REG_POWER_STATE_CTRL,
241             AR9170_MAC_POWER_STATE_CTRL_RESET);
242
243         /* Reset USB FIFO */
244         set(AR9170_PWR_REG_RESET, AR9170_PWR_RESET_COMMIT_RESET_MASK |
245                                   AR9170_PWR_RESET_DMA_MASK |
246                                   AR9170_PWR_RESET_WLAN_MASK);
247         set(AR9170_PWR_REG_RESET, 0x0);
248
249         clock_set(AHB_20_22MHZ, false);
250
251         set(AR9170_PWR_REG_PLL_ADDAC, 0x5163);  /* 0x502b; */
252         set(AR9170_PHY_REG_ADC_SERIAL_CTL, AR9170_PHY_ADC_SCTL_SEL_EXTERNAL_RADIO);
253         set(0x1c589c, 0);       /* 7-0 */
254         set(0x1c589c, 0);       /* 15-8 */
255         set(0x1c589c, 0);       /* 23-16 */
256         set(0x1c589c, 0);       /* 31- */
257         set(0x1c589c, 0);       /* 39- */
258         set(0x1c589c, 0);       /* 47- */
259         set(0x1c589c, 0);       /* 55- */
260         set(0x1c589c, 0xf8);    /* 63- */
261         set(0x1c589c, 0x27);    /* 0x24;        71-     modified */
262         set(0x1c589c, 0xf9);    /* 79- */
263         set(0x1c589c, 0x90);    /* 87- */
264         set(0x1c589c, 0x04);    /* 95- */
265         set(0x1c589c, 0x48);    /* 103- */
266         set(0x1c589c, 0x19);    /* 0;           111-    modified */
267         set(0x1c589c, 0);       /* 119- */
268         set(0x1c589c, 0);       /* 127- */
269         set(0x1c589c, 0);       /* 135- */
270         set(0x1c589c, 0);       /* 143- */
271         set(0x1c589c, 0);       /* 151- */
272         set(0x1c589c, 0x70);    /* 159- */
273         set(0x1c589c, 0x0c);    /* 167- */
274         set(0x1c589c, 0);       /* 175- */
275         set(0x1c589c, 0);       /* 183-176 */
276         set(0x1c589c, 0);       /* 191-184 */
277         set(0x1c589c, 0);       /* 199- */
278         set(0x1c589c, 0);       /* 207- */
279         set(0x1c589c, 0);       /* 215- */
280         set(0x1c589c, 0);       /* 223- */
281         set(0x1c589c, 0);       /* 231- */
282         set(0x1c58c4, 0);       /* 233- 232 */
283         set(AR9170_PHY_REG_ADC_SERIAL_CTL, AR9170_PHY_ADC_SCTL_SEL_INTERNAL_ADDAC);
284 }
285
286 static void disable_watchdog(void)
287 {
288         if (!fw.watchdog_enable)
289                 return;
290
291         /* write watchdog magic pattern for suspend  */
292         andl(AR9170_PWR_REG_WATCH_DOG_MAGIC, 0xffff);
293         orl(AR9170_PWR_REG_WATCH_DOG_MAGIC, 0x98760000);
294
295         /* Disable watchdog */
296         set(AR9170_TIMER_REG_WATCH_DOG, 0xffff);
297 }
298
299 void __noreturn reboot(void)
300 {
301         disable_watchdog();
302
303         /* Turn off power */
304         turn_power_off();
305
306         /* clean bootloader workspace */
307         memset(&dma_mem, 0, sizeof(dma_mem));
308
309         /* add by ygwei for work around USB PHY chirp sequence problem */
310         set(0x10f100, 0x12345678);
311
312         /* Jump to boot code */
313         jump_to_bootcode();
314 }
315
316 /* service USB events and re-enable USB interrupt */
317 static void usb_handler(uint8_t usb_interrupt_level1)
318 {
319         uint8_t usb_interrupt_level2;
320
321         if (usb_interrupt_level1 & BIT(5))
322                 usb_data_in();
323
324         if (usb_interrupt_level1 & BIT(4))
325                 usb_reg_out();
326
327         if (usb_interrupt_level1 & BIT(6))
328                 usb_status_in();
329
330         if (usb_interrupt_level1 & BIT(0)) {
331                 usb_interrupt_level2 = getb(AR9170_USB_REG_INTR_SOURCE_0);
332
333                 if (usb_interrupt_level2 & AR9170_USB_INTR_SRC0_SETUP)
334                         usb_ep0setup();
335
336                 if (usb_interrupt_level2 & AR9170_USB_INTR_SRC0_IN)
337                         usb_ep0tx();
338
339                 if (usb_interrupt_level2 & AR9170_USB_INTR_SRC0_OUT)
340                         usb_ep0rx();
341
342                 if (usb_interrupt_level2 & AR9170_USB_INTR_SRC0_ABORT) {
343                         /* Clear the command abort interrupt */
344                         andb(AR9170_USB_REG_INTR_SOURCE_0, (uint8_t)
345                              ~AR9170_USB_INTR_SRC0_ABORT);
346                 }
347
348                 if (usb_interrupt_level2 & AR9170_USB_INTR_SRC0_FAIL ||
349                     fw.usb.ep0_action & CARL9170_EP0_STALL) {
350                         /*
351                          * transmission failure.
352                          * stall ep 0
353                          */
354                         setb(AR9170_USB_REG_CX_CONFIG_STATUS, BIT(2));
355                         fw.usb.ep0_action &= ~CARL9170_EP0_STALL;
356                 }
357
358                 if (usb_interrupt_level2 & AR9170_USB_INTR_SRC0_END ||
359                     fw.usb.ep0_action & CARL9170_EP0_TRIGGER) {
360                         /*
361                          * transmission done.
362                          * set DONE bit.
363                          */
364                         setb(AR9170_USB_REG_CX_CONFIG_STATUS, BIT(0));
365                         fw.usb.ep0_action &= ~CARL9170_EP0_TRIGGER;
366                 }
367         }
368
369         if (usb_interrupt_level1 & BIT(7)) {
370                 usb_interrupt_level2 = getb(AR9170_USB_REG_INTR_SOURCE_7);
371
372                 if (usb_interrupt_level2 & AR9170_USB_INTR_SRC7_RX0BYTE)
373                         usb_data_out0Byte();
374
375                 if (usb_interrupt_level2 & AR9170_USB_INTR_SRC7_TX0BYTE)
376                         usb_data_in0Byte();
377
378                 if (usb_interrupt_level2 & AR9170_USB_INTR_SRC7_USB_RESET) {
379                         usb_reset_ack();
380                         reboot();
381                 }
382
383                 if (usb_interrupt_level2 & AR9170_USB_INTR_SRC7_USB_SUSPEND) {
384                         usb_suspend_ack();
385
386                         fw.suspend_mode = CARL9170_HOST_SUSPENDED;
387
388 #ifdef CONFIG_CARL9170FW_WOL
389                         if (!(fw.usb.device_feature & USB_DEVICE_REMOTE_WAKEUP) ||
390                             !fw.wol.cmd.flags) {
391                                 disable_watchdog();
392
393                                 /* GO_TO_SUSPEND stops the CPU clock too. */
394                                 orb(AR9170_USB_REG_MAIN_CTRL, AR9170_USB_MAIN_CTRL_GO_TO_SUSPEND);
395                         } else {
396                                 wol_prepare();
397                         }
398 #else /* CONFIG_CARL9170FW_WOL */
399                         disable_watchdog();
400
401                         /* GO_TO_SUSPEND stops the CPU clock too. */
402                         orb(AR9170_USB_REG_MAIN_CTRL, AR9170_USB_MAIN_CTRL_GO_TO_SUSPEND);
403 #endif /* CONFIG_CARL9170FW_WOL */
404                 }
405
406                 if (usb_interrupt_level2 & AR9170_USB_INTR_SRC7_USB_RESUME) {
407                         usb_resume_ack();
408
409                         fw.suspend_mode = CARL9170_HOST_AWAKE;
410                         set(AR9170_USB_REG_WAKE_UP, 0);
411
412                         reboot();
413                 }
414         }
415 }
416
417 void handle_usb(void)
418 {
419         uint8_t usb_interrupt_level1;
420
421         usb_interrupt_level1 = getb(AR9170_USB_REG_INTR_GROUP);
422
423         if (usb_interrupt_level1)
424                 usb_handler(usb_interrupt_level1);
425
426         if (fw.usb.int_pending > 0)
427                 usb_trigger_in();
428 }
429
430 void usb_timer(void)
431 {
432 }