common.c 8.9 KB

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