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> |
Dzmitry Sankouski | f5be5c8 | 2021-10-17 13:44:27 +0300 | [diff] [blame] | 12 | #include <dm.h> |
Dzmitry Sankouski | f5be5c8 | 2021-10-17 13:44:27 +0300 | [diff] [blame] | 13 | #include <errno.h> |
Dzmitry Sankouski | f5be5c8 | 2021-10-17 13:44:27 +0300 | [diff] [blame] | 14 | #include <linux/delay.h> |
Igor Prusov | c3421ea | 2023-11-09 20:10:04 +0300 | [diff] [blame] | 15 | #include <linux/time.h> |
Vladimir Zapolskiy | 75c472b | 2023-04-21 20:50:40 +0300 | [diff] [blame] | 16 | #include <misc.h> |
Dzmitry Sankouski | f5be5c8 | 2021-10-17 13:44:27 +0300 | [diff] [blame] | 17 | #include <serial.h> |
Dzmitry Sankouski | f5be5c8 | 2021-10-17 13:44:27 +0300 | [diff] [blame] | 18 | |
| 19 | #define UART_OVERSAMPLING 32 |
| 20 | #define STALE_TIMEOUT 160 |
| 21 | |
Dzmitry Sankouski | f5be5c8 | 2021-10-17 13:44:27 +0300 | [diff] [blame] | 22 | /* 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 Zapolskiy | 75c472b | 2023-04-21 20:50:40 +0300 | [diff] [blame] | 112 | /* GENI SE QUP Registers */ |
| 113 | #define QUP_HW_VER_REG 0x4 |
| 114 | #define QUP_SE_VERSION_2_5 0x20050000 |
| 115 | |
Dzmitry Sankouski | f5be5c8 | 2021-10-17 13:44:27 +0300 | [diff] [blame] | 116 | /* |
| 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 | |
| 128 | DECLARE_GLOBAL_DATA_PTR; |
| 129 | |
| 130 | struct msm_serial_data { |
| 131 | phys_addr_t base; |
| 132 | u32 baud; |
Vladimir Zapolskiy | 75c472b | 2023-04-21 20:50:40 +0300 | [diff] [blame] | 133 | u32 oversampling; |
Dzmitry Sankouski | f5be5c8 | 2021-10-17 13:44:27 +0300 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | unsigned long root_freq[] = {7372800, 14745600, 19200000, 29491200, |
Vladimir Zapolskiy | f76cc18 | 2023-04-21 20:50:37 +0300 | [diff] [blame] | 137 | 32000000, 48000000, 64000000, 80000000, |
| 138 | 96000000, 100000000}; |
Dzmitry Sankouski | f5be5c8 | 2021-10-17 13:44:27 +0300 | [diff] [blame] | 139 | |
| 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 | */ |
| 146 | static 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 Zapolskiy | f76cc18 | 2023-04-21 20:50:37 +0300 | [diff] [blame] | 167 | static int get_clk_div_rate(u32 baud, u64 sampling_rate, u32 *clk_div) |
Dzmitry Sankouski | f5be5c8 | 2021-10-17 13:44:27 +0300 | [diff] [blame] | 168 | { |
| 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 | |
| 184 | static int geni_serial_set_clock_rate(struct udevice *dev, u64 rate) |
| 185 | { |
| 186 | struct clk *clk; |
| 187 | int ret; |
| 188 | |
Vladimir Zapolskiy | 5025119 | 2023-04-21 20:50:36 +0300 | [diff] [blame] | 189 | clk = devm_clk_get(dev, NULL); |
Caleb Connolly | 0649d07 | 2023-11-14 12:51:12 +0000 | [diff] [blame] | 190 | if (IS_ERR(clk)) |
| 191 | return PTR_ERR(clk); |
Dzmitry Sankouski | f5be5c8 | 2021-10-17 13:44:27 +0300 | [diff] [blame] | 192 | |
| 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 | */ |
| 206 | static 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 | */ |
| 224 | static 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 | |
| 233 | static inline void geni_serial_baud(phys_addr_t base_address, u32 clk_div, |
Vladimir Zapolskiy | f76cc18 | 2023-04-21 20:50:37 +0300 | [diff] [blame] | 234 | int baud) |
Dzmitry Sankouski | f5be5c8 | 2021-10-17 13:44:27 +0300 | [diff] [blame] | 235 | { |
| 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 Zapolskiy | 51ff6c3 | 2023-04-21 20:50:38 +0300 | [diff] [blame] | 245 | static int msm_serial_setbrg(struct udevice *dev, int baud) |
Dzmitry Sankouski | f5be5c8 | 2021-10-17 13:44:27 +0300 | [diff] [blame] | 246 | { |
| 247 | struct msm_serial_data *priv = dev_get_priv(dev); |
Vladimir Zapolskiy | 51ff6c3 | 2023-04-21 20:50:38 +0300 | [diff] [blame] | 248 | u64 clk_rate; |
| 249 | u32 clk_div; |
Caleb Connolly | 0649d07 | 2023-11-14 12:51:12 +0000 | [diff] [blame] | 250 | int ret; |
Dzmitry Sankouski | f5be5c8 | 2021-10-17 13:44:27 +0300 | [diff] [blame] | 251 | |
| 252 | priv->baud = baud; |
Dzmitry Sankouski | f5be5c8 | 2021-10-17 13:44:27 +0300 | [diff] [blame] | 253 | |
Vladimir Zapolskiy | 75c472b | 2023-04-21 20:50:40 +0300 | [diff] [blame] | 254 | clk_rate = get_clk_div_rate(baud, priv->oversampling, &clk_div); |
Caleb Connolly | 0649d07 | 2023-11-14 12:51:12 +0000 | [diff] [blame] | 255 | 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 Sankouski | f5be5c8 | 2021-10-17 13:44:27 +0300 | [diff] [blame] | 260 | 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 | */ |
| 278 | static bool qcom_geni_serial_poll_bit(const struct udevice *dev, int offset, |
Vladimir Zapolskiy | f76cc18 | 2023-04-21 20:50:37 +0300 | [diff] [blame] | 279 | int field, bool set) |
Dzmitry Sankouski | f5be5c8 | 2021-10-17 13:44:27 +0300 | [diff] [blame] | 280 | { |
| 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 | |
| 316 | static 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 | |
| 325 | static 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 | |
| 342 | static 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 | */ |
| 356 | static 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 | |
| 367 | static 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 | |
| 386 | static 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 | |
| 399 | static 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 | |
| 412 | static 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 | |
| 427 | static 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 | |
| 445 | static 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 | |
| 471 | static 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 | |
| 485 | static 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 Connolly | 29ac8d9 | 2023-11-14 12:51:11 +0000 | [diff] [blame] | 492 | static int geni_set_oversampling(struct udevice *dev) |
Vladimir Zapolskiy | 75c472b | 2023-04-21 20:50:40 +0300 | [diff] [blame] | 493 | { |
| 494 | struct msm_serial_data *priv = dev_get_priv(dev); |
Caleb Connolly | 29ac8d9 | 2023-11-14 12:51:11 +0000 | [diff] [blame] | 495 | ofnode parent_node = ofnode_get_parent(dev_ofnode(dev)); |
Vladimir Zapolskiy | 75c472b | 2023-04-21 20:50:40 +0300 | [diff] [blame] | 496 | u32 geni_se_version; |
Caleb Connolly | 29ac8d9 | 2023-11-14 12:51:11 +0000 | [diff] [blame] | 497 | fdt_addr_t addr; |
Vladimir Zapolskiy | 75c472b | 2023-04-21 20:50:40 +0300 | [diff] [blame] | 498 | |
| 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 Connolly | 29ac8d9 | 2023-11-14 12:51:11 +0000 | [diff] [blame] | 505 | 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 Zapolskiy | 75c472b | 2023-04-21 20:50:40 +0300 | [diff] [blame] | 510 | |
Caleb Connolly | 29ac8d9 | 2023-11-14 12:51:11 +0000 | [diff] [blame] | 511 | /* 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 Zapolskiy | 75c472b | 2023-04-21 20:50:40 +0300 | [diff] [blame] | 514 | |
| 515 | if (geni_se_version >= QUP_SE_VERSION_2_5) |
| 516 | priv->oversampling /= 2; |
Caleb Connolly | 29ac8d9 | 2023-11-14 12:51:11 +0000 | [diff] [blame] | 517 | |
| 518 | return 0; |
Vladimir Zapolskiy | 75c472b | 2023-04-21 20:50:40 +0300 | [diff] [blame] | 519 | } |
| 520 | |
Dzmitry Sankouski | f5be5c8 | 2021-10-17 13:44:27 +0300 | [diff] [blame] | 521 | static 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 | |
| 560 | static int msm_serial_probe(struct udevice *dev) |
| 561 | { |
| 562 | struct msm_serial_data *priv = dev_get_priv(dev); |
Caleb Connolly | 29ac8d9 | 2023-11-14 12:51:11 +0000 | [diff] [blame] | 563 | int ret; |
Dzmitry Sankouski | f5be5c8 | 2021-10-17 13:44:27 +0300 | [diff] [blame] | 564 | |
Caleb Connolly | 29ac8d9 | 2023-11-14 12:51:11 +0000 | [diff] [blame] | 565 | ret = geni_set_oversampling(dev); |
| 566 | if (ret < 0) |
| 567 | return ret; |
Vladimir Zapolskiy | 75c472b | 2023-04-21 20:50:40 +0300 | [diff] [blame] | 568 | |
Dzmitry Sankouski | f5be5c8 | 2021-10-17 13:44:27 +0300 | [diff] [blame] | 569 | /* 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 | |
| 581 | static 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 | |
| 592 | static const struct udevice_id msm_serial_ids[] = { |
Konrad Dybcio | 0c8712b | 2023-04-21 20:50:39 +0300 | [diff] [blame] | 593 | { .compatible = "qcom,geni-debug-uart" }, |
| 594 | { } |
| 595 | }; |
Dzmitry Sankouski | f5be5c8 | 2021-10-17 13:44:27 +0300 | [diff] [blame] | 596 | |
| 597 | U_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 Connolly | e07ce56 | 2024-04-03 14:07:39 +0200 | [diff] [blame] | 605 | .flags = DM_FLAG_PRE_RELOC | DM_FLAG_DEFAULT_PD_CTRL_OFF, |
Dzmitry Sankouski | f5be5c8 | 2021-10-17 13:44:27 +0300 | [diff] [blame] | 606 | }; |
| 607 | |
Caleb Connolly | 7f6b918 | 2024-04-03 14:07:44 +0200 | [diff] [blame] | 608 | static const struct udevice_id geniqup_ids[] = { |
| 609 | { .compatible = "qcom,geni-se-qup" }, |
| 610 | { } |
| 611 | }; |
| 612 | |
| 613 | U_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 Sankouski | f5be5c8 | 2021-10-17 13:44:27 +0300 | [diff] [blame] | 621 | #ifdef CONFIG_DEBUG_UART_MSM_GENI |
| 622 | |
| 623 | static struct msm_serial_data init_serial_data = { |
Pali Rohár | 8864b35 | 2022-05-27 22:15:24 +0200 | [diff] [blame] | 624 | .base = CONFIG_VAL(DEBUG_UART_BASE) |
Dzmitry Sankouski | f5be5c8 | 2021-10-17 13:44:27 +0300 | [diff] [blame] | 625 | }; |
| 626 | |
| 627 | /* Serial dumb device, to reuse driver code */ |
| 628 | static 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 | |
| 640 | static inline void _debug_uart_init(void) |
| 641 | { |
Pali Rohár | 8864b35 | 2022-05-27 22:15:24 +0200 | [diff] [blame] | 642 | phys_addr_t base = CONFIG_VAL(DEBUG_UART_BASE); |
Dzmitry Sankouski | f5be5c8 | 2021-10-17 13:44:27 +0300 | [diff] [blame] | 643 | |
| 644 | geni_serial_init(&init_dev); |
| 645 | geni_serial_baud(base, CLK_DIV, CONFIG_BAUDRATE); |
| 646 | qcom_geni_serial_start_tx(base); |
| 647 | } |
| 648 | |
| 649 | static inline void _debug_uart_putc(int ch) |
| 650 | { |
Pali Rohár | 8864b35 | 2022-05-27 22:15:24 +0200 | [diff] [blame] | 651 | phys_addr_t base = CONFIG_VAL(DEBUG_UART_BASE); |
Dzmitry Sankouski | f5be5c8 | 2021-10-17 13:44:27 +0300 | [diff] [blame] | 652 | |
| 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 | |
| 663 | DEBUG_UART_FUNCS |
| 664 | |
| 665 | #endif |