s5p_mfc_ctrl.c 13 KB

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