blob: c0a0789d8b1e282ef799e89af77b4f8892c4fbca [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-1.0+
Kuninori Morimotof1407d52011-04-04 13:44:59 +09002/*
3 * Renesas USB driver
4 *
5 * Copyright (C) 2011 Renesas Solutions Corp.
6 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 *
17 */
18#include <linux/interrupt.h>
19
Paul Bollecc502bb2012-06-03 18:39:13 +020020#include "common.h"
21#include "mod.h"
Kuninori Morimotof1407d52011-04-04 13:44:59 +090022
23#define usbhs_priv_to_modinfo(priv) (&priv->mod_info)
Kuninori Morimotob002ff62011-04-28 16:41:20 +090024#define usbhs_mod_info_call(priv, func, param...) \
25({ \
26 struct usbhs_mod_info *info; \
27 info = usbhs_priv_to_modinfo(priv); \
28 !info->func ? 0 : \
29 info->func(param); \
30})
31
32/*
33 * autonomy
34 *
35 * these functions are used if platform doesn't have external phy.
36 * -> there is no "notify_hotplug" callback from platform
37 * -> call "notify_hotplug" by itself
38 * -> use own interrupt to connect/disconnect
39 * -> it mean module clock is always ON
40 * ~~~~~~~~~~~~~~~~~~~~~~~~~
41 */
42static int usbhsm_autonomy_get_vbus(struct platform_device *pdev)
43{
44 struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
45
46 return VBSTS & usbhs_read(priv, INTSTS0);
47}
48
49static int usbhsm_autonomy_irq_vbus(struct usbhs_priv *priv,
50 struct usbhs_irq_state *irq_state)
51{
52 struct platform_device *pdev = usbhs_priv_to_pdev(priv);
53
Kuninori Morimotob4fcea22011-10-24 02:25:48 -070054 renesas_usbhs_call_notify_hotplug(pdev);
55
56 return 0;
Kuninori Morimotob002ff62011-04-28 16:41:20 +090057}
58
59void usbhs_mod_autonomy_mode(struct usbhs_priv *priv)
60{
61 struct usbhs_mod_info *info = usbhs_priv_to_modinfo(priv);
62
63 info->irq_vbus = usbhsm_autonomy_irq_vbus;
Kuninori Morimoto48298202011-10-12 21:02:22 -070064 priv->pfunc.get_vbus = usbhsm_autonomy_get_vbus;
Kuninori Morimotob002ff62011-04-28 16:41:20 +090065
66 usbhs_irq_callback_update(priv, NULL);
67}
Kuninori Morimotof1407d52011-04-04 13:44:59 +090068
69/*
70 * host / gadget functions
71 *
72 * renesas_usbhs host/gadget can register itself by below functions.
73 * these functions are called when probe
74 *
75 */
76void usbhs_mod_register(struct usbhs_priv *priv, struct usbhs_mod *mod, int id)
77{
78 struct usbhs_mod_info *info = usbhs_priv_to_modinfo(priv);
79
80 info->mod[id] = mod;
81 mod->priv = priv;
82}
83
84struct usbhs_mod *usbhs_mod_get(struct usbhs_priv *priv, int id)
85{
86 struct usbhs_mod_info *info = usbhs_priv_to_modinfo(priv);
87 struct usbhs_mod *ret = NULL;
88
89 switch (id) {
90 case USBHS_HOST:
91 case USBHS_GADGET:
92 ret = info->mod[id];
93 break;
94 }
95
96 return ret;
97}
98
Kuninori Morimoto0deb3e72011-10-10 22:02:45 -070099int usbhs_mod_is_host(struct usbhs_priv *priv)
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900100{
Kuninori Morimoto0deb3e72011-10-10 22:02:45 -0700101 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900102 struct usbhs_mod_info *info = usbhs_priv_to_modinfo(priv);
103
104 if (!mod)
105 return -EINVAL;
106
107 return info->mod[USBHS_HOST] == mod;
108}
109
110struct usbhs_mod *usbhs_mod_get_current(struct usbhs_priv *priv)
111{
112 struct usbhs_mod_info *info = usbhs_priv_to_modinfo(priv);
113
114 return info->curt;
115}
116
117int usbhs_mod_change(struct usbhs_priv *priv, int id)
118{
119 struct usbhs_mod_info *info = usbhs_priv_to_modinfo(priv);
120 struct usbhs_mod *mod = NULL;
121 int ret = 0;
122
123 /* id < 0 mean no current */
124 switch (id) {
125 case USBHS_HOST:
126 case USBHS_GADGET:
127 mod = info->mod[id];
128 break;
129 default:
130 ret = -EINVAL;
131 }
132 info->curt = mod;
133
134 return ret;
135}
136
137static irqreturn_t usbhs_interrupt(int irq, void *data);
138int usbhs_mod_probe(struct usbhs_priv *priv)
139{
140 struct device *dev = usbhs_priv_to_dev(priv);
141 int ret;
142
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900143 /*
144 * install host/gadget driver
145 */
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700146 ret = usbhs_mod_host_probe(priv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900147 if (ret < 0)
148 return ret;
149
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700150 ret = usbhs_mod_gadget_probe(priv);
151 if (ret < 0)
152 goto mod_init_host_err;
153
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900154 /* irq settings */
Kuninori Morimoto797b4e12012-10-29 00:44:41 -0700155 ret = devm_request_irq(dev, priv->irq, usbhs_interrupt,
Shimoda, Yoshihiro53069af2012-01-05 15:37:18 +0900156 priv->irqflags, dev_name(dev), priv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900157 if (ret) {
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900158 dev_err(dev, "irq request err\n");
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900159 goto mod_init_gadget_err;
160 }
161
162 return ret;
163
164mod_init_gadget_err:
165 usbhs_mod_gadget_remove(priv);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700166mod_init_host_err:
167 usbhs_mod_host_remove(priv);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900168
169 return ret;
170}
171
172void usbhs_mod_remove(struct usbhs_priv *priv)
173{
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700174 usbhs_mod_host_remove(priv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900175 usbhs_mod_gadget_remove(priv);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900176}
177
178/*
179 * status functions
180 */
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900181int usbhs_status_get_device_state(struct usbhs_irq_state *irq_state)
182{
183 int state = irq_state->intsts0 & DVSQ_MASK;
184
185 switch (state) {
186 case POWER_STATE:
187 case DEFAULT_STATE:
188 case ADDRESS_STATE:
189 case CONFIGURATION_STATE:
190 return state;
191 }
192
193 return -EIO;
194}
195
196int usbhs_status_get_ctrl_stage(struct usbhs_irq_state *irq_state)
197{
198 /*
199 * return value
200 *
201 * IDLE_SETUP_STAGE
202 * READ_DATA_STAGE
203 * READ_STATUS_STAGE
204 * WRITE_DATA_STAGE
205 * WRITE_STATUS_STAGE
206 * NODATA_STATUS_STAGE
207 * SEQUENCE_ERROR
208 */
209 return (int)irq_state->intsts0 & CTSQ_MASK;
210}
211
Shimoda, Yoshihiro697d5c02012-08-20 18:39:23 +0900212static int usbhs_status_get_each_irq(struct usbhs_priv *priv,
213 struct usbhs_irq_state *state)
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900214{
215 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
Shimoda, Yoshihiro697d5c02012-08-20 18:39:23 +0900216 u16 intenb0, intenb1;
Yoshihiro Shimodac4d81992014-08-22 20:14:00 +0900217 unsigned long flags;
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900218
Yoshihiro Shimodac4d81992014-08-22 20:14:00 +0900219 /******************** spin lock ********************/
220 usbhs_lock(priv, flags);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900221 state->intsts0 = usbhs_read(priv, INTSTS0);
Shimoda, Yoshihiro697d5c02012-08-20 18:39:23 +0900222 intenb0 = usbhs_read(priv, INTENB0);
Nobuhiro Iwamatsu88a25e02015-01-09 09:28:41 +0900223
224 if (usbhs_mod_is_host(priv)) {
225 state->intsts1 = usbhs_read(priv, INTSTS1);
226 intenb1 = usbhs_read(priv, INTENB1);
Arnd Bergmann672bfda2015-05-22 13:06:35 +0200227 } else {
228 state->intsts1 = intenb1 = 0;
Nobuhiro Iwamatsu88a25e02015-01-09 09:28:41 +0900229 }
Shimoda, Yoshihiro697d5c02012-08-20 18:39:23 +0900230
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900231 /* mask */
Kuninori Morimoto5ea68d52011-04-28 16:41:07 +0900232 if (mod) {
233 state->brdysts = usbhs_read(priv, BRDYSTS);
234 state->nrdysts = usbhs_read(priv, NRDYSTS);
235 state->bempsts = usbhs_read(priv, BEMPSTS);
236
237 state->bempsts &= mod->irq_bempsts;
238 state->brdysts &= mod->irq_brdysts;
239 }
Yoshihiro Shimodac4d81992014-08-22 20:14:00 +0900240 usbhs_unlock(priv, flags);
241 /******************** spin unlock ******************/
Shimoda, Yoshihiro697d5c02012-08-20 18:39:23 +0900242
243 /*
244 * Check whether the irq enable registers and the irq status are set
245 * when IRQF_SHARED is set.
246 */
247 if (priv->irqflags & IRQF_SHARED) {
248 if (!(intenb0 & state->intsts0) &&
249 !(intenb1 & state->intsts1) &&
250 !(state->bempsts) &&
251 !(state->brdysts))
252 return -EIO;
253 }
254
255 return 0;
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900256}
257
258/*
259 * interrupt
260 */
261#define INTSTS0_MAGIC 0xF800 /* acknowledge magical interrupt sources */
262#define INTSTS1_MAGIC 0xA870 /* acknowledge magical interrupt sources */
263static irqreturn_t usbhs_interrupt(int irq, void *data)
264{
265 struct usbhs_priv *priv = data;
266 struct usbhs_irq_state irq_state;
267
Shimoda, Yoshihiro697d5c02012-08-20 18:39:23 +0900268 if (usbhs_status_get_each_irq(priv, &irq_state) < 0)
269 return IRQ_NONE;
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900270
271 /*
272 * clear interrupt
273 *
274 * The hardware is _very_ picky to clear interrupt bit.
275 * Especially INTSTS0_MAGIC, INTSTS1_MAGIC value.
276 *
277 * see
278 * "Operation"
279 * - "Control Transfer (DCP)"
280 * - Function :: VALID bit should 0
281 */
282 usbhs_write(priv, INTSTS0, ~irq_state.intsts0 & INTSTS0_MAGIC);
Nobuhiro Iwamatsu88a25e02015-01-09 09:28:41 +0900283 if (usbhs_mod_is_host(priv))
284 usbhs_write(priv, INTSTS1, ~irq_state.intsts1 & INTSTS1_MAGIC);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900285
Yoshihiro Shimoda519d8bd2016-08-29 18:00:38 +0900286 /*
287 * The driver should not clear the xxxSTS after the line of
288 * "call irq callback functions" because each "if" statement is
289 * possible to call the callback function for avoiding any side effects.
290 */
291 if (irq_state.intsts0 & BRDY)
292 usbhs_write(priv, BRDYSTS, ~irq_state.brdysts);
Kuninori Morimotod5c6a1e2012-10-11 21:49:38 -0700293 usbhs_write(priv, NRDYSTS, ~irq_state.nrdysts);
Yoshihiro Shimoda519d8bd2016-08-29 18:00:38 +0900294 if (irq_state.intsts0 & BEMP)
295 usbhs_write(priv, BEMPSTS, ~irq_state.bempsts);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900296
297 /*
298 * call irq callback functions
299 * see also
300 * usbhs_irq_setting_update
301 */
Kuninori Morimoto89c1d2e2011-10-10 22:06:57 -0700302
303 /* INTSTS0 */
Kuninori Morimotob002ff62011-04-28 16:41:20 +0900304 if (irq_state.intsts0 & VBINT)
305 usbhs_mod_info_call(priv, irq_vbus, priv, &irq_state);
306
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900307 if (irq_state.intsts0 & DVST)
308 usbhs_mod_call(priv, irq_dev_state, priv, &irq_state);
309
310 if (irq_state.intsts0 & CTRT)
311 usbhs_mod_call(priv, irq_ctrl_stage, priv, &irq_state);
312
313 if (irq_state.intsts0 & BEMP)
314 usbhs_mod_call(priv, irq_empty, priv, &irq_state);
315
316 if (irq_state.intsts0 & BRDY)
317 usbhs_mod_call(priv, irq_ready, priv, &irq_state);
318
Nobuhiro Iwamatsu88a25e02015-01-09 09:28:41 +0900319 if (usbhs_mod_is_host(priv)) {
320 /* INTSTS1 */
321 if (irq_state.intsts1 & ATTCH)
322 usbhs_mod_call(priv, irq_attch, priv, &irq_state);
Kuninori Morimoto89c1d2e2011-10-10 22:06:57 -0700323
Nobuhiro Iwamatsu88a25e02015-01-09 09:28:41 +0900324 if (irq_state.intsts1 & DTCH)
325 usbhs_mod_call(priv, irq_dtch, priv, &irq_state);
Kuninori Morimoto89c1d2e2011-10-10 22:06:57 -0700326
Nobuhiro Iwamatsu88a25e02015-01-09 09:28:41 +0900327 if (irq_state.intsts1 & SIGN)
328 usbhs_mod_call(priv, irq_sign, priv, &irq_state);
Kuninori Morimoto89c1d2e2011-10-10 22:06:57 -0700329
Nobuhiro Iwamatsu88a25e02015-01-09 09:28:41 +0900330 if (irq_state.intsts1 & SACK)
331 usbhs_mod_call(priv, irq_sack, priv, &irq_state);
332 }
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900333 return IRQ_HANDLED;
334}
335
336void usbhs_irq_callback_update(struct usbhs_priv *priv, struct usbhs_mod *mod)
337{
338 u16 intenb0 = 0;
Kuninori Morimoto89c1d2e2011-10-10 22:06:57 -0700339 u16 intenb1 = 0;
Kuninori Morimotob002ff62011-04-28 16:41:20 +0900340 struct usbhs_mod_info *info = usbhs_priv_to_modinfo(priv);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900341
Kuninori Morimoto651f5e42011-10-10 22:01:15 -0700342 /*
343 * BEMPENB/BRDYENB are picky.
344 * below method is required
345 *
346 * - clear INTSTS0
347 * - update BEMPENB/BRDYENB
348 * - update INTSTS0
349 */
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900350 usbhs_write(priv, INTENB0, 0);
Nobuhiro Iwamatsu88a25e02015-01-09 09:28:41 +0900351 if (usbhs_mod_is_host(priv))
352 usbhs_write(priv, INTENB1, 0);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900353
354 usbhs_write(priv, BEMPENB, 0);
355 usbhs_write(priv, BRDYENB, 0);
356
357 /*
358 * see also
359 * usbhs_interrupt
360 */
361
362 /*
363 * it don't enable DVSE (intenb0) here
364 * but "mod->irq_dev_state" will be called.
365 */
Kuninori Morimotob002ff62011-04-28 16:41:20 +0900366 if (info->irq_vbus)
367 intenb0 |= VBSE;
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900368
Kuninori Morimoto5ea68d52011-04-28 16:41:07 +0900369 if (mod) {
Kuninori Morimoto89c1d2e2011-10-10 22:06:57 -0700370 /*
371 * INTSTS0
372 */
Kuninori Morimoto5ea68d52011-04-28 16:41:07 +0900373 if (mod->irq_ctrl_stage)
374 intenb0 |= CTRE;
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900375
Kuninori Morimoto5ea68d52011-04-28 16:41:07 +0900376 if (mod->irq_empty && mod->irq_bempsts) {
377 usbhs_write(priv, BEMPENB, mod->irq_bempsts);
378 intenb0 |= BEMPE;
379 }
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900380
Kuninori Morimoto5ea68d52011-04-28 16:41:07 +0900381 if (mod->irq_ready && mod->irq_brdysts) {
382 usbhs_write(priv, BRDYENB, mod->irq_brdysts);
383 intenb0 |= BRDYE;
384 }
Kuninori Morimoto89c1d2e2011-10-10 22:06:57 -0700385
Nobuhiro Iwamatsu88a25e02015-01-09 09:28:41 +0900386 if (usbhs_mod_is_host(priv)) {
387 /*
388 * INTSTS1
389 */
390 if (mod->irq_attch)
391 intenb1 |= ATTCHE;
Kuninori Morimoto89c1d2e2011-10-10 22:06:57 -0700392
Nobuhiro Iwamatsu88a25e02015-01-09 09:28:41 +0900393 if (mod->irq_dtch)
394 intenb1 |= DTCHE;
Kuninori Morimoto89c1d2e2011-10-10 22:06:57 -0700395
Nobuhiro Iwamatsu88a25e02015-01-09 09:28:41 +0900396 if (mod->irq_sign)
397 intenb1 |= SIGNE;
Kuninori Morimoto89c1d2e2011-10-10 22:06:57 -0700398
Nobuhiro Iwamatsu88a25e02015-01-09 09:28:41 +0900399 if (mod->irq_sack)
400 intenb1 |= SACKE;
401 }
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900402 }
403
Kuninori Morimoto651f5e42011-10-10 22:01:15 -0700404 if (intenb0)
405 usbhs_write(priv, INTENB0, intenb0);
Kuninori Morimoto89c1d2e2011-10-10 22:06:57 -0700406
Nobuhiro Iwamatsu88a25e02015-01-09 09:28:41 +0900407 if (usbhs_mod_is_host(priv) && intenb1)
Kuninori Morimoto89c1d2e2011-10-10 22:06:57 -0700408 usbhs_write(priv, INTENB1, intenb1);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900409}