blob: ed4b837017fed0f118a6f84f0403bc60ffe51e24 [file] [log] [blame]
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -06001/*
Mukesh Kumar Savaliya17692d92018-04-12 23:36:27 +05302 * Copyright (c) 2017-2018, The Linux Foundation. All rights reserved.
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -06003 *
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
Mukesh Kumar Savaliya17692d92018-04-12 23:36:27 +0530168/**
169 * get_se_m_fw() - Read the Firmware ver for the Main seqeuncer engine
170 * @base: Base address of the serial engine's register block.
171 *
172 * Return: Firmware version for the Main seqeuncer engine
173 */
174int get_se_m_fw(void __iomem *base)
175{
176 int fw_ver_m;
177
178 fw_ver_m = ((geni_read_reg(base, GENI_FW_REVISION_RO)
179 & FW_REV_VERSION_MSK));
180 return fw_ver_m;
181}
182EXPORT_SYMBOL(get_se_m_fw);
183
184/**
185 * get_se_s_fw() - Read the Firmware ver for the Secondry seqeuncer engine
186 * @base: Base address of the serial engine's register block.
187 *
188 * Return: Firmware version for the Secondry seqeuncer engine
189 */
190int get_se_s_fw(void __iomem *base)
191{
192 int fw_ver_s;
193
194 fw_ver_s = ((geni_read_reg(base, GENI_FW_S_REVISION_RO)
195 & FW_REV_VERSION_MSK));
196 return fw_ver_s;
197}
198EXPORT_SYMBOL(get_se_s_fw);
199
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600200static int se_geni_irq_en(void __iomem *base)
201{
202 unsigned int common_geni_m_irq_en;
203 unsigned int common_geni_s_irq_en;
204
205 common_geni_m_irq_en = geni_read_reg(base, SE_GENI_M_IRQ_EN);
206 common_geni_s_irq_en = geni_read_reg(base, SE_GENI_S_IRQ_EN);
207 /* Common to all modes */
208 common_geni_m_irq_en |= M_COMMON_GENI_M_IRQ_EN;
209 common_geni_s_irq_en |= S_COMMON_GENI_S_IRQ_EN;
210
211 geni_write_reg(common_geni_m_irq_en, base, SE_GENI_M_IRQ_EN);
212 geni_write_reg(common_geni_s_irq_en, base, SE_GENI_S_IRQ_EN);
213 return 0;
214}
215
216
217static void se_set_rx_rfr_wm(void __iomem *base, unsigned int rx_wm,
218 unsigned int rx_rfr)
219{
220 geni_write_reg(rx_wm, base, SE_GENI_RX_WATERMARK_REG);
221 geni_write_reg(rx_rfr, base, SE_GENI_RX_RFR_WATERMARK_REG);
222}
223
224static int se_io_set_mode(void __iomem *base)
225{
226 unsigned int io_mode;
227 unsigned int geni_dma_mode;
228
229 io_mode = geni_read_reg(base, SE_IRQ_EN);
230 geni_dma_mode = geni_read_reg(base, SE_GENI_DMA_MODE_EN);
231
232 io_mode |= (GENI_M_IRQ_EN | GENI_S_IRQ_EN);
233 io_mode |= (DMA_TX_IRQ_EN | DMA_RX_IRQ_EN);
234 geni_dma_mode &= ~GENI_DMA_MODE_EN;
235
236 geni_write_reg(io_mode, base, SE_IRQ_EN);
237 geni_write_reg(geni_dma_mode, base, SE_GENI_DMA_MODE_EN);
238 geni_write_reg(0, base, SE_GSI_EVENT_EN);
239 return 0;
240}
241
242static void se_io_init(void __iomem *base)
243{
244 unsigned int io_op_ctrl;
245 unsigned int geni_cgc_ctrl;
246 unsigned int dma_general_cfg;
247
248 geni_cgc_ctrl = geni_read_reg(base, GENI_CGC_CTRL);
249 dma_general_cfg = geni_read_reg(base, SE_DMA_GENERAL_CFG);
250 geni_cgc_ctrl |= DEFAULT_CGC_EN;
251 dma_general_cfg |= (AHB_SEC_SLV_CLK_CGC_ON | DMA_AHB_SLV_CFG_ON |
252 DMA_TX_CLK_CGC_ON | DMA_RX_CLK_CGC_ON);
253 io_op_ctrl = DEFAULT_IO_OUTPUT_CTRL_MSK;
254 geni_write_reg(geni_cgc_ctrl, base, GENI_CGC_CTRL);
255 geni_write_reg(dma_general_cfg, base, SE_DMA_GENERAL_CFG);
256
257 geni_write_reg(io_op_ctrl, base, GENI_OUTPUT_CTRL);
258 geni_write_reg(FORCE_DEFAULT, base, GENI_FORCE_DEFAULT_REG);
259}
260
261/**
262 * geni_se_init() - Initialize the GENI Serial Engine
263 * @base: Base address of the serial engine's register block.
264 * @rx_wm: Receive watermark to be configured.
265 * @rx_rfr_wm: Ready-for-receive watermark to be configured.
266 *
267 * This function is used to initialize the GENI serial engine, configure
268 * receive watermark and ready-for-receive watermarks.
269 *
270 * Return: 0 on success, standard Linux error codes on failure/error.
271 */
272int geni_se_init(void __iomem *base, unsigned int rx_wm, unsigned int rx_rfr)
273{
274 int ret;
275
276 se_io_init(base);
277 ret = se_io_set_mode(base);
278 if (ret)
279 return ret;
280
281 se_set_rx_rfr_wm(base, rx_wm, rx_rfr);
282 ret = se_geni_irq_en(base);
283 return ret;
284}
285EXPORT_SYMBOL(geni_se_init);
286
287static int geni_se_select_fifo_mode(void __iomem *base)
288{
289 int proto = get_se_proto(base);
290 unsigned int common_geni_m_irq_en;
291 unsigned int common_geni_s_irq_en;
292 unsigned int geni_dma_mode;
293
294 geni_write_reg(0, base, SE_GSI_EVENT_EN);
295 geni_write_reg(0xFFFFFFFF, base, SE_GENI_M_IRQ_CLEAR);
296 geni_write_reg(0xFFFFFFFF, base, SE_GENI_S_IRQ_CLEAR);
297 geni_write_reg(0xFFFFFFFF, base, SE_DMA_TX_IRQ_CLR);
298 geni_write_reg(0xFFFFFFFF, base, SE_DMA_RX_IRQ_CLR);
299 geni_write_reg(0xFFFFFFFF, base, SE_IRQ_EN);
300
301 common_geni_m_irq_en = geni_read_reg(base, SE_GENI_M_IRQ_EN);
302 common_geni_s_irq_en = geni_read_reg(base, SE_GENI_S_IRQ_EN);
303 geni_dma_mode = geni_read_reg(base, SE_GENI_DMA_MODE_EN);
304 if (proto != UART) {
305 common_geni_m_irq_en |=
306 (M_CMD_DONE_EN | M_TX_FIFO_WATERMARK_EN |
307 M_RX_FIFO_WATERMARK_EN | M_RX_FIFO_LAST_EN);
308 common_geni_s_irq_en |= S_CMD_DONE_EN;
309 }
310 geni_dma_mode &= ~GENI_DMA_MODE_EN;
311
312 geni_write_reg(common_geni_m_irq_en, base, SE_GENI_M_IRQ_EN);
313 geni_write_reg(common_geni_s_irq_en, base, SE_GENI_S_IRQ_EN);
314 geni_write_reg(geni_dma_mode, base, SE_GENI_DMA_MODE_EN);
315 return 0;
316}
317
318static int geni_se_select_dma_mode(void __iomem *base)
319{
Shrey Vijay317d9a52018-05-09 15:11:49 +0530320 int proto = get_se_proto(base);
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600321 unsigned int geni_dma_mode = 0;
Shrey Vijay317d9a52018-05-09 15:11:49 +0530322 unsigned int common_geni_m_irq_en;
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600323
324 geni_write_reg(0, base, SE_GSI_EVENT_EN);
325 geni_write_reg(0xFFFFFFFF, base, SE_GENI_M_IRQ_CLEAR);
326 geni_write_reg(0xFFFFFFFF, base, SE_GENI_S_IRQ_CLEAR);
327 geni_write_reg(0xFFFFFFFF, base, SE_DMA_TX_IRQ_CLR);
328 geni_write_reg(0xFFFFFFFF, base, SE_DMA_RX_IRQ_CLR);
329 geni_write_reg(0xFFFFFFFF, base, SE_IRQ_EN);
330
Shrey Vijay317d9a52018-05-09 15:11:49 +0530331 common_geni_m_irq_en = geni_read_reg(base, SE_GENI_M_IRQ_EN);
332 if (proto != UART)
333 common_geni_m_irq_en &=
334 ~(M_TX_FIFO_WATERMARK_EN | M_RX_FIFO_WATERMARK_EN);
335
336 geni_write_reg(common_geni_m_irq_en, base, SE_GENI_M_IRQ_EN);
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600337 geni_dma_mode = geni_read_reg(base, SE_GENI_DMA_MODE_EN);
338 geni_dma_mode |= GENI_DMA_MODE_EN;
339 geni_write_reg(geni_dma_mode, base, SE_GENI_DMA_MODE_EN);
340 return 0;
341}
342
343static int geni_se_select_gsi_mode(void __iomem *base)
344{
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600345 unsigned int geni_dma_mode = 0;
346 unsigned int gsi_event_en = 0;
Girish Mahadevanaf5f2bc2017-08-15 12:05:40 -0600347 unsigned int common_geni_m_irq_en = 0;
348 unsigned int common_geni_s_irq_en = 0;
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600349
Girish Mahadevanaf5f2bc2017-08-15 12:05:40 -0600350 common_geni_m_irq_en = geni_read_reg(base, SE_GENI_M_IRQ_EN);
351 common_geni_s_irq_en = geni_read_reg(base, SE_GENI_S_IRQ_EN);
352 common_geni_m_irq_en &=
353 ~(M_CMD_DONE_EN | M_TX_FIFO_WATERMARK_EN |
354 M_RX_FIFO_WATERMARK_EN | M_RX_FIFO_LAST_EN);
355 common_geni_s_irq_en &= ~S_CMD_DONE_EN;
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600356 geni_dma_mode = geni_read_reg(base, SE_GENI_DMA_MODE_EN);
357 gsi_event_en = geni_read_reg(base, SE_GSI_EVENT_EN);
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600358
359 geni_dma_mode |= GENI_DMA_MODE_EN;
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600360 gsi_event_en |= (DMA_RX_EVENT_EN | DMA_TX_EVENT_EN |
361 GENI_M_EVENT_EN | GENI_S_EVENT_EN);
362
Girish Mahadevanaf5f2bc2017-08-15 12:05:40 -0600363 geni_write_reg(0, base, SE_IRQ_EN);
364 geni_write_reg(common_geni_s_irq_en, base, SE_GENI_S_IRQ_EN);
365 geni_write_reg(common_geni_m_irq_en, base, SE_GENI_M_IRQ_EN);
366 geni_write_reg(0xFFFFFFFF, base, SE_GENI_M_IRQ_CLEAR);
367 geni_write_reg(0xFFFFFFFF, base, SE_GENI_S_IRQ_CLEAR);
368 geni_write_reg(0xFFFFFFFF, base, SE_DMA_TX_IRQ_CLR);
369 geni_write_reg(0xFFFFFFFF, base, SE_DMA_RX_IRQ_CLR);
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600370 geni_write_reg(geni_dma_mode, base, SE_GENI_DMA_MODE_EN);
371 geni_write_reg(gsi_event_en, base, SE_GSI_EVENT_EN);
372 return 0;
373
374}
375
376/**
377 * geni_se_select_mode() - Select the serial engine transfer mode
378 * @base: Base address of the serial engine's register block.
379 * @mode: Transfer mode to be selected.
380 *
381 * Return: 0 on success, standard Linux error codes on failure.
382 */
383int geni_se_select_mode(void __iomem *base, int mode)
384{
385 int ret = 0;
386
387 switch (mode) {
388 case FIFO_MODE:
389 geni_se_select_fifo_mode(base);
390 break;
391 case SE_DMA:
392 geni_se_select_dma_mode(base);
393 break;
394 case GSI_DMA:
395 geni_se_select_gsi_mode(base);
396 break;
397 default:
398 ret = -EINVAL;
399 break;
400 }
401
402 return ret;
403}
404EXPORT_SYMBOL(geni_se_select_mode);
405
406/**
407 * geni_setup_m_cmd() - Setup the primary sequencer
408 * @base: Base address of the serial engine's register block.
409 * @cmd: Command/Operation to setup in the primary sequencer.
410 * @params: Parameter for the sequencer command.
411 *
412 * This function is used to configure the primary sequencer with the
413 * command and its assoicated parameters.
414 */
415void geni_setup_m_cmd(void __iomem *base, u32 cmd, u32 params)
416{
417 u32 m_cmd = (cmd << M_OPCODE_SHFT);
418
419 m_cmd |= (params & M_PARAMS_MSK);
420 geni_write_reg(m_cmd, base, SE_GENI_M_CMD0);
421}
422EXPORT_SYMBOL(geni_setup_m_cmd);
423
424/**
425 * geni_setup_s_cmd() - Setup the secondary sequencer
426 * @base: Base address of the serial engine's register block.
427 * @cmd: Command/Operation to setup in the secondary sequencer.
428 * @params: Parameter for the sequencer command.
429 *
430 * This function is used to configure the secondary sequencer with the
431 * command and its assoicated parameters.
432 */
433void geni_setup_s_cmd(void __iomem *base, u32 cmd, u32 params)
434{
435 u32 s_cmd = geni_read_reg(base, SE_GENI_S_CMD0);
436
437 s_cmd &= ~(S_OPCODE_MSK | S_PARAMS_MSK);
438 s_cmd |= (cmd << S_OPCODE_SHFT);
439 s_cmd |= (params & S_PARAMS_MSK);
440 geni_write_reg(s_cmd, base, SE_GENI_S_CMD0);
441}
442EXPORT_SYMBOL(geni_setup_s_cmd);
443
444/**
445 * geni_cancel_m_cmd() - Cancel the command configured in the primary sequencer
446 * @base: Base address of the serial engine's register block.
447 *
448 * This function is used to cancel the currently configured command in the
449 * primary sequencer.
450 */
451void geni_cancel_m_cmd(void __iomem *base)
452{
Girish Mahadevan6d97cbc2017-11-07 15:28:27 -0700453 geni_write_reg(M_GENI_CMD_CANCEL, base, SE_GENI_M_CMD_CTRL_REG);
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600454}
455EXPORT_SYMBOL(geni_cancel_m_cmd);
456
457/**
458 * geni_cancel_s_cmd() - Cancel the command configured in the secondary
459 * sequencer
460 * @base: Base address of the serial engine's register block.
461 *
462 * This function is used to cancel the currently configured command in the
463 * secondary sequencer.
464 */
465void geni_cancel_s_cmd(void __iomem *base)
466{
467 geni_write_reg(S_GENI_CMD_CANCEL, base, SE_GENI_S_CMD_CTRL_REG);
468}
469EXPORT_SYMBOL(geni_cancel_s_cmd);
470
471/**
472 * geni_abort_m_cmd() - Abort the command configured in the primary sequencer
473 * @base: Base address of the serial engine's register block.
474 *
475 * This function is used to force abort the currently configured command in the
476 * primary sequencer.
477 */
478void geni_abort_m_cmd(void __iomem *base)
479{
480 geni_write_reg(M_GENI_CMD_ABORT, base, SE_GENI_M_CMD_CTRL_REG);
481}
482EXPORT_SYMBOL(geni_abort_m_cmd);
483
484/**
485 * geni_abort_s_cmd() - Abort the command configured in the secondary
486 * sequencer
487 * @base: Base address of the serial engine's register block.
488 *
489 * This function is used to force abort the currently configured command in the
490 * secondary sequencer.
491 */
492void geni_abort_s_cmd(void __iomem *base)
493{
494 geni_write_reg(S_GENI_CMD_ABORT, base, SE_GENI_S_CMD_CTRL_REG);
495}
496EXPORT_SYMBOL(geni_abort_s_cmd);
497
498/**
499 * get_tx_fifo_depth() - Get the TX fifo depth of the serial engine
500 * @base: Base address of the serial engine's register block.
501 *
502 * This function is used to get the depth i.e. number of elements in the
503 * TX fifo of the serial engine.
504 *
505 * Return: TX fifo depth in units of FIFO words.
506 */
507int get_tx_fifo_depth(void __iomem *base)
508{
509 int tx_fifo_depth;
510
511 tx_fifo_depth = ((geni_read_reg(base, SE_HW_PARAM_0)
512 & TX_FIFO_DEPTH_MSK) >> TX_FIFO_DEPTH_SHFT);
513 return tx_fifo_depth;
514}
515EXPORT_SYMBOL(get_tx_fifo_depth);
516
517/**
518 * get_tx_fifo_width() - Get the TX fifo width of the serial engine
519 * @base: Base address of the serial engine's register block.
520 *
521 * This function is used to get the width i.e. word size per element in the
522 * TX fifo of the serial engine.
523 *
524 * Return: TX fifo width in bits
525 */
526int get_tx_fifo_width(void __iomem *base)
527{
528 int tx_fifo_width;
529
530 tx_fifo_width = ((geni_read_reg(base, SE_HW_PARAM_0)
531 & TX_FIFO_WIDTH_MSK) >> TX_FIFO_WIDTH_SHFT);
532 return tx_fifo_width;
533}
534EXPORT_SYMBOL(get_tx_fifo_width);
535
536/**
537 * get_rx_fifo_depth() - Get the RX fifo depth of the serial engine
538 * @base: Base address of the serial engine's register block.
539 *
540 * This function is used to get the depth i.e. number of elements in the
541 * RX fifo of the serial engine.
542 *
543 * Return: RX fifo depth in units of FIFO words
544 */
545int get_rx_fifo_depth(void __iomem *base)
546{
547 int rx_fifo_depth;
548
549 rx_fifo_depth = ((geni_read_reg(base, SE_HW_PARAM_1)
550 & RX_FIFO_DEPTH_MSK) >> RX_FIFO_DEPTH_SHFT);
551 return rx_fifo_depth;
552}
553EXPORT_SYMBOL(get_rx_fifo_depth);
554
555/**
556 * se_get_packing_config() - Get the packing configuration based on input
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 * @cfg0: Output buffer to hold the first half of configuration.
561 * @cfg1: Output buffer to hold the second half of configuration.
562 *
563 * This function is used to calculate the packing configuration based on
564 * the input packing requirement and the configuration logic.
565 */
566void se_get_packing_config(int bpw, int pack_words, bool msb_to_lsb,
567 unsigned long *cfg0, unsigned long *cfg1)
568{
569 u32 cfg[4] = {0};
570 int len;
571 int temp_bpw = bpw;
572 int idx_start = (msb_to_lsb ? (bpw - 1) : 0);
573 int idx = idx_start;
574 int idx_delta = (msb_to_lsb ? -BITS_PER_BYTE : BITS_PER_BYTE);
575 int ceil_bpw = ((bpw & (BITS_PER_BYTE - 1)) ?
576 ((bpw & ~(BITS_PER_BYTE - 1)) + BITS_PER_BYTE) : bpw);
577 int iter = (ceil_bpw * pack_words) >> 3;
578 int i;
579
580 if (unlikely(iter <= 0 || iter > 4)) {
581 *cfg0 = 0;
582 *cfg1 = 0;
583 return;
584 }
585
586 for (i = 0; i < iter; i++) {
587 len = (temp_bpw < BITS_PER_BYTE) ?
588 (temp_bpw - 1) : BITS_PER_BYTE - 1;
589 cfg[i] = ((idx << 5) | (msb_to_lsb << 4) | (len << 1));
590 idx = ((temp_bpw - BITS_PER_BYTE) <= 0) ?
591 ((i + 1) * BITS_PER_BYTE) + idx_start :
592 idx + idx_delta;
593 temp_bpw = ((temp_bpw - BITS_PER_BYTE) <= 0) ?
594 bpw : (temp_bpw - BITS_PER_BYTE);
595 }
596 cfg[iter - 1] |= 1;
597 *cfg0 = cfg[0] | (cfg[1] << 10);
598 *cfg1 = cfg[2] | (cfg[3] << 10);
599}
600EXPORT_SYMBOL(se_get_packing_config);
601
602/**
603 * se_config_packing() - Packing configuration of the serial engine
604 * @base: Base address of the serial engine's register block.
605 * @bpw: Bits of data per transfer word.
606 * @pack_words: Number of words per fifo element.
607 * @msb_to_lsb: Transfer from MSB to LSB or vice-versa.
608 *
609 * This function is used to configure the packing rules for the current
610 * transfer.
611 */
612void se_config_packing(void __iomem *base, int bpw,
613 int pack_words, bool msb_to_lsb)
614{
615 unsigned long cfg0, cfg1;
616
617 se_get_packing_config(bpw, pack_words, msb_to_lsb, &cfg0, &cfg1);
618 geni_write_reg(cfg0, base, SE_GENI_TX_PACKING_CFG0);
619 geni_write_reg(cfg1, base, SE_GENI_TX_PACKING_CFG1);
620 geni_write_reg(cfg0, base, SE_GENI_RX_PACKING_CFG0);
621 geni_write_reg(cfg1, base, SE_GENI_RX_PACKING_CFG1);
622 if (pack_words || bpw == 32)
623 geni_write_reg((bpw >> 4), base, SE_GENI_BYTE_GRAN);
624}
625EXPORT_SYMBOL(se_config_packing);
626
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600627static bool geni_se_check_bus_bw(struct geni_se_device *geni_se_dev)
628{
629 int i;
630 int new_bus_bw_idx = geni_se_dev->bus_bw_set_size - 1;
631 unsigned long new_bus_bw;
632 bool bus_bw_update = false;
633
634 new_bus_bw = max(geni_se_dev->cur_ib, geni_se_dev->cur_ab) /
635 DEFAULT_BUS_WIDTH;
636 for (i = 0; i < geni_se_dev->bus_bw_set_size; i++) {
637 if (geni_se_dev->bus_bw_set[i] >= new_bus_bw) {
638 new_bus_bw_idx = i;
639 break;
640 }
641 }
642
643 if (geni_se_dev->cur_bus_bw_idx != new_bus_bw_idx) {
644 geni_se_dev->cur_bus_bw_idx = new_bus_bw_idx;
645 bus_bw_update = true;
646 }
647 return bus_bw_update;
648}
649
650static int geni_se_rmv_ab_ib(struct geni_se_device *geni_se_dev,
651 struct se_geni_rsc *rsc)
652{
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600653 struct se_geni_rsc *tmp;
654 bool bus_bw_update = false;
655 int ret = 0;
656
657 if (unlikely(list_empty(&rsc->ab_list) || list_empty(&rsc->ib_list)))
658 return -EINVAL;
659
Girish Mahadevand11aefc2017-11-30 15:41:19 -0700660 mutex_lock(&geni_se_dev->geni_dev_lock);
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600661 list_del_init(&rsc->ab_list);
662 geni_se_dev->cur_ab -= rsc->ab;
663
664 list_del_init(&rsc->ib_list);
665 tmp = list_first_entry_or_null(&geni_se_dev->ib_list_head,
666 struct se_geni_rsc, ib_list);
667 if (tmp && tmp->ib != geni_se_dev->cur_ib)
668 geni_se_dev->cur_ib = tmp->ib;
669 else if (!tmp && geni_se_dev->cur_ib)
670 geni_se_dev->cur_ib = 0;
671
672 bus_bw_update = geni_se_check_bus_bw(geni_se_dev);
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600673 if (bus_bw_update)
674 ret = msm_bus_scale_update_bw(geni_se_dev->bus_bw,
675 geni_se_dev->cur_ab,
676 geni_se_dev->cur_ib);
677 GENI_SE_DBG(geni_se_dev->log_ctx, false, NULL,
678 "%s: %lu:%lu (%lu:%lu) %d\n", __func__,
679 geni_se_dev->cur_ab, geni_se_dev->cur_ib,
680 rsc->ab, rsc->ib, bus_bw_update);
Girish Mahadevand11aefc2017-11-30 15:41:19 -0700681 mutex_unlock(&geni_se_dev->geni_dev_lock);
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600682 return ret;
683}
684
685/**
Karthikeyan Ramasubramanian383b41e2017-10-25 17:32:52 -0600686 * se_geni_clks_off() - Turn off clocks associated with the serial
687 * engine
688 * @rsc: Handle to resources associated with the serial engine.
689 *
690 * Return: 0 on success, standard Linux error codes on failure/error.
691 */
692int se_geni_clks_off(struct se_geni_rsc *rsc)
693{
694 int ret = 0;
695 struct geni_se_device *geni_se_dev;
696
697 if (unlikely(!rsc || !rsc->wrapper_dev))
698 return -EINVAL;
699
700 geni_se_dev = dev_get_drvdata(rsc->wrapper_dev);
701 if (unlikely(!geni_se_dev || !geni_se_dev->bus_bw))
702 return -ENODEV;
703
704 clk_disable_unprepare(rsc->se_clk);
705 clk_disable_unprepare(rsc->s_ahb_clk);
706 clk_disable_unprepare(rsc->m_ahb_clk);
707
708 ret = geni_se_rmv_ab_ib(geni_se_dev, rsc);
709 if (ret)
710 GENI_SE_ERR(geni_se_dev->log_ctx, false, NULL,
711 "%s: Error %d during bus_bw_update\n", __func__, ret);
712 return ret;
713}
714EXPORT_SYMBOL(se_geni_clks_off);
715
716/**
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600717 * se_geni_resources_off() - Turn off resources associated with the serial
718 * engine
719 * @rsc: Handle to resources associated with the serial engine.
720 *
721 * Return: 0 on success, standard Linux error codes on failure/error.
722 */
723int se_geni_resources_off(struct se_geni_rsc *rsc)
724{
725 int ret = 0;
726 struct geni_se_device *geni_se_dev;
727
728 if (unlikely(!rsc || !rsc->wrapper_dev))
729 return -EINVAL;
730
731 geni_se_dev = dev_get_drvdata(rsc->wrapper_dev);
732 if (unlikely(!geni_se_dev || !geni_se_dev->bus_bw))
733 return -ENODEV;
734
Karthikeyan Ramasubramanian383b41e2017-10-25 17:32:52 -0600735 ret = se_geni_clks_off(rsc);
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600736 if (ret)
737 GENI_SE_ERR(geni_se_dev->log_ctx, false, NULL,
Karthikeyan Ramasubramanian383b41e2017-10-25 17:32:52 -0600738 "%s: Error %d turning off clocks\n", __func__, ret);
Girish Mahadevan6d97cbc2017-11-07 15:28:27 -0700739 ret = pinctrl_select_state(rsc->geni_pinctrl, rsc->geni_gpio_sleep);
740 if (ret)
741 GENI_SE_ERR(geni_se_dev->log_ctx, false, NULL,
742 "%s: Error %d pinctrl_select_state\n", __func__, ret);
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600743 return ret;
744}
745EXPORT_SYMBOL(se_geni_resources_off);
746
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600747static int geni_se_add_ab_ib(struct geni_se_device *geni_se_dev,
748 struct se_geni_rsc *rsc)
749{
Karthikeyan Ramasubramanian409370c2017-08-11 17:31:45 -0600750 struct se_geni_rsc *tmp = NULL;
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600751 struct list_head *ins_list_head;
752 bool bus_bw_update = false;
753 int ret = 0;
754
Girish Mahadevand11aefc2017-11-30 15:41:19 -0700755 mutex_lock(&geni_se_dev->geni_dev_lock);
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600756 list_add(&rsc->ab_list, &geni_se_dev->ab_list_head);
757 geni_se_dev->cur_ab += rsc->ab;
758
759 ins_list_head = &geni_se_dev->ib_list_head;
760 list_for_each_entry(tmp, &geni_se_dev->ib_list_head, ib_list) {
761 if (tmp->ib < rsc->ib)
762 break;
763 ins_list_head = &tmp->ib_list;
764 }
765 list_add(&rsc->ib_list, ins_list_head);
766 /* Currently inserted node has greater average BW value */
767 if (ins_list_head == &geni_se_dev->ib_list_head)
Karthikeyan Ramasubramanian409370c2017-08-11 17:31:45 -0600768 geni_se_dev->cur_ib = rsc->ib;
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600769
770 bus_bw_update = geni_se_check_bus_bw(geni_se_dev);
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600771 if (bus_bw_update)
772 ret = msm_bus_scale_update_bw(geni_se_dev->bus_bw,
773 geni_se_dev->cur_ab,
774 geni_se_dev->cur_ib);
775 GENI_SE_DBG(geni_se_dev->log_ctx, false, NULL,
776 "%s: %lu:%lu (%lu:%lu) %d\n", __func__,
777 geni_se_dev->cur_ab, geni_se_dev->cur_ib,
778 rsc->ab, rsc->ib, bus_bw_update);
Girish Mahadevand11aefc2017-11-30 15:41:19 -0700779 mutex_unlock(&geni_se_dev->geni_dev_lock);
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600780 return ret;
781}
782
783/**
Karthikeyan Ramasubramanian383b41e2017-10-25 17:32:52 -0600784 * se_geni_clks_on() - Turn on clocks associated with the serial
785 * engine
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600786 * @rsc: Handle to resources associated with the serial engine.
787 *
788 * Return: 0 on success, standard Linux error codes on failure/error.
789 */
Karthikeyan Ramasubramanian383b41e2017-10-25 17:32:52 -0600790int se_geni_clks_on(struct se_geni_rsc *rsc)
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600791{
792 int ret = 0;
793 struct geni_se_device *geni_se_dev;
794
795 if (unlikely(!rsc || !rsc->wrapper_dev))
796 return -EINVAL;
797
798 geni_se_dev = dev_get_drvdata(rsc->wrapper_dev);
799 if (unlikely(!geni_se_dev))
800 return -EPROBE_DEFER;
801
802 ret = geni_se_add_ab_ib(geni_se_dev, rsc);
803 if (ret) {
804 GENI_SE_ERR(geni_se_dev->log_ctx, false, NULL,
805 "%s: Error %d during bus_bw_update\n", __func__, ret);
806 return ret;
807 }
808
Karthikeyan Ramasubramanian383b41e2017-10-25 17:32:52 -0600809 ret = clk_prepare_enable(rsc->m_ahb_clk);
810 if (ret)
811 goto clks_on_err1;
812
813 ret = clk_prepare_enable(rsc->s_ahb_clk);
814 if (ret)
815 goto clks_on_err2;
816
817 ret = clk_prepare_enable(rsc->se_clk);
818 if (ret)
819 goto clks_on_err3;
820 return 0;
821
822clks_on_err3:
823 clk_disable_unprepare(rsc->s_ahb_clk);
824clks_on_err2:
825 clk_disable_unprepare(rsc->m_ahb_clk);
826clks_on_err1:
827 geni_se_rmv_ab_ib(geni_se_dev, rsc);
828 return ret;
829}
830EXPORT_SYMBOL(se_geni_clks_on);
831
832/**
833 * se_geni_resources_on() - Turn on resources associated with the serial
834 * engine
835 * @rsc: Handle to resources associated with the serial engine.
836 *
837 * Return: 0 on success, standard Linux error codes on failure/error.
838 */
839int se_geni_resources_on(struct se_geni_rsc *rsc)
840{
841 int ret = 0;
842 struct geni_se_device *geni_se_dev;
843
844 if (unlikely(!rsc || !rsc->wrapper_dev))
845 return -EINVAL;
846
847 geni_se_dev = dev_get_drvdata(rsc->wrapper_dev);
848 if (unlikely(!geni_se_dev))
849 return -EPROBE_DEFER;
850
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600851 ret = pinctrl_select_state(rsc->geni_pinctrl, rsc->geni_gpio_active);
852 if (ret) {
853 GENI_SE_ERR(geni_se_dev->log_ctx, false, NULL,
854 "%s: Error %d pinctrl_select_state\n", __func__, ret);
Girish Mahadevan6d97cbc2017-11-07 15:28:27 -0700855 return ret;
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600856 }
Girish Mahadevan6d97cbc2017-11-07 15:28:27 -0700857
858 ret = se_geni_clks_on(rsc);
859 if (ret) {
860 GENI_SE_ERR(geni_se_dev->log_ctx, false, NULL,
861 "%s: Error %d during clks_on\n", __func__, ret);
862 pinctrl_select_state(rsc->geni_pinctrl, rsc->geni_gpio_sleep);
863 }
864
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -0600865 return ret;
866}
867EXPORT_SYMBOL(se_geni_resources_on);
868
869/**
870 * geni_se_resources_init() - Init the SE resource structure
871 * @rsc: SE resource structure to be initialized.
872 * @ab: Initial Average bus bandwidth request value.
873 * @ib: Initial Instantaneous bus bandwidth request value.
874 *
875 * Return: 0 on success, standard Linux error codes on failure.
876 */
877int geni_se_resources_init(struct se_geni_rsc *rsc,
878 unsigned long ab, unsigned long ib)
879{
880 struct geni_se_device *geni_se_dev;
881
882 if (unlikely(!rsc || !rsc->wrapper_dev))
883 return -EINVAL;
884
885 geni_se_dev = dev_get_drvdata(rsc->wrapper_dev);
886 if (unlikely(!geni_se_dev))
887 return -EPROBE_DEFER;
888
889 if (unlikely(IS_ERR_OR_NULL(geni_se_dev->bus_bw))) {
890 geni_se_dev->bus_bw = msm_bus_scale_register(
891 geni_se_dev->bus_mas_id,
892 geni_se_dev->bus_slv_id,
893 (char *)dev_name(geni_se_dev->dev),
894 false);
895 if (IS_ERR_OR_NULL(geni_se_dev->bus_bw)) {
896 GENI_SE_ERR(geni_se_dev->log_ctx, false, NULL,
897 "%s: Error creating bus client\n", __func__);
898 return (int)PTR_ERR(geni_se_dev->bus_bw);
899 }
900 }
901
902 rsc->ab = ab;
903 rsc->ib = ib;
904 INIT_LIST_HEAD(&rsc->ab_list);
905 INIT_LIST_HEAD(&rsc->ib_list);
906 geni_se_iommu_map_and_attach(geni_se_dev);
907 return 0;
908}
909EXPORT_SYMBOL(geni_se_resources_init);
910
911/**
Karthikeyan Ramasubramanian8bef5ea2017-05-11 17:02:46 -0600912 * geni_se_clk_tbl_get() - Get the clock table to program DFS
913 * @rsc: Resource for which the clock table is requested.
914 * @tbl: Table in which the output is returned.
915 *
916 * This function is called by the protocol drivers to determine the different
917 * clock frequencies supported by Serail Engine Core Clock. The protocol
918 * drivers use the output to determine the clock frequency index to be
919 * programmed into DFS.
920 *
921 * Return: number of valid performance levels in the table on success,
922 * standard Linux error codes on failure.
923 */
924int geni_se_clk_tbl_get(struct se_geni_rsc *rsc, unsigned long **tbl)
925{
926 struct geni_se_device *geni_se_dev;
927 int i;
928 unsigned long prev_freq = 0;
Girish Mahadevand11aefc2017-11-30 15:41:19 -0700929 int ret = 0;
Karthikeyan Ramasubramanian8bef5ea2017-05-11 17:02:46 -0600930
931 if (unlikely(!rsc || !rsc->wrapper_dev || !rsc->se_clk || !tbl))
932 return -EINVAL;
933
Karthikeyan Ramasubramanian8bef5ea2017-05-11 17:02:46 -0600934 geni_se_dev = dev_get_drvdata(rsc->wrapper_dev);
935 if (unlikely(!geni_se_dev))
936 return -EPROBE_DEFER;
Girish Mahadevand11aefc2017-11-30 15:41:19 -0700937 mutex_lock(&geni_se_dev->geni_dev_lock);
938 *tbl = NULL;
Karthikeyan Ramasubramanian8bef5ea2017-05-11 17:02:46 -0600939
940 if (geni_se_dev->clk_perf_tbl) {
941 *tbl = geni_se_dev->clk_perf_tbl;
Girish Mahadevand11aefc2017-11-30 15:41:19 -0700942 ret = geni_se_dev->num_clk_levels;
943 goto exit_se_clk_tbl_get;
Karthikeyan Ramasubramanian8bef5ea2017-05-11 17:02:46 -0600944 }
945
946 geni_se_dev->clk_perf_tbl = kzalloc(sizeof(*geni_se_dev->clk_perf_tbl) *
947 MAX_CLK_PERF_LEVEL, GFP_KERNEL);
Girish Mahadevand11aefc2017-11-30 15:41:19 -0700948 if (!geni_se_dev->clk_perf_tbl) {
949 ret = -ENOMEM;
950 goto exit_se_clk_tbl_get;
951 }
Karthikeyan Ramasubramanian8bef5ea2017-05-11 17:02:46 -0600952
953 for (i = 0; i < MAX_CLK_PERF_LEVEL; i++) {
954 geni_se_dev->clk_perf_tbl[i] = clk_round_rate(rsc->se_clk,
955 prev_freq + 1);
956 if (geni_se_dev->clk_perf_tbl[i] == prev_freq) {
957 geni_se_dev->clk_perf_tbl[i] = 0;
958 break;
959 }
960 prev_freq = geni_se_dev->clk_perf_tbl[i];
961 }
962 geni_se_dev->num_clk_levels = i;
963 *tbl = geni_se_dev->clk_perf_tbl;
Girish Mahadevand11aefc2017-11-30 15:41:19 -0700964 ret = geni_se_dev->num_clk_levels;
965exit_se_clk_tbl_get:
966 mutex_unlock(&geni_se_dev->geni_dev_lock);
967 return ret;
Karthikeyan Ramasubramanian8bef5ea2017-05-11 17:02:46 -0600968}
969EXPORT_SYMBOL(geni_se_clk_tbl_get);
970
971/**
972 * geni_se_clk_freq_match() - Get the matching or closest SE clock frequency
973 * @rsc: Resource for which the clock frequency is requested.
974 * @req_freq: Requested clock frequency.
975 * @index: Index of the resultant frequency in the table.
976 * @res_freq: Resultant frequency which matches or is closer to the
977 * requested frequency.
978 * @exact: Flag to indicate exact multiple requirement of the requested
979 * frequency .
980 *
981 * This function is called by the protocol drivers to determine the matching
982 * or closest frequency of the Serial Engine clock to be selected in order
983 * to meet the performance requirements.
984 *
985 * Return: 0 on success, standard Linux error codes on failure.
986 */
987int geni_se_clk_freq_match(struct se_geni_rsc *rsc, unsigned long req_freq,
988 unsigned int *index, unsigned long *res_freq,
989 bool exact)
990{
991 unsigned long *tbl;
992 int num_clk_levels;
993 int i;
994
995 num_clk_levels = geni_se_clk_tbl_get(rsc, &tbl);
996 if (num_clk_levels < 0)
997 return num_clk_levels;
998
999 if (num_clk_levels == 0)
1000 return -EFAULT;
1001
1002 *res_freq = 0;
1003 for (i = 0; i < num_clk_levels; i++) {
1004 if (!(tbl[i] % req_freq)) {
1005 *index = i;
1006 *res_freq = tbl[i];
1007 return 0;
1008 }
1009
1010 if (!(*res_freq) || ((tbl[i] > *res_freq) &&
1011 (tbl[i] < req_freq))) {
1012 *index = i;
1013 *res_freq = tbl[i];
1014 }
1015 }
1016
1017 if (exact || !(*res_freq))
1018 return -ENOKEY;
1019
1020 return 0;
1021}
1022EXPORT_SYMBOL(geni_se_clk_freq_match);
1023
1024/**
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -06001025 * geni_se_tx_dma_prep() - Prepare the Serial Engine for TX DMA transfer
1026 * @wrapper_dev: QUPv3 Wrapper Device to which the TX buffer is mapped.
1027 * @base: Base address of the SE register block.
1028 * @tx_buf: Pointer to the TX buffer.
1029 * @tx_len: Length of the TX buffer.
1030 * @tx_dma: Pointer to store the mapped DMA address.
1031 *
1032 * This function is used to prepare the buffers for DMA TX.
1033 *
1034 * Return: 0 on success, standard Linux error codes on error/failure.
1035 */
1036int geni_se_tx_dma_prep(struct device *wrapper_dev, void __iomem *base,
1037 void *tx_buf, int tx_len, dma_addr_t *tx_dma)
1038{
1039 int ret;
1040
1041 if (unlikely(!wrapper_dev || !base || !tx_buf || !tx_len || !tx_dma))
1042 return -EINVAL;
1043
1044 ret = geni_se_iommu_map_buf(wrapper_dev, tx_dma, tx_buf, tx_len,
1045 DMA_TO_DEVICE);
1046 if (ret)
1047 return ret;
1048
1049 geni_write_reg(7, base, SE_DMA_TX_IRQ_EN_SET);
Shrey Vijay370a18b2017-12-12 16:21:20 +05301050 geni_write_reg(GENI_SE_DMA_PTR_L(*tx_dma), base, SE_DMA_TX_PTR_L);
1051 geni_write_reg(GENI_SE_DMA_PTR_H(*tx_dma), base, SE_DMA_TX_PTR_H);
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -06001052 geni_write_reg(1, base, SE_DMA_TX_ATTR);
1053 geni_write_reg(tx_len, base, SE_DMA_TX_LEN);
1054 return 0;
1055}
1056EXPORT_SYMBOL(geni_se_tx_dma_prep);
1057
1058/**
1059 * geni_se_rx_dma_prep() - Prepare the Serial Engine for RX DMA transfer
1060 * @wrapper_dev: QUPv3 Wrapper Device to which the RX buffer is mapped.
1061 * @base: Base address of the SE register block.
1062 * @rx_buf: Pointer to the RX buffer.
1063 * @rx_len: Length of the RX buffer.
1064 * @rx_dma: Pointer to store the mapped DMA address.
1065 *
1066 * This function is used to prepare the buffers for DMA RX.
1067 *
1068 * Return: 0 on success, standard Linux error codes on error/failure.
1069 */
1070int geni_se_rx_dma_prep(struct device *wrapper_dev, void __iomem *base,
1071 void *rx_buf, int rx_len, dma_addr_t *rx_dma)
1072{
1073 int ret;
1074
1075 if (unlikely(!wrapper_dev || !base || !rx_buf || !rx_len || !rx_dma))
1076 return -EINVAL;
1077
1078 ret = geni_se_iommu_map_buf(wrapper_dev, rx_dma, rx_buf, rx_len,
1079 DMA_FROM_DEVICE);
1080 if (ret)
1081 return ret;
1082
1083 geni_write_reg(7, base, SE_DMA_RX_IRQ_EN_SET);
Shrey Vijay370a18b2017-12-12 16:21:20 +05301084 geni_write_reg(GENI_SE_DMA_PTR_L(*rx_dma), base, SE_DMA_RX_PTR_L);
1085 geni_write_reg(GENI_SE_DMA_PTR_H(*rx_dma), base, SE_DMA_RX_PTR_H);
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -06001086 /* RX does not have EOT bit */
1087 geni_write_reg(0, base, SE_DMA_RX_ATTR);
1088 geni_write_reg(rx_len, base, SE_DMA_RX_LEN);
1089 return 0;
1090}
1091EXPORT_SYMBOL(geni_se_rx_dma_prep);
1092
1093/**
1094 * geni_se_tx_dma_unprep() - Unprepare the Serial Engine after TX DMA transfer
1095 * @wrapper_dev: QUPv3 Wrapper Device to which the RX buffer is mapped.
1096 * @tx_dma: DMA address of the TX buffer.
1097 * @tx_len: Length of the TX buffer.
1098 *
1099 * This function is used to unprepare the DMA buffers after DMA TX.
1100 */
1101void geni_se_tx_dma_unprep(struct device *wrapper_dev,
1102 dma_addr_t tx_dma, int tx_len)
1103{
1104 if (tx_dma)
1105 geni_se_iommu_unmap_buf(wrapper_dev, &tx_dma, tx_len,
1106 DMA_TO_DEVICE);
1107}
1108EXPORT_SYMBOL(geni_se_tx_dma_unprep);
1109
1110/**
1111 * geni_se_rx_dma_unprep() - Unprepare the Serial Engine after RX DMA transfer
1112 * @wrapper_dev: QUPv3 Wrapper Device to which the RX buffer is mapped.
1113 * @rx_dma: DMA address of the RX buffer.
1114 * @rx_len: Length of the RX buffer.
1115 *
1116 * This function is used to unprepare the DMA buffers after DMA RX.
1117 */
1118void geni_se_rx_dma_unprep(struct device *wrapper_dev,
1119 dma_addr_t rx_dma, int rx_len)
1120{
1121 if (rx_dma)
1122 geni_se_iommu_unmap_buf(wrapper_dev, &rx_dma, rx_len,
1123 DMA_FROM_DEVICE);
1124}
1125EXPORT_SYMBOL(geni_se_rx_dma_unprep);
1126
1127/**
1128 * geni_se_qupv3_hw_version() - Read the QUPv3 Hardware version
1129 * @wrapper_dev: Pointer to the corresponding QUPv3 wrapper core.
1130 * @major: Buffer for Major Version field.
1131 * @minor: Buffer for Minor Version field.
1132 * @step: Buffer for Step Version field.
1133 *
1134 * Return: 0 on success, standard Linux error codes on failure/error.
1135 */
1136int geni_se_qupv3_hw_version(struct device *wrapper_dev, unsigned int *major,
1137 unsigned int *minor, unsigned int *step)
1138{
1139 unsigned int version;
1140 struct geni_se_device *geni_se_dev;
1141
1142 if (!wrapper_dev || !major || !minor || !step)
1143 return -EINVAL;
1144
1145 geni_se_dev = dev_get_drvdata(wrapper_dev);
1146 if (unlikely(!geni_se_dev))
1147 return -ENODEV;
1148
1149 version = geni_read_reg(geni_se_dev->base, QUPV3_HW_VER);
1150 *major = (version & HW_VER_MAJOR_MASK) >> HW_VER_MAJOR_SHFT;
1151 *minor = (version & HW_VER_MINOR_MASK) >> HW_VER_MINOR_SHFT;
1152 *step = version & HW_VER_STEP_MASK;
1153 return 0;
1154}
1155EXPORT_SYMBOL(geni_se_qupv3_hw_version);
1156
1157static int geni_se_iommu_map_and_attach(struct geni_se_device *geni_se_dev)
1158{
1159 dma_addr_t va_start = GENI_SE_IOMMU_VA_START;
1160 size_t va_size = GENI_SE_IOMMU_VA_SIZE;
1161 int bypass = 1;
1162 struct device *cb_dev = geni_se_dev->cb_dev;
1163
1164 mutex_lock(&geni_se_dev->iommu_lock);
1165 if (likely(geni_se_dev->iommu_map)) {
1166 mutex_unlock(&geni_se_dev->iommu_lock);
1167 return 0;
1168 }
1169
1170 geni_se_dev->iommu_map = arm_iommu_create_mapping(&platform_bus_type,
1171 va_start, va_size);
1172 if (IS_ERR(geni_se_dev->iommu_map)) {
1173 GENI_SE_ERR(geni_se_dev->log_ctx, false, NULL,
1174 "%s:%s iommu_create_mapping failure\n",
1175 __func__, dev_name(cb_dev));
1176 mutex_unlock(&geni_se_dev->iommu_lock);
1177 return PTR_ERR(geni_se_dev->iommu_map);
1178 }
1179
1180 if (geni_se_dev->iommu_s1_bypass) {
1181 if (iommu_domain_set_attr(geni_se_dev->iommu_map->domain,
1182 DOMAIN_ATTR_S1_BYPASS, &bypass)) {
1183 GENI_SE_ERR(geni_se_dev->log_ctx, false, NULL,
1184 "%s:%s Couldn't bypass s1 translation\n",
1185 __func__, dev_name(cb_dev));
1186 arm_iommu_release_mapping(geni_se_dev->iommu_map);
1187 geni_se_dev->iommu_map = NULL;
1188 mutex_unlock(&geni_se_dev->iommu_lock);
1189 return -EIO;
1190 }
1191 }
1192
1193 if (arm_iommu_attach_device(cb_dev, geni_se_dev->iommu_map)) {
1194 GENI_SE_ERR(geni_se_dev->log_ctx, false, NULL,
1195 "%s:%s couldn't arm_iommu_attach_device\n",
1196 __func__, dev_name(cb_dev));
1197 arm_iommu_release_mapping(geni_se_dev->iommu_map);
1198 geni_se_dev->iommu_map = NULL;
1199 mutex_unlock(&geni_se_dev->iommu_lock);
1200 return -EIO;
1201 }
1202 mutex_unlock(&geni_se_dev->iommu_lock);
1203 GENI_SE_DBG(geni_se_dev->log_ctx, false, NULL, "%s:%s successful\n",
1204 __func__, dev_name(cb_dev));
1205 return 0;
1206}
1207
1208/**
1209 * geni_se_iommu_map_buf() - Map a single buffer into QUPv3 context bank
1210 * @wrapper_dev: Pointer to the corresponding QUPv3 wrapper core.
1211 * @iova: Pointer in which the mapped virtual address is stored.
1212 * @buf: Address of the buffer that needs to be mapped.
1213 * @size: Size of the buffer.
1214 * @dir: Direction of the DMA transfer.
1215 *
1216 * This function is used to map an already allocated buffer into the
1217 * QUPv3 context bank device space.
1218 *
1219 * Return: 0 on success, standard Linux error codes on failure/error.
1220 */
1221int geni_se_iommu_map_buf(struct device *wrapper_dev, dma_addr_t *iova,
1222 void *buf, size_t size, enum dma_data_direction dir)
1223{
1224 struct device *cb_dev;
1225 struct geni_se_device *geni_se_dev;
1226
1227 if (!wrapper_dev || !iova || !buf || !size)
1228 return -EINVAL;
1229
1230 *iova = DMA_ERROR_CODE;
1231 geni_se_dev = dev_get_drvdata(wrapper_dev);
1232 if (!geni_se_dev || !geni_se_dev->cb_dev)
1233 return -ENODEV;
1234
1235 cb_dev = geni_se_dev->cb_dev;
1236
1237 *iova = dma_map_single(cb_dev, buf, size, dir);
1238 if (dma_mapping_error(cb_dev, *iova))
1239 return -EIO;
1240 return 0;
1241}
1242EXPORT_SYMBOL(geni_se_iommu_map_buf);
1243
1244/**
1245 * geni_se_iommu_alloc_buf() - Allocate & map a single buffer into QUPv3
1246 * context bank
1247 * @wrapper_dev: Pointer to the corresponding QUPv3 wrapper core.
1248 * @iova: Pointer in which the mapped virtual address is stored.
1249 * @size: Size of the buffer.
1250 *
1251 * This function is used to allocate a buffer and map it into the
1252 * QUPv3 context bank device space.
1253 *
1254 * Return: address of the buffer on success, NULL or ERR_PTR on
1255 * failure/error.
1256 */
1257void *geni_se_iommu_alloc_buf(struct device *wrapper_dev, dma_addr_t *iova,
1258 size_t size)
1259{
1260 struct device *cb_dev;
1261 struct geni_se_device *geni_se_dev;
1262 void *buf = NULL;
1263
1264 if (!wrapper_dev || !iova || !size)
1265 return ERR_PTR(-EINVAL);
1266
1267 *iova = DMA_ERROR_CODE;
1268 geni_se_dev = dev_get_drvdata(wrapper_dev);
1269 if (!geni_se_dev || !geni_se_dev->cb_dev)
1270 return ERR_PTR(-ENODEV);
1271
1272 cb_dev = geni_se_dev->cb_dev;
1273
1274 buf = dma_alloc_coherent(cb_dev, size, iova, GFP_KERNEL);
1275 if (!buf)
1276 GENI_SE_ERR(geni_se_dev->log_ctx, false, NULL,
1277 "%s: Failed dma_alloc_coherent\n", __func__);
1278 return buf;
1279}
1280EXPORT_SYMBOL(geni_se_iommu_alloc_buf);
1281
1282/**
1283 * geni_se_iommu_unmap_buf() - Unmap a single buffer from QUPv3 context bank
1284 * @wrapper_dev: Pointer to the corresponding QUPv3 wrapper core.
1285 * @iova: Pointer in which the mapped virtual address is stored.
1286 * @size: Size of the buffer.
1287 * @dir: Direction of the DMA transfer.
1288 *
1289 * This function is used to unmap an already mapped buffer from the
1290 * QUPv3 context bank device space.
1291 *
1292 * Return: 0 on success, standard Linux error codes on failure/error.
1293 */
1294int geni_se_iommu_unmap_buf(struct device *wrapper_dev, dma_addr_t *iova,
1295 size_t size, enum dma_data_direction dir)
1296{
1297 struct device *cb_dev;
1298 struct geni_se_device *geni_se_dev;
1299
1300 if (!wrapper_dev || !iova || !size)
1301 return -EINVAL;
1302
1303 geni_se_dev = dev_get_drvdata(wrapper_dev);
1304 if (!geni_se_dev || !geni_se_dev->cb_dev)
1305 return -ENODEV;
1306
1307 cb_dev = geni_se_dev->cb_dev;
1308
1309 dma_unmap_single(cb_dev, *iova, size, dir);
1310 return 0;
1311}
1312EXPORT_SYMBOL(geni_se_iommu_unmap_buf);
1313
1314/**
1315 * geni_se_iommu_free_buf() - Unmap & free a single buffer from QUPv3
1316 * context bank
1317 * @wrapper_dev: Pointer to the corresponding QUPv3 wrapper core.
1318 * @iova: Pointer in which the mapped virtual address is stored.
1319 * @buf: Address of the buffer.
1320 * @size: Size of the buffer.
1321 *
1322 * This function is used to unmap and free a buffer from the
1323 * QUPv3 context bank device space.
1324 *
1325 * Return: 0 on success, standard Linux error codes on failure/error.
1326 */
1327int geni_se_iommu_free_buf(struct device *wrapper_dev, dma_addr_t *iova,
1328 void *buf, size_t size)
1329{
1330 struct device *cb_dev;
1331 struct geni_se_device *geni_se_dev;
1332
1333 if (!wrapper_dev || !iova || !buf || !size)
1334 return -EINVAL;
1335
1336 geni_se_dev = dev_get_drvdata(wrapper_dev);
1337 if (!geni_se_dev || !geni_se_dev->cb_dev)
1338 return -ENODEV;
1339
1340 cb_dev = geni_se_dev->cb_dev;
1341
1342 dma_free_coherent(cb_dev, size, buf, *iova);
1343 return 0;
1344}
1345EXPORT_SYMBOL(geni_se_iommu_free_buf);
1346
Girish Mahadevan3b7e9742017-09-15 15:17:16 -06001347/**
1348 * geni_se_dump_dbg_regs() - Print relevant registers that capture most
1349 * accurately the state of an SE.
1350 * @_dev: Pointer to the SE's device.
1351 * @iomem: Base address of the SE's register space.
1352 * @ipc: IPC log context handle.
1353 *
1354 * This function is used to print out all the registers that capture the state
1355 * of an SE to help debug any errors.
1356 *
1357 * Return: None
1358 */
1359void geni_se_dump_dbg_regs(struct se_geni_rsc *rsc, void __iomem *base,
1360 void *ipc)
1361{
1362 u32 m_cmd0 = 0;
1363 u32 m_irq_status = 0;
1364 u32 geni_status = 0;
1365 u32 geni_ios = 0;
1366 u32 dma_rx_irq = 0;
1367 u32 dma_tx_irq = 0;
1368 u32 rx_fifo_status = 0;
1369 u32 tx_fifo_status = 0;
1370 u32 se_dma_dbg = 0;
1371 u32 m_cmd_ctrl = 0;
1372 u32 se_dma_rx_len = 0;
1373 u32 se_dma_rx_len_in = 0;
1374 u32 se_dma_tx_len = 0;
1375 u32 se_dma_tx_len_in = 0;
1376 struct geni_se_device *geni_se_dev;
1377
1378 if (!ipc)
1379 return;
1380
1381 geni_se_dev = dev_get_drvdata(rsc->wrapper_dev);
1382 if (unlikely(!geni_se_dev || !geni_se_dev->bus_bw))
1383 return;
Girish Mahadevan3b7e9742017-09-15 15:17:16 -06001384 if (unlikely(list_empty(&rsc->ab_list) || list_empty(&rsc->ib_list))) {
1385 GENI_SE_DBG(ipc, false, NULL, "%s: Clocks not on\n", __func__);
Girish Mahadevan0ef15632017-10-05 07:53:46 -06001386 return;
Girish Mahadevan3b7e9742017-09-15 15:17:16 -06001387 }
1388 m_cmd0 = geni_read_reg(base, SE_GENI_M_CMD0);
1389 m_irq_status = geni_read_reg(base, SE_GENI_M_IRQ_STATUS);
1390 geni_status = geni_read_reg(base, SE_GENI_STATUS);
1391 geni_ios = geni_read_reg(base, SE_GENI_IOS);
1392 dma_rx_irq = geni_read_reg(base, SE_DMA_TX_IRQ_STAT);
1393 dma_tx_irq = geni_read_reg(base, SE_DMA_RX_IRQ_STAT);
1394 rx_fifo_status = geni_read_reg(base, SE_GENI_RX_FIFO_STATUS);
1395 tx_fifo_status = geni_read_reg(base, SE_GENI_TX_FIFO_STATUS);
1396 se_dma_dbg = geni_read_reg(base, SE_DMA_DEBUG_REG0);
1397 m_cmd_ctrl = geni_read_reg(base, SE_GENI_M_CMD_CTRL_REG);
1398 se_dma_rx_len = geni_read_reg(base, SE_DMA_RX_LEN);
1399 se_dma_rx_len_in = geni_read_reg(base, SE_DMA_RX_LEN_IN);
1400 se_dma_tx_len = geni_read_reg(base, SE_DMA_TX_LEN);
1401 se_dma_tx_len_in = geni_read_reg(base, SE_DMA_TX_LEN_IN);
1402
1403 GENI_SE_DBG(ipc, false, NULL,
1404 "%s: m_cmd0:0x%x, m_irq_status:0x%x, geni_status:0x%x, geni_ios:0x%x\n",
1405 __func__, m_cmd0, m_irq_status, geni_status, geni_ios);
1406 GENI_SE_DBG(ipc, false, NULL,
1407 "dma_rx_irq:0x%x, dma_tx_irq:0x%x, rx_fifo_sts:0x%x, tx_fifo_sts:0x%x\n"
1408 , dma_rx_irq, dma_tx_irq, rx_fifo_status, tx_fifo_status);
1409 GENI_SE_DBG(ipc, false, NULL,
1410 "se_dma_dbg:0x%x, m_cmd_ctrl:0x%x, dma_rxlen:0x%x, dma_rxlen_in:0x%x\n",
1411 se_dma_dbg, m_cmd_ctrl, se_dma_rx_len, se_dma_rx_len_in);
1412 GENI_SE_DBG(ipc, false, NULL,
1413 "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 -06001414}
1415EXPORT_SYMBOL(geni_se_dump_dbg_regs);
1416
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -06001417static const struct of_device_id geni_se_dt_match[] = {
1418 { .compatible = "qcom,qupv3-geni-se", },
1419 { .compatible = "qcom,qupv3-geni-se-cb", },
1420 {}
1421};
1422
1423static int geni_se_iommu_probe(struct device *dev)
1424{
1425 struct geni_se_device *geni_se_dev;
1426
1427 if (unlikely(!dev->parent)) {
1428 dev_err(dev, "%s no parent for this device\n", __func__);
1429 return -EINVAL;
1430 }
1431
1432 geni_se_dev = dev_get_drvdata(dev->parent);
1433 if (unlikely(!geni_se_dev)) {
1434 dev_err(dev, "%s geni_se_dev not found\n", __func__);
1435 return -EINVAL;
1436 }
1437 geni_se_dev->cb_dev = dev;
1438
1439 GENI_SE_DBG(geni_se_dev->log_ctx, false, NULL,
1440 "%s: Probe successful\n", __func__);
1441 return 0;
1442}
1443
1444static int geni_se_probe(struct platform_device *pdev)
1445{
1446 int ret;
1447 struct device *dev = &pdev->dev;
1448 struct resource *res;
1449 struct geni_se_device *geni_se_dev;
1450
1451 if (of_device_is_compatible(dev->of_node, "qcom,qupv3-geni-se-cb"))
1452 return geni_se_iommu_probe(dev);
1453
1454 geni_se_dev = devm_kzalloc(dev, sizeof(*geni_se_dev), GFP_KERNEL);
1455 if (!geni_se_dev)
1456 return -ENOMEM;
1457
1458 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1459 if (!res) {
1460 dev_err(dev, "%s: Mandatory resource info not found\n",
1461 __func__);
1462 devm_kfree(dev, geni_se_dev);
1463 return -EINVAL;
1464 }
1465
1466 geni_se_dev->base = devm_ioremap_resource(dev, res);
1467 if (IS_ERR_OR_NULL(geni_se_dev->base)) {
1468 dev_err(dev, "%s: Error mapping the resource\n", __func__);
1469 devm_kfree(dev, geni_se_dev);
1470 return -EFAULT;
1471 }
1472
1473 geni_se_dev->dev = dev;
1474 ret = of_property_read_u32(dev->of_node, "qcom,bus-mas-id",
1475 &geni_se_dev->bus_mas_id);
1476 if (ret) {
1477 dev_err(dev, "%s: Error missing bus master id\n", __func__);
1478 devm_iounmap(dev, geni_se_dev->base);
1479 devm_kfree(dev, geni_se_dev);
1480 }
1481 ret = of_property_read_u32(dev->of_node, "qcom,bus-slv-id",
1482 &geni_se_dev->bus_slv_id);
1483 if (ret) {
1484 dev_err(dev, "%s: Error missing bus slave id\n", __func__);
1485 devm_iounmap(dev, geni_se_dev->base);
1486 devm_kfree(dev, geni_se_dev);
1487 }
1488
1489 geni_se_dev->iommu_s1_bypass = of_property_read_bool(dev->of_node,
1490 "qcom,iommu-s1-bypass");
1491 geni_se_dev->bus_bw_set = default_bus_bw_set;
1492 geni_se_dev->bus_bw_set_size = ARRAY_SIZE(default_bus_bw_set);
1493 mutex_init(&geni_se_dev->iommu_lock);
1494 INIT_LIST_HEAD(&geni_se_dev->ab_list_head);
1495 INIT_LIST_HEAD(&geni_se_dev->ib_list_head);
Girish Mahadevand11aefc2017-11-30 15:41:19 -07001496 mutex_init(&geni_se_dev->geni_dev_lock);
Karthikeyan Ramasubramanian0d578b72017-04-26 10:44:02 -06001497 geni_se_dev->log_ctx = ipc_log_context_create(NUM_LOG_PAGES,
1498 dev_name(geni_se_dev->dev), 0);
1499 if (!geni_se_dev->log_ctx)
1500 dev_err(dev, "%s Failed to allocate log context\n", __func__);
1501 dev_set_drvdata(dev, geni_se_dev);
1502
1503 ret = of_platform_populate(dev->of_node, geni_se_dt_match, NULL, dev);
1504 if (ret) {
1505 dev_err(dev, "%s: Error populating children\n", __func__);
1506 devm_iounmap(dev, geni_se_dev->base);
1507 devm_kfree(dev, geni_se_dev);
1508 }
1509
1510 GENI_SE_DBG(geni_se_dev->log_ctx, false, NULL,
1511 "%s: Probe successful\n", __func__);
1512 return ret;
1513}
1514
1515static int geni_se_remove(struct platform_device *pdev)
1516{
1517 struct device *dev = &pdev->dev;
1518 struct geni_se_device *geni_se_dev = dev_get_drvdata(dev);
1519
1520 if (likely(!IS_ERR_OR_NULL(geni_se_dev->iommu_map))) {
1521 arm_iommu_detach_device(geni_se_dev->cb_dev);
1522 arm_iommu_release_mapping(geni_se_dev->iommu_map);
1523 }
1524 ipc_log_context_destroy(geni_se_dev->log_ctx);
1525 devm_iounmap(dev, geni_se_dev->base);
1526 devm_kfree(dev, geni_se_dev);
1527 return 0;
1528}
1529
1530static struct platform_driver geni_se_driver = {
1531 .driver = {
1532 .name = "qupv3_geni_se",
1533 .of_match_table = geni_se_dt_match,
1534 },
1535 .probe = geni_se_probe,
1536 .remove = geni_se_remove,
1537};
1538
1539static int __init geni_se_driver_init(void)
1540{
1541 return platform_driver_register(&geni_se_driver);
1542}
1543arch_initcall(geni_se_driver_init);
1544
1545static void __exit geni_se_driver_exit(void)
1546{
1547 platform_driver_unregister(&geni_se_driver);
1548}
1549module_exit(geni_se_driver_exit);
1550
1551MODULE_DESCRIPTION("GENI Serial Engine Driver");
1552MODULE_LICENSE("GPL v2");