platform.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. /*
  2. * platform.c - DesignWare HS OTG Controller platform driver
  3. *
  4. * Copyright (C) Matthijs Kooijman <matthijs@stdin.nl>
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions, and the following disclaimer,
  11. * without modification.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. The names of the above-listed copyright holders may not be used
  16. * to endorse or promote products derived from this software without
  17. * specific prior written permission.
  18. *
  19. * ALTERNATIVELY, this software may be distributed under the terms of the
  20. * GNU General Public License ("GPL") as published by the Free Software
  21. * Foundation; either version 2 of the License, or (at your option) any
  22. * later version.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  25. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  26. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  27. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  28. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  29. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  30. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  31. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  32. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  33. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  34. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. #include <linux/kernel.h>
  37. #include <linux/module.h>
  38. #include <linux/slab.h>
  39. #include <linux/clk.h>
  40. #include <linux/device.h>
  41. #include <linux/dma-mapping.h>
  42. #include <linux/of_device.h>
  43. #include <linux/mutex.h>
  44. #include <linux/platform_device.h>
  45. #include <linux/phy/phy.h>
  46. #include <linux/platform_data/s3c-hsotg.h>
  47. #include <linux/reset.h>
  48. #include <linux/usb/of.h>
  49. #include "core.h"
  50. #include "hcd.h"
  51. #include "debug.h"
  52. static const char dwc2_driver_name[] = "dwc2";
  53. static const struct dwc2_core_params params_hi6220 = {
  54. .otg_cap = 2, /* No HNP/SRP capable */
  55. .otg_ver = 0, /* 1.3 */
  56. .dma_enable = 1,
  57. .dma_desc_enable = 0,
  58. .dma_desc_fs_enable = 0,
  59. .speed = 0, /* High Speed */
  60. .enable_dynamic_fifo = 1,
  61. .en_multiple_tx_fifo = 1,
  62. .host_rx_fifo_size = 512,
  63. .host_nperio_tx_fifo_size = 512,
  64. .host_perio_tx_fifo_size = 512,
  65. .max_transfer_size = 65535,
  66. .max_packet_count = 511,
  67. .host_channels = 16,
  68. .phy_type = 1, /* UTMI */
  69. .phy_utmi_width = 8,
  70. .phy_ulpi_ddr = 0, /* Single */
  71. .phy_ulpi_ext_vbus = 0,
  72. .i2c_enable = 0,
  73. .ulpi_fs_ls = 0,
  74. .host_support_fs_ls_low_power = 0,
  75. .host_ls_low_power_phy_clk = 0, /* 48 MHz */
  76. .ts_dline = 0,
  77. .reload_ctl = 0,
  78. .ahbcfg = GAHBCFG_HBSTLEN_INCR16 <<
  79. GAHBCFG_HBSTLEN_SHIFT,
  80. .uframe_sched = 0,
  81. .external_id_pin_ctl = -1,
  82. .hibernation = -1,
  83. };
  84. static const struct dwc2_core_params params_bcm2835 = {
  85. .otg_cap = 0, /* HNP/SRP capable */
  86. .otg_ver = 0, /* 1.3 */
  87. .dma_enable = 1,
  88. .dma_desc_enable = 0,
  89. .dma_desc_fs_enable = 0,
  90. .speed = 0, /* High Speed */
  91. .enable_dynamic_fifo = 1,
  92. .en_multiple_tx_fifo = 1,
  93. .host_rx_fifo_size = 774, /* 774 DWORDs */
  94. .host_nperio_tx_fifo_size = 256, /* 256 DWORDs */
  95. .host_perio_tx_fifo_size = 512, /* 512 DWORDs */
  96. .max_transfer_size = 65535,
  97. .max_packet_count = 511,
  98. .host_channels = 8,
  99. .phy_type = 1, /* UTMI */
  100. .phy_utmi_width = 8, /* 8 bits */
  101. .phy_ulpi_ddr = 0, /* Single */
  102. .phy_ulpi_ext_vbus = 0,
  103. .i2c_enable = 0,
  104. .ulpi_fs_ls = 0,
  105. .host_support_fs_ls_low_power = 0,
  106. .host_ls_low_power_phy_clk = 0, /* 48 MHz */
  107. .ts_dline = 0,
  108. .reload_ctl = 0,
  109. .ahbcfg = 0x10,
  110. .uframe_sched = 0,
  111. .external_id_pin_ctl = -1,
  112. .hibernation = -1,
  113. };
  114. static const struct dwc2_core_params params_rk3066 = {
  115. .otg_cap = 2, /* non-HNP/non-SRP */
  116. .otg_ver = -1,
  117. .dma_enable = -1,
  118. .dma_desc_enable = 0,
  119. .dma_desc_fs_enable = 0,
  120. .speed = -1,
  121. .enable_dynamic_fifo = 1,
  122. .en_multiple_tx_fifo = -1,
  123. .host_rx_fifo_size = 525, /* 525 DWORDs */
  124. .host_nperio_tx_fifo_size = 128, /* 128 DWORDs */
  125. .host_perio_tx_fifo_size = 256, /* 256 DWORDs */
  126. .max_transfer_size = -1,
  127. .max_packet_count = -1,
  128. .host_channels = -1,
  129. .phy_type = -1,
  130. .phy_utmi_width = -1,
  131. .phy_ulpi_ddr = -1,
  132. .phy_ulpi_ext_vbus = -1,
  133. .i2c_enable = -1,
  134. .ulpi_fs_ls = -1,
  135. .host_support_fs_ls_low_power = -1,
  136. .host_ls_low_power_phy_clk = -1,
  137. .ts_dline = -1,
  138. .reload_ctl = -1,
  139. .ahbcfg = GAHBCFG_HBSTLEN_INCR16 <<
  140. GAHBCFG_HBSTLEN_SHIFT,
  141. .uframe_sched = -1,
  142. .external_id_pin_ctl = -1,
  143. .hibernation = -1,
  144. };
  145. static const struct dwc2_core_params params_ltq = {
  146. .otg_cap = 2, /* non-HNP/non-SRP */
  147. .otg_ver = -1,
  148. .dma_enable = -1,
  149. .dma_desc_enable = -1,
  150. .dma_desc_fs_enable = -1,
  151. .speed = -1,
  152. .enable_dynamic_fifo = -1,
  153. .en_multiple_tx_fifo = -1,
  154. .host_rx_fifo_size = 288, /* 288 DWORDs */
  155. .host_nperio_tx_fifo_size = 128, /* 128 DWORDs */
  156. .host_perio_tx_fifo_size = 96, /* 96 DWORDs */
  157. .max_transfer_size = 65535,
  158. .max_packet_count = 511,
  159. .host_channels = -1,
  160. .phy_type = -1,
  161. .phy_utmi_width = -1,
  162. .phy_ulpi_ddr = -1,
  163. .phy_ulpi_ext_vbus = -1,
  164. .i2c_enable = -1,
  165. .ulpi_fs_ls = -1,
  166. .host_support_fs_ls_low_power = -1,
  167. .host_ls_low_power_phy_clk = -1,
  168. .ts_dline = -1,
  169. .reload_ctl = -1,
  170. .ahbcfg = GAHBCFG_HBSTLEN_INCR16 <<
  171. GAHBCFG_HBSTLEN_SHIFT,
  172. .uframe_sched = -1,
  173. .external_id_pin_ctl = -1,
  174. .hibernation = -1,
  175. };
  176. static const struct dwc2_core_params params_amlogic = {
  177. .otg_cap = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE,
  178. .otg_ver = -1,
  179. .dma_enable = 1,
  180. .dma_desc_enable = 0,
  181. .dma_desc_fs_enable = 0,
  182. .speed = DWC2_SPEED_PARAM_HIGH,
  183. .enable_dynamic_fifo = 1,
  184. .en_multiple_tx_fifo = -1,
  185. .host_rx_fifo_size = 512,
  186. .host_nperio_tx_fifo_size = 500,
  187. .host_perio_tx_fifo_size = 500,
  188. .max_transfer_size = -1,
  189. .max_packet_count = -1,
  190. .host_channels = 16,
  191. .phy_type = DWC2_PHY_TYPE_PARAM_UTMI,
  192. .phy_utmi_width = -1,
  193. .phy_ulpi_ddr = -1,
  194. .phy_ulpi_ext_vbus = -1,
  195. .i2c_enable = -1,
  196. .ulpi_fs_ls = -1,
  197. .host_support_fs_ls_low_power = -1,
  198. .host_ls_low_power_phy_clk = -1,
  199. .ts_dline = -1,
  200. .reload_ctl = 1,
  201. .ahbcfg = GAHBCFG_HBSTLEN_INCR8 <<
  202. GAHBCFG_HBSTLEN_SHIFT,
  203. .uframe_sched = 0,
  204. .external_id_pin_ctl = -1,
  205. .hibernation = -1,
  206. };
  207. /*
  208. * Check the dr_mode against the module configuration and hardware
  209. * capabilities.
  210. *
  211. * The hardware, module, and dr_mode, can each be set to host, device,
  212. * or otg. Check that all these values are compatible and adjust the
  213. * value of dr_mode if possible.
  214. *
  215. * actual
  216. * HW MOD dr_mode dr_mode
  217. * ------------------------------
  218. * HST HST any : HST
  219. * HST DEV any : ---
  220. * HST OTG any : HST
  221. *
  222. * DEV HST any : ---
  223. * DEV DEV any : DEV
  224. * DEV OTG any : DEV
  225. *
  226. * OTG HST any : HST
  227. * OTG DEV any : DEV
  228. * OTG OTG any : dr_mode
  229. */
  230. static int dwc2_get_dr_mode(struct dwc2_hsotg *hsotg)
  231. {
  232. enum usb_dr_mode mode;
  233. hsotg->dr_mode = usb_get_dr_mode(hsotg->dev);
  234. if (hsotg->dr_mode == USB_DR_MODE_UNKNOWN)
  235. hsotg->dr_mode = USB_DR_MODE_OTG;
  236. mode = hsotg->dr_mode;
  237. if (dwc2_hw_is_device(hsotg)) {
  238. if (IS_ENABLED(CONFIG_USB_DWC2_HOST)) {
  239. dev_err(hsotg->dev,
  240. "Controller does not support host mode.\n");
  241. return -EINVAL;
  242. }
  243. mode = USB_DR_MODE_PERIPHERAL;
  244. } else if (dwc2_hw_is_host(hsotg)) {
  245. if (IS_ENABLED(CONFIG_USB_DWC2_PERIPHERAL)) {
  246. dev_err(hsotg->dev,
  247. "Controller does not support device mode.\n");
  248. return -EINVAL;
  249. }
  250. mode = USB_DR_MODE_HOST;
  251. } else {
  252. if (IS_ENABLED(CONFIG_USB_DWC2_HOST))
  253. mode = USB_DR_MODE_HOST;
  254. else if (IS_ENABLED(CONFIG_USB_DWC2_PERIPHERAL))
  255. mode = USB_DR_MODE_PERIPHERAL;
  256. }
  257. if (mode != hsotg->dr_mode) {
  258. dev_warn(hsotg->dev,
  259. "Configuration mismatch. dr_mode forced to %s\n",
  260. mode == USB_DR_MODE_HOST ? "host" : "device");
  261. hsotg->dr_mode = mode;
  262. }
  263. return 0;
  264. }
  265. static int __dwc2_lowlevel_hw_enable(struct dwc2_hsotg *hsotg)
  266. {
  267. struct platform_device *pdev = to_platform_device(hsotg->dev);
  268. int ret;
  269. ret = regulator_bulk_enable(ARRAY_SIZE(hsotg->supplies),
  270. hsotg->supplies);
  271. if (ret)
  272. return ret;
  273. if (hsotg->clk) {
  274. ret = clk_prepare_enable(hsotg->clk);
  275. if (ret)
  276. return ret;
  277. }
  278. if (hsotg->uphy)
  279. ret = usb_phy_init(hsotg->uphy);
  280. else if (hsotg->plat && hsotg->plat->phy_init)
  281. ret = hsotg->plat->phy_init(pdev, hsotg->plat->phy_type);
  282. else {
  283. ret = phy_power_on(hsotg->phy);
  284. if (ret == 0)
  285. ret = phy_init(hsotg->phy);
  286. }
  287. return ret;
  288. }
  289. /**
  290. * dwc2_lowlevel_hw_enable - enable platform lowlevel hw resources
  291. * @hsotg: The driver state
  292. *
  293. * A wrapper for platform code responsible for controlling
  294. * low-level USB platform resources (phy, clock, regulators)
  295. */
  296. int dwc2_lowlevel_hw_enable(struct dwc2_hsotg *hsotg)
  297. {
  298. int ret = __dwc2_lowlevel_hw_enable(hsotg);
  299. if (ret == 0)
  300. hsotg->ll_hw_enabled = true;
  301. return ret;
  302. }
  303. static int __dwc2_lowlevel_hw_disable(struct dwc2_hsotg *hsotg)
  304. {
  305. struct platform_device *pdev = to_platform_device(hsotg->dev);
  306. int ret = 0;
  307. if (hsotg->uphy)
  308. usb_phy_shutdown(hsotg->uphy);
  309. else if (hsotg->plat && hsotg->plat->phy_exit)
  310. ret = hsotg->plat->phy_exit(pdev, hsotg->plat->phy_type);
  311. else {
  312. ret = phy_exit(hsotg->phy);
  313. if (ret == 0)
  314. ret = phy_power_off(hsotg->phy);
  315. }
  316. if (ret)
  317. return ret;
  318. if (hsotg->clk)
  319. clk_disable_unprepare(hsotg->clk);
  320. ret = regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies),
  321. hsotg->supplies);
  322. return ret;
  323. }
  324. /**
  325. * dwc2_lowlevel_hw_disable - disable platform lowlevel hw resources
  326. * @hsotg: The driver state
  327. *
  328. * A wrapper for platform code responsible for controlling
  329. * low-level USB platform resources (phy, clock, regulators)
  330. */
  331. int dwc2_lowlevel_hw_disable(struct dwc2_hsotg *hsotg)
  332. {
  333. int ret = __dwc2_lowlevel_hw_disable(hsotg);
  334. if (ret == 0)
  335. hsotg->ll_hw_enabled = false;
  336. return ret;
  337. }
  338. static int dwc2_lowlevel_hw_init(struct dwc2_hsotg *hsotg)
  339. {
  340. int i, ret;
  341. hsotg->reset = devm_reset_control_get_optional(hsotg->dev, "dwc2");
  342. if (IS_ERR(hsotg->reset)) {
  343. ret = PTR_ERR(hsotg->reset);
  344. switch (ret) {
  345. case -ENOENT:
  346. case -ENOTSUPP:
  347. hsotg->reset = NULL;
  348. break;
  349. default:
  350. dev_err(hsotg->dev, "error getting reset control %d\n",
  351. ret);
  352. return ret;
  353. }
  354. }
  355. if (hsotg->reset)
  356. reset_control_deassert(hsotg->reset);
  357. /* Set default UTMI width */
  358. hsotg->phyif = GUSBCFG_PHYIF16;
  359. /*
  360. * Attempt to find a generic PHY, then look for an old style
  361. * USB PHY and then fall back to pdata
  362. */
  363. hsotg->phy = devm_phy_get(hsotg->dev, "usb2-phy");
  364. if (IS_ERR(hsotg->phy)) {
  365. ret = PTR_ERR(hsotg->phy);
  366. switch (ret) {
  367. case -ENODEV:
  368. case -ENOSYS:
  369. hsotg->phy = NULL;
  370. break;
  371. case -EPROBE_DEFER:
  372. return ret;
  373. default:
  374. dev_err(hsotg->dev, "error getting phy %d\n", ret);
  375. return ret;
  376. }
  377. }
  378. if (!hsotg->phy) {
  379. hsotg->uphy = devm_usb_get_phy(hsotg->dev, USB_PHY_TYPE_USB2);
  380. if (IS_ERR(hsotg->uphy)) {
  381. ret = PTR_ERR(hsotg->uphy);
  382. switch (ret) {
  383. case -ENODEV:
  384. case -ENXIO:
  385. hsotg->uphy = NULL;
  386. break;
  387. case -EPROBE_DEFER:
  388. return ret;
  389. default:
  390. dev_err(hsotg->dev, "error getting usb phy %d\n",
  391. ret);
  392. return ret;
  393. }
  394. }
  395. }
  396. hsotg->plat = dev_get_platdata(hsotg->dev);
  397. if (hsotg->phy) {
  398. /*
  399. * If using the generic PHY framework, check if the PHY bus
  400. * width is 8-bit and set the phyif appropriately.
  401. */
  402. if (phy_get_bus_width(hsotg->phy) == 8)
  403. hsotg->phyif = GUSBCFG_PHYIF8;
  404. }
  405. /* Clock */
  406. hsotg->clk = devm_clk_get(hsotg->dev, "otg");
  407. if (IS_ERR(hsotg->clk)) {
  408. hsotg->clk = NULL;
  409. dev_dbg(hsotg->dev, "cannot get otg clock\n");
  410. }
  411. /* Regulators */
  412. for (i = 0; i < ARRAY_SIZE(hsotg->supplies); i++)
  413. hsotg->supplies[i].supply = dwc2_hsotg_supply_names[i];
  414. ret = devm_regulator_bulk_get(hsotg->dev, ARRAY_SIZE(hsotg->supplies),
  415. hsotg->supplies);
  416. if (ret) {
  417. dev_err(hsotg->dev, "failed to request supplies: %d\n", ret);
  418. return ret;
  419. }
  420. return 0;
  421. }
  422. /**
  423. * dwc2_driver_remove() - Called when the DWC_otg core is unregistered with the
  424. * DWC_otg driver
  425. *
  426. * @dev: Platform device
  427. *
  428. * This routine is called, for example, when the rmmod command is executed. The
  429. * device may or may not be electrically present. If it is present, the driver
  430. * stops device processing. Any resources used on behalf of this device are
  431. * freed.
  432. */
  433. static int dwc2_driver_remove(struct platform_device *dev)
  434. {
  435. struct dwc2_hsotg *hsotg = platform_get_drvdata(dev);
  436. dwc2_debugfs_exit(hsotg);
  437. if (hsotg->hcd_enabled)
  438. dwc2_hcd_remove(hsotg);
  439. if (hsotg->gadget_enabled)
  440. dwc2_hsotg_remove(hsotg);
  441. if (hsotg->ll_hw_enabled)
  442. dwc2_lowlevel_hw_disable(hsotg);
  443. if (hsotg->reset)
  444. reset_control_assert(hsotg->reset);
  445. return 0;
  446. }
  447. /**
  448. * dwc2_driver_shutdown() - Called on device shutdown
  449. *
  450. * @dev: Platform device
  451. *
  452. * In specific conditions (involving usb hubs) dwc2 devices can create a
  453. * lot of interrupts, even to the point of overwhelming devices running
  454. * at low frequencies. Some devices need to do special clock handling
  455. * at shutdown-time which may bring the system clock below the threshold
  456. * of being able to handle the dwc2 interrupts. Disabling dwc2-irqs
  457. * prevents reboots/poweroffs from getting stuck in such cases.
  458. */
  459. static void dwc2_driver_shutdown(struct platform_device *dev)
  460. {
  461. struct dwc2_hsotg *hsotg = platform_get_drvdata(dev);
  462. disable_irq(hsotg->irq);
  463. }
  464. static const struct of_device_id dwc2_of_match_table[] = {
  465. { .compatible = "brcm,bcm2835-usb", .data = &params_bcm2835 },
  466. { .compatible = "hisilicon,hi6220-usb", .data = &params_hi6220 },
  467. { .compatible = "rockchip,rk3066-usb", .data = &params_rk3066 },
  468. { .compatible = "lantiq,arx100-usb", .data = &params_ltq },
  469. { .compatible = "lantiq,xrx200-usb", .data = &params_ltq },
  470. { .compatible = "snps,dwc2", .data = NULL },
  471. { .compatible = "samsung,s3c6400-hsotg", .data = NULL},
  472. { .compatible = "amlogic,meson8b-usb", .data = &params_amlogic },
  473. { .compatible = "amlogic,meson-gxbb-usb", .data = &params_amlogic },
  474. {},
  475. };
  476. MODULE_DEVICE_TABLE(of, dwc2_of_match_table);
  477. /**
  478. * dwc2_driver_probe() - Called when the DWC_otg core is bound to the DWC_otg
  479. * driver
  480. *
  481. * @dev: Platform device
  482. *
  483. * This routine creates the driver components required to control the device
  484. * (core, HCD, and PCD) and initializes the device. The driver components are
  485. * stored in a dwc2_hsotg structure. A reference to the dwc2_hsotg is saved
  486. * in the device private data. This allows the driver to access the dwc2_hsotg
  487. * structure on subsequent calls to driver methods for this device.
  488. */
  489. static int dwc2_driver_probe(struct platform_device *dev)
  490. {
  491. const struct of_device_id *match;
  492. const struct dwc2_core_params *params;
  493. struct dwc2_core_params defparams;
  494. struct dwc2_hsotg *hsotg;
  495. struct resource *res;
  496. int retval;
  497. match = of_match_device(dwc2_of_match_table, &dev->dev);
  498. if (match && match->data) {
  499. params = match->data;
  500. } else {
  501. /* Default all params to autodetect */
  502. dwc2_set_all_params(&defparams, -1);
  503. params = &defparams;
  504. /*
  505. * Disable descriptor dma mode by default as the HW can support
  506. * it, but does not support it for SPLIT transactions.
  507. * Disable it for FS devices as well.
  508. */
  509. defparams.dma_desc_enable = 0;
  510. defparams.dma_desc_fs_enable = 0;
  511. }
  512. hsotg = devm_kzalloc(&dev->dev, sizeof(*hsotg), GFP_KERNEL);
  513. if (!hsotg)
  514. return -ENOMEM;
  515. hsotg->dev = &dev->dev;
  516. /*
  517. * Use reasonable defaults so platforms don't have to provide these.
  518. */
  519. if (!dev->dev.dma_mask)
  520. dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
  521. retval = dma_set_coherent_mask(&dev->dev, DMA_BIT_MASK(32));
  522. if (retval)
  523. return retval;
  524. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  525. hsotg->regs = devm_ioremap_resource(&dev->dev, res);
  526. if (IS_ERR(hsotg->regs))
  527. return PTR_ERR(hsotg->regs);
  528. dev_dbg(&dev->dev, "mapped PA %08lx to VA %p\n",
  529. (unsigned long)res->start, hsotg->regs);
  530. retval = dwc2_lowlevel_hw_init(hsotg);
  531. if (retval)
  532. return retval;
  533. spin_lock_init(&hsotg->lock);
  534. hsotg->core_params = devm_kzalloc(&dev->dev,
  535. sizeof(*hsotg->core_params), GFP_KERNEL);
  536. if (!hsotg->core_params)
  537. return -ENOMEM;
  538. dwc2_set_all_params(hsotg->core_params, -1);
  539. hsotg->irq = platform_get_irq(dev, 0);
  540. if (hsotg->irq < 0) {
  541. dev_err(&dev->dev, "missing IRQ resource\n");
  542. return hsotg->irq;
  543. }
  544. dev_dbg(hsotg->dev, "registering common handler for irq%d\n",
  545. hsotg->irq);
  546. retval = devm_request_irq(hsotg->dev, hsotg->irq,
  547. dwc2_handle_common_intr, IRQF_SHARED,
  548. dev_name(hsotg->dev), hsotg);
  549. if (retval)
  550. return retval;
  551. retval = dwc2_lowlevel_hw_enable(hsotg);
  552. if (retval)
  553. return retval;
  554. retval = dwc2_get_dr_mode(hsotg);
  555. if (retval)
  556. goto error;
  557. /*
  558. * Reset before dwc2_get_hwparams() then it could get power-on real
  559. * reset value form registers.
  560. */
  561. dwc2_core_reset_and_force_dr_mode(hsotg);
  562. /* Detect config values from hardware */
  563. retval = dwc2_get_hwparams(hsotg);
  564. if (retval)
  565. goto error;
  566. /* Validate parameter values */
  567. dwc2_set_parameters(hsotg, params);
  568. dwc2_force_dr_mode(hsotg);
  569. if (hsotg->dr_mode != USB_DR_MODE_HOST) {
  570. retval = dwc2_gadget_init(hsotg, hsotg->irq);
  571. if (retval)
  572. goto error;
  573. hsotg->gadget_enabled = 1;
  574. }
  575. if (hsotg->dr_mode != USB_DR_MODE_PERIPHERAL) {
  576. retval = dwc2_hcd_init(hsotg, hsotg->irq);
  577. if (retval) {
  578. if (hsotg->gadget_enabled)
  579. dwc2_hsotg_remove(hsotg);
  580. goto error;
  581. }
  582. hsotg->hcd_enabled = 1;
  583. }
  584. platform_set_drvdata(dev, hsotg);
  585. dwc2_debugfs_init(hsotg);
  586. /* Gadget code manages lowlevel hw on its own */
  587. if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
  588. dwc2_lowlevel_hw_disable(hsotg);
  589. return 0;
  590. error:
  591. dwc2_lowlevel_hw_disable(hsotg);
  592. return retval;
  593. }
  594. static int __maybe_unused dwc2_suspend(struct device *dev)
  595. {
  596. struct dwc2_hsotg *dwc2 = dev_get_drvdata(dev);
  597. int ret = 0;
  598. if (dwc2_is_device_mode(dwc2))
  599. dwc2_hsotg_suspend(dwc2);
  600. if (dwc2->ll_hw_enabled)
  601. ret = __dwc2_lowlevel_hw_disable(dwc2);
  602. return ret;
  603. }
  604. static int __maybe_unused dwc2_resume(struct device *dev)
  605. {
  606. struct dwc2_hsotg *dwc2 = dev_get_drvdata(dev);
  607. int ret = 0;
  608. if (dwc2->ll_hw_enabled) {
  609. ret = __dwc2_lowlevel_hw_enable(dwc2);
  610. if (ret)
  611. return ret;
  612. }
  613. if (dwc2_is_device_mode(dwc2))
  614. ret = dwc2_hsotg_resume(dwc2);
  615. return ret;
  616. }
  617. static const struct dev_pm_ops dwc2_dev_pm_ops = {
  618. SET_SYSTEM_SLEEP_PM_OPS(dwc2_suspend, dwc2_resume)
  619. };
  620. static struct platform_driver dwc2_platform_driver = {
  621. .driver = {
  622. .name = dwc2_driver_name,
  623. .of_match_table = dwc2_of_match_table,
  624. .pm = &dwc2_dev_pm_ops,
  625. },
  626. .probe = dwc2_driver_probe,
  627. .remove = dwc2_driver_remove,
  628. .shutdown = dwc2_driver_shutdown,
  629. };
  630. module_platform_driver(dwc2_platform_driver);
  631. MODULE_DESCRIPTION("DESIGNWARE HS OTG Platform Glue");
  632. MODULE_AUTHOR("Matthijs Kooijman <matthijs@stdin.nl>");
  633. MODULE_LICENSE("Dual BSD/GPL");