ivtv-firmware.c 8.3 KB

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