hdmi_hdcp.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437
  1. /* Copyright (c) 2010-2015, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. */
  13. #include "hdmi.h"
  14. #include <linux/qcom_scm.h>
  15. #define HDCP_REG_ENABLE 0x01
  16. #define HDCP_REG_DISABLE 0x00
  17. #define HDCP_PORT_ADDR 0x74
  18. #define HDCP_INT_STATUS_MASK ( \
  19. HDMI_HDCP_INT_CTRL_AUTH_SUCCESS_INT | \
  20. HDMI_HDCP_INT_CTRL_AUTH_FAIL_INT | \
  21. HDMI_HDCP_INT_CTRL_AUTH_XFER_REQ_INT | \
  22. HDMI_HDCP_INT_CTRL_AUTH_XFER_DONE_INT)
  23. #define AUTH_WORK_RETRIES_TIME 100
  24. #define AUTH_RETRIES_TIME 30
  25. /* QFPROM Registers for HDMI/HDCP */
  26. #define QFPROM_RAW_FEAT_CONFIG_ROW0_LSB 0x000000F8
  27. #define QFPROM_RAW_FEAT_CONFIG_ROW0_MSB 0x000000FC
  28. #define HDCP_KSV_LSB 0x000060D8
  29. #define HDCP_KSV_MSB 0x000060DC
  30. enum DS_TYPE { /* type of downstream device */
  31. DS_UNKNOWN,
  32. DS_RECEIVER,
  33. DS_REPEATER,
  34. };
  35. enum hdmi_hdcp_state {
  36. HDCP_STATE_NO_AKSV,
  37. HDCP_STATE_INACTIVE,
  38. HDCP_STATE_AUTHENTICATING,
  39. HDCP_STATE_AUTHENTICATED,
  40. HDCP_STATE_AUTH_FAILED
  41. };
  42. struct hdmi_hdcp_reg_data {
  43. u32 reg_id;
  44. u32 off;
  45. char *name;
  46. u32 reg_val;
  47. };
  48. struct hdmi_hdcp_ctrl {
  49. struct hdmi *hdmi;
  50. u32 auth_retries;
  51. bool tz_hdcp;
  52. enum hdmi_hdcp_state hdcp_state;
  53. struct work_struct hdcp_auth_work;
  54. struct work_struct hdcp_reauth_work;
  55. #define AUTH_ABORT_EV 1
  56. #define AUTH_RESULT_RDY_EV 2
  57. unsigned long auth_event;
  58. wait_queue_head_t auth_event_queue;
  59. u32 ksv_fifo_w_index;
  60. /*
  61. * store aksv from qfprom
  62. */
  63. u32 aksv_lsb;
  64. u32 aksv_msb;
  65. bool aksv_valid;
  66. u32 ds_type;
  67. u32 bksv_lsb;
  68. u32 bksv_msb;
  69. u8 dev_count;
  70. u8 depth;
  71. u8 ksv_list[5 * 127];
  72. bool max_cascade_exceeded;
  73. bool max_dev_exceeded;
  74. };
  75. static int hdmi_ddc_read(struct hdmi *hdmi, u16 addr, u8 offset,
  76. u8 *data, u16 data_len)
  77. {
  78. int rc;
  79. int retry = 5;
  80. struct i2c_msg msgs[] = {
  81. {
  82. .addr = addr >> 1,
  83. .flags = 0,
  84. .len = 1,
  85. .buf = &offset,
  86. }, {
  87. .addr = addr >> 1,
  88. .flags = I2C_M_RD,
  89. .len = data_len,
  90. .buf = data,
  91. }
  92. };
  93. DBG("Start DDC read");
  94. retry:
  95. rc = i2c_transfer(hdmi->i2c, msgs, 2);
  96. retry--;
  97. if (rc == 2)
  98. rc = 0;
  99. else if (retry > 0)
  100. goto retry;
  101. else
  102. rc = -EIO;
  103. DBG("End DDC read %d", rc);
  104. return rc;
  105. }
  106. #define HDCP_DDC_WRITE_MAX_BYTE_NUM 32
  107. static int hdmi_ddc_write(struct hdmi *hdmi, u16 addr, u8 offset,
  108. u8 *data, u16 data_len)
  109. {
  110. int rc;
  111. int retry = 10;
  112. u8 buf[HDCP_DDC_WRITE_MAX_BYTE_NUM];
  113. struct i2c_msg msgs[] = {
  114. {
  115. .addr = addr >> 1,
  116. .flags = 0,
  117. .len = 1,
  118. }
  119. };
  120. DBG("Start DDC write");
  121. if (data_len > (HDCP_DDC_WRITE_MAX_BYTE_NUM - 1)) {
  122. pr_err("%s: write size too big\n", __func__);
  123. return -ERANGE;
  124. }
  125. buf[0] = offset;
  126. memcpy(&buf[1], data, data_len);
  127. msgs[0].buf = buf;
  128. msgs[0].len = data_len + 1;
  129. retry:
  130. rc = i2c_transfer(hdmi->i2c, msgs, 1);
  131. retry--;
  132. if (rc == 1)
  133. rc = 0;
  134. else if (retry > 0)
  135. goto retry;
  136. else
  137. rc = -EIO;
  138. DBG("End DDC write %d", rc);
  139. return rc;
  140. }
  141. static int hdmi_hdcp_scm_wr(struct hdmi_hdcp_ctrl *hdcp_ctrl, u32 *preg,
  142. u32 *pdata, u32 count)
  143. {
  144. struct hdmi *hdmi = hdcp_ctrl->hdmi;
  145. struct qcom_scm_hdcp_req scm_buf[QCOM_SCM_HDCP_MAX_REQ_CNT];
  146. u32 resp, phy_addr, idx = 0;
  147. int i, ret = 0;
  148. WARN_ON(!pdata || !preg || (count == 0));
  149. if (hdcp_ctrl->tz_hdcp) {
  150. phy_addr = (u32)hdmi->mmio_phy_addr;
  151. while (count) {
  152. memset(scm_buf, 0, sizeof(scm_buf));
  153. for (i = 0; i < count && i < QCOM_SCM_HDCP_MAX_REQ_CNT;
  154. i++) {
  155. scm_buf[i].addr = phy_addr + preg[idx];
  156. scm_buf[i].val = pdata[idx];
  157. idx++;
  158. }
  159. ret = qcom_scm_hdcp_req(scm_buf, i, &resp);
  160. if (ret || resp) {
  161. pr_err("%s: error: scm_call ret=%d resp=%u\n",
  162. __func__, ret, resp);
  163. ret = -EINVAL;
  164. break;
  165. }
  166. count -= i;
  167. }
  168. } else {
  169. for (i = 0; i < count; i++)
  170. hdmi_write(hdmi, preg[i], pdata[i]);
  171. }
  172. return ret;
  173. }
  174. void hdmi_hdcp_irq(struct hdmi_hdcp_ctrl *hdcp_ctrl)
  175. {
  176. struct hdmi *hdmi = hdcp_ctrl->hdmi;
  177. u32 reg_val, hdcp_int_status;
  178. unsigned long flags;
  179. spin_lock_irqsave(&hdmi->reg_lock, flags);
  180. reg_val = hdmi_read(hdmi, REG_HDMI_HDCP_INT_CTRL);
  181. hdcp_int_status = reg_val & HDCP_INT_STATUS_MASK;
  182. if (!hdcp_int_status) {
  183. spin_unlock_irqrestore(&hdmi->reg_lock, flags);
  184. return;
  185. }
  186. /* Clear Interrupts */
  187. reg_val |= hdcp_int_status << 1;
  188. /* Clear AUTH_FAIL_INFO as well */
  189. if (hdcp_int_status & HDMI_HDCP_INT_CTRL_AUTH_FAIL_INT)
  190. reg_val |= HDMI_HDCP_INT_CTRL_AUTH_FAIL_INFO_ACK;
  191. hdmi_write(hdmi, REG_HDMI_HDCP_INT_CTRL, reg_val);
  192. spin_unlock_irqrestore(&hdmi->reg_lock, flags);
  193. DBG("hdcp irq %x", hdcp_int_status);
  194. if (hdcp_int_status & HDMI_HDCP_INT_CTRL_AUTH_SUCCESS_INT) {
  195. pr_info("%s:AUTH_SUCCESS_INT received\n", __func__);
  196. if (HDCP_STATE_AUTHENTICATING == hdcp_ctrl->hdcp_state) {
  197. set_bit(AUTH_RESULT_RDY_EV, &hdcp_ctrl->auth_event);
  198. wake_up_all(&hdcp_ctrl->auth_event_queue);
  199. }
  200. }
  201. if (hdcp_int_status & HDMI_HDCP_INT_CTRL_AUTH_FAIL_INT) {
  202. reg_val = hdmi_read(hdmi, REG_HDMI_HDCP_LINK0_STATUS);
  203. pr_info("%s: AUTH_FAIL_INT rcvd, LINK0_STATUS=0x%08x\n",
  204. __func__, reg_val);
  205. if (HDCP_STATE_AUTHENTICATED == hdcp_ctrl->hdcp_state)
  206. queue_work(hdmi->workq, &hdcp_ctrl->hdcp_reauth_work);
  207. else if (HDCP_STATE_AUTHENTICATING ==
  208. hdcp_ctrl->hdcp_state) {
  209. set_bit(AUTH_RESULT_RDY_EV, &hdcp_ctrl->auth_event);
  210. wake_up_all(&hdcp_ctrl->auth_event_queue);
  211. }
  212. }
  213. }
  214. static int hdmi_hdcp_msleep(struct hdmi_hdcp_ctrl *hdcp_ctrl, u32 ms, u32 ev)
  215. {
  216. int rc;
  217. rc = wait_event_timeout(hdcp_ctrl->auth_event_queue,
  218. !!test_bit(ev, &hdcp_ctrl->auth_event),
  219. msecs_to_jiffies(ms));
  220. if (rc) {
  221. pr_info("%s: msleep is canceled by event %d\n",
  222. __func__, ev);
  223. clear_bit(ev, &hdcp_ctrl->auth_event);
  224. return -ECANCELED;
  225. }
  226. return 0;
  227. }
  228. static int hdmi_hdcp_read_validate_aksv(struct hdmi_hdcp_ctrl *hdcp_ctrl)
  229. {
  230. struct hdmi *hdmi = hdcp_ctrl->hdmi;
  231. /* Fetch aksv from QFPROM, this info should be public. */
  232. hdcp_ctrl->aksv_lsb = hdmi_qfprom_read(hdmi, HDCP_KSV_LSB);
  233. hdcp_ctrl->aksv_msb = hdmi_qfprom_read(hdmi, HDCP_KSV_MSB);
  234. /* check there are 20 ones in AKSV */
  235. if ((hweight32(hdcp_ctrl->aksv_lsb) + hweight32(hdcp_ctrl->aksv_msb))
  236. != 20) {
  237. pr_err("%s: AKSV QFPROM doesn't have 20 1's, 20 0's\n",
  238. __func__);
  239. pr_err("%s: QFPROM AKSV chk failed (AKSV=%02x%08x)\n",
  240. __func__, hdcp_ctrl->aksv_msb,
  241. hdcp_ctrl->aksv_lsb);
  242. return -EINVAL;
  243. }
  244. DBG("AKSV=%02x%08x", hdcp_ctrl->aksv_msb, hdcp_ctrl->aksv_lsb);
  245. return 0;
  246. }
  247. static int reset_hdcp_ddc_failures(struct hdmi_hdcp_ctrl *hdcp_ctrl)
  248. {
  249. struct hdmi *hdmi = hdcp_ctrl->hdmi;
  250. u32 reg_val, failure, nack0;
  251. int rc = 0;
  252. /* Check for any DDC transfer failures */
  253. reg_val = hdmi_read(hdmi, REG_HDMI_HDCP_DDC_STATUS);
  254. failure = reg_val & HDMI_HDCP_DDC_STATUS_FAILED;
  255. nack0 = reg_val & HDMI_HDCP_DDC_STATUS_NACK0;
  256. DBG("HDCP_DDC_STATUS=0x%x, FAIL=%d, NACK0=%d",
  257. reg_val, failure, nack0);
  258. if (failure) {
  259. /*
  260. * Indicates that the last HDCP HW DDC transfer failed.
  261. * This occurs when a transfer is attempted with HDCP DDC
  262. * disabled (HDCP_DDC_DISABLE=1) or the number of retries
  263. * matches HDCP_DDC_RETRY_CNT.
  264. * Failure occurred, let's clear it.
  265. */
  266. DBG("DDC failure detected");
  267. /* First, Disable DDC */
  268. hdmi_write(hdmi, REG_HDMI_HDCP_DDC_CTRL_0,
  269. HDMI_HDCP_DDC_CTRL_0_DISABLE);
  270. /* ACK the Failure to Clear it */
  271. reg_val = hdmi_read(hdmi, REG_HDMI_HDCP_DDC_CTRL_1);
  272. reg_val |= HDMI_HDCP_DDC_CTRL_1_FAILED_ACK;
  273. hdmi_write(hdmi, REG_HDMI_HDCP_DDC_CTRL_1, reg_val);
  274. /* Check if the FAILURE got Cleared */
  275. reg_val = hdmi_read(hdmi, REG_HDMI_HDCP_DDC_STATUS);
  276. if (reg_val & HDMI_HDCP_DDC_STATUS_FAILED)
  277. pr_info("%s: Unable to clear HDCP DDC Failure\n",
  278. __func__);
  279. /* Re-Enable HDCP DDC */
  280. hdmi_write(hdmi, REG_HDMI_HDCP_DDC_CTRL_0, 0);
  281. }
  282. if (nack0) {
  283. DBG("Before: HDMI_DDC_SW_STATUS=0x%08x",
  284. hdmi_read(hdmi, REG_HDMI_DDC_SW_STATUS));
  285. /* Reset HDMI DDC software status */
  286. reg_val = hdmi_read(hdmi, REG_HDMI_DDC_CTRL);
  287. reg_val |= HDMI_DDC_CTRL_SW_STATUS_RESET;
  288. hdmi_write(hdmi, REG_HDMI_DDC_CTRL, reg_val);
  289. rc = hdmi_hdcp_msleep(hdcp_ctrl, 20, AUTH_ABORT_EV);
  290. reg_val = hdmi_read(hdmi, REG_HDMI_DDC_CTRL);
  291. reg_val &= ~HDMI_DDC_CTRL_SW_STATUS_RESET;
  292. hdmi_write(hdmi, REG_HDMI_DDC_CTRL, reg_val);
  293. /* Reset HDMI DDC Controller */
  294. reg_val = hdmi_read(hdmi, REG_HDMI_DDC_CTRL);
  295. reg_val |= HDMI_DDC_CTRL_SOFT_RESET;
  296. hdmi_write(hdmi, REG_HDMI_DDC_CTRL, reg_val);
  297. /* If previous msleep is aborted, skip this msleep */
  298. if (!rc)
  299. rc = hdmi_hdcp_msleep(hdcp_ctrl, 20, AUTH_ABORT_EV);
  300. reg_val = hdmi_read(hdmi, REG_HDMI_DDC_CTRL);
  301. reg_val &= ~HDMI_DDC_CTRL_SOFT_RESET;
  302. hdmi_write(hdmi, REG_HDMI_DDC_CTRL, reg_val);
  303. DBG("After: HDMI_DDC_SW_STATUS=0x%08x",
  304. hdmi_read(hdmi, REG_HDMI_DDC_SW_STATUS));
  305. }
  306. return rc;
  307. }
  308. static int hdmi_hdcp_hw_ddc_clean(struct hdmi_hdcp_ctrl *hdcp_ctrl)
  309. {
  310. int rc;
  311. u32 hdcp_ddc_status, ddc_hw_status;
  312. u32 xfer_done, xfer_req, hw_done;
  313. bool hw_not_ready;
  314. u32 timeout_count;
  315. struct hdmi *hdmi = hdcp_ctrl->hdmi;
  316. if (hdmi_read(hdmi, REG_HDMI_DDC_HW_STATUS) == 0)
  317. return 0;
  318. /* Wait to be clean on DDC HW engine */
  319. timeout_count = 100;
  320. do {
  321. hdcp_ddc_status = hdmi_read(hdmi, REG_HDMI_HDCP_DDC_STATUS);
  322. ddc_hw_status = hdmi_read(hdmi, REG_HDMI_DDC_HW_STATUS);
  323. xfer_done = hdcp_ddc_status & HDMI_HDCP_DDC_STATUS_XFER_DONE;
  324. xfer_req = hdcp_ddc_status & HDMI_HDCP_DDC_STATUS_XFER_REQ;
  325. hw_done = ddc_hw_status & HDMI_DDC_HW_STATUS_DONE;
  326. hw_not_ready = !xfer_done || xfer_req || !hw_done;
  327. if (hw_not_ready)
  328. break;
  329. timeout_count--;
  330. if (!timeout_count) {
  331. pr_warn("%s: hw_ddc_clean failed\n", __func__);
  332. return -ETIMEDOUT;
  333. }
  334. rc = hdmi_hdcp_msleep(hdcp_ctrl, 20, AUTH_ABORT_EV);
  335. if (rc)
  336. return rc;
  337. } while (1);
  338. return 0;
  339. }
  340. static void hdmi_hdcp_reauth_work(struct work_struct *work)
  341. {
  342. struct hdmi_hdcp_ctrl *hdcp_ctrl = container_of(work,
  343. struct hdmi_hdcp_ctrl, hdcp_reauth_work);
  344. struct hdmi *hdmi = hdcp_ctrl->hdmi;
  345. unsigned long flags;
  346. u32 reg_val;
  347. DBG("HDCP REAUTH WORK");
  348. /*
  349. * Disable HPD circuitry.
  350. * This is needed to reset the HDCP cipher engine so that when we
  351. * attempt a re-authentication, HW would clear the AN0_READY and
  352. * AN1_READY bits in HDMI_HDCP_LINK0_STATUS register
  353. */
  354. spin_lock_irqsave(&hdmi->reg_lock, flags);
  355. reg_val = hdmi_read(hdmi, REG_HDMI_HPD_CTRL);
  356. reg_val &= ~HDMI_HPD_CTRL_ENABLE;
  357. hdmi_write(hdmi, REG_HDMI_HPD_CTRL, reg_val);
  358. /* Disable HDCP interrupts */
  359. hdmi_write(hdmi, REG_HDMI_HDCP_INT_CTRL, 0);
  360. spin_unlock_irqrestore(&hdmi->reg_lock, flags);
  361. hdmi_write(hdmi, REG_HDMI_HDCP_RESET,
  362. HDMI_HDCP_RESET_LINK0_DEAUTHENTICATE);
  363. /* Wait to be clean on DDC HW engine */
  364. if (hdmi_hdcp_hw_ddc_clean(hdcp_ctrl)) {
  365. pr_info("%s: reauth work aborted\n", __func__);
  366. return;
  367. }
  368. /* Disable encryption and disable the HDCP block */
  369. hdmi_write(hdmi, REG_HDMI_HDCP_CTRL, 0);
  370. /* Enable HPD circuitry */
  371. spin_lock_irqsave(&hdmi->reg_lock, flags);
  372. reg_val = hdmi_read(hdmi, REG_HDMI_HPD_CTRL);
  373. reg_val |= HDMI_HPD_CTRL_ENABLE;
  374. hdmi_write(hdmi, REG_HDMI_HPD_CTRL, reg_val);
  375. spin_unlock_irqrestore(&hdmi->reg_lock, flags);
  376. /*
  377. * Only retry defined times then abort current authenticating process
  378. */
  379. if (++hdcp_ctrl->auth_retries == AUTH_RETRIES_TIME) {
  380. hdcp_ctrl->hdcp_state = HDCP_STATE_INACTIVE;
  381. hdcp_ctrl->auth_retries = 0;
  382. pr_info("%s: abort reauthentication!\n", __func__);
  383. return;
  384. }
  385. DBG("Queue AUTH WORK");
  386. hdcp_ctrl->hdcp_state = HDCP_STATE_AUTHENTICATING;
  387. queue_work(hdmi->workq, &hdcp_ctrl->hdcp_auth_work);
  388. }
  389. static int hdmi_hdcp_auth_prepare(struct hdmi_hdcp_ctrl *hdcp_ctrl)
  390. {
  391. struct hdmi *hdmi = hdcp_ctrl->hdmi;
  392. u32 link0_status;
  393. u32 reg_val;
  394. unsigned long flags;
  395. int rc;
  396. if (!hdcp_ctrl->aksv_valid) {
  397. rc = hdmi_hdcp_read_validate_aksv(hdcp_ctrl);
  398. if (rc) {
  399. pr_err("%s: ASKV validation failed\n", __func__);
  400. hdcp_ctrl->hdcp_state = HDCP_STATE_NO_AKSV;
  401. return -ENOTSUPP;
  402. }
  403. hdcp_ctrl->aksv_valid = true;
  404. }
  405. spin_lock_irqsave(&hdmi->reg_lock, flags);
  406. /* disable HDMI Encrypt */
  407. reg_val = hdmi_read(hdmi, REG_HDMI_CTRL);
  408. reg_val &= ~HDMI_CTRL_ENCRYPTED;
  409. hdmi_write(hdmi, REG_HDMI_CTRL, reg_val);
  410. /* Enabling Software DDC */
  411. reg_val = hdmi_read(hdmi, REG_HDMI_DDC_ARBITRATION);
  412. reg_val &= ~HDMI_DDC_ARBITRATION_HW_ARBITRATION;
  413. hdmi_write(hdmi, REG_HDMI_DDC_ARBITRATION, reg_val);
  414. spin_unlock_irqrestore(&hdmi->reg_lock, flags);
  415. /*
  416. * Write AKSV read from QFPROM to the HDCP registers.
  417. * This step is needed for HDCP authentication and must be
  418. * written before enabling HDCP.
  419. */
  420. hdmi_write(hdmi, REG_HDMI_HDCP_SW_LOWER_AKSV, hdcp_ctrl->aksv_lsb);
  421. hdmi_write(hdmi, REG_HDMI_HDCP_SW_UPPER_AKSV, hdcp_ctrl->aksv_msb);
  422. /*
  423. * HDCP setup prior to enabling HDCP_CTRL.
  424. * Setup seed values for random number An.
  425. */
  426. hdmi_write(hdmi, REG_HDMI_HDCP_ENTROPY_CTRL0, 0xB1FFB0FF);
  427. hdmi_write(hdmi, REG_HDMI_HDCP_ENTROPY_CTRL1, 0xF00DFACE);
  428. /* Disable the RngCipher state */
  429. reg_val = hdmi_read(hdmi, REG_HDMI_HDCP_DEBUG_CTRL);
  430. reg_val &= ~HDMI_HDCP_DEBUG_CTRL_RNG_CIPHER;
  431. hdmi_write(hdmi, REG_HDMI_HDCP_DEBUG_CTRL, reg_val);
  432. DBG("HDCP_DEBUG_CTRL=0x%08x",
  433. hdmi_read(hdmi, REG_HDMI_HDCP_DEBUG_CTRL));
  434. /*
  435. * Ensure that all register writes are completed before
  436. * enabling HDCP cipher
  437. */
  438. wmb();
  439. /*
  440. * Enable HDCP
  441. * This needs to be done as early as possible in order for the
  442. * hardware to make An available to read
  443. */
  444. hdmi_write(hdmi, REG_HDMI_HDCP_CTRL, HDMI_HDCP_CTRL_ENABLE);
  445. /*
  446. * If we had stale values for the An ready bit, it should most
  447. * likely be cleared now after enabling HDCP cipher
  448. */
  449. link0_status = hdmi_read(hdmi, REG_HDMI_HDCP_LINK0_STATUS);
  450. DBG("After enabling HDCP Link0_Status=0x%08x", link0_status);
  451. if (!(link0_status &
  452. (HDMI_HDCP_LINK0_STATUS_AN_0_READY |
  453. HDMI_HDCP_LINK0_STATUS_AN_1_READY)))
  454. DBG("An not ready after enabling HDCP");
  455. /* Clear any DDC failures from previous tries before enable HDCP*/
  456. rc = reset_hdcp_ddc_failures(hdcp_ctrl);
  457. return rc;
  458. }
  459. static void hdmi_hdcp_auth_fail(struct hdmi_hdcp_ctrl *hdcp_ctrl)
  460. {
  461. struct hdmi *hdmi = hdcp_ctrl->hdmi;
  462. u32 reg_val;
  463. unsigned long flags;
  464. DBG("hdcp auth failed, queue reauth work");
  465. /* clear HDMI Encrypt */
  466. spin_lock_irqsave(&hdmi->reg_lock, flags);
  467. reg_val = hdmi_read(hdmi, REG_HDMI_CTRL);
  468. reg_val &= ~HDMI_CTRL_ENCRYPTED;
  469. hdmi_write(hdmi, REG_HDMI_CTRL, reg_val);
  470. spin_unlock_irqrestore(&hdmi->reg_lock, flags);
  471. hdcp_ctrl->hdcp_state = HDCP_STATE_AUTH_FAILED;
  472. queue_work(hdmi->workq, &hdcp_ctrl->hdcp_reauth_work);
  473. }
  474. static void hdmi_hdcp_auth_done(struct hdmi_hdcp_ctrl *hdcp_ctrl)
  475. {
  476. struct hdmi *hdmi = hdcp_ctrl->hdmi;
  477. u32 reg_val;
  478. unsigned long flags;
  479. /*
  480. * Disable software DDC before going into part3 to make sure
  481. * there is no Arbitration between software and hardware for DDC
  482. */
  483. spin_lock_irqsave(&hdmi->reg_lock, flags);
  484. reg_val = hdmi_read(hdmi, REG_HDMI_DDC_ARBITRATION);
  485. reg_val |= HDMI_DDC_ARBITRATION_HW_ARBITRATION;
  486. hdmi_write(hdmi, REG_HDMI_DDC_ARBITRATION, reg_val);
  487. spin_unlock_irqrestore(&hdmi->reg_lock, flags);
  488. /* enable HDMI Encrypt */
  489. spin_lock_irqsave(&hdmi->reg_lock, flags);
  490. reg_val = hdmi_read(hdmi, REG_HDMI_CTRL);
  491. reg_val |= HDMI_CTRL_ENCRYPTED;
  492. hdmi_write(hdmi, REG_HDMI_CTRL, reg_val);
  493. spin_unlock_irqrestore(&hdmi->reg_lock, flags);
  494. hdcp_ctrl->hdcp_state = HDCP_STATE_AUTHENTICATED;
  495. hdcp_ctrl->auth_retries = 0;
  496. }
  497. /*
  498. * hdcp authenticating part 1
  499. * Wait Key/An ready
  500. * Read BCAPS from sink
  501. * Write BCAPS and AKSV into HDCP engine
  502. * Write An and AKSV to sink
  503. * Read BKSV from sink and write into HDCP engine
  504. */
  505. static int hdmi_hdcp_wait_key_an_ready(struct hdmi_hdcp_ctrl *hdcp_ctrl)
  506. {
  507. int rc;
  508. struct hdmi *hdmi = hdcp_ctrl->hdmi;
  509. u32 link0_status, keys_state;
  510. u32 timeout_count;
  511. bool an_ready;
  512. /* Wait for HDCP keys to be checked and validated */
  513. timeout_count = 100;
  514. do {
  515. link0_status = hdmi_read(hdmi, REG_HDMI_HDCP_LINK0_STATUS);
  516. keys_state = (link0_status >> 28) & 0x7;
  517. if (keys_state == HDCP_KEYS_STATE_VALID)
  518. break;
  519. DBG("Keys not ready(%d). s=%d, l0=%0x08x",
  520. timeout_count, keys_state, link0_status);
  521. timeout_count--;
  522. if (!timeout_count) {
  523. pr_err("%s: Wait key state timedout", __func__);
  524. return -ETIMEDOUT;
  525. }
  526. rc = hdmi_hdcp_msleep(hdcp_ctrl, 20, AUTH_ABORT_EV);
  527. if (rc)
  528. return rc;
  529. } while (1);
  530. timeout_count = 100;
  531. do {
  532. link0_status = hdmi_read(hdmi, REG_HDMI_HDCP_LINK0_STATUS);
  533. an_ready = (link0_status & HDMI_HDCP_LINK0_STATUS_AN_0_READY)
  534. && (link0_status & HDMI_HDCP_LINK0_STATUS_AN_1_READY);
  535. if (an_ready)
  536. break;
  537. DBG("An not ready(%d). l0_status=0x%08x",
  538. timeout_count, link0_status);
  539. timeout_count--;
  540. if (!timeout_count) {
  541. pr_err("%s: Wait An timedout", __func__);
  542. return -ETIMEDOUT;
  543. }
  544. rc = hdmi_hdcp_msleep(hdcp_ctrl, 20, AUTH_ABORT_EV);
  545. if (rc)
  546. return rc;
  547. } while (1);
  548. return 0;
  549. }
  550. static int hdmi_hdcp_send_aksv_an(struct hdmi_hdcp_ctrl *hdcp_ctrl)
  551. {
  552. int rc = 0;
  553. struct hdmi *hdmi = hdcp_ctrl->hdmi;
  554. u32 link0_aksv_0, link0_aksv_1;
  555. u32 link0_an[2];
  556. u8 aksv[5];
  557. /* Read An0 and An1 */
  558. link0_an[0] = hdmi_read(hdmi, REG_HDMI_HDCP_RCVPORT_DATA5);
  559. link0_an[1] = hdmi_read(hdmi, REG_HDMI_HDCP_RCVPORT_DATA6);
  560. /* Read AKSV */
  561. link0_aksv_0 = hdmi_read(hdmi, REG_HDMI_HDCP_RCVPORT_DATA3);
  562. link0_aksv_1 = hdmi_read(hdmi, REG_HDMI_HDCP_RCVPORT_DATA4);
  563. DBG("Link ASKV=%08x%08x", link0_aksv_0, link0_aksv_1);
  564. /* Copy An and AKSV to byte arrays for transmission */
  565. aksv[0] = link0_aksv_0 & 0xFF;
  566. aksv[1] = (link0_aksv_0 >> 8) & 0xFF;
  567. aksv[2] = (link0_aksv_0 >> 16) & 0xFF;
  568. aksv[3] = (link0_aksv_0 >> 24) & 0xFF;
  569. aksv[4] = link0_aksv_1 & 0xFF;
  570. /* Write An to offset 0x18 */
  571. rc = hdmi_ddc_write(hdmi, HDCP_PORT_ADDR, 0x18, (u8 *)link0_an,
  572. (u16)sizeof(link0_an));
  573. if (rc) {
  574. pr_err("%s:An write failed\n", __func__);
  575. return rc;
  576. }
  577. DBG("Link0-An=%08x%08x", link0_an[0], link0_an[1]);
  578. /* Write AKSV to offset 0x10 */
  579. rc = hdmi_ddc_write(hdmi, HDCP_PORT_ADDR, 0x10, aksv, 5);
  580. if (rc) {
  581. pr_err("%s:AKSV write failed\n", __func__);
  582. return rc;
  583. }
  584. DBG("Link0-AKSV=%02x%08x", link0_aksv_1 & 0xFF, link0_aksv_0);
  585. return 0;
  586. }
  587. static int hdmi_hdcp_recv_bksv(struct hdmi_hdcp_ctrl *hdcp_ctrl)
  588. {
  589. int rc = 0;
  590. struct hdmi *hdmi = hdcp_ctrl->hdmi;
  591. u8 bksv[5];
  592. u32 reg[2], data[2];
  593. /* Read BKSV at offset 0x00 */
  594. rc = hdmi_ddc_read(hdmi, HDCP_PORT_ADDR, 0x00, bksv, 5);
  595. if (rc) {
  596. pr_err("%s:BKSV read failed\n", __func__);
  597. return rc;
  598. }
  599. hdcp_ctrl->bksv_lsb = bksv[0] | (bksv[1] << 8) |
  600. (bksv[2] << 16) | (bksv[3] << 24);
  601. hdcp_ctrl->bksv_msb = bksv[4];
  602. DBG(":BKSV=%02x%08x", hdcp_ctrl->bksv_msb, hdcp_ctrl->bksv_lsb);
  603. /* check there are 20 ones in BKSV */
  604. if ((hweight32(hdcp_ctrl->bksv_lsb) + hweight32(hdcp_ctrl->bksv_msb))
  605. != 20) {
  606. pr_err(": BKSV doesn't have 20 1's and 20 0's\n");
  607. pr_err(": BKSV chk fail. BKSV=%02x%02x%02x%02x%02x\n",
  608. bksv[4], bksv[3], bksv[2], bksv[1], bksv[0]);
  609. return -EINVAL;
  610. }
  611. /* Write BKSV read from sink to HDCP registers */
  612. reg[0] = REG_HDMI_HDCP_RCVPORT_DATA0;
  613. data[0] = hdcp_ctrl->bksv_lsb;
  614. reg[1] = REG_HDMI_HDCP_RCVPORT_DATA1;
  615. data[1] = hdcp_ctrl->bksv_msb;
  616. rc = hdmi_hdcp_scm_wr(hdcp_ctrl, reg, data, 2);
  617. return rc;
  618. }
  619. static int hdmi_hdcp_recv_bcaps(struct hdmi_hdcp_ctrl *hdcp_ctrl)
  620. {
  621. int rc = 0;
  622. struct hdmi *hdmi = hdcp_ctrl->hdmi;
  623. u32 reg, data;
  624. u8 bcaps;
  625. rc = hdmi_ddc_read(hdmi, HDCP_PORT_ADDR, 0x40, &bcaps, 1);
  626. if (rc) {
  627. pr_err("%s:BCAPS read failed\n", __func__);
  628. return rc;
  629. }
  630. DBG("BCAPS=%02x", bcaps);
  631. /* receiver (0), repeater (1) */
  632. hdcp_ctrl->ds_type = (bcaps & BIT(6)) ? DS_REPEATER : DS_RECEIVER;
  633. /* Write BCAPS to the hardware */
  634. reg = REG_HDMI_HDCP_RCVPORT_DATA12;
  635. data = (u32)bcaps;
  636. rc = hdmi_hdcp_scm_wr(hdcp_ctrl, &reg, &data, 1);
  637. return rc;
  638. }
  639. static int hdmi_hdcp_auth_part1_key_exchange(struct hdmi_hdcp_ctrl *hdcp_ctrl)
  640. {
  641. struct hdmi *hdmi = hdcp_ctrl->hdmi;
  642. unsigned long flags;
  643. int rc;
  644. /* Wait for AKSV key and An ready */
  645. rc = hdmi_hdcp_wait_key_an_ready(hdcp_ctrl);
  646. if (rc) {
  647. pr_err("%s: wait key and an ready failed\n", __func__);
  648. return rc;
  649. };
  650. /* Read BCAPS and send to HDCP engine */
  651. rc = hdmi_hdcp_recv_bcaps(hdcp_ctrl);
  652. if (rc) {
  653. pr_err("%s: read bcaps error, abort\n", __func__);
  654. return rc;
  655. }
  656. /*
  657. * 1.1_Features turned off by default.
  658. * No need to write AInfo since 1.1_Features is disabled.
  659. */
  660. hdmi_write(hdmi, REG_HDMI_HDCP_RCVPORT_DATA4, 0);
  661. /* Send AKSV and An to sink */
  662. rc = hdmi_hdcp_send_aksv_an(hdcp_ctrl);
  663. if (rc) {
  664. pr_err("%s:An/Aksv write failed\n", __func__);
  665. return rc;
  666. }
  667. /* Read BKSV and send to HDCP engine*/
  668. rc = hdmi_hdcp_recv_bksv(hdcp_ctrl);
  669. if (rc) {
  670. pr_err("%s:BKSV Process failed\n", __func__);
  671. return rc;
  672. }
  673. /* Enable HDCP interrupts and ack/clear any stale interrupts */
  674. spin_lock_irqsave(&hdmi->reg_lock, flags);
  675. hdmi_write(hdmi, REG_HDMI_HDCP_INT_CTRL,
  676. HDMI_HDCP_INT_CTRL_AUTH_SUCCESS_ACK |
  677. HDMI_HDCP_INT_CTRL_AUTH_SUCCESS_MASK |
  678. HDMI_HDCP_INT_CTRL_AUTH_FAIL_ACK |
  679. HDMI_HDCP_INT_CTRL_AUTH_FAIL_MASK |
  680. HDMI_HDCP_INT_CTRL_AUTH_FAIL_INFO_ACK);
  681. spin_unlock_irqrestore(&hdmi->reg_lock, flags);
  682. return 0;
  683. }
  684. /* read R0' from sink and pass it to HDCP engine */
  685. static int hdmi_hdcp_auth_part1_recv_r0(struct hdmi_hdcp_ctrl *hdcp_ctrl)
  686. {
  687. struct hdmi *hdmi = hdcp_ctrl->hdmi;
  688. int rc = 0;
  689. u8 buf[2];
  690. /*
  691. * HDCP Compliance Test case 1A-01:
  692. * Wait here at least 100ms before reading R0'
  693. */
  694. rc = hdmi_hdcp_msleep(hdcp_ctrl, 125, AUTH_ABORT_EV);
  695. if (rc)
  696. return rc;
  697. /* Read R0' at offset 0x08 */
  698. rc = hdmi_ddc_read(hdmi, HDCP_PORT_ADDR, 0x08, buf, 2);
  699. if (rc) {
  700. pr_err("%s:R0' read failed\n", __func__);
  701. return rc;
  702. }
  703. DBG("R0'=%02x%02x", buf[1], buf[0]);
  704. /* Write R0' to HDCP registers and check to see if it is a match */
  705. hdmi_write(hdmi, REG_HDMI_HDCP_RCVPORT_DATA2_0,
  706. (((u32)buf[1]) << 8) | buf[0]);
  707. return 0;
  708. }
  709. /* Wait for authenticating result: R0/R0' are matched or not */
  710. static int hdmi_hdcp_auth_part1_verify_r0(struct hdmi_hdcp_ctrl *hdcp_ctrl)
  711. {
  712. struct hdmi *hdmi = hdcp_ctrl->hdmi;
  713. u32 link0_status;
  714. int rc;
  715. /* wait for hdcp irq, 10 sec should be long enough */
  716. rc = hdmi_hdcp_msleep(hdcp_ctrl, 10000, AUTH_RESULT_RDY_EV);
  717. if (!rc) {
  718. pr_err("%s: Wait Auth IRQ timeout\n", __func__);
  719. return -ETIMEDOUT;
  720. }
  721. link0_status = hdmi_read(hdmi, REG_HDMI_HDCP_LINK0_STATUS);
  722. if (!(link0_status & HDMI_HDCP_LINK0_STATUS_RI_MATCHES)) {
  723. pr_err("%s: Authentication Part I failed\n", __func__);
  724. return -EINVAL;
  725. }
  726. /* Enable HDCP Encryption */
  727. hdmi_write(hdmi, REG_HDMI_HDCP_CTRL,
  728. HDMI_HDCP_CTRL_ENABLE |
  729. HDMI_HDCP_CTRL_ENCRYPTION_ENABLE);
  730. return 0;
  731. }
  732. static int hdmi_hdcp_recv_check_bstatus(struct hdmi_hdcp_ctrl *hdcp_ctrl,
  733. u16 *pbstatus)
  734. {
  735. int rc;
  736. struct hdmi *hdmi = hdcp_ctrl->hdmi;
  737. bool max_devs_exceeded = false, max_cascade_exceeded = false;
  738. u32 repeater_cascade_depth = 0, down_stream_devices = 0;
  739. u16 bstatus;
  740. u8 buf[2];
  741. /* Read BSTATUS at offset 0x41 */
  742. rc = hdmi_ddc_read(hdmi, HDCP_PORT_ADDR, 0x41, buf, 2);
  743. if (rc) {
  744. pr_err("%s: BSTATUS read failed\n", __func__);
  745. goto error;
  746. }
  747. *pbstatus = bstatus = (buf[1] << 8) | buf[0];
  748. down_stream_devices = bstatus & 0x7F;
  749. repeater_cascade_depth = (bstatus >> 8) & 0x7;
  750. max_devs_exceeded = (bstatus & BIT(7)) ? true : false;
  751. max_cascade_exceeded = (bstatus & BIT(11)) ? true : false;
  752. if (down_stream_devices == 0) {
  753. /*
  754. * If no downstream devices are attached to the repeater
  755. * then part II fails.
  756. * todo: The other approach would be to continue PART II.
  757. */
  758. pr_err("%s: No downstream devices\n", __func__);
  759. rc = -EINVAL;
  760. goto error;
  761. }
  762. /*
  763. * HDCP Compliance 1B-05:
  764. * Check if no. of devices connected to repeater
  765. * exceed max_devices_connected from bit 7 of Bstatus.
  766. */
  767. if (max_devs_exceeded) {
  768. pr_err("%s: no. of devs connected exceeds max allowed",
  769. __func__);
  770. rc = -EINVAL;
  771. goto error;
  772. }
  773. /*
  774. * HDCP Compliance 1B-06:
  775. * Check if no. of cascade connected to repeater
  776. * exceed max_cascade_connected from bit 11 of Bstatus.
  777. */
  778. if (max_cascade_exceeded) {
  779. pr_err("%s: no. of cascade conn exceeds max allowed",
  780. __func__);
  781. rc = -EINVAL;
  782. goto error;
  783. }
  784. error:
  785. hdcp_ctrl->dev_count = down_stream_devices;
  786. hdcp_ctrl->max_cascade_exceeded = max_cascade_exceeded;
  787. hdcp_ctrl->max_dev_exceeded = max_devs_exceeded;
  788. hdcp_ctrl->depth = repeater_cascade_depth;
  789. return rc;
  790. }
  791. static int hdmi_hdcp_auth_part2_wait_ksv_fifo_ready(
  792. struct hdmi_hdcp_ctrl *hdcp_ctrl)
  793. {
  794. int rc;
  795. struct hdmi *hdmi = hdcp_ctrl->hdmi;
  796. u32 reg, data;
  797. u32 timeout_count;
  798. u16 bstatus;
  799. u8 bcaps;
  800. /*
  801. * Wait until READY bit is set in BCAPS, as per HDCP specifications
  802. * maximum permitted time to check for READY bit is five seconds.
  803. */
  804. timeout_count = 100;
  805. do {
  806. /* Read BCAPS at offset 0x40 */
  807. rc = hdmi_ddc_read(hdmi, HDCP_PORT_ADDR, 0x40, &bcaps, 1);
  808. if (rc) {
  809. pr_err("%s: BCAPS read failed\n", __func__);
  810. return rc;
  811. }
  812. if (bcaps & BIT(5))
  813. break;
  814. timeout_count--;
  815. if (!timeout_count) {
  816. pr_err("%s: Wait KSV fifo ready timedout", __func__);
  817. return -ETIMEDOUT;
  818. }
  819. rc = hdmi_hdcp_msleep(hdcp_ctrl, 20, AUTH_ABORT_EV);
  820. if (rc)
  821. return rc;
  822. } while (1);
  823. rc = hdmi_hdcp_recv_check_bstatus(hdcp_ctrl, &bstatus);
  824. if (rc) {
  825. pr_err("%s: bstatus error\n", __func__);
  826. return rc;
  827. }
  828. /* Write BSTATUS and BCAPS to HDCP registers */
  829. reg = REG_HDMI_HDCP_RCVPORT_DATA12;
  830. data = bcaps | (bstatus << 8);
  831. rc = hdmi_hdcp_scm_wr(hdcp_ctrl, &reg, &data, 1);
  832. if (rc) {
  833. pr_err("%s: BSTATUS write failed\n", __func__);
  834. return rc;
  835. }
  836. return 0;
  837. }
  838. /*
  839. * hdcp authenticating part 2: 2nd
  840. * read ksv fifo from sink
  841. * transfer V' from sink to HDCP engine
  842. * reset SHA engine
  843. */
  844. static int hdmi_hdcp_transfer_v_h(struct hdmi_hdcp_ctrl *hdcp_ctrl)
  845. {
  846. struct hdmi *hdmi = hdcp_ctrl->hdmi;
  847. int rc = 0;
  848. struct hdmi_hdcp_reg_data reg_data[] = {
  849. {REG_HDMI_HDCP_RCVPORT_DATA7, 0x20, "V' H0"},
  850. {REG_HDMI_HDCP_RCVPORT_DATA8, 0x24, "V' H1"},
  851. {REG_HDMI_HDCP_RCVPORT_DATA9, 0x28, "V' H2"},
  852. {REG_HDMI_HDCP_RCVPORT_DATA10, 0x2C, "V' H3"},
  853. {REG_HDMI_HDCP_RCVPORT_DATA11, 0x30, "V' H4"},
  854. };
  855. struct hdmi_hdcp_reg_data *rd;
  856. u32 size = ARRAY_SIZE(reg_data);
  857. u32 reg[ARRAY_SIZE(reg_data)];
  858. u32 data[ARRAY_SIZE(reg_data)];
  859. int i;
  860. for (i = 0; i < size; i++) {
  861. rd = &reg_data[i];
  862. rc = hdmi_ddc_read(hdmi, HDCP_PORT_ADDR,
  863. rd->off, (u8 *)&data[i], (u16)sizeof(data[i]));
  864. if (rc) {
  865. pr_err("%s: Read %s failed\n", __func__, rd->name);
  866. goto error;
  867. }
  868. DBG("%s =%x", rd->name, data[i]);
  869. reg[i] = reg_data[i].reg_id;
  870. }
  871. rc = hdmi_hdcp_scm_wr(hdcp_ctrl, reg, data, size);
  872. error:
  873. return rc;
  874. }
  875. static int hdmi_hdcp_recv_ksv_fifo(struct hdmi_hdcp_ctrl *hdcp_ctrl)
  876. {
  877. int rc;
  878. struct hdmi *hdmi = hdcp_ctrl->hdmi;
  879. u32 ksv_bytes;
  880. ksv_bytes = 5 * hdcp_ctrl->dev_count;
  881. rc = hdmi_ddc_read(hdmi, HDCP_PORT_ADDR, 0x43,
  882. hdcp_ctrl->ksv_list, ksv_bytes);
  883. if (rc)
  884. pr_err("%s: KSV FIFO read failed\n", __func__);
  885. return rc;
  886. }
  887. static int hdmi_hdcp_reset_sha_engine(struct hdmi_hdcp_ctrl *hdcp_ctrl)
  888. {
  889. u32 reg[2], data[2];
  890. u32 rc = 0;
  891. reg[0] = REG_HDMI_HDCP_SHA_CTRL;
  892. data[0] = HDCP_REG_ENABLE;
  893. reg[1] = REG_HDMI_HDCP_SHA_CTRL;
  894. data[1] = HDCP_REG_DISABLE;
  895. rc = hdmi_hdcp_scm_wr(hdcp_ctrl, reg, data, 2);
  896. return rc;
  897. }
  898. static int hdmi_hdcp_auth_part2_recv_ksv_fifo(
  899. struct hdmi_hdcp_ctrl *hdcp_ctrl)
  900. {
  901. int rc;
  902. u32 timeout_count;
  903. /*
  904. * Read KSV FIFO over DDC
  905. * Key Selection vector FIFO Used to pull downstream KSVs
  906. * from HDCP Repeaters.
  907. * All bytes (DEVICE_COUNT * 5) must be read in a single,
  908. * auto incrementing access.
  909. * All bytes read as 0x00 for HDCP Receivers that are not
  910. * HDCP Repeaters (REPEATER == 0).
  911. */
  912. timeout_count = 100;
  913. do {
  914. rc = hdmi_hdcp_recv_ksv_fifo(hdcp_ctrl);
  915. if (!rc)
  916. break;
  917. timeout_count--;
  918. if (!timeout_count) {
  919. pr_err("%s: Recv ksv fifo timedout", __func__);
  920. return -ETIMEDOUT;
  921. }
  922. rc = hdmi_hdcp_msleep(hdcp_ctrl, 25, AUTH_ABORT_EV);
  923. if (rc)
  924. return rc;
  925. } while (1);
  926. rc = hdmi_hdcp_transfer_v_h(hdcp_ctrl);
  927. if (rc) {
  928. pr_err("%s: transfer V failed\n", __func__);
  929. return rc;
  930. }
  931. /* reset SHA engine before write ksv fifo */
  932. rc = hdmi_hdcp_reset_sha_engine(hdcp_ctrl);
  933. if (rc) {
  934. pr_err("%s: fail to reset sha engine\n", __func__);
  935. return rc;
  936. }
  937. return 0;
  938. }
  939. /*
  940. * Write KSV FIFO to HDCP_SHA_DATA.
  941. * This is done 1 byte at time starting with the LSB.
  942. * Once 64 bytes have been written, we need to poll for
  943. * HDCP_SHA_BLOCK_DONE before writing any further
  944. * If the last byte is written, we need to poll for
  945. * HDCP_SHA_COMP_DONE to wait until HW finish
  946. */
  947. static int hdmi_hdcp_write_ksv_fifo(struct hdmi_hdcp_ctrl *hdcp_ctrl)
  948. {
  949. int i;
  950. struct hdmi *hdmi = hdcp_ctrl->hdmi;
  951. u32 ksv_bytes, last_byte = 0;
  952. u8 *ksv_fifo = NULL;
  953. u32 reg_val, data, reg;
  954. u32 rc = 0;
  955. ksv_bytes = 5 * hdcp_ctrl->dev_count;
  956. /* Check if need to wait for HW completion */
  957. if (hdcp_ctrl->ksv_fifo_w_index) {
  958. reg_val = hdmi_read(hdmi, REG_HDMI_HDCP_SHA_STATUS);
  959. DBG("HDCP_SHA_STATUS=%08x", reg_val);
  960. if (hdcp_ctrl->ksv_fifo_w_index == ksv_bytes) {
  961. /* check COMP_DONE if last write */
  962. if (reg_val & HDMI_HDCP_SHA_STATUS_COMP_DONE) {
  963. DBG("COMP_DONE");
  964. return 0;
  965. } else {
  966. return -EAGAIN;
  967. }
  968. } else {
  969. /* check BLOCK_DONE if not last write */
  970. if (!(reg_val & HDMI_HDCP_SHA_STATUS_BLOCK_DONE))
  971. return -EAGAIN;
  972. DBG("BLOCK_DONE");
  973. }
  974. }
  975. ksv_bytes -= hdcp_ctrl->ksv_fifo_w_index;
  976. if (ksv_bytes <= 64)
  977. last_byte = 1;
  978. else
  979. ksv_bytes = 64;
  980. ksv_fifo = hdcp_ctrl->ksv_list;
  981. ksv_fifo += hdcp_ctrl->ksv_fifo_w_index;
  982. for (i = 0; i < ksv_bytes; i++) {
  983. /* Write KSV byte and set DONE bit[0] for last byte*/
  984. reg_val = ksv_fifo[i] << 16;
  985. if ((i == (ksv_bytes - 1)) && last_byte)
  986. reg_val |= HDMI_HDCP_SHA_DATA_DONE;
  987. reg = REG_HDMI_HDCP_SHA_DATA;
  988. data = reg_val;
  989. rc = hdmi_hdcp_scm_wr(hdcp_ctrl, &reg, &data, 1);
  990. if (rc)
  991. return rc;
  992. }
  993. hdcp_ctrl->ksv_fifo_w_index += ksv_bytes;
  994. /*
  995. *return -EAGAIN to notify caller to wait for COMP_DONE or BLOCK_DONE
  996. */
  997. return -EAGAIN;
  998. }
  999. /* write ksv fifo into HDCP engine */
  1000. static int hdmi_hdcp_auth_part2_write_ksv_fifo(
  1001. struct hdmi_hdcp_ctrl *hdcp_ctrl)
  1002. {
  1003. int rc;
  1004. u32 timeout_count;
  1005. hdcp_ctrl->ksv_fifo_w_index = 0;
  1006. timeout_count = 100;
  1007. do {
  1008. rc = hdmi_hdcp_write_ksv_fifo(hdcp_ctrl);
  1009. if (!rc)
  1010. break;
  1011. if (rc != -EAGAIN)
  1012. return rc;
  1013. timeout_count--;
  1014. if (!timeout_count) {
  1015. pr_err("%s: Write KSV fifo timedout", __func__);
  1016. return -ETIMEDOUT;
  1017. }
  1018. rc = hdmi_hdcp_msleep(hdcp_ctrl, 20, AUTH_ABORT_EV);
  1019. if (rc)
  1020. return rc;
  1021. } while (1);
  1022. return 0;
  1023. }
  1024. static int hdmi_hdcp_auth_part2_check_v_match(struct hdmi_hdcp_ctrl *hdcp_ctrl)
  1025. {
  1026. int rc = 0;
  1027. struct hdmi *hdmi = hdcp_ctrl->hdmi;
  1028. u32 link0_status;
  1029. u32 timeout_count = 100;
  1030. do {
  1031. link0_status = hdmi_read(hdmi, REG_HDMI_HDCP_LINK0_STATUS);
  1032. if (link0_status & HDMI_HDCP_LINK0_STATUS_V_MATCHES)
  1033. break;
  1034. timeout_count--;
  1035. if (!timeout_count) {
  1036. pr_err("%s: HDCP V Match timedout", __func__);
  1037. return -ETIMEDOUT;
  1038. }
  1039. rc = hdmi_hdcp_msleep(hdcp_ctrl, 20, AUTH_ABORT_EV);
  1040. if (rc)
  1041. return rc;
  1042. } while (1);
  1043. return 0;
  1044. }
  1045. static void hdmi_hdcp_auth_work(struct work_struct *work)
  1046. {
  1047. struct hdmi_hdcp_ctrl *hdcp_ctrl = container_of(work,
  1048. struct hdmi_hdcp_ctrl, hdcp_auth_work);
  1049. int rc;
  1050. rc = hdmi_hdcp_auth_prepare(hdcp_ctrl);
  1051. if (rc) {
  1052. pr_err("%s: auth prepare failed %d\n", __func__, rc);
  1053. goto end;
  1054. }
  1055. /* HDCP PartI */
  1056. rc = hdmi_hdcp_auth_part1_key_exchange(hdcp_ctrl);
  1057. if (rc) {
  1058. pr_err("%s: key exchange failed %d\n", __func__, rc);
  1059. goto end;
  1060. }
  1061. rc = hdmi_hdcp_auth_part1_recv_r0(hdcp_ctrl);
  1062. if (rc) {
  1063. pr_err("%s: receive r0 failed %d\n", __func__, rc);
  1064. goto end;
  1065. }
  1066. rc = hdmi_hdcp_auth_part1_verify_r0(hdcp_ctrl);
  1067. if (rc) {
  1068. pr_err("%s: verify r0 failed %d\n", __func__, rc);
  1069. goto end;
  1070. }
  1071. pr_info("%s: Authentication Part I successful\n", __func__);
  1072. if (hdcp_ctrl->ds_type == DS_RECEIVER)
  1073. goto end;
  1074. /* HDCP PartII */
  1075. rc = hdmi_hdcp_auth_part2_wait_ksv_fifo_ready(hdcp_ctrl);
  1076. if (rc) {
  1077. pr_err("%s: wait ksv fifo ready failed %d\n", __func__, rc);
  1078. goto end;
  1079. }
  1080. rc = hdmi_hdcp_auth_part2_recv_ksv_fifo(hdcp_ctrl);
  1081. if (rc) {
  1082. pr_err("%s: recv ksv fifo failed %d\n", __func__, rc);
  1083. goto end;
  1084. }
  1085. rc = hdmi_hdcp_auth_part2_write_ksv_fifo(hdcp_ctrl);
  1086. if (rc) {
  1087. pr_err("%s: write ksv fifo failed %d\n", __func__, rc);
  1088. goto end;
  1089. }
  1090. rc = hdmi_hdcp_auth_part2_check_v_match(hdcp_ctrl);
  1091. if (rc)
  1092. pr_err("%s: check v match failed %d\n", __func__, rc);
  1093. end:
  1094. if (rc == -ECANCELED) {
  1095. pr_info("%s: hdcp authentication canceled\n", __func__);
  1096. } else if (rc == -ENOTSUPP) {
  1097. pr_info("%s: hdcp is not supported\n", __func__);
  1098. } else if (rc) {
  1099. pr_err("%s: hdcp authentication failed\n", __func__);
  1100. hdmi_hdcp_auth_fail(hdcp_ctrl);
  1101. } else {
  1102. hdmi_hdcp_auth_done(hdcp_ctrl);
  1103. }
  1104. }
  1105. void hdmi_hdcp_on(struct hdmi_hdcp_ctrl *hdcp_ctrl)
  1106. {
  1107. struct hdmi *hdmi = hdcp_ctrl->hdmi;
  1108. u32 reg_val;
  1109. unsigned long flags;
  1110. if ((HDCP_STATE_INACTIVE != hdcp_ctrl->hdcp_state) ||
  1111. (HDCP_STATE_NO_AKSV == hdcp_ctrl->hdcp_state)) {
  1112. DBG("still active or activating or no askv. returning");
  1113. return;
  1114. }
  1115. /* clear HDMI Encrypt */
  1116. spin_lock_irqsave(&hdmi->reg_lock, flags);
  1117. reg_val = hdmi_read(hdmi, REG_HDMI_CTRL);
  1118. reg_val &= ~HDMI_CTRL_ENCRYPTED;
  1119. hdmi_write(hdmi, REG_HDMI_CTRL, reg_val);
  1120. spin_unlock_irqrestore(&hdmi->reg_lock, flags);
  1121. hdcp_ctrl->auth_event = 0;
  1122. hdcp_ctrl->hdcp_state = HDCP_STATE_AUTHENTICATING;
  1123. hdcp_ctrl->auth_retries = 0;
  1124. queue_work(hdmi->workq, &hdcp_ctrl->hdcp_auth_work);
  1125. }
  1126. void hdmi_hdcp_off(struct hdmi_hdcp_ctrl *hdcp_ctrl)
  1127. {
  1128. struct hdmi *hdmi = hdcp_ctrl->hdmi;
  1129. unsigned long flags;
  1130. u32 reg_val;
  1131. if ((HDCP_STATE_INACTIVE == hdcp_ctrl->hdcp_state) ||
  1132. (HDCP_STATE_NO_AKSV == hdcp_ctrl->hdcp_state)) {
  1133. DBG("hdcp inactive or no aksv. returning");
  1134. return;
  1135. }
  1136. /*
  1137. * Disable HPD circuitry.
  1138. * This is needed to reset the HDCP cipher engine so that when we
  1139. * attempt a re-authentication, HW would clear the AN0_READY and
  1140. * AN1_READY bits in HDMI_HDCP_LINK0_STATUS register
  1141. */
  1142. spin_lock_irqsave(&hdmi->reg_lock, flags);
  1143. reg_val = hdmi_read(hdmi, REG_HDMI_HPD_CTRL);
  1144. reg_val &= ~HDMI_HPD_CTRL_ENABLE;
  1145. hdmi_write(hdmi, REG_HDMI_HPD_CTRL, reg_val);
  1146. /*
  1147. * Disable HDCP interrupts.
  1148. * Also, need to set the state to inactive here so that any ongoing
  1149. * reauth works will know that the HDCP session has been turned off.
  1150. */
  1151. hdmi_write(hdmi, REG_HDMI_HDCP_INT_CTRL, 0);
  1152. spin_unlock_irqrestore(&hdmi->reg_lock, flags);
  1153. /*
  1154. * Cancel any pending auth/reauth attempts.
  1155. * If one is ongoing, this will wait for it to finish.
  1156. * No more reauthentication attempts will be scheduled since we
  1157. * set the current state to inactive.
  1158. */
  1159. set_bit(AUTH_ABORT_EV, &hdcp_ctrl->auth_event);
  1160. wake_up_all(&hdcp_ctrl->auth_event_queue);
  1161. cancel_work_sync(&hdcp_ctrl->hdcp_auth_work);
  1162. cancel_work_sync(&hdcp_ctrl->hdcp_reauth_work);
  1163. hdmi_write(hdmi, REG_HDMI_HDCP_RESET,
  1164. HDMI_HDCP_RESET_LINK0_DEAUTHENTICATE);
  1165. /* Disable encryption and disable the HDCP block */
  1166. hdmi_write(hdmi, REG_HDMI_HDCP_CTRL, 0);
  1167. spin_lock_irqsave(&hdmi->reg_lock, flags);
  1168. reg_val = hdmi_read(hdmi, REG_HDMI_CTRL);
  1169. reg_val &= ~HDMI_CTRL_ENCRYPTED;
  1170. hdmi_write(hdmi, REG_HDMI_CTRL, reg_val);
  1171. /* Enable HPD circuitry */
  1172. reg_val = hdmi_read(hdmi, REG_HDMI_HPD_CTRL);
  1173. reg_val |= HDMI_HPD_CTRL_ENABLE;
  1174. hdmi_write(hdmi, REG_HDMI_HPD_CTRL, reg_val);
  1175. spin_unlock_irqrestore(&hdmi->reg_lock, flags);
  1176. hdcp_ctrl->hdcp_state = HDCP_STATE_INACTIVE;
  1177. DBG("HDCP: Off");
  1178. }
  1179. struct hdmi_hdcp_ctrl *hdmi_hdcp_init(struct hdmi *hdmi)
  1180. {
  1181. struct hdmi_hdcp_ctrl *hdcp_ctrl = NULL;
  1182. if (!hdmi->qfprom_mmio) {
  1183. pr_err("%s: HDCP is not supported without qfprom\n",
  1184. __func__);
  1185. return ERR_PTR(-EINVAL);
  1186. }
  1187. hdcp_ctrl = kzalloc(sizeof(*hdcp_ctrl), GFP_KERNEL);
  1188. if (!hdcp_ctrl)
  1189. return ERR_PTR(-ENOMEM);
  1190. INIT_WORK(&hdcp_ctrl->hdcp_auth_work, hdmi_hdcp_auth_work);
  1191. INIT_WORK(&hdcp_ctrl->hdcp_reauth_work, hdmi_hdcp_reauth_work);
  1192. init_waitqueue_head(&hdcp_ctrl->auth_event_queue);
  1193. hdcp_ctrl->hdmi = hdmi;
  1194. hdcp_ctrl->hdcp_state = HDCP_STATE_INACTIVE;
  1195. hdcp_ctrl->aksv_valid = false;
  1196. if (qcom_scm_hdcp_available())
  1197. hdcp_ctrl->tz_hdcp = true;
  1198. else
  1199. hdcp_ctrl->tz_hdcp = false;
  1200. return hdcp_ctrl;
  1201. }
  1202. void hdmi_hdcp_destroy(struct hdmi *hdmi)
  1203. {
  1204. if (hdmi && hdmi->hdcp_ctrl) {
  1205. kfree(hdmi->hdcp_ctrl);
  1206. hdmi->hdcp_ctrl = NULL;
  1207. }
  1208. }