params.c 24 KB

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