Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / media / pci / ttpci / av7110_hw.c
blobb2b79bb739176c019aae693cc63de96690d47aad
1 /*
2 * av7110_hw.c: av7110 low level hardware access and firmware interface
4 * Copyright (C) 1999-2002 Ralph Metzler
5 * & Marcus Metzler for convergence integrated media GmbH
7 * originally based on code by:
8 * Copyright (C) 1998,1999 Christian Theiss <mistert@rz.fh-augsburg.de>
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 * To obtain the license, point your browser to
20 * http://www.gnu.org/copyleft/gpl.html
22 * the project's page is at https://linuxtv.org
25 /* for debugging ARM communication: */
26 //#define COM_DEBUG
28 #include <stdarg.h>
29 #include <linux/types.h>
30 #include <linux/kernel.h>
31 #include <linux/string.h>
32 #include <linux/delay.h>
33 #include <linux/fs.h>
35 #include "av7110.h"
36 #include "av7110_hw.h"
38 #define _NOHANDSHAKE
41 * Max transfer size done by av7110_fw_cmd()
43 * The maximum size passed to this function is 6 bytes. The buffer also
44 * uses two additional ones for type and size. So, 8 bytes is enough.
46 #define MAX_XFER_SIZE 8
48 /****************************************************************************
49 * DEBI functions
50 ****************************************************************************/
52 /* This DEBI code is based on the Stradis driver
53 by Nathan Laredo <laredo@gnu.org> */
55 int av7110_debiwrite(struct av7110 *av7110, u32 config,
56 int addr, u32 val, unsigned int count)
58 struct saa7146_dev *dev = av7110->dev;
60 if (count > 32764) {
61 printk("%s: invalid count %d\n", __func__, count);
62 return -1;
64 if (saa7146_wait_for_debi_done(av7110->dev, 0) < 0) {
65 printk("%s: wait_for_debi_done failed\n", __func__);
66 return -1;
68 saa7146_write(dev, DEBI_CONFIG, config);
69 if (count <= 4) /* immediate transfer */
70 saa7146_write(dev, DEBI_AD, val);
71 else /* block transfer */
72 saa7146_write(dev, DEBI_AD, av7110->debi_bus);
73 saa7146_write(dev, DEBI_COMMAND, (count << 17) | (addr & 0xffff));
74 saa7146_write(dev, MC2, (2 << 16) | 2);
75 return 0;
78 u32 av7110_debiread(struct av7110 *av7110, u32 config, int addr, unsigned int count)
80 struct saa7146_dev *dev = av7110->dev;
81 u32 result = 0;
83 if (count > 32764) {
84 printk("%s: invalid count %d\n", __func__, count);
85 return 0;
87 if (saa7146_wait_for_debi_done(av7110->dev, 0) < 0) {
88 printk("%s: wait_for_debi_done #1 failed\n", __func__);
89 return 0;
91 saa7146_write(dev, DEBI_AD, av7110->debi_bus);
92 saa7146_write(dev, DEBI_COMMAND, (count << 17) | 0x10000 | (addr & 0xffff));
94 saa7146_write(dev, DEBI_CONFIG, config);
95 saa7146_write(dev, MC2, (2 << 16) | 2);
96 if (count > 4)
97 return count;
98 if (saa7146_wait_for_debi_done(av7110->dev, 0) < 0) {
99 printk("%s: wait_for_debi_done #2 failed\n", __func__);
100 return 0;
103 result = saa7146_read(dev, DEBI_AD);
104 result &= (0xffffffffUL >> ((4 - count) * 8));
105 return result;
110 /* av7110 ARM core boot stuff */
111 #if 0
112 void av7110_reset_arm(struct av7110 *av7110)
114 saa7146_setgpio(av7110->dev, RESET_LINE, SAA7146_GPIO_OUTLO);
116 /* Disable DEBI and GPIO irq */
117 SAA7146_IER_DISABLE(av7110->dev, MASK_19 | MASK_03);
118 SAA7146_ISR_CLEAR(av7110->dev, MASK_19 | MASK_03);
120 saa7146_setgpio(av7110->dev, RESET_LINE, SAA7146_GPIO_OUTHI);
121 msleep(30); /* the firmware needs some time to initialize */
123 ARM_ResetMailBox(av7110);
125 SAA7146_ISR_CLEAR(av7110->dev, MASK_19 | MASK_03);
126 SAA7146_IER_ENABLE(av7110->dev, MASK_03);
128 av7110->arm_ready = 1;
129 dprintk(1, "reset ARM\n");
131 #endif /* 0 */
133 static int waitdebi(struct av7110 *av7110, int adr, int state)
135 int k;
137 dprintk(4, "%p\n", av7110);
139 for (k = 0; k < 100; k++) {
140 if (irdebi(av7110, DEBINOSWAP, adr, 0, 2) == state)
141 return 0;
142 udelay(5);
144 return -ETIMEDOUT;
147 static int load_dram(struct av7110 *av7110, u32 *data, int len)
149 int i;
150 int blocks, rest;
151 u32 base, bootblock = AV7110_BOOT_BLOCK;
153 dprintk(4, "%p\n", av7110);
155 blocks = len / AV7110_BOOT_MAX_SIZE;
156 rest = len % AV7110_BOOT_MAX_SIZE;
157 base = DRAM_START_CODE;
159 for (i = 0; i < blocks; i++) {
160 if (waitdebi(av7110, AV7110_BOOT_STATE, BOOTSTATE_BUFFER_EMPTY) < 0) {
161 printk(KERN_ERR "dvb-ttpci: load_dram(): timeout at block %d\n", i);
162 return -ETIMEDOUT;
164 dprintk(4, "writing DRAM block %d\n", i);
165 mwdebi(av7110, DEBISWAB, bootblock,
166 ((u8 *)data) + i * AV7110_BOOT_MAX_SIZE, AV7110_BOOT_MAX_SIZE);
167 bootblock ^= 0x1400;
168 iwdebi(av7110, DEBISWAB, AV7110_BOOT_BASE, swab32(base), 4);
169 iwdebi(av7110, DEBINOSWAP, AV7110_BOOT_SIZE, AV7110_BOOT_MAX_SIZE, 2);
170 iwdebi(av7110, DEBINOSWAP, AV7110_BOOT_STATE, BOOTSTATE_BUFFER_FULL, 2);
171 base += AV7110_BOOT_MAX_SIZE;
174 if (rest > 0) {
175 if (waitdebi(av7110, AV7110_BOOT_STATE, BOOTSTATE_BUFFER_EMPTY) < 0) {
176 printk(KERN_ERR "dvb-ttpci: load_dram(): timeout at last block\n");
177 return -ETIMEDOUT;
179 if (rest > 4)
180 mwdebi(av7110, DEBISWAB, bootblock,
181 ((u8 *)data) + i * AV7110_BOOT_MAX_SIZE, rest);
182 else
183 mwdebi(av7110, DEBISWAB, bootblock,
184 ((u8 *)data) + i * AV7110_BOOT_MAX_SIZE - 4, rest + 4);
186 iwdebi(av7110, DEBISWAB, AV7110_BOOT_BASE, swab32(base), 4);
187 iwdebi(av7110, DEBINOSWAP, AV7110_BOOT_SIZE, rest, 2);
188 iwdebi(av7110, DEBINOSWAP, AV7110_BOOT_STATE, BOOTSTATE_BUFFER_FULL, 2);
190 if (waitdebi(av7110, AV7110_BOOT_STATE, BOOTSTATE_BUFFER_EMPTY) < 0) {
191 printk(KERN_ERR "dvb-ttpci: load_dram(): timeout after last block\n");
192 return -ETIMEDOUT;
194 iwdebi(av7110, DEBINOSWAP, AV7110_BOOT_SIZE, 0, 2);
195 iwdebi(av7110, DEBINOSWAP, AV7110_BOOT_STATE, BOOTSTATE_BUFFER_FULL, 2);
196 if (waitdebi(av7110, AV7110_BOOT_STATE, BOOTSTATE_AV7110_BOOT_COMPLETE) < 0) {
197 printk(KERN_ERR "dvb-ttpci: load_dram(): final handshake timeout\n");
198 return -ETIMEDOUT;
200 return 0;
204 /* we cannot write av7110 DRAM directly, so load a bootloader into
205 * the DPRAM which implements a simple boot protocol */
206 int av7110_bootarm(struct av7110 *av7110)
208 const struct firmware *fw;
209 const char *fw_name = "av7110/bootcode.bin";
210 struct saa7146_dev *dev = av7110->dev;
211 u32 ret;
212 int i;
214 dprintk(4, "%p\n", av7110);
216 av7110->arm_ready = 0;
218 saa7146_setgpio(dev, RESET_LINE, SAA7146_GPIO_OUTLO);
220 /* Disable DEBI and GPIO irq */
221 SAA7146_IER_DISABLE(av7110->dev, MASK_03 | MASK_19);
222 SAA7146_ISR_CLEAR(av7110->dev, MASK_19 | MASK_03);
224 /* enable DEBI */
225 saa7146_write(av7110->dev, MC1, 0x08800880);
226 saa7146_write(av7110->dev, DD1_STREAM_B, 0x00000000);
227 saa7146_write(av7110->dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
229 /* test DEBI */
230 iwdebi(av7110, DEBISWAP, DPRAM_BASE, 0x76543210, 4);
231 /* FIXME: Why does Nexus CA require 2x iwdebi for first init? */
232 iwdebi(av7110, DEBISWAP, DPRAM_BASE, 0x76543210, 4);
234 if ((ret=irdebi(av7110, DEBINOSWAP, DPRAM_BASE, 0, 4)) != 0x10325476) {
235 printk(KERN_ERR "dvb-ttpci: debi test in av7110_bootarm() failed: %08x != %08x (check your BIOS 'Plug&Play OS' settings)\n",
236 ret, 0x10325476);
237 return -1;
239 for (i = 0; i < 8192; i += 4)
240 iwdebi(av7110, DEBISWAP, DPRAM_BASE + i, 0x00, 4);
241 dprintk(2, "debi test OK\n");
243 /* boot */
244 dprintk(1, "load boot code\n");
245 saa7146_setgpio(dev, ARM_IRQ_LINE, SAA7146_GPIO_IRQLO);
246 //saa7146_setgpio(dev, DEBI_DONE_LINE, SAA7146_GPIO_INPUT);
247 //saa7146_setgpio(dev, 3, SAA7146_GPIO_INPUT);
249 ret = request_firmware(&fw, fw_name, &dev->pci->dev);
250 if (ret) {
251 printk(KERN_ERR "dvb-ttpci: Failed to load firmware \"%s\"\n",
252 fw_name);
253 return ret;
256 mwdebi(av7110, DEBISWAB, DPRAM_BASE, fw->data, fw->size);
257 release_firmware(fw);
258 iwdebi(av7110, DEBINOSWAP, AV7110_BOOT_STATE, BOOTSTATE_BUFFER_FULL, 2);
260 if (saa7146_wait_for_debi_done(av7110->dev, 1)) {
261 printk(KERN_ERR "dvb-ttpci: av7110_bootarm(): saa7146_wait_for_debi_done() timed out\n");
262 return -ETIMEDOUT;
264 saa7146_setgpio(dev, RESET_LINE, SAA7146_GPIO_OUTHI);
265 mdelay(1);
267 dprintk(1, "load dram code\n");
268 if (load_dram(av7110, (u32 *)av7110->bin_root, av7110->size_root) < 0) {
269 printk(KERN_ERR "dvb-ttpci: av7110_bootarm(): load_dram() failed\n");
270 return -1;
273 saa7146_setgpio(dev, RESET_LINE, SAA7146_GPIO_OUTLO);
274 mdelay(1);
276 dprintk(1, "load dpram code\n");
277 mwdebi(av7110, DEBISWAB, DPRAM_BASE, av7110->bin_dpram, av7110->size_dpram);
279 if (saa7146_wait_for_debi_done(av7110->dev, 1)) {
280 printk(KERN_ERR "dvb-ttpci: av7110_bootarm(): saa7146_wait_for_debi_done() timed out after loading DRAM\n");
281 return -ETIMEDOUT;
283 saa7146_setgpio(dev, RESET_LINE, SAA7146_GPIO_OUTHI);
284 msleep(30); /* the firmware needs some time to initialize */
286 //ARM_ClearIrq(av7110);
287 ARM_ResetMailBox(av7110);
288 SAA7146_ISR_CLEAR(av7110->dev, MASK_19 | MASK_03);
289 SAA7146_IER_ENABLE(av7110->dev, MASK_03);
291 av7110->arm_errors = 0;
292 av7110->arm_ready = 1;
293 return 0;
295 MODULE_FIRMWARE("av7110/bootcode.bin");
297 /****************************************************************************
298 * DEBI command polling
299 ****************************************************************************/
301 int av7110_wait_msgstate(struct av7110 *av7110, u16 flags)
303 unsigned long start;
304 u32 stat;
305 int err;
307 if (FW_VERSION(av7110->arm_app) <= 0x261c) {
308 /* not supported by old firmware */
309 msleep(50);
310 return 0;
313 /* new firmware */
314 start = jiffies;
315 for (;;) {
316 err = time_after(jiffies, start + ARM_WAIT_FREE);
317 if (mutex_lock_interruptible(&av7110->dcomlock))
318 return -ERESTARTSYS;
319 stat = rdebi(av7110, DEBINOSWAP, MSGSTATE, 0, 2);
320 mutex_unlock(&av7110->dcomlock);
321 if ((stat & flags) == 0)
322 break;
323 if (err) {
324 printk(KERN_ERR "%s: timeout waiting for MSGSTATE %04x\n",
325 __func__, stat & flags);
326 return -ETIMEDOUT;
328 msleep(1);
330 return 0;
333 static int __av7110_send_fw_cmd(struct av7110 *av7110, u16* buf, int length)
335 int i;
336 unsigned long start;
337 char *type = NULL;
338 u16 flags[2] = {0, 0};
339 u32 stat;
340 int err;
342 // dprintk(4, "%p\n", av7110);
344 if (!av7110->arm_ready) {
345 dprintk(1, "arm not ready.\n");
346 return -ENXIO;
349 start = jiffies;
350 while (1) {
351 err = time_after(jiffies, start + ARM_WAIT_FREE);
352 if (rdebi(av7110, DEBINOSWAP, COMMAND, 0, 2) == 0)
353 break;
354 if (err) {
355 printk(KERN_ERR "dvb-ttpci: %s(): timeout waiting for COMMAND idle\n", __func__);
356 av7110->arm_errors++;
357 return -ETIMEDOUT;
359 msleep(1);
362 if (FW_VERSION(av7110->arm_app) <= 0x261f)
363 wdebi(av7110, DEBINOSWAP, COM_IF_LOCK, 0xffff, 2);
365 #ifndef _NOHANDSHAKE
366 start = jiffies;
367 while (1) {
368 err = time_after(jiffies, start + ARM_WAIT_SHAKE);
369 if (rdebi(av7110, DEBINOSWAP, HANDSHAKE_REG, 0, 2) == 0)
370 break;
371 if (err) {
372 printk(KERN_ERR "dvb-ttpci: %s(): timeout waiting for HANDSHAKE_REG\n", __func__);
373 return -ETIMEDOUT;
375 msleep(1);
377 #endif
379 switch ((buf[0] >> 8) & 0xff) {
380 case COMTYPE_PIDFILTER:
381 case COMTYPE_ENCODER:
382 case COMTYPE_REC_PLAY:
383 case COMTYPE_MPEGDECODER:
384 type = "MSG";
385 flags[0] = GPMQOver;
386 flags[1] = GPMQFull;
387 break;
388 case COMTYPE_OSD:
389 type = "OSD";
390 flags[0] = OSDQOver;
391 flags[1] = OSDQFull;
392 break;
393 case COMTYPE_MISC:
394 if (FW_VERSION(av7110->arm_app) >= 0x261d) {
395 type = "MSG";
396 flags[0] = GPMQOver;
397 flags[1] = GPMQBusy;
399 break;
400 default:
401 break;
404 if (type != NULL) {
405 /* non-immediate COMMAND type */
406 start = jiffies;
407 for (;;) {
408 err = time_after(jiffies, start + ARM_WAIT_FREE);
409 stat = rdebi(av7110, DEBINOSWAP, MSGSTATE, 0, 2);
410 if (stat & flags[0]) {
411 printk(KERN_ERR "%s: %s QUEUE overflow\n",
412 __func__, type);
413 return -1;
415 if ((stat & flags[1]) == 0)
416 break;
417 if (err) {
418 printk(KERN_ERR "%s: timeout waiting on busy %s QUEUE\n",
419 __func__, type);
420 av7110->arm_errors++;
421 return -ETIMEDOUT;
423 msleep(1);
427 for (i = 2; i < length; i++)
428 wdebi(av7110, DEBINOSWAP, COMMAND + 2 * i, (u32) buf[i], 2);
430 if (length)
431 wdebi(av7110, DEBINOSWAP, COMMAND + 2, (u32) buf[1], 2);
432 else
433 wdebi(av7110, DEBINOSWAP, COMMAND + 2, 0, 2);
435 wdebi(av7110, DEBINOSWAP, COMMAND, (u32) buf[0], 2);
437 if (FW_VERSION(av7110->arm_app) <= 0x261f)
438 wdebi(av7110, DEBINOSWAP, COM_IF_LOCK, 0x0000, 2);
440 #ifdef COM_DEBUG
441 start = jiffies;
442 while (1) {
443 err = time_after(jiffies, start + ARM_WAIT_FREE);
444 if (rdebi(av7110, DEBINOSWAP, COMMAND, 0, 2) == 0)
445 break;
446 if (err) {
447 printk(KERN_ERR "dvb-ttpci: %s(): timeout waiting for COMMAND %d to complete\n",
448 __func__, (buf[0] >> 8) & 0xff);
449 return -ETIMEDOUT;
451 msleep(1);
454 stat = rdebi(av7110, DEBINOSWAP, MSGSTATE, 0, 2);
455 if (stat & GPMQOver) {
456 printk(KERN_ERR "dvb-ttpci: %s(): GPMQOver\n", __func__);
457 return -ENOSPC;
459 else if (stat & OSDQOver) {
460 printk(KERN_ERR "dvb-ttpci: %s(): OSDQOver\n", __func__);
461 return -ENOSPC;
463 #endif
465 return 0;
468 static int av7110_send_fw_cmd(struct av7110 *av7110, u16* buf, int length)
470 int ret;
472 // dprintk(4, "%p\n", av7110);
474 if (!av7110->arm_ready) {
475 dprintk(1, "arm not ready.\n");
476 return -1;
478 if (mutex_lock_interruptible(&av7110->dcomlock))
479 return -ERESTARTSYS;
481 ret = __av7110_send_fw_cmd(av7110, buf, length);
482 mutex_unlock(&av7110->dcomlock);
483 if (ret && ret!=-ERESTARTSYS)
484 printk(KERN_ERR "dvb-ttpci: %s(): av7110_send_fw_cmd error %d\n",
485 __func__, ret);
486 return ret;
489 int av7110_fw_cmd(struct av7110 *av7110, int type, int com, int num, ...)
491 va_list args;
492 u16 buf[MAX_XFER_SIZE];
493 int i, ret;
495 // dprintk(4, "%p\n", av7110);
497 if (2 + num > ARRAY_SIZE(buf)) {
498 printk(KERN_WARNING
499 "%s: %s len=%d is too big!\n",
500 KBUILD_MODNAME, __func__, num);
501 return -EINVAL;
504 buf[0] = ((type << 8) | com);
505 buf[1] = num;
507 if (num) {
508 va_start(args, num);
509 for (i = 0; i < num; i++)
510 buf[i + 2] = va_arg(args, u32);
511 va_end(args);
514 ret = av7110_send_fw_cmd(av7110, buf, num + 2);
515 if (ret && ret != -ERESTARTSYS)
516 printk(KERN_ERR "dvb-ttpci: av7110_fw_cmd error %d\n", ret);
517 return ret;
520 #if 0
521 int av7110_send_ci_cmd(struct av7110 *av7110, u8 subcom, u8 *buf, u8 len)
523 int i, ret;
524 u16 cmd[18] = { ((COMTYPE_COMMON_IF << 8) + subcom),
525 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
527 dprintk(4, "%p\n", av7110);
529 for(i = 0; i < len && i < 32; i++)
531 if(i % 2 == 0)
532 cmd[(i / 2) + 2] = (u16)(buf[i]) << 8;
533 else
534 cmd[(i / 2) + 2] |= buf[i];
537 ret = av7110_send_fw_cmd(av7110, cmd, 18);
538 if (ret && ret != -ERESTARTSYS)
539 printk(KERN_ERR "dvb-ttpci: av7110_send_ci_cmd error %d\n", ret);
540 return ret;
542 #endif /* 0 */
544 int av7110_fw_request(struct av7110 *av7110, u16 *request_buf,
545 int request_buf_len, u16 *reply_buf, int reply_buf_len)
547 int err;
548 s16 i;
549 unsigned long start;
550 #ifdef COM_DEBUG
551 u32 stat;
552 #endif
554 dprintk(4, "%p\n", av7110);
556 if (!av7110->arm_ready) {
557 dprintk(1, "arm not ready.\n");
558 return -1;
561 if (mutex_lock_interruptible(&av7110->dcomlock))
562 return -ERESTARTSYS;
564 if ((err = __av7110_send_fw_cmd(av7110, request_buf, request_buf_len)) < 0) {
565 mutex_unlock(&av7110->dcomlock);
566 printk(KERN_ERR "dvb-ttpci: av7110_fw_request error %d\n", err);
567 return err;
570 start = jiffies;
571 while (1) {
572 err = time_after(jiffies, start + ARM_WAIT_FREE);
573 if (rdebi(av7110, DEBINOSWAP, COMMAND, 0, 2) == 0)
574 break;
575 if (err) {
576 printk(KERN_ERR "%s: timeout waiting for COMMAND to complete\n", __func__);
577 mutex_unlock(&av7110->dcomlock);
578 return -ETIMEDOUT;
580 #ifdef _NOHANDSHAKE
581 msleep(1);
582 #endif
585 #ifndef _NOHANDSHAKE
586 start = jiffies;
587 while (1) {
588 err = time_after(jiffies, start + ARM_WAIT_SHAKE);
589 if (rdebi(av7110, DEBINOSWAP, HANDSHAKE_REG, 0, 2) == 0)
590 break;
591 if (err) {
592 printk(KERN_ERR "%s: timeout waiting for HANDSHAKE_REG\n", __func__);
593 mutex_unlock(&av7110->dcomlock);
594 return -ETIMEDOUT;
596 msleep(1);
598 #endif
600 #ifdef COM_DEBUG
601 stat = rdebi(av7110, DEBINOSWAP, MSGSTATE, 0, 2);
602 if (stat & GPMQOver) {
603 printk(KERN_ERR "%s: GPMQOver\n", __func__);
604 mutex_unlock(&av7110->dcomlock);
605 return -1;
607 else if (stat & OSDQOver) {
608 printk(KERN_ERR "%s: OSDQOver\n", __func__);
609 mutex_unlock(&av7110->dcomlock);
610 return -1;
612 #endif
614 for (i = 0; i < reply_buf_len; i++)
615 reply_buf[i] = rdebi(av7110, DEBINOSWAP, COM_BUFF + 2 * i, 0, 2);
617 mutex_unlock(&av7110->dcomlock);
618 return 0;
621 static int av7110_fw_query(struct av7110 *av7110, u16 tag, u16* buf, s16 length)
623 int ret;
624 ret = av7110_fw_request(av7110, &tag, 0, buf, length);
625 if (ret)
626 printk(KERN_ERR "dvb-ttpci: av7110_fw_query error %d\n", ret);
627 return ret;
631 /****************************************************************************
632 * Firmware commands
633 ****************************************************************************/
635 /* get version of the firmware ROM, RTSL, video ucode and ARM application */
636 int av7110_firmversion(struct av7110 *av7110)
638 u16 buf[20];
639 u16 tag = ((COMTYPE_REQUEST << 8) + ReqVersion);
641 dprintk(4, "%p\n", av7110);
643 if (av7110_fw_query(av7110, tag, buf, 16)) {
644 printk("dvb-ttpci: failed to boot firmware @ card %d\n",
645 av7110->dvb_adapter.num);
646 return -EIO;
649 av7110->arm_fw = (buf[0] << 16) + buf[1];
650 av7110->arm_rtsl = (buf[2] << 16) + buf[3];
651 av7110->arm_vid = (buf[4] << 16) + buf[5];
652 av7110->arm_app = (buf[6] << 16) + buf[7];
653 av7110->avtype = (buf[8] << 16) + buf[9];
655 printk("dvb-ttpci: info @ card %d: firm %08x, rtsl %08x, vid %08x, app %08x\n",
656 av7110->dvb_adapter.num, av7110->arm_fw,
657 av7110->arm_rtsl, av7110->arm_vid, av7110->arm_app);
659 /* print firmware capabilities */
660 if (FW_CI_LL_SUPPORT(av7110->arm_app))
661 printk("dvb-ttpci: firmware @ card %d supports CI link layer interface\n",
662 av7110->dvb_adapter.num);
663 else
664 printk("dvb-ttpci: no firmware support for CI link layer interface @ card %d\n",
665 av7110->dvb_adapter.num);
667 return 0;
671 int av7110_diseqc_send(struct av7110 *av7110, int len, u8 *msg, unsigned long burst)
673 int i, ret;
674 u16 buf[18] = { ((COMTYPE_AUDIODAC << 8) + SendDiSEqC),
675 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
677 dprintk(4, "%p\n", av7110);
679 if (len > 10)
680 len = 10;
682 buf[1] = len + 2;
683 buf[2] = len;
685 if (burst != -1)
686 buf[3] = burst ? 0x01 : 0x00;
687 else
688 buf[3] = 0xffff;
690 for (i = 0; i < len; i++)
691 buf[i + 4] = msg[i];
693 ret = av7110_send_fw_cmd(av7110, buf, 18);
694 if (ret && ret!=-ERESTARTSYS)
695 printk(KERN_ERR "dvb-ttpci: av7110_diseqc_send error %d\n", ret);
696 return ret;
700 #ifdef CONFIG_DVB_AV7110_OSD
702 static inline int SetColorBlend(struct av7110 *av7110, u8 windownr)
704 return av7110_fw_cmd(av7110, COMTYPE_OSD, SetCBlend, 1, windownr);
707 static inline int SetBlend_(struct av7110 *av7110, u8 windownr,
708 enum av7110_osd_palette_type colordepth, u16 index, u8 blending)
710 return av7110_fw_cmd(av7110, COMTYPE_OSD, SetBlend, 4,
711 windownr, colordepth, index, blending);
714 static inline int SetColor_(struct av7110 *av7110, u8 windownr,
715 enum av7110_osd_palette_type colordepth, u16 index, u16 colorhi, u16 colorlo)
717 return av7110_fw_cmd(av7110, COMTYPE_OSD, SetColor, 5,
718 windownr, colordepth, index, colorhi, colorlo);
721 static inline int SetFont(struct av7110 *av7110, u8 windownr, u8 fontsize,
722 u16 colorfg, u16 colorbg)
724 return av7110_fw_cmd(av7110, COMTYPE_OSD, Set_Font, 4,
725 windownr, fontsize, colorfg, colorbg);
728 static int FlushText(struct av7110 *av7110)
730 unsigned long start;
731 int err;
733 if (mutex_lock_interruptible(&av7110->dcomlock))
734 return -ERESTARTSYS;
735 start = jiffies;
736 while (1) {
737 err = time_after(jiffies, start + ARM_WAIT_OSD);
738 if (rdebi(av7110, DEBINOSWAP, BUFF1_BASE, 0, 2) == 0)
739 break;
740 if (err) {
741 printk(KERN_ERR "dvb-ttpci: %s(): timeout waiting for BUFF1_BASE == 0\n",
742 __func__);
743 mutex_unlock(&av7110->dcomlock);
744 return -ETIMEDOUT;
746 msleep(1);
748 mutex_unlock(&av7110->dcomlock);
749 return 0;
752 static int WriteText(struct av7110 *av7110, u8 win, u16 x, u16 y, char *buf)
754 int i, ret;
755 unsigned long start;
756 int length = strlen(buf) + 1;
757 u16 cbuf[5] = { (COMTYPE_OSD << 8) + DText, 3, win, x, y };
759 if (mutex_lock_interruptible(&av7110->dcomlock))
760 return -ERESTARTSYS;
762 start = jiffies;
763 while (1) {
764 ret = time_after(jiffies, start + ARM_WAIT_OSD);
765 if (rdebi(av7110, DEBINOSWAP, BUFF1_BASE, 0, 2) == 0)
766 break;
767 if (ret) {
768 printk(KERN_ERR "dvb-ttpci: %s: timeout waiting for BUFF1_BASE == 0\n",
769 __func__);
770 mutex_unlock(&av7110->dcomlock);
771 return -ETIMEDOUT;
773 msleep(1);
775 #ifndef _NOHANDSHAKE
776 start = jiffies;
777 while (1) {
778 ret = time_after(jiffies, start + ARM_WAIT_SHAKE);
779 if (rdebi(av7110, DEBINOSWAP, HANDSHAKE_REG, 0, 2) == 0)
780 break;
781 if (ret) {
782 printk(KERN_ERR "dvb-ttpci: %s: timeout waiting for HANDSHAKE_REG\n",
783 __func__);
784 mutex_unlock(&av7110->dcomlock);
785 return -ETIMEDOUT;
787 msleep(1);
789 #endif
790 for (i = 0; i < length / 2; i++)
791 wdebi(av7110, DEBINOSWAP, BUFF1_BASE + i * 2,
792 swab16(*(u16 *)(buf + 2 * i)), 2);
793 if (length & 1)
794 wdebi(av7110, DEBINOSWAP, BUFF1_BASE + i * 2, 0, 2);
795 ret = __av7110_send_fw_cmd(av7110, cbuf, 5);
796 mutex_unlock(&av7110->dcomlock);
797 if (ret && ret!=-ERESTARTSYS)
798 printk(KERN_ERR "dvb-ttpci: WriteText error %d\n", ret);
799 return ret;
802 static inline int DrawLine(struct av7110 *av7110, u8 windownr,
803 u16 x, u16 y, u16 dx, u16 dy, u16 color)
805 return av7110_fw_cmd(av7110, COMTYPE_OSD, DLine, 6,
806 windownr, x, y, dx, dy, color);
809 static inline int DrawBlock(struct av7110 *av7110, u8 windownr,
810 u16 x, u16 y, u16 dx, u16 dy, u16 color)
812 return av7110_fw_cmd(av7110, COMTYPE_OSD, DBox, 6,
813 windownr, x, y, dx, dy, color);
816 static inline int HideWindow(struct av7110 *av7110, u8 windownr)
818 return av7110_fw_cmd(av7110, COMTYPE_OSD, WHide, 1, windownr);
821 static inline int MoveWindowRel(struct av7110 *av7110, u8 windownr, u16 x, u16 y)
823 return av7110_fw_cmd(av7110, COMTYPE_OSD, WMoveD, 3, windownr, x, y);
826 static inline int MoveWindowAbs(struct av7110 *av7110, u8 windownr, u16 x, u16 y)
828 return av7110_fw_cmd(av7110, COMTYPE_OSD, WMoveA, 3, windownr, x, y);
831 static inline int DestroyOSDWindow(struct av7110 *av7110, u8 windownr)
833 return av7110_fw_cmd(av7110, COMTYPE_OSD, WDestroy, 1, windownr);
836 static inline int CreateOSDWindow(struct av7110 *av7110, u8 windownr,
837 osd_raw_window_t disptype,
838 u16 width, u16 height)
840 return av7110_fw_cmd(av7110, COMTYPE_OSD, WCreate, 4,
841 windownr, disptype, width, height);
845 static enum av7110_osd_palette_type bpp2pal[8] = {
846 Pal1Bit, Pal2Bit, 0, Pal4Bit, 0, 0, 0, Pal8Bit
848 static osd_raw_window_t bpp2bit[8] = {
849 OSD_BITMAP1, OSD_BITMAP2, 0, OSD_BITMAP4, 0, 0, 0, OSD_BITMAP8
852 static inline int WaitUntilBmpLoaded(struct av7110 *av7110)
854 int ret = wait_event_timeout(av7110->bmpq,
855 av7110->bmp_state != BMP_LOADING, 10*HZ);
856 if (ret == 0) {
857 printk("dvb-ttpci: warning: timeout waiting in LoadBitmap: %d, %d\n",
858 ret, av7110->bmp_state);
859 av7110->bmp_state = BMP_NONE;
860 return -ETIMEDOUT;
862 return 0;
865 static inline int LoadBitmap(struct av7110 *av7110,
866 u16 dx, u16 dy, int inc, u8 __user * data)
868 u16 format;
869 int bpp;
870 int i;
871 int d, delta;
872 u8 c;
873 int ret;
875 dprintk(4, "%p\n", av7110);
877 format = bpp2bit[av7110->osdbpp[av7110->osdwin]];
879 av7110->bmp_state = BMP_LOADING;
880 if (format == OSD_BITMAP8) {
881 bpp=8; delta = 1;
882 } else if (format == OSD_BITMAP4) {
883 bpp=4; delta = 2;
884 } else if (format == OSD_BITMAP2) {
885 bpp=2; delta = 4;
886 } else if (format == OSD_BITMAP1) {
887 bpp=1; delta = 8;
888 } else {
889 av7110->bmp_state = BMP_NONE;
890 return -EINVAL;
892 av7110->bmplen = ((dx * dy * bpp + 7) & ~7) / 8;
893 av7110->bmpp = 0;
894 if (av7110->bmplen > 32768) {
895 av7110->bmp_state = BMP_NONE;
896 return -EINVAL;
898 for (i = 0; i < dy; i++) {
899 if (copy_from_user(av7110->bmpbuf + 1024 + i * dx, data + i * inc, dx)) {
900 av7110->bmp_state = BMP_NONE;
901 return -EINVAL;
904 if (format != OSD_BITMAP8) {
905 for (i = 0; i < dx * dy / delta; i++) {
906 c = ((u8 *)av7110->bmpbuf)[1024 + i * delta + delta - 1];
907 for (d = delta - 2; d >= 0; d--) {
908 c |= (((u8 *)av7110->bmpbuf)[1024 + i * delta + d]
909 << ((delta - d - 1) * bpp));
910 ((u8 *)av7110->bmpbuf)[1024 + i] = c;
914 av7110->bmplen += 1024;
915 dprintk(4, "av7110_fw_cmd: LoadBmp size %d\n", av7110->bmplen);
916 ret = av7110_fw_cmd(av7110, COMTYPE_OSD, LoadBmp, 3, format, dx, dy);
917 if (!ret)
918 ret = WaitUntilBmpLoaded(av7110);
919 return ret;
922 static int BlitBitmap(struct av7110 *av7110, u16 x, u16 y)
924 dprintk(4, "%p\n", av7110);
926 return av7110_fw_cmd(av7110, COMTYPE_OSD, BlitBmp, 4, av7110->osdwin, x, y, 0);
929 static inline int ReleaseBitmap(struct av7110 *av7110)
931 dprintk(4, "%p\n", av7110);
933 if (av7110->bmp_state != BMP_LOADED && FW_VERSION(av7110->arm_app) < 0x261e)
934 return -1;
935 if (av7110->bmp_state == BMP_LOADING)
936 dprintk(1,"ReleaseBitmap called while BMP_LOADING\n");
937 av7110->bmp_state = BMP_NONE;
938 return av7110_fw_cmd(av7110, COMTYPE_OSD, ReleaseBmp, 0);
941 static u32 RGB2YUV(u16 R, u16 G, u16 B)
943 u16 y, u, v;
944 u16 Y, Cr, Cb;
946 y = R * 77 + G * 150 + B * 29; /* Luma=0.299R+0.587G+0.114B 0..65535 */
947 u = 2048 + B * 8 -(y >> 5); /* Cr 0..4095 */
948 v = 2048 + R * 8 -(y >> 5); /* Cb 0..4095 */
950 Y = y / 256;
951 Cb = u / 16;
952 Cr = v / 16;
954 return Cr | (Cb << 16) | (Y << 8);
957 static int OSDSetColor(struct av7110 *av7110, u8 color, u8 r, u8 g, u8 b, u8 blend)
959 int ret;
961 u16 ch, cl;
962 u32 yuv;
964 yuv = blend ? RGB2YUV(r,g,b) : 0;
965 cl = (yuv & 0xffff);
966 ch = ((yuv >> 16) & 0xffff);
967 ret = SetColor_(av7110, av7110->osdwin, bpp2pal[av7110->osdbpp[av7110->osdwin]],
968 color, ch, cl);
969 if (!ret)
970 ret = SetBlend_(av7110, av7110->osdwin, bpp2pal[av7110->osdbpp[av7110->osdwin]],
971 color, ((blend >> 4) & 0x0f));
972 return ret;
975 static int OSDSetPalette(struct av7110 *av7110, u32 __user * colors, u8 first, u8 last)
977 int i;
978 int length = last - first + 1;
980 if (length * 4 > DATA_BUFF3_SIZE)
981 return -EINVAL;
983 for (i = 0; i < length; i++) {
984 u32 color, blend, yuv;
986 if (get_user(color, colors + i))
987 return -EFAULT;
988 blend = (color & 0xF0000000) >> 4;
989 yuv = blend ? RGB2YUV(color & 0xFF, (color >> 8) & 0xFF,
990 (color >> 16) & 0xFF) | blend : 0;
991 yuv = ((yuv & 0xFFFF0000) >> 16) | ((yuv & 0x0000FFFF) << 16);
992 wdebi(av7110, DEBINOSWAP, DATA_BUFF3_BASE + i * 4, yuv, 4);
994 return av7110_fw_cmd(av7110, COMTYPE_OSD, Set_Palette, 4,
995 av7110->osdwin,
996 bpp2pal[av7110->osdbpp[av7110->osdwin]],
997 first, last);
1000 static int OSDSetBlock(struct av7110 *av7110, int x0, int y0,
1001 int x1, int y1, int inc, u8 __user * data)
1003 uint w, h, bpp, bpl, size, lpb, bnum, brest;
1004 int i;
1005 int rc,release_rc;
1007 w = x1 - x0 + 1;
1008 h = y1 - y0 + 1;
1009 if (inc <= 0)
1010 inc = w;
1011 if (w <= 0 || w > 720 || h <= 0 || h > 576)
1012 return -EINVAL;
1013 bpp = av7110->osdbpp[av7110->osdwin] + 1;
1014 bpl = ((w * bpp + 7) & ~7) / 8;
1015 size = h * bpl;
1016 lpb = (32 * 1024) / bpl;
1017 bnum = size / (lpb * bpl);
1018 brest = size - bnum * lpb * bpl;
1020 if (av7110->bmp_state == BMP_LOADING) {
1021 /* possible if syscall is repeated by -ERESTARTSYS and if firmware cannot abort */
1022 BUG_ON (FW_VERSION(av7110->arm_app) >= 0x261e);
1023 rc = WaitUntilBmpLoaded(av7110);
1024 if (rc)
1025 return rc;
1026 /* just continue. This should work for all fw versions
1027 * if bnum==1 && !brest && LoadBitmap was successful
1031 rc = 0;
1032 for (i = 0; i < bnum; i++) {
1033 rc = LoadBitmap(av7110, w, lpb, inc, data);
1034 if (rc)
1035 break;
1036 rc = BlitBitmap(av7110, x0, y0 + i * lpb);
1037 if (rc)
1038 break;
1039 data += lpb * inc;
1041 if (!rc && brest) {
1042 rc = LoadBitmap(av7110, w, brest / bpl, inc, data);
1043 if (!rc)
1044 rc = BlitBitmap(av7110, x0, y0 + bnum * lpb);
1046 release_rc = ReleaseBitmap(av7110);
1047 if (!rc)
1048 rc = release_rc;
1049 if (rc)
1050 dprintk(1,"returns %d\n",rc);
1051 return rc;
1054 int av7110_osd_cmd(struct av7110 *av7110, osd_cmd_t *dc)
1056 int ret;
1058 if (mutex_lock_interruptible(&av7110->osd_mutex))
1059 return -ERESTARTSYS;
1061 switch (dc->cmd) {
1062 case OSD_Close:
1063 ret = DestroyOSDWindow(av7110, av7110->osdwin);
1064 break;
1065 case OSD_Open:
1066 av7110->osdbpp[av7110->osdwin] = (dc->color - 1) & 7;
1067 ret = CreateOSDWindow(av7110, av7110->osdwin,
1068 bpp2bit[av7110->osdbpp[av7110->osdwin]],
1069 dc->x1 - dc->x0 + 1, dc->y1 - dc->y0 + 1);
1070 if (ret)
1071 break;
1072 if (!dc->data) {
1073 ret = MoveWindowAbs(av7110, av7110->osdwin, dc->x0, dc->y0);
1074 if (ret)
1075 break;
1076 ret = SetColorBlend(av7110, av7110->osdwin);
1078 break;
1079 case OSD_Show:
1080 ret = MoveWindowRel(av7110, av7110->osdwin, 0, 0);
1081 break;
1082 case OSD_Hide:
1083 ret = HideWindow(av7110, av7110->osdwin);
1084 break;
1085 case OSD_Clear:
1086 ret = DrawBlock(av7110, av7110->osdwin, 0, 0, 720, 576, 0);
1087 break;
1088 case OSD_Fill:
1089 ret = DrawBlock(av7110, av7110->osdwin, 0, 0, 720, 576, dc->color);
1090 break;
1091 case OSD_SetColor:
1092 ret = OSDSetColor(av7110, dc->color, dc->x0, dc->y0, dc->x1, dc->y1);
1093 break;
1094 case OSD_SetPalette:
1095 if (FW_VERSION(av7110->arm_app) >= 0x2618)
1096 ret = OSDSetPalette(av7110, dc->data, dc->color, dc->x0);
1097 else {
1098 int i, len = dc->x0-dc->color+1;
1099 u8 __user *colors = (u8 __user *)dc->data;
1100 u8 r, g = 0, b = 0, blend = 0;
1101 ret = 0;
1102 for (i = 0; i<len; i++) {
1103 if (get_user(r, colors + i * 4) ||
1104 get_user(g, colors + i * 4 + 1) ||
1105 get_user(b, colors + i * 4 + 2) ||
1106 get_user(blend, colors + i * 4 + 3)) {
1107 ret = -EFAULT;
1108 break;
1110 ret = OSDSetColor(av7110, dc->color + i, r, g, b, blend);
1111 if (ret)
1112 break;
1115 break;
1116 case OSD_SetPixel:
1117 ret = DrawLine(av7110, av7110->osdwin,
1118 dc->x0, dc->y0, 0, 0, dc->color);
1119 break;
1120 case OSD_SetRow:
1121 dc->y1 = dc->y0;
1122 /* fall through */
1123 case OSD_SetBlock:
1124 ret = OSDSetBlock(av7110, dc->x0, dc->y0, dc->x1, dc->y1, dc->color, dc->data);
1125 break;
1126 case OSD_FillRow:
1127 ret = DrawBlock(av7110, av7110->osdwin, dc->x0, dc->y0,
1128 dc->x1-dc->x0+1, dc->y1, dc->color);
1129 break;
1130 case OSD_FillBlock:
1131 ret = DrawBlock(av7110, av7110->osdwin, dc->x0, dc->y0,
1132 dc->x1 - dc->x0 + 1, dc->y1 - dc->y0 + 1, dc->color);
1133 break;
1134 case OSD_Line:
1135 ret = DrawLine(av7110, av7110->osdwin,
1136 dc->x0, dc->y0, dc->x1 - dc->x0, dc->y1 - dc->y0, dc->color);
1137 break;
1138 case OSD_Text:
1140 char textbuf[240];
1142 if (strncpy_from_user(textbuf, dc->data, 240) < 0) {
1143 ret = -EFAULT;
1144 break;
1146 textbuf[239] = 0;
1147 if (dc->x1 > 3)
1148 dc->x1 = 3;
1149 ret = SetFont(av7110, av7110->osdwin, dc->x1,
1150 (u16) (dc->color & 0xffff), (u16) (dc->color >> 16));
1151 if (!ret)
1152 ret = FlushText(av7110);
1153 if (!ret)
1154 ret = WriteText(av7110, av7110->osdwin, dc->x0, dc->y0, textbuf);
1155 break;
1157 case OSD_SetWindow:
1158 if (dc->x0 < 1 || dc->x0 > 7)
1159 ret = -EINVAL;
1160 else {
1161 av7110->osdwin = dc->x0;
1162 ret = 0;
1164 break;
1165 case OSD_MoveWindow:
1166 ret = MoveWindowAbs(av7110, av7110->osdwin, dc->x0, dc->y0);
1167 if (!ret)
1168 ret = SetColorBlend(av7110, av7110->osdwin);
1169 break;
1170 case OSD_OpenRaw:
1171 if (dc->color < OSD_BITMAP1 || dc->color > OSD_CURSOR) {
1172 ret = -EINVAL;
1173 break;
1175 if (dc->color >= OSD_BITMAP1 && dc->color <= OSD_BITMAP8HR)
1176 av7110->osdbpp[av7110->osdwin] = (1 << (dc->color & 3)) - 1;
1177 else
1178 av7110->osdbpp[av7110->osdwin] = 0;
1179 ret = CreateOSDWindow(av7110, av7110->osdwin, (osd_raw_window_t)dc->color,
1180 dc->x1 - dc->x0 + 1, dc->y1 - dc->y0 + 1);
1181 if (ret)
1182 break;
1183 if (!dc->data) {
1184 ret = MoveWindowAbs(av7110, av7110->osdwin, dc->x0, dc->y0);
1185 if (!ret)
1186 ret = SetColorBlend(av7110, av7110->osdwin);
1188 break;
1189 default:
1190 ret = -EINVAL;
1191 break;
1194 mutex_unlock(&av7110->osd_mutex);
1195 if (ret==-ERESTARTSYS)
1196 dprintk(1, "av7110_osd_cmd(%d) returns with -ERESTARTSYS\n",dc->cmd);
1197 else if (ret)
1198 dprintk(1, "av7110_osd_cmd(%d) returns with %d\n",dc->cmd,ret);
1200 return ret;
1203 int av7110_osd_capability(struct av7110 *av7110, osd_cap_t *cap)
1205 switch (cap->cmd) {
1206 case OSD_CAP_MEMSIZE:
1207 if (FW_4M_SDRAM(av7110->arm_app))
1208 cap->val = 1000000;
1209 else
1210 cap->val = 92000;
1211 return 0;
1212 default:
1213 return -EINVAL;
1216 #endif /* CONFIG_DVB_AV7110_OSD */