- Kai Germaschewski: ISDN update (including Makefiles)
[davej-history.git] / drivers / isdn / hysdn / boardergo.c
blob92e96ec9b40e4e3234e9155beb972f73249f8635
1 /* $Id: boardergo.c,v 1.5.6.1 2000/12/10 22:01:04 kai Exp $
3 * Linux driver for HYSDN cards, specific routines for ergo type boards.
5 * As all Linux supported cards Champ2, Ergo and Metro2/4 use the same
6 * DPRAM interface and layout with only minor differences all related
7 * stuff is done here, not in separate modules.
9 * written by Werner Cornelius (werner@titro.de) for Hypercope GmbH
11 * Copyright 1999 by Werner Cornelius (werner@titro.de)
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2, or (at your option)
16 * any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #define __NO_VERSION__
30 #include <linux/config.h>
31 #include <linux/module.h>
32 #include <linux/version.h>
33 #include <asm/io.h>
34 #include <linux/signal.h>
35 #include <linux/kernel.h>
36 #include <linux/ioport.h>
37 #include <linux/interrupt.h>
39 #include "hysdn_defs.h"
40 #include "boardergo.h"
42 #define byteout(addr,val) outb(val,addr)
43 #define bytein(addr) inb(addr)
45 /***************************************************/
46 /* The cards interrupt handler. Called from system */
47 /***************************************************/
48 static void
49 ergo_interrupt(int intno, void *dev_id, struct pt_regs *regs)
51 hysdn_card *card = dev_id; /* parameter from irq */
52 tErgDpram *dpr;
53 ulong flags;
54 uchar volatile b;
56 if (!card)
57 return; /* error -> spurious interrupt */
58 if (!card->irq_enabled)
59 return; /* other device interrupting or irq switched off */
61 save_flags(flags);
62 cli(); /* no further irqs allowed */
64 if (!(bytein(card->iobase + PCI9050_INTR_REG) & PCI9050_INTR_REG_STAT1)) {
65 restore_flags(flags); /* restore old state */
66 return; /* no interrupt requested by E1 */
68 /* clear any pending ints on the board */
69 dpr = card->dpram;
70 b = dpr->ToPcInt; /* clear for ergo */
71 b |= dpr->ToPcIntMetro; /* same for metro */
72 b |= dpr->ToHyInt; /* and for champ */
74 /* start kernel task immediately after leaving all interrupts */
75 if (!card->hw_lock) {
76 queue_task(&card->irq_queue, &tq_immediate);
77 mark_bh(IMMEDIATE_BH);
79 restore_flags(flags);
80 } /* ergo_interrupt */
82 /******************************************************************************/
83 /* ergo_irq_bh is the function called by the immediate kernel task list after */
84 /* being activated with queue_task and no interrupts active. This task is the */
85 /* only one handling data transfer from or to the card after booting. The task */
86 /* may be queued from everywhere (interrupts included). */
87 /******************************************************************************/
88 static void
89 ergo_irq_bh(hysdn_card * card)
91 tErgDpram *dpr;
92 int again;
93 ulong flags;
95 if (card->state != CARD_STATE_RUN)
96 return; /* invalid call */
98 dpr = card->dpram; /* point to DPRAM */
100 save_flags(flags);
101 cli();
102 if (card->hw_lock) {
103 restore_flags(flags); /* hardware currently unavailable */
104 return;
106 card->hw_lock = 1; /* we now lock the hardware */
108 do {
109 sti(); /* reenable other ints */
110 again = 0; /* assume loop not to be repeated */
112 if (!dpr->ToHyFlag) {
113 /* we are able to send a buffer */
115 if (hysdn_sched_tx(card, dpr->ToHyBuf, &dpr->ToHySize, &dpr->ToHyChannel,
116 ERG_TO_HY_BUF_SIZE)) {
117 dpr->ToHyFlag = 1; /* enable tx */
118 again = 1; /* restart loop */
120 } /* we are able to send a buffer */
121 if (dpr->ToPcFlag) {
122 /* a message has arrived for us, handle it */
124 if (hysdn_sched_rx(card, dpr->ToPcBuf, dpr->ToPcSize, dpr->ToPcChannel)) {
125 dpr->ToPcFlag = 0; /* we worked the data */
126 again = 1; /* restart loop */
128 } /* a message has arrived for us */
129 cli(); /* no further ints */
130 if (again) {
131 dpr->ToHyInt = 1;
132 dpr->ToPcInt = 1; /* interrupt to E1 for all cards */
133 } else
134 card->hw_lock = 0; /* free hardware again */
135 } while (again); /* until nothing more to do */
137 restore_flags(flags);
138 } /* ergo_irq_bh */
141 /*********************************************************/
142 /* stop the card (hardware reset) and disable interrupts */
143 /*********************************************************/
144 static void
145 ergo_stopcard(hysdn_card * card)
147 ulong flags;
148 uchar val;
150 hysdn_net_release(card); /* first release the net device if existing */
151 #ifdef CONFIG_HYSDN_CAPI
152 hycapi_capi_stop(card);
153 #endif /* CONFIG_HYSDN_CAPI */
154 save_flags(flags);
155 cli();
156 val = bytein(card->iobase + PCI9050_INTR_REG); /* get actual value */
157 val &= ~(PCI9050_INTR_REG_ENPCI | PCI9050_INTR_REG_EN1); /* mask irq */
158 byteout(card->iobase + PCI9050_INTR_REG, val);
159 card->irq_enabled = 0;
160 byteout(card->iobase + PCI9050_USER_IO, PCI9050_E1_RESET); /* reset E1 processor */
161 card->state = CARD_STATE_UNUSED;
162 card->err_log_state = ERRLOG_STATE_OFF; /* currently no log active */
164 restore_flags(flags);
165 } /* ergo_stopcard */
167 /**************************************************************************/
168 /* enable or disable the cards error log. The event is queued if possible */
169 /**************************************************************************/
170 static void
171 ergo_set_errlog_state(hysdn_card * card, int on)
173 ulong flags;
175 if (card->state != CARD_STATE_RUN) {
176 card->err_log_state = ERRLOG_STATE_OFF; /* must be off */
177 return;
179 save_flags(flags);
180 cli();
182 if (((card->err_log_state == ERRLOG_STATE_OFF) && !on) ||
183 ((card->err_log_state == ERRLOG_STATE_ON) && on)) {
184 restore_flags(flags);
185 return; /* nothing to do */
187 if (on)
188 card->err_log_state = ERRLOG_STATE_START; /* request start */
189 else
190 card->err_log_state = ERRLOG_STATE_STOP; /* request stop */
192 restore_flags(flags);
193 queue_task(&card->irq_queue, &tq_immediate);
194 mark_bh(IMMEDIATE_BH);
195 } /* ergo_set_errlog_state */
197 /******************************************/
198 /* test the cards RAM and return 0 if ok. */
199 /******************************************/
200 static const char TestText[36] = "This Message is filler, why read it";
202 static int
203 ergo_testram(hysdn_card * card)
205 tErgDpram *dpr = card->dpram;
207 memset(dpr->TrapTable, 0, sizeof(dpr->TrapTable)); /* clear all Traps */
208 dpr->ToHyInt = 1; /* E1 INTR state forced */
210 memcpy(&dpr->ToHyBuf[ERG_TO_HY_BUF_SIZE - sizeof(TestText)], TestText,
211 sizeof(TestText));
212 if (memcmp(&dpr->ToHyBuf[ERG_TO_HY_BUF_SIZE - sizeof(TestText)], TestText,
213 sizeof(TestText)))
214 return (-1);
216 memcpy(&dpr->ToPcBuf[ERG_TO_PC_BUF_SIZE - sizeof(TestText)], TestText,
217 sizeof(TestText));
218 if (memcmp(&dpr->ToPcBuf[ERG_TO_PC_BUF_SIZE - sizeof(TestText)], TestText,
219 sizeof(TestText)))
220 return (-1);
222 return (0);
223 } /* ergo_testram */
225 /*****************************************************************************/
226 /* this function is intended to write stage 1 boot image to the cards buffer */
227 /* this is done in two steps. First the 1024 hi-words are written (offs=0), */
228 /* then the 1024 lo-bytes are written. The remaining DPRAM is cleared, the */
229 /* PCI-write-buffers flushed and the card is taken out of reset. */
230 /* The function then waits for a reaction of the E1 processor or a timeout. */
231 /* Negative return values are interpreted as errors. */
232 /*****************************************************************************/
233 static int
234 ergo_writebootimg(struct HYSDN_CARD *card, uchar * buf, ulong offs)
236 uchar *dst;
237 tErgDpram *dpram;
238 int cnt = (BOOT_IMG_SIZE >> 2); /* number of words to move and swap (byte order!) */
240 if (card->debug_flags & LOG_POF_CARD)
241 hysdn_addlog(card, "ERGO: write bootldr offs=0x%lx ", offs);
243 dst = card->dpram; /* pointer to start of DPRAM */
244 dst += (offs + ERG_DPRAM_FILL_SIZE); /* offset in the DPRAM */
245 while (cnt--) {
246 *dst++ = *(buf + 1); /* high byte */
247 *dst++ = *buf; /* low byte */
248 dst += 2; /* point to next longword */
249 buf += 2; /* buffer only filled with words */
252 /* if low words (offs = 2) have been written, clear the rest of the DPRAM, */
253 /* flush the PCI-write-buffer and take the E1 out of reset */
254 if (offs) {
255 memset(card->dpram, 0, ERG_DPRAM_FILL_SIZE); /* fill the DPRAM still not cleared */
256 dpram = card->dpram; /* get pointer to dpram structure */
257 dpram->ToHyNoDpramErrLog = 0xFF; /* write a dpram register */
258 while (!dpram->ToHyNoDpramErrLog); /* reread volatile register to flush PCI */
260 byteout(card->iobase + PCI9050_USER_IO, PCI9050_E1_RUN); /* start E1 processor */
261 /* the interrupts are still masked */
263 sti();
264 set_current_state(TASK_INTERRUPTIBLE);
265 schedule_timeout((20 * HZ) / 1000); /* Timeout 20ms */
267 if (((tDpramBootSpooler *) card->dpram)->Len != DPRAM_SPOOLER_DATA_SIZE) {
268 if (card->debug_flags & LOG_POF_CARD)
269 hysdn_addlog(card, "ERGO: write bootldr no answer");
270 return (-ERR_BOOTIMG_FAIL);
272 } /* start_boot_img */
273 return (0); /* successfull */
274 } /* ergo_writebootimg */
276 /********************************************************************************/
277 /* ergo_writebootseq writes the buffer containing len bytes to the E1 processor */
278 /* using the boot spool mechanism. If everything works fine 0 is returned. In */
279 /* case of errors a negative error value is returned. */
280 /********************************************************************************/
281 static int
282 ergo_writebootseq(struct HYSDN_CARD *card, uchar * buf, int len)
284 tDpramBootSpooler *sp = (tDpramBootSpooler *) card->dpram;
285 uchar *dst;
286 uchar buflen;
287 int nr_write;
288 uchar tmp_rdptr;
289 uchar wr_mirror;
290 int i;
292 if (card->debug_flags & LOG_POF_CARD)
293 hysdn_addlog(card, "ERGO: write boot seq len=%d ", len);
295 dst = sp->Data; /* point to data in spool structure */
296 buflen = sp->Len; /* maximum len of spooled data */
297 wr_mirror = sp->WrPtr; /* only once read */
298 sti();
300 /* try until all bytes written or error */
301 i = 0x1000; /* timeout value */
302 while (len) {
304 /* first determine the number of bytes that may be buffered */
305 do {
306 tmp_rdptr = sp->RdPtr; /* first read the pointer */
307 i--; /* decrement timeout */
308 } while (i && (tmp_rdptr != sp->RdPtr)); /* wait for stable pointer */
310 if (!i) {
311 if (card->debug_flags & LOG_POF_CARD)
312 hysdn_addlog(card, "ERGO: write boot seq timeout");
313 return (-ERR_BOOTSEQ_FAIL); /* value not stable -> timeout */
315 if ((nr_write = tmp_rdptr - wr_mirror - 1) < 0)
316 nr_write += buflen; /* now we got number of free bytes - 1 in buffer */
318 if (!nr_write)
319 continue; /* no free bytes in buffer */
321 if (nr_write > len)
322 nr_write = len; /* limit if last few bytes */
323 i = 0x1000; /* reset timeout value */
325 /* now we know how much bytes we may put in the puffer */
326 len -= nr_write; /* we savely could adjust len before output */
327 while (nr_write--) {
328 *(dst + wr_mirror) = *buf++; /* output one byte */
329 if (++wr_mirror >= buflen)
330 wr_mirror = 0;
331 sp->WrPtr = wr_mirror; /* announce the next byte to E1 */
332 } /* while (nr_write) */
334 } /* while (len) */
335 return (0);
336 } /* ergo_writebootseq */
338 /***********************************************************************************/
339 /* ergo_waitpofready waits for a maximum of 10 seconds for the completition of the */
340 /* boot process. If the process has been successfull 0 is returned otherwise a */
341 /* negative error code is returned. */
342 /***********************************************************************************/
343 static int
344 ergo_waitpofready(struct HYSDN_CARD *card)
346 tErgDpram *dpr = card->dpram; /* pointer to DPRAM structure */
347 int timecnt = 10000 / 50; /* timeout is 10 secs max. */
348 ulong flags;
349 int msg_size;
350 int i;
352 if (card->debug_flags & LOG_POF_CARD)
353 hysdn_addlog(card, "ERGO: waiting for pof ready");
354 while (timecnt--) {
355 /* wait until timeout */
357 if (dpr->ToPcFlag) {
358 /* data has arrived */
360 if ((dpr->ToPcChannel != CHAN_SYSTEM) ||
361 (dpr->ToPcSize < MIN_RDY_MSG_SIZE) ||
362 (dpr->ToPcSize > MAX_RDY_MSG_SIZE) ||
363 ((*(ulong *) dpr->ToPcBuf) != RDY_MAGIC))
364 break; /* an error occured */
366 /* Check for additional data delivered during SysReady */
367 msg_size = dpr->ToPcSize - RDY_MAGIC_SIZE;
368 if (msg_size > 0)
369 if (EvalSysrTokData(card, dpr->ToPcBuf + RDY_MAGIC_SIZE, msg_size))
370 break;
372 if (card->debug_flags & LOG_POF_RECORD)
373 hysdn_addlog(card, "ERGO: pof boot success");
374 save_flags(flags);
375 cli();
377 card->state = CARD_STATE_RUN; /* now card is running */
378 /* enable the cards interrupt */
379 byteout(card->iobase + PCI9050_INTR_REG,
380 bytein(card->iobase + PCI9050_INTR_REG) |
381 (PCI9050_INTR_REG_ENPCI | PCI9050_INTR_REG_EN1));
382 card->irq_enabled = 1; /* we are ready to receive interrupts */
384 dpr->ToPcFlag = 0; /* reset data indicator */
385 dpr->ToHyInt = 1;
386 dpr->ToPcInt = 1; /* interrupt to E1 for all cards */
388 restore_flags(flags);
389 if ((i = hysdn_net_create(card))) {
390 ergo_stopcard(card);
391 card->state = CARD_STATE_BOOTERR;
392 return (i);
394 #ifdef CONFIG_HYSDN_CAPI
395 if((i = hycapi_capi_create(card))) {
396 printk(KERN_WARNING "HYSDN: failed to create capi-interface.\n");
398 #endif /* CONFIG_HYSDN_CAPI */
399 return (0); /* success */
400 } /* data has arrived */
401 sti();
402 set_current_state(TASK_INTERRUPTIBLE);
403 schedule_timeout((50 * HZ) / 1000); /* Timeout 50ms */
404 } /* wait until timeout */
406 if (card->debug_flags & LOG_POF_CARD)
407 hysdn_addlog(card, "ERGO: pof boot ready timeout");
408 return (-ERR_POF_TIMEOUT);
409 } /* ergo_waitpofready */
413 /************************************************************************************/
414 /* release the cards hardware. Before releasing do a interrupt disable and hardware */
415 /* reset. Also unmap dpram. */
416 /* Use only during module release. */
417 /************************************************************************************/
418 static void
419 ergo_releasehardware(hysdn_card * card)
421 ergo_stopcard(card); /* first stop the card if not already done */
422 free_irq(card->irq, card); /* release interrupt */
423 release_region(card->iobase + PCI9050_INTR_REG, 1); /* release all io ports */
424 release_region(card->iobase + PCI9050_USER_IO, 1);
425 vfree(card->dpram);
426 card->dpram = NULL; /* release shared mem */
427 } /* ergo_releasehardware */
430 /*********************************************************************************/
431 /* acquire the needed hardware ports and map dpram. If an error occurs a nonzero */
432 /* value is returned. */
433 /* Use only during module init. */
434 /*********************************************************************************/
436 ergo_inithardware(hysdn_card * card)
438 if (check_region(card->iobase + PCI9050_INTR_REG, 1) ||
439 check_region(card->iobase + PCI9050_USER_IO, 1))
440 return (-1); /* ports already in use */
442 card->memend = card->membase + ERG_DPRAM_PAGE_SIZE - 1;
443 if (!(card->dpram = ioremap(card->membase, ERG_DPRAM_PAGE_SIZE)))
444 return (-1);
446 request_region(card->iobase + PCI9050_INTR_REG, 1, "HYSDN");
447 request_region(card->iobase + PCI9050_USER_IO, 1, "HYSDN");
448 ergo_stopcard(card); /* disable interrupts */
449 if (request_irq(card->irq, ergo_interrupt, SA_SHIRQ, "HYSDN", card)) {
450 ergo_releasehardware(card); /* return the acquired hardware */
451 return (-1);
453 /* success, now setup the function pointers */
454 card->stopcard = ergo_stopcard;
455 card->releasehardware = ergo_releasehardware;
456 card->testram = ergo_testram;
457 card->writebootimg = ergo_writebootimg;
458 card->writebootseq = ergo_writebootseq;
459 card->waitpofready = ergo_waitpofready;
460 card->set_errlog_state = ergo_set_errlog_state;
461 card->irq_queue.sync = 0;
462 card->irq_queue.data = card; /* init task queue for interrupt */
463 card->irq_queue.routine = (void *) (void *) ergo_irq_bh;
465 return (0);
466 } /* ergo_inithardware */