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