params.c 21 KB

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