hfc4s8s_l1.c 39 KB

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