hfc4s8s_l1.c 43 KB

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