blob: 73604a1d68434dfdd51408fecef67300721fa7ac [file] [log] [blame]
Kuninori Morimotof1407d52011-04-04 13:44:59 +09001/*
2 * Renesas USB driver
3 *
4 * Copyright (C) 2011 Renesas Solutions Corp.
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
15 *
16 */
17#include <linux/interrupt.h>
18
19#include "./common.h"
20#include "./mod.h"
21
22#define usbhs_priv_to_modinfo(priv) (&priv->mod_info)
23
24/*
25 * host / gadget functions
26 *
27 * renesas_usbhs host/gadget can register itself by below functions.
28 * these functions are called when probe
29 *
30 */
31void usbhs_mod_register(struct usbhs_priv *priv, struct usbhs_mod *mod, int id)
32{
33 struct usbhs_mod_info *info = usbhs_priv_to_modinfo(priv);
34
35 info->mod[id] = mod;
36 mod->priv = priv;
37}
38
39struct usbhs_mod *usbhs_mod_get(struct usbhs_priv *priv, int id)
40{
41 struct usbhs_mod_info *info = usbhs_priv_to_modinfo(priv);
42 struct usbhs_mod *ret = NULL;
43
44 switch (id) {
45 case USBHS_HOST:
46 case USBHS_GADGET:
47 ret = info->mod[id];
48 break;
49 }
50
51 return ret;
52}
53
54int usbhs_mod_is_host(struct usbhs_priv *priv, struct usbhs_mod *mod)
55{
56 struct usbhs_mod_info *info = usbhs_priv_to_modinfo(priv);
57
58 if (!mod)
59 return -EINVAL;
60
61 return info->mod[USBHS_HOST] == mod;
62}
63
64struct usbhs_mod *usbhs_mod_get_current(struct usbhs_priv *priv)
65{
66 struct usbhs_mod_info *info = usbhs_priv_to_modinfo(priv);
67
68 return info->curt;
69}
70
71int usbhs_mod_change(struct usbhs_priv *priv, int id)
72{
73 struct usbhs_mod_info *info = usbhs_priv_to_modinfo(priv);
74 struct usbhs_mod *mod = NULL;
75 int ret = 0;
76
77 /* id < 0 mean no current */
78 switch (id) {
79 case USBHS_HOST:
80 case USBHS_GADGET:
81 mod = info->mod[id];
82 break;
83 default:
84 ret = -EINVAL;
85 }
86 info->curt = mod;
87
88 return ret;
89}
90
91static irqreturn_t usbhs_interrupt(int irq, void *data);
92int usbhs_mod_probe(struct usbhs_priv *priv)
93{
94 struct device *dev = usbhs_priv_to_dev(priv);
95 int ret;
96
Kuninori Morimoto2f983822011-04-05 11:40:54 +090097 /*
98 * install host/gadget driver
99 */
100 ret = usbhs_mod_gadget_probe(priv);
101 if (ret < 0)
102 return ret;
103
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900104 /* irq settings */
105 ret = request_irq(priv->irq, usbhs_interrupt,
106 IRQF_DISABLED, dev_name(dev), priv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900107 if (ret) {
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900108 dev_err(dev, "irq request err\n");
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900109 goto mod_init_gadget_err;
110 }
111
112 return ret;
113
114mod_init_gadget_err:
115 usbhs_mod_gadget_remove(priv);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900116
117 return ret;
118}
119
120void usbhs_mod_remove(struct usbhs_priv *priv)
121{
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900122 usbhs_mod_gadget_remove(priv);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900123 free_irq(priv->irq, priv);
124}
125
126/*
127 * status functions
128 */
129int usbhs_status_get_usb_speed(struct usbhs_irq_state *irq_state)
130{
131 switch (irq_state->dvstctr & RHST) {
132 case RHST_LOW_SPEED:
133 return USB_SPEED_LOW;
134 case RHST_FULL_SPEED:
135 return USB_SPEED_FULL;
136 case RHST_HIGH_SPEED:
137 return USB_SPEED_HIGH;
138 }
139
140 return USB_SPEED_UNKNOWN;
141}
142
143int usbhs_status_get_device_state(struct usbhs_irq_state *irq_state)
144{
145 int state = irq_state->intsts0 & DVSQ_MASK;
146
147 switch (state) {
148 case POWER_STATE:
149 case DEFAULT_STATE:
150 case ADDRESS_STATE:
151 case CONFIGURATION_STATE:
152 return state;
153 }
154
155 return -EIO;
156}
157
158int usbhs_status_get_ctrl_stage(struct usbhs_irq_state *irq_state)
159{
160 /*
161 * return value
162 *
163 * IDLE_SETUP_STAGE
164 * READ_DATA_STAGE
165 * READ_STATUS_STAGE
166 * WRITE_DATA_STAGE
167 * WRITE_STATUS_STAGE
168 * NODATA_STATUS_STAGE
169 * SEQUENCE_ERROR
170 */
171 return (int)irq_state->intsts0 & CTSQ_MASK;
172}
173
174static void usbhs_status_get_each_irq(struct usbhs_priv *priv,
175 struct usbhs_irq_state *state)
176{
177 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
178
179 state->intsts0 = usbhs_read(priv, INTSTS0);
180 state->intsts1 = usbhs_read(priv, INTSTS1);
181
182 state->brdysts = usbhs_read(priv, BRDYSTS);
183 state->nrdysts = usbhs_read(priv, NRDYSTS);
184 state->bempsts = usbhs_read(priv, BEMPSTS);
185
186 state->dvstctr = usbhs_read(priv, DVSTCTR);
187
188 /* mask */
189 state->bempsts &= mod->irq_bempsts;
190 state->brdysts &= mod->irq_brdysts;
191}
192
193/*
194 * interrupt
195 */
196#define INTSTS0_MAGIC 0xF800 /* acknowledge magical interrupt sources */
197#define INTSTS1_MAGIC 0xA870 /* acknowledge magical interrupt sources */
198static irqreturn_t usbhs_interrupt(int irq, void *data)
199{
200 struct usbhs_priv *priv = data;
201 struct usbhs_irq_state irq_state;
202
203 usbhs_status_get_each_irq(priv, &irq_state);
204
205 /*
206 * clear interrupt
207 *
208 * The hardware is _very_ picky to clear interrupt bit.
209 * Especially INTSTS0_MAGIC, INTSTS1_MAGIC value.
210 *
211 * see
212 * "Operation"
213 * - "Control Transfer (DCP)"
214 * - Function :: VALID bit should 0
215 */
216 usbhs_write(priv, INTSTS0, ~irq_state.intsts0 & INTSTS0_MAGIC);
217 usbhs_write(priv, INTSTS1, ~irq_state.intsts1 & INTSTS1_MAGIC);
218
219 usbhs_write(priv, BRDYSTS, 0);
220 usbhs_write(priv, NRDYSTS, 0);
221 usbhs_write(priv, BEMPSTS, 0);
222
223 /*
224 * call irq callback functions
225 * see also
226 * usbhs_irq_setting_update
227 */
228 if (irq_state.intsts0 & DVST)
229 usbhs_mod_call(priv, irq_dev_state, priv, &irq_state);
230
231 if (irq_state.intsts0 & CTRT)
232 usbhs_mod_call(priv, irq_ctrl_stage, priv, &irq_state);
233
234 if (irq_state.intsts0 & BEMP)
235 usbhs_mod_call(priv, irq_empty, priv, &irq_state);
236
237 if (irq_state.intsts0 & BRDY)
238 usbhs_mod_call(priv, irq_ready, priv, &irq_state);
239
240 return IRQ_HANDLED;
241}
242
243void usbhs_irq_callback_update(struct usbhs_priv *priv, struct usbhs_mod *mod)
244{
245 u16 intenb0 = 0;
246
247 usbhs_write(priv, INTENB0, 0);
248
249 usbhs_write(priv, BEMPENB, 0);
250 usbhs_write(priv, BRDYENB, 0);
251
252 /*
253 * see also
254 * usbhs_interrupt
255 */
256
257 /*
258 * it don't enable DVSE (intenb0) here
259 * but "mod->irq_dev_state" will be called.
260 */
261
262 if (mod->irq_ctrl_stage)
263 intenb0 |= CTRE;
264
265 if (mod->irq_empty && mod->irq_bempsts) {
266 usbhs_write(priv, BEMPENB, mod->irq_bempsts);
267 intenb0 |= BEMPE;
268 }
269
270 if (mod->irq_ready && mod->irq_brdysts) {
271 usbhs_write(priv, BRDYENB, mod->irq_brdysts);
272 intenb0 |= BRDYE;
273 }
274
275 usbhs_write(priv, INTENB0, intenb0);
276}