params.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
  2. /*
  3. * Copyright (C) 2004-2016 Synopsys, Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions, and the following disclaimer,
  10. * without modification.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. The names of the above-listed copyright holders may not be used
  15. * to endorse or promote products derived from this software without
  16. * specific prior written permission.
  17. *
  18. * ALTERNATIVELY, this software may be distributed under the terms of the
  19. * GNU General Public License ("GPL") as published by the Free Software
  20. * Foundation; either version 2 of the License, or (at your option) any
  21. * later version.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  24. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  25. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  26. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  27. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  28. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  29. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  30. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  31. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  32. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  33. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. #include <linux/kernel.h>
  36. #include <linux/module.h>
  37. #include <linux/of_device.h>
  38. #include "core.h"
  39. static void dwc2_set_bcm_params(struct dwc2_hsotg *hsotg)
  40. {
  41. struct dwc2_core_params *p = &hsotg->params;
  42. p->host_rx_fifo_size = 774;
  43. p->max_transfer_size = 65535;
  44. p->max_packet_count = 511;
  45. p->ahbcfg = 0x10;
  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->change_speed_quirk = true;
  65. p->power_down = false;
  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. }
  102. static void dwc2_set_amcc_params(struct dwc2_hsotg *hsotg)
  103. {
  104. struct dwc2_core_params *p = &hsotg->params;
  105. p->ahbcfg = GAHBCFG_HBSTLEN_INCR16 << GAHBCFG_HBSTLEN_SHIFT;
  106. }
  107. static void dwc2_set_stm32f4x9_fsotg_params(struct dwc2_hsotg *hsotg)
  108. {
  109. struct dwc2_core_params *p = &hsotg->params;
  110. p->otg_cap = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE;
  111. p->speed = DWC2_SPEED_PARAM_FULL;
  112. p->host_rx_fifo_size = 128;
  113. p->host_nperio_tx_fifo_size = 96;
  114. p->host_perio_tx_fifo_size = 96;
  115. p->max_packet_count = 256;
  116. p->phy_type = DWC2_PHY_TYPE_PARAM_FS;
  117. p->i2c_enable = false;
  118. p->activate_stm_fs_transceiver = true;
  119. }
  120. static void dwc2_set_stm32f7_hsotg_params(struct dwc2_hsotg *hsotg)
  121. {
  122. struct dwc2_core_params *p = &hsotg->params;
  123. p->host_rx_fifo_size = 622;
  124. p->host_nperio_tx_fifo_size = 128;
  125. p->host_perio_tx_fifo_size = 256;
  126. }
  127. const struct of_device_id dwc2_of_match_table[] = {
  128. { .compatible = "brcm,bcm2835-usb", .data = dwc2_set_bcm_params },
  129. { .compatible = "hisilicon,hi6220-usb", .data = dwc2_set_his_params },
  130. { .compatible = "rockchip,rk3066-usb", .data = dwc2_set_rk_params },
  131. { .compatible = "lantiq,arx100-usb", .data = dwc2_set_ltq_params },
  132. { .compatible = "lantiq,xrx200-usb", .data = dwc2_set_ltq_params },
  133. { .compatible = "snps,dwc2" },
  134. { .compatible = "samsung,s3c6400-hsotg" },
  135. { .compatible = "amlogic,meson8-usb",
  136. .data = dwc2_set_amlogic_params },
  137. { .compatible = "amlogic,meson8b-usb",
  138. .data = dwc2_set_amlogic_params },
  139. { .compatible = "amlogic,meson-gxbb-usb",
  140. .data = dwc2_set_amlogic_params },
  141. { .compatible = "amcc,dwc-otg", .data = dwc2_set_amcc_params },
  142. { .compatible = "st,stm32f4x9-fsotg",
  143. .data = dwc2_set_stm32f4x9_fsotg_params },
  144. { .compatible = "st,stm32f4x9-hsotg" },
  145. { .compatible = "st,stm32f7-hsotg",
  146. .data = dwc2_set_stm32f7_hsotg_params },
  147. {},
  148. };
  149. MODULE_DEVICE_TABLE(of, dwc2_of_match_table);
  150. static void dwc2_set_param_otg_cap(struct dwc2_hsotg *hsotg)
  151. {
  152. u8 val;
  153. switch (hsotg->hw_params.op_mode) {
  154. case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
  155. val = DWC2_CAP_PARAM_HNP_SRP_CAPABLE;
  156. break;
  157. case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
  158. case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
  159. case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
  160. val = DWC2_CAP_PARAM_SRP_ONLY_CAPABLE;
  161. break;
  162. default:
  163. val = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE;
  164. break;
  165. }
  166. hsotg->params.otg_cap = val;
  167. }
  168. static void dwc2_set_param_phy_type(struct dwc2_hsotg *hsotg)
  169. {
  170. int val;
  171. u32 hs_phy_type = hsotg->hw_params.hs_phy_type;
  172. val = DWC2_PHY_TYPE_PARAM_FS;
  173. if (hs_phy_type != GHWCFG2_HS_PHY_TYPE_NOT_SUPPORTED) {
  174. if (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI ||
  175. hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI)
  176. val = DWC2_PHY_TYPE_PARAM_UTMI;
  177. else
  178. val = DWC2_PHY_TYPE_PARAM_ULPI;
  179. }
  180. if (dwc2_is_fs_iot(hsotg))
  181. hsotg->params.phy_type = DWC2_PHY_TYPE_PARAM_FS;
  182. hsotg->params.phy_type = val;
  183. }
  184. static void dwc2_set_param_speed(struct dwc2_hsotg *hsotg)
  185. {
  186. int val;
  187. val = hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS ?
  188. DWC2_SPEED_PARAM_FULL : DWC2_SPEED_PARAM_HIGH;
  189. if (dwc2_is_fs_iot(hsotg))
  190. val = DWC2_SPEED_PARAM_FULL;
  191. if (dwc2_is_hs_iot(hsotg))
  192. val = DWC2_SPEED_PARAM_HIGH;
  193. hsotg->params.speed = val;
  194. }
  195. static void dwc2_set_param_phy_utmi_width(struct dwc2_hsotg *hsotg)
  196. {
  197. int val;
  198. val = (hsotg->hw_params.utmi_phy_data_width ==
  199. GHWCFG4_UTMI_PHY_DATA_WIDTH_8) ? 8 : 16;
  200. hsotg->params.phy_utmi_width = val;
  201. }
  202. static void dwc2_set_param_tx_fifo_sizes(struct dwc2_hsotg *hsotg)
  203. {
  204. struct dwc2_core_params *p = &hsotg->params;
  205. int depth_average;
  206. int fifo_count;
  207. int i;
  208. fifo_count = dwc2_hsotg_tx_fifo_count(hsotg);
  209. memset(p->g_tx_fifo_size, 0, sizeof(p->g_tx_fifo_size));
  210. depth_average = dwc2_hsotg_tx_fifo_average_depth(hsotg);
  211. for (i = 1; i <= fifo_count; i++)
  212. p->g_tx_fifo_size[i] = depth_average;
  213. }
  214. static void dwc2_set_param_power_down(struct dwc2_hsotg *hsotg)
  215. {
  216. int val;
  217. if (hsotg->hw_params.hibernation)
  218. val = 2;
  219. else if (hsotg->hw_params.power_optimized)
  220. val = 1;
  221. else
  222. val = 0;
  223. hsotg->params.power_down = val;
  224. }
  225. /**
  226. * dwc2_set_default_params() - Set all core parameters to their
  227. * auto-detected default values.
  228. *
  229. * @hsotg: Programming view of the DWC_otg controller
  230. *
  231. */
  232. static void dwc2_set_default_params(struct dwc2_hsotg *hsotg)
  233. {
  234. struct dwc2_hw_params *hw = &hsotg->hw_params;
  235. struct dwc2_core_params *p = &hsotg->params;
  236. bool dma_capable = !(hw->arch == GHWCFG2_SLAVE_ONLY_ARCH);
  237. dwc2_set_param_otg_cap(hsotg);
  238. dwc2_set_param_phy_type(hsotg);
  239. dwc2_set_param_speed(hsotg);
  240. dwc2_set_param_phy_utmi_width(hsotg);
  241. dwc2_set_param_power_down(hsotg);
  242. p->phy_ulpi_ddr = false;
  243. p->phy_ulpi_ext_vbus = false;
  244. p->enable_dynamic_fifo = hw->enable_dynamic_fifo;
  245. p->en_multiple_tx_fifo = hw->en_multiple_tx_fifo;
  246. p->i2c_enable = hw->i2c_enable;
  247. p->acg_enable = hw->acg_enable;
  248. p->ulpi_fs_ls = false;
  249. p->ts_dline = false;
  250. p->reload_ctl = (hw->snpsid >= DWC2_CORE_REV_2_92a);
  251. p->uframe_sched = true;
  252. p->external_id_pin_ctl = false;
  253. p->lpm = true;
  254. p->lpm_clock_gating = true;
  255. p->besl = true;
  256. p->hird_threshold_en = true;
  257. p->hird_threshold = 4;
  258. p->ipg_isoc_en = false;
  259. p->max_packet_count = hw->max_packet_count;
  260. p->max_transfer_size = hw->max_transfer_size;
  261. p->ahbcfg = GAHBCFG_HBSTLEN_INCR << GAHBCFG_HBSTLEN_SHIFT;
  262. if ((hsotg->dr_mode == USB_DR_MODE_HOST) ||
  263. (hsotg->dr_mode == USB_DR_MODE_OTG)) {
  264. p->host_dma = dma_capable;
  265. p->dma_desc_enable = false;
  266. p->dma_desc_fs_enable = false;
  267. p->host_support_fs_ls_low_power = false;
  268. p->host_ls_low_power_phy_clk = false;
  269. p->host_channels = hw->host_channels;
  270. p->host_rx_fifo_size = hw->rx_fifo_size;
  271. p->host_nperio_tx_fifo_size = hw->host_nperio_tx_fifo_size;
  272. p->host_perio_tx_fifo_size = hw->host_perio_tx_fifo_size;
  273. }
  274. if ((hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) ||
  275. (hsotg->dr_mode == USB_DR_MODE_OTG)) {
  276. p->g_dma = dma_capable;
  277. p->g_dma_desc = hw->dma_desc_enable;
  278. /*
  279. * The values for g_rx_fifo_size (2048) and
  280. * g_np_tx_fifo_size (1024) come from the legacy s3c
  281. * gadget driver. These defaults have been hard-coded
  282. * for some time so many platforms depend on these
  283. * values. Leave them as defaults for now and only
  284. * auto-detect if the hardware does not support the
  285. * default.
  286. */
  287. p->g_rx_fifo_size = 2048;
  288. p->g_np_tx_fifo_size = 1024;
  289. dwc2_set_param_tx_fifo_sizes(hsotg);
  290. }
  291. }
  292. /**
  293. * dwc2_get_device_properties() - Read in device properties.
  294. *
  295. * @hsotg: Programming view of the DWC_otg controller
  296. *
  297. * Read in the device properties and adjust core parameters if needed.
  298. */
  299. static void dwc2_get_device_properties(struct dwc2_hsotg *hsotg)
  300. {
  301. struct dwc2_core_params *p = &hsotg->params;
  302. int num;
  303. if ((hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) ||
  304. (hsotg->dr_mode == USB_DR_MODE_OTG)) {
  305. device_property_read_u32(hsotg->dev, "g-rx-fifo-size",
  306. &p->g_rx_fifo_size);
  307. device_property_read_u32(hsotg->dev, "g-np-tx-fifo-size",
  308. &p->g_np_tx_fifo_size);
  309. num = device_property_read_u32_array(hsotg->dev,
  310. "g-tx-fifo-size",
  311. NULL, 0);
  312. if (num > 0) {
  313. num = min(num, 15);
  314. memset(p->g_tx_fifo_size, 0,
  315. sizeof(p->g_tx_fifo_size));
  316. device_property_read_u32_array(hsotg->dev,
  317. "g-tx-fifo-size",
  318. &p->g_tx_fifo_size[1],
  319. num);
  320. }
  321. }
  322. if (of_find_property(hsotg->dev->of_node, "disable-over-current", NULL))
  323. p->oc_disable = true;
  324. }
  325. static void dwc2_check_param_otg_cap(struct dwc2_hsotg *hsotg)
  326. {
  327. int valid = 1;
  328. switch (hsotg->params.otg_cap) {
  329. case DWC2_CAP_PARAM_HNP_SRP_CAPABLE:
  330. if (hsotg->hw_params.op_mode != GHWCFG2_OP_MODE_HNP_SRP_CAPABLE)
  331. valid = 0;
  332. break;
  333. case DWC2_CAP_PARAM_SRP_ONLY_CAPABLE:
  334. switch (hsotg->hw_params.op_mode) {
  335. case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
  336. case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
  337. case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
  338. case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
  339. break;
  340. default:
  341. valid = 0;
  342. break;
  343. }
  344. break;
  345. case DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE:
  346. /* always valid */
  347. break;
  348. default:
  349. valid = 0;
  350. break;
  351. }
  352. if (!valid)
  353. dwc2_set_param_otg_cap(hsotg);
  354. }
  355. static void dwc2_check_param_phy_type(struct dwc2_hsotg *hsotg)
  356. {
  357. int valid = 0;
  358. u32 hs_phy_type;
  359. u32 fs_phy_type;
  360. hs_phy_type = hsotg->hw_params.hs_phy_type;
  361. fs_phy_type = hsotg->hw_params.fs_phy_type;
  362. switch (hsotg->params.phy_type) {
  363. case DWC2_PHY_TYPE_PARAM_FS:
  364. if (fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED)
  365. valid = 1;
  366. break;
  367. case DWC2_PHY_TYPE_PARAM_UTMI:
  368. if ((hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI) ||
  369. (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI))
  370. valid = 1;
  371. break;
  372. case DWC2_PHY_TYPE_PARAM_ULPI:
  373. if ((hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI) ||
  374. (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI))
  375. valid = 1;
  376. break;
  377. default:
  378. break;
  379. }
  380. if (!valid)
  381. dwc2_set_param_phy_type(hsotg);
  382. }
  383. static void dwc2_check_param_speed(struct dwc2_hsotg *hsotg)
  384. {
  385. int valid = 1;
  386. int phy_type = hsotg->params.phy_type;
  387. int speed = hsotg->params.speed;
  388. switch (speed) {
  389. case DWC2_SPEED_PARAM_HIGH:
  390. if ((hsotg->params.speed == DWC2_SPEED_PARAM_HIGH) &&
  391. (phy_type == DWC2_PHY_TYPE_PARAM_FS))
  392. valid = 0;
  393. break;
  394. case DWC2_SPEED_PARAM_FULL:
  395. case DWC2_SPEED_PARAM_LOW:
  396. break;
  397. default:
  398. valid = 0;
  399. break;
  400. }
  401. if (!valid)
  402. dwc2_set_param_speed(hsotg);
  403. }
  404. static void dwc2_check_param_phy_utmi_width(struct dwc2_hsotg *hsotg)
  405. {
  406. int valid = 0;
  407. int param = hsotg->params.phy_utmi_width;
  408. int width = hsotg->hw_params.utmi_phy_data_width;
  409. switch (width) {
  410. case GHWCFG4_UTMI_PHY_DATA_WIDTH_8:
  411. valid = (param == 8);
  412. break;
  413. case GHWCFG4_UTMI_PHY_DATA_WIDTH_16:
  414. valid = (param == 16);
  415. break;
  416. case GHWCFG4_UTMI_PHY_DATA_WIDTH_8_OR_16:
  417. valid = (param == 8 || param == 16);
  418. break;
  419. }
  420. if (!valid)
  421. dwc2_set_param_phy_utmi_width(hsotg);
  422. }
  423. static void dwc2_check_param_power_down(struct dwc2_hsotg *hsotg)
  424. {
  425. int param = hsotg->params.power_down;
  426. switch (param) {
  427. case DWC2_POWER_DOWN_PARAM_NONE:
  428. break;
  429. case DWC2_POWER_DOWN_PARAM_PARTIAL:
  430. if (hsotg->hw_params.power_optimized)
  431. break;
  432. dev_dbg(hsotg->dev,
  433. "Partial power down isn't supported by HW\n");
  434. param = DWC2_POWER_DOWN_PARAM_NONE;
  435. break;
  436. case DWC2_POWER_DOWN_PARAM_HIBERNATION:
  437. if (hsotg->hw_params.hibernation)
  438. break;
  439. dev_dbg(hsotg->dev,
  440. "Hibernation isn't supported by HW\n");
  441. param = DWC2_POWER_DOWN_PARAM_NONE;
  442. break;
  443. default:
  444. dev_err(hsotg->dev,
  445. "%s: Invalid parameter power_down=%d\n",
  446. __func__, param);
  447. param = DWC2_POWER_DOWN_PARAM_NONE;
  448. break;
  449. }
  450. hsotg->params.power_down = param;
  451. }
  452. static void dwc2_check_param_tx_fifo_sizes(struct dwc2_hsotg *hsotg)
  453. {
  454. int fifo_count;
  455. int fifo;
  456. int min;
  457. u32 total = 0;
  458. u32 dptxfszn;
  459. fifo_count = dwc2_hsotg_tx_fifo_count(hsotg);
  460. min = hsotg->hw_params.en_multiple_tx_fifo ? 16 : 4;
  461. for (fifo = 1; fifo <= fifo_count; fifo++)
  462. total += hsotg->params.g_tx_fifo_size[fifo];
  463. if (total > dwc2_hsotg_tx_fifo_total_depth(hsotg) || !total) {
  464. dev_warn(hsotg->dev, "%s: Invalid parameter g-tx-fifo-size, setting to default average\n",
  465. __func__);
  466. dwc2_set_param_tx_fifo_sizes(hsotg);
  467. }
  468. for (fifo = 1; fifo <= fifo_count; fifo++) {
  469. dptxfszn = hsotg->hw_params.g_tx_fifo_size[fifo];
  470. if (hsotg->params.g_tx_fifo_size[fifo] < min ||
  471. hsotg->params.g_tx_fifo_size[fifo] > dptxfszn) {
  472. dev_warn(hsotg->dev, "%s: Invalid parameter g_tx_fifo_size[%d]=%d\n",
  473. __func__, fifo,
  474. hsotg->params.g_tx_fifo_size[fifo]);
  475. hsotg->params.g_tx_fifo_size[fifo] = dptxfszn;
  476. }
  477. }
  478. }
  479. #define CHECK_RANGE(_param, _min, _max, _def) do { \
  480. if ((int)(hsotg->params._param) < (_min) || \
  481. (hsotg->params._param) > (_max)) { \
  482. dev_warn(hsotg->dev, "%s: Invalid parameter %s=%d\n", \
  483. __func__, #_param, hsotg->params._param); \
  484. hsotg->params._param = (_def); \
  485. } \
  486. } while (0)
  487. #define CHECK_BOOL(_param, _check) do { \
  488. if (hsotg->params._param && !(_check)) { \
  489. dev_warn(hsotg->dev, "%s: Invalid parameter %s=%d\n", \
  490. __func__, #_param, hsotg->params._param); \
  491. hsotg->params._param = false; \
  492. } \
  493. } while (0)
  494. static void dwc2_check_params(struct dwc2_hsotg *hsotg)
  495. {
  496. struct dwc2_hw_params *hw = &hsotg->hw_params;
  497. struct dwc2_core_params *p = &hsotg->params;
  498. bool dma_capable = !(hw->arch == GHWCFG2_SLAVE_ONLY_ARCH);
  499. dwc2_check_param_otg_cap(hsotg);
  500. dwc2_check_param_phy_type(hsotg);
  501. dwc2_check_param_speed(hsotg);
  502. dwc2_check_param_phy_utmi_width(hsotg);
  503. dwc2_check_param_power_down(hsotg);
  504. CHECK_BOOL(enable_dynamic_fifo, hw->enable_dynamic_fifo);
  505. CHECK_BOOL(en_multiple_tx_fifo, hw->en_multiple_tx_fifo);
  506. CHECK_BOOL(i2c_enable, hw->i2c_enable);
  507. CHECK_BOOL(ipg_isoc_en, hw->ipg_isoc_en);
  508. CHECK_BOOL(acg_enable, hw->acg_enable);
  509. CHECK_BOOL(reload_ctl, (hsotg->hw_params.snpsid > DWC2_CORE_REV_2_92a));
  510. CHECK_BOOL(lpm, (hsotg->hw_params.snpsid >= DWC2_CORE_REV_2_80a));
  511. CHECK_BOOL(lpm, hw->lpm_mode);
  512. CHECK_BOOL(lpm_clock_gating, hsotg->params.lpm);
  513. CHECK_BOOL(besl, hsotg->params.lpm);
  514. CHECK_BOOL(besl, (hsotg->hw_params.snpsid >= DWC2_CORE_REV_3_00a));
  515. CHECK_BOOL(hird_threshold_en, hsotg->params.lpm);
  516. CHECK_RANGE(hird_threshold, 0, hsotg->params.besl ? 12 : 7, 0);
  517. CHECK_RANGE(max_packet_count,
  518. 15, hw->max_packet_count,
  519. hw->max_packet_count);
  520. CHECK_RANGE(max_transfer_size,
  521. 2047, hw->max_transfer_size,
  522. hw->max_transfer_size);
  523. if ((hsotg->dr_mode == USB_DR_MODE_HOST) ||
  524. (hsotg->dr_mode == USB_DR_MODE_OTG)) {
  525. CHECK_BOOL(host_dma, dma_capable);
  526. CHECK_BOOL(dma_desc_enable, p->host_dma);
  527. CHECK_BOOL(dma_desc_fs_enable, p->dma_desc_enable);
  528. CHECK_BOOL(host_ls_low_power_phy_clk,
  529. p->phy_type == DWC2_PHY_TYPE_PARAM_FS);
  530. CHECK_RANGE(host_channels,
  531. 1, hw->host_channels,
  532. hw->host_channels);
  533. CHECK_RANGE(host_rx_fifo_size,
  534. 16, hw->rx_fifo_size,
  535. hw->rx_fifo_size);
  536. CHECK_RANGE(host_nperio_tx_fifo_size,
  537. 16, hw->host_nperio_tx_fifo_size,
  538. hw->host_nperio_tx_fifo_size);
  539. CHECK_RANGE(host_perio_tx_fifo_size,
  540. 16, hw->host_perio_tx_fifo_size,
  541. hw->host_perio_tx_fifo_size);
  542. }
  543. if ((hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) ||
  544. (hsotg->dr_mode == USB_DR_MODE_OTG)) {
  545. CHECK_BOOL(g_dma, dma_capable);
  546. CHECK_BOOL(g_dma_desc, (p->g_dma && hw->dma_desc_enable));
  547. CHECK_RANGE(g_rx_fifo_size,
  548. 16, hw->rx_fifo_size,
  549. hw->rx_fifo_size);
  550. CHECK_RANGE(g_np_tx_fifo_size,
  551. 16, hw->dev_nperio_tx_fifo_size,
  552. hw->dev_nperio_tx_fifo_size);
  553. dwc2_check_param_tx_fifo_sizes(hsotg);
  554. }
  555. }
  556. /*
  557. * Gets host hardware parameters. Forces host mode if not currently in
  558. * host mode. Should be called immediately after a core soft reset in
  559. * order to get the reset values.
  560. */
  561. static void dwc2_get_host_hwparams(struct dwc2_hsotg *hsotg)
  562. {
  563. struct dwc2_hw_params *hw = &hsotg->hw_params;
  564. u32 gnptxfsiz;
  565. u32 hptxfsiz;
  566. if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
  567. return;
  568. dwc2_force_mode(hsotg, true);
  569. gnptxfsiz = dwc2_readl(hsotg, GNPTXFSIZ);
  570. hptxfsiz = dwc2_readl(hsotg, HPTXFSIZ);
  571. hw->host_nperio_tx_fifo_size = (gnptxfsiz & FIFOSIZE_DEPTH_MASK) >>
  572. FIFOSIZE_DEPTH_SHIFT;
  573. hw->host_perio_tx_fifo_size = (hptxfsiz & FIFOSIZE_DEPTH_MASK) >>
  574. FIFOSIZE_DEPTH_SHIFT;
  575. }
  576. /*
  577. * Gets device hardware parameters. Forces device mode if not
  578. * currently in device mode. Should be called immediately after a core
  579. * soft reset in order to get the reset values.
  580. */
  581. static void dwc2_get_dev_hwparams(struct dwc2_hsotg *hsotg)
  582. {
  583. struct dwc2_hw_params *hw = &hsotg->hw_params;
  584. u32 gnptxfsiz;
  585. int fifo, fifo_count;
  586. if (hsotg->dr_mode == USB_DR_MODE_HOST)
  587. return;
  588. dwc2_force_mode(hsotg, false);
  589. gnptxfsiz = dwc2_readl(hsotg, GNPTXFSIZ);
  590. fifo_count = dwc2_hsotg_tx_fifo_count(hsotg);
  591. for (fifo = 1; fifo <= fifo_count; fifo++) {
  592. hw->g_tx_fifo_size[fifo] =
  593. (dwc2_readl(hsotg, DPTXFSIZN(fifo)) &
  594. FIFOSIZE_DEPTH_MASK) >> FIFOSIZE_DEPTH_SHIFT;
  595. }
  596. hw->dev_nperio_tx_fifo_size = (gnptxfsiz & FIFOSIZE_DEPTH_MASK) >>
  597. FIFOSIZE_DEPTH_SHIFT;
  598. }
  599. /**
  600. * During device initialization, read various hardware configuration
  601. * registers and interpret the contents.
  602. *
  603. * @hsotg: Programming view of the DWC_otg controller
  604. *
  605. */
  606. int dwc2_get_hwparams(struct dwc2_hsotg *hsotg)
  607. {
  608. struct dwc2_hw_params *hw = &hsotg->hw_params;
  609. unsigned int width;
  610. u32 hwcfg1, hwcfg2, hwcfg3, hwcfg4;
  611. u32 grxfsiz;
  612. /*
  613. * Attempt to ensure this device is really a DWC_otg Controller.
  614. * Read and verify the GSNPSID register contents. The value should be
  615. * 0x45f4xxxx, 0x5531xxxx or 0x5532xxxx
  616. */
  617. hw->snpsid = dwc2_readl(hsotg, GSNPSID);
  618. if ((hw->snpsid & GSNPSID_ID_MASK) != DWC2_OTG_ID &&
  619. (hw->snpsid & GSNPSID_ID_MASK) != DWC2_FS_IOT_ID &&
  620. (hw->snpsid & GSNPSID_ID_MASK) != DWC2_HS_IOT_ID) {
  621. dev_err(hsotg->dev, "Bad value for GSNPSID: 0x%08x\n",
  622. hw->snpsid);
  623. return -ENODEV;
  624. }
  625. dev_dbg(hsotg->dev, "Core Release: %1x.%1x%1x%1x (snpsid=%x)\n",
  626. hw->snpsid >> 12 & 0xf, hw->snpsid >> 8 & 0xf,
  627. hw->snpsid >> 4 & 0xf, hw->snpsid & 0xf, hw->snpsid);
  628. hwcfg1 = dwc2_readl(hsotg, GHWCFG1);
  629. hwcfg2 = dwc2_readl(hsotg, GHWCFG2);
  630. hwcfg3 = dwc2_readl(hsotg, GHWCFG3);
  631. hwcfg4 = dwc2_readl(hsotg, GHWCFG4);
  632. grxfsiz = dwc2_readl(hsotg, GRXFSIZ);
  633. /* hwcfg1 */
  634. hw->dev_ep_dirs = hwcfg1;
  635. /* hwcfg2 */
  636. hw->op_mode = (hwcfg2 & GHWCFG2_OP_MODE_MASK) >>
  637. GHWCFG2_OP_MODE_SHIFT;
  638. hw->arch = (hwcfg2 & GHWCFG2_ARCHITECTURE_MASK) >>
  639. GHWCFG2_ARCHITECTURE_SHIFT;
  640. hw->enable_dynamic_fifo = !!(hwcfg2 & GHWCFG2_DYNAMIC_FIFO);
  641. hw->host_channels = 1 + ((hwcfg2 & GHWCFG2_NUM_HOST_CHAN_MASK) >>
  642. GHWCFG2_NUM_HOST_CHAN_SHIFT);
  643. hw->hs_phy_type = (hwcfg2 & GHWCFG2_HS_PHY_TYPE_MASK) >>
  644. GHWCFG2_HS_PHY_TYPE_SHIFT;
  645. hw->fs_phy_type = (hwcfg2 & GHWCFG2_FS_PHY_TYPE_MASK) >>
  646. GHWCFG2_FS_PHY_TYPE_SHIFT;
  647. hw->num_dev_ep = (hwcfg2 & GHWCFG2_NUM_DEV_EP_MASK) >>
  648. GHWCFG2_NUM_DEV_EP_SHIFT;
  649. hw->nperio_tx_q_depth =
  650. (hwcfg2 & GHWCFG2_NONPERIO_TX_Q_DEPTH_MASK) >>
  651. GHWCFG2_NONPERIO_TX_Q_DEPTH_SHIFT << 1;
  652. hw->host_perio_tx_q_depth =
  653. (hwcfg2 & GHWCFG2_HOST_PERIO_TX_Q_DEPTH_MASK) >>
  654. GHWCFG2_HOST_PERIO_TX_Q_DEPTH_SHIFT << 1;
  655. hw->dev_token_q_depth =
  656. (hwcfg2 & GHWCFG2_DEV_TOKEN_Q_DEPTH_MASK) >>
  657. GHWCFG2_DEV_TOKEN_Q_DEPTH_SHIFT;
  658. /* hwcfg3 */
  659. width = (hwcfg3 & GHWCFG3_XFER_SIZE_CNTR_WIDTH_MASK) >>
  660. GHWCFG3_XFER_SIZE_CNTR_WIDTH_SHIFT;
  661. hw->max_transfer_size = (1 << (width + 11)) - 1;
  662. width = (hwcfg3 & GHWCFG3_PACKET_SIZE_CNTR_WIDTH_MASK) >>
  663. GHWCFG3_PACKET_SIZE_CNTR_WIDTH_SHIFT;
  664. hw->max_packet_count = (1 << (width + 4)) - 1;
  665. hw->i2c_enable = !!(hwcfg3 & GHWCFG3_I2C);
  666. hw->total_fifo_size = (hwcfg3 & GHWCFG3_DFIFO_DEPTH_MASK) >>
  667. GHWCFG3_DFIFO_DEPTH_SHIFT;
  668. hw->lpm_mode = !!(hwcfg3 & GHWCFG3_OTG_LPM_EN);
  669. /* hwcfg4 */
  670. hw->en_multiple_tx_fifo = !!(hwcfg4 & GHWCFG4_DED_FIFO_EN);
  671. hw->num_dev_perio_in_ep = (hwcfg4 & GHWCFG4_NUM_DEV_PERIO_IN_EP_MASK) >>
  672. GHWCFG4_NUM_DEV_PERIO_IN_EP_SHIFT;
  673. hw->num_dev_in_eps = (hwcfg4 & GHWCFG4_NUM_IN_EPS_MASK) >>
  674. GHWCFG4_NUM_IN_EPS_SHIFT;
  675. hw->dma_desc_enable = !!(hwcfg4 & GHWCFG4_DESC_DMA);
  676. hw->power_optimized = !!(hwcfg4 & GHWCFG4_POWER_OPTIMIZ);
  677. hw->hibernation = !!(hwcfg4 & GHWCFG4_HIBER);
  678. hw->utmi_phy_data_width = (hwcfg4 & GHWCFG4_UTMI_PHY_DATA_WIDTH_MASK) >>
  679. GHWCFG4_UTMI_PHY_DATA_WIDTH_SHIFT;
  680. hw->acg_enable = !!(hwcfg4 & GHWCFG4_ACG_SUPPORTED);
  681. hw->ipg_isoc_en = !!(hwcfg4 & GHWCFG4_IPG_ISOC_SUPPORTED);
  682. /* fifo sizes */
  683. hw->rx_fifo_size = (grxfsiz & GRXFSIZ_DEPTH_MASK) >>
  684. GRXFSIZ_DEPTH_SHIFT;
  685. /*
  686. * Host specific hardware parameters. Reading these parameters
  687. * requires the controller to be in host mode. The mode will
  688. * be forced, if necessary, to read these values.
  689. */
  690. dwc2_get_host_hwparams(hsotg);
  691. dwc2_get_dev_hwparams(hsotg);
  692. return 0;
  693. }
  694. int dwc2_init_params(struct dwc2_hsotg *hsotg)
  695. {
  696. const struct of_device_id *match;
  697. void (*set_params)(void *data);
  698. dwc2_set_default_params(hsotg);
  699. dwc2_get_device_properties(hsotg);
  700. match = of_match_device(dwc2_of_match_table, hsotg->dev);
  701. if (match && match->data) {
  702. set_params = match->data;
  703. set_params(hsotg);
  704. }
  705. dwc2_check_params(hsotg);
  706. return 0;
  707. }