bb05427fdeb6d1b738bd474c7d47dc85c870b8af
[safe/jmp/linux-2.6] / drivers / media / dvb / mantis / mantis_hif.c
1 /*
2         Mantis PCI bridge driver
3
4         Copyright (C) 2005, 2006 Manu Abraham (abraham.manu@gmail.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         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., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include <linux/kernel.h>
22 #include <linux/signal.h>
23 #include <linux/sched.h>
24
25 #include <asm/irq.h>
26 #include <linux/signal.h>
27 #include <linux/sched.h>
28 #include <linux/interrupt.h>
29
30 #include "dmxdev.h"
31 #include "dvbdev.h"
32 #include "dvb_demux.h"
33 #include "dvb_frontend.h"
34 #include "dvb_net.h"
35
36 #include "mantis_common.h"
37
38 #include "mantis_hif.h"
39 #include "mantis_link.h" /* temporary due to physical layer stuff */
40
41 #include "mantis_reg.h"
42
43 static int mantis_hif_data_available(struct mantis_ca *ca)
44 {
45         struct mantis_pci *mantis = ca->ca_priv;
46         int rc = 0;
47
48         if (wait_event_interruptible_timeout(ca->hif_data_wq,
49                                              ca->sbuf_status & MANTIS_SBUF_DATA_AVAIL,
50                                              msecs_to_jiffies(500)) == -ERESTARTSYS) {
51
52                 dprintk(MANTIS_ERROR, 1, "Adapter(%d) Slot(0): HIF Read wait event timeout !", mantis->num);
53                 rc = -EREMOTEIO;
54         }
55         ca->sbuf_status &= ~MANTIS_SBUF_DATA_AVAIL;
56         udelay(2);
57         return rc;
58 }
59
60 static int mantis_hif_sbuf_opdone_wait(struct mantis_ca *ca)
61 {
62         struct mantis_pci *mantis = ca->ca_priv;
63         int rc = 0;
64
65         if (wait_event_timeout(ca->hif_opdone_wq,
66                                ca->hif_event & MANTIS_SBUF_OPDONE,
67                                msecs_to_jiffies(500)) == -ERESTARTSYS) {
68
69                 dprintk(MANTIS_ERROR, 1, "Adapter(%d) Slot(0): Smart buffer operation timeout !", mantis->num);
70                 rc = -EREMOTEIO;
71         }
72         dprintk(MANTIS_DEBUG, 1, "Smart Buffer Operation complete");
73         ca->hif_event &= ~MANTIS_SBUF_OPDONE;
74         return rc;
75 }
76
77 static int mantis_hif_write_wait(struct mantis_ca *ca)
78 {
79         struct mantis_pci *mantis = ca->ca_priv;
80         u32 opdone = 0, timeout = 0;
81         int rc = 0;
82
83         if (wait_event_timeout(ca->hif_write_wq,
84                                mantis->gpif_status & MANTIS_GPIF_WRACK,
85                                msecs_to_jiffies(500)) == -ERESTARTSYS) {
86
87                 dprintk(MANTIS_ERROR, 1, "Adapter(%d) Slot(0): Write ACK timed out !", mantis->num);
88                 rc = -EREMOTEIO;
89         }
90         dprintk(MANTIS_DEBUG, 1, "Write Acknowledged");
91         mantis->gpif_status &= ~MANTIS_GPIF_WRACK;
92         while (!opdone) {
93                 opdone = (mmread(MANTIS_GPIF_STATUS) & MANTIS_SBUF_OPDONE);
94                 udelay(500);
95                 timeout++;
96                 if (timeout > 100) {
97                         dprintk(MANTIS_ERROR, 1, "Adater(%d) Slot(0): Write operation timed out!", mantis->num);
98                         rc = -ETIMEDOUT;
99                         break;
100                 }
101         }
102         dprintk(MANTIS_DEBUG, 1, "HIF Write success");
103         return rc;
104 }
105
106
107 int mantis_hif_read_mem(struct mantis_ca *ca, u32 addr)
108 {
109         struct mantis_pci *mantis = ca->ca_priv;
110         u32 hif_addr = 0, data, count = 4;
111
112         dprintk(MANTIS_DEBUG, 1, "Adapter(%d) Slot(0): Request HIF Mem Read", mantis->num);
113         mutex_lock(&ca->ca_lock);
114         hif_addr &= ~MANTIS_GPIF_PCMCIAREG;
115         hif_addr &= ~MANTIS_GPIF_PCMCIAIOM;
116         hif_addr |=  MANTIS_HIF_STATUS;
117         hif_addr |=  addr;
118
119         mmwrite(hif_addr, MANTIS_GPIF_BRADDR);
120         mmwrite(count, MANTIS_GPIF_BRBYTES);
121         udelay(20);
122         mmwrite(hif_addr | MANTIS_GPIF_HIFRDWRN, MANTIS_GPIF_ADDR);
123
124         if (mantis_hif_sbuf_opdone_wait(ca) != 0) {
125                 dprintk(MANTIS_ERROR, 1, "Adapter(%d) Slot(0): GPIF Smart Buffer operation failed", mantis->num);
126                 mutex_unlock(&ca->ca_lock);
127                 return -EREMOTEIO;
128         }
129         data = mmread(MANTIS_GPIF_DIN);
130         mutex_unlock(&ca->ca_lock);
131         dprintk(MANTIS_DEBUG, 1, "Mem Read: 0x%02x", data);
132         return (data >> 24) & 0xff;
133 }
134
135 int mantis_hif_write_mem(struct mantis_ca *ca, u32 addr, u8 data)
136 {
137         struct mantis_slot *slot = ca->slot;
138         struct mantis_pci *mantis = ca->ca_priv;
139         u32 hif_addr = 0;
140
141         dprintk(MANTIS_DEBUG, 1, "Adapter(%d) Slot(0): Request HIF Mem Write", mantis->num);
142         mutex_lock(&ca->ca_lock);
143         hif_addr &= ~MANTIS_GPIF_HIFRDWRN;
144         hif_addr &= ~MANTIS_GPIF_PCMCIAREG;
145         hif_addr &= ~MANTIS_GPIF_PCMCIAIOM;
146         hif_addr |=  MANTIS_HIF_STATUS;
147         hif_addr |=  addr;
148
149         mmwrite(slot->slave_cfg, MANTIS_GPIF_CFGSLA); /* Slot0 alone for now */
150         mmwrite(hif_addr, MANTIS_GPIF_ADDR);
151         mmwrite(data, MANTIS_GPIF_DOUT);
152
153         if (mantis_hif_write_wait(ca) != 0) {
154                 dprintk(MANTIS_ERROR, 1, "Adapter(%d) Slot(0): HIF Smart Buffer operation failed", mantis->num);
155                 mutex_unlock(&ca->ca_lock);
156                 return -EREMOTEIO;
157         }
158         dprintk(MANTIS_DEBUG, 1, "Mem Write: (0x%02x to 0x%02x)", data, addr);
159         mutex_unlock(&ca->ca_lock);
160
161         return 0;
162 }
163
164 int mantis_hif_read_iom(struct mantis_ca *ca, u32 addr)
165 {
166         struct mantis_pci *mantis = ca->ca_priv;
167         u32 data, hif_addr = 0;
168
169         dprintk(MANTIS_DEBUG, 1, "Adapter(%d) Slot(0): Request HIF I/O Read", mantis->num);
170         mutex_lock(&ca->ca_lock);
171         hif_addr &= ~MANTIS_GPIF_PCMCIAREG;
172         hif_addr |=  MANTIS_GPIF_PCMCIAIOM;
173         hif_addr |=  MANTIS_HIF_STATUS;
174         hif_addr |=  addr;
175
176         mmwrite(hif_addr, MANTIS_GPIF_BRADDR);
177         mmwrite(1, MANTIS_GPIF_BRBYTES);
178         udelay(20);
179         mmwrite(hif_addr | MANTIS_GPIF_HIFRDWRN, MANTIS_GPIF_ADDR);
180
181         if (mantis_hif_sbuf_opdone_wait(ca) != 0) {
182                 dprintk(MANTIS_ERROR, 1, "Adapter(%d) Slot(0): HIF Smart Buffer operation failed", mantis->num);
183                 mutex_unlock(&ca->ca_lock);
184                 return -EREMOTEIO;
185         }
186         data = mmread(MANTIS_GPIF_DIN);
187         dprintk(MANTIS_DEBUG, 1, "I/O Read: 0x%02x", data);
188         udelay(50);
189         mutex_unlock(&ca->ca_lock);
190
191         return (u8) data;
192 }
193
194 int mantis_hif_write_iom(struct mantis_ca *ca, u32 addr, u8 data)
195 {
196         struct mantis_pci *mantis = ca->ca_priv;
197         u32 hif_addr = 0;
198
199         dprintk(MANTIS_DEBUG, 1, "Adapter(%d) Slot(0): Request HIF I/O Write", mantis->num);
200         mutex_lock(&ca->ca_lock);
201         hif_addr &= ~MANTIS_GPIF_PCMCIAREG;
202         hif_addr &= ~MANTIS_GPIF_HIFRDWRN;
203         hif_addr |=  MANTIS_GPIF_PCMCIAIOM;
204         hif_addr |=  MANTIS_HIF_STATUS;
205         hif_addr |=  addr;
206
207         mmwrite(hif_addr, MANTIS_GPIF_ADDR);
208         mmwrite(data, MANTIS_GPIF_DOUT);
209
210         if (mantis_hif_write_wait(ca) != 0) {
211                 dprintk(MANTIS_ERROR, 1, "Adapter(%d) Slot(0): HIF Smart Buffer operation failed", mantis->num);
212                 mutex_unlock(&ca->ca_lock);
213                 return -EREMOTEIO;
214         }
215         dprintk(MANTIS_DEBUG, 1, "I/O Write: (0x%02x to 0x%02x)", data, addr);
216         mutex_unlock(&ca->ca_lock);
217         udelay(50);
218
219         return 0;
220 }
221
222 int mantis_hif_init(struct mantis_ca *ca)
223 {
224         struct mantis_slot *slot = ca->slot;
225         struct mantis_pci *mantis = ca->ca_priv;
226         u32 irqcfg;
227
228         slot[0].slave_cfg = 0x70773028;
229         dprintk(MANTIS_ERROR, 1, "Adapter(%d) Initializing Mantis Host Interface", mantis->num);
230
231         mutex_lock(&ca->ca_lock);
232         irqcfg = mmread(MANTIS_GPIF_IRQCFG);
233         irqcfg = MANTIS_MASK_BRRDY      |
234                  MANTIS_MASK_WRACK      |
235                  MANTIS_MASK_EXTIRQ     |
236                  MANTIS_MASK_WSTO       |
237                  MANTIS_MASK_OTHERR     |
238                  MANTIS_MASK_OVFLW;
239
240         mmwrite(irqcfg, MANTIS_GPIF_IRQCFG);
241         mutex_unlock(&ca->ca_lock);
242
243         return 0;
244 }
245
246 void mantis_hif_exit(struct mantis_ca *ca)
247 {
248         struct mantis_pci *mantis = ca->ca_priv;
249         u32 irqcfg;
250
251         dprintk(MANTIS_ERROR, 1, "Adapter(%d) Exiting Mantis Host Interface", mantis->num);
252         mutex_lock(&ca->ca_lock);
253         irqcfg = mmread(MANTIS_GPIF_IRQCFG);
254         irqcfg &= ~MANTIS_MASK_BRRDY;
255         mmwrite(irqcfg, MANTIS_GPIF_IRQCFG);
256         mutex_unlock(&ca->ca_lock);
257 }