1 /* $Id: teleint.c,v 1.1.4.1 2001/11/20 14:19:36 kai Exp $
2 *
3 * low level stuff for TeleInt isdn cards
4 *
5 * Author Karsten Keil
6 * Copyright by Karsten Keil <keil@isdn4linux.de>
7 *
8 * This software may be used and distributed according to the terms
9 * of the GNU General Public License, incorporated herein by reference.
10 *
11 */
12
13 #define __NO_VERSION__
14 #include <linux/init.h>
15 #include "hisax.h"
16 #include "isac.h"
17 #include "hfc_2bs0.h"
18 #include "isdnl1.h"
19
20 extern const char *CardType[];
21
22 const char *TeleInt_revision = "$Revision: 1.1.4.1 $";
23
24 #define byteout(addr,val) outb(val,addr)
25 #define bytein(addr) inb(addr)
26
27 static inline u_char
readreg(unsigned int ale,unsigned int adr,u_char off)28 readreg(unsigned int ale, unsigned int adr, u_char off)
29 {
30 register u_char ret;
31 int max_delay = 2000;
32 long flags;
33
34 save_flags(flags);
35 cli();
36 byteout(ale, off);
37 ret = HFC_BUSY & bytein(ale);
38 while (ret && --max_delay)
39 ret = HFC_BUSY & bytein(ale);
40 if (!max_delay) {
41 printk(KERN_WARNING "TeleInt Busy not inactive\n");
42 restore_flags(flags);
43 return (0);
44 }
45 ret = bytein(adr);
46 restore_flags(flags);
47 return (ret);
48 }
49
50 static inline void
readfifo(unsigned int ale,unsigned int adr,u_char off,u_char * data,int size)51 readfifo(unsigned int ale, unsigned int adr, u_char off, u_char * data, int size)
52 {
53 register u_char ret;
54 register int max_delay = 20000;
55 register int i;
56
57 byteout(ale, off);
58 for (i = 0; i<size; i++) {
59 ret = HFC_BUSY & bytein(ale);
60 while (ret && --max_delay)
61 ret = HFC_BUSY & bytein(ale);
62 if (!max_delay) {
63 printk(KERN_WARNING "TeleInt Busy not inactive\n");
64 return;
65 }
66 data[i] = bytein(adr);
67 }
68 }
69
70
71 static inline void
writereg(unsigned int ale,unsigned int adr,u_char off,u_char data)72 writereg(unsigned int ale, unsigned int adr, u_char off, u_char data)
73 {
74 register u_char ret;
75 int max_delay = 2000;
76 long flags;
77
78 save_flags(flags);
79 cli();
80 byteout(ale, off);
81 ret = HFC_BUSY & bytein(ale);
82 while (ret && --max_delay)
83 ret = HFC_BUSY & bytein(ale);
84 if (!max_delay) {
85 printk(KERN_WARNING "TeleInt Busy not inactive\n");
86 restore_flags(flags);
87 return;
88 }
89 byteout(adr, data);
90 restore_flags(flags);
91 }
92
93 static inline void
writefifo(unsigned int ale,unsigned int adr,u_char off,u_char * data,int size)94 writefifo(unsigned int ale, unsigned int adr, u_char off, u_char * data, int size)
95 {
96 register u_char ret;
97 register int max_delay = 20000;
98 register int i;
99
100 /* fifo write without cli because it's allready done */
101 byteout(ale, off);
102 for (i = 0; i<size; i++) {
103 ret = HFC_BUSY & bytein(ale);
104 while (ret && --max_delay)
105 ret = HFC_BUSY & bytein(ale);
106 if (!max_delay) {
107 printk(KERN_WARNING "TeleInt Busy not inactive\n");
108 return;
109 }
110 byteout(adr, data[i]);
111 }
112 }
113
114 /* Interface functions */
115
116 static u_char
ReadISAC(struct IsdnCardState * cs,u_char offset)117 ReadISAC(struct IsdnCardState *cs, u_char offset)
118 {
119 cs->hw.hfc.cip = offset;
120 return (readreg(cs->hw.hfc.addr | 1, cs->hw.hfc.addr, offset));
121 }
122
123 static void
WriteISAC(struct IsdnCardState * cs,u_char offset,u_char value)124 WriteISAC(struct IsdnCardState *cs, u_char offset, u_char value)
125 {
126 cs->hw.hfc.cip = offset;
127 writereg(cs->hw.hfc.addr | 1, cs->hw.hfc.addr, offset, value);
128 }
129
130 static void
ReadISACfifo(struct IsdnCardState * cs,u_char * data,int size)131 ReadISACfifo(struct IsdnCardState *cs, u_char * data, int size)
132 {
133 cs->hw.hfc.cip = 0;
134 readfifo(cs->hw.hfc.addr | 1, cs->hw.hfc.addr, 0, data, size);
135 }
136
137 static void
WriteISACfifo(struct IsdnCardState * cs,u_char * data,int size)138 WriteISACfifo(struct IsdnCardState *cs, u_char * data, int size)
139 {
140 cs->hw.hfc.cip = 0;
141 writefifo(cs->hw.hfc.addr | 1, cs->hw.hfc.addr, 0, data, size);
142 }
143
144 static u_char
ReadHFC(struct IsdnCardState * cs,int data,u_char reg)145 ReadHFC(struct IsdnCardState *cs, int data, u_char reg)
146 {
147 register u_char ret;
148
149 if (data) {
150 cs->hw.hfc.cip = reg;
151 byteout(cs->hw.hfc.addr | 1, reg);
152 ret = bytein(cs->hw.hfc.addr);
153 if (cs->debug & L1_DEB_HSCX_FIFO && (data != 2))
154 debugl1(cs, "hfc RD %02x %02x", reg, ret);
155 } else
156 ret = bytein(cs->hw.hfc.addr | 1);
157 return (ret);
158 }
159
160 static void
WriteHFC(struct IsdnCardState * cs,int data,u_char reg,u_char value)161 WriteHFC(struct IsdnCardState *cs, int data, u_char reg, u_char value)
162 {
163 byteout(cs->hw.hfc.addr | 1, reg);
164 cs->hw.hfc.cip = reg;
165 if (data)
166 byteout(cs->hw.hfc.addr, value);
167 if (cs->debug & L1_DEB_HSCX_FIFO && (data != 2))
168 debugl1(cs, "hfc W%c %02x %02x", data ? 'D' : 'C', reg, value);
169 }
170
171 static void
TeleInt_interrupt(int intno,void * dev_id,struct pt_regs * regs)172 TeleInt_interrupt(int intno, void *dev_id, struct pt_regs *regs)
173 {
174 struct IsdnCardState *cs = dev_id;
175 u_char val;
176
177 if (!cs) {
178 printk(KERN_WARNING "TeleInt: Spurious interrupt!\n");
179 return;
180 }
181 val = readreg(cs->hw.hfc.addr | 1, cs->hw.hfc.addr, ISAC_ISTA);
182 Start_ISAC:
183 if (val)
184 isac_interrupt(cs, val);
185 val = readreg(cs->hw.hfc.addr | 1, cs->hw.hfc.addr, ISAC_ISTA);
186 if (val) {
187 if (cs->debug & L1_DEB_ISAC)
188 debugl1(cs, "ISAC IntStat after IntRoutine");
189 goto Start_ISAC;
190 }
191 writereg(cs->hw.hfc.addr | 1, cs->hw.hfc.addr, ISAC_MASK, 0xFF);
192 writereg(cs->hw.hfc.addr | 1, cs->hw.hfc.addr, ISAC_MASK, 0x0);
193 }
194
195 static void
TeleInt_Timer(struct IsdnCardState * cs)196 TeleInt_Timer(struct IsdnCardState *cs)
197 {
198 int stat = 0;
199
200 if (cs->bcs[0].mode) {
201 stat |= 1;
202 main_irq_hfc(&cs->bcs[0]);
203 }
204 if (cs->bcs[1].mode) {
205 stat |= 2;
206 main_irq_hfc(&cs->bcs[1]);
207 }
208 cs->hw.hfc.timer.expires = jiffies + 1;
209 add_timer(&cs->hw.hfc.timer);
210 }
211
212 void
release_io_TeleInt(struct IsdnCardState * cs)213 release_io_TeleInt(struct IsdnCardState *cs)
214 {
215 del_timer(&cs->hw.hfc.timer);
216 releasehfc(cs);
217 if (cs->hw.hfc.addr)
218 release_region(cs->hw.hfc.addr, 2);
219 }
220
221 static void
reset_TeleInt(struct IsdnCardState * cs)222 reset_TeleInt(struct IsdnCardState *cs)
223 {
224 long flags;
225
226 printk(KERN_INFO "TeleInt: resetting card\n");
227 cs->hw.hfc.cirm |= HFC_RESET;
228 byteout(cs->hw.hfc.addr | 1, cs->hw.hfc.cirm); /* Reset On */
229 save_flags(flags);
230 sti();
231 set_current_state(TASK_UNINTERRUPTIBLE);
232 schedule_timeout((30*HZ)/1000);
233 cs->hw.hfc.cirm &= ~HFC_RESET;
234 byteout(cs->hw.hfc.addr | 1, cs->hw.hfc.cirm); /* Reset Off */
235 set_current_state(TASK_UNINTERRUPTIBLE);
236 schedule_timeout((10*HZ)/1000);
237 restore_flags(flags);
238 }
239
240 static int
TeleInt_card_msg(struct IsdnCardState * cs,int mt,void * arg)241 TeleInt_card_msg(struct IsdnCardState *cs, int mt, void *arg)
242 {
243 switch (mt) {
244 case CARD_RESET:
245 reset_TeleInt(cs);
246 return(0);
247 case CARD_RELEASE:
248 release_io_TeleInt(cs);
249 return(0);
250 case CARD_INIT:
251 inithfc(cs);
252 clear_pending_isac_ints(cs);
253 initisac(cs);
254 /* Reenable all IRQ */
255 cs->writeisac(cs, ISAC_MASK, 0);
256 cs->writeisac(cs, ISAC_CMDR, 0x41);
257 cs->hw.hfc.timer.expires = jiffies + 1;
258 add_timer(&cs->hw.hfc.timer);
259 return(0);
260 case CARD_TEST:
261 return(0);
262 }
263 return(0);
264 }
265
266 int __init
setup_TeleInt(struct IsdnCard * card)267 setup_TeleInt(struct IsdnCard *card)
268 {
269 struct IsdnCardState *cs = card->cs;
270 char tmp[64];
271
272 strcpy(tmp, TeleInt_revision);
273 printk(KERN_INFO "HiSax: TeleInt driver Rev. %s\n", HiSax_getrev(tmp));
274 if (cs->typ != ISDN_CTYPE_TELEINT)
275 return (0);
276
277 cs->hw.hfc.addr = card->para[1] & 0x3fe;
278 cs->irq = card->para[0];
279 cs->hw.hfc.cirm = HFC_CIRM;
280 cs->hw.hfc.isac_spcr = 0x00;
281 cs->hw.hfc.cip = 0;
282 cs->hw.hfc.ctmt = HFC_CTMT | HFC_CLTIMER;
283 cs->bcs[0].hw.hfc.send = NULL;
284 cs->bcs[1].hw.hfc.send = NULL;
285 cs->hw.hfc.fifosize = 7 * 1024 + 512;
286 cs->hw.hfc.timer.function = (void *) TeleInt_Timer;
287 cs->hw.hfc.timer.data = (long) cs;
288 init_timer(&cs->hw.hfc.timer);
289 if (check_region((cs->hw.hfc.addr), 2)) {
290 printk(KERN_WARNING
291 "HiSax: %s config port %x-%x already in use\n",
292 CardType[card->typ],
293 cs->hw.hfc.addr,
294 cs->hw.hfc.addr + 2);
295 return (0);
296 } else {
297 request_region(cs->hw.hfc.addr, 2, "TeleInt isdn");
298 }
299 /* HW IO = IO */
300 byteout(cs->hw.hfc.addr, cs->hw.hfc.addr & 0xff);
301 byteout(cs->hw.hfc.addr | 1, ((cs->hw.hfc.addr & 0x300) >> 8) | 0x54);
302 switch (cs->irq) {
303 case 3:
304 cs->hw.hfc.cirm |= HFC_INTA;
305 break;
306 case 4:
307 cs->hw.hfc.cirm |= HFC_INTB;
308 break;
309 case 5:
310 cs->hw.hfc.cirm |= HFC_INTC;
311 break;
312 case 7:
313 cs->hw.hfc.cirm |= HFC_INTD;
314 break;
315 case 10:
316 cs->hw.hfc.cirm |= HFC_INTE;
317 break;
318 case 11:
319 cs->hw.hfc.cirm |= HFC_INTF;
320 break;
321 default:
322 printk(KERN_WARNING "TeleInt: wrong IRQ\n");
323 release_io_TeleInt(cs);
324 return (0);
325 }
326 byteout(cs->hw.hfc.addr | 1, cs->hw.hfc.cirm);
327 byteout(cs->hw.hfc.addr | 1, cs->hw.hfc.ctmt);
328
329 printk(KERN_INFO
330 "TeleInt: defined at 0x%x IRQ %d\n",
331 cs->hw.hfc.addr,
332 cs->irq);
333
334 reset_TeleInt(cs);
335 cs->readisac = &ReadISAC;
336 cs->writeisac = &WriteISAC;
337 cs->readisacfifo = &ReadISACfifo;
338 cs->writeisacfifo = &WriteISACfifo;
339 cs->BC_Read_Reg = &ReadHFC;
340 cs->BC_Write_Reg = &WriteHFC;
341 cs->cardmsg = &TeleInt_card_msg;
342 cs->irq_func = &TeleInt_interrupt;
343 ISACVersion(cs, "TeleInt:");
344 return (1);
345 }
346