common.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. * platform call back
  25. *
  26. * renesas usb support platform callback function.
  27. * Below macro call it.
  28. * if platform doesn't have callback, it return 0 (no error)
  29. */
  30. #define usbhs_platform_call(priv, func, args...)\
  31. (!(priv) ? -ENODEV : \
  32. !((priv)->pfunc->func) ? 0 : \
  33. (priv)->pfunc->func(args))
  34. /*
  35. * common functions
  36. */
  37. u16 usbhs_read(struct usbhs_priv *priv, u32 reg)
  38. {
  39. return ioread16(priv->base + reg);
  40. }
  41. void usbhs_write(struct usbhs_priv *priv, u32 reg, u16 data)
  42. {
  43. iowrite16(data, priv->base + reg);
  44. }
  45. void usbhs_bset(struct usbhs_priv *priv, u32 reg, u16 mask, u16 data)
  46. {
  47. u16 val = usbhs_read(priv, reg);
  48. val &= ~mask;
  49. val |= data & mask;
  50. usbhs_write(priv, reg, val);
  51. }
  52. /*
  53. * syscfg functions
  54. */
  55. void usbhs_sys_clock_ctrl(struct usbhs_priv *priv, int enable)
  56. {
  57. usbhs_bset(priv, SYSCFG, SCKE, enable ? SCKE : 0);
  58. }
  59. void usbhs_sys_hispeed_ctrl(struct usbhs_priv *priv, int enable)
  60. {
  61. usbhs_bset(priv, SYSCFG, HSE, enable ? HSE : 0);
  62. }
  63. void usbhs_sys_usb_ctrl(struct usbhs_priv *priv, int enable)
  64. {
  65. usbhs_bset(priv, SYSCFG, USBE, enable ? USBE : 0);
  66. }
  67. void usbhs_sys_host_ctrl(struct usbhs_priv *priv, int enable)
  68. {
  69. u16 mask = DCFM | DRPD | DPRPU;
  70. u16 val = DCFM | DRPD;
  71. /*
  72. * if enable
  73. *
  74. * - select Host mode
  75. * - D+ Line/D- Line Pull-down
  76. */
  77. usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
  78. }
  79. void usbhs_sys_function_ctrl(struct usbhs_priv *priv, int enable)
  80. {
  81. u16 mask = DCFM | DRPD | DPRPU;
  82. u16 val = DPRPU;
  83. /*
  84. * if enable
  85. *
  86. * - select Function mode
  87. * - D+ Line Pull-up
  88. */
  89. usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
  90. }
  91. /*
  92. * frame functions
  93. */
  94. int usbhs_frame_get_num(struct usbhs_priv *priv)
  95. {
  96. return usbhs_read(priv, FRMNUM) & FRNM_MASK;
  97. }
  98. /*
  99. * local functions
  100. */
  101. static struct usbhs_priv *usbhsc_pdev_to_priv(struct platform_device *pdev)
  102. {
  103. return dev_get_drvdata(&pdev->dev);
  104. }
  105. static void usbhsc_bus_ctrl(struct usbhs_priv *priv, int enable)
  106. {
  107. int wait = usbhs_get_dparam(priv, buswait_bwait);
  108. u16 data = 0;
  109. if (enable) {
  110. /* set bus wait if platform have */
  111. if (wait)
  112. usbhs_bset(priv, BUSWAIT, 0x000F, wait);
  113. }
  114. usbhs_write(priv, DVSTCTR, data);
  115. }
  116. /*
  117. * platform default param
  118. */
  119. static u32 usbhsc_default_pipe_type[] = {
  120. USB_ENDPOINT_XFER_CONTROL,
  121. USB_ENDPOINT_XFER_ISOC,
  122. USB_ENDPOINT_XFER_ISOC,
  123. USB_ENDPOINT_XFER_BULK,
  124. USB_ENDPOINT_XFER_BULK,
  125. USB_ENDPOINT_XFER_BULK,
  126. USB_ENDPOINT_XFER_INT,
  127. USB_ENDPOINT_XFER_INT,
  128. USB_ENDPOINT_XFER_INT,
  129. USB_ENDPOINT_XFER_INT,
  130. };
  131. /*
  132. * driver callback functions
  133. */
  134. static void usbhsc_notify_hotplug(struct work_struct *work)
  135. {
  136. struct usbhs_priv *priv = container_of(work,
  137. struct usbhs_priv,
  138. notify_hotplug_work);
  139. struct platform_device *pdev = usbhs_priv_to_pdev(priv);
  140. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  141. int id;
  142. int enable;
  143. int ret;
  144. /*
  145. * get vbus status from platform
  146. */
  147. enable = usbhs_platform_call(priv, get_vbus, pdev);
  148. /*
  149. * get id from platform
  150. */
  151. id = usbhs_platform_call(priv, get_id, pdev);
  152. if (enable && !mod) {
  153. ret = usbhs_mod_change(priv, id);
  154. if (ret < 0)
  155. return;
  156. dev_dbg(&pdev->dev, "%s enable\n", __func__);
  157. /* enable PM */
  158. pm_runtime_get_sync(&pdev->dev);
  159. /* USB on */
  160. usbhs_sys_clock_ctrl(priv, enable);
  161. usbhsc_bus_ctrl(priv, enable);
  162. /* module start */
  163. usbhs_mod_call(priv, start, priv);
  164. } else if (!enable && mod) {
  165. dev_dbg(&pdev->dev, "%s disable\n", __func__);
  166. /* module stop */
  167. usbhs_mod_call(priv, stop, priv);
  168. /* USB off */
  169. usbhsc_bus_ctrl(priv, enable);
  170. usbhs_sys_clock_ctrl(priv, enable);
  171. /* disable PM */
  172. pm_runtime_put_sync(&pdev->dev);
  173. usbhs_mod_change(priv, -1);
  174. /* reset phy for next connection */
  175. usbhs_platform_call(priv, phy_reset, pdev);
  176. }
  177. }
  178. static int usbhsc_drvcllbck_notify_hotplug(struct platform_device *pdev)
  179. {
  180. struct usbhs_priv *priv = usbhsc_pdev_to_priv(pdev);
  181. /*
  182. * This functions will be called in interrupt.
  183. * To make sure safety context,
  184. * use workqueue for usbhs_notify_hotplug
  185. */
  186. schedule_work(&priv->notify_hotplug_work);
  187. return 0;
  188. }
  189. /*
  190. * platform functions
  191. */
  192. static int __devinit usbhs_probe(struct platform_device *pdev)
  193. {
  194. struct renesas_usbhs_platform_info *info = pdev->dev.platform_data;
  195. struct renesas_usbhs_driver_callback *dfunc;
  196. struct usbhs_priv *priv;
  197. struct resource *res;
  198. unsigned int irq;
  199. int ret;
  200. /* check platform information */
  201. if (!info ||
  202. !info->platform_callback.get_id ||
  203. !info->platform_callback.get_vbus) {
  204. dev_err(&pdev->dev, "no platform information\n");
  205. return -EINVAL;
  206. }
  207. /* platform data */
  208. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  209. irq = platform_get_irq(pdev, 0);
  210. if (!res || (int)irq <= 0) {
  211. dev_err(&pdev->dev, "Not enough Renesas USB platform resources.\n");
  212. return -ENODEV;
  213. }
  214. /* usb private data */
  215. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  216. if (!priv) {
  217. dev_err(&pdev->dev, "Could not allocate priv\n");
  218. return -ENOMEM;
  219. }
  220. priv->base = ioremap_nocache(res->start, resource_size(res));
  221. if (!priv->base) {
  222. dev_err(&pdev->dev, "ioremap error.\n");
  223. ret = -ENOMEM;
  224. goto probe_end_kfree;
  225. }
  226. /*
  227. * care platform info
  228. */
  229. priv->pfunc = &info->platform_callback;
  230. priv->dparam = &info->driver_param;
  231. /* set driver callback functions for platform */
  232. dfunc = &info->driver_callback;
  233. dfunc->notify_hotplug = usbhsc_drvcllbck_notify_hotplug;
  234. /* set default param if platform doesn't have */
  235. if (!priv->dparam->pipe_type) {
  236. priv->dparam->pipe_type = usbhsc_default_pipe_type;
  237. priv->dparam->pipe_size = ARRAY_SIZE(usbhsc_default_pipe_type);
  238. }
  239. /*
  240. * priv settings
  241. */
  242. priv->irq = irq;
  243. priv->pdev = pdev;
  244. INIT_WORK(&priv->notify_hotplug_work, usbhsc_notify_hotplug);
  245. spin_lock_init(usbhs_priv_to_lock(priv));
  246. /* call pipe and module init */
  247. ret = usbhs_pipe_probe(priv);
  248. if (ret < 0)
  249. goto probe_end_mod_exit;
  250. ret = usbhs_mod_probe(priv);
  251. if (ret < 0)
  252. goto probe_end_iounmap;
  253. /* dev_set_drvdata should be called after usbhs_mod_init */
  254. dev_set_drvdata(&pdev->dev, priv);
  255. /*
  256. * deviece reset here because
  257. * USB device might be used in boot loader.
  258. */
  259. usbhs_sys_clock_ctrl(priv, 0);
  260. /*
  261. * platform call
  262. *
  263. * USB phy setup might depend on CPU/Board.
  264. * If platform has its callback functions,
  265. * call it here.
  266. */
  267. ret = usbhs_platform_call(priv, hardware_init, pdev);
  268. if (ret < 0) {
  269. dev_err(&pdev->dev, "platform prove failed.\n");
  270. goto probe_end_pipe_exit;
  271. }
  272. /* reset phy for connection */
  273. usbhs_platform_call(priv, phy_reset, pdev);
  274. /*
  275. * manual call notify_hotplug for cold plug
  276. */
  277. pm_runtime_enable(&pdev->dev);
  278. ret = usbhsc_drvcllbck_notify_hotplug(pdev);
  279. if (ret < 0)
  280. goto probe_end_call_remove;
  281. dev_info(&pdev->dev, "probed\n");
  282. return ret;
  283. probe_end_call_remove:
  284. usbhs_platform_call(priv, hardware_exit, pdev);
  285. probe_end_pipe_exit:
  286. usbhs_pipe_remove(priv);
  287. probe_end_mod_exit:
  288. usbhs_mod_remove(priv);
  289. probe_end_iounmap:
  290. iounmap(priv->base);
  291. probe_end_kfree:
  292. kfree(priv);
  293. dev_info(&pdev->dev, "probe failed\n");
  294. return ret;
  295. }
  296. static int __devexit usbhs_remove(struct platform_device *pdev)
  297. {
  298. struct usbhs_priv *priv = usbhsc_pdev_to_priv(pdev);
  299. struct renesas_usbhs_platform_info *info = pdev->dev.platform_data;
  300. struct renesas_usbhs_driver_callback *dfunc = &info->driver_callback;
  301. dev_dbg(&pdev->dev, "usb remove\n");
  302. dfunc->notify_hotplug = NULL;
  303. pm_runtime_disable(&pdev->dev);
  304. usbhsc_bus_ctrl(priv, 0);
  305. usbhs_platform_call(priv, hardware_exit, pdev);
  306. usbhs_pipe_remove(priv);
  307. usbhs_mod_remove(priv);
  308. iounmap(priv->base);
  309. kfree(priv);
  310. return 0;
  311. }
  312. static struct platform_driver renesas_usbhs_driver = {
  313. .driver = {
  314. .name = "renesas_usbhs",
  315. },
  316. .probe = usbhs_probe,
  317. .remove = __devexit_p(usbhs_remove),
  318. };
  319. static int __init usbhs_init(void)
  320. {
  321. return platform_driver_register(&renesas_usbhs_driver);
  322. }
  323. static void __exit usbhs_exit(void)
  324. {
  325. platform_driver_unregister(&renesas_usbhs_driver);
  326. }
  327. module_init(usbhs_init);
  328. module_exit(usbhs_exit);
  329. MODULE_LICENSE("GPL");
  330. MODULE_DESCRIPTION("Renesas USB driver");
  331. MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");