qcom-geni-se.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2017-2018, The Linux Foundation. All rights reserved.
  3. #include <linux/clk.h>
  4. #include <linux/slab.h>
  5. #include <linux/dma-mapping.h>
  6. #include <linux/io.h>
  7. #include <linux/module.h>
  8. #include <linux/of.h>
  9. #include <linux/of_platform.h>
  10. #include <linux/pinctrl/consumer.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/qcom-geni-se.h>
  13. /**
  14. * DOC: Overview
  15. *
  16. * Generic Interface (GENI) Serial Engine (SE) Wrapper driver is introduced
  17. * to manage GENI firmware based Qualcomm Universal Peripheral (QUP) Wrapper
  18. * controller. QUP Wrapper is designed to support various serial bus protocols
  19. * like UART, SPI, I2C, I3C, etc.
  20. */
  21. /**
  22. * DOC: Hardware description
  23. *
  24. * GENI based QUP is a highly-flexible and programmable module for supporting
  25. * a wide range of serial interfaces like UART, SPI, I2C, I3C, etc. A single
  26. * QUP module can provide upto 8 serial interfaces, using its internal
  27. * serial engines. The actual configuration is determined by the target
  28. * platform configuration. The protocol supported by each interface is
  29. * determined by the firmware loaded to the serial engine. Each SE consists
  30. * of a DMA Engine and GENI sub modules which enable serial engines to
  31. * support FIFO and DMA modes of operation.
  32. *
  33. *
  34. * +-----------------------------------------+
  35. * |QUP Wrapper |
  36. * | +----------------------------+ |
  37. * --QUP & SE Clocks--> | Serial Engine N | +-IO------>
  38. * | | ... | | Interface
  39. * <---Clock Perf.----+ +----+-----------------------+ | |
  40. * State Interface | | Serial Engine 1 | | |
  41. * | | | | |
  42. * | | | | |
  43. * <--------AHB-------> | | | |
  44. * | | +----+ |
  45. * | | | |
  46. * | | | |
  47. * <------SE IRQ------+ +----------------------------+ |
  48. * | |
  49. * +-----------------------------------------+
  50. *
  51. * Figure 1: GENI based QUP Wrapper
  52. *
  53. * The GENI submodules include primary and secondary sequencers which are
  54. * used to drive TX & RX operations. On serial interfaces that operate using
  55. * master-slave model, primary sequencer drives both TX & RX operations. On
  56. * serial interfaces that operate using peer-to-peer model, primary sequencer
  57. * drives TX operation and secondary sequencer drives RX operation.
  58. */
  59. /**
  60. * DOC: Software description
  61. *
  62. * GENI SE Wrapper driver is structured into 2 parts:
  63. *
  64. * geni_wrapper represents QUP Wrapper controller. This part of the driver
  65. * manages QUP Wrapper information such as hardware version, clock
  66. * performance table that is common to all the internal serial engines.
  67. *
  68. * geni_se represents serial engine. This part of the driver manages serial
  69. * engine information such as clocks, containing QUP Wrapper, etc. This part
  70. * of driver also supports operations (eg. initialize the concerned serial
  71. * engine, select between FIFO and DMA mode of operation etc.) that are
  72. * common to all the serial engines and are independent of serial interfaces.
  73. */
  74. #define MAX_CLK_PERF_LEVEL 32
  75. #define NUM_AHB_CLKS 2
  76. /**
  77. * @struct geni_wrapper - Data structure to represent the QUP Wrapper Core
  78. * @dev: Device pointer of the QUP wrapper core
  79. * @base: Base address of this instance of QUP wrapper core
  80. * @ahb_clks: Handle to the primary & secondary AHB clocks
  81. */
  82. struct geni_wrapper {
  83. struct device *dev;
  84. void __iomem *base;
  85. struct clk_bulk_data ahb_clks[NUM_AHB_CLKS];
  86. };
  87. #define QUP_HW_VER_REG 0x4
  88. /* Common SE registers */
  89. #define GENI_INIT_CFG_REVISION 0x0
  90. #define GENI_S_INIT_CFG_REVISION 0x4
  91. #define GENI_OUTPUT_CTRL 0x24
  92. #define GENI_CGC_CTRL 0x28
  93. #define GENI_CLK_CTRL_RO 0x60
  94. #define GENI_IF_DISABLE_RO 0x64
  95. #define GENI_FW_S_REVISION_RO 0x6c
  96. #define SE_GENI_BYTE_GRAN 0x254
  97. #define SE_GENI_TX_PACKING_CFG0 0x260
  98. #define SE_GENI_TX_PACKING_CFG1 0x264
  99. #define SE_GENI_RX_PACKING_CFG0 0x284
  100. #define SE_GENI_RX_PACKING_CFG1 0x288
  101. #define SE_GENI_M_GP_LENGTH 0x910
  102. #define SE_GENI_S_GP_LENGTH 0x914
  103. #define SE_DMA_TX_PTR_L 0xc30
  104. #define SE_DMA_TX_PTR_H 0xc34
  105. #define SE_DMA_TX_ATTR 0xc38
  106. #define SE_DMA_TX_LEN 0xc3c
  107. #define SE_DMA_TX_IRQ_EN 0xc48
  108. #define SE_DMA_TX_IRQ_EN_SET 0xc4c
  109. #define SE_DMA_TX_IRQ_EN_CLR 0xc50
  110. #define SE_DMA_TX_LEN_IN 0xc54
  111. #define SE_DMA_TX_MAX_BURST 0xc5c
  112. #define SE_DMA_RX_PTR_L 0xd30
  113. #define SE_DMA_RX_PTR_H 0xd34
  114. #define SE_DMA_RX_ATTR 0xd38
  115. #define SE_DMA_RX_LEN 0xd3c
  116. #define SE_DMA_RX_IRQ_EN 0xd48
  117. #define SE_DMA_RX_IRQ_EN_SET 0xd4c
  118. #define SE_DMA_RX_IRQ_EN_CLR 0xd50
  119. #define SE_DMA_RX_LEN_IN 0xd54
  120. #define SE_DMA_RX_MAX_BURST 0xd5c
  121. #define SE_DMA_RX_FLUSH 0xd60
  122. #define SE_GSI_EVENT_EN 0xe18
  123. #define SE_IRQ_EN 0xe1c
  124. #define SE_DMA_GENERAL_CFG 0xe30
  125. /* GENI_OUTPUT_CTRL fields */
  126. #define DEFAULT_IO_OUTPUT_CTRL_MSK GENMASK(6, 0)
  127. /* GENI_CGC_CTRL fields */
  128. #define CFG_AHB_CLK_CGC_ON BIT(0)
  129. #define CFG_AHB_WR_ACLK_CGC_ON BIT(1)
  130. #define DATA_AHB_CLK_CGC_ON BIT(2)
  131. #define SCLK_CGC_ON BIT(3)
  132. #define TX_CLK_CGC_ON BIT(4)
  133. #define RX_CLK_CGC_ON BIT(5)
  134. #define EXT_CLK_CGC_ON BIT(6)
  135. #define PROG_RAM_HCLK_OFF BIT(8)
  136. #define PROG_RAM_SCLK_OFF BIT(9)
  137. #define DEFAULT_CGC_EN GENMASK(6, 0)
  138. /* SE_GSI_EVENT_EN fields */
  139. #define DMA_RX_EVENT_EN BIT(0)
  140. #define DMA_TX_EVENT_EN BIT(1)
  141. #define GENI_M_EVENT_EN BIT(2)
  142. #define GENI_S_EVENT_EN BIT(3)
  143. /* SE_IRQ_EN fields */
  144. #define DMA_RX_IRQ_EN BIT(0)
  145. #define DMA_TX_IRQ_EN BIT(1)
  146. #define GENI_M_IRQ_EN BIT(2)
  147. #define GENI_S_IRQ_EN BIT(3)
  148. /* SE_DMA_GENERAL_CFG */
  149. #define DMA_RX_CLK_CGC_ON BIT(0)
  150. #define DMA_TX_CLK_CGC_ON BIT(1)
  151. #define DMA_AHB_SLV_CFG_ON BIT(2)
  152. #define AHB_SEC_SLV_CLK_CGC_ON BIT(3)
  153. #define DUMMY_RX_NON_BUFFERABLE BIT(4)
  154. #define RX_DMA_ZERO_PADDING_EN BIT(5)
  155. #define RX_DMA_IRQ_DELAY_MSK GENMASK(8, 6)
  156. #define RX_DMA_IRQ_DELAY_SHFT 6
  157. /**
  158. * geni_se_get_qup_hw_version() - Read the QUP wrapper Hardware version
  159. * @se: Pointer to the corresponding serial engine.
  160. *
  161. * Return: Hardware Version of the wrapper.
  162. */
  163. u32 geni_se_get_qup_hw_version(struct geni_se *se)
  164. {
  165. struct geni_wrapper *wrapper = se->wrapper;
  166. return readl_relaxed(wrapper->base + QUP_HW_VER_REG);
  167. }
  168. EXPORT_SYMBOL(geni_se_get_qup_hw_version);
  169. static void geni_se_io_set_mode(void __iomem *base)
  170. {
  171. u32 val;
  172. val = readl_relaxed(base + SE_IRQ_EN);
  173. val |= GENI_M_IRQ_EN | GENI_S_IRQ_EN;
  174. val |= DMA_TX_IRQ_EN | DMA_RX_IRQ_EN;
  175. writel_relaxed(val, base + SE_IRQ_EN);
  176. val = readl_relaxed(base + SE_GENI_DMA_MODE_EN);
  177. val &= ~GENI_DMA_MODE_EN;
  178. writel_relaxed(val, base + SE_GENI_DMA_MODE_EN);
  179. writel_relaxed(0, base + SE_GSI_EVENT_EN);
  180. }
  181. static void geni_se_io_init(void __iomem *base)
  182. {
  183. u32 val;
  184. val = readl_relaxed(base + GENI_CGC_CTRL);
  185. val |= DEFAULT_CGC_EN;
  186. writel_relaxed(val, base + GENI_CGC_CTRL);
  187. val = readl_relaxed(base + SE_DMA_GENERAL_CFG);
  188. val |= AHB_SEC_SLV_CLK_CGC_ON | DMA_AHB_SLV_CFG_ON;
  189. val |= DMA_TX_CLK_CGC_ON | DMA_RX_CLK_CGC_ON;
  190. writel_relaxed(val, base + SE_DMA_GENERAL_CFG);
  191. writel_relaxed(DEFAULT_IO_OUTPUT_CTRL_MSK, base + GENI_OUTPUT_CTRL);
  192. writel_relaxed(FORCE_DEFAULT, base + GENI_FORCE_DEFAULT_REG);
  193. }
  194. /**
  195. * geni_se_init() - Initialize the GENI serial engine
  196. * @se: Pointer to the concerned serial engine.
  197. * @rx_wm: Receive watermark, in units of FIFO words.
  198. * @rx_rfr_wm: Ready-for-receive watermark, in units of FIFO words.
  199. *
  200. * This function is used to initialize the GENI serial engine, configure
  201. * receive watermark and ready-for-receive watermarks.
  202. */
  203. void geni_se_init(struct geni_se *se, u32 rx_wm, u32 rx_rfr)
  204. {
  205. u32 val;
  206. geni_se_io_init(se->base);
  207. geni_se_io_set_mode(se->base);
  208. writel_relaxed(rx_wm, se->base + SE_GENI_RX_WATERMARK_REG);
  209. writel_relaxed(rx_rfr, se->base + SE_GENI_RX_RFR_WATERMARK_REG);
  210. val = readl_relaxed(se->base + SE_GENI_M_IRQ_EN);
  211. val |= M_COMMON_GENI_M_IRQ_EN;
  212. writel_relaxed(val, se->base + SE_GENI_M_IRQ_EN);
  213. val = readl_relaxed(se->base + SE_GENI_S_IRQ_EN);
  214. val |= S_COMMON_GENI_S_IRQ_EN;
  215. writel_relaxed(val, se->base + SE_GENI_S_IRQ_EN);
  216. }
  217. EXPORT_SYMBOL(geni_se_init);
  218. static void geni_se_select_fifo_mode(struct geni_se *se)
  219. {
  220. u32 proto = geni_se_read_proto(se);
  221. u32 val;
  222. writel_relaxed(0, se->base + SE_GSI_EVENT_EN);
  223. writel_relaxed(0xffffffff, se->base + SE_GENI_M_IRQ_CLEAR);
  224. writel_relaxed(0xffffffff, se->base + SE_GENI_S_IRQ_CLEAR);
  225. writel_relaxed(0xffffffff, se->base + SE_DMA_TX_IRQ_CLR);
  226. writel_relaxed(0xffffffff, se->base + SE_DMA_RX_IRQ_CLR);
  227. writel_relaxed(0xffffffff, se->base + SE_IRQ_EN);
  228. val = readl_relaxed(se->base + SE_GENI_M_IRQ_EN);
  229. if (proto != GENI_SE_UART) {
  230. val |= M_CMD_DONE_EN | M_TX_FIFO_WATERMARK_EN;
  231. val |= M_RX_FIFO_WATERMARK_EN | M_RX_FIFO_LAST_EN;
  232. }
  233. writel_relaxed(val, se->base + SE_GENI_M_IRQ_EN);
  234. val = readl_relaxed(se->base + SE_GENI_S_IRQ_EN);
  235. if (proto != GENI_SE_UART)
  236. val |= S_CMD_DONE_EN;
  237. writel_relaxed(val, se->base + SE_GENI_S_IRQ_EN);
  238. val = readl_relaxed(se->base + SE_GENI_DMA_MODE_EN);
  239. val &= ~GENI_DMA_MODE_EN;
  240. writel_relaxed(val, se->base + SE_GENI_DMA_MODE_EN);
  241. }
  242. static void geni_se_select_dma_mode(struct geni_se *se)
  243. {
  244. u32 proto = geni_se_read_proto(se);
  245. u32 val;
  246. writel_relaxed(0, se->base + SE_GSI_EVENT_EN);
  247. writel_relaxed(0xffffffff, se->base + SE_GENI_M_IRQ_CLEAR);
  248. writel_relaxed(0xffffffff, se->base + SE_GENI_S_IRQ_CLEAR);
  249. writel_relaxed(0xffffffff, se->base + SE_DMA_TX_IRQ_CLR);
  250. writel_relaxed(0xffffffff, se->base + SE_DMA_RX_IRQ_CLR);
  251. writel_relaxed(0xffffffff, se->base + SE_IRQ_EN);
  252. val = readl_relaxed(se->base + SE_GENI_M_IRQ_EN);
  253. if (proto != GENI_SE_UART) {
  254. val &= ~(M_CMD_DONE_EN | M_TX_FIFO_WATERMARK_EN);
  255. val &= ~(M_RX_FIFO_WATERMARK_EN | M_RX_FIFO_LAST_EN);
  256. }
  257. writel_relaxed(val, se->base + SE_GENI_M_IRQ_EN);
  258. val = readl_relaxed(se->base + SE_GENI_S_IRQ_EN);
  259. if (proto != GENI_SE_UART)
  260. val &= ~S_CMD_DONE_EN;
  261. writel_relaxed(val, se->base + SE_GENI_S_IRQ_EN);
  262. val = readl_relaxed(se->base + SE_GENI_DMA_MODE_EN);
  263. val |= GENI_DMA_MODE_EN;
  264. writel_relaxed(val, se->base + SE_GENI_DMA_MODE_EN);
  265. }
  266. /**
  267. * geni_se_select_mode() - Select the serial engine transfer mode
  268. * @se: Pointer to the concerned serial engine.
  269. * @mode: Transfer mode to be selected.
  270. */
  271. void geni_se_select_mode(struct geni_se *se, enum geni_se_xfer_mode mode)
  272. {
  273. WARN_ON(mode != GENI_SE_FIFO && mode != GENI_SE_DMA);
  274. switch (mode) {
  275. case GENI_SE_FIFO:
  276. geni_se_select_fifo_mode(se);
  277. break;
  278. case GENI_SE_DMA:
  279. geni_se_select_dma_mode(se);
  280. break;
  281. case GENI_SE_INVALID:
  282. default:
  283. break;
  284. }
  285. }
  286. EXPORT_SYMBOL(geni_se_select_mode);
  287. /**
  288. * DOC: Overview
  289. *
  290. * GENI FIFO packing is highly configurable. TX/RX packing/unpacking consist
  291. * of up to 4 operations, each operation represented by 4 configuration vectors
  292. * of 10 bits programmed in GENI_TX_PACKING_CFG0 and GENI_TX_PACKING_CFG1 for
  293. * TX FIFO and in GENI_RX_PACKING_CFG0 and GENI_RX_PACKING_CFG1 for RX FIFO.
  294. * Refer to below examples for detailed bit-field description.
  295. *
  296. * Example 1: word_size = 7, packing_mode = 4 x 8, msb_to_lsb = 1
  297. *
  298. * +-----------+-------+-------+-------+-------+
  299. * | | vec_0 | vec_1 | vec_2 | vec_3 |
  300. * +-----------+-------+-------+-------+-------+
  301. * | start | 0x6 | 0xe | 0x16 | 0x1e |
  302. * | direction | 1 | 1 | 1 | 1 |
  303. * | length | 6 | 6 | 6 | 6 |
  304. * | stop | 0 | 0 | 0 | 1 |
  305. * +-----------+-------+-------+-------+-------+
  306. *
  307. * Example 2: word_size = 15, packing_mode = 2 x 16, msb_to_lsb = 0
  308. *
  309. * +-----------+-------+-------+-------+-------+
  310. * | | vec_0 | vec_1 | vec_2 | vec_3 |
  311. * +-----------+-------+-------+-------+-------+
  312. * | start | 0x0 | 0x8 | 0x10 | 0x18 |
  313. * | direction | 0 | 0 | 0 | 0 |
  314. * | length | 7 | 6 | 7 | 6 |
  315. * | stop | 0 | 0 | 0 | 1 |
  316. * +-----------+-------+-------+-------+-------+
  317. *
  318. * Example 3: word_size = 23, packing_mode = 1 x 32, msb_to_lsb = 1
  319. *
  320. * +-----------+-------+-------+-------+-------+
  321. * | | vec_0 | vec_1 | vec_2 | vec_3 |
  322. * +-----------+-------+-------+-------+-------+
  323. * | start | 0x16 | 0xe | 0x6 | 0x0 |
  324. * | direction | 1 | 1 | 1 | 1 |
  325. * | length | 7 | 7 | 6 | 0 |
  326. * | stop | 0 | 0 | 1 | 0 |
  327. * +-----------+-------+-------+-------+-------+
  328. *
  329. */
  330. #define NUM_PACKING_VECTORS 4
  331. #define PACKING_START_SHIFT 5
  332. #define PACKING_DIR_SHIFT 4
  333. #define PACKING_LEN_SHIFT 1
  334. #define PACKING_STOP_BIT BIT(0)
  335. #define PACKING_VECTOR_SHIFT 10
  336. /**
  337. * geni_se_config_packing() - Packing configuration of the serial engine
  338. * @se: Pointer to the concerned serial engine
  339. * @bpw: Bits of data per transfer word.
  340. * @pack_words: Number of words per fifo element.
  341. * @msb_to_lsb: Transfer from MSB to LSB or vice-versa.
  342. * @tx_cfg: Flag to configure the TX Packing.
  343. * @rx_cfg: Flag to configure the RX Packing.
  344. *
  345. * This function is used to configure the packing rules for the current
  346. * transfer.
  347. */
  348. void geni_se_config_packing(struct geni_se *se, int bpw, int pack_words,
  349. bool msb_to_lsb, bool tx_cfg, bool rx_cfg)
  350. {
  351. u32 cfg0, cfg1, cfg[NUM_PACKING_VECTORS] = {0};
  352. int len;
  353. int temp_bpw = bpw;
  354. int idx_start = msb_to_lsb ? bpw - 1 : 0;
  355. int idx = idx_start;
  356. int idx_delta = msb_to_lsb ? -BITS_PER_BYTE : BITS_PER_BYTE;
  357. int ceil_bpw = ALIGN(bpw, BITS_PER_BYTE);
  358. int iter = (ceil_bpw * pack_words) / BITS_PER_BYTE;
  359. int i;
  360. if (iter <= 0 || iter > NUM_PACKING_VECTORS)
  361. return;
  362. for (i = 0; i < iter; i++) {
  363. len = min_t(int, temp_bpw, BITS_PER_BYTE) - 1;
  364. cfg[i] = idx << PACKING_START_SHIFT;
  365. cfg[i] |= msb_to_lsb << PACKING_DIR_SHIFT;
  366. cfg[i] |= len << PACKING_LEN_SHIFT;
  367. if (temp_bpw <= BITS_PER_BYTE) {
  368. idx = ((i + 1) * BITS_PER_BYTE) + idx_start;
  369. temp_bpw = bpw;
  370. } else {
  371. idx = idx + idx_delta;
  372. temp_bpw = temp_bpw - BITS_PER_BYTE;
  373. }
  374. }
  375. cfg[iter - 1] |= PACKING_STOP_BIT;
  376. cfg0 = cfg[0] | (cfg[1] << PACKING_VECTOR_SHIFT);
  377. cfg1 = cfg[2] | (cfg[3] << PACKING_VECTOR_SHIFT);
  378. if (tx_cfg) {
  379. writel_relaxed(cfg0, se->base + SE_GENI_TX_PACKING_CFG0);
  380. writel_relaxed(cfg1, se->base + SE_GENI_TX_PACKING_CFG1);
  381. }
  382. if (rx_cfg) {
  383. writel_relaxed(cfg0, se->base + SE_GENI_RX_PACKING_CFG0);
  384. writel_relaxed(cfg1, se->base + SE_GENI_RX_PACKING_CFG1);
  385. }
  386. /*
  387. * Number of protocol words in each FIFO entry
  388. * 0 - 4x8, four words in each entry, max word size of 8 bits
  389. * 1 - 2x16, two words in each entry, max word size of 16 bits
  390. * 2 - 1x32, one word in each entry, max word size of 32 bits
  391. * 3 - undefined
  392. */
  393. if (pack_words || bpw == 32)
  394. writel_relaxed(bpw / 16, se->base + SE_GENI_BYTE_GRAN);
  395. }
  396. EXPORT_SYMBOL(geni_se_config_packing);
  397. static void geni_se_clks_off(struct geni_se *se)
  398. {
  399. struct geni_wrapper *wrapper = se->wrapper;
  400. clk_disable_unprepare(se->clk);
  401. clk_bulk_disable_unprepare(ARRAY_SIZE(wrapper->ahb_clks),
  402. wrapper->ahb_clks);
  403. }
  404. /**
  405. * geni_se_resources_off() - Turn off resources associated with the serial
  406. * engine
  407. * @se: Pointer to the concerned serial engine.
  408. *
  409. * Return: 0 on success, standard Linux error codes on failure/error.
  410. */
  411. int geni_se_resources_off(struct geni_se *se)
  412. {
  413. int ret;
  414. ret = pinctrl_pm_select_sleep_state(se->dev);
  415. if (ret)
  416. return ret;
  417. geni_se_clks_off(se);
  418. return 0;
  419. }
  420. EXPORT_SYMBOL(geni_se_resources_off);
  421. static int geni_se_clks_on(struct geni_se *se)
  422. {
  423. int ret;
  424. struct geni_wrapper *wrapper = se->wrapper;
  425. ret = clk_bulk_prepare_enable(ARRAY_SIZE(wrapper->ahb_clks),
  426. wrapper->ahb_clks);
  427. if (ret)
  428. return ret;
  429. ret = clk_prepare_enable(se->clk);
  430. if (ret)
  431. clk_bulk_disable_unprepare(ARRAY_SIZE(wrapper->ahb_clks),
  432. wrapper->ahb_clks);
  433. return ret;
  434. }
  435. /**
  436. * geni_se_resources_on() - Turn on resources associated with the serial
  437. * engine
  438. * @se: Pointer to the concerned serial engine.
  439. *
  440. * Return: 0 on success, standard Linux error codes on failure/error.
  441. */
  442. int geni_se_resources_on(struct geni_se *se)
  443. {
  444. int ret;
  445. ret = geni_se_clks_on(se);
  446. if (ret)
  447. return ret;
  448. ret = pinctrl_pm_select_default_state(se->dev);
  449. if (ret)
  450. geni_se_clks_off(se);
  451. return ret;
  452. }
  453. EXPORT_SYMBOL(geni_se_resources_on);
  454. /**
  455. * geni_se_clk_tbl_get() - Get the clock table to program DFS
  456. * @se: Pointer to the concerned serial engine.
  457. * @tbl: Table in which the output is returned.
  458. *
  459. * This function is called by the protocol drivers to determine the different
  460. * clock frequencies supported by serial engine core clock. The protocol
  461. * drivers use the output to determine the clock frequency index to be
  462. * programmed into DFS.
  463. *
  464. * Return: number of valid performance levels in the table on success,
  465. * standard Linux error codes on failure.
  466. */
  467. int geni_se_clk_tbl_get(struct geni_se *se, unsigned long **tbl)
  468. {
  469. long freq = 0;
  470. int i;
  471. if (se->clk_perf_tbl) {
  472. *tbl = se->clk_perf_tbl;
  473. return se->num_clk_levels;
  474. }
  475. se->clk_perf_tbl = devm_kcalloc(se->dev, MAX_CLK_PERF_LEVEL,
  476. sizeof(*se->clk_perf_tbl),
  477. GFP_KERNEL);
  478. if (!se->clk_perf_tbl)
  479. return -ENOMEM;
  480. for (i = 0; i < MAX_CLK_PERF_LEVEL; i++) {
  481. freq = clk_round_rate(se->clk, freq + 1);
  482. if (freq <= 0 || freq == se->clk_perf_tbl[i - 1])
  483. break;
  484. se->clk_perf_tbl[i] = freq;
  485. }
  486. se->num_clk_levels = i;
  487. *tbl = se->clk_perf_tbl;
  488. return se->num_clk_levels;
  489. }
  490. EXPORT_SYMBOL(geni_se_clk_tbl_get);
  491. /**
  492. * geni_se_clk_freq_match() - Get the matching or closest SE clock frequency
  493. * @se: Pointer to the concerned serial engine.
  494. * @req_freq: Requested clock frequency.
  495. * @index: Index of the resultant frequency in the table.
  496. * @res_freq: Resultant frequency of the source clock.
  497. * @exact: Flag to indicate exact multiple requirement of the requested
  498. * frequency.
  499. *
  500. * This function is called by the protocol drivers to determine the best match
  501. * of the requested frequency as provided by the serial engine clock in order
  502. * to meet the performance requirements.
  503. *
  504. * If we return success:
  505. * - if @exact is true then @res_freq / <an_integer> == @req_freq
  506. * - if @exact is false then @res_freq / <an_integer> <= @req_freq
  507. *
  508. * Return: 0 on success, standard Linux error codes on failure.
  509. */
  510. int geni_se_clk_freq_match(struct geni_se *se, unsigned long req_freq,
  511. unsigned int *index, unsigned long *res_freq,
  512. bool exact)
  513. {
  514. unsigned long *tbl;
  515. int num_clk_levels;
  516. int i;
  517. unsigned long best_delta;
  518. unsigned long new_delta;
  519. unsigned int divider;
  520. num_clk_levels = geni_se_clk_tbl_get(se, &tbl);
  521. if (num_clk_levels < 0)
  522. return num_clk_levels;
  523. if (num_clk_levels == 0)
  524. return -EINVAL;
  525. best_delta = ULONG_MAX;
  526. for (i = 0; i < num_clk_levels; i++) {
  527. divider = DIV_ROUND_UP(tbl[i], req_freq);
  528. new_delta = req_freq - tbl[i] / divider;
  529. if (new_delta < best_delta) {
  530. /* We have a new best! */
  531. *index = i;
  532. *res_freq = tbl[i];
  533. /* If the new best is exact then we're done */
  534. if (new_delta == 0)
  535. return 0;
  536. /* Record how close we got */
  537. best_delta = new_delta;
  538. }
  539. }
  540. if (exact)
  541. return -EINVAL;
  542. return 0;
  543. }
  544. EXPORT_SYMBOL(geni_se_clk_freq_match);
  545. #define GENI_SE_DMA_DONE_EN BIT(0)
  546. #define GENI_SE_DMA_EOT_EN BIT(1)
  547. #define GENI_SE_DMA_AHB_ERR_EN BIT(2)
  548. #define GENI_SE_DMA_EOT_BUF BIT(0)
  549. /**
  550. * geni_se_tx_dma_prep() - Prepare the serial engine for TX DMA transfer
  551. * @se: Pointer to the concerned serial engine.
  552. * @buf: Pointer to the TX buffer.
  553. * @len: Length of the TX buffer.
  554. * @iova: Pointer to store the mapped DMA address.
  555. *
  556. * This function is used to prepare the buffers for DMA TX.
  557. *
  558. * Return: 0 on success, standard Linux error codes on failure.
  559. */
  560. int geni_se_tx_dma_prep(struct geni_se *se, void *buf, size_t len,
  561. dma_addr_t *iova)
  562. {
  563. struct geni_wrapper *wrapper = se->wrapper;
  564. u32 val;
  565. *iova = dma_map_single(wrapper->dev, buf, len, DMA_TO_DEVICE);
  566. if (dma_mapping_error(wrapper->dev, *iova))
  567. return -EIO;
  568. val = GENI_SE_DMA_DONE_EN;
  569. val |= GENI_SE_DMA_EOT_EN;
  570. val |= GENI_SE_DMA_AHB_ERR_EN;
  571. writel_relaxed(val, se->base + SE_DMA_TX_IRQ_EN_SET);
  572. writel_relaxed(lower_32_bits(*iova), se->base + SE_DMA_TX_PTR_L);
  573. writel_relaxed(upper_32_bits(*iova), se->base + SE_DMA_TX_PTR_H);
  574. writel_relaxed(GENI_SE_DMA_EOT_BUF, se->base + SE_DMA_TX_ATTR);
  575. writel(len, se->base + SE_DMA_TX_LEN);
  576. return 0;
  577. }
  578. EXPORT_SYMBOL(geni_se_tx_dma_prep);
  579. /**
  580. * geni_se_rx_dma_prep() - Prepare the serial engine for RX DMA transfer
  581. * @se: Pointer to the concerned serial engine.
  582. * @buf: Pointer to the RX buffer.
  583. * @len: Length of the RX buffer.
  584. * @iova: Pointer to store the mapped DMA address.
  585. *
  586. * This function is used to prepare the buffers for DMA RX.
  587. *
  588. * Return: 0 on success, standard Linux error codes on failure.
  589. */
  590. int geni_se_rx_dma_prep(struct geni_se *se, void *buf, size_t len,
  591. dma_addr_t *iova)
  592. {
  593. struct geni_wrapper *wrapper = se->wrapper;
  594. u32 val;
  595. *iova = dma_map_single(wrapper->dev, buf, len, DMA_FROM_DEVICE);
  596. if (dma_mapping_error(wrapper->dev, *iova))
  597. return -EIO;
  598. val = GENI_SE_DMA_DONE_EN;
  599. val |= GENI_SE_DMA_EOT_EN;
  600. val |= GENI_SE_DMA_AHB_ERR_EN;
  601. writel_relaxed(val, se->base + SE_DMA_RX_IRQ_EN_SET);
  602. writel_relaxed(lower_32_bits(*iova), se->base + SE_DMA_RX_PTR_L);
  603. writel_relaxed(upper_32_bits(*iova), se->base + SE_DMA_RX_PTR_H);
  604. /* RX does not have EOT buffer type bit. So just reset RX_ATTR */
  605. writel_relaxed(0, se->base + SE_DMA_RX_ATTR);
  606. writel(len, se->base + SE_DMA_RX_LEN);
  607. return 0;
  608. }
  609. EXPORT_SYMBOL(geni_se_rx_dma_prep);
  610. /**
  611. * geni_se_tx_dma_unprep() - Unprepare the serial engine after TX DMA transfer
  612. * @se: Pointer to the concerned serial engine.
  613. * @iova: DMA address of the TX buffer.
  614. * @len: Length of the TX buffer.
  615. *
  616. * This function is used to unprepare the DMA buffers after DMA TX.
  617. */
  618. void geni_se_tx_dma_unprep(struct geni_se *se, dma_addr_t iova, size_t len)
  619. {
  620. struct geni_wrapper *wrapper = se->wrapper;
  621. if (iova && !dma_mapping_error(wrapper->dev, iova))
  622. dma_unmap_single(wrapper->dev, iova, len, DMA_TO_DEVICE);
  623. }
  624. EXPORT_SYMBOL(geni_se_tx_dma_unprep);
  625. /**
  626. * geni_se_rx_dma_unprep() - Unprepare the serial engine after RX DMA transfer
  627. * @se: Pointer to the concerned serial engine.
  628. * @iova: DMA address of the RX buffer.
  629. * @len: Length of the RX buffer.
  630. *
  631. * This function is used to unprepare the DMA buffers after DMA RX.
  632. */
  633. void geni_se_rx_dma_unprep(struct geni_se *se, dma_addr_t iova, size_t len)
  634. {
  635. struct geni_wrapper *wrapper = se->wrapper;
  636. if (iova && !dma_mapping_error(wrapper->dev, iova))
  637. dma_unmap_single(wrapper->dev, iova, len, DMA_FROM_DEVICE);
  638. }
  639. EXPORT_SYMBOL(geni_se_rx_dma_unprep);
  640. static int geni_se_probe(struct platform_device *pdev)
  641. {
  642. struct device *dev = &pdev->dev;
  643. struct resource *res;
  644. struct geni_wrapper *wrapper;
  645. int ret;
  646. wrapper = devm_kzalloc(dev, sizeof(*wrapper), GFP_KERNEL);
  647. if (!wrapper)
  648. return -ENOMEM;
  649. wrapper->dev = dev;
  650. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  651. wrapper->base = devm_ioremap_resource(dev, res);
  652. if (IS_ERR(wrapper->base))
  653. return PTR_ERR(wrapper->base);
  654. wrapper->ahb_clks[0].id = "m-ahb";
  655. wrapper->ahb_clks[1].id = "s-ahb";
  656. ret = devm_clk_bulk_get(dev, NUM_AHB_CLKS, wrapper->ahb_clks);
  657. if (ret) {
  658. dev_err(dev, "Err getting AHB clks %d\n", ret);
  659. return ret;
  660. }
  661. dev_set_drvdata(dev, wrapper);
  662. dev_dbg(dev, "GENI SE Driver probed\n");
  663. return devm_of_platform_populate(dev);
  664. }
  665. static const struct of_device_id geni_se_dt_match[] = {
  666. { .compatible = "qcom,geni-se-qup", },
  667. {}
  668. };
  669. MODULE_DEVICE_TABLE(of, geni_se_dt_match);
  670. static struct platform_driver geni_se_driver = {
  671. .driver = {
  672. .name = "geni_se_qup",
  673. .of_match_table = geni_se_dt_match,
  674. },
  675. .probe = geni_se_probe,
  676. };
  677. module_platform_driver(geni_se_driver);
  678. MODULE_DESCRIPTION("GENI Serial Engine Driver");
  679. MODULE_LICENSE("GPL v2");