params.c 21 KB

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