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 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)
76 /* Pop up data from Interrupt IN Queue to USB Response buffer */
77 static struct carl9170_rsp *dequeue_int_buf(unsigned int space)
79 struct carl9170_rsp *tmp = NULL;
81 if (fw.usb.int_pending > 0) {
82 tmp = &fw.usb.int_buf[fw.usb.int_head_index];
84 if ((unsigned int)(tmp->hdr.len + 8) > space)
87 fw.usb.int_head_index++;
88 fw.usb.int_head_index %= CARL9170_INT_RQ_CACHES;
95 static void usb_data_in(void)
99 static void usb_reg_out(void)
101 uint32_t *regaddr = (uint32_t *) &dma_mem.reserved.cmd;
102 uint16_t usbfifolen, i;
106 usbfifolen = getb(AR9170_USB_REG_EP4_BYTE_COUNT_LOW) |
107 getb(AR9170_USB_REG_EP4_BYTE_COUNT_HIGH) << 8;
109 if (usbfifolen & 0x3)
110 usbfifolen = (usbfifolen >> 2) + 1;
112 usbfifolen = usbfifolen >> 2;
114 for (i = 0; i < usbfifolen; i++)
115 *regaddr++ = get(AR9170_USB_REG_EP4_DATA);
117 handle_cmd(get_int_buf());
122 static void usb_status_in(void)
124 struct carl9170_rsp *rsp;
125 unsigned int rem, tlen, elen;
127 if (!fw.usb.int_desc_available)
130 fw.usb.int_desc_available = 0;
132 rem = AR9170_BLOCK_SIZE - AR9170_INT_MAGIC_HEADER_SIZE;
133 tlen = AR9170_INT_MAGIC_HEADER_SIZE;
137 while (fw.usb.int_pending) {
138 rsp = dequeue_int_buf(rem);
142 elen = rsp->hdr.len + 4;
144 memcpy(DESC_PAYLOAD_OFF(fw.usb.int_desc, tlen), rsp, elen);
150 if (tlen == AR9170_INT_MAGIC_HEADER_SIZE) {
151 DBG("attempted to send an empty int response!\n");
155 fw.usb.int_desc->totalLen = tlen;
156 fw.usb.int_desc->dataSize = tlen;
159 dma_put(&fw.pta.up_queue, fw.usb.int_desc);
161 /* Trigger PTA UP DMA */
162 set(AR9170_PTA_REG_UP_DMA_TRIGGER, 1);
168 /* TODO: not sure what to do here */
169 fw.usb.int_desc_available = 1;
172 void send_cmd_to_host(const uint8_t len, const uint8_t type,
173 const uint8_t ext, const uint8_t *body)
175 struct carl9170_cmd *resp;
177 #ifdef CONFIG_CARL9170FW_DEBUG
178 if (unlikely(len > sizeof(resp->data))) {
179 DBG("CMD too long:%x %d\n", type, len);
183 /* Element length must be a multiple of 4. */
184 if (unlikely(len & 0x3)) {
185 DBG("CMD length not mult. of 4:%x %d\n", type, len);
188 #endif /* CONFIG_CARL9170FW_DEBUG */
190 resp = (struct carl9170_cmd *) get_int_buf();
191 if (unlikely(resp == NULL)) {
192 /* not very helpful for NON UART users */
193 DBG("out of msg buffers\n");
198 resp->hdr.cmd = type;
201 memcpy(resp->data, body, len);
205 /* Reset all the USB FIFO used for WLAN */
206 static void usb_reset_FIFO(void)
212 * simpley ORing AR9170_MAC_POWER_STATE_CTRL_RESET
213 * would be... I dunno, maybe: just to simple?
216 val = get(AR9170_MAC_REG_POWER_STATE_CTRL);
217 val |= AR9170_MAC_POWER_STATE_CTRL_RESET;
218 set(AR9170_MAC_REG_POWER_STATE_CTRL, val);
221 set(AR9170_PWR_REG_ADDA_BB, AR9170_PWR_ADDA_BB_USB_FIFO_RESET);
222 set(AR9170_PWR_REG_ADDA_BB, 0x0);
225 /* Turn off ADDA/RF power, PLL */
226 static void turn_power_off(void)
228 set(AR9170_PHY_REG_ACTIVE, AR9170_PHY_ACTIVE_DIS);
229 set(AR9170_PHY_REG_ADC_CTL, 0xa0000000 |
230 AR9170_PHY_ADC_CTL_OFF_PWDADC | AR9170_PHY_ADC_CTL_OFF_PWDDAC);
232 set(AR9170_GPIO_REG_PORT_DATA, 0);
233 set(AR9170_GPIO_REG_PORT_TYPE, 0xf);
235 set(AR9170_PWR_REG_BASE, 0x40021);
236 set(AR9170_PWR_REG_ADDA_BB, 0);
238 clock_set(false, AHB_20_22MHZ);
240 set(AR9170_PWR_REG_PLL_ADDAC, 0x5163); /* 0x502b; */
241 set(AR9170_PHY_REG_ADC_SERIAL_CTL, AR9170_PHY_ADC_SCTL_SEL_EXTERNAL_RADIO);
242 set(0x1c589c, 0); /* 7-0 */
243 set(0x1c589c, 0); /* 15-8 */
244 set(0x1c589c, 0); /* 23-16 */
245 set(0x1c589c, 0); /* 31- */
246 set(0x1c589c, 0); /* 39- */
247 set(0x1c589c, 0); /* 47- */
248 set(0x1c589c, 0); /* 55- */
249 set(0x1c589c, 0xf8); /* 63- */
250 set(0x1c589c, 0x27); /* 0x24; 71- modified */
251 set(0x1c589c, 0xf9); /* 79- */
252 set(0x1c589c, 0x90); /* 87- */
253 set(0x1c589c, 0x04); /* 95- */
254 set(0x1c589c, 0x48); /* 103- */
255 set(0x1c589c, 0x19); /* 0; 111- modified */
256 set(0x1c589c, 0); /* 119- */
257 set(0x1c589c, 0); /* 127- */
258 set(0x1c589c, 0); /* 135- */
259 set(0x1c589c, 0); /* 143- */
260 set(0x1c589c, 0); /* 151- */
261 set(0x1c589c, 0x70); /* 159- */
262 set(0x1c589c, 0x0c); /* 167- */
263 set(0x1c589c, 0); /* 175- */
264 set(0x1c589c, 0); /* 183-176 */
265 set(0x1c589c, 0); /* 191-184 */
266 set(0x1c589c, 0); /* 199- */
267 set(0x1c589c, 0); /* 207- */
268 set(0x1c589c, 0); /* 215- */
269 set(0x1c589c, 0); /* 223- */
270 set(0x1c589c, 0); /* 231- */
271 set(0x1c58c4, 0); /* 233- 232 */
272 set(AR9170_PHY_REG_ADC_SERIAL_CTL, AR9170_PHY_ADC_SCTL_SEL_INTERNAL_ADDAC);
275 void __attribute__((noreturn)) reboot(void)
280 /* write watchdog magic pattern for suspend */
281 andl(AR9170_PWR_REG_WATCH_DOG_MAGIC, 0xffff);
282 orl(AR9170_PWR_REG_WATCH_DOG_MAGIC, 0x98760000);
284 /* Disable watchdog */
285 orl(AR9170_TIMER_REG_WATCH_DOG, 0xffff);
293 /* add by ygwei for work around USB PHY chirp sequence problem */
294 set(0x10f100, 0x12345678);
296 /* Jump to boot code */
300 /* service USB events and re-enable USB interrupt */
301 static void usb_handler(uint8_t usb_interrupt_level1)
303 uint8_t usb_interrupt_level2;
305 if (usb_interrupt_level1 & BIT(5))
308 if (usb_interrupt_level1 & BIT(4))
311 if (usb_interrupt_level1 & BIT(6))
314 if (usb_interrupt_level1 & BIT(0)) {
315 usb_interrupt_level2 = getb(AR9170_USB_REG_INTR_SOURCE_0);
317 if (usb_interrupt_level2 & BIT(0))
320 if (usb_interrupt_level2 & BIT(1))
323 if (usb_interrupt_level2 & BIT(2))
326 if (usb_interrupt_level2 & BIT(7)) {
327 /* Clear the command abort interrupt */
328 andb(AR9170_USB_REG_INTR_SOURCE_0, 0x7f);
331 if (usb_interrupt_level2 & BIT(3) ||
332 fw.usb.ep0_action & CARL9170_EP0_STALL) {
334 * transmission failure.
337 setb(AR9170_USB_REG_CX_CONFIG_STATUS, BIT(2));
338 fw.usb.ep0_action &= ~CARL9170_EP0_STALL;
341 if (usb_interrupt_level2 & BIT(4) ||
342 fw.usb.ep0_action & CARL9170_EP0_TRIGGER) {
347 setb(AR9170_USB_REG_CX_CONFIG_STATUS, BIT(0));
348 fw.usb.ep0_action &= ~CARL9170_EP0_TRIGGER;
352 if (usb_interrupt_level1 & BIT(7)) {
353 usb_interrupt_level2 = getb(AR9170_USB_REG_INTR_SOURCE_7);
355 if (usb_interrupt_level2 & BIT(7))
358 if (usb_interrupt_level2 & BIT(6))
361 if (usb_interrupt_level2 & BIT(1)) {
366 if (usb_interrupt_level2 & BIT(2)) {
367 /* ACK USB suspend interrupt */
370 /* Set GO_TO_SUSPEND bit to USB main control register */
371 setb(AR9170_USB_REG_MAIN_CTRL, BIT(3));
373 /* add by ygwei for work around USB PHY chirp sequence problem */
374 set(0x10f100, 0x12345678);
379 if (usb_interrupt_level2 & BIT(3))
384 void handle_usb(void)
386 uint8_t usb_interrupt_level1;
388 usb_interrupt_level1 = getb(AR9170_USB_REG_INTR_GROUP);
390 if (usb_interrupt_level1)
391 usb_handler(usb_interrupt_level1);
393 if (fw.usb.int_pending > 0)
397 #ifdef CONFIG_CARL9170FW_USB_WATCHDOG
398 void usb_watchdog_timer(void)
400 if (fw.usb.watchdog.state == cpu_to_le32(CARL9170_USB_WATCHDOG_INACTIVE))
403 fw.usb.watchdog.state++;
405 if (le32_to_cpu(fw.usb.watchdog.state) >= CARL9170_USB_WATCHDOG_TRIGGER_THRESHOLD) {
408 * Simply wait until the HW watchdog
414 send_cmd_to_host(sizeof(fw.usb.watchdog), CARL9170_RSP_USB_WD,
415 0x80, (uint8_t *) &fw.usb.watchdog);
417 #endif /* CONFIG_CARL9170FW_USB_WATCHDOG */