blob: e5918c6fe73d09bfb99a4dc91f05ca2c4b09646b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* $Id: niccy.c,v 1.21.2.4 2004/01/13 23:48:39 keil Exp $
2 *
3 * low level stuff for Dr. Neuhaus NICCY PnP and NICCY PCI and
4 * compatible (SAGEM cybermodem)
5 *
6 * Author Karsten Keil
7 * Copyright by Karsten Keil <keil@isdn4linux.de>
8 *
9 * This software may be used and distributed according to the terms
10 * of the GNU General Public License, incorporated herein by reference.
11 *
12 * Thanks to Dr. Neuhaus and SAGEM for information
13 *
14 */
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/init.h>
17#include "hisax.h"
18#include "isac.h"
19#include "hscx.h"
20#include "isdnl1.h"
21#include <linux/pci.h>
22#include <linux/isapnp.h>
23
24extern const char *CardType[];
Adrian Bunk672c3fd2005-06-25 14:59:18 -070025static const char *niccy_revision = "$Revision: 1.21.2.4 $";
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#define byteout(addr,val) outb(val,addr)
28#define bytein(addr) inb(addr)
29
30#define ISAC_PCI_DATA 0
31#define HSCX_PCI_DATA 1
32#define ISAC_PCI_ADDR 2
33#define HSCX_PCI_ADDR 3
34#define ISAC_PNP 0
35#define HSCX_PNP 1
36
37/* SUB Types */
38#define NICCY_PNP 1
39#define NICCY_PCI 2
40
41/* PCI stuff */
42#define PCI_IRQ_CTRL_REG 0x38
43#define PCI_IRQ_ENABLE 0x1f00
44#define PCI_IRQ_DISABLE 0xff0000
45#define PCI_IRQ_ASSERT 0x800000
46
Jiri Slaby1d02a032006-10-04 02:15:42 -070047static inline u_char readreg(unsigned int ale, unsigned int adr, u_char off)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
49 register u_char ret;
50
51 byteout(ale, off);
52 ret = bytein(adr);
Jiri Slaby1d02a032006-10-04 02:15:42 -070053 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054}
55
Jiri Slaby1d02a032006-10-04 02:15:42 -070056static inline void readfifo(unsigned int ale, unsigned int adr, u_char off,
57 u_char *data, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
59 byteout(ale, off);
60 insb(adr, data, size);
61}
62
Jiri Slaby1d02a032006-10-04 02:15:42 -070063static inline void writereg(unsigned int ale, unsigned int adr, u_char off,
64 u_char data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
66 byteout(ale, off);
67 byteout(adr, data);
68}
69
Jiri Slaby1d02a032006-10-04 02:15:42 -070070static inline void writefifo(unsigned int ale, unsigned int adr, u_char off,
71 u_char *data, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
73 byteout(ale, off);
74 outsb(adr, data, size);
75}
76
77/* Interface functions */
78
Jiri Slaby1d02a032006-10-04 02:15:42 -070079static u_char ReadISAC(struct IsdnCardState *cs, u_char offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
Jiri Slaby1d02a032006-10-04 02:15:42 -070081 return readreg(cs->hw.niccy.isac_ale, cs->hw.niccy.isac, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
83
Jiri Slaby1d02a032006-10-04 02:15:42 -070084static void WriteISAC(struct IsdnCardState *cs, u_char offset, u_char value)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
86 writereg(cs->hw.niccy.isac_ale, cs->hw.niccy.isac, offset, value);
87}
88
Jiri Slaby1d02a032006-10-04 02:15:42 -070089static void ReadISACfifo(struct IsdnCardState *cs, u_char * data, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
91 readfifo(cs->hw.niccy.isac_ale, cs->hw.niccy.isac, 0, data, size);
92}
93
Jiri Slaby1d02a032006-10-04 02:15:42 -070094static void WriteISACfifo(struct IsdnCardState *cs, u_char * data, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 writefifo(cs->hw.niccy.isac_ale, cs->hw.niccy.isac, 0, data, size);
97}
98
Jiri Slaby1d02a032006-10-04 02:15:42 -070099static u_char ReadHSCX(struct IsdnCardState *cs, int hscx, u_char offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Jiri Slaby1d02a032006-10-04 02:15:42 -0700101 return readreg(cs->hw.niccy.hscx_ale,
102 cs->hw.niccy.hscx, offset + (hscx ? 0x40 : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
Jiri Slaby1d02a032006-10-04 02:15:42 -0700105static void WriteHSCX(struct IsdnCardState *cs, int hscx, u_char offset,
106 u_char value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
108 writereg(cs->hw.niccy.hscx_ale,
109 cs->hw.niccy.hscx, offset + (hscx ? 0x40 : 0), value);
110}
111
112#define READHSCX(cs, nr, reg) readreg(cs->hw.niccy.hscx_ale, \
113 cs->hw.niccy.hscx, reg + (nr ? 0x40 : 0))
114#define WRITEHSCX(cs, nr, reg, data) writereg(cs->hw.niccy.hscx_ale, \
115 cs->hw.niccy.hscx, reg + (nr ? 0x40 : 0), data)
116
117#define READHSCXFIFO(cs, nr, ptr, cnt) readfifo(cs->hw.niccy.hscx_ale, \
118 cs->hw.niccy.hscx, (nr ? 0x40 : 0), ptr, cnt)
119
120#define WRITEHSCXFIFO(cs, nr, ptr, cnt) writefifo(cs->hw.niccy.hscx_ale, \
121 cs->hw.niccy.hscx, (nr ? 0x40 : 0), ptr, cnt)
122
123#include "hscx_irq.c"
124
David Howells7d12e782006-10-05 14:55:46 +0100125static irqreturn_t niccy_interrupt(int intno, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
127 struct IsdnCardState *cs = dev_id;
128 u_char val;
129 u_long flags;
130
131 spin_lock_irqsave(&cs->lock, flags);
132 if (cs->subtyp == NICCY_PCI) {
133 int ival;
134 ival = inl(cs->hw.niccy.cfg_reg + PCI_IRQ_CTRL_REG);
Jiri Slaby1d02a032006-10-04 02:15:42 -0700135 if (!(ival & PCI_IRQ_ASSERT)) { /* IRQ not for us (shared) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 spin_unlock_irqrestore(&cs->lock, flags);
137 return IRQ_NONE;
138 }
139 outl(ival, cs->hw.niccy.cfg_reg + PCI_IRQ_CTRL_REG);
140 }
Jiri Slaby1d02a032006-10-04 02:15:42 -0700141 val = readreg(cs->hw.niccy.hscx_ale, cs->hw.niccy.hscx,
142 HSCX_ISTA + 0x40);
143Start_HSCX:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 if (val)
145 hscx_int_main(cs, val);
146 val = readreg(cs->hw.niccy.isac_ale, cs->hw.niccy.isac, ISAC_ISTA);
Jiri Slaby1d02a032006-10-04 02:15:42 -0700147Start_ISAC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 if (val)
149 isac_interrupt(cs, val);
Jiri Slaby1d02a032006-10-04 02:15:42 -0700150 val = readreg(cs->hw.niccy.hscx_ale, cs->hw.niccy.hscx,
151 HSCX_ISTA + 0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 if (val) {
153 if (cs->debug & L1_DEB_HSCX)
154 debugl1(cs, "HSCX IntStat after IntRoutine");
155 goto Start_HSCX;
156 }
157 val = readreg(cs->hw.niccy.isac_ale, cs->hw.niccy.isac, ISAC_ISTA);
158 if (val) {
159 if (cs->debug & L1_DEB_ISAC)
160 debugl1(cs, "ISAC IntStat after IntRoutine");
161 goto Start_ISAC;
162 }
163 writereg(cs->hw.niccy.hscx_ale, cs->hw.niccy.hscx, HSCX_MASK, 0xFF);
Jiri Slaby1d02a032006-10-04 02:15:42 -0700164 writereg(cs->hw.niccy.hscx_ale, cs->hw.niccy.hscx, HSCX_MASK + 0x40,
165 0xFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 writereg(cs->hw.niccy.isac_ale, cs->hw.niccy.isac, ISAC_MASK, 0xFF);
167 writereg(cs->hw.niccy.isac_ale, cs->hw.niccy.isac, ISAC_MASK, 0);
168 writereg(cs->hw.niccy.hscx_ale, cs->hw.niccy.hscx, HSCX_MASK, 0);
Jiri Slaby1d02a032006-10-04 02:15:42 -0700169 writereg(cs->hw.niccy.hscx_ale, cs->hw.niccy.hscx, HSCX_MASK + 0x40,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 spin_unlock_irqrestore(&cs->lock, flags);
171 return IRQ_HANDLED;
172}
173
Jiri Slaby1d02a032006-10-04 02:15:42 -0700174static void release_io_niccy(struct IsdnCardState *cs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
176 if (cs->subtyp == NICCY_PCI) {
177 int val;
Jiri Slaby1d02a032006-10-04 02:15:42 -0700178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 val = inl(cs->hw.niccy.cfg_reg + PCI_IRQ_CTRL_REG);
180 val &= PCI_IRQ_DISABLE;
181 outl(val, cs->hw.niccy.cfg_reg + PCI_IRQ_CTRL_REG);
182 release_region(cs->hw.niccy.cfg_reg, 0x40);
183 release_region(cs->hw.niccy.isac, 4);
184 } else {
185 release_region(cs->hw.niccy.isac, 2);
186 release_region(cs->hw.niccy.isac_ale, 2);
187 }
188}
189
Jiri Slaby1d02a032006-10-04 02:15:42 -0700190static void niccy_reset(struct IsdnCardState *cs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
192 if (cs->subtyp == NICCY_PCI) {
193 int val;
194
195 val = inl(cs->hw.niccy.cfg_reg + PCI_IRQ_CTRL_REG);
196 val |= PCI_IRQ_ENABLE;
197 outl(val, cs->hw.niccy.cfg_reg + PCI_IRQ_CTRL_REG);
198 }
199 inithscxisac(cs, 3);
200}
201
Jiri Slaby1d02a032006-10-04 02:15:42 -0700202static int niccy_card_msg(struct IsdnCardState *cs, int mt, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
204 u_long flags;
205
206 switch (mt) {
Jiri Slaby1d02a032006-10-04 02:15:42 -0700207 case CARD_RESET:
208 spin_lock_irqsave(&cs->lock, flags);
209 niccy_reset(cs);
210 spin_unlock_irqrestore(&cs->lock, flags);
211 return 0;
212 case CARD_RELEASE:
213 release_io_niccy(cs);
214 return 0;
215 case CARD_INIT:
216 spin_lock_irqsave(&cs->lock, flags);
217 niccy_reset(cs);
218 spin_unlock_irqrestore(&cs->lock, flags);
219 return 0;
220 case CARD_TEST:
221 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
Jiri Slaby1d02a032006-10-04 02:15:42 -0700223 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
225
Karsten Keil67eb5db2006-07-10 04:44:11 -0700226static struct pci_dev *niccy_dev __devinitdata = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227#ifdef __ISAPNP__
228static struct pnp_card *pnp_c __devinitdata = NULL;
229#endif
230
Jiri Slaby1d02a032006-10-04 02:15:42 -0700231int __devinit setup_niccy(struct IsdnCard *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
233 struct IsdnCardState *cs = card->cs;
234 char tmp[64];
235
236 strcpy(tmp, niccy_revision);
237 printk(KERN_INFO "HiSax: Niccy driver Rev. %s\n", HiSax_getrev(tmp));
238 if (cs->typ != ISDN_CTYPE_NICCY)
Jiri Slaby1d02a032006-10-04 02:15:42 -0700239 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240#ifdef __ISAPNP__
241 if (!card->para[1] && isapnp_present()) {
242 struct pnp_dev *pnp_d = NULL;
243 int err;
244
Jiri Slaby1d02a032006-10-04 02:15:42 -0700245 pnp_c = pnp_find_card(ISAPNP_VENDOR('S', 'D', 'A'),
246 ISAPNP_FUNCTION(0x0150), pnp_c);
247 if (pnp_c) {
248 pnp_d = pnp_find_dev(pnp_c,
249 ISAPNP_VENDOR('S', 'D', 'A'),
250 ISAPNP_FUNCTION(0x0150), pnp_d);
251 if (!pnp_d) {
252 printk(KERN_ERR "NiccyPnP: PnP error card "
253 "found, no device\n");
254 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 }
256 pnp_disable_dev(pnp_d);
257 err = pnp_activate_dev(pnp_d);
Jiri Slaby1d02a032006-10-04 02:15:42 -0700258 if (err < 0) {
259 printk(KERN_WARNING "%s: pnp_activate_dev "
260 "ret(%d)\n", __FUNCTION__, err);
261 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 }
263 card->para[1] = pnp_port_start(pnp_d, 0);
264 card->para[2] = pnp_port_start(pnp_d, 1);
265 card->para[0] = pnp_irq(pnp_d, 0);
Jiri Slaby1d02a032006-10-04 02:15:42 -0700266 if (!card->para[0] || !card->para[1] ||
267 !card->para[2]) {
268 printk(KERN_ERR "NiccyPnP:some resources are "
269 "missing %ld/%lx/%lx\n",
270 card->para[0], card->para[1],
271 card->para[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 pnp_disable_dev(pnp_d);
Jiri Slaby1d02a032006-10-04 02:15:42 -0700273 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 }
Jiri Slaby1d02a032006-10-04 02:15:42 -0700275 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 printk(KERN_INFO "NiccyPnP: no ISAPnP card found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 }
278#endif
279 if (card->para[1]) {
280 cs->hw.niccy.isac = card->para[1] + ISAC_PNP;
281 cs->hw.niccy.hscx = card->para[1] + HSCX_PNP;
282 cs->hw.niccy.isac_ale = card->para[2] + ISAC_PNP;
283 cs->hw.niccy.hscx_ale = card->para[2] + HSCX_PNP;
284 cs->hw.niccy.cfg_reg = 0;
285 cs->subtyp = NICCY_PNP;
286 cs->irq = card->para[0];
287 if (!request_region(cs->hw.niccy.isac, 2, "niccy data")) {
Jiri Slaby1d02a032006-10-04 02:15:42 -0700288 printk(KERN_WARNING "HiSax: %s data port %x-%x "
289 "already in use\n", CardType[card->typ],
290 cs->hw.niccy.isac, cs->hw.niccy.isac + 1);
291 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 }
293 if (!request_region(cs->hw.niccy.isac_ale, 2, "niccy addr")) {
Jiri Slaby1d02a032006-10-04 02:15:42 -0700294 printk(KERN_WARNING "HiSax: %s address port %x-%x "
295 "already in use\n", CardType[card->typ],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 cs->hw.niccy.isac_ale,
297 cs->hw.niccy.isac_ale + 1);
298 release_region(cs->hw.niccy.isac, 2);
Jiri Slaby1d02a032006-10-04 02:15:42 -0700299 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 }
301 } else {
302#ifdef CONFIG_PCI
303 u_int pci_ioaddr;
304 cs->subtyp = 0;
305 if ((niccy_dev = pci_find_device(PCI_VENDOR_ID_SATSAGEM,
Jiri Slaby1d02a032006-10-04 02:15:42 -0700306 PCI_DEVICE_ID_SATSAGEM_NICCY,
307 niccy_dev))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 if (pci_enable_device(niccy_dev))
Jiri Slaby1d02a032006-10-04 02:15:42 -0700309 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 /* get IRQ */
311 if (!niccy_dev->irq) {
Jiri Slaby1d02a032006-10-04 02:15:42 -0700312 printk(KERN_WARNING
313 "Niccy: No IRQ for PCI card found\n");
314 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 }
316 cs->irq = niccy_dev->irq;
317 cs->hw.niccy.cfg_reg = pci_resource_start(niccy_dev, 0);
318 if (!cs->hw.niccy.cfg_reg) {
Jiri Slaby1d02a032006-10-04 02:15:42 -0700319 printk(KERN_WARNING
320 "Niccy: No IO-Adr for PCI cfg found\n");
321 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 }
323 pci_ioaddr = pci_resource_start(niccy_dev, 1);
324 if (!pci_ioaddr) {
Jiri Slaby1d02a032006-10-04 02:15:42 -0700325 printk(KERN_WARNING
326 "Niccy: No IO-Adr for PCI card found\n");
327 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
329 cs->subtyp = NICCY_PCI;
330 } else {
331 printk(KERN_WARNING "Niccy: No PCI card found\n");
Jiri Slaby1d02a032006-10-04 02:15:42 -0700332 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 }
Thomas Gleixner9ba02be2006-07-01 19:29:36 -0700334 cs->irq_flags |= IRQF_SHARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 cs->hw.niccy.isac = pci_ioaddr + ISAC_PCI_DATA;
336 cs->hw.niccy.isac_ale = pci_ioaddr + ISAC_PCI_ADDR;
337 cs->hw.niccy.hscx = pci_ioaddr + HSCX_PCI_DATA;
338 cs->hw.niccy.hscx_ale = pci_ioaddr + HSCX_PCI_ADDR;
339 if (!request_region(cs->hw.niccy.isac, 4, "niccy")) {
340 printk(KERN_WARNING
Jiri Slaby1d02a032006-10-04 02:15:42 -0700341 "HiSax: %s data port %x-%x already in use\n",
342 CardType[card->typ],
343 cs->hw.niccy.isac, cs->hw.niccy.isac + 4);
344 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
346 if (!request_region(cs->hw.niccy.cfg_reg, 0x40, "niccy pci")) {
347 printk(KERN_WARNING
348 "HiSax: %s pci port %x-%x already in use\n",
Jiri Slaby1d02a032006-10-04 02:15:42 -0700349 CardType[card->typ],
350 cs->hw.niccy.cfg_reg,
351 cs->hw.niccy.cfg_reg + 0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 release_region(cs->hw.niccy.isac, 4);
Jiri Slaby1d02a032006-10-04 02:15:42 -0700353 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 }
355#else
356 printk(KERN_WARNING "Niccy: io0 0 and NO_PCI_BIOS\n");
357 printk(KERN_WARNING "Niccy: unable to config NICCY PCI\n");
Jiri Slaby1d02a032006-10-04 02:15:42 -0700358 return 0;
359#endif /* CONFIG_PCI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 }
361 printk(KERN_INFO "HiSax: %s %s config irq:%d data:0x%X ale:0x%X\n",
Jiri Slaby1d02a032006-10-04 02:15:42 -0700362 CardType[cs->typ], (cs->subtyp == 1) ? "PnP" : "PCI",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 cs->irq, cs->hw.niccy.isac, cs->hw.niccy.isac_ale);
364 setup_isac(cs);
365 cs->readisac = &ReadISAC;
366 cs->writeisac = &WriteISAC;
367 cs->readisacfifo = &ReadISACfifo;
368 cs->writeisacfifo = &WriteISACfifo;
369 cs->BC_Read_Reg = &ReadHSCX;
370 cs->BC_Write_Reg = &WriteHSCX;
371 cs->BC_Send_Data = &hscx_fill_fifo;
372 cs->cardmsg = &niccy_card_msg;
373 cs->irq_func = &niccy_interrupt;
374 ISACVersion(cs, "Niccy:");
375 if (HscxVersion(cs, "Niccy:")) {
Jiri Slaby1d02a032006-10-04 02:15:42 -0700376 printk(KERN_WARNING "Niccy: wrong HSCX versions check IO "
377 "address\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 release_io_niccy(cs);
Jiri Slaby1d02a032006-10-04 02:15:42 -0700379 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 }
Jiri Slaby1d02a032006-10-04 02:15:42 -0700381 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382}