usb: gadget: renesas_usbhs: tidyup INTENB0 method
[deliverable/linux.git] / drivers / usb / renesas_usbhs / common.c
CommitLineData
f1407d5c
KM
1/*
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/io.h>
18#include <linux/module.h>
19#include <linux/pm_runtime.h>
20#include <linux/slab.h>
21#include <linux/sysfs.h>
22#include "./common.h"
23
233f519d
KM
24/*
25 * image of renesas_usbhs
26 *
27 * ex) gadget case
28
29 * mod.c
30 * mod_gadget.c
31 * mod_host.c pipe.c fifo.c
32 *
33 * +-------+ +-----------+
34 * | pipe0 |------>| fifo pio |
35 * +------------+ +-------+ +-----------+
36 * | mod_gadget |=====> | pipe1 |--+
37 * +------------+ +-------+ | +-----------+
38 * | pipe2 | | +-| fifo dma0 |
39 * +------------+ +-------+ | | +-----------+
40 * | mod_host | | pipe3 |<-|--+
41 * +------------+ +-------+ | +-----------+
42 * | .... | +--->| fifo dma1 |
43 * | .... | +-----------+
44 */
45
46
b002ff6e
KM
47#define USBHSF_RUNTIME_PWCTRL (1 << 0)
48
49/* status */
50#define usbhsc_flags_init(p) do {(p)->flags = 0; } while (0)
51#define usbhsc_flags_set(p, b) ((p)->flags |= (b))
52#define usbhsc_flags_clr(p, b) ((p)->flags &= ~(b))
53#define usbhsc_flags_has(p, b) ((p)->flags & (b))
54
f1407d5c
KM
55/*
56 * platform call back
57 *
58 * renesas usb support platform callback function.
59 * Below macro call it.
60 * if platform doesn't have callback, it return 0 (no error)
61 */
62#define usbhs_platform_call(priv, func, args...)\
63 (!(priv) ? -ENODEV : \
64 !((priv)->pfunc->func) ? 0 : \
65 (priv)->pfunc->func(args))
66
67/*
68 * common functions
69 */
70u16 usbhs_read(struct usbhs_priv *priv, u32 reg)
71{
72 return ioread16(priv->base + reg);
73}
74
75void usbhs_write(struct usbhs_priv *priv, u32 reg, u16 data)
76{
77 iowrite16(data, priv->base + reg);
78}
79
80void usbhs_bset(struct usbhs_priv *priv, u32 reg, u16 mask, u16 data)
81{
82 u16 val = usbhs_read(priv, reg);
83
84 val &= ~mask;
85 val |= data & mask;
86
87 usbhs_write(priv, reg, val);
88}
89
206dcc2c
KM
90struct usbhs_priv *usbhs_pdev_to_priv(struct platform_device *pdev)
91{
92 return dev_get_drvdata(&pdev->dev);
93}
94
f1407d5c
KM
95/*
96 * syscfg functions
97 */
98void usbhs_sys_clock_ctrl(struct usbhs_priv *priv, int enable)
99{
100 usbhs_bset(priv, SYSCFG, SCKE, enable ? SCKE : 0);
101}
102
103void usbhs_sys_hispeed_ctrl(struct usbhs_priv *priv, int enable)
104{
105 usbhs_bset(priv, SYSCFG, HSE, enable ? HSE : 0);
106}
107
108void usbhs_sys_usb_ctrl(struct usbhs_priv *priv, int enable)
109{
110 usbhs_bset(priv, SYSCFG, USBE, enable ? USBE : 0);
111}
112
113void usbhs_sys_host_ctrl(struct usbhs_priv *priv, int enable)
114{
115 u16 mask = DCFM | DRPD | DPRPU;
116 u16 val = DCFM | DRPD;
117
118 /*
119 * if enable
120 *
121 * - select Host mode
122 * - D+ Line/D- Line Pull-down
123 */
124 usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
125}
126
127void usbhs_sys_function_ctrl(struct usbhs_priv *priv, int enable)
128{
129 u16 mask = DCFM | DRPD | DPRPU;
130 u16 val = DPRPU;
131
132 /*
133 * if enable
134 *
135 * - select Function mode
136 * - D+ Line Pull-up
137 */
138 usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
139}
140
141/*
142 * frame functions
143 */
144int usbhs_frame_get_num(struct usbhs_priv *priv)
145{
146 return usbhs_read(priv, FRMNUM) & FRNM_MASK;
147}
148
149/*
150 * local functions
151 */
f1407d5c
KM
152static void usbhsc_bus_ctrl(struct usbhs_priv *priv, int enable)
153{
154 int wait = usbhs_get_dparam(priv, buswait_bwait);
155 u16 data = 0;
156
157 if (enable) {
158 /* set bus wait if platform have */
159 if (wait)
160 usbhs_bset(priv, BUSWAIT, 0x000F, wait);
161 }
162 usbhs_write(priv, DVSTCTR, data);
163}
164
165/*
166 * platform default param
167 */
168static u32 usbhsc_default_pipe_type[] = {
169 USB_ENDPOINT_XFER_CONTROL,
170 USB_ENDPOINT_XFER_ISOC,
171 USB_ENDPOINT_XFER_ISOC,
172 USB_ENDPOINT_XFER_BULK,
173 USB_ENDPOINT_XFER_BULK,
174 USB_ENDPOINT_XFER_BULK,
175 USB_ENDPOINT_XFER_INT,
176 USB_ENDPOINT_XFER_INT,
177 USB_ENDPOINT_XFER_INT,
178 USB_ENDPOINT_XFER_INT,
179};
180
181/*
6e267da8
KM
182 * power control
183 */
184static void usbhsc_power_ctrl(struct usbhs_priv *priv, int enable)
185{
186 struct device *dev = usbhs_priv_to_dev(priv);
187
188 if (enable) {
189 /* enable PM */
190 pm_runtime_get_sync(dev);
191
192 /* USB on */
193 usbhs_sys_clock_ctrl(priv, enable);
194 usbhsc_bus_ctrl(priv, enable);
195 } else {
196 /* USB off */
197 usbhsc_bus_ctrl(priv, enable);
198 usbhs_sys_clock_ctrl(priv, enable);
199
200 /* disable PM */
201 pm_runtime_put_sync(dev);
202 }
203}
204
205/*
ca8a282a 206 * hotplug
f1407d5c 207 */
ca8a282a 208static void usbhsc_hotplug(struct usbhs_priv *priv)
f1407d5c 209{
f1407d5c
KM
210 struct platform_device *pdev = usbhs_priv_to_pdev(priv);
211 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
212 int id;
213 int enable;
214 int ret;
215
216 /*
217 * get vbus status from platform
218 */
219 enable = usbhs_platform_call(priv, get_vbus, pdev);
220
221 /*
222 * get id from platform
223 */
224 id = usbhs_platform_call(priv, get_id, pdev);
225
226 if (enable && !mod) {
227 ret = usbhs_mod_change(priv, id);
228 if (ret < 0)
229 return;
230
231 dev_dbg(&pdev->dev, "%s enable\n", __func__);
232
6e267da8 233 /* power on */
b002ff6e
KM
234 if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
235 usbhsc_power_ctrl(priv, enable);
f1407d5c
KM
236
237 /* module start */
238 usbhs_mod_call(priv, start, priv);
239
240 } else if (!enable && mod) {
241 dev_dbg(&pdev->dev, "%s disable\n", __func__);
242
243 /* module stop */
244 usbhs_mod_call(priv, stop, priv);
245
6e267da8 246 /* power off */
b002ff6e
KM
247 if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
248 usbhsc_power_ctrl(priv, enable);
f1407d5c
KM
249
250 usbhs_mod_change(priv, -1);
251
252 /* reset phy for next connection */
253 usbhs_platform_call(priv, phy_reset, pdev);
254 }
255}
256
ca8a282a
KM
257/*
258 * notify hotplug
259 */
260static void usbhsc_notify_hotplug(struct work_struct *work)
261{
262 struct usbhs_priv *priv = container_of(work,
263 struct usbhs_priv,
264 notify_hotplug_work.work);
265 usbhsc_hotplug(priv);
266}
267
bc57381e 268int usbhsc_drvcllbck_notify_hotplug(struct platform_device *pdev)
f1407d5c 269{
206dcc2c 270 struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
bc57381e 271 int delay = usbhs_get_dparam(priv, detection_delay);
f1407d5c
KM
272
273 /*
274 * This functions will be called in interrupt.
275 * To make sure safety context,
276 * use workqueue for usbhs_notify_hotplug
277 */
bc57381e 278 schedule_delayed_work(&priv->notify_hotplug_work, delay);
f1407d5c
KM
279 return 0;
280}
281
282/*
283 * platform functions
284 */
285static int __devinit usbhs_probe(struct platform_device *pdev)
286{
287 struct renesas_usbhs_platform_info *info = pdev->dev.platform_data;
288 struct renesas_usbhs_driver_callback *dfunc;
289 struct usbhs_priv *priv;
290 struct resource *res;
291 unsigned int irq;
292 int ret;
293
294 /* check platform information */
295 if (!info ||
b002ff6e 296 !info->platform_callback.get_id) {
f1407d5c
KM
297 dev_err(&pdev->dev, "no platform information\n");
298 return -EINVAL;
299 }
300
301 /* platform data */
302 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
303 irq = platform_get_irq(pdev, 0);
304 if (!res || (int)irq <= 0) {
305 dev_err(&pdev->dev, "Not enough Renesas USB platform resources.\n");
306 return -ENODEV;
307 }
308
309 /* usb private data */
310 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
311 if (!priv) {
312 dev_err(&pdev->dev, "Could not allocate priv\n");
313 return -ENOMEM;
314 }
315
316 priv->base = ioremap_nocache(res->start, resource_size(res));
317 if (!priv->base) {
318 dev_err(&pdev->dev, "ioremap error.\n");
319 ret = -ENOMEM;
320 goto probe_end_kfree;
321 }
322
323 /*
324 * care platform info
325 */
326 priv->pfunc = &info->platform_callback;
327 priv->dparam = &info->driver_param;
328
329 /* set driver callback functions for platform */
330 dfunc = &info->driver_callback;
331 dfunc->notify_hotplug = usbhsc_drvcllbck_notify_hotplug;
332
333 /* set default param if platform doesn't have */
334 if (!priv->dparam->pipe_type) {
335 priv->dparam->pipe_type = usbhsc_default_pipe_type;
336 priv->dparam->pipe_size = ARRAY_SIZE(usbhsc_default_pipe_type);
337 }
e73a9891
KM
338 if (!priv->dparam->pio_dma_border)
339 priv->dparam->pio_dma_border = 64; /* 64byte */
f1407d5c 340
b002ff6e
KM
341 /* FIXME */
342 /* runtime power control ? */
343 if (priv->pfunc->get_vbus)
344 usbhsc_flags_set(priv, USBHSF_RUNTIME_PWCTRL);
345
f1407d5c
KM
346 /*
347 * priv settings
348 */
349 priv->irq = irq;
350 priv->pdev = pdev;
bc57381e 351 INIT_DELAYED_WORK(&priv->notify_hotplug_work, usbhsc_notify_hotplug);
f1407d5c
KM
352 spin_lock_init(usbhs_priv_to_lock(priv));
353
354 /* call pipe and module init */
355 ret = usbhs_pipe_probe(priv);
356 if (ret < 0)
97f93227 357 goto probe_end_iounmap;
f1407d5c 358
d3af90a5 359 ret = usbhs_fifo_probe(priv);
f1407d5c 360 if (ret < 0)
97f93227 361 goto probe_end_pipe_exit;
f1407d5c 362
d3af90a5
KM
363 ret = usbhs_mod_probe(priv);
364 if (ret < 0)
365 goto probe_end_fifo_exit;
366
f1407d5c
KM
367 /* dev_set_drvdata should be called after usbhs_mod_init */
368 dev_set_drvdata(&pdev->dev, priv);
369
370 /*
371 * deviece reset here because
372 * USB device might be used in boot loader.
373 */
374 usbhs_sys_clock_ctrl(priv, 0);
375
376 /*
377 * platform call
378 *
379 * USB phy setup might depend on CPU/Board.
380 * If platform has its callback functions,
381 * call it here.
382 */
383 ret = usbhs_platform_call(priv, hardware_init, pdev);
384 if (ret < 0) {
385 dev_err(&pdev->dev, "platform prove failed.\n");
97f93227 386 goto probe_end_mod_exit;
f1407d5c
KM
387 }
388
389 /* reset phy for connection */
390 usbhs_platform_call(priv, phy_reset, pdev);
391
b002ff6e
KM
392 /* power control */
393 pm_runtime_enable(&pdev->dev);
394 if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) {
395 usbhsc_power_ctrl(priv, 1);
396 usbhs_mod_autonomy_mode(priv);
397 }
398
f1407d5c
KM
399 /*
400 * manual call notify_hotplug for cold plug
401 */
f1407d5c
KM
402 ret = usbhsc_drvcllbck_notify_hotplug(pdev);
403 if (ret < 0)
404 goto probe_end_call_remove;
405
406 dev_info(&pdev->dev, "probed\n");
407
408 return ret;
409
410probe_end_call_remove:
411 usbhs_platform_call(priv, hardware_exit, pdev);
f1407d5c
KM
412probe_end_mod_exit:
413 usbhs_mod_remove(priv);
d3af90a5
KM
414probe_end_fifo_exit:
415 usbhs_fifo_remove(priv);
97f93227
KM
416probe_end_pipe_exit:
417 usbhs_pipe_remove(priv);
f1407d5c
KM
418probe_end_iounmap:
419 iounmap(priv->base);
420probe_end_kfree:
421 kfree(priv);
422
423 dev_info(&pdev->dev, "probe failed\n");
424
425 return ret;
426}
427
428static int __devexit usbhs_remove(struct platform_device *pdev)
429{
206dcc2c 430 struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
af32fe51
KM
431 struct renesas_usbhs_platform_info *info = pdev->dev.platform_data;
432 struct renesas_usbhs_driver_callback *dfunc = &info->driver_callback;
f1407d5c
KM
433
434 dev_dbg(&pdev->dev, "usb remove\n");
435
af32fe51
KM
436 dfunc->notify_hotplug = NULL;
437
b002ff6e
KM
438 /* power off */
439 if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
440 usbhsc_power_ctrl(priv, 0);
f1407d5c 441
b002ff6e 442 pm_runtime_disable(&pdev->dev);
f1407d5c
KM
443
444 usbhs_platform_call(priv, hardware_exit, pdev);
f1407d5c 445 usbhs_mod_remove(priv);
d3af90a5 446 usbhs_fifo_remove(priv);
97f93227 447 usbhs_pipe_remove(priv);
f1407d5c
KM
448 iounmap(priv->base);
449 kfree(priv);
450
451 return 0;
452}
453
ca8a282a
KM
454static int usbhsc_suspend(struct device *dev)
455{
456 struct usbhs_priv *priv = dev_get_drvdata(dev);
457 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
458
459 if (mod) {
460 usbhs_mod_call(priv, stop, priv);
461 usbhs_mod_change(priv, -1);
462 }
463
464 if (mod || !usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
465 usbhsc_power_ctrl(priv, 0);
466
467 return 0;
468}
469
470static int usbhsc_resume(struct device *dev)
471{
472 struct usbhs_priv *priv = dev_get_drvdata(dev);
473 struct platform_device *pdev = usbhs_priv_to_pdev(priv);
474
475 usbhs_platform_call(priv, phy_reset, pdev);
476
477 if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
478 usbhsc_power_ctrl(priv, 1);
479
480 usbhsc_hotplug(priv);
481
482 return 0;
483}
484
485static int usbhsc_runtime_nop(struct device *dev)
486{
487 /* Runtime PM callback shared between ->runtime_suspend()
488 * and ->runtime_resume(). Simply returns success.
489 *
490 * This driver re-initializes all registers after
491 * pm_runtime_get_sync() anyway so there is no need
492 * to save and restore registers here.
493 */
494 return 0;
495}
496
497static const struct dev_pm_ops usbhsc_pm_ops = {
498 .suspend = usbhsc_suspend,
499 .resume = usbhsc_resume,
500 .runtime_suspend = usbhsc_runtime_nop,
501 .runtime_resume = usbhsc_runtime_nop,
502};
503
f1407d5c
KM
504static struct platform_driver renesas_usbhs_driver = {
505 .driver = {
506 .name = "renesas_usbhs",
ca8a282a 507 .pm = &usbhsc_pm_ops,
f1407d5c
KM
508 },
509 .probe = usbhs_probe,
510 .remove = __devexit_p(usbhs_remove),
511};
512
513static int __init usbhs_init(void)
514{
515 return platform_driver_register(&renesas_usbhs_driver);
516}
517
518static void __exit usbhs_exit(void)
519{
520 platform_driver_unregister(&renesas_usbhs_driver);
521}
522
523module_init(usbhs_init);
524module_exit(usbhs_exit);
525
526MODULE_LICENSE("GPL");
527MODULE_DESCRIPTION("Renesas USB driver");
528MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
This page took 0.074952 seconds and 5 git commands to generate.