ivtv-firmware.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 <linux/firmware.h>
  23. #define IVTV_MASK_SPU_ENABLE 0xFFFFFFFE
  24. #define IVTV_MASK_VPU_ENABLE15 0xFFFFFFF6
  25. #define IVTV_MASK_VPU_ENABLE16 0xFFFFFFFB
  26. #define IVTV_CMD_VDM_STOP 0x00000000
  27. #define IVTV_CMD_AO_STOP 0x00000005
  28. #define IVTV_CMD_APU_PING 0x00000000
  29. #define IVTV_CMD_VPU_STOP15 0xFFFFFFFE
  30. #define IVTV_CMD_VPU_STOP16 0xFFFFFFEE
  31. #define IVTV_CMD_HW_BLOCKS_RST 0xFFFFFFFF
  32. #define IVTV_CMD_SPU_STOP 0x00000001
  33. #define IVTV_CMD_SDRAM_PRECHARGE_INIT 0x0000001A
  34. #define IVTV_CMD_SDRAM_REFRESH_INIT 0x80000640
  35. #define IVTV_SDRAM_SLEEPTIME 600
  36. #define IVTV_DECODE_INIT_MPEG_FILENAME "v4l-cx2341x-init.mpg"
  37. #define IVTV_DECODE_INIT_MPEG_SIZE (152*1024)
  38. /* Encoder/decoder firmware sizes */
  39. #define IVTV_FW_ENC_SIZE (376836)
  40. #define IVTV_FW_DEC_SIZE (256*1024)
  41. static int load_fw_direct(const char *fn, volatile u8 __iomem *mem, struct ivtv *itv, long size)
  42. {
  43. const struct firmware *fw = NULL;
  44. int retries = 3;
  45. retry:
  46. if (retries && request_firmware(&fw, fn, &itv->pdev->dev) == 0) {
  47. int i;
  48. volatile u32 __iomem *dst = (volatile u32 __iomem *)mem;
  49. const u32 *src = (const u32 *)fw->data;
  50. if (fw->size != size) {
  51. /* Due to race conditions in firmware loading (esp. with udev <0.95)
  52. the wrong file was sometimes loaded. So we check filesizes to
  53. see if at least the right-sized file was loaded. If not, then we
  54. retry. */
  55. IVTV_INFO("Retry: file loaded was not %s (expected size %ld, got %zd)\n", fn, size, fw->size);
  56. release_firmware(fw);
  57. retries--;
  58. goto retry;
  59. }
  60. for (i = 0; i < fw->size; i += 4) {
  61. /* no need for endianness conversion on the ppc */
  62. __raw_writel(*src, dst);
  63. dst++;
  64. src++;
  65. }
  66. IVTV_INFO("Loaded %s firmware (%zd bytes)\n", fn, fw->size);
  67. release_firmware(fw);
  68. return size;
  69. }
  70. IVTV_ERR("Unable to open firmware %s (must be %ld bytes)\n", fn, size);
  71. IVTV_ERR("Did you put the firmware in the hotplug firmware directory?\n");
  72. return -ENOMEM;
  73. }
  74. void ivtv_halt_firmware(struct ivtv *itv)
  75. {
  76. IVTV_DEBUG_INFO("Preparing for firmware halt.\n");
  77. if (itv->has_cx23415 && itv->dec_mbox.mbox)
  78. ivtv_vapi(itv, CX2341X_DEC_HALT_FW, 0);
  79. if (itv->enc_mbox.mbox)
  80. ivtv_vapi(itv, CX2341X_ENC_HALT_FW, 0);
  81. ivtv_msleep_timeout(10, 0);
  82. itv->enc_mbox.mbox = itv->dec_mbox.mbox = NULL;
  83. IVTV_DEBUG_INFO("Stopping VDM\n");
  84. write_reg(IVTV_CMD_VDM_STOP, IVTV_REG_VDM);
  85. IVTV_DEBUG_INFO("Stopping AO\n");
  86. write_reg(IVTV_CMD_AO_STOP, IVTV_REG_AO);
  87. IVTV_DEBUG_INFO("pinging (?) APU\n");
  88. write_reg(IVTV_CMD_APU_PING, IVTV_REG_APU);
  89. IVTV_DEBUG_INFO("Stopping VPU\n");
  90. if (!itv->has_cx23415)
  91. write_reg(IVTV_CMD_VPU_STOP16, IVTV_REG_VPU);
  92. else
  93. write_reg(IVTV_CMD_VPU_STOP15, IVTV_REG_VPU);
  94. IVTV_DEBUG_INFO("Resetting Hw Blocks\n");
  95. write_reg(IVTV_CMD_HW_BLOCKS_RST, IVTV_REG_HW_BLOCKS);
  96. IVTV_DEBUG_INFO("Stopping SPU\n");
  97. write_reg(IVTV_CMD_SPU_STOP, IVTV_REG_SPU);
  98. ivtv_msleep_timeout(10, 0);
  99. IVTV_DEBUG_INFO("init Encoder SDRAM pre-charge\n");
  100. write_reg(IVTV_CMD_SDRAM_PRECHARGE_INIT, IVTV_REG_ENC_SDRAM_PRECHARGE);
  101. IVTV_DEBUG_INFO("init Encoder SDRAM refresh to 1us\n");
  102. write_reg(IVTV_CMD_SDRAM_REFRESH_INIT, IVTV_REG_ENC_SDRAM_REFRESH);
  103. if (itv->has_cx23415) {
  104. IVTV_DEBUG_INFO("init Decoder SDRAM pre-charge\n");
  105. write_reg(IVTV_CMD_SDRAM_PRECHARGE_INIT, IVTV_REG_DEC_SDRAM_PRECHARGE);
  106. IVTV_DEBUG_INFO("init Decoder SDRAM refresh to 1us\n");
  107. write_reg(IVTV_CMD_SDRAM_REFRESH_INIT, IVTV_REG_DEC_SDRAM_REFRESH);
  108. }
  109. IVTV_DEBUG_INFO("Sleeping for %dms\n", IVTV_SDRAM_SLEEPTIME);
  110. ivtv_msleep_timeout(IVTV_SDRAM_SLEEPTIME, 0);
  111. }
  112. void ivtv_firmware_versions(struct ivtv *itv)
  113. {
  114. u32 data[CX2341X_MBOX_MAX_DATA];
  115. /* Encoder */
  116. ivtv_vapi_result(itv, data, CX2341X_ENC_GET_VERSION, 0);
  117. IVTV_INFO("Encoder revision: 0x%08x\n", data[0]);
  118. if (data[0] != 0x02060039)
  119. IVTV_WARN("Recommended firmware version is 0x02060039.\n");
  120. if (itv->has_cx23415) {
  121. /* Decoder */
  122. ivtv_vapi_result(itv, data, CX2341X_DEC_GET_VERSION, 0);
  123. IVTV_INFO("Decoder revision: 0x%08x\n", data[0]);
  124. }
  125. }
  126. static int ivtv_firmware_copy(struct ivtv *itv)
  127. {
  128. IVTV_DEBUG_INFO("Loading encoder image\n");
  129. if (load_fw_direct(CX2341X_FIRM_ENC_FILENAME,
  130. itv->enc_mem, itv, IVTV_FW_ENC_SIZE) != IVTV_FW_ENC_SIZE) {
  131. IVTV_DEBUG_WARN("failed loading encoder firmware\n");
  132. return -3;
  133. }
  134. if (!itv->has_cx23415)
  135. return 0;
  136. IVTV_DEBUG_INFO("Loading decoder image\n");
  137. if (load_fw_direct(CX2341X_FIRM_DEC_FILENAME,
  138. itv->dec_mem, itv, IVTV_FW_DEC_SIZE) != IVTV_FW_DEC_SIZE) {
  139. IVTV_DEBUG_WARN("failed loading decoder firmware\n");
  140. return -1;
  141. }
  142. return 0;
  143. }
  144. static volatile struct ivtv_mailbox __iomem *ivtv_search_mailbox(const volatile u8 __iomem *mem, u32 size)
  145. {
  146. int i;
  147. /* mailbox is preceeded by a 16 byte 'magic cookie' starting at a 256-byte
  148. address boundary */
  149. for (i = 0; i < size; i += 0x100) {
  150. if (readl(mem + i) == 0x12345678 &&
  151. readl(mem + i + 4) == 0x34567812 &&
  152. readl(mem + i + 8) == 0x56781234 &&
  153. readl(mem + i + 12) == 0x78123456) {
  154. return (volatile struct ivtv_mailbox __iomem *)(mem + i + 16);
  155. }
  156. }
  157. return NULL;
  158. }
  159. int ivtv_firmware_init(struct ivtv *itv)
  160. {
  161. int err;
  162. ivtv_halt_firmware(itv);
  163. /* load firmware */
  164. err = ivtv_firmware_copy(itv);
  165. if (err) {
  166. IVTV_DEBUG_WARN("Error %d loading firmware\n", err);
  167. return err;
  168. }
  169. /* start firmware */
  170. write_reg(read_reg(IVTV_REG_SPU) & IVTV_MASK_SPU_ENABLE, IVTV_REG_SPU);
  171. ivtv_msleep_timeout(100, 0);
  172. if (itv->has_cx23415)
  173. write_reg(read_reg(IVTV_REG_VPU) & IVTV_MASK_VPU_ENABLE15, IVTV_REG_VPU);
  174. else
  175. write_reg(read_reg(IVTV_REG_VPU) & IVTV_MASK_VPU_ENABLE16, IVTV_REG_VPU);
  176. ivtv_msleep_timeout(100, 0);
  177. /* find mailboxes and ping firmware */
  178. itv->enc_mbox.mbox = ivtv_search_mailbox(itv->enc_mem, IVTV_ENCODER_SIZE);
  179. if (itv->enc_mbox.mbox == NULL)
  180. IVTV_ERR("Encoder mailbox not found\n");
  181. else if (ivtv_vapi(itv, CX2341X_ENC_PING_FW, 0)) {
  182. IVTV_ERR("Encoder firmware dead!\n");
  183. itv->enc_mbox.mbox = NULL;
  184. }
  185. if (itv->enc_mbox.mbox == NULL)
  186. return -ENODEV;
  187. if (!itv->has_cx23415)
  188. return 0;
  189. itv->dec_mbox.mbox = ivtv_search_mailbox(itv->dec_mem, IVTV_DECODER_SIZE);
  190. if (itv->dec_mbox.mbox == NULL) {
  191. IVTV_ERR("Decoder mailbox not found\n");
  192. } else if (itv->has_cx23415 && ivtv_vapi(itv, CX2341X_DEC_PING_FW, 0)) {
  193. IVTV_ERR("Decoder firmware dead!\n");
  194. itv->dec_mbox.mbox = NULL;
  195. } else {
  196. /* Firmware okay, so check yuv output filter table */
  197. ivtv_yuv_filter_check(itv);
  198. }
  199. return itv->dec_mbox.mbox ? 0 : -ENODEV;
  200. }
  201. void ivtv_init_mpeg_decoder(struct ivtv *itv)
  202. {
  203. u32 data[CX2341X_MBOX_MAX_DATA];
  204. long readbytes;
  205. volatile u8 __iomem *mem_offset;
  206. data[0] = 0;
  207. data[1] = itv->params.width; /* YUV source width */
  208. data[2] = itv->params.height;
  209. data[3] = itv->params.audio_properties; /* Audio settings to use,
  210. bitmap. see docs. */
  211. if (ivtv_api(itv, CX2341X_DEC_SET_DECODER_SOURCE, 4, data)) {
  212. IVTV_ERR("ivtv_init_mpeg_decoder failed to set decoder source\n");
  213. return;
  214. }
  215. if (ivtv_vapi(itv, CX2341X_DEC_START_PLAYBACK, 2, 0, 1) != 0) {
  216. IVTV_ERR("ivtv_init_mpeg_decoder failed to start playback\n");
  217. return;
  218. }
  219. ivtv_api_get_data(&itv->dec_mbox, IVTV_MBOX_DMA, data);
  220. mem_offset = itv->dec_mem + data[1];
  221. if ((readbytes = load_fw_direct(IVTV_DECODE_INIT_MPEG_FILENAME,
  222. mem_offset, itv, IVTV_DECODE_INIT_MPEG_SIZE)) <= 0) {
  223. IVTV_DEBUG_WARN("failed to read mpeg decoder initialisation file %s\n",
  224. IVTV_DECODE_INIT_MPEG_FILENAME);
  225. } else {
  226. ivtv_vapi(itv, CX2341X_DEC_SCHED_DMA_FROM_HOST, 3, 0, readbytes, 0);
  227. ivtv_msleep_timeout(100, 0);
  228. }
  229. ivtv_vapi(itv, CX2341X_DEC_STOP_PLAYBACK, 4, 0, 0, 0, 1);
  230. }