1865e3474SBiju Das/* 2*9979a20aSMarek Vasut * Copyright (c) 2015-2025, Renesas Electronics Corporation. All rights reserved. 399fe5c2fSBoyan Karatotev * Copyright (c) 2025, Arm Limited. All rights reserved. 4865e3474SBiju Das * 5865e3474SBiju Das * SPDX-License-Identifier: BSD-3-Clause 6865e3474SBiju Das */ 7865e3474SBiju Das 8865e3474SBiju Das#include <arch.h> 9865e3474SBiju Das#include <asm_macros.S> 10865e3474SBiju Das#include <console_macros.S> 11865e3474SBiju Das#include <drivers/renesas/rcar/console/console.h> 1299fe5c2fSBoyan Karatotev#include <platform_def.h> 13865e3474SBiju Das 14865e3474SBiju Das#define SCIF_INTERNAL_CLK 0 15865e3474SBiju Das#define SCIF_EXTARNAL_CLK 1 16865e3474SBiju Das#define SCIF_CLK SCIF_INTERNAL_CLK 17865e3474SBiju Das 18865e3474SBiju Das#define PRR_PRODUCT_H3_VER_10 (0x00004F00) 19865e3474SBiju Das 20865e3474SBiju Das/* module stop */ 21865e3474SBiju Das#define CPG_BASE (0xE6150000) 22865e3474SBiju Das#define CPG_SMSTPCR2 (0x0138) 23865e3474SBiju Das#define CPG_SMSTPCR3 (0x013C) 24865e3474SBiju Das#define CPG_MSTPSR2 (0x0040) 25865e3474SBiju Das#define CPG_MSTPSR3 (0x0048) 26865e3474SBiju Das#define MSTP207 (1 << 7) 27865e3474SBiju Das#define MSTP310 (1 << 10) 28865e3474SBiju Das#define CPG_CPGWPR (0x0900) 29865e3474SBiju Das 30865e3474SBiju Das/* scif */ 31865e3474SBiju Das#define SCIF0_BASE (0xE6E60000) 32865e3474SBiju Das#define SCIF2_BASE (0xE6E88000) 33865e3474SBiju Das#define SCIF_SCSMR (0x00) 34865e3474SBiju Das#define SCIF_SCBRR (0x04) 35865e3474SBiju Das#define SCIF_SCSCR (0x08) 36865e3474SBiju Das#define SCIF_SCFTDR (0x0C) 37865e3474SBiju Das#define SCIF_SCFSR (0x10) 38865e3474SBiju Das#define SCIF_SCFRDR (0x14) 39865e3474SBiju Das#define SCIF_SCFCR (0x18) 40865e3474SBiju Das#define SCIF_SCFDR (0x1C) 41865e3474SBiju Das#define SCIF_SCSPTR (0x20) 42865e3474SBiju Das#define SCIF_SCLSR (0x24) 43865e3474SBiju Das#define SCIF_DL (0x30) 44865e3474SBiju Das#define SCIF_CKS (0x34) 45865e3474SBiju Das 46865e3474SBiju Das#if RCAR_LSI == RCAR_V3M 47865e3474SBiju Das#define SCIF_BASE SCIF0_BASE 48865e3474SBiju Das#define CPG_SMSTPCR CPG_SMSTPCR2 49865e3474SBiju Das#define CPG_MSTPSR CPG_MSTPSR2 50865e3474SBiju Das#define MSTP MSTP207 51865e3474SBiju Das#else 52865e3474SBiju Das#define SCIF_BASE SCIF2_BASE 53865e3474SBiju Das#define CPG_SMSTPCR CPG_SMSTPCR3 54865e3474SBiju Das#define CPG_MSTPSR CPG_MSTPSR3 55865e3474SBiju Das#define MSTP MSTP310 56865e3474SBiju Das#endif 57865e3474SBiju Das 58865e3474SBiju Das/* mode pin */ 59865e3474SBiju Das#define MODEMR_MD12 (0x00001000) 60865e3474SBiju Das 61865e3474SBiju Das#define SCSMR_CA_MASK (1 << 7) 62865e3474SBiju Das#define SCSMR_CA_ASYNC (0x0000) 63865e3474SBiju Das#define SCSMR_CHR_MASK (1 << 6) 64865e3474SBiju Das#define SCSMR_CHR_8 (0x0000) 65865e3474SBiju Das#define SCSMR_PE_MASK (1 << 5) 66865e3474SBiju Das#define SCSMR_PE_DIS (0x0000) 67865e3474SBiju Das#define SCSMR_STOP_MASK (1 << 3) 68865e3474SBiju Das#define SCSMR_STOP_1 (0x0000) 69865e3474SBiju Das#define SCSMR_CKS_MASK (3 << 0) 70865e3474SBiju Das#define SCSMR_CKS_DIV1 (0x0000) 71865e3474SBiju Das#define SCSMR_INIT_DATA (SCSMR_CA_ASYNC + \ 72865e3474SBiju Das SCSMR_CHR_8 + \ 73865e3474SBiju Das SCSMR_PE_DIS + \ 74865e3474SBiju Das SCSMR_STOP_1 + \ 75865e3474SBiju Das SCSMR_CKS_DIV1) 76865e3474SBiju Das#define SCBRR_115200BPS (17) 7714f0a081SToshiyuki Ogasahara#define SCBRR_115200BPS_D3_SSCG (16) 78865e3474SBiju Das#define SCBRR_115200BPS_E3_SSCG (15) 79865e3474SBiju Das#define SCBRR_230400BPS (8) 80865e3474SBiju Das 81865e3474SBiju Das#define SCSCR_TE_MASK (1 << 5) 82865e3474SBiju Das#define SCSCR_TE_DIS (0x0000) 83865e3474SBiju Das#define SCSCR_TE_EN (0x0020) 84865e3474SBiju Das#define SCSCR_RE_MASK (1 << 4) 85865e3474SBiju Das#define SCSCR_RE_DIS (0x0000) 86865e3474SBiju Das#define SCSCR_RE_EN (0x0010) 87865e3474SBiju Das#define SCSCR_CKE_MASK (3 << 0) 88865e3474SBiju Das#define SCSCR_CKE_INT (0x0000) 89865e3474SBiju Das#define SCSCR_CKE_BRG (0x0002) 90865e3474SBiju Das#if SCIF_CLK == SCIF_EXTARNAL_CLK 91865e3474SBiju Das#define SCSCR_CKE_INT_CLK (SCSCR_CKE_BRG) 92865e3474SBiju Das#else 93865e3474SBiju Das#define SCFSR_TEND_MASK (1 << 6) 94865e3474SBiju Das#define SCFSR_TEND_TRANS_END (0x0040) 95865e3474SBiju Das#define SCSCR_CKE_INT_CLK (SCSCR_CKE_INT) 96865e3474SBiju Das#endif 97865e3474SBiju Das#define SCFSR_INIT_DATA (0x0000) 98865e3474SBiju Das#define SCFCR_TTRG_MASK (3 << 4) 99865e3474SBiju Das#define SCFCR_TTRG_8 (0x0000) 100865e3474SBiju Das#define SCFCR_TTRG_0 (0x0030) 101865e3474SBiju Das#define SCFCR_TFRST_MASK (1 << 2) 102865e3474SBiju Das#define SCFCR_TFRST_DIS (0x0000) 103865e3474SBiju Das#define SCFCR_TFRST_EN (0x0004) 104865e3474SBiju Das#define SCFCR_RFRS_MASK (1 << 1) 105865e3474SBiju Das#define SCFCR_RFRS_DIS (0x0000) 106865e3474SBiju Das#define SCFCR_RFRS_EN (0x0002) 107865e3474SBiju Das#define SCFCR_INIT_DATA (SCFCR_TTRG_8) 108865e3474SBiju Das#define SCFDR_T_MASK (0x1f << 8) 109865e3474SBiju Das#define DL_INIT_DATA (8) 110865e3474SBiju Das#define CKS_CKS_DIV_MASK (1 << 15) 111865e3474SBiju Das#define CKS_CKS_DIV_CLK (0x0000) 112865e3474SBiju Das#define CKS_XIN_MASK (1 << 14) 113865e3474SBiju Das#define CKS_XIN_SCIF_CLK (0x0000) 114865e3474SBiju Das#define CKS_INIT_DATA (CKS_CKS_DIV_CLK + CKS_XIN_SCIF_CLK) 115865e3474SBiju Das 116865e3474SBiju Das .globl console_rcar_init 117865e3474SBiju Das .globl console_rcar_putc 118865e3474SBiju Das .globl console_rcar_flush 119865e3474SBiju Das 120865e3474SBiju Das /* 121865e3474SBiju Das * int console_rcar_init(unsigned long base_addr, 122865e3474SBiju Das * unsigned int uart_clk, unsigned int baud_rate) 123865e3474SBiju Das * Function to initialize the console without a 124865e3474SBiju Das * C Runtime to print debug information. This 125865e3474SBiju Das * function will be accessed by console_rcar_register 126865e3474SBiju Das * and crash reporting. 127865e3474SBiju Das * In: x0 - console base address 128865e3474SBiju Das * w1 - Uart clock in Hz 129865e3474SBiju Das * w2 - Baud rate 130865e3474SBiju Das * Out: return 1 on success 131865e3474SBiju Das * Clobber list : x1, x2 132865e3474SBiju Das */ 133865e3474SBiju Dasfunc console_rcar_init 134865e3474SBiju Das ldr x0, =CPG_BASE 135865e3474SBiju Das ldr w1, [x0, #CPG_SMSTPCR] 136865e3474SBiju Das and w1, w1, #~MSTP 137865e3474SBiju Das mvn w2, w1 138865e3474SBiju Das str w2, [x0, #CPG_CPGWPR] 139865e3474SBiju Das str w1, [x0, #CPG_SMSTPCR] 140865e3474SBiju Das5: 141865e3474SBiju Das ldr w1, [x0, #CPG_MSTPSR] 142865e3474SBiju Das and w1, w1, #MSTP 143865e3474SBiju Das cbnz w1, 5b 144865e3474SBiju Das 145865e3474SBiju Das ldr x0, =SCIF_BASE 146865e3474SBiju Das /* Clear bits TE and RE in SCSCR to 0 */ 147865e3474SBiju Das mov w1, #(SCSCR_TE_DIS + SCSCR_RE_DIS) 148865e3474SBiju Das strh w1, [x0, #SCIF_SCSCR] 149865e3474SBiju Das /* Set bits TFRST and RFRST in SCFCR to 1 */ 150865e3474SBiju Das ldrh w1, [x0, #SCIF_SCFCR] 151865e3474SBiju Das orr w1, w1, #(SCFCR_TFRST_EN + SCFCR_RFRS_EN) 152865e3474SBiju Das strh w1, [x0, #SCIF_SCFCR] 153865e3474SBiju Das /* 154865e3474SBiju Das * Read flags of ER, DR, BRK, and RDF in SCFSR and those of TO and ORER 155865e3474SBiju Das * in SCLSR, then clear them to 0 156865e3474SBiju Das */ 157865e3474SBiju Das mov w1, #SCFSR_INIT_DATA 158865e3474SBiju Das strh w1, [x0, #SCIF_SCFSR] 159865e3474SBiju Das mov w1, #0 160865e3474SBiju Das strh w1, [x0, #SCIF_SCLSR] 161865e3474SBiju Das /* Set bits CKE[1:0] in SCSCR */ 162865e3474SBiju Das ldrh w1, [x0, #SCIF_SCSCR] 163865e3474SBiju Das and w1, w1, #~SCSCR_CKE_MASK 164865e3474SBiju Das mov w2, #SCSCR_CKE_INT_CLK 165865e3474SBiju Das orr w1, w1, w2 166865e3474SBiju Das strh w1, [x0, #SCIF_SCSCR] 167865e3474SBiju Das /* Set data transfer format in SCSMR */ 168865e3474SBiju Das mov w1, #SCSMR_INIT_DATA 169865e3474SBiju Das strh w1, [x0, #SCIF_SCSMR] 170865e3474SBiju Das /* Set value in SCBRR */ 171865e3474SBiju Das#if SCIF_CLK == SCIF_INTERNAL_CLK 172865e3474SBiju Das ldr x1, =PRR 173865e3474SBiju Das ldr w1, [x1] 174865e3474SBiju Das and w1, w1, #(PRR_PRODUCT_MASK | PRR_CUT_MASK) 175865e3474SBiju Das mov w2, #PRR_PRODUCT_H3_VER_10 176865e3474SBiju Das cmp w1, w2 177865e3474SBiju Das beq 3f 178865e3474SBiju Das and w1, w1, #PRR_PRODUCT_MASK 179865e3474SBiju Das mov w2, #PRR_PRODUCT_D3 180865e3474SBiju Das cmp w1, w2 18114f0a081SToshiyuki Ogasahara beq 5f 182865e3474SBiju Das and w1, w1, #PRR_PRODUCT_MASK 183865e3474SBiju Das mov w2, #PRR_PRODUCT_E3 184865e3474SBiju Das cmp w1, w2 18514f0a081SToshiyuki Ogasahara bne 4f 186865e3474SBiju Das 18714f0a081SToshiyuki Ogasahara /* When SSCG(MD12) on (E3) */ 188865e3474SBiju Das ldr x1, =RST_MODEMR 189865e3474SBiju Das ldr w1, [x1] 190865e3474SBiju Das and w1, w1, #MODEMR_MD12 191865e3474SBiju Das mov w2, #MODEMR_MD12 192865e3474SBiju Das cmp w1, w2 19314f0a081SToshiyuki Ogasahara bne 4f 194865e3474SBiju Das 19514f0a081SToshiyuki Ogasahara /* When SSCG(MD12) on (E3) */ 196865e3474SBiju Das mov w1, #SCBRR_115200BPS_E3_SSCG 197865e3474SBiju Das b 2f 198865e3474SBiju Das5: 19914f0a081SToshiyuki Ogasahara /* In case of D3 */ 20014f0a081SToshiyuki Ogasahara ldr x1, =RST_MODEMR 20114f0a081SToshiyuki Ogasahara ldr w1, [x1] 20214f0a081SToshiyuki Ogasahara and w1, w1, #MODEMR_MD12 20314f0a081SToshiyuki Ogasahara mov w2, #MODEMR_MD12 20414f0a081SToshiyuki Ogasahara cmp w1, w2 20514f0a081SToshiyuki Ogasahara bne 4f 20614f0a081SToshiyuki Ogasahara 20714f0a081SToshiyuki Ogasahara /* When SSCG(MD12) on (D3) */ 20814f0a081SToshiyuki Ogasahara mov w1, #SCBRR_115200BPS_D3_SSCG 209865e3474SBiju Das b 2f 210865e3474SBiju Das4: 21114f0a081SToshiyuki Ogasahara /* In case of H3/M3/M3N or when SSCG(MD12) is off in E3/D3 */ 21214f0a081SToshiyuki Ogasahara mov w1, #SCBRR_115200BPS 213865e3474SBiju Das b 2f 214865e3474SBiju Das3: 215865e3474SBiju Das mov w1, #SCBRR_230400BPS 216865e3474SBiju Das2: 217865e3474SBiju Das strb w1, [x0, SCIF_SCBRR] 218865e3474SBiju Das#else 219865e3474SBiju Das mov w1, #DL_INIT_DATA 220865e3474SBiju Das strh w1, [x0, #SCIF_DL] 221865e3474SBiju Das mov w1, #CKS_INIT_DATA 222865e3474SBiju Das strh w1, [x0, #SCIF_CKS] 223865e3474SBiju Das#endif 224865e3474SBiju Das /* 1-bit interval elapsed */ 225865e3474SBiju Das mov w1, #100 226865e3474SBiju Das1: 227865e3474SBiju Das subs w1, w1, #1 228865e3474SBiju Das cbnz w1, 1b 229865e3474SBiju Das /* 230865e3474SBiju Das * Set bits RTRG[1:0], TTRG[1:0], and MCE in SCFCR 231865e3474SBiju Das * Clear bits FRST and RFRST to 0 232865e3474SBiju Das */ 233865e3474SBiju Das mov w1, #SCFCR_INIT_DATA 234865e3474SBiju Das strh w1, [x0, #SCIF_SCFCR] 235865e3474SBiju Das /* Set bits TE and RE in SCSCR to 1 */ 236865e3474SBiju Das ldrh w1, [x0, #SCIF_SCSCR] 237865e3474SBiju Das orr w1, w1, #(SCSCR_TE_EN + SCSCR_RE_EN) 238865e3474SBiju Das strh w1, [x0, #SCIF_SCSCR] 239865e3474SBiju Das mov x0, #1 240865e3474SBiju Das 241865e3474SBiju Das ret 242865e3474SBiju Dasendfunc console_rcar_init 243865e3474SBiju Das 244865e3474SBiju Das /* 245865e3474SBiju Das * int console_rcar_putc(int c, unsigned int base_addr) 246865e3474SBiju Das * Function to output a character over the console. It 247865e3474SBiju Das * returns the character printed on success or -1 on error. 248865e3474SBiju Das * In : w0 - character to be printed 249865e3474SBiju Das * x1 - pointer to console_t structure 250865e3474SBiju Das * Out : return -1 on error else return character. 251865e3474SBiju Das * Clobber list : x2 252865e3474SBiju Das */ 253865e3474SBiju Dasfunc console_rcar_putc 254865e3474SBiju Das ldr x1, =SCIF_BASE 255865e3474SBiju Das cmp w0, #0xA 256865e3474SBiju Das /* Prepend '\r' to '\n' */ 257865e3474SBiju Das bne 2f 258865e3474SBiju Das1: 259865e3474SBiju Das /* Check if the transmit FIFO is full */ 260865e3474SBiju Das ldrh w2, [x1, #SCIF_SCFDR] 261865e3474SBiju Das ubfx w2, w2, #8, #5 262865e3474SBiju Das cmp w2, #16 263865e3474SBiju Das bcs 1b 264865e3474SBiju Das mov w2, #0x0D 265865e3474SBiju Das strb w2, [x1, #SCIF_SCFTDR] 266865e3474SBiju Das2: 267865e3474SBiju Das /* Check if the transmit FIFO is full */ 268865e3474SBiju Das ldrh w2, [x1, #SCIF_SCFDR] 269865e3474SBiju Das ubfx w2, w2, #8, #5 270865e3474SBiju Das cmp w2, #16 271865e3474SBiju Das bcs 2b 272865e3474SBiju Das strb w0, [x1, #SCIF_SCFTDR] 273865e3474SBiju Das 274865e3474SBiju Das /* Clear TEND flag */ 275865e3474SBiju Das ldrh w2, [x1, #SCIF_SCFSR] 276865e3474SBiju Das and w2, w2, #~SCFSR_TEND_MASK 277865e3474SBiju Das strh w2, [x1, #SCIF_SCFSR] 278865e3474SBiju Das 279865e3474SBiju Das ret 280865e3474SBiju Dasendfunc console_rcar_putc 281865e3474SBiju Das 282865e3474SBiju Das /* 283865e3474SBiju Das * void console_rcar_flush(void) 284865e3474SBiju Das * Function to force a write of all buffered 285865e3474SBiju Das * data that hasn't been output. It returns void 286865e3474SBiju Das * Clobber list : x0, x1 287865e3474SBiju Das */ 288865e3474SBiju Dasfunc console_rcar_flush 289865e3474SBiju Das ldr x0, =SCIF_BASE 290865e3474SBiju Das1: 291865e3474SBiju Das /* Check TEND flag */ 292865e3474SBiju Das ldrh w1, [x0, #SCIF_SCFSR] 293865e3474SBiju Das and w1, w1, #SCFSR_TEND_MASK 294865e3474SBiju Das cmp w1, #SCFSR_TEND_TRANS_END 295865e3474SBiju Das bne 1b 296865e3474SBiju Das 297865e3474SBiju Das ldr x0, =SCIF_BASE 298865e3474SBiju Das ldrh w1, [x0, #SCIF_SCSCR] 299865e3474SBiju Das and w1, w1, #~(SCSCR_TE_EN + SCSCR_RE_EN) 300865e3474SBiju Das strh w1, [x0, #SCIF_SCSCR] 301865e3474SBiju Das 302865e3474SBiju Das ret 303865e3474SBiju Dasendfunc console_rcar_flush 304