Setting up repository
[linux-libre-firmware.git] / carl9170fw / carlfw / include / usb.h
1 /*
2  * carl9170 firmware - used by the ar9170 wireless device
3  *
4  * USB definitions
5  *
6  * Copyright (c) 2000-2005 ZyDAS Technology Corporation
7  * Copyright (c) 2007-2009 Atheros Communications, Inc.
8  * Copyright    2009    Johannes Berg <johannes@sipsolutions.net>
9  * Copyright 2009-2011  Christian Lamparter <chunkeey@googlemail.com>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License along
22  * with this program; if not, write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25
26 #ifndef __CARL9170FW_USB_H
27 #define __CARL9170FW_USB_H
28
29 #include "config.h"
30 #include "types.h"
31 #include "io.h"
32 #include "hw.h"
33 #include "ch9.h"
34
35 struct ar9170_usb_config {
36         struct usb_config_descriptor cfg;
37         struct usb_interface_descriptor intf;
38         struct usb_endpoint_descriptor ep[AR9170_USB_NUM_EXTRA_EP];
39 } __packed;
40
41 static inline __inline bool usb_detect_highspeed(void)
42 {
43         return !!(getb(AR9170_USB_REG_MAIN_CTRL) &
44                   AR9170_USB_MAIN_CTRL_HIGHSPEED);
45 }
46
47 static inline __inline bool usb_configured(void)
48 {
49         return !!(getb(AR9170_USB_REG_DEVICE_ADDRESS) &
50                   AR9170_USB_DEVICE_ADDRESS_CONFIGURE);
51 }
52
53 static inline __inline void usb_enable_remote_wakeup(void)
54 {
55         orb(AR9170_USB_REG_MAIN_CTRL, AR9170_USB_MAIN_CTRL_REMOTE_WAKEUP);
56 }
57
58 static inline __inline void usb_disable_remote_wakeup(void)
59 {
60         andb(AR9170_USB_REG_MAIN_CTRL, ~AR9170_USB_MAIN_CTRL_REMOTE_WAKEUP);
61 }
62
63 static inline __inline void usb_enable_global_int(void)
64 {
65         orb(AR9170_USB_REG_MAIN_CTRL, AR9170_USB_MAIN_CTRL_ENABLE_GLOBAL_INT);
66 }
67
68 static inline __inline void usb_trigger_out(void)
69 {
70         andb(AR9170_USB_REG_INTR_MASK_BYTE_4,
71                 (uint8_t) ~AR9170_USB_INTR_DISABLE_OUT_INT);
72 }
73
74 static inline __inline void usb_reset_out(void)
75 {
76         orb(AR9170_USB_REG_INTR_MASK_BYTE_4, AR9170_USB_INTR_DISABLE_OUT_INT);
77 }
78
79 static inline __inline void usb_trigger_in(void)
80 {
81         andb(AR9170_USB_REG_INTR_MASK_BYTE_6, ~AR9170_USB_INTR_DISABLE_IN_INT);
82 }
83
84 static inline __inline void usb_reset_in(void)
85 {
86         orb(AR9170_USB_REG_INTR_MASK_BYTE_6, AR9170_USB_INTR_DISABLE_IN_INT);
87 }
88
89 static inline __inline void usb_ep3_xfer_done(void)
90 {
91         orb(AR9170_USB_REG_EP3_BYTE_COUNT_HIGH, 0x08);
92 }
93
94 static inline __inline void usb_suspend_ack(void)
95 {
96         /*
97          * uP must do-over everything it should handle
98          * and do before into the suspend mode
99          */
100         andb(AR9170_USB_REG_INTR_SOURCE_7, ~BIT(2));
101 }
102
103 static inline __inline void usb_resume_ack(void)
104 {
105         /*
106          * uP must do-over everything it should handle
107          * and do before into the suspend mode
108          */
109
110         andb(AR9170_USB_REG_INTR_SOURCE_7, ~BIT(3));
111 }
112
113 static inline __inline void usb_reset_ack(void)
114 {
115         andb(AR9170_USB_REG_INTR_SOURCE_7, ~BIT(1));
116 }
117
118 static inline __inline void usb_data_out0Byte(void)
119 {
120         andb(AR9170_USB_REG_INTR_SOURCE_7, (uint8_t) ~BIT(7));
121 }
122
123 static inline __inline void usb_data_in0Byte(void)
124 {
125         andb(AR9170_USB_REG_INTR_SOURCE_7, ~BIT(6));
126 }
127
128 static inline __inline void usb_stop_down_queue(void)
129 {
130         andl(AR9170_USB_REG_DMA_CTL, ~AR9170_USB_DMA_CTL_ENABLE_TO_DEVICE);
131 }
132
133 static inline __inline void usb_start_down_queue(void)
134 {
135         orl(AR9170_USB_REG_DMA_CTL, AR9170_USB_DMA_CTL_ENABLE_TO_DEVICE);
136 }
137
138 static inline __inline void usb_clear_input_ep_toggle(unsigned int ep)
139 {
140         andl(AR9170_USB_REG_EP_IN_MAX_SIZE_HIGH + (ep << 1),
141              ~AR9170_USB_EP_IN_TOGGLE);
142 }
143
144 static inline __inline void usb_clear_input_ep_stall(unsigned int ep)
145 {
146         andl(AR9170_USB_REG_EP_IN_MAX_SIZE_HIGH + (ep << 1),
147              ~AR9170_USB_EP_IN_STALL);
148 }
149
150 static inline __inline void usb_set_input_ep_toggle(unsigned int ep)
151 {
152         orl(AR9170_USB_REG_EP_IN_MAX_SIZE_HIGH + (ep << 1),
153             AR9170_USB_EP_IN_TOGGLE);
154 }
155
156 static inline __inline void usb_clear_output_ep_toggle(unsigned int ep)
157 {
158         andl(AR9170_USB_REG_EP_OUT_MAX_SIZE_HIGH + (ep << 1),
159              ~AR9170_USB_EP_OUT_TOGGLE);
160 }
161
162 static inline __inline void usb_set_output_ep_toggle(unsigned int ep)
163 {
164         orl(AR9170_USB_REG_EP_OUT_MAX_SIZE_HIGH + (ep << 1),
165             AR9170_USB_EP_OUT_TOGGLE);
166 }
167
168 static inline __inline void usb_clear_output_ep_stall(unsigned int ep)
169 {
170         andl(AR9170_USB_REG_EP_OUT_MAX_SIZE_HIGH + (ep << 1),
171              ~AR9170_USB_EP_OUT_STALL);
172 }
173
174 static inline void usb_structure_check(void)
175 {
176         BUILD_BUG_ON(sizeof(struct usb_config_descriptor) != USB_DT_CONFIG_SIZE);
177         BUILD_BUG_ON(sizeof(struct usb_device_descriptor) != USB_DT_DEVICE_SIZE);
178         BUILD_BUG_ON(sizeof(struct usb_endpoint_descriptor) != USB_DT_ENDPOINT_SIZE);
179         BUILD_BUG_ON(sizeof(struct usb_interface_descriptor) != USB_DT_INTERFACE_SIZE);
180 }
181
182 void __noreturn jump_to_bootcode(void);
183
184 void send_cmd_to_host(const uint8_t len, const uint8_t type,
185                       const uint8_t ext, const uint8_t *body);
186
187 void usb_reset_eps(void);
188 void usb_init(void);
189 void usb_ep0rx(void);
190 void usb_ep0tx(void);
191 void usb_ep0setup(void);
192 void handle_usb(void);
193
194 void usb_timer(void);
195 void usb_putc(const char c);
196 void usb_print_hex_dump(const void *buf, int len);
197
198 void usb_init_highspeed_fifo_cfg(void);
199 void usb_init_fullspeed_fifo_cfg(void);
200
201 void __noreturn start(void);
202 void __noreturn reboot(void);
203
204 #endif /* __CARL9170FW_USB_H */