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