common.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  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-r8a7790",
  387. .data = (void *)USBHS_TYPE_RCAR_GEN2,
  388. },
  389. {
  390. .compatible = "renesas,usbhs-r8a7791",
  391. .data = (void *)USBHS_TYPE_RCAR_GEN2,
  392. },
  393. {
  394. .compatible = "renesas,usbhs-r8a7794",
  395. .data = (void *)USBHS_TYPE_RCAR_GEN2,
  396. },
  397. {
  398. .compatible = "renesas,usbhs-r8a7795",
  399. .data = (void *)USBHS_TYPE_RCAR_GEN3,
  400. },
  401. {
  402. .compatible = "renesas,usbhs-r8a7796",
  403. .data = (void *)USBHS_TYPE_RCAR_GEN3,
  404. },
  405. {
  406. .compatible = "renesas,usbhs-r8a77995",
  407. .data = (void *)USBHS_TYPE_RCAR_GEN3_WITH_PLL,
  408. },
  409. {
  410. .compatible = "renesas,rcar-gen2-usbhs",
  411. .data = (void *)USBHS_TYPE_RCAR_GEN2,
  412. },
  413. {
  414. .compatible = "renesas,rcar-gen3-usbhs",
  415. .data = (void *)USBHS_TYPE_RCAR_GEN3,
  416. },
  417. {
  418. .compatible = "renesas,rza1-usbhs",
  419. .data = (void *)USBHS_TYPE_RZA1,
  420. },
  421. { },
  422. };
  423. MODULE_DEVICE_TABLE(of, usbhs_of_match);
  424. static struct renesas_usbhs_platform_info *usbhs_parse_dt(struct device *dev)
  425. {
  426. struct renesas_usbhs_platform_info *info;
  427. struct renesas_usbhs_driver_param *dparam;
  428. u32 tmp;
  429. int gpio;
  430. info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
  431. if (!info)
  432. return NULL;
  433. dparam = &info->driver_param;
  434. dparam->type = (uintptr_t)of_device_get_match_data(dev);
  435. if (!of_property_read_u32(dev->of_node, "renesas,buswait", &tmp))
  436. dparam->buswait_bwait = tmp;
  437. gpio = of_get_named_gpio_flags(dev->of_node, "renesas,enable-gpio", 0,
  438. NULL);
  439. if (gpio > 0)
  440. dparam->enable_gpio = gpio;
  441. if (dparam->type == USBHS_TYPE_RCAR_GEN2 ||
  442. dparam->type == USBHS_TYPE_RCAR_GEN3 ||
  443. dparam->type == USBHS_TYPE_RCAR_GEN3_WITH_PLL) {
  444. dparam->has_usb_dmac = 1;
  445. dparam->pipe_configs = usbhsc_new_pipe;
  446. dparam->pipe_size = ARRAY_SIZE(usbhsc_new_pipe);
  447. }
  448. if (dparam->type == USBHS_TYPE_RZA1) {
  449. dparam->pipe_configs = usbhsc_new_pipe;
  450. dparam->pipe_size = ARRAY_SIZE(usbhsc_new_pipe);
  451. }
  452. return info;
  453. }
  454. static int usbhs_probe(struct platform_device *pdev)
  455. {
  456. struct renesas_usbhs_platform_info *info = renesas_usbhs_get_info(pdev);
  457. struct renesas_usbhs_driver_callback *dfunc;
  458. struct usbhs_priv *priv;
  459. struct resource *res, *irq_res;
  460. int ret;
  461. /* check device node */
  462. if (pdev->dev.of_node)
  463. info = pdev->dev.platform_data = usbhs_parse_dt(&pdev->dev);
  464. /* check platform information */
  465. if (!info) {
  466. dev_err(&pdev->dev, "no platform information\n");
  467. return -EINVAL;
  468. }
  469. /* platform data */
  470. irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  471. if (!irq_res) {
  472. dev_err(&pdev->dev, "Not enough Renesas USB platform resources.\n");
  473. return -ENODEV;
  474. }
  475. /* usb private data */
  476. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  477. if (!priv)
  478. return -ENOMEM;
  479. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  480. priv->base = devm_ioremap_resource(&pdev->dev, res);
  481. if (IS_ERR(priv->base))
  482. return PTR_ERR(priv->base);
  483. if (of_property_read_bool(pdev->dev.of_node, "extcon")) {
  484. priv->edev = extcon_get_edev_by_phandle(&pdev->dev, 0);
  485. if (IS_ERR(priv->edev))
  486. return PTR_ERR(priv->edev);
  487. }
  488. /*
  489. * care platform info
  490. */
  491. memcpy(&priv->dparam,
  492. &info->driver_param,
  493. sizeof(struct renesas_usbhs_driver_param));
  494. switch (priv->dparam.type) {
  495. case USBHS_TYPE_RCAR_GEN2:
  496. priv->pfunc = usbhs_rcar2_ops;
  497. break;
  498. case USBHS_TYPE_RCAR_GEN3:
  499. priv->pfunc = usbhs_rcar3_ops;
  500. break;
  501. case USBHS_TYPE_RCAR_GEN3_WITH_PLL:
  502. priv->pfunc = usbhs_rcar3_with_pll_ops;
  503. if (!IS_ERR_OR_NULL(priv->edev)) {
  504. priv->nb.notifier_call = priv->pfunc.notifier;
  505. ret = devm_extcon_register_notifier(&pdev->dev,
  506. priv->edev,
  507. EXTCON_USB_HOST,
  508. &priv->nb);
  509. if (ret < 0)
  510. dev_err(&pdev->dev, "no notifier registered\n");
  511. }
  512. break;
  513. case USBHS_TYPE_RZA1:
  514. priv->pfunc = usbhs_rza1_ops;
  515. break;
  516. default:
  517. if (!info->platform_callback.get_id) {
  518. dev_err(&pdev->dev, "no platform callbacks");
  519. return -EINVAL;
  520. }
  521. memcpy(&priv->pfunc,
  522. &info->platform_callback,
  523. sizeof(struct renesas_usbhs_platform_callback));
  524. break;
  525. }
  526. /* set driver callback functions for platform */
  527. dfunc = &info->driver_callback;
  528. dfunc->notify_hotplug = usbhsc_drvcllbck_notify_hotplug;
  529. /* set default param if platform doesn't have */
  530. if (!priv->dparam.pipe_configs) {
  531. priv->dparam.pipe_configs = usbhsc_default_pipe;
  532. priv->dparam.pipe_size = ARRAY_SIZE(usbhsc_default_pipe);
  533. }
  534. if (!priv->dparam.pio_dma_border)
  535. priv->dparam.pio_dma_border = 64; /* 64byte */
  536. /* FIXME */
  537. /* runtime power control ? */
  538. if (priv->pfunc.get_vbus)
  539. usbhsc_flags_set(priv, USBHSF_RUNTIME_PWCTRL);
  540. /*
  541. * priv settings
  542. */
  543. priv->irq = irq_res->start;
  544. if (irq_res->flags & IORESOURCE_IRQ_SHAREABLE)
  545. priv->irqflags = IRQF_SHARED;
  546. priv->pdev = pdev;
  547. INIT_DELAYED_WORK(&priv->notify_hotplug_work, usbhsc_notify_hotplug);
  548. spin_lock_init(usbhs_priv_to_lock(priv));
  549. /* call pipe and module init */
  550. ret = usbhs_pipe_probe(priv);
  551. if (ret < 0)
  552. return ret;
  553. ret = usbhs_fifo_probe(priv);
  554. if (ret < 0)
  555. goto probe_end_pipe_exit;
  556. ret = usbhs_mod_probe(priv);
  557. if (ret < 0)
  558. goto probe_end_fifo_exit;
  559. /* dev_set_drvdata should be called after usbhs_mod_init */
  560. platform_set_drvdata(pdev, priv);
  561. /*
  562. * deviece reset here because
  563. * USB device might be used in boot loader.
  564. */
  565. usbhs_sys_clock_ctrl(priv, 0);
  566. /* check GPIO determining if USB function should be enabled */
  567. if (priv->dparam.enable_gpio) {
  568. gpio_request_one(priv->dparam.enable_gpio, GPIOF_IN, NULL);
  569. ret = !gpio_get_value(priv->dparam.enable_gpio);
  570. gpio_free(priv->dparam.enable_gpio);
  571. if (ret) {
  572. dev_warn(&pdev->dev,
  573. "USB function not selected (GPIO %d)\n",
  574. priv->dparam.enable_gpio);
  575. ret = -ENOTSUPP;
  576. goto probe_end_mod_exit;
  577. }
  578. }
  579. /*
  580. * platform call
  581. *
  582. * USB phy setup might depend on CPU/Board.
  583. * If platform has its callback functions,
  584. * call it here.
  585. */
  586. ret = usbhs_platform_call(priv, hardware_init, pdev);
  587. if (ret < 0) {
  588. dev_err(&pdev->dev, "platform init failed.\n");
  589. goto probe_end_mod_exit;
  590. }
  591. /* reset phy for connection */
  592. usbhs_platform_call(priv, phy_reset, pdev);
  593. /* power control */
  594. pm_runtime_enable(&pdev->dev);
  595. if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) {
  596. usbhsc_power_ctrl(priv, 1);
  597. usbhs_mod_autonomy_mode(priv);
  598. }
  599. /*
  600. * manual call notify_hotplug for cold plug
  601. */
  602. usbhsc_drvcllbck_notify_hotplug(pdev);
  603. dev_info(&pdev->dev, "probed\n");
  604. return ret;
  605. probe_end_mod_exit:
  606. usbhs_mod_remove(priv);
  607. probe_end_fifo_exit:
  608. usbhs_fifo_remove(priv);
  609. probe_end_pipe_exit:
  610. usbhs_pipe_remove(priv);
  611. dev_info(&pdev->dev, "probe failed (%d)\n", ret);
  612. return ret;
  613. }
  614. static int usbhs_remove(struct platform_device *pdev)
  615. {
  616. struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
  617. struct renesas_usbhs_platform_info *info = renesas_usbhs_get_info(pdev);
  618. struct renesas_usbhs_driver_callback *dfunc = &info->driver_callback;
  619. dev_dbg(&pdev->dev, "usb remove\n");
  620. dfunc->notify_hotplug = NULL;
  621. /* power off */
  622. if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
  623. usbhsc_power_ctrl(priv, 0);
  624. pm_runtime_disable(&pdev->dev);
  625. usbhs_platform_call(priv, hardware_exit, pdev);
  626. usbhs_mod_remove(priv);
  627. usbhs_fifo_remove(priv);
  628. usbhs_pipe_remove(priv);
  629. return 0;
  630. }
  631. static int usbhsc_suspend(struct device *dev)
  632. {
  633. struct usbhs_priv *priv = dev_get_drvdata(dev);
  634. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  635. if (mod) {
  636. usbhs_mod_call(priv, stop, priv);
  637. usbhs_mod_change(priv, -1);
  638. }
  639. if (mod || !usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
  640. usbhsc_power_ctrl(priv, 0);
  641. return 0;
  642. }
  643. static int usbhsc_resume(struct device *dev)
  644. {
  645. struct usbhs_priv *priv = dev_get_drvdata(dev);
  646. struct platform_device *pdev = usbhs_priv_to_pdev(priv);
  647. if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) {
  648. usbhsc_power_ctrl(priv, 1);
  649. usbhs_mod_autonomy_mode(priv);
  650. }
  651. usbhs_platform_call(priv, phy_reset, pdev);
  652. usbhsc_drvcllbck_notify_hotplug(pdev);
  653. return 0;
  654. }
  655. static int usbhsc_runtime_nop(struct device *dev)
  656. {
  657. /* Runtime PM callback shared between ->runtime_suspend()
  658. * and ->runtime_resume(). Simply returns success.
  659. *
  660. * This driver re-initializes all registers after
  661. * pm_runtime_get_sync() anyway so there is no need
  662. * to save and restore registers here.
  663. */
  664. return 0;
  665. }
  666. static const struct dev_pm_ops usbhsc_pm_ops = {
  667. .suspend = usbhsc_suspend,
  668. .resume = usbhsc_resume,
  669. .runtime_suspend = usbhsc_runtime_nop,
  670. .runtime_resume = usbhsc_runtime_nop,
  671. };
  672. static struct platform_driver renesas_usbhs_driver = {
  673. .driver = {
  674. .name = "renesas_usbhs",
  675. .pm = &usbhsc_pm_ops,
  676. .of_match_table = of_match_ptr(usbhs_of_match),
  677. },
  678. .probe = usbhs_probe,
  679. .remove = usbhs_remove,
  680. };
  681. module_platform_driver(renesas_usbhs_driver);
  682. MODULE_LICENSE("GPL");
  683. MODULE_DESCRIPTION("Renesas USB driver");
  684. MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");