b1dma.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  1. /* $Id: b1dma.c,v 1.1.2.3 2004/02/10 01:07:12 keil Exp $
  2. *
  3. * Common module for AVM B1 cards that support dma with AMCC
  4. *
  5. * Copyright 2000 by Carsten Paeth <calle@calle.de>
  6. *
  7. * This software may be used and distributed according to the terms
  8. * of the GNU General Public License, incorporated herein by reference.
  9. *
  10. */
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/proc_fs.h>
  14. #include <linux/seq_file.h>
  15. #include <linux/skbuff.h>
  16. #include <linux/delay.h>
  17. #include <linux/mm.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/ioport.h>
  20. #include <linux/capi.h>
  21. #include <linux/kernelcapi.h>
  22. #include <linux/gfp.h>
  23. #include <asm/io.h>
  24. #include <linux/init.h>
  25. #include <asm/uaccess.h>
  26. #include <linux/netdevice.h>
  27. #include <linux/isdn/capilli.h>
  28. #include "avmcard.h"
  29. #include <linux/isdn/capicmd.h>
  30. #include <linux/isdn/capiutil.h>
  31. static char *revision = "$Revision: 1.1.2.3 $";
  32. #undef AVM_B1DMA_DEBUG
  33. /* ------------------------------------------------------------- */
  34. MODULE_DESCRIPTION("CAPI4Linux: DMA support for active AVM cards");
  35. MODULE_AUTHOR("Carsten Paeth");
  36. MODULE_LICENSE("GPL");
  37. static int suppress_pollack = 0;
  38. module_param(suppress_pollack, bool, 0);
  39. /* ------------------------------------------------------------- */
  40. static void b1dma_dispatch_tx(avmcard *card);
  41. /* ------------------------------------------------------------- */
  42. /* S5933 */
  43. #define AMCC_RXPTR 0x24
  44. #define AMCC_RXLEN 0x28
  45. #define AMCC_TXPTR 0x2c
  46. #define AMCC_TXLEN 0x30
  47. #define AMCC_INTCSR 0x38
  48. # define EN_READ_TC_INT 0x00008000L
  49. # define EN_WRITE_TC_INT 0x00004000L
  50. # define EN_TX_TC_INT EN_READ_TC_INT
  51. # define EN_RX_TC_INT EN_WRITE_TC_INT
  52. # define AVM_FLAG 0x30000000L
  53. # define ANY_S5933_INT 0x00800000L
  54. # define READ_TC_INT 0x00080000L
  55. # define WRITE_TC_INT 0x00040000L
  56. # define TX_TC_INT READ_TC_INT
  57. # define RX_TC_INT WRITE_TC_INT
  58. # define MASTER_ABORT_INT 0x00100000L
  59. # define TARGET_ABORT_INT 0x00200000L
  60. # define BUS_MASTER_INT 0x00200000L
  61. # define ALL_INT 0x000C0000L
  62. #define AMCC_MCSR 0x3c
  63. # define A2P_HI_PRIORITY 0x00000100L
  64. # define EN_A2P_TRANSFERS 0x00000400L
  65. # define P2A_HI_PRIORITY 0x00001000L
  66. # define EN_P2A_TRANSFERS 0x00004000L
  67. # define RESET_A2P_FLAGS 0x04000000L
  68. # define RESET_P2A_FLAGS 0x02000000L
  69. /* ------------------------------------------------------------- */
  70. static inline void b1dma_writel(avmcard *card, u32 value, int off)
  71. {
  72. writel(value, card->mbase + off);
  73. }
  74. static inline u32 b1dma_readl(avmcard *card, int off)
  75. {
  76. return readl(card->mbase + off);
  77. }
  78. /* ------------------------------------------------------------- */
  79. static inline int b1dma_tx_empty(unsigned int port)
  80. {
  81. return inb(port + 0x03) & 0x1;
  82. }
  83. static inline int b1dma_rx_full(unsigned int port)
  84. {
  85. return inb(port + 0x02) & 0x1;
  86. }
  87. static int b1dma_tolink(avmcard *card, void *buf, unsigned int len)
  88. {
  89. unsigned long stop = jiffies + 1 * HZ; /* maximum wait time 1 sec */
  90. unsigned char *s = (unsigned char *)buf;
  91. while (len--) {
  92. while ( !b1dma_tx_empty(card->port)
  93. && time_before(jiffies, stop));
  94. if (!b1dma_tx_empty(card->port))
  95. return -1;
  96. t1outp(card->port, 0x01, *s++);
  97. }
  98. return 0;
  99. }
  100. static int b1dma_fromlink(avmcard *card, void *buf, unsigned int len)
  101. {
  102. unsigned long stop = jiffies + 1 * HZ; /* maximum wait time 1 sec */
  103. unsigned char *s = (unsigned char *)buf;
  104. while (len--) {
  105. while ( !b1dma_rx_full(card->port)
  106. && time_before(jiffies, stop));
  107. if (!b1dma_rx_full(card->port))
  108. return -1;
  109. *s++ = t1inp(card->port, 0x00);
  110. }
  111. return 0;
  112. }
  113. static int WriteReg(avmcard *card, u32 reg, u8 val)
  114. {
  115. u8 cmd = 0x00;
  116. if ( b1dma_tolink(card, &cmd, 1) == 0
  117. && b1dma_tolink(card, &reg, 4) == 0) {
  118. u32 tmp = val;
  119. return b1dma_tolink(card, &tmp, 4);
  120. }
  121. return -1;
  122. }
  123. static u8 ReadReg(avmcard *card, u32 reg)
  124. {
  125. u8 cmd = 0x01;
  126. if ( b1dma_tolink(card, &cmd, 1) == 0
  127. && b1dma_tolink(card, &reg, 4) == 0) {
  128. u32 tmp;
  129. if (b1dma_fromlink(card, &tmp, 4) == 0)
  130. return (u8)tmp;
  131. }
  132. return 0xff;
  133. }
  134. /* ------------------------------------------------------------- */
  135. static inline void _put_byte(void **pp, u8 val)
  136. {
  137. u8 *s = *pp;
  138. *s++ = val;
  139. *pp = s;
  140. }
  141. static inline void _put_word(void **pp, u32 val)
  142. {
  143. u8 *s = *pp;
  144. *s++ = val & 0xff;
  145. *s++ = (val >> 8) & 0xff;
  146. *s++ = (val >> 16) & 0xff;
  147. *s++ = (val >> 24) & 0xff;
  148. *pp = s;
  149. }
  150. static inline void _put_slice(void **pp, unsigned char *dp, unsigned int len)
  151. {
  152. unsigned i = len;
  153. _put_word(pp, i);
  154. while (i-- > 0)
  155. _put_byte(pp, *dp++);
  156. }
  157. static inline u8 _get_byte(void **pp)
  158. {
  159. u8 *s = *pp;
  160. u8 val;
  161. val = *s++;
  162. *pp = s;
  163. return val;
  164. }
  165. static inline u32 _get_word(void **pp)
  166. {
  167. u8 *s = *pp;
  168. u32 val;
  169. val = *s++;
  170. val |= (*s++ << 8);
  171. val |= (*s++ << 16);
  172. val |= (*s++ << 24);
  173. *pp = s;
  174. return val;
  175. }
  176. static inline u32 _get_slice(void **pp, unsigned char *dp)
  177. {
  178. unsigned int len, i;
  179. len = i = _get_word(pp);
  180. while (i-- > 0) *dp++ = _get_byte(pp);
  181. return len;
  182. }
  183. /* ------------------------------------------------------------- */
  184. void b1dma_reset(avmcard *card)
  185. {
  186. card->csr = 0x0;
  187. b1dma_writel(card, card->csr, AMCC_INTCSR);
  188. b1dma_writel(card, 0, AMCC_MCSR);
  189. b1dma_writel(card, 0, AMCC_RXLEN);
  190. b1dma_writel(card, 0, AMCC_TXLEN);
  191. t1outp(card->port, 0x10, 0x00);
  192. t1outp(card->port, 0x07, 0x00);
  193. b1dma_writel(card, 0, AMCC_MCSR);
  194. mdelay(10);
  195. b1dma_writel(card, 0x0f000000, AMCC_MCSR); /* reset all */
  196. mdelay(10);
  197. b1dma_writel(card, 0, AMCC_MCSR);
  198. if (card->cardtype == avm_t1pci)
  199. mdelay(42);
  200. else
  201. mdelay(10);
  202. }
  203. /* ------------------------------------------------------------- */
  204. static int b1dma_detect(avmcard *card)
  205. {
  206. b1dma_writel(card, 0, AMCC_MCSR);
  207. mdelay(10);
  208. b1dma_writel(card, 0x0f000000, AMCC_MCSR); /* reset all */
  209. mdelay(10);
  210. b1dma_writel(card, 0, AMCC_MCSR);
  211. mdelay(42);
  212. b1dma_writel(card, 0, AMCC_RXLEN);
  213. b1dma_writel(card, 0, AMCC_TXLEN);
  214. card->csr = 0x0;
  215. b1dma_writel(card, card->csr, AMCC_INTCSR);
  216. if (b1dma_readl(card, AMCC_MCSR) != 0x000000E6)
  217. return 1;
  218. b1dma_writel(card, 0xffffffff, AMCC_RXPTR);
  219. b1dma_writel(card, 0xffffffff, AMCC_TXPTR);
  220. if ( b1dma_readl(card, AMCC_RXPTR) != 0xfffffffc
  221. || b1dma_readl(card, AMCC_TXPTR) != 0xfffffffc)
  222. return 2;
  223. b1dma_writel(card, 0x0, AMCC_RXPTR);
  224. b1dma_writel(card, 0x0, AMCC_TXPTR);
  225. if ( b1dma_readl(card, AMCC_RXPTR) != 0x0
  226. || b1dma_readl(card, AMCC_TXPTR) != 0x0)
  227. return 3;
  228. t1outp(card->port, 0x10, 0x00);
  229. t1outp(card->port, 0x07, 0x00);
  230. t1outp(card->port, 0x02, 0x02);
  231. t1outp(card->port, 0x03, 0x02);
  232. if ( (t1inp(card->port, 0x02) & 0xFE) != 0x02
  233. || t1inp(card->port, 0x3) != 0x03)
  234. return 4;
  235. t1outp(card->port, 0x02, 0x00);
  236. t1outp(card->port, 0x03, 0x00);
  237. if ( (t1inp(card->port, 0x02) & 0xFE) != 0x00
  238. || t1inp(card->port, 0x3) != 0x01)
  239. return 5;
  240. return 0;
  241. }
  242. int t1pci_detect(avmcard *card)
  243. {
  244. int ret;
  245. if ((ret = b1dma_detect(card)) != 0)
  246. return ret;
  247. /* Transputer test */
  248. if ( WriteReg(card, 0x80001000, 0x11) != 0
  249. || WriteReg(card, 0x80101000, 0x22) != 0
  250. || WriteReg(card, 0x80201000, 0x33) != 0
  251. || WriteReg(card, 0x80301000, 0x44) != 0)
  252. return 6;
  253. if ( ReadReg(card, 0x80001000) != 0x11
  254. || ReadReg(card, 0x80101000) != 0x22
  255. || ReadReg(card, 0x80201000) != 0x33
  256. || ReadReg(card, 0x80301000) != 0x44)
  257. return 7;
  258. if ( WriteReg(card, 0x80001000, 0x55) != 0
  259. || WriteReg(card, 0x80101000, 0x66) != 0
  260. || WriteReg(card, 0x80201000, 0x77) != 0
  261. || WriteReg(card, 0x80301000, 0x88) != 0)
  262. return 8;
  263. if ( ReadReg(card, 0x80001000) != 0x55
  264. || ReadReg(card, 0x80101000) != 0x66
  265. || ReadReg(card, 0x80201000) != 0x77
  266. || ReadReg(card, 0x80301000) != 0x88)
  267. return 9;
  268. return 0;
  269. }
  270. int b1pciv4_detect(avmcard *card)
  271. {
  272. int ret, i;
  273. if ((ret = b1dma_detect(card)) != 0)
  274. return ret;
  275. for (i=0; i < 5 ; i++) {
  276. if (WriteReg(card, 0x80A00000, 0x21) != 0)
  277. return 6;
  278. if ((ReadReg(card, 0x80A00000) & 0x01) != 0x01)
  279. return 7;
  280. }
  281. for (i=0; i < 5 ; i++) {
  282. if (WriteReg(card, 0x80A00000, 0x20) != 0)
  283. return 8;
  284. if ((ReadReg(card, 0x80A00000) & 0x01) != 0x00)
  285. return 9;
  286. }
  287. return 0;
  288. }
  289. static void b1dma_queue_tx(avmcard *card, struct sk_buff *skb)
  290. {
  291. unsigned long flags;
  292. spin_lock_irqsave(&card->lock, flags);
  293. skb_queue_tail(&card->dma->send_queue, skb);
  294. if (!(card->csr & EN_TX_TC_INT)) {
  295. b1dma_dispatch_tx(card);
  296. b1dma_writel(card, card->csr, AMCC_INTCSR);
  297. }
  298. spin_unlock_irqrestore(&card->lock, flags);
  299. }
  300. /* ------------------------------------------------------------- */
  301. static void b1dma_dispatch_tx(avmcard *card)
  302. {
  303. avmcard_dmainfo *dma = card->dma;
  304. struct sk_buff *skb;
  305. u8 cmd, subcmd;
  306. u16 len;
  307. u32 txlen;
  308. void *p;
  309. skb = skb_dequeue(&dma->send_queue);
  310. len = CAPIMSG_LEN(skb->data);
  311. if (len) {
  312. cmd = CAPIMSG_COMMAND(skb->data);
  313. subcmd = CAPIMSG_SUBCOMMAND(skb->data);
  314. p = dma->sendbuf.dmabuf;
  315. if (CAPICMD(cmd, subcmd) == CAPI_DATA_B3_REQ) {
  316. u16 dlen = CAPIMSG_DATALEN(skb->data);
  317. _put_byte(&p, SEND_DATA_B3_REQ);
  318. _put_slice(&p, skb->data, len);
  319. _put_slice(&p, skb->data + len, dlen);
  320. } else {
  321. _put_byte(&p, SEND_MESSAGE);
  322. _put_slice(&p, skb->data, len);
  323. }
  324. txlen = (u8 *)p - (u8 *)dma->sendbuf.dmabuf;
  325. #ifdef AVM_B1DMA_DEBUG
  326. printk(KERN_DEBUG "tx: put msg len=%d\n", txlen);
  327. #endif
  328. } else {
  329. txlen = skb->len-2;
  330. #ifdef AVM_B1DMA_POLLDEBUG
  331. if (skb->data[2] == SEND_POLLACK)
  332. printk(KERN_INFO "%s: send ack\n", card->name);
  333. #endif
  334. #ifdef AVM_B1DMA_DEBUG
  335. printk(KERN_DEBUG "tx: put 0x%x len=%d\n",
  336. skb->data[2], txlen);
  337. #endif
  338. skb_copy_from_linear_data_offset(skb, 2, dma->sendbuf.dmabuf,
  339. skb->len - 2);
  340. }
  341. txlen = (txlen + 3) & ~3;
  342. b1dma_writel(card, dma->sendbuf.dmaaddr, AMCC_TXPTR);
  343. b1dma_writel(card, txlen, AMCC_TXLEN);
  344. card->csr |= EN_TX_TC_INT;
  345. dev_kfree_skb_any(skb);
  346. }
  347. /* ------------------------------------------------------------- */
  348. static void queue_pollack(avmcard *card)
  349. {
  350. struct sk_buff *skb;
  351. void *p;
  352. skb = alloc_skb(3, GFP_ATOMIC);
  353. if (!skb) {
  354. printk(KERN_CRIT "%s: no memory, lost poll ack\n",
  355. card->name);
  356. return;
  357. }
  358. p = skb->data;
  359. _put_byte(&p, 0);
  360. _put_byte(&p, 0);
  361. _put_byte(&p, SEND_POLLACK);
  362. skb_put(skb, (u8 *)p - (u8 *)skb->data);
  363. b1dma_queue_tx(card, skb);
  364. }
  365. /* ------------------------------------------------------------- */
  366. static void b1dma_handle_rx(avmcard *card)
  367. {
  368. avmctrl_info *cinfo = &card->ctrlinfo[0];
  369. avmcard_dmainfo *dma = card->dma;
  370. struct capi_ctr *ctrl = &cinfo->capi_ctrl;
  371. struct sk_buff *skb;
  372. void *p = dma->recvbuf.dmabuf+4;
  373. u32 ApplId, MsgLen, DataB3Len, NCCI, WindowSize;
  374. u8 b1cmd = _get_byte(&p);
  375. #ifdef AVM_B1DMA_DEBUG
  376. printk(KERN_DEBUG "rx: 0x%x %lu\n", b1cmd, (unsigned long)dma->recvlen);
  377. #endif
  378. switch (b1cmd) {
  379. case RECEIVE_DATA_B3_IND:
  380. ApplId = (unsigned) _get_word(&p);
  381. MsgLen = _get_slice(&p, card->msgbuf);
  382. DataB3Len = _get_slice(&p, card->databuf);
  383. if (MsgLen < 30) { /* not CAPI 64Bit */
  384. memset(card->msgbuf+MsgLen, 0, 30-MsgLen);
  385. MsgLen = 30;
  386. CAPIMSG_SETLEN(card->msgbuf, 30);
  387. }
  388. if (!(skb = alloc_skb(DataB3Len+MsgLen, GFP_ATOMIC))) {
  389. printk(KERN_ERR "%s: incoming packet dropped\n",
  390. card->name);
  391. } else {
  392. memcpy(skb_put(skb, MsgLen), card->msgbuf, MsgLen);
  393. memcpy(skb_put(skb, DataB3Len), card->databuf, DataB3Len);
  394. capi_ctr_handle_message(ctrl, ApplId, skb);
  395. }
  396. break;
  397. case RECEIVE_MESSAGE:
  398. ApplId = (unsigned) _get_word(&p);
  399. MsgLen = _get_slice(&p, card->msgbuf);
  400. if (!(skb = alloc_skb(MsgLen, GFP_ATOMIC))) {
  401. printk(KERN_ERR "%s: incoming packet dropped\n",
  402. card->name);
  403. } else {
  404. memcpy(skb_put(skb, MsgLen), card->msgbuf, MsgLen);
  405. if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_CONF) {
  406. spin_lock(&card->lock);
  407. capilib_data_b3_conf(&cinfo->ncci_head, ApplId,
  408. CAPIMSG_NCCI(skb->data),
  409. CAPIMSG_MSGID(skb->data));
  410. spin_unlock(&card->lock);
  411. }
  412. capi_ctr_handle_message(ctrl, ApplId, skb);
  413. }
  414. break;
  415. case RECEIVE_NEW_NCCI:
  416. ApplId = _get_word(&p);
  417. NCCI = _get_word(&p);
  418. WindowSize = _get_word(&p);
  419. spin_lock(&card->lock);
  420. capilib_new_ncci(&cinfo->ncci_head, ApplId, NCCI, WindowSize);
  421. spin_unlock(&card->lock);
  422. break;
  423. case RECEIVE_FREE_NCCI:
  424. ApplId = _get_word(&p);
  425. NCCI = _get_word(&p);
  426. if (NCCI != 0xffffffff) {
  427. spin_lock(&card->lock);
  428. capilib_free_ncci(&cinfo->ncci_head, ApplId, NCCI);
  429. spin_unlock(&card->lock);
  430. }
  431. break;
  432. case RECEIVE_START:
  433. #ifdef AVM_B1DMA_POLLDEBUG
  434. printk(KERN_INFO "%s: receive poll\n", card->name);
  435. #endif
  436. if (!suppress_pollack)
  437. queue_pollack(card);
  438. capi_ctr_resume_output(ctrl);
  439. break;
  440. case RECEIVE_STOP:
  441. capi_ctr_suspend_output(ctrl);
  442. break;
  443. case RECEIVE_INIT:
  444. cinfo->versionlen = _get_slice(&p, cinfo->versionbuf);
  445. b1_parse_version(cinfo);
  446. printk(KERN_INFO "%s: %s-card (%s) now active\n",
  447. card->name,
  448. cinfo->version[VER_CARDTYPE],
  449. cinfo->version[VER_DRIVER]);
  450. capi_ctr_ready(ctrl);
  451. break;
  452. case RECEIVE_TASK_READY:
  453. ApplId = (unsigned) _get_word(&p);
  454. MsgLen = _get_slice(&p, card->msgbuf);
  455. card->msgbuf[MsgLen] = 0;
  456. while ( MsgLen > 0
  457. && ( card->msgbuf[MsgLen-1] == '\n'
  458. || card->msgbuf[MsgLen-1] == '\r')) {
  459. card->msgbuf[MsgLen-1] = 0;
  460. MsgLen--;
  461. }
  462. printk(KERN_INFO "%s: task %d \"%s\" ready.\n",
  463. card->name, ApplId, card->msgbuf);
  464. break;
  465. case RECEIVE_DEBUGMSG:
  466. MsgLen = _get_slice(&p, card->msgbuf);
  467. card->msgbuf[MsgLen] = 0;
  468. while ( MsgLen > 0
  469. && ( card->msgbuf[MsgLen-1] == '\n'
  470. || card->msgbuf[MsgLen-1] == '\r')) {
  471. card->msgbuf[MsgLen-1] = 0;
  472. MsgLen--;
  473. }
  474. printk(KERN_INFO "%s: DEBUG: %s\n", card->name, card->msgbuf);
  475. break;
  476. default:
  477. printk(KERN_ERR "%s: b1dma_interrupt: 0x%x ???\n",
  478. card->name, b1cmd);
  479. return;
  480. }
  481. }
  482. /* ------------------------------------------------------------- */
  483. static void b1dma_handle_interrupt(avmcard *card)
  484. {
  485. u32 status;
  486. u32 newcsr;
  487. spin_lock(&card->lock);
  488. status = b1dma_readl(card, AMCC_INTCSR);
  489. if ((status & ANY_S5933_INT) == 0) {
  490. spin_unlock(&card->lock);
  491. return;
  492. }
  493. newcsr = card->csr | (status & ALL_INT);
  494. if (status & TX_TC_INT) newcsr &= ~EN_TX_TC_INT;
  495. if (status & RX_TC_INT) newcsr &= ~EN_RX_TC_INT;
  496. b1dma_writel(card, newcsr, AMCC_INTCSR);
  497. if ((status & RX_TC_INT) != 0) {
  498. struct avmcard_dmainfo *dma = card->dma;
  499. u32 rxlen;
  500. if (card->dma->recvlen == 0) {
  501. rxlen = b1dma_readl(card, AMCC_RXLEN);
  502. if (rxlen == 0) {
  503. dma->recvlen = *((u32 *)dma->recvbuf.dmabuf);
  504. rxlen = (dma->recvlen + 3) & ~3;
  505. b1dma_writel(card, dma->recvbuf.dmaaddr+4, AMCC_RXPTR);
  506. b1dma_writel(card, rxlen, AMCC_RXLEN);
  507. #ifdef AVM_B1DMA_DEBUG
  508. } else {
  509. printk(KERN_ERR "%s: rx not complete (%d).\n",
  510. card->name, rxlen);
  511. #endif
  512. }
  513. } else {
  514. spin_unlock(&card->lock);
  515. b1dma_handle_rx(card);
  516. dma->recvlen = 0;
  517. spin_lock(&card->lock);
  518. b1dma_writel(card, dma->recvbuf.dmaaddr, AMCC_RXPTR);
  519. b1dma_writel(card, 4, AMCC_RXLEN);
  520. }
  521. }
  522. if ((status & TX_TC_INT) != 0) {
  523. if (skb_queue_empty(&card->dma->send_queue))
  524. card->csr &= ~EN_TX_TC_INT;
  525. else
  526. b1dma_dispatch_tx(card);
  527. }
  528. b1dma_writel(card, card->csr, AMCC_INTCSR);
  529. spin_unlock(&card->lock);
  530. }
  531. irqreturn_t b1dma_interrupt(int interrupt, void *devptr)
  532. {
  533. avmcard *card = devptr;
  534. b1dma_handle_interrupt(card);
  535. return IRQ_HANDLED;
  536. }
  537. /* ------------------------------------------------------------- */
  538. static int b1dma_loaded(avmcard *card)
  539. {
  540. unsigned long stop;
  541. unsigned char ans;
  542. unsigned long tout = 2;
  543. unsigned int base = card->port;
  544. for (stop = jiffies + tout * HZ; time_before(jiffies, stop);) {
  545. if (b1_tx_empty(base))
  546. break;
  547. }
  548. if (!b1_tx_empty(base)) {
  549. printk(KERN_ERR "%s: b1dma_loaded: tx err, corrupted t4 file ?\n",
  550. card->name);
  551. return 0;
  552. }
  553. b1_put_byte(base, SEND_POLLACK);
  554. for (stop = jiffies + tout * HZ; time_before(jiffies, stop);) {
  555. if (b1_rx_full(base)) {
  556. if ((ans = b1_get_byte(base)) == RECEIVE_POLLDWORD) {
  557. return 1;
  558. }
  559. printk(KERN_ERR "%s: b1dma_loaded: got 0x%x, firmware not running in dword mode\n", card->name, ans);
  560. return 0;
  561. }
  562. }
  563. printk(KERN_ERR "%s: b1dma_loaded: firmware not running\n", card->name);
  564. return 0;
  565. }
  566. /* ------------------------------------------------------------- */
  567. static void b1dma_send_init(avmcard *card)
  568. {
  569. struct sk_buff *skb;
  570. void *p;
  571. skb = alloc_skb(15, GFP_ATOMIC);
  572. if (!skb) {
  573. printk(KERN_CRIT "%s: no memory, lost register appl.\n",
  574. card->name);
  575. return;
  576. }
  577. p = skb->data;
  578. _put_byte(&p, 0);
  579. _put_byte(&p, 0);
  580. _put_byte(&p, SEND_INIT);
  581. _put_word(&p, CAPI_MAXAPPL);
  582. _put_word(&p, AVM_NCCI_PER_CHANNEL*30);
  583. _put_word(&p, card->cardnr - 1);
  584. skb_put(skb, (u8 *)p - (u8 *)skb->data);
  585. b1dma_queue_tx(card, skb);
  586. }
  587. int b1dma_load_firmware(struct capi_ctr *ctrl, capiloaddata *data)
  588. {
  589. avmctrl_info *cinfo = (avmctrl_info *)(ctrl->driverdata);
  590. avmcard *card = cinfo->card;
  591. int retval;
  592. b1dma_reset(card);
  593. if ((retval = b1_load_t4file(card, &data->firmware))) {
  594. b1dma_reset(card);
  595. printk(KERN_ERR "%s: failed to load t4file!!\n",
  596. card->name);
  597. return retval;
  598. }
  599. if (data->configuration.len > 0 && data->configuration.data) {
  600. if ((retval = b1_load_config(card, &data->configuration))) {
  601. b1dma_reset(card);
  602. printk(KERN_ERR "%s: failed to load config!!\n",
  603. card->name);
  604. return retval;
  605. }
  606. }
  607. if (!b1dma_loaded(card)) {
  608. b1dma_reset(card);
  609. printk(KERN_ERR "%s: failed to load t4file.\n", card->name);
  610. return -EIO;
  611. }
  612. card->csr = AVM_FLAG;
  613. b1dma_writel(card, card->csr, AMCC_INTCSR);
  614. b1dma_writel(card, EN_A2P_TRANSFERS|EN_P2A_TRANSFERS|A2P_HI_PRIORITY|
  615. P2A_HI_PRIORITY|RESET_A2P_FLAGS|RESET_P2A_FLAGS,
  616. AMCC_MCSR);
  617. t1outp(card->port, 0x07, 0x30);
  618. t1outp(card->port, 0x10, 0xF0);
  619. card->dma->recvlen = 0;
  620. b1dma_writel(card, card->dma->recvbuf.dmaaddr, AMCC_RXPTR);
  621. b1dma_writel(card, 4, AMCC_RXLEN);
  622. card->csr |= EN_RX_TC_INT;
  623. b1dma_writel(card, card->csr, AMCC_INTCSR);
  624. b1dma_send_init(card);
  625. return 0;
  626. }
  627. void b1dma_reset_ctr(struct capi_ctr *ctrl)
  628. {
  629. avmctrl_info *cinfo = (avmctrl_info *)(ctrl->driverdata);
  630. avmcard *card = cinfo->card;
  631. unsigned long flags;
  632. spin_lock_irqsave(&card->lock, flags);
  633. b1dma_reset(card);
  634. memset(cinfo->version, 0, sizeof(cinfo->version));
  635. capilib_release(&cinfo->ncci_head);
  636. spin_unlock_irqrestore(&card->lock, flags);
  637. capi_ctr_down(ctrl);
  638. }
  639. /* ------------------------------------------------------------- */
  640. void b1dma_register_appl(struct capi_ctr *ctrl,
  641. u16 appl,
  642. capi_register_params *rp)
  643. {
  644. avmctrl_info *cinfo = (avmctrl_info *)(ctrl->driverdata);
  645. avmcard *card = cinfo->card;
  646. struct sk_buff *skb;
  647. int want = rp->level3cnt;
  648. int nconn;
  649. void *p;
  650. if (want > 0) nconn = want;
  651. else nconn = ctrl->profile.nbchannel * -want;
  652. if (nconn == 0) nconn = ctrl->profile.nbchannel;
  653. skb = alloc_skb(23, GFP_ATOMIC);
  654. if (!skb) {
  655. printk(KERN_CRIT "%s: no memory, lost register appl.\n",
  656. card->name);
  657. return;
  658. }
  659. p = skb->data;
  660. _put_byte(&p, 0);
  661. _put_byte(&p, 0);
  662. _put_byte(&p, SEND_REGISTER);
  663. _put_word(&p, appl);
  664. _put_word(&p, 1024 * (nconn+1));
  665. _put_word(&p, nconn);
  666. _put_word(&p, rp->datablkcnt);
  667. _put_word(&p, rp->datablklen);
  668. skb_put(skb, (u8 *)p - (u8 *)skb->data);
  669. b1dma_queue_tx(card, skb);
  670. }
  671. /* ------------------------------------------------------------- */
  672. void b1dma_release_appl(struct capi_ctr *ctrl, u16 appl)
  673. {
  674. avmctrl_info *cinfo = (avmctrl_info *)(ctrl->driverdata);
  675. avmcard *card = cinfo->card;
  676. struct sk_buff *skb;
  677. void *p;
  678. unsigned long flags;
  679. spin_lock_irqsave(&card->lock, flags);
  680. capilib_release_appl(&cinfo->ncci_head, appl);
  681. spin_unlock_irqrestore(&card->lock, flags);
  682. skb = alloc_skb(7, GFP_ATOMIC);
  683. if (!skb) {
  684. printk(KERN_CRIT "%s: no memory, lost release appl.\n",
  685. card->name);
  686. return;
  687. }
  688. p = skb->data;
  689. _put_byte(&p, 0);
  690. _put_byte(&p, 0);
  691. _put_byte(&p, SEND_RELEASE);
  692. _put_word(&p, appl);
  693. skb_put(skb, (u8 *)p - (u8 *)skb->data);
  694. b1dma_queue_tx(card, skb);
  695. }
  696. /* ------------------------------------------------------------- */
  697. u16 b1dma_send_message(struct capi_ctr *ctrl, struct sk_buff *skb)
  698. {
  699. avmctrl_info *cinfo = (avmctrl_info *)(ctrl->driverdata);
  700. avmcard *card = cinfo->card;
  701. u16 retval = CAPI_NOERROR;
  702. if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
  703. unsigned long flags;
  704. spin_lock_irqsave(&card->lock, flags);
  705. retval = capilib_data_b3_req(&cinfo->ncci_head,
  706. CAPIMSG_APPID(skb->data),
  707. CAPIMSG_NCCI(skb->data),
  708. CAPIMSG_MSGID(skb->data));
  709. spin_unlock_irqrestore(&card->lock, flags);
  710. }
  711. if (retval == CAPI_NOERROR)
  712. b1dma_queue_tx(card, skb);
  713. return retval;
  714. }
  715. /* ------------------------------------------------------------- */
  716. static int b1dmactl_proc_show(struct seq_file *m, void *v)
  717. {
  718. struct capi_ctr *ctrl = m->private;
  719. avmctrl_info *cinfo = (avmctrl_info *)(ctrl->driverdata);
  720. avmcard *card = cinfo->card;
  721. u8 flag;
  722. char *s;
  723. u32 txoff, txlen, rxoff, rxlen, csr;
  724. unsigned long flags;
  725. seq_printf(m, "%-16s %s\n", "name", card->name);
  726. seq_printf(m, "%-16s 0x%x\n", "io", card->port);
  727. seq_printf(m, "%-16s %d\n", "irq", card->irq);
  728. seq_printf(m, "%-16s 0x%lx\n", "membase", card->membase);
  729. switch (card->cardtype) {
  730. case avm_b1isa: s = "B1 ISA"; break;
  731. case avm_b1pci: s = "B1 PCI"; break;
  732. case avm_b1pcmcia: s = "B1 PCMCIA"; break;
  733. case avm_m1: s = "M1"; break;
  734. case avm_m2: s = "M2"; break;
  735. case avm_t1isa: s = "T1 ISA (HEMA)"; break;
  736. case avm_t1pci: s = "T1 PCI"; break;
  737. case avm_c4: s = "C4"; break;
  738. case avm_c2: s = "C2"; break;
  739. default: s = "???"; break;
  740. }
  741. seq_printf(m, "%-16s %s\n", "type", s);
  742. if ((s = cinfo->version[VER_DRIVER]) != NULL)
  743. seq_printf(m, "%-16s %s\n", "ver_driver", s);
  744. if ((s = cinfo->version[VER_CARDTYPE]) != NULL)
  745. seq_printf(m, "%-16s %s\n", "ver_cardtype", s);
  746. if ((s = cinfo->version[VER_SERIAL]) != NULL)
  747. seq_printf(m, "%-16s %s\n", "ver_serial", s);
  748. if (card->cardtype != avm_m1) {
  749. flag = ((u8 *)(ctrl->profile.manu))[3];
  750. if (flag)
  751. seq_printf(m, "%-16s%s%s%s%s%s%s%s\n",
  752. "protocol",
  753. (flag & 0x01) ? " DSS1" : "",
  754. (flag & 0x02) ? " CT1" : "",
  755. (flag & 0x04) ? " VN3" : "",
  756. (flag & 0x08) ? " NI1" : "",
  757. (flag & 0x10) ? " AUSTEL" : "",
  758. (flag & 0x20) ? " ESS" : "",
  759. (flag & 0x40) ? " 1TR6" : ""
  760. );
  761. }
  762. if (card->cardtype != avm_m1) {
  763. flag = ((u8 *)(ctrl->profile.manu))[5];
  764. if (flag)
  765. seq_printf(m, "%-16s%s%s%s%s\n",
  766. "linetype",
  767. (flag & 0x01) ? " point to point" : "",
  768. (flag & 0x02) ? " point to multipoint" : "",
  769. (flag & 0x08) ? " leased line without D-channel" : "",
  770. (flag & 0x04) ? " leased line with D-channel" : ""
  771. );
  772. }
  773. seq_printf(m, "%-16s %s\n", "cardname", cinfo->cardname);
  774. spin_lock_irqsave(&card->lock, flags);
  775. txoff = (dma_addr_t)b1dma_readl(card, AMCC_TXPTR)-card->dma->sendbuf.dmaaddr;
  776. txlen = b1dma_readl(card, AMCC_TXLEN);
  777. rxoff = (dma_addr_t)b1dma_readl(card, AMCC_RXPTR)-card->dma->recvbuf.dmaaddr;
  778. rxlen = b1dma_readl(card, AMCC_RXLEN);
  779. csr = b1dma_readl(card, AMCC_INTCSR);
  780. spin_unlock_irqrestore(&card->lock, flags);
  781. seq_printf(m, "%-16s 0x%lx\n", "csr (cached)", (unsigned long)card->csr);
  782. seq_printf(m, "%-16s 0x%lx\n", "csr", (unsigned long)csr);
  783. seq_printf(m, "%-16s %lu\n", "txoff", (unsigned long)txoff);
  784. seq_printf(m, "%-16s %lu\n", "txlen", (unsigned long)txlen);
  785. seq_printf(m, "%-16s %lu\n", "rxoff", (unsigned long)rxoff);
  786. seq_printf(m, "%-16s %lu\n", "rxlen", (unsigned long)rxlen);
  787. return 0;
  788. }
  789. static int b1dmactl_proc_open(struct inode *inode, struct file *file)
  790. {
  791. return single_open(file, b1dmactl_proc_show, PDE(inode)->data);
  792. }
  793. const struct file_operations b1dmactl_proc_fops = {
  794. .owner = THIS_MODULE,
  795. .open = b1dmactl_proc_open,
  796. .read = seq_read,
  797. .llseek = seq_lseek,
  798. .release = single_release,
  799. };
  800. EXPORT_SYMBOL(b1dmactl_proc_fops);
  801. /* ------------------------------------------------------------- */
  802. EXPORT_SYMBOL(b1dma_reset);
  803. EXPORT_SYMBOL(t1pci_detect);
  804. EXPORT_SYMBOL(b1pciv4_detect);
  805. EXPORT_SYMBOL(b1dma_interrupt);
  806. EXPORT_SYMBOL(b1dma_load_firmware);
  807. EXPORT_SYMBOL(b1dma_reset_ctr);
  808. EXPORT_SYMBOL(b1dma_register_appl);
  809. EXPORT_SYMBOL(b1dma_release_appl);
  810. EXPORT_SYMBOL(b1dma_send_message);
  811. static int __init b1dma_init(void)
  812. {
  813. char *p;
  814. char rev[32];
  815. if ((p = strchr(revision, ':')) != NULL && p[1]) {
  816. strlcpy(rev, p + 2, sizeof(rev));
  817. if ((p = strchr(rev, '$')) != NULL && p > rev)
  818. *(p-1) = 0;
  819. } else
  820. strcpy(rev, "1.0");
  821. printk(KERN_INFO "b1dma: revision %s\n", rev);
  822. return 0;
  823. }
  824. static void __exit b1dma_exit(void)
  825. {
  826. }
  827. module_init(b1dma_init);
  828. module_exit(b1dma_exit);