platform.c 18 KB

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