Commit | Line | Data |
---|---|---|
5fd54ace | 1 | // SPDX-License-Identifier: GPL-1.0+ |
f1407d5c KM |
2 | /* |
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 | ||
cc502bb7 PB |
20 | #include "common.h" |
21 | #include "mod.h" | |
f1407d5c KM |
22 | |
23 | #define usbhs_priv_to_modinfo(priv) (&priv->mod_info) | |
b002ff6e KM |
24 | #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 | */ | |
42 | static 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 | ||
49 | static 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 | ||
b4fcea2a KM |
54 | renesas_usbhs_call_notify_hotplug(pdev); |
55 | ||
56 | return 0; | |
b002ff6e KM |
57 | } |
58 | ||
59 | void 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; | |
48298206 | 64 | priv->pfunc.get_vbus = usbhsm_autonomy_get_vbus; |
b002ff6e KM |
65 | |
66 | usbhs_irq_callback_update(priv, NULL); | |
67 | } | |
f1407d5c KM |
68 | |
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 | */ | |
76 | void 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 | ||
84 | struct 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 | ||
0deb3e77 | 99 | int usbhs_mod_is_host(struct usbhs_priv *priv) |
f1407d5c | 100 | { |
0deb3e77 | 101 | struct usbhs_mod *mod = usbhs_mod_get_current(priv); |
f1407d5c KM |
102 | 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 | ||
110 | struct 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 | ||
117 | int 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 | ||
137 | static irqreturn_t usbhs_interrupt(int irq, void *data); | |
138 | int usbhs_mod_probe(struct usbhs_priv *priv) | |
139 | { | |
140 | struct device *dev = usbhs_priv_to_dev(priv); | |
141 | int ret; | |
142 | ||
2f98382d KM |
143 | /* |
144 | * install host/gadget driver | |
145 | */ | |
034d7c13 | 146 | ret = usbhs_mod_host_probe(priv); |
2f98382d KM |
147 | if (ret < 0) |
148 | return ret; | |
149 | ||
034d7c13 KM |
150 | ret = usbhs_mod_gadget_probe(priv); |
151 | if (ret < 0) | |
152 | goto mod_init_host_err; | |
153 | ||
f1407d5c | 154 | /* irq settings */ |
797b4e14 | 155 | ret = devm_request_irq(dev, priv->irq, usbhs_interrupt, |
53069af3 | 156 | priv->irqflags, dev_name(dev), priv); |
2f98382d | 157 | if (ret) { |
f1407d5c | 158 | dev_err(dev, "irq request err\n"); |
2f98382d KM |
159 | goto mod_init_gadget_err; |
160 | } | |
161 | ||
162 | return ret; | |
163 | ||
164 | mod_init_gadget_err: | |
165 | usbhs_mod_gadget_remove(priv); | |
034d7c13 KM |
166 | mod_init_host_err: |
167 | usbhs_mod_host_remove(priv); | |
f1407d5c KM |
168 | |
169 | return ret; | |
170 | } | |
171 | ||
172 | void usbhs_mod_remove(struct usbhs_priv *priv) | |
173 | { | |
034d7c13 | 174 | usbhs_mod_host_remove(priv); |
2f98382d | 175 | usbhs_mod_gadget_remove(priv); |
f1407d5c KM |
176 | } |
177 | ||
178 | /* | |
179 | * status functions | |
180 | */ | |
f1407d5c KM |
181 | int 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 | ||
196 | int 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 | ||
697d5c00 SY |
212 | static int usbhs_status_get_each_irq(struct usbhs_priv *priv, |
213 | struct usbhs_irq_state *state) | |
f1407d5c KM |
214 | { |
215 | struct usbhs_mod *mod = usbhs_mod_get_current(priv); | |
697d5c00 | 216 | u16 intenb0, intenb1; |
c4d8199b | 217 | unsigned long flags; |
f1407d5c | 218 | |
c4d8199b YS |
219 | /******************** spin lock ********************/ |
220 | usbhs_lock(priv, flags); | |
f1407d5c | 221 | state->intsts0 = usbhs_read(priv, INTSTS0); |
697d5c00 | 222 | intenb0 = usbhs_read(priv, INTENB0); |
88a25e02 NI |
223 | |
224 | if (usbhs_mod_is_host(priv)) { | |
225 | state->intsts1 = usbhs_read(priv, INTSTS1); | |
226 | intenb1 = usbhs_read(priv, INTENB1); | |
672bfdaa AB |
227 | } else { |
228 | state->intsts1 = intenb1 = 0; | |
88a25e02 | 229 | } |
697d5c00 | 230 | |
f1407d5c | 231 | /* mask */ |
5ea68d54 KM |
232 | 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 | } | |
c4d8199b YS |
240 | usbhs_unlock(priv, flags); |
241 | /******************** spin unlock ******************/ | |
697d5c00 SY |
242 | |
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; | |
f1407d5c KM |
256 | } |
257 | ||
258 | /* | |
259 | * interrupt | |
260 | */ | |
261 | #define INTSTS0_MAGIC 0xF800 /* acknowledge magical interrupt sources */ | |
262 | #define INTSTS1_MAGIC 0xA870 /* acknowledge magical interrupt sources */ | |
263 | static irqreturn_t usbhs_interrupt(int irq, void *data) | |
264 | { | |
265 | struct usbhs_priv *priv = data; | |
266 | struct usbhs_irq_state irq_state; | |
267 | ||
697d5c00 SY |
268 | if (usbhs_status_get_each_irq(priv, &irq_state) < 0) |
269 | return IRQ_NONE; | |
f1407d5c KM |
270 | |
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); | |
88a25e02 NI |
283 | if (usbhs_mod_is_host(priv)) |
284 | usbhs_write(priv, INTSTS1, ~irq_state.intsts1 & INTSTS1_MAGIC); | |
f1407d5c | 285 | |
519d8bd4 YS |
286 | /* |
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); | |
d5c6a1e0 | 293 | usbhs_write(priv, NRDYSTS, ~irq_state.nrdysts); |
519d8bd4 YS |
294 | if (irq_state.intsts0 & BEMP) |
295 | usbhs_write(priv, BEMPSTS, ~irq_state.bempsts); | |
f1407d5c KM |
296 | |
297 | /* | |
298 | * call irq callback functions | |
299 | * see also | |
300 | * usbhs_irq_setting_update | |
301 | */ | |
89c1d2e7 KM |
302 | |
303 | /* INTSTS0 */ | |
b002ff6e KM |
304 | if (irq_state.intsts0 & VBINT) |
305 | usbhs_mod_info_call(priv, irq_vbus, priv, &irq_state); | |
306 | ||
f1407d5c KM |
307 | 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 | ||
88a25e02 NI |
319 | if (usbhs_mod_is_host(priv)) { |
320 | /* INTSTS1 */ | |
321 | if (irq_state.intsts1 & ATTCH) | |
322 | usbhs_mod_call(priv, irq_attch, priv, &irq_state); | |
89c1d2e7 | 323 | |
88a25e02 NI |
324 | if (irq_state.intsts1 & DTCH) |
325 | usbhs_mod_call(priv, irq_dtch, priv, &irq_state); | |
89c1d2e7 | 326 | |
88a25e02 NI |
327 | if (irq_state.intsts1 & SIGN) |
328 | usbhs_mod_call(priv, irq_sign, priv, &irq_state); | |
89c1d2e7 | 329 | |
88a25e02 NI |
330 | if (irq_state.intsts1 & SACK) |
331 | usbhs_mod_call(priv, irq_sack, priv, &irq_state); | |
332 | } | |
f1407d5c KM |
333 | return IRQ_HANDLED; |
334 | } | |
335 | ||
336 | void usbhs_irq_callback_update(struct usbhs_priv *priv, struct usbhs_mod *mod) | |
337 | { | |
338 | u16 intenb0 = 0; | |
89c1d2e7 | 339 | u16 intenb1 = 0; |
b002ff6e | 340 | struct usbhs_mod_info *info = usbhs_priv_to_modinfo(priv); |
f1407d5c | 341 | |
651f5e49 KM |
342 | /* |
343 | * BEMPENB/BRDYENB are picky. | |
344 | * below method is required | |
345 | * | |
346 | * - clear INTSTS0 | |
347 | * - update BEMPENB/BRDYENB | |
348 | * - update INTSTS0 | |
349 | */ | |
f1407d5c | 350 | usbhs_write(priv, INTENB0, 0); |
88a25e02 NI |
351 | if (usbhs_mod_is_host(priv)) |
352 | usbhs_write(priv, INTENB1, 0); | |
f1407d5c KM |
353 | |
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 | */ | |
b002ff6e KM |
366 | if (info->irq_vbus) |
367 | intenb0 |= VBSE; | |
f1407d5c | 368 | |
5ea68d54 | 369 | if (mod) { |
89c1d2e7 KM |
370 | /* |
371 | * INTSTS0 | |
372 | */ | |
5ea68d54 KM |
373 | if (mod->irq_ctrl_stage) |
374 | intenb0 |= CTRE; | |
f1407d5c | 375 | |
5ea68d54 KM |
376 | if (mod->irq_empty && mod->irq_bempsts) { |
377 | usbhs_write(priv, BEMPENB, mod->irq_bempsts); | |
378 | intenb0 |= BEMPE; | |
379 | } | |
f1407d5c | 380 | |
5ea68d54 KM |
381 | if (mod->irq_ready && mod->irq_brdysts) { |
382 | usbhs_write(priv, BRDYENB, mod->irq_brdysts); | |
383 | intenb0 |= BRDYE; | |
384 | } | |
89c1d2e7 | 385 | |
88a25e02 NI |
386 | if (usbhs_mod_is_host(priv)) { |
387 | /* | |
388 | * INTSTS1 | |
389 | */ | |
390 | if (mod->irq_attch) | |
391 | intenb1 |= ATTCHE; | |
89c1d2e7 | 392 | |
88a25e02 NI |
393 | if (mod->irq_dtch) |
394 | intenb1 |= DTCHE; | |
89c1d2e7 | 395 | |
88a25e02 NI |
396 | if (mod->irq_sign) |
397 | intenb1 |= SIGNE; | |
89c1d2e7 | 398 | |
88a25e02 NI |
399 | if (mod->irq_sack) |
400 | intenb1 |= SACKE; | |
401 | } | |
f1407d5c KM |
402 | } |
403 | ||
651f5e49 KM |
404 | if (intenb0) |
405 | usbhs_write(priv, INTENB0, intenb0); | |
89c1d2e7 | 406 | |
88a25e02 | 407 | if (usbhs_mod_is_host(priv) && intenb1) |
89c1d2e7 | 408 | usbhs_write(priv, INTENB1, intenb1); |
f1407d5c | 409 | } |