i2caux.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /*
  2. * Copyright 2012-15 Advanced Micro Devices, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: AMD
  23. *
  24. */
  25. #include "dm_services.h"
  26. /*
  27. * Pre-requisites: headers required by header of this unit
  28. */
  29. #include "include/i2caux_interface.h"
  30. #include "dc_bios_types.h"
  31. /*
  32. * Header of this unit
  33. */
  34. #include "i2caux.h"
  35. /*
  36. * Post-requisites: headers required by this unit
  37. */
  38. #include "engine.h"
  39. #include "i2c_engine.h"
  40. #include "aux_engine.h"
  41. /*
  42. * This unit
  43. */
  44. #include "dce80/i2caux_dce80.h"
  45. #include "dce100/i2caux_dce100.h"
  46. #include "dce110/i2caux_dce110.h"
  47. #include "dce112/i2caux_dce112.h"
  48. #include "dce120/i2caux_dce120.h"
  49. #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
  50. #include "dcn10/i2caux_dcn10.h"
  51. #endif
  52. #include "diagnostics/i2caux_diag.h"
  53. /*
  54. * @brief
  55. * Plain API, available publicly
  56. */
  57. struct i2caux *dal_i2caux_create(
  58. struct dc_context *ctx)
  59. {
  60. if (IS_FPGA_MAXIMUS_DC(ctx->dce_environment)) {
  61. return dal_i2caux_diag_fpga_create(ctx);
  62. }
  63. switch (ctx->dce_version) {
  64. case DCE_VERSION_8_0:
  65. case DCE_VERSION_8_1:
  66. case DCE_VERSION_8_3:
  67. return dal_i2caux_dce80_create(ctx);
  68. case DCE_VERSION_11_2:
  69. return dal_i2caux_dce112_create(ctx);
  70. case DCE_VERSION_11_0:
  71. return dal_i2caux_dce110_create(ctx);
  72. case DCE_VERSION_10_0:
  73. return dal_i2caux_dce100_create(ctx);
  74. case DCE_VERSION_12_0:
  75. return dal_i2caux_dce120_create(ctx);
  76. #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
  77. case DCN_VERSION_1_0:
  78. return dal_i2caux_dcn10_create(ctx);
  79. #endif
  80. default:
  81. BREAK_TO_DEBUGGER();
  82. return NULL;
  83. }
  84. }
  85. bool dal_i2caux_submit_i2c_command(
  86. struct i2caux *i2caux,
  87. struct ddc *ddc,
  88. struct i2c_command *cmd)
  89. {
  90. struct i2c_engine *engine;
  91. uint8_t index_of_payload = 0;
  92. bool result;
  93. if (!ddc) {
  94. BREAK_TO_DEBUGGER();
  95. return false;
  96. }
  97. if (!cmd) {
  98. BREAK_TO_DEBUGGER();
  99. return false;
  100. }
  101. /*
  102. * default will be SW, however there is a feature flag in adapter
  103. * service that determines whether SW i2c_engine will be available or
  104. * not, if sw i2c is not available we will fallback to hw. This feature
  105. * flag is set to not creating sw i2c engine for every dce except dce80
  106. * currently
  107. */
  108. switch (cmd->engine) {
  109. case I2C_COMMAND_ENGINE_DEFAULT:
  110. case I2C_COMMAND_ENGINE_SW:
  111. /* try to acquire SW engine first,
  112. * acquire HW engine if SW engine not available */
  113. engine = i2caux->funcs->acquire_i2c_sw_engine(i2caux, ddc);
  114. if (!engine)
  115. engine = i2caux->funcs->acquire_i2c_hw_engine(
  116. i2caux, ddc);
  117. break;
  118. case I2C_COMMAND_ENGINE_HW:
  119. default:
  120. /* try to acquire HW engine first,
  121. * acquire SW engine if HW engine not available */
  122. engine = i2caux->funcs->acquire_i2c_hw_engine(i2caux, ddc);
  123. if (!engine)
  124. engine = i2caux->funcs->acquire_i2c_sw_engine(
  125. i2caux, ddc);
  126. }
  127. if (!engine)
  128. return false;
  129. engine->funcs->set_speed(engine, cmd->speed);
  130. result = true;
  131. while (index_of_payload < cmd->number_of_payloads) {
  132. bool mot = (index_of_payload != cmd->number_of_payloads - 1);
  133. struct i2c_payload *payload = cmd->payloads + index_of_payload;
  134. struct i2caux_transaction_request request = { 0 };
  135. request.operation = payload->write ?
  136. I2CAUX_TRANSACTION_WRITE :
  137. I2CAUX_TRANSACTION_READ;
  138. request.payload.address_space =
  139. I2CAUX_TRANSACTION_ADDRESS_SPACE_I2C;
  140. request.payload.address = (payload->address << 1) |
  141. !payload->write;
  142. request.payload.length = payload->length;
  143. request.payload.data = payload->data;
  144. if (!engine->base.funcs->submit_request(
  145. &engine->base, &request, mot)) {
  146. result = false;
  147. break;
  148. }
  149. ++index_of_payload;
  150. }
  151. i2caux->funcs->release_engine(i2caux, &engine->base);
  152. return result;
  153. }
  154. bool dal_i2caux_submit_aux_command(
  155. struct i2caux *i2caux,
  156. struct ddc *ddc,
  157. struct aux_command *cmd)
  158. {
  159. struct aux_engine *engine;
  160. uint8_t index_of_payload = 0;
  161. bool result;
  162. bool mot;
  163. if (!ddc) {
  164. BREAK_TO_DEBUGGER();
  165. return false;
  166. }
  167. if (!cmd) {
  168. BREAK_TO_DEBUGGER();
  169. return false;
  170. }
  171. engine = i2caux->funcs->acquire_aux_engine(i2caux, ddc);
  172. if (!engine)
  173. return false;
  174. engine->delay = cmd->defer_delay;
  175. engine->max_defer_write_retry = cmd->max_defer_write_retry;
  176. result = true;
  177. while (index_of_payload < cmd->number_of_payloads) {
  178. struct aux_payload *payload = cmd->payloads + index_of_payload;
  179. struct i2caux_transaction_request request = { 0 };
  180. if (cmd->mot == I2C_MOT_UNDEF)
  181. mot = (index_of_payload != cmd->number_of_payloads - 1);
  182. else
  183. mot = (cmd->mot == I2C_MOT_TRUE);
  184. request.operation = payload->write ?
  185. I2CAUX_TRANSACTION_WRITE :
  186. I2CAUX_TRANSACTION_READ;
  187. if (payload->i2c_over_aux) {
  188. request.payload.address_space =
  189. I2CAUX_TRANSACTION_ADDRESS_SPACE_I2C;
  190. request.payload.address = (payload->address << 1) |
  191. !payload->write;
  192. } else {
  193. request.payload.address_space =
  194. I2CAUX_TRANSACTION_ADDRESS_SPACE_DPCD;
  195. request.payload.address = payload->address;
  196. }
  197. request.payload.length = payload->length;
  198. request.payload.data = payload->data;
  199. if (!engine->base.funcs->submit_request(
  200. &engine->base, &request, mot)) {
  201. result = false;
  202. break;
  203. }
  204. ++index_of_payload;
  205. }
  206. i2caux->funcs->release_engine(i2caux, &engine->base);
  207. return result;
  208. }
  209. static bool get_hw_supported_ddc_line(
  210. struct ddc *ddc,
  211. enum gpio_ddc_line *line)
  212. {
  213. enum gpio_ddc_line line_found;
  214. *line = GPIO_DDC_LINE_UNKNOWN;
  215. if (!ddc) {
  216. BREAK_TO_DEBUGGER();
  217. return false;
  218. }
  219. if (!ddc->hw_info.hw_supported)
  220. return false;
  221. line_found = dal_ddc_get_line(ddc);
  222. if (line_found >= GPIO_DDC_LINE_COUNT)
  223. return false;
  224. *line = line_found;
  225. return true;
  226. }
  227. void dal_i2caux_configure_aux(
  228. struct i2caux *i2caux,
  229. struct ddc *ddc,
  230. union aux_config cfg)
  231. {
  232. struct aux_engine *engine =
  233. i2caux->funcs->acquire_aux_engine(i2caux, ddc);
  234. if (!engine)
  235. return;
  236. engine->funcs->configure(engine, cfg);
  237. i2caux->funcs->release_engine(i2caux, &engine->base);
  238. }
  239. void dal_i2caux_destroy(
  240. struct i2caux **i2caux)
  241. {
  242. if (!i2caux || !*i2caux) {
  243. BREAK_TO_DEBUGGER();
  244. return;
  245. }
  246. (*i2caux)->funcs->destroy(i2caux);
  247. *i2caux = NULL;
  248. }
  249. /*
  250. * @brief
  251. * An utility function used by 'struct i2caux' and its descendants
  252. */
  253. uint32_t dal_i2caux_get_reference_clock(
  254. struct dc_bios *bios)
  255. {
  256. struct dc_firmware_info info = { { 0 } };
  257. if (bios->funcs->get_firmware_info(bios, &info) != BP_RESULT_OK)
  258. return 0;
  259. return info.pll_info.crystal_frequency;
  260. }
  261. /*
  262. * @brief
  263. * i2caux
  264. */
  265. enum {
  266. /* following are expressed in KHz */
  267. DEFAULT_I2C_SW_SPEED = 50,
  268. DEFAULT_I2C_HW_SPEED = 50,
  269. DEFAULT_I2C_SW_SPEED_100KHZ = 100,
  270. DEFAULT_I2C_HW_SPEED_100KHZ = 100,
  271. /* This is the timeout as defined in DP 1.2a,
  272. * 2.3.4 "Detailed uPacket TX AUX CH State Description". */
  273. AUX_TIMEOUT_PERIOD = 400,
  274. /* Ideally, the SW timeout should be just above 550usec
  275. * which is programmed in HW.
  276. * But the SW timeout of 600usec is not reliable,
  277. * because on some systems, delay_in_microseconds()
  278. * returns faster than it should.
  279. * EPR #379763: by trial-and-error on different systems,
  280. * 700usec is the minimum reliable SW timeout for polling
  281. * the AUX_SW_STATUS.AUX_SW_DONE bit.
  282. * This timeout expires *only* when there is
  283. * AUX Error or AUX Timeout conditions - not during normal operation.
  284. * During normal operation, AUX_SW_STATUS.AUX_SW_DONE bit is set
  285. * at most within ~240usec. That means,
  286. * increasing this timeout will not affect normal operation,
  287. * and we'll timeout after
  288. * SW_AUX_TIMEOUT_PERIOD_MULTIPLIER * AUX_TIMEOUT_PERIOD = 1600usec.
  289. * This timeout is especially important for
  290. * resume from S3 and CTS. */
  291. SW_AUX_TIMEOUT_PERIOD_MULTIPLIER = 4
  292. };
  293. struct i2c_engine *dal_i2caux_acquire_i2c_sw_engine(
  294. struct i2caux *i2caux,
  295. struct ddc *ddc)
  296. {
  297. enum gpio_ddc_line line;
  298. struct i2c_engine *engine = NULL;
  299. if (get_hw_supported_ddc_line(ddc, &line))
  300. engine = i2caux->i2c_sw_engines[line];
  301. if (!engine)
  302. engine = i2caux->i2c_generic_sw_engine;
  303. if (!engine)
  304. return NULL;
  305. if (!engine->base.funcs->acquire(&engine->base, ddc))
  306. return NULL;
  307. return engine;
  308. }
  309. struct aux_engine *dal_i2caux_acquire_aux_engine(
  310. struct i2caux *i2caux,
  311. struct ddc *ddc)
  312. {
  313. enum gpio_ddc_line line;
  314. struct aux_engine *engine;
  315. if (!get_hw_supported_ddc_line(ddc, &line))
  316. return NULL;
  317. engine = i2caux->aux_engines[line];
  318. if (!engine)
  319. return NULL;
  320. if (!engine->base.funcs->acquire(&engine->base, ddc))
  321. return NULL;
  322. return engine;
  323. }
  324. void dal_i2caux_release_engine(
  325. struct i2caux *i2caux,
  326. struct engine *engine)
  327. {
  328. engine->funcs->release_engine(engine);
  329. dal_ddc_close(engine->ddc);
  330. engine->ddc = NULL;
  331. }
  332. void dal_i2caux_construct(
  333. struct i2caux *i2caux,
  334. struct dc_context *ctx)
  335. {
  336. uint32_t i = 0;
  337. i2caux->ctx = ctx;
  338. do {
  339. i2caux->i2c_sw_engines[i] = NULL;
  340. i2caux->i2c_hw_engines[i] = NULL;
  341. i2caux->aux_engines[i] = NULL;
  342. ++i;
  343. } while (i < GPIO_DDC_LINE_COUNT);
  344. i2caux->i2c_generic_sw_engine = NULL;
  345. i2caux->i2c_generic_hw_engine = NULL;
  346. i2caux->aux_timeout_period =
  347. SW_AUX_TIMEOUT_PERIOD_MULTIPLIER * AUX_TIMEOUT_PERIOD;
  348. if (ctx->dce_version >= DCE_VERSION_11_2) {
  349. i2caux->default_i2c_hw_speed = DEFAULT_I2C_HW_SPEED_100KHZ;
  350. i2caux->default_i2c_sw_speed = DEFAULT_I2C_SW_SPEED_100KHZ;
  351. } else {
  352. i2caux->default_i2c_hw_speed = DEFAULT_I2C_HW_SPEED;
  353. i2caux->default_i2c_sw_speed = DEFAULT_I2C_SW_SPEED;
  354. }
  355. }
  356. void dal_i2caux_destruct(
  357. struct i2caux *i2caux)
  358. {
  359. uint32_t i = 0;
  360. if (i2caux->i2c_generic_hw_engine)
  361. i2caux->i2c_generic_hw_engine->funcs->destroy(
  362. &i2caux->i2c_generic_hw_engine);
  363. if (i2caux->i2c_generic_sw_engine)
  364. i2caux->i2c_generic_sw_engine->funcs->destroy(
  365. &i2caux->i2c_generic_sw_engine);
  366. do {
  367. if (i2caux->aux_engines[i])
  368. i2caux->aux_engines[i]->funcs->destroy(
  369. &i2caux->aux_engines[i]);
  370. if (i2caux->i2c_hw_engines[i])
  371. i2caux->i2c_hw_engines[i]->funcs->destroy(
  372. &i2caux->i2c_hw_engines[i]);
  373. if (i2caux->i2c_sw_engines[i])
  374. i2caux->i2c_sw_engines[i]->funcs->destroy(
  375. &i2caux->i2c_sw_engines[i]);
  376. ++i;
  377. } while (i < GPIO_DDC_LINE_COUNT);
  378. }