hfc4s8s_l1.c 43 KB

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