blob: bd3529f29e86bb07ce56356086eb4507e90c708b [file] [log] [blame]
Alexander Shishkine443b332012-05-11 17:25:46 +03001/*
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>
Alexander Shishkin5f36e232012-05-11 17:25:47 +030017#include <linux/irqreturn.h>
Alexander Shishkineb70e5a2012-05-11 17:25:54 +030018#include <linux/usb.h>
Alexander Shishkine443b332012-05-11 17:25:46 +030019#include <linux/usb/gadget.h>
Li Jun57677be2014-04-23 15:56:44 +080020#include <linux/usb/otg-fsm.h>
Alexander Shishkine443b332012-05-11 17:25:46 +030021
22/******************************************************************************
23 * DEFINE
24 *****************************************************************************/
Michael Grzeschikb983e512013-03-30 12:54:10 +020025#define TD_PAGE_COUNT 5
Alexander Shishkin8e229782013-06-24 14:46:36 +030026#define CI_HDRC_PAGE_SIZE 4096ul /* page size for TD's */
Alexander Shishkine443b332012-05-11 17:25:46 +030027#define ENDPT_MAX 32
28
29/******************************************************************************
Marc Kleine-Budde21395a12014-01-06 10:10:38 +080030 * 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
58/******************************************************************************
Alexander Shishkine443b332012-05-11 17:25:46 +030059 * STRUCTURES
60 *****************************************************************************/
Alexander Shishkin551a8ac2012-05-11 17:25:49 +030061/**
Alexander Shishkin8e229782013-06-24 14:46:36 +030062 * struct ci_hw_ep - endpoint representation
Alexander Shishkin551a8ac2012-05-11 17:25:49 +030063 * @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
Richard Zhao26c696c2012-07-07 22:56:40 +080070 * @ci: pointer to the controller
Alexander Shishkin551a8ac2012-05-11 17:25:49 +030071 * @lock: pointer to controller's spinlock
Alexander Shishkin551a8ac2012-05-11 17:25:49 +030072 * @td_pool: pointer to controller's TD pool
73 */
Alexander Shishkin8e229782013-06-24 14:46:36 +030074struct ci_hw_ep {
Alexander Shishkin551a8ac2012-05-11 17:25:49 +030075 struct usb_ep ep;
76 u8 dir;
77 u8 num;
78 u8 type;
79 char name[16];
Alexander Shishkine443b332012-05-11 17:25:46 +030080 struct {
Alexander Shishkin551a8ac2012-05-11 17:25:49 +030081 struct list_head queue;
Alexander Shishkin8e229782013-06-24 14:46:36 +030082 struct ci_hw_qh *ptr;
Alexander Shishkin551a8ac2012-05-11 17:25:49 +030083 dma_addr_t dma;
84 } qh;
85 int wedge;
Alexander Shishkine443b332012-05-11 17:25:46 +030086
87 /* global resources */
Alexander Shishkin8e229782013-06-24 14:46:36 +030088 struct ci_hdrc *ci;
Alexander Shishkin551a8ac2012-05-11 17:25:49 +030089 spinlock_t *lock;
Alexander Shishkin551a8ac2012-05-11 17:25:49 +030090 struct dma_pool *td_pool;
Michael Grzeschik2e270412013-06-13 17:59:54 +030091 struct td_node *pending_td;
Alexander Shishkine443b332012-05-11 17:25:46 +030092};
93
Alexander Shishkin5f36e232012-05-11 17:25:47 +030094enum 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
102 * start: start this role
103 * stop: stop this role
104 * irq: irq handler for this role
105 * name: role name string (host/gadget)
106 */
107struct ci_role_driver {
Alexander Shishkin8e229782013-06-24 14:46:36 +0300108 int (*start)(struct ci_hdrc *);
109 void (*stop)(struct ci_hdrc *);
110 irqreturn_t (*irq)(struct ci_hdrc *);
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300111 const char *name;
112};
113
Alexander Shishkin551a8ac2012-05-11 17:25:49 +0300114/**
115 * struct hw_bank - hardware register mapping representation
116 * @lpm: set if the device is LPM capable
Alexander Shishkineb70e5a2012-05-11 17:25:54 +0300117 * @phys: physical address of the controller's registers
Alexander Shishkin551a8ac2012-05-11 17:25:49 +0300118 * @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 */
Alexander Shishkine443b332012-05-11 17:25:46 +0300124struct hw_bank {
Alexander Shishkin551a8ac2012-05-11 17:25:49 +0300125 unsigned lpm;
Alexander Shishkineb70e5a2012-05-11 17:25:54 +0300126 resource_size_t phys;
Alexander Shishkin551a8ac2012-05-11 17:25:49 +0300127 void __iomem *abs;
128 void __iomem *cap;
129 void __iomem *op;
130 size_t size;
Marc Kleine-Budde21395a12014-01-06 10:10:38 +0800131 void __iomem *regmap[OP_LAST + 1];
Alexander Shishkine443b332012-05-11 17:25:46 +0300132};
133
Alexander Shishkin551a8ac2012-05-11 17:25:49 +0300134/**
Alexander Shishkin8e229782013-06-24 14:46:36 +0300135 * struct ci_hdrc - chipidea device representation
Alexander Shishkin551a8ac2012-05-11 17:25:49 +0300136 * @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
Li Jun57677be2014-04-23 15:56:44 +0800143 * @fsm: otg finite state machine
Alexander Shishkin551a8ac2012-05-11 17:25:49 +0300144 * @work: work for role changing
145 * @wq: workqueue thread
146 * @qh_pool: allocation pool for queue heads
147 * @td_pool: allocation pool for transfer descriptors
148 * @gadget: device side representation for peripheral controller
149 * @driver: gadget driver
150 * @hw_ep_max: total number of endpoints supported by hardware
Alexander Shishkin8e229782013-06-24 14:46:36 +0300151 * @ci_hw_ep: array of endpoints
Alexander Shishkin551a8ac2012-05-11 17:25:49 +0300152 * @ep0_dir: ep0 direction
153 * @ep0out: pointer to ep0 OUT endpoint
154 * @ep0in: pointer to ep0 IN endpoint
155 * @status: ep0 status request
156 * @setaddr: if we should set the address on status completion
157 * @address: usb address received from the host
158 * @remote_wakeup: host-enabled remote wakeup
159 * @suspended: suspended by host
160 * @test_mode: the selected test mode
Richard Zhao77c44002012-06-29 17:48:53 +0800161 * @platdata: platform specific information supplied by parent device
Alexander Shishkin551a8ac2012-05-11 17:25:49 +0300162 * @vbus_active: is VBUS active
163 * @transceiver: pointer to USB PHY, if any
Alexander Shishkineb70e5a2012-05-11 17:25:54 +0300164 * @hcd: pointer to usb_hcd for ehci host driver
Alexander Shishkin2d651282013-03-30 12:53:51 +0200165 * @debugfs: root dentry for this controller in debugfs
Peter Chena107f8c2013-08-14 12:44:11 +0300166 * @id_event: indicates there is an id event, and handled at ci_otg_work
167 * @b_sess_valid_event: indicates there is a vbus event, and handled
168 * at ci_otg_work
Peter Chened8f8312014-01-10 13:51:27 +0800169 * @imx28_write_fix: Freescale imx28 needs swp instruction for writing
Alexander Shishkin551a8ac2012-05-11 17:25:49 +0300170 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300171struct ci_hdrc {
Alexander Shishkin551a8ac2012-05-11 17:25:49 +0300172 struct device *dev;
173 spinlock_t lock;
174 struct hw_bank hw_bank;
175 int irq;
176 struct ci_role_driver *roles[CI_ROLE_END];
177 enum ci_role role;
178 bool is_otg;
Li Jun57677be2014-04-23 15:56:44 +0800179 struct otg_fsm fsm;
Alexander Shishkin551a8ac2012-05-11 17:25:49 +0300180 struct work_struct work;
181 struct workqueue_struct *wq;
Alexander Shishkine443b332012-05-11 17:25:46 +0300182
Alexander Shishkin551a8ac2012-05-11 17:25:49 +0300183 struct dma_pool *qh_pool;
184 struct dma_pool *td_pool;
Alexander Shishkine443b332012-05-11 17:25:46 +0300185
Alexander Shishkin551a8ac2012-05-11 17:25:49 +0300186 struct usb_gadget gadget;
187 struct usb_gadget_driver *driver;
188 unsigned hw_ep_max;
Alexander Shishkin8e229782013-06-24 14:46:36 +0300189 struct ci_hw_ep ci_hw_ep[ENDPT_MAX];
Alexander Shishkin551a8ac2012-05-11 17:25:49 +0300190 u32 ep0_dir;
Alexander Shishkin8e229782013-06-24 14:46:36 +0300191 struct ci_hw_ep *ep0out, *ep0in;
Alexander Shishkine443b332012-05-11 17:25:46 +0300192
Alexander Shishkin551a8ac2012-05-11 17:25:49 +0300193 struct usb_request *status;
194 bool setaddr;
195 u8 address;
196 u8 remote_wakeup;
197 u8 suspended;
198 u8 test_mode;
Alexander Shishkine443b332012-05-11 17:25:46 +0300199
Alexander Shishkin8e229782013-06-24 14:46:36 +0300200 struct ci_hdrc_platform_data *platdata;
Alexander Shishkin551a8ac2012-05-11 17:25:49 +0300201 int vbus_active;
202 struct usb_phy *transceiver;
Alexander Shishkineb70e5a2012-05-11 17:25:54 +0300203 struct usb_hcd *hcd;
Alexander Shishkin2d651282013-03-30 12:53:51 +0200204 struct dentry *debugfs;
Peter Chena107f8c2013-08-14 12:44:11 +0300205 bool id_event;
206 bool b_sess_valid_event;
Peter Chened8f8312014-01-10 13:51:27 +0800207 bool imx28_write_fix;
Alexander Shishkine443b332012-05-11 17:25:46 +0300208};
209
Alexander Shishkin8e229782013-06-24 14:46:36 +0300210static inline struct ci_role_driver *ci_role(struct ci_hdrc *ci)
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300211{
212 BUG_ON(ci->role >= CI_ROLE_END || !ci->roles[ci->role]);
213 return ci->roles[ci->role];
214}
215
Alexander Shishkin8e229782013-06-24 14:46:36 +0300216static inline int ci_role_start(struct ci_hdrc *ci, enum ci_role role)
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300217{
218 int ret;
219
220 if (role >= CI_ROLE_END)
221 return -EINVAL;
222
223 if (!ci->roles[role])
224 return -ENXIO;
225
226 ret = ci->roles[role]->start(ci);
227 if (!ret)
228 ci->role = role;
229 return ret;
230}
231
Alexander Shishkin8e229782013-06-24 14:46:36 +0300232static inline void ci_role_stop(struct ci_hdrc *ci)
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300233{
234 enum ci_role role = ci->role;
235
236 if (role == CI_ROLE_END)
237 return;
238
239 ci->role = CI_ROLE_END;
240
241 ci->roles[role]->stop(ci);
242}
243
Alexander Shishkine443b332012-05-11 17:25:46 +0300244/**
Alexander Shishkine443b332012-05-11 17:25:46 +0300245 * hw_read: reads from a hw register
246 * @reg: register index
247 * @mask: bitfield mask
248 *
249 * This function returns register contents
250 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300251static inline u32 hw_read(struct ci_hdrc *ci, enum ci_hw_regs reg, u32 mask)
Alexander Shishkine443b332012-05-11 17:25:46 +0300252{
Richard Zhao26c696c2012-07-07 22:56:40 +0800253 return ioread32(ci->hw_bank.regmap[reg]) & mask;
Alexander Shishkine443b332012-05-11 17:25:46 +0300254}
255
Peter Chened8f8312014-01-10 13:51:27 +0800256#ifdef CONFIG_SOC_IMX28
257static inline void imx28_ci_writel(u32 val, volatile void __iomem *addr)
258{
259 __asm__ ("swp %0, %0, [%1]" : : "r"(val), "r"(addr));
260}
261#else
262static inline void imx28_ci_writel(u32 val, volatile void __iomem *addr)
263{
264}
265#endif
266
267static inline void __hw_write(struct ci_hdrc *ci, u32 val,
268 void __iomem *addr)
269{
270 if (ci->imx28_write_fix)
271 imx28_ci_writel(val, addr);
272 else
273 iowrite32(val, addr);
274}
275
Alexander Shishkine443b332012-05-11 17:25:46 +0300276/**
277 * hw_write: writes to a hw register
278 * @reg: register index
279 * @mask: bitfield mask
280 * @data: new value
281 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300282static inline void hw_write(struct ci_hdrc *ci, enum ci_hw_regs reg,
Alexander Shishkine443b332012-05-11 17:25:46 +0300283 u32 mask, u32 data)
284{
285 if (~mask)
Richard Zhao26c696c2012-07-07 22:56:40 +0800286 data = (ioread32(ci->hw_bank.regmap[reg]) & ~mask)
Alexander Shishkine443b332012-05-11 17:25:46 +0300287 | (data & mask);
288
Peter Chened8f8312014-01-10 13:51:27 +0800289 __hw_write(ci, data, ci->hw_bank.regmap[reg]);
Alexander Shishkine443b332012-05-11 17:25:46 +0300290}
291
292/**
293 * hw_test_and_clear: tests & clears a hw register
294 * @reg: register index
295 * @mask: bitfield mask
296 *
297 * This function returns register contents
298 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300299static inline u32 hw_test_and_clear(struct ci_hdrc *ci, enum ci_hw_regs reg,
Alexander Shishkine443b332012-05-11 17:25:46 +0300300 u32 mask)
301{
Richard Zhao26c696c2012-07-07 22:56:40 +0800302 u32 val = ioread32(ci->hw_bank.regmap[reg]) & mask;
Alexander Shishkine443b332012-05-11 17:25:46 +0300303
Peter Chened8f8312014-01-10 13:51:27 +0800304 __hw_write(ci, val, ci->hw_bank.regmap[reg]);
Alexander Shishkine443b332012-05-11 17:25:46 +0300305 return val;
306}
307
308/**
309 * hw_test_and_write: tests & writes a hw register
310 * @reg: register index
311 * @mask: bitfield mask
312 * @data: new value
313 *
314 * This function returns register contents
315 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300316static inline u32 hw_test_and_write(struct ci_hdrc *ci, enum ci_hw_regs reg,
Alexander Shishkine443b332012-05-11 17:25:46 +0300317 u32 mask, u32 data)
318{
Richard Zhao26c696c2012-07-07 22:56:40 +0800319 u32 val = hw_read(ci, reg, ~0);
Alexander Shishkine443b332012-05-11 17:25:46 +0300320
Richard Zhao26c696c2012-07-07 22:56:40 +0800321 hw_write(ci, reg, mask, data);
Felipe Balbi727b4dd2013-03-30 12:53:55 +0200322 return (val & mask) >> __ffs(mask);
Alexander Shishkine443b332012-05-11 17:25:46 +0300323}
324
Li Jun57677be2014-04-23 15:56:44 +0800325/**
326 * ci_otg_is_fsm_mode: runtime check if otg controller
327 * is in otg fsm mode.
328 */
329static inline bool ci_otg_is_fsm_mode(struct ci_hdrc *ci)
330{
331#ifdef CONFIG_USB_OTG_FSM
332 return ci->is_otg && ci->roles[CI_ROLE_HOST] &&
333 ci->roles[CI_ROLE_GADGET];
334#else
335 return false;
336#endif
337}
338
Li Jun36304b02014-04-23 15:56:39 +0800339u32 hw_read_intr_enable(struct ci_hdrc *ci);
340
341u32 hw_read_intr_status(struct ci_hdrc *ci);
342
Alexander Shishkin8e229782013-06-24 14:46:36 +0300343int hw_device_reset(struct ci_hdrc *ci, u32 mode);
Alexander Shishkine443b332012-05-11 17:25:46 +0300344
Alexander Shishkin8e229782013-06-24 14:46:36 +0300345int hw_port_test_set(struct ci_hdrc *ci, u8 mode);
Alexander Shishkine443b332012-05-11 17:25:46 +0300346
Alexander Shishkin8e229782013-06-24 14:46:36 +0300347u8 hw_port_test_get(struct ci_hdrc *ci);
Alexander Shishkine443b332012-05-11 17:25:46 +0300348
Peter Chen22fa8442013-08-14 12:44:12 +0300349int hw_wait_reg(struct ci_hdrc *ci, enum ci_hw_regs reg, u32 mask,
350 u32 value, unsigned int timeout_ms);
351
Alexander Shishkine443b332012-05-11 17:25:46 +0300352#endif /* __DRIVERS_USB_CHIPIDEA_CI_H */