usb: chipidea: usbmisc_imx: add imx6sx initialization routine
[linux-2.6-block.git] / drivers / usb / chipidea / ci.h
CommitLineData
e443b333
AS
1/*
2 * ci.h - common structures, functions, and macros of the ChipIdea driver
3 *
4 * Copyright (C) 2008 Chipidea - MIPS Technologies, Inc. All rights reserved.
5 *
6 * Author: David Lopo
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13#ifndef __DRIVERS_USB_CHIPIDEA_CI_H
14#define __DRIVERS_USB_CHIPIDEA_CI_H
15
16#include <linux/list.h>
5f36e231 17#include <linux/irqreturn.h>
eb70e5ab 18#include <linux/usb.h>
e443b333 19#include <linux/usb/gadget.h>
57677be5 20#include <linux/usb/otg-fsm.h>
e443b333
AS
21
22/******************************************************************************
23 * DEFINE
24 *****************************************************************************/
b983e51a 25#define TD_PAGE_COUNT 5
8e22978c 26#define CI_HDRC_PAGE_SIZE 4096ul /* page size for TD's */
e443b333
AS
27#define ENDPT_MAX 32
28
21395a1a
MKB
29/******************************************************************************
30 * REGISTERS
31 *****************************************************************************/
32/* register indices */
33enum ci_hw_regs {
34 CAP_CAPLENGTH,
35 CAP_HCCPARAMS,
36 CAP_DCCPARAMS,
37 CAP_TESTMODE,
38 CAP_LAST = CAP_TESTMODE,
39 OP_USBCMD,
40 OP_USBSTS,
41 OP_USBINTR,
42 OP_DEVICEADDR,
43 OP_ENDPTLISTADDR,
44 OP_PORTSC,
45 OP_DEVLC,
46 OP_OTGSC,
47 OP_USBMODE,
48 OP_ENDPTSETUPSTAT,
49 OP_ENDPTPRIME,
50 OP_ENDPTFLUSH,
51 OP_ENDPTSTAT,
52 OP_ENDPTCOMPLETE,
53 OP_ENDPTCTRL,
54 /* endptctrl1..15 follow */
55 OP_LAST = OP_ENDPTCTRL + ENDPT_MAX / 2,
56};
57
e443b333
AS
58/******************************************************************************
59 * STRUCTURES
60 *****************************************************************************/
551a8ac6 61/**
8e22978c 62 * struct ci_hw_ep - endpoint representation
551a8ac6
AS
63 * @ep: endpoint structure for gadget drivers
64 * @dir: endpoint direction (TX/RX)
65 * @num: endpoint number
66 * @type: endpoint type
67 * @name: string description of the endpoint
68 * @qh: queue head for this endpoint
69 * @wedge: is the endpoint wedged
26c696c6 70 * @ci: pointer to the controller
551a8ac6 71 * @lock: pointer to controller's spinlock
551a8ac6
AS
72 * @td_pool: pointer to controller's TD pool
73 */
8e22978c 74struct ci_hw_ep {
551a8ac6
AS
75 struct usb_ep ep;
76 u8 dir;
77 u8 num;
78 u8 type;
79 char name[16];
e443b333 80 struct {
551a8ac6 81 struct list_head queue;
8e22978c 82 struct ci_hw_qh *ptr;
551a8ac6
AS
83 dma_addr_t dma;
84 } qh;
85 int wedge;
e443b333
AS
86
87 /* global resources */
8e22978c 88 struct ci_hdrc *ci;
551a8ac6 89 spinlock_t *lock;
551a8ac6 90 struct dma_pool *td_pool;
2e270412 91 struct td_node *pending_td;
e443b333
AS
92};
93
5f36e231
AS
94enum ci_role {
95 CI_ROLE_HOST = 0,
96 CI_ROLE_GADGET,
97 CI_ROLE_END,
98};
99
100/**
101 * struct ci_role_driver - host/gadget role driver
19353881
PC
102 * @start: start this role
103 * @stop: stop this role
104 * @irq: irq handler for this role
105 * @name: role name string (host/gadget)
5f36e231
AS
106 */
107struct ci_role_driver {
8e22978c
AS
108 int (*start)(struct ci_hdrc *);
109 void (*stop)(struct ci_hdrc *);
110 irqreturn_t (*irq)(struct ci_hdrc *);
5f36e231
AS
111 const char *name;
112};
113
551a8ac6
AS
114/**
115 * struct hw_bank - hardware register mapping representation
116 * @lpm: set if the device is LPM capable
eb70e5ab 117 * @phys: physical address of the controller's registers
551a8ac6
AS
118 * @abs: absolute address of the beginning of register window
119 * @cap: capability registers
120 * @op: operational registers
121 * @size: size of the register window
122 * @regmap: register lookup table
123 */
e443b333 124struct hw_bank {
551a8ac6 125 unsigned lpm;
eb70e5ab 126 resource_size_t phys;
551a8ac6
AS
127 void __iomem *abs;
128 void __iomem *cap;
129 void __iomem *op;
130 size_t size;
21395a1a 131 void __iomem *regmap[OP_LAST + 1];
e443b333
AS
132};
133
551a8ac6 134/**
8e22978c 135 * struct ci_hdrc - chipidea device representation
551a8ac6
AS
136 * @dev: pointer to parent device
137 * @lock: access synchronization
138 * @hw_bank: hardware register mapping
139 * @irq: IRQ number
140 * @roles: array of supported roles for this controller
141 * @role: current role
142 * @is_otg: if the device is otg-capable
57677be5 143 * @fsm: otg finite state machine
826cfe75 144 * @fsm_timer: pointer to timer list of otg fsm
551a8ac6
AS
145 * @work: work for role changing
146 * @wq: workqueue thread
147 * @qh_pool: allocation pool for queue heads
148 * @td_pool: allocation pool for transfer descriptors
149 * @gadget: device side representation for peripheral controller
150 * @driver: gadget driver
151 * @hw_ep_max: total number of endpoints supported by hardware
8e22978c 152 * @ci_hw_ep: array of endpoints
551a8ac6
AS
153 * @ep0_dir: ep0 direction
154 * @ep0out: pointer to ep0 OUT endpoint
155 * @ep0in: pointer to ep0 IN endpoint
156 * @status: ep0 status request
157 * @setaddr: if we should set the address on status completion
158 * @address: usb address received from the host
159 * @remote_wakeup: host-enabled remote wakeup
160 * @suspended: suspended by host
161 * @test_mode: the selected test mode
77c4400f 162 * @platdata: platform specific information supplied by parent device
551a8ac6 163 * @vbus_active: is VBUS active
1e5e2d3d
AT
164 * @phy: pointer to PHY, if any
165 * @usb_phy: pointer to USB PHY, if any and if using the USB PHY framework
eb70e5ab 166 * @hcd: pointer to usb_hcd for ehci host driver
2d651289 167 * @debugfs: root dentry for this controller in debugfs
a107f8c5
PC
168 * @id_event: indicates there is an id event, and handled at ci_otg_work
169 * @b_sess_valid_event: indicates there is a vbus event, and handled
170 * at ci_otg_work
ed8f8318 171 * @imx28_write_fix: Freescale imx28 needs swp instruction for writing
1f874edc
PC
172 * @supports_runtime_pm: if runtime pm is supported
173 * @in_lpm: if the core in low power mode
174 * @wakeup_int: if wakeup interrupt occur
551a8ac6 175 */
8e22978c 176struct ci_hdrc {
551a8ac6
AS
177 struct device *dev;
178 spinlock_t lock;
179 struct hw_bank hw_bank;
180 int irq;
181 struct ci_role_driver *roles[CI_ROLE_END];
182 enum ci_role role;
183 bool is_otg;
ef44cb42 184 struct usb_otg otg;
57677be5 185 struct otg_fsm fsm;
826cfe75 186 struct ci_otg_fsm_timer_list *fsm_timer;
551a8ac6
AS
187 struct work_struct work;
188 struct workqueue_struct *wq;
189
190 struct dma_pool *qh_pool;
191 struct dma_pool *td_pool;
192
193 struct usb_gadget gadget;
194 struct usb_gadget_driver *driver;
195 unsigned hw_ep_max;
8e22978c 196 struct ci_hw_ep ci_hw_ep[ENDPT_MAX];
551a8ac6 197 u32 ep0_dir;
8e22978c 198 struct ci_hw_ep *ep0out, *ep0in;
551a8ac6
AS
199
200 struct usb_request *status;
201 bool setaddr;
202 u8 address;
203 u8 remote_wakeup;
204 u8 suspended;
205 u8 test_mode;
206
8e22978c 207 struct ci_hdrc_platform_data *platdata;
551a8ac6 208 int vbus_active;
1e5e2d3d
AT
209 struct phy *phy;
210 /* old usb_phy interface */
ef44cb42 211 struct usb_phy *usb_phy;
eb70e5ab 212 struct usb_hcd *hcd;
2d651289 213 struct dentry *debugfs;
a107f8c5
PC
214 bool id_event;
215 bool b_sess_valid_event;
ed8f8318 216 bool imx28_write_fix;
1f874edc
PC
217 bool supports_runtime_pm;
218 bool in_lpm;
219 bool wakeup_int;
e443b333
AS
220};
221
8e22978c 222static inline struct ci_role_driver *ci_role(struct ci_hdrc *ci)
5f36e231
AS
223{
224 BUG_ON(ci->role >= CI_ROLE_END || !ci->roles[ci->role]);
225 return ci->roles[ci->role];
226}
227
8e22978c 228static inline int ci_role_start(struct ci_hdrc *ci, enum ci_role role)
5f36e231
AS
229{
230 int ret;
231
232 if (role >= CI_ROLE_END)
233 return -EINVAL;
234
235 if (!ci->roles[role])
236 return -ENXIO;
237
238 ret = ci->roles[role]->start(ci);
239 if (!ret)
240 ci->role = role;
241 return ret;
242}
243
8e22978c 244static inline void ci_role_stop(struct ci_hdrc *ci)
5f36e231
AS
245{
246 enum ci_role role = ci->role;
247
248 if (role == CI_ROLE_END)
249 return;
250
251 ci->role = CI_ROLE_END;
252
253 ci->roles[role]->stop(ci);
254}
255
e443b333
AS
256/**
257 * hw_read: reads from a hw register
19353881 258 * @ci: the controller
e443b333
AS
259 * @reg: register index
260 * @mask: bitfield mask
261 *
262 * This function returns register contents
263 */
8e22978c 264static inline u32 hw_read(struct ci_hdrc *ci, enum ci_hw_regs reg, u32 mask)
e443b333 265{
26c696c6 266 return ioread32(ci->hw_bank.regmap[reg]) & mask;
e443b333
AS
267}
268
ed8f8318
PC
269#ifdef CONFIG_SOC_IMX28
270static inline void imx28_ci_writel(u32 val, volatile void __iomem *addr)
271{
272 __asm__ ("swp %0, %0, [%1]" : : "r"(val), "r"(addr));
273}
274#else
275static inline void imx28_ci_writel(u32 val, volatile void __iomem *addr)
276{
277}
278#endif
279
280static inline void __hw_write(struct ci_hdrc *ci, u32 val,
281 void __iomem *addr)
282{
283 if (ci->imx28_write_fix)
284 imx28_ci_writel(val, addr);
285 else
286 iowrite32(val, addr);
287}
288
e443b333
AS
289/**
290 * hw_write: writes to a hw register
19353881 291 * @ci: the controller
e443b333
AS
292 * @reg: register index
293 * @mask: bitfield mask
294 * @data: new value
295 */
8e22978c 296static inline void hw_write(struct ci_hdrc *ci, enum ci_hw_regs reg,
e443b333
AS
297 u32 mask, u32 data)
298{
299 if (~mask)
26c696c6 300 data = (ioread32(ci->hw_bank.regmap[reg]) & ~mask)
e443b333
AS
301 | (data & mask);
302
ed8f8318 303 __hw_write(ci, data, ci->hw_bank.regmap[reg]);
e443b333
AS
304}
305
306/**
307 * hw_test_and_clear: tests & clears a hw register
19353881 308 * @ci: the controller
e443b333
AS
309 * @reg: register index
310 * @mask: bitfield mask
311 *
312 * This function returns register contents
313 */
8e22978c 314static inline u32 hw_test_and_clear(struct ci_hdrc *ci, enum ci_hw_regs reg,
e443b333
AS
315 u32 mask)
316{
26c696c6 317 u32 val = ioread32(ci->hw_bank.regmap[reg]) & mask;
e443b333 318
ed8f8318 319 __hw_write(ci, val, ci->hw_bank.regmap[reg]);
e443b333
AS
320 return val;
321}
322
323/**
324 * hw_test_and_write: tests & writes a hw register
19353881 325 * @ci: the controller
e443b333
AS
326 * @reg: register index
327 * @mask: bitfield mask
328 * @data: new value
329 *
330 * This function returns register contents
331 */
8e22978c 332static inline u32 hw_test_and_write(struct ci_hdrc *ci, enum ci_hw_regs reg,
e443b333
AS
333 u32 mask, u32 data)
334{
26c696c6 335 u32 val = hw_read(ci, reg, ~0);
e443b333 336
26c696c6 337 hw_write(ci, reg, mask, data);
727b4ddb 338 return (val & mask) >> __ffs(mask);
e443b333
AS
339}
340
57677be5
LJ
341/**
342 * ci_otg_is_fsm_mode: runtime check if otg controller
343 * is in otg fsm mode.
19353881
PC
344 *
345 * @ci: chipidea device
57677be5
LJ
346 */
347static inline bool ci_otg_is_fsm_mode(struct ci_hdrc *ci)
348{
349#ifdef CONFIG_USB_OTG_FSM
350 return ci->is_otg && ci->roles[CI_ROLE_HOST] &&
351 ci->roles[CI_ROLE_GADGET];
352#else
353 return false;
354#endif
355}
356
36304b06
LJ
357u32 hw_read_intr_enable(struct ci_hdrc *ci);
358
359u32 hw_read_intr_status(struct ci_hdrc *ci);
360
5b157300 361int hw_device_reset(struct ci_hdrc *ci);
e443b333 362
8e22978c 363int hw_port_test_set(struct ci_hdrc *ci, u8 mode);
e443b333 364
8e22978c 365u8 hw_port_test_get(struct ci_hdrc *ci);
e443b333 366
22fa8445
PC
367int hw_wait_reg(struct ci_hdrc *ci, enum ci_hw_regs reg, u32 mask,
368 u32 value, unsigned int timeout_ms);
369
e443b333 370#endif /* __DRIVERS_USB_CHIPIDEA_CI_H */