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>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "ivtv-driver.h"
23 #include "ivtv-mailbox.h"
24 #include "ivtv-firmware.h"
26 #include "ivtv-ioctl.h"
27 #include "ivtv-cards.h"
28 #include <linux/firmware.h>
29 #include <media/saa7127.h>
31 #define IVTV_MASK_SPU_ENABLE 0xFFFFFFFE
32 #define IVTV_MASK_VPU_ENABLE15 0xFFFFFFF6
33 #define IVTV_MASK_VPU_ENABLE16 0xFFFFFFFB
34 #define IVTV_CMD_VDM_STOP 0x00000000
35 #define IVTV_CMD_AO_STOP 0x00000005
36 #define IVTV_CMD_APU_PING 0x00000000
37 #define IVTV_CMD_VPU_STOP15 0xFFFFFFFE
38 #define IVTV_CMD_VPU_STOP16 0xFFFFFFEE
39 #define IVTV_CMD_HW_BLOCKS_RST 0xFFFFFFFF
40 #define IVTV_CMD_SPU_STOP 0x00000001
41 #define IVTV_CMD_SDRAM_PRECHARGE_INIT 0x0000001A
42 #define IVTV_CMD_SDRAM_REFRESH_INIT 0x80000640
43 #define IVTV_SDRAM_SLEEPTIME 600
45 #define IVTV_DECODE_INIT_MPEG_FILENAME "v4l-cx2341x-init.mpg"
46 #define IVTV_DECODE_INIT_MPEG_SIZE (152*1024)
48 /* Encoder/decoder firmware sizes */
49 #define IVTV_FW_ENC_SIZE (376836)
50 #define IVTV_FW_DEC_SIZE (256*1024)
52 static int load_fw_direct(const char *fn
, volatile u8 __iomem
*mem
, struct ivtv
*itv
, long size
)
54 const struct firmware
*fw
= NULL
;
58 if (retries
&& request_firmware(&fw
, fn
, &itv
->pdev
->dev
) == 0) {
60 volatile u32 __iomem
*dst
= (volatile u32 __iomem
*)mem
;
61 const u32
*src
= (const u32
*)fw
->data
;
63 if (fw
->size
!= size
) {
64 /* Due to race conditions in firmware loading (esp. with udev <0.95)
65 the wrong file was sometimes loaded. So we check filesizes to
66 see if at least the right-sized file was loaded. If not, then we
68 IVTV_INFO("Retry: file loaded was not %s (expected size %ld, got %zd)\n", fn
, size
, fw
->size
);
73 for (i
= 0; i
< fw
->size
; i
+= 4) {
74 /* no need for endianness conversion on the ppc */
75 __raw_writel(*src
, dst
);
79 IVTV_INFO("Loaded %s firmware (%zd bytes)\n", fn
, fw
->size
);
83 IVTV_ERR("Unable to open firmware %s (must be %ld bytes)\n", fn
, size
);
84 IVTV_ERR("Did you put the firmware in the hotplug firmware directory?\n");
88 void ivtv_halt_firmware(struct ivtv
*itv
)
90 IVTV_DEBUG_INFO("Preparing for firmware halt.\n");
91 if (itv
->has_cx23415
&& itv
->dec_mbox
.mbox
)
92 ivtv_vapi(itv
, CX2341X_DEC_HALT_FW
, 0);
93 if (itv
->enc_mbox
.mbox
)
94 ivtv_vapi(itv
, CX2341X_ENC_HALT_FW
, 0);
96 ivtv_msleep_timeout(10, 0);
97 itv
->enc_mbox
.mbox
= itv
->dec_mbox
.mbox
= NULL
;
99 IVTV_DEBUG_INFO("Stopping VDM\n");
100 write_reg(IVTV_CMD_VDM_STOP
, IVTV_REG_VDM
);
102 IVTV_DEBUG_INFO("Stopping AO\n");
103 write_reg(IVTV_CMD_AO_STOP
, IVTV_REG_AO
);
105 IVTV_DEBUG_INFO("pinging (?) APU\n");
106 write_reg(IVTV_CMD_APU_PING
, IVTV_REG_APU
);
108 IVTV_DEBUG_INFO("Stopping VPU\n");
109 if (!itv
->has_cx23415
)
110 write_reg(IVTV_CMD_VPU_STOP16
, IVTV_REG_VPU
);
112 write_reg(IVTV_CMD_VPU_STOP15
, IVTV_REG_VPU
);
114 IVTV_DEBUG_INFO("Resetting Hw Blocks\n");
115 write_reg(IVTV_CMD_HW_BLOCKS_RST
, IVTV_REG_HW_BLOCKS
);
117 IVTV_DEBUG_INFO("Stopping SPU\n");
118 write_reg(IVTV_CMD_SPU_STOP
, IVTV_REG_SPU
);
120 ivtv_msleep_timeout(10, 0);
122 IVTV_DEBUG_INFO("init Encoder SDRAM pre-charge\n");
123 write_reg(IVTV_CMD_SDRAM_PRECHARGE_INIT
, IVTV_REG_ENC_SDRAM_PRECHARGE
);
125 IVTV_DEBUG_INFO("init Encoder SDRAM refresh to 1us\n");
126 write_reg(IVTV_CMD_SDRAM_REFRESH_INIT
, IVTV_REG_ENC_SDRAM_REFRESH
);
128 if (itv
->has_cx23415
) {
129 IVTV_DEBUG_INFO("init Decoder SDRAM pre-charge\n");
130 write_reg(IVTV_CMD_SDRAM_PRECHARGE_INIT
, IVTV_REG_DEC_SDRAM_PRECHARGE
);
132 IVTV_DEBUG_INFO("init Decoder SDRAM refresh to 1us\n");
133 write_reg(IVTV_CMD_SDRAM_REFRESH_INIT
, IVTV_REG_DEC_SDRAM_REFRESH
);
136 IVTV_DEBUG_INFO("Sleeping for %dms\n", IVTV_SDRAM_SLEEPTIME
);
137 ivtv_msleep_timeout(IVTV_SDRAM_SLEEPTIME
, 0);
140 void ivtv_firmware_versions(struct ivtv
*itv
)
142 u32 data
[CX2341X_MBOX_MAX_DATA
];
145 ivtv_vapi_result(itv
, data
, CX2341X_ENC_GET_VERSION
, 0);
146 IVTV_INFO("Encoder revision: 0x%08x\n", data
[0]);
148 if (data
[0] != 0x02060039)
149 IVTV_WARN("Recommended firmware version is 0x02060039.\n");
151 if (itv
->has_cx23415
) {
153 ivtv_vapi_result(itv
, data
, CX2341X_DEC_GET_VERSION
, 0);
154 IVTV_INFO("Decoder revision: 0x%08x\n", data
[0]);
158 static int ivtv_firmware_copy(struct ivtv
*itv
)
160 IVTV_DEBUG_INFO("Loading encoder image\n");
161 if (load_fw_direct(CX2341X_FIRM_ENC_FILENAME
,
162 itv
->enc_mem
, itv
, IVTV_FW_ENC_SIZE
) != IVTV_FW_ENC_SIZE
) {
163 IVTV_DEBUG_WARN("failed loading encoder firmware\n");
166 if (!itv
->has_cx23415
)
169 IVTV_DEBUG_INFO("Loading decoder image\n");
170 if (load_fw_direct(CX2341X_FIRM_DEC_FILENAME
,
171 itv
->dec_mem
, itv
, IVTV_FW_DEC_SIZE
) != IVTV_FW_DEC_SIZE
) {
172 IVTV_DEBUG_WARN("failed loading decoder firmware\n");
178 static volatile struct ivtv_mailbox __iomem
*ivtv_search_mailbox(const volatile u8 __iomem
*mem
, u32 size
)
182 /* mailbox is preceded by a 16 byte 'magic cookie' starting at a 256-byte
184 for (i
= 0; i
< size
; i
+= 0x100) {
185 if (readl(mem
+ i
) == 0x12345678 &&
186 readl(mem
+ i
+ 4) == 0x34567812 &&
187 readl(mem
+ i
+ 8) == 0x56781234 &&
188 readl(mem
+ i
+ 12) == 0x78123456) {
189 return (volatile struct ivtv_mailbox __iomem
*)(mem
+ i
+ 16);
195 int ivtv_firmware_init(struct ivtv
*itv
)
199 ivtv_halt_firmware(itv
);
202 err
= ivtv_firmware_copy(itv
);
204 IVTV_DEBUG_WARN("Error %d loading firmware\n", err
);
209 write_reg(read_reg(IVTV_REG_SPU
) & IVTV_MASK_SPU_ENABLE
, IVTV_REG_SPU
);
210 ivtv_msleep_timeout(100, 0);
211 if (itv
->has_cx23415
)
212 write_reg(read_reg(IVTV_REG_VPU
) & IVTV_MASK_VPU_ENABLE15
, IVTV_REG_VPU
);
214 write_reg(read_reg(IVTV_REG_VPU
) & IVTV_MASK_VPU_ENABLE16
, IVTV_REG_VPU
);
215 ivtv_msleep_timeout(100, 0);
217 /* find mailboxes and ping firmware */
218 itv
->enc_mbox
.mbox
= ivtv_search_mailbox(itv
->enc_mem
, IVTV_ENCODER_SIZE
);
219 if (itv
->enc_mbox
.mbox
== NULL
)
220 IVTV_ERR("Encoder mailbox not found\n");
221 else if (ivtv_vapi(itv
, CX2341X_ENC_PING_FW
, 0)) {
222 IVTV_ERR("Encoder firmware dead!\n");
223 itv
->enc_mbox
.mbox
= NULL
;
225 if (itv
->enc_mbox
.mbox
== NULL
)
228 if (!itv
->has_cx23415
)
231 itv
->dec_mbox
.mbox
= ivtv_search_mailbox(itv
->dec_mem
, IVTV_DECODER_SIZE
);
232 if (itv
->dec_mbox
.mbox
== NULL
) {
233 IVTV_ERR("Decoder mailbox not found\n");
234 } else if (itv
->has_cx23415
&& ivtv_vapi(itv
, CX2341X_DEC_PING_FW
, 0)) {
235 IVTV_ERR("Decoder firmware dead!\n");
236 itv
->dec_mbox
.mbox
= NULL
;
238 /* Firmware okay, so check yuv output filter table */
239 ivtv_yuv_filter_check(itv
);
241 return itv
->dec_mbox
.mbox
? 0 : -ENODEV
;
244 void ivtv_init_mpeg_decoder(struct ivtv
*itv
)
246 u32 data
[CX2341X_MBOX_MAX_DATA
];
248 volatile u8 __iomem
*mem_offset
;
251 data
[1] = itv
->cxhdl
.width
; /* YUV source width */
252 data
[2] = itv
->cxhdl
.height
;
253 data
[3] = itv
->cxhdl
.audio_properties
; /* Audio settings to use,
255 if (ivtv_api(itv
, CX2341X_DEC_SET_DECODER_SOURCE
, 4, data
)) {
256 IVTV_ERR("ivtv_init_mpeg_decoder failed to set decoder source\n");
260 if (ivtv_vapi(itv
, CX2341X_DEC_START_PLAYBACK
, 2, 0, 1) != 0) {
261 IVTV_ERR("ivtv_init_mpeg_decoder failed to start playback\n");
264 ivtv_api_get_data(&itv
->dec_mbox
, IVTV_MBOX_DMA
, 2, data
);
265 mem_offset
= itv
->dec_mem
+ data
[1];
267 if ((readbytes
= load_fw_direct(IVTV_DECODE_INIT_MPEG_FILENAME
,
268 mem_offset
, itv
, IVTV_DECODE_INIT_MPEG_SIZE
)) <= 0) {
269 IVTV_DEBUG_WARN("failed to read mpeg decoder initialisation file %s\n",
270 IVTV_DECODE_INIT_MPEG_FILENAME
);
272 ivtv_vapi(itv
, CX2341X_DEC_SCHED_DMA_FROM_HOST
, 3, 0, readbytes
, 0);
273 ivtv_msleep_timeout(100, 0);
275 ivtv_vapi(itv
, CX2341X_DEC_STOP_PLAYBACK
, 4, 0, 0, 0, 1);
278 /* Try to restart the card & restore previous settings */
279 int ivtv_firmware_restart(struct ivtv
*itv
)
284 if (itv
->v4l2_cap
& V4L2_CAP_VIDEO_OUTPUT
)
285 /* Display test image during restart */
286 ivtv_call_hw(itv
, IVTV_HW_SAA7127
, video
, s_routing
,
287 SAA7127_INPUT_TYPE_TEST_IMAGE
,
288 itv
->card
->video_outputs
[itv
->active_output
].video_output
,
291 mutex_lock(&itv
->udma
.lock
);
293 rc
= ivtv_firmware_init(itv
);
295 mutex_unlock(&itv
->udma
.lock
);
299 /* Allow settings to reload */
300 ivtv_mailbox_cache_invalidate(itv
);
302 /* Restore encoder video standard */
305 ivtv_s_std_enc(itv
, &std
);
307 if (itv
->v4l2_cap
& V4L2_CAP_VIDEO_OUTPUT
) {
308 ivtv_init_mpeg_decoder(itv
);
310 /* Restore decoder video standard */
313 ivtv_s_std_dec(itv
, &std
);
315 /* Restore framebuffer if active */
316 if (itv
->ivtvfb_restore
)
317 itv
->ivtvfb_restore(itv
);
319 /* Restore alpha settings */
320 ivtv_set_osd_alpha(itv
);
322 /* Restore normal output */
323 ivtv_call_hw(itv
, IVTV_HW_SAA7127
, video
, s_routing
,
324 SAA7127_INPUT_TYPE_NORMAL
,
325 itv
->card
->video_outputs
[itv
->active_output
].video_output
,
329 mutex_unlock(&itv
->udma
.lock
);
333 /* Check firmware running state. The checks fall through
334 allowing multiple failures to be logged. */
335 int ivtv_firmware_check(struct ivtv
*itv
, char *where
)
339 /* Check encoder is still running */
340 if (ivtv_vapi(itv
, CX2341X_ENC_PING_FW
, 0) < 0) {
341 IVTV_WARN("Encoder has died : %s\n", where
);
345 /* Also check audio. Only check if not in use & encoder is okay */
346 if (!res
&& !atomic_read(&itv
->capturing
) &&
347 (!atomic_read(&itv
->decoding
) ||
348 (atomic_read(&itv
->decoding
) < 2 && test_bit(IVTV_F_I_DEC_YUV
,
351 if (ivtv_vapi(itv
, CX2341X_ENC_MISC
, 1, 12) < 0) {
352 IVTV_WARN("Audio has died (Encoder OK) : %s\n", where
);
357 if (itv
->v4l2_cap
& V4L2_CAP_VIDEO_OUTPUT
) {
358 /* Second audio check. Skip if audio already failed */
359 if (res
!= -2 && read_dec(0x100) != read_dec(0x104)) {
360 /* Wait & try again to be certain. */
361 ivtv_msleep_timeout(14, 0);
362 if (read_dec(0x100) != read_dec(0x104)) {
363 IVTV_WARN("Audio has died (Decoder) : %s\n",
369 /* Check decoder is still running */
370 if (ivtv_vapi(itv
, CX2341X_DEC_PING_FW
, 0) < 0) {
371 IVTV_WARN("Decoder has died : %s\n", where
);
376 /* If something failed & currently idle, try to reload */
377 if (res
&& !atomic_read(&itv
->capturing
) &&
378 !atomic_read(&itv
->decoding
)) {
379 IVTV_INFO("Detected in %s that firmware had failed - "
380 "Reloading\n", where
);
381 res
= ivtv_firmware_restart(itv
);
383 * Even if restarted ok, still signal a problem had occurred.
384 * The caller can come through this function again to check
385 * if things are really ok after the restart.
388 IVTV_INFO("Firmware restart okay\n");
391 IVTV_INFO("Firmware restart failed\n");