2 * Driver for the NXP SAA7164 PCIe bridge
4 * Copyright (c) 2009 Steven Toth <stoth@kernellabs.com>
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.
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
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., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <linux/firmware.h>
26 #define SAA7164_REV2_FIRMWARE "v4l-saa7164-1.0.2.fw"
27 #define SAA7164_REV2_FIRMWARE_SIZE 3978608
29 #define SAA7164_REV3_FIRMWARE "v4l-saa7164-1.0.3.fw"
30 #define SAA7164_REV3_FIRMWARE_SIZE 3978608
39 int saa7164_dl_wait_ack(struct saa7164_dev
*dev
, u32 reg
)
41 u32 timeout
= SAA_DEVICE_TIMEOUT
;
42 while ((saa7164_readl(reg
) & 0x01) == 0) {
45 printk(KERN_ERR
"%s() timeout (no d/l ack)\n",
55 int saa7164_dl_wait_clr(struct saa7164_dev
*dev
, u32 reg
)
57 u32 timeout
= SAA_DEVICE_TIMEOUT
;
58 while (saa7164_readl(reg
) & 0x01) {
61 printk(KERN_ERR
"%s() timeout (no d/l clr)\n",
71 /* TODO: move dlflags into dev-> and change to write/readl/b */
72 /* TODO: Excessive levels of debug */
73 int saa7164_downloadimage(struct saa7164_dev
*dev
, u8
*src
, u32 srcsize
,
74 u32 dlflags
, u8
*dst
, u32 dstsize
)
76 u32 reg
, timeout
, offset
;
81 u32 dlflag_ack
= dlflag
+ 4;
82 u32 drflag
= dlflag_ack
+ 4;
83 u32 drflag_ack
= drflag
+ 4;
84 u32 bleflag
= drflag_ack
+ 4;
87 "%s(image=%p, size=%d, flags=0x%x, dst=%p, dstsize=0x%x)\n",
88 __func__
, src
, srcsize
, dlflags
, dst
, dstsize
);
90 if ((src
== 0) || (dst
== 0)) {
95 srcbuf
= kzalloc(4 * 1048576, GFP_KERNEL
);
101 if (srcsize
> (4*1048576)) {
106 memcpy(srcbuf
, src
, srcsize
);
108 dprintk(DBGLVL_FW
, "%s() dlflag = 0x%x\n", __func__
, dlflag
);
109 dprintk(DBGLVL_FW
, "%s() dlflag_ack = 0x%x\n", __func__
, dlflag_ack
);
110 dprintk(DBGLVL_FW
, "%s() drflag = 0x%x\n", __func__
, drflag
);
111 dprintk(DBGLVL_FW
, "%s() drflag_ack = 0x%x\n", __func__
, drflag_ack
);
112 dprintk(DBGLVL_FW
, "%s() bleflag = 0x%x\n", __func__
, bleflag
);
114 reg
= saa7164_readl(dlflag
);
115 dprintk(DBGLVL_FW
, "%s() dlflag (0x%x)= 0x%x\n", __func__
, dlflag
, reg
);
118 "%s() Download flag already set, please reboot\n",
121 /* Indicate download start */
122 saa7164_writel(dlflag
, 1);
123 ret
= saa7164_dl_wait_ack(dev
, dlflag_ack
);
127 /* Ack download start, then wait for wait */
128 saa7164_writel(dlflag
, 0);
129 ret
= saa7164_dl_wait_clr(dev
, dlflag_ack
);
133 /* Deal with the raw firmware, in the appropriate chunk size */
134 for (offset
= 0; srcsize
> dstsize
;
135 srcsize
-= dstsize
, offset
+= dstsize
) {
137 dprintk(DBGLVL_FW
, "%s() memcpy %d\n", __func__
, dstsize
);
138 memcpy(dst
, srcbuf
+ offset
, dstsize
);
140 /* Flag the data as ready */
141 saa7164_writel(drflag
, 1);
142 ret
= saa7164_dl_wait_ack(dev
, drflag_ack
);
146 /* Wait for indication data was received */
147 saa7164_writel(drflag
, 0);
148 ret
= saa7164_dl_wait_clr(dev
, drflag_ack
);
154 dprintk(DBGLVL_FW
, "%s() memcpy(l) %d\n", __func__
, dstsize
);
155 /* Write last block to the device */
156 memcpy(dst
, srcbuf
+offset
, srcsize
);
158 /* Flag the data as ready */
159 saa7164_writel(drflag
, 1);
160 ret
= saa7164_dl_wait_ack(dev
, drflag_ack
);
164 saa7164_writel(drflag
, 0);
166 while (saa7164_readl(bleflag
) != SAA_DEVICE_IMAGE_BOOTING
) {
167 if (saa7164_readl(bleflag
) & SAA_DEVICE_IMAGE_CORRUPT
) {
168 printk(KERN_ERR
"%s() image corrupt\n", __func__
);
173 if (saa7164_readl(bleflag
) & SAA_DEVICE_MEMORY_CORRUPT
) {
174 printk(KERN_ERR
"%s() device memory corrupt\n",
185 printk(KERN_INFO
"%s() Image downloaded, booting...\n", __func__
);
187 ret
= saa7164_dl_wait_clr(dev
, drflag_ack
);
191 printk(KERN_INFO
"%s() Image booted successfully.\n", __func__
);
199 /* TODO: Excessive debug */
200 /* Load the firmware. Optionally it can be in ROM or newer versions
201 * can be on disk, saving the expense of the ROM hardware. */
202 int saa7164_downloadfirmware(struct saa7164_dev
*dev
)
204 /* u32 second_timeout = 60 * SAA_DEVICE_TIMEOUT; */
205 u32 tmp
, filesize
, version
, err_flags
, first_timeout
, fwlength
;
206 u32 second_timeout
, updatebootloader
= 1, bootloadersize
= 0;
207 const struct firmware
*fw
= NULL
;
208 struct fw_header
*hdr
, *boothdr
= NULL
, *fwhdr
;
209 u32 bootloaderversion
= 0, fwloadersize
;
210 u8
*bootloaderoffset
= NULL
, *fwloaderoffset
;
214 dprintk(DBGLVL_FW
, "%s()\n", __func__
);
216 if (saa7164_boards
[dev
->board
].chiprev
== SAA7164_CHIP_REV2
) {
217 fwname
= SAA7164_REV2_FIRMWARE
;
218 fwlength
= SAA7164_REV2_FIRMWARE_SIZE
;
220 fwname
= SAA7164_REV3_FIRMWARE
;
221 fwlength
= SAA7164_REV3_FIRMWARE_SIZE
;
224 version
= saa7164_getcurrentfirmwareversion(dev
);
226 if (version
== 0x00) {
228 second_timeout
= 100;
230 err_flags
= saa7164_readl(SAA_BOOTLOADERERROR_FLAGS
);
231 dprintk(DBGLVL_FW
, "%s() err_flags = %x\n",
232 __func__
, err_flags
);
234 while (err_flags
!= SAA_DEVICE_IMAGE_BOOTING
) {
235 dprintk(DBGLVL_FW
, "%s() err_flags = %x\n",
236 __func__
, err_flags
);
239 if (err_flags
& SAA_DEVICE_IMAGE_CORRUPT
) {
240 printk(KERN_ERR
"%s() firmware corrupt\n",
244 if (err_flags
& SAA_DEVICE_MEMORY_CORRUPT
) {
245 printk(KERN_ERR
"%s() device memory corrupt\n",
249 if (err_flags
& SAA_DEVICE_NO_IMAGE
) {
250 printk(KERN_ERR
"%s() no first image\n",
254 if (err_flags
& SAA_DEVICE_IMAGE_SEARCHING
) {
256 if (first_timeout
== 0) {
258 "%s() no first image\n",
262 } else if (err_flags
& SAA_DEVICE_IMAGE_LOADING
) {
263 second_timeout
-= 10;
264 if (second_timeout
== 0) {
266 "%s() FW load time exceeded\n",
271 second_timeout
-= 10;
272 if (second_timeout
== 0) {
274 "%s() Unknown bootloader flags 0x%x\n",
275 __func__
, err_flags
);
280 err_flags
= saa7164_readl(SAA_BOOTLOADERERROR_FLAGS
);
281 } /* While != Booting */
283 if (err_flags
== SAA_DEVICE_IMAGE_BOOTING
) {
284 dprintk(DBGLVL_FW
, "%s() Loader 1 has loaded.\n",
286 first_timeout
= SAA_DEVICE_TIMEOUT
;
287 second_timeout
= 60 * SAA_DEVICE_TIMEOUT
;
288 second_timeout
= 100;
290 err_flags
= saa7164_readl(SAA_SECONDSTAGEERROR_FLAGS
);
291 dprintk(DBGLVL_FW
, "%s() err_flags2 = %x\n",
292 __func__
, err_flags
);
293 while (err_flags
!= SAA_DEVICE_IMAGE_BOOTING
) {
294 dprintk(DBGLVL_FW
, "%s() err_flags2 = %x\n",
295 __func__
, err_flags
);
298 if (err_flags
& SAA_DEVICE_IMAGE_CORRUPT
) {
300 "%s() firmware corrupt\n",
304 if (err_flags
& SAA_DEVICE_MEMORY_CORRUPT
) {
306 "%s() device memory corrupt\n",
310 if (err_flags
& SAA_DEVICE_NO_IMAGE
) {
311 printk(KERN_ERR
"%s() no first image\n",
315 if (err_flags
& SAA_DEVICE_IMAGE_SEARCHING
) {
317 if (first_timeout
== 0) {
319 "%s() no second image\n",
323 } else if (err_flags
&
324 SAA_DEVICE_IMAGE_LOADING
) {
325 second_timeout
-= 10;
326 if (second_timeout
== 0) {
328 "%s() FW load time exceeded\n",
333 second_timeout
-= 10;
334 if (second_timeout
== 0) {
336 "%s() Unknown bootloader flags 0x%x\n",
337 __func__
, err_flags
);
343 saa7164_readl(SAA_SECONDSTAGEERROR_FLAGS
);
344 } /* err_flags != SAA_DEVICE_IMAGE_BOOTING */
346 dprintk(DBGLVL_FW
, "%s() Loader flags 1:0x%x 2:0x%x.\n",
348 saa7164_readl(SAA_BOOTLOADERERROR_FLAGS
),
349 saa7164_readl(SAA_SECONDSTAGEERROR_FLAGS
));
351 } /* err_flags == SAA_DEVICE_IMAGE_BOOTING */
353 /* It's possible for both firmwares to have booted,
354 * but that doesn't mean they've finished booting yet.
356 if ((saa7164_readl(SAA_BOOTLOADERERROR_FLAGS
) ==
357 SAA_DEVICE_IMAGE_BOOTING
) &&
358 (saa7164_readl(SAA_SECONDSTAGEERROR_FLAGS
) ==
359 SAA_DEVICE_IMAGE_BOOTING
)) {
362 dprintk(DBGLVL_FW
, "%s() Loader 2 has loaded.\n",
365 first_timeout
= SAA_DEVICE_TIMEOUT
;
366 while (first_timeout
) {
370 saa7164_getcurrentfirmwareversion(dev
);
373 "%s() All f/w loaded successfully\n",
378 if (first_timeout
== 0) {
380 "%s() FW did not boot\n",
387 version
= saa7164_getcurrentfirmwareversion(dev
);
390 /* Has the firmware really booted? */
391 if ((saa7164_readl(SAA_BOOTLOADERERROR_FLAGS
) ==
392 SAA_DEVICE_IMAGE_BOOTING
) &&
393 (saa7164_readl(SAA_SECONDSTAGEERROR_FLAGS
) ==
394 SAA_DEVICE_IMAGE_BOOTING
) && (version
== 0)) {
397 "%s() The firmware hung, probably bad firmware\n",
400 /* Tell the second stage loader we have a deadlock */
401 saa7164_writel(SAA_DEVICE_DEADLOCK_DETECTED_OFFSET
,
402 SAA_DEVICE_DEADLOCK_DETECTED
);
404 saa7164_getfirmwarestatus(dev
);
409 dprintk(DBGLVL_FW
, "Device has Firmware Version %d.%d.%d.%d\n",
410 (version
& 0x0000fc00) >> 10,
411 (version
& 0x000003e0) >> 5,
412 (version
& 0x0000001f),
413 (version
& 0xffff0000) >> 16);
415 /* Load the firmwware from the disk if required */
418 printk(KERN_INFO
"%s() Waiting for firmware upload (%s)\n",
421 ret
= request_firmware(&fw
, fwname
, &dev
->pci
->dev
);
423 printk(KERN_ERR
"%s() Upload failed. "
424 "(file not found?)\n", __func__
);
428 printk(KERN_INFO
"%s() firmware read %Zu bytes.\n",
431 if (fw
->size
!= fwlength
) {
432 printk(KERN_ERR
"xc5000: firmware incorrect size\n");
437 printk(KERN_INFO
"%s() firmware loaded.\n", __func__
);
439 hdr
= (struct fw_header
*)fw
->data
;
440 printk(KERN_INFO
"Firmware file header part 1:\n");
441 printk(KERN_INFO
" .FirmwareSize = 0x%x\n", hdr
->firmwaresize
);
442 printk(KERN_INFO
" .BSLSize = 0x%x\n", hdr
->bslsize
);
443 printk(KERN_INFO
" .Reserved = 0x%x\n", hdr
->reserved
);
444 printk(KERN_INFO
" .Version = 0x%x\n", hdr
->version
);
446 /* Retreive bootloader if reqd */
447 if ((hdr
->firmwaresize
== 0) && (hdr
->bslsize
== 0))
448 /* Second bootloader in the firmware file */
449 filesize
= hdr
->reserved
* 16;
451 filesize
= (hdr
->firmwaresize
+ hdr
->bslsize
) *
452 16 + sizeof(struct fw_header
);
454 printk(KERN_INFO
"%s() SecBootLoader.FileSize = %d\n",
457 /* Get bootloader (if reqd) and firmware header */
458 if ((hdr
->firmwaresize
== 0) && (hdr
->bslsize
== 0)) {
459 /* Second boot loader is required */
461 /* Get the loader header */
462 boothdr
= (struct fw_header
*)(fw
->data
+
463 sizeof(struct fw_header
));
466 saa7164_readl(SAA_DEVICE_2ND_VERSION
);
467 dprintk(DBGLVL_FW
, "Onboard BootLoader:\n");
468 dprintk(DBGLVL_FW
, "->Flag 0x%x\n",
469 saa7164_readl(SAA_BOOTLOADERERROR_FLAGS
));
470 dprintk(DBGLVL_FW
, "->Ack 0x%x\n",
471 saa7164_readl(SAA_DATAREADY_FLAG_ACK
));
472 dprintk(DBGLVL_FW
, "->FW Version 0x%x\n", version
);
473 dprintk(DBGLVL_FW
, "->Loader Version 0x%x\n",
476 if ((saa7164_readl(SAA_BOOTLOADERERROR_FLAGS
) ==
477 0x03) && (saa7164_readl(SAA_DATAREADY_FLAG_ACK
)
478 == 0x00) && (version
== 0x00)) {
480 dprintk(DBGLVL_FW
, "BootLoader version in "
482 (bootloaderversion
& 0x0000fc00) >> 10,
483 (bootloaderversion
& 0x000003e0) >> 5,
484 (bootloaderversion
& 0x0000001f),
485 (bootloaderversion
& 0xffff0000) >> 16
487 dprintk(DBGLVL_FW
, "BootLoader version "
488 "in file %d.%d.%d.%d\n",
489 (boothdr
->version
& 0x0000fc00) >> 10,
490 (boothdr
->version
& 0x000003e0) >> 5,
491 (boothdr
->version
& 0x0000001f),
492 (boothdr
->version
& 0xffff0000) >> 16
495 if (bootloaderversion
== boothdr
->version
)
496 updatebootloader
= 0;
499 /* Calculate offset to firmware header */
500 tmp
= (boothdr
->firmwaresize
+ boothdr
->bslsize
) * 16 +
501 (sizeof(struct fw_header
) +
502 sizeof(struct fw_header
));
504 fwhdr
= (struct fw_header
*)(fw
->data
+tmp
);
506 /* No second boot loader */
510 dprintk(DBGLVL_FW
, "Firmware version in file %d.%d.%d.%d\n",
511 (fwhdr
->version
& 0x0000fc00) >> 10,
512 (fwhdr
->version
& 0x000003e0) >> 5,
513 (fwhdr
->version
& 0x0000001f),
514 (fwhdr
->version
& 0xffff0000) >> 16
517 if (version
== fwhdr
->version
) {
518 /* No download, firmware already on board */
523 if ((hdr
->firmwaresize
== 0) && (hdr
->bslsize
== 0)) {
524 if (updatebootloader
) {
525 /* Get ready to upload the bootloader */
526 bootloadersize
= (boothdr
->firmwaresize
+
527 boothdr
->bslsize
) * 16 +
528 sizeof(struct fw_header
);
530 bootloaderoffset
= (u8
*)(fw
->data
+
531 sizeof(struct fw_header
));
533 dprintk(DBGLVL_FW
, "bootloader d/l starts.\n");
534 printk(KERN_INFO
"%s() FirmwareSize = 0x%x\n",
535 __func__
, boothdr
->firmwaresize
);
536 printk(KERN_INFO
"%s() BSLSize = 0x%x\n",
537 __func__
, boothdr
->bslsize
);
538 printk(KERN_INFO
"%s() Reserved = 0x%x\n",
539 __func__
, boothdr
->reserved
);
540 printk(KERN_INFO
"%s() Version = 0x%x\n",
541 __func__
, boothdr
->version
);
542 ret
= saa7164_downloadimage(
547 dev
->bmmio
+ SAA_DEVICE_DOWNLOAD_OFFSET
,
548 SAA_DEVICE_BUFFERBLOCKSIZE
);
551 "bootloader d/l has failed\n");
555 "bootloader download complete.\n");
559 printk(KERN_ERR
"starting firmware download(2)\n");
560 bootloadersize
= (boothdr
->firmwaresize
+
561 boothdr
->bslsize
) * 16 +
562 sizeof(struct fw_header
);
565 (u8
*)(fw
->data
+ sizeof(struct fw_header
));
567 fwloaderoffset
= bootloaderoffset
+ bootloadersize
;
569 /* TODO: fix this bounds overrun here with old f/ws */
570 fwloadersize
= (fwhdr
->firmwaresize
+ fwhdr
->bslsize
) *
571 16 + sizeof(struct fw_header
);
573 ret
= saa7164_downloadimage(
577 SAA_DEVICE_2ND_DOWNLOADFLAG_OFFSET
,
578 dev
->bmmio
+ SAA_DEVICE_2ND_DOWNLOAD_OFFSET
,
579 SAA_DEVICE_2ND_BUFFERBLOCKSIZE
);
581 printk(KERN_ERR
"firmware download failed\n");
584 printk(KERN_ERR
"firmware download complete.\n");
588 /* No bootloader update reqd, download firmware only */
589 printk(KERN_ERR
"starting firmware download(3)\n");
591 ret
= saa7164_downloadimage(
596 dev
->bmmio
+ SAA_DEVICE_DOWNLOAD_OFFSET
,
597 SAA_DEVICE_BUFFERBLOCKSIZE
);
599 printk(KERN_ERR
"firmware download failed\n");
602 printk(KERN_ERR
"firmware download complete.\n");
610 release_firmware(fw
);