blob: 0ab83411d800d4d050641800fd519e0254d08c40 [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 Shishkine443b332012-05-11 17:25:46 +030018#include <linux/usb/gadget.h>
19
20/******************************************************************************
21 * DEFINE
22 *****************************************************************************/
23#define DMA_ADDR_INVALID (~(dma_addr_t)0)
24#define CI13XXX_PAGE_SIZE 4096ul /* page size for TD's */
25#define ENDPT_MAX 32
26
27/******************************************************************************
28 * STRUCTURES
29 *****************************************************************************/
Alexander Shishkin551a8ac2012-05-11 17:25:49 +030030/**
31 * struct ci13xxx_ep - endpoint representation
32 * @ep: endpoint structure for gadget drivers
33 * @dir: endpoint direction (TX/RX)
34 * @num: endpoint number
35 * @type: endpoint type
36 * @name: string description of the endpoint
37 * @qh: queue head for this endpoint
38 * @wedge: is the endpoint wedged
39 * @udc: pointer to the controller
40 * @lock: pointer to controller's spinlock
41 * @device: pointer to gadget's struct device
42 * @td_pool: pointer to controller's TD pool
43 */
Alexander Shishkine443b332012-05-11 17:25:46 +030044struct ci13xxx_ep {
Alexander Shishkin551a8ac2012-05-11 17:25:49 +030045 struct usb_ep ep;
46 u8 dir;
47 u8 num;
48 u8 type;
49 char name[16];
Alexander Shishkine443b332012-05-11 17:25:46 +030050 struct {
Alexander Shishkin551a8ac2012-05-11 17:25:49 +030051 struct list_head queue;
52 struct ci13xxx_qh *ptr;
53 dma_addr_t dma;
54 } qh;
55 int wedge;
Alexander Shishkine443b332012-05-11 17:25:46 +030056
57 /* global resources */
Alexander Shishkin551a8ac2012-05-11 17:25:49 +030058 struct ci13xxx *udc;
59 spinlock_t *lock;
60 struct device *device;
61 struct dma_pool *td_pool;
Alexander Shishkine443b332012-05-11 17:25:46 +030062};
63
Alexander Shishkin5f36e232012-05-11 17:25:47 +030064enum ci_role {
65 CI_ROLE_HOST = 0,
66 CI_ROLE_GADGET,
67 CI_ROLE_END,
68};
69
70/**
71 * struct ci_role_driver - host/gadget role driver
72 * start: start this role
73 * stop: stop this role
74 * irq: irq handler for this role
75 * name: role name string (host/gadget)
76 */
77struct ci_role_driver {
78 int (*start)(struct ci13xxx *);
79 void (*stop)(struct ci13xxx *);
80 irqreturn_t (*irq)(struct ci13xxx *);
81 const char *name;
82};
83
Alexander Shishkin551a8ac2012-05-11 17:25:49 +030084/**
85 * struct hw_bank - hardware register mapping representation
86 * @lpm: set if the device is LPM capable
87 * @abs: absolute address of the beginning of register window
88 * @cap: capability registers
89 * @op: operational registers
90 * @size: size of the register window
91 * @regmap: register lookup table
92 */
Alexander Shishkine443b332012-05-11 17:25:46 +030093struct hw_bank {
Alexander Shishkin551a8ac2012-05-11 17:25:49 +030094 unsigned lpm;
95 void __iomem *abs;
96 void __iomem *cap;
97 void __iomem *op;
98 size_t size;
99 void __iomem **regmap;
Alexander Shishkine443b332012-05-11 17:25:46 +0300100};
101
Alexander Shishkin551a8ac2012-05-11 17:25:49 +0300102/**
103 * struct ci13xxx - chipidea device representation
104 * @dev: pointer to parent device
105 * @lock: access synchronization
106 * @hw_bank: hardware register mapping
107 * @irq: IRQ number
108 * @roles: array of supported roles for this controller
109 * @role: current role
110 * @is_otg: if the device is otg-capable
111 * @work: work for role changing
112 * @wq: workqueue thread
113 * @qh_pool: allocation pool for queue heads
114 * @td_pool: allocation pool for transfer descriptors
115 * @gadget: device side representation for peripheral controller
116 * @driver: gadget driver
117 * @hw_ep_max: total number of endpoints supported by hardware
118 * @ci13xxx_ep: array of endpoints
119 * @ep0_dir: ep0 direction
120 * @ep0out: pointer to ep0 OUT endpoint
121 * @ep0in: pointer to ep0 IN endpoint
122 * @status: ep0 status request
123 * @setaddr: if we should set the address on status completion
124 * @address: usb address received from the host
125 * @remote_wakeup: host-enabled remote wakeup
126 * @suspended: suspended by host
127 * @test_mode: the selected test mode
128 * @udc_driver: platform specific information supplied by parent device
129 * @vbus_active: is VBUS active
130 * @transceiver: pointer to USB PHY, if any
131 */
Alexander Shishkine443b332012-05-11 17:25:46 +0300132struct ci13xxx {
Alexander Shishkin551a8ac2012-05-11 17:25:49 +0300133 struct device *dev;
134 spinlock_t lock;
135 struct hw_bank hw_bank;
136 int irq;
137 struct ci_role_driver *roles[CI_ROLE_END];
138 enum ci_role role;
139 bool is_otg;
140 struct work_struct work;
141 struct workqueue_struct *wq;
Alexander Shishkine443b332012-05-11 17:25:46 +0300142
Alexander Shishkin551a8ac2012-05-11 17:25:49 +0300143 struct dma_pool *qh_pool;
144 struct dma_pool *td_pool;
Alexander Shishkine443b332012-05-11 17:25:46 +0300145
Alexander Shishkin551a8ac2012-05-11 17:25:49 +0300146 struct usb_gadget gadget;
147 struct usb_gadget_driver *driver;
148 unsigned hw_ep_max;
149 struct ci13xxx_ep ci13xxx_ep[ENDPT_MAX];
150 u32 ep0_dir;
151 struct ci13xxx_ep *ep0out, *ep0in;
Alexander Shishkine443b332012-05-11 17:25:46 +0300152
Alexander Shishkin551a8ac2012-05-11 17:25:49 +0300153 struct usb_request *status;
154 bool setaddr;
155 u8 address;
156 u8 remote_wakeup;
157 u8 suspended;
158 u8 test_mode;
Alexander Shishkine443b332012-05-11 17:25:46 +0300159
Alexander Shishkin551a8ac2012-05-11 17:25:49 +0300160 struct ci13xxx_udc_driver *udc_driver;
161 int vbus_active;
162 struct usb_phy *transceiver;
Alexander Shishkine443b332012-05-11 17:25:46 +0300163};
164
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300165static inline struct ci_role_driver *ci_role(struct ci13xxx *ci)
166{
167 BUG_ON(ci->role >= CI_ROLE_END || !ci->roles[ci->role]);
168 return ci->roles[ci->role];
169}
170
171static inline int ci_role_start(struct ci13xxx *ci, enum ci_role role)
172{
173 int ret;
174
175 if (role >= CI_ROLE_END)
176 return -EINVAL;
177
178 if (!ci->roles[role])
179 return -ENXIO;
180
181 ret = ci->roles[role]->start(ci);
182 if (!ret)
183 ci->role = role;
184 return ret;
185}
186
187static inline void ci_role_stop(struct ci13xxx *ci)
188{
189 enum ci_role role = ci->role;
190
191 if (role == CI_ROLE_END)
192 return;
193
194 ci->role = CI_ROLE_END;
195
196 ci->roles[role]->stop(ci);
197}
198
Alexander Shishkine443b332012-05-11 17:25:46 +0300199/******************************************************************************
200 * REGISTERS
201 *****************************************************************************/
202/* register size */
203#define REG_BITS (32)
204
205/* register indices */
206enum ci13xxx_regs {
207 CAP_CAPLENGTH,
208 CAP_HCCPARAMS,
209 CAP_DCCPARAMS,
210 CAP_TESTMODE,
211 CAP_LAST = CAP_TESTMODE,
212 OP_USBCMD,
213 OP_USBSTS,
214 OP_USBINTR,
215 OP_DEVICEADDR,
216 OP_ENDPTLISTADDR,
217 OP_PORTSC,
218 OP_DEVLC,
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300219 OP_OTGSC,
Alexander Shishkine443b332012-05-11 17:25:46 +0300220 OP_USBMODE,
221 OP_ENDPTSETUPSTAT,
222 OP_ENDPTPRIME,
223 OP_ENDPTFLUSH,
224 OP_ENDPTSTAT,
225 OP_ENDPTCOMPLETE,
226 OP_ENDPTCTRL,
227 /* endptctrl1..15 follow */
228 OP_LAST = OP_ENDPTCTRL + ENDPT_MAX / 2,
229};
230
Alexander Shishkine443b332012-05-11 17:25:46 +0300231/**
232 * ffs_nr: find first (least significant) bit set
233 * @x: the word to search
234 *
235 * This function returns bit number (instead of position)
236 */
237static inline int ffs_nr(u32 x)
238{
239 int n = ffs(x);
240
241 return n ? n-1 : 32;
242}
243
244/**
245 * hw_read: reads from a hw register
246 * @reg: register index
247 * @mask: bitfield mask
248 *
249 * This function returns register contents
250 */
251static inline u32 hw_read(struct ci13xxx *udc, enum ci13xxx_regs reg, u32 mask)
252{
253 return ioread32(udc->hw_bank.regmap[reg]) & mask;
254}
255
256/**
257 * hw_write: writes to a hw register
258 * @reg: register index
259 * @mask: bitfield mask
260 * @data: new value
261 */
262static inline void hw_write(struct ci13xxx *udc, enum ci13xxx_regs reg,
263 u32 mask, u32 data)
264{
265 if (~mask)
266 data = (ioread32(udc->hw_bank.regmap[reg]) & ~mask)
267 | (data & mask);
268
269 iowrite32(data, udc->hw_bank.regmap[reg]);
270}
271
272/**
273 * hw_test_and_clear: tests & clears a hw register
274 * @reg: register index
275 * @mask: bitfield mask
276 *
277 * This function returns register contents
278 */
279static inline u32 hw_test_and_clear(struct ci13xxx *udc, enum ci13xxx_regs reg,
280 u32 mask)
281{
282 u32 val = ioread32(udc->hw_bank.regmap[reg]) & mask;
283
284 iowrite32(val, udc->hw_bank.regmap[reg]);
285 return val;
286}
287
288/**
289 * hw_test_and_write: tests & writes a hw register
290 * @reg: register index
291 * @mask: bitfield mask
292 * @data: new value
293 *
294 * This function returns register contents
295 */
296static inline u32 hw_test_and_write(struct ci13xxx *udc, enum ci13xxx_regs reg,
297 u32 mask, u32 data)
298{
299 u32 val = hw_read(udc, reg, ~0);
300
301 hw_write(udc, reg, mask, data);
302 return (val & mask) >> ffs_nr(mask);
303}
304
Alexander Shishkine443b332012-05-11 17:25:46 +0300305int hw_device_reset(struct ci13xxx *ci);
306
307int hw_port_test_set(struct ci13xxx *ci, u8 mode);
308
309u8 hw_port_test_get(struct ci13xxx *ci);
310
311#endif /* __DRIVERS_USB_CHIPIDEA_CI_H */