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 const 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 timer_list *t)
  536. {
  537. struct hfc4s8s_l1 *l1 = from_timer(l1, t, l1_timer);
  538. u_long flags;
  539. if (!l1->enabled)
  540. return;
  541. spin_lock_irqsave(&l1->lock, flags);
  542. if (l1->nt_mode) {
  543. l1->l1_state = 1;
  544. Write_hfc8(l1->hw, R_ST_SEL, l1->st_num);
  545. Write_hfc8(l1->hw, A_ST_WR_STA, 0x11);
  546. spin_unlock_irqrestore(&l1->lock, flags);
  547. l1->d_if.ifc.l1l2(&l1->d_if.ifc,
  548. PH_DEACTIVATE | INDICATION, NULL);
  549. spin_lock_irqsave(&l1->lock, flags);
  550. l1->l1_state = 1;
  551. Write_hfc8(l1->hw, A_ST_WR_STA, 0x1);
  552. spin_unlock_irqrestore(&l1->lock, flags);
  553. } else {
  554. /* activation timed out */
  555. Write_hfc8(l1->hw, R_ST_SEL, l1->st_num);
  556. Write_hfc8(l1->hw, A_ST_WR_STA, 0x13);
  557. spin_unlock_irqrestore(&l1->lock, flags);
  558. l1->d_if.ifc.l1l2(&l1->d_if.ifc,
  559. PH_DEACTIVATE | INDICATION, NULL);
  560. spin_lock_irqsave(&l1->lock, flags);
  561. Write_hfc8(l1->hw, R_ST_SEL, l1->st_num);
  562. Write_hfc8(l1->hw, A_ST_WR_STA, 0x3);
  563. spin_unlock_irqrestore(&l1->lock, flags);
  564. }
  565. } /* hfc_l1_timer */
  566. /****************************************/
  567. /* a complete D-frame has been received */
  568. /****************************************/
  569. static void
  570. rx_d_frame(struct hfc4s8s_l1 *l1p, int ech)
  571. {
  572. int z1, z2;
  573. u_char f1, f2, df;
  574. struct sk_buff *skb;
  575. u_char *cp;
  576. if (!l1p->enabled)
  577. return;
  578. do {
  579. /* E/D RX fifo */
  580. Write_hfc8(l1p->hw, R_FIFO,
  581. (l1p->st_num * 8 + ((ech) ? 7 : 5)));
  582. wait_busy(l1p->hw);
  583. f1 = Read_hfc8_stable(l1p->hw, A_F1);
  584. f2 = Read_hfc8(l1p->hw, A_F2);
  585. if (f1 < f2)
  586. df = MAX_F_CNT + 1 + f1 - f2;
  587. else
  588. df = f1 - f2;
  589. if (!df)
  590. return; /* no complete frame in fifo */
  591. z1 = Read_hfc16_stable(l1p->hw, A_Z1);
  592. z2 = Read_hfc16(l1p->hw, A_Z2);
  593. z1 = z1 - z2 + 1;
  594. if (z1 < 0)
  595. z1 += 384;
  596. if (!(skb = dev_alloc_skb(MAX_D_FRAME_SIZE))) {
  597. printk(KERN_INFO
  598. "HFC-4S/8S: Could not allocate D/E "
  599. "channel receive buffer");
  600. Write_hfc8(l1p->hw, A_INC_RES_FIFO, 2);
  601. wait_busy(l1p->hw);
  602. return;
  603. }
  604. if (((z1 < 4) || (z1 > MAX_D_FRAME_SIZE))) {
  605. if (skb)
  606. dev_kfree_skb(skb);
  607. /* remove errornous D frame */
  608. if (df == 1) {
  609. /* reset fifo */
  610. Write_hfc8(l1p->hw, A_INC_RES_FIFO, 2);
  611. wait_busy(l1p->hw);
  612. return;
  613. } else {
  614. /* read errornous D frame */
  615. SetRegAddr(l1p->hw, A_FIFO_DATA0);
  616. while (z1 >= 4) {
  617. fRead_hfc32(l1p->hw);
  618. z1 -= 4;
  619. }
  620. while (z1--)
  621. fRead_hfc8(l1p->hw);
  622. Write_hfc8(l1p->hw, A_INC_RES_FIFO, 1);
  623. wait_busy(l1p->hw);
  624. return;
  625. }
  626. }
  627. cp = skb->data;
  628. SetRegAddr(l1p->hw, A_FIFO_DATA0);
  629. while (z1 >= 4) {
  630. *((unsigned long *) cp) = fRead_hfc32(l1p->hw);
  631. cp += 4;
  632. z1 -= 4;
  633. }
  634. while (z1--)
  635. *cp++ = fRead_hfc8(l1p->hw);
  636. Write_hfc8(l1p->hw, A_INC_RES_FIFO, 1); /* increment f counter */
  637. wait_busy(l1p->hw);
  638. if (*(--cp)) {
  639. dev_kfree_skb(skb);
  640. } else {
  641. skb->len = (cp - skb->data) - 2;
  642. if (ech)
  643. l1p->d_if.ifc.l1l2(&l1p->d_if.ifc,
  644. PH_DATA_E | INDICATION,
  645. skb);
  646. else
  647. l1p->d_if.ifc.l1l2(&l1p->d_if.ifc,
  648. PH_DATA | INDICATION,
  649. skb);
  650. }
  651. } while (1);
  652. } /* rx_d_frame */
  653. /*************************************************************/
  654. /* a B-frame has been received (perhaps not fully completed) */
  655. /*************************************************************/
  656. static void
  657. rx_b_frame(struct hfc4s8s_btype *bch)
  658. {
  659. int z1, z2, hdlc_complete;
  660. u_char f1, f2;
  661. struct hfc4s8s_l1 *l1 = bch->l1p;
  662. struct sk_buff *skb;
  663. if (!l1->enabled || (bch->mode == L1_MODE_NULL))
  664. return;
  665. do {
  666. /* RX Fifo */
  667. Write_hfc8(l1->hw, R_FIFO,
  668. (l1->st_num * 8 + ((bch->bchan == 1) ? 1 : 3)));
  669. wait_busy(l1->hw);
  670. if (bch->mode == L1_MODE_HDLC) {
  671. f1 = Read_hfc8_stable(l1->hw, A_F1);
  672. f2 = Read_hfc8(l1->hw, A_F2);
  673. hdlc_complete = ((f1 ^ f2) & MAX_F_CNT);
  674. } else
  675. hdlc_complete = 0;
  676. z1 = Read_hfc16_stable(l1->hw, A_Z1);
  677. z2 = Read_hfc16(l1->hw, A_Z2);
  678. z1 = (z1 - z2);
  679. if (hdlc_complete)
  680. z1++;
  681. if (z1 < 0)
  682. z1 += 384;
  683. if (!z1)
  684. break;
  685. if (!(skb = bch->rx_skb)) {
  686. if (!
  687. (skb =
  688. dev_alloc_skb((bch->mode ==
  689. L1_MODE_TRANS) ? z1
  690. : (MAX_B_FRAME_SIZE + 3)))) {
  691. printk(KERN_ERR
  692. "HFC-4S/8S: Could not allocate B "
  693. "channel receive buffer");
  694. return;
  695. }
  696. bch->rx_ptr = skb->data;
  697. bch->rx_skb = skb;
  698. }
  699. skb->len = (bch->rx_ptr - skb->data) + z1;
  700. /* HDLC length check */
  701. if ((bch->mode == L1_MODE_HDLC) &&
  702. ((hdlc_complete && (skb->len < 4)) ||
  703. (skb->len > (MAX_B_FRAME_SIZE + 3)))) {
  704. skb->len = 0;
  705. bch->rx_ptr = skb->data;
  706. Write_hfc8(l1->hw, A_INC_RES_FIFO, 2); /* reset fifo */
  707. wait_busy(l1->hw);
  708. return;
  709. }
  710. SetRegAddr(l1->hw, A_FIFO_DATA0);
  711. while (z1 >= 4) {
  712. *((unsigned long *) bch->rx_ptr) =
  713. fRead_hfc32(l1->hw);
  714. bch->rx_ptr += 4;
  715. z1 -= 4;
  716. }
  717. while (z1--)
  718. *(bch->rx_ptr++) = fRead_hfc8(l1->hw);
  719. if (hdlc_complete) {
  720. /* increment f counter */
  721. Write_hfc8(l1->hw, A_INC_RES_FIFO, 1);
  722. wait_busy(l1->hw);
  723. /* hdlc crc check */
  724. bch->rx_ptr--;
  725. if (*bch->rx_ptr) {
  726. skb->len = 0;
  727. bch->rx_ptr = skb->data;
  728. continue;
  729. }
  730. skb->len -= 3;
  731. }
  732. if (hdlc_complete || (bch->mode == L1_MODE_TRANS)) {
  733. bch->rx_skb = NULL;
  734. bch->rx_ptr = NULL;
  735. bch->b_if.ifc.l1l2(&bch->b_if.ifc,
  736. PH_DATA | INDICATION, skb);
  737. }
  738. } while (1);
  739. } /* rx_b_frame */
  740. /********************************************/
  741. /* a D-frame has been/should be transmitted */
  742. /********************************************/
  743. static void
  744. tx_d_frame(struct hfc4s8s_l1 *l1p)
  745. {
  746. struct sk_buff *skb;
  747. u_char f1, f2;
  748. u_char *cp;
  749. long cnt;
  750. if (l1p->l1_state != 7)
  751. return;
  752. /* TX fifo */
  753. Write_hfc8(l1p->hw, R_FIFO, (l1p->st_num * 8 + 4));
  754. wait_busy(l1p->hw);
  755. f1 = Read_hfc8(l1p->hw, A_F1);
  756. f2 = Read_hfc8_stable(l1p->hw, A_F2);
  757. if ((f1 ^ f2) & MAX_F_CNT)
  758. return; /* fifo is still filled */
  759. if (l1p->tx_cnt > 0) {
  760. cnt = l1p->tx_cnt;
  761. l1p->tx_cnt = 0;
  762. l1p->d_if.ifc.l1l2(&l1p->d_if.ifc, PH_DATA | CONFIRM,
  763. (void *) cnt);
  764. }
  765. if ((skb = skb_dequeue(&l1p->d_tx_queue))) {
  766. cp = skb->data;
  767. cnt = skb->len;
  768. SetRegAddr(l1p->hw, A_FIFO_DATA0);
  769. while (cnt >= 4) {
  770. SetRegAddr(l1p->hw, A_FIFO_DATA0);
  771. fWrite_hfc32(l1p->hw, *(unsigned long *) cp);
  772. cp += 4;
  773. cnt -= 4;
  774. }
  775. while (cnt--)
  776. fWrite_hfc8(l1p->hw, *cp++);
  777. l1p->tx_cnt = skb->truesize;
  778. Write_hfc8(l1p->hw, A_INC_RES_FIFO, 1); /* increment f counter */
  779. wait_busy(l1p->hw);
  780. dev_kfree_skb(skb);
  781. }
  782. } /* tx_d_frame */
  783. /******************************************************/
  784. /* a B-frame may be transmitted (or is not completed) */
  785. /******************************************************/
  786. static void
  787. tx_b_frame(struct hfc4s8s_btype *bch)
  788. {
  789. struct sk_buff *skb;
  790. struct hfc4s8s_l1 *l1 = bch->l1p;
  791. u_char *cp;
  792. int cnt, max, hdlc_num;
  793. long ack_len = 0;
  794. if (!l1->enabled || (bch->mode == L1_MODE_NULL))
  795. return;
  796. /* TX fifo */
  797. Write_hfc8(l1->hw, R_FIFO,
  798. (l1->st_num * 8 + ((bch->bchan == 1) ? 0 : 2)));
  799. wait_busy(l1->hw);
  800. do {
  801. if (bch->mode == L1_MODE_HDLC) {
  802. hdlc_num = Read_hfc8(l1->hw, A_F1) & MAX_F_CNT;
  803. hdlc_num -=
  804. (Read_hfc8_stable(l1->hw, A_F2) & MAX_F_CNT);
  805. if (hdlc_num < 0)
  806. hdlc_num += 16;
  807. if (hdlc_num >= 15)
  808. break; /* fifo still filled up with hdlc frames */
  809. } else
  810. hdlc_num = 0;
  811. if (!(skb = bch->tx_skb)) {
  812. if (!(skb = skb_dequeue(&bch->tx_queue))) {
  813. l1->hw->mr.fifo_slow_timer_service[l1->
  814. st_num]
  815. &= ~((bch->bchan == 1) ? 1 : 4);
  816. break; /* list empty */
  817. }
  818. bch->tx_skb = skb;
  819. bch->tx_cnt = 0;
  820. }
  821. if (!hdlc_num)
  822. l1->hw->mr.fifo_slow_timer_service[l1->st_num] |=
  823. ((bch->bchan == 1) ? 1 : 4);
  824. else
  825. l1->hw->mr.fifo_slow_timer_service[l1->st_num] &=
  826. ~((bch->bchan == 1) ? 1 : 4);
  827. max = Read_hfc16_stable(l1->hw, A_Z2);
  828. max -= Read_hfc16(l1->hw, A_Z1);
  829. if (max <= 0)
  830. max += 384;
  831. max--;
  832. if (max < 16)
  833. break; /* don't write to small amounts of bytes */
  834. cnt = skb->len - bch->tx_cnt;
  835. if (cnt > max)
  836. cnt = max;
  837. cp = skb->data + bch->tx_cnt;
  838. bch->tx_cnt += cnt;
  839. SetRegAddr(l1->hw, A_FIFO_DATA0);
  840. while (cnt >= 4) {
  841. fWrite_hfc32(l1->hw, *(unsigned long *) cp);
  842. cp += 4;
  843. cnt -= 4;
  844. }
  845. while (cnt--)
  846. fWrite_hfc8(l1->hw, *cp++);
  847. if (bch->tx_cnt >= skb->len) {
  848. if (bch->mode == L1_MODE_HDLC) {
  849. /* increment f counter */
  850. Write_hfc8(l1->hw, A_INC_RES_FIFO, 1);
  851. }
  852. ack_len += skb->truesize;
  853. bch->tx_skb = NULL;
  854. bch->tx_cnt = 0;
  855. dev_kfree_skb(skb);
  856. } else
  857. /* Re-Select */
  858. Write_hfc8(l1->hw, R_FIFO,
  859. (l1->st_num * 8 +
  860. ((bch->bchan == 1) ? 0 : 2)));
  861. wait_busy(l1->hw);
  862. } while (1);
  863. if (ack_len)
  864. bch->b_if.ifc.l1l2((struct hisax_if *) &bch->b_if,
  865. PH_DATA | CONFIRM, (void *) ack_len);
  866. } /* tx_b_frame */
  867. /*************************************/
  868. /* bottom half handler for interrupt */
  869. /*************************************/
  870. static void
  871. hfc4s8s_bh(struct work_struct *work)
  872. {
  873. hfc4s8s_hw *hw = container_of(work, hfc4s8s_hw, tqueue);
  874. u_char b;
  875. struct hfc4s8s_l1 *l1p;
  876. volatile u_char *fifo_stat;
  877. int idx;
  878. /* handle layer 1 state changes */
  879. b = 1;
  880. l1p = hw->l1;
  881. while (b) {
  882. if ((b & hw->mr.r_irq_statech)) {
  883. /* reset l1 event */
  884. hw->mr.r_irq_statech &= ~b;
  885. if (l1p->enabled) {
  886. if (l1p->nt_mode) {
  887. u_char oldstate = l1p->l1_state;
  888. Write_hfc8(l1p->hw, R_ST_SEL,
  889. l1p->st_num);
  890. l1p->l1_state =
  891. Read_hfc8(l1p->hw,
  892. A_ST_RD_STA) & 0xf;
  893. if ((oldstate == 3)
  894. && (l1p->l1_state != 3))
  895. l1p->d_if.ifc.l1l2(&l1p->
  896. d_if.
  897. ifc,
  898. PH_DEACTIVATE
  899. |
  900. INDICATION,
  901. NULL);
  902. if (l1p->l1_state != 2) {
  903. del_timer(&l1p->l1_timer);
  904. if (l1p->l1_state == 3) {
  905. l1p->d_if.ifc.
  906. l1l2(&l1p->
  907. d_if.ifc,
  908. PH_ACTIVATE
  909. |
  910. INDICATION,
  911. NULL);
  912. }
  913. } else {
  914. /* allow transition */
  915. Write_hfc8(hw, A_ST_WR_STA,
  916. M_SET_G2_G3);
  917. mod_timer(&l1p->l1_timer,
  918. jiffies +
  919. L1_TIMER_T1);
  920. }
  921. printk(KERN_INFO
  922. "HFC-4S/8S: NT ch %d l1 state %d -> %d\n",
  923. l1p->st_num, oldstate,
  924. l1p->l1_state);
  925. } else {
  926. u_char oldstate = l1p->l1_state;
  927. Write_hfc8(l1p->hw, R_ST_SEL,
  928. l1p->st_num);
  929. l1p->l1_state =
  930. Read_hfc8(l1p->hw,
  931. A_ST_RD_STA) & 0xf;
  932. if (((l1p->l1_state == 3) &&
  933. ((oldstate == 7) ||
  934. (oldstate == 8))) ||
  935. ((timer_pending
  936. (&l1p->l1_timer))
  937. && (l1p->l1_state == 8))) {
  938. mod_timer(&l1p->l1_timer,
  939. L1_TIMER_T4 +
  940. jiffies);
  941. } else {
  942. if (l1p->l1_state == 7) {
  943. del_timer(&l1p->
  944. l1_timer);
  945. l1p->d_if.ifc.
  946. l1l2(&l1p->
  947. d_if.ifc,
  948. PH_ACTIVATE
  949. |
  950. INDICATION,
  951. NULL);
  952. tx_d_frame(l1p);
  953. }
  954. if (l1p->l1_state == 3) {
  955. if (oldstate != 3)
  956. l1p->d_if.
  957. ifc.
  958. l1l2
  959. (&l1p->
  960. d_if.
  961. ifc,
  962. PH_DEACTIVATE
  963. |
  964. INDICATION,
  965. NULL);
  966. }
  967. }
  968. printk(KERN_INFO
  969. "HFC-4S/8S: TE %d ch %d l1 state %d -> %d\n",
  970. l1p->hw->cardnum,
  971. l1p->st_num, oldstate,
  972. l1p->l1_state);
  973. }
  974. }
  975. }
  976. b <<= 1;
  977. l1p++;
  978. }
  979. /* now handle the fifos */
  980. idx = 0;
  981. fifo_stat = hw->mr.r_irq_fifo_blx;
  982. l1p = hw->l1;
  983. while (idx < hw->driver_data.max_st_ports) {
  984. if (hw->mr.timer_irq) {
  985. *fifo_stat |= hw->mr.fifo_rx_trans_enables[idx];
  986. if (hw->fifo_sched_cnt <= 0) {
  987. *fifo_stat |=
  988. hw->mr.fifo_slow_timer_service[l1p->
  989. st_num];
  990. }
  991. }
  992. /* ignore fifo 6 (TX E fifo) */
  993. *fifo_stat &= 0xff - 0x40;
  994. while (*fifo_stat) {
  995. if (!l1p->nt_mode) {
  996. /* RX Fifo has data to read */
  997. if ((*fifo_stat & 0x20)) {
  998. *fifo_stat &= ~0x20;
  999. rx_d_frame(l1p, 0);
  1000. }
  1001. /* E Fifo has data to read */
  1002. if ((*fifo_stat & 0x80)) {
  1003. *fifo_stat &= ~0x80;
  1004. rx_d_frame(l1p, 1);
  1005. }
  1006. /* TX Fifo completed send */
  1007. if ((*fifo_stat & 0x10)) {
  1008. *fifo_stat &= ~0x10;
  1009. tx_d_frame(l1p);
  1010. }
  1011. }
  1012. /* B1 RX Fifo has data to read */
  1013. if ((*fifo_stat & 0x2)) {
  1014. *fifo_stat &= ~0x2;
  1015. rx_b_frame(l1p->b_ch);
  1016. }
  1017. /* B1 TX Fifo has send completed */
  1018. if ((*fifo_stat & 0x1)) {
  1019. *fifo_stat &= ~0x1;
  1020. tx_b_frame(l1p->b_ch);
  1021. }
  1022. /* B2 RX Fifo has data to read */
  1023. if ((*fifo_stat & 0x8)) {
  1024. *fifo_stat &= ~0x8;
  1025. rx_b_frame(l1p->b_ch + 1);
  1026. }
  1027. /* B2 TX Fifo has send completed */
  1028. if ((*fifo_stat & 0x4)) {
  1029. *fifo_stat &= ~0x4;
  1030. tx_b_frame(l1p->b_ch + 1);
  1031. }
  1032. }
  1033. fifo_stat++;
  1034. l1p++;
  1035. idx++;
  1036. }
  1037. if (hw->fifo_sched_cnt <= 0)
  1038. hw->fifo_sched_cnt += (1 << (7 - TRANS_TIMER_MODE));
  1039. hw->mr.timer_irq = 0; /* clear requested timer irq */
  1040. } /* hfc4s8s_bh */
  1041. /*********************/
  1042. /* interrupt handler */
  1043. /*********************/
  1044. static irqreturn_t
  1045. hfc4s8s_interrupt(int intno, void *dev_id)
  1046. {
  1047. hfc4s8s_hw *hw = dev_id;
  1048. u_char b, ovr;
  1049. volatile u_char *ovp;
  1050. int idx;
  1051. u_char old_ioreg;
  1052. if (!hw || !(hw->mr.r_irq_ctrl & M_GLOB_IRQ_EN))
  1053. return IRQ_NONE;
  1054. /* read current selected regsister */
  1055. old_ioreg = GetRegAddr(hw);
  1056. /* Layer 1 State change */
  1057. hw->mr.r_irq_statech |=
  1058. (Read_hfc8(hw, R_SCI) & hw->mr.r_irqmsk_statchg);
  1059. if (!
  1060. (b = (Read_hfc8(hw, R_STATUS) & (M_MISC_IRQSTA | M_FR_IRQSTA)))
  1061. && !hw->mr.r_irq_statech) {
  1062. SetRegAddr(hw, old_ioreg);
  1063. return IRQ_NONE;
  1064. }
  1065. /* timer event */
  1066. if (Read_hfc8(hw, R_IRQ_MISC) & M_TI_IRQ) {
  1067. hw->mr.timer_irq = 1;
  1068. hw->fifo_sched_cnt--;
  1069. }
  1070. /* FIFO event */
  1071. if ((ovr = Read_hfc8(hw, R_IRQ_OVIEW))) {
  1072. hw->mr.r_irq_oview |= ovr;
  1073. idx = R_IRQ_FIFO_BL0;
  1074. ovp = hw->mr.r_irq_fifo_blx;
  1075. while (ovr) {
  1076. if ((ovr & 1)) {
  1077. *ovp |= Read_hfc8(hw, idx);
  1078. }
  1079. ovp++;
  1080. idx++;
  1081. ovr >>= 1;
  1082. }
  1083. }
  1084. /* queue the request to allow other cards to interrupt */
  1085. schedule_work(&hw->tqueue);
  1086. SetRegAddr(hw, old_ioreg);
  1087. return IRQ_HANDLED;
  1088. } /* hfc4s8s_interrupt */
  1089. /***********************************************************************/
  1090. /* reset the complete chip, don't release the chips irq but disable it */
  1091. /***********************************************************************/
  1092. static void
  1093. chipreset(hfc4s8s_hw *hw)
  1094. {
  1095. u_long flags;
  1096. spin_lock_irqsave(&hw->lock, flags);
  1097. Write_hfc8(hw, R_CTRL, 0); /* use internal RAM */
  1098. Write_hfc8(hw, R_RAM_MISC, 0); /* 32k*8 RAM */
  1099. Write_hfc8(hw, R_FIFO_MD, 0); /* fifo mode 386 byte/fifo simple mode */
  1100. Write_hfc8(hw, R_CIRM, M_SRES); /* reset chip */
  1101. hw->mr.r_irq_ctrl = 0; /* interrupt is inactive */
  1102. spin_unlock_irqrestore(&hw->lock, flags);
  1103. udelay(3);
  1104. Write_hfc8(hw, R_CIRM, 0); /* disable reset */
  1105. wait_busy(hw);
  1106. Write_hfc8(hw, R_PCM_MD0, M_PCM_MD); /* master mode */
  1107. Write_hfc8(hw, R_RAM_MISC, M_FZ_MD); /* transmit fifo option */
  1108. if (hw->driver_data.clock_mode == 1)
  1109. Write_hfc8(hw, R_BRG_PCM_CFG, M_PCM_CLK); /* PCM clk / 2 */
  1110. Write_hfc8(hw, R_TI_WD, TRANS_TIMER_MODE); /* timer interval */
  1111. memset(&hw->mr, 0, sizeof(hw->mr));
  1112. } /* chipreset */
  1113. /********************************************/
  1114. /* disable/enable hardware in nt or te mode */
  1115. /********************************************/
  1116. static void
  1117. hfc_hardware_enable(hfc4s8s_hw *hw, int enable, int nt_mode)
  1118. {
  1119. u_long flags;
  1120. char if_name[40];
  1121. int i;
  1122. if (enable) {
  1123. /* save system vars */
  1124. hw->nt_mode = nt_mode;
  1125. /* enable fifo and state irqs, but not global irq enable */
  1126. hw->mr.r_irq_ctrl = M_FIFO_IRQ;
  1127. Write_hfc8(hw, R_IRQ_CTRL, hw->mr.r_irq_ctrl);
  1128. hw->mr.r_irqmsk_statchg = 0;
  1129. Write_hfc8(hw, R_SCI_MSK, hw->mr.r_irqmsk_statchg);
  1130. Write_hfc8(hw, R_PWM_MD, 0x80);
  1131. Write_hfc8(hw, R_PWM1, 26);
  1132. if (!nt_mode)
  1133. Write_hfc8(hw, R_ST_SYNC, M_AUTO_SYNC);
  1134. /* enable the line interfaces and fifos */
  1135. for (i = 0; i < hw->driver_data.max_st_ports; i++) {
  1136. hw->mr.r_irqmsk_statchg |= (1 << i);
  1137. Write_hfc8(hw, R_SCI_MSK, hw->mr.r_irqmsk_statchg);
  1138. Write_hfc8(hw, R_ST_SEL, i);
  1139. Write_hfc8(hw, A_ST_CLK_DLY,
  1140. ((nt_mode) ? CLKDEL_NT : CLKDEL_TE));
  1141. hw->mr.r_ctrl0 = ((nt_mode) ? CTRL0_NT : CTRL0_TE);
  1142. Write_hfc8(hw, A_ST_CTRL0, hw->mr.r_ctrl0);
  1143. Write_hfc8(hw, A_ST_CTRL2, 3);
  1144. Write_hfc8(hw, A_ST_WR_STA, 0); /* enable state machine */
  1145. hw->l1[i].enabled = 1;
  1146. hw->l1[i].nt_mode = nt_mode;
  1147. if (!nt_mode) {
  1148. /* setup E-fifo */
  1149. Write_hfc8(hw, R_FIFO, i * 8 + 7); /* E fifo */
  1150. wait_busy(hw);
  1151. Write_hfc8(hw, A_CON_HDLC, 0x11); /* HDLC mode, 1 fill, connect ST */
  1152. Write_hfc8(hw, A_SUBCH_CFG, 2); /* only 2 bits */
  1153. Write_hfc8(hw, A_IRQ_MSK, 1); /* enable interrupt */
  1154. Write_hfc8(hw, A_INC_RES_FIFO, 2); /* reset fifo */
  1155. wait_busy(hw);
  1156. /* setup D RX-fifo */
  1157. Write_hfc8(hw, R_FIFO, i * 8 + 5); /* RX fifo */
  1158. wait_busy(hw);
  1159. Write_hfc8(hw, A_CON_HDLC, 0x11); /* HDLC mode, 1 fill, connect ST */
  1160. Write_hfc8(hw, A_SUBCH_CFG, 2); /* only 2 bits */
  1161. Write_hfc8(hw, A_IRQ_MSK, 1); /* enable interrupt */
  1162. Write_hfc8(hw, A_INC_RES_FIFO, 2); /* reset fifo */
  1163. wait_busy(hw);
  1164. /* setup D TX-fifo */
  1165. Write_hfc8(hw, R_FIFO, i * 8 + 4); /* TX fifo */
  1166. wait_busy(hw);
  1167. Write_hfc8(hw, A_CON_HDLC, 0x11); /* HDLC mode, 1 fill, connect ST */
  1168. Write_hfc8(hw, A_SUBCH_CFG, 2); /* only 2 bits */
  1169. Write_hfc8(hw, A_IRQ_MSK, 1); /* enable interrupt */
  1170. Write_hfc8(hw, A_INC_RES_FIFO, 2); /* reset fifo */
  1171. wait_busy(hw);
  1172. }
  1173. sprintf(if_name, "hfc4s8s_%d%d_", hw->cardnum, i);
  1174. if (hisax_register
  1175. (&hw->l1[i].d_if, hw->l1[i].b_table, if_name,
  1176. ((nt_mode) ? 3 : 2))) {
  1177. hw->l1[i].enabled = 0;
  1178. hw->mr.r_irqmsk_statchg &= ~(1 << i);
  1179. Write_hfc8(hw, R_SCI_MSK,
  1180. hw->mr.r_irqmsk_statchg);
  1181. printk(KERN_INFO
  1182. "HFC-4S/8S: Unable to register S/T device %s, break\n",
  1183. if_name);
  1184. break;
  1185. }
  1186. }
  1187. spin_lock_irqsave(&hw->lock, flags);
  1188. hw->mr.r_irq_ctrl |= M_GLOB_IRQ_EN;
  1189. Write_hfc8(hw, R_IRQ_CTRL, hw->mr.r_irq_ctrl);
  1190. spin_unlock_irqrestore(&hw->lock, flags);
  1191. } else {
  1192. /* disable hardware */
  1193. spin_lock_irqsave(&hw->lock, flags);
  1194. hw->mr.r_irq_ctrl &= ~M_GLOB_IRQ_EN;
  1195. Write_hfc8(hw, R_IRQ_CTRL, hw->mr.r_irq_ctrl);
  1196. spin_unlock_irqrestore(&hw->lock, flags);
  1197. for (i = hw->driver_data.max_st_ports - 1; i >= 0; i--) {
  1198. hw->l1[i].enabled = 0;
  1199. hisax_unregister(&hw->l1[i].d_if);
  1200. del_timer(&hw->l1[i].l1_timer);
  1201. skb_queue_purge(&hw->l1[i].d_tx_queue);
  1202. skb_queue_purge(&hw->l1[i].b_ch[0].tx_queue);
  1203. skb_queue_purge(&hw->l1[i].b_ch[1].tx_queue);
  1204. }
  1205. chipreset(hw);
  1206. }
  1207. } /* hfc_hardware_enable */
  1208. /******************************************/
  1209. /* disable memory mapped ports / io ports */
  1210. /******************************************/
  1211. static void
  1212. release_pci_ports(hfc4s8s_hw *hw)
  1213. {
  1214. pci_write_config_word(hw->pdev, PCI_COMMAND, 0);
  1215. if (hw->iobase)
  1216. release_region(hw->iobase, 8);
  1217. }
  1218. /*****************************************/
  1219. /* enable memory mapped ports / io ports */
  1220. /*****************************************/
  1221. static void
  1222. enable_pci_ports(hfc4s8s_hw *hw)
  1223. {
  1224. pci_write_config_word(hw->pdev, PCI_COMMAND, PCI_ENA_REGIO);
  1225. }
  1226. /*************************************/
  1227. /* initialise the HFC-4s/8s hardware */
  1228. /* return 0 on success. */
  1229. /*************************************/
  1230. static int
  1231. setup_instance(hfc4s8s_hw *hw)
  1232. {
  1233. int err = -EIO;
  1234. int i;
  1235. for (i = 0; i < HFC_MAX_ST; i++) {
  1236. struct hfc4s8s_l1 *l1p;
  1237. l1p = hw->l1 + i;
  1238. spin_lock_init(&l1p->lock);
  1239. l1p->hw = hw;
  1240. timer_setup(&l1p->l1_timer, hfc_l1_timer, 0);
  1241. l1p->st_num = i;
  1242. skb_queue_head_init(&l1p->d_tx_queue);
  1243. l1p->d_if.ifc.priv = hw->l1 + i;
  1244. l1p->d_if.ifc.l2l1 = (void *) dch_l2l1;
  1245. spin_lock_init(&l1p->b_ch[0].lock);
  1246. l1p->b_ch[0].b_if.ifc.l2l1 = (void *) bch_l2l1;
  1247. l1p->b_ch[0].b_if.ifc.priv = (void *) &l1p->b_ch[0];
  1248. l1p->b_ch[0].l1p = hw->l1 + i;
  1249. l1p->b_ch[0].bchan = 1;
  1250. l1p->b_table[0] = &l1p->b_ch[0].b_if;
  1251. skb_queue_head_init(&l1p->b_ch[0].tx_queue);
  1252. spin_lock_init(&l1p->b_ch[1].lock);
  1253. l1p->b_ch[1].b_if.ifc.l2l1 = (void *) bch_l2l1;
  1254. l1p->b_ch[1].b_if.ifc.priv = (void *) &l1p->b_ch[1];
  1255. l1p->b_ch[1].l1p = hw->l1 + i;
  1256. l1p->b_ch[1].bchan = 2;
  1257. l1p->b_table[1] = &l1p->b_ch[1].b_if;
  1258. skb_queue_head_init(&l1p->b_ch[1].tx_queue);
  1259. }
  1260. enable_pci_ports(hw);
  1261. chipreset(hw);
  1262. i = Read_hfc8(hw, R_CHIP_ID) >> CHIP_ID_SHIFT;
  1263. if (i != hw->driver_data.chip_id) {
  1264. printk(KERN_INFO
  1265. "HFC-4S/8S: invalid chip id 0x%x instead of 0x%x, card ignored\n",
  1266. i, hw->driver_data.chip_id);
  1267. goto out;
  1268. }
  1269. i = Read_hfc8(hw, R_CHIP_RV) & 0xf;
  1270. if (!i) {
  1271. printk(KERN_INFO
  1272. "HFC-4S/8S: chip revision 0 not supported, card ignored\n");
  1273. goto out;
  1274. }
  1275. INIT_WORK(&hw->tqueue, hfc4s8s_bh);
  1276. if (request_irq
  1277. (hw->irq, hfc4s8s_interrupt, IRQF_SHARED, hw->card_name, hw)) {
  1278. printk(KERN_INFO
  1279. "HFC-4S/8S: unable to alloc irq %d, card ignored\n",
  1280. hw->irq);
  1281. goto out;
  1282. }
  1283. printk(KERN_INFO
  1284. "HFC-4S/8S: found PCI card at iobase 0x%x, irq %d\n",
  1285. hw->iobase, hw->irq);
  1286. hfc_hardware_enable(hw, 1, 0);
  1287. return (0);
  1288. out:
  1289. hw->irq = 0;
  1290. release_pci_ports(hw);
  1291. kfree(hw);
  1292. return (err);
  1293. }
  1294. /*****************************************/
  1295. /* PCI hotplug interface: probe new card */
  1296. /*****************************************/
  1297. static int
  1298. hfc4s8s_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  1299. {
  1300. int err = -ENOMEM;
  1301. hfc4s8s_param *driver_data = (hfc4s8s_param *) ent->driver_data;
  1302. hfc4s8s_hw *hw;
  1303. if (!(hw = kzalloc(sizeof(hfc4s8s_hw), GFP_ATOMIC))) {
  1304. printk(KERN_ERR "No kmem for HFC-4S/8S card\n");
  1305. return (err);
  1306. }
  1307. hw->pdev = pdev;
  1308. err = pci_enable_device(pdev);
  1309. if (err)
  1310. goto out;
  1311. hw->cardnum = card_cnt;
  1312. sprintf(hw->card_name, "hfc4s8s_%d", hw->cardnum);
  1313. printk(KERN_INFO "HFC-4S/8S: found adapter %s (%s) at %s\n",
  1314. driver_data->device_name, hw->card_name, pci_name(pdev));
  1315. spin_lock_init(&hw->lock);
  1316. hw->driver_data = *driver_data;
  1317. hw->irq = pdev->irq;
  1318. hw->iobase = pci_resource_start(pdev, 0);
  1319. if (!request_region(hw->iobase, 8, hw->card_name)) {
  1320. printk(KERN_INFO
  1321. "HFC-4S/8S: failed to request address space at 0x%04x\n",
  1322. hw->iobase);
  1323. err = -EBUSY;
  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);