common.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  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/clk.h>
  9. #include <linux/err.h>
  10. #include <linux/gpio.h>
  11. #include <linux/io.h>
  12. #include <linux/module.h>
  13. #include <linux/of_device.h>
  14. #include <linux/of_gpio.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. #define USBHSF_RUNTIME_PWCTRL (1 << 0)
  44. /* status */
  45. #define usbhsc_flags_init(p) do {(p)->flags = 0; } while (0)
  46. #define usbhsc_flags_set(p, b) ((p)->flags |= (b))
  47. #define usbhsc_flags_clr(p, b) ((p)->flags &= ~(b))
  48. #define usbhsc_flags_has(p, b) ((p)->flags & (b))
  49. /*
  50. * platform call back
  51. *
  52. * renesas usb support platform callback function.
  53. * Below macro call it.
  54. * if platform doesn't have callback, it return 0 (no error)
  55. */
  56. #define usbhs_platform_call(priv, func, args...)\
  57. (!(priv) ? -ENODEV : \
  58. !((priv)->pfunc.func) ? 0 : \
  59. (priv)->pfunc.func(args))
  60. /*
  61. * common functions
  62. */
  63. u16 usbhs_read(struct usbhs_priv *priv, u32 reg)
  64. {
  65. return ioread16(priv->base + reg);
  66. }
  67. void usbhs_write(struct usbhs_priv *priv, u32 reg, u16 data)
  68. {
  69. iowrite16(data, priv->base + reg);
  70. }
  71. void usbhs_bset(struct usbhs_priv *priv, u32 reg, u16 mask, u16 data)
  72. {
  73. u16 val = usbhs_read(priv, reg);
  74. val &= ~mask;
  75. val |= data & mask;
  76. usbhs_write(priv, reg, val);
  77. }
  78. struct usbhs_priv *usbhs_pdev_to_priv(struct platform_device *pdev)
  79. {
  80. return dev_get_drvdata(&pdev->dev);
  81. }
  82. /*
  83. * syscfg functions
  84. */
  85. static void usbhs_sys_clock_ctrl(struct usbhs_priv *priv, int enable)
  86. {
  87. usbhs_bset(priv, SYSCFG, SCKE, enable ? SCKE : 0);
  88. }
  89. void usbhs_sys_host_ctrl(struct usbhs_priv *priv, int enable)
  90. {
  91. u16 mask = DCFM | DRPD | DPRPU | HSE | USBE;
  92. u16 val = DCFM | DRPD | HSE | USBE;
  93. int has_otg = usbhs_get_dparam(priv, has_otg);
  94. if (has_otg)
  95. usbhs_bset(priv, DVSTCTR, (EXTLP | PWEN), (EXTLP | PWEN));
  96. /*
  97. * if enable
  98. *
  99. * - select Host mode
  100. * - D+ Line/D- Line Pull-down
  101. */
  102. usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
  103. }
  104. void usbhs_sys_function_ctrl(struct usbhs_priv *priv, int enable)
  105. {
  106. u16 mask = DCFM | DRPD | DPRPU | HSE | USBE;
  107. u16 val = HSE | USBE;
  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 = usbhs_read(priv, USBVAL);
  143. req->wIndex = usbhs_read(priv, USBINDX);
  144. req->wLength = 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, req->wValue);
  150. usbhs_write(priv, USBINDX, req->wIndex);
  151. usbhs_write(priv, USBLENG, 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. if (priv->dparam.type == USBHS_TYPE_RCAR_GEN3 ||
  250. priv->dparam.type == USBHS_TYPE_RCAR_GEN3_WITH_PLL)
  251. return true;
  252. return false;
  253. }
  254. static int usbhsc_clk_get(struct device *dev, struct usbhs_priv *priv)
  255. {
  256. if (!usbhsc_is_multi_clks(priv))
  257. return 0;
  258. /* The first clock should exist */
  259. priv->clks[0] = of_clk_get(dev->of_node, 0);
  260. if (IS_ERR(priv->clks[0]))
  261. return PTR_ERR(priv->clks[0]);
  262. /*
  263. * To backward compatibility with old DT, this driver checks the return
  264. * value if it's -ENOENT or not.
  265. */
  266. priv->clks[1] = of_clk_get(dev->of_node, 1);
  267. if (PTR_ERR(priv->clks[1]) == -ENOENT)
  268. priv->clks[1] = NULL;
  269. else if (IS_ERR(priv->clks[1]))
  270. return PTR_ERR(priv->clks[1]);
  271. return 0;
  272. }
  273. static void usbhsc_clk_put(struct usbhs_priv *priv)
  274. {
  275. int i;
  276. if (!usbhsc_is_multi_clks(priv))
  277. return;
  278. for (i = 0; i < ARRAY_SIZE(priv->clks); i++)
  279. clk_put(priv->clks[i]);
  280. }
  281. static int usbhsc_clk_prepare_enable(struct usbhs_priv *priv)
  282. {
  283. int i, ret;
  284. if (!usbhsc_is_multi_clks(priv))
  285. return 0;
  286. for (i = 0; i < ARRAY_SIZE(priv->clks); i++) {
  287. ret = clk_prepare_enable(priv->clks[i]);
  288. if (ret) {
  289. while (--i >= 0)
  290. clk_disable_unprepare(priv->clks[i]);
  291. return ret;
  292. }
  293. }
  294. return ret;
  295. }
  296. static void usbhsc_clk_disable_unprepare(struct usbhs_priv *priv)
  297. {
  298. int i;
  299. if (!usbhsc_is_multi_clks(priv))
  300. return;
  301. for (i = 0; i < ARRAY_SIZE(priv->clks); i++)
  302. clk_disable_unprepare(priv->clks[i]);
  303. }
  304. /*
  305. * platform default param
  306. */
  307. /* commonly used on old SH-Mobile SoCs */
  308. static struct renesas_usbhs_driver_pipe_config usbhsc_default_pipe[] = {
  309. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_CONTROL, 64, 0x00, false),
  310. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x08, false),
  311. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x18, false),
  312. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x28, true),
  313. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x38, true),
  314. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x48, true),
  315. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x04, false),
  316. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x05, false),
  317. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x06, false),
  318. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x07, false),
  319. };
  320. /* commonly used on newer SH-Mobile and R-Car SoCs */
  321. static struct renesas_usbhs_driver_pipe_config usbhsc_new_pipe[] = {
  322. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_CONTROL, 64, 0x00, false),
  323. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x08, true),
  324. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x28, true),
  325. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x48, true),
  326. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x58, true),
  327. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x68, true),
  328. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x04, false),
  329. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x05, false),
  330. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x06, false),
  331. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x78, true),
  332. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x88, true),
  333. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x98, true),
  334. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xa8, true),
  335. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xb8, true),
  336. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xc8, true),
  337. RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xd8, true),
  338. };
  339. /*
  340. * power control
  341. */
  342. static void usbhsc_power_ctrl(struct usbhs_priv *priv, int enable)
  343. {
  344. struct platform_device *pdev = usbhs_priv_to_pdev(priv);
  345. struct device *dev = usbhs_priv_to_dev(priv);
  346. if (enable) {
  347. /* enable PM */
  348. pm_runtime_get_sync(dev);
  349. /* enable clks */
  350. if (usbhsc_clk_prepare_enable(priv))
  351. return;
  352. /* enable platform power */
  353. usbhs_platform_call(priv, power_ctrl, pdev, priv->base, enable);
  354. /* USB on */
  355. usbhs_sys_clock_ctrl(priv, enable);
  356. } else {
  357. /* USB off */
  358. usbhs_sys_clock_ctrl(priv, enable);
  359. /* disable platform power */
  360. usbhs_platform_call(priv, power_ctrl, pdev, priv->base, enable);
  361. /* disable clks */
  362. usbhsc_clk_disable_unprepare(priv);
  363. /* disable PM */
  364. pm_runtime_put_sync(dev);
  365. }
  366. }
  367. /*
  368. * hotplug
  369. */
  370. static void usbhsc_hotplug(struct usbhs_priv *priv)
  371. {
  372. struct platform_device *pdev = usbhs_priv_to_pdev(priv);
  373. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  374. int id;
  375. int enable;
  376. int cable;
  377. int ret;
  378. /*
  379. * get vbus status from platform
  380. */
  381. enable = usbhs_platform_call(priv, get_vbus, pdev);
  382. /*
  383. * get id from platform
  384. */
  385. id = usbhs_platform_call(priv, get_id, pdev);
  386. if (enable && !mod) {
  387. if (priv->edev) {
  388. cable = extcon_get_state(priv->edev, EXTCON_USB_HOST);
  389. if ((cable > 0 && id != USBHS_HOST) ||
  390. (!cable && id != USBHS_GADGET)) {
  391. dev_info(&pdev->dev,
  392. "USB cable plugged in doesn't match the selected role!\n");
  393. return;
  394. }
  395. }
  396. ret = usbhs_mod_change(priv, id);
  397. if (ret < 0)
  398. return;
  399. dev_dbg(&pdev->dev, "%s enable\n", __func__);
  400. /* power on */
  401. if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
  402. usbhsc_power_ctrl(priv, enable);
  403. /* bus init */
  404. usbhsc_set_buswait(priv);
  405. usbhsc_bus_init(priv);
  406. /* module start */
  407. usbhs_mod_call(priv, start, priv);
  408. } else if (!enable && mod) {
  409. dev_dbg(&pdev->dev, "%s disable\n", __func__);
  410. /* module stop */
  411. usbhs_mod_call(priv, stop, priv);
  412. /* bus init */
  413. usbhsc_bus_init(priv);
  414. /* power off */
  415. if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
  416. usbhsc_power_ctrl(priv, enable);
  417. usbhs_mod_change(priv, -1);
  418. /* reset phy for next connection */
  419. usbhs_platform_call(priv, phy_reset, pdev);
  420. }
  421. }
  422. /*
  423. * notify hotplug
  424. */
  425. static void usbhsc_notify_hotplug(struct work_struct *work)
  426. {
  427. struct usbhs_priv *priv = container_of(work,
  428. struct usbhs_priv,
  429. notify_hotplug_work.work);
  430. usbhsc_hotplug(priv);
  431. }
  432. static int usbhsc_drvcllbck_notify_hotplug(struct platform_device *pdev)
  433. {
  434. struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
  435. int delay = usbhs_get_dparam(priv, detection_delay);
  436. /*
  437. * This functions will be called in interrupt.
  438. * To make sure safety context,
  439. * use workqueue for usbhs_notify_hotplug
  440. */
  441. schedule_delayed_work(&priv->notify_hotplug_work,
  442. msecs_to_jiffies(delay));
  443. return 0;
  444. }
  445. /*
  446. * platform functions
  447. */
  448. static const struct of_device_id usbhs_of_match[] = {
  449. {
  450. .compatible = "renesas,usbhs-r8a7790",
  451. .data = (void *)USBHS_TYPE_RCAR_GEN2,
  452. },
  453. {
  454. .compatible = "renesas,usbhs-r8a7791",
  455. .data = (void *)USBHS_TYPE_RCAR_GEN2,
  456. },
  457. {
  458. .compatible = "renesas,usbhs-r8a7794",
  459. .data = (void *)USBHS_TYPE_RCAR_GEN2,
  460. },
  461. {
  462. .compatible = "renesas,usbhs-r8a7795",
  463. .data = (void *)USBHS_TYPE_RCAR_GEN3,
  464. },
  465. {
  466. .compatible = "renesas,usbhs-r8a7796",
  467. .data = (void *)USBHS_TYPE_RCAR_GEN3,
  468. },
  469. {
  470. .compatible = "renesas,usbhs-r8a77990",
  471. .data = (void *)USBHS_TYPE_RCAR_GEN3_WITH_PLL,
  472. },
  473. {
  474. .compatible = "renesas,usbhs-r8a77995",
  475. .data = (void *)USBHS_TYPE_RCAR_GEN3_WITH_PLL,
  476. },
  477. {
  478. .compatible = "renesas,rcar-gen2-usbhs",
  479. .data = (void *)USBHS_TYPE_RCAR_GEN2,
  480. },
  481. {
  482. .compatible = "renesas,rcar-gen3-usbhs",
  483. .data = (void *)USBHS_TYPE_RCAR_GEN3,
  484. },
  485. {
  486. .compatible = "renesas,rza1-usbhs",
  487. .data = (void *)USBHS_TYPE_RZA1,
  488. },
  489. { },
  490. };
  491. MODULE_DEVICE_TABLE(of, usbhs_of_match);
  492. static struct renesas_usbhs_platform_info *usbhs_parse_dt(struct device *dev)
  493. {
  494. struct renesas_usbhs_platform_info *info;
  495. struct renesas_usbhs_driver_param *dparam;
  496. u32 tmp;
  497. int gpio;
  498. info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
  499. if (!info)
  500. return NULL;
  501. dparam = &info->driver_param;
  502. dparam->type = (uintptr_t)of_device_get_match_data(dev);
  503. if (!of_property_read_u32(dev->of_node, "renesas,buswait", &tmp))
  504. dparam->buswait_bwait = tmp;
  505. gpio = of_get_named_gpio_flags(dev->of_node, "renesas,enable-gpio", 0,
  506. NULL);
  507. if (gpio > 0)
  508. dparam->enable_gpio = gpio;
  509. if (dparam->type == USBHS_TYPE_RCAR_GEN2 ||
  510. dparam->type == USBHS_TYPE_RCAR_GEN3 ||
  511. dparam->type == USBHS_TYPE_RCAR_GEN3_WITH_PLL) {
  512. dparam->has_usb_dmac = 1;
  513. dparam->pipe_configs = usbhsc_new_pipe;
  514. dparam->pipe_size = ARRAY_SIZE(usbhsc_new_pipe);
  515. }
  516. if (dparam->type == USBHS_TYPE_RZA1) {
  517. dparam->pipe_configs = usbhsc_new_pipe;
  518. dparam->pipe_size = ARRAY_SIZE(usbhsc_new_pipe);
  519. }
  520. return info;
  521. }
  522. static int usbhs_probe(struct platform_device *pdev)
  523. {
  524. struct renesas_usbhs_platform_info *info = renesas_usbhs_get_info(pdev);
  525. struct renesas_usbhs_driver_callback *dfunc;
  526. struct usbhs_priv *priv;
  527. struct resource *res, *irq_res;
  528. int ret;
  529. /* check device node */
  530. if (pdev->dev.of_node)
  531. info = pdev->dev.platform_data = usbhs_parse_dt(&pdev->dev);
  532. /* check platform information */
  533. if (!info) {
  534. dev_err(&pdev->dev, "no platform information\n");
  535. return -EINVAL;
  536. }
  537. /* platform data */
  538. irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  539. if (!irq_res) {
  540. dev_err(&pdev->dev, "Not enough Renesas USB platform resources.\n");
  541. return -ENODEV;
  542. }
  543. /* usb private data */
  544. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  545. if (!priv)
  546. return -ENOMEM;
  547. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  548. priv->base = devm_ioremap_resource(&pdev->dev, res);
  549. if (IS_ERR(priv->base))
  550. return PTR_ERR(priv->base);
  551. if (of_property_read_bool(pdev->dev.of_node, "extcon")) {
  552. priv->edev = extcon_get_edev_by_phandle(&pdev->dev, 0);
  553. if (IS_ERR(priv->edev))
  554. return PTR_ERR(priv->edev);
  555. }
  556. priv->rsts = devm_reset_control_array_get_optional_shared(&pdev->dev);
  557. if (IS_ERR(priv->rsts))
  558. return PTR_ERR(priv->rsts);
  559. /*
  560. * care platform info
  561. */
  562. memcpy(&priv->dparam,
  563. &info->driver_param,
  564. sizeof(struct renesas_usbhs_driver_param));
  565. switch (priv->dparam.type) {
  566. case USBHS_TYPE_RCAR_GEN2:
  567. priv->pfunc = usbhs_rcar2_ops;
  568. break;
  569. case USBHS_TYPE_RCAR_GEN3:
  570. priv->pfunc = usbhs_rcar3_ops;
  571. break;
  572. case USBHS_TYPE_RCAR_GEN3_WITH_PLL:
  573. priv->pfunc = usbhs_rcar3_with_pll_ops;
  574. break;
  575. case USBHS_TYPE_RZA1:
  576. priv->pfunc = usbhs_rza1_ops;
  577. break;
  578. default:
  579. if (!info->platform_callback.get_id) {
  580. dev_err(&pdev->dev, "no platform callbacks");
  581. return -EINVAL;
  582. }
  583. memcpy(&priv->pfunc,
  584. &info->platform_callback,
  585. sizeof(struct renesas_usbhs_platform_callback));
  586. break;
  587. }
  588. /* set driver callback functions for platform */
  589. dfunc = &info->driver_callback;
  590. dfunc->notify_hotplug = usbhsc_drvcllbck_notify_hotplug;
  591. /* set default param if platform doesn't have */
  592. if (!priv->dparam.pipe_configs) {
  593. priv->dparam.pipe_configs = usbhsc_default_pipe;
  594. priv->dparam.pipe_size = ARRAY_SIZE(usbhsc_default_pipe);
  595. }
  596. if (!priv->dparam.pio_dma_border)
  597. priv->dparam.pio_dma_border = 64; /* 64byte */
  598. /* FIXME */
  599. /* runtime power control ? */
  600. if (priv->pfunc.get_vbus)
  601. usbhsc_flags_set(priv, USBHSF_RUNTIME_PWCTRL);
  602. /*
  603. * priv settings
  604. */
  605. priv->irq = irq_res->start;
  606. if (irq_res->flags & IORESOURCE_IRQ_SHAREABLE)
  607. priv->irqflags = IRQF_SHARED;
  608. priv->pdev = pdev;
  609. INIT_DELAYED_WORK(&priv->notify_hotplug_work, usbhsc_notify_hotplug);
  610. spin_lock_init(usbhs_priv_to_lock(priv));
  611. /* call pipe and module init */
  612. ret = usbhs_pipe_probe(priv);
  613. if (ret < 0)
  614. return ret;
  615. ret = usbhs_fifo_probe(priv);
  616. if (ret < 0)
  617. goto probe_end_pipe_exit;
  618. ret = usbhs_mod_probe(priv);
  619. if (ret < 0)
  620. goto probe_end_fifo_exit;
  621. /* dev_set_drvdata should be called after usbhs_mod_init */
  622. platform_set_drvdata(pdev, priv);
  623. ret = reset_control_deassert(priv->rsts);
  624. if (ret)
  625. goto probe_fail_rst;
  626. ret = usbhsc_clk_get(&pdev->dev, priv);
  627. if (ret)
  628. goto probe_fail_clks;
  629. /*
  630. * deviece reset here because
  631. * USB device might be used in boot loader.
  632. */
  633. usbhs_sys_clock_ctrl(priv, 0);
  634. /* check GPIO determining if USB function should be enabled */
  635. if (priv->dparam.enable_gpio) {
  636. gpio_request_one(priv->dparam.enable_gpio, GPIOF_IN, NULL);
  637. ret = !gpio_get_value(priv->dparam.enable_gpio);
  638. gpio_free(priv->dparam.enable_gpio);
  639. if (ret) {
  640. dev_warn(&pdev->dev,
  641. "USB function not selected (GPIO %d)\n",
  642. priv->dparam.enable_gpio);
  643. ret = -ENOTSUPP;
  644. goto probe_end_mod_exit;
  645. }
  646. }
  647. /*
  648. * platform call
  649. *
  650. * USB phy setup might depend on CPU/Board.
  651. * If platform has its callback functions,
  652. * call it here.
  653. */
  654. ret = usbhs_platform_call(priv, hardware_init, pdev);
  655. if (ret < 0) {
  656. dev_err(&pdev->dev, "platform init failed.\n");
  657. goto probe_end_mod_exit;
  658. }
  659. /* reset phy for connection */
  660. usbhs_platform_call(priv, phy_reset, pdev);
  661. /* power control */
  662. pm_runtime_enable(&pdev->dev);
  663. if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) {
  664. usbhsc_power_ctrl(priv, 1);
  665. usbhs_mod_autonomy_mode(priv);
  666. }
  667. /*
  668. * manual call notify_hotplug for cold plug
  669. */
  670. usbhsc_drvcllbck_notify_hotplug(pdev);
  671. dev_info(&pdev->dev, "probed\n");
  672. return ret;
  673. probe_end_mod_exit:
  674. usbhsc_clk_put(priv);
  675. probe_fail_clks:
  676. reset_control_assert(priv->rsts);
  677. probe_fail_rst:
  678. usbhs_mod_remove(priv);
  679. probe_end_fifo_exit:
  680. usbhs_fifo_remove(priv);
  681. probe_end_pipe_exit:
  682. usbhs_pipe_remove(priv);
  683. dev_info(&pdev->dev, "probe failed (%d)\n", ret);
  684. return ret;
  685. }
  686. static int usbhs_remove(struct platform_device *pdev)
  687. {
  688. struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
  689. struct renesas_usbhs_platform_info *info = renesas_usbhs_get_info(pdev);
  690. struct renesas_usbhs_driver_callback *dfunc = &info->driver_callback;
  691. dev_dbg(&pdev->dev, "usb remove\n");
  692. dfunc->notify_hotplug = NULL;
  693. /* power off */
  694. if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
  695. usbhsc_power_ctrl(priv, 0);
  696. pm_runtime_disable(&pdev->dev);
  697. usbhs_platform_call(priv, hardware_exit, pdev);
  698. usbhsc_clk_put(priv);
  699. reset_control_assert(priv->rsts);
  700. usbhs_mod_remove(priv);
  701. usbhs_fifo_remove(priv);
  702. usbhs_pipe_remove(priv);
  703. return 0;
  704. }
  705. static int usbhsc_suspend(struct device *dev)
  706. {
  707. struct usbhs_priv *priv = dev_get_drvdata(dev);
  708. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  709. if (mod) {
  710. usbhs_mod_call(priv, stop, priv);
  711. usbhs_mod_change(priv, -1);
  712. }
  713. if (mod || !usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
  714. usbhsc_power_ctrl(priv, 0);
  715. return 0;
  716. }
  717. static int usbhsc_resume(struct device *dev)
  718. {
  719. struct usbhs_priv *priv = dev_get_drvdata(dev);
  720. struct platform_device *pdev = usbhs_priv_to_pdev(priv);
  721. if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) {
  722. usbhsc_power_ctrl(priv, 1);
  723. usbhs_mod_autonomy_mode(priv);
  724. }
  725. usbhs_platform_call(priv, phy_reset, pdev);
  726. usbhsc_drvcllbck_notify_hotplug(pdev);
  727. return 0;
  728. }
  729. static int usbhsc_runtime_nop(struct device *dev)
  730. {
  731. /* Runtime PM callback shared between ->runtime_suspend()
  732. * and ->runtime_resume(). Simply returns success.
  733. *
  734. * This driver re-initializes all registers after
  735. * pm_runtime_get_sync() anyway so there is no need
  736. * to save and restore registers here.
  737. */
  738. return 0;
  739. }
  740. static const struct dev_pm_ops usbhsc_pm_ops = {
  741. .suspend = usbhsc_suspend,
  742. .resume = usbhsc_resume,
  743. .runtime_suspend = usbhsc_runtime_nop,
  744. .runtime_resume = usbhsc_runtime_nop,
  745. };
  746. static struct platform_driver renesas_usbhs_driver = {
  747. .driver = {
  748. .name = "renesas_usbhs",
  749. .pm = &usbhsc_pm_ops,
  750. .of_match_table = of_match_ptr(usbhs_of_match),
  751. },
  752. .probe = usbhs_probe,
  753. .remove = usbhs_remove,
  754. };
  755. module_platform_driver(renesas_usbhs_driver);
  756. MODULE_LICENSE("GPL");
  757. MODULE_DESCRIPTION("Renesas USB driver");
  758. MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");