1 // SPDX-License-Identifier: GPL-2.0+
3 * Support for Serial I/O using STMicroelectronics' on-chip ASC.
5 * Copyright (C) 2017, STMicroelectronics - All Rights Reserved
6 * Author(s): Patrice Chotard, <patrice.chotard@foss.st.com> for STMicroelectronics.
13 #include <asm/global_data.h>
15 #include <linux/bitops.h>
17 DECLARE_GLOBAL_DATA_PTR;
19 #define BAUDMODE 0x00001000
20 #define RXENABLE 0x00000100
21 #define RUN 0x00000080
22 #define MODE 0x00000001
23 #define MODE_8BIT 0x0001
24 #define STOP_1BIT 0x0008
25 #define PARITYODD 0x0020
28 #define STA_RBF BIT(0)
43 struct sti_asc_serial {
44 /* address of registers in physical memory */
45 struct sti_asc_uart *regs;
48 /* Values for the BAUDRATE Register */
49 #define PCLK (200ul * 1000000ul)
50 #define BAUDRATE_VAL_M0(bps) (PCLK / (16 * (bps)))
51 #define BAUDRATE_VAL_M1(bps) ((bps * (1 << 14)) + (1<<13)) / (PCLK/(1 << 6))
56 * ASCBaudRate = ----------------
60 * baudrate * 16 * 2^16
61 * ASCBaudRate = ------------------------
65 * Mode 1 should be used for baudrates of 19200, and above, as it
66 * has a lower deviation error than Mode 0 for higher frequencies.
67 * Mode 0 should be used for all baudrates below 19200.
70 static int sti_asc_pending(struct udevice *dev, bool input)
72 struct sti_asc_serial *priv = dev_get_priv(dev);
73 struct sti_asc_uart *const uart = priv->regs;
76 status = readl(&uart->status);
78 return status & STA_RBF;
80 return status & STA_TF;
83 static int _sti_asc_serial_setbrg(struct sti_asc_uart *uart, int baudrate)
90 t = BAUDRATE_VAL_M0(9600);
94 t = BAUDRATE_VAL_M1(19200);
97 t = BAUDRATE_VAL_M1(38400);
100 t = BAUDRATE_VAL_M1(57600);
103 debug("ASC: unsupported baud rate: %d, using 115200 instead.\n",
106 t = BAUDRATE_VAL_M1(115200);
110 /* disable the baudrate generator */
111 val = readl(&uart->control);
112 writel(val & ~RUN, &uart->control);
114 /* set baud generator reload value */
115 writel(t, &uart->baudrate);
116 /* reset the RX & TX buffers */
117 writel(1, &uart->txreset);
118 writel(1, &uart->rxreset);
120 /* set baud generator mode */
124 /* finally, write value and enable ASC */
125 writel(val, &uart->control);
130 /* called to adjust baud-rate */
131 static int sti_asc_serial_setbrg(struct udevice *dev, int baudrate)
133 struct sti_asc_serial *priv = dev_get_priv(dev);
134 struct sti_asc_uart *const uart = priv->regs;
136 return _sti_asc_serial_setbrg(uart, baudrate);
139 /* blocking function, that returns next char */
140 static int sti_asc_serial_getc(struct udevice *dev)
142 struct sti_asc_serial *priv = dev_get_priv(dev);
143 struct sti_asc_uart *const uart = priv->regs;
145 /* polling wait: for a char to be read */
146 if (!sti_asc_pending(dev, true))
149 return readl(&uart->rxbuf);
152 /* write write out a single char */
153 static int sti_asc_serial_putc(struct udevice *dev, const char c)
155 struct sti_asc_serial *priv = dev_get_priv(dev);
156 struct sti_asc_uart *const uart = priv->regs;
158 /* wait till safe to write next char */
159 if (sti_asc_pending(dev, false))
162 /* finally, write next char */
163 writel(c, &uart->txbuf);
168 /* initialize the ASC */
169 static int sti_asc_serial_probe(struct udevice *dev)
171 struct sti_asc_serial *priv = dev_get_priv(dev);
175 base = dev_read_addr(dev);
176 if (base == FDT_ADDR_T_NONE)
179 priv->regs = (struct sti_asc_uart *)base;
180 sti_asc_serial_setbrg(dev, gd->baudrate);
183 * build up the value to be written to CONTROL
184 * set character length, bit stop number, odd parity
186 val = RXENABLE | RUN | MODE_8BIT | STOP_1BIT | PARITYODD;
187 writel(val, &priv->regs->control);
192 static const struct dm_serial_ops sti_asc_serial_ops = {
193 .putc = sti_asc_serial_putc,
194 .pending = sti_asc_pending,
195 .getc = sti_asc_serial_getc,
196 .setbrg = sti_asc_serial_setbrg,
199 static const struct udevice_id sti_serial_of_match[] = {
200 { .compatible = "st,asc" },
204 U_BOOT_DRIVER(serial_sti_asc) = {
205 .name = "serial_sti_asc",
207 .of_match = sti_serial_of_match,
208 .ops = &sti_asc_serial_ops,
209 .probe = sti_asc_serial_probe,
210 .priv_auto = sizeof(struct sti_asc_serial),