common.c 14 KB

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