common.c 18.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Renesas USB driver
 *
 * Copyright (C) 2011 Renesas Solutions Corp.
 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */
17
#include <linux/err.h>
18
#include <linux/gpio.h>
19 20
#include <linux/io.h>
#include <linux/module.h>
21 22
#include <linux/of_device.h>
#include <linux/of_gpio.h>
23 24 25
#include <linux/pm_runtime.h>
#include <linux/slab.h>
#include <linux/sysfs.h>
26
#include "common.h"
27
#include "rcar2.h"
28

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
/*
 *		image of renesas_usbhs
 *
 * ex) gadget case

 * mod.c
 * mod_gadget.c
 * mod_host.c		pipe.c		fifo.c
 *
 *			+-------+	+-----------+
 *			| pipe0 |------>| fifo pio  |
 * +------------+	+-------+	+-----------+
 * | mod_gadget |=====> | pipe1 |--+
 * +------------+	+-------+  |	+-----------+
 *			| pipe2 |  |  +-| fifo dma0 |
 * +------------+	+-------+  |  |	+-----------+
 * | mod_host   |	| pipe3 |<-|--+
 * +------------+	+-------+  |	+-----------+
 *			| ....  |  +--->| fifo dma1 |
 *			| ....  |	+-----------+
 */


52 53 54 55 56 57 58 59
#define USBHSF_RUNTIME_PWCTRL	(1 << 0)

/* status */
#define usbhsc_flags_init(p)   do {(p)->flags = 0; } while (0)
#define usbhsc_flags_set(p, b) ((p)->flags |=  (b))
#define usbhsc_flags_clr(p, b) ((p)->flags &= ~(b))
#define usbhsc_flags_has(p, b) ((p)->flags &   (b))

60 61 62 63 64 65 66 67 68
/*
 * platform call back
 *
 * renesas usb support platform callback function.
 * Below macro call it.
 * if platform doesn't have callback, it return 0 (no error)
 */
#define usbhs_platform_call(priv, func, args...)\
	(!(priv) ? -ENODEV :			\
69 70
	 !((priv)->pfunc.func) ? 0 :		\
	 (priv)->pfunc.func(args))
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

/*
 *		common functions
 */
u16 usbhs_read(struct usbhs_priv *priv, u32 reg)
{
	return ioread16(priv->base + reg);
}

void usbhs_write(struct usbhs_priv *priv, u32 reg, u16 data)
{
	iowrite16(data, priv->base + reg);
}

void usbhs_bset(struct usbhs_priv *priv, u32 reg, u16 mask, u16 data)
{
	u16 val = usbhs_read(priv, reg);

	val &= ~mask;
	val |= data & mask;

	usbhs_write(priv, reg, val);
}

95 96 97 98 99
struct usbhs_priv *usbhs_pdev_to_priv(struct platform_device *pdev)
{
	return dev_get_drvdata(&pdev->dev);
}

100 101 102
/*
 *		syscfg functions
 */
103
static void usbhs_sys_clock_ctrl(struct usbhs_priv *priv, int enable)
104 105 106 107 108 109
{
	usbhs_bset(priv, SYSCFG, SCKE, enable ? SCKE : 0);
}

void usbhs_sys_host_ctrl(struct usbhs_priv *priv, int enable)
{
110 111
	u16 mask = DCFM | DRPD | DPRPU | HSE | USBE;
	u16 val  = DCFM | DRPD | HSE | USBE;
112 113 114 115
	int has_otg = usbhs_get_dparam(priv, has_otg);

	if (has_otg)
		usbhs_bset(priv, DVSTCTR, (EXTLP | PWEN), (EXTLP | PWEN));
116 117 118 119 120 121 122 123 124 125 126 127

	/*
	 * if enable
	 *
	 * - select Host mode
	 * - D+ Line/D- Line Pull-down
	 */
	usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
}

void usbhs_sys_function_ctrl(struct usbhs_priv *priv, int enable)
{
128
	u16 mask = DCFM | DRPD | DPRPU | HSE | USBE;
129
	u16 val  = HSE | USBE;
130 131 132 133 134

	/*
	 * if enable
	 *
	 * - select Function mode
135 136 137
	 * - D+ Line Pull-up is disabled
	 *      When D+ Line Pull-up is enabled,
	 *      calling usbhs_sys_function_pullup(,1)
138 139 140 141
	 */
	usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
}

