2 * carl9170 firmware - used by the ar9170 wireless device
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>
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.
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.
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.
31 #include "shared/phy.h"
33 #ifdef CONFIG_CARL9170FW_DEBUG_USB
34 void usb_putc(const char c)
36 fw.usb.put_buffer[fw.usb.put_index++] = (uint8_t) c;
38 if (fw.usb.put_index == CARL9170_MAX_CMD_PAYLOAD_LEN || c == '\0') {
39 fw.usb.put_buffer[fw.usb.put_index] = 0;
41 send_cmd_to_host(__roundup(fw.usb.put_index, 4),
42 CARL9170_RSP_TEXT, fw.usb.put_index,
48 void usb_print_hex_dump(const void *buf, int len)
50 unsigned int offset = 0, block = 0;
52 block = min(__roundup(len, 4), CARL9170_MAX_CMD_PAYLOAD_LEN);
54 send_cmd_to_host(block, CARL9170_RSP_HEXDUMP, len,
55 (const uint8_t *) buf + offset);
61 #endif /* CONFIG_CARL9170FW_DEBUG_USB */
63 /* grab a buffer from the interrupt in queue ring-buffer */
64 static struct carl9170_rsp *get_int_buf(void)
66 struct carl9170_rsp *tmp;
68 /* fetch the _oldest_ buffer from the ring */
69 tmp = &fw.usb.int_buf[fw.usb.int_tail_index];
71 /* assign a unique sequence for every response/trap */
72 tmp->hdr.seq = fw.usb.int_tail_index;
74 fw.usb.int_tail_index++;
76 fw.usb.int_tail_index %= CARL9170_INT_RQ_CACHES;
77 if (fw.usb.int_pending != CARL9170_INT_RQ_CACHES)
83 /* Pop up data from Interrupt IN Queue to USB Response buffer */
84 static struct carl9170_rsp *dequeue_int_buf(unsigned int space)
86 struct carl9170_rsp *tmp = NULL;
88 if (fw.usb.int_pending > 0) {
89 tmp = &fw.usb.int_buf[fw.usb.int_head_index];
91 if ((unsigned int)(tmp->hdr.len + 8) > space)
94 fw.usb.int_head_index++;
95 fw.usb.int_head_index %= CARL9170_INT_RQ_CACHES;
102 static void usb_data_in(void)
106 static void usb_reg_out(void)
108 uint32_t *regaddr = (uint32_t *) &dma_mem.reserved.cmd;
109 uint16_t usbfifolen, i;
113 usbfifolen = getb(AR9170_USB_REG_EP4_BYTE_COUNT_LOW) |
114 getb(AR9170_USB_REG_EP4_BYTE_COUNT_HIGH) << 8;
116 if (usbfifolen & 0x3)
117 usbfifolen = (usbfifolen >> 2) + 1;
119 usbfifolen = usbfifolen >> 2;
121 for (i = 0; i < usbfifolen; i++)
122 *regaddr++ = get(AR9170_USB_REG_EP4_DATA);
124 handle_cmd(get_int_buf());
129 static void usb_status_in(void)
131 struct carl9170_rsp *rsp;
132 unsigned int rem, tlen, elen;
134 if (!fw.usb.int_desc_available)
137 fw.usb.int_desc_available = 0;
139 rem = AR9170_BLOCK_SIZE - AR9170_INT_MAGIC_HEADER_SIZE;
140 tlen = AR9170_INT_MAGIC_HEADER_SIZE;
144 while (fw.usb.int_pending) {
145 rsp = dequeue_int_buf(rem);
149 elen = rsp->hdr.len + 4;
151 memcpy(DESC_PAYLOAD_OFF(fw.usb.int_desc, tlen), rsp, elen);
157 if (tlen == AR9170_INT_MAGIC_HEADER_SIZE) {
158 DBG("attempted to send an empty int response!\n");
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;
167 dma_put(&fw.pta.up_queue, fw.usb.int_desc);
169 /* Trigger PTA UP DMA */
170 set(AR9170_PTA_REG_UP_DMA_TRIGGER, 1);
176 /* TODO: not sure what to do here */
177 fw.usb.int_desc_available = 1;
180 void send_cmd_to_host(const uint8_t len, const uint8_t type,
181 const uint8_t ext, const uint8_t *body)
183 struct carl9170_cmd *resp;
185 #ifdef CONFIG_CARL9170FW_DEBUG
186 if (unlikely(len > sizeof(resp->data))) {
187 DBG("CMD too long:%x %d\n", type, len);
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);
196 #endif /* CONFIG_CARL9170FW_DEBUG */
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");
206 resp->hdr.cmd = type;
209 memcpy(resp->data, body, len);
213 /* Reset all the USB FIFO used for WLAN */
214 static void usb_reset_FIFO(void)
220 * simpley ORing AR9170_MAC_POWER_STATE_CTRL_RESET
221 * would be... I dunno, maybe: just to simple?
224 val = get(AR9170_MAC_REG_POWER_STATE_CTRL);
225 val |= AR9170_MAC_POWER_STATE_CTRL_RESET;
226 set(AR9170_MAC_REG_POWER_STATE_CTRL, val);
229 set(AR9170_PWR_REG_ADDA_BB, AR9170_PWR_ADDA_BB_USB_FIFO_RESET);
230 set(AR9170_PWR_REG_ADDA_BB, 0x0);
233 /* Turn off ADDA/RF power, PLL */
234 static void turn_power_off(void)
236 set(AR9170_PHY_REG_ACTIVE, AR9170_PHY_ACTIVE_DIS);
237 set(AR9170_PHY_REG_ADC_CTL, 0xa0000000 |
238 AR9170_PHY_ADC_CTL_OFF_PWDADC | AR9170_PHY_ADC_CTL_OFF_PWDDAC);
240 set(AR9170_GPIO_REG_PORT_DATA, 0);
241 set(AR9170_GPIO_REG_PORT_TYPE, 0xf);
243 set(AR9170_PWR_REG_BASE, 0x40021);
244 set(AR9170_PWR_REG_ADDA_BB, 0);
246 clock_set(false, AHB_20_22MHZ);
248 set(AR9170_PWR_REG_PLL_ADDAC, 0x5163); /* 0x502b; */
249 set(AR9170_PHY_REG_ADC_SERIAL_CTL, AR9170_PHY_ADC_SCTL_SEL_EXTERNAL_RADIO);
250 set(0x1c589c, 0); /* 7-0 */
251 set(0x1c589c, 0); /* 15-8 */
252 set(0x1c589c, 0); /* 23-16 */
253 set(0x1c589c, 0); /* 31- */
254 set(0x1c589c, 0); /* 39- */
255 set(0x1c589c, 0); /* 47- */
256 set(0x1c589c, 0); /* 55- */
257 set(0x1c589c, 0xf8); /* 63- */
258 set(0x1c589c, 0x27); /* 0x24; 71- modified */
259 set(0x1c589c, 0xf9); /* 79- */
260 set(0x1c589c, 0x90); /* 87- */
261 set(0x1c589c, 0x04); /* 95- */
262 set(0x1c589c, 0x48); /* 103- */
263 set(0x1c589c, 0x19); /* 0; 111- modified */
264 set(0x1c589c, 0); /* 119- */
265 set(0x1c589c, 0); /* 127- */
266 set(0x1c589c, 0); /* 135- */
267 set(0x1c589c, 0); /* 143- */
268 set(0x1c589c, 0); /* 151- */
269 set(0x1c589c, 0x70); /* 159- */
270 set(0x1c589c, 0x0c); /* 167- */
271 set(0x1c589c, 0); /* 175- */
272 set(0x1c589c, 0); /* 183-176 */
273 set(0x1c589c, 0); /* 191-184 */
274 set(0x1c589c, 0); /* 199- */
275 set(0x1c589c, 0); /* 207- */
276 set(0x1c589c, 0); /* 215- */
277 set(0x1c589c, 0); /* 223- */
278 set(0x1c589c, 0); /* 231- */
279 set(0x1c58c4, 0); /* 233- 232 */
280 set(AR9170_PHY_REG_ADC_SERIAL_CTL, AR9170_PHY_ADC_SCTL_SEL_INTERNAL_ADDAC);
283 void __attribute__((noreturn)) reboot(void)
288 /* write watchdog magic pattern for suspend */
289 andl(AR9170_PWR_REG_WATCH_DOG_MAGIC, 0xffff);
290 orl(AR9170_PWR_REG_WATCH_DOG_MAGIC, 0x98760000);
292 /* Disable watchdog */
293 orl(AR9170_TIMER_REG_WATCH_DOG, 0xffff);
301 /* add by ygwei for work around USB PHY chirp sequence problem */
302 set(0x10f100, 0x12345678);
304 /* Jump to boot code */
308 /* service USB events and re-enable USB interrupt */
309 static void usb_handler(uint8_t usb_interrupt_level1)
311 uint8_t usb_interrupt_level2;
313 if (usb_interrupt_level1 & BIT(5))
316 if (usb_interrupt_level1 & BIT(4))
319 if (usb_interrupt_level1 & BIT(6))
322 if (usb_interrupt_level1 & BIT(0)) {
323 usb_interrupt_level2 = getb(AR9170_USB_REG_INTR_SOURCE_0);
325 if (usb_interrupt_level2 & BIT(0))
328 if (usb_interrupt_level2 & BIT(1))
331 if (usb_interrupt_level2 & BIT(2))
334 if (usb_interrupt_level2 & BIT(7)) {
335 /* Clear the command abort interrupt */
336 andb(AR9170_USB_REG_INTR_SOURCE_0, 0x7f);
339 if (usb_interrupt_level2 & BIT(3) ||
340 fw.usb.ep0_action & CARL9170_EP0_STALL) {
342 * transmission failure.
345 setb(AR9170_USB_REG_CX_CONFIG_STATUS, BIT(2));
346 fw.usb.ep0_action &= ~CARL9170_EP0_STALL;
349 if (usb_interrupt_level2 & BIT(4) ||
350 fw.usb.ep0_action & CARL9170_EP0_TRIGGER) {
355 setb(AR9170_USB_REG_CX_CONFIG_STATUS, BIT(0));
356 fw.usb.ep0_action &= ~CARL9170_EP0_TRIGGER;
360 if (usb_interrupt_level1 & BIT(7)) {
361 usb_interrupt_level2 = getb(AR9170_USB_REG_INTR_SOURCE_7);
363 if (usb_interrupt_level2 & BIT(7))
366 if (usb_interrupt_level2 & BIT(6))
369 if (usb_interrupt_level2 & BIT(1)) {
374 if (usb_interrupt_level2 & BIT(2)) {
375 /* ACK USB suspend interrupt */
378 /* Set GO_TO_SUSPEND bit to USB main control register */
379 setb(AR9170_USB_REG_MAIN_CTRL, BIT(3));
381 /* add by ygwei for work around USB PHY chirp sequence problem */
382 set(0x10f100, 0x12345678);
387 if (usb_interrupt_level2 & BIT(3))
392 void handle_usb(void)
394 uint8_t usb_interrupt_level1;
396 usb_interrupt_level1 = getb(AR9170_USB_REG_INTR_GROUP);
398 if (usb_interrupt_level1)
399 usb_handler(usb_interrupt_level1);
401 if (fw.usb.int_pending > 0)
405 #ifdef CONFIG_CARL9170FW_USB_WATCHDOG
406 void usb_watchdog_timer(void)
408 if (fw.usb.watchdog.state == cpu_to_le32(CARL9170_USB_WATCHDOG_INACTIVE))
411 fw.usb.watchdog.state++;
413 if (le32_to_cpu(fw.usb.watchdog.state) >= CARL9170_USB_WATCHDOG_TRIGGER_THRESHOLD) {
416 * Simply wait until the HW watchdog
422 send_cmd_to_host(sizeof(fw.usb.watchdog), CARL9170_RSP_USB_WD,
423 0x80, (uint8_t *) &fw.usb.watchdog);
425 #endif /* CONFIG_CARL9170FW_USB_WATCHDOG */