dce_aux.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  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. #include "dce_aux.h"
  27. #include "dce/dce_11_0_sh_mask.h"
  28. #define CTX \
  29. aux110->base.ctx
  30. #define REG(reg_name)\
  31. (aux110->regs->reg_name)
  32. #define DC_LOGGER \
  33. engine->ctx->logger
  34. #include "reg_helper.h"
  35. #define FROM_AUX_ENGINE(ptr) \
  36. container_of((ptr), struct aux_engine_dce110, base)
  37. #define FROM_ENGINE(ptr) \
  38. FROM_AUX_ENGINE(container_of((ptr), struct aux_engine, base))
  39. #define FROM_AUX_ENGINE_ENGINE(ptr) \
  40. container_of((ptr), struct aux_engine, base)
  41. enum {
  42. AUX_INVALID_REPLY_RETRY_COUNTER = 1,
  43. AUX_TIMED_OUT_RETRY_COUNTER = 2,
  44. AUX_DEFER_RETRY_COUNTER = 6
  45. };
  46. static void release_engine(
  47. struct aux_engine *engine)
  48. {
  49. struct aux_engine_dce110 *aux110 = FROM_AUX_ENGINE(engine);
  50. dal_ddc_close(engine->ddc);
  51. engine->ddc = NULL;
  52. REG_UPDATE(AUX_ARB_CONTROL, AUX_SW_DONE_USING_AUX_REG, 1);
  53. }
  54. #define SW_CAN_ACCESS_AUX 1
  55. #define DMCU_CAN_ACCESS_AUX 2
  56. static bool is_engine_available(
  57. struct aux_engine *engine)
  58. {
  59. struct aux_engine_dce110 *aux110 = FROM_AUX_ENGINE(engine);
  60. uint32_t value = REG_READ(AUX_ARB_CONTROL);
  61. uint32_t field = get_reg_field_value(
  62. value,
  63. AUX_ARB_CONTROL,
  64. AUX_REG_RW_CNTL_STATUS);
  65. return (field != DMCU_CAN_ACCESS_AUX);
  66. }
  67. static bool acquire_engine(
  68. struct aux_engine *engine)
  69. {
  70. struct aux_engine_dce110 *aux110 = FROM_AUX_ENGINE(engine);
  71. uint32_t value = REG_READ(AUX_ARB_CONTROL);
  72. uint32_t field = get_reg_field_value(
  73. value,
  74. AUX_ARB_CONTROL,
  75. AUX_REG_RW_CNTL_STATUS);
  76. if (field == DMCU_CAN_ACCESS_AUX)
  77. return false;
  78. /* enable AUX before request SW to access AUX */
  79. value = REG_READ(AUX_CONTROL);
  80. field = get_reg_field_value(value,
  81. AUX_CONTROL,
  82. AUX_EN);
  83. if (field == 0) {
  84. set_reg_field_value(
  85. value,
  86. 1,
  87. AUX_CONTROL,
  88. AUX_EN);
  89. if (REG(AUX_RESET_MASK)) {
  90. /*DP_AUX block as part of the enable sequence*/
  91. set_reg_field_value(
  92. value,
  93. 1,
  94. AUX_CONTROL,
  95. AUX_RESET);
  96. }
  97. REG_WRITE(AUX_CONTROL, value);
  98. if (REG(AUX_RESET_MASK)) {
  99. /*poll HW to make sure reset it done*/
  100. REG_WAIT(AUX_CONTROL, AUX_RESET_DONE, 1,
  101. 1, 11);
  102. set_reg_field_value(
  103. value,
  104. 0,
  105. AUX_CONTROL,
  106. AUX_RESET);
  107. REG_WRITE(AUX_CONTROL, value);
  108. REG_WAIT(AUX_CONTROL, AUX_RESET_DONE, 0,
  109. 1, 11);
  110. }
  111. } /*if (field)*/
  112. /* request SW to access AUX */
  113. REG_UPDATE(AUX_ARB_CONTROL, AUX_SW_USE_AUX_REG_REQ, 1);
  114. value = REG_READ(AUX_ARB_CONTROL);
  115. field = get_reg_field_value(
  116. value,
  117. AUX_ARB_CONTROL,
  118. AUX_REG_RW_CNTL_STATUS);
  119. return (field == SW_CAN_ACCESS_AUX);
  120. }
  121. #define COMPOSE_AUX_SW_DATA_16_20(command, address) \
  122. ((command) | ((0xF0000 & (address)) >> 16))
  123. #define COMPOSE_AUX_SW_DATA_8_15(address) \
  124. ((0xFF00 & (address)) >> 8)
  125. #define COMPOSE_AUX_SW_DATA_0_7(address) \
  126. (0xFF & (address))
  127. static void submit_channel_request(
  128. struct aux_engine *engine,
  129. struct aux_request_transaction_data *request)
  130. {
  131. struct aux_engine_dce110 *aux110 = FROM_AUX_ENGINE(engine);
  132. uint32_t value;
  133. uint32_t length;
  134. bool is_write =
  135. ((request->type == AUX_TRANSACTION_TYPE_DP) &&
  136. (request->action == I2CAUX_TRANSACTION_ACTION_DP_WRITE)) ||
  137. ((request->type == AUX_TRANSACTION_TYPE_I2C) &&
  138. ((request->action == I2CAUX_TRANSACTION_ACTION_I2C_WRITE) ||
  139. (request->action == I2CAUX_TRANSACTION_ACTION_I2C_WRITE_MOT)));
  140. if (REG(AUXN_IMPCAL)) {
  141. /* clear_aux_error */
  142. REG_UPDATE_SEQ(AUXN_IMPCAL, AUXN_CALOUT_ERROR_AK,
  143. 1,
  144. 0);
  145. REG_UPDATE_SEQ(AUXP_IMPCAL, AUXP_CALOUT_ERROR_AK,
  146. 1,
  147. 0);
  148. /* force_default_calibrate */
  149. REG_UPDATE_1BY1_2(AUXN_IMPCAL,
  150. AUXN_IMPCAL_ENABLE, 1,
  151. AUXN_IMPCAL_OVERRIDE_ENABLE, 0);
  152. /* bug? why AUXN update EN and OVERRIDE_EN 1 by 1 while AUX P toggles OVERRIDE? */
  153. REG_UPDATE_SEQ(AUXP_IMPCAL, AUXP_IMPCAL_OVERRIDE_ENABLE,
  154. 1,
  155. 0);
  156. }
  157. /* set the delay and the number of bytes to write */
  158. /* The length include
  159. * the 4 bit header and the 20 bit address
  160. * (that is 3 byte).
  161. * If the requested length is non zero this means
  162. * an addition byte specifying the length is required.
  163. */
  164. length = request->length ? 4 : 3;
  165. if (is_write)
  166. length += request->length;
  167. REG_UPDATE_2(AUX_SW_CONTROL,
  168. AUX_SW_START_DELAY, request->delay,
  169. AUX_SW_WR_BYTES, length);
  170. /* program action and address and payload data (if 'is_write') */
  171. value = REG_UPDATE_4(AUX_SW_DATA,
  172. AUX_SW_INDEX, 0,
  173. AUX_SW_DATA_RW, 0,
  174. AUX_SW_AUTOINCREMENT_DISABLE, 1,
  175. AUX_SW_DATA, COMPOSE_AUX_SW_DATA_16_20(request->action, request->address));
  176. value = REG_SET_2(AUX_SW_DATA, value,
  177. AUX_SW_AUTOINCREMENT_DISABLE, 0,
  178. AUX_SW_DATA, COMPOSE_AUX_SW_DATA_8_15(request->address));
  179. value = REG_SET(AUX_SW_DATA, value,
  180. AUX_SW_DATA, COMPOSE_AUX_SW_DATA_0_7(request->address));
  181. if (request->length) {
  182. value = REG_SET(AUX_SW_DATA, value,
  183. AUX_SW_DATA, request->length - 1);
  184. }
  185. if (is_write) {
  186. /* Load the HW buffer with the Data to be sent.
  187. * This is relevant for write operation.
  188. * For read, the data recived data will be
  189. * processed in process_channel_reply().
  190. */
  191. uint32_t i = 0;
  192. while (i < request->length) {
  193. value = REG_SET(AUX_SW_DATA, value,
  194. AUX_SW_DATA, request->data[i]);
  195. ++i;
  196. }
  197. }
  198. REG_UPDATE(AUX_INTERRUPT_CONTROL, AUX_SW_DONE_ACK, 1);
  199. REG_WAIT(AUX_SW_STATUS, AUX_SW_DONE, 0,
  200. 10, aux110->timeout_period/10);
  201. REG_UPDATE(AUX_SW_CONTROL, AUX_SW_GO, 1);
  202. }
  203. static int read_channel_reply(struct aux_engine *engine, uint32_t size,
  204. uint8_t *buffer, uint8_t *reply_result,
  205. uint32_t *sw_status)
  206. {
  207. struct aux_engine_dce110 *aux110 = FROM_AUX_ENGINE(engine);
  208. uint32_t bytes_replied;
  209. uint32_t reply_result_32;
  210. *sw_status = REG_GET(AUX_SW_STATUS, AUX_SW_REPLY_BYTE_COUNT,
  211. &bytes_replied);
  212. /* In case HPD is LOW, exit AUX transaction */
  213. if ((*sw_status & AUX_SW_STATUS__AUX_SW_HPD_DISCON_MASK))
  214. return -1;
  215. /* Need at least the status byte */
  216. if (!bytes_replied)
  217. return -1;
  218. REG_UPDATE_1BY1_3(AUX_SW_DATA,
  219. AUX_SW_INDEX, 0,
  220. AUX_SW_AUTOINCREMENT_DISABLE, 1,
  221. AUX_SW_DATA_RW, 1);
  222. REG_GET(AUX_SW_DATA, AUX_SW_DATA, &reply_result_32);
  223. reply_result_32 = reply_result_32 >> 4;
  224. *reply_result = (uint8_t)reply_result_32;
  225. if (reply_result_32 == 0) { /* ACK */
  226. uint32_t i = 0;
  227. /* First byte was already used to get the command status */
  228. --bytes_replied;
  229. /* Do not overflow buffer */
  230. if (bytes_replied > size)
  231. return -1;
  232. while (i < bytes_replied) {
  233. uint32_t aux_sw_data_val;
  234. REG_GET(AUX_SW_DATA, AUX_SW_DATA, &aux_sw_data_val);
  235. buffer[i] = aux_sw_data_val;
  236. ++i;
  237. }
  238. return i;
  239. }
  240. return 0;
  241. }
  242. static void process_channel_reply(
  243. struct aux_engine *engine,
  244. struct aux_reply_transaction_data *reply)
  245. {
  246. int bytes_replied;
  247. uint8_t reply_result;
  248. uint32_t sw_status;
  249. bytes_replied = read_channel_reply(engine, reply->length, reply->data,
  250. &reply_result, &sw_status);
  251. /* in case HPD is LOW, exit AUX transaction */
  252. if ((sw_status & AUX_SW_STATUS__AUX_SW_HPD_DISCON_MASK)) {
  253. reply->status = AUX_TRANSACTION_REPLY_HPD_DISCON;
  254. return;
  255. }
  256. if (bytes_replied < 0) {
  257. /* Need to handle an error case...
  258. * Hopefully, upper layer function won't call this function if
  259. * the number of bytes in the reply was 0, because there was
  260. * surely an error that was asserted that should have been
  261. * handled for hot plug case, this could happens
  262. */
  263. if (!(sw_status & AUX_SW_STATUS__AUX_SW_HPD_DISCON_MASK)) {
  264. reply->status = AUX_TRANSACTION_REPLY_INVALID;
  265. ASSERT_CRITICAL(false);
  266. return;
  267. }
  268. } else {
  269. switch (reply_result) {
  270. case 0: /* ACK */
  271. reply->status = AUX_TRANSACTION_REPLY_AUX_ACK;
  272. break;
  273. case 1: /* NACK */
  274. reply->status = AUX_TRANSACTION_REPLY_AUX_NACK;
  275. break;
  276. case 2: /* DEFER */
  277. reply->status = AUX_TRANSACTION_REPLY_AUX_DEFER;
  278. break;
  279. case 4: /* AUX ACK / I2C NACK */
  280. reply->status = AUX_TRANSACTION_REPLY_I2C_NACK;
  281. break;
  282. case 8: /* AUX ACK / I2C DEFER */
  283. reply->status = AUX_TRANSACTION_REPLY_I2C_DEFER;
  284. break;
  285. default:
  286. reply->status = AUX_TRANSACTION_REPLY_INVALID;
  287. }
  288. }
  289. }
  290. static enum aux_channel_operation_result get_channel_status(
  291. struct aux_engine *engine,
  292. uint8_t *returned_bytes)
  293. {
  294. struct aux_engine_dce110 *aux110 = FROM_AUX_ENGINE(engine);
  295. uint32_t value;
  296. if (returned_bytes == NULL) {
  297. /*caller pass NULL pointer*/
  298. ASSERT_CRITICAL(false);
  299. return AUX_CHANNEL_OPERATION_FAILED_REASON_UNKNOWN;
  300. }
  301. *returned_bytes = 0;
  302. /* poll to make sure that SW_DONE is asserted */
  303. value = REG_WAIT(AUX_SW_STATUS, AUX_SW_DONE, 1,
  304. 10, aux110->timeout_period/10);
  305. /* in case HPD is LOW, exit AUX transaction */
  306. if ((value & AUX_SW_STATUS__AUX_SW_HPD_DISCON_MASK))
  307. return AUX_CHANNEL_OPERATION_FAILED_HPD_DISCON;
  308. /* Note that the following bits are set in 'status.bits'
  309. * during CTS 4.2.1.2 (FW 3.3.1):
  310. * AUX_SW_RX_MIN_COUNT_VIOL, AUX_SW_RX_INVALID_STOP,
  311. * AUX_SW_RX_RECV_NO_DET, AUX_SW_RX_RECV_INVALID_H.
  312. *
  313. * AUX_SW_RX_MIN_COUNT_VIOL is an internal,
  314. * HW debugging bit and should be ignored.
  315. */
  316. if (value & AUX_SW_STATUS__AUX_SW_DONE_MASK) {
  317. if ((value & AUX_SW_STATUS__AUX_SW_RX_TIMEOUT_STATE_MASK) ||
  318. (value & AUX_SW_STATUS__AUX_SW_RX_TIMEOUT_MASK))
  319. return AUX_CHANNEL_OPERATION_FAILED_TIMEOUT;
  320. else if ((value & AUX_SW_STATUS__AUX_SW_RX_INVALID_STOP_MASK) ||
  321. (value & AUX_SW_STATUS__AUX_SW_RX_RECV_NO_DET_MASK) ||
  322. (value &
  323. AUX_SW_STATUS__AUX_SW_RX_RECV_INVALID_H_MASK) ||
  324. (value & AUX_SW_STATUS__AUX_SW_RX_RECV_INVALID_L_MASK))
  325. return AUX_CHANNEL_OPERATION_FAILED_INVALID_REPLY;
  326. *returned_bytes = get_reg_field_value(value,
  327. AUX_SW_STATUS,
  328. AUX_SW_REPLY_BYTE_COUNT);
  329. if (*returned_bytes == 0)
  330. return
  331. AUX_CHANNEL_OPERATION_FAILED_INVALID_REPLY;
  332. else {
  333. *returned_bytes -= 1;
  334. return AUX_CHANNEL_OPERATION_SUCCEEDED;
  335. }
  336. } else {
  337. /*time_elapsed >= aux_engine->timeout_period
  338. * AUX_SW_STATUS__AUX_SW_HPD_DISCON = at this point
  339. */
  340. ASSERT_CRITICAL(false);
  341. return AUX_CHANNEL_OPERATION_FAILED_TIMEOUT;
  342. }
  343. }
  344. static void process_read_reply(
  345. struct aux_engine *engine,
  346. struct read_command_context *ctx)
  347. {
  348. engine->funcs->process_channel_reply(engine, &ctx->reply);
  349. switch (ctx->reply.status) {
  350. case AUX_TRANSACTION_REPLY_AUX_ACK:
  351. ctx->defer_retry_aux = 0;
  352. if (ctx->returned_byte > ctx->current_read_length) {
  353. ctx->status =
  354. I2CAUX_TRANSACTION_STATUS_FAILED_PROTOCOL_ERROR;
  355. ctx->operation_succeeded = false;
  356. } else if (ctx->returned_byte < ctx->current_read_length) {
  357. ctx->current_read_length -= ctx->returned_byte;
  358. ctx->offset += ctx->returned_byte;
  359. ++ctx->invalid_reply_retry_aux_on_ack;
  360. if (ctx->invalid_reply_retry_aux_on_ack >
  361. AUX_INVALID_REPLY_RETRY_COUNTER) {
  362. ctx->status =
  363. I2CAUX_TRANSACTION_STATUS_FAILED_PROTOCOL_ERROR;
  364. ctx->operation_succeeded = false;
  365. }
  366. } else {
  367. ctx->status = I2CAUX_TRANSACTION_STATUS_SUCCEEDED;
  368. ctx->transaction_complete = true;
  369. ctx->operation_succeeded = true;
  370. }
  371. break;
  372. case AUX_TRANSACTION_REPLY_AUX_NACK:
  373. ctx->status = I2CAUX_TRANSACTION_STATUS_FAILED_NACK;
  374. ctx->operation_succeeded = false;
  375. break;
  376. case AUX_TRANSACTION_REPLY_AUX_DEFER:
  377. ++ctx->defer_retry_aux;
  378. if (ctx->defer_retry_aux > AUX_DEFER_RETRY_COUNTER) {
  379. ctx->status = I2CAUX_TRANSACTION_STATUS_FAILED_TIMEOUT;
  380. ctx->operation_succeeded = false;
  381. }
  382. break;
  383. case AUX_TRANSACTION_REPLY_I2C_DEFER:
  384. ctx->defer_retry_aux = 0;
  385. ++ctx->defer_retry_i2c;
  386. if (ctx->defer_retry_i2c > AUX_DEFER_RETRY_COUNTER) {
  387. ctx->status = I2CAUX_TRANSACTION_STATUS_FAILED_TIMEOUT;
  388. ctx->operation_succeeded = false;
  389. }
  390. break;
  391. case AUX_TRANSACTION_REPLY_HPD_DISCON:
  392. ctx->status = I2CAUX_TRANSACTION_STATUS_FAILED_HPD_DISCON;
  393. ctx->operation_succeeded = false;
  394. break;
  395. default:
  396. ctx->status = I2CAUX_TRANSACTION_STATUS_UNKNOWN;
  397. ctx->operation_succeeded = false;
  398. }
  399. }
  400. static void process_read_request(
  401. struct aux_engine *engine,
  402. struct read_command_context *ctx)
  403. {
  404. enum aux_channel_operation_result operation_result;
  405. engine->funcs->submit_channel_request(engine, &ctx->request);
  406. operation_result = engine->funcs->get_channel_status(
  407. engine, &ctx->returned_byte);
  408. switch (operation_result) {
  409. case AUX_CHANNEL_OPERATION_SUCCEEDED:
  410. if (ctx->returned_byte > ctx->current_read_length) {
  411. ctx->status =
  412. I2CAUX_TRANSACTION_STATUS_FAILED_PROTOCOL_ERROR;
  413. ctx->operation_succeeded = false;
  414. } else {
  415. ctx->timed_out_retry_aux = 0;
  416. ctx->invalid_reply_retry_aux = 0;
  417. ctx->reply.length = ctx->returned_byte;
  418. ctx->reply.data = ctx->buffer;
  419. process_read_reply(engine, ctx);
  420. }
  421. break;
  422. case AUX_CHANNEL_OPERATION_FAILED_INVALID_REPLY:
  423. ++ctx->invalid_reply_retry_aux;
  424. if (ctx->invalid_reply_retry_aux >
  425. AUX_INVALID_REPLY_RETRY_COUNTER) {
  426. ctx->status =
  427. I2CAUX_TRANSACTION_STATUS_FAILED_PROTOCOL_ERROR;
  428. ctx->operation_succeeded = false;
  429. } else
  430. udelay(400);
  431. break;
  432. case AUX_CHANNEL_OPERATION_FAILED_TIMEOUT:
  433. ++ctx->timed_out_retry_aux;
  434. if (ctx->timed_out_retry_aux > AUX_TIMED_OUT_RETRY_COUNTER) {
  435. ctx->status = I2CAUX_TRANSACTION_STATUS_FAILED_TIMEOUT;
  436. ctx->operation_succeeded = false;
  437. } else {
  438. /* DP 1.2a, table 2-58:
  439. * "S3: AUX Request CMD PENDING:
  440. * retry 3 times, with 400usec wait on each"
  441. * The HW timeout is set to 550usec,
  442. * so we should not wait here
  443. */
  444. }
  445. break;
  446. case AUX_CHANNEL_OPERATION_FAILED_HPD_DISCON:
  447. ctx->status = I2CAUX_TRANSACTION_STATUS_FAILED_HPD_DISCON;
  448. ctx->operation_succeeded = false;
  449. break;
  450. default:
  451. ctx->status = I2CAUX_TRANSACTION_STATUS_UNKNOWN;
  452. ctx->operation_succeeded = false;
  453. }
  454. }
  455. static bool read_command(
  456. struct aux_engine *engine,
  457. struct i2caux_transaction_request *request,
  458. bool middle_of_transaction)
  459. {
  460. struct read_command_context ctx;
  461. ctx.buffer = request->payload.data;
  462. ctx.current_read_length = request->payload.length;
  463. ctx.offset = 0;
  464. ctx.timed_out_retry_aux = 0;
  465. ctx.invalid_reply_retry_aux = 0;
  466. ctx.defer_retry_aux = 0;
  467. ctx.defer_retry_i2c = 0;
  468. ctx.invalid_reply_retry_aux_on_ack = 0;
  469. ctx.transaction_complete = false;
  470. ctx.operation_succeeded = true;
  471. if (request->payload.address_space ==
  472. I2CAUX_TRANSACTION_ADDRESS_SPACE_DPCD) {
  473. ctx.request.type = AUX_TRANSACTION_TYPE_DP;
  474. ctx.request.action = I2CAUX_TRANSACTION_ACTION_DP_READ;
  475. ctx.request.address = request->payload.address;
  476. } else if (request->payload.address_space ==
  477. I2CAUX_TRANSACTION_ADDRESS_SPACE_I2C) {
  478. ctx.request.type = AUX_TRANSACTION_TYPE_I2C;
  479. ctx.request.action = middle_of_transaction ?
  480. I2CAUX_TRANSACTION_ACTION_I2C_READ_MOT :
  481. I2CAUX_TRANSACTION_ACTION_I2C_READ;
  482. ctx.request.address = request->payload.address >> 1;
  483. } else {
  484. /* in DAL2, there was no return in such case */
  485. BREAK_TO_DEBUGGER();
  486. return false;
  487. }
  488. ctx.request.delay = 0;
  489. do {
  490. memset(ctx.buffer + ctx.offset, 0, ctx.current_read_length);
  491. ctx.request.data = ctx.buffer + ctx.offset;
  492. ctx.request.length = ctx.current_read_length;
  493. process_read_request(engine, &ctx);
  494. request->status = ctx.status;
  495. if (ctx.operation_succeeded && !ctx.transaction_complete)
  496. if (ctx.request.type == AUX_TRANSACTION_TYPE_I2C)
  497. msleep(engine->delay);
  498. } while (ctx.operation_succeeded && !ctx.transaction_complete);
  499. if (request->payload.address_space ==
  500. I2CAUX_TRANSACTION_ADDRESS_SPACE_DPCD) {
  501. DC_LOG_I2C_AUX("READ: addr:0x%x value:0x%x Result:%d",
  502. request->payload.address,
  503. request->payload.data[0],
  504. ctx.operation_succeeded);
  505. }
  506. return ctx.operation_succeeded;
  507. }
  508. static void process_write_reply(
  509. struct aux_engine *engine,
  510. struct write_command_context *ctx)
  511. {
  512. engine->funcs->process_channel_reply(engine, &ctx->reply);
  513. switch (ctx->reply.status) {
  514. case AUX_TRANSACTION_REPLY_AUX_ACK:
  515. ctx->operation_succeeded = true;
  516. if (ctx->returned_byte) {
  517. ctx->request.action = ctx->mot ?
  518. I2CAUX_TRANSACTION_ACTION_I2C_STATUS_REQUEST_MOT :
  519. I2CAUX_TRANSACTION_ACTION_I2C_STATUS_REQUEST;
  520. ctx->current_write_length = 0;
  521. ++ctx->ack_m_retry;
  522. if (ctx->ack_m_retry > AUX_DEFER_RETRY_COUNTER) {
  523. ctx->status =
  524. I2CAUX_TRANSACTION_STATUS_FAILED_TIMEOUT;
  525. ctx->operation_succeeded = false;
  526. } else
  527. udelay(300);
  528. } else {
  529. ctx->status = I2CAUX_TRANSACTION_STATUS_SUCCEEDED;
  530. ctx->defer_retry_aux = 0;
  531. ctx->ack_m_retry = 0;
  532. ctx->transaction_complete = true;
  533. }
  534. break;
  535. case AUX_TRANSACTION_REPLY_AUX_NACK:
  536. ctx->status = I2CAUX_TRANSACTION_STATUS_FAILED_NACK;
  537. ctx->operation_succeeded = false;
  538. break;
  539. case AUX_TRANSACTION_REPLY_AUX_DEFER:
  540. ++ctx->defer_retry_aux;
  541. if (ctx->defer_retry_aux > ctx->max_defer_retry) {
  542. ctx->status = I2CAUX_TRANSACTION_STATUS_FAILED_TIMEOUT;
  543. ctx->operation_succeeded = false;
  544. }
  545. break;
  546. case AUX_TRANSACTION_REPLY_I2C_DEFER:
  547. ctx->defer_retry_aux = 0;
  548. ctx->current_write_length = 0;
  549. ctx->request.action = ctx->mot ?
  550. I2CAUX_TRANSACTION_ACTION_I2C_STATUS_REQUEST_MOT :
  551. I2CAUX_TRANSACTION_ACTION_I2C_STATUS_REQUEST;
  552. ++ctx->defer_retry_i2c;
  553. if (ctx->defer_retry_i2c > ctx->max_defer_retry) {
  554. ctx->status = I2CAUX_TRANSACTION_STATUS_FAILED_TIMEOUT;
  555. ctx->operation_succeeded = false;
  556. }
  557. break;
  558. case AUX_TRANSACTION_REPLY_HPD_DISCON:
  559. ctx->status = I2CAUX_TRANSACTION_STATUS_FAILED_HPD_DISCON;
  560. ctx->operation_succeeded = false;
  561. break;
  562. default:
  563. ctx->status = I2CAUX_TRANSACTION_STATUS_UNKNOWN;
  564. ctx->operation_succeeded = false;
  565. }
  566. }
  567. static void process_write_request(
  568. struct aux_engine *engine,
  569. struct write_command_context *ctx)
  570. {
  571. enum aux_channel_operation_result operation_result;
  572. engine->funcs->submit_channel_request(engine, &ctx->request);
  573. operation_result = engine->funcs->get_channel_status(
  574. engine, &ctx->returned_byte);
  575. switch (operation_result) {
  576. case AUX_CHANNEL_OPERATION_SUCCEEDED:
  577. ctx->timed_out_retry_aux = 0;
  578. ctx->invalid_reply_retry_aux = 0;
  579. ctx->reply.length = ctx->returned_byte;
  580. ctx->reply.data = ctx->reply_data;
  581. process_write_reply(engine, ctx);
  582. break;
  583. case AUX_CHANNEL_OPERATION_FAILED_INVALID_REPLY:
  584. ++ctx->invalid_reply_retry_aux;
  585. if (ctx->invalid_reply_retry_aux >
  586. AUX_INVALID_REPLY_RETRY_COUNTER) {
  587. ctx->status =
  588. I2CAUX_TRANSACTION_STATUS_FAILED_PROTOCOL_ERROR;
  589. ctx->operation_succeeded = false;
  590. } else
  591. udelay(400);
  592. break;
  593. case AUX_CHANNEL_OPERATION_FAILED_TIMEOUT:
  594. ++ctx->timed_out_retry_aux;
  595. if (ctx->timed_out_retry_aux > AUX_TIMED_OUT_RETRY_COUNTER) {
  596. ctx->status = I2CAUX_TRANSACTION_STATUS_FAILED_TIMEOUT;
  597. ctx->operation_succeeded = false;
  598. } else {
  599. /* DP 1.2a, table 2-58:
  600. * "S3: AUX Request CMD PENDING:
  601. * retry 3 times, with 400usec wait on each"
  602. * The HW timeout is set to 550usec,
  603. * so we should not wait here
  604. */
  605. }
  606. break;
  607. case AUX_CHANNEL_OPERATION_FAILED_HPD_DISCON:
  608. ctx->status = I2CAUX_TRANSACTION_STATUS_FAILED_HPD_DISCON;
  609. ctx->operation_succeeded = false;
  610. break;
  611. default:
  612. ctx->status = I2CAUX_TRANSACTION_STATUS_UNKNOWN;
  613. ctx->operation_succeeded = false;
  614. }
  615. }
  616. static bool write_command(
  617. struct aux_engine *engine,
  618. struct i2caux_transaction_request *request,
  619. bool middle_of_transaction)
  620. {
  621. struct write_command_context ctx;
  622. ctx.mot = middle_of_transaction;
  623. ctx.buffer = request->payload.data;
  624. ctx.current_write_length = request->payload.length;
  625. ctx.timed_out_retry_aux = 0;
  626. ctx.invalid_reply_retry_aux = 0;
  627. ctx.defer_retry_aux = 0;
  628. ctx.defer_retry_i2c = 0;
  629. ctx.ack_m_retry = 0;
  630. ctx.transaction_complete = false;
  631. ctx.operation_succeeded = true;
  632. if (request->payload.address_space ==
  633. I2CAUX_TRANSACTION_ADDRESS_SPACE_DPCD) {
  634. ctx.request.type = AUX_TRANSACTION_TYPE_DP;
  635. ctx.request.action = I2CAUX_TRANSACTION_ACTION_DP_WRITE;
  636. ctx.request.address = request->payload.address;
  637. } else if (request->payload.address_space ==
  638. I2CAUX_TRANSACTION_ADDRESS_SPACE_I2C) {
  639. ctx.request.type = AUX_TRANSACTION_TYPE_I2C;
  640. ctx.request.action = middle_of_transaction ?
  641. I2CAUX_TRANSACTION_ACTION_I2C_WRITE_MOT :
  642. I2CAUX_TRANSACTION_ACTION_I2C_WRITE;
  643. ctx.request.address = request->payload.address >> 1;
  644. } else {
  645. /* in DAL2, there was no return in such case */
  646. BREAK_TO_DEBUGGER();
  647. return false;
  648. }
  649. ctx.request.delay = 0;
  650. ctx.max_defer_retry =
  651. (engine->max_defer_write_retry > AUX_DEFER_RETRY_COUNTER) ?
  652. engine->max_defer_write_retry : AUX_DEFER_RETRY_COUNTER;
  653. do {
  654. ctx.request.data = ctx.buffer;
  655. ctx.request.length = ctx.current_write_length;
  656. process_write_request(engine, &ctx);
  657. request->status = ctx.status;
  658. if (ctx.operation_succeeded && !ctx.transaction_complete)
  659. if (ctx.request.type == AUX_TRANSACTION_TYPE_I2C)
  660. msleep(engine->delay);
  661. } while (ctx.operation_succeeded && !ctx.transaction_complete);
  662. if (request->payload.address_space ==
  663. I2CAUX_TRANSACTION_ADDRESS_SPACE_DPCD) {
  664. DC_LOG_I2C_AUX("WRITE: addr:0x%x value:0x%x Result:%d",
  665. request->payload.address,
  666. request->payload.data[0],
  667. ctx.operation_succeeded);
  668. }
  669. return ctx.operation_succeeded;
  670. }
  671. static bool end_of_transaction_command(
  672. struct aux_engine *engine,
  673. struct i2caux_transaction_request *request)
  674. {
  675. struct i2caux_transaction_request dummy_request;
  676. uint8_t dummy_data;
  677. /* [tcheng] We only need to send the stop (read with MOT = 0)
  678. * for I2C-over-Aux, not native AUX
  679. */
  680. if (request->payload.address_space !=
  681. I2CAUX_TRANSACTION_ADDRESS_SPACE_I2C)
  682. return false;
  683. dummy_request.operation = request->operation;
  684. dummy_request.payload.address_space = request->payload.address_space;
  685. dummy_request.payload.address = request->payload.address;
  686. /*
  687. * Add a dummy byte due to some receiver quirk
  688. * where one byte is sent along with MOT = 0.
  689. * Ideally this should be 0.
  690. */
  691. dummy_request.payload.length = 0;
  692. dummy_request.payload.data = &dummy_data;
  693. if (request->operation == I2CAUX_TRANSACTION_READ)
  694. return read_command(engine, &dummy_request, false);
  695. else
  696. return write_command(engine, &dummy_request, false);
  697. /* according Syed, it does not need now DoDummyMOT */
  698. }
  699. static bool submit_request(
  700. struct aux_engine *engine,
  701. struct i2caux_transaction_request *request,
  702. bool middle_of_transaction)
  703. {
  704. bool result;
  705. bool mot_used = true;
  706. switch (request->operation) {
  707. case I2CAUX_TRANSACTION_READ:
  708. result = read_command(engine, request, mot_used);
  709. break;
  710. case I2CAUX_TRANSACTION_WRITE:
  711. result = write_command(engine, request, mot_used);
  712. break;
  713. default:
  714. result = false;
  715. }
  716. /* [tcheng]
  717. * need to send stop for the last transaction to free up the AUX
  718. * if the above command fails, this would be the last transaction
  719. */
  720. if (!middle_of_transaction || !result)
  721. end_of_transaction_command(engine, request);
  722. /* mask AUX interrupt */
  723. return result;
  724. }
  725. enum i2caux_engine_type get_engine_type(
  726. const struct aux_engine *engine)
  727. {
  728. return I2CAUX_ENGINE_TYPE_AUX;
  729. }
  730. static bool acquire(
  731. struct aux_engine *engine,
  732. struct ddc *ddc)
  733. {
  734. enum gpio_result result;
  735. if (engine->funcs->is_engine_available) {
  736. /*check whether SW could use the engine*/
  737. if (!engine->funcs->is_engine_available(engine))
  738. return false;
  739. }
  740. result = dal_ddc_open(ddc, GPIO_MODE_HARDWARE,
  741. GPIO_DDC_CONFIG_TYPE_MODE_AUX);
  742. if (result != GPIO_RESULT_OK)
  743. return false;
  744. if (!engine->funcs->acquire_engine(engine)) {
  745. dal_ddc_close(ddc);
  746. return false;
  747. }
  748. engine->ddc = ddc;
  749. return true;
  750. }
  751. static const struct aux_engine_funcs aux_engine_funcs = {
  752. .acquire_engine = acquire_engine,
  753. .submit_channel_request = submit_channel_request,
  754. .process_channel_reply = process_channel_reply,
  755. .read_channel_reply = read_channel_reply,
  756. .get_channel_status = get_channel_status,
  757. .is_engine_available = is_engine_available,
  758. .release_engine = release_engine,
  759. .destroy_engine = dce110_engine_destroy,
  760. .submit_request = submit_request,
  761. .get_engine_type = get_engine_type,
  762. .acquire = acquire,
  763. };
  764. void dce110_engine_destroy(struct aux_engine **engine)
  765. {
  766. struct aux_engine_dce110 *engine110 = FROM_AUX_ENGINE(*engine);
  767. kfree(engine110);
  768. *engine = NULL;
  769. }
  770. struct aux_engine *dce110_aux_engine_construct(struct aux_engine_dce110 *aux_engine110,
  771. struct dc_context *ctx,
  772. uint32_t inst,
  773. uint32_t timeout_period,
  774. const struct dce110_aux_registers *regs)
  775. {
  776. aux_engine110->base.ddc = NULL;
  777. aux_engine110->base.ctx = ctx;
  778. aux_engine110->base.delay = 0;
  779. aux_engine110->base.max_defer_write_retry = 0;
  780. aux_engine110->base.funcs = &aux_engine_funcs;
  781. aux_engine110->base.inst = inst;
  782. aux_engine110->timeout_period = timeout_period;
  783. aux_engine110->regs = regs;
  784. return &aux_engine110->base;
  785. }