ivtv-firmware.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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/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. IVTV_ERR("Unable to open firmware %s (must be %ld bytes)\n", fn, size);
  74. IVTV_ERR("Did you put the firmware in the hotplug firmware directory?\n");
  75. return -ENOMEM;
  76. }
  77. void ivtv_halt_firmware(struct ivtv *itv)
  78. {
  79. IVTV_DEBUG_INFO("Preparing for firmware halt.\n");
  80. if (itv->has_cx23415 && itv->dec_mbox.mbox)
  81. ivtv_vapi(itv, CX2341X_DEC_HALT_FW, 0);
  82. if (itv->enc_mbox.mbox)
  83. ivtv_vapi(itv, CX2341X_ENC_HALT_FW, 0);
  84. ivtv_msleep_timeout(10, 0);
  85. itv->enc_mbox.mbox = itv->dec_mbox.mbox = NULL;
  86. IVTV_DEBUG_INFO("Stopping VDM\n");
  87. write_reg(IVTV_CMD_VDM_STOP, IVTV_REG_VDM);
  88. IVTV_DEBUG_INFO("Stopping AO\n");
  89. write_reg(IVTV_CMD_AO_STOP, IVTV_REG_AO);
  90. IVTV_DEBUG_INFO("pinging (?) APU\n");
  91. write_reg(IVTV_CMD_APU_PING, IVTV_REG_APU);
  92. IVTV_DEBUG_INFO("Stopping VPU\n");
  93. if (!itv->has_cx23415)
  94. write_reg(IVTV_CMD_VPU_STOP16, IVTV_REG_VPU);
  95. else
  96. write_reg(IVTV_CMD_VPU_STOP15, IVTV_REG_VPU);
  97. IVTV_DEBUG_INFO("Resetting Hw Blocks\n");
  98. write_reg(IVTV_CMD_HW_BLOCKS_RST, IVTV_REG_HW_BLOCKS);
  99. IVTV_DEBUG_INFO("Stopping SPU\n");
  100. write_reg(IVTV_CMD_SPU_STOP, IVTV_REG_SPU);
  101. ivtv_msleep_timeout(10, 0);
  102. IVTV_DEBUG_INFO("init Encoder SDRAM pre-charge\n");
  103. write_reg(IVTV_CMD_SDRAM_PRECHARGE_INIT, IVTV_REG_ENC_SDRAM_PRECHARGE);
  104. IVTV_DEBUG_INFO("init Encoder SDRAM refresh to 1us\n");
  105. write_reg(IVTV_CMD_SDRAM_REFRESH_INIT, IVTV_REG_ENC_SDRAM_REFRESH);
  106. if (itv->has_cx23415) {
  107. IVTV_DEBUG_INFO("init Decoder SDRAM pre-charge\n");
  108. write_reg(IVTV_CMD_SDRAM_PRECHARGE_INIT, IVTV_REG_DEC_SDRAM_PRECHARGE);
  109. IVTV_DEBUG_INFO("init Decoder SDRAM refresh to 1us\n");
  110. write_reg(IVTV_CMD_SDRAM_REFRESH_INIT, IVTV_REG_DEC_SDRAM_REFRESH);
  111. }
  112. IVTV_DEBUG_INFO("Sleeping for %dms\n", IVTV_SDRAM_SLEEPTIME);
  113. ivtv_msleep_timeout(IVTV_SDRAM_SLEEPTIME, 0);
  114. }
  115. void ivtv_firmware_versions(struct ivtv *itv)
  116. {
  117. u32 data[CX2341X_MBOX_MAX_DATA];
  118. /* Encoder */
  119. ivtv_vapi_result(itv, data, CX2341X_ENC_GET_VERSION, 0);
  120. IVTV_INFO("Encoder revision: 0x%08x\n", data[0]);
  121. if (data[0] != 0x02060039)
  122. IVTV_WARN("Recommended firmware version is 0x02060039.\n");
  123. if (itv->has_cx23415) {
  124. /* Decoder */
  125. ivtv_vapi_result(itv, data, CX2341X_DEC_GET_VERSION, 0);
  126. IVTV_INFO("Decoder revision: 0x%08x\n", data[0]);
  127. }
  128. }
  129. static int ivtv_firmware_copy(struct ivtv *itv)
  130. {
  131. IVTV_DEBUG_INFO("Loading encoder image\n");
  132. if (load_fw_direct(CX2341X_FIRM_ENC_FILENAME,
  133. itv->enc_mem, itv, IVTV_FW_ENC_SIZE) != IVTV_FW_ENC_SIZE) {
  134. IVTV_DEBUG_WARN("failed loading encoder firmware\n");
  135. return -3;
  136. }
  137. if (!itv->has_cx23415)
  138. return 0;
  139. IVTV_DEBUG_INFO("Loading decoder image\n");
  140. if (load_fw_direct(CX2341X_FIRM_DEC_FILENAME,
  141. itv->dec_mem, itv, IVTV_FW_DEC_SIZE) != IVTV_FW_DEC_SIZE) {
  142. IVTV_DEBUG_WARN("failed loading decoder firmware\n");
  143. return -1;
  144. }
  145. return 0;
  146. }
  147. static volatile struct ivtv_mailbox __iomem *ivtv_search_mailbox(const volatile u8 __iomem *mem, u32 size)
  148. {
  149. int i;
  150. /* mailbox is preceded by a 16 byte 'magic cookie' starting at a 256-byte
  151. address boundary */
  152. for (i = 0; i < size; i += 0x100) {
  153. if (readl(mem + i) == 0x12345678 &&
  154. readl(mem + i + 4) == 0x34567812 &&
  155. readl(mem + i + 8) == 0x56781234 &&
  156. readl(mem + i + 12) == 0x78123456) {
  157. return (volatile struct ivtv_mailbox __iomem *)(mem + i + 16);
  158. }
  159. }
  160. return NULL;
  161. }
  162. int ivtv_firmware_init(struct ivtv *itv)
  163. {
  164. int err;
  165. ivtv_halt_firmware(itv);
  166. /* load firmware */
  167. err = ivtv_firmware_copy(itv);
  168. if (err) {
  169. IVTV_DEBUG_WARN("Error %d loading firmware\n", err);
  170. return err;
  171. }
  172. /* start firmware */
  173. write_reg(read_reg(IVTV_REG_SPU) & IVTV_MASK_SPU_ENABLE, IVTV_REG_SPU);
  174. ivtv_msleep_timeout(100, 0);
  175. if (itv->has_cx23415)
  176. write_reg(read_reg(IVTV_REG_VPU) & IVTV_MASK_VPU_ENABLE15, IVTV_REG_VPU);
  177. else
  178. write_reg(read_reg(IVTV_REG_VPU) & IVTV_MASK_VPU_ENABLE16, IVTV_REG_VPU);
  179. ivtv_msleep_timeout(100, 0);
  180. /* find mailboxes and ping firmware */
  181. itv->enc_mbox.mbox = ivtv_search_mailbox(itv->enc_mem, IVTV_ENCODER_SIZE);
  182. if (itv->enc_mbox.mbox == NULL)
  183. IVTV_ERR("Encoder mailbox not found\n");
  184. else if (ivtv_vapi(itv, CX2341X_ENC_PING_FW, 0)) {
  185. IVTV_ERR("Encoder firmware dead!\n");
  186. itv->enc_mbox.mbox = NULL;
  187. }
  188. if (itv->enc_mbox.mbox == NULL)
  189. return -ENODEV;
  190. if (!itv->has_cx23415)
  191. return 0;
  192. itv->dec_mbox.mbox = ivtv_search_mailbox(itv->dec_mem, IVTV_DECODER_SIZE);
  193. if (itv->dec_mbox.mbox == NULL) {
  194. IVTV_ERR("Decoder mailbox not found\n");
  195. } else if (itv->has_cx23415 && ivtv_vapi(itv, CX2341X_DEC_PING_FW, 0)) {
  196. IVTV_ERR("Decoder firmware dead!\n");
  197. itv->dec_mbox.mbox = NULL;
  198. } else {
  199. /* Firmware okay, so check yuv output filter table */
  200. ivtv_yuv_filter_check(itv);
  201. }
  202. return itv->dec_mbox.mbox ? 0 : -ENODEV;
  203. }
  204. void ivtv_init_mpeg_decoder(struct ivtv *itv)
  205. {
  206. u32 data[CX2341X_MBOX_MAX_DATA];
  207. long readbytes;
  208. volatile u8 __iomem *mem_offset;
  209. data[0] = 0;
  210. data[1] = itv->cxhdl.width; /* YUV source width */
  211. data[2] = itv->cxhdl.height;
  212. data[3] = itv->cxhdl.audio_properties; /* Audio settings to use,
  213. bitmap. see docs. */
  214. if (ivtv_api(itv, CX2341X_DEC_SET_DECODER_SOURCE, 4, data)) {
  215. IVTV_ERR("ivtv_init_mpeg_decoder failed to set decoder source\n");
  216. return;
  217. }
  218. if (ivtv_vapi(itv, CX2341X_DEC_START_PLAYBACK, 2, 0, 1) != 0) {
  219. IVTV_ERR("ivtv_init_mpeg_decoder failed to start playback\n");
  220. return;
  221. }
  222. ivtv_api_get_data(&itv->dec_mbox, IVTV_MBOX_DMA, 2, data);
  223. mem_offset = itv->dec_mem + data[1];
  224. if ((readbytes = load_fw_direct(IVTV_DECODE_INIT_MPEG_FILENAME,
  225. mem_offset, itv, IVTV_DECODE_INIT_MPEG_SIZE)) <= 0) {
  226. IVTV_DEBUG_WARN("failed to read mpeg decoder initialisation file %s\n",
  227. IVTV_DECODE_INIT_MPEG_FILENAME);
  228. } else {
  229. ivtv_vapi(itv, CX2341X_DEC_SCHED_DMA_FROM_HOST, 3, 0, readbytes, 0);
  230. ivtv_msleep_timeout(100, 0);
  231. }
  232. ivtv_vapi(itv, CX2341X_DEC_STOP_PLAYBACK, 4, 0, 0, 0, 1);
  233. }
  234. /* Try to restart the card & restore previous settings */
  235. static int ivtv_firmware_restart(struct ivtv *itv)
  236. {
  237. int rc = 0;
  238. v4l2_std_id std;
  239. if (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT)
  240. /* Display test image during restart */
  241. ivtv_call_hw(itv, IVTV_HW_SAA7127, video, s_routing,
  242. SAA7127_INPUT_TYPE_TEST_IMAGE,
  243. itv->card->video_outputs[itv->active_output].video_output,
  244. 0);
  245. mutex_lock(&itv->udma.lock);
  246. rc = ivtv_firmware_init(itv);
  247. if (rc) {
  248. mutex_unlock(&itv->udma.lock);
  249. return rc;
  250. }
  251. /* Allow settings to reload */
  252. ivtv_mailbox_cache_invalidate(itv);
  253. /* Restore encoder video standard */
  254. std = itv->std;
  255. itv->std = 0;
  256. ivtv_s_std_enc(itv, std);
  257. if (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT) {
  258. ivtv_init_mpeg_decoder(itv);
  259. /* Restore decoder video standard */
  260. std = itv->std_out;
  261. itv->std_out = 0;
  262. ivtv_s_std_dec(itv, std);
  263. /* Restore framebuffer if active */
  264. if (itv->ivtvfb_restore)
  265. itv->ivtvfb_restore(itv);
  266. /* Restore alpha settings */
  267. ivtv_set_osd_alpha(itv);
  268. /* Restore normal output */
  269. ivtv_call_hw(itv, IVTV_HW_SAA7127, video, s_routing,
  270. SAA7127_INPUT_TYPE_NORMAL,
  271. itv->card->video_outputs[itv->active_output].video_output,
  272. 0);
  273. }
  274. mutex_unlock(&itv->udma.lock);
  275. return rc;
  276. }
  277. /* Check firmware running state. The checks fall through
  278. allowing multiple failures to be logged. */
  279. int ivtv_firmware_check(struct ivtv *itv, char *where)
  280. {
  281. int res = 0;
  282. /* Check encoder is still running */
  283. if (ivtv_vapi(itv, CX2341X_ENC_PING_FW, 0) < 0) {
  284. IVTV_WARN("Encoder has died : %s\n", where);
  285. res = -1;
  286. }
  287. /* Also check audio. Only check if not in use & encoder is okay */
  288. if (!res && !atomic_read(&itv->capturing) &&
  289. (!atomic_read(&itv->decoding) ||
  290. (atomic_read(&itv->decoding) < 2 && test_bit(IVTV_F_I_DEC_YUV,
  291. &itv->i_flags)))) {
  292. if (ivtv_vapi(itv, CX2341X_ENC_MISC, 1, 12) < 0) {
  293. IVTV_WARN("Audio has died (Encoder OK) : %s\n", where);
  294. res = -2;
  295. }
  296. }
  297. if (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT) {
  298. /* Second audio check. Skip if audio already failed */
  299. if (res != -2 && read_dec(0x100) != read_dec(0x104)) {
  300. /* Wait & try again to be certain. */
  301. ivtv_msleep_timeout(14, 0);
  302. if (read_dec(0x100) != read_dec(0x104)) {
  303. IVTV_WARN("Audio has died (Decoder) : %s\n",
  304. where);
  305. res = -1;
  306. }
  307. }
  308. /* Check decoder is still running */
  309. if (ivtv_vapi(itv, CX2341X_DEC_PING_FW, 0) < 0) {
  310. IVTV_WARN("Decoder has died : %s\n", where);
  311. res = -1;
  312. }
  313. }
  314. /* If something failed & currently idle, try to reload */
  315. if (res && !atomic_read(&itv->capturing) &&
  316. !atomic_read(&itv->decoding)) {
  317. IVTV_INFO("Detected in %s that firmware had failed - "
  318. "Reloading\n", where);
  319. res = ivtv_firmware_restart(itv);
  320. /*
  321. * Even if restarted ok, still signal a problem had occurred.
  322. * The caller can come through this function again to check
  323. * if things are really ok after the restart.
  324. */
  325. if (!res) {
  326. IVTV_INFO("Firmware restart okay\n");
  327. res = -EAGAIN;
  328. } else {
  329. IVTV_INFO("Firmware restart failed\n");
  330. }
  331. } else if (res) {
  332. res = -EIO;
  333. }
  334. return res;
  335. }
  336. MODULE_FIRMWARE(CX2341X_FIRM_ENC_FILENAME);
  337. MODULE_FIRMWARE(CX2341X_FIRM_DEC_FILENAME);
  338. MODULE_FIRMWARE(IVTV_DECODE_INIT_MPEG_FILENAME);