1 /* 2 * Driver for the NXP SAA7164 PCIe bridge 3 * 4 * Copyright (c) 2010-2015 Steven Toth <stoth@kernellabs.com> 5 * 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. 10 * 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 14 * 15 * GNU General Public License for more details. 16 */ 17 18 #include <linux/firmware.h> 19 #include <linux/slab.h> 20 21 #include "saa7164.h" 22 23 #define SAA7164_REV2_FIRMWARE "NXP7164-2010-03-10.1.fw" 24 #define SAA7164_REV2_FIRMWARE_SIZE 4019072 25 26 #define SAA7164_REV3_FIRMWARE "NXP7164-2010-03-10.1.fw" 27 #define SAA7164_REV3_FIRMWARE_SIZE 4019072 28 29 struct fw_header { 30 u32 firmwaresize; 31 u32 bslsize; 32 u32 reserved; 33 u32 version; 34 }; 35 36 static int saa7164_dl_wait_ack(struct saa7164_dev *dev, u32 reg) 37 { 38 u32 timeout = SAA_DEVICE_TIMEOUT; 39 while ((saa7164_readl(reg) & 0x01) == 0) { 40 timeout -= 10; 41 if (timeout == 0) { 42 printk(KERN_ERR "%s() timeout (no d/l ack)\n", 43 __func__); 44 return -EBUSY; 45 } 46 msleep(100); 47 } 48 49 return 0; 50 } 51 52 static int saa7164_dl_wait_clr(struct saa7164_dev *dev, u32 reg) 53 { 54 u32 timeout = SAA_DEVICE_TIMEOUT; 55 while (saa7164_readl(reg) & 0x01) { 56 timeout -= 10; 57 if (timeout == 0) { 58 printk(KERN_ERR "%s() timeout (no d/l clr)\n", 59 __func__); 60 return -EBUSY; 61 } 62 msleep(100); 63 } 64 65 return 0; 66 } 67 68 /* TODO: move dlflags into dev-> and change to write/readl/b */ 69 /* TODO: Excessive levels of debug */ 70 static int saa7164_downloadimage(struct saa7164_dev *dev, u8 *src, u32 srcsize, 71 u32 dlflags, u8 __iomem *dst, u32 dstsize) 72 { 73 u32 reg, timeout, offset; 74 u8 *srcbuf = NULL; 75 int ret; 76 77 u32 dlflag = dlflags; 78 u32 dlflag_ack = dlflag + 4; 79 u32 drflag = dlflag_ack + 4; 80 u32 drflag_ack = drflag + 4; 81 u32 bleflag = drflag_ack + 4; 82 83 dprintk(DBGLVL_FW, 84 "%s(image=%p, size=%d, flags=0x%x, dst=%p, dstsize=0x%x)\n", 85 __func__, src, srcsize, dlflags, dst, dstsize); 86 87 if ((src == NULL) || (dst == NULL)) { 88 ret = -EIO; 89 goto out; 90 } 91 92 srcbuf = kzalloc(4 * 1048576, GFP_KERNEL); 93 if (NULL == srcbuf) { 94 ret = -ENOMEM; 95 goto out; 96 } 97 98 if (srcsize > (4*1048576)) { 99 ret = -ENOMEM; 100 goto out; 101 } 102 103 memcpy(srcbuf, src, srcsize); 104 105 dprintk(DBGLVL_FW, "%s() dlflag = 0x%x\n", __func__, dlflag); 106 dprintk(DBGLVL_FW, "%s() dlflag_ack = 0x%x\n", __func__, dlflag_ack); 107 dprintk(DBGLVL_FW, "%s() drflag = 0x%x\n", __func__, drflag); 108 dprintk(DBGLVL_FW, "%s() drflag_ack = 0x%x\n", __func__, drflag_ack); 109 dprintk(DBGLVL_FW, "%s() bleflag = 0x%x\n", __func__, bleflag); 110 111 reg = saa7164_readl(dlflag); 112 dprintk(DBGLVL_FW, "%s() dlflag (0x%x)= 0x%x\n", __func__, dlflag, reg); 113 if (reg == 1) 114 dprintk(DBGLVL_FW, 115 "%s() Download flag already set, please reboot\n", 116 __func__); 117 118 /* Indicate download start */ 119 saa7164_writel(dlflag, 1); 120 ret = saa7164_dl_wait_ack(dev, dlflag_ack); 121 if (ret < 0) 122 goto out; 123 124 /* Ack download start, then wait for wait */ 125 saa7164_writel(dlflag, 0); 126 ret = saa7164_dl_wait_clr(dev, dlflag_ack); 127 if (ret < 0) 128 goto out; 129 130 /* Deal with the raw firmware, in the appropriate chunk size */ 131 for (offset = 0; srcsize > dstsize; 132 srcsize -= dstsize, offset += dstsize) { 133 134 dprintk(DBGLVL_FW, "%s() memcpy %d\n", __func__, dstsize); 135 memcpy_toio(dst, srcbuf + offset, dstsize); 136 137 /* Flag the data as ready */ 138 saa7164_writel(drflag, 1); 139 ret = saa7164_dl_wait_ack(dev, drflag_ack); 140 if (ret < 0) 141 goto out; 142 143 /* Wait for indication data was received */ 144 saa7164_writel(drflag, 0); 145 ret = saa7164_dl_wait_clr(dev, drflag_ack); 146 if (ret < 0) 147 goto out; 148 149 } 150 151 dprintk(DBGLVL_FW, "%s() memcpy(l) %d\n", __func__, dstsize); 152 /* Write last block to the device */ 153 memcpy_toio(dst, srcbuf+offset, srcsize); 154 155 /* Flag the data as ready */ 156 saa7164_writel(drflag, 1); 157 ret = saa7164_dl_wait_ack(dev, drflag_ack); 158 if (ret < 0) 159 goto out; 160 161 saa7164_writel(drflag, 0); 162 timeout = 0; 163 while (saa7164_readl(bleflag) != SAA_DEVICE_IMAGE_BOOTING) { 164 if (saa7164_readl(bleflag) & SAA_DEVICE_IMAGE_CORRUPT) { 165 printk(KERN_ERR "%s() image corrupt\n", __func__); 166 ret = -EBUSY; 167 goto out; 168 } 169 170 if (saa7164_readl(bleflag) & SAA_DEVICE_MEMORY_CORRUPT) { 171 printk(KERN_ERR "%s() device memory corrupt\n", 172 __func__); 173 ret = -EBUSY; 174 goto out; 175 } 176 177 msleep(10); /* Checkpatch throws a < 20ms warning */ 178 if (timeout++ > 60) 179 break; 180 } 181 182 printk(KERN_INFO "%s() Image downloaded, booting...\n", __func__); 183 184 ret = saa7164_dl_wait_clr(dev, drflag_ack); 185 if (ret < 0) 186 goto out; 187 188 printk(KERN_INFO "%s() Image booted successfully.\n", __func__); 189 ret = 0; 190 191 out: 192 kfree(srcbuf); 193 return ret; 194 } 195 196 /* TODO: Excessive debug */ 197 /* Load the firmware. Optionally it can be in ROM or newer versions 198 * can be on disk, saving the expense of the ROM hardware. */ 199 int saa7164_downloadfirmware(struct saa7164_dev *dev) 200 { 201 /* u32 second_timeout = 60 * SAA_DEVICE_TIMEOUT; */ 202 u32 tmp, filesize, version, err_flags, first_timeout, fwlength; 203 u32 second_timeout, updatebootloader = 1, bootloadersize = 0; 204 const struct firmware *fw = NULL; 205 struct fw_header *hdr, *boothdr = NULL, *fwhdr; 206 u32 bootloaderversion = 0, fwloadersize; 207 u8 *bootloaderoffset = NULL, *fwloaderoffset; 208 char *fwname; 209 int ret; 210 211 dprintk(DBGLVL_FW, "%s()\n", __func__); 212 213 if (saa7164_boards[dev->board].chiprev == SAA7164_CHIP_REV2) { 214 fwname = SAA7164_REV2_FIRMWARE; 215 fwlength = SAA7164_REV2_FIRMWARE_SIZE; 216 } else { 217 fwname = SAA7164_REV3_FIRMWARE; 218 fwlength = SAA7164_REV3_FIRMWARE_SIZE; 219 } 220 221 version = saa7164_getcurrentfirmwareversion(dev); 222 223 if (version == 0x00) { 224 225 second_timeout = 100; 226 first_timeout = 100; 227 err_flags = saa7164_readl(SAA_BOOTLOADERERROR_FLAGS); 228 dprintk(DBGLVL_FW, "%s() err_flags = %x\n", 229 __func__, err_flags); 230 231 while (err_flags != SAA_DEVICE_IMAGE_BOOTING) { 232 dprintk(DBGLVL_FW, "%s() err_flags = %x\n", 233 __func__, err_flags); 234 msleep(10); /* Checkpatch throws a < 20ms warning */ 235 236 if (err_flags & SAA_DEVICE_IMAGE_CORRUPT) { 237 printk(KERN_ERR "%s() firmware corrupt\n", 238 __func__); 239 break; 240 } 241 if (err_flags & SAA_DEVICE_MEMORY_CORRUPT) { 242 printk(KERN_ERR "%s() device memory corrupt\n", 243 __func__); 244 break; 245 } 246 if (err_flags & SAA_DEVICE_NO_IMAGE) { 247 printk(KERN_ERR "%s() no first image\n", 248 __func__); 249 break; 250 } 251 if (err_flags & SAA_DEVICE_IMAGE_SEARCHING) { 252 first_timeout -= 10; 253 if (first_timeout == 0) { 254 printk(KERN_ERR 255 "%s() no first image\n", 256 __func__); 257 break; 258 } 259 } else if (err_flags & SAA_DEVICE_IMAGE_LOADING) { 260 second_timeout -= 10; 261 if (second_timeout == 0) { 262 printk(KERN_ERR 263 "%s() FW load time exceeded\n", 264 __func__); 265 break; 266 } 267 } else { 268 second_timeout -= 10; 269 if (second_timeout == 0) { 270 printk(KERN_ERR 271 "%s() Unknown bootloader flags 0x%x\n", 272 __func__, err_flags); 273 break; 274 } 275 } 276 277 err_flags = saa7164_readl(SAA_BOOTLOADERERROR_FLAGS); 278 } /* While != Booting */ 279 280 if (err_flags == SAA_DEVICE_IMAGE_BOOTING) { 281 dprintk(DBGLVL_FW, "%s() Loader 1 has loaded.\n", 282 __func__); 283 first_timeout = SAA_DEVICE_TIMEOUT; 284 second_timeout = 60 * SAA_DEVICE_TIMEOUT; 285 second_timeout = 100; 286 287 err_flags = saa7164_readl(SAA_SECONDSTAGEERROR_FLAGS); 288 dprintk(DBGLVL_FW, "%s() err_flags2 = %x\n", 289 __func__, err_flags); 290 while (err_flags != SAA_DEVICE_IMAGE_BOOTING) { 291 dprintk(DBGLVL_FW, "%s() err_flags2 = %x\n", 292 __func__, err_flags); 293 msleep(10); /* Checkpatch throws a < 20ms warning */ 294 295 if (err_flags & SAA_DEVICE_IMAGE_CORRUPT) { 296 printk(KERN_ERR 297 "%s() firmware corrupt\n", 298 __func__); 299 break; 300 } 301 if (err_flags & SAA_DEVICE_MEMORY_CORRUPT) { 302 printk(KERN_ERR 303 "%s() device memory corrupt\n", 304 __func__); 305 break; 306 } 307 if (err_flags & SAA_DEVICE_NO_IMAGE) { 308 printk(KERN_ERR "%s() no second image\n", 309 __func__); 310 break; 311 } 312 if (err_flags & SAA_DEVICE_IMAGE_SEARCHING) { 313 first_timeout -= 10; 314 if (first_timeout == 0) { 315 printk(KERN_ERR 316 "%s() no second image\n", 317 __func__); 318 break; 319 } 320 } else if (err_flags & 321 SAA_DEVICE_IMAGE_LOADING) { 322 second_timeout -= 10; 323 if (second_timeout == 0) { 324 printk(KERN_ERR 325 "%s() FW load time exceeded\n", 326 __func__); 327 break; 328 } 329 } else { 330 second_timeout -= 10; 331 if (second_timeout == 0) { 332 printk(KERN_ERR 333 "%s() Unknown bootloader flags 0x%x\n", 334 __func__, err_flags); 335 break; 336 } 337 } 338 339 err_flags = 340 saa7164_readl(SAA_SECONDSTAGEERROR_FLAGS); 341 } /* err_flags != SAA_DEVICE_IMAGE_BOOTING */ 342 343 dprintk(DBGLVL_FW, "%s() Loader flags 1:0x%x 2:0x%x.\n", 344 __func__, 345 saa7164_readl(SAA_BOOTLOADERERROR_FLAGS), 346 saa7164_readl(SAA_SECONDSTAGEERROR_FLAGS)); 347 348 } /* err_flags == SAA_DEVICE_IMAGE_BOOTING */ 349 350 /* It's possible for both firmwares to have booted, 351 * but that doesn't mean they've finished booting yet. 352 */ 353 if ((saa7164_readl(SAA_BOOTLOADERERROR_FLAGS) == 354 SAA_DEVICE_IMAGE_BOOTING) && 355 (saa7164_readl(SAA_SECONDSTAGEERROR_FLAGS) == 356 SAA_DEVICE_IMAGE_BOOTING)) { 357 358 359 dprintk(DBGLVL_FW, "%s() Loader 2 has loaded.\n", 360 __func__); 361 362 first_timeout = SAA_DEVICE_TIMEOUT; 363 while (first_timeout) { 364 msleep(10); /* Checkpatch throws a < 20ms warning */ 365 366 version = 367 saa7164_getcurrentfirmwareversion(dev); 368 if (version) { 369 dprintk(DBGLVL_FW, 370 "%s() All f/w loaded successfully\n", 371 __func__); 372 break; 373 } else { 374 first_timeout -= 10; 375 if (first_timeout == 0) { 376 printk(KERN_ERR 377 "%s() FW did not boot\n", 378 __func__); 379 break; 380 } 381 } 382 } 383 } 384 version = saa7164_getcurrentfirmwareversion(dev); 385 } /* version == 0 */ 386 387 /* Has the firmware really booted? */ 388 if ((saa7164_readl(SAA_BOOTLOADERERROR_FLAGS) == 389 SAA_DEVICE_IMAGE_BOOTING) && 390 (saa7164_readl(SAA_SECONDSTAGEERROR_FLAGS) == 391 SAA_DEVICE_IMAGE_BOOTING) && (version == 0)) { 392 393 printk(KERN_ERR 394 "%s() The firmware hung, probably bad firmware\n", 395 __func__); 396 397 /* Tell the second stage loader we have a deadlock */ 398 saa7164_writel(SAA_DEVICE_DEADLOCK_DETECTED_OFFSET, 399 SAA_DEVICE_DEADLOCK_DETECTED); 400 401 saa7164_getfirmwarestatus(dev); 402 403 return -ENOMEM; 404 } 405 406 dprintk(DBGLVL_FW, "Device has Firmware Version %d.%d.%d.%d\n", 407 (version & 0x0000fc00) >> 10, 408 (version & 0x000003e0) >> 5, 409 (version & 0x0000001f), 410 (version & 0xffff0000) >> 16); 411 412 /* Load the firmwware from the disk if required */ 413 if (version == 0) { 414 415 printk(KERN_INFO "%s() Waiting for firmware upload (%s)\n", 416 __func__, fwname); 417 418 ret = request_firmware(&fw, fwname, &dev->pci->dev); 419 if (ret) { 420 printk(KERN_ERR "%s() Upload failed. (file not found?)\n", 421 __func__); 422 return -ENOMEM; 423 } 424 425 printk(KERN_INFO "%s() firmware read %zu bytes.\n", 426 __func__, fw->size); 427 428 if (fw->size != fwlength) { 429 printk(KERN_ERR "saa7164: firmware incorrect size %zu != %u\n", 430 fw->size, fwlength); 431 ret = -ENOMEM; 432 goto out; 433 } 434 435 printk(KERN_INFO "%s() firmware loaded.\n", __func__); 436 437 hdr = (struct fw_header *)fw->data; 438 printk(KERN_INFO "Firmware file header part 1:\n"); 439 printk(KERN_INFO " .FirmwareSize = 0x%x\n", hdr->firmwaresize); 440 printk(KERN_INFO " .BSLSize = 0x%x\n", hdr->bslsize); 441 printk(KERN_INFO " .Reserved = 0x%x\n", hdr->reserved); 442 printk(KERN_INFO " .Version = 0x%x\n", hdr->version); 443 444 /* Retrieve bootloader if reqd */ 445 if ((hdr->firmwaresize == 0) && (hdr->bslsize == 0)) 446 /* Second bootloader in the firmware file */ 447 filesize = hdr->reserved * 16; 448 else 449 filesize = (hdr->firmwaresize + hdr->bslsize) * 450 16 + sizeof(struct fw_header); 451 452 printk(KERN_INFO "%s() SecBootLoader.FileSize = %d\n", 453 __func__, filesize); 454 455 /* Get bootloader (if reqd) and firmware header */ 456 if ((hdr->firmwaresize == 0) && (hdr->bslsize == 0)) { 457 /* Second boot loader is required */ 458 459 /* Get the loader header */ 460 boothdr = (struct fw_header *)(fw->data + 461 sizeof(struct fw_header)); 462 463 bootloaderversion = 464 saa7164_readl(SAA_DEVICE_2ND_VERSION); 465 dprintk(DBGLVL_FW, "Onboard BootLoader:\n"); 466 dprintk(DBGLVL_FW, "->Flag 0x%x\n", 467 saa7164_readl(SAA_BOOTLOADERERROR_FLAGS)); 468 dprintk(DBGLVL_FW, "->Ack 0x%x\n", 469 saa7164_readl(SAA_DATAREADY_FLAG_ACK)); 470 dprintk(DBGLVL_FW, "->FW Version 0x%x\n", version); 471 dprintk(DBGLVL_FW, "->Loader Version 0x%x\n", 472 bootloaderversion); 473 474 if ((saa7164_readl(SAA_BOOTLOADERERROR_FLAGS) == 475 0x03) && (saa7164_readl(SAA_DATAREADY_FLAG_ACK) 476 == 0x00) && (version == 0x00)) { 477 478 dprintk(DBGLVL_FW, "BootLoader version in rom %d.%d.%d.%d\n", 479 (bootloaderversion & 0x0000fc00) >> 10, 480 (bootloaderversion & 0x000003e0) >> 5, 481 (bootloaderversion & 0x0000001f), 482 (bootloaderversion & 0xffff0000) >> 16 483 ); 484 dprintk(DBGLVL_FW, "BootLoader version in file %d.%d.%d.%d\n", 485 (boothdr->version & 0x0000fc00) >> 10, 486 (boothdr->version & 0x000003e0) >> 5, 487 (boothdr->version & 0x0000001f), 488 (boothdr->version & 0xffff0000) >> 16 489 ); 490 491 if (bootloaderversion == boothdr->version) 492 updatebootloader = 0; 493 } 494 495 /* Calculate offset to firmware header */ 496 tmp = (boothdr->firmwaresize + boothdr->bslsize) * 16 + 497 (sizeof(struct fw_header) + 498 sizeof(struct fw_header)); 499 500 fwhdr = (struct fw_header *)(fw->data+tmp); 501 } else { 502 /* No second boot loader */ 503 fwhdr = hdr; 504 } 505 506 dprintk(DBGLVL_FW, "Firmware version in file %d.%d.%d.%d\n", 507 (fwhdr->version & 0x0000fc00) >> 10, 508 (fwhdr->version & 0x000003e0) >> 5, 509 (fwhdr->version & 0x0000001f), 510 (fwhdr->version & 0xffff0000) >> 16 511 ); 512 513 if (version == fwhdr->version) { 514 /* No download, firmware already on board */ 515 ret = 0; 516 goto out; 517 } 518 519 if ((hdr->firmwaresize == 0) && (hdr->bslsize == 0)) { 520 if (updatebootloader) { 521 /* Get ready to upload the bootloader */ 522 bootloadersize = (boothdr->firmwaresize + 523 boothdr->bslsize) * 16 + 524 sizeof(struct fw_header); 525 526 bootloaderoffset = (u8 *)(fw->data + 527 sizeof(struct fw_header)); 528 529 dprintk(DBGLVL_FW, "bootloader d/l starts.\n"); 530 printk(KERN_INFO "%s() FirmwareSize = 0x%x\n", 531 __func__, boothdr->firmwaresize); 532 printk(KERN_INFO "%s() BSLSize = 0x%x\n", 533 __func__, boothdr->bslsize); 534 printk(KERN_INFO "%s() Reserved = 0x%x\n", 535 __func__, boothdr->reserved); 536 printk(KERN_INFO "%s() Version = 0x%x\n", 537 __func__, boothdr->version); 538 ret = saa7164_downloadimage( 539 dev, 540 bootloaderoffset, 541 bootloadersize, 542 SAA_DOWNLOAD_FLAGS, 543 dev->bmmio + SAA_DEVICE_DOWNLOAD_OFFSET, 544 SAA_DEVICE_BUFFERBLOCKSIZE); 545 if (ret < 0) { 546 printk(KERN_ERR 547 "bootloader d/l has failed\n"); 548 goto out; 549 } 550 dprintk(DBGLVL_FW, 551 "bootloader download complete.\n"); 552 553 } 554 555 printk(KERN_ERR "starting firmware download(2)\n"); 556 bootloadersize = (boothdr->firmwaresize + 557 boothdr->bslsize) * 16 + 558 sizeof(struct fw_header); 559 560 bootloaderoffset = 561 (u8 *)(fw->data + sizeof(struct fw_header)); 562 563 fwloaderoffset = bootloaderoffset + bootloadersize; 564 565 /* TODO: fix this bounds overrun here with old f/ws */ 566 fwloadersize = (fwhdr->firmwaresize + fwhdr->bslsize) * 567 16 + sizeof(struct fw_header); 568 569 ret = saa7164_downloadimage( 570 dev, 571 fwloaderoffset, 572 fwloadersize, 573 SAA_DEVICE_2ND_DOWNLOADFLAG_OFFSET, 574 dev->bmmio + SAA_DEVICE_2ND_DOWNLOAD_OFFSET, 575 SAA_DEVICE_2ND_BUFFERBLOCKSIZE); 576 if (ret < 0) { 577 printk(KERN_ERR "firmware download failed\n"); 578 goto out; 579 } 580 printk(KERN_ERR "firmware download complete.\n"); 581 582 } else { 583 584 /* No bootloader update reqd, download firmware only */ 585 printk(KERN_ERR "starting firmware download(3)\n"); 586 587 ret = saa7164_downloadimage( 588 dev, 589 (u8 *)fw->data, 590 fw->size, 591 SAA_DOWNLOAD_FLAGS, 592 dev->bmmio + SAA_DEVICE_DOWNLOAD_OFFSET, 593 SAA_DEVICE_BUFFERBLOCKSIZE); 594 if (ret < 0) { 595 printk(KERN_ERR "firmware download failed\n"); 596 goto out; 597 } 598 printk(KERN_ERR "firmware download complete.\n"); 599 } 600 } 601 602 dev->firmwareloaded = 1; 603 ret = 0; 604 605 out: 606 release_firmware(fw); 607 return ret; 608 } 609