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