ivtv-firmware.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 (60 * HZ / 100) /* 600 ms */
  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_sleep_timeout(HZ / 100, 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_sleep_timeout(HZ / 100, 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 (600 recommended)\n",
  109. (int)(IVTV_SDRAM_SLEEPTIME * 1000 / HZ));
  110. ivtv_sleep_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_sleep_timeout(HZ / 10, 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_sleep_timeout(HZ / 10, 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. }
  196. return itv->dec_mbox.mbox ? 0 : -ENODEV;
  197. }
  198. void ivtv_init_mpeg_decoder(struct ivtv *itv)
  199. {
  200. u32 data[CX2341X_MBOX_MAX_DATA];
  201. long readbytes;
  202. volatile u8 __iomem *mem_offset;
  203. data[0] = 0;
  204. data[1] = itv->params.width; /* YUV source width */
  205. data[2] = itv->params.height;
  206. data[3] = itv->params.audio_properties; /* Audio settings to use,
  207. bitmap. see docs. */
  208. if (ivtv_api(itv, CX2341X_DEC_SET_DECODER_SOURCE, 4, data)) {
  209. IVTV_ERR("ivtv_init_mpeg_decoder failed to set decoder source\n");
  210. return;
  211. }
  212. if (ivtv_vapi(itv, CX2341X_DEC_START_PLAYBACK, 2, 0, 1) != 0) {
  213. IVTV_ERR("ivtv_init_mpeg_decoder failed to start playback\n");
  214. return;
  215. }
  216. ivtv_api_get_data(&itv->dec_mbox, IVTV_MBOX_DMA, data);
  217. mem_offset = itv->dec_mem + data[1];
  218. if ((readbytes = load_fw_direct(IVTV_DECODE_INIT_MPEG_FILENAME,
  219. mem_offset, itv, IVTV_DECODE_INIT_MPEG_SIZE)) <= 0) {
  220. IVTV_DEBUG_WARN("failed to read mpeg decoder initialisation file %s\n",
  221. IVTV_DECODE_INIT_MPEG_FILENAME);
  222. } else {
  223. ivtv_vapi(itv, CX2341X_DEC_SCHED_DMA_FROM_HOST, 3, 0, readbytes, 0);
  224. ivtv_sleep_timeout(HZ / 10, 0);
  225. }
  226. ivtv_vapi(itv, CX2341X_DEC_STOP_PLAYBACK, 4, 0, 0, 0, 1);
  227. }