params.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. /*
  2. * Copyright (C) 2004-2016 Synopsys, Inc.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions, and the following disclaimer,
  9. * without modification.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. The names of the above-listed copyright holders may not be used
  14. * to endorse or promote products derived from this software without
  15. * specific prior written permission.
  16. *
  17. * ALTERNATIVELY, this software may be distributed under the terms of the
  18. * GNU General Public License ("GPL") as published by the Free Software
  19. * Foundation; either version 2 of the License, or (at your option) any
  20. * later version.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  23. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  24. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  25. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  26. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  27. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  28. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  29. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  30. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  31. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include "usb_os_adapter.h"
  35. #include "trace.h"
  36. #include <asm/dma-mapping.h>
  37. #include <linux/usb/ch9.h>
  38. #include <linux/usb/gadget.h>
  39. #include "core.h"
  40. #include "board.h"
  41. //#define USB_DMA
  42. /* static void dwc2_set_bcm_params(struct dwc2_hsotg *hsotg)
  43. {
  44. struct dwc2_core_params *p = &hsotg->params;
  45. p->host_rx_fifo_size = 774;
  46. p->max_transfer_size = 65535;
  47. p->max_packet_count = 511;
  48. p->ahbcfg = 0x10;
  49. p->uframe_sched = false;
  50. }
  51. static void dwc2_set_his_params(struct dwc2_hsotg *hsotg)
  52. {
  53. struct dwc2_core_params *p = &hsotg->params;
  54. p->otg_cap = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE;
  55. p->speed = DWC2_SPEED_PARAM_HIGH;
  56. p->host_rx_fifo_size = 512;
  57. p->host_nperio_tx_fifo_size = 512;
  58. p->host_perio_tx_fifo_size = 512;
  59. p->max_transfer_size = 65535;
  60. p->max_packet_count = 511;
  61. p->host_channels = 16;
  62. p->phy_type = DWC2_PHY_TYPE_PARAM_UTMI;
  63. p->phy_utmi_width = 8;
  64. p->i2c_enable = false;
  65. p->reload_ctl = false;
  66. p->ahbcfg = GAHBCFG_HBSTLEN_INCR16 <<
  67. GAHBCFG_HBSTLEN_SHIFT;
  68. p->uframe_sched = false;
  69. p->change_speed_quirk = true;
  70. }
  71. static void dwc2_set_rk_params(struct dwc2_hsotg *hsotg)
  72. {
  73. struct dwc2_core_params *p = &hsotg->params;
  74. p->otg_cap = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE;
  75. p->host_rx_fifo_size = 525;
  76. p->host_nperio_tx_fifo_size = 128;
  77. p->host_perio_tx_fifo_size = 256;
  78. p->ahbcfg = GAHBCFG_HBSTLEN_INCR16 <<
  79. GAHBCFG_HBSTLEN_SHIFT;
  80. }
  81. static void dwc2_set_ltq_params(struct dwc2_hsotg *hsotg)
  82. {
  83. struct dwc2_core_params *p = &hsotg->params;
  84. p->otg_cap = 2;
  85. p->host_rx_fifo_size = 288;
  86. p->host_nperio_tx_fifo_size = 128;
  87. p->host_perio_tx_fifo_size = 96;
  88. p->max_transfer_size = 65535;
  89. p->max_packet_count = 511;
  90. p->ahbcfg = GAHBCFG_HBSTLEN_INCR16 <<
  91. GAHBCFG_HBSTLEN_SHIFT;
  92. }
  93. static void dwc2_set_amlogic_params(struct dwc2_hsotg *hsotg)
  94. {
  95. struct dwc2_core_params *p = &hsotg->params;
  96. p->otg_cap = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE;
  97. p->speed = DWC2_SPEED_PARAM_HIGH;
  98. p->host_rx_fifo_size = 512;
  99. p->host_nperio_tx_fifo_size = 500;
  100. p->host_perio_tx_fifo_size = 500;
  101. p->host_channels = 16;
  102. p->phy_type = DWC2_PHY_TYPE_PARAM_UTMI;
  103. p->ahbcfg = GAHBCFG_HBSTLEN_INCR8 <<
  104. GAHBCFG_HBSTLEN_SHIFT;
  105. p->uframe_sched = false;
  106. }
  107. static void dwc2_set_amcc_params(struct dwc2_hsotg *hsotg)
  108. {
  109. struct dwc2_core_params *p = &hsotg->params;
  110. p->ahbcfg = GAHBCFG_HBSTLEN_INCR16 << GAHBCFG_HBSTLEN_SHIFT;
  111. }
  112. static void dwc2_set_stm32f4x9_fsotg_params(struct dwc2_hsotg *hsotg)
  113. {
  114. struct dwc2_core_params *p = &hsotg->params;
  115. p->otg_cap = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE;
  116. p->speed = DWC2_SPEED_PARAM_FULL;
  117. p->host_rx_fifo_size = 128;
  118. p->host_nperio_tx_fifo_size = 96;
  119. p->host_perio_tx_fifo_size = 96;
  120. p->max_packet_count = 256;
  121. p->phy_type = DWC2_PHY_TYPE_PARAM_FS;
  122. p->i2c_enable = false;
  123. p->uframe_sched = false;
  124. p->activate_stm_fs_transceiver = true;
  125. } */
  126. static void dwc2_set_param_otg_cap(struct dwc2_hsotg *hsotg)
  127. {
  128. u8 val;
  129. switch (hsotg->hw_params.op_mode) {
  130. case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
  131. val = DWC2_CAP_PARAM_HNP_SRP_CAPABLE;
  132. break;
  133. case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
  134. case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
  135. case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
  136. val = DWC2_CAP_PARAM_SRP_ONLY_CAPABLE;
  137. break;
  138. default:
  139. val = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE;
  140. break;
  141. }
  142. hsotg->params.otg_cap = val;
  143. }
  144. static void dwc2_set_param_phy_type(struct dwc2_hsotg *hsotg)
  145. {
  146. int val;
  147. u32 hs_phy_type = hsotg->hw_params.hs_phy_type;
  148. val = DWC2_PHY_TYPE_PARAM_FS;
  149. if (hs_phy_type != GHWCFG2_HS_PHY_TYPE_NOT_SUPPORTED) {
  150. if (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI ||
  151. hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI)
  152. val = DWC2_PHY_TYPE_PARAM_UTMI;
  153. else
  154. val = DWC2_PHY_TYPE_PARAM_ULPI;
  155. }
  156. if (dwc2_is_fs_iot(hsotg))
  157. hsotg->params.phy_type = DWC2_PHY_TYPE_PARAM_FS;
  158. hsotg->params.phy_type = val;
  159. }
  160. static void dwc2_set_param_speed(struct dwc2_hsotg *hsotg)
  161. {
  162. int val;
  163. val = hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS ?
  164. DWC2_SPEED_PARAM_FULL : DWC2_SPEED_PARAM_HIGH;
  165. if (dwc2_is_fs_iot(hsotg))
  166. val = DWC2_SPEED_PARAM_FULL;
  167. if (dwc2_is_hs_iot(hsotg))
  168. val = DWC2_SPEED_PARAM_HIGH;
  169. hsotg->params.speed = val;
  170. }
  171. static void dwc2_set_param_phy_utmi_width(struct dwc2_hsotg *hsotg)
  172. {
  173. int val;
  174. val = (hsotg->hw_params.utmi_phy_data_width ==
  175. GHWCFG4_UTMI_PHY_DATA_WIDTH_8) ? 8 : 16;
  176. hsotg->params.phy_utmi_width = val;
  177. }
  178. static void dwc2_set_param_tx_fifo_sizes(struct dwc2_hsotg *hsotg)
  179. {
  180. struct dwc2_core_params *p = &hsotg->params;
  181. int depth_average;
  182. int fifo_count;
  183. int i;
  184. fifo_count = dwc2_hsotg_tx_fifo_count(hsotg);
  185. memset(p->g_tx_fifo_size, 0, sizeof(p->g_tx_fifo_size));
  186. depth_average = dwc2_hsotg_tx_fifo_average_depth(hsotg);
  187. for (i = 1; i <= fifo_count; i++)
  188. p->g_tx_fifo_size[i] = depth_average;
  189. }
  190. /**
  191. * dwc2_set_default_params() - Set all core parameters to their
  192. * auto-detected default values.
  193. */
  194. static void dwc2_set_default_params(struct dwc2_hsotg *hsotg)
  195. {
  196. struct dwc2_hw_params *hw = &hsotg->hw_params;
  197. struct dwc2_core_params *p = &hsotg->params;
  198. #ifdef USB_DMA
  199. bool dma_capable = !(hw->arch == GHWCFG2_SLAVE_ONLY_ARCH);
  200. #endif
  201. dwc2_set_param_otg_cap(hsotg);
  202. dwc2_set_param_phy_type(hsotg);
  203. dwc2_set_param_speed(hsotg);
  204. dwc2_set_param_phy_utmi_width(hsotg);
  205. p->phy_ulpi_ddr = false;
  206. p->phy_ulpi_ext_vbus = false;
  207. p->enable_dynamic_fifo = hw->enable_dynamic_fifo;
  208. p->en_multiple_tx_fifo = hw->en_multiple_tx_fifo;
  209. p->i2c_enable = hw->i2c_enable;
  210. p->ulpi_fs_ls = false;
  211. p->ts_dline = false;
  212. p->reload_ctl = (hw->snpsid >= DWC2_CORE_REV_2_92a);
  213. p->uframe_sched = true;
  214. p->external_id_pin_ctl = false;
  215. p->hibernation = false;
  216. p->max_packet_count = hw->max_packet_count;
  217. p->max_transfer_size = hw->max_transfer_size;
  218. p->ahbcfg = GAHBCFG_HBSTLEN_INCR4 << GAHBCFG_HBSTLEN_SHIFT;
  219. if ((hsotg->dr_mode == USB_DR_MODE_HOST) ||
  220. (hsotg->dr_mode == USB_DR_MODE_OTG)) {
  221. #ifdef USB_DMA
  222. p->host_dma = dma_capable;
  223. #else
  224. p->host_dma = false;
  225. #endif
  226. p->dma_desc_enable = false;
  227. p->dma_desc_fs_enable = false;
  228. p->host_support_fs_ls_low_power = false;
  229. p->host_ls_low_power_phy_clk = false;
  230. p->host_channels = hw->host_channels;
  231. p->host_rx_fifo_size = hw->rx_fifo_size;
  232. p->host_nperio_tx_fifo_size = hw->host_nperio_tx_fifo_size;
  233. p->host_perio_tx_fifo_size = hw->host_perio_tx_fifo_size;
  234. }
  235. if ((hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) ||
  236. (hsotg->dr_mode == USB_DR_MODE_OTG)) {
  237. #ifdef USB_DMA
  238. p->g_dma = dma_capable;
  239. #else
  240. p->g_dma = false;
  241. #endif
  242. p->g_dma_desc = hw->dma_desc_enable;
  243. /*
  244. * The values for g_rx_fifo_size (2048) and
  245. * g_np_tx_fifo_size (1024) come from the legacy s3c
  246. * gadget driver. These defaults have been hard-coded
  247. * for some time so many platforms depend on these
  248. * values. Leave them as defaults for now and only
  249. * auto-detect if the hardware does not support the
  250. * default.
  251. */
  252. p->g_rx_fifo_size = 2048;
  253. p->g_np_tx_fifo_size = 1024;
  254. dwc2_set_param_tx_fifo_sizes(hsotg);
  255. }
  256. }
  257. /**
  258. * dwc2_get_device_properties() - Read in device properties.
  259. *
  260. * Read in the device properties and adjust core parameters if needed.
  261. */
  262. static void dwc2_get_device_properties(struct dwc2_hsotg *hsotg)
  263. {
  264. struct dwc2_core_params *p = &hsotg->params;
  265. if ((hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) ||
  266. (hsotg->dr_mode == USB_DR_MODE_OTG)) {
  267. p->g_rx_fifo_size = 1588;
  268. p->g_np_tx_fifo_size = 384;
  269. p->g_tx_fifo_size[0] = 1024;
  270. p->g_tx_fifo_size[1] = 1024;
  271. p->g_tx_fifo_size[2] = 1024;
  272. p->g_tx_fifo_size[3] = 1024;
  273. /*p->g_tx_fifo_size[4] = 1024;
  274. p->g_tx_fifo_size[5] = 1024;
  275. p->g_tx_fifo_size[6] = 1024;
  276. p->g_tx_fifo_size[7] = 1024;*/
  277. }
  278. }
  279. static void dwc2_check_param_otg_cap(struct dwc2_hsotg *hsotg)
  280. {
  281. int valid = 1;
  282. switch (hsotg->params.otg_cap) {
  283. case DWC2_CAP_PARAM_HNP_SRP_CAPABLE:
  284. if (hsotg->hw_params.op_mode != GHWCFG2_OP_MODE_HNP_SRP_CAPABLE)
  285. valid = 0;
  286. break;
  287. case DWC2_CAP_PARAM_SRP_ONLY_CAPABLE:
  288. switch (hsotg->hw_params.op_mode) {
  289. case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
  290. case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
  291. case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
  292. case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
  293. break;
  294. default:
  295. valid = 0;
  296. break;
  297. }
  298. break;
  299. case DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE:
  300. /* always valid */
  301. break;
  302. default:
  303. valid = 0;
  304. break;
  305. }
  306. if (!valid)
  307. dwc2_set_param_otg_cap(hsotg);
  308. }
  309. static void dwc2_check_param_phy_type(struct dwc2_hsotg *hsotg)
  310. {
  311. int valid = 0;
  312. u32 hs_phy_type;
  313. u32 fs_phy_type;
  314. hs_phy_type = hsotg->hw_params.hs_phy_type;
  315. fs_phy_type = hsotg->hw_params.fs_phy_type;
  316. switch (hsotg->params.phy_type) {
  317. case DWC2_PHY_TYPE_PARAM_FS:
  318. if (fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED)
  319. valid = 1;
  320. break;
  321. case DWC2_PHY_TYPE_PARAM_UTMI:
  322. if ((hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI) ||
  323. (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI))
  324. valid = 1;
  325. break;
  326. case DWC2_PHY_TYPE_PARAM_ULPI:
  327. if ((hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI) ||
  328. (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI))
  329. valid = 1;
  330. break;
  331. default:
  332. break;
  333. }
  334. if (!valid)
  335. dwc2_set_param_phy_type(hsotg);
  336. }
  337. static void dwc2_check_param_speed(struct dwc2_hsotg *hsotg)
  338. {
  339. int valid = 1;
  340. int phy_type = hsotg->params.phy_type;
  341. int speed = hsotg->params.speed;
  342. switch (speed) {
  343. case DWC2_SPEED_PARAM_HIGH:
  344. if ((hsotg->params.speed == DWC2_SPEED_PARAM_HIGH) &&
  345. (phy_type == DWC2_PHY_TYPE_PARAM_FS))
  346. valid = 0;
  347. break;
  348. case DWC2_SPEED_PARAM_FULL:
  349. case DWC2_SPEED_PARAM_LOW:
  350. break;
  351. default:
  352. valid = 0;
  353. break;
  354. }
  355. if (!valid)
  356. dwc2_set_param_speed(hsotg);
  357. }
  358. static void dwc2_check_param_phy_utmi_width(struct dwc2_hsotg *hsotg)
  359. {
  360. int valid = 0;
  361. int param = hsotg->params.phy_utmi_width;
  362. int width = hsotg->hw_params.utmi_phy_data_width;
  363. switch (width) {
  364. case GHWCFG4_UTMI_PHY_DATA_WIDTH_8:
  365. valid = (param == 8);
  366. break;
  367. case GHWCFG4_UTMI_PHY_DATA_WIDTH_16:
  368. valid = (param == 16);
  369. break;
  370. case GHWCFG4_UTMI_PHY_DATA_WIDTH_8_OR_16:
  371. valid = (param == 8 || param == 16);
  372. break;
  373. }
  374. if (!valid)
  375. dwc2_set_param_phy_utmi_width(hsotg);
  376. }
  377. static void dwc2_check_param_tx_fifo_sizes(struct dwc2_hsotg *hsotg)
  378. {
  379. int fifo_count;
  380. int fifo;
  381. int min;
  382. u32 total = 0;
  383. u32 dptxfszn;
  384. fifo_count = dwc2_hsotg_tx_fifo_count(hsotg);
  385. min = hsotg->hw_params.en_multiple_tx_fifo ? 16 : 4;
  386. for (fifo = 1; fifo <= fifo_count; fifo++)
  387. total += hsotg->params.g_tx_fifo_size[fifo];
  388. if (total > dwc2_hsotg_tx_fifo_total_depth(hsotg) || !total) {
  389. dev_warn(hsotg->dev, "%s: Invalid parameter g-tx-fifo-size, setting to default average\n",
  390. __func__);
  391. dwc2_set_param_tx_fifo_sizes(hsotg);
  392. }
  393. for (fifo = 1; fifo <= fifo_count; fifo++) {
  394. dptxfszn = (dwc2_readl(hsotg->regs + DPTXFSIZN(fifo)) &
  395. FIFOSIZE_DEPTH_MASK) >> FIFOSIZE_DEPTH_SHIFT;
  396. if (hsotg->params.g_tx_fifo_size[fifo] < min ||
  397. hsotg->params.g_tx_fifo_size[fifo] > dptxfszn) {
  398. dev_warn(hsotg->dev, "%s: Invalid parameter g_tx_fifo_size[%d]=%d\n",
  399. __func__, fifo,
  400. hsotg->params.g_tx_fifo_size[fifo]);
  401. hsotg->params.g_tx_fifo_size[fifo] = dptxfszn;
  402. }
  403. }
  404. }
  405. #define CHECK_RANGE(_param, _min, _max, _def) do { \
  406. if ((hsotg->params._param) < (_min) || \
  407. (hsotg->params._param) > (_max)) { \
  408. dev_warn(hsotg->dev, "%s: Invalid parameter %s=%d\n", \
  409. __func__, #_param, hsotg->params._param); \
  410. hsotg->params._param = (_def); \
  411. } \
  412. } while (0)
  413. #define CHECK_BOOL(_param, _check) do { \
  414. if (hsotg->params._param && !(_check)) { \
  415. dev_warn(hsotg->dev, "%s: Invalid parameter %s=%d\n", \
  416. __func__, #_param, hsotg->params._param); \
  417. hsotg->params._param = false; \
  418. } \
  419. } while (0)
  420. static void dwc2_check_params(struct dwc2_hsotg *hsotg)
  421. {
  422. struct dwc2_hw_params *hw = &hsotg->hw_params;
  423. struct dwc2_core_params *p = &hsotg->params;
  424. bool dma_capable = !(hw->arch == GHWCFG2_SLAVE_ONLY_ARCH);
  425. dwc2_check_param_otg_cap(hsotg);
  426. dwc2_check_param_phy_type(hsotg);
  427. dwc2_check_param_speed(hsotg);
  428. dwc2_check_param_phy_utmi_width(hsotg);
  429. CHECK_BOOL(enable_dynamic_fifo, hw->enable_dynamic_fifo);
  430. CHECK_BOOL(en_multiple_tx_fifo, hw->en_multiple_tx_fifo);
  431. CHECK_BOOL(i2c_enable, hw->i2c_enable);
  432. CHECK_BOOL(reload_ctl, (hsotg->hw_params.snpsid > DWC2_CORE_REV_2_92a));
  433. CHECK_RANGE(max_packet_count,
  434. 15, hw->max_packet_count,
  435. hw->max_packet_count);
  436. CHECK_RANGE(max_transfer_size,
  437. 2047, hw->max_transfer_size,
  438. hw->max_transfer_size);
  439. if ((hsotg->dr_mode == USB_DR_MODE_HOST) ||
  440. (hsotg->dr_mode == USB_DR_MODE_OTG)) {
  441. CHECK_BOOL(host_dma, dma_capable);
  442. CHECK_BOOL(dma_desc_enable, p->host_dma);
  443. CHECK_BOOL(dma_desc_fs_enable, p->dma_desc_enable);
  444. CHECK_BOOL(host_ls_low_power_phy_clk,
  445. p->phy_type == DWC2_PHY_TYPE_PARAM_FS);
  446. CHECK_RANGE(host_channels,
  447. 1, hw->host_channels,
  448. hw->host_channels);
  449. CHECK_RANGE(host_rx_fifo_size,
  450. 16, hw->rx_fifo_size,
  451. hw->rx_fifo_size);
  452. CHECK_RANGE(host_nperio_tx_fifo_size,
  453. 16, hw->host_nperio_tx_fifo_size,
  454. hw->host_nperio_tx_fifo_size);
  455. CHECK_RANGE(host_perio_tx_fifo_size,
  456. 16, hw->host_perio_tx_fifo_size,
  457. hw->host_perio_tx_fifo_size);
  458. }
  459. if ((hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) ||
  460. (hsotg->dr_mode == USB_DR_MODE_OTG)) {
  461. CHECK_BOOL(g_dma, dma_capable);
  462. CHECK_BOOL(g_dma_desc, (p->g_dma && hw->dma_desc_enable));
  463. CHECK_RANGE(g_rx_fifo_size,
  464. 16, hw->rx_fifo_size,
  465. hw->rx_fifo_size);
  466. CHECK_RANGE(g_np_tx_fifo_size,
  467. 16, hw->dev_nperio_tx_fifo_size,
  468. hw->dev_nperio_tx_fifo_size);
  469. dwc2_check_param_tx_fifo_sizes(hsotg);
  470. }
  471. }
  472. /*
  473. * Gets host hardware parameters. Forces host mode if not currently in
  474. * host mode. Should be called immediately after a core soft reset in
  475. * order to get the reset values.
  476. */
  477. static void dwc2_get_host_hwparams(struct dwc2_hsotg *hsotg)
  478. {
  479. struct dwc2_hw_params *hw = &hsotg->hw_params;
  480. u32 gnptxfsiz;
  481. u32 hptxfsiz;
  482. bool forced;
  483. if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
  484. return;
  485. forced = dwc2_force_mode_if_needed(hsotg, true);
  486. gnptxfsiz = dwc2_readl(hsotg->regs + GNPTXFSIZ);
  487. hptxfsiz = dwc2_readl(hsotg->regs + HPTXFSIZ);
  488. if (forced)
  489. dwc2_clear_force_mode(hsotg);
  490. hw->host_nperio_tx_fifo_size = (gnptxfsiz & FIFOSIZE_DEPTH_MASK) >>
  491. FIFOSIZE_DEPTH_SHIFT;
  492. hw->host_perio_tx_fifo_size = (hptxfsiz & FIFOSIZE_DEPTH_MASK) >>
  493. FIFOSIZE_DEPTH_SHIFT;
  494. }
  495. /*
  496. * Gets device hardware parameters. Forces device mode if not
  497. * currently in device mode. Should be called immediately after a core
  498. * soft reset in order to get the reset values.
  499. */
  500. static void dwc2_get_dev_hwparams(struct dwc2_hsotg *hsotg)
  501. {
  502. struct dwc2_hw_params *hw = &hsotg->hw_params;
  503. bool forced;
  504. u32 gnptxfsiz;
  505. if (hsotg->dr_mode == USB_DR_MODE_HOST)
  506. return;
  507. forced = dwc2_force_mode_if_needed(hsotg, false);
  508. gnptxfsiz = dwc2_readl(hsotg->regs + GNPTXFSIZ);
  509. if (forced)
  510. dwc2_clear_force_mode(hsotg);
  511. hw->dev_nperio_tx_fifo_size = (gnptxfsiz & FIFOSIZE_DEPTH_MASK) >>
  512. FIFOSIZE_DEPTH_SHIFT;
  513. }
  514. /**
  515. * During device initialization, read various hardware configuration
  516. * registers and interpret the contents.
  517. */
  518. int dwc2_get_hwparams(struct dwc2_hsotg *hsotg)
  519. {
  520. struct dwc2_hw_params *hw = &hsotg->hw_params;
  521. unsigned int width;
  522. u32 hwcfg1, hwcfg2, hwcfg3, hwcfg4;
  523. u32 grxfsiz;
  524. /*
  525. * Attempt to ensure this device is really a DWC_otg Controller.
  526. * Read and verify the GSNPSID register contents. The value should be
  527. * 0x45f42xxx or 0x45f43xxx, which corresponds to either "OT2" or "OT3",
  528. * as in "OTG version 2.xx" or "OTG version 3.xx".
  529. */
  530. hw->snpsid = dwc2_readl(hsotg->regs + GSNPSID);
  531. if ((hw->snpsid & 0xfffff000) != 0x4f542000 &&
  532. (hw->snpsid & 0xfffff000) != 0x4f543000 &&
  533. (hw->snpsid & 0xffff0000) != 0x55310000 &&
  534. (hw->snpsid & 0xffff0000) != 0x55320000) {
  535. dev_err(hsotg->dev, "Bad value for GSNPSID: 0x%08x\n",
  536. hw->snpsid);
  537. return -ENODEV;
  538. }
  539. dev_dbg(hsotg->dev, "Core Release: %1x.%1x%1x%1x (snpsid=%x)\n",
  540. hw->snpsid >> 12 & 0xf, hw->snpsid >> 8 & 0xf,
  541. hw->snpsid >> 4 & 0xf, hw->snpsid & 0xf, hw->snpsid);
  542. hwcfg1 = dwc2_readl(hsotg->regs + GHWCFG1);
  543. hwcfg2 = dwc2_readl(hsotg->regs + GHWCFG2);
  544. hwcfg3 = dwc2_readl(hsotg->regs + GHWCFG3);
  545. hwcfg4 = dwc2_readl(hsotg->regs + GHWCFG4);
  546. grxfsiz = dwc2_readl(hsotg->regs + GRXFSIZ);
  547. /*
  548. * Host specific hardware parameters. Reading these parameters
  549. * requires the controller to be in host mode. The mode will
  550. * be forced, if necessary, to read these values.
  551. */
  552. dwc2_get_host_hwparams(hsotg);
  553. dwc2_get_dev_hwparams(hsotg);
  554. /* hwcfg1 */
  555. hw->dev_ep_dirs = hwcfg1;
  556. /* hwcfg2 */
  557. hw->op_mode = (hwcfg2 & GHWCFG2_OP_MODE_MASK) >>
  558. GHWCFG2_OP_MODE_SHIFT;
  559. hw->arch = (hwcfg2 & GHWCFG2_ARCHITECTURE_MASK) >>
  560. GHWCFG2_ARCHITECTURE_SHIFT;
  561. hw->enable_dynamic_fifo = !!(hwcfg2 & GHWCFG2_DYNAMIC_FIFO);
  562. hw->host_channels = 1 + ((hwcfg2 & GHWCFG2_NUM_HOST_CHAN_MASK) >>
  563. GHWCFG2_NUM_HOST_CHAN_SHIFT);
  564. hw->hs_phy_type = (hwcfg2 & GHWCFG2_HS_PHY_TYPE_MASK) >>
  565. GHWCFG2_HS_PHY_TYPE_SHIFT;
  566. hw->fs_phy_type = (hwcfg2 & GHWCFG2_FS_PHY_TYPE_MASK) >>
  567. GHWCFG2_FS_PHY_TYPE_SHIFT;
  568. hw->num_dev_ep = (hwcfg2 & GHWCFG2_NUM_DEV_EP_MASK) >>
  569. GHWCFG2_NUM_DEV_EP_SHIFT;
  570. hw->nperio_tx_q_depth =
  571. (hwcfg2 & GHWCFG2_NONPERIO_TX_Q_DEPTH_MASK) >>
  572. GHWCFG2_NONPERIO_TX_Q_DEPTH_SHIFT << 1;
  573. hw->host_perio_tx_q_depth =
  574. (hwcfg2 & GHWCFG2_HOST_PERIO_TX_Q_DEPTH_MASK) >>
  575. GHWCFG2_HOST_PERIO_TX_Q_DEPTH_SHIFT << 1;
  576. hw->dev_token_q_depth =
  577. (hwcfg2 & GHWCFG2_DEV_TOKEN_Q_DEPTH_MASK) >>
  578. GHWCFG2_DEV_TOKEN_Q_DEPTH_SHIFT;
  579. /* hwcfg3 */
  580. width = (hwcfg3 & GHWCFG3_XFER_SIZE_CNTR_WIDTH_MASK) >>
  581. GHWCFG3_XFER_SIZE_CNTR_WIDTH_SHIFT;
  582. hw->max_transfer_size = (1 << (width + 11)) - 1;
  583. width = (hwcfg3 & GHWCFG3_PACKET_SIZE_CNTR_WIDTH_MASK) >>
  584. GHWCFG3_PACKET_SIZE_CNTR_WIDTH_SHIFT;
  585. hw->max_packet_count = (1 << (width + 4)) - 1;
  586. hw->i2c_enable = !!(hwcfg3 & GHWCFG3_I2C);
  587. hw->total_fifo_size = (hwcfg3 & GHWCFG3_DFIFO_DEPTH_MASK) >>
  588. GHWCFG3_DFIFO_DEPTH_SHIFT;
  589. /* hwcfg4 */
  590. hw->en_multiple_tx_fifo = !!(hwcfg4 & GHWCFG4_DED_FIFO_EN);
  591. hw->num_dev_perio_in_ep = (hwcfg4 & GHWCFG4_NUM_DEV_PERIO_IN_EP_MASK) >>
  592. GHWCFG4_NUM_DEV_PERIO_IN_EP_SHIFT;
  593. hw->dma_desc_enable = !!(hwcfg4 & GHWCFG4_DESC_DMA);
  594. hw->power_optimized = !!(hwcfg4 & GHWCFG4_POWER_OPTIMIZ);
  595. hw->utmi_phy_data_width = (hwcfg4 & GHWCFG4_UTMI_PHY_DATA_WIDTH_MASK) >>
  596. GHWCFG4_UTMI_PHY_DATA_WIDTH_SHIFT;
  597. /* fifo sizes */
  598. hw->rx_fifo_size = (grxfsiz & GRXFSIZ_DEPTH_MASK) >>
  599. GRXFSIZ_DEPTH_SHIFT;
  600. return 0;
  601. }
  602. int dwc2_init_params(struct dwc2_hsotg *hsotg)
  603. {
  604. dwc2_set_default_params(hsotg);
  605. dwc2_get_device_properties(hsotg);
  606. dwc2_check_params(hsotg);
  607. return 0;
  608. }