common.c 9.7 KB

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