hfc4s8s_l1.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714
  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. int mode = (int) 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. int 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, ack_len = 0;
  870. if (!l1->enabled || (bch->mode == L1_MODE_NULL))
  871. return;
  872. /* TX fifo */
  873. Write_hfc8(l1->hw, R_FIFO,
  874. (l1->st_num * 8 + ((bch->bchan == 1) ? 0 : 2)));
  875. wait_busy(l1->hw);
  876. do {
  877. if (bch->mode == L1_MODE_HDLC) {
  878. hdlc_num = Read_hfc8(l1->hw, A_F1) & MAX_F_CNT;
  879. hdlc_num -=
  880. (Read_hfc8_stable(l1->hw, A_F2) & MAX_F_CNT);
  881. if (hdlc_num < 0)
  882. hdlc_num += 16;
  883. if (hdlc_num >= 15)
  884. break; /* fifo still filled up with hdlc frames */
  885. } else
  886. hdlc_num = 0;
  887. if (!(skb = bch->tx_skb)) {
  888. if (!(skb = skb_dequeue(&bch->tx_queue))) {
  889. l1->hw->mr.fifo_slow_timer_service[l1->
  890. st_num]
  891. &= ~((bch->bchan == 1) ? 1 : 4);
  892. break; /* list empty */
  893. }
  894. bch->tx_skb = skb;
  895. bch->tx_cnt = 0;
  896. }
  897. if (!hdlc_num)
  898. l1->hw->mr.fifo_slow_timer_service[l1->st_num] |=
  899. ((bch->bchan == 1) ? 1 : 4);
  900. else
  901. l1->hw->mr.fifo_slow_timer_service[l1->st_num] &=
  902. ~((bch->bchan == 1) ? 1 : 4);
  903. max = Read_hfc16_stable(l1->hw, A_Z2);
  904. max -= Read_hfc16(l1->hw, A_Z1);
  905. if (max <= 0)
  906. max += 384;
  907. max--;
  908. if (max < 16)
  909. break; /* don't write to small amounts of bytes */
  910. cnt = skb->len - bch->tx_cnt;
  911. if (cnt > max)
  912. cnt = max;
  913. cp = skb->data + bch->tx_cnt;
  914. bch->tx_cnt += cnt;
  915. #ifndef CONFIG_HISAX_HFC4S8S_PCIMEM
  916. SetRegAddr(l1->hw, A_FIFO_DATA0);
  917. #endif
  918. while (cnt >= 4) {
  919. #ifdef CONFIG_HISAX_HFC4S8S_PCIMEM
  920. fWrite_hfc32(l1->hw, A_FIFO_DATA0,
  921. *(unsigned long *) cp);
  922. #else
  923. fWrite_hfc32(l1->hw, *(unsigned long *) cp);
  924. #endif
  925. cp += 4;
  926. cnt -= 4;
  927. }
  928. while (cnt--)
  929. #ifdef CONFIG_HISAX_HFC4S8S_PCIMEM
  930. fWrite_hfc8(l1->hw, A_FIFO_DATA0, *cp++);
  931. #else
  932. fWrite_hfc8(l1->hw, *cp++);
  933. #endif
  934. if (bch->tx_cnt >= skb->len) {
  935. if (bch->mode == L1_MODE_HDLC) {
  936. /* increment f counter */
  937. Write_hfc8(l1->hw, A_INC_RES_FIFO, 1);
  938. }
  939. ack_len += skb->truesize;
  940. bch->tx_skb = NULL;
  941. bch->tx_cnt = 0;
  942. dev_kfree_skb(skb);
  943. } else
  944. /* Re-Select */
  945. Write_hfc8(l1->hw, R_FIFO,
  946. (l1->st_num * 8 +
  947. ((bch->bchan == 1) ? 0 : 2)));
  948. wait_busy(l1->hw);
  949. } while (1);
  950. if (ack_len)
  951. bch->b_if.ifc.l1l2((struct hisax_if *) &bch->b_if,
  952. PH_DATA | CONFIRM, (void *) ack_len);
  953. } /* tx_b_frame */
  954. /*************************************/
  955. /* bottom half handler for interrupt */
  956. /*************************************/
  957. static void
  958. hfc4s8s_bh(hfc4s8s_hw * hw)
  959. {
  960. u_char b;
  961. struct hfc4s8s_l1 *l1p;
  962. volatile u_char *fifo_stat;
  963. int idx;
  964. /* handle layer 1 state changes */
  965. b = 1;
  966. l1p = hw->l1;
  967. while (b) {
  968. if ((b & hw->mr.r_irq_statech)) {
  969. /* reset l1 event */
  970. hw->mr.r_irq_statech &= ~b;
  971. if (l1p->enabled) {
  972. if (l1p->nt_mode) {
  973. u_char oldstate = l1p->l1_state;
  974. Write_hfc8(l1p->hw, R_ST_SEL,
  975. l1p->st_num);
  976. l1p->l1_state =
  977. Read_hfc8(l1p->hw,
  978. A_ST_RD_STA) & 0xf;
  979. if ((oldstate == 3)
  980. && (l1p->l1_state != 3))
  981. l1p->d_if.ifc.l1l2(&l1p->
  982. d_if.
  983. ifc,
  984. PH_DEACTIVATE
  985. |
  986. INDICATION,
  987. NULL);
  988. if (l1p->l1_state != 2) {
  989. del_timer(&l1p->l1_timer);
  990. if (l1p->l1_state == 3) {
  991. l1p->d_if.ifc.
  992. l1l2(&l1p->
  993. d_if.ifc,
  994. PH_ACTIVATE
  995. |
  996. INDICATION,
  997. NULL);
  998. }
  999. } else {
  1000. /* allow transition */
  1001. Write_hfc8(hw, A_ST_WR_STA,
  1002. M_SET_G2_G3);
  1003. mod_timer(&l1p->l1_timer,
  1004. jiffies +
  1005. L1_TIMER_T1);
  1006. }
  1007. printk(KERN_INFO
  1008. "HFC-4S/8S: NT ch %d l1 state %d -> %d\n",
  1009. l1p->st_num, oldstate,
  1010. l1p->l1_state);
  1011. } else {
  1012. u_char oldstate = l1p->l1_state;
  1013. Write_hfc8(l1p->hw, R_ST_SEL,
  1014. l1p->st_num);
  1015. l1p->l1_state =
  1016. Read_hfc8(l1p->hw,
  1017. A_ST_RD_STA) & 0xf;
  1018. if (((l1p->l1_state == 3) &&
  1019. ((oldstate == 7) ||
  1020. (oldstate == 8))) ||
  1021. ((timer_pending
  1022. (&l1p->l1_timer))
  1023. && (l1p->l1_state == 8))) {
  1024. mod_timer(&l1p->l1_timer,
  1025. L1_TIMER_T4 +
  1026. jiffies);
  1027. } else {
  1028. if (l1p->l1_state == 7) {
  1029. del_timer(&l1p->
  1030. l1_timer);
  1031. l1p->d_if.ifc.
  1032. l1l2(&l1p->
  1033. d_if.ifc,
  1034. PH_ACTIVATE
  1035. |
  1036. INDICATION,
  1037. NULL);
  1038. tx_d_frame(l1p);
  1039. }
  1040. if (l1p->l1_state == 3) {
  1041. if (oldstate != 3)
  1042. l1p->d_if.
  1043. ifc.
  1044. l1l2
  1045. (&l1p->
  1046. d_if.
  1047. ifc,
  1048. PH_DEACTIVATE
  1049. |
  1050. INDICATION,
  1051. NULL);
  1052. }
  1053. }
  1054. printk(KERN_INFO
  1055. "HFC-4S/8S: TE %d ch %d l1 state %d -> %d\n",
  1056. l1p->hw->cardnum,
  1057. l1p->st_num, oldstate,
  1058. l1p->l1_state);
  1059. }
  1060. }
  1061. }
  1062. b <<= 1;
  1063. l1p++;
  1064. }
  1065. /* now handle the fifos */
  1066. idx = 0;
  1067. fifo_stat = hw->mr.r_irq_fifo_blx;
  1068. l1p = hw->l1;
  1069. while (idx < hw->driver_data.max_st_ports) {
  1070. if (hw->mr.timer_irq) {
  1071. *fifo_stat |= hw->mr.fifo_rx_trans_enables[idx];
  1072. if (hw->fifo_sched_cnt <= 0) {
  1073. *fifo_stat |=
  1074. hw->mr.fifo_slow_timer_service[l1p->
  1075. st_num];
  1076. }
  1077. }
  1078. /* ignore fifo 6 (TX E fifo) */
  1079. *fifo_stat &= 0xff - 0x40;
  1080. while (*fifo_stat) {
  1081. if (!l1p->nt_mode) {
  1082. /* RX Fifo has data to read */
  1083. if ((*fifo_stat & 0x20)) {
  1084. *fifo_stat &= ~0x20;
  1085. rx_d_frame(l1p, 0);
  1086. }
  1087. /* E Fifo has data to read */
  1088. if ((*fifo_stat & 0x80)) {
  1089. *fifo_stat &= ~0x80;
  1090. rx_d_frame(l1p, 1);
  1091. }
  1092. /* TX Fifo completed send */
  1093. if ((*fifo_stat & 0x10)) {
  1094. *fifo_stat &= ~0x10;
  1095. tx_d_frame(l1p);
  1096. }
  1097. }
  1098. /* B1 RX Fifo has data to read */
  1099. if ((*fifo_stat & 0x2)) {
  1100. *fifo_stat &= ~0x2;
  1101. rx_b_frame(l1p->b_ch);
  1102. }
  1103. /* B1 TX Fifo has send completed */
  1104. if ((*fifo_stat & 0x1)) {
  1105. *fifo_stat &= ~0x1;
  1106. tx_b_frame(l1p->b_ch);
  1107. }
  1108. /* B2 RX Fifo has data to read */
  1109. if ((*fifo_stat & 0x8)) {
  1110. *fifo_stat &= ~0x8;
  1111. rx_b_frame(l1p->b_ch + 1);
  1112. }
  1113. /* B2 TX Fifo has send completed */
  1114. if ((*fifo_stat & 0x4)) {
  1115. *fifo_stat &= ~0x4;
  1116. tx_b_frame(l1p->b_ch + 1);
  1117. }
  1118. }
  1119. fifo_stat++;
  1120. l1p++;
  1121. idx++;
  1122. }
  1123. if (hw->fifo_sched_cnt <= 0)
  1124. hw->fifo_sched_cnt += (1 << (7 - TRANS_TIMER_MODE));
  1125. hw->mr.timer_irq = 0; /* clear requested timer irq */
  1126. } /* hfc4s8s_bh */
  1127. /*********************/
  1128. /* interrupt handler */
  1129. /*********************/
  1130. static irqreturn_t
  1131. hfc4s8s_interrupt(int intno, void *dev_id, struct pt_regs *regs)
  1132. {
  1133. hfc4s8s_hw *hw = dev_id;
  1134. u_char b, ovr;
  1135. volatile u_char *ovp;
  1136. int idx;
  1137. u_char old_ioreg;
  1138. if (!hw || !(hw->mr.r_irq_ctrl & M_GLOB_IRQ_EN))
  1139. return IRQ_NONE;
  1140. #ifndef CONFIG_HISAX_HFC4S8S_PCIMEM
  1141. /* read current selected regsister */
  1142. old_ioreg = GetRegAddr(hw);
  1143. #endif
  1144. /* Layer 1 State change */
  1145. hw->mr.r_irq_statech |=
  1146. (Read_hfc8(hw, R_SCI) & hw->mr.r_irqmsk_statchg);
  1147. if (!
  1148. (b = (Read_hfc8(hw, R_STATUS) & (M_MISC_IRQSTA | M_FR_IRQSTA)))
  1149. && !hw->mr.r_irq_statech) {
  1150. #ifndef CONFIG_HISAX_HFC4S8S_PCIMEM
  1151. SetRegAddr(hw, old_ioreg);
  1152. #endif
  1153. return IRQ_NONE;
  1154. }
  1155. /* timer event */
  1156. if (Read_hfc8(hw, R_IRQ_MISC) & M_TI_IRQ) {
  1157. hw->mr.timer_irq = 1;
  1158. hw->fifo_sched_cnt--;
  1159. }
  1160. /* FIFO event */
  1161. if ((ovr = Read_hfc8(hw, R_IRQ_OVIEW))) {
  1162. hw->mr.r_irq_oview |= ovr;
  1163. idx = R_IRQ_FIFO_BL0;
  1164. ovp = hw->mr.r_irq_fifo_blx;
  1165. while (ovr) {
  1166. if ((ovr & 1)) {
  1167. *ovp |= Read_hfc8(hw, idx);
  1168. }
  1169. ovp++;
  1170. idx++;
  1171. ovr >>= 1;
  1172. }
  1173. }
  1174. /* queue the request to allow other cards to interrupt */
  1175. schedule_work(&hw->tqueue);
  1176. #ifndef CONFIG_HISAX_HFC4S8S_PCIMEM
  1177. SetRegAddr(hw, old_ioreg);
  1178. #endif
  1179. return IRQ_HANDLED;
  1180. } /* hfc4s8s_interrupt */
  1181. /***********************************************************************/
  1182. /* reset the complete chip, don't release the chips irq but disable it */
  1183. /***********************************************************************/
  1184. static void
  1185. chipreset(hfc4s8s_hw * hw)
  1186. {
  1187. u_long flags;
  1188. spin_lock_irqsave(&hw->lock, flags);
  1189. Write_hfc8(hw, R_CTRL, 0); /* use internal RAM */
  1190. Write_hfc8(hw, R_RAM_MISC, 0); /* 32k*8 RAM */
  1191. Write_hfc8(hw, R_FIFO_MD, 0); /* fifo mode 386 byte/fifo simple mode */
  1192. Write_hfc8(hw, R_CIRM, M_SRES); /* reset chip */
  1193. hw->mr.r_irq_ctrl = 0; /* interrupt is inactive */
  1194. spin_unlock_irqrestore(&hw->lock, flags);
  1195. udelay(3);
  1196. Write_hfc8(hw, R_CIRM, 0); /* disable reset */
  1197. wait_busy(hw);
  1198. Write_hfc8(hw, R_PCM_MD0, M_PCM_MD); /* master mode */
  1199. Write_hfc8(hw, R_RAM_MISC, M_FZ_MD); /* transmit fifo option */
  1200. if (hw->driver_data.clock_mode == 1)
  1201. Write_hfc8(hw, R_BRG_PCM_CFG, M_PCM_CLK); /* PCM clk / 2 */
  1202. Write_hfc8(hw, R_TI_WD, TRANS_TIMER_MODE); /* timer interval */
  1203. memset(&hw->mr, 0, sizeof(hw->mr));
  1204. } /* chipreset */
  1205. /********************************************/
  1206. /* disable/enable hardware in nt or te mode */
  1207. /********************************************/
  1208. static void
  1209. hfc_hardware_enable(hfc4s8s_hw * hw, int enable, int nt_mode)
  1210. {
  1211. u_long flags;
  1212. char if_name[40];
  1213. int i;
  1214. if (enable) {
  1215. /* save system vars */
  1216. hw->nt_mode = nt_mode;
  1217. /* enable fifo and state irqs, but not global irq enable */
  1218. hw->mr.r_irq_ctrl = M_FIFO_IRQ;
  1219. Write_hfc8(hw, R_IRQ_CTRL, hw->mr.r_irq_ctrl);
  1220. hw->mr.r_irqmsk_statchg = 0;
  1221. Write_hfc8(hw, R_SCI_MSK, hw->mr.r_irqmsk_statchg);
  1222. Write_hfc8(hw, R_PWM_MD, 0x80);
  1223. Write_hfc8(hw, R_PWM1, 26);
  1224. if (!nt_mode)
  1225. Write_hfc8(hw, R_ST_SYNC, M_AUTO_SYNC);
  1226. /* enable the line interfaces and fifos */
  1227. for (i = 0; i < hw->driver_data.max_st_ports; i++) {
  1228. hw->mr.r_irqmsk_statchg |= (1 << i);
  1229. Write_hfc8(hw, R_SCI_MSK, hw->mr.r_irqmsk_statchg);
  1230. Write_hfc8(hw, R_ST_SEL, i);
  1231. Write_hfc8(hw, A_ST_CLK_DLY,
  1232. ((nt_mode) ? CLKDEL_NT : CLKDEL_TE));
  1233. hw->mr.r_ctrl0 = ((nt_mode) ? CTRL0_NT : CTRL0_TE);
  1234. Write_hfc8(hw, A_ST_CTRL0, hw->mr.r_ctrl0);
  1235. Write_hfc8(hw, A_ST_CTRL2, 3);
  1236. Write_hfc8(hw, A_ST_WR_STA, 0); /* enable state machine */
  1237. hw->l1[i].enabled = 1;
  1238. hw->l1[i].nt_mode = nt_mode;
  1239. if (!nt_mode) {
  1240. /* setup E-fifo */
  1241. Write_hfc8(hw, R_FIFO, i * 8 + 7); /* E fifo */
  1242. wait_busy(hw);
  1243. Write_hfc8(hw, A_CON_HDLC, 0x11); /* HDLC mode, 1 fill, connect ST */
  1244. Write_hfc8(hw, A_SUBCH_CFG, 2); /* only 2 bits */
  1245. Write_hfc8(hw, A_IRQ_MSK, 1); /* enable interrupt */
  1246. Write_hfc8(hw, A_INC_RES_FIFO, 2); /* reset fifo */
  1247. wait_busy(hw);
  1248. /* setup D RX-fifo */
  1249. Write_hfc8(hw, R_FIFO, i * 8 + 5); /* RX fifo */
  1250. wait_busy(hw);
  1251. Write_hfc8(hw, A_CON_HDLC, 0x11); /* HDLC mode, 1 fill, connect ST */
  1252. Write_hfc8(hw, A_SUBCH_CFG, 2); /* only 2 bits */
  1253. Write_hfc8(hw, A_IRQ_MSK, 1); /* enable interrupt */
  1254. Write_hfc8(hw, A_INC_RES_FIFO, 2); /* reset fifo */
  1255. wait_busy(hw);
  1256. /* setup D TX-fifo */
  1257. Write_hfc8(hw, R_FIFO, i * 8 + 4); /* TX fifo */
  1258. wait_busy(hw);
  1259. Write_hfc8(hw, A_CON_HDLC, 0x11); /* HDLC mode, 1 fill, connect ST */
  1260. Write_hfc8(hw, A_SUBCH_CFG, 2); /* only 2 bits */
  1261. Write_hfc8(hw, A_IRQ_MSK, 1); /* enable interrupt */
  1262. Write_hfc8(hw, A_INC_RES_FIFO, 2); /* reset fifo */
  1263. wait_busy(hw);
  1264. }
  1265. sprintf(if_name, "hfc4s8s_%d%d_", hw->cardnum, i);
  1266. if (hisax_register
  1267. (&hw->l1[i].d_if, hw->l1[i].b_table, if_name,
  1268. ((nt_mode) ? 3 : 2))) {
  1269. hw->l1[i].enabled = 0;
  1270. hw->mr.r_irqmsk_statchg &= ~(1 << i);
  1271. Write_hfc8(hw, R_SCI_MSK,
  1272. hw->mr.r_irqmsk_statchg);
  1273. printk(KERN_INFO
  1274. "HFC-4S/8S: Unable to register S/T device %s, break\n",
  1275. if_name);
  1276. break;
  1277. }
  1278. }
  1279. spin_lock_irqsave(&hw->lock, flags);
  1280. hw->mr.r_irq_ctrl |= M_GLOB_IRQ_EN;
  1281. Write_hfc8(hw, R_IRQ_CTRL, hw->mr.r_irq_ctrl);
  1282. spin_unlock_irqrestore(&hw->lock, flags);
  1283. } else {
  1284. /* disable hardware */
  1285. spin_lock_irqsave(&hw->lock, flags);
  1286. hw->mr.r_irq_ctrl &= ~M_GLOB_IRQ_EN;
  1287. Write_hfc8(hw, R_IRQ_CTRL, hw->mr.r_irq_ctrl);
  1288. spin_unlock_irqrestore(&hw->lock, flags);
  1289. for (i = hw->driver_data.max_st_ports - 1; i >= 0; i--) {
  1290. hw->l1[i].enabled = 0;
  1291. hisax_unregister(&hw->l1[i].d_if);
  1292. del_timer(&hw->l1[i].l1_timer);
  1293. skb_queue_purge(&hw->l1[i].d_tx_queue);
  1294. skb_queue_purge(&hw->l1[i].b_ch[0].tx_queue);
  1295. skb_queue_purge(&hw->l1[i].b_ch[1].tx_queue);
  1296. }
  1297. chipreset(hw);
  1298. }
  1299. } /* hfc_hardware_enable */
  1300. /******************************************/
  1301. /* disable memory mapped ports / io ports */
  1302. /******************************************/
  1303. static void
  1304. release_pci_ports(hfc4s8s_hw * hw)
  1305. {
  1306. pci_write_config_word(hw->pdev, PCI_COMMAND, 0);
  1307. #ifdef CONFIG_HISAX_HFC4S8S_PCIMEM
  1308. if (hw->membase)
  1309. iounmap((void *) hw->membase);
  1310. #else
  1311. if (hw->iobase)
  1312. release_region(hw->iobase, 8);
  1313. #endif
  1314. }
  1315. /*****************************************/
  1316. /* enable memory mapped ports / io ports */
  1317. /*****************************************/
  1318. static void
  1319. enable_pci_ports(hfc4s8s_hw * hw)
  1320. {
  1321. #ifdef CONFIG_HISAX_HFC4S8S_PCIMEM
  1322. pci_write_config_word(hw->pdev, PCI_COMMAND, PCI_ENA_MEMIO);
  1323. #else
  1324. pci_write_config_word(hw->pdev, PCI_COMMAND, PCI_ENA_REGIO);
  1325. #endif
  1326. }
  1327. /*************************************/
  1328. /* initialise the HFC-4s/8s hardware */
  1329. /* return 0 on success. */
  1330. /*************************************/
  1331. static int __devinit
  1332. setup_instance(hfc4s8s_hw * hw)
  1333. {
  1334. int err = -EIO;
  1335. int i;
  1336. for (i = 0; i < HFC_MAX_ST; i++) {
  1337. struct hfc4s8s_l1 *l1p;
  1338. l1p = hw->l1 + i;
  1339. spin_lock_init(&l1p->lock);
  1340. l1p->hw = hw;
  1341. l1p->l1_timer.function = (void *) hfc_l1_timer;
  1342. l1p->l1_timer.data = (long) (l1p);
  1343. init_timer(&l1p->l1_timer);
  1344. l1p->st_num = i;
  1345. skb_queue_head_init(&l1p->d_tx_queue);
  1346. l1p->d_if.ifc.priv = hw->l1 + i;
  1347. l1p->d_if.ifc.l2l1 = (void *) dch_l2l1;
  1348. spin_lock_init(&l1p->b_ch[0].lock);
  1349. l1p->b_ch[0].b_if.ifc.l2l1 = (void *) bch_l2l1;
  1350. l1p->b_ch[0].b_if.ifc.priv = (void *) &l1p->b_ch[0];
  1351. l1p->b_ch[0].l1p = hw->l1 + i;
  1352. l1p->b_ch[0].bchan = 1;
  1353. l1p->b_table[0] = &l1p->b_ch[0].b_if;
  1354. skb_queue_head_init(&l1p->b_ch[0].tx_queue);
  1355. spin_lock_init(&l1p->b_ch[1].lock);
  1356. l1p->b_ch[1].b_if.ifc.l2l1 = (void *) bch_l2l1;
  1357. l1p->b_ch[1].b_if.ifc.priv = (void *) &l1p->b_ch[1];
  1358. l1p->b_ch[1].l1p = hw->l1 + i;
  1359. l1p->b_ch[1].bchan = 2;
  1360. l1p->b_table[1] = &l1p->b_ch[1].b_if;
  1361. skb_queue_head_init(&l1p->b_ch[1].tx_queue);
  1362. }
  1363. enable_pci_ports(hw);
  1364. chipreset(hw);
  1365. i = Read_hfc8(hw, R_CHIP_ID) >> CHIP_ID_SHIFT;
  1366. if (i != hw->driver_data.chip_id) {
  1367. printk(KERN_INFO
  1368. "HFC-4S/8S: invalid chip id 0x%x instead of 0x%x, card ignored\n",
  1369. i, hw->driver_data.chip_id);
  1370. goto out;
  1371. }
  1372. i = Read_hfc8(hw, R_CHIP_RV) & 0xf;
  1373. if (!i) {
  1374. printk(KERN_INFO
  1375. "HFC-4S/8S: chip revision 0 not supported, card ignored\n");
  1376. goto out;
  1377. }
  1378. INIT_WORK(&hw->tqueue, (void *) (void *) hfc4s8s_bh, hw);
  1379. if (request_irq
  1380. (hw->irq, hfc4s8s_interrupt, IRQF_SHARED, hw->card_name, hw)) {
  1381. printk(KERN_INFO
  1382. "HFC-4S/8S: unable to alloc irq %d, card ignored\n",
  1383. hw->irq);
  1384. goto out;
  1385. }
  1386. #ifdef CONFIG_HISAX_HFC4S8S_PCIMEM
  1387. printk(KERN_INFO
  1388. "HFC-4S/8S: found PCI card at membase 0x%p, irq %d\n",
  1389. hw->hw_membase, hw->irq);
  1390. #else
  1391. printk(KERN_INFO
  1392. "HFC-4S/8S: found PCI card at iobase 0x%x, irq %d\n",
  1393. hw->iobase, hw->irq);
  1394. #endif
  1395. hfc_hardware_enable(hw, 1, 0);
  1396. return (0);
  1397. out:
  1398. hw->irq = 0;
  1399. release_pci_ports(hw);
  1400. kfree(hw);
  1401. return (err);
  1402. }
  1403. /*****************************************/
  1404. /* PCI hotplug interface: probe new card */
  1405. /*****************************************/
  1406. static int __devinit
  1407. hfc4s8s_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  1408. {
  1409. int err = -ENOMEM;
  1410. hfc4s8s_param *driver_data = (hfc4s8s_param *) ent->driver_data;
  1411. hfc4s8s_hw *hw;
  1412. if (!(hw = kmalloc(sizeof(hfc4s8s_hw), GFP_ATOMIC))) {
  1413. printk(KERN_ERR "No kmem for HFC-4S/8S card\n");
  1414. return (err);
  1415. }
  1416. memset(hw, 0, sizeof(hfc4s8s_hw));
  1417. hw->pdev = pdev;
  1418. err = pci_enable_device(pdev);
  1419. if (err)
  1420. goto out;
  1421. hw->cardnum = card_cnt;
  1422. sprintf(hw->card_name, "hfc4s8s_%d", hw->cardnum);
  1423. printk(KERN_INFO "HFC-4S/8S: found adapter %s (%s) at %s\n",
  1424. driver_data->device_name, hw->card_name, pci_name(pdev));
  1425. spin_lock_init(&hw->lock);
  1426. hw->driver_data = *driver_data;
  1427. hw->irq = pdev->irq;
  1428. hw->iobase = pci_resource_start(pdev, 0);
  1429. #ifdef CONFIG_HISAX_HFC4S8S_PCIMEM
  1430. hw->hw_membase = (u_char *) pci_resource_start(pdev, 1);
  1431. hw->membase = ioremap((ulong) hw->hw_membase, 256);
  1432. #else
  1433. if (!request_region(hw->iobase, 8, hw->card_name)) {
  1434. printk(KERN_INFO
  1435. "HFC-4S/8S: failed to rquest address space at 0x%04x\n",
  1436. hw->iobase);
  1437. goto out;
  1438. }
  1439. #endif
  1440. pci_set_drvdata(pdev, hw);
  1441. err = setup_instance(hw);
  1442. if (!err)
  1443. card_cnt++;
  1444. return (err);
  1445. out:
  1446. kfree(hw);
  1447. return (err);
  1448. }
  1449. /**************************************/
  1450. /* PCI hotplug interface: remove card */
  1451. /**************************************/
  1452. static void __devexit
  1453. hfc4s8s_remove(struct pci_dev *pdev)
  1454. {
  1455. hfc4s8s_hw *hw = pci_get_drvdata(pdev);
  1456. printk(KERN_INFO "HFC-4S/8S: removing card %d\n", hw->cardnum);
  1457. hfc_hardware_enable(hw, 0, 0);
  1458. if (hw->irq)
  1459. free_irq(hw->irq, hw);
  1460. hw->irq = 0;
  1461. release_pci_ports(hw);
  1462. card_cnt--;
  1463. pci_disable_device(pdev);
  1464. kfree(hw);
  1465. return;
  1466. }
  1467. static struct pci_driver hfc4s8s_driver = {
  1468. .name = "hfc4s8s_l1",
  1469. .probe = hfc4s8s_probe,
  1470. .remove = __devexit_p(hfc4s8s_remove),
  1471. .id_table = hfc4s8s_ids,
  1472. };
  1473. /**********************/
  1474. /* driver Module init */
  1475. /**********************/
  1476. static int __init
  1477. hfc4s8s_module_init(void)
  1478. {
  1479. int err;
  1480. printk(KERN_INFO
  1481. "HFC-4S/8S: Layer 1 driver module for HFC-4S/8S isdn chips, %s\n",
  1482. hfc4s8s_rev);
  1483. printk(KERN_INFO
  1484. "HFC-4S/8S: (C) 2003 Cornelius Consult, www.cornelius-consult.de\n");
  1485. card_cnt = 0;
  1486. err = pci_register_driver(&hfc4s8s_driver);
  1487. if (err < 0) {
  1488. goto out;
  1489. }
  1490. printk(KERN_INFO "HFC-4S/8S: found %d cards\n", card_cnt);
  1491. #if !defined(CONFIG_HOTPLUG)
  1492. if (err == 0) {
  1493. err = -ENODEV;
  1494. pci_unregister_driver(&hfc4s8s_driver);
  1495. goto out;
  1496. }
  1497. #endif
  1498. return 0;
  1499. out:
  1500. return (err);
  1501. } /* hfc4s8s_init_hw */
  1502. /*************************************/
  1503. /* driver module exit : */
  1504. /* release the HFC-4s/8s hardware */
  1505. /*************************************/
  1506. static void __exit
  1507. hfc4s8s_module_exit(void)
  1508. {
  1509. pci_unregister_driver(&hfc4s8s_driver);
  1510. printk(KERN_INFO "HFC-4S/8S: module removed\n");
  1511. } /* hfc4s8s_release_hw */
  1512. module_init(hfc4s8s_module_init);
  1513. module_exit(hfc4s8s_module_exit);