common.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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/err.h>
  18. #include <linux/io.h>
  19. #include <linux/module.h>
  20. #include <linux/pm_runtime.h>
  21. #include <linux/slab.h>
  22. #include <linux/sysfs.h>
  23. #include "common.h"
  24. /*
  25. * image of renesas_usbhs
  26. *
  27. * ex) gadget case
  28. * mod.c
  29. * mod_gadget.c
  30. * mod_host.c pipe.c fifo.c
  31. *
  32. * +-------+ +-----------+
  33. * | pipe0 |------>| fifo pio |
  34. * +------------+ +-------+ +-----------+
  35. * | mod_gadget |=====> | pipe1 |--+
  36. * +------------+ +-------+ | +-----------+
  37. * | pipe2 | | +-| fifo dma0 |
  38. * +------------+ +-------+ | | +-----------+
  39. * | mod_host | | pipe3 |<-|--+
  40. * +------------+ +-------+ | +-----------+
  41. * | .... | +--->| fifo dma1 |
  42. * | .... | +-----------+
  43. */
  44. #define USBHSF_RUNTIME_PWCTRL (1 << 0)
  45. /* status */
  46. #define usbhsc_flags_init(p) do {(p)->flags = 0; } while (0)
  47. #define usbhsc_flags_set(p, b) ((p)->flags |= (b))
  48. #define usbhsc_flags_clr(p, b) ((p)->flags &= ~(b))
  49. #define usbhsc_flags_has(p, b) ((p)->flags & (b))
  50. /*
  51. * platform call back
  52. *
  53. * renesas usb support platform callback function.
  54. * Below macro call it.
  55. * if platform doesn't have callback, it return 0 (no error)
  56. */
  57. #define usbhs_platform_call(priv, func, args...)\
  58. (!(priv) ? -ENODEV : \
  59. !((priv)->pfunc.func) ? 0 : \
  60. (priv)->pfunc.func(args))
  61. /*
  62. * common functions
  63. */
  64. u16 usbhs_read(struct usbhs_priv *priv, u32 reg)
  65. {
  66. return ioread16(priv->base + reg);
  67. }
  68. void usbhs_write(struct usbhs_priv *priv, u32 reg, u16 data)
  69. {
  70. iowrite16(data, priv->base + reg);
  71. }
  72. void usbhs_bset(struct usbhs_priv *priv, u32 reg, u16 mask, u16 data)
  73. {
  74. u16 val = usbhs_read(priv, reg);
  75. val &= ~mask;
  76. val |= data & mask;
  77. usbhs_write(priv, reg, val);
  78. }
  79. struct usbhs_priv *usbhs_pdev_to_priv(struct platform_device *pdev)
  80. {
  81. return dev_get_drvdata(&pdev->dev);
  82. }
  83. /*
  84. * syscfg functions
  85. */
  86. static void usbhs_sys_clock_ctrl(struct usbhs_priv *priv, int enable)
  87. {
  88. usbhs_bset(priv, SYSCFG, SCKE, enable ? SCKE : 0);
  89. }
  90. void usbhs_sys_host_ctrl(struct usbhs_priv *priv, int enable)
  91. {
  92. u16 mask = DCFM | DRPD | DPRPU | HSE | USBE;
  93. u16 val = DCFM | DRPD | HSE | USBE;
  94. int has_otg = usbhs_get_dparam(priv, has_otg);
  95. if (has_otg)
  96. usbhs_bset(priv, DVSTCTR, (EXTLP | PWEN), (EXTLP | PWEN));
  97. /*
  98. * if enable
  99. *
  100. * - select Host mode
  101. * - D+ Line/D- Line Pull-down
  102. */
  103. usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
  104. }
  105. void usbhs_sys_function_ctrl(struct usbhs_priv *priv, int enable)
  106. {
  107. u16 mask = DCFM | DRPD | DPRPU | HSE | USBE;
  108. u16 val = DPRPU | HSE | USBE;
  109. /*
  110. * if enable
  111. *
  112. * - select Function mode
  113. * - D+ Line Pull-up
  114. */
  115. usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
  116. }
  117. void usbhs_sys_function_pullup(struct usbhs_priv *priv, int enable)
  118. {
  119. usbhs_bset(priv, SYSCFG, DPRPU, enable ? DPRPU : 0);
  120. }
  121. void usbhs_sys_set_test_mode(struct usbhs_priv *priv, u16 mode)
  122. {
  123. usbhs_write(priv, TESTMODE, mode);
  124. }
  125. /*
  126. * frame functions
  127. */
  128. int usbhs_frame_get_num(struct usbhs_priv *priv)
  129. {
  130. return usbhs_read(priv, FRMNUM) & FRNM_MASK;
  131. }
  132. /*
  133. * usb request functions
  134. */
  135. void usbhs_usbreq_get_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
  136. {
  137. u16 val;
  138. val = usbhs_read(priv, USBREQ);
  139. req->bRequest = (val >> 8) & 0xFF;
  140. req->bRequestType = (val >> 0) & 0xFF;
  141. req->wValue = usbhs_read(priv, USBVAL);
  142. req->wIndex = usbhs_read(priv, USBINDX);
  143. req->wLength = usbhs_read(priv, USBLENG);
  144. }
  145. void usbhs_usbreq_set_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
  146. {
  147. usbhs_write(priv, USBREQ, (req->bRequest << 8) | req->bRequestType);
  148. usbhs_write(priv, USBVAL, req->wValue);
  149. usbhs_write(priv, USBINDX, req->wIndex);
  150. usbhs_write(priv, USBLENG, req->wLength);
  151. usbhs_bset(priv, DCPCTR, SUREQ, SUREQ);
  152. }
  153. /*
  154. * bus/vbus functions
  155. */
  156. void usbhs_bus_send_sof_enable(struct usbhs_priv *priv)
  157. {
  158. u16 status = usbhs_read(priv, DVSTCTR) & (USBRST | UACT);
  159. if (status != USBRST) {
  160. struct device *dev = usbhs_priv_to_dev(priv);
  161. dev_err(dev, "usbhs should be reset\n");
  162. }
  163. usbhs_bset(priv, DVSTCTR, (USBRST | UACT), UACT);
  164. }
  165. void usbhs_bus_send_reset(struct usbhs_priv *priv)
  166. {
  167. usbhs_bset(priv, DVSTCTR, (USBRST | UACT), USBRST);
  168. }
  169. int usbhs_bus_get_speed(struct usbhs_priv *priv)
  170. {
  171. u16 dvstctr = usbhs_read(priv, DVSTCTR);
  172. switch (RHST & dvstctr) {
  173. case RHST_LOW_SPEED:
  174. return USB_SPEED_LOW;
  175. case RHST_FULL_SPEED:
  176. return USB_SPEED_FULL;
  177. case RHST_HIGH_SPEED:
  178. return USB_SPEED_HIGH;
  179. }
  180. return USB_SPEED_UNKNOWN;
  181. }
  182. int usbhs_vbus_ctrl(struct usbhs_priv *priv, int enable)
  183. {
  184. struct platform_device *pdev = usbhs_priv_to_pdev(priv);
  185. return usbhs_platform_call(priv, set_vbus, pdev, enable);
  186. }
  187. static void usbhsc_bus_init(struct usbhs_priv *priv)
  188. {
  189. usbhs_write(priv, DVSTCTR, 0);
  190. usbhs_vbus_ctrl(priv, 0);
  191. }
  192. /*
  193. * device configuration
  194. */
  195. int usbhs_set_device_config(struct usbhs_priv *priv, int devnum,
  196. u16 upphub, u16 hubport, u16 speed)
  197. {
  198. struct device *dev = usbhs_priv_to_dev(priv);
  199. u16 usbspd = 0;
  200. u32 reg = DEVADD0 + (2 * devnum);
  201. if (devnum > 10) {
  202. dev_err(dev, "cannot set speed to unknown device %d\n", devnum);
  203. return -EIO;
  204. }
  205. if (upphub > 0xA) {
  206. dev_err(dev, "unsupported hub number %d\n", upphub);
  207. return -EIO;
  208. }
  209. switch (speed) {
  210. case USB_SPEED_LOW:
  211. usbspd = USBSPD_SPEED_LOW;
  212. break;
  213. case USB_SPEED_FULL:
  214. usbspd = USBSPD_SPEED_FULL;
  215. break;
  216. case USB_SPEED_HIGH:
  217. usbspd = USBSPD_SPEED_HIGH;
  218. break;
  219. default:
  220. dev_err(dev, "unsupported speed %d\n", speed);
  221. return -EIO;
  222. }
  223. usbhs_write(priv, reg, UPPHUB(upphub) |
  224. HUBPORT(hubport)|
  225. USBSPD(usbspd));
  226. return 0;
  227. }
  228. /*
  229. * local functions
  230. */
  231. static void usbhsc_set_buswait(struct usbhs_priv *priv)
  232. {
  233. int wait = usbhs_get_dparam(priv, buswait_bwait);
  234. /* set bus wait if platform have */
  235. if (wait)
  236. usbhs_bset(priv, BUSWAIT, 0x000F, wait);
  237. }
  238. /*
  239. * platform default param
  240. */
  241. static u32 usbhsc_default_pipe_type[] = {
  242. USB_ENDPOINT_XFER_CONTROL,
  243. USB_ENDPOINT_XFER_ISOC,
  244. USB_ENDPOINT_XFER_ISOC,
  245. USB_ENDPOINT_XFER_BULK,
  246. USB_ENDPOINT_XFER_BULK,
  247. USB_ENDPOINT_XFER_BULK,
  248. USB_ENDPOINT_XFER_INT,
  249. USB_ENDPOINT_XFER_INT,
  250. USB_ENDPOINT_XFER_INT,
  251. USB_ENDPOINT_XFER_INT,
  252. };
  253. /*
  254. * power control
  255. */
  256. static void usbhsc_power_ctrl(struct usbhs_priv *priv, int enable)
  257. {
  258. struct platform_device *pdev = usbhs_priv_to_pdev(priv);
  259. struct device *dev = usbhs_priv_to_dev(priv);
  260. if (enable) {
  261. /* enable PM */
  262. pm_runtime_get_sync(dev);
  263. /* enable platform power */
  264. usbhs_platform_call(priv, power_ctrl, pdev, priv->base, enable);
  265. /* USB on */
  266. usbhs_sys_clock_ctrl(priv, enable);
  267. } else {
  268. /* USB off */
  269. usbhs_sys_clock_ctrl(priv, enable);
  270. /* disable platform power */
  271. usbhs_platform_call(priv, power_ctrl, pdev, priv->base, enable);
  272. /* disable PM */
  273. pm_runtime_put_sync(dev);
  274. }
  275. }
  276. /*
  277. * hotplug
  278. */
  279. static void usbhsc_hotplug(struct usbhs_priv *priv)
  280. {
  281. struct platform_device *pdev = usbhs_priv_to_pdev(priv);
  282. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  283. int id;
  284. int enable;
  285. int ret;
  286. /*
  287. * get vbus status from platform
  288. */
  289. enable = usbhs_platform_call(priv, get_vbus, pdev);
  290. /*
  291. * get id from platform
  292. */
  293. id = usbhs_platform_call(priv, get_id, pdev);
  294. if (enable && !mod) {
  295. ret = usbhs_mod_change(priv, id);
  296. if (ret < 0)
  297. return;
  298. dev_dbg(&pdev->dev, "%s enable\n", __func__);
  299. /* power on */
  300. if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
  301. usbhsc_power_ctrl(priv, enable);
  302. /* bus init */
  303. usbhsc_set_buswait(priv);
  304. usbhsc_bus_init(priv);
  305. /* module start */
  306. usbhs_mod_call(priv, start, priv);
  307. } else if (!enable && mod) {
  308. dev_dbg(&pdev->dev, "%s disable\n", __func__);
  309. /* module stop */
  310. usbhs_mod_call(priv, stop, priv);
  311. /* bus init */
  312. usbhsc_bus_init(priv);
  313. /* power off */
  314. if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
  315. usbhsc_power_ctrl(priv, enable);
  316. usbhs_mod_change(priv, -1);
  317. /* reset phy for next connection */
  318. usbhs_platform_call(priv, phy_reset, pdev);
  319. }
  320. }
  321. /*
  322. * notify hotplug
  323. */
  324. static void usbhsc_notify_hotplug(struct work_struct *work)
  325. {
  326. struct usbhs_priv *priv = container_of(work,
  327. struct usbhs_priv,
  328. notify_hotplug_work.work);
  329. usbhsc_hotplug(priv);
  330. }
  331. static int usbhsc_drvcllbck_notify_hotplug(struct platform_device *pdev)
  332. {
  333. struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
  334. int delay = usbhs_get_dparam(priv, detection_delay);
  335. /*
  336. * This functions will be called in interrupt.
  337. * To make sure safety context,
  338. * use workqueue for usbhs_notify_hotplug
  339. */
  340. schedule_delayed_work(&priv->notify_hotplug_work,
  341. msecs_to_jiffies(delay));
  342. return 0;
  343. }
  344. /*
  345. * platform functions
  346. */
  347. static int usbhs_probe(struct platform_device *pdev)
  348. {
  349. struct renesas_usbhs_platform_info *info = pdev->dev.platform_data;
  350. struct renesas_usbhs_driver_callback *dfunc;
  351. struct usbhs_priv *priv;
  352. struct resource *res, *irq_res;
  353. int ret;
  354. /* check platform information */
  355. if (!info ||
  356. !info->platform_callback.get_id) {
  357. dev_err(&pdev->dev, "no platform information\n");
  358. return -EINVAL;
  359. }
  360. /* platform data */
  361. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  362. irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  363. if (!res || !irq_res) {
  364. dev_err(&pdev->dev, "Not enough Renesas USB platform resources.\n");
  365. return -ENODEV;
  366. }
  367. /* usb private data */
  368. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  369. if (!priv) {
  370. dev_err(&pdev->dev, "Could not allocate priv\n");
  371. return -ENOMEM;
  372. }
  373. priv->base = devm_ioremap_resource(&pdev->dev, res);
  374. if (IS_ERR(priv->base))
  375. return PTR_ERR(priv->base);
  376. /*
  377. * care platform info
  378. */
  379. memcpy(&priv->pfunc,
  380. &info->platform_callback,
  381. sizeof(struct renesas_usbhs_platform_callback));
  382. memcpy(&priv->dparam,
  383. &info->driver_param,
  384. sizeof(struct renesas_usbhs_driver_param));
  385. /* set driver callback functions for platform */
  386. dfunc = &info->driver_callback;
  387. dfunc->notify_hotplug = usbhsc_drvcllbck_notify_hotplug;
  388. /* set default param if platform doesn't have */
  389. if (!priv->dparam.pipe_type) {
  390. priv->dparam.pipe_type = usbhsc_default_pipe_type;
  391. priv->dparam.pipe_size = ARRAY_SIZE(usbhsc_default_pipe_type);
  392. }
  393. if (!priv->dparam.pio_dma_border)
  394. priv->dparam.pio_dma_border = 64; /* 64byte */
  395. /* FIXME */
  396. /* runtime power control ? */
  397. if (priv->pfunc.get_vbus)
  398. usbhsc_flags_set(priv, USBHSF_RUNTIME_PWCTRL);
  399. /*
  400. * priv settings
  401. */
  402. priv->irq = irq_res->start;
  403. if (irq_res->flags & IORESOURCE_IRQ_SHAREABLE)
  404. priv->irqflags = IRQF_SHARED;
  405. priv->pdev = pdev;
  406. INIT_DELAYED_WORK(&priv->notify_hotplug_work, usbhsc_notify_hotplug);
  407. spin_lock_init(usbhs_priv_to_lock(priv));
  408. /* call pipe and module init */
  409. ret = usbhs_pipe_probe(priv);
  410. if (ret < 0)
  411. return ret;
  412. ret = usbhs_fifo_probe(priv);
  413. if (ret < 0)
  414. goto probe_end_pipe_exit;
  415. ret = usbhs_mod_probe(priv);
  416. if (ret < 0)
  417. goto probe_end_fifo_exit;
  418. /* dev_set_drvdata should be called after usbhs_mod_init */
  419. dev_set_drvdata(&pdev->dev, priv);
  420. /*
  421. * deviece reset here because
  422. * USB device might be used in boot loader.
  423. */
  424. usbhs_sys_clock_ctrl(priv, 0);
  425. /*
  426. * platform call
  427. *
  428. * USB phy setup might depend on CPU/Board.
  429. * If platform has its callback functions,
  430. * call it here.
  431. */
  432. ret = usbhs_platform_call(priv, hardware_init, pdev);
  433. if (ret < 0) {
  434. dev_err(&pdev->dev, "platform prove failed.\n");
  435. goto probe_end_mod_exit;
  436. }
  437. /* reset phy for connection */
  438. usbhs_platform_call(priv, phy_reset, pdev);
  439. /* power control */
  440. pm_runtime_enable(&pdev->dev);
  441. if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) {
  442. usbhsc_power_ctrl(priv, 1);
  443. usbhs_mod_autonomy_mode(priv);
  444. }
  445. /*
  446. * manual call notify_hotplug for cold plug
  447. */
  448. ret = usbhsc_drvcllbck_notify_hotplug(pdev);
  449. if (ret < 0)
  450. goto probe_end_call_remove;
  451. dev_info(&pdev->dev, "probed\n");
  452. return ret;
  453. probe_end_call_remove:
  454. usbhs_platform_call(priv, hardware_exit, pdev);
  455. probe_end_mod_exit:
  456. usbhs_mod_remove(priv);
  457. probe_end_fifo_exit:
  458. usbhs_fifo_remove(priv);
  459. probe_end_pipe_exit:
  460. usbhs_pipe_remove(priv);
  461. dev_info(&pdev->dev, "probe failed\n");
  462. return ret;
  463. }
  464. static int usbhs_remove(struct platform_device *pdev)
  465. {
  466. struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
  467. struct renesas_usbhs_platform_info *info = pdev->dev.platform_data;
  468. struct renesas_usbhs_driver_callback *dfunc = &info->driver_callback;
  469. dev_dbg(&pdev->dev, "usb remove\n");
  470. dfunc->notify_hotplug = NULL;
  471. /* power off */
  472. if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
  473. usbhsc_power_ctrl(priv, 0);
  474. pm_runtime_disable(&pdev->dev);
  475. usbhs_platform_call(priv, hardware_exit, pdev);
  476. usbhs_mod_remove(priv);
  477. usbhs_fifo_remove(priv);
  478. usbhs_pipe_remove(priv);
  479. return 0;
  480. }
  481. static int usbhsc_suspend(struct device *dev)
  482. {
  483. struct usbhs_priv *priv = dev_get_drvdata(dev);
  484. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  485. if (mod) {
  486. usbhs_mod_call(priv, stop, priv);
  487. usbhs_mod_change(priv, -1);
  488. }
  489. if (mod || !usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
  490. usbhsc_power_ctrl(priv, 0);
  491. return 0;
  492. }
  493. static int usbhsc_resume(struct device *dev)
  494. {
  495. struct usbhs_priv *priv = dev_get_drvdata(dev);
  496. struct platform_device *pdev = usbhs_priv_to_pdev(priv);
  497. if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
  498. usbhsc_power_ctrl(priv, 1);
  499. usbhs_platform_call(priv, phy_reset, pdev);
  500. usbhsc_drvcllbck_notify_hotplug(pdev);
  501. return 0;
  502. }
  503. static int usbhsc_runtime_nop(struct device *dev)
  504. {
  505. /* Runtime PM callback shared between ->runtime_suspend()
  506. * and ->runtime_resume(). Simply returns success.
  507. *
  508. * This driver re-initializes all registers after
  509. * pm_runtime_get_sync() anyway so there is no need
  510. * to save and restore registers here.
  511. */
  512. return 0;
  513. }
  514. static const struct dev_pm_ops usbhsc_pm_ops = {
  515. .suspend = usbhsc_suspend,
  516. .resume = usbhsc_resume,
  517. .runtime_suspend = usbhsc_runtime_nop,
  518. .runtime_resume = usbhsc_runtime_nop,
  519. };
  520. static struct platform_driver renesas_usbhs_driver = {
  521. .driver = {
  522. .name = "renesas_usbhs",
  523. .pm = &usbhsc_pm_ops,
  524. },
  525. .probe = usbhs_probe,
  526. .remove = usbhs_remove,
  527. };
  528. module_platform_driver(renesas_usbhs_driver);
  529. MODULE_LICENSE("GPL");
  530. MODULE_DESCRIPTION("Renesas USB driver");
  531. MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");