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 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. 11 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. 16 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 20 */ 21 22 #include "ivtv-driver.h" 23 #include "ivtv-mailbox.h" 24 #include "ivtv-firmware.h" 25 #include "ivtv-yuv.h" 26 #include "ivtv-ioctl.h" 27 #include "ivtv-cards.h" 28 #include <linux/firmware.h> 29 #include <media/saa7127.h> 30 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 44 45 #define IVTV_DECODE_INIT_MPEG_FILENAME "v4l-cx2341x-init.mpg" 46 #define IVTV_DECODE_INIT_MPEG_SIZE (152*1024) 47 48 /* Encoder/decoder firmware sizes */ 49 #define IVTV_FW_ENC_SIZE (376836) 50 #define IVTV_FW_DEC_SIZE (256*1024) 51 52 static int load_fw_direct(const char *fn, volatile u8 __iomem *mem, struct ivtv *itv, long size) 53 { 54 const struct firmware *fw = NULL; 55 int retries = 3; 56 57 retry: 58 if (retries && request_firmware(&fw, fn, &itv->pdev->dev) == 0) { 59 int i; 60 volatile u32 __iomem *dst = (volatile u32 __iomem *)mem; 61 const u32 *src = (const u32 *)fw->data; 62 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 67 retry. */ 68 IVTV_INFO("Retry: file loaded was not %s (expected size %ld, got %zu)\n", fn, size, fw->size); 69 release_firmware(fw); 70 retries--; 71 goto retry; 72 } 73 for (i = 0; i < fw->size; i += 4) { 74 /* no need for endianness conversion on the ppc */ 75 __raw_writel(*src, dst); 76 dst++; 77 src++; 78 } 79 IVTV_INFO("Loaded %s firmware (%zu bytes)\n", fn, fw->size); 80 release_firmware(fw); 81 return size; 82 } 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"); 85 return -ENOMEM; 86 } 87 88 void ivtv_halt_firmware(struct ivtv *itv) 89 { 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); 95 96 ivtv_msleep_timeout(10, 0); 97 itv->enc_mbox.mbox = itv->dec_mbox.mbox = NULL; 98 99 IVTV_DEBUG_INFO("Stopping VDM\n"); 100 write_reg(IVTV_CMD_VDM_STOP, IVTV_REG_VDM); 101 102 IVTV_DEBUG_INFO("Stopping AO\n"); 103 write_reg(IVTV_CMD_AO_STOP, IVTV_REG_AO); 104 105 IVTV_DEBUG_INFO("pinging (?) APU\n"); 106 write_reg(IVTV_CMD_APU_PING, IVTV_REG_APU); 107 108 IVTV_DEBUG_INFO("Stopping VPU\n"); 109 if (!itv->has_cx23415) 110 write_reg(IVTV_CMD_VPU_STOP16, IVTV_REG_VPU); 111 else 112 write_reg(IVTV_CMD_VPU_STOP15, IVTV_REG_VPU); 113 114 IVTV_DEBUG_INFO("Resetting Hw Blocks\n"); 115 write_reg(IVTV_CMD_HW_BLOCKS_RST, IVTV_REG_HW_BLOCKS); 116 117 IVTV_DEBUG_INFO("Stopping SPU\n"); 118 write_reg(IVTV_CMD_SPU_STOP, IVTV_REG_SPU); 119 120 ivtv_msleep_timeout(10, 0); 121 122 IVTV_DEBUG_INFO("init Encoder SDRAM pre-charge\n"); 123 write_reg(IVTV_CMD_SDRAM_PRECHARGE_INIT, IVTV_REG_ENC_SDRAM_PRECHARGE); 124 125 IVTV_DEBUG_INFO("init Encoder SDRAM refresh to 1us\n"); 126 write_reg(IVTV_CMD_SDRAM_REFRESH_INIT, IVTV_REG_ENC_SDRAM_REFRESH); 127 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); 131 132 IVTV_DEBUG_INFO("init Decoder SDRAM refresh to 1us\n"); 133 write_reg(IVTV_CMD_SDRAM_REFRESH_INIT, IVTV_REG_DEC_SDRAM_REFRESH); 134 } 135 136 IVTV_DEBUG_INFO("Sleeping for %dms\n", IVTV_SDRAM_SLEEPTIME); 137 ivtv_msleep_timeout(IVTV_SDRAM_SLEEPTIME, 0); 138 } 139 140 void ivtv_firmware_versions(struct ivtv *itv) 141 { 142 u32 data[CX2341X_MBOX_MAX_DATA]; 143 144 /* Encoder */ 145 ivtv_vapi_result(itv, data, CX2341X_ENC_GET_VERSION, 0); 146 IVTV_INFO("Encoder revision: 0x%08x\n", data[0]); 147 148 if (data[0] != 0x02060039) 149 IVTV_WARN("Recommended firmware version is 0x02060039.\n"); 150 151 if (itv->has_cx23415) { 152 /* Decoder */ 153 ivtv_vapi_result(itv, data, CX2341X_DEC_GET_VERSION, 0); 154 IVTV_INFO("Decoder revision: 0x%08x\n", data[0]); 155 } 156 } 157 158 static int ivtv_firmware_copy(struct ivtv *itv) 159 { 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"); 164 return -3; 165 } 166 if (!itv->has_cx23415) 167 return 0; 168 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"); 173 return -1; 174 } 175 return 0; 176 } 177 178 static volatile struct ivtv_mailbox __iomem *ivtv_search_mailbox(const volatile u8 __iomem *mem, u32 size) 179 { 180 int i; 181 182 /* mailbox is preceded by a 16 byte 'magic cookie' starting at a 256-byte 183 address boundary */ 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); 190 } 191 } 192 return NULL; 193 } 194 195 int ivtv_firmware_init(struct ivtv *itv) 196 { 197 int err; 198 199 ivtv_halt_firmware(itv); 200 201 /* load firmware */ 202 err = ivtv_firmware_copy(itv); 203 if (err) { 204 IVTV_DEBUG_WARN("Error %d loading firmware\n", err); 205 return err; 206 } 207 208 /* start firmware */ 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); 213 else 214 write_reg(read_reg(IVTV_REG_VPU) & IVTV_MASK_VPU_ENABLE16, IVTV_REG_VPU); 215 ivtv_msleep_timeout(100, 0); 216 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; 224 } 225 if (itv->enc_mbox.mbox == NULL) 226 return -ENODEV; 227 228 if (!itv->has_cx23415) 229 return 0; 230 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; 237 } else { 238 /* Firmware okay, so check yuv output filter table */ 239 ivtv_yuv_filter_check(itv); 240 } 241 return itv->dec_mbox.mbox ? 0 : -ENODEV; 242 } 243 244 void ivtv_init_mpeg_decoder(struct ivtv *itv) 245 { 246 u32 data[CX2341X_MBOX_MAX_DATA]; 247 long readbytes; 248 volatile u8 __iomem *mem_offset; 249 250 data[0] = 0; 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, 254 bitmap. see docs. */ 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"); 257 return; 258 } 259 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"); 262 return; 263 } 264 ivtv_api_get_data(&itv->dec_mbox, IVTV_MBOX_DMA, 2, data); 265 mem_offset = itv->dec_mem + data[1]; 266 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); 271 } else { 272 ivtv_vapi(itv, CX2341X_DEC_SCHED_DMA_FROM_HOST, 3, 0, readbytes, 0); 273 ivtv_msleep_timeout(100, 0); 274 } 275 ivtv_vapi(itv, CX2341X_DEC_STOP_PLAYBACK, 4, 0, 0, 0, 1); 276 } 277 278 /* Try to restart the card & restore previous settings */ 279 static int ivtv_firmware_restart(struct ivtv *itv) 280 { 281 int rc = 0; 282 v4l2_std_id std; 283 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, 289 0); 290 291 mutex_lock(&itv->udma.lock); 292 293 rc = ivtv_firmware_init(itv); 294 if (rc) { 295 mutex_unlock(&itv->udma.lock); 296 return rc; 297 } 298 299 /* Allow settings to reload */ 300 ivtv_mailbox_cache_invalidate(itv); 301 302 /* Restore encoder video standard */ 303 std = itv->std; 304 itv->std = 0; 305 ivtv_s_std_enc(itv, std); 306 307 if (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT) { 308 ivtv_init_mpeg_decoder(itv); 309 310 /* Restore decoder video standard */ 311 std = itv->std_out; 312 itv->std_out = 0; 313 ivtv_s_std_dec(itv, std); 314 315 /* Restore framebuffer if active */ 316 if (itv->ivtvfb_restore) 317 itv->ivtvfb_restore(itv); 318 319 /* Restore alpha settings */ 320 ivtv_set_osd_alpha(itv); 321 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, 326 0); 327 } 328 329 mutex_unlock(&itv->udma.lock); 330 return rc; 331 } 332 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) 336 { 337 int res = 0; 338 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); 342 res = -1; 343 } 344 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, 349 &itv->i_flags)))) { 350 351 if (ivtv_vapi(itv, CX2341X_ENC_MISC, 1, 12) < 0) { 352 IVTV_WARN("Audio has died (Encoder OK) : %s\n", where); 353 res = -2; 354 } 355 } 356 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", 364 where); 365 res = -1; 366 } 367 } 368 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); 372 res = -1; 373 } 374 } 375 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); 382 /* 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. 386 */ 387 if (!res) { 388 IVTV_INFO("Firmware restart okay\n"); 389 res = -EAGAIN; 390 } else { 391 IVTV_INFO("Firmware restart failed\n"); 392 } 393 } else if (res) { 394 res = -EIO; 395 } 396 397 return res; 398 } 399 400 MODULE_FIRMWARE(CX2341X_FIRM_ENC_FILENAME); 401 MODULE_FIRMWARE(CX2341X_FIRM_DEC_FILENAME); 402 MODULE_FIRMWARE(IVTV_DECODE_INIT_MPEG_FILENAME); 403