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