i2caux.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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. case DCE_VERSION_11_22:
  70. return dal_i2caux_dce112_create(ctx);
  71. case DCE_VERSION_11_0:
  72. return dal_i2caux_dce110_create(ctx);
  73. case DCE_VERSION_10_0:
  74. return dal_i2caux_dce100_create(ctx);
  75. case DCE_VERSION_12_0:
  76. return dal_i2caux_dce120_create(ctx);
  77. #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
  78. case DCN_VERSION_1_0:
  79. return dal_i2caux_dcn10_create(ctx);
  80. #endif
  81. default:
  82. BREAK_TO_DEBUGGER();
  83. return NULL;
  84. }
  85. }
  86. bool dal_i2caux_submit_i2c_command(
  87. struct i2caux *i2caux,
  88. struct ddc *ddc,
  89. struct i2c_command *cmd)
  90. {
  91. struct i2c_engine *engine;
  92. uint8_t index_of_payload = 0;
  93. bool result;
  94. if (!ddc) {
  95. BREAK_TO_DEBUGGER();
  96. return false;
  97. }
  98. if (!cmd) {
  99. BREAK_TO_DEBUGGER();
  100. return false;
  101. }
  102. /*
  103. * default will be SW, however there is a feature flag in adapter
  104. * service that determines whether SW i2c_engine will be available or
  105. * not, if sw i2c is not available we will fallback to hw. This feature
  106. * flag is set to not creating sw i2c engine for every dce except dce80
  107. * currently
  108. */
  109. switch (cmd->engine) {
  110. case I2C_COMMAND_ENGINE_DEFAULT:
  111. case I2C_COMMAND_ENGINE_SW:
  112. /* try to acquire SW engine first,
  113. * acquire HW engine if SW engine not available */
  114. engine = i2caux->funcs->acquire_i2c_sw_engine(i2caux, ddc);
  115. if (!engine)
  116. engine = i2caux->funcs->acquire_i2c_hw_engine(
  117. i2caux, ddc);
  118. break;
  119. case I2C_COMMAND_ENGINE_HW:
  120. default:
  121. /* try to acquire HW engine first,
  122. * acquire SW engine if HW engine not available */
  123. engine = i2caux->funcs->acquire_i2c_hw_engine(i2caux, ddc);
  124. if (!engine)
  125. engine = i2caux->funcs->acquire_i2c_sw_engine(
  126. i2caux, ddc);
  127. }
  128. if (!engine)
  129. return false;
  130. engine->funcs->set_speed(engine, cmd->speed);
  131. result = true;
  132. while (index_of_payload < cmd->number_of_payloads) {
  133. bool mot = (index_of_payload != cmd->number_of_payloads - 1);
  134. struct i2c_payload *payload = cmd->payloads + index_of_payload;
  135. struct i2caux_transaction_request request = { 0 };
  136. request.operation = payload->write ?
  137. I2CAUX_TRANSACTION_WRITE :
  138. I2CAUX_TRANSACTION_READ;
  139. request.payload.address_space =
  140. I2CAUX_TRANSACTION_ADDRESS_SPACE_I2C;
  141. request.payload.address = (payload->address << 1) |
  142. !payload->write;
  143. request.payload.length = payload->length;
  144. request.payload.data = payload->data;
  145. if (!engine->base.funcs->submit_request(
  146. &engine->base, &request, mot)) {
  147. result = false;
  148. break;
  149. }
  150. ++index_of_payload;
  151. }
  152. i2caux->funcs->release_engine(i2caux, &engine->base);
  153. return result;
  154. }
  155. bool dal_i2caux_submit_aux_command(
  156. struct i2caux *i2caux,
  157. struct ddc *ddc,
  158. struct aux_command *cmd)
  159. {
  160. struct aux_engine *engine;
  161. uint8_t index_of_payload = 0;
  162. bool result;
  163. bool mot;
  164. if (!ddc) {
  165. BREAK_TO_DEBUGGER();
  166. return false;
  167. }
  168. if (!cmd) {
  169. BREAK_TO_DEBUGGER();
  170. return false;
  171. }
  172. engine = i2caux->funcs->acquire_aux_engine(i2caux, ddc);
  173. if (!engine)
  174. return false;
  175. engine->delay = cmd->defer_delay;
  176. engine->max_defer_write_retry = cmd->max_defer_write_retry;
  177. result = true;
  178. while (index_of_payload < cmd->number_of_payloads) {
  179. struct aux_payload *payload = cmd->payloads + index_of_payload;
  180. struct i2caux_transaction_request request = { 0 };
  181. if (cmd->mot == I2C_MOT_UNDEF)
  182. mot = (index_of_payload != cmd->number_of_payloads - 1);
  183. else
  184. mot = (cmd->mot == I2C_MOT_TRUE);
  185. request.operation = payload->write ?
  186. I2CAUX_TRANSACTION_WRITE :
  187. I2CAUX_TRANSACTION_READ;
  188. if (payload->i2c_over_aux) {
  189. request.payload.address_space =
  190. I2CAUX_TRANSACTION_ADDRESS_SPACE_I2C;
  191. request.payload.address = (payload->address << 1) |
  192. !payload->write;
  193. } else {
  194. request.payload.address_space =
  195. I2CAUX_TRANSACTION_ADDRESS_SPACE_DPCD;
  196. request.payload.address = payload->address;
  197. }
  198. request.payload.length = payload->length;
  199. request.payload.data = payload->data;
  200. if (!engine->base.funcs->submit_request(
  201. &engine->base, &request, mot)) {
  202. result = false;
  203. break;
  204. }
  205. cmd->payloads->length = request.payload.length;
  206. ++index_of_payload;
  207. }
  208. i2caux->funcs->release_engine(i2caux, &engine->base);
  209. return result;
  210. }
  211. static bool get_hw_supported_ddc_line(
  212. struct ddc *ddc,
  213. enum gpio_ddc_line *line)
  214. {
  215. enum gpio_ddc_line line_found;
  216. *line = GPIO_DDC_LINE_UNKNOWN;
  217. if (!ddc) {
  218. BREAK_TO_DEBUGGER();
  219. return false;
  220. }
  221. if (!ddc->hw_info.hw_supported)
  222. return false;
  223. line_found = dal_ddc_get_line(ddc);
  224. if (line_found >= GPIO_DDC_LINE_COUNT)
  225. return false;
  226. *line = line_found;
  227. return true;
  228. }
  229. void dal_i2caux_configure_aux(
  230. struct i2caux *i2caux,
  231. struct ddc *ddc,
  232. union aux_config cfg)
  233. {
  234. struct aux_engine *engine =
  235. i2caux->funcs->acquire_aux_engine(i2caux, ddc);
  236. if (!engine)
  237. return;
  238. engine->funcs->configure(engine, cfg);
  239. i2caux->funcs->release_engine(i2caux, &engine->base);
  240. }
  241. void dal_i2caux_destroy(
  242. struct i2caux **i2caux)
  243. {
  244. if (!i2caux || !*i2caux) {
  245. BREAK_TO_DEBUGGER();
  246. return;
  247. }
  248. (*i2caux)->funcs->destroy(i2caux);
  249. *i2caux = NULL;
  250. }
  251. /*
  252. * @brief
  253. * An utility function used by 'struct i2caux' and its descendants
  254. */
  255. uint32_t dal_i2caux_get_reference_clock(
  256. struct dc_bios *bios)
  257. {
  258. struct dc_firmware_info info = { { 0 } };
  259. if (bios->funcs->get_firmware_info(bios, &info) != BP_RESULT_OK)
  260. return 0;
  261. return info.pll_info.crystal_frequency;
  262. }
  263. /*
  264. * @brief
  265. * i2caux
  266. */
  267. enum {
  268. /* following are expressed in KHz */
  269. DEFAULT_I2C_SW_SPEED = 50,
  270. DEFAULT_I2C_HW_SPEED = 50,
  271. DEFAULT_I2C_SW_SPEED_100KHZ = 100,
  272. DEFAULT_I2C_HW_SPEED_100KHZ = 100,
  273. /* This is the timeout as defined in DP 1.2a,
  274. * 2.3.4 "Detailed uPacket TX AUX CH State Description". */
  275. AUX_TIMEOUT_PERIOD = 400,
  276. /* Ideally, the SW timeout should be just above 550usec
  277. * which is programmed in HW.
  278. * But the SW timeout of 600usec is not reliable,
  279. * because on some systems, delay_in_microseconds()
  280. * returns faster than it should.
  281. * EPR #379763: by trial-and-error on different systems,
  282. * 700usec is the minimum reliable SW timeout for polling
  283. * the AUX_SW_STATUS.AUX_SW_DONE bit.
  284. * This timeout expires *only* when there is
  285. * AUX Error or AUX Timeout conditions - not during normal operation.
  286. * During normal operation, AUX_SW_STATUS.AUX_SW_DONE bit is set
  287. * at most within ~240usec. That means,
  288. * increasing this timeout will not affect normal operation,
  289. * and we'll timeout after
  290. * SW_AUX_TIMEOUT_PERIOD_MULTIPLIER * AUX_TIMEOUT_PERIOD = 1600usec.
  291. * This timeout is especially important for
  292. * resume from S3 and CTS. */
  293. SW_AUX_TIMEOUT_PERIOD_MULTIPLIER = 4
  294. };
  295. struct i2c_engine *dal_i2caux_acquire_i2c_sw_engine(
  296. struct i2caux *i2caux,
  297. struct ddc *ddc)
  298. {
  299. enum gpio_ddc_line line;
  300. struct i2c_engine *engine = NULL;
  301. if (get_hw_supported_ddc_line(ddc, &line))
  302. engine = i2caux->i2c_sw_engines[line];
  303. if (!engine)
  304. engine = i2caux->i2c_generic_sw_engine;
  305. if (!engine)
  306. return NULL;
  307. if (!engine->base.funcs->acquire(&engine->base, ddc))
  308. return NULL;
  309. return engine;
  310. }
  311. struct aux_engine *dal_i2caux_acquire_aux_engine(
  312. struct i2caux *i2caux,
  313. struct ddc *ddc)
  314. {
  315. enum gpio_ddc_line line;
  316. struct aux_engine *engine;
  317. if (!get_hw_supported_ddc_line(ddc, &line))
  318. return NULL;
  319. engine = i2caux->aux_engines[line];
  320. if (!engine)
  321. return NULL;
  322. if (!engine->base.funcs->acquire(&engine->base, ddc))
  323. return NULL;
  324. return engine;
  325. }
  326. void dal_i2caux_release_engine(
  327. struct i2caux *i2caux,
  328. struct engine *engine)
  329. {
  330. engine->funcs->release_engine(engine);
  331. dal_ddc_close(engine->ddc);
  332. engine->ddc = NULL;
  333. }
  334. void dal_i2caux_construct(
  335. struct i2caux *i2caux,
  336. struct dc_context *ctx)
  337. {
  338. uint32_t i = 0;
  339. i2caux->ctx = ctx;
  340. do {
  341. i2caux->i2c_sw_engines[i] = NULL;
  342. i2caux->i2c_hw_engines[i] = NULL;
  343. i2caux->aux_engines[i] = NULL;
  344. ++i;
  345. } while (i < GPIO_DDC_LINE_COUNT);
  346. i2caux->i2c_generic_sw_engine = NULL;
  347. i2caux->i2c_generic_hw_engine = NULL;
  348. i2caux->aux_timeout_period =
  349. SW_AUX_TIMEOUT_PERIOD_MULTIPLIER * AUX_TIMEOUT_PERIOD;
  350. if (ctx->dce_version >= DCE_VERSION_11_2) {
  351. i2caux->default_i2c_hw_speed = DEFAULT_I2C_HW_SPEED_100KHZ;
  352. i2caux->default_i2c_sw_speed = DEFAULT_I2C_SW_SPEED_100KHZ;
  353. } else {
  354. i2caux->default_i2c_hw_speed = DEFAULT_I2C_HW_SPEED;
  355. i2caux->default_i2c_sw_speed = DEFAULT_I2C_SW_SPEED;
  356. }
  357. }
  358. void dal_i2caux_destruct(
  359. struct i2caux *i2caux)
  360. {
  361. uint32_t i = 0;
  362. if (i2caux->i2c_generic_hw_engine)
  363. i2caux->i2c_generic_hw_engine->funcs->destroy(
  364. &i2caux->i2c_generic_hw_engine);
  365. if (i2caux->i2c_generic_sw_engine)
  366. i2caux->i2c_generic_sw_engine->funcs->destroy(
  367. &i2caux->i2c_generic_sw_engine);
  368. do {
  369. if (i2caux->aux_engines[i])
  370. i2caux->aux_engines[i]->funcs->destroy(
  371. &i2caux->aux_engines[i]);
  372. if (i2caux->i2c_hw_engines[i])
  373. i2caux->i2c_hw_engines[i]->funcs->destroy(
  374. &i2caux->i2c_hw_engines[i]);
  375. if (i2caux->i2c_sw_engines[i])
  376. i2caux->i2c_sw_engines[i]->funcs->destroy(
  377. &i2caux->i2c_sw_engines[i]);
  378. ++i;
  379. } while (i < GPIO_DDC_LINE_COUNT);
  380. }