Debian Codegraph @

carlfw/usb/main.c

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