aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/video/saa7164/saa7164-fw.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/media/video/saa7164/saa7164-fw.c')
-rw-r--r--drivers/media/video/saa7164/saa7164-fw.c615
1 files changed, 615 insertions, 0 deletions
diff --git a/drivers/media/video/saa7164/saa7164-fw.c b/drivers/media/video/saa7164/saa7164-fw.c
new file mode 100644
index 000000000000..6595dd67c5c3
--- /dev/null
+++ b/drivers/media/video/saa7164/saa7164-fw.c
@@ -0,0 +1,615 @@
1/*
2 * Driver for the NXP SAA7164 PCIe bridge
3 *
4 * Copyright (c) 2009 Steven Toth <stoth@kernellabs.com>
5 *
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 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 *
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., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22#include <linux/firmware.h>
23
24#include "saa7164.h"
25
26#define SAA7164_REV2_FIRMWARE "v4l-saa7164-1.0.2.fw"
27#define SAA7164_REV2_FIRMWARE_SIZE 3978608
28
29#define SAA7164_REV3_FIRMWARE "v4l-saa7164-1.0.3.fw"
30#define SAA7164_REV3_FIRMWARE_SIZE 3978608
31
32struct fw_header {
33 u32 firmwaresize;
34 u32 bslsize;
35 u32 reserved;
36 u32 version;
37};
38
39int saa7164_dl_wait_ack(struct saa7164_dev *dev, u32 reg)
40{
41 u32 timeout = SAA_DEVICE_TIMEOUT;
42 while ((saa7164_readl(reg) & 0x01) == 0) {
43 timeout -= 5;
44 if (timeout == 0) {
45 printk(KERN_ERR "%s() timeout (no d/l ack)\n",
46 __func__);
47 return -EBUSY;
48 }
49 /* TODO: Review this for efficiency, f/w load is slow */
50 msleep(1);
51 }
52
53 return 0;
54}
55
56int saa7164_dl_wait_clr(struct saa7164_dev *dev, u32 reg)
57{
58 u32 timeout = SAA_DEVICE_TIMEOUT;
59 while (saa7164_readl(reg) & 0x01) {
60 timeout -= 5;
61 if (timeout == 0) {
62 printk(KERN_ERR "%s() timeout (no d/l clr)\n",
63 __func__);
64 return -EBUSY;
65 }
66 /* TODO: Review this for efficiency, f/w load is slow */
67 msleep(1);
68 }
69
70 return 0;
71}
72
73/* TODO: move dlflags into dev-> and change to write/readl/b */
74/* TODO: Excessive levels of debug */
75int saa7164_downloadimage(struct saa7164_dev *dev, u8 *src, u32 srcsize,
76 u32 dlflags, u8 *dst, u32 dstsize)
77{
78 u32 reg, timeout, offset;
79 u8 *srcbuf = NULL;
80 int ret;
81
82 u32 dlflag = dlflags;
83 u32 dlflag_ack = dlflag + 4;
84 u32 drflag = dlflag_ack + 4;
85 u32 drflag_ack = drflag + 4;
86 u32 bleflag = drflag_ack + 4;
87
88 dprintk(DBGLVL_FW,
89 "%s(image=%p, size=%d, flags=0x%x, dst=%p, dstsize=0x%x)\n",
90 __func__, src, srcsize, dlflags, dst, dstsize);
91
92 if ((src == 0) || (dst == 0)) {
93 ret = -EIO;
94 goto out;
95 }
96
97 srcbuf = kzalloc(4 * 1048576, GFP_KERNEL);
98 if (NULL == srcbuf) {
99 ret = -ENOMEM;
100 goto out;
101 }
102
103 if (srcsize > (4*1048576)) {
104 ret = -ENOMEM;
105 goto out;
106 }
107
108 memcpy(srcbuf, src, srcsize);
109
110 dprintk(DBGLVL_FW, "%s() dlflag = 0x%x\n", __func__, dlflag);
111 dprintk(DBGLVL_FW, "%s() dlflag_ack = 0x%x\n", __func__, dlflag_ack);
112 dprintk(DBGLVL_FW, "%s() drflag = 0x%x\n", __func__, drflag);
113 dprintk(DBGLVL_FW, "%s() drflag_ack = 0x%x\n", __func__, drflag_ack);
114 dprintk(DBGLVL_FW, "%s() bleflag = 0x%x\n", __func__, bleflag);
115
116 reg = saa7164_readl(dlflag);
117 dprintk(DBGLVL_FW, "%s() dlflag (0x%x)= 0x%x\n", __func__, dlflag, reg);
118 if (reg == 1)
119 dprintk(DBGLVL_FW,
120 "%s() Download flag already set, please reboot\n",
121 __func__);
122
123 /* Indicate download start */
124 saa7164_writel(dlflag, 1);
125 ret = saa7164_dl_wait_ack(dev, dlflag_ack);
126 if (ret < 0)
127 goto out;
128
129 /* Ack download start, then wait for wait */
130 saa7164_writel(dlflag, 0);
131 ret = saa7164_dl_wait_clr(dev, dlflag_ack);
132 if (ret < 0)
133 goto out;
134
135 /* Deal with the raw firmware, in the appropriate chunk size */
136 for (offset = 0; srcsize > dstsize;
137 srcsize -= dstsize, offset += dstsize) {
138
139 dprintk(DBGLVL_FW, "%s() memcpy %d\n", __func__, dstsize);
140 memcpy(dst, srcbuf + offset, dstsize);
141
142 /* Flag the data as ready */
143 saa7164_writel(drflag, 1);
144 ret = saa7164_dl_wait_ack(dev, drflag_ack);
145 if (ret < 0)
146 goto out;
147
148 /* Wait for indication data was received */
149 saa7164_writel(drflag, 0);
150 ret = saa7164_dl_wait_clr(dev, drflag_ack);
151 if (ret < 0)
152 goto out;
153
154 }
155
156 dprintk(DBGLVL_FW, "%s() memcpy(l) %d\n", __func__, dstsize);
157 /* Write last block to the device */
158 memcpy(dst, srcbuf+offset, srcsize);
159
160 /* Flag the data as ready */
161 saa7164_writel(drflag, 1);
162 ret = saa7164_dl_wait_ack(dev, drflag_ack);
163 if (ret < 0)
164 goto out;
165
166 saa7164_writel(drflag, 0);
167 timeout = 0;
168 while (saa7164_readl(bleflag) != SAA_DEVICE_IMAGE_BOOTING) {
169 if (saa7164_readl(bleflag) & SAA_DEVICE_IMAGE_CORRUPT) {
170 printk(KERN_ERR "%s() image corrupt\n", __func__);
171 ret = -EBUSY;
172 goto out;
173 }
174
175 if (saa7164_readl(bleflag) & SAA_DEVICE_MEMORY_CORRUPT) {
176 printk(KERN_ERR "%s() device memory corrupt\n",
177 __func__);
178 ret = -EBUSY;
179 goto out;
180 }
181
182 msleep(10);
183 if (timeout++ > 60)
184 break;
185 }
186
187 printk(KERN_INFO "%s() Image downloaded, booting...\n", __func__);
188
189 ret = saa7164_dl_wait_clr(dev, drflag_ack);
190 if (ret < 0)
191 goto out;
192
193 printk(KERN_INFO "%s() Image booted successfully.\n", __func__);
194 ret = 0;
195
196out:
197 kfree(srcbuf);
198 return ret;
199}
200
201/* TODO: Excessive debug */
202/* Load the firmware. Optionally it can be in ROM or newer versions
203 * can be on disk, saving the expense of the ROM hardware. */
204int saa7164_downloadfirmware(struct saa7164_dev *dev)
205{
206 /* u32 second_timeout = 60 * SAA_DEVICE_TIMEOUT; */
207 u32 tmp, filesize, version, err_flags, first_timeout, fwlength;
208 u32 second_timeout, updatebootloader = 1, bootloadersize = 0;
209 const struct firmware *fw = NULL;
210 struct fw_header *hdr, *boothdr = NULL, *fwhdr;
211 u32 bootloaderversion = 0, fwloadersize;
212 u8 *bootloaderoffset = NULL, *fwloaderoffset;
213 char *fwname;
214 int ret;
215
216 dprintk(DBGLVL_FW, "%s()\n", __func__);
217
218 if (saa7164_boards[dev->board].chiprev == SAA7164_CHIP_REV2) {
219 fwname = SAA7164_REV2_FIRMWARE;
220 fwlength = SAA7164_REV2_FIRMWARE_SIZE;
221 } else {
222 fwname = SAA7164_REV3_FIRMWARE;
223 fwlength = SAA7164_REV3_FIRMWARE_SIZE;
224 }
225
226 version = saa7164_getcurrentfirmwareversion(dev);
227
228 if (version == 0x00) {
229
230 second_timeout = 100;
231 first_timeout = 100;
232 err_flags = saa7164_readl(SAA_BOOTLOADERERROR_FLAGS);
233 dprintk(DBGLVL_FW, "%s() err_flags = %x\n",
234 __func__, err_flags);
235
236 while (err_flags != SAA_DEVICE_IMAGE_BOOTING) {
237 dprintk(DBGLVL_FW, "%s() err_flags = %x\n",
238 __func__, err_flags);
239 msleep(10);
240
241 if (err_flags & SAA_DEVICE_IMAGE_CORRUPT) {
242 printk(KERN_ERR "%s() firmware corrupt\n",
243 __func__);
244 break;
245 }
246 if (err_flags & SAA_DEVICE_MEMORY_CORRUPT) {
247 printk(KERN_ERR "%s() device memory corrupt\n",
248 __func__);
249 break;
250 }
251 if (err_flags & SAA_DEVICE_NO_IMAGE) {
252 printk(KERN_ERR "%s() no first image\n",
253 __func__);
254 break;
255 }
256 if (err_flags & SAA_DEVICE_IMAGE_SEARCHING) {
257 first_timeout -= 10;
258 if (first_timeout == 0) {
259 printk(KERN_ERR
260 "%s() no first image\n",
261 __func__);
262 break;
263 }
264 } else if (err_flags & SAA_DEVICE_IMAGE_LOADING) {
265 second_timeout -= 10;
266 if (second_timeout == 0) {
267 printk(KERN_ERR
268 "%s() FW load time exceeded\n",
269 __func__);
270 break;
271 }
272 } else {
273 second_timeout -= 10;
274 if (second_timeout == 0) {
275 printk(KERN_ERR
276 "%s() Unknown bootloader flags 0x%x\n",
277 __func__, err_flags);
278 break;
279 }
280 }
281
282 err_flags = saa7164_readl(SAA_BOOTLOADERERROR_FLAGS);
283 } /* While != Booting */
284
285 if (err_flags == SAA_DEVICE_IMAGE_BOOTING) {
286 dprintk(DBGLVL_FW, "%s() Loader 1 has loaded.\n",
287 __func__);
288 first_timeout = SAA_DEVICE_TIMEOUT;
289 second_timeout = 60 * SAA_DEVICE_TIMEOUT;
290 second_timeout = 100;
291
292 err_flags = saa7164_readl(SAA_SECONDSTAGEERROR_FLAGS);
293 dprintk(DBGLVL_FW, "%s() err_flags2 = %x\n",
294 __func__, err_flags);
295 while (err_flags != SAA_DEVICE_IMAGE_BOOTING) {
296 dprintk(DBGLVL_FW, "%s() err_flags2 = %x\n",
297 __func__, err_flags);
298 msleep(10);
299
300 if (err_flags & SAA_DEVICE_IMAGE_CORRUPT) {
301 printk(KERN_ERR
302 "%s() firmware corrupt\n",
303 __func__);
304 break;
305 }
306 if (err_flags & SAA_DEVICE_MEMORY_CORRUPT) {
307 printk(KERN_ERR
308 "%s() device memory corrupt\n",
309 __func__);
310 break;
311 }
312 if (err_flags & SAA_DEVICE_NO_IMAGE) {
313 printk(KERN_ERR "%s() no first image\n",
314 __func__);
315 break;
316 }
317 if (err_flags & SAA_DEVICE_IMAGE_SEARCHING) {
318 first_timeout -= 10;
319 if (first_timeout == 0) {
320 printk(KERN_ERR
321 "%s() no second image\n",
322 __func__);
323 break;
324 }
325 } else if (err_flags &
326 SAA_DEVICE_IMAGE_LOADING) {
327 second_timeout -= 10;
328 if (second_timeout == 0) {
329 printk(KERN_ERR
330 "%s() FW load time exceeded\n",
331 __func__);
332 break;
333 }
334 } else {
335 second_timeout -= 10;
336 if (second_timeout == 0) {
337 printk(KERN_ERR
338 "%s() Unknown bootloader flags 0x%x\n",
339 __func__, err_flags);
340 break;
341 }
342 }
343
344 err_flags =
345 saa7164_readl(SAA_SECONDSTAGEERROR_FLAGS);
346 } /* err_flags != SAA_DEVICE_IMAGE_BOOTING */
347
348 dprintk(DBGLVL_FW, "%s() Loader flags 1:0x%x 2:0x%x.\n",
349 __func__,
350 saa7164_readl(SAA_BOOTLOADERERROR_FLAGS),
351 saa7164_readl(SAA_SECONDSTAGEERROR_FLAGS));
352
353 } /* err_flags == SAA_DEVICE_IMAGE_BOOTING */
354
355 /* It's possible for both firmwares to have booted,
356 * but that doesn't mean they've finished booting yet.
357 */
358 if ((saa7164_readl(SAA_BOOTLOADERERROR_FLAGS) ==
359 SAA_DEVICE_IMAGE_BOOTING) &&
360 (saa7164_readl(SAA_SECONDSTAGEERROR_FLAGS) ==
361 SAA_DEVICE_IMAGE_BOOTING)) {
362
363
364 dprintk(DBGLVL_FW, "%s() Loader 2 has loaded.\n",
365 __func__);
366
367 first_timeout = SAA_DEVICE_TIMEOUT;
368 while (first_timeout) {
369 msleep(10);
370
371 version =
372 saa7164_getcurrentfirmwareversion(dev);
373 if (version) {
374 dprintk(DBGLVL_FW,
375 "%s() All f/w loaded successfully\n",
376 __func__);
377 break;
378 } else {
379 first_timeout -= 10;
380 if (first_timeout == 0) {
381 printk(KERN_ERR
382 "%s() FW did not boot\n",
383 __func__);
384 break;
385 }
386 }
387 }
388 }
389 version = saa7164_getcurrentfirmwareversion(dev);
390 } /* version == 0 */
391
392 /* Has the firmware really booted? */
393 if ((saa7164_readl(SAA_BOOTLOADERERROR_FLAGS) ==
394 SAA_DEVICE_IMAGE_BOOTING) &&
395 (saa7164_readl(SAA_SECONDSTAGEERROR_FLAGS) ==
396 SAA_DEVICE_IMAGE_BOOTING) && (version == 0)) {
397
398 printk(KERN_ERR
399 "%s() The firmware hung, probably bad firmware\n",
400 __func__);
401
402 /* Tell the second stage loader we have a deadlock */
403 saa7164_writel(SAA_DEVICE_DEADLOCK_DETECTED_OFFSET,
404 SAA_DEVICE_DEADLOCK_DETECTED);
405
406 saa7164_getfirmwarestatus(dev);
407
408 return -ENOMEM;
409 }
410
411 dprintk(DBGLVL_FW, "Device has Firmware Version %d.%d.%d.%d\n",
412 (version & 0x0000fc00) >> 10,
413 (version & 0x000003e0) >> 5,
414 (version & 0x0000001f),
415 (version & 0xffff0000) >> 16);
416
417 /* Load the firmwware from the disk if required */
418 if (version == 0) {
419
420 printk(KERN_INFO "%s() Waiting for firmware upload (%s)\n",
421 __func__, fwname);
422
423 ret = request_firmware(&fw, fwname, &dev->pci->dev);
424 if (ret) {
425 printk(KERN_ERR "%s() Upload failed. "
426 "(file not found?)\n", __func__);
427 return -ENOMEM;
428 }
429
430 printk(KERN_INFO "%s() firmware read %Zu bytes.\n",
431 __func__, fw->size);
432
433 if (fw->size != fwlength) {
434 printk(KERN_ERR "xc5000: firmware incorrect size\n");
435 ret = -ENOMEM;
436 goto out;
437 }
438
439 printk(KERN_INFO "%s() firmware loaded.\n", __func__);
440
441 hdr = (struct fw_header *)fw->data;
442 printk(KERN_INFO "Firmware file header part 1:\n");
443 printk(KERN_INFO " .FirmwareSize = 0x%x\n", hdr->firmwaresize);
444 printk(KERN_INFO " .BSLSize = 0x%x\n", hdr->bslsize);
445 printk(KERN_INFO " .Reserved = 0x%x\n", hdr->reserved);
446 printk(KERN_INFO " .Version = 0x%x\n", hdr->version);
447
448 /* Retreive bootloader if reqd */
449 if ((hdr->firmwaresize == 0) && (hdr->bslsize == 0))
450 /* Second bootloader in the firmware file */
451 filesize = hdr->reserved * 16;
452 else
453 filesize = (hdr->firmwaresize + hdr->bslsize) *
454 16 + sizeof(struct fw_header);
455
456 printk(KERN_INFO "%s() SecBootLoader.FileSize = %d\n",
457 __func__, filesize);
458
459 /* Get bootloader (if reqd) and firmware header */
460 if ((hdr->firmwaresize == 0) && (hdr->bslsize == 0)) {
461 /* Second boot loader is required */
462
463 /* Get the loader header */
464 boothdr = (struct fw_header *)(fw->data +
465 sizeof(struct fw_header));
466
467 bootloaderversion =
468 saa7164_readl(SAA_DEVICE_2ND_VERSION);
469 dprintk(DBGLVL_FW, "Onboard BootLoader:\n");
470 dprintk(DBGLVL_FW, "->Flag 0x%x\n",
471 saa7164_readl(SAA_BOOTLOADERERROR_FLAGS));
472 dprintk(DBGLVL_FW, "->Ack 0x%x\n",
473 saa7164_readl(SAA_DATAREADY_FLAG_ACK));
474 dprintk(DBGLVL_FW, "->FW Version 0x%x\n", version);
475 dprintk(DBGLVL_FW, "->Loader Version 0x%x\n",
476 bootloaderversion);
477
478 if ((saa7164_readl(SAA_BOOTLOADERERROR_FLAGS) ==
479 0x03) && (saa7164_readl(SAA_DATAREADY_FLAG_ACK)
480 == 0x00) && (version == 0x00)) {
481
482 dprintk(DBGLVL_FW, "BootLoader version in "
483 "rom %d.%d.%d.%d\n",
484 (bootloaderversion & 0x0000fc00) >> 10,
485 (bootloaderversion & 0x000003e0) >> 5,
486 (bootloaderversion & 0x0000001f),
487 (bootloaderversion & 0xffff0000) >> 16
488 );
489 dprintk(DBGLVL_FW, "BootLoader version "
490 "in file %d.%d.%d.%d\n",
491 (boothdr->version & 0x0000fc00) >> 10,
492 (boothdr->version & 0x000003e0) >> 5,
493 (boothdr->version & 0x0000001f),
494 (boothdr->version & 0xffff0000) >> 16
495 );
496
497 if (bootloaderversion == boothdr->version)
498 updatebootloader = 0;
499 }
500
501 /* Calculate offset to firmware header */
502 tmp = (boothdr->firmwaresize + boothdr->bslsize) * 16 +
503 (sizeof(struct fw_header) +
504 sizeof(struct fw_header));
505
506 fwhdr = (struct fw_header *)(fw->data+tmp);
507 } else {
508 /* No second boot loader */
509 fwhdr = hdr;
510 }
511
512 dprintk(DBGLVL_FW, "Firmware version in file %d.%d.%d.%d\n",
513 (fwhdr->version & 0x0000fc00) >> 10,
514 (fwhdr->version & 0x000003e0) >> 5,
515 (fwhdr->version & 0x0000001f),
516 (fwhdr->version & 0xffff0000) >> 16
517 );
518
519 if (version == fwhdr->version) {
520 /* No download, firmware already on board */
521 ret = 0;
522 goto out;
523 }
524
525 if ((hdr->firmwaresize == 0) && (hdr->bslsize == 0)) {
526 if (updatebootloader) {
527 /* Get ready to upload the bootloader */
528 bootloadersize = (boothdr->firmwaresize +
529 boothdr->bslsize) * 16 +
530 sizeof(struct fw_header);
531
532 bootloaderoffset = (u8 *)(fw->data +
533 sizeof(struct fw_header));
534
535 dprintk(DBGLVL_FW, "bootloader d/l starts.\n");
536 printk(KERN_INFO "%s() FirmwareSize = 0x%x\n",
537 __func__, boothdr->firmwaresize);
538 printk(KERN_INFO "%s() BSLSize = 0x%x\n",
539 __func__, boothdr->bslsize);
540 printk(KERN_INFO "%s() Reserved = 0x%x\n",
541 __func__, boothdr->reserved);
542 printk(KERN_INFO "%s() Version = 0x%x\n",
543 __func__, boothdr->version);
544 ret = saa7164_downloadimage(
545 dev,
546 bootloaderoffset,
547 bootloadersize,
548 SAA_DOWNLOAD_FLAGS,
549 dev->bmmio + SAA_DEVICE_DOWNLOAD_OFFSET,
550 SAA_DEVICE_BUFFERBLOCKSIZE);
551 if (ret < 0) {
552 printk(KERN_ERR
553 "bootloader d/l has failed\n");
554 goto out;
555 }
556 dprintk(DBGLVL_FW,
557 "bootloader download complete.\n");
558
559 }
560
561 printk(KERN_ERR "starting firmware download(2)\n");
562 bootloadersize = (boothdr->firmwaresize +
563 boothdr->bslsize) * 16 +
564 sizeof(struct fw_header);
565
566 bootloaderoffset =
567 (u8 *)(fw->data + sizeof(struct fw_header));
568
569 fwloaderoffset = bootloaderoffset + bootloadersize;
570
571 /* TODO: fix this bounds overrun here with old f/ws */
572 fwloadersize = (fwhdr->firmwaresize + fwhdr->bslsize) *
573 16 + sizeof(struct fw_header);
574
575 ret = saa7164_downloadimage(
576 dev,
577 fwloaderoffset,
578 fwloadersize,
579 SAA_DEVICE_2ND_DOWNLOADFLAG_OFFSET,
580 dev->bmmio + SAA_DEVICE_2ND_DOWNLOAD_OFFSET,
581 SAA_DEVICE_2ND_BUFFERBLOCKSIZE);
582 if (ret < 0) {
583 printk(KERN_ERR "firmware download failed\n");
584 goto out;
585 }
586 printk(KERN_ERR "firmware download complete.\n");
587
588 } else {
589
590 /* No bootloader update reqd, download firmware only */
591 printk(KERN_ERR "starting firmware download(3)\n");
592
593 ret = saa7164_downloadimage(
594 dev,
595 (u8 *)fw->data,
596 fw->size,
597 SAA_DOWNLOAD_FLAGS,
598 dev->bmmio + SAA_DEVICE_DOWNLOAD_OFFSET,
599 SAA_DEVICE_BUFFERBLOCKSIZE);
600 if (ret < 0) {
601 printk(KERN_ERR "firmware download failed\n");
602 goto out;
603 }
604 printk(KERN_ERR "firmware download complete.\n");
605 }
606 }
607
608 ret = 0;
609
610out:
611 if (fw)
612 release_firmware(fw);
613
614 return ret;
615}