Merge branch 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jack/linux...
[linux-2.6] / drivers / isdn / hisax / teles3.c
1 /* $Id: teles3.c,v 2.19.2.4 2004/01/13 23:48:39 keil Exp $
2  *
3  * low level stuff for Teles 16.3 & PNP 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  * Thanks to    Jan den Ouden
12  *              Fritz Elfert
13  *              Beat Doebeli
14  *
15  */
16 #include <linux/init.h>
17 #include <linux/isapnp.h>
18 #include "hisax.h"
19 #include "isac.h"
20 #include "hscx.h"
21 #include "isdnl1.h"
22
23 extern const char *CardType[];
24 static const char *teles3_revision = "$Revision: 2.19.2.4 $";
25
26 #define byteout(addr,val) outb(val,addr)
27 #define bytein(addr) inb(addr)
28
29 static inline u_char
30 readreg(unsigned int adr, u_char off)
31 {
32         return (bytein(adr + off));
33 }
34
35 static inline void
36 writereg(unsigned int adr, u_char off, u_char data)
37 {
38         byteout(adr + off, data);
39 }
40
41
42 static inline void
43 read_fifo(unsigned int adr, u_char * data, int size)
44 {
45         insb(adr, data, size);
46 }
47
48 static void
49 write_fifo(unsigned int adr, u_char * data, int size)
50 {
51         outsb(adr, data, size);
52 }
53
54 /* Interface functions */
55
56 static u_char
57 ReadISAC(struct IsdnCardState *cs, u_char offset)
58 {
59         return (readreg(cs->hw.teles3.isac, offset));
60 }
61
62 static void
63 WriteISAC(struct IsdnCardState *cs, u_char offset, u_char value)
64 {
65         writereg(cs->hw.teles3.isac, offset, value);
66 }
67
68 static void
69 ReadISACfifo(struct IsdnCardState *cs, u_char * data, int size)
70 {
71         read_fifo(cs->hw.teles3.isacfifo, data, size);
72 }
73
74 static void
75 WriteISACfifo(struct IsdnCardState *cs, u_char * data, int size)
76 {
77         write_fifo(cs->hw.teles3.isacfifo, data, size);
78 }
79
80 static u_char
81 ReadHSCX(struct IsdnCardState *cs, int hscx, u_char offset)
82 {
83         return (readreg(cs->hw.teles3.hscx[hscx], offset));
84 }
85
86 static void
87 WriteHSCX(struct IsdnCardState *cs, int hscx, u_char offset, u_char value)
88 {
89         writereg(cs->hw.teles3.hscx[hscx], offset, value);
90 }
91
92 /*
93  * fast interrupt HSCX stuff goes here
94  */
95
96 #define READHSCX(cs, nr, reg) readreg(cs->hw.teles3.hscx[nr], reg)
97 #define WRITEHSCX(cs, nr, reg, data) writereg(cs->hw.teles3.hscx[nr], reg, data)
98 #define READHSCXFIFO(cs, nr, ptr, cnt) read_fifo(cs->hw.teles3.hscxfifo[nr], ptr, cnt)
99 #define WRITEHSCXFIFO(cs, nr, ptr, cnt) write_fifo(cs->hw.teles3.hscxfifo[nr], ptr, cnt)
100
101 #include "hscx_irq.c"
102
103 static irqreturn_t
104 teles3_interrupt(int intno, void *dev_id)
105 {
106 #define MAXCOUNT 5
107         struct IsdnCardState *cs = dev_id;
108         u_char val;
109         u_long flags;
110         int count = 0;
111
112         spin_lock_irqsave(&cs->lock, flags);
113         val = readreg(cs->hw.teles3.hscx[1], HSCX_ISTA);
114       Start_HSCX:
115         if (val)
116                 hscx_int_main(cs, val);
117         val = readreg(cs->hw.teles3.isac, ISAC_ISTA);
118       Start_ISAC:
119         if (val)
120                 isac_interrupt(cs, val);
121         count++;
122         val = readreg(cs->hw.teles3.hscx[1], HSCX_ISTA);
123         if (val && count < MAXCOUNT) {
124                 if (cs->debug & L1_DEB_HSCX)
125                         debugl1(cs, "HSCX IntStat after IntRoutine");
126                 goto Start_HSCX;
127         }
128         val = readreg(cs->hw.teles3.isac, ISAC_ISTA);
129         if (val && count < MAXCOUNT) {
130                 if (cs->debug & L1_DEB_ISAC)
131                         debugl1(cs, "ISAC IntStat after IntRoutine");
132                 goto Start_ISAC;
133         }
134         if (count >= MAXCOUNT)
135                 printk(KERN_WARNING "Teles3: more than %d loops in teles3_interrupt\n", count);
136         writereg(cs->hw.teles3.hscx[0], HSCX_MASK, 0xFF);
137         writereg(cs->hw.teles3.hscx[1], HSCX_MASK, 0xFF);
138         writereg(cs->hw.teles3.isac, ISAC_MASK, 0xFF);
139         writereg(cs->hw.teles3.isac, ISAC_MASK, 0x0);
140         writereg(cs->hw.teles3.hscx[0], HSCX_MASK, 0x0);
141         writereg(cs->hw.teles3.hscx[1], HSCX_MASK, 0x0);
142         spin_unlock_irqrestore(&cs->lock, flags);
143         return IRQ_HANDLED;
144 }
145
146 static inline void
147 release_ioregs(struct IsdnCardState *cs, int mask)
148 {
149         if (mask & 1)
150                 release_region(cs->hw.teles3.isac + 32, 32);
151         if (mask & 2)
152                 release_region(cs->hw.teles3.hscx[0] + 32, 32);
153         if (mask & 4)
154                 release_region(cs->hw.teles3.hscx[1] + 32, 32);
155 }
156
157 static void
158 release_io_teles3(struct IsdnCardState *cs)
159 {
160         if (cs->typ == ISDN_CTYPE_TELESPCMCIA) {
161                 release_region(cs->hw.teles3.hscx[1], 96);
162         } else {
163                 if (cs->hw.teles3.cfg_reg) {
164                         if (cs->typ == ISDN_CTYPE_COMPAQ_ISA) {
165                                 release_region(cs->hw.teles3.cfg_reg, 1);
166                         } else {
167                                 release_region(cs->hw.teles3.cfg_reg, 8);
168                         }
169                 }
170                 release_ioregs(cs, 0x7);
171         }
172 }
173
174 static int
175 reset_teles3(struct IsdnCardState *cs)
176 {
177         u_char irqcfg;
178
179         if (cs->typ != ISDN_CTYPE_TELESPCMCIA) {
180                 if ((cs->hw.teles3.cfg_reg) && (cs->typ != ISDN_CTYPE_COMPAQ_ISA)) {
181                         switch (cs->irq) {
182                                 case 2:
183                                 case 9:
184                                         irqcfg = 0x00;
185                                         break;
186                                 case 3:
187                                         irqcfg = 0x02;
188                                         break;
189                                 case 4:
190                                         irqcfg = 0x04;
191                                         break;
192                                 case 5:
193                                         irqcfg = 0x06;
194                                         break;
195                                 case 10:
196                                         irqcfg = 0x08;
197                                         break;
198                                 case 11:
199                                         irqcfg = 0x0A;
200                                         break;
201                                 case 12:
202                                         irqcfg = 0x0C;
203                                         break;
204                                 case 15:
205                                         irqcfg = 0x0E;
206                                         break;
207                                 default:
208                                         return(1);
209                         }
210                         byteout(cs->hw.teles3.cfg_reg + 4, irqcfg);
211                         HZDELAY(HZ / 10 + 1);
212                         byteout(cs->hw.teles3.cfg_reg + 4, irqcfg | 1);
213                         HZDELAY(HZ / 10 + 1);
214                 } else if (cs->typ == ISDN_CTYPE_COMPAQ_ISA) {
215                         byteout(cs->hw.teles3.cfg_reg, 0xff);
216                         HZDELAY(2);
217                         byteout(cs->hw.teles3.cfg_reg, 0x00);
218                         HZDELAY(2);
219                 } else {
220                         /* Reset off for 16.3 PnP , thanks to Georg Acher */
221                         byteout(cs->hw.teles3.isac + 0x3c, 0);
222                         HZDELAY(2);
223                         byteout(cs->hw.teles3.isac + 0x3c, 1);
224                         HZDELAY(2);
225                 }
226         }
227         return(0);
228 }
229
230 static int
231 Teles_card_msg(struct IsdnCardState *cs, int mt, void *arg)
232 {
233         u_long flags;
234
235         switch (mt) {
236                 case CARD_RESET:
237                         spin_lock_irqsave(&cs->lock, flags);
238                         reset_teles3(cs);
239                         spin_unlock_irqrestore(&cs->lock, flags);
240                         return(0);
241                 case CARD_RELEASE:
242                         release_io_teles3(cs);
243                         return(0);
244                 case CARD_INIT:
245                         spin_lock_irqsave(&cs->lock, flags);
246                         inithscxisac(cs, 3);
247                         spin_unlock_irqrestore(&cs->lock, flags);
248                         return(0);
249                 case CARD_TEST:
250                         return(0);
251         }
252         return(0);
253 }
254
255 #ifdef __ISAPNP__
256
257 static struct isapnp_device_id teles_ids[] __devinitdata = {
258         { ISAPNP_VENDOR('T', 'A', 'G'), ISAPNP_FUNCTION(0x2110),
259           ISAPNP_VENDOR('T', 'A', 'G'), ISAPNP_FUNCTION(0x2110), 
260           (unsigned long) "Teles 16.3 PnP" },
261         { ISAPNP_VENDOR('C', 'T', 'X'), ISAPNP_FUNCTION(0x0),
262           ISAPNP_VENDOR('C', 'T', 'X'), ISAPNP_FUNCTION(0x0), 
263           (unsigned long) "Creatix 16.3 PnP" },
264         { ISAPNP_VENDOR('C', 'P', 'Q'), ISAPNP_FUNCTION(0x1002),
265           ISAPNP_VENDOR('C', 'P', 'Q'), ISAPNP_FUNCTION(0x1002), 
266           (unsigned long) "Compaq ISDN S0" },
267         { 0, }
268 };
269
270 static struct isapnp_device_id *ipid __devinitdata = &teles_ids[0];
271 static struct pnp_card *pnp_c __devinitdata = NULL;
272 #endif
273
274 int __devinit
275 setup_teles3(struct IsdnCard *card)
276 {
277         u_char val;
278         struct IsdnCardState *cs = card->cs;
279         char tmp[64];
280
281         strcpy(tmp, teles3_revision);
282         printk(KERN_INFO "HiSax: Teles IO driver Rev. %s\n", HiSax_getrev(tmp));
283         if ((cs->typ != ISDN_CTYPE_16_3) && (cs->typ != ISDN_CTYPE_PNP)
284             && (cs->typ != ISDN_CTYPE_TELESPCMCIA) && (cs->typ != ISDN_CTYPE_COMPAQ_ISA))
285                 return (0);
286
287 #ifdef __ISAPNP__
288         if (!card->para[1] && isapnp_present()) {
289                 struct pnp_dev *pnp_d;
290                 while(ipid->card_vendor) {
291                         if ((pnp_c = pnp_find_card(ipid->card_vendor,
292                                 ipid->card_device, pnp_c))) {
293                                 pnp_d = NULL;
294                                 if ((pnp_d = pnp_find_dev(pnp_c,
295                                         ipid->vendor, ipid->function, pnp_d))) {
296                                         int err;
297
298                                         printk(KERN_INFO "HiSax: %s detected\n",
299                                                 (char *)ipid->driver_data);
300                                         pnp_disable_dev(pnp_d);
301                                         err = pnp_activate_dev(pnp_d);
302                                         if (err<0) {
303                                                 printk(KERN_WARNING "%s: pnp_activate_dev ret(%d)\n",
304                                                         __func__, err);
305                                                 return(0);
306                                         }
307                                         card->para[3] = pnp_port_start(pnp_d, 2);
308                                         card->para[2] = pnp_port_start(pnp_d, 1);
309                                         card->para[1] = pnp_port_start(pnp_d, 0);
310                                         card->para[0] = pnp_irq(pnp_d, 0);
311                                         if (!card->para[0] || !card->para[1] || !card->para[2]) {
312                                                 printk(KERN_ERR "Teles PnP:some resources are missing %ld/%lx/%lx\n",
313                                                         card->para[0], card->para[1], card->para[2]);
314                                                 pnp_disable_dev(pnp_d);
315                                                 return(0);
316                                         }
317                                         break;
318                                 } else {
319                                         printk(KERN_ERR "Teles PnP: PnP error card found, no device\n");
320                                 }
321                         }
322                         ipid++;
323                         pnp_c = NULL;
324                 } 
325                 if (!ipid->card_vendor) {
326                         printk(KERN_INFO "Teles PnP: no ISAPnP card found\n");
327                         return(0);
328                 }
329         }
330 #endif
331         if (cs->typ == ISDN_CTYPE_16_3) {
332                 cs->hw.teles3.cfg_reg = card->para[1];
333                 switch (cs->hw.teles3.cfg_reg) {
334                         case 0x180:
335                         case 0x280:
336                         case 0x380:
337                                 cs->hw.teles3.cfg_reg |= 0xc00;
338                                 break;
339                 }
340                 cs->hw.teles3.isac = cs->hw.teles3.cfg_reg - 0x420;
341                 cs->hw.teles3.hscx[0] = cs->hw.teles3.cfg_reg - 0xc20;
342                 cs->hw.teles3.hscx[1] = cs->hw.teles3.cfg_reg - 0x820;
343         } else if (cs->typ == ISDN_CTYPE_TELESPCMCIA) {
344                 cs->hw.teles3.cfg_reg = 0;
345                 cs->hw.teles3.hscx[0] = card->para[1] - 0x20;
346                 cs->hw.teles3.hscx[1] = card->para[1];
347                 cs->hw.teles3.isac = card->para[1] + 0x20;
348         } else if (cs->typ == ISDN_CTYPE_COMPAQ_ISA) {
349                 cs->hw.teles3.cfg_reg = card->para[3];
350                 cs->hw.teles3.isac = card->para[2] - 32;
351                 cs->hw.teles3.hscx[0] = card->para[1] - 32;
352                 cs->hw.teles3.hscx[1] = card->para[1];
353         } else {        /* PNP */
354                 cs->hw.teles3.cfg_reg = 0;
355                 cs->hw.teles3.isac = card->para[1] - 32;
356                 cs->hw.teles3.hscx[0] = card->para[2] - 32;
357                 cs->hw.teles3.hscx[1] = card->para[2];
358         }
359         cs->irq = card->para[0];
360         cs->hw.teles3.isacfifo = cs->hw.teles3.isac + 0x3e;
361         cs->hw.teles3.hscxfifo[0] = cs->hw.teles3.hscx[0] + 0x3e;
362         cs->hw.teles3.hscxfifo[1] = cs->hw.teles3.hscx[1] + 0x3e;
363         if (cs->typ == ISDN_CTYPE_TELESPCMCIA) {
364                 if (!request_region(cs->hw.teles3.hscx[1], 96, "HiSax Teles PCMCIA")) {
365                         printk(KERN_WARNING
366                                "HiSax: %s ports %x-%x already in use\n",
367                                CardType[cs->typ],
368                                cs->hw.teles3.hscx[1],
369                                cs->hw.teles3.hscx[1] + 96);
370                         return (0);
371                 }
372                 cs->irq_flags |= IRQF_SHARED; /* cardbus can share */
373         } else {
374                 if (cs->hw.teles3.cfg_reg) {
375                         if (cs->typ == ISDN_CTYPE_COMPAQ_ISA) {
376                                 if (!request_region(cs->hw.teles3.cfg_reg, 1, "teles3 cfg")) {
377                                         printk(KERN_WARNING
378                                                 "HiSax: %s config port %x already in use\n",
379                                                 CardType[card->typ],
380                                                 cs->hw.teles3.cfg_reg);
381                                         return (0);
382                                 }
383                         } else {
384                                 if (!request_region(cs->hw.teles3.cfg_reg, 8, "teles3 cfg")) {
385                                         printk(KERN_WARNING
386                                                "HiSax: %s config port %x-%x already in use\n",
387                                                CardType[card->typ],
388                                                cs->hw.teles3.cfg_reg,
389                                                 cs->hw.teles3.cfg_reg + 8);
390                                         return (0);
391                                 }
392                         }
393                 }
394                 if (!request_region(cs->hw.teles3.isac + 32, 32, "HiSax isac")) {
395                         printk(KERN_WARNING
396                            "HiSax: %s isac ports %x-%x already in use\n",
397                                CardType[cs->typ],
398                                cs->hw.teles3.isac + 32,
399                                cs->hw.teles3.isac + 64);
400                         if (cs->hw.teles3.cfg_reg) {
401                                 if (cs->typ == ISDN_CTYPE_COMPAQ_ISA) {
402                                         release_region(cs->hw.teles3.cfg_reg, 1);
403                                 } else {
404                                         release_region(cs->hw.teles3.cfg_reg, 8);
405                                 }
406                         }
407                         return (0);
408                 }
409                 if (!request_region(cs->hw.teles3.hscx[0] + 32, 32, "HiSax hscx A")) {
410                         printk(KERN_WARNING
411                          "HiSax: %s hscx A ports %x-%x already in use\n",
412                                CardType[cs->typ],
413                                cs->hw.teles3.hscx[0] + 32,
414                                cs->hw.teles3.hscx[0] + 64);
415                         if (cs->hw.teles3.cfg_reg) {
416                                 if (cs->typ == ISDN_CTYPE_COMPAQ_ISA) {
417                                         release_region(cs->hw.teles3.cfg_reg, 1);
418                                 } else {
419                                         release_region(cs->hw.teles3.cfg_reg, 8);
420                                 }
421                         }
422                         release_ioregs(cs, 1);
423                         return (0);
424                 }
425                 if (!request_region(cs->hw.teles3.hscx[1] + 32, 32, "HiSax hscx B")) {
426                         printk(KERN_WARNING
427                          "HiSax: %s hscx B ports %x-%x already in use\n",
428                                CardType[cs->typ],
429                                cs->hw.teles3.hscx[1] + 32,
430                                cs->hw.teles3.hscx[1] + 64);
431                         if (cs->hw.teles3.cfg_reg) {
432                                 if (cs->typ == ISDN_CTYPE_COMPAQ_ISA) {
433                                         release_region(cs->hw.teles3.cfg_reg, 1);
434                                 } else {
435                                         release_region(cs->hw.teles3.cfg_reg, 8);
436                                 }
437                         }
438                         release_ioregs(cs, 3);
439                         return (0);
440                 }
441         }
442         if ((cs->hw.teles3.cfg_reg) && (cs->typ != ISDN_CTYPE_COMPAQ_ISA)) {
443                 if ((val = bytein(cs->hw.teles3.cfg_reg + 0)) != 0x51) {
444                         printk(KERN_WARNING "Teles: 16.3 Byte at %x is %x\n",
445                                cs->hw.teles3.cfg_reg + 0, val);
446                         release_io_teles3(cs);
447                         return (0);
448                 }
449                 if ((val = bytein(cs->hw.teles3.cfg_reg + 1)) != 0x93) {
450                         printk(KERN_WARNING "Teles: 16.3 Byte at %x is %x\n",
451                                cs->hw.teles3.cfg_reg + 1, val);
452                         release_io_teles3(cs);
453                         return (0);
454                 }
455                 val = bytein(cs->hw.teles3.cfg_reg + 2);/* 0x1e=without AB
456                                                          * 0x1f=with AB
457                                                          * 0x1c 16.3 ???
458                                                          * 0x39 16.3 1.1
459                                                          * 0x38 16.3 1.3
460                                                          * 0x46 16.3 with AB + Video (Teles-Vision)
461                                                          */
462                 if (val != 0x46 && val != 0x39 && val != 0x38 && val != 0x1c && val != 0x1e && val != 0x1f) {
463                         printk(KERN_WARNING "Teles: 16.3 Byte at %x is %x\n",
464                                cs->hw.teles3.cfg_reg + 2, val);
465                         release_io_teles3(cs);
466                         return (0);
467                 }
468         }
469         printk(KERN_INFO
470                "HiSax: %s config irq:%d isac:0x%X  cfg:0x%X\n",
471                CardType[cs->typ], cs->irq,
472                cs->hw.teles3.isac + 32, cs->hw.teles3.cfg_reg);
473         printk(KERN_INFO
474                "HiSax: hscx A:0x%X  hscx B:0x%X\n",
475                cs->hw.teles3.hscx[0] + 32, cs->hw.teles3.hscx[1] + 32);
476
477         setup_isac(cs);
478         if (reset_teles3(cs)) {
479                 printk(KERN_WARNING "Teles3: wrong IRQ\n");
480                 release_io_teles3(cs);
481                 return (0);
482         }
483         cs->readisac = &ReadISAC;
484         cs->writeisac = &WriteISAC;
485         cs->readisacfifo = &ReadISACfifo;
486         cs->writeisacfifo = &WriteISACfifo;
487         cs->BC_Read_Reg = &ReadHSCX;
488         cs->BC_Write_Reg = &WriteHSCX;
489         cs->BC_Send_Data = &hscx_fill_fifo;
490         cs->cardmsg = &Teles_card_msg;
491         cs->irq_func = &teles3_interrupt;
492         ISACVersion(cs, "Teles3:");
493         if (HscxVersion(cs, "Teles3:")) {
494                 printk(KERN_WARNING
495                        "Teles3: wrong HSCX versions check IO address\n");
496                 release_io_teles3(cs);
497                 return (0);
498         }
499         return (1);
500 }