sps.c 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817
  1. /* Copyright (c) 2011-2017, 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. /* Smart-Peripheral-Switch (SPS) Module. */
  13. #include <linux/types.h> /* u32 */
  14. #include <linux/kernel.h> /* pr_info() */
  15. #include <linux/module.h> /* module_init() */
  16. #include <linux/slab.h> /* kzalloc() */
  17. #include <linux/mutex.h> /* mutex */
  18. #include <linux/device.h> /* device */
  19. #include <linux/fs.h> /* alloc_chrdev_region() */
  20. #include <linux/list.h> /* list_head */
  21. #include <linux/memory.h> /* memset */
  22. #include <linux/io.h> /* ioremap() */
  23. #include <linux/clk.h> /* clk_enable() */
  24. #include <linux/platform_device.h> /* platform_get_resource_byname() */
  25. #include <linux/debugfs.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/of.h>
  28. #include <linux/of_device.h>
  29. #include "sps_bam.h"
  30. #include "spsi.h"
  31. #include "sps_core.h"
  32. #define SPS_DRV_NAME "msm_sps" /* must match the platform_device name */
  33. /**
  34. * SPS Driver state struct
  35. */
  36. struct sps_drv {
  37. struct class *dev_class;
  38. dev_t dev_num;
  39. struct device *dev;
  40. struct clk *pmem_clk;
  41. struct clk *bamdma_clk;
  42. struct clk *dfab_clk;
  43. int is_ready;
  44. /* Platform data */
  45. phys_addr_t pipemem_phys_base;
  46. u32 pipemem_size;
  47. phys_addr_t bamdma_bam_phys_base;
  48. u32 bamdma_bam_size;
  49. phys_addr_t bamdma_dma_phys_base;
  50. u32 bamdma_dma_size;
  51. u32 bamdma_irq;
  52. u32 bamdma_restricted_pipes;
  53. /* Driver options bitflags (see SPS_OPT_*) */
  54. u32 options;
  55. /* Mutex to protect BAM and connection queues */
  56. struct mutex lock;
  57. /* BAM devices */
  58. struct list_head bams_q;
  59. char *hal_bam_version;
  60. /* Connection control state */
  61. struct sps_rm connection_ctrl;
  62. };
  63. /**
  64. * SPS driver state
  65. */
  66. static struct sps_drv *sps;
  67. u32 d_type;
  68. bool enhd_pipe;
  69. bool imem;
  70. enum sps_bam_type bam_type;
  71. enum sps_bam_type bam_types[] = {SPS_BAM_LEGACY, SPS_BAM_NDP, SPS_BAM_NDP_4K};
  72. static void sps_device_de_init(void);
  73. #ifdef CONFIG_DEBUG_FS
  74. u8 debugfs_record_enabled;
  75. u8 logging_option;
  76. u8 debug_level_option;
  77. u8 print_limit_option;
  78. u8 reg_dump_option;
  79. u32 testbus_sel;
  80. u32 bam_pipe_sel;
  81. u32 desc_option;
  82. static char *debugfs_buf;
  83. static u32 debugfs_buf_size;
  84. static u32 debugfs_buf_used;
  85. static int wraparound;
  86. static struct mutex sps_debugfs_lock;
  87. struct dentry *dent;
  88. struct dentry *dfile_info;
  89. struct dentry *dfile_logging_option;
  90. struct dentry *dfile_debug_level_option;
  91. struct dentry *dfile_print_limit_option;
  92. struct dentry *dfile_reg_dump_option;
  93. struct dentry *dfile_testbus_sel;
  94. struct dentry *dfile_bam_pipe_sel;
  95. struct dentry *dfile_desc_option;
  96. struct dentry *dfile_bam_addr;
  97. static struct sps_bam *phy2bam(phys_addr_t phys_addr);
  98. /* record debug info for debugfs */
  99. void sps_debugfs_record(const char *msg)
  100. {
  101. mutex_lock(&sps_debugfs_lock);
  102. if (debugfs_record_enabled) {
  103. if (debugfs_buf_used + MAX_MSG_LEN >= debugfs_buf_size) {
  104. debugfs_buf_used = 0;
  105. wraparound = true;
  106. }
  107. debugfs_buf_used += scnprintf(debugfs_buf + debugfs_buf_used,
  108. debugfs_buf_size - debugfs_buf_used, msg);
  109. if (wraparound)
  110. scnprintf(debugfs_buf + debugfs_buf_used,
  111. debugfs_buf_size - debugfs_buf_used,
  112. "\n**** end line of sps log ****\n\n");
  113. }
  114. mutex_unlock(&sps_debugfs_lock);
  115. }
  116. /* read the recorded debug info to userspace */
  117. static ssize_t sps_read_info(struct file *file, char __user *ubuf,
  118. size_t count, loff_t *ppos)
  119. {
  120. int ret = 0;
  121. int size;
  122. mutex_lock(&sps_debugfs_lock);
  123. if (debugfs_record_enabled) {
  124. if (wraparound)
  125. size = debugfs_buf_size - MAX_MSG_LEN;
  126. else
  127. size = debugfs_buf_used;
  128. ret = simple_read_from_buffer(ubuf, count, ppos,
  129. debugfs_buf, size);
  130. }
  131. mutex_unlock(&sps_debugfs_lock);
  132. return ret;
  133. }
  134. /*
  135. * set the buffer size (in KB) for debug info
  136. */
  137. static ssize_t sps_set_info(struct file *file, const char __user *buf,
  138. size_t count, loff_t *ppos)
  139. {
  140. unsigned long missing;
  141. char str[MAX_MSG_LEN];
  142. int i;
  143. u32 buf_size_kb = 0;
  144. u32 new_buf_size;
  145. memset(str, 0, sizeof(str));
  146. missing = copy_from_user(str, buf, sizeof(str));
  147. if (missing)
  148. return -EFAULT;
  149. for (i = 0; i < sizeof(str) && (str[i] >= '0') && (str[i] <= '9'); ++i)
  150. buf_size_kb = (buf_size_kb * 10) + (str[i] - '0');
  151. pr_info("sps:debugfs: input buffer size is %dKB\n", buf_size_kb);
  152. if ((logging_option == 0) || (logging_option == 2)) {
  153. pr_info("sps:debugfs: need to first turn on recording.\n");
  154. return -EFAULT;
  155. }
  156. if (buf_size_kb < 1) {
  157. pr_info("sps:debugfs: buffer size should be "
  158. "no less than 1KB.\n");
  159. return -EFAULT;
  160. }
  161. if (buf_size_kb > (INT_MAX/SZ_1K)) {
  162. pr_err("sps:debugfs: buffer size is too large\n");
  163. return -EFAULT;
  164. }
  165. new_buf_size = buf_size_kb * SZ_1K;
  166. mutex_lock(&sps_debugfs_lock);
  167. if (debugfs_record_enabled) {
  168. if (debugfs_buf_size == new_buf_size) {
  169. /* need do nothing */
  170. pr_info("sps:debugfs: input buffer size "
  171. "is the same as before.\n");
  172. mutex_unlock(&sps_debugfs_lock);
  173. return count;
  174. } else {
  175. /* release the current buffer */
  176. debugfs_record_enabled = false;
  177. debugfs_buf_used = 0;
  178. wraparound = false;
  179. kfree(debugfs_buf);
  180. debugfs_buf = NULL;
  181. }
  182. }
  183. /* allocate new buffer */
  184. debugfs_buf_size = new_buf_size;
  185. debugfs_buf = kzalloc(sizeof(char) * debugfs_buf_size,
  186. GFP_KERNEL);
  187. if (!debugfs_buf) {
  188. debugfs_buf_size = 0;
  189. pr_err("sps:fail to allocate memory for debug_fs.\n");
  190. mutex_unlock(&sps_debugfs_lock);
  191. return -ENOMEM;
  192. }
  193. debugfs_buf_used = 0;
  194. wraparound = false;
  195. debugfs_record_enabled = true;
  196. mutex_unlock(&sps_debugfs_lock);
  197. return count;
  198. }
  199. const struct file_operations sps_info_ops = {
  200. .read = sps_read_info,
  201. .write = sps_set_info,
  202. };
  203. /* return the current logging option to userspace */
  204. static ssize_t sps_read_logging_option(struct file *file, char __user *ubuf,
  205. size_t count, loff_t *ppos)
  206. {
  207. char value[MAX_MSG_LEN];
  208. int nbytes;
  209. nbytes = snprintf(value, MAX_MSG_LEN, "%d\n", logging_option);
  210. return simple_read_from_buffer(ubuf, count, ppos, value, nbytes);
  211. }
  212. /*
  213. * set the logging option
  214. */
  215. static ssize_t sps_set_logging_option(struct file *file, const char __user *buf,
  216. size_t count, loff_t *ppos)
  217. {
  218. unsigned long missing;
  219. char str[MAX_MSG_LEN];
  220. int i;
  221. u8 option = 0;
  222. memset(str, 0, sizeof(str));
  223. missing = copy_from_user(str, buf, sizeof(str));
  224. if (missing)
  225. return -EFAULT;
  226. for (i = 0; i < sizeof(str) && (str[i] >= '0') && (str[i] <= '9'); ++i)
  227. option = (option * 10) + (str[i] - '0');
  228. pr_info("sps:debugfs: try to change logging option to %d\n", option);
  229. if (option > 3) {
  230. pr_err("sps:debugfs: invalid logging option:%d\n", option);
  231. return count;
  232. }
  233. mutex_lock(&sps_debugfs_lock);
  234. if (((option == 0) || (option == 2)) &&
  235. ((logging_option == 1) || (logging_option == 3))) {
  236. debugfs_record_enabled = false;
  237. kfree(debugfs_buf);
  238. debugfs_buf = NULL;
  239. debugfs_buf_used = 0;
  240. debugfs_buf_size = 0;
  241. wraparound = false;
  242. }
  243. logging_option = option;
  244. mutex_unlock(&sps_debugfs_lock);
  245. return count;
  246. }
  247. const struct file_operations sps_logging_option_ops = {
  248. .read = sps_read_logging_option,
  249. .write = sps_set_logging_option,
  250. };
  251. /*
  252. * input the bam physical address
  253. */
  254. static ssize_t sps_set_bam_addr(struct file *file, const char __user *buf,
  255. size_t count, loff_t *ppos)
  256. {
  257. unsigned long missing;
  258. char str[MAX_MSG_LEN];
  259. u32 i;
  260. u32 bam_addr = 0;
  261. struct sps_bam *bam;
  262. u32 num_pipes = 0;
  263. void *vir_addr;
  264. memset(str, 0, sizeof(str));
  265. missing = copy_from_user(str, buf, sizeof(str));
  266. if (missing)
  267. return -EFAULT;
  268. for (i = 0; i < sizeof(str) && (str[i] >= '0') && (str[i] <= '9'); ++i)
  269. bam_addr = (bam_addr * 10) + (str[i] - '0');
  270. pr_info("sps:debugfs:input BAM physical address:0x%x\n", bam_addr);
  271. bam = phy2bam(bam_addr);
  272. if (bam == NULL) {
  273. pr_err("sps:debugfs:BAM 0x%x is not registered.", bam_addr);
  274. return count;
  275. } else {
  276. vir_addr = bam->base;
  277. num_pipes = bam->props.num_pipes;
  278. }
  279. switch (reg_dump_option) {
  280. case 1: /* output all registers of this BAM */
  281. print_bam_reg(vir_addr);
  282. for (i = 0; i < num_pipes; i++)
  283. print_bam_pipe_reg(vir_addr, i);
  284. break;
  285. case 2: /* output BAM-level registers */
  286. print_bam_reg(vir_addr);
  287. break;
  288. case 3: /* output selected BAM-level registers */
  289. print_bam_selected_reg(vir_addr, bam->props.ee);
  290. break;
  291. case 4: /* output selected registers of all pipes */
  292. for (i = 0; i < num_pipes; i++)
  293. print_bam_pipe_selected_reg(vir_addr, i);
  294. break;
  295. case 5: /* output selected registers of selected pipes */
  296. for (i = 0; i < num_pipes; i++)
  297. if (bam_pipe_sel & (1UL << i))
  298. print_bam_pipe_selected_reg(vir_addr, i);
  299. break;
  300. case 6: /* output selected registers of typical pipes */
  301. print_bam_pipe_selected_reg(vir_addr, 4);
  302. print_bam_pipe_selected_reg(vir_addr, 5);
  303. break;
  304. case 7: /* output desc FIFO of all pipes */
  305. for (i = 0; i < num_pipes; i++)
  306. print_bam_pipe_desc_fifo(vir_addr, i, 0);
  307. break;
  308. case 8: /* output desc FIFO of selected pipes */
  309. for (i = 0; i < num_pipes; i++)
  310. if (bam_pipe_sel & (1UL << i))
  311. print_bam_pipe_desc_fifo(vir_addr, i, 0);
  312. break;
  313. case 9: /* output desc FIFO of typical pipes */
  314. print_bam_pipe_desc_fifo(vir_addr, 4, 0);
  315. print_bam_pipe_desc_fifo(vir_addr, 5, 0);
  316. break;
  317. case 10: /* output selected registers and desc FIFO of all pipes */
  318. for (i = 0; i < num_pipes; i++) {
  319. print_bam_pipe_selected_reg(vir_addr, i);
  320. print_bam_pipe_desc_fifo(vir_addr, i, 0);
  321. }
  322. break;
  323. case 11: /* output selected registers and desc FIFO of selected pipes */
  324. for (i = 0; i < num_pipes; i++)
  325. if (bam_pipe_sel & (1UL << i)) {
  326. print_bam_pipe_selected_reg(vir_addr, i);
  327. print_bam_pipe_desc_fifo(vir_addr, i, 0);
  328. }
  329. break;
  330. case 12: /* output selected registers and desc FIFO of typical pipes */
  331. print_bam_pipe_selected_reg(vir_addr, 4);
  332. print_bam_pipe_desc_fifo(vir_addr, 4, 0);
  333. print_bam_pipe_selected_reg(vir_addr, 5);
  334. print_bam_pipe_desc_fifo(vir_addr, 5, 0);
  335. break;
  336. case 13: /* output BAM_TEST_BUS_REG */
  337. if (testbus_sel)
  338. print_bam_test_bus_reg(vir_addr, testbus_sel);
  339. else {
  340. pr_info("sps:output TEST_BUS_REG for all TEST_BUS_SEL");
  341. print_bam_test_bus_reg(vir_addr, testbus_sel);
  342. }
  343. break;
  344. case 14: /* output partial desc FIFO of selected pipes */
  345. if (desc_option == 0)
  346. desc_option = 1;
  347. for (i = 0; i < num_pipes; i++)
  348. if (bam_pipe_sel & (1UL << i))
  349. print_bam_pipe_desc_fifo(vir_addr, i,
  350. desc_option);
  351. break;
  352. case 15: /* output partial data blocks of descriptors */
  353. for (i = 0; i < num_pipes; i++)
  354. if (bam_pipe_sel & (1UL << i))
  355. print_bam_pipe_desc_fifo(vir_addr, i, 100);
  356. break;
  357. case 16: /* output all registers of selected pipes */
  358. for (i = 0; i < num_pipes; i++)
  359. if (bam_pipe_sel & (1UL << i))
  360. print_bam_pipe_reg(vir_addr, i);
  361. break;
  362. case 91: /* output testbus register, BAM global regisers
  363. and registers of all pipes */
  364. print_bam_test_bus_reg(vir_addr, testbus_sel);
  365. print_bam_selected_reg(vir_addr, bam->props.ee);
  366. for (i = 0; i < num_pipes; i++)
  367. print_bam_pipe_selected_reg(vir_addr, i);
  368. break;
  369. case 92: /* output testbus register, BAM global regisers
  370. and registers of selected pipes */
  371. print_bam_test_bus_reg(vir_addr, testbus_sel);
  372. print_bam_selected_reg(vir_addr, bam->props.ee);
  373. for (i = 0; i < num_pipes; i++)
  374. if (bam_pipe_sel & (1UL << i))
  375. print_bam_pipe_selected_reg(vir_addr, i);
  376. break;
  377. case 93: /* output registers and partial desc FIFOs
  378. of selected pipes: format 1 */
  379. if (desc_option == 0)
  380. desc_option = 1;
  381. print_bam_test_bus_reg(vir_addr, testbus_sel);
  382. print_bam_selected_reg(vir_addr, bam->props.ee);
  383. for (i = 0; i < num_pipes; i++)
  384. if (bam_pipe_sel & (1UL << i))
  385. print_bam_pipe_selected_reg(vir_addr, i);
  386. for (i = 0; i < num_pipes; i++)
  387. if (bam_pipe_sel & (1UL << i))
  388. print_bam_pipe_desc_fifo(vir_addr, i,
  389. desc_option);
  390. break;
  391. case 94: /* output registers and partial desc FIFOs
  392. of selected pipes: format 2 */
  393. if (desc_option == 0)
  394. desc_option = 1;
  395. print_bam_test_bus_reg(vir_addr, testbus_sel);
  396. print_bam_selected_reg(vir_addr, bam->props.ee);
  397. for (i = 0; i < num_pipes; i++)
  398. if (bam_pipe_sel & (1UL << i)) {
  399. print_bam_pipe_selected_reg(vir_addr, i);
  400. print_bam_pipe_desc_fifo(vir_addr, i,
  401. desc_option);
  402. }
  403. break;
  404. case 95: /* output registers and desc FIFOs
  405. of selected pipes: format 1 */
  406. print_bam_test_bus_reg(vir_addr, testbus_sel);
  407. print_bam_selected_reg(vir_addr, bam->props.ee);
  408. for (i = 0; i < num_pipes; i++)
  409. if (bam_pipe_sel & (1UL << i))
  410. print_bam_pipe_selected_reg(vir_addr, i);
  411. for (i = 0; i < num_pipes; i++)
  412. if (bam_pipe_sel & (1UL << i))
  413. print_bam_pipe_desc_fifo(vir_addr, i, 0);
  414. break;
  415. case 96: /* output registers and desc FIFOs
  416. of selected pipes: format 2 */
  417. print_bam_test_bus_reg(vir_addr, testbus_sel);
  418. print_bam_selected_reg(vir_addr, bam->props.ee);
  419. for (i = 0; i < num_pipes; i++)
  420. if (bam_pipe_sel & (1UL << i)) {
  421. print_bam_pipe_selected_reg(vir_addr, i);
  422. print_bam_pipe_desc_fifo(vir_addr, i, 0);
  423. }
  424. break;
  425. case 97: /* output registers, desc FIFOs and partial data blocks
  426. of selected pipes: format 1 */
  427. print_bam_test_bus_reg(vir_addr, testbus_sel);
  428. print_bam_selected_reg(vir_addr, bam->props.ee);
  429. for (i = 0; i < num_pipes; i++)
  430. if (bam_pipe_sel & (1UL << i))
  431. print_bam_pipe_selected_reg(vir_addr, i);
  432. for (i = 0; i < num_pipes; i++)
  433. if (bam_pipe_sel & (1UL << i))
  434. print_bam_pipe_desc_fifo(vir_addr, i, 0);
  435. for (i = 0; i < num_pipes; i++)
  436. if (bam_pipe_sel & (1UL << i))
  437. print_bam_pipe_desc_fifo(vir_addr, i, 100);
  438. break;
  439. case 98: /* output registers, desc FIFOs and partial data blocks
  440. of selected pipes: format 2 */
  441. print_bam_test_bus_reg(vir_addr, testbus_sel);
  442. print_bam_selected_reg(vir_addr, bam->props.ee);
  443. for (i = 0; i < num_pipes; i++)
  444. if (bam_pipe_sel & (1UL << i)) {
  445. print_bam_pipe_selected_reg(vir_addr, i);
  446. print_bam_pipe_desc_fifo(vir_addr, i, 0);
  447. print_bam_pipe_desc_fifo(vir_addr, i, 100);
  448. }
  449. break;
  450. case 99: /* output all registers, desc FIFOs and partial data blocks */
  451. print_bam_test_bus_reg(vir_addr, testbus_sel);
  452. print_bam_reg(vir_addr);
  453. for (i = 0; i < num_pipes; i++)
  454. print_bam_pipe_reg(vir_addr, i);
  455. print_bam_selected_reg(vir_addr, bam->props.ee);
  456. for (i = 0; i < num_pipes; i++)
  457. print_bam_pipe_selected_reg(vir_addr, i);
  458. for (i = 0; i < num_pipes; i++)
  459. print_bam_pipe_desc_fifo(vir_addr, i, 0);
  460. for (i = 0; i < num_pipes; i++)
  461. print_bam_pipe_desc_fifo(vir_addr, i, 100);
  462. break;
  463. default:
  464. pr_info("sps:no dump option is chosen yet.");
  465. }
  466. return count;
  467. }
  468. const struct file_operations sps_bam_addr_ops = {
  469. .write = sps_set_bam_addr,
  470. };
  471. static void sps_debugfs_init(void)
  472. {
  473. debugfs_record_enabled = false;
  474. logging_option = 0;
  475. debug_level_option = 0;
  476. print_limit_option = 0;
  477. reg_dump_option = 0;
  478. testbus_sel = 0;
  479. bam_pipe_sel = 0;
  480. desc_option = 0;
  481. debugfs_buf_size = 0;
  482. debugfs_buf_used = 0;
  483. wraparound = false;
  484. dent = debugfs_create_dir("sps", 0);
  485. if (IS_ERR(dent)) {
  486. pr_err("sps:fail to create the folder for debug_fs.\n");
  487. return;
  488. }
  489. dfile_info = debugfs_create_file("info", 0664, dent, 0,
  490. &sps_info_ops);
  491. if (!dfile_info || IS_ERR(dfile_info)) {
  492. pr_err("sps:fail to create the file for debug_fs info.\n");
  493. goto info_err;
  494. }
  495. dfile_logging_option = debugfs_create_file("logging_option", 0664,
  496. dent, 0, &sps_logging_option_ops);
  497. if (!dfile_logging_option || IS_ERR(dfile_logging_option)) {
  498. pr_err("sps:fail to create the file for debug_fs "
  499. "logging_option.\n");
  500. goto logging_option_err;
  501. }
  502. dfile_debug_level_option = debugfs_create_u8("debug_level_option",
  503. 0664, dent, &debug_level_option);
  504. if (!dfile_debug_level_option || IS_ERR(dfile_debug_level_option)) {
  505. pr_err("sps:fail to create the file for debug_fs "
  506. "debug_level_option.\n");
  507. goto debug_level_option_err;
  508. }
  509. dfile_print_limit_option = debugfs_create_u8("print_limit_option",
  510. 0664, dent, &print_limit_option);
  511. if (!dfile_print_limit_option || IS_ERR(dfile_print_limit_option)) {
  512. pr_err("sps:fail to create the file for debug_fs "
  513. "print_limit_option.\n");
  514. goto print_limit_option_err;
  515. }
  516. dfile_reg_dump_option = debugfs_create_u8("reg_dump_option", 0664,
  517. dent, &reg_dump_option);
  518. if (!dfile_reg_dump_option || IS_ERR(dfile_reg_dump_option)) {
  519. pr_err("sps:fail to create the file for debug_fs "
  520. "reg_dump_option.\n");
  521. goto reg_dump_option_err;
  522. }
  523. dfile_testbus_sel = debugfs_create_u32("testbus_sel", 0664,
  524. dent, &testbus_sel);
  525. if (!dfile_testbus_sel || IS_ERR(dfile_testbus_sel)) {
  526. pr_err("sps:fail to create debug_fs file for testbus_sel.\n");
  527. goto testbus_sel_err;
  528. }
  529. dfile_bam_pipe_sel = debugfs_create_u32("bam_pipe_sel", 0664,
  530. dent, &bam_pipe_sel);
  531. if (!dfile_bam_pipe_sel || IS_ERR(dfile_bam_pipe_sel)) {
  532. pr_err("sps:fail to create debug_fs file for bam_pipe_sel.\n");
  533. goto bam_pipe_sel_err;
  534. }
  535. dfile_desc_option = debugfs_create_u32("desc_option", 0664,
  536. dent, &desc_option);
  537. if (!dfile_desc_option || IS_ERR(dfile_desc_option)) {
  538. pr_err("sps:fail to create debug_fs file for desc_option.\n");
  539. goto desc_option_err;
  540. }
  541. dfile_bam_addr = debugfs_create_file("bam_addr", 0664,
  542. dent, 0, &sps_bam_addr_ops);
  543. if (!dfile_bam_addr || IS_ERR(dfile_bam_addr)) {
  544. pr_err("sps:fail to create the file for debug_fs "
  545. "bam_addr.\n");
  546. goto bam_addr_err;
  547. }
  548. mutex_init(&sps_debugfs_lock);
  549. return;
  550. bam_addr_err:
  551. debugfs_remove(dfile_desc_option);
  552. desc_option_err:
  553. debugfs_remove(dfile_bam_pipe_sel);
  554. bam_pipe_sel_err:
  555. debugfs_remove(dfile_testbus_sel);
  556. testbus_sel_err:
  557. debugfs_remove(dfile_reg_dump_option);
  558. reg_dump_option_err:
  559. debugfs_remove(dfile_print_limit_option);
  560. print_limit_option_err:
  561. debugfs_remove(dfile_debug_level_option);
  562. debug_level_option_err:
  563. debugfs_remove(dfile_logging_option);
  564. logging_option_err:
  565. debugfs_remove(dfile_info);
  566. info_err:
  567. debugfs_remove(dent);
  568. }
  569. static void sps_debugfs_exit(void)
  570. {
  571. if (dfile_info)
  572. debugfs_remove(dfile_info);
  573. if (dfile_logging_option)
  574. debugfs_remove(dfile_logging_option);
  575. if (dfile_debug_level_option)
  576. debugfs_remove(dfile_debug_level_option);
  577. if (dfile_print_limit_option)
  578. debugfs_remove(dfile_print_limit_option);
  579. if (dfile_reg_dump_option)
  580. debugfs_remove(dfile_reg_dump_option);
  581. if (dfile_testbus_sel)
  582. debugfs_remove(dfile_testbus_sel);
  583. if (dfile_bam_pipe_sel)
  584. debugfs_remove(dfile_bam_pipe_sel);
  585. if (dfile_desc_option)
  586. debugfs_remove(dfile_desc_option);
  587. if (dfile_bam_addr)
  588. debugfs_remove(dfile_bam_addr);
  589. if (dent)
  590. debugfs_remove(dent);
  591. kfree(debugfs_buf);
  592. debugfs_buf = NULL;
  593. }
  594. #endif
  595. /* Get the debug info of BAM registers and descriptor FIFOs */
  596. int sps_get_bam_debug_info(unsigned long dev, u32 option, u32 para,
  597. u32 tb_sel, u32 desc_sel)
  598. {
  599. int res = 0;
  600. struct sps_bam *bam;
  601. u32 i;
  602. u32 num_pipes = 0;
  603. void *vir_addr;
  604. if (dev == 0) {
  605. SPS_ERR("sps:%s:device handle should not be 0.\n", __func__);
  606. return SPS_ERROR;
  607. }
  608. if (sps == NULL || !sps->is_ready) {
  609. SPS_DBG2("sps:%s:sps driver is not ready.\n", __func__);
  610. return -EPROBE_DEFER;
  611. }
  612. mutex_lock(&sps->lock);
  613. /* Search for the target BAM device */
  614. bam = sps_h2bam(dev);
  615. if (bam == NULL) {
  616. pr_err("sps:Can't find any BAM with handle 0x%lx.", dev);
  617. mutex_unlock(&sps->lock);
  618. return SPS_ERROR;
  619. }
  620. mutex_unlock(&sps->lock);
  621. vir_addr = bam->base;
  622. num_pipes = bam->props.num_pipes;
  623. SPS_INFO("sps:<bam-addr> dump BAM:%pa.\n", &bam->props.phys_addr);
  624. switch (option) {
  625. case 1: /* output all registers of this BAM */
  626. print_bam_reg(vir_addr);
  627. for (i = 0; i < num_pipes; i++)
  628. print_bam_pipe_reg(vir_addr, i);
  629. break;
  630. case 2: /* output BAM-level registers */
  631. print_bam_reg(vir_addr);
  632. break;
  633. case 3: /* output selected BAM-level registers */
  634. print_bam_selected_reg(vir_addr, bam->props.ee);
  635. break;
  636. case 4: /* output selected registers of all pipes */
  637. for (i = 0; i < num_pipes; i++)
  638. print_bam_pipe_selected_reg(vir_addr, i);
  639. break;
  640. case 5: /* output selected registers of selected pipes */
  641. for (i = 0; i < num_pipes; i++)
  642. if (para & (1UL << i))
  643. print_bam_pipe_selected_reg(vir_addr, i);
  644. break;
  645. case 6: /* output selected registers of typical pipes */
  646. print_bam_pipe_selected_reg(vir_addr, 4);
  647. print_bam_pipe_selected_reg(vir_addr, 5);
  648. break;
  649. case 7: /* output desc FIFO of all pipes */
  650. for (i = 0; i < num_pipes; i++)
  651. print_bam_pipe_desc_fifo(vir_addr, i, 0);
  652. break;
  653. case 8: /* output desc FIFO of selected pipes */
  654. for (i = 0; i < num_pipes; i++)
  655. if (para & (1UL << i))
  656. print_bam_pipe_desc_fifo(vir_addr, i, 0);
  657. break;
  658. case 9: /* output desc FIFO of typical pipes */
  659. print_bam_pipe_desc_fifo(vir_addr, 4, 0);
  660. print_bam_pipe_desc_fifo(vir_addr, 5, 0);
  661. break;
  662. case 10: /* output selected registers and desc FIFO of all pipes */
  663. for (i = 0; i < num_pipes; i++) {
  664. print_bam_pipe_selected_reg(vir_addr, i);
  665. print_bam_pipe_desc_fifo(vir_addr, i, 0);
  666. }
  667. break;
  668. case 11: /* output selected registers and desc FIFO of selected pipes */
  669. for (i = 0; i < num_pipes; i++)
  670. if (para & (1UL << i)) {
  671. print_bam_pipe_selected_reg(vir_addr, i);
  672. print_bam_pipe_desc_fifo(vir_addr, i, 0);
  673. }
  674. break;
  675. case 12: /* output selected registers and desc FIFO of typical pipes */
  676. print_bam_pipe_selected_reg(vir_addr, 4);
  677. print_bam_pipe_desc_fifo(vir_addr, 4, 0);
  678. print_bam_pipe_selected_reg(vir_addr, 5);
  679. print_bam_pipe_desc_fifo(vir_addr, 5, 0);
  680. break;
  681. case 13: /* output BAM_TEST_BUS_REG */
  682. if (tb_sel)
  683. print_bam_test_bus_reg(vir_addr, tb_sel);
  684. else
  685. pr_info("sps:TEST_BUS_SEL should NOT be zero.");
  686. break;
  687. case 14: /* output partial desc FIFO of selected pipes */
  688. if (desc_sel == 0)
  689. desc_sel = 1;
  690. for (i = 0; i < num_pipes; i++)
  691. if (para & (1UL << i))
  692. print_bam_pipe_desc_fifo(vir_addr, i,
  693. desc_sel);
  694. break;
  695. case 15: /* output partial data blocks of descriptors */
  696. for (i = 0; i < num_pipes; i++)
  697. if (para & (1UL << i))
  698. print_bam_pipe_desc_fifo(vir_addr, i, 100);
  699. break;
  700. case 16: /* output all registers of selected pipes */
  701. for (i = 0; i < num_pipes; i++)
  702. if (para & (1UL << i))
  703. print_bam_pipe_reg(vir_addr, i);
  704. break;
  705. case 91: /* output testbus register, BAM global regisers
  706. and registers of all pipes */
  707. print_bam_test_bus_reg(vir_addr, tb_sel);
  708. print_bam_selected_reg(vir_addr, bam->props.ee);
  709. for (i = 0; i < num_pipes; i++)
  710. print_bam_pipe_selected_reg(vir_addr, i);
  711. break;
  712. case 92: /* output testbus register, BAM global regisers
  713. and registers of selected pipes */
  714. print_bam_test_bus_reg(vir_addr, tb_sel);
  715. print_bam_selected_reg(vir_addr, bam->props.ee);
  716. for (i = 0; i < num_pipes; i++)
  717. if (para & (1UL << i))
  718. print_bam_pipe_selected_reg(vir_addr, i);
  719. break;
  720. case 93: /* output registers and partial desc FIFOs
  721. of selected pipes: format 1 */
  722. if (desc_sel == 0)
  723. desc_sel = 1;
  724. print_bam_test_bus_reg(vir_addr, tb_sel);
  725. print_bam_selected_reg(vir_addr, bam->props.ee);
  726. for (i = 0; i < num_pipes; i++)
  727. if (para & (1UL << i))
  728. print_bam_pipe_selected_reg(vir_addr, i);
  729. for (i = 0; i < num_pipes; i++)
  730. if (para & (1UL << i))
  731. print_bam_pipe_desc_fifo(vir_addr, i,
  732. desc_sel);
  733. break;
  734. case 94: /* output registers and partial desc FIFOs
  735. of selected pipes: format 2 */
  736. if (desc_sel == 0)
  737. desc_sel = 1;
  738. print_bam_test_bus_reg(vir_addr, tb_sel);
  739. print_bam_selected_reg(vir_addr, bam->props.ee);
  740. for (i = 0; i < num_pipes; i++)
  741. if (para & (1UL << i)) {
  742. print_bam_pipe_selected_reg(vir_addr, i);
  743. print_bam_pipe_desc_fifo(vir_addr, i,
  744. desc_sel);
  745. }
  746. break;
  747. case 95: /* output registers and desc FIFOs
  748. of selected pipes: format 1 */
  749. print_bam_test_bus_reg(vir_addr, tb_sel);
  750. print_bam_selected_reg(vir_addr, bam->props.ee);
  751. for (i = 0; i < num_pipes; i++)
  752. if (para & (1UL << i))
  753. print_bam_pipe_selected_reg(vir_addr, i);
  754. for (i = 0; i < num_pipes; i++)
  755. if (para & (1UL << i))
  756. print_bam_pipe_desc_fifo(vir_addr, i, 0);
  757. break;
  758. case 96: /* output registers and desc FIFOs
  759. of selected pipes: format 2 */
  760. print_bam_test_bus_reg(vir_addr, tb_sel);
  761. print_bam_selected_reg(vir_addr, bam->props.ee);
  762. for (i = 0; i < num_pipes; i++)
  763. if (para & (1UL << i)) {
  764. print_bam_pipe_selected_reg(vir_addr, i);
  765. print_bam_pipe_desc_fifo(vir_addr, i, 0);
  766. }
  767. break;
  768. case 97: /* output registers, desc FIFOs and partial data blocks
  769. of selected pipes: format 1 */
  770. print_bam_test_bus_reg(vir_addr, tb_sel);
  771. print_bam_selected_reg(vir_addr, bam->props.ee);
  772. for (i = 0; i < num_pipes; i++)
  773. if (para & (1UL << i))
  774. print_bam_pipe_selected_reg(vir_addr, i);
  775. for (i = 0; i < num_pipes; i++)
  776. if (para & (1UL << i))
  777. print_bam_pipe_desc_fifo(vir_addr, i, 0);
  778. for (i = 0; i < num_pipes; i++)
  779. if (para & (1UL << i))
  780. print_bam_pipe_desc_fifo(vir_addr, i, 100);
  781. break;
  782. case 98: /* output registers, desc FIFOs and partial data blocks
  783. of selected pipes: format 2 */
  784. print_bam_test_bus_reg(vir_addr, tb_sel);
  785. print_bam_selected_reg(vir_addr, bam->props.ee);
  786. for (i = 0; i < num_pipes; i++)
  787. if (para & (1UL << i)) {
  788. print_bam_pipe_selected_reg(vir_addr, i);
  789. print_bam_pipe_desc_fifo(vir_addr, i, 0);
  790. print_bam_pipe_desc_fifo(vir_addr, i, 100);
  791. }
  792. break;
  793. case 99: /* output all registers, desc FIFOs and partial data blocks */
  794. print_bam_test_bus_reg(vir_addr, tb_sel);
  795. print_bam_reg(vir_addr);
  796. for (i = 0; i < num_pipes; i++)
  797. print_bam_pipe_reg(vir_addr, i);
  798. print_bam_selected_reg(vir_addr, bam->props.ee);
  799. for (i = 0; i < num_pipes; i++)
  800. print_bam_pipe_selected_reg(vir_addr, i);
  801. for (i = 0; i < num_pipes; i++)
  802. print_bam_pipe_desc_fifo(vir_addr, i, 0);
  803. for (i = 0; i < num_pipes; i++)
  804. print_bam_pipe_desc_fifo(vir_addr, i, 100);
  805. break;
  806. default:
  807. pr_info("sps:no option is chosen yet.");
  808. }
  809. return res;
  810. }
  811. EXPORT_SYMBOL(sps_get_bam_debug_info);
  812. /**
  813. * Initialize SPS device
  814. *
  815. * This function initializes the SPS device.
  816. *
  817. * @return 0 on success, negative value on error
  818. *
  819. */
  820. static int sps_device_init(void)
  821. {
  822. int result;
  823. int success;
  824. #ifdef CONFIG_SPS_SUPPORT_BAMDMA
  825. struct sps_bam_props bamdma_props = {0};
  826. #endif
  827. SPS_DBG2("sps:%s.", __func__);
  828. success = false;
  829. result = sps_mem_init(sps->pipemem_phys_base, sps->pipemem_size);
  830. if (result) {
  831. SPS_ERR("sps:SPS memory init failed");
  832. goto exit_err;
  833. }
  834. INIT_LIST_HEAD(&sps->bams_q);
  835. mutex_init(&sps->lock);
  836. if (sps_rm_init(&sps->connection_ctrl, sps->options)) {
  837. SPS_ERR("sps:Fail to init SPS resource manager");
  838. goto exit_err;
  839. }
  840. result = sps_bam_driver_init(sps->options);
  841. if (result) {
  842. SPS_ERR("sps:SPS BAM driver init failed");
  843. goto exit_err;
  844. }
  845. /* Initialize the BAM DMA device */
  846. #ifdef CONFIG_SPS_SUPPORT_BAMDMA
  847. bamdma_props.phys_addr = sps->bamdma_bam_phys_base;
  848. bamdma_props.virt_addr = ioremap(sps->bamdma_bam_phys_base,
  849. sps->bamdma_bam_size);
  850. if (!bamdma_props.virt_addr) {
  851. SPS_ERR("sps:Fail to IO map BAM-DMA BAM registers.\n");
  852. goto exit_err;
  853. }
  854. SPS_DBG2("sps:bamdma_bam.phys=%pa.virt=0x%p.",
  855. &bamdma_props.phys_addr,
  856. bamdma_props.virt_addr);
  857. bamdma_props.periph_phys_addr = sps->bamdma_dma_phys_base;
  858. bamdma_props.periph_virt_size = sps->bamdma_dma_size;
  859. bamdma_props.periph_virt_addr = ioremap(sps->bamdma_dma_phys_base,
  860. sps->bamdma_dma_size);
  861. if (!bamdma_props.periph_virt_addr) {
  862. SPS_ERR("sps:Fail to IO map BAM-DMA peripheral reg.\n");
  863. goto exit_err;
  864. }
  865. SPS_DBG2("sps:bamdma_dma.phys=%pa.virt=0x%p.",
  866. &bamdma_props.periph_phys_addr,
  867. bamdma_props.periph_virt_addr);
  868. bamdma_props.irq = sps->bamdma_irq;
  869. bamdma_props.event_threshold = 0x10; /* Pipe event threshold */
  870. bamdma_props.summing_threshold = 0x10; /* BAM event threshold */
  871. bamdma_props.options = SPS_BAM_OPT_BAMDMA;
  872. bamdma_props.restricted_pipes = sps->bamdma_restricted_pipes;
  873. result = sps_dma_init(&bamdma_props);
  874. if (result) {
  875. SPS_ERR("sps:SPS BAM DMA driver init failed");
  876. goto exit_err;
  877. }
  878. #endif /* CONFIG_SPS_SUPPORT_BAMDMA */
  879. result = sps_map_init(NULL, sps->options);
  880. if (result) {
  881. SPS_ERR("sps:SPS connection mapping init failed");
  882. goto exit_err;
  883. }
  884. success = true;
  885. exit_err:
  886. if (!success) {
  887. #ifdef CONFIG_SPS_SUPPORT_BAMDMA
  888. sps_device_de_init();
  889. #endif
  890. return SPS_ERROR;
  891. }
  892. return 0;
  893. }
  894. /**
  895. * De-initialize SPS device
  896. *
  897. * This function de-initializes the SPS device.
  898. *
  899. * @return 0 on success, negative value on error
  900. *
  901. */
  902. static void sps_device_de_init(void)
  903. {
  904. SPS_DBG2("sps:%s.", __func__);
  905. if (sps != NULL) {
  906. #ifdef CONFIG_SPS_SUPPORT_BAMDMA
  907. sps_dma_de_init();
  908. #endif
  909. /* Are there any remaining BAM registrations? */
  910. if (!list_empty(&sps->bams_q))
  911. SPS_ERR("sps:SPS de-init: BAMs are still registered");
  912. sps_map_de_init();
  913. kfree(sps);
  914. }
  915. sps_mem_de_init();
  916. }
  917. /**
  918. * Initialize client state context
  919. *
  920. * This function initializes a client state context struct.
  921. *
  922. * @client - Pointer to client state context
  923. *
  924. * @return 0 on success, negative value on error
  925. *
  926. */
  927. static int sps_client_init(struct sps_pipe *client)
  928. {
  929. SPS_DBG("sps:%s.", __func__);
  930. if (client == NULL)
  931. return -EINVAL;
  932. /*
  933. * NOTE: Cannot store any state within the SPS driver because
  934. * the driver init function may not have been called yet.
  935. */
  936. memset(client, 0, sizeof(*client));
  937. sps_rm_config_init(&client->connect);
  938. client->client_state = SPS_STATE_DISCONNECT;
  939. client->bam = NULL;
  940. return 0;
  941. }
  942. /**
  943. * De-initialize client state context
  944. *
  945. * This function de-initializes a client state context struct.
  946. *
  947. * @client - Pointer to client state context
  948. *
  949. * @return 0 on success, negative value on error
  950. *
  951. */
  952. static int sps_client_de_init(struct sps_pipe *client)
  953. {
  954. SPS_DBG("sps:%s.", __func__);
  955. if (client->client_state != SPS_STATE_DISCONNECT) {
  956. SPS_ERR("sps:De-init client in connected state: 0x%x",
  957. client->client_state);
  958. return SPS_ERROR;
  959. }
  960. client->bam = NULL;
  961. client->map = NULL;
  962. memset(&client->connect, 0, sizeof(client->connect));
  963. return 0;
  964. }
  965. /**
  966. * Find the BAM device from the physical address
  967. *
  968. * This function finds a BAM device in the BAM registration list that
  969. * matches the specified physical address.
  970. *
  971. * @phys_addr - physical address of the BAM
  972. *
  973. * @return - pointer to the BAM device struct, or NULL on error
  974. *
  975. */
  976. static struct sps_bam *phy2bam(phys_addr_t phys_addr)
  977. {
  978. struct sps_bam *bam;
  979. SPS_DBG("sps:%s.", __func__);
  980. list_for_each_entry(bam, &sps->bams_q, list) {
  981. if (bam->props.phys_addr == phys_addr)
  982. return bam;
  983. }
  984. return NULL;
  985. }
  986. /**
  987. * Find the handle of a BAM device based on the physical address
  988. *
  989. * This function finds a BAM device in the BAM registration list that
  990. * matches the specified physical address, and returns its handle.
  991. *
  992. * @phys_addr - physical address of the BAM
  993. *
  994. * @h - device handle of the BAM
  995. *
  996. * @return 0 on success, negative value on error
  997. *
  998. */
  999. int sps_phy2h(phys_addr_t phys_addr, unsigned long *handle)
  1000. {
  1001. struct sps_bam *bam;
  1002. SPS_DBG("sps:%s.", __func__);
  1003. if (sps == NULL || !sps->is_ready) {
  1004. SPS_DBG2("sps:%s:sps driver is not ready.\n", __func__);
  1005. return -EPROBE_DEFER;
  1006. }
  1007. if (handle == NULL) {
  1008. SPS_ERR("sps:%s:handle is NULL.\n", __func__);
  1009. return SPS_ERROR;
  1010. }
  1011. list_for_each_entry(bam, &sps->bams_q, list) {
  1012. if (bam->props.phys_addr == phys_addr) {
  1013. *handle = (uintptr_t) bam;
  1014. return 0;
  1015. }
  1016. }
  1017. SPS_ERR("sps: BAM device %pa is not registered yet.\n", &phys_addr);
  1018. return -ENODEV;
  1019. }
  1020. EXPORT_SYMBOL(sps_phy2h);
  1021. /**
  1022. * Setup desc/data FIFO for bam-to-bam connection
  1023. *
  1024. * @mem_buffer - Pointer to struct for allocated memory properties.
  1025. *
  1026. * @addr - address of FIFO
  1027. *
  1028. * @size - FIFO size
  1029. *
  1030. * @use_offset - use address offset instead of absolute address
  1031. *
  1032. * @return 0 on success, negative value on error
  1033. *
  1034. */
  1035. int sps_setup_bam2bam_fifo(struct sps_mem_buffer *mem_buffer,
  1036. u32 addr, u32 size, int use_offset)
  1037. {
  1038. SPS_DBG("sps:%s.", __func__);
  1039. if ((mem_buffer == NULL) || (size == 0)) {
  1040. SPS_ERR("sps:invalid buffer address or size.");
  1041. return SPS_ERROR;
  1042. }
  1043. if (sps == NULL || !sps->is_ready) {
  1044. SPS_DBG2("sps:%s:sps driver is not ready.\n", __func__);
  1045. return -EPROBE_DEFER;
  1046. }
  1047. if (use_offset) {
  1048. if ((addr + size) <= sps->pipemem_size)
  1049. mem_buffer->phys_base = sps->pipemem_phys_base + addr;
  1050. else {
  1051. SPS_ERR("sps:requested mem is out of "
  1052. "pipe mem range.\n");
  1053. return SPS_ERROR;
  1054. }
  1055. } else {
  1056. if (addr >= sps->pipemem_phys_base &&
  1057. (addr + size) <= (sps->pipemem_phys_base
  1058. + sps->pipemem_size))
  1059. mem_buffer->phys_base = addr;
  1060. else {
  1061. SPS_ERR("sps:requested mem is out of "
  1062. "pipe mem range.\n");
  1063. return SPS_ERROR;
  1064. }
  1065. }
  1066. mem_buffer->base = spsi_get_mem_ptr(mem_buffer->phys_base);
  1067. mem_buffer->size = size;
  1068. memset(mem_buffer->base, 0, mem_buffer->size);
  1069. return 0;
  1070. }
  1071. EXPORT_SYMBOL(sps_setup_bam2bam_fifo);
  1072. /**
  1073. * Find the BAM device from the handle
  1074. *
  1075. * This function finds a BAM device in the BAM registration list that
  1076. * matches the specified device handle.
  1077. *
  1078. * @h - device handle of the BAM
  1079. *
  1080. * @return - pointer to the BAM device struct, or NULL on error
  1081. *
  1082. */
  1083. struct sps_bam *sps_h2bam(unsigned long h)
  1084. {
  1085. struct sps_bam *bam;
  1086. SPS_DBG("sps:%s.", __func__);
  1087. if (h == SPS_DEV_HANDLE_MEM || h == SPS_DEV_HANDLE_INVALID)
  1088. return NULL;
  1089. list_for_each_entry(bam, &sps->bams_q, list) {
  1090. if ((uintptr_t) bam == h)
  1091. return bam;
  1092. }
  1093. SPS_ERR("sps:Can't find BAM device for handle 0x%lx.", h);
  1094. return NULL;
  1095. }
  1096. /**
  1097. * Lock BAM device
  1098. *
  1099. * This function obtains the BAM spinlock on the client's connection.
  1100. *
  1101. * @pipe - pointer to client pipe state
  1102. *
  1103. * @return pointer to BAM device struct, or NULL on error
  1104. *
  1105. */
  1106. static struct sps_bam *sps_bam_lock(struct sps_pipe *pipe)
  1107. {
  1108. struct sps_bam *bam;
  1109. u32 pipe_index;
  1110. bam = pipe->bam;
  1111. if (bam == NULL) {
  1112. SPS_ERR("sps:Connection is not in connected state.");
  1113. return NULL;
  1114. }
  1115. spin_lock_irqsave(&bam->connection_lock, bam->irqsave_flags);
  1116. /* Verify client owns this pipe */
  1117. pipe_index = pipe->pipe_index;
  1118. if (pipe_index >= bam->props.num_pipes ||
  1119. pipe != bam->pipes[pipe_index]) {
  1120. SPS_ERR("sps:Client not owner of BAM %pa pipe: %d (max %d)",
  1121. &bam->props.phys_addr, pipe_index,
  1122. bam->props.num_pipes);
  1123. spin_unlock_irqrestore(&bam->connection_lock,
  1124. bam->irqsave_flags);
  1125. return NULL;
  1126. }
  1127. return bam;
  1128. }
  1129. /**
  1130. * Unlock BAM device
  1131. *
  1132. * This function releases the BAM spinlock on the client's connection.
  1133. *
  1134. * @bam - pointer to BAM device struct
  1135. *
  1136. */
  1137. static inline void sps_bam_unlock(struct sps_bam *bam)
  1138. {
  1139. spin_unlock_irqrestore(&bam->connection_lock, bam->irqsave_flags);
  1140. }
  1141. /**
  1142. * Connect an SPS connection end point
  1143. *
  1144. */
  1145. int sps_connect(struct sps_pipe *h, struct sps_connect *connect)
  1146. {
  1147. struct sps_pipe *pipe = h;
  1148. unsigned long dev;
  1149. struct sps_bam *bam;
  1150. int result;
  1151. SPS_DBG2("sps:%s.", __func__);
  1152. if (h == NULL) {
  1153. SPS_ERR("sps:%s:pipe is NULL.\n", __func__);
  1154. return SPS_ERROR;
  1155. } else if (connect == NULL) {
  1156. SPS_ERR("sps:%s:connection is NULL.\n", __func__);
  1157. return SPS_ERROR;
  1158. }
  1159. if (sps == NULL)
  1160. return -ENODEV;
  1161. if (!sps->is_ready) {
  1162. SPS_ERR("sps:sps_connect:sps driver is not ready.\n");
  1163. return -EAGAIN;
  1164. }
  1165. if ((connect->lock_group != SPSRM_CLEAR)
  1166. && (connect->lock_group > BAM_MAX_P_LOCK_GROUP_NUM)) {
  1167. SPS_ERR("sps:The value of pipe lock group is invalid.\n");
  1168. return SPS_ERROR;
  1169. }
  1170. mutex_lock(&sps->lock);
  1171. /*
  1172. * Must lock the BAM device at the top level function, so must
  1173. * determine which BAM is the target for the connection
  1174. */
  1175. if (connect->mode == SPS_MODE_SRC)
  1176. dev = connect->source;
  1177. else
  1178. dev = connect->destination;
  1179. bam = sps_h2bam(dev);
  1180. if (bam == NULL) {
  1181. SPS_ERR("sps:Invalid BAM device handle: 0x%lx", dev);
  1182. result = SPS_ERROR;
  1183. goto exit_err;
  1184. }
  1185. SPS_DBG2("sps:sps_connect: bam %pa src 0x%lx dest 0x%lx mode %s",
  1186. BAM_ID(bam),
  1187. connect->source,
  1188. connect->destination,
  1189. connect->mode == SPS_MODE_SRC ? "SRC" : "DEST");
  1190. /* Allocate resources for the specified connection */
  1191. pipe->connect = *connect;
  1192. mutex_lock(&bam->lock);
  1193. result = sps_rm_state_change(pipe, SPS_STATE_ALLOCATE);
  1194. mutex_unlock(&bam->lock);
  1195. if (result)
  1196. goto exit_err;
  1197. /* Configure the connection */
  1198. mutex_lock(&bam->lock);
  1199. result = sps_rm_state_change(pipe, SPS_STATE_CONNECT);
  1200. mutex_unlock(&bam->lock);
  1201. if (result) {
  1202. sps_disconnect(h);
  1203. goto exit_err;
  1204. }
  1205. exit_err:
  1206. mutex_unlock(&sps->lock);
  1207. return result;
  1208. }
  1209. EXPORT_SYMBOL(sps_connect);
  1210. /**
  1211. * Disconnect an SPS connection end point
  1212. *
  1213. * This function disconnects an SPS connection end point.
  1214. * The SPS hardware associated with that end point will be disabled.
  1215. * For a connection involving system memory (SPS_DEV_HANDLE_MEM), all
  1216. * connection resources are deallocated. For a peripheral-to-peripheral
  1217. * connection, the resources associated with the connection will not be
  1218. * deallocated until both end points are closed.
  1219. *
  1220. * The client must call sps_connect() for the handle before calling
  1221. * this function.
  1222. *
  1223. * @h - client context for SPS connection end point
  1224. *
  1225. * @return 0 on success, negative value on error
  1226. *
  1227. */
  1228. int sps_disconnect(struct sps_pipe *h)
  1229. {
  1230. struct sps_pipe *pipe = h;
  1231. struct sps_pipe *check;
  1232. struct sps_bam *bam;
  1233. int result;
  1234. SPS_DBG2("sps:%s.", __func__);
  1235. if (pipe == NULL) {
  1236. SPS_ERR("sps:Invalid pipe.");
  1237. return SPS_ERROR;
  1238. }
  1239. bam = pipe->bam;
  1240. if (bam == NULL) {
  1241. SPS_ERR("sps:BAM device of this pipe is NULL.");
  1242. return SPS_ERROR;
  1243. }
  1244. SPS_DBG2("sps:sps_disconnect: bam %pa src 0x%lx dest 0x%lx mode %s",
  1245. BAM_ID(bam),
  1246. pipe->connect.source,
  1247. pipe->connect.destination,
  1248. pipe->connect.mode == SPS_MODE_SRC ? "SRC" : "DEST");
  1249. result = SPS_ERROR;
  1250. /* Cross-check client with map table */
  1251. if (pipe->connect.mode == SPS_MODE_SRC)
  1252. check = pipe->map->client_src;
  1253. else
  1254. check = pipe->map->client_dest;
  1255. if (check != pipe) {
  1256. SPS_ERR("sps:Client context is corrupt");
  1257. goto exit_err;
  1258. }
  1259. /* Disconnect the BAM pipe */
  1260. mutex_lock(&bam->lock);
  1261. result = sps_rm_state_change(pipe, SPS_STATE_DISCONNECT);
  1262. mutex_unlock(&bam->lock);
  1263. if (result)
  1264. goto exit_err;
  1265. sps_rm_config_init(&pipe->connect);
  1266. result = 0;
  1267. exit_err:
  1268. return result;
  1269. }
  1270. EXPORT_SYMBOL(sps_disconnect);
  1271. /**
  1272. * Register an event object for an SPS connection end point
  1273. *
  1274. */
  1275. int sps_register_event(struct sps_pipe *h, struct sps_register_event *reg)
  1276. {
  1277. struct sps_pipe *pipe = h;
  1278. struct sps_bam *bam;
  1279. int result;
  1280. SPS_DBG2("sps:%s.", __func__);
  1281. if (h == NULL) {
  1282. SPS_ERR("sps:%s:pipe is NULL.\n", __func__);
  1283. return SPS_ERROR;
  1284. } else if (reg == NULL) {
  1285. SPS_ERR("sps:%s:registered event is NULL.\n", __func__);
  1286. return SPS_ERROR;
  1287. }
  1288. if (sps == NULL)
  1289. return -ENODEV;
  1290. if (!sps->is_ready) {
  1291. SPS_ERR("sps:sps_connect:sps driver not ready.\n");
  1292. return -EAGAIN;
  1293. }
  1294. bam = sps_bam_lock(pipe);
  1295. if (bam == NULL)
  1296. return SPS_ERROR;
  1297. result = sps_bam_pipe_reg_event(bam, pipe->pipe_index, reg);
  1298. sps_bam_unlock(bam);
  1299. if (result)
  1300. SPS_ERR("sps:Fail to register event for BAM %pa pipe %d",
  1301. &pipe->bam->props.phys_addr, pipe->pipe_index);
  1302. return result;
  1303. }
  1304. EXPORT_SYMBOL(sps_register_event);
  1305. /**
  1306. * Enable an SPS connection end point
  1307. *
  1308. */
  1309. int sps_flow_on(struct sps_pipe *h)
  1310. {
  1311. struct sps_pipe *pipe = h;
  1312. struct sps_bam *bam;
  1313. int result = 0;
  1314. SPS_DBG2("sps:%s.", __func__);
  1315. if (h == NULL) {
  1316. SPS_ERR("sps:%s:pipe is NULL.\n", __func__);
  1317. return SPS_ERROR;
  1318. }
  1319. bam = sps_bam_lock(pipe);
  1320. if (bam == NULL)
  1321. return SPS_ERROR;
  1322. bam_pipe_halt(bam->base, pipe->pipe_index, false);
  1323. sps_bam_unlock(bam);
  1324. return result;
  1325. }
  1326. EXPORT_SYMBOL(sps_flow_on);
  1327. /**
  1328. * Disable an SPS connection end point
  1329. *
  1330. */
  1331. int sps_flow_off(struct sps_pipe *h, enum sps_flow_off mode)
  1332. {
  1333. struct sps_pipe *pipe = h;
  1334. struct sps_bam *bam;
  1335. int result = 0;
  1336. SPS_DBG2("sps:%s.", __func__);
  1337. if (h == NULL) {
  1338. SPS_ERR("sps:%s:pipe is NULL.\n", __func__);
  1339. return SPS_ERROR;
  1340. }
  1341. bam = sps_bam_lock(pipe);
  1342. if (bam == NULL)
  1343. return SPS_ERROR;
  1344. bam_pipe_halt(bam->base, pipe->pipe_index, true);
  1345. sps_bam_unlock(bam);
  1346. return result;
  1347. }
  1348. EXPORT_SYMBOL(sps_flow_off);
  1349. /**
  1350. * Check if the flags on a descriptor/iovec are valid
  1351. *
  1352. * @flags - flags on a descriptor/iovec
  1353. *
  1354. * @return 0 on success, negative value on error
  1355. *
  1356. */
  1357. static int sps_check_iovec_flags(u32 flags)
  1358. {
  1359. if ((flags & SPS_IOVEC_FLAG_NWD) &&
  1360. !(flags & (SPS_IOVEC_FLAG_EOT | SPS_IOVEC_FLAG_CMD))) {
  1361. SPS_ERR("sps:NWD is only valid with EOT or CMD.\n");
  1362. return SPS_ERROR;
  1363. } else if ((flags & SPS_IOVEC_FLAG_EOT) &&
  1364. (flags & SPS_IOVEC_FLAG_CMD)) {
  1365. SPS_ERR("sps:EOT and CMD are not allowed to coexist.\n");
  1366. return SPS_ERROR;
  1367. } else if (!(flags & SPS_IOVEC_FLAG_CMD) &&
  1368. (flags & (SPS_IOVEC_FLAG_LOCK | SPS_IOVEC_FLAG_UNLOCK))) {
  1369. static char err_msg[] =
  1370. "pipe lock/unlock flags are only valid with Command Descriptor";
  1371. SPS_ERR("sps:%s.\n", err_msg);
  1372. return SPS_ERROR;
  1373. } else if ((flags & SPS_IOVEC_FLAG_LOCK) &&
  1374. (flags & SPS_IOVEC_FLAG_UNLOCK)) {
  1375. static char err_msg[] =
  1376. "Can't lock and unlock a pipe by the same Command Descriptor";
  1377. SPS_ERR("sps:%s.\n", err_msg);
  1378. return SPS_ERROR;
  1379. } else if ((flags & SPS_IOVEC_FLAG_IMME) &&
  1380. (flags & SPS_IOVEC_FLAG_CMD)) {
  1381. SPS_ERR("sps:Immediate and CMD are not allowed to coexist.\n");
  1382. return SPS_ERROR;
  1383. } else if ((flags & SPS_IOVEC_FLAG_IMME) &&
  1384. (flags & SPS_IOVEC_FLAG_NWD)) {
  1385. SPS_ERR("sps:Immediate and NWD are not allowed to coexist.\n");
  1386. return SPS_ERROR;
  1387. }
  1388. return 0;
  1389. }
  1390. /**
  1391. * Perform a DMA transfer on an SPS connection end point
  1392. *
  1393. */
  1394. int sps_transfer(struct sps_pipe *h, struct sps_transfer *transfer)
  1395. {
  1396. struct sps_pipe *pipe = h;
  1397. struct sps_bam *bam;
  1398. int result;
  1399. struct sps_iovec *iovec;
  1400. int i;
  1401. SPS_DBG("sps:%s.", __func__);
  1402. if (h == NULL) {
  1403. SPS_ERR("sps:%s:pipe is NULL.\n", __func__);
  1404. return SPS_ERROR;
  1405. } else if (transfer == NULL) {
  1406. SPS_ERR("sps:%s:transfer is NULL.\n", __func__);
  1407. return SPS_ERROR;
  1408. } else if (transfer->iovec == NULL) {
  1409. SPS_ERR("sps:%s:iovec list is NULL.\n", __func__);
  1410. return SPS_ERROR;
  1411. } else if (transfer->iovec_count == 0) {
  1412. SPS_ERR("sps:%s:iovec list is empty.\n", __func__);
  1413. return SPS_ERROR;
  1414. } else if (transfer->iovec_phys == 0) {
  1415. SPS_ERR("sps:%s:iovec list address is invalid.\n", __func__);
  1416. return SPS_ERROR;
  1417. }
  1418. /* Verify content of IOVECs */
  1419. iovec = transfer->iovec;
  1420. for (i = 0; i < transfer->iovec_count; i++) {
  1421. u32 flags = iovec->flags;
  1422. if (iovec->size > SPS_IOVEC_MAX_SIZE) {
  1423. SPS_ERR("sps:%s:iovec size is invalid.\n", __func__);
  1424. return SPS_ERROR;
  1425. }
  1426. if (sps_check_iovec_flags(flags))
  1427. return SPS_ERROR;
  1428. iovec++;
  1429. }
  1430. bam = sps_bam_lock(pipe);
  1431. if (bam == NULL)
  1432. return SPS_ERROR;
  1433. result = sps_bam_pipe_transfer(bam, pipe->pipe_index, transfer);
  1434. sps_bam_unlock(bam);
  1435. return result;
  1436. }
  1437. EXPORT_SYMBOL(sps_transfer);
  1438. /**
  1439. * Perform a single DMA transfer on an SPS connection end point
  1440. *
  1441. */
  1442. int sps_transfer_one(struct sps_pipe *h, phys_addr_t addr, u32 size,
  1443. void *user, u32 flags)
  1444. {
  1445. struct sps_pipe *pipe = h;
  1446. struct sps_bam *bam;
  1447. int result;
  1448. SPS_DBG("sps:%s.", __func__);
  1449. if (h == NULL) {
  1450. SPS_ERR("sps:%s:pipe is NULL.\n", __func__);
  1451. return SPS_ERROR;
  1452. }
  1453. if (sps_check_iovec_flags(flags))
  1454. return SPS_ERROR;
  1455. bam = sps_bam_lock(pipe);
  1456. if (bam == NULL)
  1457. return SPS_ERROR;
  1458. result = sps_bam_pipe_transfer_one(bam, pipe->pipe_index,
  1459. SPS_GET_LOWER_ADDR(addr), size, user,
  1460. DESC_FLAG_WORD(flags, addr));
  1461. sps_bam_unlock(bam);
  1462. return result;
  1463. }
  1464. EXPORT_SYMBOL(sps_transfer_one);
  1465. /**
  1466. * Read event queue for an SPS connection end point
  1467. *
  1468. */
  1469. int sps_get_event(struct sps_pipe *h, struct sps_event_notify *notify)
  1470. {
  1471. struct sps_pipe *pipe = h;
  1472. struct sps_bam *bam;
  1473. int result;
  1474. SPS_DBG("sps:%s.", __func__);
  1475. if (h == NULL) {
  1476. SPS_ERR("sps:%s:pipe is NULL.\n", __func__);
  1477. return SPS_ERROR;
  1478. } else if (notify == NULL) {
  1479. SPS_ERR("sps:%s:event_notify is NULL.\n", __func__);
  1480. return SPS_ERROR;
  1481. }
  1482. bam = sps_bam_lock(pipe);
  1483. if (bam == NULL)
  1484. return SPS_ERROR;
  1485. result = sps_bam_pipe_get_event(bam, pipe->pipe_index, notify);
  1486. sps_bam_unlock(bam);
  1487. return result;
  1488. }
  1489. EXPORT_SYMBOL(sps_get_event);
  1490. /**
  1491. * Determine whether an SPS connection end point FIFO is empty
  1492. *
  1493. */
  1494. int sps_is_pipe_empty(struct sps_pipe *h, u32 *empty)
  1495. {
  1496. struct sps_pipe *pipe = h;
  1497. struct sps_bam *bam;
  1498. int result;
  1499. SPS_DBG("sps:%s.", __func__);
  1500. if (h == NULL) {
  1501. SPS_ERR("sps:%s:pipe is NULL.\n", __func__);
  1502. return SPS_ERROR;
  1503. } else if (empty == NULL) {
  1504. SPS_ERR("sps:%s:result pointer is NULL.\n", __func__);
  1505. return SPS_ERROR;
  1506. }
  1507. bam = sps_bam_lock(pipe);
  1508. if (bam == NULL)
  1509. return SPS_ERROR;
  1510. result = sps_bam_pipe_is_empty(bam, pipe->pipe_index, empty);
  1511. sps_bam_unlock(bam);
  1512. return result;
  1513. }
  1514. EXPORT_SYMBOL(sps_is_pipe_empty);
  1515. /**
  1516. * Get number of free transfer entries for an SPS connection end point
  1517. *
  1518. */
  1519. int sps_get_free_count(struct sps_pipe *h, u32 *count)
  1520. {
  1521. struct sps_pipe *pipe = h;
  1522. struct sps_bam *bam;
  1523. int result;
  1524. SPS_DBG("sps:%s.", __func__);
  1525. if (h == NULL) {
  1526. SPS_ERR("sps:%s:pipe is NULL.\n", __func__);
  1527. return SPS_ERROR;
  1528. } else if (count == NULL) {
  1529. SPS_ERR("sps:%s:result pointer is NULL.\n", __func__);
  1530. return SPS_ERROR;
  1531. }
  1532. bam = sps_bam_lock(pipe);
  1533. if (bam == NULL)
  1534. return SPS_ERROR;
  1535. result = sps_bam_get_free_count(bam, pipe->pipe_index, count);
  1536. sps_bam_unlock(bam);
  1537. return result;
  1538. }
  1539. EXPORT_SYMBOL(sps_get_free_count);
  1540. /**
  1541. * Reset an SPS BAM device
  1542. *
  1543. */
  1544. int sps_device_reset(unsigned long dev)
  1545. {
  1546. struct sps_bam *bam;
  1547. int result;
  1548. SPS_DBG2("sps:%s: dev = 0x%lx", __func__, dev);
  1549. if (dev == 0) {
  1550. SPS_ERR("sps:%s:device handle should not be 0.\n", __func__);
  1551. return SPS_ERROR;
  1552. }
  1553. if (sps == NULL || !sps->is_ready) {
  1554. SPS_DBG2("sps:%s:sps driver is not ready.\n", __func__);
  1555. return -EPROBE_DEFER;
  1556. }
  1557. mutex_lock(&sps->lock);
  1558. /* Search for the target BAM device */
  1559. bam = sps_h2bam(dev);
  1560. if (bam == NULL) {
  1561. SPS_ERR("sps:Invalid BAM device handle: 0x%lx", dev);
  1562. result = SPS_ERROR;
  1563. goto exit_err;
  1564. }
  1565. mutex_lock(&bam->lock);
  1566. result = sps_bam_reset(bam);
  1567. mutex_unlock(&bam->lock);
  1568. if (result) {
  1569. SPS_ERR("sps:Fail to reset BAM device: 0x%lx", dev);
  1570. goto exit_err;
  1571. }
  1572. exit_err:
  1573. mutex_unlock(&sps->lock);
  1574. return result;
  1575. }
  1576. EXPORT_SYMBOL(sps_device_reset);
  1577. /**
  1578. * Get the configuration parameters for an SPS connection end point
  1579. *
  1580. */
  1581. int sps_get_config(struct sps_pipe *h, struct sps_connect *config)
  1582. {
  1583. struct sps_pipe *pipe = h;
  1584. SPS_DBG("sps:%s.", __func__);
  1585. if (h == NULL) {
  1586. SPS_ERR("sps:%s:pipe is NULL.\n", __func__);
  1587. return SPS_ERROR;
  1588. } else if (config == NULL) {
  1589. SPS_ERR("sps:%s:config pointer is NULL.\n", __func__);
  1590. return SPS_ERROR;
  1591. }
  1592. /* Copy current client connection state */
  1593. *config = pipe->connect;
  1594. return 0;
  1595. }
  1596. EXPORT_SYMBOL(sps_get_config);
  1597. /**
  1598. * Set the configuration parameters for an SPS connection end point
  1599. *
  1600. */
  1601. int sps_set_config(struct sps_pipe *h, struct sps_connect *config)
  1602. {
  1603. struct sps_pipe *pipe = h;
  1604. struct sps_bam *bam;
  1605. int result;
  1606. SPS_DBG("sps:%s.", __func__);
  1607. if (h == NULL) {
  1608. SPS_ERR("sps:%s:pipe is NULL.\n", __func__);
  1609. return SPS_ERROR;
  1610. } else if (config == NULL) {
  1611. SPS_ERR("sps:%s:config pointer is NULL.\n", __func__);
  1612. return SPS_ERROR;
  1613. }
  1614. bam = sps_bam_lock(pipe);
  1615. if (bam == NULL)
  1616. return SPS_ERROR;
  1617. result = sps_bam_pipe_set_params(bam, pipe->pipe_index,
  1618. config->options);
  1619. if (result == 0)
  1620. pipe->connect.options = config->options;
  1621. sps_bam_unlock(bam);
  1622. return result;
  1623. }
  1624. EXPORT_SYMBOL(sps_set_config);
  1625. /**
  1626. * Set ownership of an SPS connection end point
  1627. *
  1628. */
  1629. int sps_set_owner(struct sps_pipe *h, enum sps_owner owner,
  1630. struct sps_satellite *connect)
  1631. {
  1632. struct sps_pipe *pipe = h;
  1633. struct sps_bam *bam;
  1634. int result;
  1635. SPS_DBG("sps:%s.", __func__);
  1636. if (h == NULL) {
  1637. SPS_ERR("sps:%s:pipe is NULL.\n", __func__);
  1638. return SPS_ERROR;
  1639. } else if (connect == NULL) {
  1640. SPS_ERR("sps:%s:connection is NULL.\n", __func__);
  1641. return SPS_ERROR;
  1642. }
  1643. if (owner != SPS_OWNER_REMOTE) {
  1644. SPS_ERR("sps:Unsupported ownership state: %d", owner);
  1645. return SPS_ERROR;
  1646. }
  1647. bam = sps_bam_lock(pipe);
  1648. if (bam == NULL)
  1649. return SPS_ERROR;
  1650. result = sps_bam_set_satellite(bam, pipe->pipe_index);
  1651. if (result)
  1652. goto exit_err;
  1653. /* Return satellite connect info */
  1654. if (connect == NULL)
  1655. goto exit_err;
  1656. if (pipe->connect.mode == SPS_MODE_SRC) {
  1657. connect->dev = pipe->map->src.bam_phys;
  1658. connect->pipe_index = pipe->map->src.pipe_index;
  1659. } else {
  1660. connect->dev = pipe->map->dest.bam_phys;
  1661. connect->pipe_index = pipe->map->dest.pipe_index;
  1662. }
  1663. connect->config = SPS_CONFIG_SATELLITE;
  1664. connect->options = (enum sps_option) 0;
  1665. exit_err:
  1666. sps_bam_unlock(bam);
  1667. return result;
  1668. }
  1669. EXPORT_SYMBOL(sps_set_owner);
  1670. /**
  1671. * Allocate memory from the SPS Pipe-Memory.
  1672. *
  1673. */
  1674. int sps_alloc_mem(struct sps_pipe *h, enum sps_mem mem,
  1675. struct sps_mem_buffer *mem_buffer)
  1676. {
  1677. SPS_DBG("sps:%s.", __func__);
  1678. if (sps == NULL)
  1679. return -ENODEV;
  1680. if (!sps->is_ready) {
  1681. SPS_ERR("sps:sps_alloc_mem:sps driver is not ready.");
  1682. return -EAGAIN;
  1683. }
  1684. if (mem_buffer == NULL || mem_buffer->size == 0) {
  1685. SPS_ERR("sps:invalid memory buffer address or size");
  1686. return SPS_ERROR;
  1687. }
  1688. if (h == NULL)
  1689. SPS_DBG("sps:allocate pipe memory before setup pipe");
  1690. else
  1691. SPS_DBG("sps:allocate pipe memory for pipe %d", h->pipe_index);
  1692. mem_buffer->phys_base = sps_mem_alloc_io(mem_buffer->size);
  1693. if (mem_buffer->phys_base == SPS_ADDR_INVALID) {
  1694. SPS_ERR("sps:invalid address of allocated memory");
  1695. return SPS_ERROR;
  1696. }
  1697. mem_buffer->base = spsi_get_mem_ptr(mem_buffer->phys_base);
  1698. return 0;
  1699. }
  1700. EXPORT_SYMBOL(sps_alloc_mem);
  1701. /**
  1702. * Free memory from the SPS Pipe-Memory.
  1703. *
  1704. */
  1705. int sps_free_mem(struct sps_pipe *h, struct sps_mem_buffer *mem_buffer)
  1706. {
  1707. SPS_DBG("sps:%s.", __func__);
  1708. if (mem_buffer == NULL || mem_buffer->phys_base == SPS_ADDR_INVALID) {
  1709. SPS_ERR("sps:invalid memory to free");
  1710. return SPS_ERROR;
  1711. }
  1712. if (h == NULL)
  1713. SPS_DBG("sps:free pipe memory.");
  1714. else
  1715. SPS_DBG("sps:free pipe memory for pipe %d.", h->pipe_index);
  1716. sps_mem_free_io(mem_buffer->phys_base, mem_buffer->size);
  1717. return 0;
  1718. }
  1719. EXPORT_SYMBOL(sps_free_mem);
  1720. /**
  1721. * Get the number of unused descriptors in the descriptor FIFO
  1722. * of a pipe
  1723. *
  1724. */
  1725. int sps_get_unused_desc_num(struct sps_pipe *h, u32 *desc_num)
  1726. {
  1727. struct sps_pipe *pipe = h;
  1728. struct sps_bam *bam;
  1729. int result;
  1730. SPS_DBG("sps:%s.", __func__);
  1731. if (h == NULL) {
  1732. SPS_ERR("sps:%s:pipe is NULL.\n", __func__);
  1733. return SPS_ERROR;
  1734. } else if (desc_num == NULL) {
  1735. SPS_ERR("sps:%s:result pointer is NULL.\n", __func__);
  1736. return SPS_ERROR;
  1737. }
  1738. bam = sps_bam_lock(pipe);
  1739. if (bam == NULL)
  1740. return SPS_ERROR;
  1741. result = sps_bam_pipe_get_unused_desc_num(bam, pipe->pipe_index,
  1742. desc_num);
  1743. sps_bam_unlock(bam);
  1744. return result;
  1745. }
  1746. EXPORT_SYMBOL(sps_get_unused_desc_num);
  1747. /**
  1748. * Vote for or relinquish BAM DMA clock
  1749. *
  1750. */
  1751. int sps_ctrl_bam_dma_clk(bool clk_on)
  1752. {
  1753. int ret;
  1754. SPS_DBG("sps:%s.", __func__);
  1755. if (sps == NULL || !sps->is_ready) {
  1756. SPS_DBG2("sps:%s:sps driver is not ready.\n", __func__);
  1757. return -EPROBE_DEFER;
  1758. }
  1759. if (clk_on == true) {
  1760. SPS_DBG("sps:vote for bam dma clk.\n");
  1761. ret = clk_prepare_enable(sps->bamdma_clk);
  1762. if (ret) {
  1763. SPS_ERR("sps:fail to enable bamdma_clk:ret=%d\n", ret);
  1764. return ret;
  1765. }
  1766. } else {
  1767. SPS_DBG("sps:relinquish bam dma clk.\n");
  1768. clk_disable_unprepare(sps->bamdma_clk);
  1769. }
  1770. return 0;
  1771. }
  1772. EXPORT_SYMBOL(sps_ctrl_bam_dma_clk);
  1773. /**
  1774. * Register a BAM device
  1775. *
  1776. */
  1777. int sps_register_bam_device(const struct sps_bam_props *bam_props,
  1778. unsigned long *dev_handle)
  1779. {
  1780. struct sps_bam *bam = NULL;
  1781. void *virt_addr = NULL;
  1782. u32 manage;
  1783. int ok;
  1784. int result;
  1785. SPS_DBG2("sps:%s.", __func__);
  1786. if (bam_props == NULL) {
  1787. SPS_ERR("sps:%s:bam_props is NULL.\n", __func__);
  1788. return SPS_ERROR;
  1789. } else if (dev_handle == NULL) {
  1790. SPS_ERR("sps:%s:device handle is NULL.\n", __func__);
  1791. return SPS_ERROR;
  1792. }
  1793. if (sps == NULL) {
  1794. SPS_DBG2("sps:%s:sps driver is not ready.\n", __func__);
  1795. return -EPROBE_DEFER;
  1796. }
  1797. /* BAM-DMA is registered internally during power-up */
  1798. if ((!sps->is_ready) && !(bam_props->options & SPS_BAM_OPT_BAMDMA)) {
  1799. SPS_ERR("sps:sps_register_bam_device:sps driver not ready.\n");
  1800. return -EAGAIN;
  1801. }
  1802. /* Check BAM parameters */
  1803. manage = bam_props->manage & SPS_BAM_MGR_ACCESS_MASK;
  1804. if (manage != SPS_BAM_MGR_NONE) {
  1805. if (bam_props->virt_addr == NULL && bam_props->virt_size == 0) {
  1806. SPS_ERR("sps:Invalid properties for BAM: %pa",
  1807. &bam_props->phys_addr);
  1808. return SPS_ERROR;
  1809. }
  1810. }
  1811. if ((bam_props->manage & SPS_BAM_MGR_DEVICE_REMOTE) == 0) {
  1812. /* BAM global is configured by local processor */
  1813. if (bam_props->summing_threshold == 0) {
  1814. SPS_ERR(
  1815. "sps:Invalid device ctrl properties for "
  1816. "BAM: %pa", &bam_props->phys_addr);
  1817. return SPS_ERROR;
  1818. }
  1819. }
  1820. manage = bam_props->manage &
  1821. (SPS_BAM_MGR_PIPE_NO_CONFIG | SPS_BAM_MGR_PIPE_NO_CTRL);
  1822. /* In case of error */
  1823. *dev_handle = SPS_DEV_HANDLE_INVALID;
  1824. result = SPS_ERROR;
  1825. mutex_lock(&sps->lock);
  1826. /* Is this BAM already registered? */
  1827. bam = phy2bam(bam_props->phys_addr);
  1828. if (bam != NULL) {
  1829. mutex_unlock(&sps->lock);
  1830. SPS_ERR("sps:BAM is already registered: %pa",
  1831. &bam->props.phys_addr);
  1832. result = -EEXIST;
  1833. bam = NULL; /* Avoid error clean-up kfree(bam) */
  1834. goto exit_err;
  1835. }
  1836. /* Perform virtual mapping if required */
  1837. if ((bam_props->manage & SPS_BAM_MGR_ACCESS_MASK) !=
  1838. SPS_BAM_MGR_NONE && bam_props->virt_addr == NULL) {
  1839. /* Map the memory region */
  1840. virt_addr = ioremap(bam_props->phys_addr, bam_props->virt_size);
  1841. if (virt_addr == NULL) {
  1842. SPS_ERR("sps:Unable to map BAM IO mem:%pa size:0x%x",
  1843. &bam_props->phys_addr, bam_props->virt_size);
  1844. goto exit_err;
  1845. }
  1846. }
  1847. bam = kzalloc(sizeof(*bam), GFP_KERNEL);
  1848. if (bam == NULL) {
  1849. SPS_ERR("sps:Unable to allocate BAM device state: size 0x%zu",
  1850. sizeof(*bam));
  1851. goto exit_err;
  1852. }
  1853. memset(bam, 0, sizeof(*bam));
  1854. mutex_init(&bam->lock);
  1855. mutex_lock(&bam->lock);
  1856. /* Copy configuration to BAM device descriptor */
  1857. bam->props = *bam_props;
  1858. if (virt_addr != NULL)
  1859. bam->props.virt_addr = virt_addr;
  1860. ok = sps_bam_device_init(bam);
  1861. mutex_unlock(&bam->lock);
  1862. if (ok) {
  1863. SPS_ERR("sps:Fail to init BAM device: phys %pa",
  1864. &bam->props.phys_addr);
  1865. goto exit_err;
  1866. }
  1867. /* Add BAM to the list */
  1868. list_add_tail(&bam->list, &sps->bams_q);
  1869. *dev_handle = (uintptr_t) bam;
  1870. result = 0;
  1871. exit_err:
  1872. mutex_unlock(&sps->lock);
  1873. if (result) {
  1874. if (bam != NULL) {
  1875. if (virt_addr != NULL)
  1876. iounmap(bam->props.virt_addr);
  1877. kfree(bam);
  1878. }
  1879. return result;
  1880. }
  1881. /* If this BAM is attached to a BAM-DMA, init the BAM-DMA device */
  1882. #ifdef CONFIG_SPS_SUPPORT_BAMDMA
  1883. if ((bam->props.options & SPS_BAM_OPT_BAMDMA)) {
  1884. if (sps_dma_device_init((uintptr_t) bam)) {
  1885. bam->props.options &= ~SPS_BAM_OPT_BAMDMA;
  1886. sps_deregister_bam_device((uintptr_t) bam);
  1887. SPS_ERR("sps:Fail to init BAM-DMA BAM: phys %pa",
  1888. &bam->props.phys_addr);
  1889. return SPS_ERROR;
  1890. }
  1891. }
  1892. #endif /* CONFIG_SPS_SUPPORT_BAMDMA */
  1893. SPS_INFO("sps:BAM %pa is registered.", &bam->props.phys_addr);
  1894. return 0;
  1895. }
  1896. EXPORT_SYMBOL(sps_register_bam_device);
  1897. /**
  1898. * Deregister a BAM device
  1899. *
  1900. */
  1901. int sps_deregister_bam_device(unsigned long dev_handle)
  1902. {
  1903. struct sps_bam *bam;
  1904. int n;
  1905. SPS_DBG2("sps:%s.", __func__);
  1906. if (dev_handle == 0) {
  1907. SPS_ERR("sps:%s:device handle should not be 0.\n", __func__);
  1908. return SPS_ERROR;
  1909. }
  1910. bam = sps_h2bam(dev_handle);
  1911. if (bam == NULL) {
  1912. SPS_ERR("sps:did not find a BAM for this handle");
  1913. return SPS_ERROR;
  1914. }
  1915. SPS_DBG2("sps:SPS deregister BAM: phys %pa.", &bam->props.phys_addr);
  1916. if (bam->props.options & SPS_BAM_HOLD_MEM) {
  1917. for (n = 0; n < BAM_MAX_PIPES; n++)
  1918. if (bam->desc_cache_pointers[n] != NULL)
  1919. kfree(bam->desc_cache_pointers[n]);
  1920. }
  1921. /* If this BAM is attached to a BAM-DMA, init the BAM-DMA device */
  1922. #ifdef CONFIG_SPS_SUPPORT_BAMDMA
  1923. if ((bam->props.options & SPS_BAM_OPT_BAMDMA)) {
  1924. mutex_lock(&bam->lock);
  1925. (void)sps_dma_device_de_init((uintptr_t) bam);
  1926. bam->props.options &= ~SPS_BAM_OPT_BAMDMA;
  1927. mutex_unlock(&bam->lock);
  1928. }
  1929. #endif
  1930. /* Remove the BAM from the registration list */
  1931. mutex_lock(&sps->lock);
  1932. list_del(&bam->list);
  1933. mutex_unlock(&sps->lock);
  1934. /* De-init the BAM and free resources */
  1935. mutex_lock(&bam->lock);
  1936. sps_bam_device_de_init(bam);
  1937. mutex_unlock(&bam->lock);
  1938. if (bam->props.virt_size)
  1939. (void)iounmap(bam->props.virt_addr);
  1940. kfree(bam);
  1941. return 0;
  1942. }
  1943. EXPORT_SYMBOL(sps_deregister_bam_device);
  1944. /**
  1945. * Get processed I/O vector (completed transfers)
  1946. *
  1947. */
  1948. int sps_get_iovec(struct sps_pipe *h, struct sps_iovec *iovec)
  1949. {
  1950. struct sps_pipe *pipe = h;
  1951. struct sps_bam *bam;
  1952. int result;
  1953. SPS_DBG("sps:%s.", __func__);
  1954. if (h == NULL) {
  1955. SPS_ERR("sps:%s:pipe is NULL.\n", __func__);
  1956. return SPS_ERROR;
  1957. } else if (iovec == NULL) {
  1958. SPS_ERR("sps:%s:iovec pointer is NULL.\n", __func__);
  1959. return SPS_ERROR;
  1960. }
  1961. bam = sps_bam_lock(pipe);
  1962. if (bam == NULL)
  1963. return SPS_ERROR;
  1964. /* Get an iovec from the BAM pipe descriptor FIFO */
  1965. result = sps_bam_pipe_get_iovec(bam, pipe->pipe_index, iovec);
  1966. sps_bam_unlock(bam);
  1967. return result;
  1968. }
  1969. EXPORT_SYMBOL(sps_get_iovec);
  1970. /**
  1971. * Perform timer control
  1972. *
  1973. */
  1974. int sps_timer_ctrl(struct sps_pipe *h,
  1975. struct sps_timer_ctrl *timer_ctrl,
  1976. struct sps_timer_result *timer_result)
  1977. {
  1978. struct sps_pipe *pipe = h;
  1979. struct sps_bam *bam;
  1980. int result;
  1981. SPS_DBG("sps:%s.", __func__);
  1982. if (h == NULL) {
  1983. SPS_ERR("sps:%s:pipe is NULL.\n", __func__);
  1984. return SPS_ERROR;
  1985. } else if (timer_ctrl == NULL) {
  1986. SPS_ERR("sps:%s:timer_ctrl pointer is NULL.\n", __func__);
  1987. return SPS_ERROR;
  1988. } else if (timer_result == NULL) {
  1989. SPS_DBG("sps:%s:no result to return.\n", __func__);
  1990. }
  1991. bam = sps_bam_lock(pipe);
  1992. if (bam == NULL)
  1993. return SPS_ERROR;
  1994. /* Perform the BAM pipe timer control operation */
  1995. result = sps_bam_pipe_timer_ctrl(bam, pipe->pipe_index, timer_ctrl,
  1996. timer_result);
  1997. sps_bam_unlock(bam);
  1998. return result;
  1999. }
  2000. EXPORT_SYMBOL(sps_timer_ctrl);
  2001. /*
  2002. * Reset a BAM pipe
  2003. */
  2004. int sps_pipe_reset(unsigned long dev, u32 pipe)
  2005. {
  2006. struct sps_bam *bam;
  2007. SPS_DBG("sps:%s.", __func__);
  2008. if (!dev) {
  2009. SPS_ERR("sps:%s:BAM handle is NULL.\n", __func__);
  2010. return SPS_ERROR;
  2011. }
  2012. if (pipe >= BAM_MAX_PIPES) {
  2013. SPS_ERR("sps:%s:pipe index is invalid.\n", __func__);
  2014. return SPS_ERROR;
  2015. }
  2016. bam = sps_h2bam(dev);
  2017. if (bam == NULL) {
  2018. SPS_ERR("sps:%s:BAM is not found by handle.\n", __func__);
  2019. return SPS_ERROR;
  2020. }
  2021. bam_pipe_reset(bam->base, pipe);
  2022. return 0;
  2023. }
  2024. EXPORT_SYMBOL(sps_pipe_reset);
  2025. /*
  2026. * Process any pending IRQ of a BAM
  2027. */
  2028. int sps_bam_process_irq(unsigned long dev)
  2029. {
  2030. struct sps_bam *bam;
  2031. SPS_DBG("sps:%s.", __func__);
  2032. if (!dev) {
  2033. SPS_ERR("sps:%s:BAM handle is NULL.\n", __func__);
  2034. return SPS_ERROR;
  2035. }
  2036. bam = sps_h2bam(dev);
  2037. if (bam == NULL) {
  2038. SPS_ERR("sps:%s:BAM is not found by handle.\n", __func__);
  2039. return SPS_ERROR;
  2040. }
  2041. sps_bam_check_irq(bam);
  2042. return 0;
  2043. }
  2044. EXPORT_SYMBOL(sps_bam_process_irq);
  2045. /**
  2046. * Allocate client state context
  2047. *
  2048. */
  2049. struct sps_pipe *sps_alloc_endpoint(void)
  2050. {
  2051. struct sps_pipe *ctx = NULL;
  2052. SPS_DBG("sps:%s.", __func__);
  2053. ctx = kzalloc(sizeof(struct sps_pipe), GFP_KERNEL);
  2054. if (ctx == NULL) {
  2055. SPS_ERR("sps:Fail to allocate pipe context.");
  2056. return NULL;
  2057. }
  2058. sps_client_init(ctx);
  2059. return ctx;
  2060. }
  2061. EXPORT_SYMBOL(sps_alloc_endpoint);
  2062. /**
  2063. * Free client state context
  2064. *
  2065. */
  2066. int sps_free_endpoint(struct sps_pipe *ctx)
  2067. {
  2068. int res;
  2069. SPS_DBG("sps:%s.", __func__);
  2070. if (ctx == NULL) {
  2071. SPS_ERR("sps:%s:pipe is NULL.\n", __func__);
  2072. return SPS_ERROR;
  2073. }
  2074. res = sps_client_de_init(ctx);
  2075. if (res == 0)
  2076. kfree(ctx);
  2077. return res;
  2078. }
  2079. EXPORT_SYMBOL(sps_free_endpoint);
  2080. /**
  2081. * Platform Driver.
  2082. */
  2083. static int get_platform_data(struct platform_device *pdev)
  2084. {
  2085. struct resource *resource;
  2086. struct msm_sps_platform_data *pdata;
  2087. SPS_DBG("sps:%s.", __func__);
  2088. pdata = pdev->dev.platform_data;
  2089. if (pdata == NULL) {
  2090. SPS_ERR("sps:inavlid platform data.\n");
  2091. sps->bamdma_restricted_pipes = 0;
  2092. return -EINVAL;
  2093. } else {
  2094. sps->bamdma_restricted_pipes = pdata->bamdma_restricted_pipes;
  2095. SPS_DBG("sps:bamdma_restricted_pipes=0x%x.",
  2096. sps->bamdma_restricted_pipes);
  2097. }
  2098. resource = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  2099. "pipe_mem");
  2100. if (resource) {
  2101. sps->pipemem_phys_base = resource->start;
  2102. sps->pipemem_size = resource_size(resource);
  2103. SPS_DBG("sps:pipemem.base=%pa,size=0x%x.",
  2104. &sps->pipemem_phys_base,
  2105. sps->pipemem_size);
  2106. }
  2107. #ifdef CONFIG_SPS_SUPPORT_BAMDMA
  2108. resource = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  2109. "bamdma_bam");
  2110. if (resource) {
  2111. sps->bamdma_bam_phys_base = resource->start;
  2112. sps->bamdma_bam_size = resource_size(resource);
  2113. SPS_DBG("sps:bamdma_bam.base=%pa,size=0x%x.",
  2114. &sps->bamdma_bam_phys_base,
  2115. sps->bamdma_bam_size);
  2116. }
  2117. resource = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  2118. "bamdma_dma");
  2119. if (resource) {
  2120. sps->bamdma_dma_phys_base = resource->start;
  2121. sps->bamdma_dma_size = resource_size(resource);
  2122. SPS_DBG("sps:bamdma_dma.base=%pa,size=0x%x.",
  2123. &sps->bamdma_dma_phys_base,
  2124. sps->bamdma_dma_size);
  2125. }
  2126. resource = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
  2127. "bamdma_irq");
  2128. if (resource) {
  2129. sps->bamdma_irq = resource->start;
  2130. SPS_DBG("sps:bamdma_irq=%d.", sps->bamdma_irq);
  2131. }
  2132. #endif
  2133. return 0;
  2134. }
  2135. /**
  2136. * Read data from device tree
  2137. */
  2138. static int get_device_tree_data(struct platform_device *pdev)
  2139. {
  2140. #ifdef CONFIG_SPS_SUPPORT_BAMDMA
  2141. struct resource *resource;
  2142. SPS_DBG("sps:%s.", __func__);
  2143. if (of_property_read_u32((&pdev->dev)->of_node,
  2144. "qcom,bam-dma-res-pipes",
  2145. &sps->bamdma_restricted_pipes))
  2146. SPS_DBG("sps:No restricted bamdma pipes on this target.\n");
  2147. else
  2148. SPS_DBG("sps:bamdma_restricted_pipes=0x%x.",
  2149. sps->bamdma_restricted_pipes);
  2150. resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2151. if (resource) {
  2152. sps->bamdma_bam_phys_base = resource->start;
  2153. sps->bamdma_bam_size = resource_size(resource);
  2154. SPS_DBG("sps:bamdma_bam.base=%pa,size=0x%x.",
  2155. &sps->bamdma_bam_phys_base,
  2156. sps->bamdma_bam_size);
  2157. } else {
  2158. SPS_ERR("sps:BAM DMA BAM mem unavailable.");
  2159. return -ENODEV;
  2160. }
  2161. resource = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  2162. if (resource) {
  2163. sps->bamdma_dma_phys_base = resource->start;
  2164. sps->bamdma_dma_size = resource_size(resource);
  2165. SPS_DBG("sps:bamdma_dma.base=%pa,size=0x%x.",
  2166. &sps->bamdma_dma_phys_base,
  2167. sps->bamdma_dma_size);
  2168. } else {
  2169. SPS_ERR("sps:BAM DMA mem unavailable.");
  2170. return -ENODEV;
  2171. }
  2172. resource = platform_get_resource(pdev, IORESOURCE_MEM, 2);
  2173. if (resource) {
  2174. imem = true;
  2175. sps->pipemem_phys_base = resource->start;
  2176. sps->pipemem_size = resource_size(resource);
  2177. SPS_DBG("sps:pipemem.base=%pa,size=0x%x.",
  2178. &sps->pipemem_phys_base,
  2179. sps->pipemem_size);
  2180. } else {
  2181. imem = false;
  2182. SPS_DBG("sps:No pipe memory on this target.\n");
  2183. }
  2184. resource = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  2185. if (resource) {
  2186. sps->bamdma_irq = resource->start;
  2187. SPS_DBG("sps:bamdma_irq=%d.", sps->bamdma_irq);
  2188. } else {
  2189. SPS_ERR("sps:BAM DMA IRQ unavailable.");
  2190. return -ENODEV;
  2191. }
  2192. #endif
  2193. if (of_property_read_u32((&pdev->dev)->of_node,
  2194. "qcom,device-type",
  2195. &d_type)) {
  2196. d_type = 1;
  2197. SPS_DBG("sps:default device type.\n");
  2198. } else
  2199. SPS_DBG("sps:device type is %d.", d_type);
  2200. enhd_pipe = of_property_read_bool((&pdev->dev)->of_node,
  2201. "qcom,pipe-attr-ee");
  2202. SPS_DBG2("sps:PIPE_ATTR_EE is %s supported.\n",
  2203. (enhd_pipe ? "" : "not"));
  2204. return 0;
  2205. }
  2206. static struct of_device_id msm_sps_match[] = {
  2207. { .compatible = "qcom,msm_sps",
  2208. .data = &bam_types[SPS_BAM_NDP]
  2209. },
  2210. { .compatible = "qcom,msm_sps_4k",
  2211. .data = &bam_types[SPS_BAM_NDP_4K]
  2212. },
  2213. {}
  2214. };
  2215. static int __devinit msm_sps_probe(struct platform_device *pdev)
  2216. {
  2217. int ret = -ENODEV;
  2218. SPS_DBG2("sps:%s.", __func__);
  2219. if (pdev->dev.of_node) {
  2220. const struct of_device_id *match;
  2221. if (get_device_tree_data(pdev)) {
  2222. SPS_ERR("sps:Fail to get data from device tree.");
  2223. return -ENODEV;
  2224. } else
  2225. SPS_DBG("sps:get data from device tree.");
  2226. match = of_match_device(msm_sps_match, &pdev->dev);
  2227. if (match) {
  2228. bam_type = *((enum sps_bam_type *)(match->data));
  2229. SPS_DBG("sps:BAM type is:%d\n", bam_type);
  2230. } else {
  2231. bam_type = SPS_BAM_NDP;
  2232. SPS_DBG("sps:use default BAM type:%d\n", bam_type);
  2233. }
  2234. } else {
  2235. d_type = 0;
  2236. if (get_platform_data(pdev)) {
  2237. SPS_ERR("sps:Fail to get platform data.");
  2238. return -ENODEV;
  2239. } else
  2240. SPS_DBG("sps:get platform data.");
  2241. bam_type = SPS_BAM_LEGACY;
  2242. }
  2243. /* Create Device */
  2244. sps->dev_class = class_create(THIS_MODULE, SPS_DRV_NAME);
  2245. ret = alloc_chrdev_region(&sps->dev_num, 0, 1, SPS_DRV_NAME);
  2246. if (ret) {
  2247. SPS_ERR("sps:alloc_chrdev_region err.");
  2248. goto alloc_chrdev_region_err;
  2249. }
  2250. sps->dev = device_create(sps->dev_class, NULL, sps->dev_num, sps,
  2251. SPS_DRV_NAME);
  2252. if (IS_ERR(sps->dev)) {
  2253. SPS_ERR("sps:device_create err.");
  2254. goto device_create_err;
  2255. }
  2256. if (pdev->dev.of_node)
  2257. sps->dev->of_node = pdev->dev.of_node;
  2258. if (!d_type) {
  2259. sps->pmem_clk = clk_get(sps->dev, "mem_clk");
  2260. if (IS_ERR(sps->pmem_clk)) {
  2261. if (PTR_ERR(sps->pmem_clk) == -EPROBE_DEFER)
  2262. ret = -EPROBE_DEFER;
  2263. else
  2264. SPS_ERR("sps:fail to get pmem_clk.");
  2265. goto pmem_clk_err;
  2266. } else {
  2267. ret = clk_prepare_enable(sps->pmem_clk);
  2268. if (ret) {
  2269. SPS_ERR("sps:failed to enable pmem_clk.");
  2270. goto pmem_clk_en_err;
  2271. }
  2272. }
  2273. }
  2274. #ifdef CONFIG_SPS_SUPPORT_BAMDMA
  2275. sps->dfab_clk = clk_get(sps->dev, "dfab_clk");
  2276. if (IS_ERR(sps->dfab_clk)) {
  2277. if (PTR_ERR(sps->dfab_clk) == -EPROBE_DEFER)
  2278. ret = -EPROBE_DEFER;
  2279. else
  2280. SPS_ERR("sps:fail to get dfab_clk.");
  2281. goto dfab_clk_err;
  2282. } else {
  2283. ret = clk_set_rate(sps->dfab_clk, 64000000);
  2284. if (ret) {
  2285. SPS_ERR("sps:failed to set dfab_clk rate.");
  2286. clk_put(sps->dfab_clk);
  2287. goto dfab_clk_err;
  2288. }
  2289. }
  2290. sps->bamdma_clk = clk_get(sps->dev, "dma_bam_pclk");
  2291. if (IS_ERR(sps->bamdma_clk)) {
  2292. if (PTR_ERR(sps->bamdma_clk) == -EPROBE_DEFER)
  2293. ret = -EPROBE_DEFER;
  2294. else
  2295. SPS_ERR("sps:fail to get bamdma_clk.");
  2296. clk_put(sps->dfab_clk);
  2297. goto dfab_clk_err;
  2298. } else {
  2299. ret = clk_prepare_enable(sps->bamdma_clk);
  2300. if (ret) {
  2301. SPS_ERR("sps:failed to enable bamdma_clk. ret=%d", ret);
  2302. clk_put(sps->bamdma_clk);
  2303. clk_put(sps->dfab_clk);
  2304. goto dfab_clk_err;
  2305. }
  2306. }
  2307. ret = clk_prepare_enable(sps->dfab_clk);
  2308. if (ret) {
  2309. SPS_ERR("sps:failed to enable dfab_clk. ret=%d", ret);
  2310. clk_disable_unprepare(sps->bamdma_clk);
  2311. clk_put(sps->bamdma_clk);
  2312. clk_put(sps->dfab_clk);
  2313. goto dfab_clk_err;
  2314. }
  2315. #endif
  2316. ret = sps_device_init();
  2317. if (ret) {
  2318. SPS_ERR("sps:sps_device_init err.");
  2319. #ifdef CONFIG_SPS_SUPPORT_BAMDMA
  2320. clk_disable_unprepare(sps->dfab_clk);
  2321. clk_disable_unprepare(sps->bamdma_clk);
  2322. clk_put(sps->bamdma_clk);
  2323. clk_put(sps->dfab_clk);
  2324. #endif
  2325. goto dfab_clk_err;
  2326. }
  2327. #ifdef CONFIG_SPS_SUPPORT_BAMDMA
  2328. clk_disable_unprepare(sps->dfab_clk);
  2329. clk_disable_unprepare(sps->bamdma_clk);
  2330. #endif
  2331. sps->is_ready = true;
  2332. SPS_INFO("sps:sps is ready.");
  2333. return 0;
  2334. dfab_clk_err:
  2335. if (!d_type)
  2336. clk_disable_unprepare(sps->pmem_clk);
  2337. pmem_clk_en_err:
  2338. if (!d_type)
  2339. clk_put(sps->pmem_clk);
  2340. pmem_clk_err:
  2341. device_destroy(sps->dev_class, sps->dev_num);
  2342. device_create_err:
  2343. unregister_chrdev_region(sps->dev_num, 1);
  2344. alloc_chrdev_region_err:
  2345. class_destroy(sps->dev_class);
  2346. return ret;
  2347. }
  2348. static int __devexit msm_sps_remove(struct platform_device *pdev)
  2349. {
  2350. SPS_DBG("sps:%s.", __func__);
  2351. device_destroy(sps->dev_class, sps->dev_num);
  2352. unregister_chrdev_region(sps->dev_num, 1);
  2353. class_destroy(sps->dev_class);
  2354. sps_device_de_init();
  2355. clk_put(sps->dfab_clk);
  2356. if (!d_type)
  2357. clk_put(sps->pmem_clk);
  2358. clk_put(sps->bamdma_clk);
  2359. return 0;
  2360. }
  2361. static struct platform_driver msm_sps_driver = {
  2362. .probe = msm_sps_probe,
  2363. .driver = {
  2364. .name = SPS_DRV_NAME,
  2365. .owner = THIS_MODULE,
  2366. .of_match_table = msm_sps_match,
  2367. },
  2368. .remove = __exit_p(msm_sps_remove),
  2369. };
  2370. /**
  2371. * Module Init.
  2372. */
  2373. static int __init sps_init(void)
  2374. {
  2375. int ret;
  2376. #ifdef CONFIG_DEBUG_FS
  2377. sps_debugfs_init();
  2378. #endif
  2379. SPS_DBG("sps:%s.", __func__);
  2380. /* Allocate the SPS driver state struct */
  2381. sps = kzalloc(sizeof(*sps), GFP_KERNEL);
  2382. if (sps == NULL) {
  2383. SPS_ERR("sps:Unable to allocate driver state context.");
  2384. return -ENOMEM;
  2385. }
  2386. ret = platform_driver_register(&msm_sps_driver);
  2387. return ret;
  2388. }
  2389. /**
  2390. * Module Exit.
  2391. */
  2392. static void __exit sps_exit(void)
  2393. {
  2394. SPS_DBG("sps:%s.", __func__);
  2395. platform_driver_unregister(&msm_sps_driver);
  2396. if (sps != NULL) {
  2397. kfree(sps);
  2398. sps = NULL;
  2399. }
  2400. #ifdef CONFIG_DEBUG_FS
  2401. sps_debugfs_exit();
  2402. #endif
  2403. }
  2404. arch_initcall(sps_init);
  2405. module_exit(sps_exit);
  2406. MODULE_LICENSE("GPL v2");
  2407. MODULE_DESCRIPTION("Smart Peripheral Switch (SPS)");