1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for the NXP SAA7164 PCIe bridge
5 * Copyright (c) 2010-2015 Steven Toth <stoth@kernellabs.com>
8 #include <linux/firmware.h>
9 #include <linux/slab.h>
13 #define SAA7164_REV2_FIRMWARE "NXP7164-2010-03-10.1.fw"
14 #define SAA7164_REV2_FIRMWARE_SIZE 4019072
16 #define SAA7164_REV3_FIRMWARE "NXP7164-2010-03-10.1.fw"
17 #define SAA7164_REV3_FIRMWARE_SIZE 4019072
26 static int saa7164_dl_wait_ack(struct saa7164_dev
*dev
, u32 reg
)
28 u32 timeout
= SAA_DEVICE_TIMEOUT
;
29 while ((saa7164_readl(reg
) & 0x01) == 0) {
32 printk(KERN_ERR
"%s() timeout (no d/l ack)\n",
42 static int saa7164_dl_wait_clr(struct saa7164_dev
*dev
, u32 reg
)
44 u32 timeout
= SAA_DEVICE_TIMEOUT
;
45 while (saa7164_readl(reg
) & 0x01) {
48 printk(KERN_ERR
"%s() timeout (no d/l clr)\n",
58 /* TODO: move dlflags into dev-> and change to write/readl/b */
59 /* TODO: Excessive levels of debug */
60 static int saa7164_downloadimage(struct saa7164_dev
*dev
, u8
*src
, u32 srcsize
,
61 u32 dlflags
, u8 __iomem
*dst
, u32 dstsize
)
63 u32 reg
, timeout
, offset
;
68 u32 dlflag_ack
= dlflag
+ 4;
69 u32 drflag
= dlflag_ack
+ 4;
70 u32 drflag_ack
= drflag
+ 4;
71 u32 bleflag
= drflag_ack
+ 4;
74 "%s(image=%p, size=%d, flags=0x%x, dst=%p, dstsize=0x%x)\n",
75 __func__
, src
, srcsize
, dlflags
, dst
, dstsize
);
77 if ((src
== NULL
) || (dst
== NULL
)) {
82 srcbuf
= kzalloc(4 * 1048576, GFP_KERNEL
);
88 if (srcsize
> (4*1048576)) {
93 memcpy(srcbuf
, src
, srcsize
);
95 dprintk(DBGLVL_FW
, "%s() dlflag = 0x%x\n", __func__
, dlflag
);
96 dprintk(DBGLVL_FW
, "%s() dlflag_ack = 0x%x\n", __func__
, dlflag_ack
);
97 dprintk(DBGLVL_FW
, "%s() drflag = 0x%x\n", __func__
, drflag
);
98 dprintk(DBGLVL_FW
, "%s() drflag_ack = 0x%x\n", __func__
, drflag_ack
);
99 dprintk(DBGLVL_FW
, "%s() bleflag = 0x%x\n", __func__
, bleflag
);
101 reg
= saa7164_readl(dlflag
);
102 dprintk(DBGLVL_FW
, "%s() dlflag (0x%x)= 0x%x\n", __func__
, dlflag
, reg
);
105 "%s() Download flag already set, please reboot\n",
108 /* Indicate download start */
109 saa7164_writel(dlflag
, 1);
110 ret
= saa7164_dl_wait_ack(dev
, dlflag_ack
);
114 /* Ack download start, then wait for wait */
115 saa7164_writel(dlflag
, 0);
116 ret
= saa7164_dl_wait_clr(dev
, dlflag_ack
);
120 /* Deal with the raw firmware, in the appropriate chunk size */
121 for (offset
= 0; srcsize
> dstsize
;
122 srcsize
-= dstsize
, offset
+= dstsize
) {
124 dprintk(DBGLVL_FW
, "%s() memcpy %d\n", __func__
, dstsize
);
125 memcpy_toio(dst
, srcbuf
+ offset
, dstsize
);
127 /* Flag the data as ready */
128 saa7164_writel(drflag
, 1);
129 ret
= saa7164_dl_wait_ack(dev
, drflag_ack
);
133 /* Wait for indication data was received */
134 saa7164_writel(drflag
, 0);
135 ret
= saa7164_dl_wait_clr(dev
, drflag_ack
);
141 dprintk(DBGLVL_FW
, "%s() memcpy(l) %d\n", __func__
, dstsize
);
142 /* Write last block to the device */
143 memcpy_toio(dst
, srcbuf
+offset
, srcsize
);
145 /* Flag the data as ready */
146 saa7164_writel(drflag
, 1);
147 ret
= saa7164_dl_wait_ack(dev
, drflag_ack
);
151 saa7164_writel(drflag
, 0);
153 while (saa7164_readl(bleflag
) != SAA_DEVICE_IMAGE_BOOTING
) {
154 if (saa7164_readl(bleflag
) & SAA_DEVICE_IMAGE_CORRUPT
) {
155 printk(KERN_ERR
"%s() image corrupt\n", __func__
);
160 if (saa7164_readl(bleflag
) & SAA_DEVICE_MEMORY_CORRUPT
) {
161 printk(KERN_ERR
"%s() device memory corrupt\n",
167 msleep(10); /* Checkpatch throws a < 20ms warning */
172 printk(KERN_INFO
"%s() Image downloaded, booting...\n", __func__
);
174 ret
= saa7164_dl_wait_clr(dev
, drflag_ack
);
178 printk(KERN_INFO
"%s() Image booted successfully.\n", __func__
);
186 /* TODO: Excessive debug */
187 /* Load the firmware. Optionally it can be in ROM or newer versions
188 * can be on disk, saving the expense of the ROM hardware. */
189 int saa7164_downloadfirmware(struct saa7164_dev
*dev
)
191 /* u32 second_timeout = 60 * SAA_DEVICE_TIMEOUT; */
192 u32 tmp
, filesize
, version
, err_flags
, first_timeout
, fwlength
;
193 u32 second_timeout
, updatebootloader
= 1, bootloadersize
= 0;
194 const struct firmware
*fw
= NULL
;
195 struct fw_header
*hdr
, *boothdr
= NULL
, *fwhdr
;
196 u32 bootloaderversion
= 0, fwloadersize
;
197 u8
*bootloaderoffset
= NULL
, *fwloaderoffset
;
201 dprintk(DBGLVL_FW
, "%s()\n", __func__
);
203 if (saa7164_boards
[dev
->board
].chiprev
== SAA7164_CHIP_REV2
) {
204 fwname
= SAA7164_REV2_FIRMWARE
;
205 fwlength
= SAA7164_REV2_FIRMWARE_SIZE
;
207 fwname
= SAA7164_REV3_FIRMWARE
;
208 fwlength
= SAA7164_REV3_FIRMWARE_SIZE
;
211 version
= saa7164_getcurrentfirmwareversion(dev
);
213 if (version
== 0x00) {
215 second_timeout
= 100;
217 err_flags
= saa7164_readl(SAA_BOOTLOADERERROR_FLAGS
);
218 dprintk(DBGLVL_FW
, "%s() err_flags = %x\n",
219 __func__
, err_flags
);
221 while (err_flags
!= SAA_DEVICE_IMAGE_BOOTING
) {
222 dprintk(DBGLVL_FW
, "%s() err_flags = %x\n",
223 __func__
, err_flags
);
224 msleep(10); /* Checkpatch throws a < 20ms warning */
226 if (err_flags
& SAA_DEVICE_IMAGE_CORRUPT
) {
227 printk(KERN_ERR
"%s() firmware corrupt\n",
231 if (err_flags
& SAA_DEVICE_MEMORY_CORRUPT
) {
232 printk(KERN_ERR
"%s() device memory corrupt\n",
236 if (err_flags
& SAA_DEVICE_NO_IMAGE
) {
237 printk(KERN_ERR
"%s() no first image\n",
241 if (err_flags
& SAA_DEVICE_IMAGE_SEARCHING
) {
243 if (first_timeout
== 0) {
245 "%s() no first image\n",
249 } else if (err_flags
& SAA_DEVICE_IMAGE_LOADING
) {
250 second_timeout
-= 10;
251 if (second_timeout
== 0) {
253 "%s() FW load time exceeded\n",
258 second_timeout
-= 10;
259 if (second_timeout
== 0) {
261 "%s() Unknown bootloader flags 0x%x\n",
262 __func__
, err_flags
);
267 err_flags
= saa7164_readl(SAA_BOOTLOADERERROR_FLAGS
);
268 } /* While != Booting */
270 if (err_flags
== SAA_DEVICE_IMAGE_BOOTING
) {
271 dprintk(DBGLVL_FW
, "%s() Loader 1 has loaded.\n",
273 first_timeout
= SAA_DEVICE_TIMEOUT
;
274 second_timeout
= 60 * SAA_DEVICE_TIMEOUT
;
275 second_timeout
= 100;
277 err_flags
= saa7164_readl(SAA_SECONDSTAGEERROR_FLAGS
);
278 dprintk(DBGLVL_FW
, "%s() err_flags2 = %x\n",
279 __func__
, err_flags
);
280 while (err_flags
!= SAA_DEVICE_IMAGE_BOOTING
) {
281 dprintk(DBGLVL_FW
, "%s() err_flags2 = %x\n",
282 __func__
, err_flags
);
283 msleep(10); /* Checkpatch throws a < 20ms warning */
285 if (err_flags
& SAA_DEVICE_IMAGE_CORRUPT
) {
287 "%s() firmware corrupt\n",
291 if (err_flags
& SAA_DEVICE_MEMORY_CORRUPT
) {
293 "%s() device memory corrupt\n",
297 if (err_flags
& SAA_DEVICE_NO_IMAGE
) {
298 printk(KERN_ERR
"%s() no second image\n",
302 if (err_flags
& SAA_DEVICE_IMAGE_SEARCHING
) {
304 if (first_timeout
== 0) {
306 "%s() no second image\n",
310 } else if (err_flags
&
311 SAA_DEVICE_IMAGE_LOADING
) {
312 second_timeout
-= 10;
313 if (second_timeout
== 0) {
315 "%s() FW load time exceeded\n",
320 second_timeout
-= 10;
321 if (second_timeout
== 0) {
323 "%s() Unknown bootloader flags 0x%x\n",
324 __func__
, err_flags
);
330 saa7164_readl(SAA_SECONDSTAGEERROR_FLAGS
);
331 } /* err_flags != SAA_DEVICE_IMAGE_BOOTING */
333 dprintk(DBGLVL_FW
, "%s() Loader flags 1:0x%x 2:0x%x.\n",
335 saa7164_readl(SAA_BOOTLOADERERROR_FLAGS
),
336 saa7164_readl(SAA_SECONDSTAGEERROR_FLAGS
));
338 } /* err_flags == SAA_DEVICE_IMAGE_BOOTING */
340 /* It's possible for both firmwares to have booted,
341 * but that doesn't mean they've finished booting yet.
343 if ((saa7164_readl(SAA_BOOTLOADERERROR_FLAGS
) ==
344 SAA_DEVICE_IMAGE_BOOTING
) &&
345 (saa7164_readl(SAA_SECONDSTAGEERROR_FLAGS
) ==
346 SAA_DEVICE_IMAGE_BOOTING
)) {
349 dprintk(DBGLVL_FW
, "%s() Loader 2 has loaded.\n",
352 first_timeout
= SAA_DEVICE_TIMEOUT
;
353 while (first_timeout
) {
354 msleep(10); /* Checkpatch throws a < 20ms warning */
357 saa7164_getcurrentfirmwareversion(dev
);
360 "%s() All f/w loaded successfully\n",
365 if (first_timeout
== 0) {
367 "%s() FW did not boot\n",
374 version
= saa7164_getcurrentfirmwareversion(dev
);
377 /* Has the firmware really booted? */
378 if ((saa7164_readl(SAA_BOOTLOADERERROR_FLAGS
) ==
379 SAA_DEVICE_IMAGE_BOOTING
) &&
380 (saa7164_readl(SAA_SECONDSTAGEERROR_FLAGS
) ==
381 SAA_DEVICE_IMAGE_BOOTING
) && (version
== 0)) {
384 "%s() The firmware hung, probably bad firmware\n",
387 /* Tell the second stage loader we have a deadlock */
388 saa7164_writel(SAA_DEVICE_DEADLOCK_DETECTED_OFFSET
,
389 SAA_DEVICE_DEADLOCK_DETECTED
);
391 saa7164_getfirmwarestatus(dev
);
396 dprintk(DBGLVL_FW
, "Device has Firmware Version %d.%d.%d.%d\n",
397 (version
& 0x0000fc00) >> 10,
398 (version
& 0x000003e0) >> 5,
399 (version
& 0x0000001f),
400 (version
& 0xffff0000) >> 16);
402 /* Load the firmware from the disk if required */
405 printk(KERN_INFO
"%s() Waiting for firmware upload (%s)\n",
408 ret
= request_firmware(&fw
, fwname
, &dev
->pci
->dev
);
410 printk(KERN_ERR
"%s() Upload failed. (file not found?)\n",
415 printk(KERN_INFO
"%s() firmware read %zu bytes.\n",
418 if (fw
->size
!= fwlength
) {
419 printk(KERN_ERR
"saa7164: firmware incorrect size %zu != %u\n",
425 printk(KERN_INFO
"%s() firmware loaded.\n", __func__
);
427 hdr
= (struct fw_header
*)fw
->data
;
428 printk(KERN_INFO
"Firmware file header part 1:\n");
429 printk(KERN_INFO
" .FirmwareSize = 0x%x\n", hdr
->firmwaresize
);
430 printk(KERN_INFO
" .BSLSize = 0x%x\n", hdr
->bslsize
);
431 printk(KERN_INFO
" .Reserved = 0x%x\n", hdr
->reserved
);
432 printk(KERN_INFO
" .Version = 0x%x\n", hdr
->version
);
434 /* Retrieve bootloader if reqd */
435 if ((hdr
->firmwaresize
== 0) && (hdr
->bslsize
== 0))
436 /* Second bootloader in the firmware file */
437 filesize
= hdr
->reserved
* 16;
439 filesize
= (hdr
->firmwaresize
+ hdr
->bslsize
) *
440 16 + sizeof(struct fw_header
);
442 printk(KERN_INFO
"%s() SecBootLoader.FileSize = %d\n",
445 /* Get bootloader (if reqd) and firmware header */
446 if ((hdr
->firmwaresize
== 0) && (hdr
->bslsize
== 0)) {
447 /* Second boot loader is required */
449 /* Get the loader header */
450 boothdr
= (struct fw_header
*)(fw
->data
+
451 sizeof(struct fw_header
));
454 saa7164_readl(SAA_DEVICE_2ND_VERSION
);
455 dprintk(DBGLVL_FW
, "Onboard BootLoader:\n");
456 dprintk(DBGLVL_FW
, "->Flag 0x%x\n",
457 saa7164_readl(SAA_BOOTLOADERERROR_FLAGS
));
458 dprintk(DBGLVL_FW
, "->Ack 0x%x\n",
459 saa7164_readl(SAA_DATAREADY_FLAG_ACK
));
460 dprintk(DBGLVL_FW
, "->FW Version 0x%x\n", version
);
461 dprintk(DBGLVL_FW
, "->Loader Version 0x%x\n",
464 if ((saa7164_readl(SAA_BOOTLOADERERROR_FLAGS
) ==
465 0x03) && (saa7164_readl(SAA_DATAREADY_FLAG_ACK
)
466 == 0x00) && (version
== 0x00)) {
468 dprintk(DBGLVL_FW
, "BootLoader version in rom %d.%d.%d.%d\n",
469 (bootloaderversion
& 0x0000fc00) >> 10,
470 (bootloaderversion
& 0x000003e0) >> 5,
471 (bootloaderversion
& 0x0000001f),
472 (bootloaderversion
& 0xffff0000) >> 16
474 dprintk(DBGLVL_FW
, "BootLoader version in file %d.%d.%d.%d\n",
475 (boothdr
->version
& 0x0000fc00) >> 10,
476 (boothdr
->version
& 0x000003e0) >> 5,
477 (boothdr
->version
& 0x0000001f),
478 (boothdr
->version
& 0xffff0000) >> 16
481 if (bootloaderversion
== boothdr
->version
)
482 updatebootloader
= 0;
485 /* Calculate offset to firmware header */
486 tmp
= (boothdr
->firmwaresize
+ boothdr
->bslsize
) * 16 +
487 (sizeof(struct fw_header
) +
488 sizeof(struct fw_header
));
490 fwhdr
= (struct fw_header
*)(fw
->data
+tmp
);
492 /* No second boot loader */
496 dprintk(DBGLVL_FW
, "Firmware version in file %d.%d.%d.%d\n",
497 (fwhdr
->version
& 0x0000fc00) >> 10,
498 (fwhdr
->version
& 0x000003e0) >> 5,
499 (fwhdr
->version
& 0x0000001f),
500 (fwhdr
->version
& 0xffff0000) >> 16
503 if (version
== fwhdr
->version
) {
504 /* No download, firmware already on board */
509 if ((hdr
->firmwaresize
== 0) && (hdr
->bslsize
== 0)) {
510 if (updatebootloader
) {
511 /* Get ready to upload the bootloader */
512 bootloadersize
= (boothdr
->firmwaresize
+
513 boothdr
->bslsize
) * 16 +
514 sizeof(struct fw_header
);
516 bootloaderoffset
= (u8
*)(fw
->data
+
517 sizeof(struct fw_header
));
519 dprintk(DBGLVL_FW
, "bootloader d/l starts.\n");
520 printk(KERN_INFO
"%s() FirmwareSize = 0x%x\n",
521 __func__
, boothdr
->firmwaresize
);
522 printk(KERN_INFO
"%s() BSLSize = 0x%x\n",
523 __func__
, boothdr
->bslsize
);
524 printk(KERN_INFO
"%s() Reserved = 0x%x\n",
525 __func__
, boothdr
->reserved
);
526 printk(KERN_INFO
"%s() Version = 0x%x\n",
527 __func__
, boothdr
->version
);
528 ret
= saa7164_downloadimage(
533 dev
->bmmio
+ SAA_DEVICE_DOWNLOAD_OFFSET
,
534 SAA_DEVICE_BUFFERBLOCKSIZE
);
537 "bootloader d/l has failed\n");
541 "bootloader download complete.\n");
545 printk(KERN_ERR
"starting firmware download(2)\n");
546 bootloadersize
= (boothdr
->firmwaresize
+
547 boothdr
->bslsize
) * 16 +
548 sizeof(struct fw_header
);
551 (u8
*)(fw
->data
+ sizeof(struct fw_header
));
553 fwloaderoffset
= bootloaderoffset
+ bootloadersize
;
555 /* TODO: fix this bounds overrun here with old f/ws */
556 fwloadersize
= (fwhdr
->firmwaresize
+ fwhdr
->bslsize
) *
557 16 + sizeof(struct fw_header
);
559 ret
= saa7164_downloadimage(
563 SAA_DEVICE_2ND_DOWNLOADFLAG_OFFSET
,
564 dev
->bmmio
+ SAA_DEVICE_2ND_DOWNLOAD_OFFSET
,
565 SAA_DEVICE_2ND_BUFFERBLOCKSIZE
);
567 printk(KERN_ERR
"firmware download failed\n");
570 printk(KERN_ERR
"firmware download complete.\n");
574 /* No bootloader update reqd, download firmware only */
575 printk(KERN_ERR
"starting firmware download(3)\n");
577 ret
= saa7164_downloadimage(
582 dev
->bmmio
+ SAA_DEVICE_DOWNLOAD_OFFSET
,
583 SAA_DEVICE_BUFFERBLOCKSIZE
);
585 printk(KERN_ERR
"firmware download failed\n");
588 printk(KERN_ERR
"firmware download complete.\n");
592 dev
->firmwareloaded
= 1;
596 release_firmware(fw
);