Wolfgang Denk | 5e1d9ec | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004-2006 Atmel Corporation |
| 3 | * |
Andreas Bießmann | fb37868 | 2010-09-03 10:28:05 +0200 | [diff] [blame] | 4 | * Modified to support C structur SoC access by |
| 5 | * Andreas Bießmann <biessmann@corscience.de> |
| 6 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 7 | * SPDX-License-Identifier: GPL-2.0+ |
Wolfgang Denk | 5e1d9ec | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 8 | */ |
| 9 | #include <common.h> |
Simon Glass | 7cbab24 | 2014-10-29 13:09:00 -0600 | [diff] [blame] | 10 | #include <dm.h> |
| 11 | #include <errno.h> |
Jean-Christophe PLAGNIOL-VILLARD | d5ee38e | 2009-03-27 23:26:42 +0100 | [diff] [blame] | 12 | #include <watchdog.h> |
Marek Vasut | ff093ed | 2012-09-13 16:50:30 +0200 | [diff] [blame] | 13 | #include <serial.h> |
Wenyou Yang | 6b611e6 | 2016-10-17 09:49:55 +0800 | [diff] [blame] | 14 | #include <debug_uart.h> |
Marek Vasut | ff093ed | 2012-09-13 16:50:30 +0200 | [diff] [blame] | 15 | #include <linux/compiler.h> |
Wolfgang Denk | 5e1d9ec | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 16 | |
Wolfgang Denk | 5e1d9ec | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 17 | #include <asm/io.h> |
Simon Glass | 7cbab24 | 2014-10-29 13:09:00 -0600 | [diff] [blame] | 18 | #ifdef CONFIG_DM_SERIAL |
| 19 | #include <asm/arch/atmel_serial.h> |
| 20 | #endif |
Haavard Skinnemoen | 0a2743f | 2006-11-19 18:06:53 +0100 | [diff] [blame] | 21 | #include <asm/arch/clk.h> |
Reinhard Meyer | 7f619cb | 2010-11-03 16:32:56 +0100 | [diff] [blame] | 22 | #include <asm/arch/hardware.h> |
Wolfgang Denk | 5e1d9ec | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 23 | |
| 24 | #include "atmel_usart.h" |
| 25 | |
| 26 | DECLARE_GLOBAL_DATA_PTR; |
| 27 | |
Simon Glass | 1e5c2a8 | 2014-10-29 13:08:59 -0600 | [diff] [blame] | 28 | static void atmel_serial_setbrg_internal(atmel_usart3_t *usart, int id, |
| 29 | int baudrate) |
Wolfgang Denk | 5e1d9ec | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 30 | { |
| 31 | unsigned long divisor; |
| 32 | unsigned long usart_hz; |
| 33 | |
| 34 | /* |
| 35 | * Master Clock |
| 36 | * Baud Rate = -------------- |
| 37 | * 16 * CD |
| 38 | */ |
Simon Glass | 1e5c2a8 | 2014-10-29 13:08:59 -0600 | [diff] [blame] | 39 | usart_hz = get_usart_clk_rate(id); |
| 40 | divisor = (usart_hz / 16 + baudrate / 2) / baudrate; |
Andreas Bießmann | fb37868 | 2010-09-03 10:28:05 +0200 | [diff] [blame] | 41 | writel(USART3_BF(CD, divisor), &usart->brgr); |
Wolfgang Denk | 5e1d9ec | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 42 | } |
| 43 | |
Simon Glass | 1e5c2a8 | 2014-10-29 13:08:59 -0600 | [diff] [blame] | 44 | static void atmel_serial_init_internal(atmel_usart3_t *usart) |
Wolfgang Denk | 5e1d9ec | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 45 | { |
Xu, Hong | 9db5799 | 2011-08-02 01:05:04 +0000 | [diff] [blame] | 46 | /* |
| 47 | * Just in case: drain transmitter register |
| 48 | * 1000us is enough for baudrate >= 9600 |
| 49 | */ |
| 50 | if (!(readl(&usart->csr) & USART3_BIT(TXEMPTY))) |
| 51 | __udelay(1000); |
| 52 | |
Andreas Bießmann | fb37868 | 2010-09-03 10:28:05 +0200 | [diff] [blame] | 53 | writel(USART3_BIT(RSTRX) | USART3_BIT(RSTTX), &usart->cr); |
Simon Glass | 1e5c2a8 | 2014-10-29 13:08:59 -0600 | [diff] [blame] | 54 | } |
Wolfgang Denk | 5e1d9ec | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 55 | |
Simon Glass | 1e5c2a8 | 2014-10-29 13:08:59 -0600 | [diff] [blame] | 56 | static void atmel_serial_activate(atmel_usart3_t *usart) |
| 57 | { |
Andreas Bießmann | fb37868 | 2010-09-03 10:28:05 +0200 | [diff] [blame] | 58 | writel((USART3_BF(USART_MODE, USART3_USART_MODE_NORMAL) |
Haavard Skinnemoen | 0a2743f | 2006-11-19 18:06:53 +0100 | [diff] [blame] | 59 | | USART3_BF(USCLKS, USART3_USCLKS_MCK) |
| 60 | | USART3_BF(CHRL, USART3_CHRL_8) |
| 61 | | USART3_BF(PAR, USART3_PAR_NONE) |
Andreas Bießmann | fb37868 | 2010-09-03 10:28:05 +0200 | [diff] [blame] | 62 | | USART3_BF(NBSTOP, USART3_NBSTOP_1)), |
| 63 | &usart->mr); |
Xu, Hong | 9db5799 | 2011-08-02 01:05:04 +0000 | [diff] [blame] | 64 | writel(USART3_BIT(RXEN) | USART3_BIT(TXEN), &usart->cr); |
| 65 | /* 100us is enough for the new settings to be settled */ |
| 66 | __udelay(100); |
Simon Glass | 1e5c2a8 | 2014-10-29 13:08:59 -0600 | [diff] [blame] | 67 | } |
| 68 | |
Simon Glass | 7cbab24 | 2014-10-29 13:09:00 -0600 | [diff] [blame] | 69 | #ifndef CONFIG_DM_SERIAL |
Simon Glass | 1e5c2a8 | 2014-10-29 13:08:59 -0600 | [diff] [blame] | 70 | static void atmel_serial_setbrg(void) |
| 71 | { |
| 72 | atmel_serial_setbrg_internal((atmel_usart3_t *)CONFIG_USART_BASE, |
| 73 | CONFIG_USART_ID, gd->baudrate); |
| 74 | } |
| 75 | |
| 76 | static int atmel_serial_init(void) |
| 77 | { |
| 78 | atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE; |
| 79 | |
| 80 | atmel_serial_init_internal(usart); |
| 81 | serial_setbrg(); |
| 82 | atmel_serial_activate(usart); |
Wolfgang Denk | 5e1d9ec | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 83 | |
| 84 | return 0; |
| 85 | } |
| 86 | |
Marek Vasut | ff093ed | 2012-09-13 16:50:30 +0200 | [diff] [blame] | 87 | static void atmel_serial_putc(char c) |
Wolfgang Denk | 5e1d9ec | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 88 | { |
Reinhard Meyer | 7f619cb | 2010-11-03 16:32:56 +0100 | [diff] [blame] | 89 | atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE; |
Andreas Bießmann | fb37868 | 2010-09-03 10:28:05 +0200 | [diff] [blame] | 90 | |
Wolfgang Denk | 5e1d9ec | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 91 | if (c == '\n') |
| 92 | serial_putc('\r'); |
| 93 | |
Andreas Bießmann | fb37868 | 2010-09-03 10:28:05 +0200 | [diff] [blame] | 94 | while (!(readl(&usart->csr) & USART3_BIT(TXRDY))); |
| 95 | writel(c, &usart->thr); |
Wolfgang Denk | 5e1d9ec | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 96 | } |
| 97 | |
Marek Vasut | ff093ed | 2012-09-13 16:50:30 +0200 | [diff] [blame] | 98 | static int atmel_serial_getc(void) |
Wolfgang Denk | 5e1d9ec | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 99 | { |
Reinhard Meyer | 7f619cb | 2010-11-03 16:32:56 +0100 | [diff] [blame] | 100 | atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE; |
Andreas Bießmann | fb37868 | 2010-09-03 10:28:05 +0200 | [diff] [blame] | 101 | |
| 102 | while (!(readl(&usart->csr) & USART3_BIT(RXRDY))) |
Jean-Christophe PLAGNIOL-VILLARD | d5ee38e | 2009-03-27 23:26:42 +0100 | [diff] [blame] | 103 | WATCHDOG_RESET(); |
Andreas Bießmann | fb37868 | 2010-09-03 10:28:05 +0200 | [diff] [blame] | 104 | return readl(&usart->rhr); |
Wolfgang Denk | 5e1d9ec | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 105 | } |
| 106 | |
Marek Vasut | ff093ed | 2012-09-13 16:50:30 +0200 | [diff] [blame] | 107 | static int atmel_serial_tstc(void) |
Wolfgang Denk | 5e1d9ec | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 108 | { |
Reinhard Meyer | 7f619cb | 2010-11-03 16:32:56 +0100 | [diff] [blame] | 109 | atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE; |
Andreas Bießmann | fb37868 | 2010-09-03 10:28:05 +0200 | [diff] [blame] | 110 | return (readl(&usart->csr) & USART3_BIT(RXRDY)) != 0; |
Wolfgang Denk | 5e1d9ec | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 111 | } |
Marek Vasut | ff093ed | 2012-09-13 16:50:30 +0200 | [diff] [blame] | 112 | |
Marek Vasut | ff093ed | 2012-09-13 16:50:30 +0200 | [diff] [blame] | 113 | static struct serial_device atmel_serial_drv = { |
| 114 | .name = "atmel_serial", |
| 115 | .start = atmel_serial_init, |
| 116 | .stop = NULL, |
| 117 | .setbrg = atmel_serial_setbrg, |
| 118 | .putc = atmel_serial_putc, |
Marek Vasut | d9c6449 | 2012-10-06 14:07:02 +0000 | [diff] [blame] | 119 | .puts = default_serial_puts, |
Marek Vasut | ff093ed | 2012-09-13 16:50:30 +0200 | [diff] [blame] | 120 | .getc = atmel_serial_getc, |
| 121 | .tstc = atmel_serial_tstc, |
| 122 | }; |
| 123 | |
| 124 | void atmel_serial_initialize(void) |
| 125 | { |
| 126 | serial_register(&atmel_serial_drv); |
| 127 | } |
| 128 | |
| 129 | __weak struct serial_device *default_serial_console(void) |
| 130 | { |
| 131 | return &atmel_serial_drv; |
| 132 | } |
Simon Glass | 7cbab24 | 2014-10-29 13:09:00 -0600 | [diff] [blame] | 133 | #endif |
| 134 | |
| 135 | #ifdef CONFIG_DM_SERIAL |
| 136 | |
| 137 | struct atmel_serial_priv { |
| 138 | atmel_usart3_t *usart; |
| 139 | }; |
| 140 | |
| 141 | int atmel_serial_setbrg(struct udevice *dev, int baudrate) |
| 142 | { |
| 143 | struct atmel_serial_priv *priv = dev_get_priv(dev); |
| 144 | |
| 145 | atmel_serial_setbrg_internal(priv->usart, 0 /* ignored */, baudrate); |
| 146 | atmel_serial_activate(priv->usart); |
| 147 | |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | static int atmel_serial_getc(struct udevice *dev) |
| 152 | { |
| 153 | struct atmel_serial_priv *priv = dev_get_priv(dev); |
| 154 | |
| 155 | if (!(readl(&priv->usart->csr) & USART3_BIT(RXRDY))) |
| 156 | return -EAGAIN; |
| 157 | |
| 158 | return readl(&priv->usart->rhr); |
| 159 | } |
| 160 | |
| 161 | static int atmel_serial_putc(struct udevice *dev, const char ch) |
| 162 | { |
| 163 | struct atmel_serial_priv *priv = dev_get_priv(dev); |
| 164 | |
| 165 | if (!(readl(&priv->usart->csr) & USART3_BIT(TXRDY))) |
| 166 | return -EAGAIN; |
| 167 | |
| 168 | writel(ch, &priv->usart->thr); |
| 169 | |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | static int atmel_serial_pending(struct udevice *dev, bool input) |
| 174 | { |
| 175 | struct atmel_serial_priv *priv = dev_get_priv(dev); |
| 176 | uint32_t csr = readl(&priv->usart->csr); |
| 177 | |
| 178 | if (input) |
| 179 | return csr & USART3_BIT(RXRDY) ? 1 : 0; |
| 180 | else |
| 181 | return csr & USART3_BIT(TXEMPTY) ? 0 : 1; |
| 182 | } |
| 183 | |
| 184 | static const struct dm_serial_ops atmel_serial_ops = { |
| 185 | .putc = atmel_serial_putc, |
| 186 | .pending = atmel_serial_pending, |
| 187 | .getc = atmel_serial_getc, |
| 188 | .setbrg = atmel_serial_setbrg, |
| 189 | }; |
| 190 | |
| 191 | static int atmel_serial_probe(struct udevice *dev) |
| 192 | { |
| 193 | struct atmel_serial_platdata *plat = dev->platdata; |
| 194 | struct atmel_serial_priv *priv = dev_get_priv(dev); |
Wenyou Yang | b0e59ee | 2016-06-01 08:36:56 +0800 | [diff] [blame] | 195 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
| 196 | fdt_addr_t addr_base; |
Simon Glass | 7cbab24 | 2014-10-29 13:09:00 -0600 | [diff] [blame] | 197 | |
Wenyou Yang | b0e59ee | 2016-06-01 08:36:56 +0800 | [diff] [blame] | 198 | addr_base = dev_get_addr(dev); |
| 199 | if (addr_base == FDT_ADDR_T_NONE) |
| 200 | return -ENODEV; |
| 201 | |
| 202 | plat->base_addr = (uint32_t)addr_base; |
| 203 | #endif |
Simon Glass | 7cbab24 | 2014-10-29 13:09:00 -0600 | [diff] [blame] | 204 | priv->usart = (atmel_usart3_t *)plat->base_addr; |
| 205 | atmel_serial_init_internal(priv->usart); |
| 206 | |
| 207 | return 0; |
| 208 | } |
| 209 | |
Wenyou Yang | b0e59ee | 2016-06-01 08:36:56 +0800 | [diff] [blame] | 210 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
| 211 | static const struct udevice_id atmel_serial_ids[] = { |
| 212 | { .compatible = "atmel,at91sam9260-usart" }, |
| 213 | { } |
| 214 | }; |
| 215 | #endif |
| 216 | |
Simon Glass | 7cbab24 | 2014-10-29 13:09:00 -0600 | [diff] [blame] | 217 | U_BOOT_DRIVER(serial_atmel) = { |
| 218 | .name = "serial_atmel", |
| 219 | .id = UCLASS_SERIAL, |
Wenyou Yang | b0e59ee | 2016-06-01 08:36:56 +0800 | [diff] [blame] | 220 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
| 221 | .of_match = atmel_serial_ids, |
| 222 | .platdata_auto_alloc_size = sizeof(struct atmel_serial_platdata), |
| 223 | #endif |
Simon Glass | 7cbab24 | 2014-10-29 13:09:00 -0600 | [diff] [blame] | 224 | .probe = atmel_serial_probe, |
| 225 | .ops = &atmel_serial_ops, |
| 226 | .flags = DM_FLAG_PRE_RELOC, |
| 227 | .priv_auto_alloc_size = sizeof(struct atmel_serial_priv), |
| 228 | }; |
| 229 | #endif |
Wenyou Yang | 6b611e6 | 2016-10-17 09:49:55 +0800 | [diff] [blame] | 230 | |
| 231 | #ifdef CONFIG_DEBUG_UART_ATMEL |
| 232 | static inline void _debug_uart_init(void) |
| 233 | { |
| 234 | atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_DEBUG_UART_BASE; |
| 235 | |
| 236 | atmel_serial_setbrg_internal(usart, 0, CONFIG_BAUDRATE); |
| 237 | } |
| 238 | |
| 239 | static inline void _debug_uart_putc(int ch) |
| 240 | { |
| 241 | atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_DEBUG_UART_BASE; |
| 242 | |
| 243 | while (!(readl(&usart->csr) & USART3_BIT(TXRDY))) |
| 244 | ; |
| 245 | |
| 246 | writel(ch, &usart->thr); |
| 247 | } |
| 248 | |
| 249 | DEBUG_UART_FUNCS |
| 250 | #endif |