142 143 144 145 146
void usbhs_sys_function_pullup(struct usbhs_priv *priv, int enable)
{
	usbhs_bset(priv, SYSCFG, DPRPU, enable ? DPRPU : 0);
}

147 148 149 150 151
void usbhs_sys_set_test_mode(struct usbhs_priv *priv, u16 mode)
{
	usbhs_write(priv, TESTMODE, mode);
}

152 153 154 155 156 157 158 159
/*
 *		frame functions
 */
int usbhs_frame_get_num(struct usbhs_priv *priv)
{
	return usbhs_read(priv, FRMNUM) & FRNM_MASK;
}

160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
/*
 *		usb request functions
 */
void usbhs_usbreq_get_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
{
	u16 val;

	val = usbhs_read(priv, USBREQ);
	req->bRequest		= (val >> 8) & 0xFF;
	req->bRequestType	= (val >> 0) & 0xFF;

	req->wValue	= usbhs_read(priv, USBVAL);
	req->wIndex	= usbhs_read(priv, USBINDX);
	req->wLength	= usbhs_read(priv, USBLENG);
}

void usbhs_usbreq_set_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
{
	usbhs_write(priv, USBREQ,  (req->bRequest << 8) | req->bRequestType);
	usbhs_write(priv, USBVAL,  req->wValue);
	usbhs_write(priv, USBINDX, req->wIndex);
	usbhs_write(priv, USBLENG, req->wLength);

	usbhs_bset(priv, DCPCTR, SUREQ, SUREQ);
}

186 187 188 189 190
/*
 *		bus/vbus functions
 */
void usbhs_bus_send_sof_enable(struct usbhs_priv *priv)
{
191 192 193 194 195 196 197
	u16 status = usbhs_read(priv, DVSTCTR) & (USBRST | UACT);

	if (status != USBRST) {
		struct device *dev = usbhs_priv_to_dev(priv);
		dev_err(dev, "usbhs should be reset\n");
	}

198 199 200 201 202 203 204 205
	usbhs_bset(priv, DVSTCTR, (USBRST | UACT), UACT);
}

void usbhs_bus_send_reset(struct usbhs_priv *priv)
{
	usbhs_bset(priv, DVSTCTR, (USBRST | UACT), USBRST);
}

206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
int usbhs_bus_get_speed(struct usbhs_priv *priv)
{
	u16 dvstctr = usbhs_read(priv, DVSTCTR);

	switch (RHST & dvstctr) {
	case RHST_LOW_SPEED:
		return USB_SPEED_LOW;
	case RHST_FULL_SPEED:
		return USB_SPEED_FULL;
	case RHST_HIGH_SPEED:
		return USB_SPEED_HIGH;
	}

	return USB_SPEED_UNKNOWN;
}

222 223 224 225 226 227 228 229 230 231 232 233 234 235
int usbhs_vbus_ctrl(struct usbhs_priv *priv, int enable)
{
	struct platform_device *pdev = usbhs_priv_to_pdev(priv);

	return usbhs_platform_call(priv, set_vbus, pdev, enable);
}

static void usbhsc_bus_init(struct usbhs_priv *priv)
{
	usbhs_write(priv, DVSTCTR, 0);

	usbhs_vbus_ctrl(priv, 0);
}

236 237 238
/*
 *		device configuration
 */
239
int usbhs_set_device_config(struct usbhs_priv *priv, int devnum,
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
			   u16 upphub, u16 hubport, u16 speed)
{
	struct device *dev = usbhs_priv_to_dev(priv);
	u16 usbspd = 0;
	u32 reg = DEVADD0 + (2 * devnum);

	if (devnum > 10) {
		dev_err(dev, "cannot set speed to unknown device %d\n", devnum);
		return -EIO;
	}

	if (upphub > 0xA) {
		dev_err(dev, "unsupported hub number %d\n", upphub);
		return -EIO;
	}

	switch (speed) {
	case USB_SPEED_LOW:
		usbspd = USBSPD_SPEED_LOW;
		break;
	case USB_SPEED_FULL:
		usbspd = USBSPD_SPEED_FULL;
		break;
	case USB_SPEED_HIGH:
		usbspd = USBSPD_SPEED_HIGH;
		break;
	default:
		dev_err(dev, "unsupported speed %d\n", speed);
		return -EIO;
	}

	usbhs_write(priv, reg,	UPPHUB(upphub)	|
				HUBPORT(hubport)|
				USBSPD(usbspd));

	return 0;
}

