blob: 8b2830b946a83b9bf3675c4fef03d2c8fc815882 [file] [log] [blame]
rev13@wp.plab3f0c72015-03-01 12:44:41 +01001/*
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
kunhuahuang60570df2015-04-28 03:01:19 +080013/*
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.plab3f0c72015-03-01 12:44:41 +010026#define STM32_USART1_BASE (STM32_APB2PERIPH_BASE + 0x1000)
kunhuahuang60570df2015-04-28 03:01:19 +080027#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.plab3f0c72015-03-01 12:44:41 +010041
42struct 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
63DECLARE_GLOBAL_DATA_PTR;
64
kunhuahuang60570df2015-04-28 03:01:19 +080065static 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
74static 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.plab3f0c72015-03-01 12:44:41 +010083static void stm32_serial_setbrg(void)
84{
85 serial_init();
86}
87
88static int stm32_serial_init(void)
89{
kunhuahuang60570df2015-04-28 03:01:19 +080090 struct stm32_serial *usart =
91 (struct stm32_serial *)usart_base[USART_PORT];
rev13@wp.plab3f0c72015-03-01 12:44:41 +010092 u32 clock, int_div, frac_div, tmp;
93
kunhuahuang60570df2015-04-28 03:01:19 +080094 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.plab3f0c72015-03-01 12:44:41 +010097 clock = clock_get(CLOCK_APB1);
kunhuahuang60570df2015-04-28 03:01:19 +080098 } 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.plab3f0c72015-03-01 12:44:41 +0100101 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
117static int stm32_serial_getc(void)
118{
kunhuahuang60570df2015-04-28 03:01:19 +0800119 struct stm32_serial *usart =
120 (struct stm32_serial *)usart_base[USART_PORT];
rev13@wp.plab3f0c72015-03-01 12:44:41 +0100121 while ((readl(&usart->sr) & USART_SR_FLAG_RXNE) == 0)
122 ;
123 return readl(&usart->dr);
124}
125
126static void stm32_serial_putc(const char c)
127{
kunhuahuang60570df2015-04-28 03:01:19 +0800128 struct stm32_serial *usart =
129 (struct stm32_serial *)usart_base[USART_PORT];
kunhuahuang85e5f5b2015-04-28 02:25:45 +0800130
Tom Rini445614c2015-05-27 13:24:10 -0400131 if (c == '\n')
132 stm32_serial_putc('\r');
133
rev13@wp.plab3f0c72015-03-01 12:44:41 +0100134 while ((readl(&usart->sr) & USART_SR_FLAG_TXE) == 0)
135 ;
136 writel(c, &usart->dr);
137}
138
139static int stm32_serial_tstc(void)
140{
kunhuahuang60570df2015-04-28 03:01:19 +0800141 struct stm32_serial *usart =
142 (struct stm32_serial *)usart_base[USART_PORT];
rev13@wp.plab3f0c72015-03-01 12:44:41 +0100143 u8 ret;
144
145 ret = readl(&usart->sr) & USART_SR_FLAG_RXNE;
146 return ret;
147}
148
149static struct serial_device stm32_serial_drv = {
150 .name = "stm32_serial",
151 .start = stm32_serial_init,
152 .stop = NULL,
153 .setbrg = stm32_serial_setbrg,
154 .putc = stm32_serial_putc,
155 .puts = default_serial_puts,
156 .getc = stm32_serial_getc,
157 .tstc = stm32_serial_tstc,
158};
159
160void stm32_serial_initialize(void)
161{
162 serial_register(&stm32_serial_drv);
163}
164
165__weak struct serial_device *default_serial_console(void)
166{
167 return &stm32_serial_drv;
168}