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