common.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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/io.h>
  18. #include <linux/module.h>
  19. #include <linux/pm_runtime.h>
  20. #include <linux/slab.h>
  21. #include <linux/sysfs.h>
  22. #include "./common.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. 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_hispeed_ctrl(struct usbhs_priv *priv, int enable)
  90. {
  91. usbhs_bset(priv, SYSCFG, HSE, enable ? HSE : 0);
  92. }
  93. void usbhs_sys_usb_ctrl(struct usbhs_priv *priv, int enable)
  94. {
  95. usbhs_bset(priv, SYSCFG, USBE, enable ? USBE : 0);
  96. }
  97. void usbhs_sys_host_ctrl(struct usbhs_priv *priv, int enable)
  98. {
  99. u16 mask = DCFM | DRPD | DPRPU;
  100. u16 val = DCFM | DRPD;
  101. /*
  102. * if enable
  103. *
  104. * - select Host mode
  105. * - D+ Line/D- Line Pull-down
  106. */
  107. usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
  108. }
  109. void usbhs_sys_function_ctrl(struct usbhs_priv *priv, int enable)
  110. {
  111. u16 mask = DCFM | DRPD | DPRPU;
  112. u16 val = DPRPU;
  113. /*
  114. * if enable
  115. *
  116. * - select Function mode
  117. * - D+ Line Pull-up
  118. */
  119. usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
  120. }
  121. /*
  122. * frame functions
  123. */
  124. int usbhs_frame_get_num(struct usbhs_priv *priv)
  125. {
  126. return usbhs_read(priv, FRMNUM) & FRNM_MASK;
  127. }
  128. /*
  129. * usb request functions
  130. */
  131. void usbhs_usbreq_get_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
  132. {
  133. u16 val;
  134. val = usbhs_read(priv, USBREQ);
  135. req->bRequest = (val >> 8) & 0xFF;
  136. req->bRequestType = (val >> 0) & 0xFF;
  137. req->wValue = usbhs_read(priv, USBVAL);
  138. req->wIndex = usbhs_read(priv, USBINDX);
  139. req->wLength = usbhs_read(priv, USBLENG);
  140. }
  141. void usbhs_usbreq_set_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
  142. {
  143. usbhs_write(priv, USBREQ, (req->bRequest << 8) | req->bRequestType);
  144. usbhs_write(priv, USBVAL, req->wValue);
  145. usbhs_write(priv, USBINDX, req->wIndex);
  146. usbhs_write(priv, USBLENG, req->wLength);
  147. usbhs_bset(priv, DCPCTR, SUREQ, SUREQ);
  148. }
  149. /*
  150. * bus/vbus functions
  151. */
  152. void usbhs_bus_send_sof_enable(struct usbhs_priv *priv)
  153. {
  154. usbhs_bset(priv, DVSTCTR, (USBRST | UACT), UACT);
  155. }
  156. void usbhs_bus_send_reset(struct usbhs_priv *priv)
  157. {
  158. usbhs_bset(priv, DVSTCTR, (USBRST | UACT), USBRST);
  159. }
  160. int usbhs_bus_get_speed(struct usbhs_priv *priv)
  161. {
  162. u16 dvstctr = usbhs_read(priv, DVSTCTR);
  163. switch (RHST & dvstctr) {
  164. case RHST_LOW_SPEED:
  165. return USB_SPEED_LOW;
  166. case RHST_FULL_SPEED:
  167. return USB_SPEED_FULL;
  168. case RHST_HIGH_SPEED:
  169. return USB_SPEED_HIGH;
  170. }
  171. return USB_SPEED_UNKNOWN;
  172. }
  173. int usbhs_vbus_ctrl(struct usbhs_priv *priv, int enable)
  174. {
  175. struct platform_device *pdev = usbhs_priv_to_pdev(priv);
  176. return usbhs_platform_call(priv, set_vbus, pdev, enable);
  177. }
  178. static void usbhsc_bus_init(struct usbhs_priv *priv)
  179. {
  180. usbhs_write(priv, DVSTCTR, 0);
  181. usbhs_vbus_ctrl(priv, 0);
  182. }
  183. /*
  184. * local functions
  185. */
  186. static void usbhsc_set_buswait(struct usbhs_priv *priv)
  187. {
  188. int wait = usbhs_get_dparam(priv, buswait_bwait);
  189. /* set bus wait if platform have */
  190. if (wait)
  191. usbhs_bset(priv, BUSWAIT, 0x000F, wait);
  192. }
  193. /*
  194. * platform default param
  195. */
  196. static u32 usbhsc_default_pipe_type[] = {
  197. USB_ENDPOINT_XFER_CONTROL,
  198. USB_ENDPOINT_XFER_ISOC,
  199. USB_ENDPOINT_XFER_ISOC,
  200. USB_ENDPOINT_XFER_BULK,
  201. USB_ENDPOINT_XFER_BULK,
  202. USB_ENDPOINT_XFER_BULK,
  203. USB_ENDPOINT_XFER_INT,
  204. USB_ENDPOINT_XFER_INT,
  205. USB_ENDPOINT_XFER_INT,
  206. USB_ENDPOINT_XFER_INT,
  207. };
  208. /*
  209. * power control
  210. */
  211. static void usbhsc_power_ctrl(struct usbhs_priv *priv, int enable)
  212. {
  213. struct device *dev = usbhs_priv_to_dev(priv);
  214. if (enable) {
  215. /* enable PM */
  216. pm_runtime_get_sync(dev);
  217. /* USB on */
  218. usbhs_sys_clock_ctrl(priv, enable);
  219. } else {
  220. /* USB off */
  221. usbhs_sys_clock_ctrl(priv, enable);
  222. /* disable PM */
  223. pm_runtime_put_sync(dev);
  224. }
  225. }
  226. /*
  227. * hotplug
  228. */
  229. static void usbhsc_hotplug(struct usbhs_priv *priv)
  230. {
  231. struct platform_device *pdev = usbhs_priv_to_pdev(priv);
  232. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  233. int id;
  234. int enable;
  235. int ret;
  236. /*
  237. * get vbus status from platform
  238. */
  239. enable = usbhs_platform_call(priv, get_vbus, pdev);
  240. /*
  241. * get id from platform
  242. */
  243. id = usbhs_platform_call(priv, get_id, pdev);
  244. if (enable && !mod) {
  245. ret = usbhs_mod_change(priv, id);
  246. if (ret < 0)
  247. return;
  248. dev_dbg(&pdev->dev, "%s enable\n", __func__);
  249. /* power on */
  250. if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
  251. usbhsc_power_ctrl(priv, enable);
  252. /* bus init */
  253. usbhsc_set_buswait(priv);
  254. usbhsc_bus_init(priv);
  255. /* module start */
  256. usbhs_mod_call(priv, start, priv);
  257. } else if (!enable && mod) {
  258. dev_dbg(&pdev->dev, "%s disable\n", __func__);
  259. /* module stop */
  260. usbhs_mod_call(priv, stop, priv);
  261. /* bus init */
  262. usbhsc_bus_init(priv);
  263. /* power off */
  264. if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
  265. usbhsc_power_ctrl(priv, enable);
  266. usbhs_mod_change(priv, -1);
  267. /* reset phy for next connection */
  268. usbhs_platform_call(priv, phy_reset, pdev);
  269. }
  270. }
  271. /*
  272. * notify hotplug
  273. */
  274. static void usbhsc_notify_hotplug(struct work_struct *work)
  275. {
  276. struct usbhs_priv *priv = container_of(work,
  277. struct usbhs_priv,
  278. notify_hotplug_work.work);
  279. usbhsc_hotplug(priv);
  280. }
  281. int usbhsc_drvcllbck_notify_hotplug(struct platform_device *pdev)
  282. {
  283. struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
  284. int delay = usbhs_get_dparam(priv, detection_delay);
  285. /*
  286. * This functions will be called in interrupt.
  287. * To make sure safety context,
  288. * use workqueue for usbhs_notify_hotplug
  289. */
  290. schedule_delayed_work(&priv->notify_hotplug_work, delay);
  291. return 0;
  292. }
  293. /*
  294. * platform functions
  295. */
  296. static int __devinit usbhs_probe(struct platform_device *pdev)
  297. {
  298. struct renesas_usbhs_platform_info *info = pdev->dev.platform_data;
  299. struct renesas_usbhs_driver_callback *dfunc;
  300. struct usbhs_priv *priv;
  301. struct resource *res;
  302. unsigned int irq;
  303. int ret;
  304. /* check platform information */
  305. if (!info ||
  306. !info->platform_callback.get_id) {
  307. dev_err(&pdev->dev, "no platform information\n");
  308. return -EINVAL;
  309. }
  310. /* platform data */
  311. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  312. irq = platform_get_irq(pdev, 0);
  313. if (!res || (int)irq <= 0) {
  314. dev_err(&pdev->dev, "Not enough Renesas USB platform resources.\n");
  315. return -ENODEV;
  316. }
  317. /* usb private data */
  318. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  319. if (!priv) {
  320. dev_err(&pdev->dev, "Could not allocate priv\n");
  321. return -ENOMEM;
  322. }
  323. priv->base = ioremap_nocache(res->start, resource_size(res));
  324. if (!priv->base) {
  325. dev_err(&pdev->dev, "ioremap error.\n");
  326. ret = -ENOMEM;
  327. goto probe_end_kfree;
  328. }
  329. /*
  330. * care platform info
  331. */
  332. priv->pfunc = &info->platform_callback;
  333. priv->dparam = &info->driver_param;
  334. /* set driver callback functions for platform */
  335. dfunc = &info->driver_callback;
  336. dfunc->notify_hotplug = usbhsc_drvcllbck_notify_hotplug;
  337. /* set default param if platform doesn't have */
  338. if (!priv->dparam->pipe_type) {
  339. priv->dparam->pipe_type = usbhsc_default_pipe_type;
  340. priv->dparam->pipe_size = ARRAY_SIZE(usbhsc_default_pipe_type);
  341. }
  342. if (!priv->dparam->pio_dma_border)
  343. priv->dparam->pio_dma_border = 64; /* 64byte */
  344. /* FIXME */
  345. /* runtime power control ? */
  346. if (priv->pfunc->get_vbus)
  347. usbhsc_flags_set(priv, USBHSF_RUNTIME_PWCTRL);
  348. /*
  349. * priv settings
  350. */
  351. priv->irq = irq;
  352. priv->pdev = pdev;
  353. INIT_DELAYED_WORK(&priv->notify_hotplug_work, usbhsc_notify_hotplug);
  354. spin_lock_init(usbhs_priv_to_lock(priv));
  355. /* call pipe and module init */
  356. ret = usbhs_pipe_probe(priv);
  357. if (ret < 0)
  358. goto probe_end_iounmap;
  359. ret = usbhs_fifo_probe(priv);
  360. if (ret < 0)
  361. goto probe_end_pipe_exit;
  362. ret = usbhs_mod_probe(priv);
  363. if (ret < 0)
  364. goto probe_end_fifo_exit;
  365. /* dev_set_drvdata should be called after usbhs_mod_init */
  366. dev_set_drvdata(&pdev->dev, priv);
  367. /*
  368. * deviece reset here because
  369. * USB device might be used in boot loader.
  370. */
  371. usbhs_sys_clock_ctrl(priv, 0);
  372. /*
  373. * platform call
  374. *
  375. * USB phy setup might depend on CPU/Board.
  376. * If platform has its callback functions,
  377. * call it here.
  378. */
  379. ret = usbhs_platform_call(priv, hardware_init, pdev);
  380. if (ret < 0) {
  381. dev_err(&pdev->dev, "platform prove failed.\n");
  382. goto probe_end_mod_exit;
  383. }
  384. /* reset phy for connection */
  385. usbhs_platform_call(priv, phy_reset, pdev);
  386. /* power control */
  387. pm_runtime_enable(&pdev->dev);
  388. if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) {
  389. usbhsc_power_ctrl(priv, 1);
  390. usbhs_mod_autonomy_mode(priv);
  391. }
  392. /*
  393. * manual call notify_hotplug for cold plug
  394. */
  395. ret = usbhsc_drvcllbck_notify_hotplug(pdev);
  396. if (ret < 0)
  397. goto probe_end_call_remove;
  398. dev_info(&pdev->dev, "probed\n");
  399. return ret;
  400. probe_end_call_remove:
  401. usbhs_platform_call(priv, hardware_exit, pdev);
  402. probe_end_mod_exit:
  403. usbhs_mod_remove(priv);
  404. probe_end_fifo_exit:
  405. usbhs_fifo_remove(priv);
  406. probe_end_pipe_exit:
  407. usbhs_pipe_remove(priv);
  408. probe_end_iounmap:
  409. iounmap(priv->base);
  410. probe_end_kfree:
  411. kfree(priv);
  412. dev_info(&pdev->dev, "probe failed\n");
  413. return ret;
  414. }
  415. static int __devexit usbhs_remove(struct platform_device *pdev)
  416. {
  417. struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
  418. struct renesas_usbhs_platform_info *info = pdev->dev.platform_data;
  419. struct renesas_usbhs_driver_callback *dfunc = &info->driver_callback;
  420. dev_dbg(&pdev->dev, "usb remove\n");
  421. dfunc->notify_hotplug = NULL;
  422. /* power off */
  423. if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
  424. usbhsc_power_ctrl(priv, 0);
  425. pm_runtime_disable(&pdev->dev);
  426. usbhs_platform_call(priv, hardware_exit, pdev);
  427. usbhs_mod_remove(priv);
  428. usbhs_fifo_remove(priv);
  429. usbhs_pipe_remove(priv);
  430. iounmap(priv->base);
  431. kfree(priv);
  432. return 0;
  433. }
  434. static int usbhsc_suspend(struct device *dev)
  435. {
  436. struct usbhs_priv *priv = dev_get_drvdata(dev);
  437. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  438. if (mod) {
  439. usbhs_mod_call(priv, stop, priv);
  440. usbhs_mod_change(priv, -1);
  441. }
  442. if (mod || !usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
  443. usbhsc_power_ctrl(priv, 0);
  444. return 0;
  445. }
  446. static int usbhsc_resume(struct device *dev)
  447. {
  448. struct usbhs_priv *priv = dev_get_drvdata(dev);
  449. struct platform_device *pdev = usbhs_priv_to_pdev(priv);
  450. usbhs_platform_call(priv, phy_reset, pdev);
  451. if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
  452. usbhsc_power_ctrl(priv, 1);
  453. usbhsc_hotplug(priv);
  454. return 0;
  455. }
  456. static int usbhsc_runtime_nop(struct device *dev)
  457. {
  458. /* Runtime PM callback shared between ->runtime_suspend()
  459. * and ->runtime_resume(). Simply returns success.
  460. *
  461. * This driver re-initializes all registers after
  462. * pm_runtime_get_sync() anyway so there is no need
  463. * to save and restore registers here.
  464. */
  465. return 0;
  466. }
  467. static const struct dev_pm_ops usbhsc_pm_ops = {
  468. .suspend = usbhsc_suspend,
  469. .resume = usbhsc_resume,
  470. .runtime_suspend = usbhsc_runtime_nop,
  471. .runtime_resume = usbhsc_runtime_nop,
  472. };
  473. static struct platform_driver renesas_usbhs_driver = {
  474. .driver = {
  475. .name = "renesas_usbhs",
  476. .pm = &usbhsc_pm_ops,
  477. },
  478. .probe = usbhs_probe,
  479. .remove = __devexit_p(usbhs_remove),
  480. };
  481. static int __init usbhs_init(void)
  482. {
  483. return platform_driver_register(&renesas_usbhs_driver);
  484. }
  485. static void __exit usbhs_exit(void)
  486. {
  487. platform_driver_unregister(&renesas_usbhs_driver);
  488. }
  489. module_init(usbhs_init);
  490. module_exit(usbhs_exit);
  491. MODULE_LICENSE("GPL");
  492. MODULE_DESCRIPTION("Renesas USB driver");
  493. MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");