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
= 100;
276 err_flags
= saa7164_readl(SAA_SECONDSTAGEERROR_FLAGS
);
277 dprintk(DBGLVL_FW
, "%s() err_flags2 = %x\n",
278 __func__
, err_flags
);
279 while (err_flags
!= SAA_DEVICE_IMAGE_BOOTING
) {
280 dprintk(DBGLVL_FW
, "%s() err_flags2 = %x\n",
281 __func__
, err_flags
);
282 msleep(10); /* Checkpatch throws a < 20ms warning */
284 if (err_flags
& SAA_DEVICE_IMAGE_CORRUPT
) {
286 "%s() firmware corrupt\n",
290 if (err_flags
& SAA_DEVICE_MEMORY_CORRUPT
) {
292 "%s() device memory corrupt\n",
296 if (err_flags
& SAA_DEVICE_NO_IMAGE
) {
297 printk(KERN_ERR
"%s() no second image\n",
301 if (err_flags
& SAA_DEVICE_IMAGE_SEARCHING
) {
303 if (first_timeout
== 0) {
305 "%s() no second image\n",
309 } else if (err_flags
&
310 SAA_DEVICE_IMAGE_LOADING
) {
311 second_timeout
-= 10;
312 if (second_timeout
== 0) {
314 "%s() FW load time exceeded\n",
319 second_timeout
-= 10;
320 if (second_timeout
== 0) {
322 "%s() Unknown bootloader flags 0x%x\n",
323 __func__
, err_flags
);
329 saa7164_readl(SAA_SECONDSTAGEERROR_FLAGS
);
330 } /* err_flags != SAA_DEVICE_IMAGE_BOOTING */
332 dprintk(DBGLVL_FW
, "%s() Loader flags 1:0x%x 2:0x%x.\n",
334 saa7164_readl(SAA_BOOTLOADERERROR_FLAGS
),
335 saa7164_readl(SAA_SECONDSTAGEERROR_FLAGS
));
337 } /* err_flags == SAA_DEVICE_IMAGE_BOOTING */
339 /* It's possible for both firmwares to have booted,
340 * but that doesn't mean they've finished booting yet.
342 if ((saa7164_readl(SAA_BOOTLOADERERROR_FLAGS
) ==
343 SAA_DEVICE_IMAGE_BOOTING
) &&
344 (saa7164_readl(SAA_SECONDSTAGEERROR_FLAGS
) ==
345 SAA_DEVICE_IMAGE_BOOTING
)) {
348 dprintk(DBGLVL_FW
, "%s() Loader 2 has loaded.\n",
351 first_timeout
= SAA_DEVICE_TIMEOUT
;
352 while (first_timeout
) {
353 msleep(10); /* Checkpatch throws a < 20ms warning */
356 saa7164_getcurrentfirmwareversion(dev
);
359 "%s() All f/w loaded successfully\n",
364 if (first_timeout
== 0) {
366 "%s() FW did not boot\n",
373 version
= saa7164_getcurrentfirmwareversion(dev
);
376 /* Has the firmware really booted? */
377 if ((saa7164_readl(SAA_BOOTLOADERERROR_FLAGS
) ==
378 SAA_DEVICE_IMAGE_BOOTING
) &&
379 (saa7164_readl(SAA_SECONDSTAGEERROR_FLAGS
) ==
380 SAA_DEVICE_IMAGE_BOOTING
) && (version
== 0)) {
383 "%s() The firmware hung, probably bad firmware\n",
386 /* Tell the second stage loader we have a deadlock */
387 saa7164_writel(SAA_DEVICE_DEADLOCK_DETECTED_OFFSET
,
388 SAA_DEVICE_DEADLOCK_DETECTED
);
390 saa7164_getfirmwarestatus(dev
);
395 dprintk(DBGLVL_FW
, "Device has Firmware Version %d.%d.%d.%d\n",
396 (version
& 0x0000fc00) >> 10,
397 (version
& 0x000003e0) >> 5,
398 (version
& 0x0000001f),
399 (version
& 0xffff0000) >> 16);
401 /* Load the firmware from the disk if required */
404 printk(KERN_INFO
"%s() Waiting for firmware upload (%s)\n",
407 ret
= request_firmware(&fw
, fwname
, &dev
->pci
->dev
);
409 printk(KERN_ERR
"%s() Upload failed. (file not found?)\n",
414 printk(KERN_INFO
"%s() firmware read %zu bytes.\n",
417 if (fw
->size
!= fwlength
) {
418 printk(KERN_ERR
"saa7164: firmware incorrect size %zu != %u\n",
424 printk(KERN_INFO
"%s() firmware loaded.\n", __func__
);
426 hdr
= (struct fw_header
*)fw
->data
;
427 printk(KERN_INFO
"Firmware file header part 1:\n");
428 printk(KERN_INFO
" .FirmwareSize = 0x%x\n", hdr
->firmwaresize
);
429 printk(KERN_INFO
" .BSLSize = 0x%x\n", hdr
->bslsize
);
430 printk(KERN_INFO
" .Reserved = 0x%x\n", hdr
->reserved
);
431 printk(KERN_INFO
" .Version = 0x%x\n", hdr
->version
);
433 /* Retrieve bootloader if reqd */
434 if ((hdr
->firmwaresize
== 0) && (hdr
->bslsize
== 0))
435 /* Second bootloader in the firmware file */
436 filesize
= hdr
->reserved
* 16;
438 filesize
= (hdr
->firmwaresize
+ hdr
->bslsize
) *
439 16 + sizeof(struct fw_header
);
441 printk(KERN_INFO
"%s() SecBootLoader.FileSize = %d\n",
444 /* Get bootloader (if reqd) and firmware header */
445 if ((hdr
->firmwaresize
== 0) && (hdr
->bslsize
== 0)) {
446 /* Second boot loader is required */
448 /* Get the loader header */
449 boothdr
= (struct fw_header
*)(fw
->data
+
450 sizeof(struct fw_header
));
453 saa7164_readl(SAA_DEVICE_2ND_VERSION
);
454 dprintk(DBGLVL_FW
, "Onboard BootLoader:\n");
455 dprintk(DBGLVL_FW
, "->Flag 0x%x\n",
456 saa7164_readl(SAA_BOOTLOADERERROR_FLAGS
));
457 dprintk(DBGLVL_FW
, "->Ack 0x%x\n",
458 saa7164_readl(SAA_DATAREADY_FLAG_ACK
));
459 dprintk(DBGLVL_FW
, "->FW Version 0x%x\n", version
);
460 dprintk(DBGLVL_FW
, "->Loader Version 0x%x\n",
463 if ((saa7164_readl(SAA_BOOTLOADERERROR_FLAGS
) ==
464 0x03) && (saa7164_readl(SAA_DATAREADY_FLAG_ACK
)
465 == 0x00) && (version
== 0x00)) {
467 dprintk(DBGLVL_FW
, "BootLoader version in rom %d.%d.%d.%d\n",
468 (bootloaderversion
& 0x0000fc00) >> 10,
469 (bootloaderversion
& 0x000003e0) >> 5,
470 (bootloaderversion
& 0x0000001f),
471 (bootloaderversion
& 0xffff0000) >> 16
473 dprintk(DBGLVL_FW
, "BootLoader version in file %d.%d.%d.%d\n",
474 (boothdr
->version
& 0x0000fc00) >> 10,
475 (boothdr
->version
& 0x000003e0) >> 5,
476 (boothdr
->version
& 0x0000001f),
477 (boothdr
->version
& 0xffff0000) >> 16
480 if (bootloaderversion
== boothdr
->version
)
481 updatebootloader
= 0;
484 /* Calculate offset to firmware header */
485 tmp
= (boothdr
->firmwaresize
+ boothdr
->bslsize
) * 16 +
486 (sizeof(struct fw_header
) +
487 sizeof(struct fw_header
));
489 fwhdr
= (struct fw_header
*)(fw
->data
+tmp
);
491 /* No second boot loader */
495 dprintk(DBGLVL_FW
, "Firmware version in file %d.%d.%d.%d\n",
496 (fwhdr
->version
& 0x0000fc00) >> 10,
497 (fwhdr
->version
& 0x000003e0) >> 5,
498 (fwhdr
->version
& 0x0000001f),
499 (fwhdr
->version
& 0xffff0000) >> 16
502 if (version
== fwhdr
->version
) {
503 /* No download, firmware already on board */
508 if ((hdr
->firmwaresize
== 0) && (hdr
->bslsize
== 0)) {
509 if (updatebootloader
) {
510 /* Get ready to upload the bootloader */
511 bootloadersize
= (boothdr
->firmwaresize
+
512 boothdr
->bslsize
) * 16 +
513 sizeof(struct fw_header
);
515 bootloaderoffset
= (u8
*)(fw
->data
+
516 sizeof(struct fw_header
));
518 dprintk(DBGLVL_FW
, "bootloader d/l starts.\n");
519 printk(KERN_INFO
"%s() FirmwareSize = 0x%x\n",
520 __func__
, boothdr
->firmwaresize
);
521 printk(KERN_INFO
"%s() BSLSize = 0x%x\n",
522 __func__
, boothdr
->bslsize
);
523 printk(KERN_INFO
"%s() Reserved = 0x%x\n",
524 __func__
, boothdr
->reserved
);
525 printk(KERN_INFO
"%s() Version = 0x%x\n",
526 __func__
, boothdr
->version
);
527 ret
= saa7164_downloadimage(
532 dev
->bmmio
+ SAA_DEVICE_DOWNLOAD_OFFSET
,
533 SAA_DEVICE_BUFFERBLOCKSIZE
);
536 "bootloader d/l has failed\n");
540 "bootloader download complete.\n");
544 printk(KERN_ERR
"starting firmware download(2)\n");
545 bootloadersize
= (boothdr
->firmwaresize
+
546 boothdr
->bslsize
) * 16 +
547 sizeof(struct fw_header
);
550 (u8
*)(fw
->data
+ sizeof(struct fw_header
));
552 fwloaderoffset
= bootloaderoffset
+ bootloadersize
;
554 /* TODO: fix this bounds overrun here with old f/ws */
555 fwloadersize
= (fwhdr
->firmwaresize
+ fwhdr
->bslsize
) *
556 16 + sizeof(struct fw_header
);
558 ret
= saa7164_downloadimage(
562 SAA_DEVICE_2ND_DOWNLOADFLAG_OFFSET
,
563 dev
->bmmio
+ SAA_DEVICE_2ND_DOWNLOAD_OFFSET
,
564 SAA_DEVICE_2ND_BUFFERBLOCKSIZE
);
566 printk(KERN_ERR
"firmware download failed\n");
569 printk(KERN_ERR
"firmware download complete.\n");
573 /* No bootloader update reqd, download firmware only */
574 printk(KERN_ERR
"starting firmware download(3)\n");
576 ret
= saa7164_downloadimage(
581 dev
->bmmio
+ SAA_DEVICE_DOWNLOAD_OFFSET
,
582 SAA_DEVICE_BUFFERBLOCKSIZE
);
584 printk(KERN_ERR
"firmware download failed\n");
587 printk(KERN_ERR
"firmware download complete.\n");
591 dev
->firmwareloaded
= 1;
595 release_firmware(fw
);