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 "xc5000: firmware incorrect size\n"); 430 ret = -ENOMEM; 431 goto out; 432 } 433 434 printk(KERN_INFO "%s() firmware loaded.\n", __func__); 435 436 hdr = (struct fw_header *)fw->data; 437 printk(KERN_INFO "Firmware file header part 1:\n"); 438 printk(KERN_INFO " .FirmwareSize = 0x%x\n", hdr->firmwaresize); 439 printk(KERN_INFO " .BSLSize = 0x%x\n", hdr->bslsize); 440 printk(KERN_INFO " .Reserved = 0x%x\n", hdr->reserved); 441 printk(KERN_INFO " .Version = 0x%x\n", hdr->version); 442 443 /* Retrieve bootloader if reqd */ 444 if ((hdr->firmwaresize == 0) && (hdr->bslsize == 0)) 445 /* Second bootloader in the firmware file */ 446 filesize = hdr->reserved * 16; 447 else 448 filesize = (hdr->firmwaresize + hdr->bslsize) * 449 16 + sizeof(struct fw_header); 450 451 printk(KERN_INFO "%s() SecBootLoader.FileSize = %d\n", 452 __func__, filesize); 453 454 /* Get bootloader (if reqd) and firmware header */ 455 if ((hdr->firmwaresize == 0) && (hdr->bslsize == 0)) { 456 /* Second boot loader is required */ 457 458 /* Get the loader header */ 459 boothdr = (struct fw_header *)(fw->data + 460 sizeof(struct fw_header)); 461 462 bootloaderversion = 463 saa7164_readl(SAA_DEVICE_2ND_VERSION); 464 dprintk(DBGLVL_FW, "Onboard BootLoader:\n"); 465 dprintk(DBGLVL_FW, "->Flag 0x%x\n", 466 saa7164_readl(SAA_BOOTLOADERERROR_FLAGS)); 467 dprintk(DBGLVL_FW, "->Ack 0x%x\n", 468 saa7164_readl(SAA_DATAREADY_FLAG_ACK)); 469 dprintk(DBGLVL_FW, "->FW Version 0x%x\n", version); 470 dprintk(DBGLVL_FW, "->Loader Version 0x%x\n", 471 bootloaderversion); 472 473 if ((saa7164_readl(SAA_BOOTLOADERERROR_FLAGS) == 474 0x03) && (saa7164_readl(SAA_DATAREADY_FLAG_ACK) 475 == 0x00) && (version == 0x00)) { 476 477 dprintk(DBGLVL_FW, "BootLoader version in rom %d.%d.%d.%d\n", 478 (bootloaderversion & 0x0000fc00) >> 10, 479 (bootloaderversion & 0x000003e0) >> 5, 480 (bootloaderversion & 0x0000001f), 481 (bootloaderversion & 0xffff0000) >> 16 482 ); 483 dprintk(DBGLVL_FW, "BootLoader version in file %d.%d.%d.%d\n", 484 (boothdr->version & 0x0000fc00) >> 10, 485 (boothdr->version & 0x000003e0) >> 5, 486 (boothdr->version & 0x0000001f), 487 (boothdr->version & 0xffff0000) >> 16 488 ); 489 490 if (bootloaderversion == boothdr->version) 491 updatebootloader = 0; 492 } 493 494 /* Calculate offset to firmware header */ 495 tmp = (boothdr->firmwaresize + boothdr->bslsize) * 16 + 496 (sizeof(struct fw_header) + 497 sizeof(struct fw_header)); 498 499 fwhdr = (struct fw_header *)(fw->data+tmp); 500 } else { 501 /* No second boot loader */ 502 fwhdr = hdr; 503 } 504 505 dprintk(DBGLVL_FW, "Firmware version in file %d.%d.%d.%d\n", 506 (fwhdr->version & 0x0000fc00) >> 10, 507 (fwhdr->version & 0x000003e0) >> 5, 508 (fwhdr->version & 0x0000001f), 509 (fwhdr->version & 0xffff0000) >> 16 510 ); 511 512 if (version == fwhdr->version) { 513 /* No download, firmware already on board */ 514 ret = 0; 515 goto out; 516 } 517 518 if ((hdr->firmwaresize == 0) && (hdr->bslsize == 0)) { 519 if (updatebootloader) { 520 /* Get ready to upload the bootloader */ 521 bootloadersize = (boothdr->firmwaresize + 522 boothdr->bslsize) * 16 + 523 sizeof(struct fw_header); 524 525 bootloaderoffset = (u8 *)(fw->data + 526 sizeof(struct fw_header)); 527 528 dprintk(DBGLVL_FW, "bootloader d/l starts.\n"); 529 printk(KERN_INFO "%s() FirmwareSize = 0x%x\n", 530 __func__, boothdr->firmwaresize); 531 printk(KERN_INFO "%s() BSLSize = 0x%x\n", 532 __func__, boothdr->bslsize); 533 printk(KERN_INFO "%s() Reserved = 0x%x\n", 534 __func__, boothdr->reserved); 535 printk(KERN_INFO "%s() Version = 0x%x\n", 536 __func__, boothdr->version); 537 ret = saa7164_downloadimage( 538 dev, 539 bootloaderoffset, 540 bootloadersize, 541 SAA_DOWNLOAD_FLAGS, 542 dev->bmmio + SAA_DEVICE_DOWNLOAD_OFFSET, 543 SAA_DEVICE_BUFFERBLOCKSIZE); 544 if (ret < 0) { 545 printk(KERN_ERR 546 "bootloader d/l has failed\n"); 547 goto out; 548 } 549 dprintk(DBGLVL_FW, 550 "bootloader download complete.\n"); 551 552 } 553 554 printk(KERN_ERR "starting firmware download(2)\n"); 555 bootloadersize = (boothdr->firmwaresize + 556 boothdr->bslsize) * 16 + 557 sizeof(struct fw_header); 558 559 bootloaderoffset = 560 (u8 *)(fw->data + sizeof(struct fw_header)); 561 562 fwloaderoffset = bootloaderoffset + bootloadersize; 563 564 /* TODO: fix this bounds overrun here with old f/ws */ 565 fwloadersize = (fwhdr->firmwaresize + fwhdr->bslsize) * 566 16 + sizeof(struct fw_header); 567 568 ret = saa7164_downloadimage( 569 dev, 570 fwloaderoffset, 571 fwloadersize, 572 SAA_DEVICE_2ND_DOWNLOADFLAG_OFFSET, 573 dev->bmmio + SAA_DEVICE_2ND_DOWNLOAD_OFFSET, 574 SAA_DEVICE_2ND_BUFFERBLOCKSIZE); 575 if (ret < 0) { 576 printk(KERN_ERR "firmware download failed\n"); 577 goto out; 578 } 579 printk(KERN_ERR "firmware download complete.\n"); 580 581 } else { 582 583 /* No bootloader update reqd, download firmware only */ 584 printk(KERN_ERR "starting firmware download(3)\n"); 585 586 ret = saa7164_downloadimage( 587 dev, 588 (u8 *)fw->data, 589 fw->size, 590 SAA_DOWNLOAD_FLAGS, 591 dev->bmmio + SAA_DEVICE_DOWNLOAD_OFFSET, 592 SAA_DEVICE_BUFFERBLOCKSIZE); 593 if (ret < 0) { 594 printk(KERN_ERR "firmware download failed\n"); 595 goto out; 596 } 597 printk(KERN_ERR "firmware download complete.\n"); 598 } 599 } 600 601 dev->firmwareloaded = 1; 602 ret = 0; 603 604 out: 605 release_firmware(fw); 606 return ret; 607 } 608