hfc4s8s_l1.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584
  1. /*************************************************************************/
  2. /* $Id: hfc4s8s_l1.c,v 1.10 2005/02/09 16:31:09 martinb1 Exp $ */
  3. /* HFC-4S/8S low layer interface for Cologne Chip HFC-4S/8S isdn chips */
  4. /* The low layer (L1) is implemented as a loadable module for usage with */
  5. /* the HiSax isdn driver for passive cards. */
  6. /* */
  7. /* Author: Werner Cornelius */
  8. /* (C) 2003 Cornelius Consult (werner@cornelius-consult.de) */
  9. /* */
  10. /* Driver maintained by Cologne Chip */
  11. /* - Martin Bachem, support@colognechip.com */
  12. /* */
  13. /* This driver only works with chip revisions >= 1, older revision 0 */
  14. /* engineering samples (only first manufacturer sample cards) will not */
  15. /* work and are rejected by the driver. */
  16. /* */
  17. /* This file distributed under the GNU GPL. */
  18. /* */
  19. /* See Version History at the end of this file */
  20. /* */
  21. /*************************************************************************/
  22. #include <linux/module.h>
  23. #include <linux/init.h>
  24. #include <linux/pci.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/delay.h>
  27. #include <linux/slab.h>
  28. #include <linux/timer.h>
  29. #include <linux/skbuff.h>
  30. #include <linux/wait.h>
  31. #include <asm/io.h>
  32. #include "hisax_if.h"
  33. #include "hfc4s8s_l1.h"
  34. static const char hfc4s8s_rev[] = "Revision: 1.10";
  35. /***************************************************************/
  36. /* adjustable transparent mode fifo threshold */
  37. /* The value defines the used fifo threshold with the equation */
  38. /* */
  39. /* notify number of bytes = 2 * 2 ^ TRANS_FIFO_THRES */
  40. /* */
  41. /* The default value is 5 which results in a buffer size of 64 */
  42. /* and an interrupt rate of 8ms. */
  43. /* The maximum value is 7 due to fifo size restrictions. */
  44. /* Values below 3-4 are not recommended due to high interrupt */
  45. /* load of the processor. For non critical applications the */
  46. /* value should be raised to 7 to reduce any interrupt overhead*/
  47. /***************************************************************/
  48. #define TRANS_FIFO_THRES 5
  49. /*************/
  50. /* constants */
  51. /*************/
  52. #define CLOCKMODE_0 0 /* ext. 24.576 MhZ clk freq, int. single clock mode */
  53. #define CLOCKMODE_1 1 /* ext. 49.576 MhZ clk freq, int. single clock mode */
  54. #define CHIP_ID_SHIFT 4
  55. #define HFC_MAX_ST 8
  56. #define MAX_D_FRAME_SIZE 270
  57. #define MAX_B_FRAME_SIZE 1536
  58. #define TRANS_TIMER_MODE (TRANS_FIFO_THRES & 0xf)
  59. #define TRANS_FIFO_BYTES (2 << TRANS_FIFO_THRES)
  60. #define MAX_F_CNT 0x0f
  61. #define CLKDEL_NT 0x6c
  62. #define CLKDEL_TE 0xf
  63. #define CTRL0_NT 4
  64. #define CTRL0_TE 0
  65. #define L1_TIMER_T4 2 /* minimum in jiffies */
  66. #define L1_TIMER_T3 (7 * HZ) /* activation timeout */
  67. #define L1_TIMER_T1 ((120 * HZ) / 1000) /* NT mode deactivation timeout */
  68. /******************/
  69. /* types and vars */
  70. /******************/
  71. static int card_cnt;
  72. /* private driver_data */
  73. typedef struct {
  74. int chip_id;
  75. int clock_mode;
  76. int max_st_ports;
  77. char *device_name;
  78. } hfc4s8s_param;
  79. static struct pci_device_id hfc4s8s_ids[] = {
  80. {.vendor = PCI_VENDOR_ID_CCD,
  81. .device = PCI_DEVICE_ID_4S,
  82. .subvendor = 0x1397,
  83. .subdevice = 0x08b4,
  84. .driver_data =
  85. (unsigned long) &((hfc4s8s_param) {CHIP_ID_4S, CLOCKMODE_0, 4,
  86. "HFC-4S Evaluation Board"}),
  87. },
  88. {.vendor = PCI_VENDOR_ID_CCD,
  89. .device = PCI_DEVICE_ID_8S,
  90. .subvendor = 0x1397,
  91. .subdevice = 0x16b8,
  92. .driver_data =
  93. (unsigned long) &((hfc4s8s_param) {CHIP_ID_8S, CLOCKMODE_0, 8,
  94. "HFC-8S Evaluation Board"}),
  95. },
  96. {.vendor = PCI_VENDOR_ID_CCD,
  97. .device = PCI_DEVICE_ID_4S,
  98. .subvendor = 0x1397,
  99. .subdevice = 0xb520,
  100. .driver_data =
  101. (unsigned long) &((hfc4s8s_param) {CHIP_ID_4S, CLOCKMODE_1, 4,
  102. "IOB4ST"}),
  103. },
  104. {.vendor = PCI_VENDOR_ID_CCD,
  105. .device = PCI_DEVICE_ID_8S,
  106. .subvendor = 0x1397,
  107. .subdevice = 0xb522,
  108. .driver_data =
  109. (unsigned long) &((hfc4s8s_param) {CHIP_ID_8S, CLOCKMODE_1, 8,
  110. "IOB8ST"}),
  111. },
  112. {}
  113. };
  114. MODULE_DEVICE_TABLE(pci, hfc4s8s_ids);
  115. MODULE_AUTHOR("Werner Cornelius, werner@cornelius-consult.de");
  116. MODULE_DESCRIPTION("ISDN layer 1 for Cologne Chip HFC-4S/8S chips");
  117. MODULE_LICENSE("GPL");
  118. /***********/
  119. /* layer 1 */
  120. /***********/
  121. struct hfc4s8s_btype {
  122. spinlock_t lock;
  123. struct hisax_b_if b_if;
  124. struct hfc4s8s_l1 *l1p;
  125. struct sk_buff_head tx_queue;
  126. struct sk_buff *tx_skb;
  127. struct sk_buff *rx_skb;
  128. __u8 *rx_ptr;
  129. int tx_cnt;
  130. int bchan;
  131. int mode;
  132. };
  133. struct _hfc4s8s_hw;
  134. struct hfc4s8s_l1 {
  135. spinlock_t lock;
  136. struct _hfc4s8s_hw *hw; /* pointer to hardware area */
  137. int l1_state; /* actual l1 state */
  138. struct timer_list l1_timer; /* layer 1 timer structure */
  139. int nt_mode; /* set to nt mode */
  140. int st_num; /* own index */
  141. int enabled; /* interface is enabled */
  142. struct sk_buff_head d_tx_queue; /* send queue */
  143. int tx_cnt; /* bytes to send */
  144. struct hisax_d_if d_if; /* D-channel interface */
  145. struct hfc4s8s_btype b_ch[2]; /* B-channel data */
  146. struct hisax_b_if *b_table[2];
  147. };
  148. /**********************/
  149. /* hardware structure */
  150. /**********************/
  151. typedef struct _hfc4s8s_hw {
  152. spinlock_t lock;
  153. int cardnum;
  154. int ifnum;
  155. int iobase;
  156. int nt_mode;
  157. u_char *membase;
  158. u_char *hw_membase;
  159. void *pdev;
  160. int max_fifo;
  161. hfc4s8s_param driver_data;
  162. int irq;
  163. int fifo_sched_cnt;
  164. struct work_struct tqueue;
  165. struct hfc4s8s_l1 l1[HFC_MAX_ST];
  166. char card_name[60];
  167. struct {
  168. u_char r_irq_ctrl;
  169. u_char r_ctrl0;
  170. volatile u_char r_irq_statech; /* active isdn l1 status */
  171. u_char r_irqmsk_statchg; /* enabled isdn status ints */
  172. u_char r_irq_fifo_blx[8]; /* fifo status registers */
  173. u_char fifo_rx_trans_enables[8]; /* mask for enabled transparent rx fifos */
  174. u_char fifo_slow_timer_service[8]; /* mask for fifos needing slower timer service */
  175. volatile u_char r_irq_oview; /* contents of overview register */
  176. volatile u_char timer_irq;
  177. int timer_usg_cnt; /* number of channels using timer */
  178. } mr;
  179. } hfc4s8s_hw;
  180. /* inline functions io mapped */
  181. static inline void
  182. SetRegAddr(hfc4s8s_hw *a, u_char b)
  183. {
  184. outb(b, (a->iobase) + 4);
  185. }
  186. static inline u_char
  187. GetRegAddr(hfc4s8s_hw *a)
  188. {
  189. return (inb((volatile u_int) (a->iobase + 4)));
  190. }
  191. static inline void
  192. Write_hfc8(hfc4s8s_hw *a, u_char b, u_char c)
  193. {
  194. SetRegAddr(a, b);
  195. outb(c, a->iobase);
  196. }
  197. static inline void
  198. fWrite_hfc8(hfc4s8s_hw *a, u_char c)
  199. {
  200. outb(c, a->iobase);
  201. }
  202. static inline void
  203. fWrite_hfc32(hfc4s8s_hw *a, u_long c)
  204. {
  205. outl(c, a->iobase);
  206. }
  207. static inline u_char
  208. Read_hfc8(hfc4s8s_hw *a, u_char b)
  209. {
  210. SetRegAddr(a, b);
  211. return (inb((volatile u_int) a->iobase));
  212. }
  213. static inline u_char
  214. fRead_hfc8(hfc4s8s_hw *a)
  215. {
  216. return (inb((volatile u_int) a->iobase));
  217. }
  218. static inline u_short
  219. Read_hfc16(hfc4s8s_hw *a, u_char b)
  220. {
  221. SetRegAddr(a, b);
  222. return (inw((volatile u_int) a->iobase));
  223. }
  224. static inline u_long
  225. fRead_hfc32(hfc4s8s_hw *a)
  226. {
  227. return (inl((volatile u_int) a->iobase));
  228. }
  229. static inline void
  230. wait_busy(hfc4s8s_hw *a)
  231. {
  232. SetRegAddr(a, R_STATUS);
  233. while (inb((volatile u_int) a->iobase) & M_BUSY);
  234. }
  235. #define PCI_ENA_REGIO 0x01
  236. /******************************************************/
  237. /* function to read critical counter registers that */
  238. /* may be updated by the chip during read */
  239. /******************************************************/
  240. static u_char
  241. Read_hfc8_stable(hfc4s8s_hw *hw, int reg)
  242. {
  243. u_char ref8;
  244. u_char in8;
  245. ref8 = Read_hfc8(hw, reg);
  246. while (((in8 = Read_hfc8(hw, reg)) != ref8)) {
  247. ref8 = in8;
  248. }
  249. return in8;
  250. }
  251. static int
  252. Read_hfc16_stable(hfc4s8s_hw *hw, int reg)
  253. {
  254. int ref16;
  255. int in16;
  256. ref16 = Read_hfc16(hw, reg);
  257. while (((in16 = Read_hfc16(hw, reg)) != ref16)) {
  258. ref16 = in16;
  259. }
  260. return in16;
  261. }
  262. /*****************************/
  263. /* D-channel call from HiSax */
  264. /*****************************/
  265. static void
  266. dch_l2l1(struct hisax_d_if *iface, int pr, void *arg)
  267. {
  268. struct hfc4s8s_l1 *l1 = iface->ifc.priv;
  269. struct sk_buff *skb = (struct sk_buff *) arg;
  270. u_long flags;
  271. switch (pr) {
  272. case (PH_DATA | REQUEST):
  273. if (!l1->enabled) {
  274. dev_kfree_skb(skb);
  275. break;
  276. }
  277. spin_lock_irqsave(&l1->lock, flags);
  278. skb_queue_tail(&l1->d_tx_queue, skb);
  279. if ((skb_queue_len(&l1->d_tx_queue) == 1) &&
  280. (l1->tx_cnt <= 0)) {
  281. l1->hw->mr.r_irq_fifo_blx[l1->st_num] |=
  282. 0x10;
  283. spin_unlock_irqrestore(&l1->lock, flags);
  284. schedule_work(&l1->hw->tqueue);
  285. } else
  286. spin_unlock_irqrestore(&l1->lock, flags);
  287. break;
  288. case (PH_ACTIVATE | REQUEST):
  289. if (!l1->enabled)
  290. break;
  291. if (!l1->nt_mode) {
  292. if (l1->l1_state < 6) {
  293. spin_lock_irqsave(&l1->lock,
  294. flags);
  295. Write_hfc8(l1->hw, R_ST_SEL,
  296. l1->st_num);
  297. Write_hfc8(l1->hw, A_ST_WR_STA,
  298. 0x60);
  299. mod_timer(&l1->l1_timer,
  300. jiffies + L1_TIMER_T3);
  301. spin_unlock_irqrestore(&l1->lock,
  302. flags);
  303. } else if (l1->l1_state == 7)
  304. l1->d_if.ifc.l1l2(&l1->d_if.ifc,
  305. PH_ACTIVATE |
  306. INDICATION,
  307. NULL);
  308. } else {
  309. if (l1->l1_state != 3) {
  310. spin_lock_irqsave(&l1->lock,
  311. flags);
  312. Write_hfc8(l1->hw, R_ST_SEL,
  313. l1->st_num);
  314. Write_hfc8(l1->hw, A_ST_WR_STA,
  315. 0x60);
  316. spin_unlock_irqrestore(&l1->lock,
  317. flags);
  318. } else if (l1->l1_state == 3)
  319. l1->d_if.ifc.l1l2(&l1->d_if.ifc,
  320. PH_ACTIVATE |
  321. INDICATION,
  322. NULL);
  323. }
  324. break;
  325. default:
  326. printk(KERN_INFO
  327. "HFC-4S/8S: Unknown D-chan cmd 0x%x received, ignored\n",
  328. pr);
  329. break;
  330. }
  331. if (!l1->enabled)
  332. l1->d_if.ifc.l1l2(&l1->d_if.ifc,
  333. PH_DEACTIVATE | INDICATION, NULL);
  334. } /* dch_l2l1 */
  335. /*****************************/
  336. /* B-channel call from HiSax */
  337. /*****************************/
  338. static void
  339. bch_l2l1(struct hisax_if *ifc, int pr, void *arg)
  340. {
  341. struct hfc4s8s_btype *bch = ifc->priv;
  342. struct hfc4s8s_l1 *l1 = bch->l1p;
  343. struct sk_buff *skb = (struct sk_buff *) arg;
  344. long mode = (long) arg;
  345. u_long flags;
  346. switch (pr) {
  347. case (PH_DATA | REQUEST):
  348. if (!l1->enabled || (bch->mode == L1_MODE_NULL)) {
  349. dev_kfree_skb(skb);
  350. break;
  351. }
  352. spin_lock_irqsave(&l1->lock, flags);
  353. skb_queue_tail(&bch->tx_queue, skb);
  354. if (!bch->tx_skb && (bch->tx_cnt <= 0)) {
  355. l1->hw->mr.r_irq_fifo_blx[l1->st_num] |=
  356. ((bch->bchan == 1) ? 1 : 4);
  357. spin_unlock_irqrestore(&l1->lock, flags);
  358. schedule_work(&l1->hw->tqueue);
  359. } else
  360. spin_unlock_irqrestore(&l1->lock, flags);
  361. break;
  362. case (PH_ACTIVATE | REQUEST):
  363. case (PH_DEACTIVATE | REQUEST):
  364. if (!l1->enabled)
  365. break;
  366. if (pr == (PH_DEACTIVATE | REQUEST))
  367. mode = L1_MODE_NULL;
  368. switch (mode) {
  369. case L1_MODE_HDLC:
  370. spin_lock_irqsave(&l1->lock,
  371. flags);
  372. l1->hw->mr.timer_usg_cnt++;
  373. l1->hw->mr.
  374. fifo_slow_timer_service[l1->
  375. st_num]
  376. |=
  377. ((bch->bchan ==
  378. 1) ? 0x2 : 0x8);
  379. Write_hfc8(l1->hw, R_FIFO,
  380. (l1->st_num * 8 +
  381. ((bch->bchan ==
  382. 1) ? 0 : 2)));
  383. wait_busy(l1->hw);
  384. Write_hfc8(l1->hw, A_CON_HDLC, 0xc); /* HDLC mode, flag fill, connect ST */
  385. Write_hfc8(l1->hw, A_SUBCH_CFG, 0); /* 8 bits */
  386. Write_hfc8(l1->hw, A_IRQ_MSK, 1); /* enable TX interrupts for hdlc */
  387. Write_hfc8(l1->hw, A_INC_RES_FIFO, 2); /* reset fifo */
  388. wait_busy(l1->hw);
  389. Write_hfc8(l1->hw, R_FIFO,
  390. (l1->st_num * 8 +
  391. ((bch->bchan ==
  392. 1) ? 1 : 3)));
  393. wait_busy(l1->hw);
  394. Write_hfc8(l1->hw, A_CON_HDLC, 0xc); /* HDLC mode, flag fill, connect ST */
  395. Write_hfc8(l1->hw, A_SUBCH_CFG, 0); /* 8 bits */
  396. Write_hfc8(l1->hw, A_IRQ_MSK, 1); /* enable RX interrupts for hdlc */
  397. Write_hfc8(l1->hw, A_INC_RES_FIFO, 2); /* reset fifo */
  398. Write_hfc8(l1->hw, R_ST_SEL,
  399. l1->st_num);
  400. l1->hw->mr.r_ctrl0 |=
  401. (bch->bchan & 3);
  402. Write_hfc8(l1->hw, A_ST_CTRL0,
  403. l1->hw->mr.r_ctrl0);
  404. bch->mode = L1_MODE_HDLC;
  405. spin_unlock_irqrestore(&l1->lock,
  406. flags);
  407. bch->b_if.ifc.l1l2(&bch->b_if.ifc,
  408. PH_ACTIVATE |
  409. INDICATION,
  410. NULL);
  411. break;
  412. case L1_MODE_TRANS:
  413. spin_lock_irqsave(&l1->lock,
  414. flags);
  415. l1->hw->mr.
  416. fifo_rx_trans_enables[l1->
  417. st_num]
  418. |=
  419. ((bch->bchan ==
  420. 1) ? 0x2 : 0x8);
  421. l1->hw->mr.timer_usg_cnt++;
  422. Write_hfc8(l1->hw, R_FIFO,
  423. (l1->st_num * 8 +
  424. ((bch->bchan ==
  425. 1) ? 0 : 2)));
  426. wait_busy(l1->hw);
  427. Write_hfc8(l1->hw, A_CON_HDLC, 0xf); /* Transparent mode, 1 fill, connect ST */
  428. Write_hfc8(l1->hw, A_SUBCH_CFG, 0); /* 8 bits */
  429. Write_hfc8(l1->hw, A_IRQ_MSK, 0); /* disable TX interrupts */
  430. Write_hfc8(l1->hw, A_INC_RES_FIFO, 2); /* reset fifo */
  431. wait_busy(l1->hw);
  432. Write_hfc8(l1->hw, R_FIFO,
  433. (l1->st_num * 8 +
  434. ((bch->bchan ==
  435. 1) ? 1 : 3)));
  436. wait_busy(l1->hw);
  437. Write_hfc8(l1->hw, A_CON_HDLC, 0xf); /* Transparent mode, 1 fill, connect ST */
  438. Write_hfc8(l1->hw, A_SUBCH_CFG, 0); /* 8 bits */
  439. Write_hfc8(l1->hw, A_IRQ_MSK, 0); /* disable RX interrupts */
  440. Write_hfc8(l1->hw, A_INC_RES_FIFO, 2); /* reset fifo */
  441. Write_hfc8(l1->hw, R_ST_SEL,
  442. l1->st_num);
  443. l1->hw->mr.r_ctrl0 |=
  444. (bch->bchan & 3);
  445. Write_hfc8(l1->hw, A_ST_CTRL0,
  446. l1->hw->mr.r_ctrl0);
  447. bch->mode = L1_MODE_TRANS;
  448. spin_unlock_irqrestore(&l1->lock,
  449. flags);
  450. bch->b_if.ifc.l1l2(&bch->b_if.ifc,
  451. PH_ACTIVATE |
  452. INDICATION,
  453. NULL);
  454. break;
  455. default:
  456. if (bch->mode == L1_MODE_NULL)
  457. break;
  458. spin_lock_irqsave(&l1->lock,
  459. flags);
  460. l1->hw->mr.
  461. fifo_slow_timer_service[l1->
  462. st_num]
  463. &=
  464. ~((bch->bchan ==
  465. 1) ? 0x3 : 0xc);
  466. l1->hw->mr.
  467. fifo_rx_trans_enables[l1->
  468. st_num]
  469. &=
  470. ~((bch->bchan ==
  471. 1) ? 0x3 : 0xc);
  472. l1->hw->mr.timer_usg_cnt--;
  473. Write_hfc8(l1->hw, R_FIFO,
  474. (l1->st_num * 8 +
  475. ((bch->bchan ==
  476. 1) ? 0 : 2)));
  477. wait_busy(l1->hw);
  478. Write_hfc8(l1->hw, A_IRQ_MSK, 0); /* disable TX interrupts */
  479. wait_busy(l1->hw);
  480. Write_hfc8(l1->hw, R_FIFO,
  481. (l1->st_num * 8 +
  482. ((bch->bchan ==
  483. 1) ? 1 : 3)));
  484. wait_busy(l1->hw);
  485. Write_hfc8(l1->hw, A_IRQ_MSK, 0); /* disable RX interrupts */
  486. Write_hfc8(l1->hw, R_ST_SEL,
  487. l1->st_num);
  488. l1->hw->mr.r_ctrl0 &=
  489. ~(bch->bchan & 3);
  490. Write_hfc8(l1->hw, A_ST_CTRL0,
  491. l1->hw->mr.r_ctrl0);
  492. spin_unlock_irqrestore(&l1->lock,
  493. flags);
  494. bch->mode = L1_MODE_NULL;
  495. bch->b_if.ifc.l1l2(&bch->b_if.ifc,
  496. PH_DEACTIVATE |
  497. INDICATION,
  498. NULL);
  499. if (bch->tx_skb) {
  500. dev_kfree_skb(bch->tx_skb);
  501. bch->tx_skb = NULL;
  502. }
  503. if (bch->rx_skb) {
  504. dev_kfree_skb(bch->rx_skb);
  505. bch->rx_skb = NULL;
  506. }
  507. skb_queue_purge(&bch->tx_queue);
  508. bch->tx_cnt = 0;
  509. bch->rx_ptr = NULL;
  510. break;
  511. }
  512. /* timer is only used when at least one b channel */
  513. /* is set up to transparent mode */
  514. if (l1->hw->mr.timer_usg_cnt) {
  515. Write_hfc8(l1->hw, R_IRQMSK_MISC,
  516. M_TI_IRQMSK);
  517. } else {
  518. Write_hfc8(l1->hw, R_IRQMSK_MISC, 0);
  519. }
  520. break;
  521. default:
  522. printk(KERN_INFO
  523. "HFC-4S/8S: Unknown B-chan cmd 0x%x received, ignored\n",
  524. pr);
  525. break;
  526. }
  527. if (!l1->enabled)
  528. bch->b_if.ifc.l1l2(&bch->b_if.ifc,
  529. PH_DEACTIVATE | INDICATION, NULL);
  530. } /* bch_l2l1 */
  531. /**************************/
  532. /* layer 1 timer function */
  533. /**************************/
  534. static void
  535. hfc_l1_timer(struct hfc4s8s_l1 *l1)
  536. {
  537. u_long flags;
  538. if (!l1->enabled)
  539. return;
  540. spin_lock_irqsave(&l1->lock, flags);
  541. if (l1->nt_mode) {
  542. l1->l1_state = 1;
  543. Write_hfc8(l1->hw, R_ST_SEL, l1->st_num);
  544. Write_hfc8(l1->hw, A_ST_WR_STA, 0x11);
  545. spin_unlock_irqrestore(&l1->lock, flags);
  546. l1->d_if.ifc.l1l2(&l1->d_if.ifc,
  547. PH_DEACTIVATE | INDICATION, NULL);
  548. spin_lock_irqsave(&l1->lock, flags);
  549. l1->l1_state = 1;
  550. Write_hfc8(l1->hw, A_ST_WR_STA, 0x1);
  551. spin_unlock_irqrestore(&l1->lock, flags);
  552. } else {
  553. /* activation timed out */
  554. Write_hfc8(l1->hw, R_ST_SEL, l1->st_num);
  555. Write_hfc8(l1->hw, A_ST_WR_STA, 0x13);
  556. spin_unlock_irqrestore(&l1->lock, flags);
  557. l1->d_if.ifc.l1l2(&l1->d_if.ifc,
  558. PH_DEACTIVATE | INDICATION, NULL);
  559. spin_lock_irqsave(&l1->lock, flags);
  560. Write_hfc8(l1->hw, R_ST_SEL, l1->st_num);
  561. Write_hfc8(l1->hw, A_ST_WR_STA, 0x3);
  562. spin_unlock_irqrestore(&l1->lock, flags);
  563. }
  564. } /* hfc_l1_timer */
  565. /****************************************/
  566. /* a complete D-frame has been received */
  567. /****************************************/
  568. static void
  569. rx_d_frame(struct hfc4s8s_l1 *l1p, int ech)
  570. {
  571. int z1, z2;
  572. u_char f1, f2, df;
  573. struct sk_buff *skb;
  574. u_char *cp;
  575. if (!l1p->enabled)
  576. return;
  577. do {
  578. /* E/D RX fifo */
  579. Write_hfc8(l1p->hw, R_FIFO,
  580. (l1p->st_num * 8 + ((ech) ? 7 : 5)));
  581. wait_busy(l1p->hw);
  582. f1 = Read_hfc8_stable(l1p->hw, A_F1);
  583. f2 = Read_hfc8(l1p->hw, A_F2);
  584. if (f1 < f2)
  585. df = MAX_F_CNT + 1 + f1 - f2;
  586. else
  587. df = f1 - f2;
  588. if (!df)
  589. return; /* no complete frame in fifo */
  590. z1 = Read_hfc16_stable(l1p->hw, A_Z1);
  591. z2 = Read_hfc16(l1p->hw, A_Z2);
  592. z1 = z1 - z2 + 1;
  593. if (z1 < 0)
  594. z1 += 384;
  595. if (!(skb = dev_alloc_skb(MAX_D_FRAME_SIZE))) {
  596. printk(KERN_INFO
  597. "HFC-4S/8S: Could not allocate D/E "
  598. "channel receive buffer");
  599. Write_hfc8(l1p->hw, A_INC_RES_FIFO, 2);
  600. wait_busy(l1p->hw);
  601. return;
  602. }
  603. if (((z1 < 4) || (z1 > MAX_D_FRAME_SIZE))) {
  604. if (skb)
  605. dev_kfree_skb(skb);
  606. /* remove errornous D frame */
  607. if (df == 1) {
  608. /* reset fifo */
  609. Write_hfc8(l1p->hw, A_INC_RES_FIFO, 2);
  610. wait_busy(l1p->hw);
  611. return;
  612. } else {
  613. /* read errornous D frame */
  614. SetRegAddr(l1p->hw, A_FIFO_DATA0);
  615. while (z1 >= 4) {
  616. fRead_hfc32(l1p->hw);
  617. z1 -= 4;
  618. }
  619. while (z1--)
  620. fRead_hfc8(l1p->hw);
  621. Write_hfc8(l1p->hw, A_INC_RES_FIFO, 1);
  622. wait_busy(l1p->hw);
  623. return;
  624. }
  625. }
  626. cp = skb->data;
  627. SetRegAddr(l1p->hw, A_FIFO_DATA0);
  628. while (z1 >= 4) {
  629. *((unsigned long *) cp) = fRead_hfc32(l1p->hw);
  630. cp += 4;
  631. z1 -= 4;
  632. }
  633. while (z1--)
  634. *cp++ = fRead_hfc8(l1p->hw);
  635. Write_hfc8(l1p->hw, A_INC_RES_FIFO, 1); /* increment f counter */
  636. wait_busy(l1p->hw);
  637. if (*(--cp)) {
  638. dev_kfree_skb(skb);
  639. } else {
  640. skb->len = (cp - skb->data) - 2;
  641. if (ech)
  642. l1p->d_if.ifc.l1l2(&l1p->d_if.ifc,
  643. PH_DATA_E | INDICATION,
  644. skb);
  645. else
  646. l1p->d_if.ifc.l1l2(&l1p->d_if.ifc,
  647. PH_DATA | INDICATION,
  648. skb);
  649. }
  650. } while (1);
  651. } /* rx_d_frame */
  652. /*************************************************************/
  653. /* a B-frame has been received (perhaps not fully completed) */
  654. /*************************************************************/
  655. static void
  656. rx_b_frame(struct hfc4s8s_btype *bch)
  657. {
  658. int z1, z2, hdlc_complete;
  659. u_char f1, f2;
  660. struct hfc4s8s_l1 *l1 = bch->l1p;
  661. struct sk_buff *skb;
  662. if (!l1->enabled || (bch->mode == L1_MODE_NULL))
  663. return;
  664. do {
  665. /* RX Fifo */
  666. Write_hfc8(l1->hw, R_FIFO,
  667. (l1->st_num * 8 + ((bch->bchan == 1) ? 1 : 3)));
  668. wait_busy(l1->hw);
  669. if (bch->mode == L1_MODE_HDLC) {
  670. f1 = Read_hfc8_stable(l1->hw, A_F1);
  671. f2 = Read_hfc8(l1->hw, A_F2);
  672. hdlc_complete = ((f1 ^ f2) & MAX_F_CNT);
  673. } else
  674. hdlc_complete = 0;
  675. z1 = Read_hfc16_stable(l1->hw, A_Z1);
  676. z2 = Read_hfc16(l1->hw, A_Z2);
  677. z1 = (z1 - z2);
  678. if (hdlc_complete)
  679. z1++;
  680. if (z1 < 0)
  681. z1 += 384;
  682. if (!z1)
  683. break;
  684. if (!(skb = bch->rx_skb)) {
  685. if (!
  686. (skb =
  687. dev_alloc_skb((bch->mode ==
  688. L1_MODE_TRANS) ? z1
  689. : (MAX_B_FRAME_SIZE + 3)))) {
  690. printk(KERN_ERR
  691. "HFC-4S/8S: Could not allocate B "
  692. "channel receive buffer");
  693. return;
  694. }
  695. bch->rx_ptr = skb->data;
  696. bch->rx_skb = skb;
  697. }
  698. skb->len = (bch->rx_ptr - skb->data) + z1;
  699. /* HDLC length check */
  700. if ((bch->mode == L1_MODE_HDLC) &&
  701. ((hdlc_complete && (skb->len < 4)) ||
  702. (skb->len > (MAX_B_FRAME_SIZE + 3)))) {
  703. skb->len = 0;
  704. bch->rx_ptr = skb->data;
  705. Write_hfc8(l1->hw, A_INC_RES_FIFO, 2); /* reset fifo */
  706. wait_busy(l1->hw);
  707. return;
  708. }
  709. SetRegAddr(l1->hw, A_FIFO_DATA0);
  710. while (z1 >= 4) {
  711. *((unsigned long *) bch->rx_ptr) =
  712. fRead_hfc32(l1->hw);
  713. bch->rx_ptr += 4;
  714. z1 -= 4;
  715. }
  716. while (z1--)
  717. *(bch->rx_ptr++) = fRead_hfc8(l1->hw);
  718. if (hdlc_complete) {
  719. /* increment f counter */
  720. Write_hfc8(l1->hw, A_INC_RES_FIFO, 1);
  721. wait_busy(l1->hw);
  722. /* hdlc crc check */
  723. bch->rx_ptr--;
  724. if (*bch->rx_ptr) {
  725. skb->len = 0;
  726. bch->rx_ptr = skb->data;
  727. continue;
  728. }
  729. skb->len -= 3;
  730. }
  731. if (hdlc_complete || (bch->mode == L1_MODE_TRANS)) {
  732. bch->rx_skb = NULL;
  733. bch->rx_ptr = NULL;
  734. bch->b_if.ifc.l1l2(&bch->b_if.ifc,
  735. PH_DATA | INDICATION, skb);
  736. }
  737. } while (1);
  738. } /* rx_b_frame */
  739. /********************************************/
  740. /* a D-frame has been/should be transmitted */
  741. /********************************************/
  742. static void
  743. tx_d_frame(struct hfc4s8s_l1 *l1p)
  744. {
  745. struct sk_buff *skb;
  746. u_char f1, f2;
  747. u_char *cp;
  748. long cnt;
  749. if (l1p->l1_state != 7)
  750. return;
  751. /* TX fifo */
  752. Write_hfc8(l1p->hw, R_FIFO, (l1p->st_num * 8 + 4));
  753. wait_busy(l1p->hw);
  754. f1 = Read_hfc8(l1p->hw, A_F1);
  755. f2 = Read_hfc8_stable(l1p->hw, A_F2);
  756. if ((f1 ^ f2) & MAX_F_CNT)
  757. return; /* fifo is still filled */
  758. if (l1p->tx_cnt > 0) {
  759. cnt = l1p->tx_cnt;
  760. l1p->tx_cnt = 0;
  761. l1p->d_if.ifc.l1l2(&l1p->d_if.ifc, PH_DATA | CONFIRM,
  762. (void *) cnt);
  763. }
  764. if ((skb = skb_dequeue(&l1p->d_tx_queue))) {
  765. cp = skb->data;
  766. cnt = skb->len;
  767. SetRegAddr(l1p->hw, A_FIFO_DATA0);
  768. while (cnt >= 4) {
  769. SetRegAddr(l1p->hw, A_FIFO_DATA0);
  770. fWrite_hfc32(l1p->hw, *(unsigned long *) cp);
  771. cp += 4;
  772. cnt -= 4;
  773. }
  774. while (cnt--)
  775. fWrite_hfc8(l1p->hw, *cp++);
  776. l1p->tx_cnt = skb->truesize;
  777. Write_hfc8(l1p->hw, A_INC_RES_FIFO, 1); /* increment f counter */
  778. wait_busy(l1p->hw);
  779. dev_kfree_skb(skb);
  780. }
  781. } /* tx_d_frame */
  782. /******************************************************/
  783. /* a B-frame may be transmitted (or is not completed) */
  784. /******************************************************/
  785. static void
  786. tx_b_frame(struct hfc4s8s_btype *bch)
  787. {
  788. struct sk_buff *skb;
  789. struct hfc4s8s_l1 *l1 = bch->l1p;
  790. u_char *cp;
  791. int cnt, max, hdlc_num;
  792. long ack_len = 0;
  793. if (!l1->enabled || (bch->mode == L1_MODE_NULL))
  794. return;
  795. /* TX fifo */
  796. Write_hfc8(l1->hw, R_FIFO,
  797. (l1->st_num * 8 + ((bch->bchan == 1) ? 0 : 2)));
  798. wait_busy(l1->hw);
  799. do {
  800. if (bch->mode == L1_MODE_HDLC) {
  801. hdlc_num = Read_hfc8(l1->hw, A_F1) & MAX_F_CNT;
  802. hdlc_num -=
  803. (Read_hfc8_stable(l1->hw, A_F2) & MAX_F_CNT);
  804. if (hdlc_num < 0)
  805. hdlc_num += 16;
  806. if (hdlc_num >= 15)
  807. break; /* fifo still filled up with hdlc frames */
  808. } else
  809. hdlc_num = 0;
  810. if (!(skb = bch->tx_skb)) {
  811. if (!(skb = skb_dequeue(&bch->tx_queue))) {
  812. l1->hw->mr.fifo_slow_timer_service[l1->
  813. st_num]
  814. &= ~((bch->bchan == 1) ? 1 : 4);
  815. break; /* list empty */
  816. }
  817. bch->tx_skb = skb;
  818. bch->tx_cnt = 0;
  819. }
  820. if (!hdlc_num)
  821. l1->hw->mr.fifo_slow_timer_service[l1->st_num] |=
  822. ((bch->bchan == 1) ? 1 : 4);
  823. else
  824. l1->hw->mr.fifo_slow_timer_service[l1->st_num] &=
  825. ~((bch->bchan == 1) ? 1 : 4);
  826. max = Read_hfc16_stable(l1->hw, A_Z2);
  827. max -= Read_hfc16(l1->hw, A_Z1);
  828. if (max <= 0)
  829. max += 384;
  830. max--;
  831. if (max < 16)
  832. break; /* don't write to small amounts of bytes */
  833. cnt = skb->len - bch->tx_cnt;
  834. if (cnt > max)
  835. cnt = max;
  836. cp = skb->data + bch->tx_cnt;
  837. bch->tx_cnt += cnt;
  838. SetRegAddr(l1->hw, A_FIFO_DATA0);
  839. while (cnt >= 4) {
  840. fWrite_hfc32(l1->hw, *(unsigned long *) cp);
  841. cp += 4;
  842. cnt -= 4;
  843. }
  844. while (cnt--)
  845. fWrite_hfc8(l1->hw, *cp++);
  846. if (bch->tx_cnt >= skb->len) {
  847. if (bch->mode == L1_MODE_HDLC) {
  848. /* increment f counter */
  849. Write_hfc8(l1->hw, A_INC_RES_FIFO, 1);
  850. }
  851. ack_len += skb->truesize;
  852. bch->tx_skb = NULL;
  853. bch->tx_cnt = 0;
  854. dev_kfree_skb(skb);
  855. } else
  856. /* Re-Select */
  857. Write_hfc8(l1->hw, R_FIFO,
  858. (l1->st_num * 8 +
  859. ((bch->bchan == 1) ? 0 : 2)));
  860. wait_busy(l1->hw);
  861. } while (1);
  862. if (ack_len)
  863. bch->b_if.ifc.l1l2((struct hisax_if *) &bch->b_if,
  864. PH_DATA | CONFIRM, (void *) ack_len);
  865. } /* tx_b_frame */
  866. /*************************************/
  867. /* bottom half handler for interrupt */
  868. /*************************************/
  869. static void
  870. hfc4s8s_bh(struct work_struct *work)
  871. {
  872. hfc4s8s_hw *hw = container_of(work, hfc4s8s_hw, tqueue);
  873. u_char b;
  874. struct hfc4s8s_l1 *l1p;
  875. volatile u_char *fifo_stat;
  876. int idx;
  877. /* handle layer 1 state changes */
  878. b = 1;
  879. l1p = hw->l1;
  880. while (b) {
  881. if ((b & hw->mr.r_irq_statech)) {
  882. /* reset l1 event */
  883. hw->mr.r_irq_statech &= ~b;
  884. if (l1p->enabled) {
  885. if (l1p->nt_mode) {
  886. u_char oldstate = l1p->l1_state;
  887. Write_hfc8(l1p->hw, R_ST_SEL,
  888. l1p->st_num);
  889. l1p->l1_state =
  890. Read_hfc8(l1p->hw,
  891. A_ST_RD_STA) & 0xf;
  892. if ((oldstate == 3)
  893. && (l1p->l1_state != 3))
  894. l1p->d_if.ifc.l1l2(&l1p->
  895. d_if.
  896. ifc,
  897. PH_DEACTIVATE
  898. |
  899. INDICATION,
  900. NULL);
  901. if (l1p->l1_state != 2) {
  902. del_timer(&l1p->l1_timer);
  903. if (l1p->l1_state == 3) {
  904. l1p->d_if.ifc.
  905. l1l2(&l1p->
  906. d_if.ifc,
  907. PH_ACTIVATE
  908. |
  909. INDICATION,
  910. NULL);
  911. }
  912. } else {
  913. /* allow transition */
  914. Write_hfc8(hw, A_ST_WR_STA,
  915. M_SET_G2_G3);
  916. mod_timer(&l1p->l1_timer,
  917. jiffies +
  918. L1_TIMER_T1);
  919. }
  920. printk(KERN_INFO
  921. "HFC-4S/8S: NT ch %d l1 state %d -> %d\n",
  922. l1p->st_num, oldstate,
  923. l1p->l1_state);
  924. } else {
  925. u_char oldstate = l1p->l1_state;
  926. Write_hfc8(l1p->hw, R_ST_SEL,
  927. l1p->st_num);
  928. l1p->l1_state =
  929. Read_hfc8(l1p->hw,
  930. A_ST_RD_STA) & 0xf;
  931. if (((l1p->l1_state == 3) &&
  932. ((oldstate == 7) ||
  933. (oldstate == 8))) ||
  934. ((timer_pending
  935. (&l1p->l1_timer))
  936. && (l1p->l1_state == 8))) {
  937. mod_timer(&l1p->l1_timer,
  938. L1_TIMER_T4 +
  939. jiffies);
  940. } else {
  941. if (l1p->l1_state == 7) {
  942. del_timer(&l1p->
  943. l1_timer);
  944. l1p->d_if.ifc.
  945. l1l2(&l1p->
  946. d_if.ifc,
  947. PH_ACTIVATE
  948. |
  949. INDICATION,
  950. NULL);
  951. tx_d_frame(l1p);
  952. }
  953. if (l1p->l1_state == 3) {
  954. if (oldstate != 3)
  955. l1p->d_if.
  956. ifc.
  957. l1l2
  958. (&l1p->
  959. d_if.
  960. ifc,
  961. PH_DEACTIVATE
  962. |
  963. INDICATION,
  964. NULL);
  965. }
  966. }
  967. printk(KERN_INFO
  968. "HFC-4S/8S: TE %d ch %d l1 state %d -> %d\n",
  969. l1p->hw->cardnum,
  970. l1p->st_num, oldstate,
  971. l1p->l1_state);
  972. }
  973. }
  974. }
  975. b <<= 1;
  976. l1p++;
  977. }
  978. /* now handle the fifos */
  979. idx = 0;
  980. fifo_stat = hw->mr.r_irq_fifo_blx;
  981. l1p = hw->l1;
  982. while (idx < hw->driver_data.max_st_ports) {
  983. if (hw->mr.timer_irq) {
  984. *fifo_stat |= hw->mr.fifo_rx_trans_enables[idx];
  985. if (hw->fifo_sched_cnt <= 0) {
  986. *fifo_stat |=
  987. hw->mr.fifo_slow_timer_service[l1p->
  988. st_num];
  989. }
  990. }
  991. /* ignore fifo 6 (TX E fifo) */
  992. *fifo_stat &= 0xff - 0x40;
  993. while (*fifo_stat) {
  994. if (!l1p->nt_mode) {
  995. /* RX Fifo has data to read */
  996. if ((*fifo_stat & 0x20)) {
  997. *fifo_stat &= ~0x20;
  998. rx_d_frame(l1p, 0);
  999. }
  1000. /* E Fifo has data to read */
  1001. if ((*fifo_stat & 0x80)) {
  1002. *fifo_stat &= ~0x80;
  1003. rx_d_frame(l1p, 1);
  1004. }
  1005. /* TX Fifo completed send */
  1006. if ((*fifo_stat & 0x10)) {
  1007. *fifo_stat &= ~0x10;
  1008. tx_d_frame(l1p);
  1009. }
  1010. }
  1011. /* B1 RX Fifo has data to read */
  1012. if ((*fifo_stat & 0x2)) {
  1013. *fifo_stat &= ~0x2;
  1014. rx_b_frame(l1p->b_ch);
  1015. }
  1016. /* B1 TX Fifo has send completed */
  1017. if ((*fifo_stat & 0x1)) {
  1018. *fifo_stat &= ~0x1;
  1019. tx_b_frame(l1p->b_ch);
  1020. }
  1021. /* B2 RX Fifo has data to read */
  1022. if ((*fifo_stat & 0x8)) {
  1023. *fifo_stat &= ~0x8;
  1024. rx_b_frame(l1p->b_ch + 1);
  1025. }
  1026. /* B2 TX Fifo has send completed */
  1027. if ((*fifo_stat & 0x4)) {
  1028. *fifo_stat &= ~0x4;
  1029. tx_b_frame(l1p->b_ch + 1);
  1030. }
  1031. }
  1032. fifo_stat++;
  1033. l1p++;
  1034. idx++;
  1035. }
  1036. if (hw->fifo_sched_cnt <= 0)
  1037. hw->fifo_sched_cnt += (1 << (7 - TRANS_TIMER_MODE));
  1038. hw->mr.timer_irq = 0; /* clear requested timer irq */
  1039. } /* hfc4s8s_bh */
  1040. /*********************/
  1041. /* interrupt handler */
  1042. /*********************/
  1043. static irqreturn_t
  1044. hfc4s8s_interrupt(int intno, void *dev_id)
  1045. {
  1046. hfc4s8s_hw *hw = dev_id;
  1047. u_char b, ovr;
  1048. volatile u_char *ovp;
  1049. int idx;
  1050. u_char old_ioreg;
  1051. if (!hw || !(hw->mr.r_irq_ctrl & M_GLOB_IRQ_EN))
  1052. return IRQ_NONE;
  1053. /* read current selected regsister */
  1054. old_ioreg = GetRegAddr(hw);
  1055. /* Layer 1 State change */
  1056. hw->mr.r_irq_statech |=
  1057. (Read_hfc8(hw, R_SCI) & hw->mr.r_irqmsk_statchg);
  1058. if (!
  1059. (b = (Read_hfc8(hw, R_STATUS) & (M_MISC_IRQSTA | M_FR_IRQSTA)))
  1060. && !hw->mr.r_irq_statech) {
  1061. SetRegAddr(hw, old_ioreg);
  1062. return IRQ_NONE;
  1063. }
  1064. /* timer event */
  1065. if (Read_hfc8(hw, R_IRQ_MISC) & M_TI_IRQ) {
  1066. hw->mr.timer_irq = 1;
  1067. hw->fifo_sched_cnt--;
  1068. }
  1069. /* FIFO event */
  1070. if ((ovr = Read_hfc8(hw, R_IRQ_OVIEW))) {
  1071. hw->mr.r_irq_oview |= ovr;
  1072. idx = R_IRQ_FIFO_BL0;
  1073. ovp = hw->mr.r_irq_fifo_blx;
  1074. while (ovr) {
  1075. if ((ovr & 1)) {
  1076. *ovp |= Read_hfc8(hw, idx);
  1077. }
  1078. ovp++;
  1079. idx++;
  1080. ovr >>= 1;
  1081. }
  1082. }
  1083. /* queue the request to allow other cards to interrupt */
  1084. schedule_work(&hw->tqueue);
  1085. SetRegAddr(hw, old_ioreg);
  1086. return IRQ_HANDLED;
  1087. } /* hfc4s8s_interrupt */
  1088. /***********************************************************************/
  1089. /* reset the complete chip, don't release the chips irq but disable it */
  1090. /***********************************************************************/
  1091. static void
  1092. chipreset(hfc4s8s_hw *hw)
  1093. {
  1094. u_long flags;
  1095. spin_lock_irqsave(&hw->lock, flags);
  1096. Write_hfc8(hw, R_CTRL, 0); /* use internal RAM */
  1097. Write_hfc8(hw, R_RAM_MISC, 0); /* 32k*8 RAM */
  1098. Write_hfc8(hw, R_FIFO_MD, 0); /* fifo mode 386 byte/fifo simple mode */
  1099. Write_hfc8(hw, R_CIRM, M_SRES); /* reset chip */
  1100. hw->mr.r_irq_ctrl = 0; /* interrupt is inactive */
  1101. spin_unlock_irqrestore(&hw->lock, flags);
  1102. udelay(3);
  1103. Write_hfc8(hw, R_CIRM, 0); /* disable reset */
  1104. wait_busy(hw);
  1105. Write_hfc8(hw, R_PCM_MD0, M_PCM_MD); /* master mode */
  1106. Write_hfc8(hw, R_RAM_MISC, M_FZ_MD); /* transmit fifo option */
  1107. if (hw->driver_data.clock_mode == 1)
  1108. Write_hfc8(hw, R_BRG_PCM_CFG, M_PCM_CLK); /* PCM clk / 2 */
  1109. Write_hfc8(hw, R_TI_WD, TRANS_TIMER_MODE); /* timer interval */
  1110. memset(&hw->mr, 0, sizeof(hw->mr));
  1111. } /* chipreset */
  1112. /********************************************/
  1113. /* disable/enable hardware in nt or te mode */
  1114. /********************************************/
  1115. static void
  1116. hfc_hardware_enable(hfc4s8s_hw *hw, int enable, int nt_mode)
  1117. {
  1118. u_long flags;
  1119. char if_name[40];
  1120. int i;
  1121. if (enable) {
  1122. /* save system vars */
  1123. hw->nt_mode = nt_mode;
  1124. /* enable fifo and state irqs, but not global irq enable */
  1125. hw->mr.r_irq_ctrl = M_FIFO_IRQ;
  1126. Write_hfc8(hw, R_IRQ_CTRL, hw->mr.r_irq_ctrl);
  1127. hw->mr.r_irqmsk_statchg = 0;
  1128. Write_hfc8(hw, R_SCI_MSK, hw->mr.r_irqmsk_statchg);
  1129. Write_hfc8(hw, R_PWM_MD, 0x80);
  1130. Write_hfc8(hw, R_PWM1, 26);
  1131. if (!nt_mode)
  1132. Write_hfc8(hw, R_ST_SYNC, M_AUTO_SYNC);
  1133. /* enable the line interfaces and fifos */
  1134. for (i = 0; i < hw->driver_data.max_st_ports; i++) {
  1135. hw->mr.r_irqmsk_statchg |= (1 << i);
  1136. Write_hfc8(hw, R_SCI_MSK, hw->mr.r_irqmsk_statchg);
  1137. Write_hfc8(hw, R_ST_SEL, i);
  1138. Write_hfc8(hw, A_ST_CLK_DLY,
  1139. ((nt_mode) ? CLKDEL_NT : CLKDEL_TE));
  1140. hw->mr.r_ctrl0 = ((nt_mode) ? CTRL0_NT : CTRL0_TE);
  1141. Write_hfc8(hw, A_ST_CTRL0, hw->mr.r_ctrl0);
  1142. Write_hfc8(hw, A_ST_CTRL2, 3);
  1143. Write_hfc8(hw, A_ST_WR_STA, 0); /* enable state machine */
  1144. hw->l1[i].enabled = 1;
  1145. hw->l1[i].nt_mode = nt_mode;
  1146. if (!nt_mode) {
  1147. /* setup E-fifo */
  1148. Write_hfc8(hw, R_FIFO, i * 8 + 7); /* E fifo */
  1149. wait_busy(hw);
  1150. Write_hfc8(hw, A_CON_HDLC, 0x11); /* HDLC mode, 1 fill, connect ST */
  1151. Write_hfc8(hw, A_SUBCH_CFG, 2); /* only 2 bits */
  1152. Write_hfc8(hw, A_IRQ_MSK, 1); /* enable interrupt */
  1153. Write_hfc8(hw, A_INC_RES_FIFO, 2); /* reset fifo */
  1154. wait_busy(hw);
  1155. /* setup D RX-fifo */
  1156. Write_hfc8(hw, R_FIFO, i * 8 + 5); /* RX fifo */
  1157. wait_busy(hw);
  1158. Write_hfc8(hw, A_CON_HDLC, 0x11); /* HDLC mode, 1 fill, connect ST */
  1159. Write_hfc8(hw, A_SUBCH_CFG, 2); /* only 2 bits */
  1160. Write_hfc8(hw, A_IRQ_MSK, 1); /* enable interrupt */
  1161. Write_hfc8(hw, A_INC_RES_FIFO, 2); /* reset fifo */
  1162. wait_busy(hw);
  1163. /* setup D TX-fifo */
  1164. Write_hfc8(hw, R_FIFO, i * 8 + 4); /* TX fifo */
  1165. wait_busy(hw);
  1166. Write_hfc8(hw, A_CON_HDLC, 0x11); /* HDLC mode, 1 fill, connect ST */
  1167. Write_hfc8(hw, A_SUBCH_CFG, 2); /* only 2 bits */
  1168. Write_hfc8(hw, A_IRQ_MSK, 1); /* enable interrupt */
  1169. Write_hfc8(hw, A_INC_RES_FIFO, 2); /* reset fifo */
  1170. wait_busy(hw);
  1171. }
  1172. sprintf(if_name, "hfc4s8s_%d%d_", hw->cardnum, i);
  1173. if (hisax_register
  1174. (&hw->l1[i].d_if, hw->l1[i].b_table, if_name,
  1175. ((nt_mode) ? 3 : 2))) {
  1176. hw->l1[i].enabled = 0;
  1177. hw->mr.r_irqmsk_statchg &= ~(1 << i);
  1178. Write_hfc8(hw, R_SCI_MSK,
  1179. hw->mr.r_irqmsk_statchg);
  1180. printk(KERN_INFO
  1181. "HFC-4S/8S: Unable to register S/T device %s, break\n",
  1182. if_name);
  1183. break;
  1184. }
  1185. }
  1186. spin_lock_irqsave(&hw->lock, flags);
  1187. hw->mr.r_irq_ctrl |= M_GLOB_IRQ_EN;
  1188. Write_hfc8(hw, R_IRQ_CTRL, hw->mr.r_irq_ctrl);
  1189. spin_unlock_irqrestore(&hw->lock, flags);
  1190. } else {
  1191. /* disable hardware */
  1192. spin_lock_irqsave(&hw->lock, flags);
  1193. hw->mr.r_irq_ctrl &= ~M_GLOB_IRQ_EN;
  1194. Write_hfc8(hw, R_IRQ_CTRL, hw->mr.r_irq_ctrl);
  1195. spin_unlock_irqrestore(&hw->lock, flags);
  1196. for (i = hw->driver_data.max_st_ports - 1; i >= 0; i--) {
  1197. hw->l1[i].enabled = 0;
  1198. hisax_unregister(&hw->l1[i].d_if);
  1199. del_timer(&hw->l1[i].l1_timer);
  1200. skb_queue_purge(&hw->l1[i].d_tx_queue);
  1201. skb_queue_purge(&hw->l1[i].b_ch[0].tx_queue);
  1202. skb_queue_purge(&hw->l1[i].b_ch[1].tx_queue);
  1203. }
  1204. chipreset(hw);
  1205. }
  1206. } /* hfc_hardware_enable */
  1207. /******************************************/
  1208. /* disable memory mapped ports / io ports */
  1209. /******************************************/
  1210. static void
  1211. release_pci_ports(hfc4s8s_hw *hw)
  1212. {
  1213. pci_write_config_word(hw->pdev, PCI_COMMAND, 0);
  1214. if (hw->iobase)
  1215. release_region(hw->iobase, 8);
  1216. }
  1217. /*****************************************/
  1218. /* enable memory mapped ports / io ports */
  1219. /*****************************************/
  1220. static void
  1221. enable_pci_ports(hfc4s8s_hw *hw)
  1222. {
  1223. pci_write_config_word(hw->pdev, PCI_COMMAND, PCI_ENA_REGIO);
  1224. }
  1225. /*************************************/
  1226. /* initialise the HFC-4s/8s hardware */
  1227. /* return 0 on success. */
  1228. /*************************************/
  1229. static int
  1230. setup_instance(hfc4s8s_hw *hw)
  1231. {
  1232. int err = -EIO;
  1233. int i;
  1234. for (i = 0; i < HFC_MAX_ST; i++) {
  1235. struct hfc4s8s_l1 *l1p;
  1236. l1p = hw->l1 + i;
  1237. spin_lock_init(&l1p->lock);
  1238. l1p->hw = hw;
  1239. l1p->l1_timer.function = (void *) hfc_l1_timer;
  1240. l1p->l1_timer.data = (long) (l1p);
  1241. init_timer(&l1p->l1_timer);
  1242. l1p->st_num = i;
  1243. skb_queue_head_init(&l1p->d_tx_queue);
  1244. l1p->d_if.ifc.priv = hw->l1 + i;
  1245. l1p->d_if.ifc.l2l1 = (void *) dch_l2l1;
  1246. spin_lock_init(&l1p->b_ch[0].lock);
  1247. l1p->b_ch[0].b_if.ifc.l2l1 = (void *) bch_l2l1;
  1248. l1p->b_ch[0].b_if.ifc.priv = (void *) &l1p->b_ch[0];
  1249. l1p->b_ch[0].l1p = hw->l1 + i;
  1250. l1p->b_ch[0].bchan = 1;
  1251. l1p->b_table[0] = &l1p->b_ch[0].b_if;
  1252. skb_queue_head_init(&l1p->b_ch[0].tx_queue);
  1253. spin_lock_init(&l1p->b_ch[1].lock);
  1254. l1p->b_ch[1].b_if.ifc.l2l1 = (void *) bch_l2l1;
  1255. l1p->b_ch[1].b_if.ifc.priv = (void *) &l1p->b_ch[1];
  1256. l1p->b_ch[1].l1p = hw->l1 + i;
  1257. l1p->b_ch[1].bchan = 2;
  1258. l1p->b_table[1] = &l1p->b_ch[1].b_if;
  1259. skb_queue_head_init(&l1p->b_ch[1].tx_queue);
  1260. }
  1261. enable_pci_ports(hw);
  1262. chipreset(hw);
  1263. i = Read_hfc8(hw, R_CHIP_ID) >> CHIP_ID_SHIFT;
  1264. if (i != hw->driver_data.chip_id) {
  1265. printk(KERN_INFO
  1266. "HFC-4S/8S: invalid chip id 0x%x instead of 0x%x, card ignored\n",
  1267. i, hw->driver_data.chip_id);
  1268. goto out;
  1269. }
  1270. i = Read_hfc8(hw, R_CHIP_RV) & 0xf;
  1271. if (!i) {
  1272. printk(KERN_INFO
  1273. "HFC-4S/8S: chip revision 0 not supported, card ignored\n");
  1274. goto out;
  1275. }
  1276. INIT_WORK(&hw->tqueue, hfc4s8s_bh);
  1277. if (request_irq
  1278. (hw->irq, hfc4s8s_interrupt, IRQF_SHARED, hw->card_name, hw)) {
  1279. printk(KERN_INFO
  1280. "HFC-4S/8S: unable to alloc irq %d, card ignored\n",
  1281. hw->irq);
  1282. goto out;
  1283. }
  1284. printk(KERN_INFO
  1285. "HFC-4S/8S: found PCI card at iobase 0x%x, irq %d\n",
  1286. hw->iobase, hw->irq);
  1287. hfc_hardware_enable(hw, 1, 0);
  1288. return (0);
  1289. out:
  1290. hw->irq = 0;
  1291. release_pci_ports(hw);
  1292. kfree(hw);
  1293. return (err);
  1294. }
  1295. /*****************************************/
  1296. /* PCI hotplug interface: probe new card */
  1297. /*****************************************/
  1298. static int
  1299. hfc4s8s_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  1300. {
  1301. int err = -ENOMEM;
  1302. hfc4s8s_param *driver_data = (hfc4s8s_param *) ent->driver_data;
  1303. hfc4s8s_hw *hw;
  1304. if (!(hw = kzalloc(sizeof(hfc4s8s_hw), GFP_ATOMIC))) {
  1305. printk(KERN_ERR "No kmem for HFC-4S/8S card\n");
  1306. return (err);
  1307. }
  1308. hw->pdev = pdev;
  1309. err = pci_enable_device(pdev);
  1310. if (err)
  1311. goto out;
  1312. hw->cardnum = card_cnt;
  1313. sprintf(hw->card_name, "hfc4s8s_%d", hw->cardnum);
  1314. printk(KERN_INFO "HFC-4S/8S: found adapter %s (%s) at %s\n",
  1315. driver_data->device_name, hw->card_name, pci_name(pdev));
  1316. spin_lock_init(&hw->lock);
  1317. hw->driver_data = *driver_data;
  1318. hw->irq = pdev->irq;
  1319. hw->iobase = pci_resource_start(pdev, 0);
  1320. if (!request_region(hw->iobase, 8, hw->card_name)) {
  1321. printk(KERN_INFO
  1322. "HFC-4S/8S: failed to request address space at 0x%04x\n",
  1323. hw->iobase);
  1324. goto out;
  1325. }
  1326. pci_set_drvdata(pdev, hw);
  1327. err = setup_instance(hw);
  1328. if (!err)
  1329. card_cnt++;
  1330. return (err);
  1331. out:
  1332. kfree(hw);
  1333. return (err);
  1334. }
  1335. /**************************************/
  1336. /* PCI hotplug interface: remove card */
  1337. /**************************************/
  1338. static void
  1339. hfc4s8s_remove(struct pci_dev *pdev)
  1340. {
  1341. hfc4s8s_hw *hw = pci_get_drvdata(pdev);
  1342. printk(KERN_INFO "HFC-4S/8S: removing card %d\n", hw->cardnum);
  1343. hfc_hardware_enable(hw, 0, 0);
  1344. if (hw->irq)
  1345. free_irq(hw->irq, hw);
  1346. hw->irq = 0;
  1347. release_pci_ports(hw);
  1348. card_cnt--;
  1349. pci_disable_device(pdev);
  1350. kfree(hw);
  1351. return;
  1352. }
  1353. static struct pci_driver hfc4s8s_driver = {
  1354. .name = "hfc4s8s_l1",
  1355. .probe = hfc4s8s_probe,
  1356. .remove = hfc4s8s_remove,
  1357. .id_table = hfc4s8s_ids,
  1358. };
  1359. /**********************/
  1360. /* driver Module init */
  1361. /**********************/
  1362. static int __init
  1363. hfc4s8s_module_init(void)
  1364. {
  1365. int err;
  1366. printk(KERN_INFO
  1367. "HFC-4S/8S: Layer 1 driver module for HFC-4S/8S isdn chips, %s\n",
  1368. hfc4s8s_rev);
  1369. printk(KERN_INFO
  1370. "HFC-4S/8S: (C) 2003 Cornelius Consult, www.cornelius-consult.de\n");
  1371. card_cnt = 0;
  1372. err = pci_register_driver(&hfc4s8s_driver);
  1373. if (err < 0) {
  1374. goto out;
  1375. }
  1376. printk(KERN_INFO "HFC-4S/8S: found %d cards\n", card_cnt);
  1377. return 0;
  1378. out:
  1379. return (err);
  1380. } /* hfc4s8s_init_hw */
  1381. /*************************************/
  1382. /* driver module exit : */
  1383. /* release the HFC-4s/8s hardware */
  1384. /*************************************/
  1385. static void __exit
  1386. hfc4s8s_module_exit(void)
  1387. {
  1388. pci_unregister_driver(&hfc4s8s_driver);
  1389. printk(KERN_INFO "HFC-4S/8S: module removed\n");
  1390. } /* hfc4s8s_release_hw */
  1391. module_init(hfc4s8s_module_init);
  1392. module_exit(hfc4s8s_module_exit);