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.
30 #include "shared/phy.h"
32 #ifdef CONFIG_CARL9170FW_DEBUG_USB
33 void usb_putc(const char c)
35 fw.usb.put_buffer[fw.usb.put_index++] = (uint8_t) c;
37 if (fw.usb.put_index == CARL9170_MAX_CMD_PAYLOAD_LEN || c == '\0') {
38 fw.usb.put_buffer[fw.usb.put_index] = 0;
40 send_cmd_to_host(__roundup(fw.usb.put_index, 4),
41 CARL9170_RSP_TEXT, fw.usb.put_index,
47 void usb_print_hex_dump(const void *buf, int len)
49 unsigned int offset = 0, block = 0;
51 block = min(__roundup(len, 4), CARL9170_MAX_CMD_PAYLOAD_LEN);
53 send_cmd_to_host(block, CARL9170_RSP_HEXDUMP, len,
54 (const uint8_t *) buf + offset);
60 #endif /* CONFIG_CARL9170FW_DEBUG_USB */
62 /* grab a buffer from the interrupt in queue ring-buffer */
63 static struct carl9170_rsp *get_int_buf(void)
65 struct carl9170_rsp *tmp;
67 /* fetch the _oldest_ buffer from the ring */
68 tmp = &fw.usb.int_buf[fw.usb.int_tail_index];
70 /* assign a unique sequence for every response/trap */
71 tmp->hdr.seq = fw.usb.int_tail_index;
73 fw.usb.int_tail_index++;
75 fw.usb.int_tail_index %= CARL9170_INT_RQ_CACHES;
76 if (fw.usb.int_pending != CARL9170_INT_RQ_CACHES)
82 /* Pop up data from Interrupt IN Queue to USB Response buffer */
83 static struct carl9170_rsp *dequeue_int_buf(unsigned int space)
85 struct carl9170_rsp *tmp = NULL;
87 if (fw.usb.int_pending > 0) {
88 tmp = &fw.usb.int_buf[fw.usb.int_head_index];
90 if ((unsigned int)(tmp->hdr.len + 8) > space)
93 fw.usb.int_head_index++;
94 fw.usb.int_head_index %= CARL9170_INT_RQ_CACHES;
101 static void usb_data_in(void)
105 static void usb_reg_out(void)
107 uint32_t *regaddr = (uint32_t *) &dma_mem.reserved.cmd;
108 uint16_t usbfifolen, i;
112 usbfifolen = getb(AR9170_USB_REG_EP4_BYTE_COUNT_LOW) |
113 getb(AR9170_USB_REG_EP4_BYTE_COUNT_HIGH) << 8;
115 if (usbfifolen & 0x3)
116 usbfifolen = (usbfifolen >> 2) + 1;
118 usbfifolen = usbfifolen >> 2;
120 for (i = 0; i < usbfifolen; i++)
121 *regaddr++ = get(AR9170_USB_REG_EP4_DATA);
123 handle_cmd(get_int_buf());
128 static void usb_status_in(void)
130 struct carl9170_rsp *rsp;
131 unsigned int rem, tlen, elen;
133 if (!fw.usb.int_desc_available)
136 fw.usb.int_desc_available = 0;
138 rem = AR9170_BLOCK_SIZE - AR9170_INT_MAGIC_HEADER_SIZE;
139 tlen = AR9170_INT_MAGIC_HEADER_SIZE;
143 while (fw.usb.int_pending) {
144 rsp = dequeue_int_buf(rem);
148 elen = rsp->hdr.len + 4;
150 memcpy(DESC_PAYLOAD_OFF(fw.usb.int_desc, tlen), rsp, elen);
156 if (tlen == AR9170_INT_MAGIC_HEADER_SIZE) {
157 DBG("attempted to send an empty int response!\n");
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;
166 dma_put(&fw.pta.up_queue, fw.usb.int_desc);
168 /* Trigger PTA UP DMA */
169 set(AR9170_PTA_REG_UP_DMA_TRIGGER, 1);
175 /* TODO: not sure what to do here */
176 fw.usb.int_desc_available = 1;
179 void send_cmd_to_host(const uint8_t len, const uint8_t type,
180 const uint8_t ext, const uint8_t *body)
182 struct carl9170_cmd *resp;
184 #ifdef CONFIG_CARL9170FW_DEBUG
185 if (unlikely(len > sizeof(resp->data))) {
186 DBG("CMD too long:%x %d\n", type, len);
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);
195 #endif /* CONFIG_CARL9170FW_DEBUG */
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");
205 resp->hdr.cmd = type;
208 memcpy(resp->data, body, len);
212 /* Turn off ADDA/RF power, PLL */
213 static void turn_power_off(void)
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);
219 /* This will also turn-off the LEDs */
220 set(AR9170_GPIO_REG_PORT_DATA, 0);
221 set(AR9170_GPIO_REG_PORT_TYPE, 0xf);
223 set(AR9170_PWR_REG_BASE, 0x40021);
225 set(AR9170_MAC_REG_DMA_TRIGGER, 0);
227 set(AR9170_MAC_REG_POWER_STATE_CTRL,
228 AR9170_MAC_POWER_STATE_CTRL_RESET);
231 set(AR9170_PWR_REG_RESET, AR9170_PWR_RESET_COMMIT_RESET_MASK |
232 AR9170_PWR_RESET_DMA_MASK |
233 AR9170_PWR_RESET_WLAN_MASK);
234 set(AR9170_PWR_REG_RESET, 0x0);
236 clock_set(false, AHB_20_22MHZ);
238 set(AR9170_PWR_REG_PLL_ADDAC, 0x5163); /* 0x502b; */
239 set(AR9170_PHY_REG_ADC_SERIAL_CTL, AR9170_PHY_ADC_SCTL_SEL_EXTERNAL_RADIO);
240 set(0x1c589c, 0); /* 7-0 */
241 set(0x1c589c, 0); /* 15-8 */
242 set(0x1c589c, 0); /* 23-16 */
243 set(0x1c589c, 0); /* 31- */
244 set(0x1c589c, 0); /* 39- */
245 set(0x1c589c, 0); /* 47- */
246 set(0x1c589c, 0); /* 55- */
247 set(0x1c589c, 0xf8); /* 63- */
248 set(0x1c589c, 0x27); /* 0x24; 71- modified */
249 set(0x1c589c, 0xf9); /* 79- */
250 set(0x1c589c, 0x90); /* 87- */
251 set(0x1c589c, 0x04); /* 95- */
252 set(0x1c589c, 0x48); /* 103- */
253 set(0x1c589c, 0x19); /* 0; 111- modified */
254 set(0x1c589c, 0); /* 119- */
255 set(0x1c589c, 0); /* 127- */
256 set(0x1c589c, 0); /* 135- */
257 set(0x1c589c, 0); /* 143- */
258 set(0x1c589c, 0); /* 151- */
259 set(0x1c589c, 0x70); /* 159- */
260 set(0x1c589c, 0x0c); /* 167- */
261 set(0x1c589c, 0); /* 175- */
262 set(0x1c589c, 0); /* 183-176 */
263 set(0x1c589c, 0); /* 191-184 */
264 set(0x1c589c, 0); /* 199- */
265 set(0x1c589c, 0); /* 207- */
266 set(0x1c589c, 0); /* 215- */
267 set(0x1c589c, 0); /* 223- */
268 set(0x1c589c, 0); /* 231- */
269 set(0x1c58c4, 0); /* 233- 232 */
270 set(AR9170_PHY_REG_ADC_SERIAL_CTL, AR9170_PHY_ADC_SCTL_SEL_INTERNAL_ADDAC);
273 void __attribute__((noreturn)) reboot(void)
275 /* write watchdog magic pattern for suspend */
276 andl(AR9170_PWR_REG_WATCH_DOG_MAGIC, 0xffff);
277 orl(AR9170_PWR_REG_WATCH_DOG_MAGIC, 0x98760000);
279 /* Disable watchdog */
280 orl(AR9170_TIMER_REG_WATCH_DOG, 0xffff);
285 /* add by ygwei for work around USB PHY chirp sequence problem */
286 set(0x10f100, 0x12345678);
288 /* Jump to boot code */
292 /* service USB events and re-enable USB interrupt */
293 static void usb_handler(uint8_t usb_interrupt_level1)
295 uint8_t usb_interrupt_level2;
297 if (usb_interrupt_level1 & BIT(5))
300 if (usb_interrupt_level1 & BIT(4))
303 if (usb_interrupt_level1 & BIT(6))
306 if (usb_interrupt_level1 & BIT(0)) {
307 usb_interrupt_level2 = getb(AR9170_USB_REG_INTR_SOURCE_0);
309 if (usb_interrupt_level2 & BIT(0))
312 if (usb_interrupt_level2 & BIT(1))
315 if (usb_interrupt_level2 & BIT(2))
318 if (usb_interrupt_level2 & BIT(7)) {
319 /* Clear the command abort interrupt */
320 andb(AR9170_USB_REG_INTR_SOURCE_0, 0x7f);
323 if (usb_interrupt_level2 & BIT(3) ||
324 fw.usb.ep0_action & CARL9170_EP0_STALL) {
326 * transmission failure.
329 setb(AR9170_USB_REG_CX_CONFIG_STATUS, BIT(2));
330 fw.usb.ep0_action &= ~CARL9170_EP0_STALL;
333 if (usb_interrupt_level2 & BIT(4) ||
334 fw.usb.ep0_action & CARL9170_EP0_TRIGGER) {
339 setb(AR9170_USB_REG_CX_CONFIG_STATUS, BIT(0));
340 fw.usb.ep0_action &= ~CARL9170_EP0_TRIGGER;
344 if (usb_interrupt_level1 & BIT(7)) {
345 usb_interrupt_level2 = getb(AR9170_USB_REG_INTR_SOURCE_7);
347 if (usb_interrupt_level2 & BIT(7))
350 if (usb_interrupt_level2 & BIT(6))
353 if (usb_interrupt_level2 & BIT(1)) {
358 if (usb_interrupt_level2 & BIT(2)) {
359 /* ACK USB suspend interrupt */
362 /* Set GO_TO_SUSPEND bit to USB main control register */
363 setb(AR9170_USB_REG_MAIN_CTRL, BIT(3));
365 /* add by ygwei for work around USB PHY chirp sequence problem */
366 set(0x10f100, 0x12345678);
371 if (usb_interrupt_level2 & BIT(3))
376 void handle_usb(void)
378 uint8_t usb_interrupt_level1;
380 usb_interrupt_level1 = getb(AR9170_USB_REG_INTR_GROUP);
382 if (usb_interrupt_level1)
383 usb_handler(usb_interrupt_level1);
385 if (fw.usb.int_pending > 0)