usb_bam.c 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757
  1. /* Copyright (c) 2011-2013, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/types.h>
  13. #include <linux/kernel.h>
  14. #include <linux/list.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/clk.h>
  18. #include <linux/io.h>
  19. #include <linux/stat.h>
  20. #include <linux/module.h>
  21. #include <linux/of.h>
  22. #include <linux/usb/msm_hsusb.h>
  23. #include <mach/usb_bam.h>
  24. #include <mach/sps.h>
  25. #include <mach/ipa.h>
  26. #include <linux/workqueue.h>
  27. #include <linux/dma-mapping.h>
  28. #include <mach/msm_smsm.h>
  29. #include <linux/pm_runtime.h>
  30. #define USB_THRESHOLD 512
  31. #define USB_BAM_MAX_STR_LEN 50
  32. #define USB_BAM_TIMEOUT (10*HZ)
  33. enum usb_bam_sm {
  34. USB_BAM_SM_INIT = 0,
  35. USB_BAM_SM_PLUG_NOTIFIED,
  36. USB_BAM_SM_PLUG_ACKED,
  37. USB_BAM_SM_UNPLUG_NOTIFIED,
  38. };
  39. struct usb_bam_peer_handshake_info {
  40. enum usb_bam_sm state;
  41. bool client_ready;
  42. bool ack_received;
  43. int pending_work;
  44. struct usb_bam_event_info reset_event;
  45. };
  46. struct usb_bam_sps_type {
  47. struct sps_bam_props usb_props;
  48. struct sps_pipe **sps_pipes;
  49. struct sps_connect *sps_connections;
  50. };
  51. /**
  52. * struct usb_bam_ctx_type - represents the usb bam driver entity
  53. * @usb_bam_sps: holds the sps pipes the usb bam driver holds
  54. * against the sps driver.
  55. * @usb_bam_pdev: the platfrom device that represents the usb bam.
  56. * @usb_bam_wq: Worqueue used for managing states of reset against
  57. * a peer bam.
  58. * @qscratch_ram1_reg: The memory region mapped to the qscratch
  59. * registers.
  60. * @max_connections: The maximum number of pipes that are configured
  61. * in the platform data.
  62. * @mem_clk: Clock that controls the usb bam driver memory in
  63. * case the usb bam uses its private memory for the pipes.
  64. * @mem_iface_clk: Clock that controls the usb bam private memory in
  65. * case the usb bam uses its private memory for the pipes.
  66. * @qdss_core_name: Stores the name of the core ("ssusb", "hsusb" or "hsic")
  67. * that it used as a peer of the qdss in bam2bam mode.
  68. * @h_bam: This array stores for each BAM ("ssusb", "hsusb" or "hsic") the
  69. * handle/device of the sps driver.
  70. * @pipes_enabled_per_bam: This array stores for each BAM
  71. * ("ssusb", "hsusb" or "hsic") the number of pipes currently enabled.
  72. * @inactivity_timer_ms: The timeout configuration per each bam for inactivity
  73. * timer feature.
  74. * @is_bam_inactivity: Is there no activity on all pipes belongs to a
  75. * specific bam. (no activity = no data is pulled or pushed
  76. * from/into ones of the pipes).
  77. */
  78. struct usb_bam_ctx_type {
  79. struct usb_bam_sps_type usb_bam_sps;
  80. struct platform_device *usb_bam_pdev;
  81. struct workqueue_struct *usb_bam_wq;
  82. void __iomem *qscratch_ram1_reg;
  83. u8 max_connections;
  84. struct clk *mem_clk;
  85. struct clk *mem_iface_clk;
  86. char qdss_core_name[USB_BAM_MAX_STR_LEN];
  87. u32 h_bam[MAX_BAMS];
  88. u8 pipes_enabled_per_bam[MAX_BAMS];
  89. u32 inactivity_timer_ms[MAX_BAMS];
  90. bool is_bam_inactivity[MAX_BAMS];
  91. struct completion reset_done;
  92. };
  93. static char *bam_enable_strings[MAX_BAMS] = {
  94. [SSUSB_BAM] = "ssusb",
  95. [HSUSB_BAM] = "hsusb",
  96. [HSIC_BAM] = "hsic",
  97. };
  98. static enum usb_bam ipa_rm_bams[] = {HSUSB_BAM, HSIC_BAM};
  99. static enum ipa_client_type ipa_rm_resource_prod[MAX_BAMS] = {
  100. [HSUSB_BAM] = IPA_RM_RESOURCE_USB_PROD,
  101. [HSIC_BAM] = IPA_RM_RESOURCE_HSIC_PROD,
  102. };
  103. static enum ipa_client_type ipa_rm_resource_cons[MAX_BAMS] = {
  104. [HSUSB_BAM] = IPA_RM_RESOURCE_USB_CONS,
  105. [HSIC_BAM] = IPA_RM_RESOURCE_HSIC_CONS,
  106. };
  107. static int usb_cons_request_resource(void);
  108. static int usb_cons_release_resource(void);
  109. static int hsic_cons_request_resource(void);
  110. static int hsic_cons_release_resource(void);
  111. static int (*request_resource_cb[MAX_BAMS])(void) = {
  112. [HSUSB_BAM] = usb_cons_request_resource,
  113. [HSIC_BAM] = hsic_cons_request_resource,
  114. };
  115. static int (*release_resource_cb[MAX_BAMS])(void) = {
  116. [HSUSB_BAM] = usb_cons_release_resource,
  117. [HSIC_BAM] = hsic_cons_release_resource,
  118. };
  119. struct usb_bam_ipa_handshake_info {
  120. enum ipa_rm_event cur_prod_state[MAX_BAMS];
  121. enum ipa_rm_event cur_cons_state[MAX_BAMS];
  122. bool lpm_wait_handshake[MAX_BAMS];
  123. int connect_complete;
  124. bool lpm_wait_pipes;
  125. int bus_suspend;
  126. bool disconnected;
  127. bool in_lpm[MAX_BAMS];
  128. bool pending_lpm;
  129. int (*wake_cb)(void *);
  130. void *wake_param;
  131. void (*start)(void *, enum usb_bam_pipe_dir);
  132. void (*stop)(void *, enum usb_bam_pipe_dir);
  133. void *start_stop_param;
  134. u32 src_idx;
  135. u32 dst_idx;
  136. bool cons_stopped;
  137. bool prod_stopped;
  138. struct completion prod_avail[MAX_BAMS];
  139. struct completion prod_released[MAX_BAMS];
  140. struct mutex suspend_resume_mutex;
  141. struct work_struct resume_work;
  142. struct work_struct suspend_work;
  143. struct work_struct finish_suspend_work;
  144. };
  145. struct usb_bam_hsic_host_info {
  146. struct device *dev;
  147. bool in_lpm;
  148. };
  149. static spinlock_t usb_bam_ipa_handshake_info_lock;
  150. static struct usb_bam_ipa_handshake_info info;
  151. static spinlock_t usb_bam_peer_handshake_info_lock;
  152. static struct usb_bam_peer_handshake_info peer_handshake_info;
  153. static spinlock_t usb_bam_lock; /* Protect ctx and usb_bam_connections */
  154. static struct usb_bam_pipe_connect *usb_bam_connections;
  155. static struct usb_bam_ctx_type ctx;
  156. static struct usb_bam_hsic_host_info hsic_host_info;
  157. static int __usb_bam_register_wake_cb(u8 idx, int (*callback)(void *user),
  158. void *param, bool trigger_cb_per_pipe);
  159. static void wait_for_prod_release(enum usb_bam cur_bam);
  160. void msm_bam_set_hsic_host_dev(struct device *dev)
  161. {
  162. if (dev) {
  163. /* Hold the device until allowing lpm */
  164. info.in_lpm[HSIC_BAM] = false;
  165. pr_debug("%s: Getting hsic device %x\n", __func__,
  166. (int)dev);
  167. pm_runtime_get(dev);
  168. } else if (hsic_host_info.dev) {
  169. pr_debug("%s: Putting hsic device %x\n", __func__,
  170. (int)hsic_host_info.dev);
  171. /* Just free previous device*/
  172. info.in_lpm[HSIC_BAM] = true;
  173. pm_runtime_put(hsic_host_info.dev);
  174. }
  175. hsic_host_info.dev = dev;
  176. hsic_host_info.in_lpm = false;
  177. }
  178. static int get_bam_type_from_core_name(const char *name)
  179. {
  180. if (strnstr(name, bam_enable_strings[SSUSB_BAM],
  181. USB_BAM_MAX_STR_LEN) ||
  182. strnstr(name, "dwc3", USB_BAM_MAX_STR_LEN))
  183. return SSUSB_BAM;
  184. else if (strnstr(name, bam_enable_strings[HSIC_BAM],
  185. USB_BAM_MAX_STR_LEN))
  186. return HSIC_BAM;
  187. else if (strnstr(name, bam_enable_strings[HSUSB_BAM],
  188. USB_BAM_MAX_STR_LEN) ||
  189. strnstr(name, "ci", USB_BAM_MAX_STR_LEN))
  190. return HSUSB_BAM;
  191. pr_err("%s: invalid BAM name(%s)\n", __func__, name);
  192. return -EINVAL;
  193. }
  194. static bool bam_use_private_mem(enum usb_bam bam)
  195. {
  196. int i;
  197. for (i = 0; i < ctx.max_connections; i++)
  198. if (usb_bam_connections[i].bam_type == bam &&
  199. usb_bam_connections[i].mem_type == USB_PRIVATE_MEM)
  200. return true;
  201. return false;
  202. }
  203. static void usb_bam_set_inactivity_timer(enum usb_bam bam)
  204. {
  205. struct sps_timer_ctrl timer_ctrl;
  206. struct usb_bam_pipe_connect *pipe_connect;
  207. struct sps_pipe *pipe = NULL;
  208. int i;
  209. pr_debug("%s: enter\n", __func__);
  210. /*
  211. * Since we configure global incativity timer for all pipes
  212. * and not per each pipe, it is enough to use some pipe
  213. * handle associated with this bam, so just find the first one.
  214. * This pipe handle is required due to SPS driver API we use below.
  215. */
  216. for (i = 0; i < ctx.max_connections; i++) {
  217. pipe_connect = &usb_bam_connections[i];
  218. if (pipe_connect->bam_type == bam &&
  219. pipe_connect->enabled) {
  220. pipe = ctx.usb_bam_sps.sps_pipes[i];
  221. break;
  222. }
  223. }
  224. if (!pipe) {
  225. pr_warning("%s: Bam %s has no connected pipes\n", __func__,
  226. bam_enable_strings[bam]);
  227. return;
  228. }
  229. timer_ctrl.op = SPS_TIMER_OP_CONFIG;
  230. timer_ctrl.mode = SPS_TIMER_MODE_ONESHOT;
  231. timer_ctrl.timeout_msec = ctx.inactivity_timer_ms[bam];
  232. sps_timer_ctrl(pipe, &timer_ctrl, NULL);
  233. timer_ctrl.op = SPS_TIMER_OP_RESET;
  234. sps_timer_ctrl(pipe, &timer_ctrl, NULL);
  235. }
  236. static int connect_pipe(u8 idx, u32 *usb_pipe_idx)
  237. {
  238. int ret, ram1_value;
  239. enum usb_bam bam;
  240. struct usb_bam_sps_type usb_bam_sps = ctx.usb_bam_sps;
  241. struct sps_pipe **pipe = &(usb_bam_sps.sps_pipes[idx]);
  242. struct sps_connect *sps_connection = &usb_bam_sps.sps_connections[idx];
  243. struct msm_usb_bam_platform_data *pdata =
  244. ctx.usb_bam_pdev->dev.platform_data;
  245. struct usb_bam_pipe_connect *pipe_connect = &usb_bam_connections[idx];
  246. enum usb_bam_pipe_dir dir = pipe_connect->dir;
  247. struct sps_mem_buffer *data_buf = &(pipe_connect->data_mem_buf);
  248. struct sps_mem_buffer *desc_buf = &(pipe_connect->desc_mem_buf);
  249. *pipe = sps_alloc_endpoint();
  250. if (*pipe == NULL) {
  251. pr_err("%s: sps_alloc_endpoint failed\n", __func__);
  252. return -ENOMEM;
  253. }
  254. ret = sps_get_config(*pipe, sps_connection);
  255. if (ret) {
  256. pr_err("%s: tx get config failed %d\n", __func__, ret);
  257. goto free_sps_endpoint;
  258. }
  259. ret = sps_phy2h(pipe_connect->src_phy_addr, &(sps_connection->source));
  260. if (ret) {
  261. pr_err("%s: sps_phy2h failed (src BAM) %d\n", __func__, ret);
  262. goto free_sps_endpoint;
  263. }
  264. sps_connection->src_pipe_index = pipe_connect->src_pipe_index;
  265. ret = sps_phy2h(pipe_connect->dst_phy_addr,
  266. &(sps_connection->destination));
  267. if (ret) {
  268. pr_err("%s: sps_phy2h failed (dst BAM) %d\n", __func__, ret);
  269. goto free_sps_endpoint;
  270. }
  271. sps_connection->dest_pipe_index = pipe_connect->dst_pipe_index;
  272. if (dir == USB_TO_PEER_PERIPHERAL) {
  273. sps_connection->mode = SPS_MODE_SRC;
  274. *usb_pipe_idx = pipe_connect->src_pipe_index;
  275. } else {
  276. sps_connection->mode = SPS_MODE_DEST;
  277. *usb_pipe_idx = pipe_connect->dst_pipe_index;
  278. }
  279. switch (pipe_connect->mem_type) {
  280. case SPS_PIPE_MEM:
  281. pr_debug("%s: USB BAM using SPS pipe memory\n", __func__);
  282. ret = sps_setup_bam2bam_fifo(
  283. data_buf,
  284. pipe_connect->data_fifo_base_offset,
  285. pipe_connect->data_fifo_size, 1);
  286. if (ret) {
  287. pr_err("%s: data fifo setup failure %d\n", __func__,
  288. ret);
  289. goto free_sps_endpoint;
  290. }
  291. ret = sps_setup_bam2bam_fifo(
  292. desc_buf,
  293. pipe_connect->desc_fifo_base_offset,
  294. pipe_connect->desc_fifo_size, 1);
  295. if (ret) {
  296. pr_err("%s: desc. fifo setup failure %d\n", __func__,
  297. ret);
  298. goto free_sps_endpoint;
  299. }
  300. break;
  301. case USB_PRIVATE_MEM:
  302. pr_debug("%s: USB BAM using private memory\n", __func__);
  303. if (IS_ERR(ctx.mem_clk) || IS_ERR(ctx.mem_iface_clk)) {
  304. pr_err("%s: Failed to enable USB mem_clk\n", __func__);
  305. ret = IS_ERR(ctx.mem_clk);
  306. goto free_sps_endpoint;
  307. }
  308. clk_prepare_enable(ctx.mem_clk);
  309. clk_prepare_enable(ctx.mem_iface_clk);
  310. /*
  311. * Enable USB PRIVATE RAM to be used for BAM FIFOs
  312. * HSUSB: Only RAM13 is used for BAM FIFOs
  313. * SSUSB: RAM11, 12, 13 are used for BAM FIFOs
  314. */
  315. bam = pipe_connect->bam_type;
  316. if (bam < 0)
  317. goto free_sps_endpoint;
  318. if (bam == HSUSB_BAM)
  319. ram1_value = 0x4;
  320. else
  321. ram1_value = 0x7;
  322. pr_debug("Writing 0x%x to QSCRATCH_RAM1\n", ram1_value);
  323. writel_relaxed(ram1_value, ctx.qscratch_ram1_reg);
  324. /* fall through */
  325. case OCI_MEM:
  326. pr_debug("%s: USB BAM using oci memory\n", __func__);
  327. data_buf->phys_base =
  328. pipe_connect->data_fifo_base_offset +
  329. pdata->usb_bam_fifo_baseaddr;
  330. data_buf->size = pipe_connect->data_fifo_size;
  331. data_buf->base =
  332. ioremap(data_buf->phys_base, data_buf->size);
  333. memset(data_buf->base, 0, data_buf->size);
  334. desc_buf->phys_base =
  335. pipe_connect->desc_fifo_base_offset +
  336. pdata->usb_bam_fifo_baseaddr;
  337. desc_buf->size = pipe_connect->desc_fifo_size;
  338. desc_buf->base =
  339. ioremap(desc_buf->phys_base, desc_buf->size);
  340. memset(desc_buf->base, 0, desc_buf->size);
  341. break;
  342. case SYSTEM_MEM:
  343. pr_debug("%s: USB BAM using system memory\n", __func__);
  344. /* BAM would use system memory, allocate FIFOs */
  345. data_buf->size = pipe_connect->data_fifo_size;
  346. data_buf->base =
  347. dma_alloc_coherent(&ctx.usb_bam_pdev->dev,
  348. pipe_connect->data_fifo_size,
  349. &(data_buf->phys_base),
  350. 0);
  351. memset(data_buf->base, 0, pipe_connect->data_fifo_size);
  352. desc_buf->size = pipe_connect->desc_fifo_size;
  353. desc_buf->base =
  354. dma_alloc_coherent(&ctx.usb_bam_pdev->dev,
  355. pipe_connect->desc_fifo_size,
  356. &(desc_buf->phys_base),
  357. 0);
  358. memset(desc_buf->base, 0, pipe_connect->desc_fifo_size);
  359. break;
  360. default:
  361. pr_err("%s: invalid mem type\n", __func__);
  362. goto free_sps_endpoint;
  363. }
  364. sps_connection->data = *data_buf;
  365. sps_connection->desc = *desc_buf;
  366. sps_connection->event_thresh = 16;
  367. sps_connection->options = SPS_O_AUTO_ENABLE;
  368. ret = sps_connect(*pipe, sps_connection);
  369. if (ret < 0) {
  370. pr_err("%s: sps_connect failed %d\n", __func__, ret);
  371. goto error;
  372. }
  373. return 0;
  374. error:
  375. sps_disconnect(*pipe);
  376. free_sps_endpoint:
  377. sps_free_endpoint(*pipe);
  378. return ret;
  379. }
  380. static int connect_pipe_ipa(u8 idx,
  381. struct usb_bam_connect_ipa_params *ipa_params)
  382. {
  383. int ret;
  384. struct usb_bam_sps_type usb_bam_sps = ctx.usb_bam_sps;
  385. enum usb_bam_pipe_dir dir = ipa_params->dir;
  386. struct sps_pipe **pipe = &(usb_bam_sps.sps_pipes[idx]);
  387. struct sps_connect *sps_connection = &usb_bam_sps.sps_connections[idx];
  388. struct usb_bam_pipe_connect *pipe_connect = &usb_bam_connections[idx];
  389. struct ipa_connect_params ipa_in_params;
  390. struct ipa_sps_params sps_out_params;
  391. u32 usb_handle, usb_phy_addr;
  392. u32 clnt_hdl = 0;
  393. memset(&ipa_in_params, 0, sizeof(ipa_in_params));
  394. memset(&sps_out_params, 0, sizeof(sps_out_params));
  395. if (dir == USB_TO_PEER_PERIPHERAL) {
  396. usb_phy_addr = pipe_connect->src_phy_addr;
  397. ipa_in_params.client_ep_idx = pipe_connect->src_pipe_index;
  398. } else {
  399. usb_phy_addr = pipe_connect->dst_phy_addr;
  400. ipa_in_params.client_ep_idx = pipe_connect->dst_pipe_index;
  401. }
  402. /* Get HSUSB / HSIC bam handle */
  403. ret = sps_phy2h(usb_phy_addr, &usb_handle);
  404. if (ret) {
  405. pr_err("%s: sps_phy2h failed (HSUSB/HSIC BAM) %d\n",
  406. __func__, ret);
  407. return ret;
  408. }
  409. pipe_connect->activity_notify = ipa_params->activity_notify;
  410. pipe_connect->inactivity_notify = ipa_params->inactivity_notify;
  411. pipe_connect->priv = ipa_params->priv;
  412. /* IPA input parameters */
  413. ipa_in_params.client_bam_hdl = usb_handle;
  414. ipa_in_params.desc_fifo_sz = pipe_connect->desc_fifo_size;
  415. ipa_in_params.data_fifo_sz = pipe_connect->data_fifo_size;
  416. ipa_in_params.notify = ipa_params->notify;
  417. ipa_in_params.priv = ipa_params->priv;
  418. ipa_in_params.client = ipa_params->client;
  419. /* If BAM is using dedicated SPS pipe memory, get it */
  420. if (pipe_connect->mem_type == SPS_PIPE_MEM) {
  421. pr_debug("%s: USB BAM using SPS pipe memory\n", __func__);
  422. ret = sps_setup_bam2bam_fifo(
  423. &pipe_connect->data_mem_buf,
  424. pipe_connect->data_fifo_base_offset,
  425. pipe_connect->data_fifo_size, 1);
  426. if (ret) {
  427. pr_err("%s: data fifo setup failure %d\n",
  428. __func__, ret);
  429. return ret;
  430. }
  431. ret = sps_setup_bam2bam_fifo(
  432. &pipe_connect->desc_mem_buf,
  433. pipe_connect->desc_fifo_base_offset,
  434. pipe_connect->desc_fifo_size, 1);
  435. if (ret) {
  436. pr_err("%s: desc. fifo setup failure %d\n",
  437. __func__, ret);
  438. return ret;
  439. }
  440. ipa_in_params.desc = pipe_connect->desc_mem_buf;
  441. ipa_in_params.data = pipe_connect->data_mem_buf;
  442. }
  443. memcpy(&ipa_in_params.ipa_ep_cfg, &ipa_params->ipa_ep_cfg,
  444. sizeof(struct ipa_ep_cfg));
  445. ret = ipa_connect(&ipa_in_params, &sps_out_params, &clnt_hdl);
  446. if (ret) {
  447. pr_err("%s: ipa_connect failed\n", __func__);
  448. return ret;
  449. }
  450. pipe_connect->ipa_clnt_hdl = clnt_hdl;
  451. *pipe = sps_alloc_endpoint();
  452. if (*pipe == NULL) {
  453. pr_err("%s: sps_alloc_endpoint failed\n", __func__);
  454. ret = -ENOMEM;
  455. goto disconnect_ipa;
  456. }
  457. ret = sps_get_config(*pipe, sps_connection);
  458. if (ret) {
  459. pr_err("%s: tx get config failed %d\n", __func__, ret);
  460. goto free_sps_endpoints;
  461. }
  462. if (dir == USB_TO_PEER_PERIPHERAL) {
  463. /* USB src IPA dest */
  464. sps_connection->mode = SPS_MODE_SRC;
  465. ipa_params->cons_clnt_hdl = clnt_hdl;
  466. sps_connection->source = usb_handle;
  467. sps_connection->destination = sps_out_params.ipa_bam_hdl;
  468. sps_connection->src_pipe_index = pipe_connect->src_pipe_index;
  469. sps_connection->dest_pipe_index = sps_out_params.ipa_ep_idx;
  470. *(ipa_params->src_pipe) = sps_connection->src_pipe_index;
  471. pipe_connect->dst_pipe_index = sps_out_params.ipa_ep_idx;
  472. pr_debug("%s: BAM pipe usb[%x]->ipa[%x] connection\n",
  473. __func__,
  474. pipe_connect->src_pipe_index,
  475. pipe_connect->dst_pipe_index);
  476. sps_connection->options = SPS_O_NO_DISABLE;
  477. } else {
  478. /* IPA src, USB dest */
  479. sps_connection->mode = SPS_MODE_DEST;
  480. ipa_params->prod_clnt_hdl = clnt_hdl;
  481. sps_connection->source = sps_out_params.ipa_bam_hdl;
  482. sps_connection->destination = usb_handle;
  483. sps_connection->src_pipe_index = sps_out_params.ipa_ep_idx;
  484. sps_connection->dest_pipe_index = pipe_connect->dst_pipe_index;
  485. *(ipa_params->dst_pipe) = sps_connection->dest_pipe_index;
  486. pipe_connect->src_pipe_index = sps_out_params.ipa_ep_idx;
  487. pr_debug("%s: BAM pipe ipa[%x]->usb[%x] connection\n",
  488. __func__,
  489. pipe_connect->src_pipe_index,
  490. pipe_connect->dst_pipe_index);
  491. sps_connection->options = 0;
  492. }
  493. sps_connection->data = sps_out_params.data;
  494. sps_connection->desc = sps_out_params.desc;
  495. sps_connection->event_thresh = 16;
  496. sps_connection->options |= SPS_O_AUTO_ENABLE;
  497. ret = sps_connect(*pipe, sps_connection);
  498. if (ret < 0) {
  499. pr_err("%s: sps_connect failed %d\n", __func__, ret);
  500. goto error;
  501. }
  502. return 0;
  503. error:
  504. sps_disconnect(*pipe);
  505. free_sps_endpoints:
  506. sps_free_endpoint(*pipe);
  507. disconnect_ipa:
  508. ipa_disconnect(clnt_hdl);
  509. return ret;
  510. }
  511. static int disconnect_pipe(u8 idx)
  512. {
  513. struct usb_bam_pipe_connect *pipe_connect =
  514. &usb_bam_connections[idx];
  515. struct sps_pipe *pipe = ctx.usb_bam_sps.sps_pipes[idx];
  516. struct sps_connect *sps_connection =
  517. &ctx.usb_bam_sps.sps_connections[idx];
  518. sps_disconnect(pipe);
  519. sps_free_endpoint(pipe);
  520. switch (pipe_connect->mem_type) {
  521. case SYSTEM_MEM:
  522. pr_debug("%s: Freeing system memory used by PIPE\n", __func__);
  523. if (sps_connection->data.phys_base)
  524. dma_free_coherent(&ctx.usb_bam_pdev->dev,
  525. sps_connection->data.size,
  526. sps_connection->data.base,
  527. sps_connection->data.phys_base);
  528. if (sps_connection->desc.phys_base)
  529. dma_free_coherent(&ctx.usb_bam_pdev->dev,
  530. sps_connection->desc.size,
  531. sps_connection->desc.base,
  532. sps_connection->desc.phys_base);
  533. break;
  534. case USB_PRIVATE_MEM:
  535. pr_debug("Freeing private memory used by BAM PIPE\n");
  536. writel_relaxed(0x0, ctx.qscratch_ram1_reg);
  537. clk_disable_unprepare(ctx.mem_clk);
  538. clk_disable_unprepare(ctx.mem_iface_clk);
  539. case OCI_MEM:
  540. pr_debug("Freeing oci memory used by BAM PIPE\n");
  541. iounmap(sps_connection->data.base);
  542. iounmap(sps_connection->desc.base);
  543. break;
  544. case SPS_PIPE_MEM:
  545. pr_debug("%s: nothing to be be\n", __func__);
  546. break;
  547. }
  548. sps_connection->options &= ~SPS_O_AUTO_ENABLE;
  549. return 0;
  550. }
  551. static void usb_bam_resume_core(enum usb_bam cur_bam)
  552. {
  553. struct usb_phy *trans = usb_get_transceiver();
  554. if (cur_bam != HSUSB_BAM)
  555. return;
  556. BUG_ON(trans == NULL);
  557. pr_debug("%s: resume core", __func__);
  558. pm_runtime_resume(trans->dev);
  559. }
  560. static void usb_bam_start_lpm(bool disconnect)
  561. {
  562. struct usb_phy *trans = usb_get_transceiver();
  563. BUG_ON(trans == NULL);
  564. spin_lock(&usb_bam_ipa_handshake_info_lock);
  565. info.lpm_wait_handshake[HSUSB_BAM] = false;
  566. info.lpm_wait_pipes = 0;
  567. if (disconnect)
  568. pm_runtime_put_noidle(trans->dev);
  569. if (info.pending_lpm) {
  570. info.pending_lpm = 0;
  571. spin_unlock(&usb_bam_ipa_handshake_info_lock);
  572. pr_debug("%s: Going to LPM\n", __func__);
  573. pm_runtime_suspend(trans->dev);
  574. } else
  575. spin_unlock(&usb_bam_ipa_handshake_info_lock);
  576. }
  577. int usb_bam_connect(u8 idx, u32 *bam_pipe_idx)
  578. {
  579. int ret;
  580. struct usb_bam_pipe_connect *pipe_connect = &usb_bam_connections[idx];
  581. struct msm_usb_bam_platform_data *pdata;
  582. if (!ctx.usb_bam_pdev) {
  583. pr_err("%s: usb_bam device not found\n", __func__);
  584. return -ENODEV;
  585. }
  586. pdata = ctx.usb_bam_pdev->dev.platform_data;
  587. if (pipe_connect->enabled) {
  588. pr_debug("%s: connection %d was already established\n",
  589. __func__, idx);
  590. return 0;
  591. }
  592. if (!bam_pipe_idx) {
  593. pr_err("%s: invalid bam_pipe_idx\n", __func__);
  594. return -EINVAL;
  595. }
  596. if (idx < 0 || idx > ctx.max_connections) {
  597. pr_err("idx is wrong %d", idx);
  598. return -EINVAL;
  599. }
  600. spin_lock(&usb_bam_lock);
  601. /* Check if BAM requires RESET before connect and reset of first pipe */
  602. if ((pdata->reset_on_connect[pipe_connect->bam_type] == true) &&
  603. (ctx.pipes_enabled_per_bam[pipe_connect->bam_type] == 0))
  604. sps_device_reset(ctx.h_bam[pipe_connect->bam_type]);
  605. spin_unlock(&usb_bam_lock);
  606. ret = connect_pipe(idx, bam_pipe_idx);
  607. if (ret) {
  608. pr_err("%s: pipe connection[%d] failure\n", __func__, idx);
  609. return ret;
  610. }
  611. pipe_connect->enabled = 1;
  612. spin_lock(&usb_bam_lock);
  613. ctx.pipes_enabled_per_bam[pipe_connect->bam_type] += 1;
  614. spin_unlock(&usb_bam_lock);
  615. return 0;
  616. }
  617. static int ipa_suspend_pipes(void)
  618. {
  619. struct usb_bam_pipe_connect *dst_pipe_connect, *src_pipe_connect;
  620. int ret1, ret2;
  621. dst_pipe_connect = &usb_bam_connections[info.dst_idx];
  622. src_pipe_connect = &usb_bam_connections[info.src_idx];
  623. if (dst_pipe_connect->ipa_clnt_hdl == -1 ||
  624. src_pipe_connect->ipa_clnt_hdl == -1) {
  625. pr_err("%s: One of handles is -1, not connected?", __func__);
  626. }
  627. ret1 = ipa_suspend(dst_pipe_connect->ipa_clnt_hdl);
  628. if (ret1)
  629. pr_err("%s: ipa_suspend on dst failed with %d", __func__, ret1);
  630. ret2 = ipa_suspend(src_pipe_connect->ipa_clnt_hdl);
  631. if (ret2)
  632. pr_err("%s: ipa_suspend on src failed with %d", __func__, ret2);
  633. return ret1 | ret2;
  634. }
  635. static int ipa_resume_pipes(void)
  636. {
  637. struct usb_bam_pipe_connect *dst_pipe_connect, *src_pipe_connect;
  638. int ret1, ret2;
  639. src_pipe_connect = &usb_bam_connections[info.src_idx];
  640. dst_pipe_connect = &usb_bam_connections[info.dst_idx];
  641. if (dst_pipe_connect->ipa_clnt_hdl == -1 ||
  642. src_pipe_connect->ipa_clnt_hdl == -1) {
  643. pr_err("%s: One of handles is -1, not connected?", __func__);
  644. }
  645. ret1 = ipa_resume(dst_pipe_connect->ipa_clnt_hdl);
  646. if (ret1)
  647. pr_err("%s: ipa_resume on dst failed with %d", __func__, ret1);
  648. ret2 = ipa_resume(src_pipe_connect->ipa_clnt_hdl);
  649. if (ret2)
  650. pr_err("%s: ipa_resume on src failed with %d", __func__, ret2);
  651. return ret1 | ret2;
  652. }
  653. static void usb_bam_finish_suspend(void)
  654. {
  655. int ret;
  656. u32 cons_empty;
  657. struct sps_pipe *cons_pipe = ctx.usb_bam_sps.sps_pipes[info.dst_idx];
  658. struct usb_bam_pipe_connect *pipe_connect = &
  659. usb_bam_connections[info.dst_idx];
  660. enum usb_bam cur_bam = pipe_connect->bam_type;
  661. if (cur_bam != HSUSB_BAM) {
  662. pr_err("%s: Wrong type of BAM=%s\n", __func__,
  663. bam_enable_strings[cur_bam]);
  664. return;
  665. }
  666. mutex_lock(&info.suspend_resume_mutex);
  667. spin_lock(&usb_bam_ipa_handshake_info_lock);
  668. /* If cable was disconnected, let disconnection seq do everything */
  669. if (info.disconnected || info.cons_stopped) {
  670. spin_unlock(&usb_bam_ipa_handshake_info_lock);
  671. mutex_unlock(&info.suspend_resume_mutex);
  672. pr_debug("%s: Cable disconnected\n", __func__);
  673. return;
  674. }
  675. /* If resume was called don't finish this work */
  676. if (!info.bus_suspend) {
  677. spin_unlock(&usb_bam_ipa_handshake_info_lock);
  678. pr_err("%s: Bus resume in progress\n", __func__);
  679. goto no_lpm;
  680. }
  681. spin_unlock(&usb_bam_ipa_handshake_info_lock);
  682. ret = sps_is_pipe_empty(cons_pipe, &cons_empty);
  683. if (ret) {
  684. pr_err("%s: sps_is_pipe_empty failed with %d\n", __func__, ret);
  685. goto no_lpm;
  686. }
  687. /* Stop CONS transfers and go to lpm if no more data in the pipe */
  688. if (cons_empty) {
  689. if (info.stop && !info.cons_stopped)
  690. info.stop(info.start_stop_param,
  691. PEER_PERIPHERAL_TO_USB);
  692. pr_err("%s: Starting LPM on Bus Suspend\n", __func__);
  693. info.cons_stopped = 1;
  694. if (info.cur_cons_state[cur_bam] == IPA_RM_RESOURCE_RELEASED) {
  695. ipa_rm_notify_completion(IPA_RM_RESOURCE_RELEASED,
  696. ipa_rm_resource_cons[cur_bam]);
  697. }
  698. ipa_suspend_pipes();
  699. usb_bam_start_lpm(0);
  700. mutex_unlock(&info.suspend_resume_mutex);
  701. return;
  702. }
  703. no_lpm:
  704. /* Finish the handshake. Resume Sequence will start automatically
  705. by the data in the pipes */
  706. if (info.cur_cons_state[cur_bam] == IPA_RM_RESOURCE_RELEASED)
  707. ipa_rm_notify_completion(IPA_RM_RESOURCE_RELEASED,
  708. ipa_rm_resource_cons[cur_bam]);
  709. mutex_unlock(&info.suspend_resume_mutex);
  710. }
  711. void usb_bam_finish_suspend_(struct work_struct *w)
  712. {
  713. usb_bam_finish_suspend();
  714. }
  715. static void usb_prod_notify_cb(void *user_data, enum ipa_rm_event event,
  716. unsigned long data)
  717. {
  718. enum usb_bam *cur_bam = (void *)user_data;
  719. switch (event) {
  720. case IPA_RM_RESOURCE_GRANTED:
  721. pr_debug("%s: %s_PROD resource granted\n",
  722. __func__, bam_enable_strings[*cur_bam]);
  723. info.cur_prod_state[*cur_bam] = IPA_RM_RESOURCE_GRANTED;
  724. complete_all(&info.prod_avail[*cur_bam]);
  725. break;
  726. case IPA_RM_RESOURCE_RELEASED:
  727. pr_debug("%s: %s_PROD resource released\n",
  728. __func__, bam_enable_strings[*cur_bam]);
  729. info.cur_prod_state[*cur_bam] = IPA_RM_RESOURCE_RELEASED;
  730. complete_all(&info.prod_released[*cur_bam]);
  731. break;
  732. default:
  733. break;
  734. }
  735. return;
  736. }
  737. /**
  738. * usb_bam_resume_hsic_host: vote for hsic host core resume.
  739. * In addition also resume all hsic pipes that are connected to
  740. * the ipa peer bam.
  741. *
  742. * NOTE: This function should be called in a context that hold
  743. * usb_bam_lock.
  744. */
  745. static void usb_bam_resume_hsic_host(void)
  746. {
  747. int i;
  748. struct usb_bam_pipe_connect *pipe_iter;
  749. /* Exit from "full suspend" in case of hsic host */
  750. if (hsic_host_info.dev && info.in_lpm[HSIC_BAM]) {
  751. pr_debug("%s: Getting hsic device %x\n", __func__,
  752. (int)hsic_host_info.dev);
  753. pm_runtime_get(hsic_host_info.dev);
  754. info.in_lpm[HSIC_BAM] = false;
  755. for (i = 0; i < ctx.max_connections; i++) {
  756. pipe_iter = &usb_bam_connections[i];
  757. if (pipe_iter->bam_type == HSIC_BAM &&
  758. pipe_iter->enabled &&
  759. pipe_iter->suspended) {
  760. spin_unlock(&usb_bam_lock);
  761. ipa_resume(pipe_iter->ipa_clnt_hdl);
  762. pipe_iter->suspended = false;
  763. spin_lock(&usb_bam_lock);
  764. }
  765. }
  766. }
  767. }
  768. static int cons_request_resource(enum usb_bam cur_bam)
  769. {
  770. int ret = -EINPROGRESS;
  771. pr_debug("%s: Request %s_CONS resource\n",
  772. __func__, bam_enable_strings[cur_bam]);
  773. spin_lock(&usb_bam_ipa_handshake_info_lock);
  774. info.cur_cons_state[cur_bam] = IPA_RM_RESOURCE_GRANTED;
  775. spin_lock(&usb_bam_lock);
  776. switch (cur_bam) {
  777. case HSUSB_BAM:
  778. if (ctx.pipes_enabled_per_bam[HSUSB_BAM] &&
  779. info.connect_complete &&
  780. !info.cons_stopped && !info.prod_stopped) {
  781. pr_debug("%s: ACK on cons_request", __func__);
  782. ret = 0;
  783. } else if (ctx.pipes_enabled_per_bam[HSUSB_BAM] &&
  784. info.connect_complete && info.bus_suspend) {
  785. info.bus_suspend = 0;
  786. if (info.wake_cb)
  787. info.wake_cb(info.wake_param);
  788. }
  789. break;
  790. case HSIC_BAM:
  791. /*
  792. * Vote for hsic resume, however the core
  793. * resume may not be completed yet or on the other hand
  794. * hsic core might already be resumed, due to a vote
  795. * by other driver, in this case we will just renew our
  796. * vote here.
  797. */
  798. usb_bam_resume_hsic_host();
  799. /*
  800. * Return sucess if there are pipes connected
  801. * and hsic core is actually not in lpm.
  802. * If in lpm, grant will occur on resume
  803. * finish (see msm_bam_hsic_notify_on_resume)
  804. */
  805. if (ctx.pipes_enabled_per_bam[cur_bam] &&
  806. !hsic_host_info.in_lpm) {
  807. ret = 0;
  808. }
  809. break;
  810. case SSUSB_BAM:
  811. default:
  812. break;
  813. }
  814. spin_unlock(&usb_bam_ipa_handshake_info_lock);
  815. spin_unlock(&usb_bam_lock);
  816. if (ret == -EINPROGRESS)
  817. pr_debug("%s: EINPROGRESS on cons_request", __func__);
  818. return ret;
  819. }
  820. static int usb_cons_request_resource(void)
  821. {
  822. return cons_request_resource(HSUSB_BAM);
  823. }
  824. static int hsic_cons_request_resource(void)
  825. {
  826. return cons_request_resource(HSIC_BAM);
  827. }
  828. static int cons_release_resource(enum usb_bam cur_bam)
  829. {
  830. pr_debug("%s: Release %s_CONS resource\n",
  831. __func__, bam_enable_strings[cur_bam]);
  832. info.cur_cons_state[cur_bam] = IPA_RM_RESOURCE_RELEASED;
  833. spin_lock(&usb_bam_lock);
  834. if (!ctx.pipes_enabled_per_bam[cur_bam]) {
  835. spin_unlock(&usb_bam_lock);
  836. pr_debug("%s: ACK on cons_release", __func__);
  837. return 0;
  838. }
  839. spin_unlock(&usb_bam_lock);
  840. if (cur_bam == HSUSB_BAM) {
  841. spin_lock(&usb_bam_ipa_handshake_info_lock);
  842. if (info.bus_suspend)
  843. queue_work(ctx.usb_bam_wq, &info.finish_suspend_work);
  844. spin_unlock(&usb_bam_ipa_handshake_info_lock);
  845. pr_debug("%s: EINPROGRESS cons_release", __func__);
  846. return -EINPROGRESS;
  847. } else if (cur_bam == HSIC_BAM) {
  848. /*
  849. * Allow to go to lpm for now. Actual state will be checked
  850. * in msm_bam_hsic_lpm_ok() just before going to lpm.
  851. */
  852. if (hsic_host_info.dev && !info.in_lpm[HSIC_BAM]) {
  853. pr_debug("%s: Putting hsic device %x\n", __func__,
  854. (int)hsic_host_info.dev);
  855. pm_runtime_put(hsic_host_info.dev);
  856. info.in_lpm[HSIC_BAM] = true;
  857. }
  858. }
  859. return 0;
  860. }
  861. static int hsic_cons_release_resource(void)
  862. {
  863. return cons_release_resource(HSIC_BAM);
  864. }
  865. static int usb_cons_release_resource(void)
  866. {
  867. return cons_release_resource(HSUSB_BAM);
  868. }
  869. static void usb_bam_ipa_create_resources(void)
  870. {
  871. struct ipa_rm_create_params usb_prod_create_params;
  872. struct ipa_rm_create_params usb_cons_create_params;
  873. enum usb_bam cur_bam;
  874. int ret, i;
  875. for (i = 0; i < ARRAY_SIZE(ipa_rm_bams); i++) {
  876. /* Create USB/HSIC_PROD entity */
  877. cur_bam = ipa_rm_bams[i];
  878. memset(&usb_prod_create_params, 0,
  879. sizeof(usb_prod_create_params));
  880. usb_prod_create_params.name = ipa_rm_resource_prod[cur_bam];
  881. usb_prod_create_params.reg_params.notify_cb =
  882. usb_prod_notify_cb;
  883. usb_prod_create_params.reg_params.user_data = &ipa_rm_bams[i];
  884. ret = ipa_rm_create_resource(&usb_prod_create_params);
  885. if (ret) {
  886. pr_err("%s: Failed to create USB_PROD resource\n",
  887. __func__);
  888. return;
  889. }
  890. /* Create USB_CONS entity */
  891. memset(&usb_cons_create_params, 0,
  892. sizeof(usb_cons_create_params));
  893. usb_cons_create_params.name = ipa_rm_resource_cons[cur_bam];
  894. usb_cons_create_params.request_resource =
  895. request_resource_cb[cur_bam];
  896. usb_cons_create_params.release_resource =
  897. release_resource_cb[cur_bam];
  898. ret = ipa_rm_create_resource(&usb_cons_create_params);
  899. if (ret) {
  900. pr_err("%s: Failed to create USB_CONS resource\n",
  901. __func__);
  902. return ;
  903. }
  904. }
  905. }
  906. static void wait_for_prod_granted(enum usb_bam cur_bam)
  907. {
  908. int ret;
  909. pr_debug("%s Request %s_PROD_RES\n", __func__,
  910. bam_enable_strings[cur_bam]);
  911. if (info.cur_cons_state[cur_bam] == IPA_RM_RESOURCE_GRANTED)
  912. pr_debug("%s: CONS already granted for some reason\n",
  913. __func__);
  914. if (info.cur_prod_state[cur_bam] == IPA_RM_RESOURCE_GRANTED)
  915. pr_debug("%s: PROD already granted for some reason\n",
  916. __func__);
  917. init_completion(&info.prod_avail[cur_bam]);
  918. ret = ipa_rm_request_resource(ipa_rm_resource_prod[cur_bam]);
  919. if (!ret) {
  920. info.cur_prod_state[cur_bam] = IPA_RM_RESOURCE_GRANTED;
  921. complete_all(&info.prod_avail[cur_bam]);
  922. pr_debug("%s: PROD_GRANTED without wait\n", __func__);
  923. } else if (ret == -EINPROGRESS) {
  924. pr_debug("%s: Waiting for PROD_GRANTED\n", __func__);
  925. if (!wait_for_completion_timeout(&info.prod_avail[cur_bam],
  926. USB_BAM_TIMEOUT))
  927. pr_err("%s: Timeout wainting for PROD_GRANTED\n",
  928. __func__);
  929. } else
  930. pr_err("%s: ipa_rm_request_resource ret =%d\n", __func__, ret);
  931. }
  932. void notify_usb_connected(enum usb_bam cur_bam)
  933. {
  934. pr_debug("%s: enter\n", __func__);
  935. spin_lock(&usb_bam_ipa_handshake_info_lock);
  936. if (cur_bam == HSUSB_BAM)
  937. info.connect_complete = 1;
  938. spin_unlock(&usb_bam_ipa_handshake_info_lock);
  939. if (info.cur_cons_state[cur_bam] == IPA_RM_RESOURCE_GRANTED) {
  940. pr_debug("%s: Notify %s_CONS_GRANTED\n", __func__,
  941. bam_enable_strings[cur_bam]);
  942. ipa_rm_notify_completion(IPA_RM_RESOURCE_GRANTED,
  943. ipa_rm_resource_cons[cur_bam]);
  944. }
  945. }
  946. static void wait_for_prod_release(enum usb_bam cur_bam)
  947. {
  948. int ret;
  949. if (info.cur_cons_state[cur_bam] == IPA_RM_RESOURCE_RELEASED)
  950. pr_debug("%s consumer already released\n", __func__);
  951. if (info.cur_prod_state[cur_bam] == IPA_RM_RESOURCE_RELEASED)
  952. pr_debug("%s producer already released\n", __func__);
  953. init_completion(&info.prod_released[cur_bam]);
  954. pr_debug("%s: Releasing %s_PROD\n", __func__,
  955. bam_enable_strings[cur_bam]);
  956. ret = ipa_rm_release_resource(ipa_rm_resource_prod[cur_bam]);
  957. if (!ret) {
  958. pr_debug("%s: Released without waiting\n", __func__);
  959. info.cur_prod_state[cur_bam] = IPA_RM_RESOURCE_RELEASED;
  960. complete_all(&info.prod_released[cur_bam]);
  961. } else if (ret == -EINPROGRESS) {
  962. pr_debug("%s: Waiting for PROD_RELEASED\n", __func__);
  963. if (!wait_for_completion_timeout(&info.prod_released[cur_bam],
  964. USB_BAM_TIMEOUT))
  965. pr_err("%s: Timeout waiting for PROD_RELEASED\n",
  966. __func__);
  967. } else
  968. pr_err("%s: ipa_rm_request_resource ret =%d", __func__, ret);
  969. }
  970. static int check_pipes_empty(u8 src_idx, u8 dst_idx)
  971. {
  972. struct sps_pipe *prod_pipe, *cons_pipe;
  973. u32 prod_empty, cons_empty;
  974. /* If we have any remaints in the pipes we don't go to sleep */
  975. prod_pipe = ctx.usb_bam_sps.sps_pipes[src_idx];
  976. cons_pipe = ctx.usb_bam_sps.sps_pipes[dst_idx];
  977. if (sps_is_pipe_empty(prod_pipe, &prod_empty) ||
  978. sps_is_pipe_empty(cons_pipe, &cons_empty)) {
  979. pr_err("%s: sps_is_pipe_empty failed with\n", __func__);
  980. return 0;
  981. }
  982. if (!prod_empty || !cons_empty) {
  983. pr_err("%s: pipes not empty prod=%d cond=%d", __func__,
  984. prod_empty, cons_empty);
  985. return 0;
  986. }
  987. return 1;
  988. }
  989. void usb_bam_suspend(struct usb_bam_connect_ipa_params *ipa_params)
  990. {
  991. struct usb_bam_pipe_connect *pipe_connect;
  992. enum usb_bam cur_bam;
  993. u8 src_idx, dst_idx;
  994. if (!ipa_params) {
  995. pr_err("%s: Invalid ipa params\n", __func__);
  996. return;
  997. }
  998. src_idx = ipa_params->src_idx;
  999. dst_idx = ipa_params->dst_idx;
  1000. if (src_idx >= ctx.max_connections || dst_idx >= ctx.max_connections) {
  1001. pr_err("%s: Invalid connection index src=%d dst=%d\n",
  1002. __func__, src_idx, dst_idx);
  1003. }
  1004. pipe_connect = &usb_bam_connections[src_idx];
  1005. cur_bam = pipe_connect->bam_type;
  1006. if (cur_bam != HSUSB_BAM)
  1007. return;
  1008. info.src_idx = src_idx;
  1009. info.dst_idx = dst_idx;
  1010. pr_err("%s: Starting suspend sequence(BAM=%s)\n", __func__,
  1011. bam_enable_strings[cur_bam]);
  1012. spin_lock(&usb_bam_ipa_handshake_info_lock);
  1013. info.bus_suspend = 1;
  1014. spin_unlock(&usb_bam_ipa_handshake_info_lock);
  1015. /* Stop PROD transfers */
  1016. if (info.stop) {
  1017. spin_lock(&usb_bam_ipa_handshake_info_lock);
  1018. info.stop(info.start_stop_param, USB_TO_PEER_PERIPHERAL);
  1019. info.prod_stopped = true;
  1020. spin_unlock(&usb_bam_ipa_handshake_info_lock);
  1021. }
  1022. spin_lock(&usb_bam_ipa_handshake_info_lock);
  1023. /* If cable was disconnected, let disconnection seq do everything */
  1024. if (info.disconnected) {
  1025. spin_unlock(&usb_bam_ipa_handshake_info_lock);
  1026. pr_debug("%s: Cable disconnected\n", __func__);
  1027. return;
  1028. }
  1029. /* Don't go to LPM if data in the pipes */
  1030. if (!check_pipes_empty(src_idx, dst_idx)) {
  1031. spin_unlock(&usb_bam_ipa_handshake_info_lock);
  1032. pr_err("%s: pipes not empty, won't start suspend", __func__);
  1033. return;
  1034. }
  1035. spin_unlock(&usb_bam_ipa_handshake_info_lock);
  1036. queue_work(ctx.usb_bam_wq, &info.suspend_work);
  1037. }
  1038. static void usb_bam_start_suspend(struct work_struct *w)
  1039. {
  1040. pr_debug("%s: enter", __func__);
  1041. mutex_lock(&info.suspend_resume_mutex);
  1042. spin_lock(&usb_bam_ipa_handshake_info_lock);
  1043. /* If cable was disconnected, let disconnection seq do everything */
  1044. if (info.disconnected) {
  1045. spin_unlock(&usb_bam_ipa_handshake_info_lock);
  1046. mutex_unlock(&info.suspend_resume_mutex);
  1047. pr_debug("%s: Cable disconnected\n", __func__);
  1048. return;
  1049. }
  1050. if (!info.bus_suspend) {
  1051. spin_unlock(&usb_bam_ipa_handshake_info_lock);
  1052. pr_err("%s: Resume started, not suspending", __func__);
  1053. mutex_unlock(&info.suspend_resume_mutex);
  1054. return;
  1055. }
  1056. /* Stop PROD transfers in case they were started */
  1057. if (info.stop && !info.prod_stopped) {
  1058. info.stop(info.start_stop_param, USB_TO_PEER_PERIPHERAL);
  1059. info.prod_stopped = true;
  1060. }
  1061. spin_unlock(&usb_bam_ipa_handshake_info_lock);
  1062. /* Don't start LPM seq if data in the pipes */
  1063. if (!check_pipes_empty(info.src_idx, info.dst_idx)) {
  1064. mutex_unlock(&info.suspend_resume_mutex);
  1065. return;
  1066. }
  1067. spin_lock(&usb_bam_ipa_handshake_info_lock);
  1068. info.lpm_wait_handshake[HSUSB_BAM] = true;
  1069. spin_unlock(&usb_bam_ipa_handshake_info_lock);
  1070. wait_for_prod_release(HSUSB_BAM);
  1071. mutex_unlock(&info.suspend_resume_mutex);
  1072. if (info.cur_cons_state[HSUSB_BAM] == IPA_RM_RESOURCE_RELEASED)
  1073. usb_bam_finish_suspend();
  1074. else
  1075. pr_debug("Consumer not released yet\n");
  1076. }
  1077. static void usb_bam_finish_resume(struct work_struct *w)
  1078. {
  1079. struct usb_phy *trans = usb_get_transceiver();
  1080. BUG_ON(trans == NULL);
  1081. pr_debug("%s: enter", __func__);
  1082. mutex_lock(&info.suspend_resume_mutex);
  1083. /* Suspend happened in the meantime */
  1084. spin_lock(&usb_bam_ipa_handshake_info_lock);
  1085. if (info.bus_suspend) {
  1086. spin_unlock(&usb_bam_ipa_handshake_info_lock);
  1087. pr_err("%s: Bus suspended, not resuming", __func__);
  1088. mutex_unlock(&info.suspend_resume_mutex);
  1089. return;
  1090. }
  1091. info.lpm_wait_handshake[HSUSB_BAM] = true;
  1092. spin_unlock(&usb_bam_ipa_handshake_info_lock);
  1093. wait_for_prod_granted(HSUSB_BAM);
  1094. notify_usb_connected(HSUSB_BAM);
  1095. if (info.cons_stopped) {
  1096. ipa_resume_pipes();
  1097. if (info.start) {
  1098. pr_debug("%s: Enqueue CONS transfer", __func__);
  1099. info.start(info.start_stop_param,
  1100. PEER_PERIPHERAL_TO_USB);
  1101. info.cons_stopped = 0;
  1102. }
  1103. }
  1104. if (info.start) {
  1105. spin_lock(&usb_bam_ipa_handshake_info_lock);
  1106. info.start(info.start_stop_param, USB_TO_PEER_PERIPHERAL);
  1107. info.prod_stopped = false;
  1108. spin_unlock(&usb_bam_ipa_handshake_info_lock);
  1109. }
  1110. if (info.cur_cons_state[HSUSB_BAM] == IPA_RM_RESOURCE_GRANTED) {
  1111. pr_debug("%s: Notify CONS_GRANTED\n", __func__);
  1112. ipa_rm_notify_completion(IPA_RM_RESOURCE_GRANTED,
  1113. ipa_rm_resource_cons[HSUSB_BAM]);
  1114. }
  1115. mutex_unlock(&info.suspend_resume_mutex);
  1116. pr_debug("%s: done", __func__);
  1117. }
  1118. void usb_bam_resume(struct usb_bam_connect_ipa_params *ipa_params)
  1119. {
  1120. enum usb_bam cur_bam;
  1121. u8 src_idx, dst_idx;
  1122. struct usb_bam_pipe_connect *pipe_connect;
  1123. pr_debug("%s: Resuming\n", __func__);
  1124. if (!ipa_params) {
  1125. pr_err("%s: Invalid ipa params\n", __func__);
  1126. return;
  1127. }
  1128. src_idx = ipa_params->src_idx;
  1129. dst_idx = ipa_params->dst_idx;
  1130. if (src_idx >= ctx.max_connections || dst_idx >= ctx.max_connections) {
  1131. pr_err("%s: Invalid connection index src=%d dst=%d\n",
  1132. __func__, src_idx, dst_idx);
  1133. return;
  1134. }
  1135. pipe_connect = &usb_bam_connections[src_idx];
  1136. cur_bam = pipe_connect->bam_type;
  1137. if (cur_bam != HSUSB_BAM)
  1138. return;
  1139. info.in_lpm[HSUSB_BAM] = false;
  1140. spin_lock(&usb_bam_ipa_handshake_info_lock);
  1141. info.bus_suspend = 0;
  1142. spin_unlock(&usb_bam_ipa_handshake_info_lock);
  1143. queue_work(ctx.usb_bam_wq, &info.resume_work);
  1144. }
  1145. void msm_bam_wait_for_hsic_prod_granted(void)
  1146. {
  1147. spin_lock(&usb_bam_lock);
  1148. ctx.is_bam_inactivity[HSIC_BAM] = false;
  1149. /* Get back to resume state including wakeup ipa */
  1150. usb_bam_resume_hsic_host();
  1151. /* Ensure getting the producer resource */
  1152. wait_for_prod_granted(HSIC_BAM);
  1153. spin_unlock(&usb_bam_lock);
  1154. }
  1155. void msm_bam_hsic_notify_on_resume(void)
  1156. {
  1157. spin_lock(&usb_bam_lock);
  1158. hsic_host_info.in_lpm = false;
  1159. /* HSIC resume completed. Notify CONS grant if CONS was requested */
  1160. notify_usb_connected(HSIC_BAM);
  1161. /*
  1162. * This function is called to notify the usb bam driver
  1163. * that the hsic core and hsic bam hw are fully resumed
  1164. * and clocked on. Therefore we can now set the inactivity
  1165. * timer to the hsic bam hw.
  1166. */
  1167. if (ctx.inactivity_timer_ms[HSIC_BAM])
  1168. usb_bam_set_inactivity_timer(HSIC_BAM);
  1169. spin_unlock(&usb_bam_lock);
  1170. }
  1171. bool msm_bam_hsic_lpm_ok(void)
  1172. {
  1173. int i;
  1174. struct usb_bam_pipe_connect *pipe_iter;
  1175. if (hsic_host_info.dev) {
  1176. pr_debug("%s: Starting hsic full suspend sequence\n",
  1177. __func__);
  1178. /*
  1179. * Start low power mode by releasing the device
  1180. * only in case that indeed the resources were released
  1181. * and we are still in inactivity state (wake event
  1182. * have not been occured while we were waiting to the
  1183. * resources release)
  1184. */
  1185. spin_lock(&usb_bam_lock);
  1186. if (info.cur_cons_state[HSIC_BAM] ==
  1187. IPA_RM_RESOURCE_RELEASED &&
  1188. info.cur_prod_state[HSIC_BAM] ==
  1189. IPA_RM_RESOURCE_RELEASED &&
  1190. ctx.is_bam_inactivity[HSIC_BAM] && info.in_lpm[HSIC_BAM]) {
  1191. /* HSIC host will go now to lpm */
  1192. pr_debug("%s: vote for suspend hsic %x\n",
  1193. __func__, (int)hsic_host_info.dev);
  1194. for (i = 0; i < ctx.max_connections; i++) {
  1195. pipe_iter =
  1196. &usb_bam_connections[i];
  1197. if (pipe_iter->bam_type == HSIC_BAM &&
  1198. pipe_iter->enabled &&
  1199. !pipe_iter->suspended) {
  1200. spin_unlock(&usb_bam_lock);
  1201. ipa_suspend(
  1202. pipe_iter->ipa_clnt_hdl);
  1203. pipe_iter->suspended = true;
  1204. spin_lock(&usb_bam_lock);
  1205. }
  1206. }
  1207. hsic_host_info.in_lpm = true;
  1208. spin_unlock(&usb_bam_lock);
  1209. return true;
  1210. }
  1211. /* We don't allow lpm, therefore renew our vote here */
  1212. if (info.in_lpm[HSIC_BAM]) {
  1213. pr_err("%s: Not allow lpm while ref count=0\n",
  1214. __func__);
  1215. pr_err("%s: inactivity=%d, c_s=%d p_s=%d lpm=%d\n",
  1216. __func__, ctx.is_bam_inactivity[HSIC_BAM],
  1217. info.cur_cons_state[HSIC_BAM],
  1218. info.cur_prod_state[HSIC_BAM],
  1219. info.in_lpm[HSIC_BAM]);
  1220. pm_runtime_get(hsic_host_info.dev);
  1221. info.in_lpm[HSIC_BAM] = false;
  1222. spin_unlock(&usb_bam_lock);
  1223. } else
  1224. spin_unlock(&usb_bam_lock);
  1225. return false;
  1226. }
  1227. return true;
  1228. }
  1229. int usb_bam_connect_ipa(struct usb_bam_connect_ipa_params *ipa_params)
  1230. {
  1231. u8 idx;
  1232. enum usb_bam cur_bam;
  1233. struct usb_bam_pipe_connect *pipe_connect;
  1234. int ret;
  1235. struct msm_usb_bam_platform_data *pdata =
  1236. ctx.usb_bam_pdev->dev.platform_data;
  1237. if (!ipa_params) {
  1238. pr_err("%s: Invalid ipa params\n",
  1239. __func__);
  1240. return -EINVAL;
  1241. }
  1242. if (ipa_params->dir == USB_TO_PEER_PERIPHERAL)
  1243. idx = ipa_params->src_idx;
  1244. else
  1245. idx = ipa_params->dst_idx;
  1246. if (idx >= ctx.max_connections) {
  1247. pr_err("%s: Invalid connection index\n",
  1248. __func__);
  1249. return -EINVAL;
  1250. }
  1251. pipe_connect = &usb_bam_connections[idx];
  1252. cur_bam = pipe_connect->bam_type;
  1253. if (pipe_connect->enabled) {
  1254. pr_err("%s: connection %d was already established\n",
  1255. __func__, idx);
  1256. return 0;
  1257. }
  1258. pr_debug("%s: enter", __func__);
  1259. if (cur_bam == HSUSB_BAM) {
  1260. mutex_lock(&info.suspend_resume_mutex);
  1261. spin_lock(&usb_bam_lock);
  1262. if (ctx.pipes_enabled_per_bam[HSUSB_BAM] == 0) {
  1263. spin_unlock(&usb_bam_lock);
  1264. spin_lock(&usb_bam_ipa_handshake_info_lock);
  1265. info.lpm_wait_handshake[HSUSB_BAM] = true;
  1266. info.connect_complete = 0;
  1267. info.disconnected = 0;
  1268. info.pending_lpm = 0;
  1269. info.lpm_wait_pipes = 1;
  1270. info.bus_suspend = 0;
  1271. info.cons_stopped = 0;
  1272. info.prod_stopped = 0;
  1273. spin_unlock(&usb_bam_ipa_handshake_info_lock);
  1274. usb_bam_resume_core(cur_bam);
  1275. } else
  1276. spin_unlock(&usb_bam_lock);
  1277. }
  1278. /* Check if BAM requires RESET before connect and reset first pipe */
  1279. spin_lock(&usb_bam_lock);
  1280. if ((pdata->reset_on_connect[cur_bam] == true) &&
  1281. (ctx.pipes_enabled_per_bam[cur_bam] == 0)) {
  1282. spin_unlock(&usb_bam_lock);
  1283. sps_device_reset(ctx.h_bam[cur_bam]);
  1284. /* On re-connect assume out from lpm for HSIC BAM */
  1285. if (cur_bam == HSIC_BAM && hsic_host_info.dev &&
  1286. info.in_lpm[HSIC_BAM]) {
  1287. pr_debug("%s: Getting hsic device %x\n",
  1288. __func__, (int)hsic_host_info.dev);
  1289. pm_runtime_get(hsic_host_info.dev);
  1290. }
  1291. /* On re-connect assume out from lpm for all BAMs */
  1292. info.in_lpm[cur_bam] = false;
  1293. } else
  1294. spin_unlock(&usb_bam_lock);
  1295. if (ipa_params->dir == USB_TO_PEER_PERIPHERAL) {
  1296. pr_debug("%s: Starting connect sequence\n", __func__);
  1297. wait_for_prod_granted(cur_bam);
  1298. }
  1299. ret = connect_pipe_ipa(idx, ipa_params);
  1300. if (ret) {
  1301. pr_err("%s: pipe connection failure\n", __func__);
  1302. if (cur_bam == HSUSB_BAM)
  1303. mutex_unlock(&info.suspend_resume_mutex);
  1304. return ret;
  1305. }
  1306. spin_lock(&usb_bam_lock);
  1307. pipe_connect->enabled = 1;
  1308. pipe_connect->suspended = 0;
  1309. /* Set global inactivity timer upon first pipe connection */
  1310. if (ctx.pipes_enabled_per_bam[pipe_connect->bam_type] == 0 &&
  1311. ctx.inactivity_timer_ms[pipe_connect->bam_type] &&
  1312. pipe_connect->inactivity_notify)
  1313. usb_bam_set_inactivity_timer(pipe_connect->bam_type);
  1314. ctx.pipes_enabled_per_bam[cur_bam] += 1;
  1315. spin_unlock(&usb_bam_lock);
  1316. if (ipa_params->dir == PEER_PERIPHERAL_TO_USB)
  1317. notify_usb_connected(cur_bam);
  1318. if (cur_bam == HSUSB_BAM)
  1319. mutex_unlock(&info.suspend_resume_mutex);
  1320. pr_debug("%s: done", __func__);
  1321. return 0;
  1322. }
  1323. EXPORT_SYMBOL(usb_bam_connect_ipa);
  1324. int usb_bam_client_ready(bool ready)
  1325. {
  1326. spin_lock(&usb_bam_peer_handshake_info_lock);
  1327. if (peer_handshake_info.client_ready == ready) {
  1328. pr_debug("%s: client state is already %d\n",
  1329. __func__, ready);
  1330. spin_unlock(&usb_bam_peer_handshake_info_lock);
  1331. return 0;
  1332. }
  1333. peer_handshake_info.client_ready = ready;
  1334. if (peer_handshake_info.state == USB_BAM_SM_PLUG_ACKED && !ready) {
  1335. pr_debug("Starting reset sequence");
  1336. INIT_COMPLETION(ctx.reset_done);
  1337. }
  1338. spin_unlock(&usb_bam_peer_handshake_info_lock);
  1339. if (!queue_work(ctx.usb_bam_wq,
  1340. &peer_handshake_info.reset_event.event_w)) {
  1341. spin_lock(&usb_bam_peer_handshake_info_lock);
  1342. peer_handshake_info.pending_work++;
  1343. spin_unlock(&usb_bam_peer_handshake_info_lock);
  1344. }
  1345. return 0;
  1346. }
  1347. static void usb_bam_work(struct work_struct *w)
  1348. {
  1349. int i;
  1350. struct usb_bam_event_info *event_info =
  1351. container_of(w, struct usb_bam_event_info, event_w);
  1352. struct usb_bam_pipe_connect *pipe_connect =
  1353. container_of(event_info, struct usb_bam_pipe_connect, event);
  1354. struct usb_bam_pipe_connect *pipe_iter;
  1355. int (*callback)(void *priv);
  1356. void *param = NULL;
  1357. switch (event_info->type) {
  1358. case USB_BAM_EVENT_WAKEUP:
  1359. case USB_BAM_EVENT_WAKEUP_PIPE:
  1360. pr_debug("%s recieved USB_BAM_EVENT_WAKEUP\n", __func__);
  1361. /*
  1362. * Make sure the PROD resource is granted before
  1363. * wakeup hsic host class driver (done by the callback below)
  1364. */
  1365. if (pipe_connect->peer_bam == IPA_P_BAM &&
  1366. pipe_connect->bam_type == HSIC_BAM &&
  1367. info.cur_prod_state[HSIC_BAM] != IPA_RM_RESOURCE_GRANTED) {
  1368. wait_for_prod_granted(HSIC_BAM);
  1369. }
  1370. /*
  1371. * Check if need to resume the hsic host.
  1372. * On one hand, since we got the wakeup interrupt
  1373. * the hsic bam clocks are already enabled, so no need
  1374. * to actualluy resume the hardware... However, we still need
  1375. * to update the usb bam driver state (to set in_lpm=false),
  1376. * and to wake ipa (ipa_resume) and to hold again the hsic host
  1377. * device again to avoid it going to low poer mode next time
  1378. * until we complete releasing the hsic consumer and producer
  1379. * resources against the ipa resource manager.
  1380. */
  1381. spin_lock(&usb_bam_lock);
  1382. if (pipe_connect->bam_type == HSIC_BAM)
  1383. usb_bam_resume_hsic_host();
  1384. spin_unlock(&usb_bam_lock);
  1385. /* Notify about wakeup / activity of the bam */
  1386. if (event_info->callback)
  1387. event_info->callback(event_info->param);
  1388. /*
  1389. * Reset inactivity timer counter if this pipe's bam
  1390. * has inactivity timeout.
  1391. */
  1392. spin_lock(&usb_bam_lock);
  1393. if (ctx.inactivity_timer_ms[pipe_connect->bam_type])
  1394. usb_bam_set_inactivity_timer(pipe_connect->bam_type);
  1395. spin_unlock(&usb_bam_lock);
  1396. if (pipe_connect->bam_type == HSUSB_BAM) {
  1397. /* A2 wakeup not from LPM (CONS was up) */
  1398. wait_for_prod_granted(pipe_connect->bam_type);
  1399. if (info.start) {
  1400. pr_debug("%s: Enqueue PROD transfer", __func__);
  1401. info.start(info.start_stop_param,
  1402. USB_TO_PEER_PERIPHERAL);
  1403. }
  1404. }
  1405. break;
  1406. case USB_BAM_EVENT_INACTIVITY:
  1407. pr_debug("%s recieved USB_BAM_EVENT_INACTIVITY\n", __func__);
  1408. /*
  1409. * Since event info is one structure per pipe, it might be
  1410. * overriden when we will register the wakeup events below,
  1411. * and still we want ot register the wakeup events before we
  1412. * notify on the inactivity in order to identify the next
  1413. * activity as soon as possible.
  1414. */
  1415. callback = event_info->callback;
  1416. param = event_info->param;
  1417. /*
  1418. * Upon inactivity, configure wakeup irq for all pipes
  1419. * that are into the usb bam.
  1420. */
  1421. spin_lock(&usb_bam_lock);
  1422. for (i = 0; i < ctx.max_connections; i++) {
  1423. pipe_iter = &usb_bam_connections[i];
  1424. if (pipe_iter->bam_type ==
  1425. pipe_connect->bam_type &&
  1426. pipe_iter->dir ==
  1427. PEER_PERIPHERAL_TO_USB &&
  1428. pipe_iter->enabled) {
  1429. pr_debug("%s: Register wakeup on pipe %pK\n",
  1430. __func__, pipe_iter);
  1431. __usb_bam_register_wake_cb(i,
  1432. pipe_iter->activity_notify,
  1433. pipe_iter->priv,
  1434. false);
  1435. }
  1436. }
  1437. spin_unlock(&usb_bam_lock);
  1438. /* Notify about the inactivity to the USB class driver */
  1439. if (callback)
  1440. callback(param);
  1441. wait_for_prod_release(pipe_connect->bam_type);
  1442. pr_debug("%s: complete wait on hsic producer s=%d\n",
  1443. __func__, info.cur_prod_state[pipe_connect->bam_type]);
  1444. /*
  1445. * Allow to go to lpm for now if also consumer is down.
  1446. * If consumer is up, we will wait to the release consumer
  1447. * notification.
  1448. */
  1449. if (hsic_host_info.dev &&
  1450. info.cur_cons_state[HSIC_BAM] ==
  1451. IPA_RM_RESOURCE_RELEASED && !info.in_lpm[HSIC_BAM]) {
  1452. pr_debug("%s: Putting hsic device %x\n", __func__,
  1453. (int)hsic_host_info.dev);
  1454. pm_runtime_put(hsic_host_info.dev);
  1455. info.in_lpm[HSIC_BAM] = true;
  1456. }
  1457. break;
  1458. default:
  1459. pr_err("%s: unknown usb bam event type %d\n", __func__,
  1460. event_info->type);
  1461. }
  1462. }
  1463. static void usb_bam_wake_cb(struct sps_event_notify *notify)
  1464. {
  1465. struct usb_bam_event_info *event_info =
  1466. (struct usb_bam_event_info *)notify->user;
  1467. struct usb_bam_pipe_connect *pipe_connect =
  1468. container_of(event_info,
  1469. struct usb_bam_pipe_connect,
  1470. event);
  1471. enum usb_bam bam = pipe_connect->bam_type;
  1472. spin_lock(&usb_bam_lock);
  1473. if (event_info->type == USB_BAM_EVENT_WAKEUP_PIPE)
  1474. queue_work(ctx.usb_bam_wq, &event_info->event_w);
  1475. else if (event_info->type == USB_BAM_EVENT_WAKEUP &&
  1476. ctx.is_bam_inactivity[bam]) {
  1477. /*
  1478. * Sps wake event is per pipe, so usb_bam_wake_cb is
  1479. * called per pipe. However, we want to filter the wake
  1480. * event to be wake event per all the pipes.
  1481. * Therefore, the first pipe that awaked will be considered
  1482. * as global bam wake event.
  1483. */
  1484. ctx.is_bam_inactivity[bam] = false;
  1485. queue_work(ctx.usb_bam_wq, &event_info->event_w);
  1486. }
  1487. spin_unlock(&usb_bam_lock);
  1488. }
  1489. static void usb_bam_sm_work(struct work_struct *w)
  1490. {
  1491. pr_debug("%s: current state: %d\n", __func__,
  1492. peer_handshake_info.state);
  1493. spin_lock(&usb_bam_peer_handshake_info_lock);
  1494. switch (peer_handshake_info.state) {
  1495. case USB_BAM_SM_INIT:
  1496. if (peer_handshake_info.client_ready) {
  1497. spin_unlock(&usb_bam_peer_handshake_info_lock);
  1498. smsm_change_state(SMSM_APPS_STATE, 0,
  1499. SMSM_USB_PLUG_UNPLUG);
  1500. spin_lock(&usb_bam_peer_handshake_info_lock);
  1501. peer_handshake_info.state = USB_BAM_SM_PLUG_NOTIFIED;
  1502. }
  1503. break;
  1504. case USB_BAM_SM_PLUG_NOTIFIED:
  1505. if (peer_handshake_info.ack_received) {
  1506. peer_handshake_info.state = USB_BAM_SM_PLUG_ACKED;
  1507. peer_handshake_info.ack_received = 0;
  1508. }
  1509. break;
  1510. case USB_BAM_SM_PLUG_ACKED:
  1511. if (!peer_handshake_info.client_ready) {
  1512. spin_unlock(&usb_bam_peer_handshake_info_lock);
  1513. pr_debug("Starting A2 reset sequence");
  1514. smsm_change_state(SMSM_APPS_STATE,
  1515. SMSM_USB_PLUG_UNPLUG, 0);
  1516. spin_lock(&usb_bam_peer_handshake_info_lock);
  1517. peer_handshake_info.state = USB_BAM_SM_UNPLUG_NOTIFIED;
  1518. }
  1519. break;
  1520. case USB_BAM_SM_UNPLUG_NOTIFIED:
  1521. if (peer_handshake_info.ack_received) {
  1522. spin_unlock(&usb_bam_peer_handshake_info_lock);
  1523. peer_handshake_info.reset_event.
  1524. callback(peer_handshake_info.reset_event.param);
  1525. spin_lock(&usb_bam_peer_handshake_info_lock);
  1526. complete_all(&ctx.reset_done);
  1527. pr_debug("Finished reset sequence");
  1528. peer_handshake_info.state = USB_BAM_SM_INIT;
  1529. peer_handshake_info.ack_received = 0;
  1530. }
  1531. break;
  1532. }
  1533. if (peer_handshake_info.pending_work) {
  1534. peer_handshake_info.pending_work--;
  1535. spin_unlock(&usb_bam_peer_handshake_info_lock);
  1536. queue_work(ctx.usb_bam_wq,
  1537. &peer_handshake_info.reset_event.event_w);
  1538. spin_lock(&usb_bam_peer_handshake_info_lock);
  1539. }
  1540. spin_unlock(&usb_bam_peer_handshake_info_lock);
  1541. }
  1542. static void usb_bam_ack_toggle_cb(void *priv,
  1543. uint32_t old_state, uint32_t new_state)
  1544. {
  1545. static int last_processed_state;
  1546. int current_state;
  1547. spin_lock(&usb_bam_peer_handshake_info_lock);
  1548. current_state = new_state & SMSM_USB_PLUG_UNPLUG;
  1549. if (current_state == last_processed_state) {
  1550. spin_unlock(&usb_bam_peer_handshake_info_lock);
  1551. return;
  1552. }
  1553. last_processed_state = current_state;
  1554. peer_handshake_info.ack_received = true;
  1555. spin_unlock(&usb_bam_peer_handshake_info_lock);
  1556. if (!queue_work(ctx.usb_bam_wq,
  1557. &peer_handshake_info.reset_event.event_w)) {
  1558. spin_lock(&usb_bam_peer_handshake_info_lock);
  1559. peer_handshake_info.pending_work++;
  1560. spin_unlock(&usb_bam_peer_handshake_info_lock);
  1561. }
  1562. }
  1563. static int __usb_bam_register_wake_cb(u8 idx, int (*callback)(void *user),
  1564. void *param, bool trigger_cb_per_pipe)
  1565. {
  1566. struct sps_pipe *pipe = ctx.usb_bam_sps.sps_pipes[idx];
  1567. struct sps_connect *sps_connection;
  1568. struct usb_bam_pipe_connect *pipe_connect;
  1569. struct usb_bam_event_info *wake_event_info;
  1570. int ret;
  1571. if (idx < 0 || idx > ctx.max_connections) {
  1572. pr_err("%s:idx is wrong %d", __func__, idx);
  1573. return -EINVAL;
  1574. }
  1575. pipe = ctx.usb_bam_sps.sps_pipes[idx];
  1576. sps_connection = &ctx.usb_bam_sps.sps_connections[idx];
  1577. pipe_connect = &usb_bam_connections[idx];
  1578. wake_event_info = &pipe_connect->event;
  1579. wake_event_info->type = (trigger_cb_per_pipe ?
  1580. USB_BAM_EVENT_WAKEUP_PIPE :
  1581. USB_BAM_EVENT_WAKEUP);
  1582. wake_event_info->param = param;
  1583. wake_event_info->callback = callback;
  1584. wake_event_info->event.mode = SPS_TRIGGER_CALLBACK;
  1585. wake_event_info->event.xfer_done = NULL;
  1586. wake_event_info->event.callback = callback ? usb_bam_wake_cb : NULL;
  1587. wake_event_info->event.user = wake_event_info;
  1588. wake_event_info->event.options = SPS_O_WAKEUP;
  1589. ret = sps_register_event(pipe, &wake_event_info->event);
  1590. if (ret) {
  1591. pr_err("%s: sps_register_event() failed %d\n", __func__, ret);
  1592. return ret;
  1593. }
  1594. sps_connection->options = callback ?
  1595. (SPS_O_AUTO_ENABLE | SPS_O_WAKEUP | SPS_O_WAKEUP_IS_ONESHOT) :
  1596. SPS_O_AUTO_ENABLE;
  1597. ret = sps_set_config(pipe, sps_connection);
  1598. if (ret) {
  1599. pr_err("%s: sps_set_config() failed %d\n", __func__, ret);
  1600. return ret;
  1601. }
  1602. return 0;
  1603. }
  1604. int usb_bam_register_wake_cb(u8 idx, int (*callback)(void *user),
  1605. void *param)
  1606. {
  1607. info.wake_cb = callback;
  1608. info.wake_param = param;
  1609. return __usb_bam_register_wake_cb(idx, callback, param, true);
  1610. }
  1611. int usb_bam_register_start_stop_cbs(
  1612. void (*start)(void *, enum usb_bam_pipe_dir),
  1613. void (*stop)(void *, enum usb_bam_pipe_dir),
  1614. void *param)
  1615. {
  1616. info.start = start;
  1617. info.stop = stop;
  1618. info.start_stop_param = param;
  1619. return 0;
  1620. }
  1621. int usb_bam_register_peer_reset_cb(int (*callback)(void *), void *param)
  1622. {
  1623. u32 ret = 0;
  1624. if (callback) {
  1625. peer_handshake_info.reset_event.param = param;
  1626. peer_handshake_info.reset_event.callback = callback;
  1627. ret = smsm_state_cb_register(SMSM_MODEM_STATE,
  1628. SMSM_USB_PLUG_UNPLUG, usb_bam_ack_toggle_cb, NULL);
  1629. if (ret) {
  1630. pr_err("%s: failed to register SMSM callback\n",
  1631. __func__);
  1632. } else {
  1633. if (smsm_get_state(SMSM_MODEM_STATE) &
  1634. SMSM_USB_PLUG_UNPLUG)
  1635. usb_bam_ack_toggle_cb(NULL, 0,
  1636. SMSM_USB_PLUG_UNPLUG);
  1637. }
  1638. } else {
  1639. peer_handshake_info.reset_event.param = NULL;
  1640. peer_handshake_info.reset_event.callback = NULL;
  1641. smsm_state_cb_deregister(SMSM_MODEM_STATE,
  1642. SMSM_USB_PLUG_UNPLUG, usb_bam_ack_toggle_cb, NULL);
  1643. }
  1644. return ret;
  1645. }
  1646. int usb_bam_disconnect_pipe(u8 idx)
  1647. {
  1648. struct usb_bam_pipe_connect *pipe_connect;
  1649. int ret;
  1650. pipe_connect = &usb_bam_connections[idx];
  1651. if (!pipe_connect->enabled) {
  1652. pr_err("%s: connection %d isn't enabled\n",
  1653. __func__, idx);
  1654. return 0;
  1655. }
  1656. ret = disconnect_pipe(idx);
  1657. if (ret) {
  1658. pr_err("%s: src pipe disconnection failure\n", __func__);
  1659. return ret;
  1660. }
  1661. pipe_connect->enabled = 0;
  1662. spin_lock(&usb_bam_lock);
  1663. if (ctx.pipes_enabled_per_bam[pipe_connect->bam_type] == 0)
  1664. pr_err("%s: wrong pipes enabled counter for bam_type=%d\n",
  1665. __func__, pipe_connect->bam_type);
  1666. else
  1667. ctx.pipes_enabled_per_bam[pipe_connect->bam_type] -= 1;
  1668. spin_unlock(&usb_bam_lock);
  1669. return 0;
  1670. }
  1671. int usb_bam_disconnect_ipa(struct usb_bam_connect_ipa_params *ipa_params)
  1672. {
  1673. int ret;
  1674. u8 idx;
  1675. struct usb_bam_pipe_connect *pipe_connect;
  1676. struct sps_connect *sps_connection;
  1677. enum usb_bam cur_bam;
  1678. if (!ipa_params->prod_clnt_hdl && !ipa_params->cons_clnt_hdl) {
  1679. pr_err("%s: Both of the handles is missing\n", __func__);
  1680. return -EINVAL;
  1681. }
  1682. pr_debug("%s: Starting disconnect sequence\n", __func__);
  1683. mutex_lock(&info.suspend_resume_mutex);
  1684. /* Delay USB core to go into lpm before we finish our handshake */
  1685. if (ipa_params->prod_clnt_hdl) {
  1686. idx = ipa_params->dst_idx;
  1687. pipe_connect = &usb_bam_connections[idx];
  1688. pipe_connect->activity_notify = NULL;
  1689. pipe_connect->inactivity_notify = NULL;
  1690. pipe_connect->priv = NULL;
  1691. /* Do the release handshake with the A2 via RM */
  1692. cur_bam = pipe_connect->bam_type;
  1693. spin_lock(&usb_bam_ipa_handshake_info_lock);
  1694. if (cur_bam == HSUSB_BAM) {
  1695. info.connect_complete = 0;
  1696. info.lpm_wait_pipes = 1;
  1697. info.disconnected = 1;
  1698. }
  1699. spin_unlock(&usb_bam_ipa_handshake_info_lock);
  1700. wait_for_prod_release(cur_bam);
  1701. /* close USB -> IPA pipe */
  1702. usb_bam_resume_core(cur_bam);
  1703. ret = ipa_disconnect(ipa_params->prod_clnt_hdl);
  1704. if (ret) {
  1705. pr_err("%s: dst pipe disconnection failure\n",
  1706. __func__);
  1707. mutex_unlock(&info.suspend_resume_mutex);
  1708. return ret;
  1709. }
  1710. sps_connection = &ctx.usb_bam_sps.sps_connections[idx];
  1711. sps_connection->data.phys_base = 0;
  1712. sps_connection->desc.phys_base = 0;
  1713. ret = usb_bam_disconnect_pipe(idx);
  1714. if (ret) {
  1715. pr_err("%s: failure to disconnect pipe %d\n",
  1716. __func__, idx);
  1717. mutex_unlock(&info.suspend_resume_mutex);
  1718. return ret;
  1719. }
  1720. }
  1721. if (ipa_params->cons_clnt_hdl) {
  1722. idx = ipa_params->src_idx;
  1723. pipe_connect = &usb_bam_connections[idx];
  1724. pipe_connect->activity_notify = NULL;
  1725. pipe_connect->inactivity_notify = NULL;
  1726. pipe_connect->priv = NULL;
  1727. cur_bam = pipe_connect->bam_type;
  1728. /* close IPA -> USB pipe */
  1729. ret = ipa_disconnect(ipa_params->cons_clnt_hdl);
  1730. if (ret) {
  1731. pr_err("%s: src pipe disconnection failure\n",
  1732. __func__);
  1733. mutex_unlock(&info.suspend_resume_mutex);
  1734. return ret;
  1735. }
  1736. sps_connection = &ctx.usb_bam_sps.sps_connections[idx];
  1737. sps_connection->data.phys_base = 0;
  1738. sps_connection->desc.phys_base = 0;
  1739. ret = usb_bam_disconnect_pipe(idx);
  1740. if (ret) {
  1741. pr_err("%s: failure to disconnect pipe %d\n",
  1742. __func__, idx);
  1743. mutex_unlock(&info.suspend_resume_mutex);
  1744. return ret;
  1745. }
  1746. pipe_connect->ipa_clnt_hdl = -1;
  1747. if (info.cur_cons_state[cur_bam] == IPA_RM_RESOURCE_RELEASED) {
  1748. pr_debug("%s Notify CONS _RELEASED\n", __func__);
  1749. ipa_rm_notify_completion(IPA_RM_RESOURCE_RELEASED,
  1750. ipa_rm_resource_cons[cur_bam]);
  1751. }
  1752. if (cur_bam == HSUSB_BAM) {
  1753. /*
  1754. * Currently we have for HSUSB BAM only one consumer
  1755. * pipe. Therefore ending disconnect sequence and
  1756. * starting hsusb lpm. This limitation will be changed
  1757. * in future patch.
  1758. */
  1759. pr_debug("%s Ended disconnect sequence\n", __func__);
  1760. usb_bam_start_lpm(1);
  1761. }
  1762. mutex_unlock(&info.suspend_resume_mutex);
  1763. return 0;
  1764. }
  1765. mutex_unlock(&info.suspend_resume_mutex);
  1766. return 0;
  1767. }
  1768. EXPORT_SYMBOL(usb_bam_disconnect_ipa);
  1769. void usb_bam_reset_complete(void)
  1770. {
  1771. pr_debug("Waiting for reset compelte");
  1772. if (wait_for_completion_interruptible_timeout(&ctx.reset_done,
  1773. 10*HZ) <= 0)
  1774. pr_warn("Timeout while waiting for reset");
  1775. pr_debug("Finished Waiting for reset complete");
  1776. }
  1777. int usb_bam_a2_reset(bool to_reconnect)
  1778. {
  1779. struct usb_bam_pipe_connect *pipe_connect;
  1780. int i;
  1781. int ret = 0, ret_int;
  1782. u8 bam = -1;
  1783. int reconnect_pipe_idx[ctx.max_connections];
  1784. for (i = 0; i < ctx.max_connections; i++)
  1785. reconnect_pipe_idx[i] = -1;
  1786. /* Disconnect a2 pipes */
  1787. for (i = 0; i < ctx.max_connections; i++) {
  1788. pipe_connect = &usb_bam_connections[i];
  1789. if (strnstr(pipe_connect->name, "a2", USB_BAM_MAX_STR_LEN) &&
  1790. pipe_connect->enabled) {
  1791. if (pipe_connect->dir == USB_TO_PEER_PERIPHERAL)
  1792. reconnect_pipe_idx[i] =
  1793. pipe_connect->src_pipe_index;
  1794. else
  1795. reconnect_pipe_idx[i] =
  1796. pipe_connect->dst_pipe_index;
  1797. bam = pipe_connect->bam_type;
  1798. if (bam < 0) {
  1799. ret = -EINVAL;
  1800. continue;
  1801. }
  1802. ret_int = usb_bam_disconnect_pipe(i);
  1803. if (ret_int) {
  1804. pr_err("%s: failure to connect pipe %d\n",
  1805. __func__, i);
  1806. ret = ret_int;
  1807. continue;
  1808. }
  1809. }
  1810. }
  1811. /* Reset A2 (USB/HSIC) BAM */
  1812. if (bam != -1 && sps_device_reset(ctx.h_bam[bam]))
  1813. pr_err("%s: BAM reset failed\n", __func__);
  1814. if (!to_reconnect)
  1815. return ret;
  1816. /* Reconnect A2 pipes */
  1817. for (i = 0; i < ctx.max_connections; i++) {
  1818. pipe_connect = &usb_bam_connections[i];
  1819. if (reconnect_pipe_idx[i] != -1) {
  1820. ret_int = usb_bam_connect(i, &reconnect_pipe_idx[i]);
  1821. if (ret_int) {
  1822. pr_err("%s: failure to reconnect pipe %d\n",
  1823. __func__, i);
  1824. ret = ret_int;
  1825. continue;
  1826. }
  1827. }
  1828. }
  1829. return ret;
  1830. }
  1831. static void usb_bam_sps_events(enum sps_callback_case sps_cb_case, void *user)
  1832. {
  1833. int i;
  1834. enum usb_bam bam;
  1835. struct usb_bam_pipe_connect *pipe_connect;
  1836. struct usb_bam_event_info *event_info;
  1837. switch (sps_cb_case) {
  1838. case SPS_CALLBACK_BAM_TIMER_IRQ:
  1839. pr_debug("%s:recieved SPS_CALLBACK_BAM_TIMER_IRQ\n", __func__);
  1840. spin_lock(&usb_bam_lock);
  1841. bam = get_bam_type_from_core_name((char *)user);
  1842. ctx.is_bam_inactivity[bam] = true;
  1843. pr_debug("%s: Incativity happened on bam=%s,%d\n", __func__,
  1844. (char *)user, bam);
  1845. for (i = 0; i < ctx.max_connections; i++) {
  1846. pipe_connect = &usb_bam_connections[i];
  1847. /*
  1848. * Notify inactivity once, Since it is global
  1849. * for all pipes on bam. Notify only if we have
  1850. * connected pipes.
  1851. */
  1852. if (pipe_connect->bam_type == bam &&
  1853. pipe_connect->enabled) {
  1854. event_info = &pipe_connect->event;
  1855. event_info->type = USB_BAM_EVENT_INACTIVITY;
  1856. event_info->param = pipe_connect->priv;
  1857. event_info->callback =
  1858. pipe_connect->inactivity_notify;
  1859. queue_work(ctx.usb_bam_wq,
  1860. &event_info->event_w);
  1861. break;
  1862. }
  1863. }
  1864. spin_unlock(&usb_bam_lock);
  1865. break;
  1866. default:
  1867. pr_debug("%s:received sps_cb_case=%d\n", __func__,
  1868. (int)sps_cb_case);
  1869. }
  1870. }
  1871. static struct msm_usb_bam_platform_data *usb_bam_dt_to_pdata(
  1872. struct platform_device *pdev)
  1873. {
  1874. struct msm_usb_bam_platform_data *pdata;
  1875. struct device_node *node = pdev->dev.of_node;
  1876. int rc = 0;
  1877. u8 i = 0;
  1878. bool reset_bam;
  1879. enum usb_bam bam;
  1880. u32 addr;
  1881. ctx.max_connections = 0;
  1882. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  1883. if (!pdata) {
  1884. pr_err("unable to allocate platform data\n");
  1885. return NULL;
  1886. }
  1887. rc = of_property_read_u32(node, "qcom,usb-bam-num-pipes",
  1888. &pdata->usb_bam_num_pipes);
  1889. if (rc) {
  1890. pr_err("Invalid usb bam num pipes property\n");
  1891. return NULL;
  1892. }
  1893. rc = of_property_read_u32(node, "qcom,usb-bam-fifo-baseaddr",
  1894. &addr);
  1895. if (rc)
  1896. pr_debug("%s: Invalid usb base address property\n", __func__);
  1897. else
  1898. pdata->usb_bam_fifo_baseaddr = addr;
  1899. pdata->ignore_core_reset_ack = of_property_read_bool(node,
  1900. "qcom,ignore-core-reset-ack");
  1901. pdata->disable_clk_gating = of_property_read_bool(node,
  1902. "qcom,disable-clk-gating");
  1903. for_each_child_of_node(pdev->dev.of_node, node)
  1904. ctx.max_connections++;
  1905. if (!ctx.max_connections) {
  1906. pr_err("%s: error: max_connections is zero\n", __func__);
  1907. goto err;
  1908. }
  1909. usb_bam_connections = devm_kzalloc(&pdev->dev, ctx.max_connections *
  1910. sizeof(struct usb_bam_pipe_connect), GFP_KERNEL);
  1911. if (!usb_bam_connections) {
  1912. pr_err("%s: devm_kzalloc failed(%d)\n", __func__, __LINE__);
  1913. return NULL;
  1914. }
  1915. /* retrieve device tree parameters */
  1916. for_each_child_of_node(pdev->dev.of_node, node) {
  1917. rc = of_property_read_string(node, "label",
  1918. &usb_bam_connections[i].name);
  1919. if (rc)
  1920. goto err;
  1921. rc = of_property_read_u32(node, "qcom,usb-bam-mem-type",
  1922. &usb_bam_connections[i].mem_type);
  1923. if (rc)
  1924. goto err;
  1925. if (usb_bam_connections[i].mem_type == USB_PRIVATE_MEM ||
  1926. usb_bam_connections[i].mem_type == OCI_MEM) {
  1927. if (!pdata->usb_bam_fifo_baseaddr) {
  1928. pr_err("%s: base address is missing\n",
  1929. __func__);
  1930. goto err;
  1931. }
  1932. }
  1933. rc = of_property_read_u32(node, "qcom,bam-type",
  1934. &usb_bam_connections[i].bam_type);
  1935. if (rc) {
  1936. pr_err("%s: bam type is missing in device tree\n",
  1937. __func__);
  1938. goto err;
  1939. }
  1940. bam = usb_bam_connections[i].bam_type;
  1941. rc = of_property_read_u32(node, "qcom,peer-bam",
  1942. &usb_bam_connections[i].peer_bam);
  1943. if (rc) {
  1944. pr_err("%s: peer bam is missing in device tree\n",
  1945. __func__);
  1946. goto err;
  1947. }
  1948. rc = of_property_read_u32(node, "qcom,dir",
  1949. &usb_bam_connections[i].dir);
  1950. if (rc) {
  1951. pr_err("%s: direction is missing in device tree\n",
  1952. __func__);
  1953. goto err;
  1954. }
  1955. rc = of_property_read_u32(node, "qcom,pipe-num",
  1956. &usb_bam_connections[i].pipe_num);
  1957. if (rc) {
  1958. pr_err("%s: pipe num is missing in device tree\n",
  1959. __func__);
  1960. goto err;
  1961. }
  1962. reset_bam = of_property_read_bool(node,
  1963. "qcom,reset-bam-on-connect");
  1964. if (reset_bam)
  1965. pdata->reset_on_connect[bam] = true;
  1966. of_property_read_u32(node, "qcom,src-bam-physical-address",
  1967. &usb_bam_connections[i].src_phy_addr);
  1968. of_property_read_u32(node, "qcom,src-bam-pipe-index",
  1969. &usb_bam_connections[i].src_pipe_index);
  1970. of_property_read_u32(node, "qcom,dst-bam-physical-address",
  1971. &usb_bam_connections[i].dst_phy_addr);
  1972. of_property_read_u32(node, "qcom,dst-bam-pipe-index",
  1973. &usb_bam_connections[i].dst_pipe_index);
  1974. of_property_read_u32(node, "qcom,data-fifo-offset",
  1975. &usb_bam_connections[i].data_fifo_base_offset);
  1976. rc = of_property_read_u32(node, "qcom,data-fifo-size",
  1977. &usb_bam_connections[i].data_fifo_size);
  1978. if (rc)
  1979. goto err;
  1980. of_property_read_u32(node, "qcom,descriptor-fifo-offset",
  1981. &usb_bam_connections[i].desc_fifo_base_offset);
  1982. rc = of_property_read_u32(node, "qcom,descriptor-fifo-size",
  1983. &usb_bam_connections[i].desc_fifo_size);
  1984. if (rc)
  1985. goto err;
  1986. i++;
  1987. }
  1988. pdata->connections = usb_bam_connections;
  1989. return pdata;
  1990. err:
  1991. pr_err("%s: failed\n", __func__);
  1992. return NULL;
  1993. }
  1994. static int usb_bam_init(int bam_idx)
  1995. {
  1996. int ret, irq;
  1997. void *usb_virt_addr;
  1998. struct msm_usb_bam_platform_data *pdata =
  1999. ctx.usb_bam_pdev->dev.platform_data;
  2000. struct resource *res, *ram_resource;
  2001. struct sps_bam_props props = ctx.usb_bam_sps.usb_props;
  2002. pr_debug("%s: usb_bam_init - %s\n", __func__,
  2003. bam_enable_strings[bam_idx]);
  2004. res = platform_get_resource_byname(ctx.usb_bam_pdev, IORESOURCE_MEM,
  2005. bam_enable_strings[bam_idx]);
  2006. if (!res) {
  2007. dev_dbg(&ctx.usb_bam_pdev->dev, "bam not initialized\n");
  2008. return 0;
  2009. }
  2010. irq = platform_get_irq_byname(ctx.usb_bam_pdev,
  2011. bam_enable_strings[bam_idx]);
  2012. if (irq < 0) {
  2013. dev_err(&ctx.usb_bam_pdev->dev, "Unable to get IRQ resource\n");
  2014. return irq;
  2015. }
  2016. usb_virt_addr = devm_ioremap(&ctx.usb_bam_pdev->dev, res->start,
  2017. resource_size(res));
  2018. if (!usb_virt_addr) {
  2019. pr_err("%s: ioremap failed\n", __func__);
  2020. return -ENOMEM;
  2021. }
  2022. /* Check if USB3 pipe memory needs to be enabled */
  2023. if (bam_idx == SSUSB_BAM && bam_use_private_mem(bam_idx)) {
  2024. pr_debug("%s: Enabling USB private memory for: %s\n", __func__,
  2025. bam_enable_strings[bam_idx]);
  2026. ram_resource = platform_get_resource_byname(ctx.usb_bam_pdev,
  2027. IORESOURCE_MEM, "qscratch_ram1_reg");
  2028. if (!res) {
  2029. dev_err(&ctx.usb_bam_pdev->dev, "Unable to get qscratch\n");
  2030. ret = -ENODEV;
  2031. goto free_bam_regs;
  2032. }
  2033. ctx.qscratch_ram1_reg = devm_ioremap(&ctx.usb_bam_pdev->dev,
  2034. ram_resource->start,
  2035. resource_size(ram_resource));
  2036. if (!ctx.qscratch_ram1_reg) {
  2037. pr_err("%s: ioremap failed for qscratch\n", __func__);
  2038. ret = -ENOMEM;
  2039. goto free_bam_regs;
  2040. }
  2041. }
  2042. props.phys_addr = res->start;
  2043. props.virt_addr = usb_virt_addr;
  2044. props.virt_size = resource_size(res);
  2045. props.irq = irq;
  2046. props.summing_threshold = USB_THRESHOLD;
  2047. props.event_threshold = USB_THRESHOLD;
  2048. props.num_pipes = pdata->usb_bam_num_pipes;
  2049. props.callback = usb_bam_sps_events;
  2050. props.user = bam_enable_strings[bam_idx];
  2051. props.options = SPS_BAM_OPT_IRQ_WAKEUP;
  2052. /*
  2053. * HSUSB and HSIC Cores don't support RESET ACK signal to BAMs
  2054. * Hence, let BAM to ignore acknowledge from USB while resetting PIPE
  2055. */
  2056. if (pdata->ignore_core_reset_ack && bam_idx != SSUSB_BAM)
  2057. props.options = SPS_BAM_NO_EXT_P_RST;
  2058. if (pdata->disable_clk_gating)
  2059. props.options |= SPS_BAM_NO_LOCAL_CLK_GATING;
  2060. ret = sps_register_bam_device(&props, &(ctx.h_bam[bam_idx]));
  2061. if (ret < 0) {
  2062. pr_err("%s: register bam error %d\n", __func__, ret);
  2063. ret = -EFAULT;
  2064. goto free_qscratch_reg;
  2065. }
  2066. return 0;
  2067. free_qscratch_reg:
  2068. iounmap(ctx.qscratch_ram1_reg);
  2069. free_bam_regs:
  2070. iounmap(usb_virt_addr);
  2071. return ret;
  2072. }
  2073. static int enable_usb_bams(struct platform_device *pdev)
  2074. {
  2075. int ret, i;
  2076. for (i = 0; i < ARRAY_SIZE(bam_enable_strings); i++) {
  2077. ret = usb_bam_init(i);
  2078. if (ret) {
  2079. pr_err("failed to init usb bam %s\n",
  2080. bam_enable_strings[i]);
  2081. return ret;
  2082. }
  2083. }
  2084. ctx.usb_bam_sps.sps_pipes = devm_kzalloc(&pdev->dev,
  2085. ctx.max_connections * sizeof(struct sps_pipe *),
  2086. GFP_KERNEL);
  2087. if (!ctx.usb_bam_sps.sps_pipes) {
  2088. pr_err("%s: failed to allocate sps_pipes\n", __func__);
  2089. return -ENOMEM;
  2090. }
  2091. ctx.usb_bam_sps.sps_connections = devm_kzalloc(&pdev->dev,
  2092. ctx.max_connections * sizeof(struct sps_connect),
  2093. GFP_KERNEL);
  2094. if (!ctx.usb_bam_sps.sps_connections) {
  2095. pr_err("%s: failed to allocate sps_connections\n", __func__);
  2096. return -ENOMEM;
  2097. }
  2098. return 0;
  2099. }
  2100. static ssize_t
  2101. usb_bam_show_inactivity_timer(struct device *dev, struct device_attribute *attr,
  2102. char *buf)
  2103. {
  2104. char *buff = buf;
  2105. int i;
  2106. spin_lock(&usb_bam_lock);
  2107. for (i = 0; i < ARRAY_SIZE(bam_enable_strings); i++) {
  2108. buff += snprintf(buff, PAGE_SIZE, "%s: %dms\n",
  2109. bam_enable_strings[i],
  2110. ctx.inactivity_timer_ms[i]);
  2111. }
  2112. spin_unlock(&usb_bam_lock);
  2113. return buff - buf;
  2114. }
  2115. static ssize_t usb_bam_store_inactivity_timer(struct device *dev,
  2116. struct device_attribute *attr,
  2117. const char *buff, size_t count)
  2118. {
  2119. char buf[USB_BAM_MAX_STR_LEN];
  2120. char *trimmed_buf, *bam_str, *bam_name, *timer;
  2121. int timer_d;
  2122. enum usb_bam bam;
  2123. if (strnstr(buff, "help", USB_BAM_MAX_STR_LEN)) {
  2124. pr_info("Usage: <bam_name> <ms>,<bam_name> <ms>,...\n");
  2125. pr_info("\tbam_name: [%s, %s, %s]\n",
  2126. bam_enable_strings[SSUSB_BAM],
  2127. bam_enable_strings[HSUSB_BAM],
  2128. bam_enable_strings[HSIC_BAM]);
  2129. pr_info("\tms: time in ms. Use 0 to disable timer\n");
  2130. return count;
  2131. }
  2132. strlcpy(buf, buff, sizeof(buf));
  2133. trimmed_buf = strim(buf);
  2134. while (trimmed_buf) {
  2135. bam_str = strsep(&trimmed_buf, ",");
  2136. if (bam_str) {
  2137. bam_name = strsep(&bam_str, " ");
  2138. bam = get_bam_type_from_core_name(bam_name);
  2139. timer = strsep(&bam_str, " ");
  2140. sscanf(timer, "%d", &timer_d);
  2141. spin_lock(&usb_bam_lock);
  2142. /* Apply new timer setting if bam has running pipes */
  2143. if (ctx.inactivity_timer_ms[bam] != timer_d) {
  2144. ctx.inactivity_timer_ms[bam] = timer_d;
  2145. if (ctx.pipes_enabled_per_bam[bam] > 0 &&
  2146. !info.in_lpm[bam])
  2147. usb_bam_set_inactivity_timer(bam);
  2148. }
  2149. spin_unlock(&usb_bam_lock);
  2150. }
  2151. }
  2152. return count;
  2153. }
  2154. static DEVICE_ATTR(inactivity_timer, S_IWUSR | S_IRUSR,
  2155. usb_bam_show_inactivity_timer,
  2156. usb_bam_store_inactivity_timer);
  2157. static int usb_bam_probe(struct platform_device *pdev)
  2158. {
  2159. int ret, i;
  2160. struct msm_usb_bam_platform_data *pdata;
  2161. dev_dbg(&pdev->dev, "usb_bam_probe\n");
  2162. ret = device_create_file(&pdev->dev, &dev_attr_inactivity_timer);
  2163. if (ret) {
  2164. dev_err(&pdev->dev, "failed to create fs node\n");
  2165. return ret;
  2166. }
  2167. ctx.mem_clk = devm_clk_get(&pdev->dev, "mem_clk");
  2168. if (IS_ERR(ctx.mem_clk))
  2169. dev_dbg(&pdev->dev, "failed to get mem_clock\n");
  2170. ctx.mem_iface_clk = devm_clk_get(&pdev->dev, "mem_iface_clk");
  2171. if (IS_ERR(ctx.mem_iface_clk))
  2172. dev_dbg(&pdev->dev, "failed to get mem_iface_clock\n");
  2173. if (pdev->dev.of_node) {
  2174. dev_dbg(&pdev->dev, "device tree enabled\n");
  2175. pdata = usb_bam_dt_to_pdata(pdev);
  2176. if (!pdata)
  2177. return -EINVAL;
  2178. pdev->dev.platform_data = pdata;
  2179. } else if (!pdev->dev.platform_data) {
  2180. dev_err(&pdev->dev, "missing platform_data\n");
  2181. return -ENODEV;
  2182. } else {
  2183. pdata = pdev->dev.platform_data;
  2184. usb_bam_connections = pdata->connections;
  2185. ctx.max_connections = pdata->max_connections;
  2186. }
  2187. ctx.usb_bam_pdev = pdev;
  2188. for (i = 0; i < ctx.max_connections; i++) {
  2189. usb_bam_connections[i].enabled = 0;
  2190. INIT_WORK(&usb_bam_connections[i].event.event_w,
  2191. usb_bam_work);
  2192. }
  2193. for (i = 0; i < MAX_BAMS; i++) {
  2194. ctx.pipes_enabled_per_bam[i] = 0;
  2195. ctx.inactivity_timer_ms[i] = 0;
  2196. ctx.is_bam_inactivity[i] = false;
  2197. init_completion(&info.prod_avail[i]);
  2198. complete(&info.prod_avail[i]);
  2199. init_completion(&info.prod_released[i]);
  2200. complete(&info.prod_released[i]);
  2201. info.cur_prod_state[i] = IPA_RM_RESOURCE_RELEASED;
  2202. info.cur_cons_state[i] = IPA_RM_RESOURCE_RELEASED;
  2203. info.lpm_wait_handshake[i] = false;
  2204. }
  2205. INIT_WORK(&info.resume_work, usb_bam_finish_resume);
  2206. INIT_WORK(&info.suspend_work, usb_bam_start_suspend);
  2207. INIT_WORK(&info.finish_suspend_work, usb_bam_finish_suspend_);
  2208. mutex_init(&info.suspend_resume_mutex);
  2209. spin_lock_init(&usb_bam_peer_handshake_info_lock);
  2210. INIT_WORK(&peer_handshake_info.reset_event.event_w, usb_bam_sm_work);
  2211. init_completion(&ctx.reset_done);
  2212. complete(&ctx.reset_done);
  2213. ctx.usb_bam_wq = alloc_workqueue("usb_bam_wq",
  2214. WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
  2215. if (!ctx.usb_bam_wq) {
  2216. pr_err("unable to create workqueue usb_bam_wq\n");
  2217. return -ENOMEM;
  2218. }
  2219. ret = enable_usb_bams(pdev);
  2220. if (ret) {
  2221. destroy_workqueue(ctx.usb_bam_wq);
  2222. return ret;
  2223. }
  2224. spin_lock_init(&usb_bam_ipa_handshake_info_lock);
  2225. usb_bam_ipa_create_resources();
  2226. spin_lock_init(&usb_bam_lock);
  2227. return ret;
  2228. }
  2229. int usb_bam_get_qdss_idx(u8 num)
  2230. {
  2231. return usb_bam_get_connection_idx(ctx.qdss_core_name, QDSS_P_BAM,
  2232. PEER_PERIPHERAL_TO_USB, num);
  2233. }
  2234. EXPORT_SYMBOL(usb_bam_get_qdss_idx);
  2235. void usb_bam_set_qdss_core(const char *qdss_core)
  2236. {
  2237. strlcpy(ctx.qdss_core_name, qdss_core, USB_BAM_MAX_STR_LEN);
  2238. }
  2239. int get_bam2bam_connection_info(u8 idx, u32 *usb_bam_handle,
  2240. u32 *usb_bam_pipe_idx, u32 *peer_pipe_idx,
  2241. struct sps_mem_buffer *desc_fifo, struct sps_mem_buffer *data_fifo)
  2242. {
  2243. struct usb_bam_pipe_connect *pipe_connect = &usb_bam_connections[idx];
  2244. enum usb_bam_pipe_dir dir = pipe_connect->dir;
  2245. struct sps_connect *sps_connection =
  2246. &ctx.usb_bam_sps.sps_connections[idx];
  2247. if (dir == USB_TO_PEER_PERIPHERAL) {
  2248. *usb_bam_handle = sps_connection->source;
  2249. *usb_bam_pipe_idx = sps_connection->src_pipe_index;
  2250. *peer_pipe_idx = sps_connection->dest_pipe_index;
  2251. } else {
  2252. *usb_bam_handle = sps_connection->destination;
  2253. *usb_bam_pipe_idx = sps_connection->dest_pipe_index;
  2254. *peer_pipe_idx = sps_connection->src_pipe_index;
  2255. }
  2256. if (data_fifo)
  2257. memcpy(data_fifo, &pipe_connect->data_mem_buf,
  2258. sizeof(struct sps_mem_buffer));
  2259. if (desc_fifo)
  2260. memcpy(desc_fifo, &pipe_connect->desc_mem_buf,
  2261. sizeof(struct sps_mem_buffer));
  2262. return 0;
  2263. }
  2264. EXPORT_SYMBOL(get_bam2bam_connection_info);
  2265. int usb_bam_get_connection_idx(const char *core_name, enum peer_bam client,
  2266. enum usb_bam_pipe_dir dir, u32 num)
  2267. {
  2268. u8 i;
  2269. int bam_type;
  2270. bam_type = get_bam_type_from_core_name(core_name);
  2271. if (bam_type < 0)
  2272. return -EINVAL;
  2273. for (i = 0; i < ctx.max_connections; i++)
  2274. if (usb_bam_connections[i].bam_type == bam_type &&
  2275. usb_bam_connections[i].peer_bam == client &&
  2276. usb_bam_connections[i].dir == dir &&
  2277. usb_bam_connections[i].pipe_num == num) {
  2278. pr_debug("%s: index %d was found\n", __func__, i);
  2279. return i;
  2280. }
  2281. pr_err("%s: failed for %s\n", __func__, core_name);
  2282. return -ENODEV;
  2283. }
  2284. EXPORT_SYMBOL(usb_bam_get_connection_idx);
  2285. bool msm_bam_lpm_ok(void)
  2286. {
  2287. spin_lock(&usb_bam_ipa_handshake_info_lock);
  2288. if (info.lpm_wait_handshake[HSUSB_BAM] || info.lpm_wait_pipes) {
  2289. info.pending_lpm = 1;
  2290. spin_unlock(&usb_bam_ipa_handshake_info_lock);
  2291. pr_err("%s: Scheduling LPM for later\n", __func__);
  2292. return 0;
  2293. } else {
  2294. info.pending_lpm = 0;
  2295. info.in_lpm[HSUSB_BAM] = true;
  2296. spin_unlock(&usb_bam_ipa_handshake_info_lock);
  2297. pr_err("%s: Going to LPM now\n", __func__);
  2298. return 1;
  2299. }
  2300. }
  2301. EXPORT_SYMBOL(msm_bam_lpm_ok);
  2302. void msm_bam_notify_lpm_resume()
  2303. {
  2304. /*
  2305. * If core was resumed from lpm, just clear the
  2306. * pending indication, in case it is set.
  2307. */
  2308. info.pending_lpm = 0;
  2309. }
  2310. EXPORT_SYMBOL(msm_bam_notify_lpm_resume);
  2311. static int usb_bam_remove(struct platform_device *pdev)
  2312. {
  2313. destroy_workqueue(ctx.usb_bam_wq);
  2314. device_remove_file(&pdev->dev, &dev_attr_inactivity_timer);
  2315. return 0;
  2316. }
  2317. static const struct of_device_id usb_bam_dt_match[] = {
  2318. { .compatible = "qcom,usb-bam-msm",
  2319. },
  2320. {}
  2321. };
  2322. MODULE_DEVICE_TABLE(of, usb_bam_dt_match);
  2323. static struct platform_driver usb_bam_driver = {
  2324. .probe = usb_bam_probe,
  2325. .remove = usb_bam_remove,
  2326. .driver = {
  2327. .name = "usb_bam",
  2328. .of_match_table = usb_bam_dt_match,
  2329. },
  2330. };
  2331. static int __init init(void)
  2332. {
  2333. return platform_driver_register(&usb_bam_driver);
  2334. }
  2335. module_init(init);
  2336. static void __exit cleanup(void)
  2337. {
  2338. platform_driver_unregister(&usb_bam_driver);
  2339. }
  2340. module_exit(cleanup);
  2341. MODULE_DESCRIPTION("MSM USB BAM DRIVER");
  2342. MODULE_LICENSE("GPL v2");