V4L/DVB (9515): cx18: Use correct Mailbox IRQ Ack values and misc IRQ handling cleanup
[linux-2.6] / drivers / media / video / cx18 / cx18-irq.c
1 /*
2  *  cx18 interrupt handling
3  *
4  *  Copyright (C) 2007  Hans Verkuil <hverkuil@xs4all.nl>
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  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19  *  02111-1307  USA
20  */
21
22 #include "cx18-driver.h"
23 #include "cx18-io.h"
24 #include "cx18-firmware.h"
25 #include "cx18-fileops.h"
26 #include "cx18-queue.h"
27 #include "cx18-irq.h"
28 #include "cx18-ioctl.h"
29 #include "cx18-mailbox.h"
30 #include "cx18-vbi.h"
31 #include "cx18-scb.h"
32
33 static void epu_dma_done(struct cx18 *cx, struct cx18_mailbox *mb)
34 {
35         u32 handle = mb->args[0];
36         struct cx18_stream *s = NULL;
37         struct cx18_buffer *buf;
38         u32 off;
39         int i;
40         int id;
41
42         for (i = 0; i < CX18_MAX_STREAMS; i++) {
43                 s = &cx->streams[i];
44                 if ((handle == s->handle) && (s->dvb.enabled))
45                         break;
46                 if (s->v4l2dev && handle == s->handle)
47                         break;
48         }
49         if (i == CX18_MAX_STREAMS) {
50                 CX18_WARN("Got DMA done notification for unknown/inactive"
51                           " handle %d\n", handle);
52                 mb->error = CXERR_NOT_OPEN;
53                 mb->cmd = 0;
54                 cx18_mb_ack(cx, mb);
55                 return;
56         }
57
58         off = mb->args[1];
59         if (mb->args[2] != 1)
60                 CX18_WARN("Ack struct = %d for %s\n",
61                         mb->args[2], s->name);
62         id = cx18_read_enc(cx, off);
63         buf = cx18_queue_get_buf_irq(s, id, cx18_read_enc(cx, off + 4));
64         CX18_DEBUG_HI_DMA("DMA DONE for %s (buffer %d)\n", s->name, id);
65         if (buf) {
66                 cx18_buf_sync_for_cpu(s, buf);
67                 if (s->type == CX18_ENC_STREAM_TYPE_TS && s->dvb.enabled) {
68                         /* process the buffer here */
69                         CX18_DEBUG_HI_DMA("TS recv and sent bytesused=%d\n",
70                                         buf->bytesused);
71
72                         dvb_dmx_swfilter(&s->dvb.demux, buf->buf,
73                                         buf->bytesused);
74
75                         cx18_buf_sync_for_device(s, buf);
76                         cx18_vapi(cx, CX18_CPU_DE_SET_MDL, 5, s->handle,
77                             (void __iomem *)&cx->scb->cpu_mdl[buf->id] - cx->enc_mem,
78                             1, buf->id, s->buf_size);
79                 } else
80                         set_bit(CX18_F_B_NEED_BUF_SWAP, &buf->b_flags);
81         } else {
82                 CX18_WARN("Could not find buf %d for stream %s\n",
83                                 cx18_read_enc(cx, off), s->name);
84         }
85         mb->error = 0;
86         mb->cmd = 0;
87         cx18_mb_ack(cx, mb);
88         wake_up(&cx->dma_waitq);
89         if (s->id != -1)
90                 wake_up(&s->waitq);
91 }
92
93 static void epu_debug(struct cx18 *cx, struct cx18_mailbox *mb)
94 {
95         char str[256] = { 0 };
96         char *p;
97
98         if (mb->args[1]) {
99                 cx18_setup_page(cx, mb->args[1]);
100                 cx18_memcpy_fromio(cx, str, cx->enc_mem + mb->args[1], 252);
101                 str[252] = 0;
102         }
103         cx18_mb_ack(cx, mb);
104         CX18_DEBUG_INFO("%x %s\n", mb->args[0], str);
105         p = strchr(str, '.');
106         if (!test_bit(CX18_F_I_LOADED_FW, &cx->i_flags) && p && p > str)
107                 CX18_INFO("FW version: %s\n", p - 1);
108 }
109
110 static void epu_cmd(struct cx18 *cx, u32 sw1)
111 {
112         struct cx18_mailbox mb;
113
114         if (sw1 & IRQ_CPU_TO_EPU) {
115                 cx18_memcpy_fromio(cx, &mb, &cx->scb->cpu2epu_mb, sizeof(mb));
116                 mb.error = 0;
117
118                 switch (mb.cmd) {
119                 case CX18_EPU_DMA_DONE:
120                         epu_dma_done(cx, &mb);
121                         break;
122                 case CX18_EPU_DEBUG:
123                         epu_debug(cx, &mb);
124                         break;
125                 default:
126                         CX18_WARN("Unknown CPU_TO_EPU mailbox command %#08x\n",
127                                   mb.cmd);
128                         break;
129                 }
130         }
131
132         if (sw1 & IRQ_APU_TO_EPU) {
133                 cx18_memcpy_fromio(cx, &mb, &cx->scb->apu2epu_mb, sizeof(mb));
134                 CX18_WARN("Unknown APU_TO_EPU mailbox command %#08x\n", mb.cmd);
135         }
136
137         if (sw1 & IRQ_HPU_TO_EPU) {
138                 cx18_memcpy_fromio(cx, &mb, &cx->scb->hpu2epu_mb, sizeof(mb));
139                 CX18_WARN("Unknown HPU_TO_EPU mailbox command %#08x\n", mb.cmd);
140         }
141 }
142
143 static void xpu_ack(struct cx18 *cx, u32 sw2)
144 {
145         if (sw2 & IRQ_CPU_TO_EPU_ACK)
146                 wake_up(&cx->mb_cpu_waitq);
147         if (sw2 & IRQ_APU_TO_EPU_ACK)
148                 wake_up(&cx->mb_apu_waitq);
149         if (sw2 & IRQ_HPU_TO_EPU_ACK)
150                 wake_up(&cx->mb_hpu_waitq);
151 }
152
153 irqreturn_t cx18_irq_handler(int irq, void *dev_id)
154 {
155         struct cx18 *cx = (struct cx18 *)dev_id;
156         u32 sw1, sw1_mask;
157         u32 sw2, sw2_mask;
158         u32 hw2, hw2_mask;
159
160         sw1_mask = cx18_read_reg(cx, SW1_INT_ENABLE_PCI);
161         sw1 = cx18_read_reg(cx, SW1_INT_STATUS) & sw1_mask;
162         sw2_mask = cx18_read_reg(cx, SW2_INT_ENABLE_PCI);
163         sw2 = cx18_read_reg(cx, SW2_INT_STATUS) & sw2_mask;
164         hw2_mask = cx18_read_reg(cx, HW2_INT_MASK5_PCI);
165         hw2 = cx18_read_reg(cx, HW2_INT_CLR_STATUS) & hw2_mask;
166
167         if (sw1)
168                 cx18_write_reg_expect(cx, sw1, SW1_INT_STATUS, ~sw1, sw1);
169         if (sw2)
170                 cx18_write_reg_expect(cx, sw2, SW2_INT_STATUS, ~sw2, sw2);
171         if (hw2)
172                 cx18_write_reg_expect(cx, hw2, HW2_INT_CLR_STATUS, ~hw2, hw2);
173
174         if (sw1 || sw2 || hw2)
175                 CX18_DEBUG_HI_IRQ("SW1: %x  SW2: %x  HW2: %x\n", sw1, sw2, hw2);
176
177         /* To do: interrupt-based I2C handling
178         if (hw2 & (HW2_I2C1_INT|HW2_I2C2_INT)) {
179         }
180         */
181
182         if (sw2)
183                 xpu_ack(cx, sw2);
184
185         if (sw1)
186                 epu_cmd(cx, sw1);
187
188         return (sw1 || sw2 || hw2) ? IRQ_HANDLED : IRQ_NONE;
189 }