278 279 280 281 282 283 284 285 286 287
/*
 *		interrupt functions
 */
void usbhs_xxxsts_clear(struct usbhs_priv *priv, u16 sts_reg, u16 bit)
{
	u16 pipe_mask = (u16)GENMASK(usbhs_get_dparam(priv, pipe_size), 0);

	usbhs_write(priv, sts_reg, ~(1 << bit) & pipe_mask);
}

288 289 290
/*
 *		local functions
 */
291
static void usbhsc_set_buswait(struct usbhs_priv *priv)
292 293 294
{
	int wait = usbhs_get_dparam(priv, buswait_bwait);

295 296 297
	/* set bus wait if platform have */
	if (wait)
		usbhs_bset(priv, BUSWAIT, 0x000F, wait);
298 299 300 301 302
}

/*
 *		platform default param
 */
303 304

/* commonly used on old SH-Mobile SoCs */
305 306 307 308 309 310 311 312 313 314 315
static struct renesas_usbhs_driver_pipe_config usbhsc_default_pipe[] = {
	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_CONTROL, 64, 0x00, false),
	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x08, false),
	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x18, false),
	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x28, true),
	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x38, true),
	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x48, true),
	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x04, false),
	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x05, false),
	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x06, false),
	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x07, false),
316 317
};

318
/* commonly used on newer SH-Mobile and R-Car SoCs */
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
static struct renesas_usbhs_driver_pipe_config usbhsc_new_pipe[] = {
	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_CONTROL, 64, 0x00, false),
	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x08, true),
	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x28, true),
	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x48, true),
	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x58, true),
	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x68, true),
	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x04, false),
	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x05, false),
	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x06, false),
	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x78, true),
	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x88, true),
	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x98, true),
	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xa8, true),
	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xb8, true),
	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xc8, true),
	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xd8, true),
336 337
};

338
/*
339 340 341 342
 *		power control
 */
static void usbhsc_power_ctrl(struct usbhs_priv *priv, int enable)
{
343
	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
344 345 346 347 348 349
	struct device *dev = usbhs_priv_to_dev(priv);

	if (enable) {
		/* enable PM */
		pm_runtime_get_sync(dev);

350 351 352
		/* enable platform power */
		usbhs_platform_call(priv, power_ctrl, pdev, priv->base, enable);

353 354 355 356 357 358
		/* USB on */
		usbhs_sys_clock_ctrl(priv, enable);
	} else {
		/* USB off */
		usbhs_sys_clock_ctrl(priv, enable);

359 360 361
		/* disable platform power */
		usbhs_platform_call(priv, power_ctrl, pdev, priv->base, enable);

362 363 364 365 366 367
		/* disable PM */
		pm_runtime_put_sync(dev);
	}
}

/*
368
 *		hotplug
369
 */
370
static void usbhsc_hotplug(struct usbhs_priv *priv)
371 372 373 374 375
{
	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
	struct usbhs_mod *mod = usbhs_mod_get_current(priv);
	int id;
	int enable;
376
	int cable;
377 378 379 380 381 382 383 384 385 386 387 388 389
	int ret;

	/*
	 * get vbus status from platform
	 */
	enable = usbhs_platform_call(priv, get_vbus, pdev);

	/*
	 * get id from platform
	 */
	id = usbhs_platform_call(priv, get_id, pdev);

	if (enable && !mod) {
390
		if (priv->edev) {
391
			cable = extcon_get_cable_state_(priv->edev, EXTCON_USB_HOST);
392 393 394 395 396 397 398 399
			if ((cable > 0 && id != USBHS_HOST) ||
			    (!cable && id != USBHS_GADGET)) {
				dev_info(&pdev->dev,
					 "USB cable plugged in doesn't match the selected role!\n");
				return;
			}
		}

400 401 402 403 404 405
		ret = usbhs_mod_change(priv, id);
		if (ret < 0)
			return;

		dev_dbg(&pdev->dev, "%s enable\n", __func__);

406
		/* power on */
407 408
		if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
			usbhsc_power_ctrl(priv, enable);
409

410 411 412 413
		/* bus init */
		usbhsc_set_buswait(priv);
		usbhsc_bus_init(priv);

414 415 416 417 418 419 420 421 422
		/* module start */
		usbhs_mod_call(priv, start, priv);

	} else if (!enable && mod) {
		dev_dbg(&pdev->dev, "%s disable\n", __func__);

		/* module stop */
		usbhs_mod_call(priv, stop, priv);

423 424 425
		/* bus init */
		usbhsc_bus_init(priv);

426
		/* power off */
427 428
		if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
			usbhsc_power_ctrl(priv, enable);
429 430 431 432 433 434 435 436

		usbhs_mod_change(priv, -1);

		/* reset phy for next connection */
		usbhs_platform_call(priv, phy_reset, pdev);
	}
}

