tspp.c 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396
  1. /* Copyright (c) 2011-2014, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/module.h> /* Just for modules */
  13. #include <linux/kernel.h> /* Only for KERN_INFO */
  14. #include <linux/err.h> /* Error macros */
  15. #include <linux/list.h> /* Linked list */
  16. #include <linux/cdev.h>
  17. #include <linux/init.h> /* Needed for the macros */
  18. #include <linux/io.h> /* IO macros */
  19. #include <linux/device.h> /* Device drivers need this */
  20. #include <linux/sched.h> /* Externally defined globals */
  21. #include <linux/pm_runtime.h> /* Runtime power management */
  22. #include <linux/fs.h>
  23. #include <linux/uaccess.h> /* copy_to_user */
  24. #include <linux/slab.h> /* kfree, kzalloc */
  25. #include <linux/ioport.h> /* XXX_ mem_region */
  26. #include <linux/dma-mapping.h> /* dma_XXX */
  27. #include <linux/dmapool.h> /* DMA pools */
  28. #include <linux/delay.h> /* msleep */
  29. #include <linux/platform_device.h>
  30. #include <linux/clk.h>
  31. #include <linux/poll.h> /* poll() file op */
  32. #include <linux/wait.h> /* wait() macros, sleeping */
  33. #include <linux/tspp.h> /* tspp functions */
  34. #include <linux/bitops.h> /* BIT() macro */
  35. #include <linux/regulator/consumer.h>
  36. #include <mach/sps.h> /* BAM stuff */
  37. #include <mach/gpio.h>
  38. #include <linux/wakelock.h> /* Locking functions */
  39. #include <linux/timer.h> /* Timer services */
  40. #include <linux/jiffies.h> /* Jiffies counter */
  41. #include <mach/dma.h>
  42. #include <mach/msm_tspp.h>
  43. #include <mach/rpm-regulator-smd.h>
  44. #include <linux/debugfs.h>
  45. #include <linux/of.h>
  46. #include <linux/of_gpio.h>
  47. #include <linux/string.h>
  48. #include <mach/msm_bus.h>
  49. /*
  50. * General defines
  51. */
  52. #define TSPP_TSIF_INSTANCES 2
  53. #define TSPP_GPIOS_PER_TSIF 4
  54. #define TSPP_FILTER_TABLES 3
  55. #define TSPP_MAX_DEVICES 1
  56. #define TSPP_NUM_CHANNELS 16
  57. #define TSPP_NUM_PRIORITIES 16
  58. #define TSPP_NUM_KEYS 8
  59. #define INVALID_CHANNEL 0xFFFFFFFF
  60. /*
  61. * BAM descriptor FIFO size (in number of descriptors).
  62. * Max number of descriptors allowed by SPS which is 8K-1.
  63. */
  64. #define TSPP_SPS_DESCRIPTOR_COUNT (8 * 1024 - 1)
  65. #define TSPP_PACKET_LENGTH 188
  66. #define TSPP_MIN_BUFFER_SIZE (TSPP_PACKET_LENGTH)
  67. /* Max descriptor buffer size allowed by SPS */
  68. #define TSPP_MAX_BUFFER_SIZE (32 * 1024 - 1)
  69. /*
  70. * Returns whether to use DMA pool for TSPP output buffers.
  71. * For buffers smaller than page size, using DMA pool
  72. * provides better memory utilization as dma_alloc_coherent
  73. * allocates minimum of page size.
  74. */
  75. #define TSPP_USE_DMA_POOL(buff_size) ((buff_size) < PAGE_SIZE)
  76. /*
  77. * Max allowed TSPP buffers/descriptors.
  78. * If SPS desc FIFO holds X descriptors, we can queue up to X-1 descriptors.
  79. */
  80. #define TSPP_NUM_BUFFERS (TSPP_SPS_DESCRIPTOR_COUNT - 1)
  81. #define TSPP_TSIF_DEFAULT_TIME_LIMIT 60
  82. #define SPS_DESCRIPTOR_SIZE 8
  83. #define MIN_ACCEPTABLE_BUFFER_COUNT 2
  84. #define TSPP_DEBUG(msg...)
  85. /*
  86. * TSIF register offsets
  87. */
  88. #define TSIF_STS_CTL_OFF (0x0)
  89. #define TSIF_TIME_LIMIT_OFF (0x4)
  90. #define TSIF_CLK_REF_OFF (0x8)
  91. #define TSIF_LPBK_FLAGS_OFF (0xc)
  92. #define TSIF_LPBK_DATA_OFF (0x10)
  93. #define TSIF_TEST_CTL_OFF (0x14)
  94. #define TSIF_TEST_MODE_OFF (0x18)
  95. #define TSIF_TEST_RESET_OFF (0x1c)
  96. #define TSIF_TEST_EXPORT_OFF (0x20)
  97. #define TSIF_TEST_CURRENT_OFF (0x24)
  98. #define TSIF_DATA_PORT_OFF (0x100)
  99. /* bits for TSIF_STS_CTL register */
  100. #define TSIF_STS_CTL_EN_IRQ BIT(28)
  101. #define TSIF_STS_CTL_PACK_AVAIL BIT(27)
  102. #define TSIF_STS_CTL_1ST_PACKET BIT(26)
  103. #define TSIF_STS_CTL_OVERFLOW BIT(25)
  104. #define TSIF_STS_CTL_LOST_SYNC BIT(24)
  105. #define TSIF_STS_CTL_TIMEOUT BIT(23)
  106. #define TSIF_STS_CTL_INV_SYNC BIT(21)
  107. #define TSIF_STS_CTL_INV_NULL BIT(20)
  108. #define TSIF_STS_CTL_INV_ERROR BIT(19)
  109. #define TSIF_STS_CTL_INV_ENABLE BIT(18)
  110. #define TSIF_STS_CTL_INV_DATA BIT(17)
  111. #define TSIF_STS_CTL_INV_CLOCK BIT(16)
  112. #define TSIF_STS_CTL_SPARE BIT(15)
  113. #define TSIF_STS_CTL_EN_NULL BIT(11)
  114. #define TSIF_STS_CTL_EN_ERROR BIT(10)
  115. #define TSIF_STS_CTL_LAST_BIT BIT(9)
  116. #define TSIF_STS_CTL_EN_TIME_LIM BIT(8)
  117. #define TSIF_STS_CTL_EN_TCR BIT(7)
  118. #define TSIF_STS_CTL_TEST_MODE BIT(6)
  119. #define TSIF_STS_CTL_MODE_2 BIT(5)
  120. #define TSIF_STS_CTL_EN_DM BIT(4)
  121. #define TSIF_STS_CTL_STOP BIT(3)
  122. #define TSIF_STS_CTL_START BIT(0)
  123. /*
  124. * TSPP register offsets
  125. */
  126. #define TSPP_RST 0x00
  127. #define TSPP_CLK_CONTROL 0x04
  128. #define TSPP_CONFIG 0x08
  129. #define TSPP_CONTROL 0x0C
  130. #define TSPP_PS_DISABLE 0x10
  131. #define TSPP_MSG_IRQ_STATUS 0x14
  132. #define TSPP_MSG_IRQ_MASK 0x18
  133. #define TSPP_IRQ_STATUS 0x1C
  134. #define TSPP_IRQ_MASK 0x20
  135. #define TSPP_IRQ_CLEAR 0x24
  136. #define TSPP_PIPE_ERROR_STATUS(_n) (0x28 + (_n << 2))
  137. #define TSPP_STATUS 0x68
  138. #define TSPP_CURR_TSP_HEADER 0x6C
  139. #define TSPP_CURR_PID_FILTER 0x70
  140. #define TSPP_SYSTEM_KEY(_n) (0x74 + (_n << 2))
  141. #define TSPP_CBC_INIT_VAL(_n) (0x94 + (_n << 2))
  142. #define TSPP_DATA_KEY_RESET 0x9C
  143. #define TSPP_KEY_VALID 0xA0
  144. #define TSPP_KEY_ERROR 0xA4
  145. #define TSPP_TEST_CTRL 0xA8
  146. #define TSPP_VERSION 0xAC
  147. #define TSPP_GENERICS 0xB0
  148. #define TSPP_NOP 0xB4
  149. /*
  150. * Register bit definitions
  151. */
  152. /* TSPP_RST */
  153. #define TSPP_RST_RESET BIT(0)
  154. /* TSPP_CLK_CONTROL */
  155. #define TSPP_CLK_CONTROL_FORCE_CRYPTO BIT(9)
  156. #define TSPP_CLK_CONTROL_FORCE_PES_PL BIT(8)
  157. #define TSPP_CLK_CONTROL_FORCE_PES_AF BIT(7)
  158. #define TSPP_CLK_CONTROL_FORCE_RAW_CTRL BIT(6)
  159. #define TSPP_CLK_CONTROL_FORCE_PERF_CNT BIT(5)
  160. #define TSPP_CLK_CONTROL_FORCE_CTX_SEARCH BIT(4)
  161. #define TSPP_CLK_CONTROL_FORCE_TSP_PROC BIT(3)
  162. #define TSPP_CLK_CONTROL_FORCE_CONS_AHB2MEM BIT(2)
  163. #define TSPP_CLK_CONTROL_FORCE_TS_AHB2MEM BIT(1)
  164. #define TSPP_CLK_CONTROL_SET_CLKON BIT(0)
  165. /* TSPP_CONFIG */
  166. #define TSPP_CONFIG_SET_PACKET_LENGTH(_a, _b) (_a = (_a & 0xF0) | \
  167. ((_b & 0xF) << 8))
  168. #define TSPP_CONFIG_GET_PACKET_LENGTH(_a) ((_a >> 8) & 0xF)
  169. #define TSPP_CONFIG_DUP_WITH_DISC_EN BIT(7)
  170. #define TSPP_CONFIG_PES_SYNC_ERROR_MASK BIT(6)
  171. #define TSPP_CONFIG_PS_LEN_ERR_MASK BIT(5)
  172. #define TSPP_CONFIG_PS_CONT_ERR_UNSP_MASK BIT(4)
  173. #define TSPP_CONFIG_PS_CONT_ERR_MASK BIT(3)
  174. #define TSPP_CONFIG_PS_DUP_TSP_MASK BIT(2)
  175. #define TSPP_CONFIG_TSP_ERR_IND_MASK BIT(1)
  176. #define TSPP_CONFIG_TSP_SYNC_ERR_MASK BIT(0)
  177. /* TSPP_CONTROL */
  178. #define TSPP_CONTROL_PID_FILTER_LOCK BIT(5)
  179. #define TSPP_CONTROL_FORCE_KEY_CALC BIT(4)
  180. #define TSPP_CONTROL_TSP_CONS_SRC_DIS BIT(3)
  181. #define TSPP_CONTROL_TSP_TSIF1_SRC_DIS BIT(2)
  182. #define TSPP_CONTROL_TSP_TSIF0_SRC_DIS BIT(1)
  183. #define TSPP_CONTROL_PERF_COUNT_INIT BIT(0)
  184. /* TSPP_MSG_IRQ_STATUS + TSPP_MSG_IRQ_MASK */
  185. #define TSPP_MSG_TSPP_IRQ BIT(2)
  186. #define TSPP_MSG_TSIF_1_IRQ BIT(1)
  187. #define TSPP_MSG_TSIF_0_IRQ BIT(0)
  188. /* TSPP_IRQ_STATUS + TSPP_IRQ_MASK + TSPP_IRQ_CLEAR */
  189. #define TSPP_IRQ_STATUS_TSP_RD_CMPL BIT(19)
  190. #define TSPP_IRQ_STATUS_KEY_ERROR BIT(18)
  191. #define TSPP_IRQ_STATUS_KEY_SWITCHED_BAD BIT(17)
  192. #define TSPP_IRQ_STATUS_KEY_SWITCHED BIT(16)
  193. #define TSPP_IRQ_STATUS_PS_BROKEN(_n) BIT((_n))
  194. /* TSPP_PIPE_ERROR_STATUS */
  195. #define TSPP_PIPE_PES_SYNC_ERROR BIT(3)
  196. #define TSPP_PIPE_PS_LENGTH_ERROR BIT(2)
  197. #define TSPP_PIPE_PS_CONTINUITY_ERROR BIT(1)
  198. #define TSPP_PIP_PS_LOST_START BIT(0)
  199. /* TSPP_STATUS */
  200. #define TSPP_STATUS_TSP_PKT_AVAIL BIT(10)
  201. #define TSPP_STATUS_TSIF1_DM_REQ BIT(6)
  202. #define TSPP_STATUS_TSIF0_DM_REQ BIT(2)
  203. #define TSPP_CURR_FILTER_TABLE BIT(0)
  204. /* TSPP_GENERICS */
  205. #define TSPP_GENERICS_CRYPTO_GEN BIT(12)
  206. #define TSPP_GENERICS_MAX_CONS_PIPES BIT(7)
  207. #define TSPP_GENERICS_MAX_PIPES BIT(2)
  208. #define TSPP_GENERICS_TSIF_1_GEN BIT(1)
  209. #define TSPP_GENERICS_TSIF_0_GEN BIT(0)
  210. /*
  211. * TSPP memory regions
  212. */
  213. #define TSPP_PID_FILTER_TABLE0 0x800
  214. #define TSPP_PID_FILTER_TABLE1 0x880
  215. #define TSPP_PID_FILTER_TABLE2 0x900
  216. #define TSPP_GLOBAL_PERFORMANCE 0x980 /* see tspp_global_performance */
  217. #define TSPP_PIPE_CONTEXT 0x990 /* see tspp_pipe_context */
  218. #define TSPP_PIPE_PERFORMANCE 0x998 /* see tspp_pipe_performance */
  219. #define TSPP_TSP_BUFF_WORD(_n) (0xC10 + (_n << 2))
  220. #define TSPP_DATA_KEY 0xCD0
  221. struct debugfs_entry {
  222. const char *name;
  223. mode_t mode;
  224. int offset;
  225. };
  226. static const struct debugfs_entry debugfs_tsif_regs[] = {
  227. {"sts_ctl", S_IRUGO | S_IWUSR, TSIF_STS_CTL_OFF},
  228. {"time_limit", S_IRUGO | S_IWUSR, TSIF_TIME_LIMIT_OFF},
  229. {"clk_ref", S_IRUGO | S_IWUSR, TSIF_CLK_REF_OFF},
  230. {"lpbk_flags", S_IRUGO | S_IWUSR, TSIF_LPBK_FLAGS_OFF},
  231. {"lpbk_data", S_IRUGO | S_IWUSR, TSIF_LPBK_DATA_OFF},
  232. {"test_ctl", S_IRUGO | S_IWUSR, TSIF_TEST_CTL_OFF},
  233. {"test_mode", S_IRUGO | S_IWUSR, TSIF_TEST_MODE_OFF},
  234. {"test_reset", S_IWUSR, TSIF_TEST_RESET_OFF},
  235. {"test_export", S_IRUGO | S_IWUSR, TSIF_TEST_EXPORT_OFF},
  236. {"test_current", S_IRUGO, TSIF_TEST_CURRENT_OFF},
  237. {"data_port", S_IRUSR, TSIF_DATA_PORT_OFF},
  238. };
  239. static const struct debugfs_entry debugfs_tspp_regs[] = {
  240. {"rst", S_IRUGO | S_IWUSR, TSPP_RST},
  241. {"clk_control", S_IRUGO | S_IWUSR, TSPP_CLK_CONTROL},
  242. {"config", S_IRUGO | S_IWUSR, TSPP_CONFIG},
  243. {"control", S_IRUGO | S_IWUSR, TSPP_CONTROL},
  244. {"ps_disable", S_IRUGO | S_IWUSR, TSPP_PS_DISABLE},
  245. {"msg_irq_status", S_IRUGO | S_IWUSR, TSPP_MSG_IRQ_STATUS},
  246. {"msg_irq_mask", S_IRUGO | S_IWUSR, TSPP_MSG_IRQ_MASK},
  247. {"irq_status", S_IRUGO | S_IWUSR, TSPP_IRQ_STATUS},
  248. {"irq_mask", S_IRUGO | S_IWUSR, TSPP_IRQ_MASK},
  249. {"irq_clear", S_IRUGO | S_IWUSR, TSPP_IRQ_CLEAR},
  250. /* {"pipe_error_status",S_IRUGO | S_IWUSR, TSPP_PIPE_ERROR_STATUS}, */
  251. {"status", S_IRUGO | S_IWUSR, TSPP_STATUS},
  252. {"curr_tsp_header", S_IRUGO | S_IWUSR, TSPP_CURR_TSP_HEADER},
  253. {"curr_pid_filter", S_IRUGO | S_IWUSR, TSPP_CURR_PID_FILTER},
  254. /* {"system_key", S_IRUGO | S_IWUSR, TSPP_SYSTEM_KEY}, */
  255. /* {"cbc_init_val", S_IRUGO | S_IWUSR, TSPP_CBC_INIT_VAL}, */
  256. {"data_key_reset", S_IRUGO | S_IWUSR, TSPP_DATA_KEY_RESET},
  257. {"key_valid", S_IRUGO | S_IWUSR, TSPP_KEY_VALID},
  258. {"key_error", S_IRUGO | S_IWUSR, TSPP_KEY_ERROR},
  259. {"test_ctrl", S_IRUGO | S_IWUSR, TSPP_TEST_CTRL},
  260. {"version", S_IRUGO | S_IWUSR, TSPP_VERSION},
  261. {"generics", S_IRUGO | S_IWUSR, TSPP_GENERICS},
  262. {"pid_filter_table0", S_IRUGO | S_IWUSR, TSPP_PID_FILTER_TABLE0},
  263. {"pid_filter_table1", S_IRUGO | S_IWUSR, TSPP_PID_FILTER_TABLE1},
  264. {"pid_filter_table2", S_IRUGO | S_IWUSR, TSPP_PID_FILTER_TABLE2},
  265. {"global_performance", S_IRUGO | S_IWUSR, TSPP_GLOBAL_PERFORMANCE},
  266. {"pipe_context", S_IRUGO | S_IWUSR, TSPP_PIPE_CONTEXT},
  267. {"pipe_performance", S_IRUGO | S_IWUSR, TSPP_PIPE_PERFORMANCE},
  268. {"data_key", S_IRUGO | S_IWUSR, TSPP_DATA_KEY}
  269. };
  270. struct tspp_pid_filter {
  271. u32 filter; /* see FILTER_ macros */
  272. u32 config; /* see FILTER_ macros */
  273. };
  274. /* tsp_info */
  275. #define FILTER_HEADER_ERROR_MASK BIT(7)
  276. #define FILTER_TRANS_END_DISABLE BIT(6)
  277. #define FILTER_DEC_ON_ERROR_EN BIT(5)
  278. #define FILTER_DECRYPT BIT(4)
  279. #define FILTER_HAS_ENCRYPTION(_p) (_p->config & FILTER_DECRYPT)
  280. #define FILTER_GET_PIPE_NUMBER0(_p) (_p->config & 0xF)
  281. #define FILTER_SET_PIPE_NUMBER0(_p, _b) (_p->config = \
  282. (_p->config & ~0xF) | (_b & 0xF))
  283. #define FILTER_GET_PIPE_PROCESS0(_p) ((_p->filter >> 30) & 0x3)
  284. #define FILTER_SET_PIPE_PROCESS0(_p, _b) (_p->filter = \
  285. (_p->filter & ~(0x3<<30)) | ((_b & 0x3) << 30))
  286. #define FILTER_GET_PIPE_PID(_p) ((_p->filter >> 13) & 0x1FFF)
  287. #define FILTER_SET_PIPE_PID(_p, _b) (_p->filter = \
  288. (_p->filter & ~(0x1FFF<<13)) | ((_b & 0x1FFF) << 13))
  289. #define FILTER_GET_PID_MASK(_p) (_p->filter & 0x1FFF)
  290. #define FILTER_SET_PID_MASK(_p, _b) (_p->filter = \
  291. (_p->filter & ~0x1FFF) | (_b & 0x1FFF))
  292. #define FILTER_GET_PIPE_PROCESS1(_p) ((_p->config >> 30) & 0x3)
  293. #define FILTER_SET_PIPE_PROCESS1(_p, _b) (_p->config = \
  294. (_p->config & ~(0x3<<30)) | ((_b & 0x3) << 30))
  295. #define FILTER_GET_KEY_NUMBER(_p) ((_p->config >> 8) & 0x7)
  296. #define FILTER_SET_KEY_NUMBER(_p, _b) (_p->config = \
  297. (_p->config & ~(0x7<<8)) | ((_b & 0x7) << 8))
  298. struct tspp_global_performance_regs {
  299. u32 tsp_total;
  300. u32 tsp_ignored;
  301. u32 tsp_error;
  302. u32 tsp_sync;
  303. };
  304. struct tspp_pipe_context_regs {
  305. u16 pes_bytes_left;
  306. u16 count;
  307. u32 tsif_suffix;
  308. } __packed;
  309. #define CONTEXT_GET_STATE(_a) (_a & 0x3)
  310. #define CONTEXT_UNSPEC_LENGTH BIT(11)
  311. #define CONTEXT_GET_CONT_COUNT(_a) ((_a >> 12) & 0xF)
  312. #define MSEC_TO_JIFFIES(msec) ((msec) * HZ / 1000)
  313. struct tspp_pipe_performance_regs {
  314. u32 tsp_total;
  315. u32 ps_duplicate_tsp;
  316. u32 tsp_no_payload;
  317. u32 tsp_broken_ps;
  318. u32 ps_total_num;
  319. u32 ps_continuity_error;
  320. u32 ps_length_error;
  321. u32 pes_sync_error;
  322. };
  323. struct tspp_tsif_device {
  324. void __iomem *base;
  325. u32 time_limit;
  326. u32 ref_count;
  327. enum tspp_tsif_mode mode;
  328. int clock_inverse;
  329. int data_inverse;
  330. int sync_inverse;
  331. int enable_inverse;
  332. u32 tsif_irq;
  333. /* debugfs */
  334. struct dentry *dent_tsif;
  335. struct dentry *debugfs_tsif_regs[ARRAY_SIZE(debugfs_tsif_regs)];
  336. u32 stat_rx;
  337. u32 stat_overflow;
  338. u32 stat_lost_sync;
  339. u32 stat_timeout;
  340. };
  341. enum tspp_buf_state {
  342. TSPP_BUF_STATE_EMPTY, /* buffer has been allocated, but not waiting */
  343. TSPP_BUF_STATE_WAITING, /* buffer is waiting to be filled */
  344. TSPP_BUF_STATE_DATA, /* buffer is not empty and can be read */
  345. TSPP_BUF_STATE_LOCKED /* buffer is being read by a client */
  346. };
  347. struct tspp_mem_buffer {
  348. struct tspp_mem_buffer *next;
  349. struct sps_mem_buffer sps;
  350. struct tspp_data_descriptor desc; /* buffer descriptor for kernel api */
  351. enum tspp_buf_state state;
  352. size_t filled; /* how much data this buffer is holding */
  353. int read_index; /* where to start reading data from */
  354. };
  355. /* this represents each char device 'channel' */
  356. struct tspp_channel {
  357. struct cdev cdev;
  358. struct device *dd;
  359. struct tspp_device *pdev; /* can use container_of instead? */
  360. struct sps_pipe *pipe;
  361. struct sps_connect config;
  362. struct sps_register_event event;
  363. struct tspp_mem_buffer *data; /* list of buffers */
  364. struct tspp_mem_buffer *read; /* first buffer ready to be read */
  365. struct tspp_mem_buffer *waiting; /* first outstanding transfer */
  366. struct tspp_mem_buffer *locked; /* buffer currently being read */
  367. wait_queue_head_t in_queue; /* set when data is received */
  368. u32 id; /* channel id (0-15) */
  369. int used; /* is this channel in use? */
  370. int key; /* which encryption key index is used */
  371. u32 buffer_size; /* size of the sps transfer buffers */
  372. u32 max_buffers; /* how many buffers should be allocated */
  373. u32 buffer_count; /* how many buffers are actually allocated */
  374. u32 filter_count; /* how many filters have been added to this channel */
  375. u32 int_freq; /* generate interrupts every x descriptors */
  376. enum tspp_source src;
  377. enum tspp_mode mode;
  378. tspp_notifier *notifier; /* used only with kernel api */
  379. void *notify_data; /* data to be passed with the notifier */
  380. u32 expiration_period_ms; /* notification on partially filled buffers */
  381. struct timer_list expiration_timer;
  382. struct dma_pool *dma_pool;
  383. tspp_memfree *memfree; /* user defined memory free function */
  384. void *user_info; /* user cookie passed to memory alloc/free function */
  385. };
  386. struct tspp_pid_filter_table {
  387. struct tspp_pid_filter filter[TSPP_NUM_PRIORITIES];
  388. };
  389. struct tspp_key_entry {
  390. u32 even_lsb;
  391. u32 even_msb;
  392. u32 odd_lsb;
  393. u32 odd_msb;
  394. };
  395. struct tspp_key_table {
  396. struct tspp_key_entry entry[TSPP_NUM_KEYS];
  397. };
  398. /* this represents the actual hardware device */
  399. struct tspp_device {
  400. struct list_head devlist; /* list of all devices */
  401. struct platform_device *pdev;
  402. void __iomem *base;
  403. uint32_t tsif_bus_client;
  404. unsigned int tspp_irq;
  405. unsigned int bam_irq;
  406. u32 bam_handle;
  407. struct sps_bam_props bam_props;
  408. struct wake_lock wake_lock;
  409. spinlock_t spinlock;
  410. struct tasklet_struct tlet;
  411. struct tspp_tsif_device tsif[TSPP_TSIF_INSTANCES];
  412. /* clocks */
  413. struct clk *tsif_pclk;
  414. struct clk *tsif_ref_clk;
  415. /* regulators */
  416. struct regulator *tsif_vreg;
  417. /* data */
  418. struct tspp_pid_filter_table *filters[TSPP_FILTER_TABLES];
  419. struct tspp_channel channels[TSPP_NUM_CHANNELS];
  420. struct tspp_key_table *tspp_key_table;
  421. struct tspp_global_performance_regs *tspp_global_performance;
  422. struct tspp_pipe_context_regs *tspp_pipe_context;
  423. struct tspp_pipe_performance_regs *tspp_pipe_performance;
  424. bool req_irqs;
  425. struct dentry *dent;
  426. struct dentry *debugfs_regs[ARRAY_SIZE(debugfs_tspp_regs)];
  427. };
  428. static struct class *tspp_class;
  429. static int tspp_key_entry;
  430. static dev_t tspp_minor; /* next minor number to assign */
  431. static LIST_HEAD(tspp_devices);
  432. /* forward declarations */
  433. static ssize_t tspp_read(struct file *, char __user *, size_t, loff_t *);
  434. static ssize_t tspp_open(struct inode *inode, struct file *filp);
  435. static unsigned int tspp_poll(struct file *filp, struct poll_table_struct *p);
  436. static ssize_t tspp_release(struct inode *inode, struct file *filp);
  437. static long tspp_ioctl(struct file *, unsigned int, unsigned long);
  438. /* file operations */
  439. static const struct file_operations tspp_fops = {
  440. .owner = THIS_MODULE,
  441. .read = tspp_read,
  442. .open = tspp_open,
  443. .poll = tspp_poll,
  444. .release = tspp_release,
  445. .unlocked_ioctl = tspp_ioctl,
  446. };
  447. /*** IRQ ***/
  448. static irqreturn_t tspp_isr(int irq, void *dev)
  449. {
  450. struct tspp_device *device = dev;
  451. u32 status, mask;
  452. u32 data;
  453. status = readl_relaxed(device->base + TSPP_IRQ_STATUS);
  454. mask = readl_relaxed(device->base + TSPP_IRQ_MASK);
  455. status &= mask;
  456. if (!status) {
  457. dev_warn(&device->pdev->dev, "Spurious interrupt");
  458. return IRQ_NONE;
  459. }
  460. /* if (status & TSPP_IRQ_STATUS_TSP_RD_CMPL) */
  461. if (status & TSPP_IRQ_STATUS_KEY_ERROR) {
  462. /* read the key error info */
  463. data = readl_relaxed(device->base + TSPP_KEY_ERROR);
  464. dev_info(&device->pdev->dev, "key error 0x%x", data);
  465. }
  466. if (status & TSPP_IRQ_STATUS_KEY_SWITCHED_BAD) {
  467. data = readl_relaxed(device->base + TSPP_KEY_VALID);
  468. dev_info(&device->pdev->dev, "key invalidated: 0x%x", data);
  469. }
  470. if (status & TSPP_IRQ_STATUS_KEY_SWITCHED)
  471. dev_info(&device->pdev->dev, "key switched");
  472. if (status & 0xffff)
  473. dev_info(&device->pdev->dev, "broken pipe %i", status & 0xffff);
  474. writel_relaxed(status, device->base + TSPP_IRQ_CLEAR);
  475. /*
  476. * Before returning IRQ_HANDLED to the generic interrupt handling
  477. * framework need to make sure all operations including clearing of
  478. * interrupt status registers in the hardware is performed.
  479. * Thus a barrier after clearing the interrupt status register
  480. * is required to guarantee that the interrupt status register has
  481. * really been cleared by the time we return from this handler.
  482. */
  483. wmb();
  484. return IRQ_HANDLED;
  485. }
  486. static irqreturn_t tsif_isr(int irq, void *dev)
  487. {
  488. struct tspp_tsif_device *tsif_device = dev;
  489. u32 sts_ctl = ioread32(tsif_device->base + TSIF_STS_CTL_OFF);
  490. if (!(sts_ctl & (TSIF_STS_CTL_PACK_AVAIL |
  491. TSIF_STS_CTL_OVERFLOW |
  492. TSIF_STS_CTL_LOST_SYNC |
  493. TSIF_STS_CTL_TIMEOUT)))
  494. return IRQ_NONE;
  495. if (sts_ctl & TSIF_STS_CTL_OVERFLOW)
  496. tsif_device->stat_overflow++;
  497. if (sts_ctl & TSIF_STS_CTL_LOST_SYNC)
  498. tsif_device->stat_lost_sync++;
  499. if (sts_ctl & TSIF_STS_CTL_TIMEOUT)
  500. tsif_device->stat_timeout++;
  501. iowrite32(sts_ctl, tsif_device->base + TSIF_STS_CTL_OFF);
  502. /*
  503. * Before returning IRQ_HANDLED to the generic interrupt handling
  504. * framework need to make sure all operations including clearing of
  505. * interrupt status registers in the hardware is performed.
  506. * Thus a barrier after clearing the interrupt status register
  507. * is required to guarantee that the interrupt status register has
  508. * really been cleared by the time we return from this handler.
  509. */
  510. wmb();
  511. return IRQ_HANDLED;
  512. }
  513. /*** callbacks ***/
  514. static void tspp_sps_complete_cb(struct sps_event_notify *notify)
  515. {
  516. struct tspp_device *pdev = notify->user;
  517. tasklet_schedule(&pdev->tlet);
  518. }
  519. static void tspp_expiration_timer(unsigned long data)
  520. {
  521. struct tspp_device *pdev = (struct tspp_device *)data;
  522. if (pdev)
  523. tasklet_schedule(&pdev->tlet);
  524. }
  525. /*** tasklet ***/
  526. static void tspp_sps_complete_tlet(unsigned long data)
  527. {
  528. int i;
  529. int complete;
  530. unsigned long flags;
  531. struct sps_iovec iovec;
  532. struct tspp_channel *channel;
  533. struct tspp_device *device = (struct tspp_device *)data;
  534. spin_lock_irqsave(&device->spinlock, flags);
  535. for (i = 0; i < TSPP_NUM_CHANNELS; i++) {
  536. complete = 0;
  537. channel = &device->channels[i];
  538. if (!channel->used || !channel->waiting)
  539. continue;
  540. /* stop the expiration timer */
  541. if (channel->expiration_period_ms)
  542. del_timer(&channel->expiration_timer);
  543. /* get completions */
  544. while (channel->waiting->state == TSPP_BUF_STATE_WAITING) {
  545. if (sps_get_iovec(channel->pipe, &iovec) != 0) {
  546. pr_err("tspp: Error in iovec on channel %i",
  547. channel->id);
  548. break;
  549. }
  550. if (iovec.size == 0)
  551. break;
  552. if (iovec.addr != channel->waiting->sps.phys_base)
  553. pr_err("tspp: buffer mismatch %pa",
  554. &channel->waiting->sps.phys_base);
  555. complete = 1;
  556. channel->waiting->state = TSPP_BUF_STATE_DATA;
  557. channel->waiting->filled = iovec.size;
  558. channel->waiting->read_index = 0;
  559. if (channel->src == TSPP_SOURCE_TSIF0)
  560. device->tsif[0].stat_rx++;
  561. else if (channel->src == TSPP_SOURCE_TSIF1)
  562. device->tsif[1].stat_rx++;
  563. /* update the pointers */
  564. channel->waiting = channel->waiting->next;
  565. }
  566. /* wake any waiting processes */
  567. if (complete) {
  568. wake_up_interruptible(&channel->in_queue);
  569. /* call notifiers */
  570. if (channel->notifier)
  571. channel->notifier(channel->id,
  572. channel->notify_data);
  573. }
  574. /* restart expiration timer */
  575. if (channel->expiration_period_ms)
  576. mod_timer(&channel->expiration_timer,
  577. jiffies +
  578. MSEC_TO_JIFFIES(
  579. channel->expiration_period_ms));
  580. }
  581. spin_unlock_irqrestore(&device->spinlock, flags);
  582. }
  583. /*** GPIO functions ***/
  584. static int tspp_gpios_disable(const struct tspp_tsif_device *tsif_device,
  585. const struct msm_gpio *table,
  586. int size)
  587. {
  588. int rc = 0;
  589. int i;
  590. const struct msm_gpio *g;
  591. for (i = size-1; i >= 0; i--) {
  592. int tmp;
  593. g = table + i;
  594. /* don't use sync GPIO when not working in mode 2 */
  595. if ((tsif_device->mode != TSPP_TSIF_MODE_2) &&
  596. (strnstr(g->label, "sync", strlen(g->label)) != NULL))
  597. continue;
  598. tmp = gpio_tlmm_config(GPIO_CFG(GPIO_PIN(g->gpio_cfg),
  599. 0, GPIO_CFG_INPUT, GPIO_CFG_PULL_DOWN, GPIO_CFG_2MA),
  600. GPIO_CFG_DISABLE);
  601. if (tmp) {
  602. pr_err("tspp_gpios_disable(0x%08x, GPIO_CFG_DISABLE) <%s> failed: %d\n",
  603. g->gpio_cfg, g->label ?: "?", rc);
  604. pr_err("tspp: pin %d func %d dir %d pull %d drvstr %d\n",
  605. GPIO_PIN(g->gpio_cfg), GPIO_FUNC(g->gpio_cfg),
  606. GPIO_DIR(g->gpio_cfg), GPIO_PULL(g->gpio_cfg),
  607. GPIO_DRVSTR(g->gpio_cfg));
  608. if (!rc)
  609. rc = tmp;
  610. }
  611. }
  612. return rc;
  613. }
  614. static int tspp_gpios_enable(const struct tspp_tsif_device *tsif_device,
  615. const struct msm_gpio *table,
  616. int size)
  617. {
  618. int rc;
  619. int i;
  620. const struct msm_gpio *g;
  621. for (i = 0; i < size; i++) {
  622. g = table + i;
  623. /* don't use sync GPIO when not working in mode 2 */
  624. if ((tsif_device->mode != TSPP_TSIF_MODE_2) &&
  625. (strnstr(g->label, "sync", strlen(g->label)) != NULL))
  626. continue;
  627. rc = gpio_tlmm_config(g->gpio_cfg, GPIO_CFG_ENABLE);
  628. if (rc) {
  629. pr_err("tspp: gpio_tlmm_config(0x%08x, GPIO_CFG_ENABLE) <%s> failed: %d\n",
  630. g->gpio_cfg, g->label ?: "?", rc);
  631. pr_err("tspp: pin %d func %d dir %d pull %d drvstr %d\n",
  632. GPIO_PIN(g->gpio_cfg), GPIO_FUNC(g->gpio_cfg),
  633. GPIO_DIR(g->gpio_cfg), GPIO_PULL(g->gpio_cfg),
  634. GPIO_DRVSTR(g->gpio_cfg));
  635. goto err;
  636. }
  637. }
  638. return 0;
  639. err:
  640. tspp_gpios_disable(tsif_device, table, i);
  641. return rc;
  642. }
  643. static int tspp_config_gpios(struct tspp_device *device,
  644. enum tspp_source source,
  645. int enable)
  646. {
  647. const struct msm_gpio *table;
  648. struct msm_tspp_platform_data *pdata = device->pdev->dev.platform_data;
  649. int num_gpios = (pdata->num_gpios / TSPP_TSIF_INSTANCES);
  650. int i = 0;
  651. if (num_gpios != TSPP_GPIOS_PER_TSIF) {
  652. pr_err("tspp %s: unexpected number of GPIOs %d, expected %d\n",
  653. __func__, num_gpios, TSPP_GPIOS_PER_TSIF);
  654. return -EINVAL;
  655. }
  656. /*
  657. * Note: this code assumes that the GPIO definitions in the
  658. * pdata->gpios table are according to the TSIF instance number,
  659. * i.e., that TSIF0 GPIOs are defined first, then TSIF1 GPIOs etc.
  660. */
  661. switch (source) {
  662. case TSPP_SOURCE_TSIF0:
  663. i = 0;
  664. break;
  665. case TSPP_SOURCE_TSIF1:
  666. i = 1;
  667. break;
  668. default:
  669. pr_err("tspp %s: invalid source\n", __func__);
  670. return -EINVAL;
  671. }
  672. table = pdata->gpios + (i * num_gpios);
  673. if (enable)
  674. return tspp_gpios_enable(&device->tsif[i], table, num_gpios);
  675. else
  676. return tspp_gpios_disable(&device->tsif[i], table, num_gpios);
  677. }
  678. /*** Clock functions ***/
  679. static int tspp_clock_start(struct tspp_device *device)
  680. {
  681. int rc;
  682. if (device == NULL) {
  683. pr_err("tspp: Can't start clocks, invalid device\n");
  684. return -EINVAL;
  685. }
  686. if (device->tsif_bus_client) {
  687. rc = msm_bus_scale_client_update_request(
  688. device->tsif_bus_client, 1);
  689. if (rc) {
  690. pr_err("tspp: Can't enable bus\n");
  691. return -EBUSY;
  692. }
  693. }
  694. if (device->tsif_vreg) {
  695. rc = regulator_set_voltage(device->tsif_vreg,
  696. RPM_REGULATOR_CORNER_SUPER_TURBO,
  697. RPM_REGULATOR_CORNER_SUPER_TURBO);
  698. if (rc) {
  699. pr_err("Unable to set CX voltage.\n");
  700. if (device->tsif_bus_client)
  701. msm_bus_scale_client_update_request(
  702. device->tsif_bus_client, 0);
  703. return rc;
  704. }
  705. }
  706. if (device->tsif_pclk && clk_prepare_enable(device->tsif_pclk) != 0) {
  707. pr_err("tspp: Can't start pclk");
  708. if (device->tsif_vreg) {
  709. regulator_set_voltage(device->tsif_vreg,
  710. RPM_REGULATOR_CORNER_NONE,
  711. RPM_REGULATOR_CORNER_SUPER_TURBO);
  712. }
  713. if (device->tsif_bus_client)
  714. msm_bus_scale_client_update_request(
  715. device->tsif_bus_client, 0);
  716. return -EBUSY;
  717. }
  718. if (device->tsif_ref_clk &&
  719. clk_prepare_enable(device->tsif_ref_clk) != 0) {
  720. pr_err("tspp: Can't start ref clk");
  721. clk_disable_unprepare(device->tsif_pclk);
  722. if (device->tsif_vreg) {
  723. regulator_set_voltage(device->tsif_vreg,
  724. RPM_REGULATOR_CORNER_NONE,
  725. RPM_REGULATOR_CORNER_SUPER_TURBO);
  726. }
  727. if (device->tsif_bus_client)
  728. msm_bus_scale_client_update_request(
  729. device->tsif_bus_client, 0);
  730. return -EBUSY;
  731. }
  732. return 0;
  733. }
  734. static void tspp_clock_stop(struct tspp_device *device)
  735. {
  736. int rc;
  737. if (device == NULL) {
  738. pr_err("tspp: Can't stop clocks, invalid device\n");
  739. return;
  740. }
  741. if (device->tsif_pclk)
  742. clk_disable_unprepare(device->tsif_pclk);
  743. if (device->tsif_ref_clk)
  744. clk_disable_unprepare(device->tsif_ref_clk);
  745. if (device->tsif_vreg) {
  746. rc = regulator_set_voltage(device->tsif_vreg,
  747. RPM_REGULATOR_CORNER_NONE,
  748. RPM_REGULATOR_CORNER_SUPER_TURBO);
  749. if (rc)
  750. pr_err("Unable to set CX voltage.\n");
  751. }
  752. if (device->tsif_bus_client) {
  753. rc = msm_bus_scale_client_update_request(
  754. device->tsif_bus_client, 0);
  755. if (rc)
  756. pr_err("tspp: Can't disable bus\n");
  757. }
  758. }
  759. /*** TSIF functions ***/
  760. static int tspp_start_tsif(struct tspp_tsif_device *tsif_device)
  761. {
  762. int start_hardware = 0;
  763. u32 ctl;
  764. if (tsif_device->ref_count == 0) {
  765. start_hardware = 1;
  766. } else if (tsif_device->ref_count > 0) {
  767. ctl = readl_relaxed(tsif_device->base + TSIF_STS_CTL_OFF);
  768. if ((ctl & TSIF_STS_CTL_START) != 1) {
  769. /* this hardware should already be running */
  770. pr_warn("tspp: tsif hw not started but ref count > 0");
  771. start_hardware = 1;
  772. }
  773. }
  774. if (start_hardware) {
  775. ctl = TSIF_STS_CTL_EN_IRQ |
  776. TSIF_STS_CTL_EN_DM;
  777. if (tsif_device->clock_inverse)
  778. ctl |= TSIF_STS_CTL_INV_CLOCK;
  779. if (tsif_device->data_inverse)
  780. ctl |= TSIF_STS_CTL_INV_DATA;
  781. if (tsif_device->sync_inverse)
  782. ctl |= TSIF_STS_CTL_INV_SYNC;
  783. if (tsif_device->enable_inverse)
  784. ctl |= TSIF_STS_CTL_INV_ENABLE;
  785. switch (tsif_device->mode) {
  786. case TSPP_TSIF_MODE_LOOPBACK:
  787. ctl |= TSIF_STS_CTL_EN_NULL |
  788. TSIF_STS_CTL_EN_ERROR |
  789. TSIF_STS_CTL_TEST_MODE;
  790. break;
  791. case TSPP_TSIF_MODE_1:
  792. ctl |= TSIF_STS_CTL_EN_TIME_LIM |
  793. TSIF_STS_CTL_EN_TCR;
  794. break;
  795. case TSPP_TSIF_MODE_2:
  796. ctl |= TSIF_STS_CTL_EN_TIME_LIM |
  797. TSIF_STS_CTL_EN_TCR |
  798. TSIF_STS_CTL_MODE_2;
  799. break;
  800. default:
  801. pr_warn("tspp: unknown tsif mode 0x%x",
  802. tsif_device->mode);
  803. }
  804. writel_relaxed(ctl, tsif_device->base + TSIF_STS_CTL_OFF);
  805. writel_relaxed(tsif_device->time_limit,
  806. tsif_device->base + TSIF_TIME_LIMIT_OFF);
  807. wmb();
  808. writel_relaxed(ctl | TSIF_STS_CTL_START,
  809. tsif_device->base + TSIF_STS_CTL_OFF);
  810. wmb();
  811. }
  812. ctl = readl_relaxed(tsif_device->base + TSIF_STS_CTL_OFF);
  813. if (!(ctl & TSIF_STS_CTL_START))
  814. return -EBUSY;
  815. tsif_device->ref_count++;
  816. return 0;
  817. }
  818. static void tspp_stop_tsif(struct tspp_tsif_device *tsif_device)
  819. {
  820. if (tsif_device->ref_count == 0)
  821. return;
  822. tsif_device->ref_count--;
  823. if (tsif_device->ref_count == 0) {
  824. writel_relaxed(TSIF_STS_CTL_STOP,
  825. tsif_device->base + TSIF_STS_CTL_OFF);
  826. wmb();
  827. }
  828. }
  829. /*** local TSPP functions ***/
  830. static int tspp_channels_in_use(struct tspp_device *pdev)
  831. {
  832. int i;
  833. int count = 0;
  834. for (i = 0; i < TSPP_NUM_CHANNELS; i++)
  835. count += (pdev->channels[i].used ? 1 : 0);
  836. return count;
  837. }
  838. static struct tspp_device *tspp_find_by_id(int id)
  839. {
  840. struct tspp_device *dev;
  841. list_for_each_entry(dev, &tspp_devices, devlist) {
  842. if (dev->pdev->id == id)
  843. return dev;
  844. }
  845. return NULL;
  846. }
  847. static int tspp_get_key_entry(void)
  848. {
  849. int i;
  850. for (i = 0; i < TSPP_NUM_KEYS; i++) {
  851. if (!(tspp_key_entry & (1 << i))) {
  852. tspp_key_entry |= (1 << i);
  853. return i;
  854. }
  855. }
  856. return 1 < TSPP_NUM_KEYS;
  857. }
  858. static void tspp_free_key_entry(int entry)
  859. {
  860. if (entry > TSPP_NUM_KEYS) {
  861. pr_err("tspp_free_key_entry: index out of bounds");
  862. return;
  863. }
  864. tspp_key_entry &= ~(1 << entry);
  865. }
  866. static int tspp_alloc_buffer(u32 channel_id, struct tspp_data_descriptor *desc,
  867. u32 size, struct dma_pool *dma_pool, tspp_allocator *alloc, void *user)
  868. {
  869. if (size < TSPP_MIN_BUFFER_SIZE ||
  870. size > TSPP_MAX_BUFFER_SIZE) {
  871. pr_err("tspp: bad buffer size %i", size);
  872. return -ENOMEM;
  873. }
  874. if (alloc) {
  875. TSPP_DEBUG("tspp using alloc function");
  876. desc->virt_base = alloc(channel_id, size,
  877. &desc->phys_base, user);
  878. } else {
  879. if (!dma_pool)
  880. desc->virt_base = dma_alloc_coherent(NULL, size,
  881. &desc->phys_base, GFP_KERNEL);
  882. else
  883. desc->virt_base = dma_pool_alloc(dma_pool, GFP_KERNEL,
  884. &desc->phys_base);
  885. if (desc->virt_base == 0) {
  886. pr_err("tspp: dma buffer allocation failed %i\n", size);
  887. return -ENOMEM;
  888. }
  889. }
  890. desc->size = size;
  891. return 0;
  892. }
  893. static int tspp_queue_buffer(struct tspp_channel *channel,
  894. struct tspp_mem_buffer *buffer)
  895. {
  896. int rc;
  897. u32 flags = 0;
  898. /* make sure the interrupt frequency is valid */
  899. if (channel->int_freq < 1)
  900. channel->int_freq = 1;
  901. /* generate interrupt according to requested frequency */
  902. if (buffer->desc.id % channel->int_freq == channel->int_freq-1)
  903. flags = SPS_IOVEC_FLAG_INT;
  904. /* start the transfer */
  905. rc = sps_transfer_one(channel->pipe,
  906. buffer->sps.phys_base,
  907. buffer->sps.size,
  908. channel->pdev,
  909. flags);
  910. if (rc < 0)
  911. return rc;
  912. buffer->state = TSPP_BUF_STATE_WAITING;
  913. return 0;
  914. }
  915. static int tspp_global_reset(struct tspp_device *pdev)
  916. {
  917. u32 i, val;
  918. /* stop all TSIFs */
  919. for (i = 0; i < TSPP_TSIF_INSTANCES; i++) {
  920. pdev->tsif[i].ref_count = 1; /* allows stopping hw */
  921. tspp_stop_tsif(&pdev->tsif[i]); /* will reset ref_count to 0 */
  922. pdev->tsif[i].time_limit = TSPP_TSIF_DEFAULT_TIME_LIMIT;
  923. pdev->tsif[i].clock_inverse = 0;
  924. pdev->tsif[i].data_inverse = 0;
  925. pdev->tsif[i].sync_inverse = 0;
  926. pdev->tsif[i].enable_inverse = 0;
  927. }
  928. writel_relaxed(TSPP_RST_RESET, pdev->base + TSPP_RST);
  929. wmb();
  930. /* TSPP tables */
  931. for (i = 0; i < TSPP_FILTER_TABLES; i++)
  932. memset(pdev->filters[i],
  933. 0, sizeof(struct tspp_pid_filter_table));
  934. /* disable all filters */
  935. val = (2 << TSPP_NUM_CHANNELS) - 1;
  936. writel_relaxed(val, pdev->base + TSPP_PS_DISABLE);
  937. /* TSPP registers */
  938. val = readl_relaxed(pdev->base + TSPP_CONTROL);
  939. writel_relaxed(val | TSPP_CLK_CONTROL_FORCE_PERF_CNT,
  940. pdev->base + TSPP_CONTROL);
  941. wmb();
  942. memset(pdev->tspp_global_performance, 0,
  943. sizeof(struct tspp_global_performance_regs));
  944. memset(pdev->tspp_pipe_context, 0,
  945. sizeof(struct tspp_pipe_context_regs));
  946. memset(pdev->tspp_pipe_performance, 0,
  947. sizeof(struct tspp_pipe_performance_regs));
  948. wmb();
  949. writel_relaxed(val & ~TSPP_CLK_CONTROL_FORCE_PERF_CNT,
  950. pdev->base + TSPP_CONTROL);
  951. wmb();
  952. val = readl_relaxed(pdev->base + TSPP_CONFIG);
  953. val &= ~(TSPP_CONFIG_PS_LEN_ERR_MASK |
  954. TSPP_CONFIG_PS_CONT_ERR_UNSP_MASK |
  955. TSPP_CONFIG_PS_CONT_ERR_MASK);
  956. TSPP_CONFIG_SET_PACKET_LENGTH(val, TSPP_PACKET_LENGTH);
  957. writel_relaxed(val, pdev->base + TSPP_CONFIG);
  958. writel_relaxed(0x0007ffff, pdev->base + TSPP_IRQ_MASK);
  959. writel_relaxed(0x000fffff, pdev->base + TSPP_IRQ_CLEAR);
  960. writel_relaxed(0, pdev->base + TSPP_RST);
  961. wmb();
  962. tspp_key_entry = 0;
  963. return 0;
  964. }
  965. static int tspp_select_source(u32 dev, u32 channel_id,
  966. struct tspp_select_source *src)
  967. {
  968. /* make sure the requested src id is in bounds */
  969. if (src->source > TSPP_SOURCE_MEM) {
  970. pr_err("tspp source out of bounds");
  971. return -EINVAL;
  972. }
  973. /* open the stream */
  974. tspp_open_stream(dev, channel_id, src);
  975. return 0;
  976. }
  977. static int tspp_set_iv(struct tspp_channel *channel, struct tspp_iv *iv)
  978. {
  979. struct tspp_device *pdev = channel->pdev;
  980. writel_relaxed(iv->data[0], pdev->base + TSPP_CBC_INIT_VAL(0));
  981. writel_relaxed(iv->data[1], pdev->base + TSPP_CBC_INIT_VAL(1));
  982. return 0;
  983. }
  984. static int tspp_set_system_keys(struct tspp_channel *channel,
  985. struct tspp_system_keys *keys)
  986. {
  987. int i;
  988. struct tspp_device *pdev = channel->pdev;
  989. for (i = 0; i < TSPP_NUM_SYSTEM_KEYS; i++)
  990. writel_relaxed(keys->data[i], pdev->base + TSPP_SYSTEM_KEY(i));
  991. return 0;
  992. }
  993. static int tspp_channel_init(struct tspp_channel *channel,
  994. struct tspp_device *pdev)
  995. {
  996. channel->cdev.owner = THIS_MODULE;
  997. cdev_init(&channel->cdev, &tspp_fops);
  998. channel->pdev = pdev;
  999. channel->data = NULL;
  1000. channel->read = NULL;
  1001. channel->waiting = NULL;
  1002. channel->locked = NULL;
  1003. channel->id = MINOR(tspp_minor);
  1004. channel->used = 0;
  1005. channel->buffer_size = TSPP_MIN_BUFFER_SIZE;
  1006. channel->max_buffers = TSPP_NUM_BUFFERS;
  1007. channel->buffer_count = 0;
  1008. channel->filter_count = 0;
  1009. channel->int_freq = 1;
  1010. channel->src = TSPP_SOURCE_NONE;
  1011. channel->mode = TSPP_MODE_DISABLED;
  1012. channel->notifier = NULL;
  1013. channel->notify_data = NULL;
  1014. channel->expiration_period_ms = 0;
  1015. channel->memfree = NULL;
  1016. channel->user_info = NULL;
  1017. init_waitqueue_head(&channel->in_queue);
  1018. if (cdev_add(&channel->cdev, tspp_minor++, 1) != 0) {
  1019. pr_err("tspp: cdev_add failed");
  1020. return -EBUSY;
  1021. }
  1022. channel->dd = device_create(tspp_class, NULL, channel->cdev.dev,
  1023. channel, "tspp%02d", channel->id);
  1024. if (IS_ERR(channel->dd)) {
  1025. pr_err("tspp: device_create failed: %i",
  1026. (int)PTR_ERR(channel->dd));
  1027. cdev_del(&channel->cdev);
  1028. return -EBUSY;
  1029. }
  1030. return 0;
  1031. }
  1032. static int tspp_set_buffer_size(struct tspp_channel *channel,
  1033. struct tspp_buffer *buf)
  1034. {
  1035. if (channel->buffer_count > 0) {
  1036. pr_err("tspp: cannot set buffer size - buffers already allocated\n");
  1037. return -EPERM;
  1038. }
  1039. if (buf->size < TSPP_MIN_BUFFER_SIZE)
  1040. channel->buffer_size = TSPP_MIN_BUFFER_SIZE;
  1041. else if (buf->size > TSPP_MAX_BUFFER_SIZE)
  1042. channel->buffer_size = TSPP_MAX_BUFFER_SIZE;
  1043. else
  1044. channel->buffer_size = buf->size;
  1045. return 0;
  1046. }
  1047. static void tspp_set_tsif_mode(struct tspp_channel *channel,
  1048. enum tspp_tsif_mode mode)
  1049. {
  1050. int index;
  1051. switch (channel->src) {
  1052. case TSPP_SOURCE_TSIF0:
  1053. index = 0;
  1054. break;
  1055. case TSPP_SOURCE_TSIF1:
  1056. index = 1;
  1057. break;
  1058. default:
  1059. pr_warn("tspp: can't set mode for non-tsif source %d",
  1060. channel->src);
  1061. return;
  1062. }
  1063. channel->pdev->tsif[index].mode = mode;
  1064. }
  1065. static void tspp_set_signal_inversion(struct tspp_channel *channel,
  1066. int clock_inverse, int data_inverse,
  1067. int sync_inverse, int enable_inverse)
  1068. {
  1069. int index;
  1070. switch (channel->src) {
  1071. case TSPP_SOURCE_TSIF0:
  1072. index = 0;
  1073. break;
  1074. case TSPP_SOURCE_TSIF1:
  1075. index = 1;
  1076. break;
  1077. default:
  1078. return;
  1079. }
  1080. channel->pdev->tsif[index].clock_inverse = clock_inverse;
  1081. channel->pdev->tsif[index].data_inverse = data_inverse;
  1082. channel->pdev->tsif[index].sync_inverse = sync_inverse;
  1083. channel->pdev->tsif[index].enable_inverse = enable_inverse;
  1084. }
  1085. static int tspp_is_buffer_size_aligned(u32 size, enum tspp_mode mode)
  1086. {
  1087. u32 alignment;
  1088. switch (mode) {
  1089. case TSPP_MODE_RAW:
  1090. /* must be a multiple of 192 */
  1091. alignment = (TSPP_PACKET_LENGTH + 4);
  1092. if (size % alignment)
  1093. return 0;
  1094. return 1;
  1095. case TSPP_MODE_RAW_NO_SUFFIX:
  1096. /* must be a multiple of 188 */
  1097. alignment = TSPP_PACKET_LENGTH;
  1098. if (size % alignment)
  1099. return 0;
  1100. return 1;
  1101. case TSPP_MODE_DISABLED:
  1102. case TSPP_MODE_PES:
  1103. default:
  1104. /* no alignment requirement */
  1105. return 1;
  1106. }
  1107. }
  1108. static u32 tspp_align_buffer_size_by_mode(u32 size, enum tspp_mode mode)
  1109. {
  1110. u32 new_size;
  1111. u32 alignment;
  1112. switch (mode) {
  1113. case TSPP_MODE_RAW:
  1114. /* must be a multiple of 192 */
  1115. alignment = (TSPP_PACKET_LENGTH + 4);
  1116. break;
  1117. case TSPP_MODE_RAW_NO_SUFFIX:
  1118. /* must be a multiple of 188 */
  1119. alignment = TSPP_PACKET_LENGTH;
  1120. break;
  1121. case TSPP_MODE_DISABLED:
  1122. case TSPP_MODE_PES:
  1123. default:
  1124. /* no alignment requirement - give the user what he asks for */
  1125. alignment = 1;
  1126. break;
  1127. }
  1128. /* align up */
  1129. new_size = (((size + alignment - 1) / alignment) * alignment);
  1130. return new_size;
  1131. }
  1132. static void tspp_destroy_buffers(u32 channel_id, struct tspp_channel *channel)
  1133. {
  1134. int i;
  1135. struct tspp_mem_buffer *pbuf, *temp;
  1136. pbuf = channel->data;
  1137. for (i = 0; i < channel->buffer_count; i++) {
  1138. if (pbuf->desc.phys_base) {
  1139. if (channel->memfree) {
  1140. channel->memfree(channel_id,
  1141. pbuf->desc.size,
  1142. pbuf->desc.virt_base,
  1143. pbuf->desc.phys_base,
  1144. channel->user_info);
  1145. } else {
  1146. if (!channel->dma_pool)
  1147. dma_free_coherent(NULL,
  1148. pbuf->desc.size,
  1149. pbuf->desc.virt_base,
  1150. pbuf->desc.phys_base);
  1151. else
  1152. dma_pool_free(channel->dma_pool,
  1153. pbuf->desc.virt_base,
  1154. pbuf->desc.phys_base);
  1155. }
  1156. pbuf->desc.phys_base = 0;
  1157. }
  1158. pbuf->desc.virt_base = 0;
  1159. pbuf->state = TSPP_BUF_STATE_EMPTY;
  1160. temp = pbuf;
  1161. pbuf = pbuf->next;
  1162. kfree(temp);
  1163. }
  1164. }
  1165. static int msm_tspp_req_irqs(struct tspp_device *device)
  1166. {
  1167. int rc;
  1168. int i;
  1169. int j;
  1170. rc = request_irq(device->tspp_irq, tspp_isr, IRQF_SHARED,
  1171. dev_name(&device->pdev->dev), device);
  1172. if (rc) {
  1173. dev_err(&device->pdev->dev,
  1174. "failed to request TSPP IRQ %d : %d",
  1175. device->tspp_irq, rc);
  1176. return rc;
  1177. }
  1178. for (i = 0; i < TSPP_TSIF_INSTANCES; i++) {
  1179. rc = request_irq(device->tsif[i].tsif_irq,
  1180. tsif_isr, IRQF_SHARED, dev_name(&device->pdev->dev),
  1181. &device->tsif[i]);
  1182. if (rc) {
  1183. dev_err(&device->pdev->dev,
  1184. "failed to request TSIF%d IRQ: %d",
  1185. i, rc);
  1186. goto failed;
  1187. }
  1188. }
  1189. device->req_irqs = true;
  1190. return 0;
  1191. failed:
  1192. free_irq(device->tspp_irq, device);
  1193. for (j = 0; j < i; j++)
  1194. free_irq(device->tsif[j].tsif_irq, device);
  1195. return rc;
  1196. }
  1197. static inline void msm_tspp_free_irqs(struct tspp_device *device)
  1198. {
  1199. int i;
  1200. for (i = 0; i < TSPP_TSIF_INSTANCES; i++) {
  1201. if (device->tsif[i].tsif_irq)
  1202. free_irq(device->tsif[i].tsif_irq, &device->tsif[i]);
  1203. }
  1204. if (device->tspp_irq)
  1205. free_irq(device->tspp_irq, device);
  1206. device->req_irqs = false;
  1207. }
  1208. /*** TSPP API functions ***/
  1209. /**
  1210. * tspp_open_stream - open a TSPP stream for use.
  1211. *
  1212. * @dev: TSPP device (up to TSPP_MAX_DEVICES)
  1213. * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
  1214. * @source: stream source parameters.
  1215. *
  1216. * Return error status
  1217. *
  1218. */
  1219. int tspp_open_stream(u32 dev, u32 channel_id,
  1220. struct tspp_select_source *source)
  1221. {
  1222. u32 val;
  1223. int rc;
  1224. struct tspp_device *pdev;
  1225. struct tspp_channel *channel;
  1226. bool req_irqs = false;
  1227. TSPP_DEBUG("tspp_open_stream %i %i %i %i",
  1228. dev, channel_id, source->source, source->mode);
  1229. if (dev >= TSPP_MAX_DEVICES) {
  1230. pr_err("tspp: device id out of range");
  1231. return -ENODEV;
  1232. }
  1233. if (channel_id >= TSPP_NUM_CHANNELS) {
  1234. pr_err("tspp: channel id out of range");
  1235. return -ECHRNG;
  1236. }
  1237. pdev = tspp_find_by_id(dev);
  1238. if (!pdev) {
  1239. pr_err("tspp_str: can't find device %i", dev);
  1240. return -ENODEV;
  1241. }
  1242. channel = &pdev->channels[channel_id];
  1243. channel->src = source->source;
  1244. tspp_set_tsif_mode(channel, source->mode);
  1245. tspp_set_signal_inversion(channel, source->clk_inverse,
  1246. source->data_inverse, source->sync_inverse,
  1247. source->enable_inverse);
  1248. /* Request IRQ resources on first open */
  1249. if (!pdev->req_irqs && (source->source == TSPP_SOURCE_TSIF0 ||
  1250. source->source == TSPP_SOURCE_TSIF1)) {
  1251. rc = msm_tspp_req_irqs(pdev);
  1252. if (rc) {
  1253. pr_err("tspp: error requesting irqs\n");
  1254. return rc;
  1255. }
  1256. req_irqs = true;
  1257. }
  1258. switch (source->source) {
  1259. case TSPP_SOURCE_TSIF0:
  1260. if (tspp_config_gpios(pdev, channel->src, 1) != 0) {
  1261. rc = -EBUSY;
  1262. pr_err("tspp: error enabling tsif0 GPIOs\n");
  1263. goto free_irq;
  1264. }
  1265. /* make sure TSIF0 is running & enabled */
  1266. if (tspp_start_tsif(&pdev->tsif[0]) != 0) {
  1267. rc = -EBUSY;
  1268. pr_err("tspp: error starting tsif0");
  1269. goto free_irq;
  1270. }
  1271. if (pdev->tsif[0].ref_count == 1) {
  1272. val = readl_relaxed(pdev->base + TSPP_CONTROL);
  1273. writel_relaxed(val & ~TSPP_CONTROL_TSP_TSIF0_SRC_DIS,
  1274. pdev->base + TSPP_CONTROL);
  1275. wmb();
  1276. }
  1277. break;
  1278. case TSPP_SOURCE_TSIF1:
  1279. if (tspp_config_gpios(pdev, channel->src, 1) != 0) {
  1280. rc = -EBUSY;
  1281. pr_err("tspp: error enabling tsif1 GPIOs\n");
  1282. goto free_irq;
  1283. }
  1284. /* make sure TSIF1 is running & enabled */
  1285. if (tspp_start_tsif(&pdev->tsif[1]) != 0) {
  1286. rc = -EBUSY;
  1287. pr_err("tspp: error starting tsif1");
  1288. goto free_irq;
  1289. }
  1290. if (pdev->tsif[1].ref_count == 1) {
  1291. val = readl_relaxed(pdev->base + TSPP_CONTROL);
  1292. writel_relaxed(val & ~TSPP_CONTROL_TSP_TSIF1_SRC_DIS,
  1293. pdev->base + TSPP_CONTROL);
  1294. wmb();
  1295. }
  1296. break;
  1297. case TSPP_SOURCE_MEM:
  1298. break;
  1299. default:
  1300. pr_err("tspp: channel %i invalid source %i",
  1301. channel->id, source->source);
  1302. return -EBUSY;
  1303. }
  1304. return 0;
  1305. free_irq:
  1306. /* Free irqs only if were requested during opening of this stream */
  1307. if (req_irqs)
  1308. msm_tspp_free_irqs(pdev);
  1309. return rc;
  1310. }
  1311. EXPORT_SYMBOL(tspp_open_stream);
  1312. /**
  1313. * tspp_close_stream - close a TSPP stream.
  1314. *
  1315. * @dev: TSPP device (up to TSPP_MAX_DEVICES)
  1316. * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
  1317. *
  1318. * Return error status
  1319. *
  1320. */
  1321. int tspp_close_stream(u32 dev, u32 channel_id)
  1322. {
  1323. u32 val;
  1324. u32 prev_ref_count = 0;
  1325. struct tspp_device *pdev;
  1326. struct tspp_channel *channel;
  1327. if (channel_id >= TSPP_NUM_CHANNELS) {
  1328. pr_err("tspp: channel id out of range");
  1329. return -ECHRNG;
  1330. }
  1331. pdev = tspp_find_by_id(dev);
  1332. if (!pdev) {
  1333. pr_err("tspp_cs: can't find device %i", dev);
  1334. return -EBUSY;
  1335. }
  1336. channel = &pdev->channels[channel_id];
  1337. switch (channel->src) {
  1338. case TSPP_SOURCE_TSIF0:
  1339. prev_ref_count = pdev->tsif[0].ref_count;
  1340. tspp_stop_tsif(&pdev->tsif[0]);
  1341. if (tspp_config_gpios(pdev, channel->src, 0) != 0)
  1342. pr_err("tspp: error disabling tsif0 GPIOs\n");
  1343. if (prev_ref_count == 1) {
  1344. val = readl_relaxed(pdev->base + TSPP_CONTROL);
  1345. writel_relaxed(val | TSPP_CONTROL_TSP_TSIF0_SRC_DIS,
  1346. pdev->base + TSPP_CONTROL);
  1347. wmb();
  1348. }
  1349. break;
  1350. case TSPP_SOURCE_TSIF1:
  1351. prev_ref_count = pdev->tsif[1].ref_count;
  1352. tspp_stop_tsif(&pdev->tsif[1]);
  1353. if (tspp_config_gpios(pdev, channel->src, 0) != 0)
  1354. pr_err("tspp: error disabling tsif0 GPIOs\n");
  1355. if (prev_ref_count == 1) {
  1356. val = readl_relaxed(pdev->base + TSPP_CONTROL);
  1357. writel_relaxed(val | TSPP_CONTROL_TSP_TSIF1_SRC_DIS,
  1358. pdev->base + TSPP_CONTROL);
  1359. wmb();
  1360. }
  1361. break;
  1362. case TSPP_SOURCE_MEM:
  1363. break;
  1364. case TSPP_SOURCE_NONE:
  1365. break;
  1366. }
  1367. channel->src = TSPP_SOURCE_NONE;
  1368. /* Free requested interrupts to save power */
  1369. if ((pdev->tsif[0].ref_count + pdev->tsif[1].ref_count) == 0 &&
  1370. prev_ref_count)
  1371. msm_tspp_free_irqs(pdev);
  1372. return 0;
  1373. }
  1374. EXPORT_SYMBOL(tspp_close_stream);
  1375. static int tspp_init_sps_device(struct tspp_device *dev)
  1376. {
  1377. int ret;
  1378. ret = sps_register_bam_device(&dev->bam_props, &dev->bam_handle);
  1379. if (ret) {
  1380. pr_err("tspp: failed to register bam device, err-%d\n", ret);
  1381. return ret;
  1382. }
  1383. ret = sps_device_reset(dev->bam_handle);
  1384. if (ret) {
  1385. sps_deregister_bam_device(dev->bam_handle);
  1386. pr_err("tspp: error resetting bam device, err=%d\n", ret);
  1387. return ret;
  1388. }
  1389. return 0;
  1390. }
  1391. /**
  1392. * tspp_open_channel - open a TSPP channel.
  1393. *
  1394. * @dev: TSPP device (up to TSPP_MAX_DEVICES)
  1395. * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
  1396. *
  1397. * Return error status
  1398. *
  1399. */
  1400. int tspp_open_channel(u32 dev, u32 channel_id)
  1401. {
  1402. int rc = 0;
  1403. struct sps_connect *config;
  1404. struct sps_register_event *event;
  1405. struct tspp_channel *channel;
  1406. struct tspp_device *pdev;
  1407. if (channel_id >= TSPP_NUM_CHANNELS) {
  1408. pr_err("tspp: channel id out of range");
  1409. return -ECHRNG;
  1410. }
  1411. pdev = tspp_find_by_id(dev);
  1412. if (!pdev) {
  1413. pr_err("tspp_oc: can't find device %i", dev);
  1414. return -ENODEV;
  1415. }
  1416. channel = &pdev->channels[channel_id];
  1417. if (channel->used) {
  1418. pr_err("tspp channel already in use");
  1419. return -EBUSY;
  1420. }
  1421. config = &channel->config;
  1422. event = &channel->event;
  1423. /* start the clocks if needed */
  1424. if (tspp_channels_in_use(pdev) == 0) {
  1425. rc = tspp_clock_start(pdev);
  1426. if (rc)
  1427. return rc;
  1428. if (pdev->bam_handle == SPS_DEV_HANDLE_INVALID) {
  1429. rc = tspp_init_sps_device(pdev);
  1430. if (rc) {
  1431. pr_err("tspp: failed to init sps device, err=%d\n",
  1432. rc);
  1433. tspp_clock_stop(pdev);
  1434. return rc;
  1435. }
  1436. }
  1437. wake_lock(&pdev->wake_lock);
  1438. }
  1439. /* mark it as used */
  1440. channel->used = 1;
  1441. /* start the bam */
  1442. channel->pipe = sps_alloc_endpoint();
  1443. if (channel->pipe == 0) {
  1444. pr_err("tspp: error allocating endpoint");
  1445. rc = -ENOMEM;
  1446. goto err_sps_alloc;
  1447. }
  1448. /* get default configuration */
  1449. sps_get_config(channel->pipe, config);
  1450. config->source = pdev->bam_handle;
  1451. config->destination = SPS_DEV_HANDLE_MEM;
  1452. config->mode = SPS_MODE_SRC;
  1453. config->options =
  1454. SPS_O_AUTO_ENABLE | /* connection is auto-enabled */
  1455. SPS_O_STREAMING | /* streaming mode */
  1456. SPS_O_DESC_DONE | /* interrupt on end of descriptor */
  1457. SPS_O_ACK_TRANSFERS | /* must use sps_get_iovec() */
  1458. SPS_O_HYBRID; /* Read actual descriptors in sps_get_iovec() */
  1459. config->src_pipe_index = channel->id;
  1460. config->desc.size =
  1461. TSPP_SPS_DESCRIPTOR_COUNT * SPS_DESCRIPTOR_SIZE;
  1462. config->desc.base = dma_alloc_coherent(NULL,
  1463. config->desc.size,
  1464. &config->desc.phys_base,
  1465. GFP_KERNEL);
  1466. if (config->desc.base == 0) {
  1467. pr_err("tspp: error allocating sps descriptors");
  1468. rc = -ENOMEM;
  1469. goto err_desc_alloc;
  1470. }
  1471. memset(config->desc.base, 0, config->desc.size);
  1472. rc = sps_connect(channel->pipe, config);
  1473. if (rc) {
  1474. pr_err("tspp: error connecting bam");
  1475. goto err_connect;
  1476. }
  1477. event->mode = SPS_TRIGGER_CALLBACK;
  1478. event->options = SPS_O_DESC_DONE;
  1479. event->callback = tspp_sps_complete_cb;
  1480. event->xfer_done = NULL;
  1481. event->user = pdev;
  1482. rc = sps_register_event(channel->pipe, event);
  1483. if (rc) {
  1484. pr_err("tspp: error registering event");
  1485. goto err_event;
  1486. }
  1487. init_timer(&channel->expiration_timer);
  1488. channel->expiration_timer.function = tspp_expiration_timer;
  1489. channel->expiration_timer.data = (unsigned long)pdev;
  1490. channel->expiration_timer.expires = 0xffffffffL;
  1491. rc = pm_runtime_get(&pdev->pdev->dev);
  1492. if (rc < 0) {
  1493. dev_err(&pdev->pdev->dev,
  1494. "Runtime PM: Unable to wake up tspp device, rc = %d",
  1495. rc);
  1496. }
  1497. return 0;
  1498. err_event:
  1499. sps_disconnect(channel->pipe);
  1500. err_connect:
  1501. dma_free_coherent(NULL, config->desc.size, config->desc.base,
  1502. config->desc.phys_base);
  1503. err_desc_alloc:
  1504. sps_free_endpoint(channel->pipe);
  1505. err_sps_alloc:
  1506. channel->used = 0;
  1507. return rc;
  1508. }
  1509. EXPORT_SYMBOL(tspp_open_channel);
  1510. /**
  1511. * tspp_close_channel - close a TSPP channel.
  1512. *
  1513. * @dev: TSPP device (up to TSPP_MAX_DEVICES)
  1514. * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
  1515. *
  1516. * Return error status
  1517. *
  1518. */
  1519. int tspp_close_channel(u32 dev, u32 channel_id)
  1520. {
  1521. int i;
  1522. int id;
  1523. int table_idx;
  1524. u32 val;
  1525. unsigned long flags;
  1526. struct sps_connect *config;
  1527. struct tspp_device *pdev;
  1528. struct tspp_channel *channel;
  1529. if (channel_id >= TSPP_NUM_CHANNELS) {
  1530. pr_err("tspp: channel id out of range");
  1531. return -ECHRNG;
  1532. }
  1533. pdev = tspp_find_by_id(dev);
  1534. if (!pdev) {
  1535. pr_err("tspp_close: can't find device %i", dev);
  1536. return -ENODEV;
  1537. }
  1538. channel = &pdev->channels[channel_id];
  1539. /* if the channel is not used, we are done */
  1540. if (!channel->used)
  1541. return 0;
  1542. /*
  1543. * Need to protect access to used and waiting fields, as they are
  1544. * used by the tasklet which is invoked from interrupt context
  1545. */
  1546. spin_lock_irqsave(&pdev->spinlock, flags);
  1547. channel->used = 0;
  1548. channel->waiting = NULL;
  1549. spin_unlock_irqrestore(&pdev->spinlock, flags);
  1550. if (channel->expiration_period_ms)
  1551. del_timer(&channel->expiration_timer);
  1552. channel->notifier = NULL;
  1553. channel->notify_data = NULL;
  1554. channel->expiration_period_ms = 0;
  1555. config = &channel->config;
  1556. pdev = channel->pdev;
  1557. /* disable pipe (channel) */
  1558. val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
  1559. writel_relaxed(val | channel->id, pdev->base + TSPP_PS_DISABLE);
  1560. wmb();
  1561. /* unregister all filters for this channel */
  1562. for (table_idx = 0; table_idx < TSPP_FILTER_TABLES; table_idx++) {
  1563. for (i = 0; i < TSPP_NUM_PRIORITIES; i++) {
  1564. struct tspp_pid_filter *filter =
  1565. &pdev->filters[table_idx]->filter[i];
  1566. id = FILTER_GET_PIPE_NUMBER0(filter);
  1567. if (id == channel->id) {
  1568. if (FILTER_HAS_ENCRYPTION(filter))
  1569. tspp_free_key_entry(
  1570. FILTER_GET_KEY_NUMBER(filter));
  1571. filter->config = 0;
  1572. filter->filter = 0;
  1573. }
  1574. }
  1575. }
  1576. channel->filter_count = 0;
  1577. /* disconnect the bam */
  1578. if (sps_disconnect(channel->pipe) != 0)
  1579. pr_warn("tspp: Error freeing sps endpoint (%i)", channel->id);
  1580. /* destroy the buffers */
  1581. dma_free_coherent(NULL, config->desc.size, config->desc.base,
  1582. config->desc.phys_base);
  1583. sps_free_endpoint(channel->pipe);
  1584. tspp_destroy_buffers(channel_id, channel);
  1585. if (channel->dma_pool) {
  1586. dma_pool_destroy(channel->dma_pool);
  1587. channel->dma_pool = NULL;
  1588. }
  1589. channel->src = TSPP_SOURCE_NONE;
  1590. channel->mode = TSPP_MODE_DISABLED;
  1591. channel->memfree = NULL;
  1592. channel->user_info = NULL;
  1593. channel->buffer_count = 0;
  1594. channel->data = NULL;
  1595. channel->read = NULL;
  1596. channel->locked = NULL;
  1597. if (tspp_channels_in_use(pdev) == 0) {
  1598. sps_deregister_bam_device(pdev->bam_handle);
  1599. pdev->bam_handle = SPS_DEV_HANDLE_INVALID;
  1600. wake_unlock(&pdev->wake_lock);
  1601. tspp_clock_stop(pdev);
  1602. }
  1603. pm_runtime_put(&pdev->pdev->dev);
  1604. return 0;
  1605. }
  1606. EXPORT_SYMBOL(tspp_close_channel);
  1607. /**
  1608. * tspp_get_ref_clk_counter - return the TSIF clock reference (TCR) counter.
  1609. *
  1610. * @dev: TSPP device (up to TSPP_MAX_DEVICES)
  1611. * @source: The TSIF source from which the counter should be read
  1612. * @tcr_counter: the value of TCR counter
  1613. *
  1614. * Return error status
  1615. *
  1616. * TCR increments at a rate equal to 27 MHz/256 = 105.47 kHz.
  1617. * If source is neither TSIF 0 or TSIF1 0 is returned.
  1618. */
  1619. int tspp_get_ref_clk_counter(u32 dev, enum tspp_source source, u32 *tcr_counter)
  1620. {
  1621. struct tspp_device *pdev;
  1622. struct tspp_tsif_device *tsif_device;
  1623. if (!tcr_counter)
  1624. return -EINVAL;
  1625. pdev = tspp_find_by_id(dev);
  1626. if (!pdev) {
  1627. pr_err("tspp_get_ref_clk_counter: can't find device %i\n", dev);
  1628. return -ENODEV;
  1629. }
  1630. switch (source) {
  1631. case TSPP_SOURCE_TSIF0:
  1632. tsif_device = &pdev->tsif[0];
  1633. break;
  1634. case TSPP_SOURCE_TSIF1:
  1635. tsif_device = &pdev->tsif[1];
  1636. break;
  1637. default:
  1638. tsif_device = NULL;
  1639. break;
  1640. }
  1641. if (tsif_device && tsif_device->ref_count)
  1642. *tcr_counter = ioread32(tsif_device->base + TSIF_CLK_REF_OFF);
  1643. else
  1644. *tcr_counter = 0;
  1645. return 0;
  1646. }
  1647. EXPORT_SYMBOL(tspp_get_ref_clk_counter);
  1648. /**
  1649. * tspp_add_filter - add a TSPP filter to a channel.
  1650. *
  1651. * @dev: TSPP device (up to TSPP_MAX_DEVICES)
  1652. * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
  1653. * @filter: TSPP filter parameters
  1654. *
  1655. * Return error status
  1656. *
  1657. */
  1658. int tspp_add_filter(u32 dev, u32 channel_id,
  1659. struct tspp_filter *filter)
  1660. {
  1661. int i, rc;
  1662. int other_channel;
  1663. int entry;
  1664. u32 val, pid, enabled;
  1665. struct tspp_device *pdev;
  1666. struct tspp_pid_filter p;
  1667. struct tspp_channel *channel;
  1668. TSPP_DEBUG("tspp: add filter");
  1669. if (channel_id >= TSPP_NUM_CHANNELS) {
  1670. pr_err("tspp: channel id out of range");
  1671. return -ECHRNG;
  1672. }
  1673. pdev = tspp_find_by_id(dev);
  1674. if (!pdev) {
  1675. pr_err("tspp_add: can't find device %i", dev);
  1676. return -ENODEV;
  1677. }
  1678. channel = &pdev->channels[channel_id];
  1679. if (filter->source > TSPP_SOURCE_MEM) {
  1680. pr_err("tspp invalid source");
  1681. return -ENOSR;
  1682. }
  1683. if (filter->priority >= TSPP_NUM_PRIORITIES) {
  1684. pr_err("tspp invalid filter priority");
  1685. return -ENOSR;
  1686. }
  1687. channel->mode = filter->mode;
  1688. /*
  1689. * if buffers are already allocated, verify they fulfil
  1690. * the alignment requirements.
  1691. */
  1692. if ((channel->buffer_count > 0) &&
  1693. (!tspp_is_buffer_size_aligned(channel->buffer_size, channel->mode)))
  1694. pr_warn("tspp: buffers allocated with incorrect alignment\n");
  1695. if (filter->mode == TSPP_MODE_PES) {
  1696. for (i = 0; i < TSPP_NUM_PRIORITIES; i++) {
  1697. struct tspp_pid_filter *tspp_filter =
  1698. &pdev->filters[channel->src]->filter[i];
  1699. pid = FILTER_GET_PIPE_PID((tspp_filter));
  1700. enabled = FILTER_GET_PIPE_PROCESS0(tspp_filter);
  1701. if (enabled && (pid == filter->pid)) {
  1702. other_channel =
  1703. FILTER_GET_PIPE_NUMBER0(tspp_filter);
  1704. pr_err("tspp: pid 0x%x already in use by channel %i",
  1705. filter->pid, other_channel);
  1706. return -EBADSLT;
  1707. }
  1708. }
  1709. }
  1710. /* make sure this priority is not already in use */
  1711. enabled = FILTER_GET_PIPE_PROCESS0(
  1712. (&(pdev->filters[channel->src]->filter[filter->priority])));
  1713. if (enabled) {
  1714. pr_err("tspp: filter priority %i source %i is already enabled\n",
  1715. filter->priority, channel->src);
  1716. return -ENOSR;
  1717. }
  1718. if (channel->mode == TSPP_MODE_PES) {
  1719. /* if we are already processing in PES mode, disable pipe
  1720. (channel) and filter to be updated */
  1721. val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
  1722. writel_relaxed(val | (1 << channel->id),
  1723. pdev->base + TSPP_PS_DISABLE);
  1724. wmb();
  1725. }
  1726. /* update entry */
  1727. p.filter = 0;
  1728. p.config = FILTER_TRANS_END_DISABLE;
  1729. FILTER_SET_PIPE_PROCESS0((&p), filter->mode);
  1730. FILTER_SET_PIPE_PID((&p), filter->pid);
  1731. FILTER_SET_PID_MASK((&p), filter->mask);
  1732. FILTER_SET_PIPE_NUMBER0((&p), channel->id);
  1733. FILTER_SET_PIPE_PROCESS1((&p), TSPP_MODE_DISABLED);
  1734. if (filter->decrypt) {
  1735. entry = tspp_get_key_entry();
  1736. if (entry == -1) {
  1737. pr_err("tspp: no more keys available!");
  1738. } else {
  1739. p.config |= FILTER_DECRYPT;
  1740. FILTER_SET_KEY_NUMBER((&p), entry);
  1741. }
  1742. }
  1743. pdev->filters[channel->src]->
  1744. filter[filter->priority].config = p.config;
  1745. pdev->filters[channel->src]->
  1746. filter[filter->priority].filter = p.filter;
  1747. /*
  1748. * allocate buffers if needed (i.e. if user did has not already called
  1749. * tspp_allocate_buffers() explicitly).
  1750. */
  1751. if (channel->buffer_count == 0) {
  1752. channel->buffer_size =
  1753. tspp_align_buffer_size_by_mode(channel->buffer_size,
  1754. channel->mode);
  1755. rc = tspp_allocate_buffers(dev, channel->id,
  1756. channel->max_buffers,
  1757. channel->buffer_size,
  1758. channel->int_freq, NULL, NULL, NULL);
  1759. if (rc != 0) {
  1760. pr_err("tspp: tspp_allocate_buffers failed\n");
  1761. return rc;
  1762. }
  1763. }
  1764. /* reenable pipe */
  1765. val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
  1766. writel_relaxed(val & ~(1 << channel->id), pdev->base + TSPP_PS_DISABLE);
  1767. wmb();
  1768. val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
  1769. channel->filter_count++;
  1770. return 0;
  1771. }
  1772. EXPORT_SYMBOL(tspp_add_filter);
  1773. /**
  1774. * tspp_remove_filter - remove a TSPP filter from a channel.
  1775. *
  1776. * @dev: TSPP device (up to TSPP_MAX_DEVICES)
  1777. * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
  1778. * @filter: TSPP filter parameters
  1779. *
  1780. * Return error status
  1781. *
  1782. */
  1783. int tspp_remove_filter(u32 dev, u32 channel_id,
  1784. struct tspp_filter *filter)
  1785. {
  1786. int entry;
  1787. u32 val;
  1788. struct tspp_device *pdev;
  1789. int src;
  1790. struct tspp_pid_filter *tspp_filter;
  1791. struct tspp_channel *channel;
  1792. if (channel_id >= TSPP_NUM_CHANNELS) {
  1793. pr_err("tspp: channel id out of range");
  1794. return -ECHRNG;
  1795. }
  1796. pdev = tspp_find_by_id(dev);
  1797. if (!pdev) {
  1798. pr_err("tspp_remove: can't find device %i", dev);
  1799. return -ENODEV;
  1800. }
  1801. if (filter->priority >= TSPP_NUM_PRIORITIES) {
  1802. pr_err("tspp invalid filter priority");
  1803. return -ENOSR;
  1804. }
  1805. channel = &pdev->channels[channel_id];
  1806. src = channel->src;
  1807. tspp_filter = &(pdev->filters[src]->filter[filter->priority]);
  1808. /* disable pipe (channel) */
  1809. val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
  1810. writel_relaxed(val | channel->id, pdev->base + TSPP_PS_DISABLE);
  1811. wmb();
  1812. /* update data keys */
  1813. if (tspp_filter->config & FILTER_DECRYPT) {
  1814. entry = FILTER_GET_KEY_NUMBER(tspp_filter);
  1815. tspp_free_key_entry(entry);
  1816. }
  1817. /* update pid table */
  1818. tspp_filter->config = 0;
  1819. tspp_filter->filter = 0;
  1820. channel->filter_count--;
  1821. /* reenable pipe */
  1822. val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
  1823. writel_relaxed(val & ~(1 << channel->id),
  1824. pdev->base + TSPP_PS_DISABLE);
  1825. wmb();
  1826. val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
  1827. return 0;
  1828. }
  1829. EXPORT_SYMBOL(tspp_remove_filter);
  1830. /**
  1831. * tspp_set_key - set TSPP key in key table.
  1832. *
  1833. * @dev: TSPP device (up to TSPP_MAX_DEVICES)
  1834. * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
  1835. * @key: TSPP key parameters
  1836. *
  1837. * Return error status
  1838. *
  1839. */
  1840. int tspp_set_key(u32 dev, u32 channel_id, struct tspp_key *key)
  1841. {
  1842. int i;
  1843. int id;
  1844. int key_index;
  1845. int data;
  1846. struct tspp_channel *channel;
  1847. struct tspp_device *pdev;
  1848. if (channel_id >= TSPP_NUM_CHANNELS) {
  1849. pr_err("tspp: channel id out of range");
  1850. return -ECHRNG;
  1851. }
  1852. pdev = tspp_find_by_id(dev);
  1853. if (!pdev) {
  1854. pr_err("tspp_set: can't find device %i", dev);
  1855. return -ENODEV;
  1856. }
  1857. channel = &pdev->channels[channel_id];
  1858. /* read the key index used by this channel */
  1859. for (i = 0; i < TSPP_NUM_PRIORITIES; i++) {
  1860. struct tspp_pid_filter *tspp_filter =
  1861. &(pdev->filters[channel->src]->filter[i]);
  1862. id = FILTER_GET_PIPE_NUMBER0(tspp_filter);
  1863. if (id == channel->id) {
  1864. if (FILTER_HAS_ENCRYPTION(tspp_filter)) {
  1865. key_index = FILTER_GET_KEY_NUMBER(tspp_filter);
  1866. break;
  1867. }
  1868. }
  1869. }
  1870. if (i == TSPP_NUM_PRIORITIES) {
  1871. pr_err("tspp: no encryption on this channel");
  1872. return -ENOKEY;
  1873. }
  1874. if (key->parity == TSPP_KEY_PARITY_EVEN) {
  1875. pdev->tspp_key_table->entry[key_index].even_lsb = key->lsb;
  1876. pdev->tspp_key_table->entry[key_index].even_msb = key->msb;
  1877. } else {
  1878. pdev->tspp_key_table->entry[key_index].odd_lsb = key->lsb;
  1879. pdev->tspp_key_table->entry[key_index].odd_msb = key->msb;
  1880. }
  1881. data = readl_relaxed(channel->pdev->base + TSPP_KEY_VALID);
  1882. return 0;
  1883. }
  1884. EXPORT_SYMBOL(tspp_set_key);
  1885. /**
  1886. * tspp_register_notification - register TSPP channel notification function.
  1887. *
  1888. * @dev: TSPP device (up to TSPP_MAX_DEVICES)
  1889. * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
  1890. * @pNotify: notification function
  1891. * @userdata: user data to pass to notification function
  1892. * @timer_ms: notification for partially filled buffers
  1893. *
  1894. * Return error status
  1895. *
  1896. */
  1897. int tspp_register_notification(u32 dev, u32 channel_id,
  1898. tspp_notifier *pNotify, void *userdata, u32 timer_ms)
  1899. {
  1900. struct tspp_channel *channel;
  1901. struct tspp_device *pdev;
  1902. if (channel_id >= TSPP_NUM_CHANNELS) {
  1903. pr_err("tspp: channel id out of range");
  1904. return -ECHRNG;
  1905. }
  1906. pdev = tspp_find_by_id(dev);
  1907. if (!pdev) {
  1908. pr_err("tspp_reg: can't find device %i", dev);
  1909. return -ENODEV;
  1910. }
  1911. channel = &pdev->channels[channel_id];
  1912. channel->notifier = pNotify;
  1913. channel->notify_data = userdata;
  1914. channel->expiration_period_ms = timer_ms;
  1915. return 0;
  1916. }
  1917. EXPORT_SYMBOL(tspp_register_notification);
  1918. /**
  1919. * tspp_unregister_notification - unregister TSPP channel notification function.
  1920. *
  1921. * @dev: TSPP device (up to TSPP_MAX_DEVICES)
  1922. * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
  1923. *
  1924. * Return error status
  1925. *
  1926. */
  1927. int tspp_unregister_notification(u32 dev, u32 channel_id)
  1928. {
  1929. struct tspp_channel *channel;
  1930. struct tspp_device *pdev;
  1931. if (channel_id >= TSPP_NUM_CHANNELS) {
  1932. pr_err("tspp: channel id out of range");
  1933. return -ECHRNG;
  1934. }
  1935. pdev = tspp_find_by_id(dev);
  1936. if (!pdev) {
  1937. pr_err("tspp_unreg: can't find device %i", dev);
  1938. return -ENODEV;
  1939. }
  1940. channel = &pdev->channels[channel_id];
  1941. channel->notifier = NULL;
  1942. channel->notify_data = 0;
  1943. return 0;
  1944. }
  1945. EXPORT_SYMBOL(tspp_unregister_notification);
  1946. /**
  1947. * tspp_get_buffer - get TSPP data buffer.
  1948. *
  1949. * @dev: TSPP device (up to TSPP_MAX_DEVICES)
  1950. * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
  1951. *
  1952. * Return error status
  1953. *
  1954. */
  1955. const struct tspp_data_descriptor *tspp_get_buffer(u32 dev, u32 channel_id)
  1956. {
  1957. struct tspp_mem_buffer *buffer;
  1958. struct tspp_channel *channel;
  1959. struct tspp_device *pdev;
  1960. unsigned long flags;
  1961. if (channel_id >= TSPP_NUM_CHANNELS) {
  1962. pr_err("tspp: channel id out of range");
  1963. return NULL;
  1964. }
  1965. pdev = tspp_find_by_id(dev);
  1966. if (!pdev) {
  1967. pr_err("tspp_get: can't find device %i", dev);
  1968. return NULL;
  1969. }
  1970. spin_lock_irqsave(&pdev->spinlock, flags);
  1971. channel = &pdev->channels[channel_id];
  1972. if (!channel->read) {
  1973. spin_unlock_irqrestore(&pdev->spinlock, flags);
  1974. pr_warn("tspp: no buffer to get on channel %i!",
  1975. channel->id);
  1976. return NULL;
  1977. }
  1978. buffer = channel->read;
  1979. /* see if we have any buffers ready to read */
  1980. if (buffer->state != TSPP_BUF_STATE_DATA) {
  1981. spin_unlock_irqrestore(&pdev->spinlock, flags);
  1982. return NULL;
  1983. }
  1984. if (buffer->state == TSPP_BUF_STATE_DATA) {
  1985. /* mark the buffer as busy */
  1986. buffer->state = TSPP_BUF_STATE_LOCKED;
  1987. /* increment the pointer along the list */
  1988. channel->read = channel->read->next;
  1989. }
  1990. spin_unlock_irqrestore(&pdev->spinlock, flags);
  1991. return &buffer->desc;
  1992. }
  1993. EXPORT_SYMBOL(tspp_get_buffer);
  1994. /**
  1995. * tspp_release_buffer - release TSPP data buffer back to TSPP.
  1996. *
  1997. * @dev: TSPP device (up to TSPP_MAX_DEVICES)
  1998. * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
  1999. * @descriptor_id: buffer descriptor ID
  2000. *
  2001. * Return error status
  2002. *
  2003. */
  2004. int tspp_release_buffer(u32 dev, u32 channel_id, u32 descriptor_id)
  2005. {
  2006. int i, found = 0;
  2007. struct tspp_mem_buffer *buffer;
  2008. struct tspp_channel *channel;
  2009. struct tspp_device *pdev;
  2010. unsigned long flags;
  2011. if (channel_id >= TSPP_NUM_CHANNELS) {
  2012. pr_err("tspp: channel id out of range");
  2013. return -ECHRNG;
  2014. }
  2015. pdev = tspp_find_by_id(dev);
  2016. if (!pdev) {
  2017. pr_err("tspp: can't find device %i", dev);
  2018. return -ENODEV;
  2019. }
  2020. spin_lock_irqsave(&pdev->spinlock, flags);
  2021. channel = &pdev->channels[channel_id];
  2022. if (descriptor_id > channel->buffer_count)
  2023. pr_warn("tspp: desc id looks weird 0x%08x", descriptor_id);
  2024. /* find the correct descriptor */
  2025. buffer = channel->locked;
  2026. for (i = 0; i < channel->buffer_count; i++) {
  2027. if (buffer->desc.id == descriptor_id) {
  2028. found = 1;
  2029. break;
  2030. }
  2031. buffer = buffer->next;
  2032. }
  2033. channel->locked = channel->locked->next;
  2034. if (!found) {
  2035. spin_unlock_irqrestore(&pdev->spinlock, flags);
  2036. pr_err("tspp: cant find desc %i", descriptor_id);
  2037. return -EINVAL;
  2038. }
  2039. /* make sure the buffer is in the expected state */
  2040. if (buffer->state != TSPP_BUF_STATE_LOCKED) {
  2041. spin_unlock_irqrestore(&pdev->spinlock, flags);
  2042. pr_err("tspp: buffer %i not locked", descriptor_id);
  2043. return -EINVAL;
  2044. }
  2045. /* unlock the buffer and requeue it */
  2046. buffer->state = TSPP_BUF_STATE_WAITING;
  2047. if (tspp_queue_buffer(channel, buffer))
  2048. pr_warn("tspp: can't requeue buffer");
  2049. spin_unlock_irqrestore(&pdev->spinlock, flags);
  2050. return 0;
  2051. }
  2052. EXPORT_SYMBOL(tspp_release_buffer);
  2053. /**
  2054. * tspp_allocate_buffers - allocate TSPP data buffers.
  2055. *
  2056. * @dev: TSPP device (up to TSPP_MAX_DEVICES)
  2057. * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
  2058. * @count: number of buffers to allocate
  2059. * @size: size of each buffer to allocate
  2060. * @int_freq: interrupt frequency
  2061. * @alloc: user defined memory allocator function. Pass NULL for default.
  2062. * @memfree: user defined memory free function. Pass NULL for default.
  2063. * @user: user data to pass to the memory allocator/free function
  2064. *
  2065. * Return error status
  2066. *
  2067. * The user can optionally call this function explicitly to allocate the TSPP
  2068. * data buffers. Alternatively, if the user did not call this function, it
  2069. * is called implicitly by tspp_add_filter().
  2070. */
  2071. int tspp_allocate_buffers(u32 dev, u32 channel_id, u32 count, u32 size,
  2072. u32 int_freq, tspp_allocator *alloc,
  2073. tspp_memfree *memfree, void *user)
  2074. {
  2075. struct tspp_channel *channel;
  2076. struct tspp_device *pdev;
  2077. struct tspp_mem_buffer *last = NULL;
  2078. TSPP_DEBUG("tspp_allocate_buffers");
  2079. if (channel_id >= TSPP_NUM_CHANNELS) {
  2080. pr_err("%s: channel id out of range", __func__);
  2081. return -ECHRNG;
  2082. }
  2083. pdev = tspp_find_by_id(dev);
  2084. if (!pdev) {
  2085. pr_err("%s: can't find device %i", __func__, dev);
  2086. return -ENODEV;
  2087. }
  2088. if (count < MIN_ACCEPTABLE_BUFFER_COUNT) {
  2089. pr_err("%s: tspp requires a minimum of %i buffers\n",
  2090. __func__, MIN_ACCEPTABLE_BUFFER_COUNT);
  2091. return -EINVAL;
  2092. }
  2093. if (count > TSPP_NUM_BUFFERS) {
  2094. pr_err("%s: tspp requires a maximum of %i buffers\n",
  2095. __func__, TSPP_NUM_BUFFERS);
  2096. return -EINVAL;
  2097. }
  2098. channel = &pdev->channels[channel_id];
  2099. /* allow buffer allocation only if there was no previous buffer
  2100. * allocation for this channel.
  2101. */
  2102. if (channel->buffer_count > 0) {
  2103. pr_err("%s: buffers already allocated for channel %u",
  2104. __func__, channel_id);
  2105. return -EINVAL;
  2106. }
  2107. channel->max_buffers = count;
  2108. /* set up interrupt frequency */
  2109. if (int_freq > channel->max_buffers) {
  2110. int_freq = channel->max_buffers;
  2111. pr_warn("%s: setting interrupt frequency to %u\n",
  2112. __func__, int_freq);
  2113. }
  2114. channel->int_freq = int_freq;
  2115. /*
  2116. * it is the responsibility of the caller to tspp_allocate_buffers(),
  2117. * whether it's the user or the driver, to make sure the size parameter
  2118. * is compatible to the channel mode.
  2119. */
  2120. channel->buffer_size = size;
  2121. /* save user defined memory free function for later use */
  2122. channel->memfree = memfree;
  2123. channel->user_info = user;
  2124. /*
  2125. * For small buffers, create a DMA pool so that memory
  2126. * is not wasted through dma_alloc_coherent.
  2127. */
  2128. if (TSPP_USE_DMA_POOL(channel->buffer_size)) {
  2129. channel->dma_pool = dma_pool_create("tspp",
  2130. NULL, channel->buffer_size, 0, 0);
  2131. if (!channel->dma_pool) {
  2132. pr_err("%s: Can't allocate memory pool\n", __func__);
  2133. return -ENOMEM;
  2134. }
  2135. } else {
  2136. channel->dma_pool = NULL;
  2137. }
  2138. for (channel->buffer_count = 0;
  2139. channel->buffer_count < channel->max_buffers;
  2140. channel->buffer_count++) {
  2141. /* allocate the descriptor */
  2142. struct tspp_mem_buffer *desc = (struct tspp_mem_buffer *)
  2143. kmalloc(sizeof(struct tspp_mem_buffer), GFP_KERNEL);
  2144. if (!desc) {
  2145. pr_warn("%s: Can't allocate desc %i",
  2146. __func__, channel->buffer_count);
  2147. break;
  2148. }
  2149. desc->desc.id = channel->buffer_count;
  2150. /* allocate the buffer */
  2151. if (tspp_alloc_buffer(channel_id, &desc->desc,
  2152. channel->buffer_size, channel->dma_pool,
  2153. alloc, user) != 0) {
  2154. kfree(desc);
  2155. pr_warn("%s: Can't allocate buffer %i",
  2156. __func__, channel->buffer_count);
  2157. break;
  2158. }
  2159. /* add the descriptor to the list */
  2160. desc->filled = 0;
  2161. desc->read_index = 0;
  2162. if (!channel->data) {
  2163. channel->data = desc;
  2164. desc->next = channel->data;
  2165. } else {
  2166. last->next = desc;
  2167. }
  2168. last = desc;
  2169. desc->next = channel->data;
  2170. /* prepare the sps descriptor */
  2171. desc->sps.phys_base = desc->desc.phys_base;
  2172. desc->sps.base = desc->desc.virt_base;
  2173. desc->sps.size = desc->desc.size;
  2174. /* start the transfer */
  2175. if (tspp_queue_buffer(channel, desc))
  2176. pr_err("%s: can't queue buffer %i",
  2177. __func__, desc->desc.id);
  2178. }
  2179. if (channel->buffer_count < channel->max_buffers) {
  2180. /*
  2181. * we failed to allocate the requested number of buffers.
  2182. * we don't allow a partial success, so need to clean up here.
  2183. */
  2184. tspp_destroy_buffers(channel_id, channel);
  2185. channel->buffer_count = 0;
  2186. if (channel->dma_pool) {
  2187. dma_pool_destroy(channel->dma_pool);
  2188. channel->dma_pool = NULL;
  2189. }
  2190. return -ENOMEM;
  2191. }
  2192. channel->waiting = channel->data;
  2193. channel->read = channel->data;
  2194. channel->locked = channel->data;
  2195. /* Now that buffers are scheduled to HW, kick data expiration timer */
  2196. if (channel->expiration_period_ms)
  2197. mod_timer(&channel->expiration_timer,
  2198. jiffies +
  2199. MSEC_TO_JIFFIES(
  2200. channel->expiration_period_ms));
  2201. return 0;
  2202. }
  2203. EXPORT_SYMBOL(tspp_allocate_buffers);
  2204. /*** File Operations ***/
  2205. static ssize_t tspp_open(struct inode *inode, struct file *filp)
  2206. {
  2207. u32 dev;
  2208. struct tspp_channel *channel;
  2209. TSPP_DEBUG("tspp_open");
  2210. channel = container_of(inode->i_cdev, struct tspp_channel, cdev);
  2211. filp->private_data = channel;
  2212. dev = channel->pdev->pdev->id;
  2213. /* if this channel is already in use, quit */
  2214. if (channel->used) {
  2215. pr_err("tspp channel %i already in use",
  2216. MINOR(channel->cdev.dev));
  2217. return -EACCES;
  2218. }
  2219. if (tspp_open_channel(dev, channel->id) != 0) {
  2220. pr_err("tspp: error opening channel");
  2221. return -EACCES;
  2222. }
  2223. return 0;
  2224. }
  2225. static unsigned int tspp_poll(struct file *filp, struct poll_table_struct *p)
  2226. {
  2227. unsigned long flags;
  2228. unsigned int mask = 0;
  2229. struct tspp_channel *channel;
  2230. channel = filp->private_data;
  2231. /* register the wait queue for this channel */
  2232. poll_wait(filp, &channel->in_queue, p);
  2233. spin_lock_irqsave(&channel->pdev->spinlock, flags);
  2234. if (channel->read &&
  2235. channel->read->state == TSPP_BUF_STATE_DATA)
  2236. mask = POLLIN | POLLRDNORM;
  2237. spin_unlock_irqrestore(&channel->pdev->spinlock, flags);
  2238. return mask;
  2239. }
  2240. static ssize_t tspp_release(struct inode *inode, struct file *filp)
  2241. {
  2242. struct tspp_channel *channel = filp->private_data;
  2243. u32 dev = channel->pdev->pdev->id;
  2244. TSPP_DEBUG("tspp_release");
  2245. tspp_close_channel(dev, channel->id);
  2246. return 0;
  2247. }
  2248. static ssize_t tspp_read(struct file *filp, char __user *buf, size_t count,
  2249. loff_t *f_pos)
  2250. {
  2251. size_t size = 0;
  2252. size_t transferred = 0;
  2253. struct tspp_channel *channel;
  2254. struct tspp_mem_buffer *buffer;
  2255. channel = filp->private_data;
  2256. TSPP_DEBUG("tspp_read");
  2257. while (!channel->read) {
  2258. if (filp->f_flags & O_NONBLOCK) {
  2259. pr_warn("tspp: no buffer on channel %i!",
  2260. channel->id);
  2261. return -EAGAIN;
  2262. }
  2263. /* go to sleep if there is nothing to read */
  2264. if (wait_event_interruptible(channel->in_queue,
  2265. (channel->read != NULL))) {
  2266. pr_err("tspp: rude awakening\n");
  2267. return -ERESTARTSYS;
  2268. }
  2269. }
  2270. buffer = channel->read;
  2271. /* see if we have any buffers ready to read */
  2272. while (buffer->state != TSPP_BUF_STATE_DATA) {
  2273. if (filp->f_flags & O_NONBLOCK) {
  2274. pr_warn("tspp: nothing to read on channel %i!",
  2275. channel->id);
  2276. return -EAGAIN;
  2277. }
  2278. /* go to sleep if there is nothing to read */
  2279. if (wait_event_interruptible(channel->in_queue,
  2280. (buffer->state == TSPP_BUF_STATE_DATA))) {
  2281. pr_err("tspp: rude awakening\n");
  2282. return -ERESTARTSYS;
  2283. }
  2284. }
  2285. while (buffer->state == TSPP_BUF_STATE_DATA) {
  2286. size = min(count, buffer->filled);
  2287. if (size == 0)
  2288. break;
  2289. if (copy_to_user(buf, buffer->desc.virt_base +
  2290. buffer->read_index, size)) {
  2291. pr_err("tspp: error copying to user buffer");
  2292. return -EBUSY;
  2293. }
  2294. buf += size;
  2295. count -= size;
  2296. transferred += size;
  2297. buffer->read_index += size;
  2298. /*
  2299. * after reading the end of the buffer, requeue it,
  2300. * and set up for reading the next one
  2301. */
  2302. if (buffer->read_index == buffer->filled) {
  2303. buffer->state = TSPP_BUF_STATE_WAITING;
  2304. if (tspp_queue_buffer(channel, buffer))
  2305. pr_err("tspp: can't submit transfer");
  2306. channel->locked = channel->read;
  2307. channel->read = channel->read->next;
  2308. }
  2309. }
  2310. return transferred;
  2311. }
  2312. static long tspp_ioctl(struct file *filp,
  2313. unsigned int param0, unsigned long param1)
  2314. {
  2315. u32 dev;
  2316. int rc = -1;
  2317. struct tspp_channel *channel;
  2318. struct tspp_select_source ss;
  2319. struct tspp_filter f;
  2320. struct tspp_key k;
  2321. struct tspp_iv iv;
  2322. struct tspp_system_keys sk;
  2323. struct tspp_buffer b;
  2324. channel = filp->private_data;
  2325. dev = channel->pdev->pdev->id;
  2326. if ((param0 != TSPP_IOCTL_CLOSE_STREAM) && !param1)
  2327. return -EINVAL;
  2328. switch (param0) {
  2329. case TSPP_IOCTL_SELECT_SOURCE:
  2330. if (!access_ok(VERIFY_READ, param1,
  2331. sizeof(struct tspp_select_source))) {
  2332. return -EBUSY;
  2333. }
  2334. if (__copy_from_user(&ss, (void *)param1,
  2335. sizeof(struct tspp_select_source)) == 0)
  2336. rc = tspp_select_source(dev, channel->id, &ss);
  2337. break;
  2338. case TSPP_IOCTL_ADD_FILTER:
  2339. if (!access_ok(VERIFY_READ, param1,
  2340. sizeof(struct tspp_filter))) {
  2341. return -ENOSR;
  2342. }
  2343. if (__copy_from_user(&f, (void *)param1,
  2344. sizeof(struct tspp_filter)) == 0)
  2345. rc = tspp_add_filter(dev, channel->id, &f);
  2346. break;
  2347. case TSPP_IOCTL_REMOVE_FILTER:
  2348. if (!access_ok(VERIFY_READ, param1,
  2349. sizeof(struct tspp_filter))) {
  2350. return -EBUSY;
  2351. }
  2352. if (__copy_from_user(&f, (void *)param1,
  2353. sizeof(struct tspp_filter)) == 0)
  2354. rc = tspp_remove_filter(dev, channel->id, &f);
  2355. break;
  2356. case TSPP_IOCTL_SET_KEY:
  2357. if (!access_ok(VERIFY_READ, param1,
  2358. sizeof(struct tspp_key))) {
  2359. return -EBUSY;
  2360. }
  2361. if (__copy_from_user(&k, (void *)param1,
  2362. sizeof(struct tspp_key)) == 0)
  2363. rc = tspp_set_key(dev, channel->id, &k);
  2364. break;
  2365. case TSPP_IOCTL_SET_IV:
  2366. if (!access_ok(VERIFY_READ, param1,
  2367. sizeof(struct tspp_iv))) {
  2368. return -EBUSY;
  2369. }
  2370. if (__copy_from_user(&iv, (void *)param1,
  2371. sizeof(struct tspp_iv)) == 0)
  2372. rc = tspp_set_iv(channel, &iv);
  2373. break;
  2374. case TSPP_IOCTL_SET_SYSTEM_KEYS:
  2375. if (!access_ok(VERIFY_READ, param1,
  2376. sizeof(struct tspp_system_keys))) {
  2377. return -EINVAL;
  2378. }
  2379. if (__copy_from_user(&sk, (void *)param1,
  2380. sizeof(struct tspp_system_keys)) == 0)
  2381. rc = tspp_set_system_keys(channel, &sk);
  2382. break;
  2383. case TSPP_IOCTL_BUFFER_SIZE:
  2384. if (!access_ok(VERIFY_READ, param1,
  2385. sizeof(struct tspp_buffer))) {
  2386. rc = -EINVAL;
  2387. }
  2388. if (__copy_from_user(&b, (void *)param1,
  2389. sizeof(struct tspp_buffer)) == 0)
  2390. rc = tspp_set_buffer_size(channel, &b);
  2391. break;
  2392. case TSPP_IOCTL_CLOSE_STREAM:
  2393. rc = tspp_close_stream(dev, channel->id);
  2394. break;
  2395. default:
  2396. pr_err("tspp: Unknown ioctl %i", param0);
  2397. }
  2398. /*
  2399. * normalize the return code in case one of the subfunctions does
  2400. * something weird
  2401. */
  2402. if (rc != 0)
  2403. rc = -ENOIOCTLCMD;
  2404. return rc;
  2405. }
  2406. /*** debugfs ***/
  2407. static int debugfs_iomem_x32_set(void *data, u64 val)
  2408. {
  2409. writel_relaxed(val, data);
  2410. wmb();
  2411. return 0;
  2412. }
  2413. static int debugfs_iomem_x32_get(void *data, u64 *val)
  2414. {
  2415. *val = readl_relaxed(data);
  2416. return 0;
  2417. }
  2418. DEFINE_SIMPLE_ATTRIBUTE(fops_iomem_x32, debugfs_iomem_x32_get,
  2419. debugfs_iomem_x32_set, "0x%08llx");
  2420. static void tsif_debugfs_init(struct tspp_tsif_device *tsif_device,
  2421. int instance)
  2422. {
  2423. char name[10];
  2424. snprintf(name, 10, "tsif%i", instance);
  2425. tsif_device->dent_tsif = debugfs_create_dir(
  2426. name, NULL);
  2427. if (tsif_device->dent_tsif) {
  2428. int i;
  2429. void __iomem *base = tsif_device->base;
  2430. for (i = 0; i < ARRAY_SIZE(debugfs_tsif_regs); i++) {
  2431. tsif_device->debugfs_tsif_regs[i] =
  2432. debugfs_create_file(
  2433. debugfs_tsif_regs[i].name,
  2434. debugfs_tsif_regs[i].mode,
  2435. tsif_device->dent_tsif,
  2436. base + debugfs_tsif_regs[i].offset,
  2437. &fops_iomem_x32);
  2438. }
  2439. debugfs_create_u32(
  2440. "stat_rx_chunks",
  2441. S_IRUGO | S_IWUSR | S_IWGRP,
  2442. tsif_device->dent_tsif,
  2443. &tsif_device->stat_rx);
  2444. debugfs_create_u32(
  2445. "stat_overflow",
  2446. S_IRUGO | S_IWUSR | S_IWGRP,
  2447. tsif_device->dent_tsif,
  2448. &tsif_device->stat_overflow);
  2449. debugfs_create_u32(
  2450. "stat_lost_sync",
  2451. S_IRUGO | S_IWUSR | S_IWGRP,
  2452. tsif_device->dent_tsif,
  2453. &tsif_device->stat_lost_sync);
  2454. debugfs_create_u32(
  2455. "stat_timeout",
  2456. S_IRUGO | S_IWUSR | S_IWGRP,
  2457. tsif_device->dent_tsif,
  2458. &tsif_device->stat_timeout);
  2459. }
  2460. }
  2461. static void tsif_debugfs_exit(struct tspp_tsif_device *tsif_device)
  2462. {
  2463. if (tsif_device->dent_tsif) {
  2464. int i;
  2465. debugfs_remove_recursive(tsif_device->dent_tsif);
  2466. tsif_device->dent_tsif = NULL;
  2467. for (i = 0; i < ARRAY_SIZE(debugfs_tsif_regs); i++)
  2468. tsif_device->debugfs_tsif_regs[i] = NULL;
  2469. }
  2470. }
  2471. static void tspp_debugfs_init(struct tspp_device *device, int instance)
  2472. {
  2473. char name[10];
  2474. snprintf(name, 10, "tspp%i", instance);
  2475. device->dent = debugfs_create_dir(
  2476. name, NULL);
  2477. if (device->dent) {
  2478. int i;
  2479. void __iomem *base = device->base;
  2480. for (i = 0; i < ARRAY_SIZE(debugfs_tspp_regs); i++) {
  2481. device->debugfs_regs[i] =
  2482. debugfs_create_file(
  2483. debugfs_tspp_regs[i].name,
  2484. debugfs_tspp_regs[i].mode,
  2485. device->dent,
  2486. base + debugfs_tspp_regs[i].offset,
  2487. &fops_iomem_x32);
  2488. }
  2489. }
  2490. }
  2491. static void tspp_debugfs_exit(struct tspp_device *device)
  2492. {
  2493. if (device->dent) {
  2494. int i;
  2495. debugfs_remove_recursive(device->dent);
  2496. device->dent = NULL;
  2497. for (i = 0; i < ARRAY_SIZE(debugfs_tspp_regs); i++)
  2498. device->debugfs_regs[i] = NULL;
  2499. }
  2500. }
  2501. /* copy device-tree data to platfrom data struct */
  2502. static __devinit struct msm_tspp_platform_data *
  2503. msm_tspp_dt_to_pdata(struct platform_device *pdev)
  2504. {
  2505. struct device_node *node = pdev->dev.of_node;
  2506. struct msm_tspp_platform_data *data;
  2507. struct msm_gpio *gpios;
  2508. struct property *prop;
  2509. int i, rc;
  2510. int gpio;
  2511. u32 gpio_func;
  2512. /* Note: memory allocated by devm_kzalloc is freed automatically */
  2513. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  2514. if (!data) {
  2515. pr_err("tspp: Unable to allocate platform data\n");
  2516. return NULL;
  2517. }
  2518. rc = of_property_read_string(node, "qcom,tsif-pclk", &data->tsif_pclk);
  2519. if (rc) {
  2520. pr_err("tspp: Could not find tsif-pclk property, err = %d\n",
  2521. rc);
  2522. return NULL;
  2523. }
  2524. rc = of_property_read_string(node, "qcom,tsif-ref-clk",
  2525. &data->tsif_ref_clk);
  2526. if (rc) {
  2527. pr_err("tspp: Could not find tsif-ref-clk property, err = %d\n",
  2528. rc);
  2529. return NULL;
  2530. }
  2531. data->tsif_vreg_present = 0;
  2532. prop = of_find_property(node, "vdd_cx-supply", NULL);
  2533. if (prop)
  2534. data->tsif_vreg_present = 1;
  2535. data->num_gpios = of_gpio_count(node);
  2536. if (data->num_gpios == 0) {
  2537. pr_err("tspp: Could not find GPIO definitions\n");
  2538. return NULL;
  2539. }
  2540. gpios = devm_kzalloc(&pdev->dev,
  2541. (data->num_gpios * sizeof(struct msm_gpio)),
  2542. GFP_KERNEL);
  2543. if (!gpios) {
  2544. pr_err("tspp: Unable to allocate memory for GPIOs table\n");
  2545. return NULL;
  2546. }
  2547. /* Assuming GPIO FUNC property is the same for all GPIOs */
  2548. if (of_property_read_u32(node, "qcom,gpios-func", &gpio_func)) {
  2549. pr_err("tspp: Could not find gpios-func property\n");
  2550. return NULL;
  2551. }
  2552. for (i = 0; i < data->num_gpios; i++) {
  2553. gpio = of_get_gpio(node, i);
  2554. gpios[i].gpio_cfg = GPIO_CFG(gpio, gpio_func,
  2555. GPIO_CFG_INPUT,
  2556. GPIO_CFG_PULL_DOWN,
  2557. GPIO_CFG_2MA);
  2558. rc = of_property_read_string_index(node, "qcom,gpio-names",
  2559. i, &gpios[i].label);
  2560. if (rc)
  2561. pr_warn("tspp: Could not find gpio-names property\n");
  2562. }
  2563. data->gpios = gpios;
  2564. return data;
  2565. }
  2566. static int msm_tspp_map_irqs(struct platform_device *pdev,
  2567. struct tspp_device *device)
  2568. {
  2569. int rc;
  2570. /* get IRQ numbers from platform information */
  2571. /* map TSPP IRQ */
  2572. rc = platform_get_irq_byname(pdev, "TSIF_TSPP_IRQ");
  2573. if (rc > 0) {
  2574. device->tspp_irq = rc;
  2575. } else {
  2576. dev_err(&pdev->dev, "failed to get TSPP IRQ");
  2577. return -EINVAL;
  2578. }
  2579. /* map TSIF IRQs */
  2580. rc = platform_get_irq_byname(pdev, "TSIF0_IRQ");
  2581. if (rc > 0) {
  2582. device->tsif[0].tsif_irq = rc;
  2583. } else {
  2584. dev_err(&pdev->dev, "failed to get TSIF0 IRQ");
  2585. return -EINVAL;
  2586. }
  2587. rc = platform_get_irq_byname(pdev, "TSIF1_IRQ");
  2588. if (rc > 0) {
  2589. device->tsif[1].tsif_irq = rc;
  2590. } else {
  2591. dev_err(&pdev->dev, "failed to get TSIF1 IRQ");
  2592. return -EINVAL;
  2593. }
  2594. /* map BAM IRQ */
  2595. rc = platform_get_irq_byname(pdev, "TSIF_BAM_IRQ");
  2596. if (rc > 0) {
  2597. device->bam_irq = rc;
  2598. } else {
  2599. dev_err(&pdev->dev, "failed to get TSPP BAM IRQ");
  2600. return -EINVAL;
  2601. }
  2602. return 0;
  2603. }
  2604. static int __devinit msm_tspp_probe(struct platform_device *pdev)
  2605. {
  2606. int rc = -ENODEV;
  2607. u32 version;
  2608. u32 i, j;
  2609. struct msm_tspp_platform_data *data;
  2610. struct tspp_device *device;
  2611. struct resource *mem_tsif0;
  2612. struct resource *mem_tsif1;
  2613. struct resource *mem_tspp;
  2614. struct resource *mem_bam;
  2615. struct tspp_channel *channel;
  2616. struct msm_bus_scale_pdata *tspp_bus_pdata = NULL;
  2617. if (pdev->dev.of_node) {
  2618. /* get information from device tree */
  2619. data = msm_tspp_dt_to_pdata(pdev);
  2620. /* get device ID */
  2621. rc = of_property_read_u32(pdev->dev.of_node,
  2622. "cell-index", &pdev->id);
  2623. if (rc)
  2624. pdev->id = -1;
  2625. pdev->dev.platform_data = data;
  2626. tspp_bus_pdata = msm_bus_cl_get_pdata(pdev);
  2627. } else {
  2628. /* must have platform data */
  2629. data = pdev->dev.platform_data;
  2630. tspp_bus_pdata = NULL;
  2631. }
  2632. if (!data) {
  2633. pr_err("tspp: Platform data not available");
  2634. rc = -EINVAL;
  2635. goto out;
  2636. }
  2637. /* check for valid device id */
  2638. if ((pdev->id < 0) || (pdev->id >= TSPP_MAX_DEVICES)) {
  2639. pr_err("tspp: Invalid device ID %d", pdev->id);
  2640. rc = -EINVAL;
  2641. goto out;
  2642. }
  2643. /* OK, we will use this device */
  2644. device = kzalloc(sizeof(struct tspp_device), GFP_KERNEL);
  2645. if (!device) {
  2646. pr_err("tspp: Failed to allocate memory for device");
  2647. rc = -ENOMEM;
  2648. goto out;
  2649. }
  2650. /* set up references */
  2651. device->pdev = pdev;
  2652. platform_set_drvdata(pdev, device);
  2653. /* register bus client */
  2654. if (tspp_bus_pdata) {
  2655. device->tsif_bus_client =
  2656. msm_bus_scale_register_client(tspp_bus_pdata);
  2657. if (!device->tsif_bus_client)
  2658. pr_err("tspp: Unable to register bus client\n");
  2659. } else {
  2660. device->tsif_bus_client = 0;
  2661. }
  2662. /* map regulators */
  2663. if (data->tsif_vreg_present) {
  2664. device->tsif_vreg = devm_regulator_get(&pdev->dev, "vdd_cx");
  2665. if (IS_ERR(device->tsif_vreg)) {
  2666. rc = PTR_ERR(device->tsif_vreg);
  2667. device->tsif_vreg = NULL;
  2668. goto err_regultaor;
  2669. }
  2670. /* Set an initial voltage and enable the regulator */
  2671. rc = regulator_set_voltage(device->tsif_vreg,
  2672. RPM_REGULATOR_CORNER_NONE,
  2673. RPM_REGULATOR_CORNER_SUPER_TURBO);
  2674. if (rc) {
  2675. dev_err(&pdev->dev, "Unable to set CX voltage.\n");
  2676. goto err_regultaor;
  2677. }
  2678. rc = regulator_enable(device->tsif_vreg);
  2679. if (rc) {
  2680. dev_err(&pdev->dev, "Unable to enable CX regulator.\n");
  2681. goto err_regultaor;
  2682. }
  2683. }
  2684. /* map clocks */
  2685. if (data->tsif_pclk) {
  2686. device->tsif_pclk = clk_get(&pdev->dev, data->tsif_pclk);
  2687. if (IS_ERR(device->tsif_pclk)) {
  2688. rc = PTR_ERR(device->tsif_pclk);
  2689. device->tsif_pclk = NULL;
  2690. goto err_pclock;
  2691. }
  2692. }
  2693. if (data->tsif_ref_clk) {
  2694. device->tsif_ref_clk = clk_get(&pdev->dev, data->tsif_ref_clk);
  2695. if (IS_ERR(device->tsif_ref_clk)) {
  2696. rc = PTR_ERR(device->tsif_ref_clk);
  2697. device->tsif_ref_clk = NULL;
  2698. goto err_refclock;
  2699. }
  2700. }
  2701. /* map I/O memory */
  2702. mem_tsif0 = platform_get_resource_byname(pdev,
  2703. IORESOURCE_MEM, "MSM_TSIF0_PHYS");
  2704. if (!mem_tsif0) {
  2705. pr_err("tspp: Missing tsif0 MEM resource");
  2706. rc = -ENXIO;
  2707. goto err_res_tsif0;
  2708. }
  2709. device->tsif[0].base = ioremap(mem_tsif0->start,
  2710. resource_size(mem_tsif0));
  2711. if (!device->tsif[0].base) {
  2712. pr_err("tspp: ioremap failed");
  2713. goto err_map_tsif0;
  2714. }
  2715. mem_tsif1 = platform_get_resource_byname(pdev,
  2716. IORESOURCE_MEM, "MSM_TSIF1_PHYS");
  2717. if (!mem_tsif1) {
  2718. dev_err(&pdev->dev, "Missing tsif1 MEM resource");
  2719. rc = -ENXIO;
  2720. goto err_res_tsif1;
  2721. }
  2722. device->tsif[1].base = ioremap(mem_tsif1->start,
  2723. resource_size(mem_tsif1));
  2724. if (!device->tsif[1].base) {
  2725. dev_err(&pdev->dev, "ioremap failed");
  2726. goto err_map_tsif1;
  2727. }
  2728. mem_tspp = platform_get_resource_byname(pdev,
  2729. IORESOURCE_MEM, "MSM_TSPP_PHYS");
  2730. if (!mem_tspp) {
  2731. dev_err(&pdev->dev, "Missing MEM resource");
  2732. rc = -ENXIO;
  2733. goto err_res_dev;
  2734. }
  2735. device->base = ioremap(mem_tspp->start, resource_size(mem_tspp));
  2736. if (!device->base) {
  2737. dev_err(&pdev->dev, "ioremap failed");
  2738. goto err_map_dev;
  2739. }
  2740. mem_bam = platform_get_resource_byname(pdev,
  2741. IORESOURCE_MEM, "MSM_TSPP_BAM_PHYS");
  2742. if (!mem_bam) {
  2743. pr_err("tspp: Missing bam MEM resource");
  2744. rc = -ENXIO;
  2745. goto err_res_bam;
  2746. }
  2747. memset(&device->bam_props, 0, sizeof(device->bam_props));
  2748. device->bam_props.phys_addr = mem_bam->start;
  2749. device->bam_props.virt_addr = ioremap(mem_bam->start,
  2750. resource_size(mem_bam));
  2751. if (!device->bam_props.virt_addr) {
  2752. dev_err(&pdev->dev, "ioremap failed");
  2753. goto err_map_bam;
  2754. }
  2755. if (msm_tspp_map_irqs(pdev, device))
  2756. goto err_irq;
  2757. device->req_irqs = false;
  2758. /* power management */
  2759. pm_runtime_set_active(&pdev->dev);
  2760. pm_runtime_enable(&pdev->dev);
  2761. tspp_debugfs_init(device, 0);
  2762. for (i = 0; i < TSPP_TSIF_INSTANCES; i++)
  2763. tsif_debugfs_init(&device->tsif[i], i);
  2764. wake_lock_init(&device->wake_lock, WAKE_LOCK_SUSPEND,
  2765. dev_name(&pdev->dev));
  2766. /* set up pointers to ram-based 'registers' */
  2767. device->filters[0] = device->base + TSPP_PID_FILTER_TABLE0;
  2768. device->filters[1] = device->base + TSPP_PID_FILTER_TABLE1;
  2769. device->filters[2] = device->base + TSPP_PID_FILTER_TABLE2;
  2770. device->tspp_key_table = device->base + TSPP_DATA_KEY;
  2771. device->tspp_global_performance =
  2772. device->base + TSPP_GLOBAL_PERFORMANCE;
  2773. device->tspp_pipe_context =
  2774. device->base + TSPP_PIPE_CONTEXT;
  2775. device->tspp_pipe_performance =
  2776. device->base + TSPP_PIPE_PERFORMANCE;
  2777. device->bam_props.summing_threshold = 0x10;
  2778. device->bam_props.irq = device->bam_irq;
  2779. device->bam_props.manage = SPS_BAM_MGR_LOCAL;
  2780. if (tspp_clock_start(device) != 0) {
  2781. dev_err(&pdev->dev, "Can't start clocks");
  2782. goto err_clock;
  2783. }
  2784. device->bam_handle = SPS_DEV_HANDLE_INVALID;
  2785. spin_lock_init(&device->spinlock);
  2786. tasklet_init(&device->tlet, tspp_sps_complete_tlet,
  2787. (unsigned long)device);
  2788. /* initialize everything to a known state */
  2789. tspp_global_reset(device);
  2790. version = readl_relaxed(device->base + TSPP_VERSION);
  2791. /*
  2792. * TSPP version can be bits [7:0] or alternatively,
  2793. * TSPP major version is bits [31:28].
  2794. */
  2795. if ((version != 0x1) && (((version >> 28) & 0xF) != 0x1))
  2796. pr_warn("tspp: unrecognized hw version=%i", version);
  2797. /* initialize the channels */
  2798. for (i = 0; i < TSPP_NUM_CHANNELS; i++) {
  2799. if (tspp_channel_init(&(device->channels[i]), device) != 0) {
  2800. pr_err("tspp_channel_init failed");
  2801. goto err_channel;
  2802. }
  2803. }
  2804. /* stop the clocks for power savings */
  2805. tspp_clock_stop(device);
  2806. /* everything is ok, so add the device to the list */
  2807. list_add_tail(&(device->devlist), &tspp_devices);
  2808. return 0;
  2809. err_channel:
  2810. /* un-initialize channels */
  2811. for (j = 0; j < i; j++) {
  2812. channel = &(device->channels[i]);
  2813. device_destroy(tspp_class, channel->cdev.dev);
  2814. cdev_del(&channel->cdev);
  2815. }
  2816. sps_deregister_bam_device(device->bam_handle);
  2817. err_clock:
  2818. tspp_debugfs_exit(device);
  2819. for (i = 0; i < TSPP_TSIF_INSTANCES; i++)
  2820. tsif_debugfs_exit(&device->tsif[i]);
  2821. err_irq:
  2822. iounmap(device->bam_props.virt_addr);
  2823. err_map_bam:
  2824. err_res_bam:
  2825. iounmap(device->base);
  2826. err_map_dev:
  2827. err_res_dev:
  2828. iounmap(device->tsif[1].base);
  2829. err_map_tsif1:
  2830. err_res_tsif1:
  2831. iounmap(device->tsif[0].base);
  2832. err_map_tsif0:
  2833. err_res_tsif0:
  2834. if (device->tsif_ref_clk)
  2835. clk_put(device->tsif_ref_clk);
  2836. err_refclock:
  2837. if (device->tsif_pclk)
  2838. clk_put(device->tsif_pclk);
  2839. err_pclock:
  2840. if (device->tsif_vreg)
  2841. regulator_disable(device->tsif_vreg);
  2842. err_regultaor:
  2843. if (device->tsif_bus_client)
  2844. msm_bus_scale_unregister_client(device->tsif_bus_client);
  2845. kfree(device);
  2846. out:
  2847. return rc;
  2848. }
  2849. static int __devexit msm_tspp_remove(struct platform_device *pdev)
  2850. {
  2851. struct tspp_channel *channel;
  2852. u32 i;
  2853. struct tspp_device *device = platform_get_drvdata(pdev);
  2854. /* free the buffers, and delete the channels */
  2855. for (i = 0; i < TSPP_NUM_CHANNELS; i++) {
  2856. channel = &device->channels[i];
  2857. tspp_close_channel(device->pdev->id, i);
  2858. device_destroy(tspp_class, channel->cdev.dev);
  2859. cdev_del(&channel->cdev);
  2860. }
  2861. for (i = 0; i < TSPP_TSIF_INSTANCES; i++)
  2862. tsif_debugfs_exit(&device->tsif[i]);
  2863. if (device->tsif_bus_client)
  2864. msm_bus_scale_unregister_client(device->tsif_bus_client);
  2865. wake_lock_destroy(&device->wake_lock);
  2866. if (device->req_irqs)
  2867. msm_tspp_free_irqs(device);
  2868. iounmap(device->bam_props.virt_addr);
  2869. iounmap(device->base);
  2870. for (i = 0; i < TSPP_TSIF_INSTANCES; i++)
  2871. iounmap(device->tsif[i].base);
  2872. if (device->tsif_ref_clk)
  2873. clk_put(device->tsif_ref_clk);
  2874. if (device->tsif_pclk)
  2875. clk_put(device->tsif_pclk);
  2876. if (device->tsif_vreg)
  2877. regulator_disable(device->tsif_vreg);
  2878. pm_runtime_disable(&pdev->dev);
  2879. kfree(device);
  2880. return 0;
  2881. }
  2882. /*** power management ***/
  2883. static int tspp_runtime_suspend(struct device *dev)
  2884. {
  2885. dev_dbg(dev, "pm_runtime: suspending...");
  2886. return 0;
  2887. }
  2888. static int tspp_runtime_resume(struct device *dev)
  2889. {
  2890. dev_dbg(dev, "pm_runtime: resuming...");
  2891. return 0;
  2892. }
  2893. static const struct dev_pm_ops tspp_dev_pm_ops = {
  2894. .runtime_suspend = tspp_runtime_suspend,
  2895. .runtime_resume = tspp_runtime_resume,
  2896. };
  2897. static struct of_device_id msm_match_table[] = {
  2898. {.compatible = "qcom,msm_tspp"},
  2899. {}
  2900. };
  2901. static struct platform_driver msm_tspp_driver = {
  2902. .probe = msm_tspp_probe,
  2903. .remove = __exit_p(msm_tspp_remove),
  2904. .driver = {
  2905. .name = "msm_tspp",
  2906. .pm = &tspp_dev_pm_ops,
  2907. .of_match_table = msm_match_table,
  2908. },
  2909. };
  2910. static int __init mod_init(void)
  2911. {
  2912. int rc;
  2913. /* make the char devs (channels) */
  2914. rc = alloc_chrdev_region(&tspp_minor, 0, TSPP_NUM_CHANNELS, "tspp");
  2915. if (rc) {
  2916. pr_err("tspp: alloc_chrdev_region failed: %d", rc);
  2917. goto err_devrgn;
  2918. }
  2919. tspp_class = class_create(THIS_MODULE, "tspp");
  2920. if (IS_ERR(tspp_class)) {
  2921. rc = PTR_ERR(tspp_class);
  2922. pr_err("tspp: Error creating class: %d", rc);
  2923. goto err_class;
  2924. }
  2925. /* register the driver, and check hardware */
  2926. rc = platform_driver_register(&msm_tspp_driver);
  2927. if (rc) {
  2928. pr_err("tspp: platform_driver_register failed: %d", rc);
  2929. goto err_register;
  2930. }
  2931. return 0;
  2932. err_register:
  2933. class_destroy(tspp_class);
  2934. err_class:
  2935. unregister_chrdev_region(0, TSPP_NUM_CHANNELS);
  2936. err_devrgn:
  2937. return rc;
  2938. }
  2939. static void __exit mod_exit(void)
  2940. {
  2941. /* delete low level driver */
  2942. platform_driver_unregister(&msm_tspp_driver);
  2943. /* delete upper layer interface */
  2944. class_destroy(tspp_class);
  2945. unregister_chrdev_region(0, TSPP_NUM_CHANNELS);
  2946. }
  2947. module_init(mod_init);
  2948. module_exit(mod_exit);
  2949. MODULE_DESCRIPTION("TSPP platform device and char dev");
  2950. MODULE_LICENSE("GPL v2");