qcom-geni-se.c 24 KB

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