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