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