xhfc.c 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782
  1. /*
  2. * B400M Quad-BRI module Driver
  3. * Written by Andrew Kohlsmith <akohlsmith@mixdown.ca>
  4. *
  5. * Copyright (C) 2010 Digium, Inc.
  6. * All rights reserved.
  7. *
  8. */
  9. /*
  10. * See http://www.asterisk.org for more information about
  11. * the Asterisk project. Please do not directly contact
  12. * any of the maintainers of this project for assistance;
  13. * the project provides a web site, mailing lists and IRC
  14. * channels for your use.
  15. *
  16. * This program is free software, distributed under the terms of
  17. * the GNU General Public License Version 2 as published by the
  18. * Free Software Foundation. See the LICENSE file included with
  19. * this program for more details.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/pci.h>
  23. #include <linux/ppp_defs.h>
  24. #include <linux/delay.h>
  25. #include <linux/sched.h>
  26. #include <linux/slab.h>
  27. #define FAST_HDLC_NEED_TABLES
  28. #include <dahdi/kernel.h>
  29. #include <dahdi/fasthdlc.h>
  30. #include "wctdm24xxp.h"
  31. #include "xhfc.h"
  32. #define HFC_NR_FIFOS 16
  33. #define HFC_ZMIN 0x00 /* from datasheet */
  34. #define HFC_ZMAX 0x7f
  35. #define HFC_FMIN 0x00
  36. #define HFC_FMAX 0x07
  37. /*
  38. * yuck. Any reg which is not mandated read/write or read-only is write-only.
  39. * Also, there are dozens of registers with the same address. Additionally,
  40. * there are array registers (A_) which have an index register These A_
  41. * registers require an index register to be written to indicate WHICH in the
  42. * array you want.
  43. */
  44. #define R_CIRM 0x00 /* WO */
  45. #define R_CTRL 0x01 /* WO */
  46. #define R_CLK_CFG 0x02 /* WO */
  47. #define A_Z1 0x04 /* RO */
  48. #define A_Z2 0x06 /* RO */
  49. #define R_RAM_ADDR 0x08 /* WO */
  50. #define R_RAM_CTRL 0x09 /* WO */
  51. #define R_FIRST_FIFO 0x0b /* WO */
  52. #define R_FIFO_THRES 0x0c /* WO */
  53. #define A_F1 0x0c /* RO */
  54. #define R_FIFO_MD 0x0d /* WO */
  55. #define A_F2 0x0d /* RO */
  56. #define A_INC_RES_FIFO 0x0e /* WO */
  57. #define A_FIFO_STA 0x0e /* RO */
  58. #define R_FSM_IDX 0x0f /* WO */
  59. #define R_FIFO 0x0f /* WO */
  60. #define R_SLOT 0x10 /* WO */
  61. #define R_IRQ_OVIEW 0x10 /* RO */
  62. #define R_MISC_IRQMSK 0x11 /* WO */
  63. #define R_MISC_IRQ 0x11 /* RO */
  64. #define R_SU_IRQMSK 0x12 /* WO */
  65. #define R_SU_IRQ 0x12 /* RO */
  66. #define R_IRQ_CTRL 0x13 /* WO */
  67. #define R_AF0_OVIEW 0x13 /* RO */
  68. #define R_PCM_MD0 0x14 /* WO */
  69. #define A_USAGE 0x14 /* RO */
  70. #define R_MSS0 0x15 /* WO */
  71. #define R_MSS1 0x15 /* WO */
  72. #define R_PCM_MD1 0x15 /* WO */
  73. #define R_PCM_MD2 0x15 /* WO */
  74. #define R_SH0H 0x15 /* WO */
  75. #define R_SH1H 0x15 /* WO */
  76. #define R_SH0L 0x15 /* WO */
  77. #define R_SH1L 0x15 /* WO */
  78. #define R_SL_SEL0 0x15 /* WO */
  79. #define R_SL_SEL1 0x15 /* WO */
  80. #define R_SL_SEL7 0x15 /* WO */
  81. #define R_RAM_USE 0x15 /* RO */
  82. #define R_SU_SEL 0x16 /* WO */
  83. #define R_CHIP_ID 0x16 /* RO */
  84. #define R_SU_SYNC 0x17 /* WO */
  85. #define R_BERT_STA 0x17 /* RO */
  86. #define R_F0_CNTL 0x18 /* RO */
  87. #define R_F0_CNTH 0x19 /* RO */
  88. #define R_TI_WD 0x1a /* WO */
  89. #define R_BERT_ECL 0x1a /* RO */
  90. #define R_BERT_WD_MD 0x1b /* WO */
  91. #define R_BERT_ECH 0x1b /* RO */
  92. #define R_STATUS 0x1c /* RO */
  93. #define R_SL_MAX 0x1d /* RO */
  94. #define R_PWM_CFG 0x1e /* WO */
  95. #define R_CHIP_RV 0x1f /* RO */
  96. #define R_FIFO_BL0_IRQ 0x20 /* RO */
  97. #define R_FIFO_BL1_IRQ 0x21 /* RO */
  98. #define R_FIFO_BL2_IRQ 0x22 /* RO */
  99. #define R_FIFO_BL3_IRQ 0x23 /* RO */
  100. #define R_FILL_BL0 0x24 /* RO */
  101. #define R_FILL_BL1 0x25 /* RO */
  102. #define R_FILL_BL2 0x26 /* RO */
  103. #define R_FILL_BL3 0x27 /* RO */
  104. #define R_CI_TX 0x28 /* WO */
  105. #define R_CI_RX 0x28 /* RO */
  106. #define R_CGI_CFG0 0x29 /* WO */
  107. #define R_CGI_STA 0x29 /* RO */
  108. #define R_CGI_CFG1 0x2a /* WO */
  109. #define R_MON_RX 0x2a /* RO */
  110. #define R_MON_TX 0x2b /* WO */
  111. #define A_SU_WR_STA 0x30 /* WO */
  112. #define A_SU_RD_STA 0x30 /* RO */
  113. #define A_SU_CTRL0 0x31 /* WO */
  114. #define A_SU_DLYL 0x31 /* RO */
  115. #define A_SU_CTRL1 0x32 /* WO */
  116. #define A_SU_DLYH 0x32 /* RO */
  117. #define A_SU_CTRL2 0x33 /* WO */
  118. #define A_MS_TX 0x34 /* WO */
  119. #define A_MS_RX 0x34 /* RO */
  120. #define A_ST_CTRL3 0x35 /* WO */
  121. #define A_UP_CTRL3 0x35 /* WO */
  122. #define A_SU_STA 0x35 /* RO */
  123. #define A_MS_DF 0x36 /* WO */
  124. #define A_SU_CLK_DLY 0x37 /* WO */
  125. #define R_PWM0 0x38 /* WO */
  126. #define R_PWM1 0x39 /* WO */
  127. #define A_B1_TX 0x3c /* WO */
  128. #define A_B1_RX 0x3c /* RO */
  129. #define A_B2_TX 0x3d /* WO */
  130. #define A_B2_RX 0x3d /* RO */
  131. #define A_D_TX 0x3e /* WO */
  132. #define A_D_RX 0x3e /* RO */
  133. #define A_BAC_S_TX 0x3f /* WO */
  134. #define A_E_RX 0x3f /* RO */
  135. #define R_GPIO_OUT1 0x40 /* WO */
  136. #define R_GPIO_IN1 0x40 /* RO */
  137. #define R_GPIO_OUT3 0x41 /* WO */
  138. #define R_GPIO_IN3 0x41 /* RO */
  139. #define R_GPIO_EN1 0x42 /* WO */
  140. #define R_GPIO_EN3 0x43 /* WO */
  141. #define R_GPIO_SEL_BL 0x44 /* WO */
  142. #define R_GPIO_OUT2 0x45 /* WO */
  143. #define R_GPIO_IN2 0x45 /* RO */
  144. #define R_PWM_MD 0x46 /* WO */
  145. #define R_GPIO_EN2 0x47 /* WO */
  146. #define R_GPIO_OUT0 0x48 /* WO */
  147. #define R_GPIO_IN0 0x48 /* RO */
  148. #define R_GPIO_EN0 0x4a /* WO */
  149. #define R_GPIO_SEL 0x4c /* WO */
  150. #define R_PLL_CTRL 0x50 /* WO */
  151. #define R_PLL_STA 0x50 /* RO */
  152. #define R_PLL_P 0x51 /* RW */
  153. #define R_PLL_N 0x52 /* RW */
  154. #define R_PLL_S 0x53 /* RW */
  155. #define A_FIFO_DATA 0x80 /* RW */
  156. #define A_FIFO_DATA_NOINC 0x84 /* RW */
  157. #define R_INT_DATA 0x88 /* RO */
  158. #define R_RAM_DATA 0xc0 /* RW */
  159. #define A_SL_CFG 0xd0 /* RW */
  160. #define A_CH_MSK 0xf4 /* RW */
  161. #define A_CON_HDLC 0xfa /* RW */
  162. #define A_SUBCH_CFG 0xfb /* RW */
  163. #define A_CHANNEL 0xfc /* RW */
  164. #define A_FIFO_SEQ 0xfd /* RW */
  165. #define A_FIFO_CTRL 0xff /* RW */
  166. /* R_CIRM bits */
  167. #define V_CLK_OFF (1 << 0) /* 1=internal clocks disabled */
  168. #define V_WAIT_PROC (1 << 1) /* 1=additional /WAIT after write access */
  169. #define V_WAIT_REG (1 << 2) /* 1=additional /WAIT for internal BUSY phase */
  170. #define V_SRES (1 << 3) /* soft reset (group 0) */
  171. #define V_HFC_RES (1 << 4) /* HFC reset (group 1) */
  172. #define V_PCM_RES (1 << 5) /* PCM reset (group 2) */
  173. #define V_SU_RES (1 << 6) /* S/T reset (group 3) */
  174. #define XHFC_FULL_RESET (V_SRES | V_HFC_RES | V_PCM_RES | V_SU_RES)
  175. /* R_STATUS bits */
  176. #define V_BUSY (1 << 0) /* 1=HFC busy, limited register access */
  177. #define V_PROC (1 << 1) /* 1=HFC in processing phase */
  178. #define V_LOST_STA (1 << 3) /* 1=frames have been lost */
  179. #define V_PCM_INIT (1 << 4) /* 1=PCM init in progress */
  180. #define V_WAK_STA (1 << 5) /* state of WAKEUP pin wien V_WAK_EN=1 */
  181. #define V_MISC_IRQSTA (1 << 6) /* 1=misc interrupt has occurred */
  182. #define V_FR_IRQSTA (1 << 7) /* 1=fifo interrupt has occured */
  183. #define XHFC_INTS (V_MISC_IRQSTA | V_FR_IRQSTA)
  184. /* R_FIFO_BLx_IRQ bits */
  185. #define V_FIFOx_TX_IRQ (1 << 0) /* FIFO TX interrupt occurred */
  186. #define V_FIFOx_RX_IRQ (1 << 1) /* FIFO RX interrupt occurred */
  187. #define FIFOx_TXRX_IRQ (V_FIFOx_TX_IRQ | V_FIFOx_RX_IRQ)
  188. /* R_FILL_BLx bits */
  189. #define V_FILL_FIFOx_TX (1 << 0) /* TX FIFO reached V_THRES_TX level */
  190. #define V_FILL_FIFOx_RX (1 << 1) /* RX FIFO reached V_THRES_RX level */
  191. #define FILL_FIFOx_TXRX (V_FILL_FIFOx_TX | V_FILL_FIFOx_RX)
  192. /* R_MISC_IRQ / R_MISC_IRQMSK bits */
  193. #define V_SLP_IRQ (1 << 0) /* frame sync pulse flips */
  194. #define V_TI_IRQ (1 << 1) /* timer elapsed */
  195. #define V_PROC_IRQ (1 << 2) /* processing/non-processing transition */
  196. #define V_CI_IRQ (1 << 4) /* indication bits changed */
  197. #define V_WAK_IRQ (1 << 5) /* WAKEUP pin */
  198. #define V_MON_TX_IRQ (1 << 6) /* monitor byte can be written */
  199. #define V_MON_RX_IRQ (1 << 7) /* monitor byte received */
  200. /* R_SU_IRQ/R_SU_IRQMSK bits */
  201. #define V_SU0_IRQ (1 << 0) /* interrupt/mask port 1 */
  202. #define V_SU1_IRQ (1 << 1) /* interrupt/mask port 2 */
  203. #define V_SU2_IRQ (1 << 2) /* interrupt/mask port 3 */
  204. #define V_SU3_IRQ (1 << 3) /* interrupt/mask port 4 */
  205. /* R_IRQ_CTRL bits */
  206. #define V_FIFO_IRQ_EN (1 << 0) /* enable any unmasked FIFO IRQs */
  207. #define V_GLOB_IRQ_EN (1 << 3) /* enable any unmasked IRQs */
  208. #define V_IRQ_POL (1 << 4) /* 1=IRQ active high */
  209. /* R_BERT_WD_MD bits */
  210. #define V_BERT_ERR (1 << 3) /* 1=generate an error bit in BERT stream */
  211. #define V_AUTO_WD_RES (1 << 5) /* 1=automatically kick the watchdog */
  212. #define V_WD_RES (1 << 7) /* 1=kick the watchdog (bit auto clears) */
  213. /* R_TI_WD bits */
  214. #define V_EV_TS_SHIFT (0)
  215. #define V_EV_TS_MASK (0x0f)
  216. #define V_WD_TS_SHIFT (4)
  217. #define V_WD_TS_MASK (0xf0)
  218. /* A_FIFO_CTRL bits */
  219. #define V_FIFO_IRQMSK (1 << 0) /* 1=FIFO can generate interrupts */
  220. #define V_BERT_EN (1 << 1) /* 1=BERT data replaces FIFO data */
  221. #define V_MIX_IRQ (1 << 2) /* IRQ when 0=end of frame only, 1=also when Z1==Z2 */
  222. #define V_FR_ABO (1 << 3) /* 1=generate frame abort/frame abort detected */
  223. #define V_NO_CRC (1 << 4) /* 1=do not send CRC at end of frame */
  224. #define V_NO_REP (1 << 5) /* 1=frame deleted after d-chan contention */
  225. /* R_CLK_CFG bits */
  226. #define V_CLK_PLL (1 << 0) /* Sysclk select 0=OSC_IN, 1=PLL output */
  227. #define V_CLKO_HI (1 << 1) /* CLKOUT selection 0=PLL/8, 1=PLL */
  228. #define V_CLKO_PLL (1 << 2) /* CLKOUT source 0=divider or PLL input, 1=PLL output */
  229. #define V_PCM_CLK (1 << 5) /* 1=PCM clk = OSC, 0 = PCM clk is 2x OSC */
  230. #define V_CLKO_OFF (1 << 6) /* CLKOUT enable 0=enabled */
  231. #define V_CLK_F1 (1 << 7) /* PLL input pin 0=OSC_IN, 1=F1_1 */
  232. /* R_PCM_MD0 bits */
  233. #define V_PCM_MD (1 << 0) /* 1=PCM master */
  234. #define V_C4_POL (1 << 1) /* 1=F0IO sampled on rising edge of C4IO */
  235. #define V_F0_NEG (1 << 2) /* 1=negative polarity of F0IO */
  236. #define V_F0_LEN (1 << 3) /* 1=F0IO active for 2 C4IO clocks */
  237. #define V_PCM_IDX_SEL0 (0x0 << 4) /* reg15 = R_SL_SEL0 */
  238. #define V_PCM_IDX_SEL1 (0x1 << 4) /* reg15 = R_SL_SEL1 */
  239. #define V_PCM_IDX_SEL7 (0x7 << 4) /* reg15 = R_SL_SEL7 */
  240. #define V_PCM_IDX_MSS0 (0x8 << 4) /* reg15 = R_MSS0 */
  241. #define V_PCM_IDX_MD1 (0x9 << 4) /* reg15 = R_PCM_MD1 */
  242. #define V_PCM_IDX_MD2 (0xa << 4) /* reg15 = R_PCM_MD2 */
  243. #define V_PCM_IDX_MSS1 (0xb << 4) /* reg15 = R_MSS1 */
  244. #define V_PCM_IDX_SH0L (0xc << 4) /* reg15 = R_SH0L */
  245. #define V_PCM_IDX_SH0H (0xd << 4) /* reg15 = R_SH0H */
  246. #define V_PCM_IDX_SH1L (0xe << 4) /* reg15 = R_SH1L */
  247. #define V_PCM_IDX_SH1H (0xf << 4) /* reg15 = R_SH1H */
  248. #define V_PCM_IDX_MASK (0xf0)
  249. /* R_PCM_MD1 bits */
  250. #define V_PLL_ADJ_00 (0x0 << 2) /* adj 4 times by 0.5 system clk cycles */
  251. #define V_PLL_ADJ_01 (0x1 << 2) /* adj 3 times by 0.5 system clk cycles */
  252. #define V_PLL_ADJ_10 (0x2 << 2) /* adj 2 times by 0.5 system clk cycles */
  253. #define V_PLL_ADJ_11 (0x3 << 2) /* adj 1 time by 0.5 system clk cycles */
  254. #define V_PCM_DR_2048 (0x0 << 4) /* 2.048Mbps, 32 timeslots */
  255. #define V_PCM_DR_4096 (0x1 << 4) /* 4.096Mbps, 64 timeslots */
  256. #define V_PCM_DR_8192 (0x2 << 4) /* 8.192Mbps, 128 timeslots */
  257. #define V_PCM_DR_075 (0x3 << 4) /* 0.75Mbps, 12 timeslots */
  258. #define V_PCM_LOOP (1 << 6) /* 1=internal loopback */
  259. #define V_PCM_SMPL (1 << 7) /* 0=sample at middle of bit cell, 1=sample at 3/4 point */
  260. #define V_PLL_ADJ_MASK (0x3 << 2)
  261. #define V_PCM_DR_MASK (0x3 << 4)
  262. /* R_PCM_MD2 bits */
  263. #define V_SYNC_OUT1 (1 << 1) /* SYNC_O source 0=SYNC_I or FSX_RX, 1=512kHz from PLL or multiframe */
  264. #define V_SYNC_SRC (1 << 2) /* 0=line interface, 1=SYNC_I */
  265. #define V_SYNC_OUT2 (1 << 3) /* SYNC_O source 0=rx sync or FSC_RX 1=SYNC_I or received superframe */
  266. #define V_C2O_EN (1 << 4) /* C2IO output enable (when V_C2I_EN=0) */
  267. #define V_C2I_EN (1 << 5) /* PCM controller clock source 0=C4IO, 1=C2IO */
  268. #define V_PLL_ICR (1 << 6) /* 0=reduce PCM frame time, 1=increase */
  269. #define V_PLL_MAN (1 << 7) /* 0=auto, 1=manual */
  270. /* A_SL_CFG bits */
  271. #define V_CH_SDIR (1 << 0) /* 1=HFC channel receives data from PCM TS */
  272. #define V_ROUT_TX_DIS (0x0 << 6) /* disabled, output disabled */
  273. #define V_ROUT_TX_LOOP (0x1 << 6) /* internally looped, output disabled */
  274. #define V_ROUT_TX_STIO1 (0x2 << 6) /* output data to STIO1 */
  275. #define V_ROUT_TX_STIO2 (0x3 << 6) /* output data to STIO2 */
  276. #define V_ROUT_RX_DIS (0x0 << 6) /* disabled, input data ignored */
  277. #define V_ROUT_RX_LOOP (0x1 << 6) /* internally looped, input data ignored */
  278. #define V_ROUT_RX_STIO2 (0x2 << 6) /* channel data comes from STIO1 */
  279. #define V_ROUT_RX_STIO1 (0x3 << 6) /* channel data comes from STIO2 */
  280. #define V_CH_SNUM_SHIFT (1)
  281. #define V_CH_SNUM_MASK (31 << 1)
  282. /* A_CON_HDLC bits */
  283. #define V_IFF (1 << 0) /* Inter-Frame Fill: 0=0x7e, 1=0xff */
  284. #define V_HDLC_TRP (1 << 1) /* 0=HDLC mode, 1=transparent */
  285. #define V_TRP_DISABLED (0x0 << 2) /* FIFO disabled, no interrupt */
  286. #define V_TRP_IRQ_64 (0x1 << 2) /* FIFO enabled, int @ 8 bytes */
  287. #define V_TRP_IRQ_128 (0x2 << 2) /* FIFO enabled, int @ 16 bytes */
  288. #define V_TRP_IRQ_256 (0x3 << 2) /* FIFO enabled, int @ 32 bytes */
  289. #define V_TRP_IRQ_512 (0x4 << 2) /* FIFO enabled, int @ 64 bytes */
  290. #define V_TRP_IRQ_1024 (0x5 << 2) /* FIFO enabled, int @ 128 bytes */
  291. #define V_TRP_NO_IRQ (0x7 << 2) /* FIFO enabled, no interrupt */
  292. #define V_HDLC_IRQ (0x3 << 2) /* HDLC: FIFO enabled, interrupt at end of frame or when FIFO > 16 byte boundary (Mixed IRQ) */
  293. #define V_DATA_FLOW_000 (0x0 << 5) /* see A_CON_HDLC reg description in datasheet */
  294. #define V_DATA_FLOW_001 (0x1 << 5) /* see A_CON_HDLC reg description in datasheet */
  295. #define V_DATA_FLOW_010 (0x2 << 5) /* see A_CON_HDLC reg description in datasheet */
  296. #define V_DATA_FLOW_011 (0x3 << 5) /* see A_CON_HDLC reg description in datasheet */
  297. #define V_DATA_FLOW_100 (0x4 << 5) /* see A_CON_HDLC reg description in datasheet */
  298. #define V_DATA_FLOW_101 (0x5 << 5) /* see A_CON_HDLC reg description in datasheet */
  299. #define V_DATA_FLOW_110 (0x6 << 5) /* see A_CON_HDLC reg description in datasheet */
  300. #define V_DATA_FLOW_111 (0x7 << 5) /* see A_CON_HDLC reg description in datasheet */
  301. /* R_FIFO bits */
  302. #define V_FIFO_DIR (1 << 0) /* 1=RX FIFO data */
  303. #define V_REV (1 << 7) /* 1=MSB first */
  304. #define V_FIFO_NUM_SHIFT (1)
  305. #define V_FIFO_NUM_MASK (0x3e)
  306. /* A_CHANNEL bits */
  307. #define V_CH_FDIR (1 << 0) /* 1=HFC chan for RX data */
  308. #define V_CH_FNUM_SHIFT (1)
  309. #define V_CH_FNUM_MASK (0x3e)
  310. /* R_SLOT bits */
  311. #define V_SL_DIR (1 << 0) /* 1=timeslot will RX PCM data from bus */
  312. #define V_SL_NUM_SHIFT (1)
  313. #define V_SL_NUM_MASK (0xfe)
  314. /* A_INC_RES_FIFO bits */
  315. #define V_INC_F (1 << 0) /* 1=increment FIFO F-counter (bit auto-clears) */
  316. #define V_RES_FIFO (1 << 1) /* 1=reset FIFO (bit auto-clears) */
  317. #define V_RES_LOST (1 << 2) /* 1=reset LOST error (bit auto-clears) */
  318. #define V_RES_FIFO_ERR (1 << 3) /* 1=reset FIFO error (bit auto-clears), check V_ABO_DONE before setting */
  319. /* R_FIFO_MD bits */
  320. #define V_FIFO_MD_00 (0x0 << 0) /* 16 FIFOs, 64 bytes TX/RX, 128 TX or RX if V_UNIDIR_RX */
  321. #define V_FIFO_MD_01 (0x1 << 0) /* 8 FIFOs, 128 bytes TX/RX, 256 TX or RX if V_UNIDIR_RX */
  322. #define V_FIFO_MD_10 (0x2 << 0) /* 4 FIFOs, 256 bytes TX/RX, invalid mode with V_UNIDIR_RX */
  323. #define V_DF_MD_SM (0x0 << 2) /* simple data flow mode */
  324. #define V_DF_MD_CSM (0x1 << 2) /* channel select mode */
  325. #define V_DF_MD_FSM (0x3 << 2) /* FIFO sequence mode */
  326. #define V_UNIDIR_MD (1 << 4) /* 1=unidirectional FIFO mode */
  327. #define V_UNIDIR_RX (1 << 5) /* 1=unidirection FIFO is RX */
  328. /* A_SUBCH_CFG bits */
  329. #define V_BIT_CNT_8BIT (0) /* process 8 bits */
  330. #define V_BIT_CNT_1BIT (1) /* process 1 bit */
  331. #define V_BIT_CNT_2BIT (2) /* process 2 bits */
  332. #define V_BIT_CNT_3BIT (3) /* process 3 bits */
  333. #define V_BIT_CNT_4BIT (4) /* process 4 bits */
  334. #define V_BIT_CNT_5BIT (5) /* process 5 bits */
  335. #define V_BIT_CNT_6BIT (6) /* process 6 bits */
  336. #define V_BIT_CNT_7BIT (7) /* process 7 bits */
  337. #define V_LOOP_FIFO (1 << 6) /* loop FIFO data */
  338. #define V_INV_DATA (1 << 7) /* invert FIFO data */
  339. #define V_START_BIT_SHIFT (3)
  340. #define V_START_BIT_MASK (0x38)
  341. /* R_SU_SYNC bits */
  342. #define V_SYNC_SEL_PORT0 (0x0 << 0) /* sync to TE port 0 */
  343. #define V_SYNC_SEL_PORT1 (0x1 << 0) /* sync to TE port 1 */
  344. #define V_SYNC_SEL_PORT2 (0x2 << 0) /* sync to TE port 2 */
  345. #define V_SYNC_SEL_PORT3 (0x3 << 0) /* sync to TE port 3 */
  346. #define V_SYNC_SEL_SYNCI (0x4 << 0) /* sync to SYNC_I */
  347. #define V_MAN_SYNC (1 << 3) /* 1=manual sync mode */
  348. #define V_AUTO_SYNCI (1 << 4) /* 1=SYNC_I used if FSC_RX not found */
  349. #define V_D_MERGE_TX (1 << 5) /* 1=all 4 dchan taken from one byte in TX */
  350. #define V_E_MERGE_RX (1 << 6) /* 1=all 4 echan combined in RX direction */
  351. #define V_D_MERGE_RX (1 << 7) /* 1=all 4 dchan combined in RX direction */
  352. #define V_SYNC_SEL_MASK (0x03)
  353. /* A_SU_WR_STA bits */
  354. #define V_SU_SET_STA_MASK (0x0f)
  355. #define V_SU_LD_STA (1 << 4) /* 1=force SU_SET_STA mode, must be manually cleared 6us later */
  356. #define V_SU_ACT_NOP (0x0 << 5) /* NOP */
  357. #define V_SU_ACT_DEACTIVATE (0x2 << 5) /* start deactivation. auto-clears */
  358. #define V_SU_ACT_ACTIVATE (0x3 << 5) /* start activation. auto-clears. */
  359. #define V_SET_G2_G3 (1 << 7) /* 1=auto G2->G3 in NT mode. auto-clears after transition. */
  360. /* A_SU_RD_STA */
  361. #define V_SU_STA_MASK (0x0f)
  362. #define V_SU_FR_SYNC (1 << 4) /* 1=synchronized */
  363. #define V_SU_T2_EXP (1 << 5) /* 1=T2 expired (NT only) */
  364. #define V_SU_INFO0 (1 << 6) /* 1=INFO0 */
  365. #define V_G2_G3 (1 << 7) /* 1=allows G2->G3 (NT only, auto-clears) */
  366. /* A_SU_CLK_DLY bits */
  367. #define V_SU_DLY_MASK (0x0f)
  368. #define V_SU_SMPL_MASK (0xf0)
  369. #define V_SU_SMPL_SHIFT (4)
  370. /* A_SU_CTRL0 bits */
  371. #define V_B1_TX_EN (1 << 0) /* 1=B1-channel transmit */
  372. #define V_B2_TX_EN (1 << 1) /* 1=B2-channel transmit */
  373. #define V_SU_MD (1 << 2) /* 0=TE, 1=NT */
  374. #define V_ST_D_LPRIO (1 << 3) /* D-Chan priority 0=high, 1=low */
  375. #define V_ST_SQ_EN (1 << 4) /* S/Q bits transmit (1=enabled) */
  376. #define V_SU_TST_SIG (1 << 5) /* 1=transmit test signal */
  377. #define V_ST_PU_CTRL (1 << 6) /* 1=enable end of pulse control */
  378. #define V_SU_STOP (1 << 7) /* 1=power down */
  379. /* A_SU_CTRL1 bits */
  380. #define V_G2_G3_EN (1 << 0) /* 1=G2->G3 allowed without V_SET_G2_G3 */
  381. #define V_D_RES (1 << 2) /* 1=D-chan reset */
  382. #define V_ST_E_IGNO (1 << 3) /* TE:1=ignore Echan, NT:should always be 1. */
  383. #define V_ST_E_LO (1 << 4) /* NT only: 1=force Echan low */
  384. #define V_BAC_D (1 << 6) /* 1=BAC bit controls Dchan TX */
  385. #define V_B12_SWAP (1 << 7) /* 1=swap B1/B2 */
  386. /* A_SU_CTRL2 bits */
  387. #define V_B1_RX_EN (1 << 0) /* 1=enable B1 RX */
  388. #define V_B2_RX_EN (1 << 1) /* 1=enable B2 RX */
  389. #define V_MS_SSYNC2 (1 << 2) /* 0 normally, see datasheet */
  390. #define V_BAC_S_SEL (1 << 3) /* see datasheet */
  391. #define V_SU_SYNC_NT (1 << 4) /* 0=sync pulses generated only in TE, 1=in TE and NT */
  392. #define V_SU_2KHZ (1 << 5) /* 0=96kHz test tone, 1=2kHz */
  393. #define V_SU_TRI (1 << 6) /* 1=tristate output buffer */
  394. #define V_SU_EXCHG (1 << 7) /* 1=invert output drivers */
  395. /* R_IRQ_OVIEW bits */
  396. #define V_FIFO_BL0_IRQ (1 << 0) /* FIFO 0-3 IRQ */
  397. #define V_FIFO_BL1_IRQ (1 << 1) /* FIFO 4-7 IRQ */
  398. #define V_FIFO_BL2_IRQ (1 << 2) /* FIFO 8-11 IRQ */
  399. #define V_FIFO_BL3_IRQ (1 << 3) /* FIFO 12-15 IRQ */
  400. #define V_MISC_IRQ (1 << 4) /* R_MISC_IRQ changed */
  401. #define V_STUP_IRQ (1 << 5) /* R_SU_IRQ changed */
  402. #define V_FIFO_BLx_IRQ (V_FIFO_BL0_IRQ | V_FIFO_BL1_IRQ | V_FIFO_BL2_IRQ | V_FIFO_BL3_IRQ)
  403. /* R_FIRST_FIFO bits */
  404. #define V_FIRST_FIFO_NUM_SHIFT (1)
  405. /* A_FIFO_SEQ bits */
  406. #define V_NEXT_FIFO_NUM_SHIFT (1)
  407. #define V_SEQ_END (1 << 6)
  408. #if (DAHDI_CHUNKSIZE != 8)
  409. #error Sorry, the b400m does not support chunksize != 8
  410. #endif
  411. /* general debug messages */
  412. #define DEBUG_GENERAL (1 << 0)
  413. /* emit DTMF detector messages */
  414. #define DEBUG_DTMF (1 << 1)
  415. /* emit register read/write, but only if the kernel's DEBUG is defined */
  416. #define DEBUG_REGS (1 << 2)
  417. /* emit file operation messages */
  418. #define DEBUG_FOPS (1 << 3)
  419. #define DEBUG_ECHOCAN (1 << 4)
  420. /* S/T state machine */
  421. #define DEBUG_ST_STATE (1 << 5)
  422. /* HDLC controller */
  423. #define DEBUG_HDLC (1 << 6)
  424. /* alarm changes */
  425. #define DEBUG_ALARM (1 << 7)
  426. /* Timing related changes */
  427. #define DEBUG_TIMING (1 << 8)
  428. #define DBG (bri_debug & DEBUG_GENERAL)
  429. #define DBG_DTMF (bri_debug & DEBUG_DTMF)
  430. #define DBG_REGS (bri_debug & DEBUG_REGS)
  431. #define DBG_FOPS (bri_debug & DEBUG_FOPS)
  432. #define DBG_EC (bri_debug & DEBUG_ECHOCAN)
  433. #define DBG_ST (bri_debug & DEBUG_ST_STATE)
  434. #define DBG_HDLC (bri_debug & DEBUG_HDLC)
  435. #define DBG_ALARM (bri_debug & DEBUG_ALARM)
  436. #define DBG_TIMING (bri_debug & DEBUG_TIMING)
  437. #define DBG_SPANFILTER ((1 << bspan->port) & bri_spanfilter)
  438. /* #define HARDHDLC_RX */
  439. /* Any static variables not initialized by default should be set
  440. * to 0 automatically */
  441. int bri_debug;
  442. int bri_spanfilter = 9;
  443. int bri_teignorered = 1;
  444. int bri_alarmdebounce;
  445. int bri_persistentlayer1;
  446. int timingcable;
  447. static int synccard = -1;
  448. static int syncspan = -1;
  449. static const int TIMER_3_MS = 30000;
  450. #define b4_info(b4, format, arg...) \
  451. dev_info(&(b4)->wc->vb.pdev->dev , format , ## arg)
  452. /* if defined, swaps ports 2 and 3 on the B400M module */
  453. #define SWAP_PORTS
  454. #define XHFC_T1 0
  455. #define XHFC_T2 1
  456. #define XHFC_T3 2
  457. /* T4 - Special timer, used for debug purposes for monitoring of L1 state during activation attempt. */
  458. #define XHFC_T4 3
  459. #define B400M_CHANNELS_PER_SPAN 3 /* 2 B-channels and 1 D-Channel for each BRI span */
  460. #define B400M_HDLC_BUF_LEN 128 /* arbitrary, just the max # of byts we will send to DAHDI per call */
  461. #define get_F(f1, f2, flen) { \
  462. f1 = hfc_readcounter8(b4, A_F1); \
  463. f2 = hfc_readcounter8(b4, A_F2); \
  464. flen = f1 - f2; \
  465. \
  466. if (flen < 0) \
  467. flen += (HFC_FMAX - HFC_FMIN) + 1; \
  468. }
  469. #define get_Z(z1, z2, zlen) { \
  470. z1 = hfc_readcounter8(b4, A_Z1); \
  471. z2 = hfc_readcounter8(b4, A_Z2); \
  472. zlen = z1 - z2; \
  473. \
  474. if (zlen < 0) \
  475. zlen += (HFC_ZMAX - HFC_ZMIN) + 1; \
  476. }
  477. struct b400m_span {
  478. struct b400m *parent;
  479. unsigned int port; /* which S/T port this span belongs to */
  480. int oldstate; /* old state machine state */
  481. int newalarm; /* alarm to send to DAHDI once alarm timer expires */
  482. unsigned long alarmtimer;
  483. unsigned int te_mode:1; /* 1=TE, 0=NT */
  484. unsigned int term_on:1; /* 1= 390 ohm termination enable, 0 = disabled */
  485. unsigned long hfc_timers[B400M_CHANNELS_PER_SPAN+1]; /* T1, T2, T3 */
  486. int hfc_timer_on[B400M_CHANNELS_PER_SPAN+1]; /* 1=timer active */
  487. int fifos[B400M_CHANNELS_PER_SPAN]; /* B1, B2, D <--> host fifo numbers */
  488. /* HDLC controller fields */
  489. struct wctdm_span *wspan; /* pointer to the actual dahdi_span */
  490. struct dahdi_chan *sigchan; /* pointer to the signalling channel for this span */
  491. int sigactive; /* nonzero means we're in the middle of sending an HDLC frame */
  492. atomic_t hdlc_pending; /* hdlc_hard_xmit() increments, hdlc_tx_frame() decrements */
  493. unsigned int frames_out;
  494. unsigned int frames_in;
  495. struct fasthdlc_state rxhdlc;
  496. int infcs;
  497. int f_sz;
  498. };
  499. /* This structure exists one per module */
  500. struct b400m {
  501. char name[10];
  502. int position; /* module position in carrier board */
  503. int b400m_no; /* 0-based B400M number in system */
  504. struct wctdm *wc; /* parent structure */
  505. spinlock_t reglock; /* lock for all register accesses */
  506. unsigned long ticks;
  507. unsigned long fifo_en_rxint; /* each bit is the RX int enable for that FIFO */
  508. unsigned long fifo_en_txint; /* each bit is the TX int enable for that FIFO */
  509. unsigned char fifo_irqstatus; /* top-half ORs in new interrupts, bottom-half ANDs them out */
  510. int setsyncspan; /* Span reported from HFC for sync on this card */
  511. int reportedsyncspan; /* Span reported from HFC for sync on this card */
  512. unsigned int running:1; /* interrupts are enabled */
  513. unsigned int shutdown:1; /* 1=bottom half doesn't process anything, just returns */
  514. unsigned int inited:1; /* FIXME: temporary */
  515. unsigned int misc_irq_mask:1; /* 1= interrupt is valid */
  516. struct b400m_span spans[4]; /* Individual spans */
  517. struct workqueue_struct *xhfc_ws;
  518. struct work_struct xhfc_wq;
  519. unsigned char irq_oview; /* copy of r_irq_oview */
  520. unsigned char fifo_fill; /* copy of R_FILL_BL0 */
  521. struct semaphore regsem; /* lock for low-level register accesses */
  522. struct semaphore fifosem; /* lock for fifo accesses */
  523. unsigned char lastreg; /* last XHFC register accessed (used to speed up multiple address "hits" */
  524. };
  525. static void hfc_start_st(struct b400m_span *s);
  526. static void hfc_stop_st(struct b400m_span *s);
  527. void b400m_set_dahdi_span(struct b400m *b4, int spanno,
  528. struct wctdm_span *wspan)
  529. {
  530. b4->spans[spanno].wspan = wspan;
  531. wspan->bspan = &b4->spans[spanno];
  532. }
  533. static inline void flush_hw(void)
  534. {
  535. }
  536. static int xhfc_getreg(struct wctdm *wc, struct wctdm_module *const mod,
  537. int addr, u8 *lastreg)
  538. {
  539. int x;
  540. if (*lastreg != (unsigned char)addr) {
  541. wctdm_setreg(wc, mod, 0x60, addr);
  542. *lastreg = (unsigned char)addr;
  543. }
  544. x = wctdm_getreg(wc, mod, 0x80);
  545. return x;
  546. }
  547. static int xhfc_setreg(struct wctdm *wc, struct wctdm_module *const mod,
  548. int addr, int val, u8 *lastreg)
  549. {
  550. if (*lastreg != (unsigned char)addr) {
  551. wctdm_setreg(wc, mod, 0x60, addr);
  552. *lastreg = (unsigned char)addr;
  553. }
  554. return wctdm_setreg(wc, mod, 0x00, val);
  555. }
  556. static inline struct wctdm_module *get_mod(struct b400m *b4)
  557. {
  558. return &b4->wc->mods[b4->position];
  559. }
  560. static int b400m_getreg(struct b400m *b4, int addr)
  561. {
  562. int x;
  563. if (down_trylock(&b4->regsem)) {
  564. if (down_interruptible(&b4->regsem)) {
  565. b4_info(b4, "b400m_getreg(0x%02x) interrupted\n",
  566. addr);
  567. return -1;
  568. }
  569. }
  570. x = xhfc_getreg(b4->wc, get_mod(b4), addr, &b4->lastreg);
  571. up(&b4->regsem);
  572. return x;
  573. }
  574. static int b400m_setreg(struct b400m *b4, const int addr, const int val)
  575. {
  576. int x;
  577. if (down_trylock(&b4->regsem)) {
  578. if (down_interruptible(&b4->regsem)) {
  579. b4_info(b4, "b400m_setreg(0x%02x -> 0x%02x) "
  580. "interrupted\n", val, addr);
  581. return -1;
  582. }
  583. }
  584. x = xhfc_setreg(b4->wc, get_mod(b4), addr, val, &b4->lastreg);
  585. up(&b4->regsem);
  586. return x;
  587. }
  588. /*
  589. * A lot of the registers in the XHFC are indexed.
  590. * this function sets the index, and then writes to the indexed register.
  591. */
  592. static void b400m_setreg_ra(struct b400m *b4, u8 r, u8 rd, u8 a, u8 ad)
  593. {
  594. if (down_trylock(&b4->regsem)) {
  595. if (down_interruptible(&b4->regsem)) {
  596. b4_info(b4, "b400m_setreg_ra(0x%02x -> 0x%02x) "
  597. "interrupted\n", a, ad);
  598. return;
  599. }
  600. }
  601. xhfc_setreg(b4->wc, get_mod(b4), r, rd, &b4->lastreg);
  602. xhfc_setreg(b4->wc, get_mod(b4), a, ad, &b4->lastreg);
  603. up(&b4->regsem);
  604. }
  605. static u8 b400m_getreg_ra(struct b400m *b4, u8 r, u8 rd, u8 a)
  606. {
  607. unsigned char res;
  608. if (down_trylock(&b4->regsem)) {
  609. if (down_interruptible(&b4->regsem)) {
  610. b4_info(b4, "b400m_getreg_ra(0x%02x) interrupted\n",
  611. a);
  612. return -1;
  613. }
  614. }
  615. xhfc_setreg(b4->wc, get_mod(b4), r, rd, &b4->lastreg);
  616. res = xhfc_getreg(b4->wc, get_mod(b4), a, &b4->lastreg);
  617. up(&b4->regsem);
  618. return res;
  619. }
  620. /*
  621. * XHFC-4S GPIO routines
  622. *
  623. * the xhfc doesn't use its gpio for anything. :-)
  624. */
  625. /*
  626. * initialize XHFC GPIO.
  627. * GPIO 0-7 are output, low (unconnected, or used for their primary function).
  628. */
  629. static void hfc_gpio_init(struct b400m *b4)
  630. {
  631. /* GPIO0..3,7 are GPIO, 4,5,6 primary function */
  632. b400m_setreg(b4, R_GPIO_SEL, 0x8f);
  633. /* GPIO0..7 drivers set low */
  634. b400m_setreg(b4, R_GPIO_OUT0, 0x00);
  635. /* GPIO0..7 drivers enabled */
  636. b400m_setreg(b4, R_GPIO_EN0, 0xff);
  637. /* all other GPIO set to primary function */
  638. b400m_setreg(b4, R_GPIO_SEL_BL, 0x00);
  639. }
  640. /* performs a register write and then waits for the HFC "busy" bit to clear
  641. * NOTE: doesn't actually read status, since busy bit is 1us typically, and
  642. * we're much, much slower than that. */
  643. static void hfc_setreg_waitbusy(struct b400m *b4, const unsigned int reg,
  644. const unsigned int val)
  645. {
  646. b400m_setreg(b4, reg, val);
  647. }
  648. /*
  649. * reads an 8-bit register over over and over until the same value is read
  650. * twice, then returns that value.
  651. */
  652. static unsigned char hfc_readcounter8(struct b400m *b4, const unsigned int reg)
  653. {
  654. unsigned char r1, r2;
  655. unsigned long maxwait = 1048576;
  656. do {
  657. r1 = b400m_getreg(b4, reg);
  658. r2 = b400m_getreg(b4, reg);
  659. } while ((r1 != r2) && maxwait--);
  660. if (!maxwait) {
  661. if (printk_ratelimit()) {
  662. dev_warn(&b4->wc->vb.pdev->dev,
  663. "hfc_readcounter8(reg 0x%02x) timed out " \
  664. "waiting for data to settle!\n", reg);
  665. }
  666. }
  667. return r1;
  668. }
  669. /* performs a soft-reset of the HFC-4S. */
  670. static void hfc_reset(struct b400m *b4)
  671. {
  672. unsigned long start;
  673. int TIMEOUT = HZ; /* 1s */
  674. /* Set the FIFOs to 8 128 bytes FIFOs, bidirectional, and set up the
  675. * flow controller for channel select mode. */
  676. /* Note, this reg has to be set *before* the SW reset */
  677. b400m_setreg(b4, R_FIFO_MD, V_FIFO_MD_01 | V_DF_MD_FSM);
  678. msleep(1); /* wait a bit for clock to settle */
  679. /* reset everything, wait 100ms, then allow the XHFC to come out of reset */
  680. b400m_setreg(b4, R_CIRM, V_SRES);
  681. flush_hw();
  682. msleep(100);
  683. b400m_setreg(b4, R_CIRM, 0x00);
  684. flush_hw();
  685. /* wait for XHFC to come out of reset. */
  686. start = jiffies;
  687. while (b400m_getreg(b4, R_STATUS) & (V_BUSY | V_PCM_INIT)) {
  688. if (time_after(jiffies, start + TIMEOUT)) {
  689. b4_info(b4, "hfc_reset() Module won't come out of "
  690. "reset... continuing.\n");
  691. break;
  692. }
  693. };
  694. /* Disable the output clock pin, and also the PLL (it's not needed) */
  695. b400m_setreg(b4, R_CTRL, 0x00);
  696. }
  697. static void hfc_enable_fifo_irqs(struct b400m *b4)
  698. {
  699. b400m_setreg(b4, R_IRQ_CTRL, V_FIFO_IRQ_EN | V_GLOB_IRQ_EN);
  700. flush_hw();
  701. }
  702. static void hfc_enable_interrupts(struct b400m *b4)
  703. {
  704. b4->running = 1;
  705. /* mask all misc interrupts */
  706. b4->misc_irq_mask = 0x01;
  707. b400m_setreg(b4, R_MISC_IRQMSK, b4->misc_irq_mask);
  708. /* clear any pending interrupts */
  709. b400m_getreg(b4, R_STATUS);
  710. b400m_getreg(b4, R_MISC_IRQ);
  711. b400m_getreg(b4, R_FIFO_BL0_IRQ);
  712. b400m_getreg(b4, R_FIFO_BL1_IRQ);
  713. b400m_getreg(b4, R_FIFO_BL2_IRQ);
  714. b400m_getreg(b4, R_FIFO_BL3_IRQ);
  715. hfc_enable_fifo_irqs(b4);
  716. }
  717. static inline void hfc_reset_fifo(struct b400m *b4)
  718. {
  719. hfc_setreg_waitbusy(b4, A_INC_RES_FIFO,
  720. V_RES_FIFO | V_RES_LOST | V_RES_FIFO_ERR);
  721. }
  722. static void hfc_setup_fifo(struct b400m *b4, int fifo)
  723. {
  724. if (fifo < 4) {
  725. /* TX */
  726. hfc_setreg_waitbusy(b4, R_FIFO, (fifo << V_FIFO_NUM_SHIFT));
  727. b400m_setreg(b4, A_CON_HDLC,
  728. V_HDLC_IRQ | V_DATA_FLOW_000 | V_IFF);
  729. hfc_reset_fifo(b4);
  730. /* RX */
  731. hfc_setreg_waitbusy(b4, R_FIFO,
  732. (fifo << V_FIFO_NUM_SHIFT) | V_FIFO_DIR);
  733. b400m_setreg(b4, A_CON_HDLC,
  734. V_HDLC_IRQ | V_DATA_FLOW_000 | V_IFF);
  735. hfc_reset_fifo(b4);
  736. } else {
  737. /* TX */
  738. hfc_setreg_waitbusy(b4, R_FIFO, (fifo << V_FIFO_NUM_SHIFT));
  739. b400m_setreg(b4, A_CON_HDLC,
  740. V_HDLC_TRP | V_TRP_NO_IRQ | V_DATA_FLOW_110);
  741. hfc_reset_fifo(b4);
  742. /* RX */
  743. hfc_setreg_waitbusy(b4, R_FIFO,
  744. (fifo << V_FIFO_NUM_SHIFT) | V_FIFO_DIR);
  745. b400m_setreg(b4, A_CON_HDLC,
  746. V_HDLC_TRP | V_TRP_NO_IRQ | V_DATA_FLOW_110);
  747. hfc_reset_fifo(b4);
  748. }
  749. }
  750. static void hfc_setup_pcm(struct b400m *b4, int port)
  751. {
  752. int physport;
  753. int offset;
  754. int hfc_chan;
  755. int ts;
  756. #ifdef HARDHDLC_RX
  757. const int MAX_OFFSET = 2;
  758. #else
  759. const int MAX_OFFSET = 3;
  760. #endif
  761. #ifdef SWAP_PORTS
  762. /* swap the middle ports */
  763. physport = (1 == port) ? 2 : (2 == port) ? 1 : port;
  764. #else
  765. physport = port;
  766. #endif
  767. for (offset = 0; offset < MAX_OFFSET; offset++) {
  768. hfc_chan = (port * 4) + offset;
  769. ts = (physport * 3) + offset;
  770. ts += (b4->b400m_no * 12);
  771. b400m_setreg(b4, R_SLOT, (ts << V_SL_NUM_SHIFT));
  772. b400m_setreg(b4, A_SL_CFG,
  773. (hfc_chan << V_CH_SNUM_SHIFT) |
  774. V_ROUT_TX_STIO2);
  775. if (offset < 2) {
  776. b400m_setreg(b4, R_SLOT,
  777. (ts << V_SL_NUM_SHIFT) |
  778. V_SL_DIR);
  779. b400m_setreg(b4, A_SL_CFG,
  780. (hfc_chan << V_CH_SNUM_SHIFT) |
  781. V_ROUT_RX_STIO1 | V_CH_SDIR);
  782. }
  783. }
  784. }
  785. #ifdef SWAP_PORTS
  786. #ifdef HARDHDLC_RX
  787. static const int fifos[24] = {0, 0, 2, 2, 1, 1, 3, 3, 4, 4, 4, 4, 6, 6, 6, 6,
  788. 5, 5, 5, 5, 7, 7, 7, 7 };
  789. #else
  790. static const int fifos[24] = {0, 4, 2, 6, 1, 5, 3, 7, 4, 4, 4, 4, 6, 6, 6, 6,
  791. 5, 5, 5, 5, 7, 7, 7, 7 };
  792. #endif
  793. static const int hfc_chans[12] = {2, 10, 6, 14, 0, 1, 8, 9, 4, 5, 12, 13 };
  794. #else
  795. #ifdef HARDHDLC_RX
  796. static const int fifos[24] = {0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5,
  797. 6, 6, 6, 6, 7, 7, 7, 7 };
  798. #else
  799. static const int fifos[24] = {0, 4, 1, 5, 2, 6, 3, 7, 4, 4, 4, 4, 5, 5, 5, 5,
  800. 6, 6, 6, 6, 7, 7, 7, 7 };
  801. #endif
  802. static const int hfc_chans[12] = { 2, 6, 10, 14, 0, 1, 4, 5, 8, 9, 12, 13 };
  803. #endif
  804. static void hfc_setup_fifo_arrays(struct b400m *b4, int fifo)
  805. {
  806. int val;
  807. if (!fifo) {
  808. val = (fifos[fifo] << V_FIRST_FIFO_NUM_SHIFT) | (fifo & 1);
  809. b400m_setreg(b4, R_FIRST_FIFO, val);
  810. } else {
  811. #ifdef HARDHDLC_RX
  812. val = (fifos[fifo] << V_NEXT_FIFO_NUM_SHIFT) | (fifo & 1);
  813. #else
  814. val = (fifo < 8) ? (fifos[fifo] << V_NEXT_FIFO_NUM_SHIFT) :
  815. (fifos[fifo] << V_NEXT_FIFO_NUM_SHIFT) |
  816. (fifo&1);
  817. #endif
  818. b400m_setreg(b4, A_FIFO_SEQ, val);
  819. }
  820. b400m_setreg(b4, R_FSM_IDX, fifo);
  821. val = (fifo < 8) ? (hfc_chans[fifo>>1] << V_CH_FNUM_SHIFT) :
  822. (hfc_chans[fifo>>1] << V_CH_FNUM_SHIFT) |
  823. (fifo & 1);
  824. b400m_setreg(b4, A_CHANNEL, val);
  825. b400m_setreg(b4, A_SUBCH_CFG, 0x02);
  826. }
  827. static void hfc_setup_fsm(struct b400m *b4)
  828. {
  829. int chan, fifo, port, offset;
  830. #ifdef SWAP_PORTS
  831. const int chan_to_fifo[12] = { 4, 4, 0, 6, 6, 2, 5, 5, 1, 7, 7, 3 };
  832. #else
  833. const int chan_to_fifo[12] = { 4, 4, 0, 5, 5, 1, 6, 6, 2, 7, 7, 3 };
  834. #endif
  835. for (port = 0; port < 4; port++) {
  836. for (offset = 0; offset < 3; offset++) {
  837. b4->spans[port].fifos[offset] =
  838. chan_to_fifo[(port * 3) + offset];
  839. }
  840. }
  841. for (chan = 0; chan < ARRAY_SIZE(fifos); chan++)
  842. hfc_setup_fifo_arrays(b4, chan);
  843. b400m_setreg(b4, A_FIFO_SEQ, V_SEQ_END);
  844. for (fifo = 0; fifo < 8; fifo++)
  845. hfc_setup_fifo(b4, fifo);
  846. for (port = 0; port < 4; port++)
  847. hfc_setup_pcm(b4, port);
  848. }
  849. /* takes a read/write fifo pair and optionally resets it, optionally enabling
  850. * the rx/tx interrupt */
  851. static void hfc_reset_fifo_pair(struct b400m *b4, int fifo,
  852. int reset, int force_no_irq)
  853. {
  854. unsigned char b;
  855. if (down_interruptible(&b4->fifosem)) {
  856. b4_info(b4, "Unable to retrieve fifo sem\n");
  857. return;
  858. }
  859. b = (!force_no_irq && b4->fifo_en_txint & (1 << fifo)) ?
  860. V_FIFO_IRQMSK : 0;
  861. hfc_setreg_waitbusy(b4, R_FIFO, (fifo << V_FIFO_NUM_SHIFT));
  862. if (fifo < 4)
  863. b |= V_MIX_IRQ;
  864. b400m_setreg(b4, A_FIFO_CTRL, b);
  865. if (reset)
  866. hfc_reset_fifo(b4);
  867. b = (!force_no_irq && b4->fifo_en_rxint & (1 << fifo)) ?
  868. V_FIFO_IRQMSK : 0;
  869. hfc_setreg_waitbusy(b4, R_FIFO,
  870. (fifo << V_FIFO_NUM_SHIFT) | V_FIFO_DIR);
  871. if (fifo < 4)
  872. b |= V_MIX_IRQ;
  873. b400m_setreg(b4, A_FIFO_CTRL, b);
  874. if (reset)
  875. hfc_reset_fifo(b4);
  876. up(&b4->fifosem);
  877. }
  878. static void xhfc_set_sync_src(struct b400m *b4, int port)
  879. {
  880. int b;
  881. /* -2 means we need to go back and try again later */
  882. if (port == -2)
  883. return;
  884. if (port == b4->setsyncspan)
  885. return;
  886. else
  887. b4->setsyncspan = port;
  888. b4_info(b4, "xhfc_set_sync_src - modpos %d: setting sync to "
  889. "be port %d\n", b4->position, port);
  890. if (port == -1) /* automatic */
  891. b = 0;
  892. else {
  893. #ifdef SWAP_PORTS
  894. port = (1 == port) ? 2 : (2 == port) ? 1 : port;
  895. #endif
  896. b = (port & V_SYNC_SEL_MASK) | V_MAN_SYNC;
  897. }
  898. b400m_setreg(b4, R_SU_SYNC, b);
  899. }
  900. static void wctdm_change_card_sync_src(struct wctdm *wc, int newsrc, int master)
  901. {
  902. int newctlreg;
  903. newctlreg = wc->ctlreg;
  904. if (master)
  905. newctlreg |= (1 << 5);
  906. else
  907. newctlreg &= ~(1 << 5);
  908. newctlreg &= 0xfc;
  909. newctlreg |= newsrc;
  910. if (DBG_TIMING) {
  911. dev_info(&wc->vb.pdev->dev,
  912. "Final ctlreg before swap: %02x\n", newctlreg);
  913. }
  914. wc->ctlreg = newctlreg;
  915. wc->oldsync = newsrc;
  916. msleep(10);
  917. }
  918. static void wctdm_change_system_sync_src(int oldsync, int oldspan,
  919. int newsync, int newspan)
  920. {
  921. struct wctdm *wc;
  922. struct wctdm *oldsyncwc = NULL, *newsyncwc = NULL;
  923. int newspot;
  924. int i;
  925. int max_latency = 0;
  926. if (oldsync > -1)
  927. oldsyncwc = ifaces[oldsync];
  928. if (newsync > -1)
  929. newsyncwc = ifaces[newsync];
  930. if (newsync == -1) {
  931. BUG_ON(!ifaces[0]);
  932. newsyncwc = ifaces[0];
  933. newsync = 0;
  934. }
  935. newspot = (-1 == newspan) ? 0 : 2 | (newspan >> 2);
  936. if ((oldsync == newsync) && (oldspan == newspan)) {
  937. dev_info(&newsyncwc->vb.pdev->dev,
  938. "No need for timing change. All is same\n");
  939. return;
  940. }
  941. /* First we set all sources to local timing */
  942. for (i = 0; i < WC_MAX_IFACES; i++) {
  943. wc = ifaces[i];
  944. if ((wc != oldsyncwc) && wc) {
  945. wctdm_change_card_sync_src(wc, 0, 0);
  946. if (voicebus_current_latency(&wc->vb) > max_latency)
  947. max_latency = voicebus_current_latency(&wc->vb);
  948. }
  949. }
  950. msleep(max_latency << 1);
  951. /* Set the old sync source to local timing, not driving timing */
  952. if (oldsyncwc) {
  953. wctdm_change_card_sync_src(oldsyncwc, 0, 0);
  954. msleep(voicebus_current_latency(&oldsyncwc->vb) << 1);
  955. }
  956. dev_info(&newsyncwc->vb.pdev->dev,
  957. "Setting new card %d now to be timing master\n", newsync);
  958. /* Finally, set the new sync source to broadcast master timing */
  959. wctdm_change_card_sync_src(newsyncwc, newspot, 1);
  960. msleep(voicebus_current_latency(&newsyncwc->vb) << 1);
  961. /* Last we double verify and set all the remaining cards to be timing
  962. * slaves */
  963. for (i = 0; (i < WC_MAX_IFACES) && ifaces[i]; i++) {
  964. wc = ifaces[i];
  965. if (i == newsync)
  966. continue;
  967. dev_info(&wc->vb.pdev->dev,
  968. "Setting card %d to be timing slave\n", i);
  969. wctdm_change_card_sync_src(wc, 1, 0);
  970. }
  971. msleep(max_latency << 1);
  972. synccard = newsync;
  973. syncspan = newspan;
  974. }
  975. static int xhfc_find_sync_with_timingcable(struct b400m *b4)
  976. {
  977. struct wctdm *wc = b4->wc;
  978. int i, j, osrc, src = -1;
  979. int lowestprio = 10000;
  980. int lowestcard = -1;
  981. if (down_trylock(&ifacelock)) {
  982. set_bit(WCTDM_CHECK_TIMING, &wc->checkflag);
  983. return -2;
  984. }
  985. for (j = 0; j < WC_MAX_IFACES && ifaces[j]; j++) {
  986. if (is_initialized(ifaces[j])) {
  987. set_bit(WCTDM_CHECK_TIMING, &wc->checkflag);
  988. osrc = -2;
  989. goto out;
  990. } else {
  991. for (i = 0; i < (MAX_SPANS - 1); i++) {
  992. struct wctdm_span *wspan = ifaces[j]->spans[i];
  993. if (wspan &&
  994. wspan->timing_priority &&
  995. !wspan->span.alarms &&
  996. (wspan->timing_priority <
  997. lowestprio)) {
  998. src = i;
  999. lowestprio = wspan->timing_priority;
  1000. lowestcard = j;
  1001. }
  1002. }
  1003. }
  1004. }
  1005. if (lowestcard != synccard) {
  1006. b4_info(b4, "Found new timing master, card "
  1007. "%d. Old is card %d\n", lowestcard, synccard);
  1008. } else if (src != syncspan) {
  1009. b4_info(b4, "Timing change, but only from %d to %d on "
  1010. "card %d\n", syncspan, src, lowestcard);
  1011. }
  1012. wctdm_change_system_sync_src(synccard, syncspan,
  1013. lowestcard, src);
  1014. osrc = -1;
  1015. if (wc == ifaces[lowestcard]) {
  1016. if (src < (b4->position + 4) && (src >= b4->position))
  1017. osrc = src - b4->position;
  1018. }
  1019. out:
  1020. up(&ifacelock);
  1021. return osrc;
  1022. }
  1023. static int xhfc_find_sync_without_timingcable(struct b400m *b4)
  1024. {
  1025. struct wctdm *wc = b4->wc;
  1026. int i, osrc, src = -1;
  1027. int lowestprio = 10000;
  1028. int newctlregmux;
  1029. if (down_trylock(&wc->syncsem)) {
  1030. set_bit(WCTDM_CHECK_TIMING, &wc->checkflag);
  1031. return -2;
  1032. }
  1033. /* Find lowest slave timing priority on digital spans */
  1034. for (i = 0; i < (MAX_SPANS - 1); i++) {
  1035. struct wctdm_span *const wspan = wc->spans[i];
  1036. if (wspan && wspan->timing_priority &&
  1037. !wspan->span.alarms &&
  1038. (wspan->timing_priority < lowestprio)) {
  1039. src = i;
  1040. lowestprio = wspan->timing_priority;
  1041. }
  1042. }
  1043. if (src < 0) {
  1044. if (DBG_TIMING)
  1045. b4_info(b4, "Picked analog span\n");
  1046. osrc = src;
  1047. goto check_card_timing;
  1048. } else {
  1049. if (DBG_TIMING) {
  1050. b4_info(b4, "Picked span offset %d to be timing "
  1051. "source\n", src);
  1052. }
  1053. }
  1054. osrc = ((src < (b4->position + 4)) && (src >= b4->position)) ?
  1055. src - b4->position : -1;
  1056. if (DBG_TIMING) {
  1057. b4_info(b4, "For b4->position %d timing is %d\n",
  1058. b4->position, osrc);
  1059. }
  1060. check_card_timing:
  1061. if (src != -1)
  1062. newctlregmux = 2 | (src >> 2);
  1063. else
  1064. newctlregmux = 0;
  1065. if ((newctlregmux & 3) != (wc->ctlreg & 3)) {
  1066. if (DBG_TIMING) {
  1067. b4_info(b4, "!!!Need to change timing "
  1068. "on baseboard to spot %d!!!\n",
  1069. src >> 2);
  1070. }
  1071. wctdm_change_card_sync_src(wc, newctlregmux, 0);
  1072. } else {
  1073. if (DBG_TIMING) {
  1074. dev_info(&b4->wc->vb.pdev->dev, "!!!No need to change timing " \
  1075. "on baseboard to spot %d, already there!!!\n",
  1076. src >> 2);
  1077. }
  1078. }
  1079. up(&wc->syncsem);
  1080. return osrc;
  1081. }
  1082. /*
  1083. * Finds the highest-priority sync span that is not in alarm and returns it.
  1084. * Note: the span #s in b4->spans[].sync are 1-based, and this returns a
  1085. * 0-based span, or -1 if no spans are found.
  1086. */
  1087. static inline int xhfc_find_sync(struct b400m *b4)
  1088. {
  1089. if (timingcable)
  1090. return xhfc_find_sync_with_timingcable(b4);
  1091. else
  1092. return xhfc_find_sync_without_timingcable(b4);
  1093. }
  1094. /*
  1095. * allocates memory and pretty-prints a given S/T state engine state to it.
  1096. * calling routine is responsible for freeing the pointer returned! Performs
  1097. * no hardware access whatsoever, but does use GFP_KERNEL so do not call from
  1098. * IRQ context. if full == 1, prints a "full" dump; otherwise just prints
  1099. * current state.
  1100. */
  1101. static char *hfc_decode_st_state(struct b400m *b4, struct b400m_span *span,
  1102. unsigned char state, int full)
  1103. {
  1104. int nt, sta;
  1105. char s[128], *str;
  1106. const char *ststr[2][16] = { /* TE, NT */
  1107. { "RESET", "?", "SENSING", "DEACT.",
  1108. "AWAIT.SIG", "IDENT.INPUT", "SYNCD", "ACTIVATED",
  1109. "LOSTFRAMING", "?", "?", "?",
  1110. "?", "?", "?", "?" },
  1111. { "RESET", "DEACT.", "PEND.ACT", "ACTIVE",
  1112. "PEND.DEACT", "?", "?", "?",
  1113. "?", "?", "?", "?",
  1114. "?", "?", "?", "?" }
  1115. };
  1116. str = kmalloc(256, GFP_KERNEL);
  1117. if (!str) {
  1118. dev_warn(&b4->wc->vb.pdev->dev,
  1119. "could not allocate mem for ST state decode " \
  1120. "string!\n");
  1121. return NULL;
  1122. }
  1123. nt = (span->te_mode == 0);
  1124. sta = (state & V_SU_STA_MASK);
  1125. sprintf(str, "P%d: %s state %c%d (%s)", span->port + 1,
  1126. (nt ? "NT" : "TE"), (nt ? 'G' : 'F'), sta,
  1127. ststr[nt][sta]);
  1128. if (full) {
  1129. sprintf(s, " SYNC: %s, RX INFO0: %s",
  1130. ((state & V_SU_FR_SYNC) ? "yes" : "no"),
  1131. ((state & V_SU_INFO0) ? "yes" : "no"));
  1132. strcat(str, s);
  1133. if (nt) {
  1134. sprintf(s, ", T2 %s, auto G2->G3: %s",
  1135. ((state & V_SU_T2_EXP) ? "expired" : "OK"),
  1136. ((state & V_G2_G3) ? "yes" : "no"));
  1137. strcat(str, s);
  1138. }
  1139. }
  1140. return str;
  1141. }
  1142. /*
  1143. * sets an S/T port state machine to a given state. if 'auto' is nonzero,
  1144. * will put the state machine back in auto mode after setting the state.
  1145. */
  1146. static void hfc_handle_state(struct b400m_span *s);
  1147. static void hfc_force_st_state(struct b400m *b4, struct b400m_span *s,
  1148. int state, int resume_auto)
  1149. {
  1150. b400m_setreg_ra(b4, R_SU_SEL, s->port, A_SU_WR_STA,
  1151. state | V_SU_LD_STA);
  1152. if (resume_auto)
  1153. b400m_setreg_ra(b4, R_SU_SEL, s->port, A_SU_WR_STA, state);
  1154. if (DBG_ST && ((1 << s->port) & bri_spanfilter)) {
  1155. char *x;
  1156. x = hfc_decode_st_state(b4, s, state, 1);
  1157. b4_info(b4, "forced port %d to state %d (auto: %d), "
  1158. "new decode: %s\n", s->port + 1, state,
  1159. resume_auto, x);
  1160. kfree(x);
  1161. }
  1162. /* make sure that we activate any timers/etc needed by this state
  1163. * change */
  1164. hfc_handle_state(s);
  1165. }
  1166. /* figures out what to do when an S/T port's timer expires. */
  1167. static void hfc_timer_expire(struct b400m_span *s, int t_no)
  1168. {
  1169. struct b400m *b4 = s->parent;
  1170. if (DBG_ST && ((1 << s->port) & bri_spanfilter)) {
  1171. b4_info(b4, "%lu: hfc_timer_expire, Port %d T%d "
  1172. "expired (value=%lu ena=%d)\n", b4->ticks,
  1173. s->port + 1, t_no + 1, s->hfc_timers[t_no],
  1174. s->hfc_timer_on[t_no]);
  1175. }
  1176. /*
  1177. * there are three timers associated with every HFC S/T port.
  1178. *
  1179. * T1 is used by the NT state machine, and is the maximum time the NT
  1180. * side should wait for G3 (active) state.
  1181. *
  1182. * T2 is not actually used in the driver, it is handled by the HFC-4S
  1183. * internally.
  1184. *
  1185. * T3 is used by the TE state machine; it is the maximum time the TE
  1186. * side should wait for the INFO4 (activated) signal.
  1187. */
  1188. /* First, disable the expired timer; hfc_force_st_state() may activate
  1189. * it again. */
  1190. s->hfc_timer_on[t_no] = 0;
  1191. switch (t_no) {
  1192. case XHFC_T1: /* switch to G4 (pending deact.), resume auto mode */
  1193. hfc_force_st_state(b4, s, 4, 1);
  1194. break;
  1195. case XHFC_T2: /* switch to G1 (deactivated), resume auto mode */
  1196. hfc_force_st_state(b4, s, 1, 1);
  1197. break;
  1198. case XHFC_T3: /* switch to F3 (deactivated), resume auto mode */
  1199. hfc_stop_st(s);
  1200. if (bri_persistentlayer1)
  1201. hfc_start_st(s);
  1202. break;
  1203. case XHFC_T4: /* switch to F3 (deactivated), resume auto mode */
  1204. hfc_handle_state(s);
  1205. s->hfc_timers[XHFC_T4] = b4->ticks + 1000;
  1206. s->hfc_timer_on[XHFC_T4] = 1;
  1207. break;
  1208. default:
  1209. if (printk_ratelimit()) {
  1210. dev_warn(&b4->wc->vb.pdev->dev,
  1211. "hfc_timer_expire found an unknown expired "
  1212. "timer (%d)??\n", t_no);
  1213. }
  1214. }
  1215. }
  1216. /*
  1217. * Run through the active timers on a card and deal with any expiries.
  1218. * Also see if the alarm debounce time has expired and if it has, tell DAHDI.
  1219. */
  1220. static void hfc_update_st_timers(struct b400m *b4)
  1221. {
  1222. int i, j;
  1223. struct b400m_span *s;
  1224. for (i = 0; i < 4; i++) {
  1225. s = &b4->spans[i];
  1226. for (j = XHFC_T1; j <= XHFC_T4; j++) {
  1227. /* we don't really do timer2, it is expired by the
  1228. * state change handler */
  1229. if (j == XHFC_T2)
  1230. continue;
  1231. if (s->hfc_timer_on[j] &&
  1232. time_after_eq(b4->ticks, s->hfc_timers[j]))
  1233. hfc_timer_expire(s, j);
  1234. }
  1235. if (s->wspan && s->newalarm != s->wspan->span.alarms &&
  1236. time_after_eq(b4->ticks, s->alarmtimer)) {
  1237. s->wspan->span.alarms = s->newalarm;
  1238. if ((!s->newalarm && bri_teignorered) || (!bri_teignorered))
  1239. dahdi_alarm_notify(&s->wspan->span);
  1240. if (DBG_ALARM) {
  1241. dev_info(&b4->wc->vb.pdev->dev, "span %d: alarm " \
  1242. "%d debounced\n", i + 1,
  1243. s->newalarm);
  1244. }
  1245. set_bit(WCTDM_CHECK_TIMING, &b4->wc->checkflag);
  1246. }
  1247. }
  1248. if (test_and_clear_bit(WCTDM_CHECK_TIMING, &b4->wc->checkflag))
  1249. xhfc_set_sync_src(b4, xhfc_find_sync(b4));
  1250. }
  1251. /* this is the driver-level state machine for an S/T port */
  1252. static void hfc_handle_state(struct b400m_span *s)
  1253. {
  1254. struct b400m *b4;
  1255. unsigned char state, sta;
  1256. int nt, newsync, oldalarm;
  1257. unsigned long oldtimer;
  1258. b4 = s->parent;
  1259. nt = !s->te_mode;
  1260. state = b400m_getreg_ra(b4, R_SU_SEL, s->port, A_SU_RD_STA);
  1261. sta = (state & V_SU_STA_MASK);
  1262. if (DBG_ST && ((1 << s->port) & bri_spanfilter)) {
  1263. char *x;
  1264. x = hfc_decode_st_state(b4, s, state, 1);
  1265. b4_info(b4, "port %d A_SU_RD_STA old=0x%02x "
  1266. "now=0x%02x, decoded: %s\n", s->port + 1,
  1267. s->oldstate, state, x);
  1268. kfree(x);
  1269. }
  1270. oldalarm = s->newalarm;
  1271. oldtimer = s->alarmtimer;
  1272. if (nt) {
  1273. switch (sta) {
  1274. default: /* Invalid NT state */
  1275. case 0x0: /* NT state G0: Reset */
  1276. case 0x1: /* NT state G1: Deactivated */
  1277. case 0x4: /* NT state G4: Pending Deactivation */
  1278. s->newalarm = DAHDI_ALARM_RED;
  1279. break;
  1280. case 0x2: /* NT state G2: Pending Activation */
  1281. s->newalarm = DAHDI_ALARM_YELLOW;
  1282. break;
  1283. case 0x3: /* NT state G3: Active */
  1284. s->hfc_timer_on[XHFC_T1] = 0;
  1285. s->newalarm = 0;
  1286. break;
  1287. }
  1288. } else {
  1289. switch (sta) {
  1290. default: /* Invalid TE state */
  1291. case 0x0: /* TE state F0: Reset */
  1292. case 0x2: /* TE state F2: Sensing */
  1293. case 0x3: /* TE state F3: Deactivated */
  1294. case 0x4: /* TE state F4: Awaiting Signal */
  1295. case 0x8: /* TE state F8: Lost Framing */
  1296. s->newalarm = DAHDI_ALARM_RED;
  1297. break;
  1298. case 0x5: /* TE state F5: Identifying Input */
  1299. case 0x6: /* TE state F6: Synchronized */
  1300. s->newalarm = DAHDI_ALARM_YELLOW;
  1301. break;
  1302. case 0x7: /* TE state F7: Activated */
  1303. s->hfc_timer_on[XHFC_T3] = 0;
  1304. s->hfc_timer_on[XHFC_T4] = 0;
  1305. s->newalarm = 0;
  1306. break;
  1307. }
  1308. }
  1309. s->alarmtimer = b4->ticks + bri_alarmdebounce;
  1310. s->oldstate = state;
  1311. if (DBG_ALARM) {
  1312. b4_info(b4, "span %d: old alarm %d expires %ld, "
  1313. "new alarm %d expires %ld\n", s->port + 1, oldalarm,
  1314. oldtimer, s->newalarm, s->alarmtimer);
  1315. }
  1316. /* we only care about T2 expiry in G4. */
  1317. if (nt && (sta == 4) && (state & V_SU_T2_EXP)) {
  1318. if (s->hfc_timer_on[XHFC_T2])
  1319. hfc_timer_expire(s, XHFC_T2); /* handle T2 expiry */
  1320. }
  1321. /* If we're in F3 and receiving INFO0, start T3 and jump to F4 */
  1322. if (!nt && (sta == 3) && (state & V_SU_INFO0)) {
  1323. if (bri_persistentlayer1) {
  1324. s->hfc_timers[XHFC_T3] = b4->ticks + TIMER_3_MS;
  1325. s->hfc_timer_on[XHFC_T3] = 1;
  1326. if (DBG_ST) {
  1327. b4_info(b4, "port %d: receiving "
  1328. "INFO0 in state 3, setting T3 and "
  1329. "jumping to F4\n", s->port + 1);
  1330. }
  1331. hfc_start_st(s);
  1332. }
  1333. }
  1334. /* read in R_BERT_STA to determine where our current sync source is */
  1335. newsync = b400m_getreg(b4, R_BERT_STA) & 0x07;
  1336. if (newsync != b4->reportedsyncspan) {
  1337. if (DBG_TIMING) {
  1338. if (newsync == 5) {
  1339. b4_info(b4, "new card sync source: SYNC_I\n");
  1340. } else {
  1341. b4_info(b4, "Card position %d: new "
  1342. "sync source: port %d\n",
  1343. b4->position, newsync);
  1344. }
  1345. }
  1346. b4->reportedsyncspan = newsync;
  1347. }
  1348. }
  1349. static void hfc_stop_all_timers(struct b400m_span *s)
  1350. {
  1351. s->hfc_timer_on[XHFC_T4] = 0;
  1352. s->hfc_timer_on[XHFC_T3] = 0;
  1353. s->hfc_timer_on[XHFC_T2] = 0;
  1354. s->hfc_timer_on[XHFC_T1] = 0;
  1355. }
  1356. static void hfc_stop_st(struct b400m_span *s)
  1357. {
  1358. struct b400m *b4 = s->parent;
  1359. hfc_stop_all_timers(s);
  1360. b400m_setreg_ra(b4, R_SU_SEL, s->port, A_SU_WR_STA, V_SU_ACT_DEACTIVATE);
  1361. }
  1362. /*
  1363. * resets an S/T interface to a given NT/TE mode
  1364. */
  1365. static void hfc_reset_st(struct b400m_span *s)
  1366. {
  1367. int b;
  1368. struct b400m *b4;
  1369. b4 = s->parent;
  1370. hfc_stop_st(s);
  1371. /* force state G0/F0 (reset), then force state 1/2
  1372. * (deactivated/sensing) */
  1373. b400m_setreg_ra(b4, R_SU_SEL, s->port, A_SU_WR_STA, V_SU_LD_STA);
  1374. flush_hw(); /* make sure write hit hardware */
  1375. s->wspan->span.alarms = DAHDI_ALARM_RED;
  1376. s->newalarm = DAHDI_ALARM_RED;
  1377. dahdi_alarm_notify(&s->wspan->span);
  1378. /* set up the clock control register. Must be done before we activate
  1379. * the interface. */
  1380. if (s->te_mode)
  1381. b = 0x0e;
  1382. else
  1383. b = 0x0c | (6 << V_SU_SMPL_SHIFT);
  1384. b400m_setreg(b4, A_SU_CLK_DLY, b);
  1385. /* set TE/NT mode, enable B and D channels. */
  1386. b400m_setreg(b4, A_SU_CTRL0, V_B1_TX_EN | V_B2_TX_EN |
  1387. (s->te_mode ? 0 : V_SU_MD) | V_ST_PU_CTRL);
  1388. b400m_setreg(b4, A_SU_CTRL1, V_G2_G3_EN);
  1389. b400m_setreg(b4, A_SU_CTRL2, V_B1_RX_EN | V_B2_RX_EN);
  1390. b400m_setreg(b4, A_ST_CTRL3, (0x7c << 1));
  1391. /* enable the state machine. */
  1392. b400m_setreg(b4, A_SU_WR_STA, 0x00);
  1393. flush_hw();
  1394. }
  1395. static void hfc_start_st(struct b400m_span *s)
  1396. {
  1397. struct b400m *b4 = s->parent;
  1398. b400m_setreg_ra(b4, R_SU_SEL, s->port, A_SU_WR_STA, V_SU_ACT_ACTIVATE);
  1399. /* start T1 if in NT mode, T3 if in TE mode */
  1400. if (s->te_mode) {
  1401. /* 500ms wait first time, TIMER_3_MS afterward. */
  1402. s->hfc_timers[XHFC_T3] = b4->ticks + TIMER_3_MS;
  1403. s->hfc_timer_on[XHFC_T3] = 1;
  1404. s->hfc_timer_on[XHFC_T1] = 0;
  1405. s->hfc_timers[XHFC_T4] = b4->ticks + 1000;
  1406. s->hfc_timer_on[XHFC_T4] = 1;
  1407. if (DBG_ST) {
  1408. b4_info(b4, "setting port %d t3 timer to %lu\n",
  1409. s->port + 1, s->hfc_timers[XHFC_T3]);
  1410. }
  1411. } else {
  1412. static const int TIMER_1_MS = 2000;
  1413. s->hfc_timers[XHFC_T1] = b4->ticks + TIMER_1_MS;
  1414. s->hfc_timer_on[XHFC_T1] = 1;
  1415. s->hfc_timer_on[XHFC_T3] = 0;
  1416. if (DBG_ST) {
  1417. b4_info(b4, "setting port %d t1 timer to %lu\n",
  1418. s->port + 1, s->hfc_timers[XHFC_T1]);
  1419. }
  1420. }
  1421. }
  1422. /*
  1423. * read in the HFC GPIO to determine each port's mode (TE or NT).
  1424. * Then, reset and start the port.
  1425. * the flow controller should be set up before this is called.
  1426. */
  1427. static int hdlc_start(struct b400m *b4, int fifo);
  1428. static void hfc_init_all_st(struct b400m *b4)
  1429. {
  1430. int i;
  1431. struct b400m_span *s;
  1432. for (i = 0; i < 4; i++) {
  1433. s = &b4->spans[i];
  1434. s->parent = b4;
  1435. #ifdef SWAP_PORTS
  1436. s->port = (1 == i) ? 2 : (2 == i) ? 1 : i;
  1437. #else
  1438. s->port = i;
  1439. #endif
  1440. s->te_mode = 1;
  1441. hdlc_start(b4, s->fifos[2]);
  1442. }
  1443. }
  1444. /* NOTE: assumes fifo lock is held */
  1445. #define debug_fz(b4, fifo, prefix, buf) \
  1446. do { \
  1447. sprintf(buf, "%s: (fifo %d): f1/f2/flen=%d/%d/%d, " \
  1448. "z1/z2/zlen=%d/%d/%d\n", prefix, fifo, f1, f2, flen, z1, \
  1449. z2, zlen); \
  1450. } while (0)
  1451. /* enable FIFO RX int and reset the FIFO */
  1452. static int hdlc_start(struct b400m *b4, int fifo)
  1453. {
  1454. b4->fifo_en_txint |= (1 << fifo);
  1455. b4->fifo_en_rxint |= (1 << fifo);
  1456. hfc_reset_fifo_pair(b4, fifo, 1, 0);
  1457. return 0;
  1458. }
  1459. #ifdef HARDHDLC_RX
  1460. /**
  1461. * hdlc_signal_complete() - Signal dahdi that we have a complete frame.
  1462. *
  1463. * @bpan: The span which received the frame.
  1464. * @stat: The frame status from the XHFC controller.
  1465. *
  1466. */
  1467. static void hdlc_signal_complete(struct b400m_span *bspan, u8 stat)
  1468. {
  1469. struct b400m *b4 = bspan->parent;
  1470. /* if STAT != 0, indicates bad frame */
  1471. if (stat != 0x00) {
  1472. if (DBG_HDLC && DBG_SPANFILTER) {
  1473. b4_info(b4, "(span %d) STAT=0x%02x indicates " \
  1474. "frame problem: %s\n", bspan->port + 1, stat,
  1475. (0xff == stat) ? "HDLC Abort" : "Bad FCS");
  1476. }
  1477. dahdi_hdlc_abort(bspan->sigchan, (0xff == stat) ?
  1478. DAHDI_EVENT_ABORT : DAHDI_EVENT_BADFCS);
  1479. /* STAT == 0, means frame was OK */
  1480. } else {
  1481. if (DBG_HDLC && DBG_SPANFILTER) {
  1482. b4_info(b4, "(span %d) Frame %d is good!\n",
  1483. bspan->port + 1, bspan->frames_in);
  1484. }
  1485. dahdi_hdlc_finish(bspan->sigchan);
  1486. }
  1487. }
  1488. /*
  1489. * Inner loop for D-channel receive function. Retrieves HDLC data from the
  1490. * hardware. If the hardware indicates that the frame is complete, we check
  1491. * the HDLC engine's STAT byte and update DAHDI as needed.
  1492. *
  1493. * Returns the number of HDLC frames left in the FIFO, or -1 if we couldn't
  1494. * get the lock.
  1495. */
  1496. static int hdlc_rx_frame(struct b400m_span *bspan)
  1497. {
  1498. int fifo, i, j, x, zleft;
  1499. int z1, z2, zlen, f1, f2, flen, new_flen;
  1500. unsigned char buf[B400M_HDLC_BUF_LEN];
  1501. char debugbuf[256];
  1502. struct b400m *b4 = bspan->parent;
  1503. fifo = bspan->fifos[2];
  1504. if (DBG_HDLC && DBG_SPANFILTER)
  1505. b4_info(b4, "hdlc_rx_frame fifo %d: start\n", fifo);
  1506. if (down_trylock(&b4->fifosem) && DBG_HDLC && DBG_SPANFILTER) {
  1507. b4_info(b4, "rx_frame: fifo %d 1: couldn't get lock\n",
  1508. fifo);
  1509. return -1;
  1510. }
  1511. hfc_setreg_waitbusy(b4, R_FIFO,
  1512. (fifo << V_FIFO_NUM_SHIFT) | V_FIFO_DIR);
  1513. get_F(f1, f2, flen);
  1514. get_Z(z1, z2, zlen);
  1515. debug_fz(b4, fifo, "hdlc_rx_frame", debugbuf);
  1516. up(&b4->fifosem);
  1517. if (DBG_HDLC && DBG_SPANFILTER)
  1518. pr_info("%s", debugbuf);
  1519. /* if we have at least one complete frame, increment zleft to include
  1520. * status byte */
  1521. zleft = zlen;
  1522. if (flen)
  1523. zleft++;
  1524. do {
  1525. if (zleft > B400M_HDLC_BUF_LEN)
  1526. j = B400M_HDLC_BUF_LEN;
  1527. else
  1528. j = zleft;
  1529. if (down_trylock(&b4->fifosem) && DBG_HDLC && DBG_SPANFILTER) {
  1530. b4_info(b4,
  1531. "rx_frame fifo %d 2: couldn't get lock\n",
  1532. fifo);
  1533. return -1;
  1534. }
  1535. hfc_setreg_waitbusy(b4, R_FIFO,
  1536. (fifo << V_FIFO_NUM_SHIFT) | V_FIFO_DIR);
  1537. for (i = 0; i < j; i++)
  1538. buf[i] = b400m_getreg(b4, A_FIFO_DATA);
  1539. up(&b4->fifosem);
  1540. /* don't send STAT byte to DAHDI */
  1541. x = j;
  1542. if (bspan->sigchan) {
  1543. if ((j != B400M_HDLC_BUF_LEN) && flen)
  1544. x--;
  1545. if (x)
  1546. dahdi_hdlc_putbuf(bspan->sigchan, buf, x);
  1547. }
  1548. zleft -= j;
  1549. if (DBG_HDLC && DBG_SPANFILTER) {
  1550. b4_info(b4, "transmitted %d bytes to dahdi, " \
  1551. "zleft=%d\n", x, zleft);
  1552. }
  1553. if (DBG_HDLC && DBG_SPANFILTER) {
  1554. /* !!! */
  1555. b4_info(b4, "hdlc_rx_frame(span %d): " \
  1556. "z1/z2/zlen=%d/%d/%d, zleft=%d\n",
  1557. bspan->port + 1, z1, z2, zlen, zleft);
  1558. for (i = 0; i < j; i++) {
  1559. b4_info(b4, "%02x%c", buf[i],
  1560. (i < (j - 1)) ? ' ' : '\n');
  1561. }
  1562. }
  1563. } while (zleft > 0);
  1564. /* Frame received, increment F2 and get an updated count of frames
  1565. * left */
  1566. if (down_trylock(&b4->fifosem) && DBG_HDLC && DBG_SPANFILTER) {
  1567. b4_info(b4, "rx_frame fifo %d 3: couldn't get lock\n",
  1568. fifo);
  1569. return 0;
  1570. }
  1571. /* go get the F count again, just in case another frame snuck in while
  1572. * we weren't looking. */
  1573. if (flen) {
  1574. hfc_setreg_waitbusy(b4, A_INC_RES_FIFO, V_INC_F);
  1575. ++bspan->frames_in;
  1576. get_F(f1, f2, new_flen);
  1577. } else
  1578. new_flen = flen;
  1579. up(&b4->fifosem);
  1580. /* If this channel is not configured with a signalling span we don't
  1581. * need to notify the rest of dahdi about this frame. */
  1582. if (!bspan->sigchan) {
  1583. if (DBG_HDLC && DBG_SPANFILTER) {
  1584. b4_info(b4, "hdlc_rx_frame fifo %d: " \
  1585. "new_flen %d, early end.\n", fifo, new_flen);
  1586. }
  1587. return new_flen;
  1588. }
  1589. if (flen) {
  1590. /* disable < 3 check for now */
  1591. if (0 && zlen < 3) {
  1592. if (DBG_HDLC && DBG_SPANFILTER)
  1593. b4_info(b4, "odd, zlen less then 3?\n");
  1594. dahdi_hdlc_abort(bspan->sigchan, DAHDI_EVENT_ABORT);
  1595. } else {
  1596. hdlc_signal_complete(bspan, buf[i - 1]);
  1597. }
  1598. }
  1599. if (DBG_HDLC && DBG_SPANFILTER) {
  1600. b4_info(b4, "hdlc_rx_frame fifo %d: new_flen=%d end.\n",
  1601. fifo, new_flen);
  1602. }
  1603. return new_flen;
  1604. }
  1605. #endif /* HARDHDLC_RX */
  1606. /*
  1607. * Takes one blob of data from DAHDI and shoots it out to the hardware. The
  1608. * blob may or may not be a complete HDLC frame. If it isn't, the D-channel
  1609. * FIFO interrupt handler will take care of pulling the rest. Returns nonzero
  1610. * if there is still data to send in the current HDLC frame.
  1611. */
  1612. static int hdlc_tx_frame(struct b400m_span *bspan)
  1613. {
  1614. struct b400m *b4 = bspan->parent;
  1615. int res, i, fifo;
  1616. int z1, z2, zlen;
  1617. int f1 = -1, f2 = -1, flen = -1;
  1618. unsigned char buf[B400M_HDLC_BUF_LEN];
  1619. unsigned int size = ARRAY_SIZE(buf);
  1620. char debugbuf[256];
  1621. /* if we're ignoring TE red alarms and we are in alarm, restart the
  1622. * S/T state machine */
  1623. if (bspan->te_mode && (bspan->newalarm != 0)) {
  1624. hfc_start_st(bspan);
  1625. }
  1626. fifo = bspan->fifos[2];
  1627. res = dahdi_hdlc_getbuf(bspan->sigchan, buf, &size);
  1628. if (down_interruptible(&b4->fifosem)) {
  1629. static int arg;
  1630. b4_info(b4, "b400m: arg (%d), grabbed data from DAHDI " \
  1631. "but couldn't grab the lock!\n", ++arg);
  1632. /* TODO: Inform DAHDI that we have grabbed data and can't use
  1633. * it */
  1634. dahdi_hdlc_abort(bspan->sigchan, DAHDI_EVENT_OVERRUN);
  1635. return 1; /* return 1 so we keep trying */
  1636. }
  1637. hfc_setreg_waitbusy(b4, R_FIFO, (fifo << V_FIFO_NUM_SHIFT));
  1638. get_Z(z1, z2, zlen);
  1639. debug_fz(b4, fifo, __func__, debugbuf);
  1640. /* TODO: check zlen, etc. */
  1641. if ((HFC_ZMAX-zlen) < size) {
  1642. static int arg;
  1643. b4_info(b4, "b400m: arg (%d), zlen (%d) < what we " \
  1644. "grabbed from DAHDI (%d)!\n", ++arg, zlen, size);
  1645. size = zlen;
  1646. dahdi_hdlc_abort(bspan->sigchan, DAHDI_EVENT_OVERRUN);
  1647. }
  1648. if (size > 0) {
  1649. bspan->sigactive = 1;
  1650. for (i = 0; i < size; i++)
  1651. b400m_setreg(b4, A_FIFO_DATA, buf[i]);
  1652. /*
  1653. * If we got a full frame from DAHDI, increment F and
  1654. * decrement our HDLC pending counter. Otherwise, select the
  1655. * FIFO again (to start transmission) and make sure the TX IRQ
  1656. * is enabled so we will get called again to finish off the
  1657. * data
  1658. */
  1659. if (res != 0) {
  1660. ++bspan->frames_out;
  1661. bspan->sigactive = 0;
  1662. hfc_setreg_waitbusy(b4, A_INC_RES_FIFO, V_INC_F);
  1663. atomic_dec(&bspan->hdlc_pending);
  1664. } else {
  1665. hfc_setreg_waitbusy(b4, R_FIFO,
  1666. (fifo << V_FIFO_NUM_SHIFT));
  1667. }
  1668. }
  1669. up(&b4->fifosem);
  1670. if (0 && DBG_HDLC && DBG_SPANFILTER) {
  1671. b4_info(b4, "%s", debugbuf);
  1672. b4_info(b4, "hdlc_tx_frame(span %d): DAHDI gave %d " \
  1673. "bytes for FIFO %d (res = %d)\n",
  1674. bspan->port + 1, size, fifo, res);
  1675. for (i = 0; i < size; i++)
  1676. b4_info(b4,
  1677. "%02x%c\n", buf[i],
  1678. (i < (size - 1)) ? ' ' : '\n');
  1679. if (size && res != 0) {
  1680. pr_info("Transmitted frame %d on span %d\n",
  1681. bspan->frames_out - 1, bspan->port);
  1682. }
  1683. }
  1684. return (res == 0);
  1685. }
  1686. /*
  1687. * b400m lowlevel functions These are functions which impact more than just
  1688. * the HFC controller. (those are named hfc_xxx())
  1689. */
  1690. /*
  1691. * Performs a total reset of the card, reinitializes GPIO. The card is
  1692. * initialized enough to have LEDs running, and that's about it. Anything to
  1693. * do with audio and enabling any kind of processing is done in stage2.
  1694. */
  1695. static void xhfc_init_stage1(struct b400m *b4)
  1696. {
  1697. int i;
  1698. hfc_reset(b4);
  1699. hfc_gpio_init(b4);
  1700. /* make sure interrupts are disabled */
  1701. b400m_setreg(b4, R_IRQ_CTRL, 0x00);
  1702. /* make sure write hits hardware */
  1703. flush_hw();
  1704. /* disable all FIFO interrupts */
  1705. for (i = 0; i < HFC_NR_FIFOS; i++) {
  1706. hfc_setreg_waitbusy(b4, R_FIFO, (i << V_FIFO_NUM_SHIFT));
  1707. /* disable the interrupt */
  1708. b400m_setreg(b4, A_FIFO_CTRL, 0x00);
  1709. hfc_setreg_waitbusy(b4, R_FIFO,
  1710. (i << V_FIFO_NUM_SHIFT) | V_FIFO_DIR);
  1711. /* disable the interrupt */
  1712. b400m_setreg(b4, A_FIFO_CTRL, 0x00);
  1713. flush_hw();
  1714. }
  1715. /* set fill threshhold to 16 bytes */
  1716. b400m_setreg(b4, R_FIFO_THRES, 0x11);
  1717. /* clear any pending FIFO interrupts */
  1718. b400m_getreg(b4, R_FIFO_BL2_IRQ);
  1719. b400m_getreg(b4, R_FIFO_BL3_IRQ);
  1720. b4->misc_irq_mask = 0x00;
  1721. b400m_setreg(b4, R_MISC_IRQMSK, b4->misc_irq_mask);
  1722. b400m_setreg(b4, R_IRQ_CTRL, 0);
  1723. }
  1724. /*
  1725. * Stage 2 hardware init. Sets up the flow controller, PCM and FIFOs.
  1726. * Initializes the echo cancellers. S/T interfaces are not initialized here,
  1727. * that is done later, in hfc_init_all_st(). Interrupts are enabled and once
  1728. * the s/t interfaces are configured, chip should be pretty much operational.
  1729. */
  1730. static void xhfc_init_stage2(struct b400m *b4)
  1731. {
  1732. /*
  1733. * set up PCM bus. XHFC is PCM slave C2IO is the clock, auto sync,
  1734. * SYNC_O follows SYNC_I. 128 timeslots, long frame sync positive
  1735. * polarity, sample on falling clock edge. STIO2 is transmit-only,
  1736. * STIO1 is receive-only.
  1737. */
  1738. b400m_setreg(b4, R_PCM_MD0, V_PCM_IDX_MD1);
  1739. b400m_setreg(b4, R_PCM_MD1, V_PCM_DR_8192 | (0x3 << 2));
  1740. b400m_setreg(b4, R_PCM_MD0, V_PCM_IDX_MD2);
  1741. b400m_setreg(b4, R_PCM_MD2, V_C2I_EN | V_SYNC_OUT1);
  1742. b400m_setreg(b4, R_SU_SYNC, V_SYNC_SEL_PORT0);
  1743. /* Now set up the flow controller. */
  1744. hfc_setup_fsm(b4);
  1745. /*
  1746. * At this point, everything's set up and ready to go. Don't actually
  1747. * enable the global interrupt pin. DAHDI still needs to start up the
  1748. * spans, and we don't know exactly when.
  1749. */
  1750. }
  1751. static inline struct b400m_span *bspan_from_dspan(struct dahdi_span *span)
  1752. {
  1753. return container_of(span, struct wctdm_span, span)->bspan;
  1754. }
  1755. static int xhfc_startup(struct dahdi_span *span)
  1756. {
  1757. struct b400m_span *bspan = bspan_from_dspan(span);
  1758. struct b400m *b4 = bspan->parent;
  1759. if (!b4->running)
  1760. hfc_enable_interrupts(bspan->parent);
  1761. return 0;
  1762. }
  1763. /* resets all the FIFOs for a given span. Disables IRQs for the span FIFOs */
  1764. static void xhfc_reset_span(struct b400m_span *bspan)
  1765. {
  1766. int i;
  1767. struct b400m *b4 = bspan->parent;
  1768. /* b4_info(b4, "xhfc_reset_span()\n"); */
  1769. for (i = 0; i < 3; i++)
  1770. hfc_reset_fifo_pair(b4, bspan->fifos[i], (i == 2) ? 1 : 0, 1);
  1771. }
  1772. static void b400m_enable_workqueues(struct wctdm *wc)
  1773. {
  1774. struct b400m *b4s[2];
  1775. int i, numb4s = 0;
  1776. unsigned long flags;
  1777. spin_lock_irqsave(&wc->reglock, flags);
  1778. for (i = 0; i < wc->mods_per_board; i += 4) {
  1779. if (wc->mods[i].type == BRI)
  1780. b4s[numb4s++] = wc->mods[i].mod.bri;
  1781. }
  1782. spin_unlock_irqrestore(&wc->reglock, flags);
  1783. for (i = 0; i < numb4s; i++) {
  1784. if (b4s[i])
  1785. b4s[i]->shutdown = 0;
  1786. }
  1787. }
  1788. static void b400m_disable_workqueues(struct wctdm *wc)
  1789. {
  1790. struct b400m *b4s[2];
  1791. int i, numb4s = 0;
  1792. unsigned long flags;
  1793. spin_lock_irqsave(&wc->reglock, flags);
  1794. for (i = 0; i < wc->mods_per_board; i += 4) {
  1795. if (wc->mods[i].type == BRI)
  1796. b4s[numb4s++] = wc->mods[i].mod.bri;
  1797. }
  1798. spin_unlock_irqrestore(&wc->reglock, flags);
  1799. for (i = 0; i < numb4s; i++) {
  1800. if (b4s[i]) {
  1801. down(&wc->syncsem);
  1802. b4s[i]->shutdown = 1;
  1803. up(&wc->syncsem);
  1804. flush_workqueue(b4s[i]->xhfc_ws);
  1805. }
  1806. }
  1807. }
  1808. /*
  1809. * Software selectable NT and TE mode settings on the B400M.
  1810. *
  1811. * mode - bitwise selection of NT vs TE mode
  1812. * 1 = NT; 0 = TE;
  1813. * bit 0 is port 0
  1814. * bit 1 is port 1
  1815. * ...
  1816. * term - termination resistance
  1817. * 0 = no termination resistance
  1818. * 1 = 390 ohm termination resistance switched on
  1819. */
  1820. static int b400m_set_ntte(struct b400m_span *bspan, int te_mode, int term_on)
  1821. {
  1822. struct b400m *b4 = bspan->parent;
  1823. unsigned char data;
  1824. unsigned char addr;
  1825. int all_modes = 0, all_terms = 0;
  1826. int i;
  1827. bspan->wspan->span.spantype = (te_mode > 0)
  1828. ? SPANTYPE_DIGITAL_BRI_TE
  1829. : SPANTYPE_DIGITAL_BRI_NT;
  1830. bspan->te_mode = te_mode;
  1831. bspan->term_on = term_on;
  1832. for (i = 0; i < 4; i++) {
  1833. if (!b4->spans[i].te_mode)
  1834. all_modes |= (1 << i);
  1835. if (b4->spans[i].term_on)
  1836. all_terms |= (1 << i);
  1837. }
  1838. data = 0x10 | ((all_terms << 4) & 0xc0) | ((all_terms << 2) & 0x0c);
  1839. addr = 0x10 | all_modes;
  1840. msleep(voicebus_current_latency(&b4->wc->vb) + 2);
  1841. wctdm_setreg(b4->wc, get_mod(b4), addr, data);
  1842. b4->lastreg = 0xff;
  1843. msleep(voicebus_current_latency(&b4->wc->vb) + 2);
  1844. hfc_reset_st(bspan);
  1845. if (bri_persistentlayer1)
  1846. hfc_start_st(bspan);
  1847. return 0;
  1848. }
  1849. /* spanconfig for us means ...? */
  1850. int b400m_spanconfig(struct file *file, struct dahdi_span *span,
  1851. struct dahdi_lineconfig *lc)
  1852. {
  1853. struct b400m_span *bspan;
  1854. struct b400m *b4;
  1855. struct wctdm *wc;
  1856. int te_mode, term;
  1857. int pos;
  1858. int res;
  1859. bspan = bspan_from_dspan(span);
  1860. b4 = bspan->parent;
  1861. wc = b4->wc;
  1862. if ((file->f_flags & O_NONBLOCK) && !is_initialized(wc))
  1863. return -EAGAIN;
  1864. res = wctdm_wait_for_ready(wc);
  1865. if (res)
  1866. return res;
  1867. b400m_disable_workqueues(b4->wc);
  1868. te_mode = (lc->lineconfig & DAHDI_CONFIG_NTTE) ? 0 : 1;
  1869. term = (lc->lineconfig & DAHDI_CONFIG_TERM) ? 1 : 0;
  1870. b4_info(b4, "xhfc: Configuring port %d span %d in %s " \
  1871. "mode with termination resistance %s\n", bspan->port,
  1872. span->spanno, (te_mode) ? "TE" : "NT",
  1873. (term) ? "ENABLED" : "DISABLED");
  1874. b400m_set_ntte(bspan, te_mode, term);
  1875. if (lc->sync < 0) {
  1876. b4_info(b4, "Span %d has invalid sync priority (%d), " \
  1877. "removing from sync source list\n", span->spanno,
  1878. lc->sync);
  1879. lc->sync = 0;
  1880. }
  1881. if (span->offset >= 4) {
  1882. pos = span->offset;
  1883. } else {
  1884. /* This is tricky. Have to figure out if we're slot 1 or slot
  1885. * 2 */
  1886. pos = span->offset + b4->position;
  1887. }
  1888. if (!te_mode && lc->sync) {
  1889. b4_info(b4, "NT Spans cannot be timing sources. " \
  1890. "Span %d requested to be timing source of " \
  1891. "priority %d. Changing priority to 0\n", pos,
  1892. lc->sync);
  1893. lc->sync = 0;
  1894. }
  1895. wc->spans[pos]->timing_priority = lc->sync;
  1896. bspan->wspan = container_of(span, struct wctdm_span, span);
  1897. xhfc_reset_span(bspan);
  1898. /* call startup() manually here, because DAHDI won't call the startup
  1899. * function unless it receives an IOCTL to do so, and dahdi_cfg
  1900. * doesn't. */
  1901. xhfc_startup(span);
  1902. span->flags |= DAHDI_FLAG_RUNNING;
  1903. set_bit(WCTDM_CHECK_TIMING, &wc->checkflag);
  1904. b400m_enable_workqueues(b4->wc);
  1905. return 0;
  1906. }
  1907. /* chanconfig for us means to configure the HDLC controller, if appropriate
  1908. *
  1909. * NOTE: apparently the DAHDI ioctl function calls us with a interrupts
  1910. * disabled. This means we cannot actually touch the hardware, because all
  1911. * register accesses are wrapped up in a mutex that can sleep.
  1912. *
  1913. * The solution to that is to simply increment the span's "restart" flag, and
  1914. * the driver's workqueue will do the dirty work on our behalf.
  1915. */
  1916. int b400m_chanconfig(struct file *file, struct dahdi_chan *chan, int sigtype)
  1917. {
  1918. int alreadyrunning;
  1919. struct b400m_span *bspan = bspan_from_dspan(chan->span);
  1920. struct b400m *b4 = bspan->parent;
  1921. int res;
  1922. if ((file->f_flags & O_NONBLOCK) && !is_initialized(b4->wc))
  1923. return -EAGAIN;
  1924. res = wctdm_wait_for_ready(b4->wc);
  1925. if (res)
  1926. return res;
  1927. alreadyrunning = bspan->wspan->span.flags & DAHDI_FLAG_RUNNING;
  1928. if (DBG_FOPS) {
  1929. b4_info(b4, "%s channel %d (%s) sigtype %08x\n",
  1930. alreadyrunning ? "Reconfigured" : "Configured",
  1931. chan->channo, chan->name, sigtype);
  1932. }
  1933. switch (sigtype) {
  1934. case DAHDI_SIG_HARDHDLC:
  1935. if (DBG_FOPS) {
  1936. b4_info(b4, "%sonfiguring hardware HDLC on %s\n",
  1937. ((sigtype == DAHDI_SIG_HARDHDLC) ? "C" :
  1938. "Unc"), chan->name);
  1939. }
  1940. bspan->sigchan = chan;
  1941. bspan->sigactive = 0;
  1942. atomic_set(&bspan->hdlc_pending, 0);
  1943. res = 0;
  1944. break;
  1945. case DAHDI_SIG_HDLCFCS:
  1946. case DAHDI_SIG_HDLCNET:
  1947. case DAHDI_SIG_HDLCRAW:
  1948. /* Only HARDHDLC is supported for the signalling channel on BRI
  1949. * spans. */
  1950. res = -EINVAL;
  1951. break;
  1952. default:
  1953. res = 0;
  1954. break;
  1955. };
  1956. return res;
  1957. }
  1958. int b400m_dchan(struct dahdi_span *span)
  1959. {
  1960. struct b400m_span *bspan;
  1961. struct b400m *b4;
  1962. unsigned char *rxb;
  1963. int res;
  1964. int i;
  1965. bspan = bspan_from_dspan(span);
  1966. b4 = bspan->parent;
  1967. #ifdef HARDHDLC_RX
  1968. return 0;
  1969. #else
  1970. #endif
  1971. if (!bspan->sigchan)
  1972. return 0;
  1973. rxb = bspan->sigchan->readchunk;
  1974. if (!rxb) {
  1975. b4_info(b4, "No RXB!\n");
  1976. return 0;
  1977. }
  1978. for (i = 0; i < DAHDI_CHUNKSIZE; i++) {
  1979. fasthdlc_rx_load_nocheck(&bspan->rxhdlc, *(rxb++));
  1980. res = fasthdlc_rx_run(&bspan->rxhdlc);
  1981. /* If there is nothing there, continue */
  1982. if (res & RETURN_EMPTY_FLAG)
  1983. continue;
  1984. else if (res & RETURN_COMPLETE_FLAG) {
  1985. if (!bspan->f_sz)
  1986. continue;
  1987. /* Only count this if it's a non-empty frame */
  1988. if (bspan->infcs != PPP_GOODFCS) {
  1989. dahdi_hdlc_abort(bspan->sigchan,
  1990. DAHDI_EVENT_BADFCS);
  1991. } else {
  1992. dahdi_hdlc_finish(bspan->sigchan);
  1993. }
  1994. bspan->infcs = PPP_INITFCS;
  1995. bspan->f_sz = 0;
  1996. continue;
  1997. } else if (res & RETURN_DISCARD_FLAG) {
  1998. if (!bspan->f_sz)
  1999. continue;
  2000. dahdi_hdlc_abort(bspan->sigchan, DAHDI_EVENT_ABORT);
  2001. bspan->infcs = PPP_INITFCS;
  2002. bspan->f_sz = 0;
  2003. break;
  2004. } else {
  2005. unsigned char rxc = res;
  2006. bspan->infcs = PPP_FCS(bspan->infcs, rxc);
  2007. bspan->f_sz++;
  2008. dahdi_hdlc_putbuf(bspan->sigchan, &rxc, 1);
  2009. }
  2010. }
  2011. return 0;
  2012. }
  2013. /* internal functions, not specific to the hardware or DAHDI */
  2014. /*
  2015. */
  2016. #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
  2017. static void xhfc_work(void *data)
  2018. {
  2019. struct b400m *b4 = data;
  2020. #else
  2021. static void xhfc_work(struct work_struct *work)
  2022. {
  2023. struct b400m *b4 = container_of(work, struct b400m, xhfc_wq);
  2024. #endif
  2025. int i, j, k, fifo;
  2026. unsigned char b, b2;
  2027. if (b4->shutdown || !is_initialized(b4->wc))
  2028. return;
  2029. b4->irq_oview = b400m_getreg(b4, R_IRQ_OVIEW);
  2030. b4->fifo_fill = b400m_getreg(b4, R_FILL_BL0);
  2031. if (b4->irq_oview & V_FIFO_BL0_IRQ) {
  2032. b4->fifo_irqstatus |= b400m_getreg(b4, R_FIFO_BL0_IRQ);
  2033. b4->irq_oview &= ~V_FIFO_BL0_IRQ;
  2034. }
  2035. /* only look at BL0, we put all D channel FIFOs in the first block. */
  2036. b = b2 = b4->fifo_irqstatus;
  2037. for (j = 0; j < 4; j++) {
  2038. #ifdef SWAP_PORTS
  2039. fifo = (1 == j) ? 2 : (2 == j) ? 1 : j;
  2040. #else
  2041. fifo = j;
  2042. #endif
  2043. #ifdef HARDHDLC_RX
  2044. if (b & V_FIFOx_RX_IRQ) {
  2045. if (fifo < 4) { /* d-channel FIFO */
  2046. /*
  2047. * I have to loop here until hdlc_rx_frame
  2048. * says there are no more frames waiting. for
  2049. * whatever reason, the HFC will not generate
  2050. * another interrupt if there are still HDLC
  2051. * frames waiting to be received. i.e. I get
  2052. * an int when F1 changes, not when F1 != F2.
  2053. *
  2054. */
  2055. do {
  2056. k = hdlc_rx_frame(&b4->spans[fifo]);
  2057. } while (k);
  2058. }
  2059. }
  2060. #endif
  2061. b >>= 2;
  2062. }
  2063. /* zero the bits we just processed */
  2064. b4->fifo_irqstatus &= ~b2;
  2065. b4->fifo_fill &= ~b2;
  2066. #if 1
  2067. /* All four D channel FIFOs are in BL0. */
  2068. b = b2 = b4->fifo_fill;
  2069. for (j = 0; j < 4; j++) {
  2070. #ifdef SWAP_PORTS
  2071. fifo = (1 == j) ? 2 : (2 == j) ? 1 : j;
  2072. #else
  2073. fifo = j;
  2074. #endif
  2075. if (b4->spans[fifo].sigactive && (b & V_FIFOx_TX_IRQ))
  2076. hdlc_tx_frame(&b4->spans[fifo]);
  2077. #ifdef HARDHDLC_RX
  2078. if (b & V_FIFOx_RX_IRQ)
  2079. hdlc_rx_frame(&b4->spans[fifo]);
  2080. #endif
  2081. b >>= 2;
  2082. }
  2083. #endif
  2084. /* Check for outgoing HDLC frame requests The HFC does not generate TX
  2085. * interrupts when there is room to send, so I use an atomic counter
  2086. * that is incremented every time DAHDI wants to send a frame, and
  2087. * decremented every time I send a frame. It'd be better if I could
  2088. * just use the interrupt handler, but the HFC seems to trigger a FIFO
  2089. * TX IRQ only when it has finished sending a frame, not when one can
  2090. * be sent.
  2091. */
  2092. for (i = 0; i < ARRAY_SIZE(b4->spans); i++) {
  2093. struct b400m_span *bspan = &b4->spans[i];
  2094. if (atomic_read(&bspan->hdlc_pending)) {
  2095. do {
  2096. k = hdlc_tx_frame(bspan);
  2097. } while (k);
  2098. }
  2099. }
  2100. b = b400m_getreg(b4, R_SU_IRQ);
  2101. if (b) {
  2102. for (i = 0; i < ARRAY_SIZE(b4->spans); i++) {
  2103. int physport;
  2104. #ifdef SWAP_PORTS
  2105. if (i == 1)
  2106. physport = 2;
  2107. else if (i == 2)
  2108. physport = 1;
  2109. else
  2110. physport = i;
  2111. #else
  2112. physport = i;
  2113. #endif
  2114. if (b & (1 << i))
  2115. hfc_handle_state(&b4->spans[physport]);
  2116. }
  2117. }
  2118. hfc_update_st_timers(b4);
  2119. }
  2120. void wctdm_bri_checkisr(struct wctdm *wc, struct wctdm_module *const mod,
  2121. int offset)
  2122. {
  2123. struct b400m *b4 = mod->mod.bri;
  2124. /* don't do anything for non-base card slots */
  2125. if (mod->card & 0x03)
  2126. return;
  2127. /* DEFINITELY don't do anything if our structures aren't ready! */
  2128. if (!is_initialized(wc) || !b4 || !b4->inited)
  2129. return;
  2130. if (offset == 0) {
  2131. if (!b4->shutdown)
  2132. queue_work(b4->xhfc_ws, &b4->xhfc_wq);
  2133. b4->ticks++;
  2134. }
  2135. return;
  2136. }
  2137. /* DAHDI calls this when it has data it wants to send to the HDLC controller */
  2138. void wctdm_hdlc_hard_xmit(struct dahdi_chan *chan)
  2139. {
  2140. struct b400m *b4;
  2141. struct b400m_span *bspan;
  2142. struct dahdi_span *dspan;
  2143. int span;
  2144. dspan = chan->span;
  2145. bspan = bspan_from_dspan(dspan);
  2146. b4 = bspan->parent;
  2147. span = bspan->port;
  2148. if ((DBG_FOPS || DBG_HDLC) && DBG_SPANFILTER) {
  2149. b4_info(b4, "hdlc_hard_xmit on chan %s (%i/%i), " \
  2150. "span=%i (sigchan=%p, chan=%p)\n", chan->name,
  2151. chan->channo, chan->chanpos, span + 1,
  2152. bspan->sigchan, chan);
  2153. }
  2154. /* Increment the hdlc_pending counter and trigger the bottom-half so
  2155. * it will be picked up and sent. */
  2156. if (bspan->sigchan == chan)
  2157. atomic_inc(&bspan->hdlc_pending);
  2158. }
  2159. static int b400m_probe(struct wctdm *wc, int modpos)
  2160. {
  2161. unsigned char id, x;
  2162. struct b400m *b4;
  2163. unsigned long flags;
  2164. int chiprev;
  2165. wctdm_setreg(wc, &wc->mods[modpos], 0x10, 0x10);
  2166. id = xhfc_getreg(wc, &wc->mods[modpos], R_CHIP_ID, &x);
  2167. /* chip ID high 7 bits must be 0x62, see datasheet */
  2168. if ((id & 0xfe) != 0x62)
  2169. return -2;
  2170. b4 = kzalloc(sizeof(struct b400m), GFP_KERNEL);
  2171. if (!b4) {
  2172. dev_err(&wc->vb.pdev->dev,
  2173. "Couldn't allocate memory for b400m structure!\n");
  2174. return -ENOMEM;
  2175. }
  2176. /* card found, enabled and main struct allocated. Fill it out. */
  2177. b4->wc = wc;
  2178. b4->position = modpos;
  2179. /* which B400M in the system is this one? count all of them found so
  2180. * far */
  2181. for (x = 0; x < modpos; x += 4) {
  2182. if (wc->mods[x].type == BRI)
  2183. ++b4->b400m_no;
  2184. }
  2185. spin_lock_init(&b4->reglock);
  2186. sema_init(&b4->regsem, 1);
  2187. sema_init(&b4->fifosem, 1);
  2188. for (x = 0; x < 4; x++) {
  2189. fasthdlc_init(&b4->spans[x].rxhdlc, FASTHDLC_MODE_16);
  2190. b4->spans[x].infcs = PPP_INITFCS;
  2191. }
  2192. b4->lastreg = 0xff; /* a register we won't hit right off the bat */
  2193. chiprev = b400m_getreg(b4, R_CHIP_RV);
  2194. b4->setsyncspan = -1; /* sync span is unknown */
  2195. b4->reportedsyncspan = -1; /* sync span is unknown */
  2196. if (DBG) {
  2197. b4_info(b4, "Identified controller rev %d in module %d.\n",
  2198. chiprev, b4->position);
  2199. }
  2200. xhfc_init_stage1(b4);
  2201. xhfc_init_stage2(b4);
  2202. hfc_init_all_st(b4);
  2203. hfc_enable_interrupts(b4);
  2204. spin_lock_irqsave(&wc->reglock, flags);
  2205. wc->mods[modpos].mod.bri = (void *)b4;
  2206. spin_unlock_irqrestore(&wc->reglock, flags);
  2207. return 0;
  2208. }
  2209. void b400m_post_init(struct b400m *b4)
  2210. {
  2211. snprintf(b4->name, sizeof(b4->name) - 1, "b400m-%d",
  2212. b4->b400m_no);
  2213. b4->xhfc_ws = create_singlethread_workqueue(b4->name);
  2214. # if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
  2215. INIT_WORK(&b4->xhfc_wq, xhfc_work, b4);
  2216. # else
  2217. INIT_WORK(&b4->xhfc_wq, xhfc_work);
  2218. # endif
  2219. b4->inited = 1;
  2220. }
  2221. /* functions called from the wctdm code */
  2222. int wctdm_init_b400m(struct wctdm *wc, int card)
  2223. {
  2224. int ret = 0;
  2225. unsigned long flags;
  2226. if (wc->mods[card & 0xfc].type == QRV)
  2227. return -2;
  2228. if (!(card & 0x03)) { /* only init if at lowest port in module */
  2229. spin_lock_irqsave(&wc->reglock, flags);
  2230. wc->mods[card + 0].type = BRI;
  2231. wc->mods[card + 0].mod.bri = NULL;
  2232. wc->mods[card + 1].type = BRI;
  2233. wc->mods[card + 1].mod.bri = NULL;
  2234. wc->mods[card + 2].type = BRI;
  2235. wc->mods[card + 2].mod.bri = NULL;
  2236. wc->mods[card + 3].type = BRI;
  2237. wc->mods[card + 3].mod.bri = NULL;
  2238. spin_unlock_irqrestore(&wc->reglock, flags);
  2239. msleep(20);
  2240. if (b400m_probe(wc, card) != 0) {
  2241. spin_lock_irqsave(&wc->reglock, flags);
  2242. wc->mods[card + 0].type = NONE;
  2243. wc->mods[card + 1].type = NONE;
  2244. wc->mods[card + 2].type = NONE;
  2245. wc->mods[card + 3].type = NONE;
  2246. spin_unlock_irqrestore(&wc->reglock, flags);
  2247. ret = -2;
  2248. }
  2249. } else { /* for the "sub-cards" */
  2250. if (wc->mods[card & 0xfc].type == BRI) {
  2251. spin_lock_irqsave(&wc->reglock, flags);
  2252. wc->mods[card].type = BRI;
  2253. wc->mods[card].mod.bri = wc->mods[card & 0xfc].mod.bri;
  2254. spin_unlock_irqrestore(&wc->reglock, flags);
  2255. } else {
  2256. ret = -2;
  2257. }
  2258. }
  2259. return ret;
  2260. }
  2261. void wctdm_unload_b400m(struct wctdm *wc, int card)
  2262. {
  2263. struct b400m *b4 = wc->mods[card].mod.bri;
  2264. int i;
  2265. /* TODO: shutdown once won't work if just a single card is hotswapped
  2266. * out. But since most of the time this is called because the entire
  2267. * driver is in the process of unloading, I'll leave it here. */
  2268. static int shutdown_once;
  2269. /* only really unload with the 'base' card number. base+1/2/3 aren't
  2270. * real. */
  2271. if (card & 0x03)
  2272. return;
  2273. if (timingcable && !shutdown_once) {
  2274. b4_info(b4, "Disabling all workqueues for B400Ms\n");
  2275. /* Gotta shut down timing change potential during this */
  2276. for (i = 0; i < WC_MAX_IFACES; i++) {
  2277. if (ifaces[i])
  2278. b400m_disable_workqueues(ifaces[i]);
  2279. }
  2280. b4_info(b4, "Forcing sync to card 0\n");
  2281. /* Put the timing configuration in a known state: card 0 is
  2282. * master */
  2283. wctdm_change_system_sync_src(synccard, syncspan, -1, -1);
  2284. /* Change all other cards in the system to self time before
  2285. * card 0 is removed */
  2286. b4_info(b4, "Setting all cards to return to self sync\n");
  2287. for (i = 1; i < WC_MAX_IFACES; i++) {
  2288. if (ifaces[i])
  2289. wctdm_change_card_sync_src(ifaces[i], 0, 0);
  2290. }
  2291. b4_info(b4,
  2292. "Finished preparing timing linked cards for "
  2293. "shutdown\n");
  2294. shutdown_once = 1;
  2295. }
  2296. if (b4) {
  2297. b4->inited = 0;
  2298. msleep(100);
  2299. /* TODO: wait for tdm24xx driver to unregister the spans */
  2300. /* do { ... } while(not_unregistered); */
  2301. /* Change sync source back to base board so we don't freeze up
  2302. * when we reset the XHFC */
  2303. b400m_disable_workqueues(wc);
  2304. for (i = 0; i < (MAX_SPANS - 1); i++) {
  2305. if (wc->spans[i])
  2306. wc->spans[i]->timing_priority = 0;
  2307. }
  2308. for (i = 0; i < ARRAY_SIZE(b4->spans); i++)
  2309. b4->spans[i].wspan->span.flags &= ~DAHDI_FLAG_RUNNING;
  2310. wctdm_change_card_sync_src(b4->wc, 0, 0);
  2311. xhfc_init_stage1(b4);
  2312. destroy_workqueue(b4->xhfc_ws);
  2313. /* Set these to NONE to ensure that our checkisr
  2314. * routines are not entered */
  2315. wc->mods[card].type = NONE;
  2316. wc->mods[card + 1].type = NONE;
  2317. wc->mods[card + 2].type = NONE;
  2318. wc->mods[card + 3].type = NONE;
  2319. wc->mods[card].mod.bri = NULL;
  2320. wc->mods[card + 1].mod.bri = NULL;
  2321. wc->mods[card + 2].mod.bri = NULL;
  2322. wc->mods[card + 3].mod.bri = NULL;
  2323. msleep(voicebus_current_latency(&wc->vb) << 1);
  2324. b4_info(b4, "Driver unloaded.\n");
  2325. kfree(b4);
  2326. }
  2327. }
  2328. void b400m_module_init(void)
  2329. {
  2330. fasthdlc_precalc();
  2331. }
  2332. void b400m_module_cleanup(void)
  2333. {
  2334. }