437 438 439 440 441 442 443 444 445 446 447
/*
 *		notify hotplug
 */
static void usbhsc_notify_hotplug(struct work_struct *work)
{
	struct usbhs_priv *priv = container_of(work,
					       struct usbhs_priv,
					       notify_hotplug_work.work);
	usbhsc_hotplug(priv);
}

448
static int usbhsc_drvcllbck_notify_hotplug(struct platform_device *pdev)
449
{
450
	struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
451
	int delay = usbhs_get_dparam(priv, detection_delay);
452 453 454 455 456 457

	/*
	 * This functions will be called in interrupt.
	 * To make sure safety context,
	 * use workqueue for usbhs_notify_hotplug
	 */
458 459
	schedule_delayed_work(&priv->notify_hotplug_work,
			      msecs_to_jiffies(delay));
460 461 462 463 464 465
	return 0;
}

/*
 *		platform functions
 */
466 467 468
static const struct of_device_id usbhs_of_match[] = {
	{
		.compatible = "renesas,usbhs-r8a7790",
469
		.data = (void *)USBHS_TYPE_RCAR_GEN2,
470 471 472
	},
	{
		.compatible = "renesas,usbhs-r8a7791",
473
		.data = (void *)USBHS_TYPE_RCAR_GEN2,
474
	},
475 476 477 478
	{
		.compatible = "renesas,usbhs-r8a7794",
		.data = (void *)USBHS_TYPE_RCAR_GEN2,
	},
479 480 481 482 483
	{
		/* Gen3 is compatible with Gen2 */
		.compatible = "renesas,usbhs-r8a7795",
		.data = (void *)USBHS_TYPE_RCAR_GEN2,
	},
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
	{ },
};
MODULE_DEVICE_TABLE(of, usbhs_of_match);

static struct renesas_usbhs_platform_info *usbhs_parse_dt(struct device *dev)
{
	struct renesas_usbhs_platform_info *info;
	struct renesas_usbhs_driver_param *dparam;
	const struct of_device_id *of_id = of_match_device(usbhs_of_match, dev);
	u32 tmp;
	int gpio;

	info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
	if (!info)
		return NULL;

	dparam = &info->driver_param;
501
	dparam->type = of_id ? (uintptr_t)of_id->data : 0;
502 503 504 505 506 507 508
	if (!of_property_read_u32(dev->of_node, "renesas,buswait", &tmp))
		dparam->buswait_bwait = tmp;
	gpio = of_get_named_gpio_flags(dev->of_node, "renesas,enable-gpio", 0,
				       NULL);
	if (gpio > 0)
		dparam->enable_gpio = gpio;

509
	if (dparam->type == USBHS_TYPE_RCAR_GEN2)
510 511
		dparam->has_usb_dmac = 1;

512 513 514
	return info;
}

