boardergo.c 16 KB

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