common.c 19 KB

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