hfc4s8s_l1.c 43 KB

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