common.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  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 <kuninori.morimoto.gx@renesas.com>
  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.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. clk_put(priv->clks[0]);
  268. return PTR_ERR(priv->clks[1]);
  269. }
  270. return 0;
  271. }
  272. static void usbhsc_clk_put(struct usbhs_priv *priv)
  273. {
  274. int i;
  275. if (!usbhsc_is_multi_clks(priv))
  276. return;
  277. for (i = 0; i < ARRAY_SIZE(priv->clks); i++)
  278. clk_put(priv->clks[i]);
  279. }
  280. static int usbhsc_clk_prepare_enable(struct usbhs_priv *priv)
  281. {
  282. int i, ret;
  283. if (!usbhsc_is_multi_clks(priv))
  284. return 0;
  285. for (i = 0; i < ARRAY_SIZE(priv->clks); i++) {
  286. ret = clk_prepare_enable(priv->clks[i]);
  287. if (ret) {
  288. while (--i >= 0)
  289. clk_disable_unprepare(priv->clks[i]);
  290. return ret;
  291. }
  292. }
  293. return ret;
  294. }
  295. static void usbhsc_clk_disable_unprepare(struct usbhs_priv *priv)
  296. {
  297. int i;
  298. if (!usbhsc_is_multi_clks(priv))
  299. return;
  300. for (i = 0; i < ARRAY_SIZE(priv->clks); i++)
  301. clk_disable_unprepare(priv->clks[i]);
  302. }
  303. /*
  304. * platform default param
  305. */
  306. /* commonly used on old SH-Mobile and RZ/G2L family SoCs */
  307. static struct renesas_usbhs_driver_pipe_config usbhsc_default_pipe[] = {
  308. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_CONTROL, 64, 0x00, false),
  309. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x08, true),
  310. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x28, true),
  311. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x48, true),
  312. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x58, true),
  313. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x68, true),
  314. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x04, false),
  315. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x05, false),
  316. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x06, false),
  317. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x07, false),
  318. };
  319. /* commonly used on newer SH-Mobile and R-Car SoCs */
  320. static struct renesas_usbhs_driver_pipe_config usbhsc_new_pipe[] = {
  321. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_CONTROL, 64, 0x00, false),
  322. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x08, true),
  323. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x28, true),
  324. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x48, true),
  325. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x58, true),
  326. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x68, true),
  327. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x04, false),
  328. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x05, false),
  329. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x06, false),
  330. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x78, true),
  331. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x88, true),
  332. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x98, true),
  333. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xa8, true),
  334. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xb8, true),
  335. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xc8, true),
  336. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xd8, true),
  337. };
  338. /*
  339. * power control
  340. */
  341. static void usbhsc_power_ctrl(struct usbhs_priv *priv, int enable)
  342. {
  343. struct platform_device *pdev = usbhs_priv_to_pdev(priv);
  344. struct device *dev = usbhs_priv_to_dev(priv);
  345. if (enable) {
  346. /* enable PM */
  347. pm_runtime_get_sync(dev);
  348. /* enable clks */
  349. if (usbhsc_clk_prepare_enable(priv))
  350. return;
  351. /* enable platform power */
  352. usbhs_platform_call(priv, power_ctrl, pdev, priv->base, enable);
  353. /* USB on */
  354. usbhs_sys_clock_ctrl(priv, enable);
  355. } else {
  356. /* USB off */
  357. usbhs_sys_clock_ctrl(priv, enable);
  358. /* disable platform power */
  359. usbhs_platform_call(priv, power_ctrl, pdev, priv->base, enable);
  360. /* disable clks */
  361. usbhsc_clk_disable_unprepare(priv);
  362. /* disable PM */
  363. pm_runtime_put_sync(dev);
  364. }
  365. }
  366. /*
  367. * hotplug
  368. */
  369. static void usbhsc_hotplug(struct usbhs_priv *priv)
  370. {
  371. struct platform_device *pdev = usbhs_priv_to_pdev(priv);
  372. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  373. int id;
  374. int enable;
  375. int cable;
  376. int ret;
  377. /*
  378. * get vbus status from platform
  379. */
  380. enable = usbhs_mod_info_call(priv, get_vbus, pdev);
  381. /*
  382. * get id from platform
  383. */
  384. id = usbhs_platform_call(priv, get_id, pdev);
  385. if (enable && !mod) {
  386. if (priv->edev) {
  387. cable = extcon_get_state(priv->edev, EXTCON_USB_HOST);
  388. if ((cable > 0 && id != USBHS_HOST) ||
  389. (!cable && id != USBHS_GADGET)) {
  390. dev_info(&pdev->dev,
  391. "USB cable plugged in doesn't match the selected role!\n");
  392. return;
  393. }
  394. }
  395. ret = usbhs_mod_change(priv, id);
  396. if (ret < 0)
  397. return;
  398. dev_dbg(&pdev->dev, "%s enable\n", __func__);
  399. /* power on */
  400. if (usbhs_get_dparam(priv, runtime_pwctrl))
  401. usbhsc_power_ctrl(priv, enable);
  402. /* bus init */
  403. usbhsc_set_buswait(priv);
  404. usbhsc_bus_init(priv);
  405. /* module start */
  406. usbhs_mod_call(priv, start, priv);
  407. } else if (!enable && mod) {
  408. dev_dbg(&pdev->dev, "%s disable\n", __func__);
  409. /* module stop */
  410. usbhs_mod_call(priv, stop, priv);
  411. /* bus init */
  412. usbhsc_bus_init(priv);
  413. /* power off */
  414. if (usbhs_get_dparam(priv, runtime_pwctrl))
  415. usbhsc_power_ctrl(priv, enable);
  416. usbhs_mod_change(priv, -1);
  417. /* reset phy for next connection */
  418. usbhs_platform_call(priv, phy_reset, pdev);
  419. }
  420. }
  421. /*
  422. * notify hotplug
  423. */
  424. static void usbhsc_notify_hotplug(struct work_struct *work)
  425. {
  426. struct usbhs_priv *priv = container_of(work,
  427. struct usbhs_priv,
  428. notify_hotplug_work.work);
  429. usbhsc_hotplug(priv);
  430. }
  431. int usbhsc_schedule_notify_hotplug(struct platform_device *pdev)
  432. {
  433. struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
  434. int delay = usbhs_get_dparam(priv, detection_delay);
  435. /*
  436. * This functions will be called in interrupt.
  437. * To make sure safety context,
  438. * use workqueue for usbhs_notify_hotplug
  439. */
  440. schedule_delayed_work(&priv->notify_hotplug_work,
  441. msecs_to_jiffies(delay));
  442. return 0;
  443. }
  444. /*
  445. * platform functions
  446. */
  447. static const struct of_device_id usbhs_of_match[] = {
  448. {
  449. .compatible = "renesas,usbhs-r8a774c0",
  450. .data = &usbhs_rcar_gen3_with_pll_plat_info,
  451. },
  452. {
  453. .compatible = "renesas,usbhs-r8a7790",
  454. .data = &usbhs_rcar_gen2_plat_info,
  455. },
  456. {
  457. .compatible = "renesas,usbhs-r8a7791",
  458. .data = &usbhs_rcar_gen2_plat_info,
  459. },
  460. {
  461. .compatible = "renesas,usbhs-r8a7794",
  462. .data = &usbhs_rcar_gen2_plat_info,
  463. },
  464. {
  465. .compatible = "renesas,usbhs-r8a7795",
  466. .data = &usbhs_rcar_gen3_plat_info,
  467. },
  468. {
  469. .compatible = "renesas,usbhs-r8a7796",
  470. .data = &usbhs_rcar_gen3_plat_info,
  471. },
  472. {
  473. .compatible = "renesas,usbhs-r8a77990",
  474. .data = &usbhs_rcar_gen3_with_pll_plat_info,
  475. },
  476. {
  477. .compatible = "renesas,usbhs-r8a77995",
  478. .data = &usbhs_rcar_gen3_with_pll_plat_info,
  479. },
  480. {
  481. .compatible = "renesas,usbhs-r9a07g043",
  482. .data = &usbhs_rzg2l_plat_info,
  483. },
  484. {
  485. .compatible = "renesas,usbhs-r9a07g044",
  486. .data = &usbhs_rzg2l_plat_info,
  487. },
  488. {
  489. .compatible = "renesas,usbhs-r9a07g054",
  490. .data = &usbhs_rzg2l_plat_info,
  491. },
  492. {
  493. .compatible = "renesas,usbhs-r9a09g077",
  494. .data = &usbhs_rzg2l_plat_info,
  495. },
  496. {
  497. .compatible = "renesas,rcar-gen2-usbhs",
  498. .data = &usbhs_rcar_gen2_plat_info,
  499. },
  500. {
  501. .compatible = "renesas,rcar-gen3-usbhs",
  502. .data = &usbhs_rcar_gen3_plat_info,
  503. },
  504. {
  505. .compatible = "renesas,rza1-usbhs",
  506. .data = &usbhs_rza1_plat_info,
  507. },
  508. {
  509. .compatible = "renesas,rza2-usbhs",
  510. .data = &usbhs_rza2_plat_info,
  511. },
  512. {
  513. .compatible = "renesas,rzg2l-usbhs",
  514. .data = &usbhs_rzg2l_plat_info,
  515. },
  516. { }
  517. };
  518. MODULE_DEVICE_TABLE(of, usbhs_of_match);
  519. static int usbhs_probe(struct platform_device *pdev)
  520. {
  521. const struct renesas_usbhs_platform_info *info;
  522. struct usbhs_priv *priv;
  523. struct device *dev = &pdev->dev;
  524. struct gpio_desc *gpiod;
  525. int ret;
  526. u32 tmp;
  527. int irq;
  528. info = of_device_get_match_data(dev);
  529. if (!info) {
  530. info = dev_get_platdata(dev);
  531. if (!info)
  532. return dev_err_probe(dev, -EINVAL, "no platform info\n");
  533. }
  534. /* platform data */
  535. irq = platform_get_irq(pdev, 0);
  536. if (irq < 0)
  537. return irq;
  538. /* usb private data */
  539. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  540. if (!priv)
  541. return -ENOMEM;
  542. priv->base = devm_platform_ioremap_resource(pdev, 0);
  543. if (IS_ERR(priv->base))
  544. return PTR_ERR(priv->base);
  545. if (of_property_present(dev_of_node(dev), "extcon")) {
  546. priv->edev = extcon_get_edev_by_phandle(dev, 0);
  547. if (IS_ERR(priv->edev))
  548. return PTR_ERR(priv->edev);
  549. }
  550. priv->rsts = devm_reset_control_array_get_optional_shared(dev);
  551. if (IS_ERR(priv->rsts))
  552. return PTR_ERR(priv->rsts);
  553. /*
  554. * care platform info
  555. */
  556. priv->dparam = info->driver_param;
  557. if (!info->platform_callback.get_id) {
  558. dev_err(dev, "no platform callbacks\n");
  559. return -EINVAL;
  560. }
  561. priv->pfunc = &info->platform_callback;
  562. /* set default param if platform doesn't have */
  563. if (usbhs_get_dparam(priv, has_new_pipe_configs)) {
  564. priv->dparam.pipe_configs = usbhsc_new_pipe;
  565. priv->dparam.pipe_size = ARRAY_SIZE(usbhsc_new_pipe);
  566. } else if (!priv->dparam.pipe_configs) {
  567. priv->dparam.pipe_configs = usbhsc_default_pipe;
  568. priv->dparam.pipe_size = ARRAY_SIZE(usbhsc_default_pipe);
  569. }
  570. if (!priv->dparam.pio_dma_border)
  571. priv->dparam.pio_dma_border = 64; /* 64byte */
  572. if (!of_property_read_u32(dev_of_node(dev), "renesas,buswait", &tmp))
  573. priv->dparam.buswait_bwait = tmp;
  574. gpiod = devm_gpiod_get_optional(dev, "renesas,enable", GPIOD_IN);
  575. if (IS_ERR(gpiod))
  576. return PTR_ERR(gpiod);
  577. /* FIXME */
  578. /* runtime power control ? */
  579. if (priv->pfunc->get_vbus)
  580. usbhs_get_dparam(priv, runtime_pwctrl) = 1;
  581. /*
  582. * priv settings
  583. */
  584. priv->irq = irq;
  585. priv->pdev = pdev;
  586. INIT_DELAYED_WORK(&priv->notify_hotplug_work, usbhsc_notify_hotplug);
  587. spin_lock_init(usbhs_priv_to_lock(priv));
  588. /*
  589. * Acquire clocks and enable power management (PM) early in the
  590. * probe process, as the driver accesses registers during
  591. * initialization. Ensure the device is active before proceeding.
  592. */
  593. pm_runtime_enable(dev);
  594. ret = usbhsc_clk_get(dev, priv);
  595. if (ret)
  596. goto probe_pm_disable;
  597. ret = pm_runtime_resume_and_get(dev);
  598. if (ret)
  599. goto probe_clk_put;
  600. ret = usbhsc_clk_prepare_enable(priv);
  601. if (ret)
  602. goto probe_pm_put;
  603. /* call pipe and module init */
  604. ret = usbhs_pipe_probe(priv);
  605. if (ret < 0)
  606. goto probe_clk_dis_unprepare;
  607. ret = usbhs_fifo_probe(priv);
  608. if (ret < 0)
  609. goto probe_end_pipe_exit;
  610. ret = usbhs_mod_probe(priv);
  611. if (ret < 0)
  612. goto probe_end_fifo_exit;
  613. /* platform_set_drvdata() should be called after usbhs_mod_probe() */
  614. platform_set_drvdata(pdev, priv);
  615. ret = reset_control_deassert(priv->rsts);
  616. if (ret)
  617. goto probe_fail_rst;
  618. /*
  619. * device reset here because
  620. * USB device might be used in boot loader.
  621. */
  622. usbhs_sys_clock_ctrl(priv, 0);
  623. /* check GPIO determining if USB function should be enabled */
  624. if (gpiod) {
  625. ret = !gpiod_get_value(gpiod);
  626. if (ret) {
  627. dev_warn(dev, "USB function not selected (GPIO)\n");
  628. ret = -ENOTSUPP;
  629. goto probe_assert_rest;
  630. }
  631. }
  632. /*
  633. * platform call
  634. *
  635. * USB phy setup might depend on CPU/Board.
  636. * If platform has its callback functions,
  637. * call it here.
  638. */
  639. ret = usbhs_platform_call(priv, hardware_init, pdev);
  640. if (ret < 0) {
  641. dev_err(dev, "platform init failed.\n");
  642. goto probe_assert_rest;
  643. }
  644. /* reset phy for connection */
  645. usbhs_platform_call(priv, phy_reset, pdev);
  646. /*
  647. * Disable the clocks that were enabled earlier in the probe path,
  648. * and let the driver handle the clocks beyond this point.
  649. */
  650. usbhsc_clk_disable_unprepare(priv);
  651. pm_runtime_put(dev);
  652. if (!usbhs_get_dparam(priv, runtime_pwctrl)) {
  653. usbhsc_power_ctrl(priv, 1);
  654. usbhs_mod_autonomy_mode(priv);
  655. } else {
  656. usbhs_mod_non_autonomy_mode(priv);
  657. }
  658. /*
  659. * manual call notify_hotplug for cold plug
  660. */
  661. usbhsc_schedule_notify_hotplug(pdev);
  662. dev_info(dev, "probed\n");
  663. return ret;
  664. probe_assert_rest:
  665. reset_control_assert(priv->rsts);
  666. probe_fail_rst:
  667. usbhs_mod_remove(priv);
  668. probe_end_fifo_exit:
  669. usbhs_fifo_remove(priv);
  670. probe_end_pipe_exit:
  671. usbhs_pipe_remove(priv);
  672. probe_clk_dis_unprepare:
  673. usbhsc_clk_disable_unprepare(priv);
  674. probe_pm_put:
  675. pm_runtime_put(dev);
  676. probe_clk_put:
  677. usbhsc_clk_put(priv);
  678. probe_pm_disable:
  679. pm_runtime_disable(dev);
  680. dev_info(dev, "probe failed (%d)\n", ret);
  681. return ret;
  682. }
  683. static void usbhs_remove(struct platform_device *pdev)
  684. {
  685. struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
  686. dev_dbg(&pdev->dev, "usb remove\n");
  687. flush_delayed_work(&priv->notify_hotplug_work);
  688. usbhs_platform_call(priv, hardware_exit, pdev);
  689. reset_control_assert(priv->rsts);
  690. /*
  691. * Explicitly free the IRQ to ensure the interrupt handler is
  692. * disabled and synchronized before freeing resources.
  693. * devm_free_irq() calls free_irq() which waits for any running
  694. * ISR to complete, preventing UAF.
  695. */
  696. devm_free_irq(&pdev->dev, priv->irq, priv);
  697. usbhs_mod_remove(priv);
  698. usbhs_fifo_remove(priv);
  699. usbhs_pipe_remove(priv);
  700. /* power off */
  701. if (!usbhs_get_dparam(priv, runtime_pwctrl))
  702. usbhsc_power_ctrl(priv, 0);
  703. usbhsc_clk_put(priv);
  704. pm_runtime_disable(&pdev->dev);
  705. }
  706. static void usbhsc_restore(struct device *dev)
  707. {
  708. struct usbhs_priv *priv = dev_get_drvdata(dev);
  709. struct platform_device *pdev = usbhs_priv_to_pdev(priv);
  710. if (!usbhs_get_dparam(priv, runtime_pwctrl)) {
  711. usbhsc_power_ctrl(priv, 1);
  712. usbhs_mod_autonomy_mode(priv);
  713. }
  714. usbhs_platform_call(priv, phy_reset, pdev);
  715. usbhsc_schedule_notify_hotplug(pdev);
  716. }
  717. static int usbhsc_suspend(struct device *dev)
  718. {
  719. struct usbhs_priv *priv = dev_get_drvdata(dev);
  720. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  721. int ret;
  722. if (mod) {
  723. usbhs_mod_call(priv, stop, priv);
  724. usbhs_mod_change(priv, -1);
  725. }
  726. if (mod || !usbhs_get_dparam(priv, runtime_pwctrl))
  727. usbhsc_power_ctrl(priv, 0);
  728. ret = reset_control_assert(priv->rsts);
  729. if (ret)
  730. usbhsc_restore(dev);
  731. return ret;
  732. }
  733. static int usbhsc_resume(struct device *dev)
  734. {
  735. struct usbhs_priv *priv = dev_get_drvdata(dev);
  736. int ret;
  737. ret = reset_control_deassert(priv->rsts);
  738. if (ret)
  739. return ret;
  740. usbhsc_restore(dev);
  741. return 0;
  742. }
  743. static DEFINE_SIMPLE_DEV_PM_OPS(usbhsc_pm_ops, usbhsc_suspend, usbhsc_resume);
  744. static struct platform_driver renesas_usbhs_driver = {
  745. .driver = {
  746. .name = "renesas_usbhs",
  747. .pm = pm_sleep_ptr(&usbhsc_pm_ops),
  748. .of_match_table = usbhs_of_match,
  749. },
  750. .probe = usbhs_probe,
  751. .remove = usbhs_remove,
  752. };
  753. module_platform_driver(renesas_usbhs_driver);
  754. MODULE_LICENSE("GPL");
  755. MODULE_DESCRIPTION("Renesas USB driver");
  756. MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");