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