slim-msm-ngd.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775
  1. /* Copyright (c) 2011-2014, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/irq.h>
  13. #include <linux/kernel.h>
  14. #include <linux/init.h>
  15. #include <linux/slab.h>
  16. #include <linux/io.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/dma-mapping.h>
  20. #include <linux/slimbus/slimbus.h>
  21. #include <linux/delay.h>
  22. #include <linux/kthread.h>
  23. #include <linux/clk.h>
  24. #include <linux/pm_runtime.h>
  25. #include <linux/of.h>
  26. #include <linux/of_slimbus.h>
  27. #include <linux/timer.h>
  28. #include <mach/sps.h>
  29. #include "slim-msm.h"
  30. #define NGD_SLIM_NAME "ngd_msm_ctrl"
  31. #define SLIM_LA_MGR 0xFF
  32. #define SLIM_ROOT_FREQ 24576000
  33. #define LADDR_RETRY 5
  34. #ifdef CONFIG_SND_SOC_ES325_SLIM
  35. #define PREVENT_SLIMBUS_SLEEP_IN_FW_DL
  36. #endif
  37. #define NGD_BASE_V1(r) (((r) % 2) ? 0x800 : 0xA00)
  38. #define NGD_BASE_V2(r) (((r) % 2) ? 0x1000 : 0x2000)
  39. #define NGD_BASE(r, v) ((v) ? NGD_BASE_V2(r) : NGD_BASE_V1(r))
  40. /* NGD (Non-ported Generic Device) registers */
  41. enum ngd_reg {
  42. NGD_CFG = 0x0,
  43. NGD_STATUS = 0x4,
  44. NGD_RX_MSGQ_CFG = 0x8,
  45. NGD_INT_EN = 0x10,
  46. NGD_INT_STAT = 0x14,
  47. NGD_INT_CLR = 0x18,
  48. NGD_TX_MSG = 0x30,
  49. NGD_RX_MSG = 0x70,
  50. NGD_IE_STAT = 0xF0,
  51. NGD_VE_STAT = 0x100,
  52. };
  53. enum ngd_msg_cfg {
  54. NGD_CFG_ENABLE = 1,
  55. NGD_CFG_RX_MSGQ_EN = 1 << 1,
  56. NGD_CFG_TX_MSGQ_EN = 1 << 2,
  57. };
  58. enum ngd_intr {
  59. NGD_INT_RECFG_DONE = 1 << 24,
  60. NGD_INT_TX_NACKED_2 = 1 << 25,
  61. NGD_INT_MSG_BUF_CONTE = 1 << 26,
  62. NGD_INT_MSG_TX_INVAL = 1 << 27,
  63. NGD_INT_IE_VE_CHG = 1 << 28,
  64. NGD_INT_DEV_ERR = 1 << 29,
  65. NGD_INT_RX_MSG_RCVD = 1 << 30,
  66. NGD_INT_TX_MSG_SENT = 1 << 31,
  67. };
  68. enum ngd_offsets {
  69. NGD_NACKED_MC = 0x7F00000,
  70. NGD_ACKED_MC = 0xFE000,
  71. NGD_ERROR = 0x1800,
  72. NGD_MSGQ_SUPPORT = 0x400,
  73. NGD_RX_MSGQ_TIME_OUT = 0x16,
  74. NGD_ENUMERATED = 0x1,
  75. NGD_TX_BUSY = 0x0,
  76. };
  77. enum ngd_status {
  78. NGD_LADDR = 1 << 1,
  79. };
  80. extern unsigned int system_rev;
  81. static int ngd_slim_runtime_resume(struct device *device);
  82. static int ngd_slim_power_up(struct msm_slim_ctrl *dev, bool mdm_restart);
  83. #if defined(PREVENT_SLIMBUS_SLEEP_IN_FW_DL)
  84. static int es325_slim_write_flag = 0;
  85. void msm_slim_es325_write_flag_set(int flag)
  86. {
  87. pr_info("%s():es325_slim_write_flag = %d\n", __func__, flag);
  88. if(es325_slim_write_flag != flag) {
  89. es325_slim_write_flag = flag;
  90. pr_info("%s():es325_slim_write_flag = %d\n", __func__, es325_slim_write_flag);
  91. }
  92. }
  93. EXPORT_SYMBOL(msm_slim_es325_write_flag_set);
  94. #endif
  95. static irqreturn_t ngd_slim_interrupt(int irq, void *d)
  96. {
  97. struct msm_slim_ctrl *dev = (struct msm_slim_ctrl *)d;
  98. void __iomem *ngd = dev->base + NGD_BASE(dev->ctrl.nr, dev->ver);
  99. u32 stat = readl_relaxed(ngd + NGD_INT_STAT);
  100. u32 pstat;
  101. if ((stat & NGD_INT_MSG_BUF_CONTE) ||
  102. (stat & NGD_INT_MSG_TX_INVAL) || (stat & NGD_INT_DEV_ERR) ||
  103. (stat & NGD_INT_TX_NACKED_2)) {
  104. writel_relaxed(stat, ngd + NGD_INT_CLR);
  105. dev->err = -EIO;
  106. SLIM_WARN(dev, "NGD interrupt error:0x%x, err:%d\n", stat,
  107. dev->err);
  108. /* Guarantee that error interrupts are cleared */
  109. mb();
  110. msm_slim_manage_tx_msgq(dev, false, NULL);
  111. } else if (stat & NGD_INT_TX_MSG_SENT) {
  112. writel_relaxed(NGD_INT_TX_MSG_SENT, ngd + NGD_INT_CLR);
  113. /* Make sure interrupt is cleared */
  114. mb();
  115. msm_slim_manage_tx_msgq(dev, false, NULL);
  116. }
  117. if (stat & NGD_INT_RX_MSG_RCVD) {
  118. u32 rx_buf[10];
  119. u8 len, i;
  120. rx_buf[0] = readl_relaxed(ngd + NGD_RX_MSG);
  121. len = rx_buf[0] & 0x1F;
  122. for (i = 1; i < ((len + 3) >> 2); i++) {
  123. rx_buf[i] = readl_relaxed(ngd + NGD_RX_MSG +
  124. (4 * i));
  125. SLIM_DBG(dev, "REG-RX data: %x\n", rx_buf[i]);
  126. }
  127. msm_slim_rx_enqueue(dev, rx_buf, len);
  128. writel_relaxed(NGD_INT_RX_MSG_RCVD,
  129. ngd + NGD_INT_CLR);
  130. /*
  131. * Guarantee that CLR bit write goes through before
  132. * queuing work
  133. */
  134. mb();
  135. if (dev->use_rx_msgqs == MSM_MSGQ_ENABLED)
  136. SLIM_WARN(dev, "direct msg rcvd with RX MSGQs\n");
  137. else
  138. complete(&dev->rx_msgq_notify);
  139. }
  140. if (stat & NGD_INT_RECFG_DONE) {
  141. writel_relaxed(NGD_INT_RECFG_DONE, ngd + NGD_INT_CLR);
  142. /* Guarantee RECONFIG DONE interrupt is cleared */
  143. mb();
  144. /* In satellite mode, just log the reconfig done IRQ */
  145. SLIM_DBG(dev, "reconfig done IRQ for NGD\n");
  146. }
  147. if (stat & NGD_INT_IE_VE_CHG) {
  148. writel_relaxed(NGD_INT_IE_VE_CHG, ngd + NGD_INT_CLR);
  149. /* Guarantee IE VE change interrupt is cleared */
  150. mb();
  151. SLIM_DBG(dev, "NGD IE VE change\n");
  152. }
  153. pstat = readl_relaxed(PGD_THIS_EE(PGD_PORT_INT_ST_EEn, dev->ver));
  154. if (pstat != 0)
  155. return msm_slim_port_irq_handler(dev, pstat);
  156. return IRQ_HANDLED;
  157. }
  158. static int ngd_qmi_available(struct notifier_block *n, unsigned long code,
  159. void *_cmd)
  160. {
  161. struct msm_slim_qmi *qmi = container_of(n, struct msm_slim_qmi, nb);
  162. struct msm_slim_ctrl *dev =
  163. container_of(qmi, struct msm_slim_ctrl, qmi);
  164. SLIM_INFO(dev, "Slimbus QMI NGD CB received event:%ld\n", code);
  165. switch (code) {
  166. case QMI_SERVER_ARRIVE:
  167. schedule_work(&qmi->ssr_up);
  168. break;
  169. default:
  170. break;
  171. }
  172. return 0;
  173. }
  174. static int dsp_ssr_notify_cb(struct notifier_block *n, unsigned long code,
  175. void *_cmd)
  176. {
  177. struct msm_slim_ss *dsp = container_of(n, struct msm_slim_ss, nb);
  178. struct msm_slim_ctrl *dev = container_of(dsp, struct msm_slim_ctrl,
  179. dsp);
  180. switch (code) {
  181. case SUBSYS_BEFORE_SHUTDOWN:
  182. SLIM_INFO(dev, "SLIM DSP SSR notify cb:%lu\n", code);
  183. /* wait for current transaction */
  184. mutex_lock(&dev->tx_lock);
  185. /* make sure autosuspend is not called until ADSP comes up*/
  186. pm_runtime_get_noresume(dev->dev);
  187. dev->state = MSM_CTRL_DOWN;
  188. /* Reset ctrl_up completion */
  189. init_completion(&dev->ctrl_up);
  190. /* disconnect BAM pipes */
  191. if (dev->use_rx_msgqs == MSM_MSGQ_ENABLED)
  192. dev->use_rx_msgqs = MSM_MSGQ_DOWN;
  193. if (dev->use_tx_msgqs == MSM_MSGQ_ENABLED)
  194. dev->use_tx_msgqs = MSM_MSGQ_DOWN;
  195. msm_slim_sps_exit(dev, false);
  196. schedule_work(&dev->qmi.ssr_down);
  197. mutex_unlock(&dev->tx_lock);
  198. break;
  199. default:
  200. break;
  201. }
  202. return NOTIFY_DONE;
  203. }
  204. static int mdm_ssr_notify_cb(struct notifier_block *n, unsigned long code,
  205. void *_cmd)
  206. {
  207. void __iomem *ngd;
  208. struct msm_slim_ss *ext_mdm = container_of(n, struct msm_slim_ss, nb);
  209. struct msm_slim_ctrl *dev = container_of(ext_mdm, struct msm_slim_ctrl,
  210. ext_mdm);
  211. struct slim_controller *ctrl = &dev->ctrl;
  212. u32 laddr;
  213. struct slim_device *sbdev;
  214. switch (code) {
  215. case SUBSYS_BEFORE_SHUTDOWN:
  216. SLIM_INFO(dev, "SLIM %lu external_modem SSR notify cb\n", code);
  217. /* vote for runtime-pm so that ADSP doesn't go down */
  218. msm_slim_get_ctrl(dev);
  219. /*
  220. * checking framer here will wake-up ADSP and may avoid framer
  221. * handover later
  222. */
  223. msm_slim_qmi_check_framer_request(dev);
  224. dev->ext_mdm.state = MSM_CTRL_DOWN;
  225. msm_slim_put_ctrl(dev);
  226. break;
  227. case SUBSYS_AFTER_POWERUP:
  228. if (dev->ext_mdm.state != MSM_CTRL_DOWN)
  229. return NOTIFY_DONE;
  230. SLIM_INFO(dev,
  231. "SLIM %lu external_modem SSR notify cb\n", code);
  232. /* vote for runtime-pm so that ADSP doesn't go down */
  233. msm_slim_get_ctrl(dev);
  234. msm_slim_qmi_check_framer_request(dev);
  235. /* If NGD enumeration is lost, we will need to power us up */
  236. ngd = dev->base + NGD_BASE(dev->ctrl.nr, dev->ver);
  237. laddr = readl_relaxed(ngd + NGD_STATUS);
  238. if (!(laddr & NGD_LADDR)) {
  239. mutex_lock(&dev->tx_lock);
  240. /* runtime-pm state should be consistent with HW */
  241. pm_runtime_disable(dev->dev);
  242. pm_runtime_set_suspended(dev->dev);
  243. dev->state = MSM_CTRL_DOWN;
  244. mutex_unlock(&dev->tx_lock);
  245. SLIM_INFO(dev,
  246. "SLIM MDM SSR (active framer on MDM) dev-down\n");
  247. list_for_each_entry(sbdev, &ctrl->devs, dev_list)
  248. slim_report_absent(sbdev);
  249. ngd_slim_runtime_resume(dev->dev);
  250. pm_runtime_set_active(dev->dev);
  251. pm_runtime_enable(dev->dev);
  252. }
  253. dev->ext_mdm.state = MSM_CTRL_AWAKE;
  254. msm_slim_put_ctrl(dev);
  255. break;
  256. default:
  257. break;
  258. }
  259. return NOTIFY_DONE;
  260. }
  261. static int ngd_get_tid(struct slim_controller *ctrl, struct slim_msg_txn *txn,
  262. u8 *tid, struct completion *done)
  263. {
  264. struct msm_slim_ctrl *dev = slim_get_ctrldata(ctrl);
  265. mutex_lock(&ctrl->m_ctrl);
  266. if (ctrl->last_tid <= 255) {
  267. ctrl->txnt = krealloc(ctrl->txnt,
  268. (ctrl->last_tid + 1) *
  269. sizeof(struct slim_msg_txn *),
  270. GFP_KERNEL);
  271. if (!ctrl->txnt) {
  272. mutex_unlock(&ctrl->m_ctrl);
  273. return -ENOMEM;
  274. }
  275. dev->msg_cnt = ctrl->last_tid;
  276. ctrl->last_tid++;
  277. } else {
  278. int i;
  279. for (i = 0; i < 256; i++) {
  280. dev->msg_cnt = ((dev->msg_cnt + 1) & 0xFF);
  281. if (ctrl->txnt[dev->msg_cnt] == NULL)
  282. break;
  283. }
  284. if (i >= 256) {
  285. dev_err(&ctrl->dev, "out of TID");
  286. mutex_unlock(&ctrl->m_ctrl);
  287. return -ENOMEM;
  288. }
  289. }
  290. ctrl->txnt[dev->msg_cnt] = txn;
  291. txn->tid = dev->msg_cnt;
  292. txn->comp = done;
  293. *tid = dev->msg_cnt;
  294. mutex_unlock(&ctrl->m_ctrl);
  295. return 0;
  296. }
  297. static int ngd_xfer_msg(struct slim_controller *ctrl, struct slim_msg_txn *txn)
  298. {
  299. DECLARE_COMPLETION_ONSTACK(done);
  300. DECLARE_COMPLETION_ONSTACK(tx_sent);
  301. struct msm_slim_ctrl *dev = slim_get_ctrldata(ctrl);
  302. u32 *pbuf;
  303. u8 *puc;
  304. int ret = 0;
  305. u8 la = txn->la;
  306. u8 txn_mt;
  307. u16 txn_mc = txn->mc;
  308. u8 wbuf[SLIM_MSGQ_BUF_LEN];
  309. bool report_sat = false;
  310. bool sync_wr = true;
  311. if (txn->mc & SLIM_MSG_CLK_PAUSE_SEQ_FLG)
  312. return -EPROTONOSUPPORT;
  313. if (txn->mt == SLIM_MSG_MT_CORE &&
  314. (txn->mc >= SLIM_MSG_MC_BEGIN_RECONFIGURATION &&
  315. txn->mc <= SLIM_MSG_MC_RECONFIGURE_NOW))
  316. return 0;
  317. if (txn->mc == SLIM_USR_MC_REPORT_SATELLITE &&
  318. txn->mt == SLIM_MSG_MT_SRC_REFERRED_USER)
  319. report_sat = true;
  320. else
  321. mutex_lock(&dev->tx_lock);
  322. if (!report_sat && !pm_runtime_enabled(dev->dev) &&
  323. dev->state == MSM_CTRL_ASLEEP) {
  324. /*
  325. * Counter-part of system-suspend when runtime-pm is not enabled
  326. * This way, resume can be left empty and device will be put in
  327. * active mode only if client requests anything on the bus
  328. * If the state was DOWN, SSR UP notification will take
  329. * care of putting the device in active state.
  330. */
  331. mutex_unlock(&dev->tx_lock);
  332. ret = ngd_slim_runtime_resume(dev->dev);
  333. if (ret) {
  334. SLIM_ERR(dev, "slim resume failed ret:%d, state:%d",
  335. ret, dev->state);
  336. return -EREMOTEIO;
  337. }
  338. mutex_lock(&dev->tx_lock);
  339. }
  340. /* If txn is tried when controller is down, wait for ADSP to boot */
  341. if (!report_sat) {
  342. if (dev->state == MSM_CTRL_DOWN) {
  343. u8 mc = (u8)txn->mc;
  344. int timeout;
  345. mutex_unlock(&dev->tx_lock);
  346. SLIM_INFO(dev, "ADSP slimbus not up yet\n");
  347. /*
  348. * Messages related to data channel management can't
  349. * wait since they are holding reconfiguration lock.
  350. * clk_pause in resume (which can change state back to
  351. * MSM_CTRL_AWAKE), will need that lock.
  352. * Port disconnection, channel removal calls should pass
  353. * through since there is no activity on the bus and
  354. * those calls are triggered by clients due to
  355. * device_down callback in that situation.
  356. * Returning 0 on the disconnections and
  357. * removals will ensure consistent state of channels,
  358. * ports with the HW
  359. * Remote requests to remove channel/port will be
  360. * returned from the path where they wait on
  361. * acknowledgement from ADSP
  362. */
  363. if ((txn->mt == SLIM_MSG_MT_DEST_REFERRED_USER) &&
  364. ((mc == SLIM_USR_MC_CHAN_CTRL ||
  365. mc == SLIM_USR_MC_DISCONNECT_PORT ||
  366. mc == SLIM_USR_MC_RECONFIG_NOW)))
  367. return -EREMOTEIO;
  368. if ((txn->mt == SLIM_MSG_MT_CORE) &&
  369. ((mc == SLIM_MSG_MC_DISCONNECT_PORT ||
  370. mc == SLIM_MSG_MC_NEXT_REMOVE_CHANNEL ||
  371. mc == SLIM_USR_MC_RECONFIG_NOW)))
  372. return 0;
  373. if ((txn->mt == SLIM_MSG_MT_CORE) &&
  374. ((mc >= SLIM_MSG_MC_CONNECT_SOURCE &&
  375. mc <= SLIM_MSG_MC_CHANGE_CONTENT) ||
  376. (mc >= SLIM_MSG_MC_BEGIN_RECONFIGURATION &&
  377. mc <= SLIM_MSG_MC_RECONFIGURE_NOW)))
  378. return -EREMOTEIO;
  379. if ((txn->mt == SLIM_MSG_MT_DEST_REFERRED_USER) &&
  380. ((mc >= SLIM_USR_MC_DEFINE_CHAN &&
  381. mc < SLIM_USR_MC_DISCONNECT_PORT)))
  382. return -EREMOTEIO;
  383. timeout = wait_for_completion_timeout(&dev->ctrl_up,
  384. HZ);
  385. if (!timeout)
  386. return -ETIMEDOUT;
  387. mutex_lock(&dev->tx_lock);
  388. }
  389. mutex_unlock(&dev->tx_lock);
  390. ret = msm_slim_get_ctrl(dev);
  391. mutex_lock(&dev->tx_lock);
  392. /*
  393. * Runtime-pm's callbacks are not called until runtime-pm's
  394. * error status is cleared
  395. * Setting runtime status to suspended clears the error
  396. * It also makes HW status cosistent with what SW has it here
  397. */
  398. if ((pm_runtime_enabled(dev->dev) && ret < 0) ||
  399. dev->state == MSM_CTRL_DOWN) {
  400. SLIM_ERR(dev, "slim ctrl vote failed ret:%d, state:%d",
  401. ret, dev->state);
  402. pm_runtime_set_suspended(dev->dev);
  403. mutex_unlock(&dev->tx_lock);
  404. msm_slim_put_ctrl(dev);
  405. return -EREMOTEIO;
  406. }
  407. }
  408. if (txn->mt == SLIM_MSG_MT_CORE &&
  409. (txn->mc == SLIM_MSG_MC_CONNECT_SOURCE ||
  410. txn->mc == SLIM_MSG_MC_CONNECT_SINK ||
  411. txn->mc == SLIM_MSG_MC_DISCONNECT_PORT)) {
  412. int i = 0;
  413. if (txn->mc != SLIM_MSG_MC_DISCONNECT_PORT)
  414. SLIM_INFO(dev,
  415. "Connect port: laddr 0x%x port_num %d chan_num %d\n",
  416. txn->la, txn->wbuf[0], txn->wbuf[1]);
  417. else
  418. SLIM_INFO(dev,
  419. "Disconnect port: laddr 0x%x port_num %d\n",
  420. txn->la, txn->wbuf[0]);
  421. txn->mt = SLIM_MSG_MT_DEST_REFERRED_USER;
  422. if (txn->mc == SLIM_MSG_MC_CONNECT_SOURCE)
  423. txn->mc = SLIM_USR_MC_CONNECT_SRC;
  424. else if (txn->mc == SLIM_MSG_MC_CONNECT_SINK)
  425. txn->mc = SLIM_USR_MC_CONNECT_SINK;
  426. else if (txn->mc == SLIM_MSG_MC_DISCONNECT_PORT)
  427. txn->mc = SLIM_USR_MC_DISCONNECT_PORT;
  428. if (txn->la == SLIM_LA_MGR) {
  429. if (dev->pgdla == SLIM_LA_MGR) {
  430. u8 ea[] = {0, QC_DEVID_PGD, 0, 0, QC_MFGID_MSB,
  431. QC_MFGID_LSB};
  432. ea[2] = (u8)(dev->pdata.eapc & 0xFF);
  433. ea[3] = (u8)((dev->pdata.eapc & 0xFF00) >> 8);
  434. mutex_unlock(&dev->tx_lock);
  435. ret = dev->ctrl.get_laddr(&dev->ctrl, ea, 6,
  436. &dev->pgdla);
  437. SLIM_DBG(dev, "SLIM PGD LA:0x%x, ret:%d\n",
  438. dev->pgdla, ret);
  439. if (ret) {
  440. SLIM_ERR(dev,
  441. "Incorrect SLIM-PGD EAPC:0x%x\n",
  442. dev->pdata.eapc);
  443. return ret;
  444. }
  445. mutex_lock(&dev->tx_lock);
  446. }
  447. txn->la = dev->pgdla;
  448. }
  449. wbuf[i++] = txn->la;
  450. la = SLIM_LA_MGR;
  451. wbuf[i++] = txn->wbuf[0];
  452. if (txn->mc != SLIM_USR_MC_DISCONNECT_PORT)
  453. wbuf[i++] = txn->wbuf[1];
  454. ret = ngd_get_tid(ctrl, txn, &wbuf[i++], &done);
  455. if (ret) {
  456. SLIM_ERR(dev, "TID for connect/disconnect fail:%d\n",
  457. ret);
  458. goto ngd_xfer_err;
  459. }
  460. txn->len = i;
  461. txn->wbuf = wbuf;
  462. txn->rl = txn->len + 4;
  463. }
  464. txn->rl--;
  465. if (txn->mt == SLIM_MSG_MT_CORE && txn->comp &&
  466. dev->use_tx_msgqs == MSM_MSGQ_ENABLED &&
  467. (txn_mc != SLIM_MSG_MC_REQUEST_INFORMATION &&
  468. txn_mc != SLIM_MSG_MC_REQUEST_VALUE &&
  469. txn_mc != SLIM_MSG_MC_REQUEST_CHANGE_VALUE &&
  470. txn_mc != SLIM_MSG_MC_REQUEST_CLEAR_INFORMATION)) {
  471. sync_wr = false;
  472. pbuf = msm_get_msg_buf(dev, txn->rl, txn->comp);
  473. } else if (txn->mt == SLIM_MSG_MT_DEST_REFERRED_USER &&
  474. dev->use_tx_msgqs == MSM_MSGQ_ENABLED &&
  475. txn->mc == SLIM_USR_MC_REPEAT_CHANGE_VALUE &&
  476. txn->comp) {
  477. sync_wr = false;
  478. pbuf = msm_get_msg_buf(dev, txn->rl, txn->comp);
  479. } else {
  480. pbuf = msm_get_msg_buf(dev, txn->rl, &tx_sent);
  481. }
  482. if (!pbuf) {
  483. SLIM_ERR(dev, "Message buffer unavailable\n");
  484. ret = -ENOMEM;
  485. goto ngd_xfer_err;
  486. }
  487. dev->err = 0;
  488. if (txn->dt == SLIM_MSG_DEST_ENUMADDR) {
  489. ret = -EPROTONOSUPPORT;
  490. goto ngd_xfer_err;
  491. }
  492. if (txn->dt == SLIM_MSG_DEST_LOGICALADDR)
  493. *pbuf = SLIM_MSG_ASM_FIRST_WORD(txn->rl, txn->mt, txn->mc, 0,
  494. la);
  495. else
  496. *pbuf = SLIM_MSG_ASM_FIRST_WORD(txn->rl, txn->mt, txn->mc, 1,
  497. la);
  498. if (txn->dt == SLIM_MSG_DEST_LOGICALADDR)
  499. puc = ((u8 *)pbuf) + 3;
  500. else
  501. puc = ((u8 *)pbuf) + 2;
  502. if (txn->rbuf)
  503. *(puc++) = txn->tid;
  504. if (((txn->mt == SLIM_MSG_MT_CORE) &&
  505. ((txn->mc >= SLIM_MSG_MC_REQUEST_INFORMATION &&
  506. txn->mc <= SLIM_MSG_MC_REPORT_INFORMATION) ||
  507. (txn->mc >= SLIM_MSG_MC_REQUEST_VALUE &&
  508. txn->mc <= SLIM_MSG_MC_CHANGE_VALUE))) ||
  509. (txn->mc == SLIM_USR_MC_REPEAT_CHANGE_VALUE &&
  510. txn->mt == SLIM_MSG_MT_DEST_REFERRED_USER)) {
  511. *(puc++) = (txn->ec & 0xFF);
  512. *(puc++) = (txn->ec >> 8)&0xFF;
  513. }
  514. if (txn->wbuf)
  515. memcpy(puc, txn->wbuf, txn->len);
  516. if (txn->mt == SLIM_MSG_MT_DEST_REFERRED_USER &&
  517. (txn->mc == SLIM_USR_MC_CONNECT_SRC ||
  518. txn->mc == SLIM_USR_MC_CONNECT_SINK ||
  519. txn->mc == SLIM_USR_MC_DISCONNECT_PORT) && txn->wbuf &&
  520. wbuf[0] == dev->pgdla) {
  521. if (txn->mc != SLIM_USR_MC_DISCONNECT_PORT)
  522. dev->err = msm_slim_connect_pipe_port(dev, wbuf[1]);
  523. else {
  524. /*
  525. * Remove channel disconnects master-side ports from
  526. * channel. No need to send that again on the bus
  527. * Only disable port
  528. */
  529. writel_relaxed(0, PGD_PORT(PGD_PORT_CFGn,
  530. (wbuf[1] + dev->port_b), dev->ver));
  531. mutex_unlock(&dev->tx_lock);
  532. msm_slim_put_ctrl(dev);
  533. return 0;
  534. }
  535. if (dev->err) {
  536. SLIM_ERR(dev, "pipe-port connect err:%d\n", dev->err);
  537. goto ngd_xfer_err;
  538. }
  539. /* Add port-base to port number if this is manager side port */
  540. puc[1] += dev->port_b;
  541. }
  542. dev->err = 0;
  543. /*
  544. * If it's a read txn, it may be freed if a response is received by
  545. * received thread before reaching end of this function.
  546. * mc, mt may have changed to convert standard slimbus code/type to
  547. * satellite user-defined message. Reinitialize again
  548. */
  549. txn_mc = txn->mc;
  550. txn_mt = txn->mt;
  551. ret = msm_send_msg_buf(dev, pbuf, txn->rl,
  552. NGD_BASE(dev->ctrl.nr, dev->ver) + NGD_TX_MSG);
  553. if (!ret && sync_wr) {
  554. int timeout = wait_for_completion_timeout(&tx_sent, HZ);
  555. if (!timeout) {
  556. ret = -ETIMEDOUT;
  557. /*
  558. * disconnect/recoonect pipe so that subsequent
  559. * transactions don't timeout due to unavailable
  560. * descriptors
  561. */
  562. if (dev->state != MSM_CTRL_DOWN) {
  563. msm_slim_disconnect_endp(dev, &dev->tx_msgq,
  564. &dev->use_tx_msgqs);
  565. msm_slim_connect_endp(dev, &dev->tx_msgq, NULL);
  566. }
  567. } else {
  568. ret = dev->err;
  569. }
  570. }
  571. if (ret) {
  572. u32 conf, stat, rx_msgq, int_stat, int_en, int_clr;
  573. void __iomem *ngd = dev->base + NGD_BASE(dev->ctrl.nr,
  574. dev->ver);
  575. SLIM_WARN(dev, "TX failed :MC:0x%x,mt:0x%x, ret:%d, ver:%d\n",
  576. txn_mc, txn_mt, ret, dev->ver);
  577. conf = readl_relaxed(ngd);
  578. stat = readl_relaxed(ngd + NGD_STATUS);
  579. rx_msgq = readl_relaxed(ngd + NGD_RX_MSGQ_CFG);
  580. int_stat = readl_relaxed(ngd + NGD_INT_STAT);
  581. int_en = readl_relaxed(ngd + NGD_INT_EN);
  582. int_clr = readl_relaxed(ngd + NGD_INT_CLR);
  583. SLIM_WARN(dev, "conf:0x%x,stat:0x%x,rxmsgq:0x%x\n",
  584. conf, stat, rx_msgq);
  585. SLIM_WARN(dev, "int_stat:0x%x,int_en:0x%x,int_cll:0x%x\n",
  586. int_stat, int_en, int_clr);
  587. } else if (txn_mt == SLIM_MSG_MT_DEST_REFERRED_USER &&
  588. (txn_mc == SLIM_USR_MC_CONNECT_SRC ||
  589. txn_mc == SLIM_USR_MC_CONNECT_SINK ||
  590. txn_mc == SLIM_USR_MC_DISCONNECT_PORT)) {
  591. int timeout;
  592. mutex_unlock(&dev->tx_lock);
  593. msm_slim_put_ctrl(dev);
  594. timeout = wait_for_completion_timeout(txn->comp, HZ);
  595. if (!timeout)
  596. ret = -ETIMEDOUT;
  597. else
  598. ret = txn->ec;
  599. if (ret) {
  600. SLIM_INFO(dev,
  601. "connect/disconnect:0x%x,tid:%d err:%d\n",
  602. txn->mc, txn->tid, ret);
  603. mutex_lock(&ctrl->m_ctrl);
  604. ctrl->txnt[txn->tid] = NULL;
  605. mutex_unlock(&ctrl->m_ctrl);
  606. }
  607. return ret ? ret : dev->err;
  608. }
  609. ngd_xfer_err:
  610. if (!report_sat) {
  611. mutex_unlock(&dev->tx_lock);
  612. msm_slim_put_ctrl(dev);
  613. }
  614. return ret ? ret : dev->err;
  615. }
  616. static int ngd_user_msg(struct slim_controller *ctrl, u8 la, u8 mt, u8 mc,
  617. struct slim_ele_access *msg, u8 *buf, u8 len)
  618. {
  619. struct slim_msg_txn txn;
  620. if (mt != SLIM_MSG_MT_DEST_REFERRED_USER ||
  621. mc != SLIM_USR_MC_REPEAT_CHANGE_VALUE) {
  622. return -EPROTONOSUPPORT;
  623. }
  624. if (len > SLIM_MAX_VE_SLC_BYTES ||
  625. msg->start_offset > MSM_SLIM_VE_MAX_MAP_ADDR)
  626. return -EINVAL;
  627. if (len <= 4) {
  628. txn.ec = len - 1;
  629. } else if (len <= 8) {
  630. if (len & 0x1)
  631. return -EINVAL;
  632. txn.ec = ((len >> 1) + 1);
  633. } else {
  634. if (len & 0x3)
  635. return -EINVAL;
  636. txn.ec = ((len >> 2) + 3);
  637. }
  638. txn.ec |= (0x8 | ((msg->start_offset & 0xF) << 4));
  639. txn.ec |= ((msg->start_offset & 0xFF0) << 4);
  640. txn.la = la;
  641. txn.mt = mt;
  642. txn.mc = mc;
  643. txn.dt = SLIM_MSG_DEST_LOGICALADDR;
  644. txn.len = len;
  645. txn.rl = len + 6;
  646. txn.wbuf = buf;
  647. txn.rbuf = NULL;
  648. txn.comp = msg->comp;
  649. return ngd_xfer_msg(ctrl, &txn);
  650. }
  651. static int ngd_xferandwait_ack(struct slim_controller *ctrl,
  652. struct slim_msg_txn *txn)
  653. {
  654. struct msm_slim_ctrl *dev = slim_get_ctrldata(ctrl);
  655. int ret = ngd_xfer_msg(ctrl, txn);
  656. if (!ret) {
  657. int timeout;
  658. timeout = wait_for_completion_timeout(txn->comp, HZ);
  659. if (!timeout)
  660. ret = -ETIMEDOUT;
  661. else
  662. ret = txn->ec;
  663. }
  664. if (ret) {
  665. if (ret != -EREMOTEIO || txn->mc != SLIM_USR_MC_CHAN_CTRL)
  666. SLIM_ERR(dev, "master msg:0x%x,tid:%d ret:%d\n",
  667. txn->mc, txn->tid, ret);
  668. mutex_lock(&ctrl->m_ctrl);
  669. ctrl->txnt[txn->tid] = NULL;
  670. mutex_unlock(&ctrl->m_ctrl);
  671. }
  672. return ret;
  673. }
  674. static int ngd_allocbw(struct slim_device *sb, int *subfrmc, int *clkgear)
  675. {
  676. int ret = 0, num_chan = 0;
  677. struct slim_pending_ch *pch;
  678. struct slim_msg_txn txn;
  679. struct slim_controller *ctrl = sb->ctrl;
  680. DECLARE_COMPLETION_ONSTACK(done);
  681. u8 wbuf[SLIM_MSGQ_BUF_LEN];
  682. struct msm_slim_ctrl *dev = slim_get_ctrldata(ctrl);
  683. *clkgear = ctrl->clkgear;
  684. *subfrmc = 0;
  685. txn.mt = SLIM_MSG_MT_DEST_REFERRED_USER;
  686. txn.dt = SLIM_MSG_DEST_LOGICALADDR;
  687. txn.la = SLIM_LA_MGR;
  688. txn.len = 0;
  689. txn.ec = 0;
  690. txn.wbuf = wbuf;
  691. txn.rbuf = NULL;
  692. if (ctrl->sched.msgsl != ctrl->sched.pending_msgsl) {
  693. SLIM_DBG(dev, "slim reserve BW for messaging: req: %d\n",
  694. ctrl->sched.pending_msgsl);
  695. txn.mc = SLIM_USR_MC_REQ_BW;
  696. wbuf[txn.len++] = ((sb->laddr & 0x1f) |
  697. ((u8)(ctrl->sched.pending_msgsl & 0x7) << 5));
  698. wbuf[txn.len++] = (u8)(ctrl->sched.pending_msgsl >> 3);
  699. ret = ngd_get_tid(ctrl, &txn, &wbuf[txn.len++], &done);
  700. if (ret)
  701. return ret;
  702. txn.rl = txn.len + 4;
  703. ret = ngd_xferandwait_ack(ctrl, &txn);
  704. if (ret)
  705. return ret;
  706. txn.mc = SLIM_USR_MC_RECONFIG_NOW;
  707. txn.len = 2;
  708. wbuf[1] = sb->laddr;
  709. txn.rl = txn.len + 4;
  710. ret = ngd_get_tid(ctrl, &txn, &wbuf[0], &done);
  711. if (ret)
  712. return ret;
  713. ret = ngd_xferandwait_ack(ctrl, &txn);
  714. if (ret)
  715. return ret;
  716. txn.len = 0;
  717. }
  718. list_for_each_entry(pch, &sb->mark_define, pending) {
  719. struct slim_ich *slc;
  720. slc = &ctrl->chans[pch->chan];
  721. if (!slc) {
  722. SLIM_WARN(dev, "no channel in define?\n");
  723. return -ENXIO;
  724. }
  725. if (txn.len == 0) {
  726. /* Per protocol, only last 5 bits for client no. */
  727. wbuf[txn.len++] = (u8) (slc->prop.dataf << 5) |
  728. (sb->laddr & 0x1f);
  729. wbuf[txn.len] = slc->seglen;
  730. if (slc->coeff == SLIM_COEFF_3)
  731. wbuf[txn.len] |= 1 << 5;
  732. wbuf[txn.len++] |= slc->prop.auxf << 6;
  733. wbuf[txn.len++] = slc->rootexp << 4 | slc->prop.prot;
  734. wbuf[txn.len++] = slc->prrate;
  735. ret = ngd_get_tid(ctrl, &txn, &wbuf[txn.len++], &done);
  736. if (ret) {
  737. SLIM_WARN(dev, "no tid for channel define?\n");
  738. return -ENXIO;
  739. }
  740. }
  741. num_chan++;
  742. wbuf[txn.len++] = slc->chan;
  743. SLIM_INFO(dev, "slim activate chan:%d, laddr: 0x%x\n",
  744. slc->chan, sb->laddr);
  745. }
  746. if (txn.len) {
  747. txn.mc = SLIM_USR_MC_DEF_ACT_CHAN;
  748. txn.rl = txn.len + 4;
  749. ret = ngd_xferandwait_ack(ctrl, &txn);
  750. if (ret)
  751. return ret;
  752. txn.mc = SLIM_USR_MC_RECONFIG_NOW;
  753. txn.len = 2;
  754. wbuf[1] = sb->laddr;
  755. txn.rl = txn.len + 4;
  756. ret = ngd_get_tid(ctrl, &txn, &wbuf[0], &done);
  757. if (ret)
  758. return ret;
  759. ret = ngd_xferandwait_ack(ctrl, &txn);
  760. if (ret)
  761. return ret;
  762. }
  763. txn.len = 0;
  764. list_for_each_entry(pch, &sb->mark_removal, pending) {
  765. struct slim_ich *slc;
  766. slc = &ctrl->chans[pch->chan];
  767. if (!slc) {
  768. SLIM_WARN(dev, "no channel in removal?\n");
  769. return -ENXIO;
  770. }
  771. if (txn.len == 0) {
  772. /* Per protocol, only last 5 bits for client no. */
  773. wbuf[txn.len++] = (u8) (SLIM_CH_REMOVE << 6) |
  774. (sb->laddr & 0x1f);
  775. ret = ngd_get_tid(ctrl, &txn, &wbuf[txn.len++], &done);
  776. if (ret) {
  777. SLIM_WARN(dev, "no tid for channel define?\n");
  778. return -ENXIO;
  779. }
  780. }
  781. wbuf[txn.len++] = slc->chan;
  782. SLIM_INFO(dev, "slim remove chan:%d, laddr: 0x%x\n",
  783. slc->chan, sb->laddr);
  784. }
  785. if (txn.len) {
  786. txn.mc = SLIM_USR_MC_CHAN_CTRL;
  787. txn.rl = txn.len + 4;
  788. ret = ngd_xferandwait_ack(ctrl, &txn);
  789. /* HW restarting, channel removal should succeed */
  790. if (ret == -EREMOTEIO)
  791. return 0;
  792. else if (ret)
  793. return ret;
  794. txn.mc = SLIM_USR_MC_RECONFIG_NOW;
  795. txn.len = 2;
  796. wbuf[1] = sb->laddr;
  797. txn.rl = txn.len + 4;
  798. ret = ngd_get_tid(ctrl, &txn, &wbuf[0], &done);
  799. if (ret)
  800. return ret;
  801. ret = ngd_xferandwait_ack(ctrl, &txn);
  802. if (ret)
  803. return ret;
  804. txn.len = 0;
  805. }
  806. return 0;
  807. }
  808. static int ngd_set_laddr(struct slim_controller *ctrl, const u8 *ea,
  809. u8 elen, u8 laddr)
  810. {
  811. return 0;
  812. }
  813. static int ngd_get_laddr(struct slim_controller *ctrl, const u8 *ea,
  814. u8 elen, u8 *laddr)
  815. {
  816. int ret;
  817. u8 wbuf[10];
  818. struct slim_msg_txn txn;
  819. DECLARE_COMPLETION_ONSTACK(done);
  820. txn.mt = SLIM_MSG_MT_DEST_REFERRED_USER;
  821. txn.dt = SLIM_MSG_DEST_LOGICALADDR;
  822. txn.la = SLIM_LA_MGR;
  823. txn.ec = 0;
  824. ret = ngd_get_tid(ctrl, &txn, &wbuf[0], &done);
  825. if (ret) {
  826. return ret;
  827. }
  828. memcpy(&wbuf[1], ea, elen);
  829. txn.mc = SLIM_USR_MC_ADDR_QUERY;
  830. txn.rl = 11;
  831. txn.len = 7;
  832. txn.wbuf = wbuf;
  833. txn.rbuf = NULL;
  834. ret = ngd_xferandwait_ack(ctrl, &txn);
  835. if (!ret && txn.la == 0xFF)
  836. ret = -ENXIO;
  837. else if (!ret)
  838. *laddr = txn.la;
  839. return ret;
  840. }
  841. static void ngd_slim_setup_msg_path(struct msm_slim_ctrl *dev)
  842. {
  843. if (dev->state == MSM_CTRL_DOWN) {
  844. msm_slim_sps_init(dev, dev->bam_mem,
  845. NGD_BASE(dev->ctrl.nr,
  846. dev->ver) + NGD_STATUS, true);
  847. } else {
  848. if (dev->use_rx_msgqs == MSM_MSGQ_DISABLED)
  849. goto setup_tx_msg_path;
  850. msm_slim_connect_endp(dev, &dev->rx_msgq,
  851. &dev->rx_msgq_notify);
  852. setup_tx_msg_path:
  853. if (dev->use_tx_msgqs == MSM_MSGQ_DISABLED)
  854. return;
  855. msm_slim_connect_endp(dev, &dev->tx_msgq,
  856. NULL);
  857. }
  858. }
  859. static void ngd_slim_rx(struct msm_slim_ctrl *dev, u8 *buf)
  860. {
  861. u8 mc, mt, len;
  862. int ret;
  863. u32 msgq_en = 1;
  864. len = buf[0] & 0x1F;
  865. mt = (buf[0] >> 5) & 0x7;
  866. mc = buf[1];
  867. if (mc == SLIM_USR_MC_MASTER_CAPABILITY &&
  868. mt == SLIM_MSG_MT_SRC_REFERRED_USER) {
  869. struct slim_msg_txn txn;
  870. int retries = 0;
  871. u8 wbuf[8];
  872. txn.dt = SLIM_MSG_DEST_LOGICALADDR;
  873. txn.ec = 0;
  874. txn.rbuf = NULL;
  875. txn.mc = SLIM_USR_MC_REPORT_SATELLITE;
  876. txn.mt = SLIM_MSG_MT_SRC_REFERRED_USER;
  877. txn.la = SLIM_LA_MGR;
  878. wbuf[0] = SAT_MAGIC_LSB;
  879. wbuf[1] = SAT_MAGIC_MSB;
  880. wbuf[2] = SAT_MSG_VER;
  881. wbuf[3] = SAT_MSG_PROT;
  882. txn.wbuf = wbuf;
  883. txn.len = 4;
  884. SLIM_INFO(dev, "SLIM SAT: Rcvd master capability\n");
  885. if (dev->state >= MSM_CTRL_ASLEEP) {
  886. ngd_slim_setup_msg_path(dev);
  887. if (dev->use_rx_msgqs == MSM_MSGQ_ENABLED)
  888. msgq_en |= NGD_CFG_RX_MSGQ_EN;
  889. if (dev->use_tx_msgqs == MSM_MSGQ_ENABLED)
  890. msgq_en |= NGD_CFG_TX_MSGQ_EN;
  891. writel_relaxed(msgq_en, dev->base +
  892. NGD_BASE(dev->ctrl.nr, dev->ver));
  893. /* make sure NGD MSG-Q config goes through */
  894. mb();
  895. }
  896. capability_retry:
  897. txn.rl = 8;
  898. ret = ngd_xfer_msg(&dev->ctrl, &txn);
  899. if (!ret) {
  900. enum msm_ctrl_state prev_state = dev->state;
  901. SLIM_INFO(dev,
  902. "SLIM SAT: capability exchange successful\n");
  903. if (prev_state >= MSM_CTRL_ASLEEP)
  904. complete(&dev->reconf);
  905. else
  906. SLIM_ERR(dev,
  907. "SLIM: unexpected capability, state:%d\n",
  908. prev_state);
  909. /* ADSP SSR, send device_up notifications */
  910. if (prev_state == MSM_CTRL_DOWN)
  911. complete(&dev->qmi.slave_notify);
  912. } else if (ret == -EIO) {
  913. SLIM_WARN(dev, "capability message NACKed, retrying\n");
  914. if (retries < INIT_MX_RETRIES) {
  915. msleep(DEF_RETRY_MS);
  916. retries++;
  917. goto capability_retry;
  918. }
  919. }
  920. }
  921. if (mc == SLIM_MSG_MC_REPLY_INFORMATION ||
  922. mc == SLIM_MSG_MC_REPLY_VALUE) {
  923. u8 tid = buf[3];
  924. dev_dbg(dev->dev, "tid:%d, len:%d\n", tid, len);
  925. slim_msg_response(&dev->ctrl, &buf[4], tid,
  926. len - 4);
  927. pm_runtime_mark_last_busy(dev->dev);
  928. }
  929. if (mc == SLIM_USR_MC_ADDR_REPLY &&
  930. mt == SLIM_MSG_MT_SRC_REFERRED_USER) {
  931. struct slim_msg_txn *txn;
  932. u8 failed_ea[6] = {0, 0, 0, 0, 0, 0};
  933. mutex_lock(&dev->ctrl.m_ctrl);
  934. txn = dev->ctrl.txnt[buf[3]];
  935. if (!txn) {
  936. SLIM_WARN(dev,
  937. "LADDR response after timeout, tid:0x%x\n",
  938. buf[3]);
  939. mutex_unlock(&dev->ctrl.m_ctrl);
  940. return;
  941. }
  942. if (memcmp(&buf[4], failed_ea, 6))
  943. txn->la = buf[10];
  944. dev->ctrl.txnt[buf[3]] = NULL;
  945. mutex_unlock(&dev->ctrl.m_ctrl);
  946. complete(txn->comp);
  947. }
  948. if (mc == SLIM_USR_MC_GENERIC_ACK &&
  949. mt == SLIM_MSG_MT_SRC_REFERRED_USER) {
  950. struct slim_msg_txn *txn;
  951. mutex_lock(&dev->ctrl.m_ctrl);
  952. txn = dev->ctrl.txnt[buf[3]];
  953. if (!txn) {
  954. SLIM_WARN(dev, "ACK received after timeout, tid:0x%x\n",
  955. buf[3]);
  956. mutex_unlock(&dev->ctrl.m_ctrl);
  957. return;
  958. }
  959. dev_dbg(dev->dev, "got response:tid:%d, response:0x%x",
  960. (int)buf[3], buf[4]);
  961. if (!(buf[4] & MSM_SAT_SUCCSS)) {
  962. SLIM_WARN(dev, "TID:%d, NACK code:0x%x\n", (int)buf[3],
  963. buf[4]);
  964. txn->ec = -EIO;
  965. }
  966. dev->ctrl.txnt[buf[3]] = NULL;
  967. mutex_unlock(&dev->ctrl.m_ctrl);
  968. complete(txn->comp);
  969. }
  970. }
  971. static int ngd_slim_power_up(struct msm_slim_ctrl *dev, bool mdm_restart)
  972. {
  973. void __iomem *ngd;
  974. int timeout, ret = 0;
  975. enum msm_ctrl_state cur_state = dev->state;
  976. u32 laddr;
  977. u32 ngd_int = (NGD_INT_TX_NACKED_2 |
  978. NGD_INT_MSG_BUF_CONTE | NGD_INT_MSG_TX_INVAL |
  979. NGD_INT_IE_VE_CHG | NGD_INT_DEV_ERR |
  980. NGD_INT_TX_MSG_SENT | NGD_INT_RX_MSG_RCVD);
  981. if (!mdm_restart && cur_state == MSM_CTRL_DOWN) {
  982. int timeout = wait_for_completion_timeout(&dev->qmi.qmi_comp,
  983. HZ);
  984. if (!timeout) {
  985. SLIM_ERR(dev, "slimbus QMI init timed out\n");
  986. return -EREMOTEIO;
  987. }
  988. }
  989. /* No need to vote if contorller is not in low power mode */
  990. if (!mdm_restart &&
  991. (cur_state == MSM_CTRL_DOWN || cur_state == MSM_CTRL_ASLEEP)) {
  992. ret = msm_slim_qmi_power_request(dev, true);
  993. if (ret) {
  994. SLIM_ERR(dev, "SLIM QMI power request failed:%d\n",
  995. ret);
  996. return ret;
  997. }
  998. }
  999. if (!dev->ver) {
  1000. dev->ver = readl_relaxed(dev->base);
  1001. /* Version info in 16 MSbits */
  1002. dev->ver >>= 16;
  1003. }
  1004. ngd = dev->base + NGD_BASE(dev->ctrl.nr, dev->ver);
  1005. laddr = readl_relaxed(ngd + NGD_STATUS);
  1006. if (laddr & NGD_LADDR) {
  1007. /*
  1008. * external MDM restart case where ADSP itself was active framer
  1009. * For example, modem restarted when playback was active
  1010. */
  1011. if (cur_state == MSM_CTRL_AWAKE) {
  1012. SLIM_INFO(dev, "Subsys restart: ADSP active framer\n");
  1013. return 0;
  1014. }
  1015. /*
  1016. * ADSP power collapse case, where HW wasn't reset.
  1017. * Reconnect BAM pipes if disconnected
  1018. */
  1019. ngd_slim_setup_msg_path(dev);
  1020. return 0;
  1021. }
  1022. if (mdm_restart) {
  1023. /*
  1024. * external MDM SSR when MDM is active framer
  1025. * ADSP will reset slimbus HW. disconnect BAM pipes so that
  1026. * they can be connected after capability message is received.
  1027. * Set device state to ASLEEP to be synchronous with the HW
  1028. */
  1029. /* make current state as DOWN */
  1030. cur_state = MSM_CTRL_DOWN;
  1031. SLIM_INFO(dev,
  1032. "SLIM MDM restart: MDM active framer: reinit HW\n");
  1033. /* disconnect BAM pipes */
  1034. if (dev->use_rx_msgqs == MSM_MSGQ_ENABLED)
  1035. dev->use_rx_msgqs = MSM_MSGQ_DOWN;
  1036. if (dev->use_tx_msgqs == MSM_MSGQ_ENABLED)
  1037. dev->use_tx_msgqs = MSM_MSGQ_DOWN;
  1038. dev->state = MSM_CTRL_DOWN;
  1039. }
  1040. /* SSR scenario, need to disconnect pipe before connecting */
  1041. if (dev->use_rx_msgqs == MSM_MSGQ_DOWN) {
  1042. struct msm_slim_endp *endpoint = &dev->rx_msgq;
  1043. sps_disconnect(endpoint->sps);
  1044. sps_free_endpoint(endpoint->sps);
  1045. dev->use_rx_msgqs = MSM_MSGQ_RESET;
  1046. }
  1047. if (dev->use_tx_msgqs == MSM_MSGQ_DOWN) {
  1048. struct msm_slim_endp *endpoint = &dev->tx_msgq;
  1049. sps_disconnect(endpoint->sps);
  1050. sps_free_endpoint(endpoint->sps);
  1051. dev->use_tx_msgqs = MSM_MSGQ_RESET;
  1052. }
  1053. /*
  1054. * ADSP power collapse case (OR SSR), where HW was reset
  1055. * BAM programming will happen when capability message is received
  1056. */
  1057. writel_relaxed(ngd_int, dev->base + NGD_INT_EN +
  1058. NGD_BASE(dev->ctrl.nr, dev->ver));
  1059. /*
  1060. * Enable NGD. Configure NGD in register acc. mode until master
  1061. * announcement is received
  1062. */
  1063. writel_relaxed(1, dev->base + NGD_BASE(dev->ctrl.nr, dev->ver));
  1064. /* make sure NGD enabling goes through */
  1065. mb();
  1066. timeout = wait_for_completion_timeout(&dev->reconf, HZ);
  1067. if (!timeout) {
  1068. SLIM_ERR(dev, "Failed to receive master capability\n");
  1069. return -ETIMEDOUT;
  1070. }
  1071. /* mutliple transactions waiting on slimbus to power up? */
  1072. if (cur_state == MSM_CTRL_DOWN)
  1073. complete_all(&dev->ctrl_up);
  1074. /* Resetting the log level */
  1075. SLIM_RST_LOGLVL(dev);
  1076. return 0;
  1077. }
  1078. static int ngd_slim_enable(struct msm_slim_ctrl *dev, bool enable)
  1079. {
  1080. int ret = 0;
  1081. if (enable) {
  1082. ret = msm_slim_qmi_init(dev, false);
  1083. /* controller state should be in sync with framework state */
  1084. if (!ret) {
  1085. complete(&dev->qmi.qmi_comp);
  1086. if (!pm_runtime_enabled(dev->dev) ||
  1087. !pm_runtime_suspended(dev->dev))
  1088. ngd_slim_runtime_resume(dev->dev);
  1089. else
  1090. pm_runtime_resume(dev->dev);
  1091. pm_runtime_mark_last_busy(dev->dev);
  1092. pm_runtime_put(dev->dev);
  1093. } else
  1094. SLIM_ERR(dev, "qmi init fail, ret:%d, state:%d\n",
  1095. ret, dev->state);
  1096. } else {
  1097. msm_slim_qmi_exit(dev);
  1098. }
  1099. return ret;
  1100. }
  1101. static int ngd_slim_power_down(struct msm_slim_ctrl *dev)
  1102. {
  1103. int i;
  1104. struct slim_controller *ctrl = &dev->ctrl;
  1105. mutex_lock(&ctrl->m_ctrl);
  1106. /* Pending response for a message */
  1107. for (i = 0; i < ctrl->last_tid; i++) {
  1108. if (ctrl->txnt[i]) {
  1109. SLIM_INFO(dev, "NGD down:txn-rsp for %d pending", i);
  1110. mutex_unlock(&ctrl->m_ctrl);
  1111. return -EBUSY;
  1112. }
  1113. }
  1114. mutex_unlock(&ctrl->m_ctrl);
  1115. msm_slim_disconnect_endp(dev, &dev->rx_msgq,
  1116. &dev->use_rx_msgqs);
  1117. msm_slim_disconnect_endp(dev, &dev->tx_msgq,
  1118. &dev->use_tx_msgqs);
  1119. return msm_slim_qmi_power_request(dev, false);
  1120. }
  1121. static int ngd_slim_rx_msgq_thread(void *data)
  1122. {
  1123. struct msm_slim_ctrl *dev = (struct msm_slim_ctrl *)data;
  1124. struct completion *notify = &dev->rx_msgq_notify;
  1125. int ret = 0, index = 0;
  1126. u32 mc = 0;
  1127. u32 mt = 0;
  1128. u32 buffer[10];
  1129. u8 msg_len = 0;
  1130. while (!kthread_should_stop()) {
  1131. set_current_state(TASK_INTERRUPTIBLE);
  1132. wait_for_completion(notify);
  1133. /* 1 irq notification per message */
  1134. if (dev->use_rx_msgqs != MSM_MSGQ_ENABLED) {
  1135. msm_slim_rx_dequeue(dev, (u8 *)buffer);
  1136. ngd_slim_rx(dev, (u8 *)buffer);
  1137. continue;
  1138. }
  1139. ret = msm_slim_rx_msgq_get(dev, buffer, index);
  1140. if (ret) {
  1141. SLIM_ERR(dev, "rx_msgq_get() failed 0x%x\n", ret);
  1142. continue;
  1143. }
  1144. /* Wait for complete message */
  1145. if (index++ == 0) {
  1146. msg_len = *buffer & 0x1F;
  1147. mt = (buffer[0] >> 5) & 0x7;
  1148. mc = (buffer[0] >> 8) & 0xff;
  1149. dev_dbg(dev->dev, "MC: %x, MT: %x\n", mc, mt);
  1150. }
  1151. if ((index * 4) >= msg_len) {
  1152. index = 0;
  1153. ngd_slim_rx(dev, (u8 *)buffer);
  1154. } else
  1155. continue;
  1156. }
  1157. return 0;
  1158. }
  1159. static int ngd_notify_slaves(void *data)
  1160. {
  1161. struct msm_slim_ctrl *dev = (struct msm_slim_ctrl *)data;
  1162. struct slim_controller *ctrl = &dev->ctrl;
  1163. struct slim_device *sbdev;
  1164. struct list_head *pos, *next;
  1165. int ret, i = 0;
  1166. ret = qmi_svc_event_notifier_register(SLIMBUS_QMI_SVC_ID,
  1167. SLIMBUS_QMI_SVC_V1,
  1168. SLIMBUS_QMI_INS_ID, &dev->qmi.nb);
  1169. if (ret) {
  1170. pr_err("Slimbus QMI service registration failed:%d", ret);
  1171. return ret;
  1172. }
  1173. while (!kthread_should_stop()) {
  1174. set_current_state(TASK_INTERRUPTIBLE);
  1175. wait_for_completion(&dev->qmi.slave_notify);
  1176. /* Probe devices for first notification */
  1177. if (!i) {
  1178. i++;
  1179. dev->err = 0;
  1180. if (dev->dev->of_node)
  1181. of_register_slim_devices(&dev->ctrl);
  1182. /*
  1183. * Add devices registered with board-info now that
  1184. * controller is up
  1185. */
  1186. slim_ctrl_add_boarddevs(&dev->ctrl);
  1187. } else {
  1188. slim_framer_booted(ctrl);
  1189. }
  1190. mutex_lock(&ctrl->m_ctrl);
  1191. list_for_each_safe(pos, next, &ctrl->devs) {
  1192. int j;
  1193. sbdev = list_entry(pos, struct slim_device, dev_list);
  1194. mutex_unlock(&ctrl->m_ctrl);
  1195. for (j = 0; j < LADDR_RETRY; j++) {
  1196. ret = slim_get_logical_addr(sbdev,
  1197. sbdev->e_addr,
  1198. 6, &sbdev->laddr);
  1199. #if defined(CONFIG_MACH_KLTE_TMO)
  1200. if (system_rev == 0xd) {
  1201. pr_info("%s : system rev = %d\n", __func__, system_rev);
  1202. if ((ret == -ENXIO) &&
  1203. ((sbdev->e_addr[4] == 0xbe) && (sbdev->e_addr[2] == 0x83))) {
  1204. pr_info("%s : es704 fail to assign retry to assign the es705\n", __func__);
  1205. sbdev->e_addr[2] = 0x03;
  1206. ret = slim_get_logical_addr(sbdev, sbdev->e_addr, 6, &sbdev->laddr);
  1207. }
  1208. }
  1209. #endif
  1210. if (!ret)
  1211. break;
  1212. else /* time for ADSP to assign LA */
  1213. msleep(20);
  1214. }
  1215. mutex_lock(&ctrl->m_ctrl);
  1216. }
  1217. mutex_unlock(&ctrl->m_ctrl);
  1218. }
  1219. return 0;
  1220. }
  1221. static void ngd_adsp_down(struct work_struct *work)
  1222. {
  1223. struct msm_slim_qmi *qmi =
  1224. container_of(work, struct msm_slim_qmi, ssr_down);
  1225. struct msm_slim_ctrl *dev =
  1226. container_of(qmi, struct msm_slim_ctrl, qmi);
  1227. struct slim_controller *ctrl = &dev->ctrl;
  1228. struct slim_device *sbdev;
  1229. mutex_lock(&dev->ssr_lock);
  1230. ngd_slim_enable(dev, false);
  1231. /* device up should be called again after SSR */
  1232. list_for_each_entry(sbdev, &ctrl->devs, dev_list)
  1233. slim_report_absent(sbdev);
  1234. SLIM_INFO(dev, "SLIM ADSP SSR (DOWN) done\n");
  1235. mutex_unlock(&dev->ssr_lock);
  1236. }
  1237. static void ngd_adsp_up(struct work_struct *work)
  1238. {
  1239. struct msm_slim_qmi *qmi =
  1240. container_of(work, struct msm_slim_qmi, ssr_up);
  1241. struct msm_slim_ctrl *dev =
  1242. container_of(qmi, struct msm_slim_ctrl, qmi);
  1243. mutex_lock(&dev->ssr_lock);
  1244. ngd_slim_enable(dev, true);
  1245. mutex_unlock(&dev->ssr_lock);
  1246. }
  1247. static ssize_t show_mask(struct device *device, struct device_attribute *attr,
  1248. char *buf)
  1249. {
  1250. struct platform_device *pdev = to_platform_device(device);
  1251. struct msm_slim_ctrl *dev = platform_get_drvdata(pdev);
  1252. return snprintf(buf, sizeof(int), "%u\n", dev->ipc_log_mask);
  1253. }
  1254. static ssize_t set_mask(struct device *device, struct device_attribute *attr,
  1255. const char *buf, size_t count)
  1256. {
  1257. struct platform_device *pdev = to_platform_device(device);
  1258. struct msm_slim_ctrl *dev = platform_get_drvdata(pdev);
  1259. dev->ipc_log_mask = buf[0] - '0';
  1260. if (dev->ipc_log_mask > DBG_LEV)
  1261. dev->ipc_log_mask = DBG_LEV;
  1262. return count;
  1263. }
  1264. static DEVICE_ATTR(debug_mask, S_IRUGO | S_IWUSR, show_mask, set_mask);
  1265. static int __devinit ngd_slim_probe(struct platform_device *pdev)
  1266. {
  1267. struct msm_slim_ctrl *dev;
  1268. int ret;
  1269. struct resource *bam_mem;
  1270. struct resource *slim_mem;
  1271. struct resource *irq, *bam_irq;
  1272. bool rxreg_access = false;
  1273. bool slim_mdm = false;
  1274. const char *ext_modem_id = NULL;
  1275. slim_mem = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  1276. "slimbus_physical");
  1277. if (!slim_mem) {
  1278. dev_err(&pdev->dev, "no slimbus physical memory resource\n");
  1279. return -ENODEV;
  1280. }
  1281. bam_mem = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  1282. "slimbus_bam_physical");
  1283. if (!bam_mem) {
  1284. dev_err(&pdev->dev, "no slimbus BAM memory resource\n");
  1285. return -ENODEV;
  1286. }
  1287. irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
  1288. "slimbus_irq");
  1289. if (!irq) {
  1290. dev_err(&pdev->dev, "no slimbus IRQ resource\n");
  1291. return -ENODEV;
  1292. }
  1293. bam_irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
  1294. "slimbus_bam_irq");
  1295. if (!bam_irq) {
  1296. dev_err(&pdev->dev, "no slimbus BAM IRQ resource\n");
  1297. return -ENODEV;
  1298. }
  1299. dev = kzalloc(sizeof(struct msm_slim_ctrl), GFP_KERNEL);
  1300. if (IS_ERR_OR_NULL(dev)) {
  1301. dev_err(&pdev->dev, "no memory for MSM slimbus controller\n");
  1302. return PTR_ERR(dev);
  1303. }
  1304. dev->wr_comp = kzalloc(sizeof(struct completion *) * MSM_TX_BUFS,
  1305. GFP_KERNEL);
  1306. if (!dev->wr_comp)
  1307. return -ENOMEM;
  1308. dev->dev = &pdev->dev;
  1309. platform_set_drvdata(pdev, dev);
  1310. slim_set_ctrldata(&dev->ctrl, dev);
  1311. /* Create IPC log context */
  1312. dev->ipc_slimbus_log = ipc_log_context_create(IPC_SLIMBUS_LOG_PAGES,
  1313. dev_name(dev->dev), 0);
  1314. if (!dev->ipc_slimbus_log)
  1315. dev_err(&pdev->dev, "error creating ipc_logging context\n");
  1316. else {
  1317. /* Initialize the log mask */
  1318. dev->ipc_log_mask = INFO_LEV;
  1319. dev->default_ipc_log_mask = INFO_LEV;
  1320. SLIM_INFO(dev, "start logging for slim dev %s\n",
  1321. dev_name(dev->dev));
  1322. }
  1323. ret = sysfs_create_file(&dev->dev->kobj, &dev_attr_debug_mask.attr);
  1324. if (ret) {
  1325. dev_err(&pdev->dev, "Failed to create dev. attr\n");
  1326. dev->sysfs_created = false;
  1327. } else
  1328. dev->sysfs_created = true;
  1329. dev->base = ioremap(slim_mem->start, resource_size(slim_mem));
  1330. if (!dev->base) {
  1331. dev_err(&pdev->dev, "IOremap failed\n");
  1332. ret = -ENOMEM;
  1333. goto err_ioremap_failed;
  1334. }
  1335. dev->bam.base = ioremap(bam_mem->start, resource_size(bam_mem));
  1336. if (!dev->bam.base) {
  1337. dev_err(&pdev->dev, "BAM IOremap failed\n");
  1338. ret = -ENOMEM;
  1339. goto err_ioremap_bam_failed;
  1340. }
  1341. if (pdev->dev.of_node) {
  1342. ret = of_property_read_u32(pdev->dev.of_node, "cell-index",
  1343. &dev->ctrl.nr);
  1344. if (ret) {
  1345. dev_err(&pdev->dev, "Cell index not specified:%d", ret);
  1346. goto err_ctrl_failed;
  1347. }
  1348. rxreg_access = of_property_read_bool(pdev->dev.of_node,
  1349. "qcom,rxreg-access");
  1350. of_property_read_u32(pdev->dev.of_node, "qcom,apps-ch-pipes",
  1351. &dev->pdata.apps_pipes);
  1352. of_property_read_u32(pdev->dev.of_node, "qcom,ea-pc",
  1353. &dev->pdata.eapc);
  1354. ret = of_property_read_string(pdev->dev.of_node,
  1355. "qcom,slim-mdm", &ext_modem_id);
  1356. if (!ret)
  1357. slim_mdm = true;
  1358. } else {
  1359. dev->ctrl.nr = pdev->id;
  1360. }
  1361. /*
  1362. * Keep PGD's logical address as manager's. Query it when first data
  1363. * channel request comes in
  1364. */
  1365. dev->pgdla = SLIM_LA_MGR;
  1366. dev->ctrl.nchans = MSM_SLIM_NCHANS;
  1367. dev->ctrl.nports = MSM_SLIM_NPORTS;
  1368. dev->framer.rootfreq = SLIM_ROOT_FREQ >> 3;
  1369. dev->framer.superfreq =
  1370. dev->framer.rootfreq / SLIM_CL_PER_SUPERFRAME_DIV8;
  1371. dev->ctrl.a_framer = &dev->framer;
  1372. dev->ctrl.clkgear = SLIM_MAX_CLK_GEAR;
  1373. dev->ctrl.set_laddr = ngd_set_laddr;
  1374. dev->ctrl.get_laddr = ngd_get_laddr;
  1375. dev->ctrl.allocbw = ngd_allocbw;
  1376. dev->ctrl.xfer_msg = ngd_xfer_msg;
  1377. dev->ctrl.xfer_user_msg = ngd_user_msg;
  1378. dev->ctrl.wakeup = NULL;
  1379. dev->ctrl.alloc_port = msm_alloc_port;
  1380. dev->ctrl.dealloc_port = msm_dealloc_port;
  1381. dev->ctrl.port_xfer = msm_slim_port_xfer;
  1382. dev->ctrl.port_xfer_status = msm_slim_port_xfer_status;
  1383. dev->bam_mem = bam_mem;
  1384. init_completion(&dev->reconf);
  1385. init_completion(&dev->ctrl_up);
  1386. mutex_init(&dev->tx_lock);
  1387. mutex_init(&dev->ssr_lock);
  1388. mutex_init(&dev->tx_buf_lock);
  1389. spin_lock_init(&dev->rx_lock);
  1390. dev->ee = 1;
  1391. dev->irq = irq->start;
  1392. dev->bam.irq = bam_irq->start;
  1393. if (rxreg_access)
  1394. dev->use_rx_msgqs = MSM_MSGQ_DISABLED;
  1395. else
  1396. dev->use_rx_msgqs = MSM_MSGQ_RESET;
  1397. /* Enable TX message queues by default as recommended by HW */
  1398. dev->use_tx_msgqs = MSM_MSGQ_RESET;
  1399. init_completion(&dev->rx_msgq_notify);
  1400. init_completion(&dev->qmi.slave_notify);
  1401. /* Register with framework */
  1402. ret = slim_add_numbered_controller(&dev->ctrl);
  1403. if (ret) {
  1404. dev_err(dev->dev, "error adding controller\n");
  1405. goto err_ctrl_failed;
  1406. }
  1407. dev->ctrl.dev.parent = &pdev->dev;
  1408. dev->ctrl.dev.of_node = pdev->dev.of_node;
  1409. dev->state = MSM_CTRL_DOWN;
  1410. ret = request_threaded_irq(dev->irq, NULL,
  1411. ngd_slim_interrupt,
  1412. IRQF_TRIGGER_HIGH | IRQF_ONESHOT, "ngd_slim_irq", dev);
  1413. if (ret) {
  1414. dev_err(&pdev->dev, "request IRQ failed\n");
  1415. goto err_request_irq_failed;
  1416. }
  1417. init_completion(&dev->qmi.qmi_comp);
  1418. dev->err = -EPROBE_DEFER;
  1419. pm_runtime_use_autosuspend(dev->dev);
  1420. pm_runtime_set_autosuspend_delay(dev->dev, MSM_SLIM_AUTOSUSPEND);
  1421. pm_runtime_set_suspended(dev->dev);
  1422. pm_runtime_enable(dev->dev);
  1423. dev->dsp.nb.notifier_call = dsp_ssr_notify_cb;
  1424. dev->dsp.ssr = subsys_notif_register_notifier("adsp",
  1425. &dev->dsp.nb);
  1426. if (IS_ERR_OR_NULL(dev->dsp.ssr))
  1427. dev_err(dev->dev,
  1428. "subsys_notif_register_notifier failed %p",
  1429. dev->dsp.ssr);
  1430. if (slim_mdm) {
  1431. dev->ext_mdm.nb.notifier_call = mdm_ssr_notify_cb;
  1432. dev->ext_mdm.ssr = subsys_notif_register_notifier(ext_modem_id,
  1433. &dev->ext_mdm.nb);
  1434. if (IS_ERR_OR_NULL(dev->ext_mdm.ssr))
  1435. dev_err(dev->dev,
  1436. "subsys_notif_register_notifier failed %p",
  1437. dev->ext_mdm.ssr);
  1438. }
  1439. INIT_WORK(&dev->qmi.ssr_down, ngd_adsp_down);
  1440. INIT_WORK(&dev->qmi.ssr_up, ngd_adsp_up);
  1441. dev->qmi.nb.notifier_call = ngd_qmi_available;
  1442. pm_runtime_get_noresume(dev->dev);
  1443. /* Fire up the Rx message queue thread */
  1444. dev->rx_msgq_thread = kthread_run(ngd_slim_rx_msgq_thread, dev,
  1445. "ngd_rx_thread%d", dev->ctrl.nr);
  1446. if (IS_ERR(dev->rx_msgq_thread)) {
  1447. ret = PTR_ERR(dev->rx_msgq_thread);
  1448. dev_err(dev->dev, "Failed to start Rx thread:%d\n", ret);
  1449. goto err_rx_thread_create_failed;
  1450. }
  1451. /* Start thread to probe, and notify slaves */
  1452. dev->qmi.slave_thread = kthread_run(ngd_notify_slaves, dev,
  1453. "ngd_notify_sl%d", dev->ctrl.nr);
  1454. if (IS_ERR(dev->qmi.slave_thread)) {
  1455. ret = PTR_ERR(dev->qmi.slave_thread);
  1456. dev_err(dev->dev, "Failed to start notifier thread:%d\n", ret);
  1457. goto err_notify_thread_create_failed;
  1458. }
  1459. SLIM_INFO(dev, "NGD SB controller is up!\n");
  1460. return 0;
  1461. err_notify_thread_create_failed:
  1462. kthread_stop(dev->rx_msgq_thread);
  1463. err_rx_thread_create_failed:
  1464. free_irq(dev->irq, dev);
  1465. err_request_irq_failed:
  1466. slim_del_controller(&dev->ctrl);
  1467. err_ctrl_failed:
  1468. iounmap(dev->bam.base);
  1469. err_ioremap_bam_failed:
  1470. iounmap(dev->base);
  1471. err_ioremap_failed:
  1472. if (dev->sysfs_created)
  1473. sysfs_remove_file(&dev->dev->kobj,
  1474. &dev_attr_debug_mask.attr);
  1475. kfree(dev->wr_comp);
  1476. kfree(dev);
  1477. return ret;
  1478. }
  1479. static int __devexit ngd_slim_remove(struct platform_device *pdev)
  1480. {
  1481. struct msm_slim_ctrl *dev = platform_get_drvdata(pdev);
  1482. ngd_slim_enable(dev, false);
  1483. if (dev->sysfs_created)
  1484. sysfs_remove_file(&dev->dev->kobj,
  1485. &dev_attr_debug_mask.attr);
  1486. qmi_svc_event_notifier_unregister(SLIMBUS_QMI_SVC_ID,
  1487. SLIMBUS_QMI_SVC_V1,
  1488. SLIMBUS_QMI_INS_ID, &dev->qmi.nb);
  1489. pm_runtime_disable(&pdev->dev);
  1490. if (!IS_ERR_OR_NULL(dev->dsp.ssr))
  1491. subsys_notif_unregister_notifier(dev->dsp.ssr,
  1492. &dev->dsp.nb);
  1493. if (!IS_ERR_OR_NULL(dev->ext_mdm.ssr))
  1494. subsys_notif_unregister_notifier(dev->ext_mdm.ssr,
  1495. &dev->ext_mdm.nb);
  1496. free_irq(dev->irq, dev);
  1497. slim_del_controller(&dev->ctrl);
  1498. kthread_stop(dev->rx_msgq_thread);
  1499. iounmap(dev->bam.base);
  1500. iounmap(dev->base);
  1501. kfree(dev->wr_comp);
  1502. kfree(dev);
  1503. return 0;
  1504. }
  1505. #if defined(PREVENT_SLIMBUS_SLEEP_IN_FW_DL)
  1506. #ifdef CONFIG_PM_RUNTIME
  1507. static int ngd_slim_runtime_idle(struct device *device)
  1508. {
  1509. struct platform_device *pdev = to_platform_device(device);
  1510. struct msm_slim_ctrl *dev = platform_get_drvdata(pdev);
  1511. if (dev->state == MSM_CTRL_AWAKE && es325_slim_write_flag == 0)
  1512. dev->state = MSM_CTRL_IDLE;
  1513. dev_dbg(device, "pm_runtime: idle...\n");
  1514. if ( dev->state == MSM_CTRL_IDLE && es325_slim_write_flag == 0){
  1515. pm_request_autosuspend(device);
  1516. }
  1517. return -EAGAIN;
  1518. }
  1519. #endif
  1520. #else
  1521. #ifdef CONFIG_PM_RUNTIME
  1522. static int ngd_slim_runtime_idle(struct device *device)
  1523. {
  1524. struct platform_device *pdev = to_platform_device(device);
  1525. struct msm_slim_ctrl *dev = platform_get_drvdata(pdev);
  1526. mutex_lock(&dev->tx_lock);
  1527. if (dev->state == MSM_CTRL_AWAKE)
  1528. dev->state = MSM_CTRL_IDLE;
  1529. mutex_unlock(&dev->tx_lock);
  1530. dev_dbg(device, "pm_runtime: idle...\n");
  1531. pm_request_autosuspend(device);
  1532. return -EAGAIN;
  1533. }
  1534. #endif
  1535. #endif
  1536. /*
  1537. * If PM_RUNTIME is not defined, these 2 functions become helper
  1538. * functions to be called from system suspend/resume. So they are not
  1539. * inside ifdef CONFIG_PM_RUNTIME
  1540. */
  1541. static int ngd_slim_runtime_resume(struct device *device)
  1542. {
  1543. struct platform_device *pdev = to_platform_device(device);
  1544. struct msm_slim_ctrl *dev = platform_get_drvdata(pdev);
  1545. int ret = 0;
  1546. mutex_lock(&dev->tx_lock);
  1547. if (dev->state >= MSM_CTRL_ASLEEP)
  1548. ret = ngd_slim_power_up(dev, false);
  1549. if (ret) {
  1550. /* Did SSR cause this power up failure */
  1551. if (dev->state != MSM_CTRL_DOWN)
  1552. dev->state = MSM_CTRL_ASLEEP;
  1553. else
  1554. SLIM_WARN(dev, "HW wakeup attempt during SSR\n");
  1555. } else {
  1556. dev->state = MSM_CTRL_AWAKE;
  1557. }
  1558. mutex_unlock(&dev->tx_lock);
  1559. SLIM_INFO(dev, "Slim runtime resume: ret %d\n", ret);
  1560. return ret;
  1561. }
  1562. #ifdef CONFIG_PM_SLEEP
  1563. static int ngd_slim_runtime_suspend(struct device *device)
  1564. {
  1565. struct platform_device *pdev = to_platform_device(device);
  1566. struct msm_slim_ctrl *dev = platform_get_drvdata(pdev);
  1567. int ret = 0;
  1568. mutex_lock(&dev->tx_lock);
  1569. ret = ngd_slim_power_down(dev);
  1570. if (ret) {
  1571. if (ret != -EBUSY)
  1572. SLIM_INFO(dev, "slim resource not idle:%d\n", ret);
  1573. dev->state = MSM_CTRL_AWAKE;
  1574. } else {
  1575. dev->state = MSM_CTRL_ASLEEP;
  1576. }
  1577. mutex_unlock(&dev->tx_lock);
  1578. SLIM_INFO(dev, "Slim runtime suspend: ret %d\n", ret);
  1579. return ret;
  1580. }
  1581. static int ngd_slim_suspend(struct device *dev)
  1582. {
  1583. int ret = -EBUSY;
  1584. struct platform_device *pdev = to_platform_device(dev);
  1585. struct msm_slim_ctrl *cdev = platform_get_drvdata(pdev);
  1586. if (!pm_runtime_enabled(dev) ||
  1587. (!pm_runtime_suspended(dev) &&
  1588. cdev->state == MSM_CTRL_IDLE)) {
  1589. ret = ngd_slim_runtime_suspend(dev);
  1590. /*
  1591. * If runtime-PM still thinks it's active, then make sure its
  1592. * status is in sync with HW status.
  1593. * Since this suspend calls QMI api, it results in holding a
  1594. * wakelock. That results in failure of first suspend.
  1595. * Subsequent suspend should not call low-power transition
  1596. * again since the HW is already in suspended state.
  1597. */
  1598. if (!ret) {
  1599. pm_runtime_disable(dev);
  1600. pm_runtime_set_suspended(dev);
  1601. pm_runtime_enable(dev);
  1602. }
  1603. }
  1604. if (ret == -EBUSY) {
  1605. /*
  1606. * There is a possibility that some audio stream is active
  1607. * during suspend. We dont want to return suspend failure in
  1608. * that case so that display and relevant components can still
  1609. * go to suspend.
  1610. * If there is some other error, then it should be passed-on
  1611. * to system level suspend
  1612. */
  1613. ret = 0;
  1614. }
  1615. SLIM_INFO(cdev, "system suspend\n");
  1616. return ret;
  1617. }
  1618. static int ngd_slim_resume(struct device *dev)
  1619. {
  1620. struct platform_device *pdev = to_platform_device(dev);
  1621. struct msm_slim_ctrl *cdev = platform_get_drvdata(pdev);
  1622. /*
  1623. * Rely on runtime-PM to call resume in case it is enabled.
  1624. * Even if it's not enabled, rely on 1st client transaction to do
  1625. * clock/power on
  1626. */
  1627. SLIM_INFO(cdev, "system resume\n");
  1628. return 0;
  1629. }
  1630. #endif /* CONFIG_PM_SLEEP */
  1631. static const struct dev_pm_ops ngd_slim_dev_pm_ops = {
  1632. SET_SYSTEM_SLEEP_PM_OPS(
  1633. ngd_slim_suspend,
  1634. ngd_slim_resume
  1635. )
  1636. SET_RUNTIME_PM_OPS(
  1637. ngd_slim_runtime_suspend,
  1638. ngd_slim_runtime_resume,
  1639. ngd_slim_runtime_idle
  1640. )
  1641. };
  1642. static struct of_device_id ngd_slim_dt_match[] = {
  1643. {
  1644. .compatible = "qcom,slim-ngd",
  1645. },
  1646. {}
  1647. };
  1648. static struct platform_driver ngd_slim_driver = {
  1649. .probe = ngd_slim_probe,
  1650. .remove = ngd_slim_remove,
  1651. .driver = {
  1652. .name = NGD_SLIM_NAME,
  1653. .owner = THIS_MODULE,
  1654. .pm = &ngd_slim_dev_pm_ops,
  1655. .of_match_table = ngd_slim_dt_match,
  1656. },
  1657. };
  1658. static int ngd_slim_init(void)
  1659. {
  1660. return platform_driver_register(&ngd_slim_driver);
  1661. }
  1662. late_initcall(ngd_slim_init);
  1663. static void ngd_slim_exit(void)
  1664. {
  1665. platform_driver_unregister(&ngd_slim_driver);
  1666. }
  1667. module_exit(ngd_slim_exit);
  1668. MODULE_LICENSE("GPL v2");
  1669. MODULE_DESCRIPTION("MSM Slimbus controller");
  1670. MODULE_ALIAS("platform:msm-slim-ngd");