qcom-geni-se-common.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (c) 2017-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #ifndef _LINUX_QCOM_GENI_SE_COMMON
  7. #define _LINUX_QCOM_GENI_SE_COMMON
  8. #include <linux/clk.h>
  9. #include <linux/dma-direction.h>
  10. #include <linux/io.h>
  11. #include <linux/dma-mapping.h>
  12. #include <linux/sched/clock.h>
  13. #include <linux/ipc_logging.h>
  14. #ifdef CONFIG_ARM64
  15. #define GENI_SE_DMA_PTR_L(ptr) ((u32)ptr)
  16. #define GENI_SE_DMA_PTR_H(ptr) ((u32)(ptr >> 32))
  17. #else
  18. #define GENI_SE_DMA_PTR_L(ptr) ((u32)ptr)
  19. #define GENI_SE_DMA_PTR_H(ptr) 0
  20. #endif
  21. #define QUPV3_TEST_BUS_EN 0x204 //write 0x11
  22. #define QUPV3_TEST_BUS_SEL 0x200 //write 0x5 [for SE index 4)
  23. #define QUPV3_TEST_BUS_REG 0x208 //Read only reg, to be read as part of dump
  24. #define IPC_LOG_KPI_PAGES (4) // KPI IPC Log size
  25. #define GENI_SE_ERR(log_ctx, print, dev, x...) do { \
  26. ipc_log_string(log_ctx, x); \
  27. if (print) { \
  28. if (dev) \
  29. dev_err((dev), x); \
  30. else \
  31. pr_err(x); \
  32. } \
  33. } while (0)
  34. #define GENI_SE_DBG(log_ctx, print, dev, x...) do { \
  35. ipc_log_string(log_ctx, x); \
  36. if (print) { \
  37. if (dev) \
  38. dev_dbg((dev), x); \
  39. else \
  40. pr_debug(x); \
  41. } \
  42. } while (0)
  43. #define DEFAULT_BUS_WIDTH (4)
  44. /* In KHz */
  45. #define DEFAULT_SE_CLK 19200
  46. #define SPI_CORE2X_VOTE 51000
  47. #define Q2SPI_CORE2X_VOTE 100000
  48. #define I2C_CORE2X_VOTE 50000
  49. #define I3C_CORE2X_VOTE 50000
  50. #define APPS_PROC_TO_QUP_VOTE 140000
  51. /* SE_DMA_GENERAL_CFG */
  52. #define SE_DMA_DEBUG_REG0 (0xE40)
  53. #define SE_DMA_TX_PTR_L (0xC30)
  54. #define SE_DMA_TX_PTR_H (0xC34)
  55. #define SE_DMA_TX_ATTR (0xC38)
  56. #define SE_DMA_TX_LEN (0xC3C)
  57. #define SE_DMA_TX_IRQ_EN (0xC48)
  58. #define SE_DMA_TX_LEN_IN (0xC54)
  59. #define GENI_SE_DMA_EOT_BUF (BIT(0))
  60. #define SE_DMA_RX_PTR_L (0xD30)
  61. #define SE_DMA_RX_PTR_H (0xD34)
  62. #define SE_DMA_RX_ATTR (0xD38)
  63. #define SE_DMA_RX_LEN (0xD3C)
  64. #define SE_DMA_RX_IRQ_EN (0xD48)
  65. #define SE_DMA_RX_LEN_IN (0xD54)
  66. #define SE_DMA_TX_IRQ_EN_SET (0xC4C)
  67. #define SE_DMA_TX_IRQ_EN_CLR (0xC50)
  68. #define SE_DMA_RX_IRQ_EN_SET (0xD4C)
  69. #define SE_DMA_RX_IRQ_EN_CLR (0xD50)
  70. #define TX_GENI_CANCEL_IRQ (BIT(14))
  71. #define SE_HW_PARAM_2 (0xE2C)
  72. /* DMA DEBUG Register fields */
  73. #define DMA_TX_ACTIVE (BIT(0))
  74. #define DMA_RX_ACTIVE (BIT(1))
  75. #define DMA_TX_STATE (GENMASK(7, 4))
  76. #define DMA_RX_STATE (GENMASK(11, 8))
  77. /* SE_IRQ_EN fields */
  78. #define DMA_RX_IRQ_EN (BIT(0))
  79. #define DMA_TX_IRQ_EN (BIT(1))
  80. #define GENI_M_IRQ_EN (BIT(2))
  81. #define GENI_S_IRQ_EN (BIT(3))
  82. #define GENI_FW_S_REVISION_RO (0x6C)
  83. #define FW_REV_VERSION_MSK (GENMASK(7, 0))
  84. /* SE_HW_PARAM_2 fields */
  85. #define GEN_HW_FSM_I2C (BIT(15))
  86. /* GENI_OUTPUT_CTRL fields */
  87. #define GENI_CFG_REG80 0x240
  88. #define GENI_IO_MUX_0_EN BIT(0)
  89. #define GENI_IO_MUX_1_EN BIT(2)
  90. /* GENI_CFG_REG80 fields */
  91. #define IO1_SEL_TX BIT(2)
  92. #define IO2_DATA_IN_SEL_PAD2 GENMASK(11, 10)
  93. #define IO3_DATA_IN_SEL_PAD2 BIT(15)
  94. #define GSI_TX_PACK_EN (BIT(0))
  95. #define GSI_RX_PACK_EN (BIT(1))
  96. #define GSI_PRESERVE_PACK (BIT(2))
  97. #define HW_VER_MAJOR_MASK GENMASK(31, 28)
  98. #define HW_VER_MAJOR_SHFT 28
  99. #define HW_VER_MINOR_MASK GENMASK(27, 16)
  100. #define HW_VER_MINOR_SHFT 16
  101. #define HW_VER_STEP_MASK GENMASK(15, 0)
  102. #define OTHER_IO_OE BIT(12)
  103. #define IO2_DATA_IN_SEL BIT(11)
  104. #define RX_DATA_IN_SEL BIT(8)
  105. #define IO_MACRO_IO3_SEL (GENMASK(7, 6))
  106. #define IO_MACRO_IO2_SEL BIT(5)
  107. #define IO_MACRO_IO0_SEL_BIT BIT(0)
  108. /**
  109. * struct kpi_time - Help to capture KPI information
  110. * @len: length of the request
  111. * @time_stamp: Time stamp of the request
  112. *
  113. * This struct used to hold length and time stamp of Tx/Rx request
  114. *
  115. */
  116. struct kpi_time {
  117. unsigned int len;
  118. unsigned long long time_stamp;
  119. };
  120. static inline int geni_se_common_resources_init(struct geni_se *se, u32 geni_to_core,
  121. u32 cpu_to_geni, u32 geni_to_ddr)
  122. {
  123. int ret;
  124. ret = geni_icc_get(se, "qup-memory");
  125. if (ret)
  126. return ret;
  127. se->icc_paths[GENI_TO_CORE].avg_bw = geni_to_core;
  128. se->icc_paths[CPU_TO_GENI].avg_bw = cpu_to_geni;
  129. se->icc_paths[GENI_TO_DDR].avg_bw = geni_to_ddr;
  130. return ret;
  131. }
  132. static inline int geni_se_common_get_proto(void __iomem *base)
  133. {
  134. int proto;
  135. proto = ((readl_relaxed(base + GENI_FW_REVISION_RO)
  136. & FW_REV_PROTOCOL_MSK) >> FW_REV_PROTOCOL_SHFT);
  137. return proto;
  138. }
  139. /**
  140. * geni_se_common_get_m_fw - Read the Firmware ver for the Main sequencer engine
  141. * @base: Base address of the serial engine's register block.
  142. *
  143. * Return: Firmware version for the Main sequencer engine
  144. */
  145. static inline int geni_se_common_get_m_fw(void __iomem *base)
  146. {
  147. int fw_ver_m;
  148. fw_ver_m = ((readl_relaxed(base + GENI_FW_REVISION_RO)
  149. & FW_REV_VERSION_MSK));
  150. return fw_ver_m;
  151. }
  152. /**
  153. * geni_se_common_get_s_fw() - Read the Firmware ver for the Secondry sequencer engine
  154. * @base: Base address of the serial engine's register block.
  155. *
  156. * Return: Firmware version for the Secondry sequencer engine
  157. */
  158. static inline int geni_se_common_get_s_fw(void __iomem *base)
  159. {
  160. int fw_ver_s;
  161. fw_ver_s = ((readl_relaxed(base + GENI_FW_S_REVISION_RO)
  162. & FW_REV_VERSION_MSK));
  163. return fw_ver_s;
  164. }
  165. /**
  166. * geni_se_common_clks_off - Disabling SE clks and common clks
  167. * @se_clk: Pointer to the SE-CLk.
  168. * @m_ahb_clk: Pointer to the SE common m_ahb_clk.
  169. * @s_ahb_clk: Pointer to the SE common s_ahb_clk.
  170. */
  171. static inline void geni_se_common_clks_off(struct clk *se_clk, struct clk *m_ahb_clk,
  172. struct clk *s_ahb_clk)
  173. {
  174. clk_disable_unprepare(se_clk);
  175. clk_disable_unprepare(m_ahb_clk);
  176. clk_disable_unprepare(s_ahb_clk);
  177. }
  178. /**
  179. * geni_se_common_clks_on - enabling SE clks and common clks
  180. * @se_clk: Pointer to the SE-CLk.
  181. * @m_ahb_clk: Pointer to the SE common m_ahb_clk.
  182. * @s_ahb_clk: Pointer to the SE common s_ahb_clk.
  183. */
  184. static inline int geni_se_common_clks_on(struct clk *se_clk, struct clk *m_ahb_clk,
  185. struct clk *s_ahb_clk)
  186. {
  187. int ret;
  188. ret = clk_prepare_enable(m_ahb_clk);
  189. if (ret)
  190. goto clks_on_err1;
  191. ret = clk_prepare_enable(s_ahb_clk);
  192. if (ret)
  193. goto clks_on_err2;
  194. ret = clk_prepare_enable(se_clk);
  195. if (ret)
  196. goto clks_on_err3;
  197. return 0;
  198. clks_on_err3:
  199. clk_disable_unprepare(s_ahb_clk);
  200. clks_on_err2:
  201. clk_disable_unprepare(m_ahb_clk);
  202. clks_on_err1:
  203. return ret;
  204. }
  205. /**
  206. * geni_write_reg() - Helper function to write into a GENI register
  207. * @value: Value to be written into the register.
  208. * @base: Base address of the serial engine's register block.
  209. * @offset: Offset within the serial engine's register block.
  210. */
  211. static inline void geni_write_reg(unsigned int value, void __iomem *base, int offset)
  212. {
  213. return writel_relaxed(value, (base + offset));
  214. }
  215. /**
  216. * geni_read_reg() - Helper function to read from a GENI register
  217. * @base: Base address of the serial engine's register block.
  218. * @offset: Offset within the serial engine's register block.
  219. *
  220. * Return: Return the contents of the register.
  221. */
  222. static inline unsigned int geni_read_reg(void __iomem *base, int offset)
  223. {
  224. return readl_relaxed(base + offset);
  225. }
  226. /**
  227. * geni_se_common_iommu_map_buf() - Map a single buffer into QUPv3 context bank
  228. * @wrapper_dev: Pointer to the corresponding QUPv3 wrapper core.
  229. * @iova: Pointer in which the mapped virtual address is stored.
  230. * @buf: Address of the buffer that needs to be mapped.
  231. * @size: Size of the buffer.
  232. * @dir: Direction of the DMA transfer.
  233. *
  234. * This function is used to map an already allocated buffer into the
  235. * QUPv3 context bank device space.
  236. *
  237. * Return: 0 on success, standard Linux error codes on failure/error.
  238. */
  239. static inline int geni_se_common_iommu_map_buf(struct device *wrapper_dev, dma_addr_t *iova,
  240. void *buf, size_t size, enum dma_data_direction dir)
  241. {
  242. if (!wrapper_dev)
  243. return -EINVAL;
  244. *iova = dma_map_single(wrapper_dev, buf, size, dir);
  245. if (dma_mapping_error(wrapper_dev, *iova))
  246. return -EIO;
  247. return 0;
  248. }
  249. /**
  250. * geni_se_common_iommu_unmap_buf() - Unmap a single buffer from QUPv3 context bank
  251. * @wrapper_dev: Pointer to the corresponding QUPv3 wrapper core.
  252. * @iova: Pointer in which the mapped virtual address is stored.
  253. * @size: Size of the buffer.
  254. * @dir: Direction of the DMA transfer.
  255. *
  256. * This function is used to unmap an already mapped buffer from the
  257. * QUPv3 context bank device space.
  258. *
  259. * Return: 0 on success, standard Linux error codes on failure/error.
  260. */
  261. static inline int geni_se_common_iommu_unmap_buf(struct device *wrapper_dev, dma_addr_t *iova,
  262. size_t size, enum dma_data_direction dir)
  263. {
  264. if (!dma_mapping_error(wrapper_dev, *iova))
  265. dma_unmap_single(wrapper_dev, *iova, size, dir);
  266. return 0;
  267. }
  268. /**
  269. * geni_se_common_iommu_alloc_buf() - Allocate & map a single buffer into QUPv3
  270. * context bank
  271. * @wrapper_dev: Pointer to the corresponding QUPv3 wrapper core.
  272. * @iova: Pointer in which the mapped virtual address is stored.
  273. * @size: Size of the buffer.
  274. *
  275. * This function is used to allocate a buffer and map it into the
  276. * QUPv3 context bank device space.
  277. *
  278. * Return: address of the buffer on success, NULL or ERR_PTR on
  279. * failure/error.
  280. */
  281. static inline void *geni_se_common_iommu_alloc_buf(struct device *wrapper_dev, dma_addr_t *iova,
  282. size_t size)
  283. {
  284. void *buf = NULL;
  285. if (!wrapper_dev || !iova || !size)
  286. return ERR_PTR(-EINVAL);
  287. *iova = DMA_MAPPING_ERROR;
  288. buf = dma_alloc_coherent(wrapper_dev, size, iova, GFP_KERNEL);
  289. return buf;
  290. }
  291. /**
  292. * geni_se_common_iommu_free_buf() - Unmap & free a single buffer from QUPv3
  293. * context bank
  294. * @wrapper_dev: Pointer to the corresponding QUPv3 wrapper core.
  295. * @iova: Pointer in which the mapped virtual address is stored.
  296. * @buf: Address of the buffer.
  297. * @size: Size of the buffer.
  298. *
  299. * This function is used to unmap and free a buffer from the
  300. * QUPv3 context bank device space.
  301. *
  302. * Return: 0 on success, standard Linux error codes on failure/error.
  303. */
  304. static inline int geni_se_common_iommu_free_buf(struct device *wrapper_dev, dma_addr_t *iova,
  305. void *buf, size_t size)
  306. {
  307. if (!wrapper_dev || !iova || !buf || !size)
  308. return -EINVAL;
  309. dma_free_coherent(wrapper_dev, size, buf, *iova);
  310. return 0;
  311. }
  312. /**
  313. * geni_se_common_rx_dma_start() - Prepare Serial Engine registers for RX DMA
  314. transfers.
  315. * @base: Base address of the SE register block.
  316. * @rx_len: Length of the RX buffer.
  317. * @rx_dma: Pointer to store the mapped DMA address.
  318. *
  319. * This function is used to prepare the Serial Engine registers for DMA RX.
  320. *
  321. * Return: None.
  322. */
  323. static inline void geni_se_common_rx_dma_start(void __iomem *base, int rx_len, dma_addr_t *rx_dma)
  324. {
  325. if (!*rx_dma || !base || !rx_len)
  326. return;
  327. geni_write_reg(7, base, SE_DMA_RX_IRQ_EN_SET);
  328. geni_write_reg(GENI_SE_DMA_PTR_L(*rx_dma), base, SE_DMA_RX_PTR_L);
  329. geni_write_reg(GENI_SE_DMA_PTR_H(*rx_dma), base, SE_DMA_RX_PTR_H);
  330. /* RX does not have EOT bit */
  331. geni_write_reg(0, base, SE_DMA_RX_ATTR);
  332. /* Ensure that above register writes went through */
  333. mb();
  334. geni_write_reg(rx_len, base, SE_DMA_RX_LEN);
  335. }
  336. /**
  337. * geni_se_common_get_major_minor_num() - Split qup hw_version into
  338. major, minor and step.
  339. * @hw_version: HW version of the qup
  340. * @major: Buffer for Major Version field.
  341. * @minor: Buffer for Minor Version field.
  342. * @step: Buffer for Step Version field.
  343. *
  344. * Return: None
  345. */
  346. static inline void geni_se_common_get_major_minor_num(u32 hw_version,
  347. unsigned int *major, unsigned int *minor, unsigned int *step)
  348. {
  349. *major = (hw_version & HW_VER_MAJOR_MASK) >> HW_VER_MAJOR_SHFT;
  350. *minor = (hw_version & HW_VER_MINOR_MASK) >> HW_VER_MINOR_SHFT;
  351. *step = hw_version & HW_VER_STEP_MASK;
  352. }
  353. /*
  354. * test_bus_enable_per_qupv3: enables particular test bus number.
  355. * @wrapper_dev: QUPV3 common driver handle from SE driver
  356. *
  357. * Note: Need to call only once.
  358. *
  359. * Return: none
  360. */
  361. static inline void test_bus_enable_per_qupv3(struct device *wrapper_dev, void *ipc)
  362. {
  363. struct geni_se *geni_se_dev;
  364. geni_se_dev = dev_get_drvdata(wrapper_dev);
  365. //Enablement of test bus is required only once.
  366. //TEST_BUS_EN:4, TEST_BUS_REG_EN:0
  367. geni_write_reg(0x11, geni_se_dev->base, QUPV3_TEST_BUS_EN);
  368. GENI_SE_ERR(ipc, false, geni_se_dev->dev,
  369. "%s: TEST_BUS_EN: 0x%x @address:0x%x\n",
  370. __func__, geni_read_reg(geni_se_dev->base, QUPV3_TEST_BUS_EN),
  371. (geni_se_dev->base + QUPV3_TEST_BUS_EN));
  372. }
  373. /*
  374. * test_bus_select_per_qupv3: Selects the test bus as required
  375. * @wrapper_dev: QUPV3 common driver handle from SE driver
  376. * @test_bus_num: GENI SE number from QUPV3 core. E.g. SE0 should pass value 1.
  377. *
  378. * @Return: None
  379. */
  380. static inline void test_bus_select_per_qupv3(struct device *wrapper_dev, u8 test_bus_num, void *ipc)
  381. {
  382. struct geni_se *geni_se_dev;
  383. geni_se_dev = dev_get_drvdata(wrapper_dev);
  384. geni_write_reg(test_bus_num, geni_se_dev->base, QUPV3_TEST_BUS_SEL);
  385. GENI_SE_ERR(ipc, false, geni_se_dev->dev,
  386. "%s: readback TEST_BUS_SEL: 0x%x @address:0x%x\n",
  387. __func__, geni_read_reg(geni_se_dev->base, QUPV3_TEST_BUS_SEL),
  388. (geni_se_dev->base + QUPV3_TEST_BUS_SEL));
  389. }
  390. /*
  391. * test_bus_read_per_qupv3: Selects the test bus as required
  392. * @wrapper_dev: QUPV3 common driver handle from SE driver
  393. *
  394. * Return: None
  395. */
  396. static inline void test_bus_read_per_qupv3(struct device *wrapper_dev, void *ipc)
  397. {
  398. struct geni_se *geni_se_dev;
  399. geni_se_dev = dev_get_drvdata(wrapper_dev);
  400. GENI_SE_ERR(ipc, false, geni_se_dev->dev,
  401. "%s: dump QUPV3_TEST_BUS_REG:0x%x\n",
  402. __func__, geni_read_reg(geni_se_dev->base, QUPV3_TEST_BUS_REG));
  403. }
  404. /**
  405. * geni_capture_start_time() - Used to capture start time of a function.
  406. * @se: serial engine device
  407. * @ipc: which IPC module to be used to log.
  408. * @func: for which function start time is captured.
  409. * @geni_kpi_capture_enabled: kpi capture enable flag to start capture the logs or not.
  410. *
  411. * Return: start time if kpi geni_kpi_capture_enabled flag enabled or error value.
  412. */
  413. static inline unsigned long long geni_capture_start_time(struct geni_se *se, void *ipc,
  414. const char *func,
  415. int geni_kpi_capture_enabled)
  416. {
  417. struct device *dev = se->dev;
  418. unsigned long long start_time = 0;
  419. if (!ipc)
  420. return -EINVAL;
  421. if (geni_kpi_capture_enabled) {
  422. start_time = sched_clock();
  423. GENI_SE_ERR(ipc, false, dev,
  424. "%s:start at %llu nsec(%llu usec)\n", func,
  425. start_time, (start_time / 1000));
  426. }
  427. return start_time;
  428. }
  429. /**
  430. * geni_capture_stop_time() - Logs the function execution time
  431. * @se: serial engine device
  432. * @ipc: which IPC module to be used to log.
  433. * @func: for which function kpi capture is used.
  434. * @geni_kpi_capture_enabled: kpi capture enable flag to start capture the logs or not.
  435. * @start_time: start time of the function
  436. * @len: Number of bytes of transfer
  437. * @freq: frequency of operation
  438. * Return: None
  439. */
  440. static inline void geni_capture_stop_time(struct geni_se *se, void *ipc,
  441. const char *func, int geni_kpi_capture_enabled,
  442. unsigned long long start_time, unsigned int len,
  443. unsigned int freq)
  444. {
  445. struct device *dev = se->dev;
  446. unsigned long long exec_time = 0;
  447. if (!ipc)
  448. return;
  449. if (geni_kpi_capture_enabled && start_time) {
  450. exec_time = sched_clock() - start_time;
  451. if (!len)
  452. GENI_SE_ERR(ipc, false, dev,
  453. "%s:took %llu nsec(%llu usec)\n",
  454. func, exec_time, (exec_time / 1000));
  455. else if (len != 0 && freq != 0)
  456. GENI_SE_ERR(ipc, false, dev,
  457. "%s:took %llu nsec(%llu usec) for %u bytes with freq %u\n",
  458. func, exec_time, (exec_time / 1000), len, freq);
  459. else
  460. GENI_SE_ERR(ipc, false, dev,
  461. "%s:took %llu nsec(%llu usec) for %u bytes\n", func,
  462. exec_time, (exec_time / 1000), len);
  463. }
  464. }
  465. #endif