hfc4s8s_l1.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586
  1. /*************************************************************************/
  2. /* $Id: hfc4s8s_l1.c,v 1.10 2005/02/09 16:31:09 martinb1 Exp $ */
  3. /* HFC-4S/8S low layer interface for Cologne Chip HFC-4S/8S isdn chips */
  4. /* The low layer (L1) is implemented as a loadable module for usage with */
  5. /* the HiSax isdn driver for passive cards. */
  6. /* */
  7. /* Author: Werner Cornelius */
  8. /* (C) 2003 Cornelius Consult (werner@cornelius-consult.de) */
  9. /* */
  10. /* Driver maintained by Cologne Chip */
  11. /* - Martin Bachem, support@colognechip.com */
  12. /* */
  13. /* This driver only works with chip revisions >= 1, older revision 0 */
  14. /* engineering samples (only first manufacturer sample cards) will not */
  15. /* work and are rejected by the driver. */
  16. /* */
  17. /* This file distributed under the GNU GPL. */
  18. /* */
  19. /* See Version History at the end of this file */
  20. /* */
  21. /*************************************************************************/
  22. #include <linux/module.h>
  23. #include <linux/init.h>
  24. #include <linux/pci.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/delay.h>
  27. #include <linux/slab.h>
  28. #include <linux/timer.h>
  29. #include <linux/skbuff.h>
  30. #include <linux/wait.h>
  31. #include <asm/io.h>
  32. #include "hisax_if.h"
  33. #include "hfc4s8s_l1.h"
  34. static const char hfc4s8s_rev[] = "Revision: 1.10";
  35. /***************************************************************/
  36. /* adjustable transparent mode fifo threshold */
  37. /* The value defines the used fifo threshold with the equation */
  38. /* */
  39. /* notify number of bytes = 2 * 2 ^ TRANS_FIFO_THRES */
  40. /* */
  41. /* The default value is 5 which results in a buffer size of 64 */
  42. /* and an interrupt rate of 8ms. */
  43. /* The maximum value is 7 due to fifo size restrictions. */
  44. /* Values below 3-4 are not recommended due to high interrupt */
  45. /* load of the processor. For non critical applications the */
  46. /* value should be raised to 7 to reduce any interrupt overhead*/
  47. /***************************************************************/
  48. #define TRANS_FIFO_THRES 5
  49. /*************/
  50. /* constants */
  51. /*************/
  52. #define CLOCKMODE_0 0 /* ext. 24.576 MhZ clk freq, int. single clock mode */
  53. #define CLOCKMODE_1 1 /* ext. 49.576 MhZ clk freq, int. single clock mode */
  54. #define CHIP_ID_SHIFT 4
  55. #define HFC_MAX_ST 8
  56. #define MAX_D_FRAME_SIZE 270
  57. #define MAX_B_FRAME_SIZE 1536
  58. #define TRANS_TIMER_MODE (TRANS_FIFO_THRES & 0xf)
  59. #define TRANS_FIFO_BYTES (2 << TRANS_FIFO_THRES)
  60. #define MAX_F_CNT 0x0f
  61. #define CLKDEL_NT 0x6c
  62. #define CLKDEL_TE 0xf
  63. #define CTRL0_NT 4
  64. #define CTRL0_TE 0
  65. #define L1_TIMER_T4 2 /* minimum in jiffies */
  66. #define L1_TIMER_T3 (7 * HZ) /* activation timeout */
  67. #define L1_TIMER_T1 ((120 * HZ) / 1000) /* NT mode deactivation timeout */
  68. /******************/
  69. /* types and vars */
  70. /******************/
  71. static int card_cnt;
  72. /* private driver_data */
  73. typedef struct {
  74. int chip_id;
  75. int clock_mode;
  76. int max_st_ports;
  77. char *device_name;
  78. } hfc4s8s_param;
  79. static struct pci_device_id hfc4s8s_ids[] = {
  80. {.vendor = PCI_VENDOR_ID_CCD,
  81. .device = PCI_DEVICE_ID_4S,
  82. .subvendor = 0x1397,
  83. .subdevice = 0x08b4,
  84. .driver_data =
  85. (unsigned long) &((hfc4s8s_param) {CHIP_ID_4S, CLOCKMODE_0, 4,
  86. "HFC-4S Evaluation Board"}),
  87. },
  88. {.vendor = PCI_VENDOR_ID_CCD,
  89. .device = PCI_DEVICE_ID_8S,
  90. .subvendor = 0x1397,
  91. .subdevice = 0x16b8,
  92. .driver_data =
  93. (unsigned long) &((hfc4s8s_param) {CHIP_ID_8S, CLOCKMODE_0, 8,
  94. "HFC-8S Evaluation Board"}),
  95. },
  96. {.vendor = PCI_VENDOR_ID_CCD,
  97. .device = PCI_DEVICE_ID_4S,
  98. .subvendor = 0x1397,
  99. .subdevice = 0xb520,
  100. .driver_data =
  101. (unsigned long) &((hfc4s8s_param) {CHIP_ID_4S, CLOCKMODE_1, 4,
  102. "IOB4ST"}),
  103. },
  104. {.vendor = PCI_VENDOR_ID_CCD,
  105. .device = PCI_DEVICE_ID_8S,
  106. .subvendor = 0x1397,
  107. .subdevice = 0xb522,
  108. .driver_data =
  109. (unsigned long) &((hfc4s8s_param) {CHIP_ID_8S, CLOCKMODE_1, 8,
  110. "IOB8ST"}),
  111. },
  112. {}
  113. };
  114. MODULE_DEVICE_TABLE(pci, hfc4s8s_ids);
  115. MODULE_AUTHOR("Werner Cornelius, werner@cornelius-consult.de");
  116. MODULE_DESCRIPTION("ISDN layer 1 for Cologne Chip HFC-4S/8S chips");
  117. MODULE_LICENSE("GPL");
  118. /***********/
  119. /* layer 1 */
  120. /***********/
  121. struct hfc4s8s_btype {
  122. spinlock_t lock;
  123. struct hisax_b_if b_if;
  124. struct hfc4s8s_l1 *l1p;
  125. struct sk_buff_head tx_queue;
  126. struct sk_buff *tx_skb;
  127. struct sk_buff *rx_skb;
  128. __u8 *rx_ptr;
  129. int tx_cnt;
  130. int bchan;
  131. int mode;
  132. };
  133. struct _hfc4s8s_hw;
  134. struct hfc4s8s_l1 {
  135. spinlock_t lock;
  136. struct _hfc4s8s_hw *hw; /* pointer to hardware area */
  137. int l1_state; /* actual l1 state */
  138. struct timer_list l1_timer; /* layer 1 timer structure */
  139. int nt_mode; /* set to nt mode */
  140. int st_num; /* own index */
  141. int enabled; /* interface is enabled */
  142. struct sk_buff_head d_tx_queue; /* send queue */
  143. int tx_cnt; /* bytes to send */
  144. struct hisax_d_if d_if; /* D-channel interface */
  145. struct hfc4s8s_btype b_ch[2]; /* B-channel data */
  146. struct hisax_b_if *b_table[2];
  147. };
  148. /**********************/
  149. /* hardware structure */
  150. /**********************/
  151. typedef struct _hfc4s8s_hw {
  152. spinlock_t lock;
  153. int cardnum;
  154. int ifnum;
  155. int iobase;
  156. int nt_mode;
  157. u_char *membase;
  158. u_char *hw_membase;
  159. void *pdev;
  160. int max_fifo;
  161. hfc4s8s_param driver_data;
  162. int irq;
  163. int fifo_sched_cnt;
  164. struct work_struct tqueue;
  165. struct hfc4s8s_l1 l1[HFC_MAX_ST];
  166. char card_name[60];
  167. struct {
  168. u_char r_irq_ctrl;
  169. u_char r_ctrl0;
  170. volatile u_char r_irq_statech; /* active isdn l1 status */
  171. u_char r_irqmsk_statchg; /* enabled isdn status ints */
  172. u_char r_irq_fifo_blx[8]; /* fifo status registers */
  173. u_char fifo_rx_trans_enables[8]; /* mask for enabled transparent rx fifos */
  174. u_char fifo_slow_timer_service[8]; /* mask for fifos needing slower timer service */
  175. volatile u_char r_irq_oview; /* contents of overview register */
  176. volatile u_char timer_irq;
  177. int timer_usg_cnt; /* number of channels using timer */
  178. } mr;
  179. } hfc4s8s_hw;
  180. /* 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. l1p->l1_timer.function = (void *) hfc_l1_timer;
  1240. l1p->l1_timer.data = (long) (l1p);
  1241. init_timer(&l1p->l1_timer);
  1242. l1p->st_num = i;
  1243. skb_queue_head_init(&l1p->d_tx_queue);
  1244. l1p->d_if.ifc.priv = hw->l1 + i;
  1245. l1p->d_if.ifc.l2l1 = (void *) dch_l2l1;
  1246. spin_lock_init(&l1p->b_ch[0].lock);
  1247. l1p->b_ch[0].b_if.ifc.l2l1 = (void *) bch_l2l1;
  1248. l1p->b_ch[0].b_if.ifc.priv = (void *) &l1p->b_ch[0];
  1249. l1p->b_ch[0].l1p = hw->l1 + i;
  1250. l1p->b_ch[0].bchan = 1;
  1251. l1p->b_table[0] = &l1p->b_ch[0].b_if;
  1252. skb_queue_head_init(&l1p->b_ch[0].tx_queue);
  1253. spin_lock_init(&l1p->b_ch[1].lock);
  1254. l1p->b_ch[1].b_if.ifc.l2l1 = (void *) bch_l2l1;
  1255. l1p->b_ch[1].b_if.ifc.priv = (void *) &l1p->b_ch[1];
  1256. l1p->b_ch[1].l1p = hw->l1 + i;
  1257. l1p->b_ch[1].bchan = 2;
  1258. l1p->b_table[1] = &l1p->b_ch[1].b_if;
  1259. skb_queue_head_init(&l1p->b_ch[1].tx_queue);
  1260. }
  1261. enable_pci_ports(hw);
  1262. chipreset(hw);
  1263. i = Read_hfc8(hw, R_CHIP_ID) >> CHIP_ID_SHIFT;
  1264. if (i != hw->driver_data.chip_id) {
  1265. printk(KERN_INFO
  1266. "HFC-4S/8S: invalid chip id 0x%x instead of 0x%x, card ignored\n",
  1267. i, hw->driver_data.chip_id);
  1268. goto out;
  1269. }
  1270. i = Read_hfc8(hw, R_CHIP_RV) & 0xf;
  1271. if (!i) {
  1272. printk(KERN_INFO
  1273. "HFC-4S/8S: chip revision 0 not supported, card ignored\n");
  1274. goto out;
  1275. }
  1276. INIT_WORK(&hw->tqueue, hfc4s8s_bh);
  1277. if (request_irq
  1278. (hw->irq, hfc4s8s_interrupt, IRQF_SHARED, hw->card_name, hw)) {
  1279. printk(KERN_INFO
  1280. "HFC-4S/8S: unable to alloc irq %d, card ignored\n",
  1281. hw->irq);
  1282. goto out;
  1283. }
  1284. printk(KERN_INFO
  1285. "HFC-4S/8S: found PCI card at iobase 0x%x, irq %d\n",
  1286. hw->iobase, hw->irq);
  1287. hfc_hardware_enable(hw, 1, 0);
  1288. return (0);
  1289. out:
  1290. hw->irq = 0;
  1291. release_pci_ports(hw);
  1292. kfree(hw);
  1293. return (err);
  1294. }
  1295. /*****************************************/
  1296. /* PCI hotplug interface: probe new card */
  1297. /*****************************************/
  1298. static int
  1299. hfc4s8s_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  1300. {
  1301. int err = -ENOMEM;
  1302. hfc4s8s_param *driver_data = (hfc4s8s_param *) ent->driver_data;
  1303. hfc4s8s_hw *hw;
  1304. if (!(hw = kzalloc(sizeof(hfc4s8s_hw), GFP_ATOMIC))) {
  1305. printk(KERN_ERR "No kmem for HFC-4S/8S card\n");
  1306. return (err);
  1307. }
  1308. hw->pdev = pdev;
  1309. err = pci_enable_device(pdev);
  1310. if (err)
  1311. goto out;
  1312. hw->cardnum = card_cnt;
  1313. sprintf(hw->card_name, "hfc4s8s_%d", hw->cardnum);
  1314. printk(KERN_INFO "HFC-4S/8S: found adapter %s (%s) at %s\n",
  1315. driver_data->device_name, hw->card_name, pci_name(pdev));
  1316. spin_lock_init(&hw->lock);
  1317. hw->driver_data = *driver_data;
  1318. hw->irq = pdev->irq;
  1319. hw->iobase = pci_resource_start(pdev, 0);
  1320. if (!request_region(hw->iobase, 8, hw->card_name)) {
  1321. printk(KERN_INFO
  1322. "HFC-4S/8S: failed to request address space at 0x%04x\n",
  1323. hw->iobase);
  1324. err = -EBUSY;
  1325. goto out;
  1326. }
  1327. pci_set_drvdata(pdev, hw);
  1328. err = setup_instance(hw);
  1329. if (!err)
  1330. card_cnt++;
  1331. return (err);
  1332. out:
  1333. kfree(hw);
  1334. return (err);
  1335. }
  1336. /**************************************/
  1337. /* PCI hotplug interface: remove card */
  1338. /**************************************/
  1339. static void
  1340. hfc4s8s_remove(struct pci_dev *pdev)
  1341. {
  1342. hfc4s8s_hw *hw = pci_get_drvdata(pdev);
  1343. printk(KERN_INFO "HFC-4S/8S: removing card %d\n", hw->cardnum);
  1344. hfc_hardware_enable(hw, 0, 0);
  1345. if (hw->irq)
  1346. free_irq(hw->irq, hw);
  1347. hw->irq = 0;
  1348. release_pci_ports(hw);
  1349. card_cnt--;
  1350. pci_disable_device(pdev);
  1351. kfree(hw);
  1352. return;
  1353. }
  1354. static struct pci_driver hfc4s8s_driver = {
  1355. .name = "hfc4s8s_l1",
  1356. .probe = hfc4s8s_probe,
  1357. .remove = hfc4s8s_remove,
  1358. .id_table = hfc4s8s_ids,
  1359. };
  1360. /**********************/
  1361. /* driver Module init */
  1362. /**********************/
  1363. static int __init
  1364. hfc4s8s_module_init(void)
  1365. {
  1366. int err;
  1367. printk(KERN_INFO
  1368. "HFC-4S/8S: Layer 1 driver module for HFC-4S/8S isdn chips, %s\n",
  1369. hfc4s8s_rev);
  1370. printk(KERN_INFO
  1371. "HFC-4S/8S: (C) 2003 Cornelius Consult, www.cornelius-consult.de\n");
  1372. card_cnt = 0;
  1373. err = pci_register_driver(&hfc4s8s_driver);
  1374. if (err < 0) {
  1375. goto out;
  1376. }
  1377. printk(KERN_INFO "HFC-4S/8S: found %d cards\n", card_cnt);
  1378. return 0;
  1379. out:
  1380. return (err);
  1381. } /* hfc4s8s_init_hw */
  1382. /*************************************/
  1383. /* driver module exit : */
  1384. /* release the HFC-4s/8s hardware */
  1385. /*************************************/
  1386. static void __exit
  1387. hfc4s8s_module_exit(void)
  1388. {
  1389. pci_unregister_driver(&hfc4s8s_driver);
  1390. printk(KERN_INFO "HFC-4S/8S: module removed\n");
  1391. } /* hfc4s8s_release_hw */
  1392. module_init(hfc4s8s_module_init);
  1393. module_exit(hfc4s8s_module_exit);