common.c 19 KB

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