params.c 22 KB

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