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. (file not found?)\n",
429 printk(KERN_INFO
"%s() firmware read %Zu bytes.\n",
432 if (fw
->size
!= fwlength
) {
433 printk(KERN_ERR
"xc5000: firmware incorrect size\n");
438 printk(KERN_INFO
"%s() firmware loaded.\n", __func__
);
440 hdr
= (struct fw_header
*)fw
->data
;
441 printk(KERN_INFO
"Firmware file header part 1:\n");
442 printk(KERN_INFO
" .FirmwareSize = 0x%x\n", hdr
->firmwaresize
);
443 printk(KERN_INFO
" .BSLSize = 0x%x\n", hdr
->bslsize
);
444 printk(KERN_INFO
" .Reserved = 0x%x\n", hdr
->reserved
);
445 printk(KERN_INFO
" .Version = 0x%x\n", hdr
->version
);
447 /* Retrieve bootloader if reqd */
448 if ((hdr
->firmwaresize
== 0) && (hdr
->bslsize
== 0))
449 /* Second bootloader in the firmware file */
450 filesize
= hdr
->reserved
* 16;
452 filesize
= (hdr
->firmwaresize
+ hdr
->bslsize
) *
453 16 + sizeof(struct fw_header
);
455 printk(KERN_INFO
"%s() SecBootLoader.FileSize = %d\n",
458 /* Get bootloader (if reqd) and firmware header */
459 if ((hdr
->firmwaresize
== 0) && (hdr
->bslsize
== 0)) {
460 /* Second boot loader is required */
462 /* Get the loader header */
463 boothdr
= (struct fw_header
*)(fw
->data
+
464 sizeof(struct fw_header
));
467 saa7164_readl(SAA_DEVICE_2ND_VERSION
);
468 dprintk(DBGLVL_FW
, "Onboard BootLoader:\n");
469 dprintk(DBGLVL_FW
, "->Flag 0x%x\n",
470 saa7164_readl(SAA_BOOTLOADERERROR_FLAGS
));
471 dprintk(DBGLVL_FW
, "->Ack 0x%x\n",
472 saa7164_readl(SAA_DATAREADY_FLAG_ACK
));
473 dprintk(DBGLVL_FW
, "->FW Version 0x%x\n", version
);
474 dprintk(DBGLVL_FW
, "->Loader Version 0x%x\n",
477 if ((saa7164_readl(SAA_BOOTLOADERERROR_FLAGS
) ==
478 0x03) && (saa7164_readl(SAA_DATAREADY_FLAG_ACK
)
479 == 0x00) && (version
== 0x00)) {
481 dprintk(DBGLVL_FW
, "BootLoader version in rom %d.%d.%d.%d\n",
482 (bootloaderversion
& 0x0000fc00) >> 10,
483 (bootloaderversion
& 0x000003e0) >> 5,
484 (bootloaderversion
& 0x0000001f),
485 (bootloaderversion
& 0xffff0000) >> 16
487 dprintk(DBGLVL_FW
, "BootLoader version in file %d.%d.%d.%d\n",
488 (boothdr
->version
& 0x0000fc00) >> 10,
489 (boothdr
->version
& 0x000003e0) >> 5,
490 (boothdr
->version
& 0x0000001f),
491 (boothdr
->version
& 0xffff0000) >> 16
494 if (bootloaderversion
== boothdr
->version
)
495 updatebootloader
= 0;
498 /* Calculate offset to firmware header */
499 tmp
= (boothdr
->firmwaresize
+ boothdr
->bslsize
) * 16 +
500 (sizeof(struct fw_header
) +
501 sizeof(struct fw_header
));
503 fwhdr
= (struct fw_header
*)(fw
->data
+tmp
);
505 /* No second boot loader */
509 dprintk(DBGLVL_FW
, "Firmware version in file %d.%d.%d.%d\n",
510 (fwhdr
->version
& 0x0000fc00) >> 10,
511 (fwhdr
->version
& 0x000003e0) >> 5,
512 (fwhdr
->version
& 0x0000001f),
513 (fwhdr
->version
& 0xffff0000) >> 16
516 if (version
== fwhdr
->version
) {
517 /* No download, firmware already on board */
522 if ((hdr
->firmwaresize
== 0) && (hdr
->bslsize
== 0)) {
523 if (updatebootloader
) {
524 /* Get ready to upload the bootloader */
525 bootloadersize
= (boothdr
->firmwaresize
+
526 boothdr
->bslsize
) * 16 +
527 sizeof(struct fw_header
);
529 bootloaderoffset
= (u8
*)(fw
->data
+
530 sizeof(struct fw_header
));
532 dprintk(DBGLVL_FW
, "bootloader d/l starts.\n");
533 printk(KERN_INFO
"%s() FirmwareSize = 0x%x\n",
534 __func__
, boothdr
->firmwaresize
);
535 printk(KERN_INFO
"%s() BSLSize = 0x%x\n",
536 __func__
, boothdr
->bslsize
);
537 printk(KERN_INFO
"%s() Reserved = 0x%x\n",
538 __func__
, boothdr
->reserved
);
539 printk(KERN_INFO
"%s() Version = 0x%x\n",
540 __func__
, boothdr
->version
);
541 ret
= saa7164_downloadimage(
546 dev
->bmmio
+ SAA_DEVICE_DOWNLOAD_OFFSET
,
547 SAA_DEVICE_BUFFERBLOCKSIZE
);
550 "bootloader d/l has failed\n");
554 "bootloader download complete.\n");
558 printk(KERN_ERR
"starting firmware download(2)\n");
559 bootloadersize
= (boothdr
->firmwaresize
+
560 boothdr
->bslsize
) * 16 +
561 sizeof(struct fw_header
);
564 (u8
*)(fw
->data
+ sizeof(struct fw_header
));
566 fwloaderoffset
= bootloaderoffset
+ bootloadersize
;
568 /* TODO: fix this bounds overrun here with old f/ws */
569 fwloadersize
= (fwhdr
->firmwaresize
+ fwhdr
->bslsize
) *
570 16 + sizeof(struct fw_header
);
572 ret
= saa7164_downloadimage(
576 SAA_DEVICE_2ND_DOWNLOADFLAG_OFFSET
,
577 dev
->bmmio
+ SAA_DEVICE_2ND_DOWNLOAD_OFFSET
,
578 SAA_DEVICE_2ND_BUFFERBLOCKSIZE
);
580 printk(KERN_ERR
"firmware download failed\n");
583 printk(KERN_ERR
"firmware download complete.\n");
587 /* No bootloader update reqd, download firmware only */
588 printk(KERN_ERR
"starting firmware download(3)\n");
590 ret
= saa7164_downloadimage(
595 dev
->bmmio
+ SAA_DEVICE_DOWNLOAD_OFFSET
,
596 SAA_DEVICE_BUFFERBLOCKSIZE
);
598 printk(KERN_ERR
"firmware download failed\n");
601 printk(KERN_ERR
"firmware download complete.\n");
605 dev
->firmwareloaded
= 1;
609 release_firmware(fw
);