ivtv-firmware.c 12 KB

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