common.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. * local functions
  130. */
  131. static void usbhsc_bus_ctrl(struct usbhs_priv *priv, int enable)
  132. {
  133. int wait = usbhs_get_dparam(priv, buswait_bwait);
  134. u16 data = 0;
  135. if (enable) {
  136. /* set bus wait if platform have */
  137. if (wait)
  138. usbhs_bset(priv, BUSWAIT, 0x000F, wait);
  139. }
  140. usbhs_write(priv, DVSTCTR, data);
  141. }
  142. /*
  143. * platform default param
  144. */
  145. static u32 usbhsc_default_pipe_type[] = {
  146. USB_ENDPOINT_XFER_CONTROL,
  147. USB_ENDPOINT_XFER_ISOC,
  148. USB_ENDPOINT_XFER_ISOC,
  149. USB_ENDPOINT_XFER_BULK,
  150. USB_ENDPOINT_XFER_BULK,
  151. USB_ENDPOINT_XFER_BULK,
  152. USB_ENDPOINT_XFER_INT,
  153. USB_ENDPOINT_XFER_INT,
  154. USB_ENDPOINT_XFER_INT,
  155. USB_ENDPOINT_XFER_INT,
  156. };
  157. /*
  158. * power control
  159. */
  160. static void usbhsc_power_ctrl(struct usbhs_priv *priv, int enable)
  161. {
  162. struct device *dev = usbhs_priv_to_dev(priv);
  163. if (enable) {
  164. /* enable PM */
  165. pm_runtime_get_sync(dev);
  166. /* USB on */
  167. usbhs_sys_clock_ctrl(priv, enable);
  168. usbhsc_bus_ctrl(priv, enable);
  169. } else {
  170. /* USB off */
  171. usbhsc_bus_ctrl(priv, enable);
  172. usbhs_sys_clock_ctrl(priv, enable);
  173. /* disable PM */
  174. pm_runtime_put_sync(dev);
  175. }
  176. }
  177. /*
  178. * notify hotplug
  179. */
  180. static void usbhsc_notify_hotplug(struct work_struct *work)
  181. {
  182. struct usbhs_priv *priv = container_of(work,
  183. struct usbhs_priv,
  184. notify_hotplug_work.work);
  185. struct platform_device *pdev = usbhs_priv_to_pdev(priv);
  186. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  187. int id;
  188. int enable;
  189. int ret;
  190. /*
  191. * get vbus status from platform
  192. */
  193. enable = usbhs_platform_call(priv, get_vbus, pdev);
  194. /*
  195. * get id from platform
  196. */
  197. id = usbhs_platform_call(priv, get_id, pdev);
  198. if (enable && !mod) {
  199. ret = usbhs_mod_change(priv, id);
  200. if (ret < 0)
  201. return;
  202. dev_dbg(&pdev->dev, "%s enable\n", __func__);
  203. /* power on */
  204. if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
  205. usbhsc_power_ctrl(priv, enable);
  206. /* module start */
  207. usbhs_mod_call(priv, start, priv);
  208. } else if (!enable && mod) {
  209. dev_dbg(&pdev->dev, "%s disable\n", __func__);
  210. /* module stop */
  211. usbhs_mod_call(priv, stop, priv);
  212. /* power off */
  213. if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
  214. usbhsc_power_ctrl(priv, enable);
  215. usbhs_mod_change(priv, -1);
  216. /* reset phy for next connection */
  217. usbhs_platform_call(priv, phy_reset, pdev);
  218. }
  219. }
  220. int usbhsc_drvcllbck_notify_hotplug(struct platform_device *pdev)
  221. {
  222. struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
  223. int delay = usbhs_get_dparam(priv, detection_delay);
  224. /*
  225. * This functions will be called in interrupt.
  226. * To make sure safety context,
  227. * use workqueue for usbhs_notify_hotplug
  228. */
  229. schedule_delayed_work(&priv->notify_hotplug_work, delay);
  230. return 0;
  231. }
  232. /*
  233. * platform functions
  234. */
  235. static int __devinit usbhs_probe(struct platform_device *pdev)
  236. {
  237. struct renesas_usbhs_platform_info *info = pdev->dev.platform_data;
  238. struct renesas_usbhs_driver_callback *dfunc;
  239. struct usbhs_priv *priv;
  240. struct resource *res;
  241. unsigned int irq;
  242. int ret;
  243. /* check platform information */
  244. if (!info ||
  245. !info->platform_callback.get_id) {
  246. dev_err(&pdev->dev, "no platform information\n");
  247. return -EINVAL;
  248. }
  249. /* platform data */
  250. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  251. irq = platform_get_irq(pdev, 0);
  252. if (!res || (int)irq <= 0) {
  253. dev_err(&pdev->dev, "Not enough Renesas USB platform resources.\n");
  254. return -ENODEV;
  255. }
  256. /* usb private data */
  257. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  258. if (!priv) {
  259. dev_err(&pdev->dev, "Could not allocate priv\n");
  260. return -ENOMEM;
  261. }
  262. priv->base = ioremap_nocache(res->start, resource_size(res));
  263. if (!priv->base) {
  264. dev_err(&pdev->dev, "ioremap error.\n");
  265. ret = -ENOMEM;
  266. goto probe_end_kfree;
  267. }
  268. /*
  269. * care platform info
  270. */
  271. priv->pfunc = &info->platform_callback;
  272. priv->dparam = &info->driver_param;
  273. /* set driver callback functions for platform */
  274. dfunc = &info->driver_callback;
  275. dfunc->notify_hotplug = usbhsc_drvcllbck_notify_hotplug;
  276. /* set default param if platform doesn't have */
  277. if (!priv->dparam->pipe_type) {
  278. priv->dparam->pipe_type = usbhsc_default_pipe_type;
  279. priv->dparam->pipe_size = ARRAY_SIZE(usbhsc_default_pipe_type);
  280. }
  281. if (!priv->dparam->pio_dma_border)
  282. priv->dparam->pio_dma_border = 64; /* 64byte */
  283. /* FIXME */
  284. /* runtime power control ? */
  285. if (priv->pfunc->get_vbus)
  286. usbhsc_flags_set(priv, USBHSF_RUNTIME_PWCTRL);
  287. /*
  288. * priv settings
  289. */
  290. priv->irq = irq;
  291. priv->pdev = pdev;
  292. INIT_DELAYED_WORK(&priv->notify_hotplug_work, usbhsc_notify_hotplug);
  293. spin_lock_init(usbhs_priv_to_lock(priv));
  294. /* call pipe and module init */
  295. ret = usbhs_pipe_probe(priv);
  296. if (ret < 0)
  297. goto probe_end_iounmap;
  298. ret = usbhs_fifo_probe(priv);
  299. if (ret < 0)
  300. goto probe_end_pipe_exit;
  301. ret = usbhs_mod_probe(priv);
  302. if (ret < 0)
  303. goto probe_end_fifo_exit;
  304. /* dev_set_drvdata should be called after usbhs_mod_init */
  305. dev_set_drvdata(&pdev->dev, priv);
  306. /*
  307. * deviece reset here because
  308. * USB device might be used in boot loader.
  309. */
  310. usbhs_sys_clock_ctrl(priv, 0);
  311. /*
  312. * platform call
  313. *
  314. * USB phy setup might depend on CPU/Board.
  315. * If platform has its callback functions,
  316. * call it here.
  317. */
  318. ret = usbhs_platform_call(priv, hardware_init, pdev);
  319. if (ret < 0) {
  320. dev_err(&pdev->dev, "platform prove failed.\n");
  321. goto probe_end_mod_exit;
  322. }
  323. /* reset phy for connection */
  324. usbhs_platform_call(priv, phy_reset, pdev);
  325. /* power control */
  326. pm_runtime_enable(&pdev->dev);
  327. if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) {
  328. usbhsc_power_ctrl(priv, 1);
  329. usbhs_mod_autonomy_mode(priv);
  330. }
  331. /*
  332. * manual call notify_hotplug for cold plug
  333. */
  334. ret = usbhsc_drvcllbck_notify_hotplug(pdev);
  335. if (ret < 0)
  336. goto probe_end_call_remove;
  337. dev_info(&pdev->dev, "probed\n");
  338. return ret;
  339. probe_end_call_remove:
  340. usbhs_platform_call(priv, hardware_exit, pdev);
  341. probe_end_mod_exit:
  342. usbhs_mod_remove(priv);
  343. probe_end_fifo_exit:
  344. usbhs_fifo_remove(priv);
  345. probe_end_pipe_exit:
  346. usbhs_pipe_remove(priv);
  347. probe_end_iounmap:
  348. iounmap(priv->base);
  349. probe_end_kfree:
  350. kfree(priv);
  351. dev_info(&pdev->dev, "probe failed\n");
  352. return ret;
  353. }
  354. static int __devexit usbhs_remove(struct platform_device *pdev)
  355. {
  356. struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
  357. struct renesas_usbhs_platform_info *info = pdev->dev.platform_data;
  358. struct renesas_usbhs_driver_callback *dfunc = &info->driver_callback;
  359. dev_dbg(&pdev->dev, "usb remove\n");
  360. dfunc->notify_hotplug = NULL;
  361. /* power off */
  362. if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
  363. usbhsc_power_ctrl(priv, 0);
  364. pm_runtime_disable(&pdev->dev);
  365. usbhs_platform_call(priv, hardware_exit, pdev);
  366. usbhs_mod_remove(priv);
  367. usbhs_fifo_remove(priv);
  368. usbhs_pipe_remove(priv);
  369. iounmap(priv->base);
  370. kfree(priv);
  371. return 0;
  372. }
  373. static struct platform_driver renesas_usbhs_driver = {
  374. .driver = {
  375. .name = "renesas_usbhs",
  376. },
  377. .probe = usbhs_probe,
  378. .remove = __devexit_p(usbhs_remove),
  379. };
  380. static int __init usbhs_init(void)
  381. {
  382. return platform_driver_register(&renesas_usbhs_driver);
  383. }
  384. static void __exit usbhs_exit(void)
  385. {
  386. platform_driver_unregister(&renesas_usbhs_driver);
  387. }
  388. module_init(usbhs_init);
  389. module_exit(usbhs_exit);
  390. MODULE_LICENSE("GPL");
  391. MODULE_DESCRIPTION("Renesas USB driver");
  392. MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");