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