2 * Driver for the NXP SAA7164 PCIe bridge
4 * Copyright (c) 2010-2015 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>
23 #include <linux/slab.h>
27 #define SAA7164_REV2_FIRMWARE "NXP7164-2010-03-10.1.fw"
28 #define SAA7164_REV2_FIRMWARE_SIZE 4019072
30 #define SAA7164_REV3_FIRMWARE "NXP7164-2010-03-10.1.fw"
31 #define SAA7164_REV3_FIRMWARE_SIZE 4019072
40 static int saa7164_dl_wait_ack(struct saa7164_dev
*dev
, u32 reg
)
42 u32 timeout
= SAA_DEVICE_TIMEOUT
;
43 while ((saa7164_readl(reg
) & 0x01) == 0) {
46 printk(KERN_ERR
"%s() timeout (no d/l ack)\n",
56 static int saa7164_dl_wait_clr(struct saa7164_dev
*dev
, u32 reg
)
58 u32 timeout
= SAA_DEVICE_TIMEOUT
;
59 while (saa7164_readl(reg
) & 0x01) {
62 printk(KERN_ERR
"%s() timeout (no d/l clr)\n",
72 /* TODO: move dlflags into dev-> and change to write/readl/b */
73 /* TODO: Excessive levels of debug */
74 static int saa7164_downloadimage(struct saa7164_dev
*dev
, u8
*src
, u32 srcsize
,
75 u32 dlflags
, u8 __iomem
*dst
, u32 dstsize
)
77 u32 reg
, timeout
, offset
;
82 u32 dlflag_ack
= dlflag
+ 4;
83 u32 drflag
= dlflag_ack
+ 4;
84 u32 drflag_ack
= drflag
+ 4;
85 u32 bleflag
= drflag_ack
+ 4;
88 "%s(image=%p, size=%d, flags=0x%x, dst=%p, dstsize=0x%x)\n",
89 __func__
, src
, srcsize
, dlflags
, dst
, dstsize
);
91 if ((src
== NULL
) || (dst
== NULL
)) {
96 srcbuf
= kzalloc(4 * 1048576, GFP_KERNEL
);
102 if (srcsize
> (4*1048576)) {
107 memcpy(srcbuf
, src
, srcsize
);
109 dprintk(DBGLVL_FW
, "%s() dlflag = 0x%x\n", __func__
, dlflag
);
110 dprintk(DBGLVL_FW
, "%s() dlflag_ack = 0x%x\n", __func__
, dlflag_ack
);
111 dprintk(DBGLVL_FW
, "%s() drflag = 0x%x\n", __func__
, drflag
);
112 dprintk(DBGLVL_FW
, "%s() drflag_ack = 0x%x\n", __func__
, drflag_ack
);
113 dprintk(DBGLVL_FW
, "%s() bleflag = 0x%x\n", __func__
, bleflag
);
115 reg
= saa7164_readl(dlflag
);
116 dprintk(DBGLVL_FW
, "%s() dlflag (0x%x)= 0x%x\n", __func__
, dlflag
, reg
);
119 "%s() Download flag already set, please reboot\n",
122 /* Indicate download start */
123 saa7164_writel(dlflag
, 1);
124 ret
= saa7164_dl_wait_ack(dev
, dlflag_ack
);
128 /* Ack download start, then wait for wait */
129 saa7164_writel(dlflag
, 0);
130 ret
= saa7164_dl_wait_clr(dev
, dlflag_ack
);
134 /* Deal with the raw firmware, in the appropriate chunk size */
135 for (offset
= 0; srcsize
> dstsize
;
136 srcsize
-= dstsize
, offset
+= dstsize
) {
138 dprintk(DBGLVL_FW
, "%s() memcpy %d\n", __func__
, dstsize
);
139 memcpy_toio(dst
, srcbuf
+ offset
, dstsize
);
141 /* Flag the data as ready */
142 saa7164_writel(drflag
, 1);
143 ret
= saa7164_dl_wait_ack(dev
, drflag_ack
);
147 /* Wait for indication data was received */
148 saa7164_writel(drflag
, 0);
149 ret
= saa7164_dl_wait_clr(dev
, drflag_ack
);
155 dprintk(DBGLVL_FW
, "%s() memcpy(l) %d\n", __func__
, dstsize
);
156 /* Write last block to the device */
157 memcpy_toio(dst
, srcbuf
+offset
, srcsize
);
159 /* Flag the data as ready */
160 saa7164_writel(drflag
, 1);
161 ret
= saa7164_dl_wait_ack(dev
, drflag_ack
);
165 saa7164_writel(drflag
, 0);
167 while (saa7164_readl(bleflag
) != SAA_DEVICE_IMAGE_BOOTING
) {
168 if (saa7164_readl(bleflag
) & SAA_DEVICE_IMAGE_CORRUPT
) {
169 printk(KERN_ERR
"%s() image corrupt\n", __func__
);
174 if (saa7164_readl(bleflag
) & SAA_DEVICE_MEMORY_CORRUPT
) {
175 printk(KERN_ERR
"%s() device memory corrupt\n",
181 msleep(10); /* Checkpatch throws a < 20ms warning */
186 printk(KERN_INFO
"%s() Image downloaded, booting...\n", __func__
);
188 ret
= saa7164_dl_wait_clr(dev
, drflag_ack
);
192 printk(KERN_INFO
"%s() Image booted successfully.\n", __func__
);
200 /* TODO: Excessive debug */
201 /* Load the firmware. Optionally it can be in ROM or newer versions
202 * can be on disk, saving the expense of the ROM hardware. */
203 int saa7164_downloadfirmware(struct saa7164_dev
*dev
)
205 /* u32 second_timeout = 60 * SAA_DEVICE_TIMEOUT; */
206 u32 tmp
, filesize
, version
, err_flags
, first_timeout
, fwlength
;
207 u32 second_timeout
, updatebootloader
= 1, bootloadersize
= 0;
208 const struct firmware
*fw
= NULL
;
209 struct fw_header
*hdr
, *boothdr
= NULL
, *fwhdr
;
210 u32 bootloaderversion
= 0, fwloadersize
;
211 u8
*bootloaderoffset
= NULL
, *fwloaderoffset
;
215 dprintk(DBGLVL_FW
, "%s()\n", __func__
);
217 if (saa7164_boards
[dev
->board
].chiprev
== SAA7164_CHIP_REV2
) {
218 fwname
= SAA7164_REV2_FIRMWARE
;
219 fwlength
= SAA7164_REV2_FIRMWARE_SIZE
;
221 fwname
= SAA7164_REV3_FIRMWARE
;
222 fwlength
= SAA7164_REV3_FIRMWARE_SIZE
;
225 version
= saa7164_getcurrentfirmwareversion(dev
);
227 if (version
== 0x00) {
229 second_timeout
= 100;
231 err_flags
= saa7164_readl(SAA_BOOTLOADERERROR_FLAGS
);
232 dprintk(DBGLVL_FW
, "%s() err_flags = %x\n",
233 __func__
, err_flags
);
235 while (err_flags
!= SAA_DEVICE_IMAGE_BOOTING
) {
236 dprintk(DBGLVL_FW
, "%s() err_flags = %x\n",
237 __func__
, err_flags
);
238 msleep(10); /* Checkpatch throws a < 20ms warning */
240 if (err_flags
& SAA_DEVICE_IMAGE_CORRUPT
) {
241 printk(KERN_ERR
"%s() firmware corrupt\n",
245 if (err_flags
& SAA_DEVICE_MEMORY_CORRUPT
) {
246 printk(KERN_ERR
"%s() device memory corrupt\n",
250 if (err_flags
& SAA_DEVICE_NO_IMAGE
) {
251 printk(KERN_ERR
"%s() no first image\n",
255 if (err_flags
& SAA_DEVICE_IMAGE_SEARCHING
) {
257 if (first_timeout
== 0) {
259 "%s() no first image\n",
263 } else if (err_flags
& SAA_DEVICE_IMAGE_LOADING
) {
264 second_timeout
-= 10;
265 if (second_timeout
== 0) {
267 "%s() FW load time exceeded\n",
272 second_timeout
-= 10;
273 if (second_timeout
== 0) {
275 "%s() Unknown bootloader flags 0x%x\n",
276 __func__
, err_flags
);
281 err_flags
= saa7164_readl(SAA_BOOTLOADERERROR_FLAGS
);
282 } /* While != Booting */
284 if (err_flags
== SAA_DEVICE_IMAGE_BOOTING
) {
285 dprintk(DBGLVL_FW
, "%s() Loader 1 has loaded.\n",
287 first_timeout
= SAA_DEVICE_TIMEOUT
;
288 second_timeout
= 60 * SAA_DEVICE_TIMEOUT
;
289 second_timeout
= 100;
291 err_flags
= saa7164_readl(SAA_SECONDSTAGEERROR_FLAGS
);
292 dprintk(DBGLVL_FW
, "%s() err_flags2 = %x\n",
293 __func__
, err_flags
);
294 while (err_flags
!= SAA_DEVICE_IMAGE_BOOTING
) {
295 dprintk(DBGLVL_FW
, "%s() err_flags2 = %x\n",
296 __func__
, err_flags
);
297 msleep(10); /* Checkpatch throws a < 20ms warning */
299 if (err_flags
& SAA_DEVICE_IMAGE_CORRUPT
) {
301 "%s() firmware corrupt\n",
305 if (err_flags
& SAA_DEVICE_MEMORY_CORRUPT
) {
307 "%s() device memory corrupt\n",
311 if (err_flags
& SAA_DEVICE_NO_IMAGE
) {
312 printk(KERN_ERR
"%s() no first image\n",
316 if (err_flags
& SAA_DEVICE_IMAGE_SEARCHING
) {
318 if (first_timeout
== 0) {
320 "%s() no second image\n",
324 } else if (err_flags
&
325 SAA_DEVICE_IMAGE_LOADING
) {
326 second_timeout
-= 10;
327 if (second_timeout
== 0) {
329 "%s() FW load time exceeded\n",
334 second_timeout
-= 10;
335 if (second_timeout
== 0) {
337 "%s() Unknown bootloader flags 0x%x\n",
338 __func__
, err_flags
);
344 saa7164_readl(SAA_SECONDSTAGEERROR_FLAGS
);
345 } /* err_flags != SAA_DEVICE_IMAGE_BOOTING */
347 dprintk(DBGLVL_FW
, "%s() Loader flags 1:0x%x 2:0x%x.\n",
349 saa7164_readl(SAA_BOOTLOADERERROR_FLAGS
),
350 saa7164_readl(SAA_SECONDSTAGEERROR_FLAGS
));
352 } /* err_flags == SAA_DEVICE_IMAGE_BOOTING */
354 /* It's possible for both firmwares to have booted,
355 * but that doesn't mean they've finished booting yet.
357 if ((saa7164_readl(SAA_BOOTLOADERERROR_FLAGS
) ==
358 SAA_DEVICE_IMAGE_BOOTING
) &&
359 (saa7164_readl(SAA_SECONDSTAGEERROR_FLAGS
) ==
360 SAA_DEVICE_IMAGE_BOOTING
)) {
363 dprintk(DBGLVL_FW
, "%s() Loader 2 has loaded.\n",
366 first_timeout
= SAA_DEVICE_TIMEOUT
;
367 while (first_timeout
) {
368 msleep(10); /* Checkpatch throws a < 20ms warning */
371 saa7164_getcurrentfirmwareversion(dev
);
374 "%s() All f/w loaded successfully\n",
379 if (first_timeout
== 0) {
381 "%s() FW did not boot\n",
388 version
= saa7164_getcurrentfirmwareversion(dev
);
391 /* Has the firmware really booted? */
392 if ((saa7164_readl(SAA_BOOTLOADERERROR_FLAGS
) ==
393 SAA_DEVICE_IMAGE_BOOTING
) &&
394 (saa7164_readl(SAA_SECONDSTAGEERROR_FLAGS
) ==
395 SAA_DEVICE_IMAGE_BOOTING
) && (version
== 0)) {
398 "%s() The firmware hung, probably bad firmware\n",
401 /* Tell the second stage loader we have a deadlock */
402 saa7164_writel(SAA_DEVICE_DEADLOCK_DETECTED_OFFSET
,
403 SAA_DEVICE_DEADLOCK_DETECTED
);
405 saa7164_getfirmwarestatus(dev
);
410 dprintk(DBGLVL_FW
, "Device has Firmware Version %d.%d.%d.%d\n",
411 (version
& 0x0000fc00) >> 10,
412 (version
& 0x000003e0) >> 5,
413 (version
& 0x0000001f),
414 (version
& 0xffff0000) >> 16);
416 /* Load the firmwware from the disk if required */
419 printk(KERN_INFO
"%s() Waiting for firmware upload (%s)\n",
422 ret
= request_firmware(&fw
, fwname
, &dev
->pci
->dev
);
424 printk(KERN_ERR
"%s() Upload failed. "
425 "(file not found?)\n", __func__
);
429 printk(KERN_INFO
"%s() firmware read %Zu bytes.\n",
432 if (fw
->size
!= fwlength
) {
433 printk(KERN_ERR
"saa7164: firmware incorrect size %zu != %u\n",
439 printk(KERN_INFO
"%s() firmware loaded.\n", __func__
);
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
);
448 /* Retrieve bootloader if reqd */
449 if ((hdr
->firmwaresize
== 0) && (hdr
->bslsize
== 0))
450 /* Second bootloader in the firmware file */
451 filesize
= hdr
->reserved
* 16;
453 filesize
= (hdr
->firmwaresize
+ hdr
->bslsize
) *
454 16 + sizeof(struct fw_header
);
456 printk(KERN_INFO
"%s() SecBootLoader.FileSize = %d\n",
459 /* Get bootloader (if reqd) and firmware header */
460 if ((hdr
->firmwaresize
== 0) && (hdr
->bslsize
== 0)) {
461 /* Second boot loader is required */
463 /* Get the loader header */
464 boothdr
= (struct fw_header
*)(fw
->data
+
465 sizeof(struct fw_header
));
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",
478 if ((saa7164_readl(SAA_BOOTLOADERERROR_FLAGS
) ==
479 0x03) && (saa7164_readl(SAA_DATAREADY_FLAG_ACK
)
480 == 0x00) && (version
== 0x00)) {
482 dprintk(DBGLVL_FW
, "BootLoader version in "
484 (bootloaderversion
& 0x0000fc00) >> 10,
485 (bootloaderversion
& 0x000003e0) >> 5,
486 (bootloaderversion
& 0x0000001f),
487 (bootloaderversion
& 0xffff0000) >> 16
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
497 if (bootloaderversion
== boothdr
->version
)
498 updatebootloader
= 0;
501 /* Calculate offset to firmware header */
502 tmp
= (boothdr
->firmwaresize
+ boothdr
->bslsize
) * 16 +
503 (sizeof(struct fw_header
) +
504 sizeof(struct fw_header
));
506 fwhdr
= (struct fw_header
*)(fw
->data
+tmp
);
508 /* No second boot loader */
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
519 if (version
== fwhdr
->version
) {
520 /* No download, firmware already on board */
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
);
532 bootloaderoffset
= (u8
*)(fw
->data
+
533 sizeof(struct fw_header
));
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(
549 dev
->bmmio
+ SAA_DEVICE_DOWNLOAD_OFFSET
,
550 SAA_DEVICE_BUFFERBLOCKSIZE
);
553 "bootloader d/l has failed\n");
557 "bootloader download complete.\n");
561 printk(KERN_ERR
"starting firmware download(2)\n");
562 bootloadersize
= (boothdr
->firmwaresize
+
563 boothdr
->bslsize
) * 16 +
564 sizeof(struct fw_header
);
567 (u8
*)(fw
->data
+ sizeof(struct fw_header
));
569 fwloaderoffset
= bootloaderoffset
+ bootloadersize
;
571 /* TODO: fix this bounds overrun here with old f/ws */
572 fwloadersize
= (fwhdr
->firmwaresize
+ fwhdr
->bslsize
) *
573 16 + sizeof(struct fw_header
);
575 ret
= saa7164_downloadimage(
579 SAA_DEVICE_2ND_DOWNLOADFLAG_OFFSET
,
580 dev
->bmmio
+ SAA_DEVICE_2ND_DOWNLOAD_OFFSET
,
581 SAA_DEVICE_2ND_BUFFERBLOCKSIZE
);
583 printk(KERN_ERR
"firmware download failed\n");
586 printk(KERN_ERR
"firmware download complete.\n");
590 /* No bootloader update reqd, download firmware only */
591 printk(KERN_ERR
"starting firmware download(3)\n");
593 ret
= saa7164_downloadimage(
598 dev
->bmmio
+ SAA_DEVICE_DOWNLOAD_OFFSET
,
599 SAA_DEVICE_BUFFERBLOCKSIZE
);
601 printk(KERN_ERR
"firmware download failed\n");
604 printk(KERN_ERR
"firmware download complete.\n");
608 dev
->firmwareloaded
= 1;
612 release_firmware(fw
);