515
static int usbhs_probe(struct platform_device *pdev)
516
{
517
	struct renesas_usbhs_platform_info *info = dev_get_platdata(&pdev->dev);
518 519
	struct renesas_usbhs_driver_callback *dfunc;
	struct usbhs_priv *priv;
520
	struct resource *res, *irq_res;
521 522
	int ret;

523 524 525 526
	/* check device node */
	if (pdev->dev.of_node)
		info = pdev->dev.platform_data = usbhs_parse_dt(&pdev->dev);

527
	/* check platform information */
528
	if (!info) {
529 530 531 532 533
		dev_err(&pdev->dev, "no platform information\n");
		return -EINVAL;
	}

	/* platform data */
534
	irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
535
	if (!irq_res) {
536 537 538 539 540
		dev_err(&pdev->dev, "Not enough Renesas USB platform resources.\n");
		return -ENODEV;
	}

	/* usb private data */
541
	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
542
	if (!priv)
543 544
		return -ENOMEM;

545
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
546 547 548
	priv->base = devm_ioremap_resource(&pdev->dev, res);
	if (IS_ERR(priv->base))
		return PTR_ERR(priv->base);
549

550 551 552 553 554 555
	if (of_property_read_bool(pdev->dev.of_node, "extcon")) {
		priv->edev = extcon_get_edev_by_phandle(&pdev->dev, 0);
		if (IS_ERR(priv->edev))
			return PTR_ERR(priv->edev);
	}

556 557 558
	/*
	 * care platform info
	 */
559

560 561 562
	memcpy(&priv->dparam,
	       &info->driver_param,
	       sizeof(struct renesas_usbhs_driver_param));
563

564
	switch (priv->dparam.type) {
565
	case USBHS_TYPE_RCAR_GEN2:
566
		priv->pfunc = usbhs_rcar2_ops;
567 568 569
		if (!priv->dparam.pipe_configs) {
			priv->dparam.pipe_configs = usbhsc_new_pipe;
			priv->dparam.pipe_size = ARRAY_SIZE(usbhsc_new_pipe);
570 571 572 573 574 575 576 577 578 579 580 581 582
		}
		break;
	default:
		if (!info->platform_callback.get_id) {
			dev_err(&pdev->dev, "no platform callbacks");
			return -EINVAL;
		}
		memcpy(&priv->pfunc,
		       &info->platform_callback,
		       sizeof(struct renesas_usbhs_platform_callback));
		break;
	}

583 584 585 586 587
	/* set driver callback functions for platform */
	dfunc			= &info->driver_callback;
	dfunc->notify_hotplug	= usbhsc_drvcllbck_notify_hotplug;

	/* set default param if platform doesn't have */
588 589 590
	if (!priv->dparam.pipe_configs) {
		priv->dparam.pipe_configs = usbhsc_default_pipe;
		priv->dparam.pipe_size = ARRAY_SIZE(usbhsc_default_pipe);
591
	}
592 593
	if (!priv->dparam.pio_dma_border)
		priv->dparam.pio_dma_border = 64; /* 64byte */
594

595 596
	/* FIXME */
	/* runtime power control ? */
597
	if (priv->pfunc.get_vbus)
598 599
		usbhsc_flags_set(priv, USBHSF_RUNTIME_PWCTRL);

600 601 602
	/*
	 * priv settings
	 */
603 604 605
	priv->irq	= irq_res->start;
	if (irq_res->flags & IORESOURCE_IRQ_SHAREABLE)
		priv->irqflags = IRQF_SHARED;
606
	priv->pdev	= pdev;
607
	INIT_DELAYED_WORK(&priv->notify_hotplug_work, usbhsc_notify_hotplug);
608 609 610 611 612
	spin_lock_init(usbhs_priv_to_lock(priv));

	/* call pipe and module init */
	ret = usbhs_pipe_probe(priv);
	if (ret < 0)
613
		return ret;
614

615
	ret = usbhs_fifo_probe(priv);
616
	if (ret < 0)
617
		goto probe_end_pipe_exit;
618

619 620 621 622
	ret = usbhs_mod_probe(priv);
	if (ret < 0)
		goto probe_end_fifo_exit;

623
	/* dev_set_drvdata should be called after usbhs_mod_init */
624
	platform_set_drvdata(pdev, priv);
625 626 627 628 629 630 631

	/*
	 * deviece reset here because
	 * USB device might be used in boot loader.
	 */
	usbhs_sys_clock_ctrl(priv, 0);

632 633 634 635 636 637 638 639 640 641 642 643 644 645
	/* check GPIO determining if USB function should be enabled */
	if (priv->dparam.enable_gpio) {
		gpio_request_one(priv->dparam.enable_gpio, GPIOF_IN, NULL);
		ret = !gpio_get_value(priv->dparam.enable_gpio);
		gpio_free(priv->dparam.enable_gpio);
		if (ret) {
			dev_warn(&pdev->dev,
				 "USB function not selected (GPIO %d)\n",
				 priv->dparam.enable_gpio);
			ret = -ENOTSUPP;
			goto probe_end_mod_exit;
		}
	}

646 647 648 649 650 651 652 653 654
	/*
	 * platform call
	 *
	 * USB phy setup might depend on CPU/Board.
	 * If platform has its callback functions,
	 * call it here.
	 */
	ret = usbhs_platform_call(priv, hardware_init, pdev);
	if (ret < 0) {
655
		dev_err(&pdev->dev, "platform init failed.\n");
656
		goto probe_end_mod_exit;
657 658 659 660 661
	}

	/* reset phy for connection */
	usbhs_platform_call(priv, phy_reset, pdev);

662 663 664 665 666 667 668
	/* power control */
	pm_runtime_enable(&pdev->dev);
	if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) {
		usbhsc_power_ctrl(priv, 1);
		usbhs_mod_autonomy_mode(priv);
	}

