rev13@wp.pl | af9873c | 2015-03-01 12:44:41 +0100 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2015 |
Kamil Lulko | 75d48a6 | 2015-12-01 09:08:19 +0100 | [diff] [blame] | 3 | * Kamil Lulko, <kamil.lulko@gmail.com> |
rev13@wp.pl | af9873c | 2015-03-01 12:44:41 +0100 | [diff] [blame] | 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0+ |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
Kamil Lulko | 75d48a6 | 2015-12-01 09:08:19 +0100 | [diff] [blame] | 9 | #include <dm.h> |
rev13@wp.pl | af9873c | 2015-03-01 12:44:41 +0100 | [diff] [blame] | 10 | #include <asm/io.h> |
| 11 | #include <serial.h> |
| 12 | #include <asm/arch/stm32.h> |
Kamil Lulko | 75d48a6 | 2015-12-01 09:08:19 +0100 | [diff] [blame] | 13 | #include <dm/platform_data/serial_stm32.h> |
rev13@wp.pl | af9873c | 2015-03-01 12:44:41 +0100 | [diff] [blame] | 14 | |
Kamil Lulko | 75d48a6 | 2015-12-01 09:08:19 +0100 | [diff] [blame] | 15 | struct stm32_usart { |
rev13@wp.pl | af9873c | 2015-03-01 12:44:41 +0100 | [diff] [blame] | 16 | u32 sr; |
| 17 | u32 dr; |
| 18 | u32 brr; |
| 19 | u32 cr1; |
| 20 | u32 cr2; |
| 21 | u32 cr3; |
| 22 | u32 gtpr; |
| 23 | }; |
| 24 | |
Kamil Lulko | 75d48a6 | 2015-12-01 09:08:19 +0100 | [diff] [blame] | 25 | #define USART_CR1_RE (1 << 2) |
| 26 | #define USART_CR1_TE (1 << 3) |
| 27 | #define USART_CR1_UE (1 << 13) |
rev13@wp.pl | af9873c | 2015-03-01 12:44:41 +0100 | [diff] [blame] | 28 | |
| 29 | #define USART_SR_FLAG_RXNE (1 << 5) |
Kamil Lulko | 75d48a6 | 2015-12-01 09:08:19 +0100 | [diff] [blame] | 30 | #define USART_SR_FLAG_TXE (1 << 7) |
rev13@wp.pl | af9873c | 2015-03-01 12:44:41 +0100 | [diff] [blame] | 31 | |
Kamil Lulko | 75d48a6 | 2015-12-01 09:08:19 +0100 | [diff] [blame] | 32 | #define USART_BRR_F_MASK 0xF |
rev13@wp.pl | af9873c | 2015-03-01 12:44:41 +0100 | [diff] [blame] | 33 | #define USART_BRR_M_SHIFT 4 |
| 34 | #define USART_BRR_M_MASK 0xFFF0 |
| 35 | |
| 36 | DECLARE_GLOBAL_DATA_PTR; |
| 37 | |
Kamil Lulko | 75d48a6 | 2015-12-01 09:08:19 +0100 | [diff] [blame] | 38 | static int stm32_serial_setbrg(struct udevice *dev, int baudrate) |
rev13@wp.pl | af9873c | 2015-03-01 12:44:41 +0100 | [diff] [blame] | 39 | { |
Kamil Lulko | 75d48a6 | 2015-12-01 09:08:19 +0100 | [diff] [blame] | 40 | struct stm32_serial_platdata *plat = dev->platdata; |
| 41 | struct stm32_usart *const usart = plat->base; |
| 42 | u32 clock, int_div, frac_div, tmp; |
rev13@wp.pl | af9873c | 2015-03-01 12:44:41 +0100 | [diff] [blame] | 43 | |
Kamil Lulko | 75d48a6 | 2015-12-01 09:08:19 +0100 | [diff] [blame] | 44 | if (((u32)usart & STM32_BUS_MASK) == STM32_APB1PERIPH_BASE) |
rev13@wp.pl | af9873c | 2015-03-01 12:44:41 +0100 | [diff] [blame] | 45 | clock = clock_get(CLOCK_APB1); |
Kamil Lulko | 75d48a6 | 2015-12-01 09:08:19 +0100 | [diff] [blame] | 46 | else if (((u32)usart & STM32_BUS_MASK) == STM32_APB2PERIPH_BASE) |
rev13@wp.pl | af9873c | 2015-03-01 12:44:41 +0100 | [diff] [blame] | 47 | clock = clock_get(CLOCK_APB2); |
Kamil Lulko | 75d48a6 | 2015-12-01 09:08:19 +0100 | [diff] [blame] | 48 | else |
| 49 | return -EINVAL; |
rev13@wp.pl | af9873c | 2015-03-01 12:44:41 +0100 | [diff] [blame] | 50 | |
Kamil Lulko | 75d48a6 | 2015-12-01 09:08:19 +0100 | [diff] [blame] | 51 | int_div = (25 * clock) / (4 * baudrate); |
rev13@wp.pl | af9873c | 2015-03-01 12:44:41 +0100 | [diff] [blame] | 52 | tmp = ((int_div / 100) << USART_BRR_M_SHIFT) & USART_BRR_M_MASK; |
| 53 | frac_div = int_div - (100 * (tmp >> USART_BRR_M_SHIFT)); |
| 54 | tmp |= (((frac_div * 16) + 50) / 100) & USART_BRR_F_MASK; |
rev13@wp.pl | af9873c | 2015-03-01 12:44:41 +0100 | [diff] [blame] | 55 | writel(tmp, &usart->brr); |
rev13@wp.pl | af9873c | 2015-03-01 12:44:41 +0100 | [diff] [blame] | 56 | |
| 57 | return 0; |
| 58 | } |
| 59 | |
Kamil Lulko | 75d48a6 | 2015-12-01 09:08:19 +0100 | [diff] [blame] | 60 | static int stm32_serial_getc(struct udevice *dev) |
rev13@wp.pl | af9873c | 2015-03-01 12:44:41 +0100 | [diff] [blame] | 61 | { |
Kamil Lulko | 75d48a6 | 2015-12-01 09:08:19 +0100 | [diff] [blame] | 62 | struct stm32_serial_platdata *plat = dev->platdata; |
| 63 | struct stm32_usart *const usart = plat->base; |
| 64 | |
| 65 | if ((readl(&usart->sr) & USART_SR_FLAG_RXNE) == 0) |
| 66 | return -EAGAIN; |
| 67 | |
rev13@wp.pl | af9873c | 2015-03-01 12:44:41 +0100 | [diff] [blame] | 68 | return readl(&usart->dr); |
| 69 | } |
| 70 | |
Kamil Lulko | 75d48a6 | 2015-12-01 09:08:19 +0100 | [diff] [blame] | 71 | static int stm32_serial_putc(struct udevice *dev, const char c) |
rev13@wp.pl | af9873c | 2015-03-01 12:44:41 +0100 | [diff] [blame] | 72 | { |
Kamil Lulko | 75d48a6 | 2015-12-01 09:08:19 +0100 | [diff] [blame] | 73 | struct stm32_serial_platdata *plat = dev->platdata; |
| 74 | struct stm32_usart *const usart = plat->base; |
kunhuahuang | 7c13ad8 | 2015-04-28 02:25:45 +0800 | [diff] [blame] | 75 | |
Kamil Lulko | 75d48a6 | 2015-12-01 09:08:19 +0100 | [diff] [blame] | 76 | if ((readl(&usart->sr) & USART_SR_FLAG_TXE) == 0) |
| 77 | return -EAGAIN; |
Tom Rini | e5d831d | 2015-05-27 13:24:10 -0400 | [diff] [blame] | 78 | |
rev13@wp.pl | af9873c | 2015-03-01 12:44:41 +0100 | [diff] [blame] | 79 | writel(c, &usart->dr); |
Kamil Lulko | 75d48a6 | 2015-12-01 09:08:19 +0100 | [diff] [blame] | 80 | |
| 81 | return 0; |
rev13@wp.pl | af9873c | 2015-03-01 12:44:41 +0100 | [diff] [blame] | 82 | } |
| 83 | |
Kamil Lulko | 75d48a6 | 2015-12-01 09:08:19 +0100 | [diff] [blame] | 84 | static int stm32_serial_pending(struct udevice *dev, bool input) |
rev13@wp.pl | af9873c | 2015-03-01 12:44:41 +0100 | [diff] [blame] | 85 | { |
Kamil Lulko | 75d48a6 | 2015-12-01 09:08:19 +0100 | [diff] [blame] | 86 | struct stm32_serial_platdata *plat = dev->platdata; |
| 87 | struct stm32_usart *const usart = plat->base; |
rev13@wp.pl | af9873c | 2015-03-01 12:44:41 +0100 | [diff] [blame] | 88 | |
Kamil Lulko | 75d48a6 | 2015-12-01 09:08:19 +0100 | [diff] [blame] | 89 | if (input) |
| 90 | return readl(&usart->sr) & USART_SR_FLAG_RXNE ? 1 : 0; |
| 91 | else |
| 92 | return readl(&usart->sr) & USART_SR_FLAG_TXE ? 0 : 1; |
rev13@wp.pl | af9873c | 2015-03-01 12:44:41 +0100 | [diff] [blame] | 93 | } |
| 94 | |
Kamil Lulko | 75d48a6 | 2015-12-01 09:08:19 +0100 | [diff] [blame] | 95 | static int stm32_serial_probe(struct udevice *dev) |
rev13@wp.pl | af9873c | 2015-03-01 12:44:41 +0100 | [diff] [blame] | 96 | { |
Kamil Lulko | 75d48a6 | 2015-12-01 09:08:19 +0100 | [diff] [blame] | 97 | struct stm32_serial_platdata *plat = dev->platdata; |
| 98 | struct stm32_usart *const usart = plat->base; |
Kamil Lulko | 75d48a6 | 2015-12-01 09:08:19 +0100 | [diff] [blame] | 99 | setbits_le32(&usart->cr1, USART_CR1_RE | USART_CR1_TE | USART_CR1_UE); |
| 100 | |
| 101 | return 0; |
rev13@wp.pl | af9873c | 2015-03-01 12:44:41 +0100 | [diff] [blame] | 102 | } |
Kamil Lulko | 75d48a6 | 2015-12-01 09:08:19 +0100 | [diff] [blame] | 103 | |
| 104 | static const struct dm_serial_ops stm32_serial_ops = { |
| 105 | .putc = stm32_serial_putc, |
| 106 | .pending = stm32_serial_pending, |
| 107 | .getc = stm32_serial_getc, |
| 108 | .setbrg = stm32_serial_setbrg, |
| 109 | }; |
| 110 | |
| 111 | U_BOOT_DRIVER(serial_stm32) = { |
| 112 | .name = "serial_stm32", |
| 113 | .id = UCLASS_SERIAL, |
| 114 | .ops = &stm32_serial_ops, |
| 115 | .probe = stm32_serial_probe, |
| 116 | .flags = DM_FLAG_PRE_RELOC, |
| 117 | }; |