qcom-geni-se.c 27 KB

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