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