common.c 19 KB

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