rev13@wp.pl | ab3f0c7 | 2015-03-01 12:44:41 +0100 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2015 |
| 3 | * Kamil Lulko, <rev13@wp.pl> |
| 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0+ |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <asm/io.h> |
| 10 | #include <serial.h> |
| 11 | #include <asm/arch/stm32.h> |
| 12 | |
kunhuahuang | 60570df | 2015-04-28 03:01:19 +0800 | [diff] [blame] | 13 | /* |
| 14 | * Set up the usart port |
| 15 | */ |
| 16 | #if (CONFIG_STM32_USART >= 1) && (CONFIG_STM32_USART <= 6) |
| 17 | #define USART_PORT (CONFIG_STM32_USART - 1) |
| 18 | #else |
| 19 | #define USART_PORT 0 |
| 20 | #endif |
| 21 | /* |
| 22 | * Set up the usart base address |
| 23 | * |
| 24 | * --STM32_USARTD_BASE means default setting |
| 25 | */ |
rev13@wp.pl | ab3f0c7 | 2015-03-01 12:44:41 +0100 | [diff] [blame] | 26 | #define STM32_USART1_BASE (STM32_APB2PERIPH_BASE + 0x1000) |
kunhuahuang | 60570df | 2015-04-28 03:01:19 +0800 | [diff] [blame] | 27 | #define STM32_USART2_BASE (STM32_APB1PERIPH_BASE + 0x4400) |
| 28 | #define STM32_USART3_BASE (STM32_APB1PERIPH_BASE + 0x4800) |
| 29 | #define STM32_USART6_BASE (STM32_APB2PERIPH_BASE + 0x1400) |
| 30 | #define STM32_USARTD_BASE STM32_USART1_BASE |
| 31 | /* |
| 32 | * RCC USART specific definitions |
| 33 | * |
| 34 | * --RCC_ENR_USARTDEN means default setting |
| 35 | */ |
| 36 | #define RCC_ENR_USART1EN (1 << 4) |
| 37 | #define RCC_ENR_USART2EN (1 << 17) |
| 38 | #define RCC_ENR_USART3EN (1 << 18) |
| 39 | #define RCC_ENR_USART6EN (1 << 5) |
| 40 | #define RCC_ENR_USARTDEN RCC_ENR_USART1EN |
rev13@wp.pl | ab3f0c7 | 2015-03-01 12:44:41 +0100 | [diff] [blame] | 41 | |
| 42 | struct stm32_serial { |
| 43 | u32 sr; |
| 44 | u32 dr; |
| 45 | u32 brr; |
| 46 | u32 cr1; |
| 47 | u32 cr2; |
| 48 | u32 cr3; |
| 49 | u32 gtpr; |
| 50 | }; |
| 51 | |
| 52 | #define USART_CR1_RE (1 << 2) |
| 53 | #define USART_CR1_TE (1 << 3) |
| 54 | #define USART_CR1_UE (1 << 13) |
| 55 | |
| 56 | #define USART_SR_FLAG_RXNE (1 << 5) |
| 57 | #define USART_SR_FLAG_TXE (1 << 7) |
| 58 | |
| 59 | #define USART_BRR_F_MASK 0xF |
| 60 | #define USART_BRR_M_SHIFT 4 |
| 61 | #define USART_BRR_M_MASK 0xFFF0 |
| 62 | |
| 63 | DECLARE_GLOBAL_DATA_PTR; |
| 64 | |
kunhuahuang | 60570df | 2015-04-28 03:01:19 +0800 | [diff] [blame] | 65 | static const unsigned long usart_base[] = { |
| 66 | STM32_USART1_BASE, |
| 67 | STM32_USART2_BASE, |
| 68 | STM32_USART3_BASE, |
| 69 | STM32_USARTD_BASE, |
| 70 | STM32_USARTD_BASE, |
| 71 | STM32_USART6_BASE |
| 72 | }; |
| 73 | |
| 74 | static const unsigned long rcc_enr_en[] = { |
| 75 | RCC_ENR_USART1EN, |
| 76 | RCC_ENR_USART2EN, |
| 77 | RCC_ENR_USART3EN, |
| 78 | RCC_ENR_USARTDEN, |
| 79 | RCC_ENR_USARTDEN, |
| 80 | RCC_ENR_USART6EN |
| 81 | }; |
| 82 | |
rev13@wp.pl | ab3f0c7 | 2015-03-01 12:44:41 +0100 | [diff] [blame] | 83 | static void stm32_serial_setbrg(void) |
| 84 | { |
| 85 | serial_init(); |
| 86 | } |
| 87 | |
| 88 | static int stm32_serial_init(void) |
| 89 | { |
kunhuahuang | 60570df | 2015-04-28 03:01:19 +0800 | [diff] [blame] | 90 | struct stm32_serial *usart = |
| 91 | (struct stm32_serial *)usart_base[USART_PORT]; |
rev13@wp.pl | ab3f0c7 | 2015-03-01 12:44:41 +0100 | [diff] [blame] | 92 | u32 clock, int_div, frac_div, tmp; |
| 93 | |
kunhuahuang | 60570df | 2015-04-28 03:01:19 +0800 | [diff] [blame] | 94 | if ((usart_base[USART_PORT] & STM32_BUS_MASK) == |
| 95 | STM32_APB1PERIPH_BASE) { |
| 96 | setbits_le32(&STM32_RCC->apb1enr, rcc_enr_en[USART_PORT]); |
rev13@wp.pl | ab3f0c7 | 2015-03-01 12:44:41 +0100 | [diff] [blame] | 97 | clock = clock_get(CLOCK_APB1); |
kunhuahuang | 60570df | 2015-04-28 03:01:19 +0800 | [diff] [blame] | 98 | } else if ((usart_base[USART_PORT] & STM32_BUS_MASK) == |
| 99 | STM32_APB2PERIPH_BASE) { |
| 100 | setbits_le32(&STM32_RCC->apb2enr, rcc_enr_en[USART_PORT]); |
rev13@wp.pl | ab3f0c7 | 2015-03-01 12:44:41 +0100 | [diff] [blame] | 101 | clock = clock_get(CLOCK_APB2); |
| 102 | } else { |
| 103 | return -1; |
| 104 | } |
| 105 | |
| 106 | int_div = (25 * clock) / (4 * gd->baudrate); |
| 107 | tmp = ((int_div / 100) << USART_BRR_M_SHIFT) & USART_BRR_M_MASK; |
| 108 | frac_div = int_div - (100 * (tmp >> USART_BRR_M_SHIFT)); |
| 109 | tmp |= (((frac_div * 16) + 50) / 100) & USART_BRR_F_MASK; |
| 110 | |
| 111 | writel(tmp, &usart->brr); |
| 112 | setbits_le32(&usart->cr1, USART_CR1_RE | USART_CR1_TE | USART_CR1_UE); |
| 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | static int stm32_serial_getc(void) |
| 118 | { |
kunhuahuang | 60570df | 2015-04-28 03:01:19 +0800 | [diff] [blame] | 119 | struct stm32_serial *usart = |
| 120 | (struct stm32_serial *)usart_base[USART_PORT]; |
rev13@wp.pl | ab3f0c7 | 2015-03-01 12:44:41 +0100 | [diff] [blame] | 121 | while ((readl(&usart->sr) & USART_SR_FLAG_RXNE) == 0) |
| 122 | ; |
| 123 | return readl(&usart->dr); |
| 124 | } |
| 125 | |
| 126 | static void stm32_serial_putc(const char c) |
| 127 | { |
kunhuahuang | 60570df | 2015-04-28 03:01:19 +0800 | [diff] [blame] | 128 | struct stm32_serial *usart = |
| 129 | (struct stm32_serial *)usart_base[USART_PORT]; |
kunhuahuang | 85e5f5b | 2015-04-28 02:25:45 +0800 | [diff] [blame] | 130 | |
rev13@wp.pl | ab3f0c7 | 2015-03-01 12:44:41 +0100 | [diff] [blame] | 131 | while ((readl(&usart->sr) & USART_SR_FLAG_TXE) == 0) |
| 132 | ; |
| 133 | writel(c, &usart->dr); |
| 134 | } |
| 135 | |
| 136 | static int stm32_serial_tstc(void) |
| 137 | { |
kunhuahuang | 60570df | 2015-04-28 03:01:19 +0800 | [diff] [blame] | 138 | struct stm32_serial *usart = |
| 139 | (struct stm32_serial *)usart_base[USART_PORT]; |
rev13@wp.pl | ab3f0c7 | 2015-03-01 12:44:41 +0100 | [diff] [blame] | 140 | u8 ret; |
| 141 | |
| 142 | ret = readl(&usart->sr) & USART_SR_FLAG_RXNE; |
| 143 | return ret; |
| 144 | } |
| 145 | |
| 146 | static struct serial_device stm32_serial_drv = { |
| 147 | .name = "stm32_serial", |
| 148 | .start = stm32_serial_init, |
| 149 | .stop = NULL, |
| 150 | .setbrg = stm32_serial_setbrg, |
| 151 | .putc = stm32_serial_putc, |
| 152 | .puts = default_serial_puts, |
| 153 | .getc = stm32_serial_getc, |
| 154 | .tstc = stm32_serial_tstc, |
| 155 | }; |
| 156 | |
| 157 | void stm32_serial_initialize(void) |
| 158 | { |
| 159 | serial_register(&stm32_serial_drv); |
| 160 | } |
| 161 | |
| 162 | __weak struct serial_device *default_serial_console(void) |
| 163 | { |
| 164 | return &stm32_serial_drv; |
| 165 | } |