blob: fd43fdd6cd81e1f583bc8ad37af9f2606d7c80b7 [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/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
Kuninori Morimoto233f5192011-07-07 00:23:24 -070024/*
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
Kuninori Morimotob002ff62011-04-28 16:41:20 +090047#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
Kuninori Morimotof1407d52011-04-04 13:44:59 +090055/*
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
Kuninori Morimoto206dcc22011-04-28 16:40:54 +090090struct usbhs_priv *usbhs_pdev_to_priv(struct platform_device *pdev)
91{
92 return dev_get_drvdata(&pdev->dev);
93}
94
Kuninori Morimotof1407d52011-04-04 13:44:59 +090095/*
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 */
Kuninori Morimoto11935de2011-10-10 22:01:28 -0700152static void usbhsc_set_buswait(struct usbhs_priv *priv)
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900153{
154 int wait = usbhs_get_dparam(priv, buswait_bwait);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900155
Kuninori Morimoto11935de2011-10-10 22:01:28 -0700156 /* set bus wait if platform have */
157 if (wait)
158 usbhs_bset(priv, BUSWAIT, 0x000F, wait);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900159}
160
161/*
162 * platform default param
163 */
164static 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/*
Kuninori Morimoto6e267da2011-04-28 16:41:02 +0900178 * power control
179 */
180static 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);
Kuninori Morimoto11935de2011-10-10 22:01:28 -0700190 usbhsc_set_buswait(priv);
Kuninori Morimoto6e267da2011-04-28 16:41:02 +0900191 } else {
192 /* USB off */
Kuninori Morimoto6e267da2011-04-28 16:41:02 +0900193 usbhs_sys_clock_ctrl(priv, enable);
194
195 /* disable PM */
196 pm_runtime_put_sync(dev);
197 }
198}
199
200/*
Kuninori Morimotoca8a2822011-10-10 21:58:19 -0700201 * hotplug
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900202 */
Kuninori Morimotoca8a2822011-10-10 21:58:19 -0700203static void usbhsc_hotplug(struct usbhs_priv *priv)
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900204{
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900205 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
Kuninori Morimoto6e267da2011-04-28 16:41:02 +0900228 /* power on */
Kuninori Morimotob002ff62011-04-28 16:41:20 +0900229 if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
230 usbhsc_power_ctrl(priv, enable);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900231
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
Kuninori Morimoto6e267da2011-04-28 16:41:02 +0900241 /* power off */
Kuninori Morimotob002ff62011-04-28 16:41:20 +0900242 if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
243 usbhsc_power_ctrl(priv, enable);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900244
245 usbhs_mod_change(priv, -1);
246
247 /* reset phy for next connection */
248 usbhs_platform_call(priv, phy_reset, pdev);
249 }
250}
251
Kuninori Morimotoca8a2822011-10-10 21:58:19 -0700252/*
253 * notify hotplug
254 */
255static 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
Kuninori Morimotobc573812011-04-28 16:41:14 +0900263int usbhsc_drvcllbck_notify_hotplug(struct platform_device *pdev)
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900264{
Kuninori Morimoto206dcc22011-04-28 16:40:54 +0900265 struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
Kuninori Morimotobc573812011-04-28 16:41:14 +0900266 int delay = usbhs_get_dparam(priv, detection_delay);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900267
268 /*
269 * This functions will be called in interrupt.
270 * To make sure safety context,
271 * use workqueue for usbhs_notify_hotplug
272 */
Kuninori Morimotobc573812011-04-28 16:41:14 +0900273 schedule_delayed_work(&priv->notify_hotplug_work, delay);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900274 return 0;
275}
276
277/*
278 * platform functions
279 */
280static 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 ||
Kuninori Morimotob002ff62011-04-28 16:41:20 +0900291 !info->platform_callback.get_id) {
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900292 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 }
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900333 if (!priv->dparam->pio_dma_border)
334 priv->dparam->pio_dma_border = 64; /* 64byte */
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900335
Kuninori Morimotob002ff62011-04-28 16:41:20 +0900336 /* FIXME */
337 /* runtime power control ? */
338 if (priv->pfunc->get_vbus)
339 usbhsc_flags_set(priv, USBHSF_RUNTIME_PWCTRL);
340
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900341 /*
342 * priv settings
343 */
344 priv->irq = irq;
345 priv->pdev = pdev;
Kuninori Morimotobc573812011-04-28 16:41:14 +0900346 INIT_DELAYED_WORK(&priv->notify_hotplug_work, usbhsc_notify_hotplug);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900347 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)
Kuninori Morimoto97f93222011-05-11 16:00:15 +0900352 goto probe_end_iounmap;
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900353
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900354 ret = usbhs_fifo_probe(priv);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900355 if (ret < 0)
Kuninori Morimoto97f93222011-05-11 16:00:15 +0900356 goto probe_end_pipe_exit;
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900357
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900358 ret = usbhs_mod_probe(priv);
359 if (ret < 0)
360 goto probe_end_fifo_exit;
361
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900362 /* 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");
Kuninori Morimoto97f93222011-05-11 16:00:15 +0900381 goto probe_end_mod_exit;
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900382 }
383
384 /* reset phy for connection */
385 usbhs_platform_call(priv, phy_reset, pdev);
386
Kuninori Morimotob002ff62011-04-28 16:41:20 +0900387 /* 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
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900394 /*
395 * manual call notify_hotplug for cold plug
396 */
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900397 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
405probe_end_call_remove:
406 usbhs_platform_call(priv, hardware_exit, pdev);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900407probe_end_mod_exit:
408 usbhs_mod_remove(priv);
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900409probe_end_fifo_exit:
410 usbhs_fifo_remove(priv);
Kuninori Morimoto97f93222011-05-11 16:00:15 +0900411probe_end_pipe_exit:
412 usbhs_pipe_remove(priv);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900413probe_end_iounmap:
414 iounmap(priv->base);
415probe_end_kfree:
416 kfree(priv);
417
418 dev_info(&pdev->dev, "probe failed\n");
419
420 return ret;
421}
422
423static int __devexit usbhs_remove(struct platform_device *pdev)
424{
Kuninori Morimoto206dcc22011-04-28 16:40:54 +0900425 struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
Kuninori Morimotoaf32fe52011-04-21 14:10:16 +0900426 struct renesas_usbhs_platform_info *info = pdev->dev.platform_data;
427 struct renesas_usbhs_driver_callback *dfunc = &info->driver_callback;
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900428
429 dev_dbg(&pdev->dev, "usb remove\n");
430
Kuninori Morimotoaf32fe52011-04-21 14:10:16 +0900431 dfunc->notify_hotplug = NULL;
432
Kuninori Morimotob002ff62011-04-28 16:41:20 +0900433 /* power off */
434 if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
435 usbhsc_power_ctrl(priv, 0);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900436
Kuninori Morimotob002ff62011-04-28 16:41:20 +0900437 pm_runtime_disable(&pdev->dev);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900438
439 usbhs_platform_call(priv, hardware_exit, pdev);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900440 usbhs_mod_remove(priv);
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900441 usbhs_fifo_remove(priv);
Kuninori Morimoto97f93222011-05-11 16:00:15 +0900442 usbhs_pipe_remove(priv);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900443 iounmap(priv->base);
444 kfree(priv);
445
446 return 0;
447}
448
Kuninori Morimotoca8a2822011-10-10 21:58:19 -0700449static 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
465static 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
480static 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
492static 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
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900499static struct platform_driver renesas_usbhs_driver = {
500 .driver = {
501 .name = "renesas_usbhs",
Kuninori Morimotoca8a2822011-10-10 21:58:19 -0700502 .pm = &usbhsc_pm_ops,
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900503 },
504 .probe = usbhs_probe,
505 .remove = __devexit_p(usbhs_remove),
506};
507
508static int __init usbhs_init(void)
509{
510 return platform_driver_register(&renesas_usbhs_driver);
511}
512
513static void __exit usbhs_exit(void)
514{
515 platform_driver_unregister(&renesas_usbhs_driver);
516}
517
518module_init(usbhs_init);
519module_exit(usbhs_exit);
520
521MODULE_LICENSE("GPL");
522MODULE_DESCRIPTION("Renesas USB driver");
523MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");