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