669 670 671
	/*
	 * manual call notify_hotplug for cold plug
	 */
672
	usbhsc_drvcllbck_notify_hotplug(pdev);
673 674 675 676 677 678 679

	dev_info(&pdev->dev, "probed\n");

	return ret;

probe_end_mod_exit:
	usbhs_mod_remove(priv);
680 681
probe_end_fifo_exit:
	usbhs_fifo_remove(priv);
682 683
probe_end_pipe_exit:
	usbhs_pipe_remove(priv);
684 685 686 687 688 689

	dev_info(&pdev->dev, "probe failed\n");

	return ret;
}

B
Bill Pemberton 已提交
690
static int usbhs_remove(struct platform_device *pdev)
691
{
692
	struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
693
	struct renesas_usbhs_platform_info *info = dev_get_platdata(&pdev->dev);
694
	struct renesas_usbhs_driver_callback *dfunc = &info->driver_callback;
695 696 697

	dev_dbg(&pdev->dev, "usb remove\n");

698 699
	dfunc->notify_hotplug = NULL;

700 701 702
	/* power off */
	if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
		usbhsc_power_ctrl(priv, 0);
703

704
	pm_runtime_disable(&pdev->dev);
705 706 707

	usbhs_platform_call(priv, hardware_exit, pdev);
	usbhs_mod_remove(priv);
708
	usbhs_fifo_remove(priv);
709
	usbhs_pipe_remove(priv);
710 711 712 713

	return 0;
}

714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
static int usbhsc_suspend(struct device *dev)
{
	struct usbhs_priv *priv = dev_get_drvdata(dev);
	struct usbhs_mod *mod = usbhs_mod_get_current(priv);

	if (mod) {
		usbhs_mod_call(priv, stop, priv);
		usbhs_mod_change(priv, -1);
	}

	if (mod || !usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
		usbhsc_power_ctrl(priv, 0);

	return 0;
}

static int usbhsc_resume(struct device *dev)
{
	struct usbhs_priv *priv = dev_get_drvdata(dev);
	struct platform_device *pdev = usbhs_priv_to_pdev(priv);

	if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
		usbhsc_power_ctrl(priv, 1);

738 739 740
	usbhs_platform_call(priv, phy_reset, pdev);

	usbhsc_drvcllbck_notify_hotplug(pdev);
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763

	return 0;
}

static int usbhsc_runtime_nop(struct device *dev)
{
	/* Runtime PM callback shared between ->runtime_suspend()
	 * and ->runtime_resume(). Simply returns success.
	 *
	 * This driver re-initializes all registers after
	 * pm_runtime_get_sync() anyway so there is no need
	 * to save and restore registers here.
	 */
	return 0;
}

static const struct dev_pm_ops usbhsc_pm_ops = {
	.suspend		= usbhsc_suspend,
	.resume			= usbhsc_resume,
	.runtime_suspend	= usbhsc_runtime_nop,
	.runtime_resume		= usbhsc_runtime_nop,
};

764 765 766
static struct platform_driver renesas_usbhs_driver = {
	.driver		= {
		.name	= "renesas_usbhs",
767
		.pm	= &usbhsc_pm_ops,
768
		.of_match_table = of_match_ptr(usbhs_of_match),
769 770
	},
	.probe		= usbhs_probe,
B
Bill Pemberton 已提交
771
	.remove		= usbhs_remove,
772 773
};

774
module_platform_driver(renesas_usbhs_driver);
775 776 777 778

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Renesas USB driver");
MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");