params.c 20 KB

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