common.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  1. // SPDX-License-Identifier: GPL-1.0+
  2. /*
  3. * Renesas USB driver
  4. *
  5. * Copyright (C) 2011 Renesas Solutions Corp.
  6. * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
  7. */
  8. #include <linux/err.h>
  9. #include <linux/gpio.h>
  10. #include <linux/io.h>
  11. #include <linux/module.h>
  12. #include <linux/of_device.h>
  13. #include <linux/of_gpio.h>
  14. #include <linux/pm_runtime.h>
  15. #include <linux/slab.h>
  16. #include <linux/sysfs.h>
  17. #include "common.h"
  18. #include "rcar2.h"
  19. #include "rcar3.h"
  20. #include "rza.h"
  21. /*
  22. * image of renesas_usbhs
  23. *
  24. * ex) gadget case
  25. * mod.c
  26. * mod_gadget.c
  27. * mod_host.c pipe.c fifo.c
  28. *
  29. * +-------+ +-----------+
  30. * | pipe0 |------>| fifo pio |
  31. * +------------+ +-------+ +-----------+
  32. * | mod_gadget |=====> | pipe1 |--+
  33. * +------------+ +-------+ | +-----------+
  34. * | pipe2 | | +-| fifo dma0 |
  35. * +------------+ +-------+ | | +-----------+
  36. * | mod_host | | pipe3 |<-|--+
  37. * +------------+ +-------+ | +-----------+
  38. * | .... | +--->| fifo dma1 |
  39. * | .... | +-----------+
  40. */
  41. #define USBHSF_RUNTIME_PWCTRL (1 << 0)
  42. /* status */
  43. #define usbhsc_flags_init(p) do {(p)->flags = 0; } while (0)
  44. #define usbhsc_flags_set(p, b) ((p)->flags |= (b))
  45. #define usbhsc_flags_clr(p, b) ((p)->flags &= ~(b))
  46. #define usbhsc_flags_has(p, b) ((p)->flags & (b))
  47. /*
  48. * platform call back
  49. *
  50. * renesas usb support platform callback function.
  51. * Below macro call it.
  52. * if platform doesn't have callback, it return 0 (no error)
  53. */
  54. #define usbhs_platform_call(priv, func, args...)\
  55. (!(priv) ? -ENODEV : \
  56. !((priv)->pfunc.func) ? 0 : \
  57. (priv)->pfunc.func(args))
  58. /*
  59. * common functions
  60. */
  61. u16 usbhs_read(struct usbhs_priv *priv, u32 reg)
  62. {
  63. return ioread16(priv->base + reg);
  64. }
  65. void usbhs_write(struct usbhs_priv *priv, u32 reg, u16 data)
  66. {
  67. iowrite16(data, priv->base + reg);
  68. }
  69. void usbhs_bset(struct usbhs_priv *priv, u32 reg, u16 mask, u16 data)
  70. {
  71. u16 val = usbhs_read(priv, reg);
  72. val &= ~mask;
  73. val |= data & mask;
  74. usbhs_write(priv, reg, val);
  75. }
  76. struct usbhs_priv *usbhs_pdev_to_priv(struct platform_device *pdev)
  77. {
  78. return dev_get_drvdata(&pdev->dev);
  79. }
  80. /*
  81. * syscfg functions
  82. */
  83. static void usbhs_sys_clock_ctrl(struct usbhs_priv *priv, int enable)
  84. {
  85. usbhs_bset(priv, SYSCFG, SCKE, enable ? SCKE : 0);
  86. }
  87. void usbhs_sys_host_ctrl(struct usbhs_priv *priv, int enable)
  88. {
  89. u16 mask = DCFM | DRPD | DPRPU | HSE | USBE;
  90. u16 val = DCFM | DRPD | HSE | USBE;
  91. int has_otg = usbhs_get_dparam(priv, has_otg);
  92. if (has_otg)
  93. usbhs_bset(priv, DVSTCTR, (EXTLP | PWEN), (EXTLP | PWEN));
  94. /*
  95. * if enable
  96. *
  97. * - select Host mode
  98. * - D+ Line/D- Line Pull-down
  99. */
  100. usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
  101. }
  102. void usbhs_sys_function_ctrl(struct usbhs_priv *priv, int enable)
  103. {
  104. u16 mask = DCFM | DRPD | DPRPU | HSE | USBE;
  105. u16 val = HSE | USBE;
  106. /*
  107. * if enable
  108. *
  109. * - select Function mode
  110. * - D+ Line Pull-up is disabled
  111. * When D+ Line Pull-up is enabled,
  112. * calling usbhs_sys_function_pullup(,1)
  113. */
  114. usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
  115. }
  116. void usbhs_sys_function_pullup(struct usbhs_priv *priv, int enable)
  117. {
  118. usbhs_bset(priv, SYSCFG, DPRPU, enable ? DPRPU : 0);
  119. }
  120. void usbhs_sys_set_test_mode(struct usbhs_priv *priv, u16 mode)
  121. {
  122. usbhs_write(priv, TESTMODE, mode);
  123. }
  124. /*
  125. * frame functions
  126. */
  127. int usbhs_frame_get_num(struct usbhs_priv *priv)
  128. {
  129. return usbhs_read(priv, FRMNUM) & FRNM_MASK;
  130. }
  131. /*
  132. * usb request functions
  133. */
  134. void usbhs_usbreq_get_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
  135. {
  136. u16 val;
  137. val = usbhs_read(priv, USBREQ);
  138. req->bRequest = (val >> 8) & 0xFF;
  139. req->bRequestType = (val >> 0) & 0xFF;
  140. req->wValue = usbhs_read(priv, USBVAL);
  141. req->wIndex = usbhs_read(priv, USBINDX);
  142. req->wLength = usbhs_read(priv, USBLENG);
  143. }
  144. void usbhs_usbreq_set_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
  145. {
  146. usbhs_write(priv, USBREQ, (req->bRequest << 8) | req->bRequestType);
  147. usbhs_write(priv, USBVAL, req->wValue);
  148. usbhs_write(priv, USBINDX, req->wIndex);
  149. usbhs_write(priv, USBLENG, req->wLength);
  150. usbhs_bset(priv, DCPCTR, SUREQ, SUREQ);
  151. }
  152. /*
  153. * bus/vbus functions
  154. */
  155. void usbhs_bus_send_sof_enable(struct usbhs_priv *priv)
  156. {
  157. u16 status = usbhs_read(priv, DVSTCTR) & (USBRST | UACT);
  158. if (status != USBRST) {
  159. struct device *dev = usbhs_priv_to_dev(priv);
  160. dev_err(dev, "usbhs should be reset\n");
  161. }
  162. usbhs_bset(priv, DVSTCTR, (USBRST | UACT), UACT);
  163. }
  164. void usbhs_bus_send_reset(struct usbhs_priv *priv)
  165. {
  166. usbhs_bset(priv, DVSTCTR, (USBRST | UACT), USBRST);
  167. }
  168. int usbhs_bus_get_speed(struct usbhs_priv *priv)
  169. {
  170. u16 dvstctr = usbhs_read(priv, DVSTCTR);
  171. switch (RHST & dvstctr) {
  172. case RHST_LOW_SPEED:
  173. return USB_SPEED_LOW;
  174. case RHST_FULL_SPEED:
  175. return USB_SPEED_FULL;
  176. case RHST_HIGH_SPEED:
  177. return USB_SPEED_HIGH;
  178. }
  179. return USB_SPEED_UNKNOWN;
  180. }
  181. int usbhs_vbus_ctrl(struct usbhs_priv *priv, int enable)
  182. {
  183. struct platform_device *pdev = usbhs_priv_to_pdev(priv);
  184. return usbhs_platform_call(priv, set_vbus, pdev, enable);
  185. }
  186. static void usbhsc_bus_init(struct usbhs_priv *priv)
  187. {
  188. usbhs_write(priv, DVSTCTR, 0);
  189. usbhs_vbus_ctrl(priv, 0);
  190. }
  191. /*
  192. * device configuration
  193. */
  194. int usbhs_set_device_config(struct usbhs_priv *priv, int devnum,
  195. u16 upphub, u16 hubport, u16 speed)
  196. {
  197. struct device *dev = usbhs_priv_to_dev(priv);
  198. u16 usbspd = 0;
  199. u32 reg = DEVADD0 + (2 * devnum);
  200. if (devnum > 10) {
  201. dev_err(dev, "cannot set speed to unknown device %d\n", devnum);
  202. return -EIO;
  203. }
  204. if (upphub > 0xA) {
  205. dev_err(dev, "unsupported hub number %d\n", upphub);
  206. return -EIO;
  207. }
  208. switch (speed) {
  209. case USB_SPEED_LOW:
  210. usbspd = USBSPD_SPEED_LOW;
  211. break;
  212. case USB_SPEED_FULL:
  213. usbspd = USBSPD_SPEED_FULL;
  214. break;
  215. case USB_SPEED_HIGH:
  216. usbspd = USBSPD_SPEED_HIGH;
  217. break;
  218. default:
  219. dev_err(dev, "unsupported speed %d\n", speed);
  220. return -EIO;
  221. }
  222. usbhs_write(priv, reg, UPPHUB(upphub) |
  223. HUBPORT(hubport)|
  224. USBSPD(usbspd));
  225. return 0;
  226. }
  227. /*
  228. * interrupt functions
  229. */
  230. void usbhs_xxxsts_clear(struct usbhs_priv *priv, u16 sts_reg, u16 bit)
  231. {
  232. u16 pipe_mask = (u16)GENMASK(usbhs_get_dparam(priv, pipe_size), 0);
  233. usbhs_write(priv, sts_reg, ~(1 << bit) & pipe_mask);
  234. }
  235. /*
  236. * local functions
  237. */
  238. static void usbhsc_set_buswait(struct usbhs_priv *priv)
  239. {
  240. int wait = usbhs_get_dparam(priv, buswait_bwait);
  241. /* set bus wait if platform have */
  242. if (wait)
  243. usbhs_bset(priv, BUSWAIT, 0x000F, wait);
  244. }
  245. /*
  246. * platform default param
  247. */
  248. /* commonly used on old SH-Mobile SoCs */
  249. static struct renesas_usbhs_driver_pipe_config usbhsc_default_pipe[] = {
  250. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_CONTROL, 64, 0x00, false),
  251. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x08, false),
  252. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x18, false),
  253. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x28, true),
  254. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x38, true),
  255. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x48, true),
  256. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x04, false),
  257. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x05, false),
  258. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x06, false),
  259. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x07, false),
  260. };
  261. /* commonly used on newer SH-Mobile and R-Car SoCs */
  262. static struct renesas_usbhs_driver_pipe_config usbhsc_new_pipe[] = {
  263. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_CONTROL, 64, 0x00, false),
  264. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x08, true),
  265. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x28, true),
  266. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x48, true),
  267. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x58, true),
  268. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x68, true),
  269. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x04, false),
  270. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x05, false),
  271. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x06, false),
  272. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x78, true),
  273. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x88, true),
  274. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x98, true),
  275. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xa8, true),
  276. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xb8, true),
  277. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xc8, true),
  278. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xd8, true),
  279. };
  280. /*
  281. * power control
  282. */
  283. static void usbhsc_power_ctrl(struct usbhs_priv *priv, int enable)
  284. {
  285. struct platform_device *pdev = usbhs_priv_to_pdev(priv);
  286. struct device *dev = usbhs_priv_to_dev(priv);
  287. if (enable) {
  288. /* enable PM */
  289. pm_runtime_get_sync(dev);
  290. /* enable platform power */
  291. usbhs_platform_call(priv, power_ctrl, pdev, priv->base, enable);
  292. /* USB on */
  293. usbhs_sys_clock_ctrl(priv, enable);
  294. } else {
  295. /* USB off */
  296. usbhs_sys_clock_ctrl(priv, enable);
  297. /* disable platform power */
  298. usbhs_platform_call(priv, power_ctrl, pdev, priv->base, enable);
  299. /* disable PM */
  300. pm_runtime_put_sync(dev);
  301. }
  302. }
  303. /*
  304. * hotplug
  305. */
  306. static void usbhsc_hotplug(struct usbhs_priv *priv)
  307. {
  308. struct platform_device *pdev = usbhs_priv_to_pdev(priv);
  309. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  310. int id;
  311. int enable;
  312. int cable;
  313. int ret;
  314. /*
  315. * get vbus status from platform
  316. */
  317. enable = usbhs_platform_call(priv, get_vbus, pdev);
  318. /*
  319. * get id from platform
  320. */
  321. id = usbhs_platform_call(priv, get_id, pdev);
  322. if (enable && !mod) {
  323. if (priv->edev) {
  324. cable = extcon_get_state(priv->edev, EXTCON_USB_HOST);
  325. if ((cable > 0 && id != USBHS_HOST) ||
  326. (!cable && id != USBHS_GADGET)) {
  327. dev_info(&pdev->dev,
  328. "USB cable plugged in doesn't match the selected role!\n");
  329. return;
  330. }
  331. }
  332. ret = usbhs_mod_change(priv, id);
  333. if (ret < 0)
  334. return;
  335. dev_dbg(&pdev->dev, "%s enable\n", __func__);
  336. /* power on */
  337. if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
  338. usbhsc_power_ctrl(priv, enable);
  339. /* bus init */
  340. usbhsc_set_buswait(priv);
  341. usbhsc_bus_init(priv);
  342. /* module start */
  343. usbhs_mod_call(priv, start, priv);
  344. } else if (!enable && mod) {
  345. dev_dbg(&pdev->dev, "%s disable\n", __func__);
  346. /* module stop */
  347. usbhs_mod_call(priv, stop, priv);
  348. /* bus init */
  349. usbhsc_bus_init(priv);
  350. /* power off */
  351. if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
  352. usbhsc_power_ctrl(priv, enable);
  353. usbhs_mod_change(priv, -1);
  354. /* reset phy for next connection */
  355. usbhs_platform_call(priv, phy_reset, pdev);
  356. }
  357. }
  358. /*
  359. * notify hotplug
  360. */
  361. static void usbhsc_notify_hotplug(struct work_struct *work)
  362. {
  363. struct usbhs_priv *priv = container_of(work,
  364. struct usbhs_priv,
  365. notify_hotplug_work.work);
  366. usbhsc_hotplug(priv);
  367. }
  368. static int usbhsc_drvcllbck_notify_hotplug(struct platform_device *pdev)
  369. {
  370. struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
  371. int delay = usbhs_get_dparam(priv, detection_delay);
  372. /*
  373. * This functions will be called in interrupt.
  374. * To make sure safety context,
  375. * use workqueue for usbhs_notify_hotplug
  376. */
  377. schedule_delayed_work(&priv->notify_hotplug_work,
  378. msecs_to_jiffies(delay));
  379. return 0;
  380. }
  381. /*
  382. * platform functions
  383. */
  384. static const struct of_device_id usbhs_of_match[] = {
  385. {
  386. .compatible = "renesas,usbhs-r8a774c0",
  387. .data = (void *)USBHS_TYPE_RCAR_GEN3_WITH_PLL,
  388. },
  389. {
  390. .compatible = "renesas,usbhs-r8a7790",
  391. .data = (void *)USBHS_TYPE_RCAR_GEN2,
  392. },
  393. {
  394. .compatible = "renesas,usbhs-r8a7791",
  395. .data = (void *)USBHS_TYPE_RCAR_GEN2,
  396. },
  397. {
  398. .compatible = "renesas,usbhs-r8a7794",
  399. .data = (void *)USBHS_TYPE_RCAR_GEN2,
  400. },
  401. {
  402. .compatible = "renesas,usbhs-r8a7795",
  403. .data = (void *)USBHS_TYPE_RCAR_GEN3,
  404. },
  405. {
  406. .compatible = "renesas,usbhs-r8a7796",
  407. .data = (void *)USBHS_TYPE_RCAR_GEN3,
  408. },
  409. {
  410. .compatible = "renesas,usbhs-r8a77995",
  411. .data = (void *)USBHS_TYPE_RCAR_GEN3_WITH_PLL,
  412. },
  413. {
  414. .compatible = "renesas,rcar-gen2-usbhs",
  415. .data = (void *)USBHS_TYPE_RCAR_GEN2,
  416. },
  417. {
  418. .compatible = "renesas,rcar-gen3-usbhs",
  419. .data = (void *)USBHS_TYPE_RCAR_GEN3,
  420. },
  421. {
  422. .compatible = "renesas,rza1-usbhs",
  423. .data = (void *)USBHS_TYPE_RZA1,
  424. },
  425. { },
  426. };
  427. MODULE_DEVICE_TABLE(of, usbhs_of_match);
  428. static struct renesas_usbhs_platform_info *usbhs_parse_dt(struct device *dev)
  429. {
  430. struct renesas_usbhs_platform_info *info;
  431. struct renesas_usbhs_driver_param *dparam;
  432. u32 tmp;
  433. int gpio;
  434. info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
  435. if (!info)
  436. return NULL;
  437. dparam = &info->driver_param;
  438. dparam->type = (uintptr_t)of_device_get_match_data(dev);
  439. if (!of_property_read_u32(dev->of_node, "renesas,buswait", &tmp))
  440. dparam->buswait_bwait = tmp;
  441. gpio = of_get_named_gpio_flags(dev->of_node, "renesas,enable-gpio", 0,
  442. NULL);
  443. if (gpio > 0)
  444. dparam->enable_gpio = gpio;
  445. if (dparam->type == USBHS_TYPE_RCAR_GEN2 ||
  446. dparam->type == USBHS_TYPE_RCAR_GEN3 ||
  447. dparam->type == USBHS_TYPE_RCAR_GEN3_WITH_PLL) {
  448. dparam->has_usb_dmac = 1;
  449. dparam->pipe_configs = usbhsc_new_pipe;
  450. dparam->pipe_size = ARRAY_SIZE(usbhsc_new_pipe);
  451. }
  452. if (dparam->type == USBHS_TYPE_RZA1) {
  453. dparam->pipe_configs = usbhsc_new_pipe;
  454. dparam->pipe_size = ARRAY_SIZE(usbhsc_new_pipe);
  455. }
  456. return info;
  457. }
  458. static int usbhs_probe(struct platform_device *pdev)
  459. {
  460. struct renesas_usbhs_platform_info *info = renesas_usbhs_get_info(pdev);
  461. struct renesas_usbhs_driver_callback *dfunc;
  462. struct usbhs_priv *priv;
  463. struct resource *res, *irq_res;
  464. int ret;
  465. /* check device node */
  466. if (pdev->dev.of_node)
  467. info = pdev->dev.platform_data = usbhs_parse_dt(&pdev->dev);
  468. /* check platform information */
  469. if (!info) {
  470. dev_err(&pdev->dev, "no platform information\n");
  471. return -EINVAL;
  472. }
  473. /* platform data */
  474. irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  475. if (!irq_res) {
  476. dev_err(&pdev->dev, "Not enough Renesas USB platform resources.\n");
  477. return -ENODEV;
  478. }
  479. /* usb private data */
  480. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  481. if (!priv)
  482. return -ENOMEM;
  483. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  484. priv->base = devm_ioremap_resource(&pdev->dev, res);
  485. if (IS_ERR(priv->base))
  486. return PTR_ERR(priv->base);
  487. if (of_property_read_bool(pdev->dev.of_node, "extcon")) {
  488. priv->edev = extcon_get_edev_by_phandle(&pdev->dev, 0);
  489. if (IS_ERR(priv->edev))
  490. return PTR_ERR(priv->edev);
  491. }
  492. /*
  493. * care platform info
  494. */
  495. memcpy(&priv->dparam,
  496. &info->driver_param,
  497. sizeof(struct renesas_usbhs_driver_param));
  498. switch (priv->dparam.type) {
  499. case USBHS_TYPE_RCAR_GEN2:
  500. priv->pfunc = usbhs_rcar2_ops;
  501. break;
  502. case USBHS_TYPE_RCAR_GEN3:
  503. priv->pfunc = usbhs_rcar3_ops;
  504. break;
  505. case USBHS_TYPE_RCAR_GEN3_WITH_PLL:
  506. priv->pfunc = usbhs_rcar3_with_pll_ops;
  507. if (!IS_ERR_OR_NULL(priv->edev)) {
  508. priv->nb.notifier_call = priv->pfunc.notifier;
  509. ret = devm_extcon_register_notifier(&pdev->dev,
  510. priv->edev,
  511. EXTCON_USB_HOST,
  512. &priv->nb);
  513. if (ret < 0)
  514. dev_err(&pdev->dev, "no notifier registered\n");
  515. }
  516. break;
  517. case USBHS_TYPE_RZA1:
  518. priv->pfunc = usbhs_rza1_ops;
  519. break;
  520. default:
  521. if (!info->platform_callback.get_id) {
  522. dev_err(&pdev->dev, "no platform callbacks");
  523. return -EINVAL;
  524. }
  525. memcpy(&priv->pfunc,
  526. &info->platform_callback,
  527. sizeof(struct renesas_usbhs_platform_callback));
  528. break;
  529. }
  530. /* set driver callback functions for platform */
  531. dfunc = &info->driver_callback;
  532. dfunc->notify_hotplug = usbhsc_drvcllbck_notify_hotplug;
  533. /* set default param if platform doesn't have */
  534. if (!priv->dparam.pipe_configs) {
  535. priv->dparam.pipe_configs = usbhsc_default_pipe;
  536. priv->dparam.pipe_size = ARRAY_SIZE(usbhsc_default_pipe);
  537. }
  538. if (!priv->dparam.pio_dma_border)
  539. priv->dparam.pio_dma_border = 64; /* 64byte */
  540. /* FIXME */
  541. /* runtime power control ? */
  542. if (priv->pfunc.get_vbus)
  543. usbhsc_flags_set(priv, USBHSF_RUNTIME_PWCTRL);
  544. /*
  545. * priv settings
  546. */
  547. priv->irq = irq_res->start;
  548. if (irq_res->flags & IORESOURCE_IRQ_SHAREABLE)
  549. priv->irqflags = IRQF_SHARED;
  550. priv->pdev = pdev;
  551. INIT_DELAYED_WORK(&priv->notify_hotplug_work, usbhsc_notify_hotplug);
  552. spin_lock_init(usbhs_priv_to_lock(priv));
  553. /* call pipe and module init */
  554. ret = usbhs_pipe_probe(priv);
  555. if (ret < 0)
  556. return ret;
  557. ret = usbhs_fifo_probe(priv);
  558. if (ret < 0)
  559. goto probe_end_pipe_exit;
  560. ret = usbhs_mod_probe(priv);
  561. if (ret < 0)
  562. goto probe_end_fifo_exit;
  563. /* dev_set_drvdata should be called after usbhs_mod_init */
  564. platform_set_drvdata(pdev, priv);
  565. /*
  566. * deviece reset here because
  567. * USB device might be used in boot loader.
  568. */
  569. usbhs_sys_clock_ctrl(priv, 0);
  570. /* check GPIO determining if USB function should be enabled */
  571. if (priv->dparam.enable_gpio) {
  572. gpio_request_one(priv->dparam.enable_gpio, GPIOF_IN, NULL);
  573. ret = !gpio_get_value(priv->dparam.enable_gpio);
  574. gpio_free(priv->dparam.enable_gpio);
  575. if (ret) {
  576. dev_warn(&pdev->dev,
  577. "USB function not selected (GPIO %d)\n",
  578. priv->dparam.enable_gpio);
  579. ret = -ENOTSUPP;
  580. goto probe_end_mod_exit;
  581. }
  582. }
  583. /*
  584. * platform call
  585. *
  586. * USB phy setup might depend on CPU/Board.
  587. * If platform has its callback functions,
  588. * call it here.
  589. */
  590. ret = usbhs_platform_call(priv, hardware_init, pdev);
  591. if (ret < 0) {
  592. dev_err(&pdev->dev, "platform init failed.\n");
  593. goto probe_end_mod_exit;
  594. }
  595. /* reset phy for connection */
  596. usbhs_platform_call(priv, phy_reset, pdev);
  597. /* power control */
  598. pm_runtime_enable(&pdev->dev);
  599. if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) {
  600. usbhsc_power_ctrl(priv, 1);
  601. usbhs_mod_autonomy_mode(priv);
  602. }
  603. /*
  604. * manual call notify_hotplug for cold plug
  605. */
  606. usbhsc_drvcllbck_notify_hotplug(pdev);
  607. dev_info(&pdev->dev, "probed\n");
  608. return ret;
  609. probe_end_mod_exit:
  610. usbhs_mod_remove(priv);
  611. probe_end_fifo_exit:
  612. usbhs_fifo_remove(priv);
  613. probe_end_pipe_exit:
  614. usbhs_pipe_remove(priv);
  615. dev_info(&pdev->dev, "probe failed (%d)\n", ret);
  616. return ret;
  617. }
  618. static int usbhs_remove(struct platform_device *pdev)
  619. {
  620. struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
  621. struct renesas_usbhs_platform_info *info = renesas_usbhs_get_info(pdev);
  622. struct renesas_usbhs_driver_callback *dfunc = &info->driver_callback;
  623. dev_dbg(&pdev->dev, "usb remove\n");
  624. dfunc->notify_hotplug = NULL;
  625. /* power off */
  626. if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
  627. usbhsc_power_ctrl(priv, 0);
  628. pm_runtime_disable(&pdev->dev);
  629. usbhs_platform_call(priv, hardware_exit, pdev);
  630. usbhs_mod_remove(priv);
  631. usbhs_fifo_remove(priv);
  632. usbhs_pipe_remove(priv);
  633. return 0;
  634. }
  635. static int usbhsc_suspend(struct device *dev)
  636. {
  637. struct usbhs_priv *priv = dev_get_drvdata(dev);
  638. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  639. if (mod) {
  640. usbhs_mod_call(priv, stop, priv);
  641. usbhs_mod_change(priv, -1);
  642. }
  643. if (mod || !usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
  644. usbhsc_power_ctrl(priv, 0);
  645. return 0;
  646. }
  647. static int usbhsc_resume(struct device *dev)
  648. {
  649. struct usbhs_priv *priv = dev_get_drvdata(dev);
  650. struct platform_device *pdev = usbhs_priv_to_pdev(priv);
  651. if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) {
  652. usbhsc_power_ctrl(priv, 1);
  653. usbhs_mod_autonomy_mode(priv);
  654. }
  655. usbhs_platform_call(priv, phy_reset, pdev);
  656. usbhsc_drvcllbck_notify_hotplug(pdev);
  657. return 0;
  658. }
  659. static int usbhsc_runtime_nop(struct device *dev)
  660. {
  661. /* Runtime PM callback shared between ->runtime_suspend()
  662. * and ->runtime_resume(). Simply returns success.
  663. *
  664. * This driver re-initializes all registers after
  665. * pm_runtime_get_sync() anyway so there is no need
  666. * to save and restore registers here.
  667. */
  668. return 0;
  669. }
  670. static const struct dev_pm_ops usbhsc_pm_ops = {
  671. .suspend = usbhsc_suspend,
  672. .resume = usbhsc_resume,
  673. .runtime_suspend = usbhsc_runtime_nop,
  674. .runtime_resume = usbhsc_runtime_nop,
  675. };
  676. static struct platform_driver renesas_usbhs_driver = {
  677. .driver = {
  678. .name = "renesas_usbhs",
  679. .pm = &usbhsc_pm_ops,
  680. .of_match_table = of_match_ptr(usbhs_of_match),
  681. },
  682. .probe = usbhs_probe,
  683. .remove = usbhs_remove,
  684. };
  685. module_platform_driver(renesas_usbhs_driver);
  686. MODULE_LICENSE("GPL");
  687. MODULE_DESCRIPTION("Renesas USB driver");
  688. MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");