s5p_mfc_ctrl.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * linux/drivers/media/platform/s5p-mfc/s5p_mfc_ctrl.c
  4. *
  5. * Copyright (c) 2010 Samsung Electronics Co., Ltd.
  6. * http://www.samsung.com/
  7. */
  8. #include <linux/delay.h>
  9. #include <linux/err.h>
  10. #include <linux/firmware.h>
  11. #include <linux/jiffies.h>
  12. #include <linux/sched.h>
  13. #include "s5p_mfc_cmd.h"
  14. #include "s5p_mfc_common.h"
  15. #include "s5p_mfc_debug.h"
  16. #include "s5p_mfc_intr.h"
  17. #include "s5p_mfc_opr.h"
  18. #include "s5p_mfc_pm.h"
  19. #include "s5p_mfc_ctrl.h"
  20. /* Allocate memory for firmware */
  21. int s5p_mfc_alloc_firmware(struct s5p_mfc_dev *dev)
  22. {
  23. struct s5p_mfc_priv_buf *fw_buf = &dev->fw_buf;
  24. int err;
  25. fw_buf->size = dev->variant->buf_size->fw;
  26. if (fw_buf->virt) {
  27. mfc_err("Attempting to allocate firmware when it seems that it is already loaded\n");
  28. return -ENOMEM;
  29. }
  30. err = s5p_mfc_alloc_priv_buf(dev, BANK_L_CTX, &dev->fw_buf);
  31. if (err) {
  32. mfc_err("Allocating bitprocessor buffer failed\n");
  33. return err;
  34. }
  35. return 0;
  36. }
  37. /* Load firmware */
  38. int s5p_mfc_load_firmware(struct s5p_mfc_dev *dev)
  39. {
  40. struct firmware *fw_blob;
  41. int i, err = -EINVAL;
  42. /* Firmware has to be present as a separate file or compiled
  43. * into kernel. */
  44. mfc_debug_enter();
  45. if (dev->fw_get_done)
  46. return 0;
  47. for (i = MFC_FW_MAX_VERSIONS - 1; i >= 0; i--) {
  48. if (!dev->variant->fw_name[i])
  49. continue;
  50. err = request_firmware((const struct firmware **)&fw_blob,
  51. dev->variant->fw_name[i], &dev->plat_dev->dev);
  52. if (!err) {
  53. dev->fw_ver = (enum s5p_mfc_fw_ver) i;
  54. break;
  55. }
  56. }
  57. if (err != 0) {
  58. mfc_err("Firmware is not present in the /lib/firmware directory nor compiled in kernel\n");
  59. return -EINVAL;
  60. }
  61. if (fw_blob->size > dev->fw_buf.size) {
  62. mfc_err("MFC firmware is too big to be loaded\n");
  63. release_firmware(fw_blob);
  64. return -ENOMEM;
  65. }
  66. memcpy(dev->fw_buf.virt, fw_blob->data, fw_blob->size);
  67. wmb();
  68. dev->fw_get_done = true;
  69. release_firmware(fw_blob);
  70. mfc_debug_leave();
  71. return 0;
  72. }
  73. /* Release firmware memory */
  74. int s5p_mfc_release_firmware(struct s5p_mfc_dev *dev)
  75. {
  76. /* Before calling this function one has to make sure
  77. * that MFC is no longer processing */
  78. s5p_mfc_release_priv_buf(dev, &dev->fw_buf);
  79. dev->fw_get_done = false;
  80. return 0;
  81. }
  82. static int s5p_mfc_bus_reset(struct s5p_mfc_dev *dev)
  83. {
  84. unsigned int status;
  85. unsigned long timeout;
  86. /* Reset */
  87. mfc_write(dev, 0x1, S5P_FIMV_MFC_BUS_RESET_CTRL);
  88. timeout = jiffies + msecs_to_jiffies(MFC_BW_TIMEOUT);
  89. /* Check bus status */
  90. do {
  91. if (time_after(jiffies, timeout)) {
  92. mfc_err("Timeout while resetting MFC.\n");
  93. return -EIO;
  94. }
  95. status = mfc_read(dev, S5P_FIMV_MFC_BUS_RESET_CTRL);
  96. } while ((status & 0x2) == 0);
  97. return 0;
  98. }
  99. /* Reset the device */
  100. int s5p_mfc_reset(struct s5p_mfc_dev *dev)
  101. {
  102. unsigned int mc_status;
  103. unsigned long timeout;
  104. int i;
  105. mfc_debug_enter();
  106. if (IS_MFCV6_PLUS(dev)) {
  107. /* Zero Initialization of MFC registers */
  108. mfc_write(dev, 0, S5P_FIMV_RISC2HOST_CMD_V6);
  109. mfc_write(dev, 0, S5P_FIMV_HOST2RISC_CMD_V6);
  110. mfc_write(dev, 0, S5P_FIMV_FW_VERSION_V6);
  111. for (i = 0; i < S5P_FIMV_REG_CLEAR_COUNT_V6; i++)
  112. mfc_write(dev, 0, S5P_FIMV_REG_CLEAR_BEGIN_V6 + (i*4));
  113. /* check bus reset control before reset */
  114. if (dev->risc_on)
  115. if (s5p_mfc_bus_reset(dev))
  116. return -EIO;
  117. /* Reset
  118. * set RISC_ON to 0 during power_on & wake_up.
  119. * V6 needs RISC_ON set to 0 during reset also.
  120. */
  121. if ((!dev->risc_on) || (!IS_MFCV7_PLUS(dev)))
  122. mfc_write(dev, 0, S5P_FIMV_RISC_ON_V6);
  123. mfc_write(dev, 0x1FFF, S5P_FIMV_MFC_RESET_V6);
  124. mfc_write(dev, 0, S5P_FIMV_MFC_RESET_V6);
  125. } else {
  126. /* Stop procedure */
  127. /* reset RISC */
  128. mfc_write(dev, 0x3f6, S5P_FIMV_SW_RESET);
  129. /* All reset except for MC */
  130. mfc_write(dev, 0x3e2, S5P_FIMV_SW_RESET);
  131. mdelay(10);
  132. timeout = jiffies + msecs_to_jiffies(MFC_BW_TIMEOUT);
  133. /* Check MC status */
  134. do {
  135. if (time_after(jiffies, timeout)) {
  136. mfc_err("Timeout while resetting MFC\n");
  137. return -EIO;
  138. }
  139. mc_status = mfc_read(dev, S5P_FIMV_MC_STATUS);
  140. } while (mc_status & 0x3);
  141. mfc_write(dev, 0x0, S5P_FIMV_SW_RESET);
  142. mfc_write(dev, 0x3fe, S5P_FIMV_SW_RESET);
  143. }
  144. mfc_debug_leave();
  145. return 0;
  146. }
  147. static inline void s5p_mfc_init_memctrl(struct s5p_mfc_dev *dev)
  148. {
  149. if (IS_MFCV6_PLUS(dev)) {
  150. mfc_write(dev, dev->dma_base[BANK_L_CTX],
  151. S5P_FIMV_RISC_BASE_ADDRESS_V6);
  152. mfc_debug(2, "Base Address : %pad\n",
  153. &dev->dma_base[BANK_L_CTX]);
  154. } else {
  155. mfc_write(dev, dev->dma_base[BANK_L_CTX],
  156. S5P_FIMV_MC_DRAMBASE_ADR_A);
  157. mfc_write(dev, dev->dma_base[BANK_R_CTX],
  158. S5P_FIMV_MC_DRAMBASE_ADR_B);
  159. mfc_debug(2, "Bank1: %pad, Bank2: %pad\n",
  160. &dev->dma_base[BANK_L_CTX],
  161. &dev->dma_base[BANK_R_CTX]);
  162. }
  163. }
  164. static inline void s5p_mfc_clear_cmds(struct s5p_mfc_dev *dev)
  165. {
  166. if (IS_MFCV6_PLUS(dev)) {
  167. /* Zero initialization should be done before RESET.
  168. * Nothing to do here. */
  169. } else {
  170. mfc_write(dev, 0xffffffff, S5P_FIMV_SI_CH0_INST_ID);
  171. mfc_write(dev, 0xffffffff, S5P_FIMV_SI_CH1_INST_ID);
  172. mfc_write(dev, 0, S5P_FIMV_RISC2HOST_CMD);
  173. mfc_write(dev, 0, S5P_FIMV_HOST2RISC_CMD);
  174. }
  175. }
  176. /* Initialize hardware */
  177. int s5p_mfc_init_hw(struct s5p_mfc_dev *dev)
  178. {
  179. unsigned int ver;
  180. int ret;
  181. mfc_debug_enter();
  182. if (!dev->fw_buf.virt) {
  183. mfc_err("Firmware memory is not allocated.\n");
  184. return -EINVAL;
  185. }
  186. /* 0. MFC reset */
  187. mfc_debug(2, "MFC reset..\n");
  188. s5p_mfc_clock_on();
  189. dev->risc_on = 0;
  190. ret = s5p_mfc_reset(dev);
  191. if (ret) {
  192. mfc_err("Failed to reset MFC - timeout\n");
  193. return ret;
  194. }
  195. mfc_debug(2, "Done MFC reset..\n");
  196. /* 1. Set DRAM base Addr */
  197. s5p_mfc_init_memctrl(dev);
  198. /* 2. Initialize registers of channel I/F */
  199. s5p_mfc_clear_cmds(dev);
  200. /* 3. Release reset signal to the RISC */
  201. s5p_mfc_clean_dev_int_flags(dev);
  202. if (IS_MFCV6_PLUS(dev)) {
  203. dev->risc_on = 1;
  204. mfc_write(dev, 0x1, S5P_FIMV_RISC_ON_V6);
  205. }
  206. else
  207. mfc_write(dev, 0x3ff, S5P_FIMV_SW_RESET);
  208. if (IS_MFCV10(dev))
  209. mfc_write(dev, 0x0, S5P_FIMV_MFC_CLOCK_OFF_V10);
  210. mfc_debug(2, "Will now wait for completion of firmware transfer\n");
  211. if (s5p_mfc_wait_for_done_dev(dev, S5P_MFC_R2H_CMD_FW_STATUS_RET)) {
  212. mfc_err("Failed to load firmware\n");
  213. s5p_mfc_reset(dev);
  214. s5p_mfc_clock_off();
  215. return -EIO;
  216. }
  217. s5p_mfc_clean_dev_int_flags(dev);
  218. /* 4. Initialize firmware */
  219. ret = s5p_mfc_hw_call(dev->mfc_cmds, sys_init_cmd, dev);
  220. if (ret) {
  221. mfc_err("Failed to send command to MFC - timeout\n");
  222. s5p_mfc_reset(dev);
  223. s5p_mfc_clock_off();
  224. return ret;
  225. }
  226. mfc_debug(2, "Ok, now will wait for completion of hardware init\n");
  227. if (s5p_mfc_wait_for_done_dev(dev, S5P_MFC_R2H_CMD_SYS_INIT_RET)) {
  228. mfc_err("Failed to init hardware\n");
  229. s5p_mfc_reset(dev);
  230. s5p_mfc_clock_off();
  231. return -EIO;
  232. }
  233. dev->int_cond = 0;
  234. if (dev->int_err != 0 || dev->int_type !=
  235. S5P_MFC_R2H_CMD_SYS_INIT_RET) {
  236. /* Failure. */
  237. mfc_err("Failed to init firmware - error: %d int: %d\n",
  238. dev->int_err, dev->int_type);
  239. s5p_mfc_reset(dev);
  240. s5p_mfc_clock_off();
  241. return -EIO;
  242. }
  243. if (IS_MFCV6_PLUS(dev))
  244. ver = mfc_read(dev, S5P_FIMV_FW_VERSION_V6);
  245. else
  246. ver = mfc_read(dev, S5P_FIMV_FW_VERSION);
  247. mfc_debug(2, "MFC F/W version : %02xyy, %02xmm, %02xdd\n",
  248. (ver >> 16) & 0xFF, (ver >> 8) & 0xFF, ver & 0xFF);
  249. s5p_mfc_clock_off();
  250. mfc_debug_leave();
  251. return 0;
  252. }
  253. /* Deinitialize hardware */
  254. void s5p_mfc_deinit_hw(struct s5p_mfc_dev *dev)
  255. {
  256. s5p_mfc_clock_on();
  257. s5p_mfc_reset(dev);
  258. s5p_mfc_hw_call(dev->mfc_ops, release_dev_context_buffer, dev);
  259. s5p_mfc_clock_off();
  260. }
  261. int s5p_mfc_sleep(struct s5p_mfc_dev *dev)
  262. {
  263. int ret;
  264. mfc_debug_enter();
  265. s5p_mfc_clock_on();
  266. s5p_mfc_clean_dev_int_flags(dev);
  267. ret = s5p_mfc_hw_call(dev->mfc_cmds, sleep_cmd, dev);
  268. if (ret) {
  269. mfc_err("Failed to send command to MFC - timeout\n");
  270. return ret;
  271. }
  272. if (s5p_mfc_wait_for_done_dev(dev, S5P_MFC_R2H_CMD_SLEEP_RET)) {
  273. mfc_err("Failed to sleep\n");
  274. return -EIO;
  275. }
  276. s5p_mfc_clock_off();
  277. dev->int_cond = 0;
  278. if (dev->int_err != 0 || dev->int_type !=
  279. S5P_MFC_R2H_CMD_SLEEP_RET) {
  280. /* Failure. */
  281. mfc_err("Failed to sleep - error: %d int: %d\n", dev->int_err,
  282. dev->int_type);
  283. return -EIO;
  284. }
  285. mfc_debug_leave();
  286. return ret;
  287. }
  288. static int s5p_mfc_v8_wait_wakeup(struct s5p_mfc_dev *dev)
  289. {
  290. int ret;
  291. /* Release reset signal to the RISC */
  292. dev->risc_on = 1;
  293. mfc_write(dev, 0x1, S5P_FIMV_RISC_ON_V6);
  294. if (s5p_mfc_wait_for_done_dev(dev, S5P_MFC_R2H_CMD_FW_STATUS_RET)) {
  295. mfc_err("Failed to reset MFCV8\n");
  296. return -EIO;
  297. }
  298. mfc_debug(2, "Write command to wakeup MFCV8\n");
  299. ret = s5p_mfc_hw_call(dev->mfc_cmds, wakeup_cmd, dev);
  300. if (ret) {
  301. mfc_err("Failed to send command to MFCV8 - timeout\n");
  302. return ret;
  303. }
  304. if (s5p_mfc_wait_for_done_dev(dev, S5P_MFC_R2H_CMD_WAKEUP_RET)) {
  305. mfc_err("Failed to wakeup MFC\n");
  306. return -EIO;
  307. }
  308. return ret;
  309. }
  310. static int s5p_mfc_wait_wakeup(struct s5p_mfc_dev *dev)
  311. {
  312. int ret;
  313. /* Send MFC wakeup command */
  314. ret = s5p_mfc_hw_call(dev->mfc_cmds, wakeup_cmd, dev);
  315. if (ret) {
  316. mfc_err("Failed to send command to MFC - timeout\n");
  317. return ret;
  318. }
  319. /* Release reset signal to the RISC */
  320. if (IS_MFCV6_PLUS(dev)) {
  321. dev->risc_on = 1;
  322. mfc_write(dev, 0x1, S5P_FIMV_RISC_ON_V6);
  323. } else {
  324. mfc_write(dev, 0x3ff, S5P_FIMV_SW_RESET);
  325. }
  326. if (s5p_mfc_wait_for_done_dev(dev, S5P_MFC_R2H_CMD_WAKEUP_RET)) {
  327. mfc_err("Failed to wakeup MFC\n");
  328. return -EIO;
  329. }
  330. return ret;
  331. }
  332. int s5p_mfc_wakeup(struct s5p_mfc_dev *dev)
  333. {
  334. int ret;
  335. mfc_debug_enter();
  336. /* 0. MFC reset */
  337. mfc_debug(2, "MFC reset..\n");
  338. s5p_mfc_clock_on();
  339. dev->risc_on = 0;
  340. ret = s5p_mfc_reset(dev);
  341. if (ret) {
  342. mfc_err("Failed to reset MFC - timeout\n");
  343. s5p_mfc_clock_off();
  344. return ret;
  345. }
  346. mfc_debug(2, "Done MFC reset..\n");
  347. /* 1. Set DRAM base Addr */
  348. s5p_mfc_init_memctrl(dev);
  349. /* 2. Initialize registers of channel I/F */
  350. s5p_mfc_clear_cmds(dev);
  351. s5p_mfc_clean_dev_int_flags(dev);
  352. /* 3. Send MFC wakeup command and wait for completion*/
  353. if (IS_MFCV8_PLUS(dev))
  354. ret = s5p_mfc_v8_wait_wakeup(dev);
  355. else
  356. ret = s5p_mfc_wait_wakeup(dev);
  357. s5p_mfc_clock_off();
  358. if (ret)
  359. return ret;
  360. dev->int_cond = 0;
  361. if (dev->int_err != 0 || dev->int_type !=
  362. S5P_MFC_R2H_CMD_WAKEUP_RET) {
  363. /* Failure. */
  364. mfc_err("Failed to wakeup - error: %d int: %d\n", dev->int_err,
  365. dev->int_type);
  366. return -EIO;
  367. }
  368. mfc_debug_leave();
  369. return 0;
  370. }
  371. int s5p_mfc_open_mfc_inst(struct s5p_mfc_dev *dev, struct s5p_mfc_ctx *ctx)
  372. {
  373. int ret = 0;
  374. ret = s5p_mfc_hw_call(dev->mfc_ops, alloc_instance_buffer, ctx);
  375. if (ret) {
  376. mfc_err("Failed allocating instance buffer\n");
  377. goto err;
  378. }
  379. if (ctx->type == MFCINST_DECODER) {
  380. ret = s5p_mfc_hw_call(dev->mfc_ops,
  381. alloc_dec_temp_buffers, ctx);
  382. if (ret) {
  383. mfc_err("Failed allocating temporary buffers\n");
  384. goto err_free_inst_buf;
  385. }
  386. }
  387. set_work_bit_irqsave(ctx);
  388. s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
  389. if (s5p_mfc_wait_for_done_ctx(ctx,
  390. S5P_MFC_R2H_CMD_OPEN_INSTANCE_RET, 0)) {
  391. /* Error or timeout */
  392. mfc_err("Error getting instance from hardware\n");
  393. ret = -EIO;
  394. goto err_free_desc_buf;
  395. }
  396. mfc_debug(2, "Got instance number: %d\n", ctx->inst_no);
  397. return ret;
  398. err_free_desc_buf:
  399. if (ctx->type == MFCINST_DECODER)
  400. s5p_mfc_hw_call(dev->mfc_ops, release_dec_desc_buffer, ctx);
  401. err_free_inst_buf:
  402. s5p_mfc_hw_call(dev->mfc_ops, release_instance_buffer, ctx);
  403. err:
  404. return ret;
  405. }
  406. void s5p_mfc_close_mfc_inst(struct s5p_mfc_dev *dev, struct s5p_mfc_ctx *ctx)
  407. {
  408. ctx->state = MFCINST_RETURN_INST;
  409. set_work_bit_irqsave(ctx);
  410. s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
  411. /* Wait until instance is returned or timeout occurred */
  412. if (s5p_mfc_wait_for_done_ctx(ctx,
  413. S5P_MFC_R2H_CMD_CLOSE_INSTANCE_RET, 0))
  414. mfc_err("Err returning instance\n");
  415. /* Free resources */
  416. s5p_mfc_hw_call(dev->mfc_ops, release_codec_buffers, ctx);
  417. s5p_mfc_hw_call(dev->mfc_ops, release_instance_buffer, ctx);
  418. if (ctx->type == MFCINST_DECODER)
  419. s5p_mfc_hw_call(dev->mfc_ops, release_dec_desc_buffer, ctx);
  420. ctx->inst_no = MFC_NO_INSTANCE_SET;
  421. ctx->state = MFCINST_FREE;
  422. }