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