ivtv-firmware.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. ivtv firmware functions.
  4. Copyright (C) 2003-2004 Kevin Thayer <nufan_wfk at yahoo.com>
  5. Copyright (C) 2004 Chris Kennedy <[email protected]>
  6. Copyright (C) 2005-2007 Hans Verkuil <[email protected]>
  7. */
  8. #include "ivtv-driver.h"
  9. #include "ivtv-mailbox.h"
  10. #include "ivtv-firmware.h"
  11. #include "ivtv-yuv.h"
  12. #include "ivtv-ioctl.h"
  13. #include "ivtv-cards.h"
  14. #include <linux/firmware.h>
  15. #include <media/i2c/saa7127.h>
  16. #define IVTV_MASK_SPU_ENABLE 0xFFFFFFFE
  17. #define IVTV_MASK_VPU_ENABLE15 0xFFFFFFF6
  18. #define IVTV_MASK_VPU_ENABLE16 0xFFFFFFFB
  19. #define IVTV_CMD_VDM_STOP 0x00000000
  20. #define IVTV_CMD_AO_STOP 0x00000005
  21. #define IVTV_CMD_APU_PING 0x00000000
  22. #define IVTV_CMD_VPU_STOP15 0xFFFFFFFE
  23. #define IVTV_CMD_VPU_STOP16 0xFFFFFFEE
  24. #define IVTV_CMD_HW_BLOCKS_RST 0xFFFFFFFF
  25. #define IVTV_CMD_SPU_STOP 0x00000001
  26. #define IVTV_CMD_SDRAM_PRECHARGE_INIT 0x0000001A
  27. #define IVTV_CMD_SDRAM_REFRESH_INIT 0x80000640
  28. #define IVTV_SDRAM_SLEEPTIME 600
  29. #define IVTV_DECODE_INIT_MPEG_FILENAME "v4l-cx2341x-init.mpg"
  30. #define IVTV_DECODE_INIT_MPEG_SIZE (152*1024)
  31. /* Encoder/decoder firmware sizes */
  32. #define IVTV_FW_ENC_SIZE (376836)
  33. #define IVTV_FW_DEC_SIZE (256*1024)
  34. static int load_fw_direct(const char *fn, volatile u8 __iomem *mem, struct ivtv *itv, long size)
  35. {
  36. const struct firmware *fw = NULL;
  37. int retries = 3;
  38. retry:
  39. if (retries && request_firmware(&fw, fn, &itv->pdev->dev) == 0) {
  40. int i;
  41. volatile u32 __iomem *dst = (volatile u32 __iomem *)mem;
  42. const u32 *src = (const u32 *)fw->data;
  43. if (fw->size != size) {
  44. /* Due to race conditions in firmware loading (esp. with udev <0.95)
  45. the wrong file was sometimes loaded. So we check filesizes to
  46. see if at least the right-sized file was loaded. If not, then we
  47. retry. */
  48. IVTV_INFO("Retry: file loaded was not %s (expected size %ld, got %zu)\n", fn, size, fw->size);
  49. release_firmware(fw);
  50. retries--;
  51. goto retry;
  52. }
  53. for (i = 0; i < fw->size; i += 4) {
  54. /* no need for endianness conversion on the ppc */
  55. __raw_writel(*src, dst);
  56. dst++;
  57. src++;
  58. }
  59. IVTV_INFO("Loaded %s firmware (%zu bytes)\n", fn, fw->size);
  60. release_firmware(fw);
  61. return size;
  62. }
  63. IVTV_ERR("Unable to open firmware %s (must be %ld bytes)\n", fn, size);
  64. IVTV_ERR("Did you put the firmware in the hotplug firmware directory?\n");
  65. return -ENOMEM;
  66. }
  67. void ivtv_halt_firmware(struct ivtv *itv)
  68. {
  69. IVTV_DEBUG_INFO("Preparing for firmware halt.\n");
  70. if (itv->has_cx23415 && itv->dec_mbox.mbox)
  71. ivtv_vapi(itv, CX2341X_DEC_HALT_FW, 0);
  72. if (itv->enc_mbox.mbox)
  73. ivtv_vapi(itv, CX2341X_ENC_HALT_FW, 0);
  74. ivtv_msleep_timeout(10, 0);
  75. itv->enc_mbox.mbox = itv->dec_mbox.mbox = NULL;
  76. IVTV_DEBUG_INFO("Stopping VDM\n");
  77. write_reg(IVTV_CMD_VDM_STOP, IVTV_REG_VDM);
  78. IVTV_DEBUG_INFO("Stopping AO\n");
  79. write_reg(IVTV_CMD_AO_STOP, IVTV_REG_AO);
  80. IVTV_DEBUG_INFO("pinging (?) APU\n");
  81. write_reg(IVTV_CMD_APU_PING, IVTV_REG_APU);
  82. IVTV_DEBUG_INFO("Stopping VPU\n");
  83. if (!itv->has_cx23415)
  84. write_reg(IVTV_CMD_VPU_STOP16, IVTV_REG_VPU);
  85. else
  86. write_reg(IVTV_CMD_VPU_STOP15, IVTV_REG_VPU);
  87. IVTV_DEBUG_INFO("Resetting Hw Blocks\n");
  88. write_reg(IVTV_CMD_HW_BLOCKS_RST, IVTV_REG_HW_BLOCKS);
  89. IVTV_DEBUG_INFO("Stopping SPU\n");
  90. write_reg(IVTV_CMD_SPU_STOP, IVTV_REG_SPU);
  91. ivtv_msleep_timeout(10, 0);
  92. IVTV_DEBUG_INFO("init Encoder SDRAM pre-charge\n");
  93. write_reg(IVTV_CMD_SDRAM_PRECHARGE_INIT, IVTV_REG_ENC_SDRAM_PRECHARGE);
  94. IVTV_DEBUG_INFO("init Encoder SDRAM refresh to 1us\n");
  95. write_reg(IVTV_CMD_SDRAM_REFRESH_INIT, IVTV_REG_ENC_SDRAM_REFRESH);
  96. if (itv->has_cx23415) {
  97. IVTV_DEBUG_INFO("init Decoder SDRAM pre-charge\n");
  98. write_reg(IVTV_CMD_SDRAM_PRECHARGE_INIT, IVTV_REG_DEC_SDRAM_PRECHARGE);
  99. IVTV_DEBUG_INFO("init Decoder SDRAM refresh to 1us\n");
  100. write_reg(IVTV_CMD_SDRAM_REFRESH_INIT, IVTV_REG_DEC_SDRAM_REFRESH);
  101. }
  102. IVTV_DEBUG_INFO("Sleeping for %dms\n", IVTV_SDRAM_SLEEPTIME);
  103. ivtv_msleep_timeout(IVTV_SDRAM_SLEEPTIME, 0);
  104. }
  105. void ivtv_firmware_versions(struct ivtv *itv)
  106. {
  107. u32 data[CX2341X_MBOX_MAX_DATA];
  108. /* Encoder */
  109. ivtv_vapi_result(itv, data, CX2341X_ENC_GET_VERSION, 0);
  110. IVTV_INFO("Encoder revision: 0x%08x\n", data[0]);
  111. if (data[0] != 0x02060039)
  112. IVTV_WARN("Recommended firmware version is 0x02060039.\n");
  113. if (itv->has_cx23415) {
  114. /* Decoder */
  115. ivtv_vapi_result(itv, data, CX2341X_DEC_GET_VERSION, 0);
  116. IVTV_INFO("Decoder revision: 0x%08x\n", data[0]);
  117. }
  118. }
  119. static int ivtv_firmware_copy(struct ivtv *itv)
  120. {
  121. IVTV_DEBUG_INFO("Loading encoder image\n");
  122. if (load_fw_direct(CX2341X_FIRM_ENC_FILENAME,
  123. itv->enc_mem, itv, IVTV_FW_ENC_SIZE) != IVTV_FW_ENC_SIZE) {
  124. IVTV_DEBUG_WARN("failed loading encoder firmware\n");
  125. return -3;
  126. }
  127. if (!itv->has_cx23415)
  128. return 0;
  129. IVTV_DEBUG_INFO("Loading decoder image\n");
  130. if (load_fw_direct(CX2341X_FIRM_DEC_FILENAME,
  131. itv->dec_mem, itv, IVTV_FW_DEC_SIZE) != IVTV_FW_DEC_SIZE) {
  132. IVTV_DEBUG_WARN("failed loading decoder firmware\n");
  133. return -1;
  134. }
  135. return 0;
  136. }
  137. static volatile struct ivtv_mailbox __iomem *ivtv_search_mailbox(const volatile u8 __iomem *mem, u32 size)
  138. {
  139. int i;
  140. /* mailbox is preceded by a 16 byte 'magic cookie' starting at a 256-byte
  141. address boundary */
  142. for (i = 0; i < size; i += 0x100) {
  143. if (readl(mem + i) == 0x12345678 &&
  144. readl(mem + i + 4) == 0x34567812 &&
  145. readl(mem + i + 8) == 0x56781234 &&
  146. readl(mem + i + 12) == 0x78123456) {
  147. return (volatile struct ivtv_mailbox __iomem *)(mem + i + 16);
  148. }
  149. }
  150. return NULL;
  151. }
  152. int ivtv_firmware_init(struct ivtv *itv)
  153. {
  154. int err;
  155. ivtv_halt_firmware(itv);
  156. /* load firmware */
  157. err = ivtv_firmware_copy(itv);
  158. if (err) {
  159. IVTV_DEBUG_WARN("Error %d loading firmware\n", err);
  160. return err;
  161. }
  162. /* start firmware */
  163. write_reg(read_reg(IVTV_REG_SPU) & IVTV_MASK_SPU_ENABLE, IVTV_REG_SPU);
  164. ivtv_msleep_timeout(100, 0);
  165. if (itv->has_cx23415)
  166. write_reg(read_reg(IVTV_REG_VPU) & IVTV_MASK_VPU_ENABLE15, IVTV_REG_VPU);
  167. else
  168. write_reg(read_reg(IVTV_REG_VPU) & IVTV_MASK_VPU_ENABLE16, IVTV_REG_VPU);
  169. ivtv_msleep_timeout(100, 0);
  170. /* find mailboxes and ping firmware */
  171. itv->enc_mbox.mbox = ivtv_search_mailbox(itv->enc_mem, IVTV_ENCODER_SIZE);
  172. if (itv->enc_mbox.mbox == NULL)
  173. IVTV_ERR("Encoder mailbox not found\n");
  174. else if (ivtv_vapi(itv, CX2341X_ENC_PING_FW, 0)) {
  175. IVTV_ERR("Encoder firmware dead!\n");
  176. itv->enc_mbox.mbox = NULL;
  177. }
  178. if (itv->enc_mbox.mbox == NULL)
  179. return -ENODEV;
  180. if (!itv->has_cx23415)
  181. return 0;
  182. itv->dec_mbox.mbox = ivtv_search_mailbox(itv->dec_mem, IVTV_DECODER_SIZE);
  183. if (itv->dec_mbox.mbox == NULL) {
  184. IVTV_ERR("Decoder mailbox not found\n");
  185. } else if (itv->has_cx23415 && ivtv_vapi(itv, CX2341X_DEC_PING_FW, 0)) {
  186. IVTV_ERR("Decoder firmware dead!\n");
  187. itv->dec_mbox.mbox = NULL;
  188. } else {
  189. /* Firmware okay, so check yuv output filter table */
  190. ivtv_yuv_filter_check(itv);
  191. }
  192. return itv->dec_mbox.mbox ? 0 : -ENODEV;
  193. }
  194. void ivtv_init_mpeg_decoder(struct ivtv *itv)
  195. {
  196. u32 data[CX2341X_MBOX_MAX_DATA];
  197. long readbytes;
  198. volatile u8 __iomem *mem_offset;
  199. data[0] = 0;
  200. data[1] = itv->cxhdl.width; /* YUV source width */
  201. data[2] = itv->cxhdl.height;
  202. data[3] = itv->cxhdl.audio_properties; /* Audio settings to use,
  203. bitmap. see docs. */
  204. if (ivtv_api(itv, CX2341X_DEC_SET_DECODER_SOURCE, 4, data)) {
  205. IVTV_ERR("ivtv_init_mpeg_decoder failed to set decoder source\n");
  206. return;
  207. }
  208. if (ivtv_vapi(itv, CX2341X_DEC_START_PLAYBACK, 2, 0, 1) != 0) {
  209. IVTV_ERR("ivtv_init_mpeg_decoder failed to start playback\n");
  210. return;
  211. }
  212. ivtv_api_get_data(&itv->dec_mbox, IVTV_MBOX_DMA, 2, data);
  213. mem_offset = itv->dec_mem + data[1];
  214. if ((readbytes = load_fw_direct(IVTV_DECODE_INIT_MPEG_FILENAME,
  215. mem_offset, itv, IVTV_DECODE_INIT_MPEG_SIZE)) <= 0) {
  216. IVTV_DEBUG_WARN("failed to read mpeg decoder initialisation file %s\n",
  217. IVTV_DECODE_INIT_MPEG_FILENAME);
  218. } else {
  219. ivtv_vapi(itv, CX2341X_DEC_SCHED_DMA_FROM_HOST, 3, 0, readbytes, 0);
  220. ivtv_msleep_timeout(100, 0);
  221. }
  222. ivtv_vapi(itv, CX2341X_DEC_STOP_PLAYBACK, 4, 0, 0, 0, 1);
  223. }
  224. /* Try to restart the card & restore previous settings */
  225. static int ivtv_firmware_restart(struct ivtv *itv)
  226. {
  227. int rc = 0;
  228. v4l2_std_id std;
  229. if (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT)
  230. /* Display test image during restart */
  231. ivtv_call_hw(itv, IVTV_HW_SAA7127, video, s_routing,
  232. SAA7127_INPUT_TYPE_TEST_IMAGE,
  233. itv->card->video_outputs[itv->active_output].video_output,
  234. 0);
  235. mutex_lock(&itv->udma.lock);
  236. rc = ivtv_firmware_init(itv);
  237. if (rc) {
  238. mutex_unlock(&itv->udma.lock);
  239. return rc;
  240. }
  241. /* Allow settings to reload */
  242. ivtv_mailbox_cache_invalidate(itv);
  243. /* Restore encoder video standard */
  244. std = itv->std;
  245. itv->std = 0;
  246. ivtv_s_std_enc(itv, std);
  247. if (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT) {
  248. ivtv_init_mpeg_decoder(itv);
  249. /* Restore decoder video standard */
  250. std = itv->std_out;
  251. itv->std_out = 0;
  252. ivtv_s_std_dec(itv, std);
  253. /* Restore framebuffer if active */
  254. if (itv->ivtvfb_restore)
  255. itv->ivtvfb_restore(itv);
  256. /* Restore alpha settings */
  257. ivtv_set_osd_alpha(itv);
  258. /* Restore normal output */
  259. ivtv_call_hw(itv, IVTV_HW_SAA7127, video, s_routing,
  260. SAA7127_INPUT_TYPE_NORMAL,
  261. itv->card->video_outputs[itv->active_output].video_output,
  262. 0);
  263. }
  264. mutex_unlock(&itv->udma.lock);
  265. return rc;
  266. }
  267. /* Check firmware running state. The checks fall through
  268. allowing multiple failures to be logged. */
  269. int ivtv_firmware_check(struct ivtv *itv, char *where)
  270. {
  271. int res = 0;
  272. /* Check encoder is still running */
  273. if (ivtv_vapi(itv, CX2341X_ENC_PING_FW, 0) < 0) {
  274. IVTV_WARN("Encoder has died : %s\n", where);
  275. res = -1;
  276. }
  277. /* Also check audio. Only check if not in use & encoder is okay */
  278. if (!res && !atomic_read(&itv->capturing) &&
  279. (!atomic_read(&itv->decoding) ||
  280. (atomic_read(&itv->decoding) < 2 && test_bit(IVTV_F_I_DEC_YUV,
  281. &itv->i_flags)))) {
  282. if (ivtv_vapi(itv, CX2341X_ENC_MISC, 1, 12) < 0) {
  283. IVTV_WARN("Audio has died (Encoder OK) : %s\n", where);
  284. res = -2;
  285. }
  286. }
  287. if (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT) {
  288. /* Second audio check. Skip if audio already failed */
  289. if (res != -2 && read_dec(0x100) != read_dec(0x104)) {
  290. /* Wait & try again to be certain. */
  291. ivtv_msleep_timeout(14, 0);
  292. if (read_dec(0x100) != read_dec(0x104)) {
  293. IVTV_WARN("Audio has died (Decoder) : %s\n",
  294. where);
  295. res = -1;
  296. }
  297. }
  298. /* Check decoder is still running */
  299. if (ivtv_vapi(itv, CX2341X_DEC_PING_FW, 0) < 0) {
  300. IVTV_WARN("Decoder has died : %s\n", where);
  301. res = -1;
  302. }
  303. }
  304. /* If something failed & currently idle, try to reload */
  305. if (res && !atomic_read(&itv->capturing) &&
  306. !atomic_read(&itv->decoding)) {
  307. IVTV_INFO("Detected in %s that firmware had failed - Reloading\n",
  308. where);
  309. res = ivtv_firmware_restart(itv);
  310. /*
  311. * Even if restarted ok, still signal a problem had occurred.
  312. * The caller can come through this function again to check
  313. * if things are really ok after the restart.
  314. */
  315. if (!res) {
  316. IVTV_INFO("Firmware restart okay\n");
  317. res = -EAGAIN;
  318. } else {
  319. IVTV_INFO("Firmware restart failed\n");
  320. }
  321. } else if (res) {
  322. res = -EIO;
  323. }
  324. return res;
  325. }
  326. MODULE_FIRMWARE(CX2341X_FIRM_ENC_FILENAME);
  327. MODULE_FIRMWARE(CX2341X_FIRM_DEC_FILENAME);
  328. MODULE_FIRMWARE(IVTV_DECODE_INIT_MPEG_FILENAME);