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