blob: 55dd9188a56c3b488c647cd21b36f0dde912d731 [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>
Dzmitry Sankouskif5be5c82021-10-17 13:44:27 +030016#include <serial.h>
Dzmitry Sankouskif5be5c82021-10-17 13:44:27 +030017
18#define UART_OVERSAMPLING 32
19#define STALE_TIMEOUT 160
20
21#define USEC_PER_SEC 1000000L
22
23/* 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
113/*
114 * Predefined packing configuration of the serial engine (CFG0, CFG1 regs)
115 * for uart mode.
116 *
117 * Defines following configuration:
118 * - Bits of data per transfer word 8
119 * - Number of words per fifo element 4
120 * - Transfer from MSB to LSB or vice-versa false
121 */
122#define UART_PACKING_CFG0 0xf
123#define UART_PACKING_CFG1 0x0
124
125DECLARE_GLOBAL_DATA_PTR;
126
127struct msm_serial_data {
128 phys_addr_t base;
129 u32 baud;
130};
131
132unsigned long root_freq[] = {7372800, 14745600, 19200000, 29491200,
Vladimir Zapolskiyf76cc182023-04-21 20:50:37 +0300133 32000000, 48000000, 64000000, 80000000,
134 96000000, 100000000};
Dzmitry Sankouskif5be5c82021-10-17 13:44:27 +0300135
136/**
137 * get_clk_cfg() - Get clock rate to apply on clock supplier.
138 * @clk_freq: Desired clock frequency after build-in divider.
139 *
140 * Return: frequency, supported by clock supplier, multiple of clk_freq.
141 */
142static int get_clk_cfg(unsigned long clk_freq)
143{
144 for (int i = 0; i < ARRAY_SIZE(root_freq); i++) {
145 if (!(root_freq[i] % clk_freq))
146 return root_freq[i];
147 }
148 return 0;
149}
150
151/**
152 * get_clk_div_rate() - Find clock supplier frequency, and calculate divisor.
153 * @baud: Baudrate.
154 * @sampling_rate: Clock ticks per character.
155 * @clk_div: Pointer to calculated divisor.
156 *
157 * This function searches for suitable frequency for clock supplier,
158 * calculates divisor for internal divider, based on found frequency,
159 * and stores divisor under clk_div pointer.
160 *
161 * Return: frequency, supported by clock supplier, multiple of clk_freq.
162 */
Vladimir Zapolskiyf76cc182023-04-21 20:50:37 +0300163static int get_clk_div_rate(u32 baud, u64 sampling_rate, u32 *clk_div)
Dzmitry Sankouskif5be5c82021-10-17 13:44:27 +0300164{
165 unsigned long ser_clk;
166 unsigned long desired_clk;
167
168 desired_clk = baud * sampling_rate;
169 ser_clk = get_clk_cfg(desired_clk);
170 if (!ser_clk) {
171 pr_err("%s: Can't find matching DFS entry for baud %d\n",
172 __func__, baud);
173 return ser_clk;
174 }
175
176 *clk_div = ser_clk / desired_clk;
177 return ser_clk;
178}
179
180static int geni_serial_set_clock_rate(struct udevice *dev, u64 rate)
181{
182 struct clk *clk;
183 int ret;
184
Vladimir Zapolskiy50251192023-04-21 20:50:36 +0300185 clk = devm_clk_get(dev, NULL);
Dzmitry Sankouskif5be5c82021-10-17 13:44:27 +0300186 if (!clk)
187 return -EINVAL;
188
189 ret = clk_set_rate(clk, rate);
190 return ret;
191}
192
193/**
194 * geni_se_get_tx_fifo_depth() - Get the TX fifo depth of the serial engine
195 * @base: Pointer to the concerned serial engine.
196 *
197 * This function is used to get the depth i.e. number of elements in the
198 * TX fifo of the serial engine.
199 *
200 * Return: TX fifo depth in units of FIFO words.
201 */
202static inline u32 geni_se_get_tx_fifo_depth(long base)
203{
204 u32 tx_fifo_depth;
205
206 tx_fifo_depth = ((readl(base + SE_HW_PARAM_0) & TX_FIFO_DEPTH_MSK) >>
207 TX_FIFO_DEPTH_SHFT);
208 return tx_fifo_depth;
209}
210
211/**
212 * geni_se_get_tx_fifo_width() - Get the TX fifo width of the serial engine
213 * @base: Pointer to the concerned serial engine.
214 *
215 * This function is used to get the width i.e. word size per element in the
216 * TX fifo of the serial engine.
217 *
218 * Return: TX fifo width in bits
219 */
220static inline u32 geni_se_get_tx_fifo_width(long base)
221{
222 u32 tx_fifo_width;
223
224 tx_fifo_width = ((readl(base + SE_HW_PARAM_0) & TX_FIFO_WIDTH_MSK) >>
225 TX_FIFO_WIDTH_SHFT);
226 return tx_fifo_width;
227}
228
229static inline void geni_serial_baud(phys_addr_t base_address, u32 clk_div,
Vladimir Zapolskiyf76cc182023-04-21 20:50:37 +0300230 int baud)
Dzmitry Sankouskif5be5c82021-10-17 13:44:27 +0300231{
232 u32 s_clk_cfg = 0;
233
234 s_clk_cfg |= SER_CLK_EN;
235 s_clk_cfg |= (clk_div << CLK_DIV_SHFT);
236
237 writel(s_clk_cfg, base_address + GENI_SER_M_CLK_CFG);
238 writel(s_clk_cfg, base_address + GENI_SER_S_CLK_CFG);
239}
240
241int msm_serial_setbrg(struct udevice *dev, int baud)
242{
243 struct msm_serial_data *priv = dev_get_priv(dev);
244
245 priv->baud = baud;
246 u32 clk_div;
247 u64 clk_rate;
248
249 clk_rate = get_clk_div_rate(baud, UART_OVERSAMPLING, &clk_div);
250 geni_serial_set_clock_rate(dev, clk_rate);
251 geni_serial_baud(priv->base, clk_div, baud);
252
253 return 0;
254}
255
256/**
257 * qcom_geni_serial_poll_bit() - Poll reg bit until desired value or timeout.
258 * @base: Pointer to the concerned serial engine.
259 * @offset: Offset to register address.
260 * @field: AND bitmask for desired bit.
261 * @set: Desired bit value.
262 *
263 * This function is used to get the width i.e. word size per element in the
264 * TX fifo of the serial engine.
265 *
266 * Return: true, when register bit equals desired value, false, when timeout
267 * reached.
268 */
269static bool qcom_geni_serial_poll_bit(const struct udevice *dev, int offset,
Vladimir Zapolskiyf76cc182023-04-21 20:50:37 +0300270 int field, bool set)
Dzmitry Sankouskif5be5c82021-10-17 13:44:27 +0300271{
272 u32 reg;
273 struct msm_serial_data *priv = dev_get_priv(dev);
274 unsigned int baud;
275 unsigned int tx_fifo_depth;
276 unsigned int tx_fifo_width;
277 unsigned int fifo_bits;
278 unsigned long timeout_us = 10000;
279
280 baud = 115200;
281
282 if (priv) {
283 baud = priv->baud;
284 if (!baud)
285 baud = 115200;
286 tx_fifo_depth = geni_se_get_tx_fifo_depth(priv->base);
287 tx_fifo_width = geni_se_get_tx_fifo_width(priv->base);
288 fifo_bits = tx_fifo_depth * tx_fifo_width;
289 /*
290 * Total polling iterations based on FIFO worth of bytes to be
291 * sent at current baud. Add a little fluff to the wait.
292 */
293 timeout_us = ((fifo_bits * USEC_PER_SEC) / baud) + 500;
294 }
295
296 timeout_us = DIV_ROUND_UP(timeout_us, 10) * 10;
297 while (timeout_us) {
298 reg = readl(priv->base + offset);
299 if ((bool)(reg & field) == set)
300 return true;
301 udelay(10);
302 timeout_us -= 10;
303 }
304 return false;
305}
306
307static void qcom_geni_serial_setup_tx(u64 base, u32 xmit_size)
308{
309 u32 m_cmd;
310
311 writel(xmit_size, base + SE_UART_TX_TRANS_LEN);
312 m_cmd = UART_START_TX << M_OPCODE_SHIFT;
313 writel(m_cmd, base + SE_GENI_M_CMD0);
314}
315
316static inline void qcom_geni_serial_poll_tx_done(const struct udevice *dev)
317{
318 struct msm_serial_data *priv = dev_get_priv(dev);
319 int done = 0;
320 u32 irq_clear = M_CMD_DONE_EN;
321
322 done = qcom_geni_serial_poll_bit(dev, SE_GENI_M_IRQ_STATUS,
323 M_CMD_DONE_EN, true);
324 if (!done) {
325 writel(M_GENI_CMD_ABORT, priv->base + SE_GENI_M_CMD_CTRL_REG);
326 irq_clear |= M_CMD_ABORT_EN;
327 qcom_geni_serial_poll_bit(dev, SE_GENI_M_IRQ_STATUS,
328 M_CMD_ABORT_EN, true);
329 }
330 writel(irq_clear, priv->base + SE_GENI_M_IRQ_CLEAR);
331}
332
333static u32 qcom_geni_serial_tx_empty(u64 base)
334{
335 return !readl(base + SE_GENI_TX_FIFO_STATUS);
336}
337
338/**
339 * geni_se_setup_s_cmd() - Setup the secondary sequencer
340 * @se: Pointer to the concerned serial engine.
341 * @cmd: Command/Operation to setup in the secondary sequencer.
342 * @params: Parameter for the sequencer command.
343 *
344 * This function is used to configure the secondary sequencer with the
345 * command and its associated parameters.
346 */
347static inline void geni_se_setup_s_cmd(u64 base, u32 cmd, u32 params)
348{
349 u32 s_cmd;
350
351 s_cmd = readl(base + SE_GENI_S_CMD0);
352 s_cmd &= ~(S_OPCODE_MSK | S_PARAMS_MSK);
353 s_cmd |= (cmd << S_OPCODE_SHIFT);
354 s_cmd |= (params & S_PARAMS_MSK);
355 writel(s_cmd, base + SE_GENI_S_CMD0);
356}
357
358static void qcom_geni_serial_start_tx(u64 base)
359{
360 u32 irq_en;
361 u32 status;
362
363 status = readl(base + SE_GENI_STATUS);
364 if (status & M_GENI_CMD_ACTIVE)
365 return;
366
367 if (!qcom_geni_serial_tx_empty(base))
368 return;
369
370 irq_en = readl(base + SE_GENI_M_IRQ_EN);
371 irq_en |= M_TX_FIFO_WATERMARK_EN | M_CMD_DONE_EN;
372
373 writel(DEF_TX_WM, base + SE_GENI_TX_WATERMARK_REG);
374 writel(irq_en, base + SE_GENI_M_IRQ_EN);
375}
376
377static void qcom_geni_serial_start_rx(struct udevice *dev)
378{
379 u32 status;
380 struct msm_serial_data *priv = dev_get_priv(dev);
381
382 status = readl(priv->base + SE_GENI_STATUS);
383
384 geni_se_setup_s_cmd(priv->base, UART_START_READ, 0);
385
386 setbits_le32(priv->base + SE_GENI_S_IRQ_EN, S_RX_FIFO_WATERMARK_EN | S_RX_FIFO_LAST_EN);
387 setbits_le32(priv->base + SE_GENI_M_IRQ_EN, M_RX_FIFO_WATERMARK_EN | M_RX_FIFO_LAST_EN);
388}
389
390static void qcom_geni_serial_abort_rx(struct udevice *dev)
391{
392 struct msm_serial_data *priv = dev_get_priv(dev);
393
394 u32 irq_clear = S_CMD_DONE_EN | S_CMD_ABORT_EN;
395
396 writel(S_GENI_CMD_ABORT, priv->base + SE_GENI_S_CMD_CTRL_REG);
397 qcom_geni_serial_poll_bit(dev, SE_GENI_S_CMD_CTRL_REG,
398 S_GENI_CMD_ABORT, false);
399 writel(irq_clear, priv->base + SE_GENI_S_IRQ_CLEAR);
400 writel(FORCE_DEFAULT, priv->base + GENI_FORCE_DEFAULT_REG);
401}
402
403static void msm_geni_serial_setup_rx(struct udevice *dev)
404{
405 struct msm_serial_data *priv = dev_get_priv(dev);
406
407 qcom_geni_serial_abort_rx(dev);
408
409 writel(UART_PACKING_CFG0, priv->base + SE_GENI_RX_PACKING_CFG0);
410 writel(UART_PACKING_CFG1, priv->base + SE_GENI_RX_PACKING_CFG1);
411
412 geni_se_setup_s_cmd(priv->base, UART_START_READ, 0);
413
414 setbits_le32(priv->base + SE_GENI_S_IRQ_EN, S_RX_FIFO_WATERMARK_EN | S_RX_FIFO_LAST_EN);
415 setbits_le32(priv->base + SE_GENI_M_IRQ_EN, M_RX_FIFO_WATERMARK_EN | M_RX_FIFO_LAST_EN);
416}
417
418static int msm_serial_putc(struct udevice *dev, const char ch)
419{
420 struct msm_serial_data *priv = dev_get_priv(dev);
421
422 writel(DEF_TX_WM, priv->base + SE_GENI_TX_WATERMARK_REG);
423 qcom_geni_serial_setup_tx(priv->base, 1);
424
425 qcom_geni_serial_poll_bit(dev, SE_GENI_M_IRQ_STATUS,
426 M_TX_FIFO_WATERMARK_EN, true);
427
428 writel(ch, priv->base + SE_GENI_TX_FIFOn);
429 writel(M_TX_FIFO_WATERMARK_EN, priv->base + SE_GENI_M_IRQ_CLEAR);
430
431 qcom_geni_serial_poll_tx_done(dev);
432
433 return 0;
434}
435
436static int msm_serial_getc(struct udevice *dev)
437{
438 struct msm_serial_data *priv = dev_get_priv(dev);
439 u32 rx_fifo;
440 u32 m_irq_status;
441 u32 s_irq_status;
442
443 writel(1 << S_OPCODE_SHIFT, priv->base + SE_GENI_S_CMD0);
444
445 qcom_geni_serial_poll_bit(dev, SE_GENI_M_IRQ_STATUS, M_SEC_IRQ_EN,
446 true);
447
448 m_irq_status = readl(priv->base + SE_GENI_M_IRQ_STATUS);
449 s_irq_status = readl(priv->base + SE_GENI_S_IRQ_STATUS);
450 writel(m_irq_status, priv->base + SE_GENI_M_IRQ_CLEAR);
451 writel(s_irq_status, priv->base + SE_GENI_S_IRQ_CLEAR);
452 qcom_geni_serial_poll_bit(dev, SE_GENI_RX_FIFO_STATUS, RX_FIFO_WC_MSK,
453 true);
454
455 if (!readl(priv->base + SE_GENI_RX_FIFO_STATUS))
456 return 0;
457
458 rx_fifo = readl(priv->base + SE_GENI_RX_FIFOn);
459 return rx_fifo & 0xff;
460}
461
462static int msm_serial_pending(struct udevice *dev, bool input)
463{
464 struct msm_serial_data *priv = dev_get_priv(dev);
465
466 if (input)
467 return readl(priv->base + SE_GENI_RX_FIFO_STATUS) &
468 RX_FIFO_WC_MSK;
469 else
470 return readl(priv->base + SE_GENI_TX_FIFO_STATUS) &
471 TX_FIFO_WC_MSK;
472
473 return 0;
474}
475
476static const struct dm_serial_ops msm_serial_ops = {
477 .putc = msm_serial_putc,
478 .pending = msm_serial_pending,
479 .getc = msm_serial_getc,
480 .setbrg = msm_serial_setbrg,
481};
482
483static inline void geni_serial_init(struct udevice *dev)
484{
485 struct msm_serial_data *priv = dev_get_priv(dev);
486 phys_addr_t base_address = priv->base;
487 u32 tx_trans_cfg;
488 u32 tx_parity_cfg = 0; /* Disable Tx Parity */
489 u32 rx_trans_cfg = 0;
490 u32 rx_parity_cfg = 0; /* Disable Rx Parity */
491 u32 stop_bit_len = 0; /* Default stop bit length - 1 bit */
492 u32 bits_per_char;
493
494 /*
495 * Ignore Flow control.
496 * n = 8.
497 */
498 tx_trans_cfg = UART_CTS_MASK;
499 bits_per_char = BITS_PER_BYTE;
500
501 /*
502 * Make an unconditional cancel on the main sequencer to reset
503 * it else we could end up in data loss scenarios.
504 */
505 qcom_geni_serial_poll_tx_done(dev);
506 qcom_geni_serial_abort_rx(dev);
507
508 writel(UART_PACKING_CFG0, base_address + SE_GENI_TX_PACKING_CFG0);
509 writel(UART_PACKING_CFG1, base_address + SE_GENI_TX_PACKING_CFG1);
510 writel(UART_PACKING_CFG0, base_address + SE_GENI_RX_PACKING_CFG0);
511 writel(UART_PACKING_CFG1, base_address + SE_GENI_RX_PACKING_CFG1);
512
513 writel(tx_trans_cfg, base_address + SE_UART_TX_TRANS_CFG);
514 writel(tx_parity_cfg, base_address + SE_UART_TX_PARITY_CFG);
515 writel(rx_trans_cfg, base_address + SE_UART_RX_TRANS_CFG);
516 writel(rx_parity_cfg, base_address + SE_UART_RX_PARITY_CFG);
517 writel(bits_per_char, base_address + SE_UART_TX_WORD_LEN);
518 writel(bits_per_char, base_address + SE_UART_RX_WORD_LEN);
519 writel(stop_bit_len, base_address + SE_UART_TX_STOP_BIT_LEN);
520}
521
522static int msm_serial_probe(struct udevice *dev)
523{
524 struct msm_serial_data *priv = dev_get_priv(dev);
525
526 /* No need to reinitialize the UART after relocation */
527 if (gd->flags & GD_FLG_RELOC)
528 return 0;
529
530 geni_serial_init(dev);
531 msm_geni_serial_setup_rx(dev);
532 qcom_geni_serial_start_rx(dev);
533 qcom_geni_serial_start_tx(priv->base);
534
535 return 0;
536}
537
538static int msm_serial_ofdata_to_platdata(struct udevice *dev)
539{
540 struct msm_serial_data *priv = dev_get_priv(dev);
541
542 priv->base = dev_read_addr(dev);
543 if (priv->base == FDT_ADDR_T_NONE)
544 return -EINVAL;
545
546 return 0;
547}
548
549static const struct udevice_id msm_serial_ids[] = {
550 {.compatible = "qcom,msm-geni-uart"}, {}};
551
552U_BOOT_DRIVER(serial_msm_geni) = {
553 .name = "serial_msm_geni",
554 .id = UCLASS_SERIAL,
555 .of_match = msm_serial_ids,
556 .of_to_plat = msm_serial_ofdata_to_platdata,
557 .priv_auto = sizeof(struct msm_serial_data),
558 .probe = msm_serial_probe,
559 .ops = &msm_serial_ops,
Konrad Dybciocb844b32023-04-21 20:50:35 +0300560 .flags = DM_FLAG_PRE_RELOC,
Dzmitry Sankouskif5be5c82021-10-17 13:44:27 +0300561};
562
563#ifdef CONFIG_DEBUG_UART_MSM_GENI
564
565static struct msm_serial_data init_serial_data = {
Pali Rohár8864b352022-05-27 22:15:24 +0200566 .base = CONFIG_VAL(DEBUG_UART_BASE)
Dzmitry Sankouskif5be5c82021-10-17 13:44:27 +0300567};
568
569/* Serial dumb device, to reuse driver code */
570static struct udevice init_dev = {
571 .priv_ = &init_serial_data,
572};
573
574#include <debug_uart.h>
575
576#define CLK_DIV (CONFIG_DEBUG_UART_CLOCK / \
577 (CONFIG_BAUDRATE * UART_OVERSAMPLING))
578#if (CONFIG_DEBUG_UART_CLOCK % (CONFIG_BAUDRATE * UART_OVERSAMPLING) > 0)
579#error Clocks cannot be set at early debug. Change CONFIG_BAUDRATE
580#endif
581
582static inline void _debug_uart_init(void)
583{
Pali Rohár8864b352022-05-27 22:15:24 +0200584 phys_addr_t base = CONFIG_VAL(DEBUG_UART_BASE);
Dzmitry Sankouskif5be5c82021-10-17 13:44:27 +0300585
586 geni_serial_init(&init_dev);
587 geni_serial_baud(base, CLK_DIV, CONFIG_BAUDRATE);
588 qcom_geni_serial_start_tx(base);
589}
590
591static inline void _debug_uart_putc(int ch)
592{
Pali Rohár8864b352022-05-27 22:15:24 +0200593 phys_addr_t base = CONFIG_VAL(DEBUG_UART_BASE);
Dzmitry Sankouskif5be5c82021-10-17 13:44:27 +0300594
595 writel(DEF_TX_WM, base + SE_GENI_TX_WATERMARK_REG);
596 qcom_geni_serial_setup_tx(base, 1);
597 qcom_geni_serial_poll_bit(&init_dev, SE_GENI_M_IRQ_STATUS,
598 M_TX_FIFO_WATERMARK_EN, true);
599
600 writel(ch, base + SE_GENI_TX_FIFOn);
601 writel(M_TX_FIFO_WATERMARK_EN, base + SE_GENI_M_IRQ_CLEAR);
602 qcom_geni_serial_poll_tx_done(&init_dev);
603}
604
605DEBUG_UART_FUNCS
606
607#endif