boardergo.c 16 KB

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