blob: 348b28757592437e93e0d110ded4cc4eeacaae9d [file] [log] [blame]
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -06001/*
2 * Copyright (c) 2017, The Linux Foundation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 */
14
15#include <asm/dma-iommu.h>
16#include <linux/clk.h>
17#include <linux/dma-mapping.h>
18#include <linux/ipc_logging.h>
19#include <linux/io.h>
20#include <linux/list.h>
21#include <linux/module.h>
Patrick Dalyde1c64d2017-09-12 16:30:12 -070022#include <linux/slab.h>
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -060023#include <linux/msm-bus.h>
24#include <linux/msm-bus-board.h>
25#include <linux/of.h>
26#include <linux/of_platform.h>
27#include <linux/pm_runtime.h>
28#include <linux/qcom-geni-se.h>
29#include <linux/spinlock.h>
30
31#define GENI_SE_IOMMU_VA_START (0x40000000)
32#define GENI_SE_IOMMU_VA_SIZE (0xC0000000)
33
Shrey Vijay370a18b2017-12-12 16:21:20 +053034#ifdef CONFIG_ARM64
35#define GENI_SE_DMA_PTR_L(ptr) ((u32)ptr)
36#define GENI_SE_DMA_PTR_H(ptr) ((u32)(ptr >> 32))
37#else
38#define GENI_SE_DMA_PTR_L(ptr) ((u32)ptr)
39#define GENI_SE_DMA_PTR_H(ptr) 0
40#endif
41
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -060042#define NUM_LOG_PAGES 2
Karthikeyan Ramasubramanian8bef5ea2017-05-11 17:02:46 -060043#define MAX_CLK_PERF_LEVEL 32
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -060044static unsigned long default_bus_bw_set[] = {0, 19200000, 50000000, 100000000};
45
46/**
47 * @struct geni_se_device - Data structure to represent the QUPv3 Core
48 * @dev: Device pointer of the QUPv3 core.
49 * @cb_dev: Device pointer of the context bank in the IOMMU.
50 * @iommu_lock: Lock to protect IOMMU Mapping & attachment.
51 * @iommu_map: IOMMU map of the memory space supported by this core.
52 * @iommu_s1_bypass: Bypass IOMMU stage 1 translation.
53 * @base: Base address of this instance of QUPv3 core.
54 * @bus_bw: Client handle to the bus bandwidth request.
55 * @bus_mas_id: Master Endpoint ID for bus BW request.
56 * @bus_slv_id: Slave Endpoint ID for bus BW request.
Girish Mahadevand11aefc2017-11-30 15:41:19 -070057 * @geni_dev_lock: Lock to protect the bus ab & ib values, list.
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -060058 * @ab_list_head: Sorted resource list based on average bus BW.
59 * @ib_list_head: Sorted resource list based on instantaneous bus BW.
60 * @cur_ab: Current Bus Average BW request value.
61 * @cur_ib: Current Bus Instantaneous BW request value.
62 * @bus_bw_set: Clock plan for the bus driver.
63 * @cur_bus_bw_idx: Current index within the bus clock plan.
Karthikeyan Ramasubramanian8bef5ea2017-05-11 17:02:46 -060064 * @num_clk_levels: Number of valid clock levels in clk_perf_tbl.
65 * @clk_perf_tbl: Table of clock frequency input to Serial Engine clock.
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -060066 * @log_ctx: Logging context to hold the debug information
67 */
68struct geni_se_device {
69 struct device *dev;
70 struct device *cb_dev;
71 struct mutex iommu_lock;
72 struct dma_iommu_mapping *iommu_map;
73 bool iommu_s1_bypass;
74 void __iomem *base;
75 struct msm_bus_client_handle *bus_bw;
76 u32 bus_mas_id;
77 u32 bus_slv_id;
Girish Mahadevand11aefc2017-11-30 15:41:19 -070078 struct mutex geni_dev_lock;
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -060079 struct list_head ab_list_head;
80 struct list_head ib_list_head;
81 unsigned long cur_ab;
82 unsigned long cur_ib;
83 int bus_bw_set_size;
84 unsigned long *bus_bw_set;
85 int cur_bus_bw_idx;
Karthikeyan Ramasubramanian8bef5ea2017-05-11 17:02:46 -060086 unsigned int num_clk_levels;
87 unsigned long *clk_perf_tbl;
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -060088 void *log_ctx;
89};
90
91/* Offset of QUPV3 Hardware Version Register */
92#define QUPV3_HW_VER (0x4)
93
94#define HW_VER_MAJOR_MASK GENMASK(31, 28)
95#define HW_VER_MAJOR_SHFT 28
96#define HW_VER_MINOR_MASK GENMASK(27, 16)
97#define HW_VER_MINOR_SHFT 16
98#define HW_VER_STEP_MASK GENMASK(15, 0)
99
100static int geni_se_iommu_map_and_attach(struct geni_se_device *geni_se_dev);
101
102/**
103 * geni_read_reg_nolog() - Helper function to read from a GENI register
104 * @base: Base address of the serial engine's register block.
105 * @offset: Offset within the serial engine's register block.
106 *
107 * Return: Return the contents of the register.
108 */
109unsigned int geni_read_reg_nolog(void __iomem *base, int offset)
110{
111 return readl_relaxed_no_log(base + offset);
112}
113EXPORT_SYMBOL(geni_read_reg_nolog);
114
115/**
116 * geni_write_reg_nolog() - Helper function to write into a GENI register
117 * @value: Value to be written into the register.
118 * @base: Base address of the serial engine's register block.
119 * @offset: Offset within the serial engine's register block.
120 */
121void geni_write_reg_nolog(unsigned int value, void __iomem *base, int offset)
122{
123 return writel_relaxed_no_log(value, (base + offset));
124}
125EXPORT_SYMBOL(geni_write_reg_nolog);
126
127/**
128 * geni_read_reg() - Helper function to read from a GENI register
129 * @base: Base address of the serial engine's register block.
130 * @offset: Offset within the serial engine's register block.
131 *
132 * Return: Return the contents of the register.
133 */
134unsigned int geni_read_reg(void __iomem *base, int offset)
135{
136 return readl_relaxed(base + offset);
137}
138EXPORT_SYMBOL(geni_read_reg);
139
140/**
141 * geni_write_reg() - Helper function to write into a GENI register
142 * @value: Value to be written into the register.
143 * @base: Base address of the serial engine's register block.
144 * @offset: Offset within the serial engine's register block.
145 */
146void geni_write_reg(unsigned int value, void __iomem *base, int offset)
147{
148 return writel_relaxed(value, (base + offset));
149}
150EXPORT_SYMBOL(geni_write_reg);
151
152/**
153 * get_se_proto() - Read the protocol configured for a serial engine
154 * @base: Base address of the serial engine's register block.
155 *
156 * Return: Protocol value as configured in the serial engine.
157 */
158int get_se_proto(void __iomem *base)
159{
160 int proto;
161
162 proto = ((geni_read_reg(base, GENI_FW_REVISION_RO)
163 & FW_REV_PROTOCOL_MSK) >> FW_REV_PROTOCOL_SHFT);
164 return proto;
165}
166EXPORT_SYMBOL(get_se_proto);
167
168static int se_geni_irq_en(void __iomem *base)
169{
170 unsigned int common_geni_m_irq_en;
171 unsigned int common_geni_s_irq_en;
172
173 common_geni_m_irq_en = geni_read_reg(base, SE_GENI_M_IRQ_EN);
174 common_geni_s_irq_en = geni_read_reg(base, SE_GENI_S_IRQ_EN);
175 /* Common to all modes */
176 common_geni_m_irq_en |= M_COMMON_GENI_M_IRQ_EN;
177 common_geni_s_irq_en |= S_COMMON_GENI_S_IRQ_EN;
178
179 geni_write_reg(common_geni_m_irq_en, base, SE_GENI_M_IRQ_EN);
180 geni_write_reg(common_geni_s_irq_en, base, SE_GENI_S_IRQ_EN);
181 return 0;
182}
183
184
185static void se_set_rx_rfr_wm(void __iomem *base, unsigned int rx_wm,
186 unsigned int rx_rfr)
187{
188 geni_write_reg(rx_wm, base, SE_GENI_RX_WATERMARK_REG);
189 geni_write_reg(rx_rfr, base, SE_GENI_RX_RFR_WATERMARK_REG);
190}
191
192static int se_io_set_mode(void __iomem *base)
193{
194 unsigned int io_mode;
195 unsigned int geni_dma_mode;
196
197 io_mode = geni_read_reg(base, SE_IRQ_EN);
198 geni_dma_mode = geni_read_reg(base, SE_GENI_DMA_MODE_EN);
199
200 io_mode |= (GENI_M_IRQ_EN | GENI_S_IRQ_EN);
201 io_mode |= (DMA_TX_IRQ_EN | DMA_RX_IRQ_EN);
202 geni_dma_mode &= ~GENI_DMA_MODE_EN;
203
204 geni_write_reg(io_mode, base, SE_IRQ_EN);
205 geni_write_reg(geni_dma_mode, base, SE_GENI_DMA_MODE_EN);
206 geni_write_reg(0, base, SE_GSI_EVENT_EN);
207 return 0;
208}
209
210static void se_io_init(void __iomem *base)
211{
212 unsigned int io_op_ctrl;
213 unsigned int geni_cgc_ctrl;
214 unsigned int dma_general_cfg;
215
216 geni_cgc_ctrl = geni_read_reg(base, GENI_CGC_CTRL);
217 dma_general_cfg = geni_read_reg(base, SE_DMA_GENERAL_CFG);
218 geni_cgc_ctrl |= DEFAULT_CGC_EN;
219 dma_general_cfg |= (AHB_SEC_SLV_CLK_CGC_ON | DMA_AHB_SLV_CFG_ON |
220 DMA_TX_CLK_CGC_ON | DMA_RX_CLK_CGC_ON);
221 io_op_ctrl = DEFAULT_IO_OUTPUT_CTRL_MSK;
222 geni_write_reg(geni_cgc_ctrl, base, GENI_CGC_CTRL);
223 geni_write_reg(dma_general_cfg, base, SE_DMA_GENERAL_CFG);
224
225 geni_write_reg(io_op_ctrl, base, GENI_OUTPUT_CTRL);
226 geni_write_reg(FORCE_DEFAULT, base, GENI_FORCE_DEFAULT_REG);
227}
228
229/**
230 * geni_se_init() - Initialize the GENI Serial Engine
231 * @base: Base address of the serial engine's register block.
232 * @rx_wm: Receive watermark to be configured.
233 * @rx_rfr_wm: Ready-for-receive watermark to be configured.
234 *
235 * This function is used to initialize the GENI serial engine, configure
236 * receive watermark and ready-for-receive watermarks.
237 *
238 * Return: 0 on success, standard Linux error codes on failure/error.
239 */
240int geni_se_init(void __iomem *base, unsigned int rx_wm, unsigned int rx_rfr)
241{
242 int ret;
243
244 se_io_init(base);
245 ret = se_io_set_mode(base);
246 if (ret)
247 return ret;
248
249 se_set_rx_rfr_wm(base, rx_wm, rx_rfr);
250 ret = se_geni_irq_en(base);
251 return ret;
252}
253EXPORT_SYMBOL(geni_se_init);
254
255static int geni_se_select_fifo_mode(void __iomem *base)
256{
257 int proto = get_se_proto(base);
258 unsigned int common_geni_m_irq_en;
259 unsigned int common_geni_s_irq_en;
260 unsigned int geni_dma_mode;
261
262 geni_write_reg(0, base, SE_GSI_EVENT_EN);
263 geni_write_reg(0xFFFFFFFF, base, SE_GENI_M_IRQ_CLEAR);
264 geni_write_reg(0xFFFFFFFF, base, SE_GENI_S_IRQ_CLEAR);
265 geni_write_reg(0xFFFFFFFF, base, SE_DMA_TX_IRQ_CLR);
266 geni_write_reg(0xFFFFFFFF, base, SE_DMA_RX_IRQ_CLR);
267 geni_write_reg(0xFFFFFFFF, base, SE_IRQ_EN);
268
269 common_geni_m_irq_en = geni_read_reg(base, SE_GENI_M_IRQ_EN);
270 common_geni_s_irq_en = geni_read_reg(base, SE_GENI_S_IRQ_EN);
271 geni_dma_mode = geni_read_reg(base, SE_GENI_DMA_MODE_EN);
272 if (proto != UART) {
273 common_geni_m_irq_en |=
274 (M_CMD_DONE_EN | M_TX_FIFO_WATERMARK_EN |
275 M_RX_FIFO_WATERMARK_EN | M_RX_FIFO_LAST_EN);
276 common_geni_s_irq_en |= S_CMD_DONE_EN;
277 }
278 geni_dma_mode &= ~GENI_DMA_MODE_EN;
279
280 geni_write_reg(common_geni_m_irq_en, base, SE_GENI_M_IRQ_EN);
281 geni_write_reg(common_geni_s_irq_en, base, SE_GENI_S_IRQ_EN);
282 geni_write_reg(geni_dma_mode, base, SE_GENI_DMA_MODE_EN);
283 return 0;
284}
285
286static int geni_se_select_dma_mode(void __iomem *base)
287{
288 unsigned int geni_dma_mode = 0;
289
290 geni_write_reg(0, base, SE_GSI_EVENT_EN);
291 geni_write_reg(0xFFFFFFFF, base, SE_GENI_M_IRQ_CLEAR);
292 geni_write_reg(0xFFFFFFFF, base, SE_GENI_S_IRQ_CLEAR);
293 geni_write_reg(0xFFFFFFFF, base, SE_DMA_TX_IRQ_CLR);
294 geni_write_reg(0xFFFFFFFF, base, SE_DMA_RX_IRQ_CLR);
295 geni_write_reg(0xFFFFFFFF, base, SE_IRQ_EN);
296
297 geni_dma_mode = geni_read_reg(base, SE_GENI_DMA_MODE_EN);
298 geni_dma_mode |= GENI_DMA_MODE_EN;
299 geni_write_reg(geni_dma_mode, base, SE_GENI_DMA_MODE_EN);
300 return 0;
301}
302
303static int geni_se_select_gsi_mode(void __iomem *base)
304{
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600305 unsigned int geni_dma_mode = 0;
306 unsigned int gsi_event_en = 0;
Girish Mahadevanaf5f2bc2017-08-15 12:05:40 -0600307 unsigned int common_geni_m_irq_en = 0;
308 unsigned int common_geni_s_irq_en = 0;
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600309
Girish Mahadevanaf5f2bc2017-08-15 12:05:40 -0600310 common_geni_m_irq_en = geni_read_reg(base, SE_GENI_M_IRQ_EN);
311 common_geni_s_irq_en = geni_read_reg(base, SE_GENI_S_IRQ_EN);
312 common_geni_m_irq_en &=
313 ~(M_CMD_DONE_EN | M_TX_FIFO_WATERMARK_EN |
314 M_RX_FIFO_WATERMARK_EN | M_RX_FIFO_LAST_EN);
315 common_geni_s_irq_en &= ~S_CMD_DONE_EN;
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600316 geni_dma_mode = geni_read_reg(base, SE_GENI_DMA_MODE_EN);
317 gsi_event_en = geni_read_reg(base, SE_GSI_EVENT_EN);
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600318
319 geni_dma_mode |= GENI_DMA_MODE_EN;
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600320 gsi_event_en |= (DMA_RX_EVENT_EN | DMA_TX_EVENT_EN |
321 GENI_M_EVENT_EN | GENI_S_EVENT_EN);
322
Girish Mahadevanaf5f2bc2017-08-15 12:05:40 -0600323 geni_write_reg(0, base, SE_IRQ_EN);
324 geni_write_reg(common_geni_s_irq_en, base, SE_GENI_S_IRQ_EN);
325 geni_write_reg(common_geni_m_irq_en, base, SE_GENI_M_IRQ_EN);
326 geni_write_reg(0xFFFFFFFF, base, SE_GENI_M_IRQ_CLEAR);
327 geni_write_reg(0xFFFFFFFF, base, SE_GENI_S_IRQ_CLEAR);
328 geni_write_reg(0xFFFFFFFF, base, SE_DMA_TX_IRQ_CLR);
329 geni_write_reg(0xFFFFFFFF, base, SE_DMA_RX_IRQ_CLR);
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600330 geni_write_reg(geni_dma_mode, base, SE_GENI_DMA_MODE_EN);
331 geni_write_reg(gsi_event_en, base, SE_GSI_EVENT_EN);
332 return 0;
333
334}
335
336/**
337 * geni_se_select_mode() - Select the serial engine transfer mode
338 * @base: Base address of the serial engine's register block.
339 * @mode: Transfer mode to be selected.
340 *
341 * Return: 0 on success, standard Linux error codes on failure.
342 */
343int geni_se_select_mode(void __iomem *base, int mode)
344{
345 int ret = 0;
346
347 switch (mode) {
348 case FIFO_MODE:
349 geni_se_select_fifo_mode(base);
350 break;
351 case SE_DMA:
352 geni_se_select_dma_mode(base);
353 break;
354 case GSI_DMA:
355 geni_se_select_gsi_mode(base);
356 break;
357 default:
358 ret = -EINVAL;
359 break;
360 }
361
362 return ret;
363}
364EXPORT_SYMBOL(geni_se_select_mode);
365
366/**
367 * geni_setup_m_cmd() - Setup the primary sequencer
368 * @base: Base address of the serial engine's register block.
369 * @cmd: Command/Operation to setup in the primary sequencer.
370 * @params: Parameter for the sequencer command.
371 *
372 * This function is used to configure the primary sequencer with the
373 * command and its assoicated parameters.
374 */
375void geni_setup_m_cmd(void __iomem *base, u32 cmd, u32 params)
376{
377 u32 m_cmd = (cmd << M_OPCODE_SHFT);
378
379 m_cmd |= (params & M_PARAMS_MSK);
380 geni_write_reg(m_cmd, base, SE_GENI_M_CMD0);
381}
382EXPORT_SYMBOL(geni_setup_m_cmd);
383
384/**
385 * geni_setup_s_cmd() - Setup the secondary sequencer
386 * @base: Base address of the serial engine's register block.
387 * @cmd: Command/Operation to setup in the secondary sequencer.
388 * @params: Parameter for the sequencer command.
389 *
390 * This function is used to configure the secondary sequencer with the
391 * command and its assoicated parameters.
392 */
393void geni_setup_s_cmd(void __iomem *base, u32 cmd, u32 params)
394{
395 u32 s_cmd = geni_read_reg(base, SE_GENI_S_CMD0);
396
397 s_cmd &= ~(S_OPCODE_MSK | S_PARAMS_MSK);
398 s_cmd |= (cmd << S_OPCODE_SHFT);
399 s_cmd |= (params & S_PARAMS_MSK);
400 geni_write_reg(s_cmd, base, SE_GENI_S_CMD0);
401}
402EXPORT_SYMBOL(geni_setup_s_cmd);
403
404/**
405 * geni_cancel_m_cmd() - Cancel the command configured in the primary sequencer
406 * @base: Base address of the serial engine's register block.
407 *
408 * This function is used to cancel the currently configured command in the
409 * primary sequencer.
410 */
411void geni_cancel_m_cmd(void __iomem *base)
412{
Girish Mahadevan6d97cbc2017-11-07 15:28:27 -0700413 geni_write_reg(M_GENI_CMD_CANCEL, base, SE_GENI_M_CMD_CTRL_REG);
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600414}
415EXPORT_SYMBOL(geni_cancel_m_cmd);
416
417/**
418 * geni_cancel_s_cmd() - Cancel the command configured in the secondary
419 * sequencer
420 * @base: Base address of the serial engine's register block.
421 *
422 * This function is used to cancel the currently configured command in the
423 * secondary sequencer.
424 */
425void geni_cancel_s_cmd(void __iomem *base)
426{
427 geni_write_reg(S_GENI_CMD_CANCEL, base, SE_GENI_S_CMD_CTRL_REG);
428}
429EXPORT_SYMBOL(geni_cancel_s_cmd);
430
431/**
432 * geni_abort_m_cmd() - Abort the command configured in the primary sequencer
433 * @base: Base address of the serial engine's register block.
434 *
435 * This function is used to force abort the currently configured command in the
436 * primary sequencer.
437 */
438void geni_abort_m_cmd(void __iomem *base)
439{
440 geni_write_reg(M_GENI_CMD_ABORT, base, SE_GENI_M_CMD_CTRL_REG);
441}
442EXPORT_SYMBOL(geni_abort_m_cmd);
443
444/**
445 * geni_abort_s_cmd() - Abort the command configured in the secondary
446 * sequencer
447 * @base: Base address of the serial engine's register block.
448 *
449 * This function is used to force abort the currently configured command in the
450 * secondary sequencer.
451 */
452void geni_abort_s_cmd(void __iomem *base)
453{
454 geni_write_reg(S_GENI_CMD_ABORT, base, SE_GENI_S_CMD_CTRL_REG);
455}
456EXPORT_SYMBOL(geni_abort_s_cmd);
457
458/**
459 * get_tx_fifo_depth() - Get the TX fifo depth of the serial engine
460 * @base: Base address of the serial engine's register block.
461 *
462 * This function is used to get the depth i.e. number of elements in the
463 * TX fifo of the serial engine.
464 *
465 * Return: TX fifo depth in units of FIFO words.
466 */
467int get_tx_fifo_depth(void __iomem *base)
468{
469 int tx_fifo_depth;
470
471 tx_fifo_depth = ((geni_read_reg(base, SE_HW_PARAM_0)
472 & TX_FIFO_DEPTH_MSK) >> TX_FIFO_DEPTH_SHFT);
473 return tx_fifo_depth;
474}
475EXPORT_SYMBOL(get_tx_fifo_depth);
476
477/**
478 * get_tx_fifo_width() - Get the TX fifo width of the serial engine
479 * @base: Base address of the serial engine's register block.
480 *
481 * This function is used to get the width i.e. word size per element in the
482 * TX fifo of the serial engine.
483 *
484 * Return: TX fifo width in bits
485 */
486int get_tx_fifo_width(void __iomem *base)
487{
488 int tx_fifo_width;
489
490 tx_fifo_width = ((geni_read_reg(base, SE_HW_PARAM_0)
491 & TX_FIFO_WIDTH_MSK) >> TX_FIFO_WIDTH_SHFT);
492 return tx_fifo_width;
493}
494EXPORT_SYMBOL(get_tx_fifo_width);
495
496/**
497 * get_rx_fifo_depth() - Get the RX fifo depth of the serial engine
498 * @base: Base address of the serial engine's register block.
499 *
500 * This function is used to get the depth i.e. number of elements in the
501 * RX fifo of the serial engine.
502 *
503 * Return: RX fifo depth in units of FIFO words
504 */
505int get_rx_fifo_depth(void __iomem *base)
506{
507 int rx_fifo_depth;
508
509 rx_fifo_depth = ((geni_read_reg(base, SE_HW_PARAM_1)
510 & RX_FIFO_DEPTH_MSK) >> RX_FIFO_DEPTH_SHFT);
511 return rx_fifo_depth;
512}
513EXPORT_SYMBOL(get_rx_fifo_depth);
514
515/**
516 * se_get_packing_config() - Get the packing configuration based on input
517 * @bpw: Bits of data per transfer word.
518 * @pack_words: Number of words per fifo element.
519 * @msb_to_lsb: Transfer from MSB to LSB or vice-versa.
520 * @cfg0: Output buffer to hold the first half of configuration.
521 * @cfg1: Output buffer to hold the second half of configuration.
522 *
523 * This function is used to calculate the packing configuration based on
524 * the input packing requirement and the configuration logic.
525 */
526void se_get_packing_config(int bpw, int pack_words, bool msb_to_lsb,
527 unsigned long *cfg0, unsigned long *cfg1)
528{
529 u32 cfg[4] = {0};
530 int len;
531 int temp_bpw = bpw;
532 int idx_start = (msb_to_lsb ? (bpw - 1) : 0);
533 int idx = idx_start;
534 int idx_delta = (msb_to_lsb ? -BITS_PER_BYTE : BITS_PER_BYTE);
535 int ceil_bpw = ((bpw & (BITS_PER_BYTE - 1)) ?
536 ((bpw & ~(BITS_PER_BYTE - 1)) + BITS_PER_BYTE) : bpw);
537 int iter = (ceil_bpw * pack_words) >> 3;
538 int i;
539
540 if (unlikely(iter <= 0 || iter > 4)) {
541 *cfg0 = 0;
542 *cfg1 = 0;
543 return;
544 }
545
546 for (i = 0; i < iter; i++) {
547 len = (temp_bpw < BITS_PER_BYTE) ?
548 (temp_bpw - 1) : BITS_PER_BYTE - 1;
549 cfg[i] = ((idx << 5) | (msb_to_lsb << 4) | (len << 1));
550 idx = ((temp_bpw - BITS_PER_BYTE) <= 0) ?
551 ((i + 1) * BITS_PER_BYTE) + idx_start :
552 idx + idx_delta;
553 temp_bpw = ((temp_bpw - BITS_PER_BYTE) <= 0) ?
554 bpw : (temp_bpw - BITS_PER_BYTE);
555 }
556 cfg[iter - 1] |= 1;
557 *cfg0 = cfg[0] | (cfg[1] << 10);
558 *cfg1 = cfg[2] | (cfg[3] << 10);
559}
560EXPORT_SYMBOL(se_get_packing_config);
561
562/**
563 * se_config_packing() - Packing configuration of the serial engine
564 * @base: Base address of the serial engine's register block.
565 * @bpw: Bits of data per transfer word.
566 * @pack_words: Number of words per fifo element.
567 * @msb_to_lsb: Transfer from MSB to LSB or vice-versa.
568 *
569 * This function is used to configure the packing rules for the current
570 * transfer.
571 */
572void se_config_packing(void __iomem *base, int bpw,
573 int pack_words, bool msb_to_lsb)
574{
575 unsigned long cfg0, cfg1;
576
577 se_get_packing_config(bpw, pack_words, msb_to_lsb, &cfg0, &cfg1);
578 geni_write_reg(cfg0, base, SE_GENI_TX_PACKING_CFG0);
579 geni_write_reg(cfg1, base, SE_GENI_TX_PACKING_CFG1);
580 geni_write_reg(cfg0, base, SE_GENI_RX_PACKING_CFG0);
581 geni_write_reg(cfg1, base, SE_GENI_RX_PACKING_CFG1);
582 if (pack_words || bpw == 32)
583 geni_write_reg((bpw >> 4), base, SE_GENI_BYTE_GRAN);
584}
585EXPORT_SYMBOL(se_config_packing);
586
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600587static bool geni_se_check_bus_bw(struct geni_se_device *geni_se_dev)
588{
589 int i;
590 int new_bus_bw_idx = geni_se_dev->bus_bw_set_size - 1;
591 unsigned long new_bus_bw;
592 bool bus_bw_update = false;
593
594 new_bus_bw = max(geni_se_dev->cur_ib, geni_se_dev->cur_ab) /
595 DEFAULT_BUS_WIDTH;
596 for (i = 0; i < geni_se_dev->bus_bw_set_size; i++) {
597 if (geni_se_dev->bus_bw_set[i] >= new_bus_bw) {
598 new_bus_bw_idx = i;
599 break;
600 }
601 }
602
603 if (geni_se_dev->cur_bus_bw_idx != new_bus_bw_idx) {
604 geni_se_dev->cur_bus_bw_idx = new_bus_bw_idx;
605 bus_bw_update = true;
606 }
607 return bus_bw_update;
608}
609
610static int geni_se_rmv_ab_ib(struct geni_se_device *geni_se_dev,
611 struct se_geni_rsc *rsc)
612{
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600613 struct se_geni_rsc *tmp;
614 bool bus_bw_update = false;
615 int ret = 0;
616
617 if (unlikely(list_empty(&rsc->ab_list) || list_empty(&rsc->ib_list)))
618 return -EINVAL;
619
Girish Mahadevand11aefc2017-11-30 15:41:19 -0700620 mutex_lock(&geni_se_dev->geni_dev_lock);
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600621 list_del_init(&rsc->ab_list);
622 geni_se_dev->cur_ab -= rsc->ab;
623
624 list_del_init(&rsc->ib_list);
625 tmp = list_first_entry_or_null(&geni_se_dev->ib_list_head,
626 struct se_geni_rsc, ib_list);
627 if (tmp && tmp->ib != geni_se_dev->cur_ib)
628 geni_se_dev->cur_ib = tmp->ib;
629 else if (!tmp && geni_se_dev->cur_ib)
630 geni_se_dev->cur_ib = 0;
631
632 bus_bw_update = geni_se_check_bus_bw(geni_se_dev);
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600633 if (bus_bw_update)
634 ret = msm_bus_scale_update_bw(geni_se_dev->bus_bw,
635 geni_se_dev->cur_ab,
636 geni_se_dev->cur_ib);
637 GENI_SE_DBG(geni_se_dev->log_ctx, false, NULL,
638 "%s: %lu:%lu (%lu:%lu) %d\n", __func__,
639 geni_se_dev->cur_ab, geni_se_dev->cur_ib,
640 rsc->ab, rsc->ib, bus_bw_update);
Girish Mahadevand11aefc2017-11-30 15:41:19 -0700641 mutex_unlock(&geni_se_dev->geni_dev_lock);
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600642 return ret;
643}
644
645/**
Karthikeyan Ramasubramanian383b41e2017-10-25 17:32:52 -0600646 * se_geni_clks_off() - Turn off clocks associated with the serial
647 * engine
648 * @rsc: Handle to resources associated with the serial engine.
649 *
650 * Return: 0 on success, standard Linux error codes on failure/error.
651 */
652int se_geni_clks_off(struct se_geni_rsc *rsc)
653{
654 int ret = 0;
655 struct geni_se_device *geni_se_dev;
656
657 if (unlikely(!rsc || !rsc->wrapper_dev))
658 return -EINVAL;
659
660 geni_se_dev = dev_get_drvdata(rsc->wrapper_dev);
661 if (unlikely(!geni_se_dev || !geni_se_dev->bus_bw))
662 return -ENODEV;
663
664 clk_disable_unprepare(rsc->se_clk);
665 clk_disable_unprepare(rsc->s_ahb_clk);
666 clk_disable_unprepare(rsc->m_ahb_clk);
667
668 ret = geni_se_rmv_ab_ib(geni_se_dev, rsc);
669 if (ret)
670 GENI_SE_ERR(geni_se_dev->log_ctx, false, NULL,
671 "%s: Error %d during bus_bw_update\n", __func__, ret);
672 return ret;
673}
674EXPORT_SYMBOL(se_geni_clks_off);
675
676/**
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600677 * se_geni_resources_off() - Turn off resources associated with the serial
678 * engine
679 * @rsc: Handle to resources associated with the serial engine.
680 *
681 * Return: 0 on success, standard Linux error codes on failure/error.
682 */
683int se_geni_resources_off(struct se_geni_rsc *rsc)
684{
685 int ret = 0;
686 struct geni_se_device *geni_se_dev;
687
688 if (unlikely(!rsc || !rsc->wrapper_dev))
689 return -EINVAL;
690
691 geni_se_dev = dev_get_drvdata(rsc->wrapper_dev);
692 if (unlikely(!geni_se_dev || !geni_se_dev->bus_bw))
693 return -ENODEV;
694
Karthikeyan Ramasubramanian383b41e2017-10-25 17:32:52 -0600695 ret = se_geni_clks_off(rsc);
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600696 if (ret)
697 GENI_SE_ERR(geni_se_dev->log_ctx, false, NULL,
Karthikeyan Ramasubramanian383b41e2017-10-25 17:32:52 -0600698 "%s: Error %d turning off clocks\n", __func__, ret);
Girish Mahadevan6d97cbc2017-11-07 15:28:27 -0700699 ret = pinctrl_select_state(rsc->geni_pinctrl, rsc->geni_gpio_sleep);
700 if (ret)
701 GENI_SE_ERR(geni_se_dev->log_ctx, false, NULL,
702 "%s: Error %d pinctrl_select_state\n", __func__, ret);
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600703 return ret;
704}
705EXPORT_SYMBOL(se_geni_resources_off);
706
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600707static int geni_se_add_ab_ib(struct geni_se_device *geni_se_dev,
708 struct se_geni_rsc *rsc)
709{
Karthikeyan Ramasubramanian409370c2017-08-11 17:31:45 -0600710 struct se_geni_rsc *tmp = NULL;
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600711 struct list_head *ins_list_head;
712 bool bus_bw_update = false;
713 int ret = 0;
714
Girish Mahadevand11aefc2017-11-30 15:41:19 -0700715 mutex_lock(&geni_se_dev->geni_dev_lock);
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600716 list_add(&rsc->ab_list, &geni_se_dev->ab_list_head);
717 geni_se_dev->cur_ab += rsc->ab;
718
719 ins_list_head = &geni_se_dev->ib_list_head;
720 list_for_each_entry(tmp, &geni_se_dev->ib_list_head, ib_list) {
721 if (tmp->ib < rsc->ib)
722 break;
723 ins_list_head = &tmp->ib_list;
724 }
725 list_add(&rsc->ib_list, ins_list_head);
726 /* Currently inserted node has greater average BW value */
727 if (ins_list_head == &geni_se_dev->ib_list_head)
Karthikeyan Ramasubramanian409370c2017-08-11 17:31:45 -0600728 geni_se_dev->cur_ib = rsc->ib;
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600729
730 bus_bw_update = geni_se_check_bus_bw(geni_se_dev);
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600731 if (bus_bw_update)
732 ret = msm_bus_scale_update_bw(geni_se_dev->bus_bw,
733 geni_se_dev->cur_ab,
734 geni_se_dev->cur_ib);
735 GENI_SE_DBG(geni_se_dev->log_ctx, false, NULL,
736 "%s: %lu:%lu (%lu:%lu) %d\n", __func__,
737 geni_se_dev->cur_ab, geni_se_dev->cur_ib,
738 rsc->ab, rsc->ib, bus_bw_update);
Girish Mahadevand11aefc2017-11-30 15:41:19 -0700739 mutex_unlock(&geni_se_dev->geni_dev_lock);
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600740 return ret;
741}
742
743/**
Karthikeyan Ramasubramanian383b41e2017-10-25 17:32:52 -0600744 * se_geni_clks_on() - Turn on clocks associated with the serial
745 * engine
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600746 * @rsc: Handle to resources associated with the serial engine.
747 *
748 * Return: 0 on success, standard Linux error codes on failure/error.
749 */
Karthikeyan Ramasubramanian383b41e2017-10-25 17:32:52 -0600750int se_geni_clks_on(struct se_geni_rsc *rsc)
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600751{
752 int ret = 0;
753 struct geni_se_device *geni_se_dev;
754
755 if (unlikely(!rsc || !rsc->wrapper_dev))
756 return -EINVAL;
757
758 geni_se_dev = dev_get_drvdata(rsc->wrapper_dev);
759 if (unlikely(!geni_se_dev))
760 return -EPROBE_DEFER;
761
762 ret = geni_se_add_ab_ib(geni_se_dev, rsc);
763 if (ret) {
764 GENI_SE_ERR(geni_se_dev->log_ctx, false, NULL,
765 "%s: Error %d during bus_bw_update\n", __func__, ret);
766 return ret;
767 }
768
Karthikeyan Ramasubramanian383b41e2017-10-25 17:32:52 -0600769 ret = clk_prepare_enable(rsc->m_ahb_clk);
770 if (ret)
771 goto clks_on_err1;
772
773 ret = clk_prepare_enable(rsc->s_ahb_clk);
774 if (ret)
775 goto clks_on_err2;
776
777 ret = clk_prepare_enable(rsc->se_clk);
778 if (ret)
779 goto clks_on_err3;
780 return 0;
781
782clks_on_err3:
783 clk_disable_unprepare(rsc->s_ahb_clk);
784clks_on_err2:
785 clk_disable_unprepare(rsc->m_ahb_clk);
786clks_on_err1:
787 geni_se_rmv_ab_ib(geni_se_dev, rsc);
788 return ret;
789}
790EXPORT_SYMBOL(se_geni_clks_on);
791
792/**
793 * se_geni_resources_on() - Turn on resources associated with the serial
794 * engine
795 * @rsc: Handle to resources associated with the serial engine.
796 *
797 * Return: 0 on success, standard Linux error codes on failure/error.
798 */
799int se_geni_resources_on(struct se_geni_rsc *rsc)
800{
801 int ret = 0;
802 struct geni_se_device *geni_se_dev;
803
804 if (unlikely(!rsc || !rsc->wrapper_dev))
805 return -EINVAL;
806
807 geni_se_dev = dev_get_drvdata(rsc->wrapper_dev);
808 if (unlikely(!geni_se_dev))
809 return -EPROBE_DEFER;
810
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600811 ret = pinctrl_select_state(rsc->geni_pinctrl, rsc->geni_gpio_active);
812 if (ret) {
813 GENI_SE_ERR(geni_se_dev->log_ctx, false, NULL,
814 "%s: Error %d pinctrl_select_state\n", __func__, ret);
Girish Mahadevan6d97cbc2017-11-07 15:28:27 -0700815 return ret;
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600816 }
Girish Mahadevan6d97cbc2017-11-07 15:28:27 -0700817
818 ret = se_geni_clks_on(rsc);
819 if (ret) {
820 GENI_SE_ERR(geni_se_dev->log_ctx, false, NULL,
821 "%s: Error %d during clks_on\n", __func__, ret);
822 pinctrl_select_state(rsc->geni_pinctrl, rsc->geni_gpio_sleep);
823 }
824
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600825 return ret;
826}
827EXPORT_SYMBOL(se_geni_resources_on);
828
829/**
830 * geni_se_resources_init() - Init the SE resource structure
831 * @rsc: SE resource structure to be initialized.
832 * @ab: Initial Average bus bandwidth request value.
833 * @ib: Initial Instantaneous bus bandwidth request value.
834 *
835 * Return: 0 on success, standard Linux error codes on failure.
836 */
837int geni_se_resources_init(struct se_geni_rsc *rsc,
838 unsigned long ab, unsigned long ib)
839{
840 struct geni_se_device *geni_se_dev;
841
842 if (unlikely(!rsc || !rsc->wrapper_dev))
843 return -EINVAL;
844
845 geni_se_dev = dev_get_drvdata(rsc->wrapper_dev);
846 if (unlikely(!geni_se_dev))
847 return -EPROBE_DEFER;
848
849 if (unlikely(IS_ERR_OR_NULL(geni_se_dev->bus_bw))) {
850 geni_se_dev->bus_bw = msm_bus_scale_register(
851 geni_se_dev->bus_mas_id,
852 geni_se_dev->bus_slv_id,
853 (char *)dev_name(geni_se_dev->dev),
854 false);
855 if (IS_ERR_OR_NULL(geni_se_dev->bus_bw)) {
856 GENI_SE_ERR(geni_se_dev->log_ctx, false, NULL,
857 "%s: Error creating bus client\n", __func__);
858 return (int)PTR_ERR(geni_se_dev->bus_bw);
859 }
860 }
861
862 rsc->ab = ab;
863 rsc->ib = ib;
864 INIT_LIST_HEAD(&rsc->ab_list);
865 INIT_LIST_HEAD(&rsc->ib_list);
866 geni_se_iommu_map_and_attach(geni_se_dev);
867 return 0;
868}
869EXPORT_SYMBOL(geni_se_resources_init);
870
871/**
Karthikeyan Ramasubramanian8bef5ea2017-05-11 17:02:46 -0600872 * geni_se_clk_tbl_get() - Get the clock table to program DFS
873 * @rsc: Resource for which the clock table is requested.
874 * @tbl: Table in which the output is returned.
875 *
876 * This function is called by the protocol drivers to determine the different
877 * clock frequencies supported by Serail Engine Core Clock. The protocol
878 * drivers use the output to determine the clock frequency index to be
879 * programmed into DFS.
880 *
881 * Return: number of valid performance levels in the table on success,
882 * standard Linux error codes on failure.
883 */
884int geni_se_clk_tbl_get(struct se_geni_rsc *rsc, unsigned long **tbl)
885{
886 struct geni_se_device *geni_se_dev;
887 int i;
888 unsigned long prev_freq = 0;
Girish Mahadevand11aefc2017-11-30 15:41:19 -0700889 int ret = 0;
Karthikeyan Ramasubramanian8bef5ea2017-05-11 17:02:46 -0600890
891 if (unlikely(!rsc || !rsc->wrapper_dev || !rsc->se_clk || !tbl))
892 return -EINVAL;
893
Karthikeyan Ramasubramanian8bef5ea2017-05-11 17:02:46 -0600894 geni_se_dev = dev_get_drvdata(rsc->wrapper_dev);
895 if (unlikely(!geni_se_dev))
896 return -EPROBE_DEFER;
Girish Mahadevand11aefc2017-11-30 15:41:19 -0700897 mutex_lock(&geni_se_dev->geni_dev_lock);
898 *tbl = NULL;
Karthikeyan Ramasubramanian8bef5ea2017-05-11 17:02:46 -0600899
900 if (geni_se_dev->clk_perf_tbl) {
901 *tbl = geni_se_dev->clk_perf_tbl;
Girish Mahadevand11aefc2017-11-30 15:41:19 -0700902 ret = geni_se_dev->num_clk_levels;
903 goto exit_se_clk_tbl_get;
Karthikeyan Ramasubramanian8bef5ea2017-05-11 17:02:46 -0600904 }
905
906 geni_se_dev->clk_perf_tbl = kzalloc(sizeof(*geni_se_dev->clk_perf_tbl) *
907 MAX_CLK_PERF_LEVEL, GFP_KERNEL);
Girish Mahadevand11aefc2017-11-30 15:41:19 -0700908 if (!geni_se_dev->clk_perf_tbl) {
909 ret = -ENOMEM;
910 goto exit_se_clk_tbl_get;
911 }
Karthikeyan Ramasubramanian8bef5ea2017-05-11 17:02:46 -0600912
913 for (i = 0; i < MAX_CLK_PERF_LEVEL; i++) {
914 geni_se_dev->clk_perf_tbl[i] = clk_round_rate(rsc->se_clk,
915 prev_freq + 1);
916 if (geni_se_dev->clk_perf_tbl[i] == prev_freq) {
917 geni_se_dev->clk_perf_tbl[i] = 0;
918 break;
919 }
920 prev_freq = geni_se_dev->clk_perf_tbl[i];
921 }
922 geni_se_dev->num_clk_levels = i;
923 *tbl = geni_se_dev->clk_perf_tbl;
Girish Mahadevand11aefc2017-11-30 15:41:19 -0700924 ret = geni_se_dev->num_clk_levels;
925exit_se_clk_tbl_get:
926 mutex_unlock(&geni_se_dev->geni_dev_lock);
927 return ret;
Karthikeyan Ramasubramanian8bef5ea2017-05-11 17:02:46 -0600928}
929EXPORT_SYMBOL(geni_se_clk_tbl_get);
930
931/**
932 * geni_se_clk_freq_match() - Get the matching or closest SE clock frequency
933 * @rsc: Resource for which the clock frequency is requested.
934 * @req_freq: Requested clock frequency.
935 * @index: Index of the resultant frequency in the table.
936 * @res_freq: Resultant frequency which matches or is closer to the
937 * requested frequency.
938 * @exact: Flag to indicate exact multiple requirement of the requested
939 * frequency .
940 *
941 * This function is called by the protocol drivers to determine the matching
942 * or closest frequency of the Serial Engine clock to be selected in order
943 * to meet the performance requirements.
944 *
945 * Return: 0 on success, standard Linux error codes on failure.
946 */
947int geni_se_clk_freq_match(struct se_geni_rsc *rsc, unsigned long req_freq,
948 unsigned int *index, unsigned long *res_freq,
949 bool exact)
950{
951 unsigned long *tbl;
952 int num_clk_levels;
953 int i;
954
955 num_clk_levels = geni_se_clk_tbl_get(rsc, &tbl);
956 if (num_clk_levels < 0)
957 return num_clk_levels;
958
959 if (num_clk_levels == 0)
960 return -EFAULT;
961
962 *res_freq = 0;
963 for (i = 0; i < num_clk_levels; i++) {
964 if (!(tbl[i] % req_freq)) {
965 *index = i;
966 *res_freq = tbl[i];
967 return 0;
968 }
969
970 if (!(*res_freq) || ((tbl[i] > *res_freq) &&
971 (tbl[i] < req_freq))) {
972 *index = i;
973 *res_freq = tbl[i];
974 }
975 }
976
977 if (exact || !(*res_freq))
978 return -ENOKEY;
979
980 return 0;
981}
982EXPORT_SYMBOL(geni_se_clk_freq_match);
983
984/**
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600985 * geni_se_tx_dma_prep() - Prepare the Serial Engine for TX DMA transfer
986 * @wrapper_dev: QUPv3 Wrapper Device to which the TX buffer is mapped.
987 * @base: Base address of the SE register block.
988 * @tx_buf: Pointer to the TX buffer.
989 * @tx_len: Length of the TX buffer.
990 * @tx_dma: Pointer to store the mapped DMA address.
991 *
992 * This function is used to prepare the buffers for DMA TX.
993 *
994 * Return: 0 on success, standard Linux error codes on error/failure.
995 */
996int geni_se_tx_dma_prep(struct device *wrapper_dev, void __iomem *base,
997 void *tx_buf, int tx_len, dma_addr_t *tx_dma)
998{
999 int ret;
1000
1001 if (unlikely(!wrapper_dev || !base || !tx_buf || !tx_len || !tx_dma))
1002 return -EINVAL;
1003
1004 ret = geni_se_iommu_map_buf(wrapper_dev, tx_dma, tx_buf, tx_len,
1005 DMA_TO_DEVICE);
1006 if (ret)
1007 return ret;
1008
1009 geni_write_reg(7, base, SE_DMA_TX_IRQ_EN_SET);
Shrey Vijay370a18b2017-12-12 16:21:20 +05301010 geni_write_reg(GENI_SE_DMA_PTR_L(*tx_dma), base, SE_DMA_TX_PTR_L);
1011 geni_write_reg(GENI_SE_DMA_PTR_H(*tx_dma), base, SE_DMA_TX_PTR_H);
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -06001012 geni_write_reg(1, base, SE_DMA_TX_ATTR);
1013 geni_write_reg(tx_len, base, SE_DMA_TX_LEN);
1014 return 0;
1015}
1016EXPORT_SYMBOL(geni_se_tx_dma_prep);
1017
1018/**
1019 * geni_se_rx_dma_prep() - Prepare the Serial Engine for RX DMA transfer
1020 * @wrapper_dev: QUPv3 Wrapper Device to which the RX buffer is mapped.
1021 * @base: Base address of the SE register block.
1022 * @rx_buf: Pointer to the RX buffer.
1023 * @rx_len: Length of the RX buffer.
1024 * @rx_dma: Pointer to store the mapped DMA address.
1025 *
1026 * This function is used to prepare the buffers for DMA RX.
1027 *
1028 * Return: 0 on success, standard Linux error codes on error/failure.
1029 */
1030int geni_se_rx_dma_prep(struct device *wrapper_dev, void __iomem *base,
1031 void *rx_buf, int rx_len, dma_addr_t *rx_dma)
1032{
1033 int ret;
1034
1035 if (unlikely(!wrapper_dev || !base || !rx_buf || !rx_len || !rx_dma))
1036 return -EINVAL;
1037
1038 ret = geni_se_iommu_map_buf(wrapper_dev, rx_dma, rx_buf, rx_len,
1039 DMA_FROM_DEVICE);
1040 if (ret)
1041 return ret;
1042
1043 geni_write_reg(7, base, SE_DMA_RX_IRQ_EN_SET);
Shrey Vijay370a18b2017-12-12 16:21:20 +05301044 geni_write_reg(GENI_SE_DMA_PTR_L(*rx_dma), base, SE_DMA_RX_PTR_L);
1045 geni_write_reg(GENI_SE_DMA_PTR_H(*rx_dma), base, SE_DMA_RX_PTR_H);
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -06001046 /* RX does not have EOT bit */
1047 geni_write_reg(0, base, SE_DMA_RX_ATTR);
1048 geni_write_reg(rx_len, base, SE_DMA_RX_LEN);
1049 return 0;
1050}
1051EXPORT_SYMBOL(geni_se_rx_dma_prep);
1052
1053/**
1054 * geni_se_tx_dma_unprep() - Unprepare the Serial Engine after TX DMA transfer
1055 * @wrapper_dev: QUPv3 Wrapper Device to which the RX buffer is mapped.
1056 * @tx_dma: DMA address of the TX buffer.
1057 * @tx_len: Length of the TX buffer.
1058 *
1059 * This function is used to unprepare the DMA buffers after DMA TX.
1060 */
1061void geni_se_tx_dma_unprep(struct device *wrapper_dev,
1062 dma_addr_t tx_dma, int tx_len)
1063{
1064 if (tx_dma)
1065 geni_se_iommu_unmap_buf(wrapper_dev, &tx_dma, tx_len,
1066 DMA_TO_DEVICE);
1067}
1068EXPORT_SYMBOL(geni_se_tx_dma_unprep);
1069
1070/**
1071 * geni_se_rx_dma_unprep() - Unprepare the Serial Engine after RX DMA transfer
1072 * @wrapper_dev: QUPv3 Wrapper Device to which the RX buffer is mapped.
1073 * @rx_dma: DMA address of the RX buffer.
1074 * @rx_len: Length of the RX buffer.
1075 *
1076 * This function is used to unprepare the DMA buffers after DMA RX.
1077 */
1078void geni_se_rx_dma_unprep(struct device *wrapper_dev,
1079 dma_addr_t rx_dma, int rx_len)
1080{
1081 if (rx_dma)
1082 geni_se_iommu_unmap_buf(wrapper_dev, &rx_dma, rx_len,
1083 DMA_FROM_DEVICE);
1084}
1085EXPORT_SYMBOL(geni_se_rx_dma_unprep);
1086
1087/**
1088 * geni_se_qupv3_hw_version() - Read the QUPv3 Hardware version
1089 * @wrapper_dev: Pointer to the corresponding QUPv3 wrapper core.
1090 * @major: Buffer for Major Version field.
1091 * @minor: Buffer for Minor Version field.
1092 * @step: Buffer for Step Version field.
1093 *
1094 * Return: 0 on success, standard Linux error codes on failure/error.
1095 */
1096int geni_se_qupv3_hw_version(struct device *wrapper_dev, unsigned int *major,
1097 unsigned int *minor, unsigned int *step)
1098{
1099 unsigned int version;
1100 struct geni_se_device *geni_se_dev;
1101
1102 if (!wrapper_dev || !major || !minor || !step)
1103 return -EINVAL;
1104
1105 geni_se_dev = dev_get_drvdata(wrapper_dev);
1106 if (unlikely(!geni_se_dev))
1107 return -ENODEV;
1108
1109 version = geni_read_reg(geni_se_dev->base, QUPV3_HW_VER);
1110 *major = (version & HW_VER_MAJOR_MASK) >> HW_VER_MAJOR_SHFT;
1111 *minor = (version & HW_VER_MINOR_MASK) >> HW_VER_MINOR_SHFT;
1112 *step = version & HW_VER_STEP_MASK;
1113 return 0;
1114}
1115EXPORT_SYMBOL(geni_se_qupv3_hw_version);
1116
1117static int geni_se_iommu_map_and_attach(struct geni_se_device *geni_se_dev)
1118{
1119 dma_addr_t va_start = GENI_SE_IOMMU_VA_START;
1120 size_t va_size = GENI_SE_IOMMU_VA_SIZE;
1121 int bypass = 1;
1122 struct device *cb_dev = geni_se_dev->cb_dev;
1123
1124 mutex_lock(&geni_se_dev->iommu_lock);
1125 if (likely(geni_se_dev->iommu_map)) {
1126 mutex_unlock(&geni_se_dev->iommu_lock);
1127 return 0;
1128 }
1129
1130 geni_se_dev->iommu_map = arm_iommu_create_mapping(&platform_bus_type,
1131 va_start, va_size);
1132 if (IS_ERR(geni_se_dev->iommu_map)) {
1133 GENI_SE_ERR(geni_se_dev->log_ctx, false, NULL,
1134 "%s:%s iommu_create_mapping failure\n",
1135 __func__, dev_name(cb_dev));
1136 mutex_unlock(&geni_se_dev->iommu_lock);
1137 return PTR_ERR(geni_se_dev->iommu_map);
1138 }
1139
1140 if (geni_se_dev->iommu_s1_bypass) {
1141 if (iommu_domain_set_attr(geni_se_dev->iommu_map->domain,
1142 DOMAIN_ATTR_S1_BYPASS, &bypass)) {
1143 GENI_SE_ERR(geni_se_dev->log_ctx, false, NULL,
1144 "%s:%s Couldn't bypass s1 translation\n",
1145 __func__, dev_name(cb_dev));
1146 arm_iommu_release_mapping(geni_se_dev->iommu_map);
1147 geni_se_dev->iommu_map = NULL;
1148 mutex_unlock(&geni_se_dev->iommu_lock);
1149 return -EIO;
1150 }
1151 }
1152
1153 if (arm_iommu_attach_device(cb_dev, geni_se_dev->iommu_map)) {
1154 GENI_SE_ERR(geni_se_dev->log_ctx, false, NULL,
1155 "%s:%s couldn't arm_iommu_attach_device\n",
1156 __func__, dev_name(cb_dev));
1157 arm_iommu_release_mapping(geni_se_dev->iommu_map);
1158 geni_se_dev->iommu_map = NULL;
1159 mutex_unlock(&geni_se_dev->iommu_lock);
1160 return -EIO;
1161 }
1162 mutex_unlock(&geni_se_dev->iommu_lock);
1163 GENI_SE_DBG(geni_se_dev->log_ctx, false, NULL, "%s:%s successful\n",
1164 __func__, dev_name(cb_dev));
1165 return 0;
1166}
1167
1168/**
1169 * geni_se_iommu_map_buf() - Map a single buffer into QUPv3 context bank
1170 * @wrapper_dev: Pointer to the corresponding QUPv3 wrapper core.
1171 * @iova: Pointer in which the mapped virtual address is stored.
1172 * @buf: Address of the buffer that needs to be mapped.
1173 * @size: Size of the buffer.
1174 * @dir: Direction of the DMA transfer.
1175 *
1176 * This function is used to map an already allocated buffer into the
1177 * QUPv3 context bank device space.
1178 *
1179 * Return: 0 on success, standard Linux error codes on failure/error.
1180 */
1181int geni_se_iommu_map_buf(struct device *wrapper_dev, dma_addr_t *iova,
1182 void *buf, size_t size, enum dma_data_direction dir)
1183{
1184 struct device *cb_dev;
1185 struct geni_se_device *geni_se_dev;
1186
1187 if (!wrapper_dev || !iova || !buf || !size)
1188 return -EINVAL;
1189
1190 *iova = DMA_ERROR_CODE;
1191 geni_se_dev = dev_get_drvdata(wrapper_dev);
1192 if (!geni_se_dev || !geni_se_dev->cb_dev)
1193 return -ENODEV;
1194
1195 cb_dev = geni_se_dev->cb_dev;
1196
1197 *iova = dma_map_single(cb_dev, buf, size, dir);
1198 if (dma_mapping_error(cb_dev, *iova))
1199 return -EIO;
1200 return 0;
1201}
1202EXPORT_SYMBOL(geni_se_iommu_map_buf);
1203
1204/**
1205 * geni_se_iommu_alloc_buf() - Allocate & map a single buffer into QUPv3
1206 * context bank
1207 * @wrapper_dev: Pointer to the corresponding QUPv3 wrapper core.
1208 * @iova: Pointer in which the mapped virtual address is stored.
1209 * @size: Size of the buffer.
1210 *
1211 * This function is used to allocate a buffer and map it into the
1212 * QUPv3 context bank device space.
1213 *
1214 * Return: address of the buffer on success, NULL or ERR_PTR on
1215 * failure/error.
1216 */
1217void *geni_se_iommu_alloc_buf(struct device *wrapper_dev, dma_addr_t *iova,
1218 size_t size)
1219{
1220 struct device *cb_dev;
1221 struct geni_se_device *geni_se_dev;
1222 void *buf = NULL;
1223
1224 if (!wrapper_dev || !iova || !size)
1225 return ERR_PTR(-EINVAL);
1226
1227 *iova = DMA_ERROR_CODE;
1228 geni_se_dev = dev_get_drvdata(wrapper_dev);
1229 if (!geni_se_dev || !geni_se_dev->cb_dev)
1230 return ERR_PTR(-ENODEV);
1231
1232 cb_dev = geni_se_dev->cb_dev;
1233
1234 buf = dma_alloc_coherent(cb_dev, size, iova, GFP_KERNEL);
1235 if (!buf)
1236 GENI_SE_ERR(geni_se_dev->log_ctx, false, NULL,
1237 "%s: Failed dma_alloc_coherent\n", __func__);
1238 return buf;
1239}
1240EXPORT_SYMBOL(geni_se_iommu_alloc_buf);
1241
1242/**
1243 * geni_se_iommu_unmap_buf() - Unmap a single buffer from QUPv3 context bank
1244 * @wrapper_dev: Pointer to the corresponding QUPv3 wrapper core.
1245 * @iova: Pointer in which the mapped virtual address is stored.
1246 * @size: Size of the buffer.
1247 * @dir: Direction of the DMA transfer.
1248 *
1249 * This function is used to unmap an already mapped buffer from the
1250 * QUPv3 context bank device space.
1251 *
1252 * Return: 0 on success, standard Linux error codes on failure/error.
1253 */
1254int geni_se_iommu_unmap_buf(struct device *wrapper_dev, dma_addr_t *iova,
1255 size_t size, enum dma_data_direction dir)
1256{
1257 struct device *cb_dev;
1258 struct geni_se_device *geni_se_dev;
1259
1260 if (!wrapper_dev || !iova || !size)
1261 return -EINVAL;
1262
1263 geni_se_dev = dev_get_drvdata(wrapper_dev);
1264 if (!geni_se_dev || !geni_se_dev->cb_dev)
1265 return -ENODEV;
1266
1267 cb_dev = geni_se_dev->cb_dev;
1268
1269 dma_unmap_single(cb_dev, *iova, size, dir);
1270 return 0;
1271}
1272EXPORT_SYMBOL(geni_se_iommu_unmap_buf);
1273
1274/**
1275 * geni_se_iommu_free_buf() - Unmap & free a single buffer from QUPv3
1276 * context bank
1277 * @wrapper_dev: Pointer to the corresponding QUPv3 wrapper core.
1278 * @iova: Pointer in which the mapped virtual address is stored.
1279 * @buf: Address of the buffer.
1280 * @size: Size of the buffer.
1281 *
1282 * This function is used to unmap and free a buffer from the
1283 * QUPv3 context bank device space.
1284 *
1285 * Return: 0 on success, standard Linux error codes on failure/error.
1286 */
1287int geni_se_iommu_free_buf(struct device *wrapper_dev, dma_addr_t *iova,
1288 void *buf, size_t size)
1289{
1290 struct device *cb_dev;
1291 struct geni_se_device *geni_se_dev;
1292
1293 if (!wrapper_dev || !iova || !buf || !size)
1294 return -EINVAL;
1295
1296 geni_se_dev = dev_get_drvdata(wrapper_dev);
1297 if (!geni_se_dev || !geni_se_dev->cb_dev)
1298 return -ENODEV;
1299
1300 cb_dev = geni_se_dev->cb_dev;
1301
1302 dma_free_coherent(cb_dev, size, buf, *iova);
1303 return 0;
1304}
1305EXPORT_SYMBOL(geni_se_iommu_free_buf);
1306
Girish Mahadevan3b7e9742017-09-15 15:17:16 -06001307/**
1308 * geni_se_dump_dbg_regs() - Print relevant registers that capture most
1309 * accurately the state of an SE.
1310 * @_dev: Pointer to the SE's device.
1311 * @iomem: Base address of the SE's register space.
1312 * @ipc: IPC log context handle.
1313 *
1314 * This function is used to print out all the registers that capture the state
1315 * of an SE to help debug any errors.
1316 *
1317 * Return: None
1318 */
1319void geni_se_dump_dbg_regs(struct se_geni_rsc *rsc, void __iomem *base,
1320 void *ipc)
1321{
1322 u32 m_cmd0 = 0;
1323 u32 m_irq_status = 0;
1324 u32 geni_status = 0;
1325 u32 geni_ios = 0;
1326 u32 dma_rx_irq = 0;
1327 u32 dma_tx_irq = 0;
1328 u32 rx_fifo_status = 0;
1329 u32 tx_fifo_status = 0;
1330 u32 se_dma_dbg = 0;
1331 u32 m_cmd_ctrl = 0;
1332 u32 se_dma_rx_len = 0;
1333 u32 se_dma_rx_len_in = 0;
1334 u32 se_dma_tx_len = 0;
1335 u32 se_dma_tx_len_in = 0;
1336 struct geni_se_device *geni_se_dev;
1337
1338 if (!ipc)
1339 return;
1340
1341 geni_se_dev = dev_get_drvdata(rsc->wrapper_dev);
1342 if (unlikely(!geni_se_dev || !geni_se_dev->bus_bw))
1343 return;
Girish Mahadevan3b7e9742017-09-15 15:17:16 -06001344 if (unlikely(list_empty(&rsc->ab_list) || list_empty(&rsc->ib_list))) {
1345 GENI_SE_DBG(ipc, false, NULL, "%s: Clocks not on\n", __func__);
Girish Mahadevan0ef15632017-10-05 07:53:46 -06001346 return;
Girish Mahadevan3b7e9742017-09-15 15:17:16 -06001347 }
1348 m_cmd0 = geni_read_reg(base, SE_GENI_M_CMD0);
1349 m_irq_status = geni_read_reg(base, SE_GENI_M_IRQ_STATUS);
1350 geni_status = geni_read_reg(base, SE_GENI_STATUS);
1351 geni_ios = geni_read_reg(base, SE_GENI_IOS);
1352 dma_rx_irq = geni_read_reg(base, SE_DMA_TX_IRQ_STAT);
1353 dma_tx_irq = geni_read_reg(base, SE_DMA_RX_IRQ_STAT);
1354 rx_fifo_status = geni_read_reg(base, SE_GENI_RX_FIFO_STATUS);
1355 tx_fifo_status = geni_read_reg(base, SE_GENI_TX_FIFO_STATUS);
1356 se_dma_dbg = geni_read_reg(base, SE_DMA_DEBUG_REG0);
1357 m_cmd_ctrl = geni_read_reg(base, SE_GENI_M_CMD_CTRL_REG);
1358 se_dma_rx_len = geni_read_reg(base, SE_DMA_RX_LEN);
1359 se_dma_rx_len_in = geni_read_reg(base, SE_DMA_RX_LEN_IN);
1360 se_dma_tx_len = geni_read_reg(base, SE_DMA_TX_LEN);
1361 se_dma_tx_len_in = geni_read_reg(base, SE_DMA_TX_LEN_IN);
1362
1363 GENI_SE_DBG(ipc, false, NULL,
1364 "%s: m_cmd0:0x%x, m_irq_status:0x%x, geni_status:0x%x, geni_ios:0x%x\n",
1365 __func__, m_cmd0, m_irq_status, geni_status, geni_ios);
1366 GENI_SE_DBG(ipc, false, NULL,
1367 "dma_rx_irq:0x%x, dma_tx_irq:0x%x, rx_fifo_sts:0x%x, tx_fifo_sts:0x%x\n"
1368 , dma_rx_irq, dma_tx_irq, rx_fifo_status, tx_fifo_status);
1369 GENI_SE_DBG(ipc, false, NULL,
1370 "se_dma_dbg:0x%x, m_cmd_ctrl:0x%x, dma_rxlen:0x%x, dma_rxlen_in:0x%x\n",
1371 se_dma_dbg, m_cmd_ctrl, se_dma_rx_len, se_dma_rx_len_in);
1372 GENI_SE_DBG(ipc, false, NULL,
1373 "dma_txlen:0x%x, dma_txlen_in:0x%x\n", se_dma_tx_len, se_dma_tx_len_in);
Girish Mahadevan3b7e9742017-09-15 15:17:16 -06001374}
1375EXPORT_SYMBOL(geni_se_dump_dbg_regs);
1376
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -06001377static const struct of_device_id geni_se_dt_match[] = {
1378 { .compatible = "qcom,qupv3-geni-se", },
1379 { .compatible = "qcom,qupv3-geni-se-cb", },
1380 {}
1381};
1382
1383static int geni_se_iommu_probe(struct device *dev)
1384{
1385 struct geni_se_device *geni_se_dev;
1386
1387 if (unlikely(!dev->parent)) {
1388 dev_err(dev, "%s no parent for this device\n", __func__);
1389 return -EINVAL;
1390 }
1391
1392 geni_se_dev = dev_get_drvdata(dev->parent);
1393 if (unlikely(!geni_se_dev)) {
1394 dev_err(dev, "%s geni_se_dev not found\n", __func__);
1395 return -EINVAL;
1396 }
1397 geni_se_dev->cb_dev = dev;
1398
1399 GENI_SE_DBG(geni_se_dev->log_ctx, false, NULL,
1400 "%s: Probe successful\n", __func__);
1401 return 0;
1402}
1403
1404static int geni_se_probe(struct platform_device *pdev)
1405{
1406 int ret;
1407 struct device *dev = &pdev->dev;
1408 struct resource *res;
1409 struct geni_se_device *geni_se_dev;
1410
1411 if (of_device_is_compatible(dev->of_node, "qcom,qupv3-geni-se-cb"))
1412 return geni_se_iommu_probe(dev);
1413
1414 geni_se_dev = devm_kzalloc(dev, sizeof(*geni_se_dev), GFP_KERNEL);
1415 if (!geni_se_dev)
1416 return -ENOMEM;
1417
1418 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1419 if (!res) {
1420 dev_err(dev, "%s: Mandatory resource info not found\n",
1421 __func__);
1422 devm_kfree(dev, geni_se_dev);
1423 return -EINVAL;
1424 }
1425
1426 geni_se_dev->base = devm_ioremap_resource(dev, res);
1427 if (IS_ERR_OR_NULL(geni_se_dev->base)) {
1428 dev_err(dev, "%s: Error mapping the resource\n", __func__);
1429 devm_kfree(dev, geni_se_dev);
1430 return -EFAULT;
1431 }
1432
1433 geni_se_dev->dev = dev;
1434 ret = of_property_read_u32(dev->of_node, "qcom,bus-mas-id",
1435 &geni_se_dev->bus_mas_id);
1436 if (ret) {
1437 dev_err(dev, "%s: Error missing bus master id\n", __func__);
1438 devm_iounmap(dev, geni_se_dev->base);
1439 devm_kfree(dev, geni_se_dev);
1440 }
1441 ret = of_property_read_u32(dev->of_node, "qcom,bus-slv-id",
1442 &geni_se_dev->bus_slv_id);
1443 if (ret) {
1444 dev_err(dev, "%s: Error missing bus slave id\n", __func__);
1445 devm_iounmap(dev, geni_se_dev->base);
1446 devm_kfree(dev, geni_se_dev);
1447 }
1448
1449 geni_se_dev->iommu_s1_bypass = of_property_read_bool(dev->of_node,
1450 "qcom,iommu-s1-bypass");
1451 geni_se_dev->bus_bw_set = default_bus_bw_set;
1452 geni_se_dev->bus_bw_set_size = ARRAY_SIZE(default_bus_bw_set);
1453 mutex_init(&geni_se_dev->iommu_lock);
1454 INIT_LIST_HEAD(&geni_se_dev->ab_list_head);
1455 INIT_LIST_HEAD(&geni_se_dev->ib_list_head);
Girish Mahadevand11aefc2017-11-30 15:41:19 -07001456 mutex_init(&geni_se_dev->geni_dev_lock);
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -06001457 geni_se_dev->log_ctx = ipc_log_context_create(NUM_LOG_PAGES,
1458 dev_name(geni_se_dev->dev), 0);
1459 if (!geni_se_dev->log_ctx)
1460 dev_err(dev, "%s Failed to allocate log context\n", __func__);
1461 dev_set_drvdata(dev, geni_se_dev);
1462
1463 ret = of_platform_populate(dev->of_node, geni_se_dt_match, NULL, dev);
1464 if (ret) {
1465 dev_err(dev, "%s: Error populating children\n", __func__);
1466 devm_iounmap(dev, geni_se_dev->base);
1467 devm_kfree(dev, geni_se_dev);
1468 }
1469
1470 GENI_SE_DBG(geni_se_dev->log_ctx, false, NULL,
1471 "%s: Probe successful\n", __func__);
1472 return ret;
1473}
1474
1475static int geni_se_remove(struct platform_device *pdev)
1476{
1477 struct device *dev = &pdev->dev;
1478 struct geni_se_device *geni_se_dev = dev_get_drvdata(dev);
1479
1480 if (likely(!IS_ERR_OR_NULL(geni_se_dev->iommu_map))) {
1481 arm_iommu_detach_device(geni_se_dev->cb_dev);
1482 arm_iommu_release_mapping(geni_se_dev->iommu_map);
1483 }
1484 ipc_log_context_destroy(geni_se_dev->log_ctx);
1485 devm_iounmap(dev, geni_se_dev->base);
1486 devm_kfree(dev, geni_se_dev);
1487 return 0;
1488}
1489
1490static struct platform_driver geni_se_driver = {
1491 .driver = {
1492 .name = "qupv3_geni_se",
1493 .of_match_table = geni_se_dt_match,
1494 },
1495 .probe = geni_se_probe,
1496 .remove = geni_se_remove,
1497};
1498
1499static int __init geni_se_driver_init(void)
1500{
1501 return platform_driver_register(&geni_se_driver);
1502}
1503arch_initcall(geni_se_driver_init);
1504
1505static void __exit geni_se_driver_exit(void)
1506{
1507 platform_driver_unregister(&geni_se_driver);
1508}
1509module_exit(geni_se_driver_exit);
1510
1511MODULE_DESCRIPTION("GENI Serial Engine Driver");
1512MODULE_LICENSE("GPL v2");