qcom-ngd-ctrl.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2011-2017, The Linux Foundation. All rights reserved.
  3. // Copyright (c) 2018, Linaro Limited
  4. #include <linux/irq.h>
  5. #include <linux/kernel.h>
  6. #include <linux/init.h>
  7. #include <linux/slab.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/dma-mapping.h>
  11. #include <linux/dmaengine.h>
  12. #include <linux/slimbus.h>
  13. #include <linux/delay.h>
  14. #include <linux/pm_runtime.h>
  15. #include <linux/of.h>
  16. #include <linux/io.h>
  17. #include <linux/soc/qcom/qmi.h>
  18. #include <net/sock.h>
  19. #include "slimbus.h"
  20. /* NGD (Non-ported Generic Device) registers */
  21. #define NGD_CFG 0x0
  22. #define NGD_CFG_ENABLE BIT(0)
  23. #define NGD_CFG_RX_MSGQ_EN BIT(1)
  24. #define NGD_CFG_TX_MSGQ_EN BIT(2)
  25. #define NGD_STATUS 0x4
  26. #define NGD_LADDR BIT(1)
  27. #define NGD_RX_MSGQ_CFG 0x8
  28. #define NGD_INT_EN 0x10
  29. #define NGD_INT_RECFG_DONE BIT(24)
  30. #define NGD_INT_TX_NACKED_2 BIT(25)
  31. #define NGD_INT_MSG_BUF_CONTE BIT(26)
  32. #define NGD_INT_MSG_TX_INVAL BIT(27)
  33. #define NGD_INT_IE_VE_CHG BIT(28)
  34. #define NGD_INT_DEV_ERR BIT(29)
  35. #define NGD_INT_RX_MSG_RCVD BIT(30)
  36. #define NGD_INT_TX_MSG_SENT BIT(31)
  37. #define NGD_INT_STAT 0x14
  38. #define NGD_INT_CLR 0x18
  39. #define DEF_NGD_INT_MASK (NGD_INT_TX_NACKED_2 | NGD_INT_MSG_BUF_CONTE | \
  40. NGD_INT_MSG_TX_INVAL | NGD_INT_IE_VE_CHG | \
  41. NGD_INT_DEV_ERR | NGD_INT_TX_MSG_SENT | \
  42. NGD_INT_RX_MSG_RCVD)
  43. /* Slimbus QMI service */
  44. #define SLIMBUS_QMI_SVC_ID 0x0301
  45. #define SLIMBUS_QMI_SVC_V1 1
  46. #define SLIMBUS_QMI_INS_ID 0
  47. #define SLIMBUS_QMI_SELECT_INSTANCE_REQ_V01 0x0020
  48. #define SLIMBUS_QMI_SELECT_INSTANCE_RESP_V01 0x0020
  49. #define SLIMBUS_QMI_POWER_REQ_V01 0x0021
  50. #define SLIMBUS_QMI_POWER_RESP_V01 0x0021
  51. #define SLIMBUS_QMI_CHECK_FRAMER_STATUS_REQ 0x0022
  52. #define SLIMBUS_QMI_CHECK_FRAMER_STATUS_RESP 0x0022
  53. #define SLIMBUS_QMI_POWER_REQ_MAX_MSG_LEN 14
  54. #define SLIMBUS_QMI_POWER_RESP_MAX_MSG_LEN 7
  55. #define SLIMBUS_QMI_SELECT_INSTANCE_REQ_MAX_MSG_LEN 14
  56. #define SLIMBUS_QMI_SELECT_INSTANCE_RESP_MAX_MSG_LEN 7
  57. #define SLIMBUS_QMI_CHECK_FRAMER_STAT_RESP_MAX_MSG_LEN 7
  58. /* QMI response timeout of 500ms */
  59. #define SLIMBUS_QMI_RESP_TOUT 1000
  60. /* User defined commands */
  61. #define SLIM_USR_MC_GENERIC_ACK 0x25
  62. #define SLIM_USR_MC_MASTER_CAPABILITY 0x0
  63. #define SLIM_USR_MC_REPORT_SATELLITE 0x1
  64. #define SLIM_USR_MC_ADDR_QUERY 0xD
  65. #define SLIM_USR_MC_ADDR_REPLY 0xE
  66. #define SLIM_USR_MC_DEFINE_CHAN 0x20
  67. #define SLIM_USR_MC_DEF_ACT_CHAN 0x21
  68. #define SLIM_USR_MC_CHAN_CTRL 0x23
  69. #define SLIM_USR_MC_RECONFIG_NOW 0x24
  70. #define SLIM_USR_MC_REQ_BW 0x28
  71. #define SLIM_USR_MC_CONNECT_SRC 0x2C
  72. #define SLIM_USR_MC_CONNECT_SINK 0x2D
  73. #define SLIM_USR_MC_DISCONNECT_PORT 0x2E
  74. #define SLIM_USR_MC_REPEAT_CHANGE_VALUE 0x0
  75. #define QCOM_SLIM_NGD_AUTOSUSPEND MSEC_PER_SEC
  76. #define SLIM_RX_MSGQ_TIMEOUT_VAL 0x10000
  77. #define SLIM_LA_MGR 0xFF
  78. #define SLIM_ROOT_FREQ 24576000
  79. #define LADDR_RETRY 5
  80. /* Per spec.max 40 bytes per received message */
  81. #define SLIM_MSGQ_BUF_LEN 40
  82. #define QCOM_SLIM_NGD_DESC_NUM 32
  83. #define SLIM_MSG_ASM_FIRST_WORD(l, mt, mc, dt, ad) \
  84. ((l) | ((mt) << 5) | ((mc) << 8) | ((dt) << 15) | ((ad) << 16))
  85. #define INIT_MX_RETRIES 10
  86. #define DEF_RETRY_MS 10
  87. #define SAT_MAGIC_LSB 0xD9
  88. #define SAT_MAGIC_MSB 0xC5
  89. #define SAT_MSG_VER 0x1
  90. #define SAT_MSG_PROT 0x1
  91. #define to_ngd(d) container_of(d, struct qcom_slim_ngd, dev)
  92. struct ngd_reg_offset_data {
  93. u32 offset, size;
  94. };
  95. static const struct ngd_reg_offset_data ngd_v1_5_offset_info = {
  96. .offset = 0x1000,
  97. .size = 0x1000,
  98. };
  99. enum qcom_slim_ngd_state {
  100. QCOM_SLIM_NGD_CTRL_AWAKE,
  101. QCOM_SLIM_NGD_CTRL_IDLE,
  102. QCOM_SLIM_NGD_CTRL_ASLEEP,
  103. QCOM_SLIM_NGD_CTRL_DOWN,
  104. };
  105. struct qcom_slim_ngd_qmi {
  106. struct qmi_handle qmi;
  107. struct sockaddr_qrtr svc_info;
  108. struct qmi_handle svc_event_hdl;
  109. struct qmi_response_type_v01 resp;
  110. struct qmi_handle *handle;
  111. struct completion qmi_comp;
  112. };
  113. struct qcom_slim_ngd_ctrl;
  114. struct qcom_slim_ngd;
  115. struct qcom_slim_ngd_dma_desc {
  116. struct dma_async_tx_descriptor *desc;
  117. struct qcom_slim_ngd_ctrl *ctrl;
  118. struct completion *comp;
  119. dma_cookie_t cookie;
  120. dma_addr_t phys;
  121. void *base;
  122. };
  123. struct qcom_slim_ngd {
  124. struct platform_device *pdev;
  125. void __iomem *base;
  126. int id;
  127. };
  128. struct qcom_slim_ngd_ctrl {
  129. struct slim_framer framer;
  130. struct slim_controller ctrl;
  131. struct qcom_slim_ngd_qmi qmi;
  132. struct qcom_slim_ngd *ngd;
  133. struct device *dev;
  134. void __iomem *base;
  135. struct dma_chan *dma_rx_channel;
  136. struct dma_chan *dma_tx_channel;
  137. struct qcom_slim_ngd_dma_desc rx_desc[QCOM_SLIM_NGD_DESC_NUM];
  138. struct qcom_slim_ngd_dma_desc txdesc[QCOM_SLIM_NGD_DESC_NUM];
  139. struct completion reconf;
  140. struct work_struct m_work;
  141. struct workqueue_struct *mwq;
  142. spinlock_t tx_buf_lock;
  143. enum qcom_slim_ngd_state state;
  144. dma_addr_t rx_phys_base;
  145. dma_addr_t tx_phys_base;
  146. void *rx_base;
  147. void *tx_base;
  148. int tx_tail;
  149. int tx_head;
  150. u32 ver;
  151. };
  152. enum slimbus_mode_enum_type_v01 {
  153. /* To force a 32 bit signed enum. Do not change or use*/
  154. SLIMBUS_MODE_ENUM_TYPE_MIN_ENUM_VAL_V01 = INT_MIN,
  155. SLIMBUS_MODE_SATELLITE_V01 = 1,
  156. SLIMBUS_MODE_MASTER_V01 = 2,
  157. SLIMBUS_MODE_ENUM_TYPE_MAX_ENUM_VAL_V01 = INT_MAX,
  158. };
  159. enum slimbus_pm_enum_type_v01 {
  160. /* To force a 32 bit signed enum. Do not change or use*/
  161. SLIMBUS_PM_ENUM_TYPE_MIN_ENUM_VAL_V01 = INT_MIN,
  162. SLIMBUS_PM_INACTIVE_V01 = 1,
  163. SLIMBUS_PM_ACTIVE_V01 = 2,
  164. SLIMBUS_PM_ENUM_TYPE_MAX_ENUM_VAL_V01 = INT_MAX,
  165. };
  166. enum slimbus_resp_enum_type_v01 {
  167. SLIMBUS_RESP_ENUM_TYPE_MIN_VAL_V01 = INT_MIN,
  168. SLIMBUS_RESP_SYNCHRONOUS_V01 = 1,
  169. SLIMBUS_RESP_ENUM_TYPE_MAX_VAL_V01 = INT_MAX,
  170. };
  171. struct slimbus_select_inst_req_msg_v01 {
  172. uint32_t instance;
  173. uint8_t mode_valid;
  174. enum slimbus_mode_enum_type_v01 mode;
  175. };
  176. struct slimbus_select_inst_resp_msg_v01 {
  177. struct qmi_response_type_v01 resp;
  178. };
  179. struct slimbus_power_req_msg_v01 {
  180. enum slimbus_pm_enum_type_v01 pm_req;
  181. uint8_t resp_type_valid;
  182. enum slimbus_resp_enum_type_v01 resp_type;
  183. };
  184. struct slimbus_power_resp_msg_v01 {
  185. struct qmi_response_type_v01 resp;
  186. };
  187. static struct qmi_elem_info slimbus_select_inst_req_msg_v01_ei[] = {
  188. {
  189. .data_type = QMI_UNSIGNED_4_BYTE,
  190. .elem_len = 1,
  191. .elem_size = sizeof(uint32_t),
  192. .array_type = NO_ARRAY,
  193. .tlv_type = 0x01,
  194. .offset = offsetof(struct slimbus_select_inst_req_msg_v01,
  195. instance),
  196. .ei_array = NULL,
  197. },
  198. {
  199. .data_type = QMI_OPT_FLAG,
  200. .elem_len = 1,
  201. .elem_size = sizeof(uint8_t),
  202. .array_type = NO_ARRAY,
  203. .tlv_type = 0x10,
  204. .offset = offsetof(struct slimbus_select_inst_req_msg_v01,
  205. mode_valid),
  206. .ei_array = NULL,
  207. },
  208. {
  209. .data_type = QMI_UNSIGNED_4_BYTE,
  210. .elem_len = 1,
  211. .elem_size = sizeof(enum slimbus_mode_enum_type_v01),
  212. .array_type = NO_ARRAY,
  213. .tlv_type = 0x10,
  214. .offset = offsetof(struct slimbus_select_inst_req_msg_v01,
  215. mode),
  216. .ei_array = NULL,
  217. },
  218. {
  219. .data_type = QMI_EOTI,
  220. .elem_len = 0,
  221. .elem_size = 0,
  222. .array_type = NO_ARRAY,
  223. .tlv_type = 0x00,
  224. .offset = 0,
  225. .ei_array = NULL,
  226. },
  227. };
  228. static struct qmi_elem_info slimbus_select_inst_resp_msg_v01_ei[] = {
  229. {
  230. .data_type = QMI_STRUCT,
  231. .elem_len = 1,
  232. .elem_size = sizeof(struct qmi_response_type_v01),
  233. .array_type = NO_ARRAY,
  234. .tlv_type = 0x02,
  235. .offset = offsetof(struct slimbus_select_inst_resp_msg_v01,
  236. resp),
  237. .ei_array = qmi_response_type_v01_ei,
  238. },
  239. {
  240. .data_type = QMI_EOTI,
  241. .elem_len = 0,
  242. .elem_size = 0,
  243. .array_type = NO_ARRAY,
  244. .tlv_type = 0x00,
  245. .offset = 0,
  246. .ei_array = NULL,
  247. },
  248. };
  249. static struct qmi_elem_info slimbus_power_req_msg_v01_ei[] = {
  250. {
  251. .data_type = QMI_UNSIGNED_4_BYTE,
  252. .elem_len = 1,
  253. .elem_size = sizeof(enum slimbus_pm_enum_type_v01),
  254. .array_type = NO_ARRAY,
  255. .tlv_type = 0x01,
  256. .offset = offsetof(struct slimbus_power_req_msg_v01,
  257. pm_req),
  258. .ei_array = NULL,
  259. },
  260. {
  261. .data_type = QMI_OPT_FLAG,
  262. .elem_len = 1,
  263. .elem_size = sizeof(uint8_t),
  264. .array_type = NO_ARRAY,
  265. .tlv_type = 0x10,
  266. .offset = offsetof(struct slimbus_power_req_msg_v01,
  267. resp_type_valid),
  268. },
  269. {
  270. .data_type = QMI_SIGNED_4_BYTE_ENUM,
  271. .elem_len = 1,
  272. .elem_size = sizeof(enum slimbus_resp_enum_type_v01),
  273. .array_type = NO_ARRAY,
  274. .tlv_type = 0x10,
  275. .offset = offsetof(struct slimbus_power_req_msg_v01,
  276. resp_type),
  277. },
  278. {
  279. .data_type = QMI_EOTI,
  280. .elem_len = 0,
  281. .elem_size = 0,
  282. .array_type = NO_ARRAY,
  283. .tlv_type = 0x00,
  284. .offset = 0,
  285. .ei_array = NULL,
  286. },
  287. };
  288. static struct qmi_elem_info slimbus_power_resp_msg_v01_ei[] = {
  289. {
  290. .data_type = QMI_STRUCT,
  291. .elem_len = 1,
  292. .elem_size = sizeof(struct qmi_response_type_v01),
  293. .array_type = NO_ARRAY,
  294. .tlv_type = 0x02,
  295. .offset = offsetof(struct slimbus_power_resp_msg_v01, resp),
  296. .ei_array = qmi_response_type_v01_ei,
  297. },
  298. {
  299. .data_type = QMI_EOTI,
  300. .elem_len = 0,
  301. .elem_size = 0,
  302. .array_type = NO_ARRAY,
  303. .tlv_type = 0x00,
  304. .offset = 0,
  305. .ei_array = NULL,
  306. },
  307. };
  308. static int qcom_slim_qmi_send_select_inst_req(struct qcom_slim_ngd_ctrl *ctrl,
  309. struct slimbus_select_inst_req_msg_v01 *req)
  310. {
  311. struct slimbus_select_inst_resp_msg_v01 resp = { { 0, 0 } };
  312. struct qmi_txn txn;
  313. int rc;
  314. rc = qmi_txn_init(ctrl->qmi.handle, &txn,
  315. slimbus_select_inst_resp_msg_v01_ei, &resp);
  316. if (rc < 0) {
  317. dev_err(ctrl->dev, "QMI TXN init fail: %d\n", rc);
  318. return rc;
  319. }
  320. rc = qmi_send_request(ctrl->qmi.handle, NULL, &txn,
  321. SLIMBUS_QMI_SELECT_INSTANCE_REQ_V01,
  322. SLIMBUS_QMI_SELECT_INSTANCE_REQ_MAX_MSG_LEN,
  323. slimbus_select_inst_req_msg_v01_ei, req);
  324. if (rc < 0) {
  325. dev_err(ctrl->dev, "QMI send req fail %d\n", rc);
  326. qmi_txn_cancel(&txn);
  327. return rc;
  328. }
  329. rc = qmi_txn_wait(&txn, SLIMBUS_QMI_RESP_TOUT);
  330. if (rc < 0) {
  331. dev_err(ctrl->dev, "QMI TXN wait fail: %d\n", rc);
  332. return rc;
  333. }
  334. /* Check the response */
  335. if (resp.resp.result != QMI_RESULT_SUCCESS_V01) {
  336. dev_err(ctrl->dev, "QMI request failed 0x%x\n",
  337. resp.resp.result);
  338. return -EREMOTEIO;
  339. }
  340. return 0;
  341. }
  342. static void qcom_slim_qmi_power_resp_cb(struct qmi_handle *handle,
  343. struct sockaddr_qrtr *sq,
  344. struct qmi_txn *txn, const void *data)
  345. {
  346. struct slimbus_power_resp_msg_v01 *resp;
  347. resp = (struct slimbus_power_resp_msg_v01 *)data;
  348. if (resp->resp.result != QMI_RESULT_SUCCESS_V01)
  349. pr_err("QMI power request failed 0x%x\n",
  350. resp->resp.result);
  351. complete(&txn->completion);
  352. }
  353. static int qcom_slim_qmi_send_power_request(struct qcom_slim_ngd_ctrl *ctrl,
  354. struct slimbus_power_req_msg_v01 *req)
  355. {
  356. struct slimbus_power_resp_msg_v01 resp = { { 0, 0 } };
  357. struct qmi_txn txn;
  358. int rc;
  359. rc = qmi_txn_init(ctrl->qmi.handle, &txn,
  360. slimbus_power_resp_msg_v01_ei, &resp);
  361. rc = qmi_send_request(ctrl->qmi.handle, NULL, &txn,
  362. SLIMBUS_QMI_POWER_REQ_V01,
  363. SLIMBUS_QMI_POWER_REQ_MAX_MSG_LEN,
  364. slimbus_power_req_msg_v01_ei, req);
  365. if (rc < 0) {
  366. dev_err(ctrl->dev, "QMI send req fail %d\n", rc);
  367. qmi_txn_cancel(&txn);
  368. return rc;
  369. }
  370. rc = qmi_txn_wait(&txn, SLIMBUS_QMI_RESP_TOUT);
  371. if (rc < 0) {
  372. dev_err(ctrl->dev, "QMI TXN wait fail: %d\n", rc);
  373. return rc;
  374. }
  375. /* Check the response */
  376. if (resp.resp.result != QMI_RESULT_SUCCESS_V01) {
  377. dev_err(ctrl->dev, "QMI request failed 0x%x\n",
  378. resp.resp.result);
  379. return -EREMOTEIO;
  380. }
  381. return 0;
  382. }
  383. static struct qmi_msg_handler qcom_slim_qmi_msg_handlers[] = {
  384. {
  385. .type = QMI_RESPONSE,
  386. .msg_id = SLIMBUS_QMI_POWER_RESP_V01,
  387. .ei = slimbus_power_resp_msg_v01_ei,
  388. .decoded_size = sizeof(struct slimbus_power_resp_msg_v01),
  389. .fn = qcom_slim_qmi_power_resp_cb,
  390. },
  391. {}
  392. };
  393. static int qcom_slim_qmi_init(struct qcom_slim_ngd_ctrl *ctrl,
  394. bool apps_is_master)
  395. {
  396. struct slimbus_select_inst_req_msg_v01 req;
  397. struct qmi_handle *handle;
  398. int rc;
  399. handle = devm_kzalloc(ctrl->dev, sizeof(*handle), GFP_KERNEL);
  400. if (!handle)
  401. return -ENOMEM;
  402. rc = qmi_handle_init(handle, SLIMBUS_QMI_POWER_REQ_MAX_MSG_LEN,
  403. NULL, qcom_slim_qmi_msg_handlers);
  404. if (rc < 0) {
  405. dev_err(ctrl->dev, "QMI client init failed: %d\n", rc);
  406. goto qmi_handle_init_failed;
  407. }
  408. rc = kernel_connect(handle->sock,
  409. (struct sockaddr *)&ctrl->qmi.svc_info,
  410. sizeof(ctrl->qmi.svc_info), 0);
  411. if (rc < 0) {
  412. dev_err(ctrl->dev, "Remote Service connect failed: %d\n", rc);
  413. goto qmi_connect_to_service_failed;
  414. }
  415. /* Instance is 0 based */
  416. req.instance = (ctrl->ngd->id >> 1);
  417. req.mode_valid = 1;
  418. /* Mode indicates the role of the ADSP */
  419. if (apps_is_master)
  420. req.mode = SLIMBUS_MODE_SATELLITE_V01;
  421. else
  422. req.mode = SLIMBUS_MODE_MASTER_V01;
  423. ctrl->qmi.handle = handle;
  424. rc = qcom_slim_qmi_send_select_inst_req(ctrl, &req);
  425. if (rc) {
  426. dev_err(ctrl->dev, "failed to select h/w instance\n");
  427. goto qmi_select_instance_failed;
  428. }
  429. return 0;
  430. qmi_select_instance_failed:
  431. ctrl->qmi.handle = NULL;
  432. qmi_connect_to_service_failed:
  433. qmi_handle_release(handle);
  434. qmi_handle_init_failed:
  435. devm_kfree(ctrl->dev, handle);
  436. return rc;
  437. }
  438. static void qcom_slim_qmi_exit(struct qcom_slim_ngd_ctrl *ctrl)
  439. {
  440. if (!ctrl->qmi.handle)
  441. return;
  442. qmi_handle_release(ctrl->qmi.handle);
  443. devm_kfree(ctrl->dev, ctrl->qmi.handle);
  444. ctrl->qmi.handle = NULL;
  445. }
  446. static int qcom_slim_qmi_power_request(struct qcom_slim_ngd_ctrl *ctrl,
  447. bool active)
  448. {
  449. struct slimbus_power_req_msg_v01 req;
  450. if (active)
  451. req.pm_req = SLIMBUS_PM_ACTIVE_V01;
  452. else
  453. req.pm_req = SLIMBUS_PM_INACTIVE_V01;
  454. req.resp_type_valid = 0;
  455. return qcom_slim_qmi_send_power_request(ctrl, &req);
  456. }
  457. static u32 *qcom_slim_ngd_tx_msg_get(struct qcom_slim_ngd_ctrl *ctrl, int len,
  458. struct completion *comp)
  459. {
  460. struct qcom_slim_ngd_dma_desc *desc;
  461. unsigned long flags;
  462. spin_lock_irqsave(&ctrl->tx_buf_lock, flags);
  463. if ((ctrl->tx_tail + 1) % QCOM_SLIM_NGD_DESC_NUM == ctrl->tx_head) {
  464. spin_unlock_irqrestore(&ctrl->tx_buf_lock, flags);
  465. return NULL;
  466. }
  467. desc = &ctrl->txdesc[ctrl->tx_tail];
  468. desc->base = ctrl->tx_base + ctrl->tx_tail * SLIM_MSGQ_BUF_LEN;
  469. desc->comp = comp;
  470. ctrl->tx_tail = (ctrl->tx_tail + 1) % QCOM_SLIM_NGD_DESC_NUM;
  471. spin_unlock_irqrestore(&ctrl->tx_buf_lock, flags);
  472. return desc->base;
  473. }
  474. static void qcom_slim_ngd_tx_msg_dma_cb(void *args)
  475. {
  476. struct qcom_slim_ngd_dma_desc *desc = args;
  477. struct qcom_slim_ngd_ctrl *ctrl = desc->ctrl;
  478. unsigned long flags;
  479. spin_lock_irqsave(&ctrl->tx_buf_lock, flags);
  480. if (desc->comp) {
  481. complete(desc->comp);
  482. desc->comp = NULL;
  483. }
  484. ctrl->tx_head = (ctrl->tx_head + 1) % QCOM_SLIM_NGD_DESC_NUM;
  485. spin_unlock_irqrestore(&ctrl->tx_buf_lock, flags);
  486. }
  487. static int qcom_slim_ngd_tx_msg_post(struct qcom_slim_ngd_ctrl *ctrl,
  488. void *buf, int len)
  489. {
  490. struct qcom_slim_ngd_dma_desc *desc;
  491. unsigned long flags;
  492. int index, offset;
  493. spin_lock_irqsave(&ctrl->tx_buf_lock, flags);
  494. offset = buf - ctrl->tx_base;
  495. index = offset/SLIM_MSGQ_BUF_LEN;
  496. desc = &ctrl->txdesc[index];
  497. desc->phys = ctrl->tx_phys_base + offset;
  498. desc->base = ctrl->tx_base + offset;
  499. desc->ctrl = ctrl;
  500. len = (len + 3) & 0xfc;
  501. desc->desc = dmaengine_prep_slave_single(ctrl->dma_tx_channel,
  502. desc->phys, len,
  503. DMA_MEM_TO_DEV,
  504. DMA_PREP_INTERRUPT);
  505. if (!desc->desc) {
  506. dev_err(ctrl->dev, "unable to prepare channel\n");
  507. spin_unlock_irqrestore(&ctrl->tx_buf_lock, flags);
  508. return -EINVAL;
  509. }
  510. desc->desc->callback = qcom_slim_ngd_tx_msg_dma_cb;
  511. desc->desc->callback_param = desc;
  512. desc->desc->cookie = dmaengine_submit(desc->desc);
  513. dma_async_issue_pending(ctrl->dma_tx_channel);
  514. spin_unlock_irqrestore(&ctrl->tx_buf_lock, flags);
  515. return 0;
  516. }
  517. static void qcom_slim_ngd_rx(struct qcom_slim_ngd_ctrl *ctrl, u8 *buf)
  518. {
  519. u8 mc, mt, len;
  520. mt = SLIM_HEADER_GET_MT(buf[0]);
  521. len = SLIM_HEADER_GET_RL(buf[0]);
  522. mc = SLIM_HEADER_GET_MC(buf[1]);
  523. if (mc == SLIM_USR_MC_MASTER_CAPABILITY &&
  524. mt == SLIM_MSG_MT_SRC_REFERRED_USER)
  525. queue_work(ctrl->mwq, &ctrl->m_work);
  526. if (mc == SLIM_MSG_MC_REPLY_INFORMATION ||
  527. mc == SLIM_MSG_MC_REPLY_VALUE || (mc == SLIM_USR_MC_ADDR_REPLY &&
  528. mt == SLIM_MSG_MT_SRC_REFERRED_USER) ||
  529. (mc == SLIM_USR_MC_GENERIC_ACK &&
  530. mt == SLIM_MSG_MT_SRC_REFERRED_USER)) {
  531. slim_msg_response(&ctrl->ctrl, &buf[4], buf[3], len - 4);
  532. pm_runtime_mark_last_busy(ctrl->dev);
  533. }
  534. }
  535. static void qcom_slim_ngd_rx_msgq_cb(void *args)
  536. {
  537. struct qcom_slim_ngd_dma_desc *desc = args;
  538. struct qcom_slim_ngd_ctrl *ctrl = desc->ctrl;
  539. qcom_slim_ngd_rx(ctrl, (u8 *)desc->base);
  540. /* Add descriptor back to the queue */
  541. desc->desc = dmaengine_prep_slave_single(ctrl->dma_rx_channel,
  542. desc->phys, SLIM_MSGQ_BUF_LEN,
  543. DMA_DEV_TO_MEM,
  544. DMA_PREP_INTERRUPT);
  545. if (!desc->desc) {
  546. dev_err(ctrl->dev, "Unable to prepare rx channel\n");
  547. return;
  548. }
  549. desc->desc->callback = qcom_slim_ngd_rx_msgq_cb;
  550. desc->desc->callback_param = desc;
  551. desc->desc->cookie = dmaengine_submit(desc->desc);
  552. dma_async_issue_pending(ctrl->dma_rx_channel);
  553. }
  554. static int qcom_slim_ngd_post_rx_msgq(struct qcom_slim_ngd_ctrl *ctrl)
  555. {
  556. struct qcom_slim_ngd_dma_desc *desc;
  557. int i;
  558. for (i = 0; i < QCOM_SLIM_NGD_DESC_NUM; i++) {
  559. desc = &ctrl->rx_desc[i];
  560. desc->phys = ctrl->rx_phys_base + i * SLIM_MSGQ_BUF_LEN;
  561. desc->ctrl = ctrl;
  562. desc->base = ctrl->rx_base + i * SLIM_MSGQ_BUF_LEN;
  563. desc->desc = dmaengine_prep_slave_single(ctrl->dma_rx_channel,
  564. desc->phys, SLIM_MSGQ_BUF_LEN,
  565. DMA_DEV_TO_MEM,
  566. DMA_PREP_INTERRUPT);
  567. if (!desc->desc) {
  568. dev_err(ctrl->dev, "Unable to prepare rx channel\n");
  569. return -EINVAL;
  570. }
  571. desc->desc->callback = qcom_slim_ngd_rx_msgq_cb;
  572. desc->desc->callback_param = desc;
  573. desc->desc->cookie = dmaengine_submit(desc->desc);
  574. }
  575. dma_async_issue_pending(ctrl->dma_rx_channel);
  576. return 0;
  577. }
  578. static int qcom_slim_ngd_init_rx_msgq(struct qcom_slim_ngd_ctrl *ctrl)
  579. {
  580. struct device *dev = ctrl->dev;
  581. int ret, size;
  582. ctrl->dma_rx_channel = dma_request_slave_channel(dev, "rx");
  583. if (!ctrl->dma_rx_channel) {
  584. dev_err(dev, "Failed to request dma channels");
  585. return -EINVAL;
  586. }
  587. size = QCOM_SLIM_NGD_DESC_NUM * SLIM_MSGQ_BUF_LEN;
  588. ctrl->rx_base = dma_alloc_coherent(dev, size, &ctrl->rx_phys_base,
  589. GFP_KERNEL);
  590. if (!ctrl->rx_base) {
  591. dev_err(dev, "dma_alloc_coherent failed\n");
  592. ret = -ENOMEM;
  593. goto rel_rx;
  594. }
  595. ret = qcom_slim_ngd_post_rx_msgq(ctrl);
  596. if (ret) {
  597. dev_err(dev, "post_rx_msgq() failed 0x%x\n", ret);
  598. goto rx_post_err;
  599. }
  600. return 0;
  601. rx_post_err:
  602. dma_free_coherent(dev, size, ctrl->rx_base, ctrl->rx_phys_base);
  603. rel_rx:
  604. dma_release_channel(ctrl->dma_rx_channel);
  605. return ret;
  606. }
  607. static int qcom_slim_ngd_init_tx_msgq(struct qcom_slim_ngd_ctrl *ctrl)
  608. {
  609. struct device *dev = ctrl->dev;
  610. unsigned long flags;
  611. int ret = 0;
  612. int size;
  613. ctrl->dma_tx_channel = dma_request_slave_channel(dev, "tx");
  614. if (!ctrl->dma_tx_channel) {
  615. dev_err(dev, "Failed to request dma channels");
  616. return -EINVAL;
  617. }
  618. size = ((QCOM_SLIM_NGD_DESC_NUM + 1) * SLIM_MSGQ_BUF_LEN);
  619. ctrl->tx_base = dma_alloc_coherent(dev, size, &ctrl->tx_phys_base,
  620. GFP_KERNEL);
  621. if (!ctrl->tx_base) {
  622. dev_err(dev, "dma_alloc_coherent failed\n");
  623. ret = -EINVAL;
  624. goto rel_tx;
  625. }
  626. spin_lock_irqsave(&ctrl->tx_buf_lock, flags);
  627. ctrl->tx_tail = 0;
  628. ctrl->tx_head = 0;
  629. spin_unlock_irqrestore(&ctrl->tx_buf_lock, flags);
  630. return 0;
  631. rel_tx:
  632. dma_release_channel(ctrl->dma_tx_channel);
  633. return ret;
  634. }
  635. static int qcom_slim_ngd_init_dma(struct qcom_slim_ngd_ctrl *ctrl)
  636. {
  637. int ret = 0;
  638. ret = qcom_slim_ngd_init_rx_msgq(ctrl);
  639. if (ret) {
  640. dev_err(ctrl->dev, "rx dma init failed\n");
  641. return ret;
  642. }
  643. ret = qcom_slim_ngd_init_tx_msgq(ctrl);
  644. if (ret)
  645. dev_err(ctrl->dev, "tx dma init failed\n");
  646. return ret;
  647. }
  648. static irqreturn_t qcom_slim_ngd_interrupt(int irq, void *d)
  649. {
  650. struct qcom_slim_ngd_ctrl *ctrl = d;
  651. void __iomem *base = ctrl->ngd->base;
  652. u32 stat = readl(base + NGD_INT_STAT);
  653. if ((stat & NGD_INT_MSG_BUF_CONTE) ||
  654. (stat & NGD_INT_MSG_TX_INVAL) || (stat & NGD_INT_DEV_ERR) ||
  655. (stat & NGD_INT_TX_NACKED_2)) {
  656. dev_err(ctrl->dev, "Error Interrupt received 0x%x\n", stat);
  657. }
  658. writel(stat, base + NGD_INT_CLR);
  659. return IRQ_HANDLED;
  660. }
  661. static int qcom_slim_ngd_xfer_msg(struct slim_controller *sctrl,
  662. struct slim_msg_txn *txn)
  663. {
  664. struct qcom_slim_ngd_ctrl *ctrl = dev_get_drvdata(sctrl->dev);
  665. DECLARE_COMPLETION_ONSTACK(tx_sent);
  666. DECLARE_COMPLETION_ONSTACK(done);
  667. int ret, timeout, i;
  668. u8 wbuf[SLIM_MSGQ_BUF_LEN];
  669. u8 rbuf[SLIM_MSGQ_BUF_LEN];
  670. u32 *pbuf;
  671. u8 *puc;
  672. u8 la = txn->la;
  673. bool usr_msg = false;
  674. if (txn->mc & SLIM_MSG_CLK_PAUSE_SEQ_FLG)
  675. return -EPROTONOSUPPORT;
  676. if (txn->mt == SLIM_MSG_MT_CORE &&
  677. (txn->mc >= SLIM_MSG_MC_BEGIN_RECONFIGURATION &&
  678. txn->mc <= SLIM_MSG_MC_RECONFIGURE_NOW))
  679. return 0;
  680. if (txn->dt == SLIM_MSG_DEST_ENUMADDR)
  681. return -EPROTONOSUPPORT;
  682. if (txn->msg->num_bytes > SLIM_MSGQ_BUF_LEN ||
  683. txn->rl > SLIM_MSGQ_BUF_LEN) {
  684. dev_err(ctrl->dev, "msg exeeds HW limit\n");
  685. return -EINVAL;
  686. }
  687. pbuf = qcom_slim_ngd_tx_msg_get(ctrl, txn->rl, &tx_sent);
  688. if (!pbuf) {
  689. dev_err(ctrl->dev, "Message buffer unavailable\n");
  690. return -ENOMEM;
  691. }
  692. if (txn->mt == SLIM_MSG_MT_CORE &&
  693. (txn->mc == SLIM_MSG_MC_CONNECT_SOURCE ||
  694. txn->mc == SLIM_MSG_MC_CONNECT_SINK ||
  695. txn->mc == SLIM_MSG_MC_DISCONNECT_PORT)) {
  696. txn->mt = SLIM_MSG_MT_DEST_REFERRED_USER;
  697. switch (txn->mc) {
  698. case SLIM_MSG_MC_CONNECT_SOURCE:
  699. txn->mc = SLIM_USR_MC_CONNECT_SRC;
  700. break;
  701. case SLIM_MSG_MC_CONNECT_SINK:
  702. txn->mc = SLIM_USR_MC_CONNECT_SINK;
  703. break;
  704. case SLIM_MSG_MC_DISCONNECT_PORT:
  705. txn->mc = SLIM_USR_MC_DISCONNECT_PORT;
  706. break;
  707. default:
  708. return -EINVAL;
  709. }
  710. usr_msg = true;
  711. i = 0;
  712. wbuf[i++] = txn->la;
  713. la = SLIM_LA_MGR;
  714. wbuf[i++] = txn->msg->wbuf[0];
  715. if (txn->mc != SLIM_USR_MC_DISCONNECT_PORT)
  716. wbuf[i++] = txn->msg->wbuf[1];
  717. txn->comp = &done;
  718. ret = slim_alloc_txn_tid(sctrl, txn);
  719. if (ret) {
  720. dev_err(ctrl->dev, "Unable to allocate TID\n");
  721. return ret;
  722. }
  723. wbuf[i++] = txn->tid;
  724. txn->msg->num_bytes = i;
  725. txn->msg->wbuf = wbuf;
  726. txn->msg->rbuf = rbuf;
  727. txn->rl = txn->msg->num_bytes + 4;
  728. }
  729. /* HW expects length field to be excluded */
  730. txn->rl--;
  731. puc = (u8 *)pbuf;
  732. *pbuf = 0;
  733. if (txn->dt == SLIM_MSG_DEST_LOGICALADDR) {
  734. *pbuf = SLIM_MSG_ASM_FIRST_WORD(txn->rl, txn->mt, txn->mc, 0,
  735. la);
  736. puc += 3;
  737. } else {
  738. *pbuf = SLIM_MSG_ASM_FIRST_WORD(txn->rl, txn->mt, txn->mc, 1,
  739. la);
  740. puc += 2;
  741. }
  742. if (slim_tid_txn(txn->mt, txn->mc))
  743. *(puc++) = txn->tid;
  744. if (slim_ec_txn(txn->mt, txn->mc)) {
  745. *(puc++) = (txn->ec & 0xFF);
  746. *(puc++) = (txn->ec >> 8) & 0xFF;
  747. }
  748. if (txn->msg && txn->msg->wbuf)
  749. memcpy(puc, txn->msg->wbuf, txn->msg->num_bytes);
  750. ret = qcom_slim_ngd_tx_msg_post(ctrl, pbuf, txn->rl);
  751. if (ret)
  752. return ret;
  753. timeout = wait_for_completion_timeout(&tx_sent, HZ);
  754. if (!timeout) {
  755. dev_err(sctrl->dev, "TX timed out:MC:0x%x,mt:0x%x", txn->mc,
  756. txn->mt);
  757. return -ETIMEDOUT;
  758. }
  759. if (usr_msg) {
  760. timeout = wait_for_completion_timeout(&done, HZ);
  761. if (!timeout) {
  762. dev_err(sctrl->dev, "TX timed out:MC:0x%x,mt:0x%x",
  763. txn->mc, txn->mt);
  764. return -ETIMEDOUT;
  765. }
  766. }
  767. return 0;
  768. }
  769. static int qcom_slim_ngd_xfer_msg_sync(struct slim_controller *ctrl,
  770. struct slim_msg_txn *txn)
  771. {
  772. DECLARE_COMPLETION_ONSTACK(done);
  773. int ret, timeout;
  774. pm_runtime_get_sync(ctrl->dev);
  775. txn->comp = &done;
  776. ret = qcom_slim_ngd_xfer_msg(ctrl, txn);
  777. if (ret)
  778. return ret;
  779. timeout = wait_for_completion_timeout(&done, HZ);
  780. if (!timeout) {
  781. dev_err(ctrl->dev, "TX timed out:MC:0x%x,mt:0x%x", txn->mc,
  782. txn->mt);
  783. return -ETIMEDOUT;
  784. }
  785. return 0;
  786. }
  787. static int qcom_slim_ngd_enable_stream(struct slim_stream_runtime *rt)
  788. {
  789. struct slim_device *sdev = rt->dev;
  790. struct slim_controller *ctrl = sdev->ctrl;
  791. struct slim_val_inf msg = {0};
  792. u8 wbuf[SLIM_MSGQ_BUF_LEN];
  793. u8 rbuf[SLIM_MSGQ_BUF_LEN];
  794. struct slim_msg_txn txn = {0,};
  795. int i, ret;
  796. txn.mt = SLIM_MSG_MT_DEST_REFERRED_USER;
  797. txn.dt = SLIM_MSG_DEST_LOGICALADDR;
  798. txn.la = SLIM_LA_MGR;
  799. txn.ec = 0;
  800. txn.msg = &msg;
  801. txn.msg->num_bytes = 0;
  802. txn.msg->wbuf = wbuf;
  803. txn.msg->rbuf = rbuf;
  804. for (i = 0; i < rt->num_ports; i++) {
  805. struct slim_port *port = &rt->ports[i];
  806. if (txn.msg->num_bytes == 0) {
  807. int seg_interval = SLIM_SLOTS_PER_SUPERFRAME/rt->ratem;
  808. int exp;
  809. wbuf[txn.msg->num_bytes++] = sdev->laddr;
  810. wbuf[txn.msg->num_bytes] = rt->bps >> 2 |
  811. (port->ch.aux_fmt << 6);
  812. /* Data channel segment interval not multiple of 3 */
  813. exp = seg_interval % 3;
  814. if (exp)
  815. wbuf[txn.msg->num_bytes] |= BIT(5);
  816. txn.msg->num_bytes++;
  817. wbuf[txn.msg->num_bytes++] = exp << 4 | rt->prot;
  818. if (rt->prot == SLIM_PROTO_ISO)
  819. wbuf[txn.msg->num_bytes++] =
  820. port->ch.prrate |
  821. SLIM_CHANNEL_CONTENT_FL;
  822. else
  823. wbuf[txn.msg->num_bytes++] = port->ch.prrate;
  824. ret = slim_alloc_txn_tid(ctrl, &txn);
  825. if (ret) {
  826. dev_err(&sdev->dev, "Fail to allocate TID\n");
  827. return -ENXIO;
  828. }
  829. wbuf[txn.msg->num_bytes++] = txn.tid;
  830. }
  831. wbuf[txn.msg->num_bytes++] = port->ch.id;
  832. }
  833. txn.mc = SLIM_USR_MC_DEF_ACT_CHAN;
  834. txn.rl = txn.msg->num_bytes + 4;
  835. ret = qcom_slim_ngd_xfer_msg_sync(ctrl, &txn);
  836. if (ret) {
  837. slim_free_txn_tid(ctrl, &txn);
  838. dev_err(&sdev->dev, "TX timed out:MC:0x%x,mt:0x%x", txn.mc,
  839. txn.mt);
  840. return ret;
  841. }
  842. txn.mc = SLIM_USR_MC_RECONFIG_NOW;
  843. txn.msg->num_bytes = 2;
  844. wbuf[1] = sdev->laddr;
  845. txn.rl = txn.msg->num_bytes + 4;
  846. ret = slim_alloc_txn_tid(ctrl, &txn);
  847. if (ret) {
  848. dev_err(ctrl->dev, "Fail to allocate TID\n");
  849. return ret;
  850. }
  851. wbuf[0] = txn.tid;
  852. ret = qcom_slim_ngd_xfer_msg_sync(ctrl, &txn);
  853. if (ret) {
  854. slim_free_txn_tid(ctrl, &txn);
  855. dev_err(&sdev->dev, "TX timed out:MC:0x%x,mt:0x%x", txn.mc,
  856. txn.mt);
  857. }
  858. return ret;
  859. }
  860. static int qcom_slim_ngd_get_laddr(struct slim_controller *ctrl,
  861. struct slim_eaddr *ea, u8 *laddr)
  862. {
  863. struct slim_val_inf msg = {0};
  864. struct slim_msg_txn txn;
  865. u8 wbuf[10] = {0};
  866. u8 rbuf[10] = {0};
  867. int ret;
  868. txn.mt = SLIM_MSG_MT_DEST_REFERRED_USER;
  869. txn.dt = SLIM_MSG_DEST_LOGICALADDR;
  870. txn.la = SLIM_LA_MGR;
  871. txn.ec = 0;
  872. txn.mc = SLIM_USR_MC_ADDR_QUERY;
  873. txn.rl = 11;
  874. txn.msg = &msg;
  875. txn.msg->num_bytes = 7;
  876. txn.msg->wbuf = wbuf;
  877. txn.msg->rbuf = rbuf;
  878. ret = slim_alloc_txn_tid(ctrl, &txn);
  879. if (ret < 0)
  880. return ret;
  881. wbuf[0] = (u8)txn.tid;
  882. memcpy(&wbuf[1], ea, sizeof(*ea));
  883. ret = qcom_slim_ngd_xfer_msg_sync(ctrl, &txn);
  884. if (ret) {
  885. slim_free_txn_tid(ctrl, &txn);
  886. return ret;
  887. }
  888. *laddr = rbuf[6];
  889. return ret;
  890. }
  891. static int qcom_slim_ngd_exit_dma(struct qcom_slim_ngd_ctrl *ctrl)
  892. {
  893. if (ctrl->dma_rx_channel) {
  894. dmaengine_terminate_sync(ctrl->dma_rx_channel);
  895. dma_release_channel(ctrl->dma_rx_channel);
  896. }
  897. if (ctrl->dma_tx_channel) {
  898. dmaengine_terminate_sync(ctrl->dma_tx_channel);
  899. dma_release_channel(ctrl->dma_tx_channel);
  900. }
  901. ctrl->dma_tx_channel = ctrl->dma_rx_channel = NULL;
  902. return 0;
  903. }
  904. static void qcom_slim_ngd_setup(struct qcom_slim_ngd_ctrl *ctrl)
  905. {
  906. u32 cfg = readl_relaxed(ctrl->ngd->base);
  907. if (ctrl->state == QCOM_SLIM_NGD_CTRL_DOWN)
  908. qcom_slim_ngd_init_dma(ctrl);
  909. /* By default enable message queues */
  910. cfg |= NGD_CFG_RX_MSGQ_EN;
  911. cfg |= NGD_CFG_TX_MSGQ_EN;
  912. /* Enable NGD if it's not already enabled*/
  913. if (!(cfg & NGD_CFG_ENABLE))
  914. cfg |= NGD_CFG_ENABLE;
  915. writel_relaxed(cfg, ctrl->ngd->base);
  916. }
  917. static int qcom_slim_ngd_power_up(struct qcom_slim_ngd_ctrl *ctrl)
  918. {
  919. enum qcom_slim_ngd_state cur_state = ctrl->state;
  920. struct qcom_slim_ngd *ngd = ctrl->ngd;
  921. u32 laddr, rx_msgq;
  922. int timeout, ret = 0;
  923. if (ctrl->state == QCOM_SLIM_NGD_CTRL_DOWN) {
  924. timeout = wait_for_completion_timeout(&ctrl->qmi.qmi_comp, HZ);
  925. if (!timeout)
  926. return -EREMOTEIO;
  927. }
  928. if (ctrl->state == QCOM_SLIM_NGD_CTRL_ASLEEP ||
  929. ctrl->state == QCOM_SLIM_NGD_CTRL_DOWN) {
  930. ret = qcom_slim_qmi_power_request(ctrl, true);
  931. if (ret) {
  932. dev_err(ctrl->dev, "SLIM QMI power request failed:%d\n",
  933. ret);
  934. return ret;
  935. }
  936. }
  937. ctrl->ver = readl_relaxed(ctrl->base);
  938. /* Version info in 16 MSbits */
  939. ctrl->ver >>= 16;
  940. laddr = readl_relaxed(ngd->base + NGD_STATUS);
  941. if (laddr & NGD_LADDR) {
  942. /*
  943. * external MDM restart case where ADSP itself was active framer
  944. * For example, modem restarted when playback was active
  945. */
  946. if (cur_state == QCOM_SLIM_NGD_CTRL_AWAKE) {
  947. dev_info(ctrl->dev, "Subsys restart: ADSP active framer\n");
  948. return 0;
  949. }
  950. return 0;
  951. }
  952. writel_relaxed(DEF_NGD_INT_MASK, ngd->base + NGD_INT_EN);
  953. rx_msgq = readl_relaxed(ngd->base + NGD_RX_MSGQ_CFG);
  954. writel_relaxed(rx_msgq|SLIM_RX_MSGQ_TIMEOUT_VAL,
  955. ngd->base + NGD_RX_MSGQ_CFG);
  956. qcom_slim_ngd_setup(ctrl);
  957. timeout = wait_for_completion_timeout(&ctrl->reconf, HZ);
  958. if (!timeout) {
  959. dev_err(ctrl->dev, "capability exchange timed-out\n");
  960. return -ETIMEDOUT;
  961. }
  962. return 0;
  963. }
  964. static void qcom_slim_ngd_notify_slaves(struct qcom_slim_ngd_ctrl *ctrl)
  965. {
  966. struct slim_device *sbdev;
  967. struct device_node *node;
  968. for_each_child_of_node(ctrl->ngd->pdev->dev.of_node, node) {
  969. sbdev = of_slim_get_device(&ctrl->ctrl, node);
  970. if (!sbdev)
  971. continue;
  972. if (slim_get_logical_addr(sbdev))
  973. dev_err(ctrl->dev, "Failed to get logical address\n");
  974. }
  975. }
  976. static void qcom_slim_ngd_master_worker(struct work_struct *work)
  977. {
  978. struct qcom_slim_ngd_ctrl *ctrl;
  979. struct slim_msg_txn txn;
  980. struct slim_val_inf msg = {0};
  981. int retries = 0;
  982. u8 wbuf[8];
  983. int ret = 0;
  984. ctrl = container_of(work, struct qcom_slim_ngd_ctrl, m_work);
  985. txn.dt = SLIM_MSG_DEST_LOGICALADDR;
  986. txn.ec = 0;
  987. txn.mc = SLIM_USR_MC_REPORT_SATELLITE;
  988. txn.mt = SLIM_MSG_MT_SRC_REFERRED_USER;
  989. txn.la = SLIM_LA_MGR;
  990. wbuf[0] = SAT_MAGIC_LSB;
  991. wbuf[1] = SAT_MAGIC_MSB;
  992. wbuf[2] = SAT_MSG_VER;
  993. wbuf[3] = SAT_MSG_PROT;
  994. txn.msg = &msg;
  995. txn.msg->wbuf = wbuf;
  996. txn.msg->num_bytes = 4;
  997. txn.rl = 8;
  998. dev_info(ctrl->dev, "SLIM SAT: Rcvd master capability\n");
  999. capability_retry:
  1000. ret = qcom_slim_ngd_xfer_msg(&ctrl->ctrl, &txn);
  1001. if (!ret) {
  1002. if (ctrl->state >= QCOM_SLIM_NGD_CTRL_ASLEEP)
  1003. complete(&ctrl->reconf);
  1004. else
  1005. dev_err(ctrl->dev, "unexpected state:%d\n",
  1006. ctrl->state);
  1007. if (ctrl->state == QCOM_SLIM_NGD_CTRL_DOWN)
  1008. qcom_slim_ngd_notify_slaves(ctrl);
  1009. } else if (ret == -EIO) {
  1010. dev_err(ctrl->dev, "capability message NACKed, retrying\n");
  1011. if (retries < INIT_MX_RETRIES) {
  1012. msleep(DEF_RETRY_MS);
  1013. retries++;
  1014. goto capability_retry;
  1015. }
  1016. } else {
  1017. dev_err(ctrl->dev, "SLIM: capability TX failed:%d\n", ret);
  1018. }
  1019. }
  1020. static int qcom_slim_ngd_runtime_resume(struct device *dev)
  1021. {
  1022. struct qcom_slim_ngd_ctrl *ctrl = dev_get_drvdata(dev);
  1023. int ret = 0;
  1024. if (ctrl->state >= QCOM_SLIM_NGD_CTRL_ASLEEP)
  1025. ret = qcom_slim_ngd_power_up(ctrl);
  1026. if (ret) {
  1027. /* Did SSR cause this power up failure */
  1028. if (ctrl->state != QCOM_SLIM_NGD_CTRL_DOWN)
  1029. ctrl->state = QCOM_SLIM_NGD_CTRL_ASLEEP;
  1030. else
  1031. dev_err(ctrl->dev, "HW wakeup attempt during SSR\n");
  1032. } else {
  1033. ctrl->state = QCOM_SLIM_NGD_CTRL_AWAKE;
  1034. }
  1035. return 0;
  1036. }
  1037. static int qcom_slim_ngd_enable(struct qcom_slim_ngd_ctrl *ctrl, bool enable)
  1038. {
  1039. if (enable) {
  1040. int ret = qcom_slim_qmi_init(ctrl, false);
  1041. if (ret) {
  1042. dev_err(ctrl->dev, "qmi init fail, ret:%d, state:%d\n",
  1043. ret, ctrl->state);
  1044. return ret;
  1045. }
  1046. /* controller state should be in sync with framework state */
  1047. complete(&ctrl->qmi.qmi_comp);
  1048. if (!pm_runtime_enabled(ctrl->dev) ||
  1049. !pm_runtime_suspended(ctrl->dev))
  1050. qcom_slim_ngd_runtime_resume(ctrl->dev);
  1051. else
  1052. pm_runtime_resume(ctrl->dev);
  1053. pm_runtime_mark_last_busy(ctrl->dev);
  1054. pm_runtime_put(ctrl->dev);
  1055. ret = slim_register_controller(&ctrl->ctrl);
  1056. if (ret) {
  1057. dev_err(ctrl->dev, "error adding slim controller\n");
  1058. return ret;
  1059. }
  1060. dev_info(ctrl->dev, "SLIM controller Registered\n");
  1061. } else {
  1062. qcom_slim_qmi_exit(ctrl);
  1063. slim_unregister_controller(&ctrl->ctrl);
  1064. }
  1065. return 0;
  1066. }
  1067. static int qcom_slim_ngd_qmi_new_server(struct qmi_handle *hdl,
  1068. struct qmi_service *service)
  1069. {
  1070. struct qcom_slim_ngd_qmi *qmi =
  1071. container_of(hdl, struct qcom_slim_ngd_qmi, svc_event_hdl);
  1072. struct qcom_slim_ngd_ctrl *ctrl =
  1073. container_of(qmi, struct qcom_slim_ngd_ctrl, qmi);
  1074. qmi->svc_info.sq_family = AF_QIPCRTR;
  1075. qmi->svc_info.sq_node = service->node;
  1076. qmi->svc_info.sq_port = service->port;
  1077. qcom_slim_ngd_enable(ctrl, true);
  1078. return 0;
  1079. }
  1080. static void qcom_slim_ngd_qmi_del_server(struct qmi_handle *hdl,
  1081. struct qmi_service *service)
  1082. {
  1083. struct qcom_slim_ngd_qmi *qmi =
  1084. container_of(hdl, struct qcom_slim_ngd_qmi, svc_event_hdl);
  1085. qmi->svc_info.sq_node = 0;
  1086. qmi->svc_info.sq_port = 0;
  1087. }
  1088. static struct qmi_ops qcom_slim_ngd_qmi_svc_event_ops = {
  1089. .new_server = qcom_slim_ngd_qmi_new_server,
  1090. .del_server = qcom_slim_ngd_qmi_del_server,
  1091. };
  1092. static int qcom_slim_ngd_qmi_svc_event_init(struct qcom_slim_ngd_ctrl *ctrl)
  1093. {
  1094. struct qcom_slim_ngd_qmi *qmi = &ctrl->qmi;
  1095. int ret;
  1096. ret = qmi_handle_init(&qmi->svc_event_hdl, 0,
  1097. &qcom_slim_ngd_qmi_svc_event_ops, NULL);
  1098. if (ret < 0) {
  1099. dev_err(ctrl->dev, "qmi_handle_init failed: %d\n", ret);
  1100. return ret;
  1101. }
  1102. ret = qmi_add_lookup(&qmi->svc_event_hdl, SLIMBUS_QMI_SVC_ID,
  1103. SLIMBUS_QMI_SVC_V1, SLIMBUS_QMI_INS_ID);
  1104. if (ret < 0) {
  1105. dev_err(ctrl->dev, "qmi_add_lookup failed: %d\n", ret);
  1106. qmi_handle_release(&qmi->svc_event_hdl);
  1107. }
  1108. return ret;
  1109. }
  1110. static void qcom_slim_ngd_qmi_svc_event_deinit(struct qcom_slim_ngd_qmi *qmi)
  1111. {
  1112. qmi_handle_release(&qmi->svc_event_hdl);
  1113. }
  1114. static struct platform_driver qcom_slim_ngd_driver;
  1115. #define QCOM_SLIM_NGD_DRV_NAME "qcom,slim-ngd"
  1116. static const struct of_device_id qcom_slim_ngd_dt_match[] = {
  1117. {
  1118. .compatible = "qcom,slim-ngd-v1.5.0",
  1119. .data = &ngd_v1_5_offset_info,
  1120. },
  1121. {}
  1122. };
  1123. MODULE_DEVICE_TABLE(of, qcom_slim_ngd_dt_match);
  1124. static int of_qcom_slim_ngd_register(struct device *parent,
  1125. struct qcom_slim_ngd_ctrl *ctrl)
  1126. {
  1127. const struct ngd_reg_offset_data *data;
  1128. struct qcom_slim_ngd *ngd;
  1129. const struct of_device_id *match;
  1130. struct device_node *node;
  1131. u32 id;
  1132. match = of_match_node(qcom_slim_ngd_dt_match, parent->of_node);
  1133. data = match->data;
  1134. for_each_available_child_of_node(parent->of_node, node) {
  1135. if (of_property_read_u32(node, "reg", &id))
  1136. continue;
  1137. ngd = kzalloc(sizeof(*ngd), GFP_KERNEL);
  1138. if (!ngd)
  1139. return -ENOMEM;
  1140. ngd->pdev = platform_device_alloc(QCOM_SLIM_NGD_DRV_NAME, id);
  1141. if (!ngd->pdev) {
  1142. kfree(ngd);
  1143. return -ENOMEM;
  1144. }
  1145. ngd->id = id;
  1146. ngd->pdev->dev.parent = parent;
  1147. ngd->pdev->driver_override = QCOM_SLIM_NGD_DRV_NAME;
  1148. ngd->pdev->dev.of_node = node;
  1149. ctrl->ngd = ngd;
  1150. platform_set_drvdata(ngd->pdev, ctrl);
  1151. platform_device_add(ngd->pdev);
  1152. ngd->base = ctrl->base + ngd->id * data->offset +
  1153. (ngd->id - 1) * data->size;
  1154. ctrl->ngd = ngd;
  1155. return 0;
  1156. }
  1157. return -ENODEV;
  1158. }
  1159. static int qcom_slim_ngd_probe(struct platform_device *pdev)
  1160. {
  1161. struct qcom_slim_ngd_ctrl *ctrl = platform_get_drvdata(pdev);
  1162. struct device *dev = &pdev->dev;
  1163. int ret;
  1164. ctrl->ctrl.dev = dev;
  1165. pm_runtime_use_autosuspend(dev);
  1166. pm_runtime_set_autosuspend_delay(dev, QCOM_SLIM_NGD_AUTOSUSPEND);
  1167. pm_runtime_set_suspended(dev);
  1168. pm_runtime_enable(dev);
  1169. pm_runtime_get_noresume(dev);
  1170. ret = qcom_slim_ngd_qmi_svc_event_init(ctrl);
  1171. if (ret) {
  1172. dev_err(&pdev->dev, "QMI service registration failed:%d", ret);
  1173. return ret;
  1174. }
  1175. INIT_WORK(&ctrl->m_work, qcom_slim_ngd_master_worker);
  1176. ctrl->mwq = create_singlethread_workqueue("ngd_master");
  1177. if (!ctrl->mwq) {
  1178. dev_err(&pdev->dev, "Failed to start master worker\n");
  1179. ret = -ENOMEM;
  1180. goto wq_err;
  1181. }
  1182. return 0;
  1183. wq_err:
  1184. qcom_slim_ngd_qmi_svc_event_deinit(&ctrl->qmi);
  1185. if (ctrl->mwq)
  1186. destroy_workqueue(ctrl->mwq);
  1187. return ret;
  1188. }
  1189. static int qcom_slim_ngd_ctrl_probe(struct platform_device *pdev)
  1190. {
  1191. struct device *dev = &pdev->dev;
  1192. struct qcom_slim_ngd_ctrl *ctrl;
  1193. struct resource *res;
  1194. int ret;
  1195. ctrl = devm_kzalloc(dev, sizeof(*ctrl), GFP_KERNEL);
  1196. if (!ctrl)
  1197. return -ENOMEM;
  1198. dev_set_drvdata(dev, ctrl);
  1199. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1200. ctrl->base = devm_ioremap_resource(dev, res);
  1201. if (IS_ERR(ctrl->base))
  1202. return PTR_ERR(ctrl->base);
  1203. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1204. if (!res) {
  1205. dev_err(&pdev->dev, "no slimbus IRQ resource\n");
  1206. return -ENODEV;
  1207. }
  1208. ret = devm_request_irq(dev, res->start, qcom_slim_ngd_interrupt,
  1209. IRQF_TRIGGER_HIGH, "slim-ngd", ctrl);
  1210. if (ret) {
  1211. dev_err(&pdev->dev, "request IRQ failed\n");
  1212. return ret;
  1213. }
  1214. ctrl->dev = dev;
  1215. ctrl->framer.rootfreq = SLIM_ROOT_FREQ >> 3;
  1216. ctrl->framer.superfreq =
  1217. ctrl->framer.rootfreq / SLIM_CL_PER_SUPERFRAME_DIV8;
  1218. ctrl->ctrl.a_framer = &ctrl->framer;
  1219. ctrl->ctrl.clkgear = SLIM_MAX_CLK_GEAR;
  1220. ctrl->ctrl.get_laddr = qcom_slim_ngd_get_laddr;
  1221. ctrl->ctrl.enable_stream = qcom_slim_ngd_enable_stream;
  1222. ctrl->ctrl.xfer_msg = qcom_slim_ngd_xfer_msg;
  1223. ctrl->ctrl.wakeup = NULL;
  1224. ctrl->state = QCOM_SLIM_NGD_CTRL_DOWN;
  1225. spin_lock_init(&ctrl->tx_buf_lock);
  1226. init_completion(&ctrl->reconf);
  1227. init_completion(&ctrl->qmi.qmi_comp);
  1228. platform_driver_register(&qcom_slim_ngd_driver);
  1229. return of_qcom_slim_ngd_register(dev, ctrl);
  1230. }
  1231. static int qcom_slim_ngd_ctrl_remove(struct platform_device *pdev)
  1232. {
  1233. platform_driver_unregister(&qcom_slim_ngd_driver);
  1234. return 0;
  1235. }
  1236. static int qcom_slim_ngd_remove(struct platform_device *pdev)
  1237. {
  1238. struct qcom_slim_ngd_ctrl *ctrl = platform_get_drvdata(pdev);
  1239. pm_runtime_disable(&pdev->dev);
  1240. qcom_slim_ngd_enable(ctrl, false);
  1241. qcom_slim_ngd_exit_dma(ctrl);
  1242. qcom_slim_ngd_qmi_svc_event_deinit(&ctrl->qmi);
  1243. if (ctrl->mwq)
  1244. destroy_workqueue(ctrl->mwq);
  1245. kfree(ctrl->ngd);
  1246. ctrl->ngd = NULL;
  1247. return 0;
  1248. }
  1249. static int __maybe_unused qcom_slim_ngd_runtime_idle(struct device *dev)
  1250. {
  1251. struct qcom_slim_ngd_ctrl *ctrl = dev_get_drvdata(dev);
  1252. if (ctrl->state == QCOM_SLIM_NGD_CTRL_AWAKE)
  1253. ctrl->state = QCOM_SLIM_NGD_CTRL_IDLE;
  1254. pm_request_autosuspend(dev);
  1255. return -EAGAIN;
  1256. }
  1257. static int __maybe_unused qcom_slim_ngd_runtime_suspend(struct device *dev)
  1258. {
  1259. struct qcom_slim_ngd_ctrl *ctrl = dev_get_drvdata(dev);
  1260. int ret = 0;
  1261. ret = qcom_slim_qmi_power_request(ctrl, false);
  1262. if (ret && ret != -EBUSY)
  1263. dev_info(ctrl->dev, "slim resource not idle:%d\n", ret);
  1264. if (!ret || ret == -ETIMEDOUT)
  1265. ctrl->state = QCOM_SLIM_NGD_CTRL_ASLEEP;
  1266. return ret;
  1267. }
  1268. static const struct dev_pm_ops qcom_slim_ngd_dev_pm_ops = {
  1269. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  1270. pm_runtime_force_resume)
  1271. SET_RUNTIME_PM_OPS(
  1272. qcom_slim_ngd_runtime_suspend,
  1273. qcom_slim_ngd_runtime_resume,
  1274. qcom_slim_ngd_runtime_idle
  1275. )
  1276. };
  1277. static struct platform_driver qcom_slim_ngd_ctrl_driver = {
  1278. .probe = qcom_slim_ngd_ctrl_probe,
  1279. .remove = qcom_slim_ngd_ctrl_remove,
  1280. .driver = {
  1281. .name = "qcom,slim-ngd-ctrl",
  1282. .of_match_table = qcom_slim_ngd_dt_match,
  1283. },
  1284. };
  1285. static struct platform_driver qcom_slim_ngd_driver = {
  1286. .probe = qcom_slim_ngd_probe,
  1287. .remove = qcom_slim_ngd_remove,
  1288. .driver = {
  1289. .name = QCOM_SLIM_NGD_DRV_NAME,
  1290. .pm = &qcom_slim_ngd_dev_pm_ops,
  1291. },
  1292. };
  1293. module_platform_driver(qcom_slim_ngd_ctrl_driver);
  1294. MODULE_LICENSE("GPL v2");
  1295. MODULE_DESCRIPTION("Qualcomm SLIMBus NGD controller");