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 static 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_host_ctrl(struct usbhs_priv *priv, int enable)
104 {
105 	u16 mask = DCFM | DRPD | DPRPU | HSE | USBE;
106 	u16 val  = DCFM | DRPD | HSE | USBE;
107 	int has_otg = usbhs_get_dparam(priv, has_otg);
108 
109 	if (has_otg)
110 		usbhs_bset(priv, DVSTCTR, (EXTLP | PWEN), (EXTLP | PWEN));
111 
112 	/*
113 	 * if enable
114 	 *
115 	 * - select Host mode
116 	 * - D+ Line/D- Line Pull-down
117 	 */
118 	usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
119 }
120 
121 void usbhs_sys_function_ctrl(struct usbhs_priv *priv, int enable)
122 {
123 	u16 mask = DCFM | DRPD | DPRPU | HSE | USBE;
124 	u16 val  = DPRPU | HSE | USBE;
125 
126 	/*
127 	 * if enable
128 	 *
129 	 * - select Function mode
130 	 * - D+ Line Pull-up
131 	 */
132 	usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
133 }
134 
135 void usbhs_sys_set_test_mode(struct usbhs_priv *priv, u16 mode)
136 {
137 	usbhs_write(priv, TESTMODE, mode);
138 }
139 
140 /*
141  *		frame functions
142  */
143 int usbhs_frame_get_num(struct usbhs_priv *priv)
144 {
145 	return usbhs_read(priv, FRMNUM) & FRNM_MASK;
146 }
147 
148 /*
149  *		usb request functions
150  */
151 void usbhs_usbreq_get_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
152 {
153 	u16 val;
154 
155 	val = usbhs_read(priv, USBREQ);
156 	req->bRequest		= (val >> 8) & 0xFF;
157 	req->bRequestType	= (val >> 0) & 0xFF;
158 
159 	req->wValue	= usbhs_read(priv, USBVAL);
160 	req->wIndex	= usbhs_read(priv, USBINDX);
161 	req->wLength	= usbhs_read(priv, USBLENG);
162 }
163 
164 void usbhs_usbreq_set_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
165 {
166 	usbhs_write(priv, USBREQ,  (req->bRequest << 8) | req->bRequestType);
167 	usbhs_write(priv, USBVAL,  req->wValue);
168 	usbhs_write(priv, USBINDX, req->wIndex);
169 	usbhs_write(priv, USBLENG, req->wLength);
170 
171 	usbhs_bset(priv, DCPCTR, SUREQ, SUREQ);
172 }
173 
174 /*
175  *		bus/vbus functions
176  */
177 void usbhs_bus_send_sof_enable(struct usbhs_priv *priv)
178 {
179 	u16 status = usbhs_read(priv, DVSTCTR) & (USBRST | UACT);
180 
181 	if (status != USBRST) {
182 		struct device *dev = usbhs_priv_to_dev(priv);
183 		dev_err(dev, "usbhs should be reset\n");
184 	}
185 
186 	usbhs_bset(priv, DVSTCTR, (USBRST | UACT), UACT);
187 }
188 
189 void usbhs_bus_send_reset(struct usbhs_priv *priv)
190 {
191 	usbhs_bset(priv, DVSTCTR, (USBRST | UACT), USBRST);
192 }
193 
194 int usbhs_bus_get_speed(struct usbhs_priv *priv)
195 {
196 	u16 dvstctr = usbhs_read(priv, DVSTCTR);
197 
198 	switch (RHST & dvstctr) {
199 	case RHST_LOW_SPEED:
200 		return USB_SPEED_LOW;
201 	case RHST_FULL_SPEED:
202 		return USB_SPEED_FULL;
203 	case RHST_HIGH_SPEED:
204 		return USB_SPEED_HIGH;
205 	}
206 
207 	return USB_SPEED_UNKNOWN;
208 }
209 
210 int usbhs_vbus_ctrl(struct usbhs_priv *priv, int enable)
211 {
212 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
213 
214 	return usbhs_platform_call(priv, set_vbus, pdev, enable);
215 }
216 
217 static void usbhsc_bus_init(struct usbhs_priv *priv)
218 {
219 	usbhs_write(priv, DVSTCTR, 0);
220 
221 	usbhs_vbus_ctrl(priv, 0);
222 }
223 
224 /*
225  *		device configuration
226  */
227 int usbhs_set_device_config(struct usbhs_priv *priv, int devnum,
228 			   u16 upphub, u16 hubport, u16 speed)
229 {
230 	struct device *dev = usbhs_priv_to_dev(priv);
231 	u16 usbspd = 0;
232 	u32 reg = DEVADD0 + (2 * devnum);
233 
234 	if (devnum > 10) {
235 		dev_err(dev, "cannot set speed to unknown device %d\n", devnum);
236 		return -EIO;
237 	}
238 
239 	if (upphub > 0xA) {
240 		dev_err(dev, "unsupported hub number %d\n", upphub);
241 		return -EIO;
242 	}
243 
244 	switch (speed) {
245 	case USB_SPEED_LOW:
246 		usbspd = USBSPD_SPEED_LOW;
247 		break;
248 	case USB_SPEED_FULL:
249 		usbspd = USBSPD_SPEED_FULL;
250 		break;
251 	case USB_SPEED_HIGH:
252 		usbspd = USBSPD_SPEED_HIGH;
253 		break;
254 	default:
255 		dev_err(dev, "unsupported speed %d\n", speed);
256 		return -EIO;
257 	}
258 
259 	usbhs_write(priv, reg,	UPPHUB(upphub)	|
260 				HUBPORT(hubport)|
261 				USBSPD(usbspd));
262 
263 	return 0;
264 }
265 
266 /*
267  *		local functions
268  */
269 static void usbhsc_set_buswait(struct usbhs_priv *priv)
270 {
271 	int wait = usbhs_get_dparam(priv, buswait_bwait);
272 
273 	/* set bus wait if platform have */
274 	if (wait)
275 		usbhs_bset(priv, BUSWAIT, 0x000F, wait);
276 }
277 
278 /*
279  *		platform default param
280  */
281 static u32 usbhsc_default_pipe_type[] = {
282 		USB_ENDPOINT_XFER_CONTROL,
283 		USB_ENDPOINT_XFER_ISOC,
284 		USB_ENDPOINT_XFER_ISOC,
285 		USB_ENDPOINT_XFER_BULK,
286 		USB_ENDPOINT_XFER_BULK,
287 		USB_ENDPOINT_XFER_BULK,
288 		USB_ENDPOINT_XFER_INT,
289 		USB_ENDPOINT_XFER_INT,
290 		USB_ENDPOINT_XFER_INT,
291 		USB_ENDPOINT_XFER_INT,
292 };
293 
294 /*
295  *		power control
296  */
297 static void usbhsc_power_ctrl(struct usbhs_priv *priv, int enable)
298 {
299 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
300 	struct device *dev = usbhs_priv_to_dev(priv);
301 
302 	if (enable) {
303 		/* enable PM */
304 		pm_runtime_get_sync(dev);
305 
306 		/* enable platform power */
307 		usbhs_platform_call(priv, power_ctrl, pdev, priv->base, enable);
308 
309 		/* USB on */
310 		usbhs_sys_clock_ctrl(priv, enable);
311 	} else {
312 		/* USB off */
313 		usbhs_sys_clock_ctrl(priv, enable);
314 
315 		/* disable platform power */
316 		usbhs_platform_call(priv, power_ctrl, pdev, priv->base, enable);
317 
318 		/* disable PM */
319 		pm_runtime_put_sync(dev);
320 	}
321 }
322 
323 /*
324  *		hotplug
325  */
326 static void usbhsc_hotplug(struct usbhs_priv *priv)
327 {
328 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
329 	struct usbhs_mod *mod = usbhs_mod_get_current(priv);
330 	int id;
331 	int enable;
332 	int ret;
333 
334 	/*
335 	 * get vbus status from platform
336 	 */
337 	enable = usbhs_platform_call(priv, get_vbus, pdev);
338 
339 	/*
340 	 * get id from platform
341 	 */
342 	id = usbhs_platform_call(priv, get_id, pdev);
343 
344 	if (enable && !mod) {
345 		ret = usbhs_mod_change(priv, id);
346 		if (ret < 0)
347 			return;
348 
349 		dev_dbg(&pdev->dev, "%s enable\n", __func__);
350 
351 		/* power on */
352 		if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
353 			usbhsc_power_ctrl(priv, enable);
354 
355 		/* bus init */
356 		usbhsc_set_buswait(priv);
357 		usbhsc_bus_init(priv);
358 
359 		/* module start */
360 		usbhs_mod_call(priv, start, priv);
361 
362 	} else if (!enable && mod) {
363 		dev_dbg(&pdev->dev, "%s disable\n", __func__);
364 
365 		/* module stop */
366 		usbhs_mod_call(priv, stop, priv);
367 
368 		/* bus init */
369 		usbhsc_bus_init(priv);
370 
371 		/* power off */
372 		if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
373 			usbhsc_power_ctrl(priv, enable);
374 
375 		usbhs_mod_change(priv, -1);
376 
377 		/* reset phy for next connection */
378 		usbhs_platform_call(priv, phy_reset, pdev);
379 	}
380 }
381 
382 /*
383  *		notify hotplug
384  */
385 static void usbhsc_notify_hotplug(struct work_struct *work)
386 {
387 	struct usbhs_priv *priv = container_of(work,
388 					       struct usbhs_priv,
389 					       notify_hotplug_work.work);
390 	usbhsc_hotplug(priv);
391 }
392 
393 static int usbhsc_drvcllbck_notify_hotplug(struct platform_device *pdev)
394 {
395 	struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
396 	int delay = usbhs_get_dparam(priv, detection_delay);
397 
398 	/*
399 	 * This functions will be called in interrupt.
400 	 * To make sure safety context,
401 	 * use workqueue for usbhs_notify_hotplug
402 	 */
403 	schedule_delayed_work(&priv->notify_hotplug_work,
404 			      msecs_to_jiffies(delay));
405 	return 0;
406 }
407 
408 /*
409  *		platform functions
410  */
411 static int usbhs_probe(struct platform_device *pdev)
412 {
413 	struct renesas_usbhs_platform_info *info = pdev->dev.platform_data;
414 	struct renesas_usbhs_driver_callback *dfunc;
415 	struct usbhs_priv *priv;
416 	struct resource *res, *irq_res;
417 	int ret;
418 
419 	/* check platform information */
420 	if (!info ||
421 	    !info->platform_callback.get_id) {
422 		dev_err(&pdev->dev, "no platform information\n");
423 		return -EINVAL;
424 	}
425 
426 	/* platform data */
427 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
428 	irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
429 	if (!res || !irq_res) {
430 		dev_err(&pdev->dev, "Not enough Renesas USB platform resources.\n");
431 		return -ENODEV;
432 	}
433 
434 	/* usb private data */
435 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
436 	if (!priv) {
437 		dev_err(&pdev->dev, "Could not allocate priv\n");
438 		return -ENOMEM;
439 	}
440 
441 	priv->base = devm_request_and_ioremap(&pdev->dev, res);
442 	if (!priv->base) {
443 		dev_err(&pdev->dev, "ioremap error.\n");
444 		return -ENOMEM;
445 	}
446 
447 	/*
448 	 * care platform info
449 	 */
450 	memcpy(&priv->pfunc,
451 	       &info->platform_callback,
452 	       sizeof(struct renesas_usbhs_platform_callback));
453 	memcpy(&priv->dparam,
454 	       &info->driver_param,
455 	       sizeof(struct renesas_usbhs_driver_param));
456 
457 	/* set driver callback functions for platform */
458 	dfunc			= &info->driver_callback;
459 	dfunc->notify_hotplug	= usbhsc_drvcllbck_notify_hotplug;
460 
461 	/* set default param if platform doesn't have */
462 	if (!priv->dparam.pipe_type) {
463 		priv->dparam.pipe_type = usbhsc_default_pipe_type;
464 		priv->dparam.pipe_size = ARRAY_SIZE(usbhsc_default_pipe_type);
465 	}
466 	if (!priv->dparam.pio_dma_border)
467 		priv->dparam.pio_dma_border = 64; /* 64byte */
468 
469 	/* FIXME */
470 	/* runtime power control ? */
471 	if (priv->pfunc.get_vbus)
472 		usbhsc_flags_set(priv, USBHSF_RUNTIME_PWCTRL);
473 
474 	/*
475 	 * priv settings
476 	 */
477 	priv->irq	= irq_res->start;
478 	if (irq_res->flags & IORESOURCE_IRQ_SHAREABLE)
479 		priv->irqflags = IRQF_SHARED;
480 	priv->pdev	= pdev;
481 	INIT_DELAYED_WORK(&priv->notify_hotplug_work, usbhsc_notify_hotplug);
482 	spin_lock_init(usbhs_priv_to_lock(priv));
483 
484 	/* call pipe and module init */
485 	ret = usbhs_pipe_probe(priv);
486 	if (ret < 0)
487 		return ret;
488 
489 	ret = usbhs_fifo_probe(priv);
490 	if (ret < 0)
491 		goto probe_end_pipe_exit;
492 
493 	ret = usbhs_mod_probe(priv);
494 	if (ret < 0)
495 		goto probe_end_fifo_exit;
496 
497 	/* dev_set_drvdata should be called after usbhs_mod_init */
498 	dev_set_drvdata(&pdev->dev, priv);
499 
500 	/*
501 	 * deviece reset here because
502 	 * USB device might be used in boot loader.
503 	 */
504 	usbhs_sys_clock_ctrl(priv, 0);
505 
506 	/*
507 	 * platform call
508 	 *
509 	 * USB phy setup might depend on CPU/Board.
510 	 * If platform has its callback functions,
511 	 * call it here.
512 	 */
513 	ret = usbhs_platform_call(priv, hardware_init, pdev);
514 	if (ret < 0) {
515 		dev_err(&pdev->dev, "platform prove failed.\n");
516 		goto probe_end_mod_exit;
517 	}
518 
519 	/* reset phy for connection */
520 	usbhs_platform_call(priv, phy_reset, pdev);
521 
522 	/* power control */
523 	pm_runtime_enable(&pdev->dev);
524 	if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) {
525 		usbhsc_power_ctrl(priv, 1);
526 		usbhs_mod_autonomy_mode(priv);
527 	}
528 
529 	/*
530 	 * manual call notify_hotplug for cold plug
531 	 */
532 	ret = usbhsc_drvcllbck_notify_hotplug(pdev);
533 	if (ret < 0)
534 		goto probe_end_call_remove;
535 
536 	dev_info(&pdev->dev, "probed\n");
537 
538 	return ret;
539 
540 probe_end_call_remove:
541 	usbhs_platform_call(priv, hardware_exit, pdev);
542 probe_end_mod_exit:
543 	usbhs_mod_remove(priv);
544 probe_end_fifo_exit:
545 	usbhs_fifo_remove(priv);
546 probe_end_pipe_exit:
547 	usbhs_pipe_remove(priv);
548 
549 	dev_info(&pdev->dev, "probe failed\n");
550 
551 	return ret;
552 }
553 
554 static int __devexit usbhs_remove(struct platform_device *pdev)
555 {
556 	struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
557 	struct renesas_usbhs_platform_info *info = pdev->dev.platform_data;
558 	struct renesas_usbhs_driver_callback *dfunc = &info->driver_callback;
559 
560 	dev_dbg(&pdev->dev, "usb remove\n");
561 
562 	dfunc->notify_hotplug = NULL;
563 
564 	/* power off */
565 	if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
566 		usbhsc_power_ctrl(priv, 0);
567 
568 	pm_runtime_disable(&pdev->dev);
569 
570 	usbhs_platform_call(priv, hardware_exit, pdev);
571 	usbhs_mod_remove(priv);
572 	usbhs_fifo_remove(priv);
573 	usbhs_pipe_remove(priv);
574 
575 	return 0;
576 }
577 
578 static int usbhsc_suspend(struct device *dev)
579 {
580 	struct usbhs_priv *priv = dev_get_drvdata(dev);
581 	struct usbhs_mod *mod = usbhs_mod_get_current(priv);
582 
583 	if (mod) {
584 		usbhs_mod_call(priv, stop, priv);
585 		usbhs_mod_change(priv, -1);
586 	}
587 
588 	if (mod || !usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
589 		usbhsc_power_ctrl(priv, 0);
590 
591 	return 0;
592 }
593 
594 static int usbhsc_resume(struct device *dev)
595 {
596 	struct usbhs_priv *priv = dev_get_drvdata(dev);
597 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
598 
599 	if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
600 		usbhsc_power_ctrl(priv, 1);
601 
602 	usbhs_platform_call(priv, phy_reset, pdev);
603 
604 	usbhsc_drvcllbck_notify_hotplug(pdev);
605 
606 	return 0;
607 }
608 
609 static int usbhsc_runtime_nop(struct device *dev)
610 {
611 	/* Runtime PM callback shared between ->runtime_suspend()
612 	 * and ->runtime_resume(). Simply returns success.
613 	 *
614 	 * This driver re-initializes all registers after
615 	 * pm_runtime_get_sync() anyway so there is no need
616 	 * to save and restore registers here.
617 	 */
618 	return 0;
619 }
620 
621 static const struct dev_pm_ops usbhsc_pm_ops = {
622 	.suspend		= usbhsc_suspend,
623 	.resume			= usbhsc_resume,
624 	.runtime_suspend	= usbhsc_runtime_nop,
625 	.runtime_resume		= usbhsc_runtime_nop,
626 };
627 
628 static struct platform_driver renesas_usbhs_driver = {
629 	.driver		= {
630 		.name	= "renesas_usbhs",
631 		.pm	= &usbhsc_pm_ops,
632 	},
633 	.probe		= usbhs_probe,
634 	.remove		= __devexit_p(usbhs_remove),
635 };
636 
637 module_platform_driver(renesas_usbhs_driver);
638 
639 MODULE_LICENSE("GPL");
640 MODULE_DESCRIPTION("Renesas USB driver");
641 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
642