hfc4s8s_l1.c 39 KB

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