Chunfeng Yun | df2069a | 2016-10-19 10:28:23 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * mtu3.h - MediaTek USB3 DRD header |
| 3 | * |
| 4 | * Copyright (C) 2016 MediaTek Inc. |
| 5 | * |
| 6 | * Author: Chunfeng Yun <chunfeng.yun@mediatek.com> |
| 7 | * |
| 8 | * This software is licensed under the terms of the GNU General Public |
| 9 | * License version 2, as published by the Free Software Foundation, and |
| 10 | * may be copied, distributed, and modified under those terms. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | */ |
| 18 | |
| 19 | #ifndef __MTU3_H__ |
| 20 | #define __MTU3_H__ |
| 21 | |
| 22 | #include <linux/device.h> |
| 23 | #include <linux/dmapool.h> |
| 24 | #include <linux/interrupt.h> |
| 25 | #include <linux/list.h> |
| 26 | #include <linux/phy/phy.h> |
| 27 | #include <linux/regulator/consumer.h> |
| 28 | #include <linux/usb.h> |
| 29 | #include <linux/usb/ch9.h> |
| 30 | #include <linux/usb/gadget.h> |
| 31 | #include <linux/usb/otg.h> |
| 32 | |
| 33 | struct mtu3; |
| 34 | struct mtu3_ep; |
| 35 | struct mtu3_request; |
| 36 | |
| 37 | #include "mtu3_hw_regs.h" |
| 38 | #include "mtu3_qmu.h" |
| 39 | |
| 40 | #define MU3D_EP_TXCR0(epnum) (U3D_TX1CSR0 + (((epnum) - 1) * 0x10)) |
| 41 | #define MU3D_EP_TXCR1(epnum) (U3D_TX1CSR1 + (((epnum) - 1) * 0x10)) |
| 42 | #define MU3D_EP_TXCR2(epnum) (U3D_TX1CSR2 + (((epnum) - 1) * 0x10)) |
| 43 | |
| 44 | #define MU3D_EP_RXCR0(epnum) (U3D_RX1CSR0 + (((epnum) - 1) * 0x10)) |
| 45 | #define MU3D_EP_RXCR1(epnum) (U3D_RX1CSR1 + (((epnum) - 1) * 0x10)) |
| 46 | #define MU3D_EP_RXCR2(epnum) (U3D_RX1CSR2 + (((epnum) - 1) * 0x10)) |
| 47 | |
| 48 | #define USB_QMU_RQCSR(epnum) (U3D_RXQCSR1 + (((epnum) - 1) * 0x10)) |
| 49 | #define USB_QMU_RQSAR(epnum) (U3D_RXQSAR1 + (((epnum) - 1) * 0x10)) |
| 50 | #define USB_QMU_RQCPR(epnum) (U3D_RXQCPR1 + (((epnum) - 1) * 0x10)) |
| 51 | |
| 52 | #define USB_QMU_TQCSR(epnum) (U3D_TXQCSR1 + (((epnum) - 1) * 0x10)) |
| 53 | #define USB_QMU_TQSAR(epnum) (U3D_TXQSAR1 + (((epnum) - 1) * 0x10)) |
| 54 | #define USB_QMU_TQCPR(epnum) (U3D_TXQCPR1 + (((epnum) - 1) * 0x10)) |
| 55 | |
| 56 | #define SSUSB_U2_CTRL(p) (U3D_SSUSB_U2_CTRL_0P + ((p) * 0x08)) |
| 57 | |
| 58 | #define MTU3_DRIVER_NAME "mtu3" |
| 59 | #define DMA_ADDR_INVALID (~(dma_addr_t)0) |
| 60 | |
| 61 | #define MTU3_EP_ENABLED BIT(0) |
| 62 | #define MTU3_EP_STALL BIT(1) |
| 63 | #define MTU3_EP_WEDGE BIT(2) |
| 64 | #define MTU3_EP_BUSY BIT(3) |
| 65 | |
| 66 | #define MTU3_U2_IP_SLOT_DEFAULT 1 |
| 67 | |
| 68 | /** |
| 69 | * Normally the device works on HS or SS, to simplify fifo management, |
| 70 | * devide fifo into some 512B parts, use bitmap to manage it; And |
| 71 | * 128 bits size of bitmap is large enough, that means it can manage |
| 72 | * up to 64KB fifo size. |
| 73 | * NOTE: MTU3_EP_FIFO_UNIT should be power of two |
| 74 | */ |
| 75 | #define MTU3_EP_FIFO_UNIT (1 << 9) |
| 76 | #define MTU3_FIFO_BIT_SIZE 128 |
| 77 | #define MTU3_U2_IP_EP0_FIFO_SIZE 64 |
| 78 | |
| 79 | /** |
| 80 | * Maximum size of ep0 response buffer for ch9 requests, |
| 81 | * the SET_SEL request uses 6 so far, and GET_STATUS is 2 |
| 82 | */ |
| 83 | #define EP0_RESPONSE_BUF 6 |
| 84 | |
| 85 | /* device operated link and speed got from DEVICE_CONF register */ |
| 86 | enum mtu3_speed { |
| 87 | MTU3_SPEED_INACTIVE = 0, |
| 88 | MTU3_SPEED_FULL = 1, |
| 89 | MTU3_SPEED_HIGH = 3, |
| 90 | }; |
| 91 | |
| 92 | /** |
| 93 | * @MU3D_EP0_STATE_SETUP: waits for SETUP or received a SETUP |
| 94 | * without data stage. |
| 95 | * @MU3D_EP0_STATE_TX: IN data stage |
| 96 | * @MU3D_EP0_STATE_RX: OUT data stage |
| 97 | * @MU3D_EP0_STATE_TX_END: the last IN data is transferred, and |
| 98 | * waits for its completion interrupt |
| 99 | * @MU3D_EP0_STATE_STALL: ep0 is in stall status, will be auto-cleared |
| 100 | * after receives a SETUP. |
| 101 | */ |
| 102 | enum mtu3_g_ep0_state { |
| 103 | MU3D_EP0_STATE_SETUP = 1, |
| 104 | MU3D_EP0_STATE_TX, |
| 105 | MU3D_EP0_STATE_RX, |
| 106 | MU3D_EP0_STATE_TX_END, |
| 107 | MU3D_EP0_STATE_STALL, |
| 108 | }; |
| 109 | |
| 110 | /** |
| 111 | * @base: the base address of fifo |
| 112 | * @limit: the bitmap size in bits |
| 113 | * @bitmap: fifo bitmap in unit of @MTU3_EP_FIFO_UNIT |
| 114 | */ |
| 115 | struct mtu3_fifo_info { |
| 116 | u32 base; |
| 117 | u32 limit; |
| 118 | DECLARE_BITMAP(bitmap, MTU3_FIFO_BIT_SIZE); |
| 119 | }; |
| 120 | |
| 121 | /** |
| 122 | * General Purpose Descriptor (GPD): |
| 123 | * The format of TX GPD is a little different from RX one. |
| 124 | * And the size of GPD is 16 bytes. |
| 125 | * |
| 126 | * @flag: |
| 127 | * bit0: Hardware Own (HWO) |
| 128 | * bit1: Buffer Descriptor Present (BDP), always 0, BD is not supported |
| 129 | * bit2: Bypass (BPS), 1: HW skips this GPD if HWO = 1 |
| 130 | * bit7: Interrupt On Completion (IOC) |
| 131 | * @chksum: This is used to validate the contents of this GPD; |
| 132 | * If TXQ_CS_EN / RXQ_CS_EN bit is set, an interrupt is issued |
| 133 | * when checksum validation fails; |
| 134 | * Checksum value is calculated over the 16 bytes of the GPD by default; |
| 135 | * @data_buf_len (RX ONLY): This value indicates the length of |
| 136 | * the assigned data buffer |
| 137 | * @next_gpd: Physical address of the next GPD |
| 138 | * @buffer: Physical address of the data buffer |
| 139 | * @buf_len: |
| 140 | * (TX): This value indicates the length of the assigned data buffer |
| 141 | * (RX): The total length of data received |
| 142 | * @ext_len: reserved |
| 143 | * @ext_flag: |
| 144 | * bit5 (TX ONLY): Zero Length Packet (ZLP), |
| 145 | */ |
| 146 | struct qmu_gpd { |
| 147 | __u8 flag; |
| 148 | __u8 chksum; |
| 149 | __le16 data_buf_len; |
| 150 | __le32 next_gpd; |
| 151 | __le32 buffer; |
| 152 | __le16 buf_len; |
| 153 | __u8 ext_len; |
| 154 | __u8 ext_flag; |
| 155 | } __packed; |
| 156 | |
| 157 | /** |
| 158 | * dma: physical base address of GPD segment |
| 159 | * start: virtual base address of GPD segment |
| 160 | * end: the last GPD element |
| 161 | * enqueue: the first empty GPD to use |
| 162 | * dequeue: the first completed GPD serviced by ISR |
| 163 | * NOTE: the size of GPD ring should be >= 2 |
| 164 | */ |
| 165 | struct mtu3_gpd_ring { |
| 166 | dma_addr_t dma; |
| 167 | struct qmu_gpd *start; |
| 168 | struct qmu_gpd *end; |
| 169 | struct qmu_gpd *enqueue; |
| 170 | struct qmu_gpd *dequeue; |
| 171 | }; |
| 172 | |
| 173 | /** |
| 174 | * @fifo_size: it is (@slot + 1) * @fifo_seg_size |
| 175 | * @fifo_seg_size: it is roundup_pow_of_two(@maxp) |
| 176 | */ |
| 177 | struct mtu3_ep { |
| 178 | struct usb_ep ep; |
| 179 | char name[12]; |
| 180 | struct mtu3 *mtu; |
| 181 | u8 epnum; |
| 182 | u8 type; |
| 183 | u8 is_in; |
| 184 | u16 maxp; |
| 185 | int slot; |
| 186 | u32 fifo_size; |
| 187 | u32 fifo_addr; |
| 188 | u32 fifo_seg_size; |
| 189 | struct mtu3_fifo_info *fifo; |
| 190 | |
| 191 | struct list_head req_list; |
| 192 | struct mtu3_gpd_ring gpd_ring; |
| 193 | const struct usb_endpoint_descriptor *desc; |
| 194 | |
| 195 | int flags; |
| 196 | u8 wedged; |
| 197 | u8 busy; |
| 198 | }; |
| 199 | |
| 200 | struct mtu3_request { |
| 201 | struct usb_request request; |
| 202 | struct list_head list; |
| 203 | struct mtu3_ep *mep; |
| 204 | struct mtu3 *mtu; |
| 205 | struct qmu_gpd *gpd; |
| 206 | int epnum; |
| 207 | }; |
| 208 | |
| 209 | /** |
| 210 | * struct mtu3 - device driver instance data. |
| 211 | * @slot: MTU3_U2_IP_SLOT_DEFAULT for U2 IP |
| 212 | * @may_wakeup: means device's remote wakeup is enabled |
| 213 | * @is_self_powered: is reported in device status and the config descriptor |
| 214 | * @ep0_req: dummy request used while handling standard USB requests |
| 215 | * for GET_STATUS and SET_SEL |
| 216 | * @setup_buf: ep0 response buffer for GET_STATUS and SET_SEL requests |
| 217 | */ |
| 218 | struct mtu3 { |
| 219 | spinlock_t lock; |
| 220 | struct device *dev; |
| 221 | void __iomem *mac_base; |
| 222 | void __iomem *ippc_base; |
| 223 | struct phy *phy; |
| 224 | struct regulator *vusb33; |
| 225 | struct clk *sys_clk; |
| 226 | int irq; |
| 227 | |
| 228 | struct mtu3_fifo_info tx_fifo; |
| 229 | struct mtu3_fifo_info rx_fifo; |
| 230 | |
| 231 | struct mtu3_ep *ep_array; |
| 232 | struct mtu3_ep *in_eps; |
| 233 | struct mtu3_ep *out_eps; |
| 234 | struct mtu3_ep *ep0; |
| 235 | int num_eps; |
| 236 | int slot; |
| 237 | int active_ep; |
| 238 | |
| 239 | struct dma_pool *qmu_gpd_pool; |
| 240 | enum mtu3_g_ep0_state ep0_state; |
| 241 | struct usb_gadget g; /* the gadget */ |
| 242 | struct usb_gadget_driver *gadget_driver; |
| 243 | struct mtu3_request ep0_req; |
| 244 | u8 setup_buf[EP0_RESPONSE_BUF]; |
| 245 | |
| 246 | unsigned is_active:1; |
| 247 | unsigned may_wakeup:1; |
| 248 | unsigned is_self_powered:1; |
| 249 | unsigned test_mode:1; |
| 250 | unsigned softconnect:1; |
| 251 | |
| 252 | u8 address; |
| 253 | u8 test_mode_nr; |
| 254 | u32 hw_version; |
| 255 | }; |
| 256 | |
| 257 | static inline struct mtu3 *gadget_to_mtu3(struct usb_gadget *g) |
| 258 | { |
| 259 | return container_of(g, struct mtu3, g); |
| 260 | } |
| 261 | |
| 262 | static inline int is_first_entry(const struct list_head *list, |
| 263 | const struct list_head *head) |
| 264 | { |
| 265 | return list_is_last(head, list); |
| 266 | } |
| 267 | |
| 268 | static inline struct mtu3_request *to_mtu3_request(struct usb_request *req) |
| 269 | { |
| 270 | return req ? container_of(req, struct mtu3_request, request) : NULL; |
| 271 | } |
| 272 | |
| 273 | static inline struct mtu3_ep *to_mtu3_ep(struct usb_ep *ep) |
| 274 | { |
| 275 | return ep ? container_of(ep, struct mtu3_ep, ep) : NULL; |
| 276 | } |
| 277 | |
| 278 | static inline struct mtu3_request *next_request(struct mtu3_ep *mep) |
| 279 | { |
| 280 | struct list_head *queue = &mep->req_list; |
| 281 | |
| 282 | if (list_empty(queue)) |
| 283 | return NULL; |
| 284 | |
| 285 | return list_first_entry(queue, struct mtu3_request, list); |
| 286 | } |
| 287 | |
| 288 | static inline void mtu3_writel(void __iomem *base, u32 offset, u32 data) |
| 289 | { |
| 290 | writel(data, base + offset); |
| 291 | } |
| 292 | |
| 293 | static inline u32 mtu3_readl(void __iomem *base, u32 offset) |
| 294 | { |
| 295 | return readl(base + offset); |
| 296 | } |
| 297 | |
| 298 | static inline void mtu3_setbits(void __iomem *base, u32 offset, u32 bits) |
| 299 | { |
| 300 | void __iomem *addr = base + offset; |
| 301 | u32 tmp = readl(addr); |
| 302 | |
| 303 | writel((tmp | (bits)), addr); |
| 304 | } |
| 305 | |
| 306 | static inline void mtu3_clrbits(void __iomem *base, u32 offset, u32 bits) |
| 307 | { |
| 308 | void __iomem *addr = base + offset; |
| 309 | u32 tmp = readl(addr); |
| 310 | |
| 311 | writel((tmp & ~(bits)), addr); |
| 312 | } |
| 313 | |
| 314 | int ssusb_check_clocks(struct mtu3 *mtu, u32 ex_clks); |
| 315 | struct usb_request *mtu3_alloc_request(struct usb_ep *ep, gfp_t gfp_flags); |
| 316 | void mtu3_free_request(struct usb_ep *ep, struct usb_request *req); |
| 317 | void mtu3_req_complete(struct mtu3_ep *mep, |
| 318 | struct usb_request *req, int status); |
| 319 | |
| 320 | int mtu3_config_ep(struct mtu3 *mtu, struct mtu3_ep *mep, |
| 321 | int interval, int burst, int mult); |
| 322 | void mtu3_deconfig_ep(struct mtu3 *mtu, struct mtu3_ep *mep); |
| 323 | void mtu3_ep_stall_set(struct mtu3_ep *mep, bool set); |
| 324 | void mtu3_ep0_setup(struct mtu3 *mtu); |
| 325 | void mtu3_start(struct mtu3 *mtu); |
| 326 | void mtu3_stop(struct mtu3 *mtu); |
| 327 | void mtu3_hs_softconn_set(struct mtu3 *mtu, bool enable); |
| 328 | |
| 329 | int mtu3_gadget_setup(struct mtu3 *mtu); |
| 330 | void mtu3_gadget_cleanup(struct mtu3 *mtu); |
| 331 | void mtu3_gadget_reset(struct mtu3 *mtu); |
| 332 | void mtu3_gadget_suspend(struct mtu3 *mtu); |
| 333 | void mtu3_gadget_resume(struct mtu3 *mtu); |
| 334 | void mtu3_gadget_disconnect(struct mtu3 *mtu); |
| 335 | int ssusb_gadget_init(struct mtu3 *mtu); |
| 336 | void ssusb_gadget_exit(struct mtu3 *mtu); |
| 337 | |
| 338 | irqreturn_t mtu3_ep0_isr(struct mtu3 *mtu); |
| 339 | extern const struct usb_ep_ops mtu3_ep0_ops; |
| 340 | |
| 341 | #endif |