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