/* $Id: boardergo.c,v 1.5.6.7 2001/11/06 21:58:19 kai Exp $1*2* Linux driver for HYSDN cards, specific routines for ergo type boards.3*4* Author Werner Cornelius ([email protected]) for Hypercope GmbH5* Copyright 1999 by Werner Cornelius ([email protected])6*7* This software may be used and distributed according to the terms8* of the GNU General Public License, incorporated herein by reference.9*10* As all Linux supported cards Champ2, Ergo and Metro2/4 use the same11* DPRAM interface and layout with only minor differences all related12* stuff is done here, not in separate modules.13*14*/1516#include <linux/signal.h>17#include <linux/kernel.h>18#include <linux/ioport.h>19#include <linux/interrupt.h>20#include <linux/vmalloc.h>21#include <linux/delay.h>22#include <asm/io.h>2324#include "hysdn_defs.h"25#include "boardergo.h"2627#define byteout(addr,val) outb(val,addr)28#define bytein(addr) inb(addr)2930/***************************************************/31/* The cards interrupt handler. Called from system */32/***************************************************/33static irqreturn_t34ergo_interrupt(int intno, void *dev_id)35{36hysdn_card *card = dev_id; /* parameter from irq */37tErgDpram *dpr;38unsigned long flags;39unsigned char volatile b;4041if (!card)42return IRQ_NONE; /* error -> spurious interrupt */43if (!card->irq_enabled)44return IRQ_NONE; /* other device interrupting or irq switched off */4546spin_lock_irqsave(&card->hysdn_lock, flags); /* no further irqs allowed */4748if (!(bytein(card->iobase + PCI9050_INTR_REG) & PCI9050_INTR_REG_STAT1)) {49spin_unlock_irqrestore(&card->hysdn_lock, flags); /* restore old state */50return IRQ_NONE; /* no interrupt requested by E1 */51}52/* clear any pending ints on the board */53dpr = card->dpram;54b = dpr->ToPcInt; /* clear for ergo */55b |= dpr->ToPcIntMetro; /* same for metro */56b |= dpr->ToHyInt; /* and for champ */5758/* start kernel task immediately after leaving all interrupts */59if (!card->hw_lock)60schedule_work(&card->irq_queue);61spin_unlock_irqrestore(&card->hysdn_lock, flags);62return IRQ_HANDLED;63} /* ergo_interrupt */6465/******************************************************************************/66/* ergo_irq_bh will be called as part of the kernel clearing its shared work */67/* queue sometime after a call to schedule_work has been made passing our */68/* work_struct. This task is the only one handling data transfer from or to */69/* the card after booting. The task may be queued from everywhere */70/* (interrupts included). */71/******************************************************************************/72static void73ergo_irq_bh(struct work_struct *ugli_api)74{75hysdn_card * card = container_of(ugli_api, hysdn_card, irq_queue);76tErgDpram *dpr;77int again;78unsigned long flags;7980if (card->state != CARD_STATE_RUN)81return; /* invalid call */8283dpr = card->dpram; /* point to DPRAM */8485spin_lock_irqsave(&card->hysdn_lock, flags);86if (card->hw_lock) {87spin_unlock_irqrestore(&card->hysdn_lock, flags); /* hardware currently unavailable */88return;89}90card->hw_lock = 1; /* we now lock the hardware */9192do {93again = 0; /* assume loop not to be repeated */9495if (!dpr->ToHyFlag) {96/* we are able to send a buffer */9798if (hysdn_sched_tx(card, dpr->ToHyBuf, &dpr->ToHySize, &dpr->ToHyChannel,99ERG_TO_HY_BUF_SIZE)) {100dpr->ToHyFlag = 1; /* enable tx */101again = 1; /* restart loop */102}103} /* we are able to send a buffer */104if (dpr->ToPcFlag) {105/* a message has arrived for us, handle it */106107if (hysdn_sched_rx(card, dpr->ToPcBuf, dpr->ToPcSize, dpr->ToPcChannel)) {108dpr->ToPcFlag = 0; /* we worked the data */109again = 1; /* restart loop */110}111} /* a message has arrived for us */112if (again) {113dpr->ToHyInt = 1;114dpr->ToPcInt = 1; /* interrupt to E1 for all cards */115} else116card->hw_lock = 0; /* free hardware again */117} while (again); /* until nothing more to do */118119spin_unlock_irqrestore(&card->hysdn_lock, flags);120} /* ergo_irq_bh */121122123/*********************************************************/124/* stop the card (hardware reset) and disable interrupts */125/*********************************************************/126static void127ergo_stopcard(hysdn_card * card)128{129unsigned long flags;130unsigned char val;131132hysdn_net_release(card); /* first release the net device if existing */133#ifdef CONFIG_HYSDN_CAPI134hycapi_capi_stop(card);135#endif /* CONFIG_HYSDN_CAPI */136spin_lock_irqsave(&card->hysdn_lock, flags);137val = bytein(card->iobase + PCI9050_INTR_REG); /* get actual value */138val &= ~(PCI9050_INTR_REG_ENPCI | PCI9050_INTR_REG_EN1); /* mask irq */139byteout(card->iobase + PCI9050_INTR_REG, val);140card->irq_enabled = 0;141byteout(card->iobase + PCI9050_USER_IO, PCI9050_E1_RESET); /* reset E1 processor */142card->state = CARD_STATE_UNUSED;143card->err_log_state = ERRLOG_STATE_OFF; /* currently no log active */144145spin_unlock_irqrestore(&card->hysdn_lock, flags);146} /* ergo_stopcard */147148/**************************************************************************/149/* enable or disable the cards error log. The event is queued if possible */150/**************************************************************************/151static void152ergo_set_errlog_state(hysdn_card * card, int on)153{154unsigned long flags;155156if (card->state != CARD_STATE_RUN) {157card->err_log_state = ERRLOG_STATE_OFF; /* must be off */158return;159}160spin_lock_irqsave(&card->hysdn_lock, flags);161162if (((card->err_log_state == ERRLOG_STATE_OFF) && !on) ||163((card->err_log_state == ERRLOG_STATE_ON) && on)) {164spin_unlock_irqrestore(&card->hysdn_lock, flags);165return; /* nothing to do */166}167if (on)168card->err_log_state = ERRLOG_STATE_START; /* request start */169else170card->err_log_state = ERRLOG_STATE_STOP; /* request stop */171172spin_unlock_irqrestore(&card->hysdn_lock, flags);173schedule_work(&card->irq_queue);174} /* ergo_set_errlog_state */175176/******************************************/177/* test the cards RAM and return 0 if ok. */178/******************************************/179static const char TestText[36] = "This Message is filler, why read it";180181static int182ergo_testram(hysdn_card * card)183{184tErgDpram *dpr = card->dpram;185186memset(dpr->TrapTable, 0, sizeof(dpr->TrapTable)); /* clear all Traps */187dpr->ToHyInt = 1; /* E1 INTR state forced */188189memcpy(&dpr->ToHyBuf[ERG_TO_HY_BUF_SIZE - sizeof(TestText)], TestText,190sizeof(TestText));191if (memcmp(&dpr->ToHyBuf[ERG_TO_HY_BUF_SIZE - sizeof(TestText)], TestText,192sizeof(TestText)))193return (-1);194195memcpy(&dpr->ToPcBuf[ERG_TO_PC_BUF_SIZE - sizeof(TestText)], TestText,196sizeof(TestText));197if (memcmp(&dpr->ToPcBuf[ERG_TO_PC_BUF_SIZE - sizeof(TestText)], TestText,198sizeof(TestText)))199return (-1);200201return (0);202} /* ergo_testram */203204/*****************************************************************************/205/* this function is intended to write stage 1 boot image to the cards buffer */206/* this is done in two steps. First the 1024 hi-words are written (offs=0), */207/* then the 1024 lo-bytes are written. The remaining DPRAM is cleared, the */208/* PCI-write-buffers flushed and the card is taken out of reset. */209/* The function then waits for a reaction of the E1 processor or a timeout. */210/* Negative return values are interpreted as errors. */211/*****************************************************************************/212static int213ergo_writebootimg(struct HYSDN_CARD *card, unsigned char *buf,214unsigned long offs)215{216unsigned char *dst;217tErgDpram *dpram;218int cnt = (BOOT_IMG_SIZE >> 2); /* number of words to move and swap (byte order!) */219220if (card->debug_flags & LOG_POF_CARD)221hysdn_addlog(card, "ERGO: write bootldr offs=0x%lx ", offs);222223dst = card->dpram; /* pointer to start of DPRAM */224dst += (offs + ERG_DPRAM_FILL_SIZE); /* offset in the DPRAM */225while (cnt--) {226*dst++ = *(buf + 1); /* high byte */227*dst++ = *buf; /* low byte */228dst += 2; /* point to next longword */229buf += 2; /* buffer only filled with words */230}231232/* if low words (offs = 2) have been written, clear the rest of the DPRAM, */233/* flush the PCI-write-buffer and take the E1 out of reset */234if (offs) {235memset(card->dpram, 0, ERG_DPRAM_FILL_SIZE); /* fill the DPRAM still not cleared */236dpram = card->dpram; /* get pointer to dpram structure */237dpram->ToHyNoDpramErrLog = 0xFF; /* write a dpram register */238while (!dpram->ToHyNoDpramErrLog); /* reread volatile register to flush PCI */239240byteout(card->iobase + PCI9050_USER_IO, PCI9050_E1_RUN); /* start E1 processor */241/* the interrupts are still masked */242243msleep_interruptible(20); /* Timeout 20ms */244245if (((tDpramBootSpooler *) card->dpram)->Len != DPRAM_SPOOLER_DATA_SIZE) {246if (card->debug_flags & LOG_POF_CARD)247hysdn_addlog(card, "ERGO: write bootldr no answer");248return (-ERR_BOOTIMG_FAIL);249}250} /* start_boot_img */251return (0); /* successful */252} /* ergo_writebootimg */253254/********************************************************************************/255/* ergo_writebootseq writes the buffer containing len bytes to the E1 processor */256/* using the boot spool mechanism. If everything works fine 0 is returned. In */257/* case of errors a negative error value is returned. */258/********************************************************************************/259static int260ergo_writebootseq(struct HYSDN_CARD *card, unsigned char *buf, int len)261{262tDpramBootSpooler *sp = (tDpramBootSpooler *) card->dpram;263unsigned char *dst;264unsigned char buflen;265int nr_write;266unsigned char tmp_rdptr;267unsigned char wr_mirror;268int i;269270if (card->debug_flags & LOG_POF_CARD)271hysdn_addlog(card, "ERGO: write boot seq len=%d ", len);272273dst = sp->Data; /* point to data in spool structure */274buflen = sp->Len; /* maximum len of spooled data */275wr_mirror = sp->WrPtr; /* only once read */276277/* try until all bytes written or error */278i = 0x1000; /* timeout value */279while (len) {280281/* first determine the number of bytes that may be buffered */282do {283tmp_rdptr = sp->RdPtr; /* first read the pointer */284i--; /* decrement timeout */285} while (i && (tmp_rdptr != sp->RdPtr)); /* wait for stable pointer */286287if (!i) {288if (card->debug_flags & LOG_POF_CARD)289hysdn_addlog(card, "ERGO: write boot seq timeout");290return (-ERR_BOOTSEQ_FAIL); /* value not stable -> timeout */291}292if ((nr_write = tmp_rdptr - wr_mirror - 1) < 0)293nr_write += buflen; /* now we got number of free bytes - 1 in buffer */294295if (!nr_write)296continue; /* no free bytes in buffer */297298if (nr_write > len)299nr_write = len; /* limit if last few bytes */300i = 0x1000; /* reset timeout value */301302/* now we know how much bytes we may put in the puffer */303len -= nr_write; /* we savely could adjust len before output */304while (nr_write--) {305*(dst + wr_mirror) = *buf++; /* output one byte */306if (++wr_mirror >= buflen)307wr_mirror = 0;308sp->WrPtr = wr_mirror; /* announce the next byte to E1 */309} /* while (nr_write) */310311} /* while (len) */312return (0);313} /* ergo_writebootseq */314315/***********************************************************************************/316/* ergo_waitpofready waits for a maximum of 10 seconds for the completition of the */317/* boot process. If the process has been successful 0 is returned otherwise a */318/* negative error code is returned. */319/***********************************************************************************/320static int321ergo_waitpofready(struct HYSDN_CARD *card)322{323tErgDpram *dpr = card->dpram; /* pointer to DPRAM structure */324int timecnt = 10000 / 50; /* timeout is 10 secs max. */325unsigned long flags;326int msg_size;327int i;328329if (card->debug_flags & LOG_POF_CARD)330hysdn_addlog(card, "ERGO: waiting for pof ready");331while (timecnt--) {332/* wait until timeout */333334if (dpr->ToPcFlag) {335/* data has arrived */336337if ((dpr->ToPcChannel != CHAN_SYSTEM) ||338(dpr->ToPcSize < MIN_RDY_MSG_SIZE) ||339(dpr->ToPcSize > MAX_RDY_MSG_SIZE) ||340((*(unsigned long *) dpr->ToPcBuf) != RDY_MAGIC))341break; /* an error occurred */342343/* Check for additional data delivered during SysReady */344msg_size = dpr->ToPcSize - RDY_MAGIC_SIZE;345if (msg_size > 0)346if (EvalSysrTokData(card, dpr->ToPcBuf + RDY_MAGIC_SIZE, msg_size))347break;348349if (card->debug_flags & LOG_POF_RECORD)350hysdn_addlog(card, "ERGO: pof boot success");351spin_lock_irqsave(&card->hysdn_lock, flags);352353card->state = CARD_STATE_RUN; /* now card is running */354/* enable the cards interrupt */355byteout(card->iobase + PCI9050_INTR_REG,356bytein(card->iobase + PCI9050_INTR_REG) |357(PCI9050_INTR_REG_ENPCI | PCI9050_INTR_REG_EN1));358card->irq_enabled = 1; /* we are ready to receive interrupts */359360dpr->ToPcFlag = 0; /* reset data indicator */361dpr->ToHyInt = 1;362dpr->ToPcInt = 1; /* interrupt to E1 for all cards */363364spin_unlock_irqrestore(&card->hysdn_lock, flags);365if ((hynet_enable & (1 << card->myid))366&& (i = hysdn_net_create(card)))367{368ergo_stopcard(card);369card->state = CARD_STATE_BOOTERR;370return (i);371}372#ifdef CONFIG_HYSDN_CAPI373if((i = hycapi_capi_create(card))) {374printk(KERN_WARNING "HYSDN: failed to create capi-interface.\n");375}376#endif /* CONFIG_HYSDN_CAPI */377return (0); /* success */378} /* data has arrived */379msleep_interruptible(50); /* Timeout 50ms */380} /* wait until timeout */381382if (card->debug_flags & LOG_POF_CARD)383hysdn_addlog(card, "ERGO: pof boot ready timeout");384return (-ERR_POF_TIMEOUT);385} /* ergo_waitpofready */386387388389/************************************************************************************/390/* release the cards hardware. Before releasing do a interrupt disable and hardware */391/* reset. Also unmap dpram. */392/* Use only during module release. */393/************************************************************************************/394static void395ergo_releasehardware(hysdn_card * card)396{397ergo_stopcard(card); /* first stop the card if not already done */398free_irq(card->irq, card); /* release interrupt */399release_region(card->iobase + PCI9050_INTR_REG, 1); /* release all io ports */400release_region(card->iobase + PCI9050_USER_IO, 1);401iounmap(card->dpram);402card->dpram = NULL; /* release shared mem */403} /* ergo_releasehardware */404405406/*********************************************************************************/407/* acquire the needed hardware ports and map dpram. If an error occurs a nonzero */408/* value is returned. */409/* Use only during module init. */410/*********************************************************************************/411int412ergo_inithardware(hysdn_card * card)413{414if (!request_region(card->iobase + PCI9050_INTR_REG, 1, "HYSDN"))415return (-1);416if (!request_region(card->iobase + PCI9050_USER_IO, 1, "HYSDN")) {417release_region(card->iobase + PCI9050_INTR_REG, 1);418return (-1); /* ports already in use */419}420card->memend = card->membase + ERG_DPRAM_PAGE_SIZE - 1;421if (!(card->dpram = ioremap(card->membase, ERG_DPRAM_PAGE_SIZE))) {422release_region(card->iobase + PCI9050_INTR_REG, 1);423release_region(card->iobase + PCI9050_USER_IO, 1);424return (-1);425}426427ergo_stopcard(card); /* disable interrupts */428if (request_irq(card->irq, ergo_interrupt, IRQF_SHARED, "HYSDN", card)) {429ergo_releasehardware(card); /* return the acquired hardware */430return (-1);431}432/* success, now setup the function pointers */433card->stopcard = ergo_stopcard;434card->releasehardware = ergo_releasehardware;435card->testram = ergo_testram;436card->writebootimg = ergo_writebootimg;437card->writebootseq = ergo_writebootseq;438card->waitpofready = ergo_waitpofready;439card->set_errlog_state = ergo_set_errlog_state;440INIT_WORK(&card->irq_queue, ergo_irq_bh);441spin_lock_init(&card->hysdn_lock);442443return (0);444} /* ergo_inithardware */445446447