blob: 0e52e32ddf3b9de0ad692a640022b5e35138648c [file] [log] [blame]
Dzmitry Sankouskif5be5c82021-10-17 13:44:27 +03001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Qualcomm GENI serial engine UART driver
4 *
5 * (C) Copyright 2021 Dzmitry Sankouski <dsankouski@gmail.com>
6 *
7 * Based on Linux driver.
8 */
9
10#include <asm/io.h>
11#include <clk.h>
12#include <common.h>
13#include <dm.h>
Dzmitry Sankouskif5be5c82021-10-17 13:44:27 +030014#include <errno.h>
Dzmitry Sankouskif5be5c82021-10-17 13:44:27 +030015#include <linux/delay.h>
Igor Prusovc3421ea2023-11-09 20:10:04 +030016#include <linux/time.h>
Vladimir Zapolskiy75c472b2023-04-21 20:50:40 +030017#include <misc.h>
Dzmitry Sankouskif5be5c82021-10-17 13:44:27 +030018#include <serial.h>
Dzmitry Sankouskif5be5c82021-10-17 13:44:27 +030019
20#define UART_OVERSAMPLING 32
21#define STALE_TIMEOUT 160
22
Dzmitry Sankouskif5be5c82021-10-17 13:44:27 +030023/* Registers*/
24#define GENI_FORCE_DEFAULT_REG 0x20
25#define GENI_SER_M_CLK_CFG 0x48
26#define GENI_SER_S_CLK_CFG 0x4C
27#define SE_HW_PARAM_0 0xE24
28#define SE_GENI_STATUS 0x40
29#define SE_GENI_S_CMD0 0x630
30#define SE_GENI_S_CMD_CTRL_REG 0x634
31#define SE_GENI_S_IRQ_CLEAR 0x648
32#define SE_GENI_S_IRQ_STATUS 0x640
33#define SE_GENI_S_IRQ_EN 0x644
34#define SE_GENI_M_CMD0 0x600
35#define SE_GENI_M_CMD_CTRL_REG 0x604
36#define SE_GENI_M_IRQ_CLEAR 0x618
37#define SE_GENI_M_IRQ_STATUS 0x610
38#define SE_GENI_M_IRQ_EN 0x614
39#define SE_GENI_TX_FIFOn 0x700
40#define SE_GENI_RX_FIFOn 0x780
41#define SE_GENI_TX_FIFO_STATUS 0x800
42#define SE_GENI_RX_FIFO_STATUS 0x804
43#define SE_GENI_TX_WATERMARK_REG 0x80C
44#define SE_GENI_TX_PACKING_CFG0 0x260
45#define SE_GENI_TX_PACKING_CFG1 0x264
46#define SE_GENI_RX_PACKING_CFG0 0x284
47#define SE_GENI_RX_PACKING_CFG1 0x288
48#define SE_UART_RX_STALE_CNT 0x294
49#define SE_UART_TX_TRANS_LEN 0x270
50#define SE_UART_TX_STOP_BIT_LEN 0x26c
51#define SE_UART_TX_WORD_LEN 0x268
52#define SE_UART_RX_WORD_LEN 0x28c
53#define SE_UART_TX_TRANS_CFG 0x25c
54#define SE_UART_TX_PARITY_CFG 0x2a4
55#define SE_UART_RX_TRANS_CFG 0x280
56#define SE_UART_RX_PARITY_CFG 0x2a8
57
58#define M_TX_FIFO_WATERMARK_EN (BIT(30))
59#define DEF_TX_WM 2
60/* GENI_FORCE_DEFAULT_REG fields */
61#define FORCE_DEFAULT (BIT(0))
62
63#define S_CMD_ABORT_EN (BIT(5))
64
65#define UART_START_READ 0x1
66
67/* GENI_M_CMD_CTRL_REG */
68#define M_GENI_CMD_CANCEL (BIT(2))
69#define M_GENI_CMD_ABORT (BIT(1))
70#define M_GENI_DISABLE (BIT(0))
71
72#define M_CMD_ABORT_EN (BIT(5))
73#define M_CMD_DONE_EN (BIT(0))
74#define M_CMD_DONE_DISABLE_MASK (~M_CMD_DONE_EN)
75
76#define S_GENI_CMD_ABORT (BIT(1))
77
78/* GENI_S_CMD0 fields */
79#define S_OPCODE_MSK (GENMASK(31, 27))
80#define S_PARAMS_MSK (GENMASK(26, 0))
81
82/* GENI_STATUS fields */
83#define M_GENI_CMD_ACTIVE (BIT(0))
84#define S_GENI_CMD_ACTIVE (BIT(12))
85#define M_CMD_DONE_EN (BIT(0))
86#define S_CMD_DONE_EN (BIT(0))
87
88#define M_OPCODE_SHIFT 27
89#define S_OPCODE_SHIFT 27
90#define M_TX_FIFO_WATERMARK_EN (BIT(30))
91#define UART_START_TX 0x1
92#define UART_CTS_MASK (BIT(1))
93#define M_SEC_IRQ_EN (BIT(31))
94#define TX_FIFO_WC_MSK (GENMASK(27, 0))
95#define RX_FIFO_WC_MSK (GENMASK(24, 0))
96
97#define S_RX_FIFO_WATERMARK_EN (BIT(26))
98#define S_RX_FIFO_LAST_EN (BIT(27))
99#define M_RX_FIFO_WATERMARK_EN (BIT(26))
100#define M_RX_FIFO_LAST_EN (BIT(27))
101
102/* GENI_SER_M_CLK_CFG/GENI_SER_S_CLK_CFG */
103#define SER_CLK_EN (BIT(0))
104#define CLK_DIV_MSK (GENMASK(15, 4))
105#define CLK_DIV_SHFT 4
106
107/* SE_HW_PARAM_0 fields */
108#define TX_FIFO_WIDTH_MSK (GENMASK(29, 24))
109#define TX_FIFO_WIDTH_SHFT 24
110#define TX_FIFO_DEPTH_MSK (GENMASK(21, 16))
111#define TX_FIFO_DEPTH_SHFT 16
112
Vladimir Zapolskiy75c472b2023-04-21 20:50:40 +0300113/* GENI SE QUP Registers */
114#define QUP_HW_VER_REG 0x4
115#define QUP_SE_VERSION_2_5 0x20050000
116
Dzmitry Sankouskif5be5c82021-10-17 13:44:27 +0300117/*
118 * Predefined packing configuration of the serial engine (CFG0, CFG1 regs)
119 * for uart mode.
120 *
121 * Defines following configuration:
122 * - Bits of data per transfer word 8
123 * - Number of words per fifo element 4
124 * - Transfer from MSB to LSB or vice-versa false
125 */
126#define UART_PACKING_CFG0 0xf
127#define UART_PACKING_CFG1 0x0
128
129DECLARE_GLOBAL_DATA_PTR;
130
131struct msm_serial_data {
132 phys_addr_t base;
133 u32 baud;
Vladimir Zapolskiy75c472b2023-04-21 20:50:40 +0300134 u32 oversampling;
Dzmitry Sankouskif5be5c82021-10-17 13:44:27 +0300135};
136
137unsigned long root_freq[] = {7372800, 14745600, 19200000, 29491200,
Vladimir Zapolskiyf76cc182023-04-21 20:50:37 +0300138 32000000, 48000000, 64000000, 80000000,
139 96000000, 100000000};
Dzmitry Sankouskif5be5c82021-10-17 13:44:27 +0300140
141/**
142 * get_clk_cfg() - Get clock rate to apply on clock supplier.
143 * @clk_freq: Desired clock frequency after build-in divider.
144 *
145 * Return: frequency, supported by clock supplier, multiple of clk_freq.
146 */
147static int get_clk_cfg(unsigned long clk_freq)
148{
149 for (int i = 0; i < ARRAY_SIZE(root_freq); i++) {
150 if (!(root_freq[i] % clk_freq))
151 return root_freq[i];
152 }
153 return 0;
154}
155
156/**
157 * get_clk_div_rate() - Find clock supplier frequency, and calculate divisor.
158 * @baud: Baudrate.
159 * @sampling_rate: Clock ticks per character.
160 * @clk_div: Pointer to calculated divisor.
161 *
162 * This function searches for suitable frequency for clock supplier,
163 * calculates divisor for internal divider, based on found frequency,
164 * and stores divisor under clk_div pointer.
165 *
166 * Return: frequency, supported by clock supplier, multiple of clk_freq.
167 */
Vladimir Zapolskiyf76cc182023-04-21 20:50:37 +0300168static int get_clk_div_rate(u32 baud, u64 sampling_rate, u32 *clk_div)
Dzmitry Sankouskif5be5c82021-10-17 13:44:27 +0300169{
170 unsigned long ser_clk;
171 unsigned long desired_clk;
172
173 desired_clk = baud * sampling_rate;
174 ser_clk = get_clk_cfg(desired_clk);
175 if (!ser_clk) {
176 pr_err("%s: Can't find matching DFS entry for baud %d\n",
177 __func__, baud);
178 return ser_clk;
179 }
180
181 *clk_div = ser_clk / desired_clk;
182 return ser_clk;
183}
184
185static int geni_serial_set_clock_rate(struct udevice *dev, u64 rate)
186{
187 struct clk *clk;
188 int ret;
189
Vladimir Zapolskiy50251192023-04-21 20:50:36 +0300190 clk = devm_clk_get(dev, NULL);
Dzmitry Sankouskif5be5c82021-10-17 13:44:27 +0300191 if (!clk)
192 return -EINVAL;
193
194 ret = clk_set_rate(clk, rate);
195 return ret;
196}
197
198/**
199 * geni_se_get_tx_fifo_depth() - Get the TX fifo depth of the serial engine
200 * @base: Pointer to the concerned serial engine.
201 *
202 * This function is used to get the depth i.e. number of elements in the
203 * TX fifo of the serial engine.
204 *
205 * Return: TX fifo depth in units of FIFO words.
206 */
207static inline u32 geni_se_get_tx_fifo_depth(long base)
208{
209 u32 tx_fifo_depth;
210
211 tx_fifo_depth = ((readl(base + SE_HW_PARAM_0) & TX_FIFO_DEPTH_MSK) >>
212 TX_FIFO_DEPTH_SHFT);
213 return tx_fifo_depth;
214}
215
216/**
217 * geni_se_get_tx_fifo_width() - Get the TX fifo width of the serial engine
218 * @base: Pointer to the concerned serial engine.
219 *
220 * This function is used to get the width i.e. word size per element in the
221 * TX fifo of the serial engine.
222 *
223 * Return: TX fifo width in bits
224 */
225static inline u32 geni_se_get_tx_fifo_width(long base)
226{
227 u32 tx_fifo_width;
228
229 tx_fifo_width = ((readl(base + SE_HW_PARAM_0) & TX_FIFO_WIDTH_MSK) >>
230 TX_FIFO_WIDTH_SHFT);
231 return tx_fifo_width;
232}
233
234static inline void geni_serial_baud(phys_addr_t base_address, u32 clk_div,
Vladimir Zapolskiyf76cc182023-04-21 20:50:37 +0300235 int baud)
Dzmitry Sankouskif5be5c82021-10-17 13:44:27 +0300236{
237 u32 s_clk_cfg = 0;
238
239 s_clk_cfg |= SER_CLK_EN;
240 s_clk_cfg |= (clk_div << CLK_DIV_SHFT);
241
242 writel(s_clk_cfg, base_address + GENI_SER_M_CLK_CFG);
243 writel(s_clk_cfg, base_address + GENI_SER_S_CLK_CFG);
244}
245
Vladimir Zapolskiy51ff6c32023-04-21 20:50:38 +0300246static int msm_serial_setbrg(struct udevice *dev, int baud)
Dzmitry Sankouskif5be5c82021-10-17 13:44:27 +0300247{
248 struct msm_serial_data *priv = dev_get_priv(dev);
Vladimir Zapolskiy51ff6c32023-04-21 20:50:38 +0300249 u64 clk_rate;
250 u32 clk_div;
Dzmitry Sankouskif5be5c82021-10-17 13:44:27 +0300251
252 priv->baud = baud;
Dzmitry Sankouskif5be5c82021-10-17 13:44:27 +0300253
Vladimir Zapolskiy75c472b2023-04-21 20:50:40 +0300254 clk_rate = get_clk_div_rate(baud, priv->oversampling, &clk_div);
Dzmitry Sankouskif5be5c82021-10-17 13:44:27 +0300255 geni_serial_set_clock_rate(dev, clk_rate);
256 geni_serial_baud(priv->base, clk_div, baud);
257
258 return 0;
259}
260
261/**
262 * qcom_geni_serial_poll_bit() - Poll reg bit until desired value or timeout.
263 * @base: Pointer to the concerned serial engine.
264 * @offset: Offset to register address.
265 * @field: AND bitmask for desired bit.
266 * @set: Desired bit value.
267 *
268 * This function is used to get the width i.e. word size per element in the
269 * TX fifo of the serial engine.
270 *
271 * Return: true, when register bit equals desired value, false, when timeout
272 * reached.
273 */
274static bool qcom_geni_serial_poll_bit(const struct udevice *dev, int offset,
Vladimir Zapolskiyf76cc182023-04-21 20:50:37 +0300275 int field, bool set)
Dzmitry Sankouskif5be5c82021-10-17 13:44:27 +0300276{
277 u32 reg;
278 struct msm_serial_data *priv = dev_get_priv(dev);
279 unsigned int baud;
280 unsigned int tx_fifo_depth;
281 unsigned int tx_fifo_width;
282 unsigned int fifo_bits;
283 unsigned long timeout_us = 10000;
284
285 baud = 115200;
286
287 if (priv) {
288 baud = priv->baud;
289 if (!baud)
290 baud = 115200;
291 tx_fifo_depth = geni_se_get_tx_fifo_depth(priv->base);
292 tx_fifo_width = geni_se_get_tx_fifo_width(priv->base);
293 fifo_bits = tx_fifo_depth * tx_fifo_width;
294 /*
295 * Total polling iterations based on FIFO worth of bytes to be
296 * sent at current baud. Add a little fluff to the wait.
297 */
298 timeout_us = ((fifo_bits * USEC_PER_SEC) / baud) + 500;
299 }
300
301 timeout_us = DIV_ROUND_UP(timeout_us, 10) * 10;
302 while (timeout_us) {
303 reg = readl(priv->base + offset);
304 if ((bool)(reg & field) == set)
305 return true;
306 udelay(10);
307 timeout_us -= 10;
308 }
309 return false;
310}
311
312static void qcom_geni_serial_setup_tx(u64 base, u32 xmit_size)
313{
314 u32 m_cmd;
315
316 writel(xmit_size, base + SE_UART_TX_TRANS_LEN);
317 m_cmd = UART_START_TX << M_OPCODE_SHIFT;
318 writel(m_cmd, base + SE_GENI_M_CMD0);
319}
320
321static inline void qcom_geni_serial_poll_tx_done(const struct udevice *dev)
322{
323 struct msm_serial_data *priv = dev_get_priv(dev);
324 int done = 0;
325 u32 irq_clear = M_CMD_DONE_EN;
326
327 done = qcom_geni_serial_poll_bit(dev, SE_GENI_M_IRQ_STATUS,
328 M_CMD_DONE_EN, true);
329 if (!done) {
330 writel(M_GENI_CMD_ABORT, priv->base + SE_GENI_M_CMD_CTRL_REG);
331 irq_clear |= M_CMD_ABORT_EN;
332 qcom_geni_serial_poll_bit(dev, SE_GENI_M_IRQ_STATUS,
333 M_CMD_ABORT_EN, true);
334 }
335 writel(irq_clear, priv->base + SE_GENI_M_IRQ_CLEAR);
336}
337
338static u32 qcom_geni_serial_tx_empty(u64 base)
339{
340 return !readl(base + SE_GENI_TX_FIFO_STATUS);
341}
342
343/**
344 * geni_se_setup_s_cmd() - Setup the secondary sequencer
345 * @se: Pointer to the concerned serial engine.
346 * @cmd: Command/Operation to setup in the secondary sequencer.
347 * @params: Parameter for the sequencer command.
348 *
349 * This function is used to configure the secondary sequencer with the
350 * command and its associated parameters.
351 */
352static inline void geni_se_setup_s_cmd(u64 base, u32 cmd, u32 params)
353{
354 u32 s_cmd;
355
356 s_cmd = readl(base + SE_GENI_S_CMD0);
357 s_cmd &= ~(S_OPCODE_MSK | S_PARAMS_MSK);
358 s_cmd |= (cmd << S_OPCODE_SHIFT);
359 s_cmd |= (params & S_PARAMS_MSK);
360 writel(s_cmd, base + SE_GENI_S_CMD0);
361}
362
363static void qcom_geni_serial_start_tx(u64 base)
364{
365 u32 irq_en;
366 u32 status;
367
368 status = readl(base + SE_GENI_STATUS);
369 if (status & M_GENI_CMD_ACTIVE)
370 return;
371
372 if (!qcom_geni_serial_tx_empty(base))
373 return;
374
375 irq_en = readl(base + SE_GENI_M_IRQ_EN);
376 irq_en |= M_TX_FIFO_WATERMARK_EN | M_CMD_DONE_EN;
377
378 writel(DEF_TX_WM, base + SE_GENI_TX_WATERMARK_REG);
379 writel(irq_en, base + SE_GENI_M_IRQ_EN);
380}
381
382static void qcom_geni_serial_start_rx(struct udevice *dev)
383{
384 u32 status;
385 struct msm_serial_data *priv = dev_get_priv(dev);
386
387 status = readl(priv->base + SE_GENI_STATUS);
388
389 geni_se_setup_s_cmd(priv->base, UART_START_READ, 0);
390
391 setbits_le32(priv->base + SE_GENI_S_IRQ_EN, S_RX_FIFO_WATERMARK_EN | S_RX_FIFO_LAST_EN);
392 setbits_le32(priv->base + SE_GENI_M_IRQ_EN, M_RX_FIFO_WATERMARK_EN | M_RX_FIFO_LAST_EN);
393}
394
395static void qcom_geni_serial_abort_rx(struct udevice *dev)
396{
397 struct msm_serial_data *priv = dev_get_priv(dev);
398
399 u32 irq_clear = S_CMD_DONE_EN | S_CMD_ABORT_EN;
400
401 writel(S_GENI_CMD_ABORT, priv->base + SE_GENI_S_CMD_CTRL_REG);
402 qcom_geni_serial_poll_bit(dev, SE_GENI_S_CMD_CTRL_REG,
403 S_GENI_CMD_ABORT, false);
404 writel(irq_clear, priv->base + SE_GENI_S_IRQ_CLEAR);
405 writel(FORCE_DEFAULT, priv->base + GENI_FORCE_DEFAULT_REG);
406}
407
408static void msm_geni_serial_setup_rx(struct udevice *dev)
409{
410 struct msm_serial_data *priv = dev_get_priv(dev);
411
412 qcom_geni_serial_abort_rx(dev);
413
414 writel(UART_PACKING_CFG0, priv->base + SE_GENI_RX_PACKING_CFG0);
415 writel(UART_PACKING_CFG1, priv->base + SE_GENI_RX_PACKING_CFG1);
416
417 geni_se_setup_s_cmd(priv->base, UART_START_READ, 0);
418
419 setbits_le32(priv->base + SE_GENI_S_IRQ_EN, S_RX_FIFO_WATERMARK_EN | S_RX_FIFO_LAST_EN);
420 setbits_le32(priv->base + SE_GENI_M_IRQ_EN, M_RX_FIFO_WATERMARK_EN | M_RX_FIFO_LAST_EN);
421}
422
423static int msm_serial_putc(struct udevice *dev, const char ch)
424{
425 struct msm_serial_data *priv = dev_get_priv(dev);
426
427 writel(DEF_TX_WM, priv->base + SE_GENI_TX_WATERMARK_REG);
428 qcom_geni_serial_setup_tx(priv->base, 1);
429
430 qcom_geni_serial_poll_bit(dev, SE_GENI_M_IRQ_STATUS,
431 M_TX_FIFO_WATERMARK_EN, true);
432
433 writel(ch, priv->base + SE_GENI_TX_FIFOn);
434 writel(M_TX_FIFO_WATERMARK_EN, priv->base + SE_GENI_M_IRQ_CLEAR);
435
436 qcom_geni_serial_poll_tx_done(dev);
437
438 return 0;
439}
440
441static int msm_serial_getc(struct udevice *dev)
442{
443 struct msm_serial_data *priv = dev_get_priv(dev);
444 u32 rx_fifo;
445 u32 m_irq_status;
446 u32 s_irq_status;
447
448 writel(1 << S_OPCODE_SHIFT, priv->base + SE_GENI_S_CMD0);
449
450 qcom_geni_serial_poll_bit(dev, SE_GENI_M_IRQ_STATUS, M_SEC_IRQ_EN,
451 true);
452
453 m_irq_status = readl(priv->base + SE_GENI_M_IRQ_STATUS);
454 s_irq_status = readl(priv->base + SE_GENI_S_IRQ_STATUS);
455 writel(m_irq_status, priv->base + SE_GENI_M_IRQ_CLEAR);
456 writel(s_irq_status, priv->base + SE_GENI_S_IRQ_CLEAR);
457 qcom_geni_serial_poll_bit(dev, SE_GENI_RX_FIFO_STATUS, RX_FIFO_WC_MSK,
458 true);
459
460 if (!readl(priv->base + SE_GENI_RX_FIFO_STATUS))
461 return 0;
462
463 rx_fifo = readl(priv->base + SE_GENI_RX_FIFOn);
464 return rx_fifo & 0xff;
465}
466
467static int msm_serial_pending(struct udevice *dev, bool input)
468{
469 struct msm_serial_data *priv = dev_get_priv(dev);
470
471 if (input)
472 return readl(priv->base + SE_GENI_RX_FIFO_STATUS) &
473 RX_FIFO_WC_MSK;
474 else
475 return readl(priv->base + SE_GENI_TX_FIFO_STATUS) &
476 TX_FIFO_WC_MSK;
477
478 return 0;
479}
480
481static const struct dm_serial_ops msm_serial_ops = {
482 .putc = msm_serial_putc,
483 .pending = msm_serial_pending,
484 .getc = msm_serial_getc,
485 .setbrg = msm_serial_setbrg,
486};
487
Caleb Connolly29ac8d92023-11-14 12:51:11 +0000488static int geni_set_oversampling(struct udevice *dev)
Vladimir Zapolskiy75c472b2023-04-21 20:50:40 +0300489{
490 struct msm_serial_data *priv = dev_get_priv(dev);
Caleb Connolly29ac8d92023-11-14 12:51:11 +0000491 ofnode parent_node = ofnode_get_parent(dev_ofnode(dev));
Vladimir Zapolskiy75c472b2023-04-21 20:50:40 +0300492 u32 geni_se_version;
Caleb Connolly29ac8d92023-11-14 12:51:11 +0000493 fdt_addr_t addr;
Vladimir Zapolskiy75c472b2023-04-21 20:50:40 +0300494
495 priv->oversampling = UART_OVERSAMPLING;
496
497 /*
498 * It could happen that GENI SE IP is missing in the board's device
499 * tree or GENI UART node is a direct child of SoC device tree node.
500 */
Caleb Connolly29ac8d92023-11-14 12:51:11 +0000501 if (!ofnode_device_is_compatible(parent_node, "qcom,geni-se-qup")) {
502 pr_err("%s: UART node must be a child of geniqup node\n",
503 __func__);
504 return -ENODEV;
505 }
Vladimir Zapolskiy75c472b2023-04-21 20:50:40 +0300506
Caleb Connolly29ac8d92023-11-14 12:51:11 +0000507 /* Read the HW_VER register relative to the parents address space */
508 addr = ofnode_get_addr(parent_node);
509 geni_se_version = readl(addr + QUP_HW_VER_REG);
Vladimir Zapolskiy75c472b2023-04-21 20:50:40 +0300510
511 if (geni_se_version >= QUP_SE_VERSION_2_5)
512 priv->oversampling /= 2;
Caleb Connolly29ac8d92023-11-14 12:51:11 +0000513
514 return 0;
Vladimir Zapolskiy75c472b2023-04-21 20:50:40 +0300515}
516
Dzmitry Sankouskif5be5c82021-10-17 13:44:27 +0300517static inline void geni_serial_init(struct udevice *dev)
518{
519 struct msm_serial_data *priv = dev_get_priv(dev);
520 phys_addr_t base_address = priv->base;
521 u32 tx_trans_cfg;
522 u32 tx_parity_cfg = 0; /* Disable Tx Parity */
523 u32 rx_trans_cfg = 0;
524 u32 rx_parity_cfg = 0; /* Disable Rx Parity */
525 u32 stop_bit_len = 0; /* Default stop bit length - 1 bit */
526 u32 bits_per_char;
527
528 /*
529 * Ignore Flow control.
530 * n = 8.
531 */
532 tx_trans_cfg = UART_CTS_MASK;
533 bits_per_char = BITS_PER_BYTE;
534
535 /*
536 * Make an unconditional cancel on the main sequencer to reset
537 * it else we could end up in data loss scenarios.
538 */
539 qcom_geni_serial_poll_tx_done(dev);
540 qcom_geni_serial_abort_rx(dev);
541
542 writel(UART_PACKING_CFG0, base_address + SE_GENI_TX_PACKING_CFG0);
543 writel(UART_PACKING_CFG1, base_address + SE_GENI_TX_PACKING_CFG1);
544 writel(UART_PACKING_CFG0, base_address + SE_GENI_RX_PACKING_CFG0);
545 writel(UART_PACKING_CFG1, base_address + SE_GENI_RX_PACKING_CFG1);
546
547 writel(tx_trans_cfg, base_address + SE_UART_TX_TRANS_CFG);
548 writel(tx_parity_cfg, base_address + SE_UART_TX_PARITY_CFG);
549 writel(rx_trans_cfg, base_address + SE_UART_RX_TRANS_CFG);
550 writel(rx_parity_cfg, base_address + SE_UART_RX_PARITY_CFG);
551 writel(bits_per_char, base_address + SE_UART_TX_WORD_LEN);
552 writel(bits_per_char, base_address + SE_UART_RX_WORD_LEN);
553 writel(stop_bit_len, base_address + SE_UART_TX_STOP_BIT_LEN);
554}
555
556static int msm_serial_probe(struct udevice *dev)
557{
558 struct msm_serial_data *priv = dev_get_priv(dev);
Caleb Connolly29ac8d92023-11-14 12:51:11 +0000559 int ret;
Dzmitry Sankouskif5be5c82021-10-17 13:44:27 +0300560
Caleb Connolly29ac8d92023-11-14 12:51:11 +0000561 ret = geni_set_oversampling(dev);
562 if (ret < 0)
563 return ret;
Vladimir Zapolskiy75c472b2023-04-21 20:50:40 +0300564
Dzmitry Sankouskif5be5c82021-10-17 13:44:27 +0300565 /* No need to reinitialize the UART after relocation */
566 if (gd->flags & GD_FLG_RELOC)
567 return 0;
568
569 geni_serial_init(dev);
570 msm_geni_serial_setup_rx(dev);
571 qcom_geni_serial_start_rx(dev);
572 qcom_geni_serial_start_tx(priv->base);
573
574 return 0;
575}
576
577static int msm_serial_ofdata_to_platdata(struct udevice *dev)
578{
579 struct msm_serial_data *priv = dev_get_priv(dev);
580
581 priv->base = dev_read_addr(dev);
582 if (priv->base == FDT_ADDR_T_NONE)
583 return -EINVAL;
584
585 return 0;
586}
587
588static const struct udevice_id msm_serial_ids[] = {
Konrad Dybcio0c8712b2023-04-21 20:50:39 +0300589 { .compatible = "qcom,geni-debug-uart" },
590 { }
591};
Dzmitry Sankouskif5be5c82021-10-17 13:44:27 +0300592
593U_BOOT_DRIVER(serial_msm_geni) = {
594 .name = "serial_msm_geni",
595 .id = UCLASS_SERIAL,
596 .of_match = msm_serial_ids,
597 .of_to_plat = msm_serial_ofdata_to_platdata,
598 .priv_auto = sizeof(struct msm_serial_data),
599 .probe = msm_serial_probe,
600 .ops = &msm_serial_ops,
Konrad Dybciocb844b32023-04-21 20:50:35 +0300601 .flags = DM_FLAG_PRE_RELOC,
Dzmitry Sankouskif5be5c82021-10-17 13:44:27 +0300602};
603
604#ifdef CONFIG_DEBUG_UART_MSM_GENI
605
606static struct msm_serial_data init_serial_data = {
Pali Rohár8864b352022-05-27 22:15:24 +0200607 .base = CONFIG_VAL(DEBUG_UART_BASE)
Dzmitry Sankouskif5be5c82021-10-17 13:44:27 +0300608};
609
610/* Serial dumb device, to reuse driver code */
611static struct udevice init_dev = {
612 .priv_ = &init_serial_data,
613};
614
615#include <debug_uart.h>
616
617#define CLK_DIV (CONFIG_DEBUG_UART_CLOCK / \
618 (CONFIG_BAUDRATE * UART_OVERSAMPLING))
619#if (CONFIG_DEBUG_UART_CLOCK % (CONFIG_BAUDRATE * UART_OVERSAMPLING) > 0)
620#error Clocks cannot be set at early debug. Change CONFIG_BAUDRATE
621#endif
622
623static inline void _debug_uart_init(void)
624{
Pali Rohár8864b352022-05-27 22:15:24 +0200625 phys_addr_t base = CONFIG_VAL(DEBUG_UART_BASE);
Dzmitry Sankouskif5be5c82021-10-17 13:44:27 +0300626
627 geni_serial_init(&init_dev);
628 geni_serial_baud(base, CLK_DIV, CONFIG_BAUDRATE);
629 qcom_geni_serial_start_tx(base);
630}
631
632static inline void _debug_uart_putc(int ch)
633{
Pali Rohár8864b352022-05-27 22:15:24 +0200634 phys_addr_t base = CONFIG_VAL(DEBUG_UART_BASE);
Dzmitry Sankouskif5be5c82021-10-17 13:44:27 +0300635
636 writel(DEF_TX_WM, base + SE_GENI_TX_WATERMARK_REG);
637 qcom_geni_serial_setup_tx(base, 1);
638 qcom_geni_serial_poll_bit(&init_dev, SE_GENI_M_IRQ_STATUS,
639 M_TX_FIFO_WATERMARK_EN, true);
640
641 writel(ch, base + SE_GENI_TX_FIFOn);
642 writel(M_TX_FIFO_WATERMARK_EN, base + SE_GENI_M_IRQ_CLEAR);
643 qcom_geni_serial_poll_tx_done(&init_dev);
644}
645
646DEBUG_UART_FUNCS
647
648#endif