Wolfgang Denk | f93ae78 | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004-2006 Atmel Corporation |
| 3 | * |
Andreas Bießmann | 125637c | 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 | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 7 | * SPDX-License-Identifier: GPL-2.0+ |
Wolfgang Denk | f93ae78 | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 8 | */ |
| 9 | #include <common.h> |
Jean-Christophe PLAGNIOL-VILLARD | 843a265 | 2009-03-27 23:26:42 +0100 | [diff] [blame] | 10 | #include <watchdog.h> |
Marek Vasut | cfba457 | 2012-09-13 16:50:30 +0200 | [diff] [blame] | 11 | #include <serial.h> |
| 12 | #include <linux/compiler.h> |
Wolfgang Denk | f93ae78 | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 13 | |
Wolfgang Denk | f93ae78 | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 14 | #include <asm/io.h> |
Haavard Skinnemoen | df548d3 | 2006-11-19 18:06:53 +0100 | [diff] [blame] | 15 | #include <asm/arch/clk.h> |
Reinhard Meyer | 329f0f5 | 2010-11-03 16:32:56 +0100 | [diff] [blame] | 16 | #include <asm/arch/hardware.h> |
Wolfgang Denk | f93ae78 | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 17 | |
| 18 | #include "atmel_usart.h" |
| 19 | |
| 20 | DECLARE_GLOBAL_DATA_PTR; |
| 21 | |
Simon Glass | 62137fc | 2014-10-29 13:08:59 -0600 | [diff] [blame^] | 22 | static void atmel_serial_setbrg_internal(atmel_usart3_t *usart, int id, |
| 23 | int baudrate) |
Wolfgang Denk | f93ae78 | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 24 | { |
| 25 | unsigned long divisor; |
| 26 | unsigned long usart_hz; |
| 27 | |
| 28 | /* |
| 29 | * Master Clock |
| 30 | * Baud Rate = -------------- |
| 31 | * 16 * CD |
| 32 | */ |
Simon Glass | 62137fc | 2014-10-29 13:08:59 -0600 | [diff] [blame^] | 33 | usart_hz = get_usart_clk_rate(id); |
| 34 | divisor = (usart_hz / 16 + baudrate / 2) / baudrate; |
Andreas Bießmann | 125637c | 2010-09-03 10:28:05 +0200 | [diff] [blame] | 35 | writel(USART3_BF(CD, divisor), &usart->brgr); |
Wolfgang Denk | f93ae78 | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 36 | } |
| 37 | |
Simon Glass | 62137fc | 2014-10-29 13:08:59 -0600 | [diff] [blame^] | 38 | static void atmel_serial_init_internal(atmel_usart3_t *usart) |
Wolfgang Denk | f93ae78 | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 39 | { |
Xu, Hong | 1f4faed | 2011-08-02 01:05:04 +0000 | [diff] [blame] | 40 | /* |
| 41 | * Just in case: drain transmitter register |
| 42 | * 1000us is enough for baudrate >= 9600 |
| 43 | */ |
| 44 | if (!(readl(&usart->csr) & USART3_BIT(TXEMPTY))) |
| 45 | __udelay(1000); |
| 46 | |
Andreas Bießmann | 125637c | 2010-09-03 10:28:05 +0200 | [diff] [blame] | 47 | writel(USART3_BIT(RSTRX) | USART3_BIT(RSTTX), &usart->cr); |
Simon Glass | 62137fc | 2014-10-29 13:08:59 -0600 | [diff] [blame^] | 48 | } |
Wolfgang Denk | f93ae78 | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 49 | |
Simon Glass | 62137fc | 2014-10-29 13:08:59 -0600 | [diff] [blame^] | 50 | static void atmel_serial_activate(atmel_usart3_t *usart) |
| 51 | { |
Andreas Bießmann | 125637c | 2010-09-03 10:28:05 +0200 | [diff] [blame] | 52 | writel((USART3_BF(USART_MODE, USART3_USART_MODE_NORMAL) |
Haavard Skinnemoen | df548d3 | 2006-11-19 18:06:53 +0100 | [diff] [blame] | 53 | | USART3_BF(USCLKS, USART3_USCLKS_MCK) |
| 54 | | USART3_BF(CHRL, USART3_CHRL_8) |
| 55 | | USART3_BF(PAR, USART3_PAR_NONE) |
Andreas Bießmann | 125637c | 2010-09-03 10:28:05 +0200 | [diff] [blame] | 56 | | USART3_BF(NBSTOP, USART3_NBSTOP_1)), |
| 57 | &usart->mr); |
Xu, Hong | 1f4faed | 2011-08-02 01:05:04 +0000 | [diff] [blame] | 58 | writel(USART3_BIT(RXEN) | USART3_BIT(TXEN), &usart->cr); |
| 59 | /* 100us is enough for the new settings to be settled */ |
| 60 | __udelay(100); |
Simon Glass | 62137fc | 2014-10-29 13:08:59 -0600 | [diff] [blame^] | 61 | } |
| 62 | |
| 63 | static void atmel_serial_setbrg(void) |
| 64 | { |
| 65 | atmel_serial_setbrg_internal((atmel_usart3_t *)CONFIG_USART_BASE, |
| 66 | CONFIG_USART_ID, gd->baudrate); |
| 67 | } |
| 68 | |
| 69 | static int atmel_serial_init(void) |
| 70 | { |
| 71 | atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE; |
| 72 | |
| 73 | atmel_serial_init_internal(usart); |
| 74 | serial_setbrg(); |
| 75 | atmel_serial_activate(usart); |
Wolfgang Denk | f93ae78 | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 76 | |
| 77 | return 0; |
| 78 | } |
| 79 | |
Marek Vasut | cfba457 | 2012-09-13 16:50:30 +0200 | [diff] [blame] | 80 | static void atmel_serial_putc(char c) |
Wolfgang Denk | f93ae78 | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 81 | { |
Reinhard Meyer | 329f0f5 | 2010-11-03 16:32:56 +0100 | [diff] [blame] | 82 | atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE; |
Andreas Bießmann | 125637c | 2010-09-03 10:28:05 +0200 | [diff] [blame] | 83 | |
Wolfgang Denk | f93ae78 | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 84 | if (c == '\n') |
| 85 | serial_putc('\r'); |
| 86 | |
Andreas Bießmann | 125637c | 2010-09-03 10:28:05 +0200 | [diff] [blame] | 87 | while (!(readl(&usart->csr) & USART3_BIT(TXRDY))); |
| 88 | writel(c, &usart->thr); |
Wolfgang Denk | f93ae78 | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 89 | } |
| 90 | |
Marek Vasut | cfba457 | 2012-09-13 16:50:30 +0200 | [diff] [blame] | 91 | static int atmel_serial_getc(void) |
Wolfgang Denk | f93ae78 | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 92 | { |
Reinhard Meyer | 329f0f5 | 2010-11-03 16:32:56 +0100 | [diff] [blame] | 93 | atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE; |
Andreas Bießmann | 125637c | 2010-09-03 10:28:05 +0200 | [diff] [blame] | 94 | |
| 95 | while (!(readl(&usart->csr) & USART3_BIT(RXRDY))) |
Jean-Christophe PLAGNIOL-VILLARD | 843a265 | 2009-03-27 23:26:42 +0100 | [diff] [blame] | 96 | WATCHDOG_RESET(); |
Andreas Bießmann | 125637c | 2010-09-03 10:28:05 +0200 | [diff] [blame] | 97 | return readl(&usart->rhr); |
Wolfgang Denk | f93ae78 | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 98 | } |
| 99 | |
Marek Vasut | cfba457 | 2012-09-13 16:50:30 +0200 | [diff] [blame] | 100 | static int atmel_serial_tstc(void) |
Wolfgang Denk | f93ae78 | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 101 | { |
Reinhard Meyer | 329f0f5 | 2010-11-03 16:32:56 +0100 | [diff] [blame] | 102 | atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE; |
Andreas Bießmann | 125637c | 2010-09-03 10:28:05 +0200 | [diff] [blame] | 103 | return (readl(&usart->csr) & USART3_BIT(RXRDY)) != 0; |
Wolfgang Denk | f93ae78 | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 104 | } |
Marek Vasut | cfba457 | 2012-09-13 16:50:30 +0200 | [diff] [blame] | 105 | |
Marek Vasut | cfba457 | 2012-09-13 16:50:30 +0200 | [diff] [blame] | 106 | static struct serial_device atmel_serial_drv = { |
| 107 | .name = "atmel_serial", |
| 108 | .start = atmel_serial_init, |
| 109 | .stop = NULL, |
| 110 | .setbrg = atmel_serial_setbrg, |
| 111 | .putc = atmel_serial_putc, |
Marek Vasut | ec3fd68 | 2012-10-06 14:07:02 +0000 | [diff] [blame] | 112 | .puts = default_serial_puts, |
Marek Vasut | cfba457 | 2012-09-13 16:50:30 +0200 | [diff] [blame] | 113 | .getc = atmel_serial_getc, |
| 114 | .tstc = atmel_serial_tstc, |
| 115 | }; |
| 116 | |
| 117 | void atmel_serial_initialize(void) |
| 118 | { |
| 119 | serial_register(&atmel_serial_drv); |
| 120 | } |
| 121 | |
| 122 | __weak struct serial_device *default_serial_console(void) |
| 123 | { |
| 124 | return &atmel_serial_drv; |
| 125 | } |