i2caux.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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. cmd->payloads->length = request.payload.length;
  205. ++index_of_payload;
  206. }
  207. i2caux->funcs->release_engine(i2caux, &engine->base);
  208. return result;
  209. }
  210. static bool get_hw_supported_ddc_line(
  211. struct ddc *ddc,
  212. enum gpio_ddc_line *line)
  213. {
  214. enum gpio_ddc_line line_found;
  215. *line = GPIO_DDC_LINE_UNKNOWN;
  216. if (!ddc) {
  217. BREAK_TO_DEBUGGER();
  218. return false;
  219. }
  220. if (!ddc->hw_info.hw_supported)
  221. return false;
  222. line_found = dal_ddc_get_line(ddc);
  223. if (line_found >= GPIO_DDC_LINE_COUNT)
  224. return false;
  225. *line = line_found;
  226. return true;
  227. }
  228. void dal_i2caux_configure_aux(
  229. struct i2caux *i2caux,
  230. struct ddc *ddc,
  231. union aux_config cfg)
  232. {
  233. struct aux_engine *engine =
  234. i2caux->funcs->acquire_aux_engine(i2caux, ddc);
  235. if (!engine)
  236. return;
  237. engine->funcs->configure(engine, cfg);
  238. i2caux->funcs->release_engine(i2caux, &engine->base);
  239. }
  240. void dal_i2caux_destroy(
  241. struct i2caux **i2caux)
  242. {
  243. if (!i2caux || !*i2caux) {
  244. BREAK_TO_DEBUGGER();
  245. return;
  246. }
  247. (*i2caux)->funcs->destroy(i2caux);
  248. *i2caux = NULL;
  249. }
  250. /*
  251. * @brief
  252. * An utility function used by 'struct i2caux' and its descendants
  253. */
  254. uint32_t dal_i2caux_get_reference_clock(
  255. struct dc_bios *bios)
  256. {
  257. struct dc_firmware_info info = { { 0 } };
  258. if (bios->funcs->get_firmware_info(bios, &info) != BP_RESULT_OK)
  259. return 0;
  260. return info.pll_info.crystal_frequency;
  261. }
  262. /*
  263. * @brief
  264. * i2caux
  265. */
  266. enum {
  267. /* following are expressed in KHz */
  268. DEFAULT_I2C_SW_SPEED = 50,
  269. DEFAULT_I2C_HW_SPEED = 50,
  270. DEFAULT_I2C_SW_SPEED_100KHZ = 100,
  271. DEFAULT_I2C_HW_SPEED_100KHZ = 100,
  272. /* This is the timeout as defined in DP 1.2a,
  273. * 2.3.4 "Detailed uPacket TX AUX CH State Description". */
  274. AUX_TIMEOUT_PERIOD = 400,
  275. /* Ideally, the SW timeout should be just above 550usec
  276. * which is programmed in HW.
  277. * But the SW timeout of 600usec is not reliable,
  278. * because on some systems, delay_in_microseconds()
  279. * returns faster than it should.
  280. * EPR #379763: by trial-and-error on different systems,
  281. * 700usec is the minimum reliable SW timeout for polling
  282. * the AUX_SW_STATUS.AUX_SW_DONE bit.
  283. * This timeout expires *only* when there is
  284. * AUX Error or AUX Timeout conditions - not during normal operation.
  285. * During normal operation, AUX_SW_STATUS.AUX_SW_DONE bit is set
  286. * at most within ~240usec. That means,
  287. * increasing this timeout will not affect normal operation,
  288. * and we'll timeout after
  289. * SW_AUX_TIMEOUT_PERIOD_MULTIPLIER * AUX_TIMEOUT_PERIOD = 1600usec.
  290. * This timeout is especially important for
  291. * resume from S3 and CTS. */
  292. SW_AUX_TIMEOUT_PERIOD_MULTIPLIER = 4
  293. };
  294. struct i2c_engine *dal_i2caux_acquire_i2c_sw_engine(
  295. struct i2caux *i2caux,
  296. struct ddc *ddc)
  297. {
  298. enum gpio_ddc_line line;
  299. struct i2c_engine *engine = NULL;
  300. if (get_hw_supported_ddc_line(ddc, &line))
  301. engine = i2caux->i2c_sw_engines[line];
  302. if (!engine)
  303. engine = i2caux->i2c_generic_sw_engine;
  304. if (!engine)
  305. return NULL;
  306. if (!engine->base.funcs->acquire(&engine->base, ddc))
  307. return NULL;
  308. return engine;
  309. }
  310. struct aux_engine *dal_i2caux_acquire_aux_engine(
  311. struct i2caux *i2caux,
  312. struct ddc *ddc)
  313. {
  314. enum gpio_ddc_line line;
  315. struct aux_engine *engine;
  316. if (!get_hw_supported_ddc_line(ddc, &line))
  317. return NULL;
  318. engine = i2caux->aux_engines[line];
  319. if (!engine)
  320. return NULL;
  321. if (!engine->base.funcs->acquire(&engine->base, ddc))
  322. return NULL;
  323. return engine;
  324. }
  325. void dal_i2caux_release_engine(
  326. struct i2caux *i2caux,
  327. struct engine *engine)
  328. {
  329. engine->funcs->release_engine(engine);
  330. dal_ddc_close(engine->ddc);
  331. engine->ddc = NULL;
  332. }
  333. void dal_i2caux_construct(
  334. struct i2caux *i2caux,
  335. struct dc_context *ctx)
  336. {
  337. uint32_t i = 0;
  338. i2caux->ctx = ctx;
  339. do {
  340. i2caux->i2c_sw_engines[i] = NULL;
  341. i2caux->i2c_hw_engines[i] = NULL;
  342. i2caux->aux_engines[i] = NULL;
  343. ++i;
  344. } while (i < GPIO_DDC_LINE_COUNT);
  345. i2caux->i2c_generic_sw_engine = NULL;
  346. i2caux->i2c_generic_hw_engine = NULL;
  347. i2caux->aux_timeout_period =
  348. SW_AUX_TIMEOUT_PERIOD_MULTIPLIER * AUX_TIMEOUT_PERIOD;
  349. if (ctx->dce_version >= DCE_VERSION_11_2) {
  350. i2caux->default_i2c_hw_speed = DEFAULT_I2C_HW_SPEED_100KHZ;
  351. i2caux->default_i2c_sw_speed = DEFAULT_I2C_SW_SPEED_100KHZ;
  352. } else {
  353. i2caux->default_i2c_hw_speed = DEFAULT_I2C_HW_SPEED;
  354. i2caux->default_i2c_sw_speed = DEFAULT_I2C_SW_SPEED;
  355. }
  356. }
  357. void dal_i2caux_destruct(
  358. struct i2caux *i2caux)
  359. {
  360. uint32_t i = 0;
  361. if (i2caux->i2c_generic_hw_engine)
  362. i2caux->i2c_generic_hw_engine->funcs->destroy(
  363. &i2caux->i2c_generic_hw_engine);
  364. if (i2caux->i2c_generic_sw_engine)
  365. i2caux->i2c_generic_sw_engine->funcs->destroy(
  366. &i2caux->i2c_generic_sw_engine);
  367. do {
  368. if (i2caux->aux_engines[i])
  369. i2caux->aux_engines[i]->funcs->destroy(
  370. &i2caux->aux_engines[i]);
  371. if (i2caux->i2c_hw_engines[i])
  372. i2caux->i2c_hw_engines[i]->funcs->destroy(
  373. &i2caux->i2c_hw_engines[i]);
  374. if (i2caux->i2c_sw_engines[i])
  375. i2caux->i2c_sw_engines[i]->funcs->destroy(
  376. &i2caux->i2c_sw_engines[i]);
  377. ++i;
  378. } while (i < GPIO_DDC_LINE_COUNT);
  379. }