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