Pull platform-drivers into test branch
[linux-2.6] / drivers / isdn / hysdn / boardergo.c
1 /* $Id: boardergo.c,v 1.5.6.7 2001/11/06 21:58:19 kai Exp $
2  *
3  * Linux driver for HYSDN cards, specific routines for ergo type boards.
4  *
5  * Author    Werner Cornelius (werner@titro.de) for Hypercope GmbH
6  * Copyright 1999 by Werner Cornelius (werner@titro.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  * As all Linux supported cards Champ2, Ergo and Metro2/4 use the same
12  * DPRAM interface and layout with only minor differences all related
13  * stuff is done here, not in separate modules.
14  *
15  */
16
17 #include <linux/sched.h>
18 #include <linux/signal.h>
19 #include <linux/kernel.h>
20 #include <linux/ioport.h>
21 #include <linux/interrupt.h>
22 #include <linux/vmalloc.h>
23 #include <linux/delay.h>
24 #include <asm/io.h>
25
26 #include "hysdn_defs.h"
27 #include "boardergo.h"
28
29 #define byteout(addr,val) outb(val,addr)
30 #define bytein(addr) inb(addr)
31
32 /***************************************************/
33 /* The cards interrupt handler. Called from system */
34 /***************************************************/
35 static irqreturn_t
36 ergo_interrupt(int intno, void *dev_id)
37 {
38         hysdn_card *card = dev_id;      /* parameter from irq */
39         tErgDpram *dpr;
40         unsigned long flags;
41         unsigned char volatile b;
42
43         if (!card)
44                 return IRQ_NONE;                /* error -> spurious interrupt */
45         if (!card->irq_enabled)
46                 return IRQ_NONE;                /* other device interrupting or irq switched off */
47
48         spin_lock_irqsave(&card->hysdn_lock, flags); /* no further irqs allowed */
49
50         if (!(bytein(card->iobase + PCI9050_INTR_REG) & PCI9050_INTR_REG_STAT1)) {
51                 spin_unlock_irqrestore(&card->hysdn_lock, flags);       /* restore old state */
52                 return IRQ_NONE;                /* no interrupt requested by E1 */
53         }
54         /* clear any pending ints on the board */
55         dpr = card->dpram;
56         b = dpr->ToPcInt;       /* clear for ergo */
57         b |= dpr->ToPcIntMetro; /* same for metro */
58         b |= dpr->ToHyInt;      /* and for champ */
59
60         /* start kernel task immediately after leaving all interrupts */
61         if (!card->hw_lock)
62                 schedule_work(&card->irq_queue);
63         spin_unlock_irqrestore(&card->hysdn_lock, flags);
64         return IRQ_HANDLED;
65 }                               /* ergo_interrupt */
66
67 /******************************************************************************/
68 /* ergo_irq_bh is the function called by the immediate kernel task list after */
69 /* being activated with queue_task and no interrupts active. This task is the */
70 /* only one handling data transfer from or to the card after booting. The task */
71 /* may be queued from everywhere (interrupts included).                       */
72 /******************************************************************************/
73 static void
74 ergo_irq_bh(struct work_struct *ugli_api)
75 {
76         hysdn_card * card = container_of(ugli_api, hysdn_card, irq_queue);
77         tErgDpram *dpr;
78         int again;
79         unsigned long flags;
80
81         if (card->state != CARD_STATE_RUN)
82                 return;         /* invalid call */
83
84         dpr = card->dpram;      /* point to DPRAM */
85
86         spin_lock_irqsave(&card->hysdn_lock, flags);
87         if (card->hw_lock) {
88                 spin_unlock_irqrestore(&card->hysdn_lock, flags);       /* hardware currently unavailable */
89                 return;
90         }
91         card->hw_lock = 1;      /* we now lock the hardware */
92
93         do {
94                 sti();          /* reenable other ints */
95                 again = 0;      /* assume loop not to be repeated */
96
97                 if (!dpr->ToHyFlag) {
98                         /* we are able to send a buffer */
99
100                         if (hysdn_sched_tx(card, dpr->ToHyBuf, &dpr->ToHySize, &dpr->ToHyChannel,
101                                            ERG_TO_HY_BUF_SIZE)) {
102                                 dpr->ToHyFlag = 1;      /* enable tx */
103                                 again = 1;      /* restart loop */
104                         }
105                 }               /* we are able to send a buffer */
106                 if (dpr->ToPcFlag) {
107                         /* a message has arrived for us, handle it */
108
109                         if (hysdn_sched_rx(card, dpr->ToPcBuf, dpr->ToPcSize, dpr->ToPcChannel)) {
110                                 dpr->ToPcFlag = 0;      /* we worked the data */
111                                 again = 1;      /* restart loop */
112                         }
113                 }               /* a message has arrived for us */
114                 cli();          /* no further ints */
115                 if (again) {
116                         dpr->ToHyInt = 1;
117                         dpr->ToPcInt = 1;       /* interrupt to E1 for all cards */
118                 } else
119                         card->hw_lock = 0;      /* free hardware again */
120         } while (again);        /* until nothing more to do */
121
122         spin_unlock_irqrestore(&card->hysdn_lock, flags);
123 }                               /* ergo_irq_bh */
124
125
126 /*********************************************************/
127 /* stop the card (hardware reset) and disable interrupts */
128 /*********************************************************/
129 static void
130 ergo_stopcard(hysdn_card * card)
131 {
132         unsigned long flags;
133         unsigned char val;
134
135         hysdn_net_release(card);        /* first release the net device if existing */
136 #ifdef CONFIG_HYSDN_CAPI
137         hycapi_capi_stop(card);
138 #endif /* CONFIG_HYSDN_CAPI */
139         spin_lock_irqsave(&card->hysdn_lock, flags);
140         val = bytein(card->iobase + PCI9050_INTR_REG);  /* get actual value */
141         val &= ~(PCI9050_INTR_REG_ENPCI | PCI9050_INTR_REG_EN1);        /* mask irq */
142         byteout(card->iobase + PCI9050_INTR_REG, val);
143         card->irq_enabled = 0;
144         byteout(card->iobase + PCI9050_USER_IO, PCI9050_E1_RESET);      /* reset E1 processor */
145         card->state = CARD_STATE_UNUSED;
146         card->err_log_state = ERRLOG_STATE_OFF;         /* currently no log active */
147
148         spin_unlock_irqrestore(&card->hysdn_lock, flags);
149 }                               /* ergo_stopcard */
150
151 /**************************************************************************/
152 /* enable or disable the cards error log. The event is queued if possible */
153 /**************************************************************************/
154 static void
155 ergo_set_errlog_state(hysdn_card * card, int on)
156 {
157         unsigned long flags;
158
159         if (card->state != CARD_STATE_RUN) {
160                 card->err_log_state = ERRLOG_STATE_OFF;         /* must be off */
161                 return;
162         }
163         spin_lock_irqsave(&card->hysdn_lock, flags);
164
165         if (((card->err_log_state == ERRLOG_STATE_OFF) && !on) ||
166             ((card->err_log_state == ERRLOG_STATE_ON) && on)) {
167                 spin_unlock_irqrestore(&card->hysdn_lock, flags);
168                 return;         /* nothing to do */
169         }
170         if (on)
171                 card->err_log_state = ERRLOG_STATE_START;       /* request start */
172         else
173                 card->err_log_state = ERRLOG_STATE_STOP;        /* request stop */
174
175         spin_unlock_irqrestore(&card->hysdn_lock, flags);
176         schedule_work(&card->irq_queue);
177 }                               /* ergo_set_errlog_state */
178
179 /******************************************/
180 /* test the cards RAM and return 0 if ok. */
181 /******************************************/
182 static const char TestText[36] = "This Message is filler, why read it";
183
184 static int
185 ergo_testram(hysdn_card * card)
186 {
187         tErgDpram *dpr = card->dpram;
188
189         memset(dpr->TrapTable, 0, sizeof(dpr->TrapTable));      /* clear all Traps */
190         dpr->ToHyInt = 1;       /* E1 INTR state forced */
191
192         memcpy(&dpr->ToHyBuf[ERG_TO_HY_BUF_SIZE - sizeof(TestText)], TestText,
193                sizeof(TestText));
194         if (memcmp(&dpr->ToHyBuf[ERG_TO_HY_BUF_SIZE - sizeof(TestText)], TestText,
195                    sizeof(TestText)))
196                 return (-1);
197
198         memcpy(&dpr->ToPcBuf[ERG_TO_PC_BUF_SIZE - sizeof(TestText)], TestText,
199                sizeof(TestText));
200         if (memcmp(&dpr->ToPcBuf[ERG_TO_PC_BUF_SIZE - sizeof(TestText)], TestText,
201                    sizeof(TestText)))
202                 return (-1);
203
204         return (0);
205 }                               /* ergo_testram */
206
207 /*****************************************************************************/
208 /* this function is intended to write stage 1 boot image to the cards buffer */
209 /* this is done in two steps. First the 1024 hi-words are written (offs=0),  */
210 /* then the 1024 lo-bytes are written. The remaining DPRAM is cleared, the   */
211 /* PCI-write-buffers flushed and the card is taken out of reset.             */
212 /* The function then waits for a reaction of the E1 processor or a timeout.  */
213 /* Negative return values are interpreted as errors.                         */
214 /*****************************************************************************/
215 static int
216 ergo_writebootimg(struct HYSDN_CARD *card, unsigned char *buf,
217                         unsigned long offs)
218 {
219         unsigned char *dst;
220         tErgDpram *dpram;
221         int cnt = (BOOT_IMG_SIZE >> 2);         /* number of words to move and swap (byte order!) */
222         
223         if (card->debug_flags & LOG_POF_CARD)
224                 hysdn_addlog(card, "ERGO: write bootldr offs=0x%lx ", offs);
225
226         dst = card->dpram;      /* pointer to start of DPRAM */
227         dst += (offs + ERG_DPRAM_FILL_SIZE);    /* offset in the DPRAM */
228         while (cnt--) {
229                 *dst++ = *(buf + 1);    /* high byte */
230                 *dst++ = *buf;  /* low byte */
231                 dst += 2;       /* point to next longword */
232                 buf += 2;       /* buffer only filled with words */
233         }
234
235         /* if low words (offs = 2) have been written, clear the rest of the DPRAM, */
236         /* flush the PCI-write-buffer and take the E1 out of reset */
237         if (offs) {
238                 memset(card->dpram, 0, ERG_DPRAM_FILL_SIZE);    /* fill the DPRAM still not cleared */
239                 dpram = card->dpram;    /* get pointer to dpram structure */
240                 dpram->ToHyNoDpramErrLog = 0xFF;        /* write a dpram register */
241                 while (!dpram->ToHyNoDpramErrLog);      /* reread volatile register to flush PCI */
242
243                 byteout(card->iobase + PCI9050_USER_IO, PCI9050_E1_RUN);        /* start E1 processor */
244                 /* the interrupts are still masked */
245
246                 sti();
247                 msleep_interruptible(20);               /* Timeout 20ms */
248
249                 if (((tDpramBootSpooler *) card->dpram)->Len != DPRAM_SPOOLER_DATA_SIZE) {
250                         if (card->debug_flags & LOG_POF_CARD)
251                                 hysdn_addlog(card, "ERGO: write bootldr no answer");
252                         return (-ERR_BOOTIMG_FAIL);
253                 }
254         }                       /* start_boot_img */
255         return (0);             /* successful */
256 }                               /* ergo_writebootimg */
257
258 /********************************************************************************/
259 /* ergo_writebootseq writes the buffer containing len bytes to the E1 processor */
260 /* using the boot spool mechanism. If everything works fine 0 is returned. In   */
261 /* case of errors a negative error value is returned.                           */
262 /********************************************************************************/
263 static int
264 ergo_writebootseq(struct HYSDN_CARD *card, unsigned char *buf, int len)
265 {
266         tDpramBootSpooler *sp = (tDpramBootSpooler *) card->dpram;
267         unsigned char *dst;
268         unsigned char buflen;
269         int nr_write;
270         unsigned char tmp_rdptr;
271         unsigned char wr_mirror;
272         int i;
273
274         if (card->debug_flags & LOG_POF_CARD)
275                 hysdn_addlog(card, "ERGO: write boot seq len=%d ", len);
276
277         dst = sp->Data;         /* point to data in spool structure */
278         buflen = sp->Len;       /* maximum len of spooled data */
279         wr_mirror = sp->WrPtr;  /* only once read */
280         sti();
281
282         /* try until all bytes written or error */
283         i = 0x1000;             /* timeout value */
284         while (len) {
285
286                 /* first determine the number of bytes that may be buffered */
287                 do {
288                         tmp_rdptr = sp->RdPtr;  /* first read the pointer */
289                         i--;    /* decrement timeout */
290                 } while (i && (tmp_rdptr != sp->RdPtr));        /* wait for stable pointer */
291
292                 if (!i) {
293                         if (card->debug_flags & LOG_POF_CARD)
294                                 hysdn_addlog(card, "ERGO: write boot seq timeout");
295                         return (-ERR_BOOTSEQ_FAIL);     /* value not stable -> timeout */
296                 }
297                 if ((nr_write = tmp_rdptr - wr_mirror - 1) < 0)
298                         nr_write += buflen;     /* now we got number of free bytes - 1 in buffer */
299
300                 if (!nr_write)
301                         continue;       /* no free bytes in buffer */
302
303                 if (nr_write > len)
304                         nr_write = len;         /* limit if last few bytes */
305                 i = 0x1000;     /* reset timeout value */
306
307                 /* now we know how much bytes we may put in the puffer */
308                 len -= nr_write;        /* we savely could adjust len before output */
309                 while (nr_write--) {
310                         *(dst + wr_mirror) = *buf++;    /* output one byte */
311                         if (++wr_mirror >= buflen)
312                                 wr_mirror = 0;
313                         sp->WrPtr = wr_mirror;  /* announce the next byte to E1 */
314                 }               /* while (nr_write) */
315
316         }                       /* while (len) */
317         return (0);
318 }                               /* ergo_writebootseq */
319
320 /***********************************************************************************/
321 /* ergo_waitpofready waits for a maximum of 10 seconds for the completition of the */
322 /* boot process. If the process has been successful 0 is returned otherwise a     */
323 /* negative error code is returned.                                                */
324 /***********************************************************************************/
325 static int
326 ergo_waitpofready(struct HYSDN_CARD *card)
327 {
328         tErgDpram *dpr = card->dpram;   /* pointer to DPRAM structure */
329         int timecnt = 10000 / 50;       /* timeout is 10 secs max. */
330         unsigned long flags;
331         int msg_size;
332         int i;
333
334         if (card->debug_flags & LOG_POF_CARD)
335                 hysdn_addlog(card, "ERGO: waiting for pof ready");
336         while (timecnt--) {
337                 /* wait until timeout  */
338
339                 if (dpr->ToPcFlag) {
340                         /* data has arrived */
341
342                         if ((dpr->ToPcChannel != CHAN_SYSTEM) ||
343                             (dpr->ToPcSize < MIN_RDY_MSG_SIZE) ||
344                             (dpr->ToPcSize > MAX_RDY_MSG_SIZE) ||
345                             ((*(unsigned long *) dpr->ToPcBuf) != RDY_MAGIC))
346                                 break;  /* an error occurred */
347
348                         /* Check for additional data delivered during SysReady */
349                         msg_size = dpr->ToPcSize - RDY_MAGIC_SIZE;
350                         if (msg_size > 0)
351                                 if (EvalSysrTokData(card, dpr->ToPcBuf + RDY_MAGIC_SIZE, msg_size))
352                                         break;
353
354                         if (card->debug_flags & LOG_POF_RECORD)
355                                 hysdn_addlog(card, "ERGO: pof boot success");
356                         spin_lock_irqsave(&card->hysdn_lock, flags);
357
358                         card->state = CARD_STATE_RUN;   /* now card is running */
359                         /* enable the cards interrupt */
360                         byteout(card->iobase + PCI9050_INTR_REG,
361                                 bytein(card->iobase + PCI9050_INTR_REG) |
362                         (PCI9050_INTR_REG_ENPCI | PCI9050_INTR_REG_EN1));
363                         card->irq_enabled = 1;  /* we are ready to receive interrupts */
364
365                         dpr->ToPcFlag = 0;      /* reset data indicator */
366                         dpr->ToHyInt = 1;
367                         dpr->ToPcInt = 1;       /* interrupt to E1 for all cards */
368
369                         spin_unlock_irqrestore(&card->hysdn_lock, flags);
370                         if ((hynet_enable & (1 << card->myid)) 
371                             && (i = hysdn_net_create(card))) 
372                         {
373                                 ergo_stopcard(card);
374                                 card->state = CARD_STATE_BOOTERR;
375                                 return (i);
376                         }
377 #ifdef CONFIG_HYSDN_CAPI
378                         if((i = hycapi_capi_create(card))) {
379                                 printk(KERN_WARNING "HYSDN: failed to create capi-interface.\n");
380                         }
381 #endif /* CONFIG_HYSDN_CAPI */
382                         return (0);     /* success */
383                 }               /* data has arrived */
384                 sti();
385                 msleep_interruptible(50);               /* Timeout 50ms */
386         }                       /* wait until timeout */
387
388         if (card->debug_flags & LOG_POF_CARD)
389                 hysdn_addlog(card, "ERGO: pof boot ready timeout");
390         return (-ERR_POF_TIMEOUT);
391 }                               /* ergo_waitpofready */
392
393
394
395 /************************************************************************************/
396 /* release the cards hardware. Before releasing do a interrupt disable and hardware */
397 /* reset. Also unmap dpram.                                                         */
398 /* Use only during module release.                                                  */
399 /************************************************************************************/
400 static void
401 ergo_releasehardware(hysdn_card * card)
402 {
403         ergo_stopcard(card);    /* first stop the card if not already done */
404         free_irq(card->irq, card);      /* release interrupt */
405         release_region(card->iobase + PCI9050_INTR_REG, 1);     /* release all io ports */
406         release_region(card->iobase + PCI9050_USER_IO, 1);
407         iounmap(card->dpram);
408         card->dpram = NULL;     /* release shared mem */
409 }                               /* ergo_releasehardware */
410
411
412 /*********************************************************************************/
413 /* acquire the needed hardware ports and map dpram. If an error occurs a nonzero */
414 /* value is returned.                                                            */
415 /* Use only during module init.                                                  */
416 /*********************************************************************************/
417 int
418 ergo_inithardware(hysdn_card * card)
419 {
420         if (!request_region(card->iobase + PCI9050_INTR_REG, 1, "HYSDN")) 
421                 return (-1);
422         if (!request_region(card->iobase + PCI9050_USER_IO, 1, "HYSDN")) {
423                 release_region(card->iobase + PCI9050_INTR_REG, 1);
424                 return (-1);    /* ports already in use */
425         }
426         card->memend = card->membase + ERG_DPRAM_PAGE_SIZE - 1;
427         if (!(card->dpram = ioremap(card->membase, ERG_DPRAM_PAGE_SIZE))) {
428                 release_region(card->iobase + PCI9050_INTR_REG, 1);
429                 release_region(card->iobase + PCI9050_USER_IO, 1);
430                 return (-1);
431         }
432
433         ergo_stopcard(card);    /* disable interrupts */
434         if (request_irq(card->irq, ergo_interrupt, IRQF_SHARED, "HYSDN", card)) {
435                 ergo_releasehardware(card); /* return the acquired hardware */
436                 return (-1);
437         }
438         /* success, now setup the function pointers */
439         card->stopcard = ergo_stopcard;
440         card->releasehardware = ergo_releasehardware;
441         card->testram = ergo_testram;
442         card->writebootimg = ergo_writebootimg;
443         card->writebootseq = ergo_writebootseq;
444         card->waitpofready = ergo_waitpofready;
445         card->set_errlog_state = ergo_set_errlog_state;
446         INIT_WORK(&card->irq_queue, ergo_irq_bh);
447         card->hysdn_lock = SPIN_LOCK_UNLOCKED;
448
449         return (0);
450 }                               /* ergo_inithardware */