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 | f93ae78 | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | */ |
| 21 | #include <common.h> |
Jean-Christophe PLAGNIOL-VILLARD | 843a265 | 2009-03-27 23:26:42 +0100 | [diff] [blame] | 22 | #include <watchdog.h> |
Marek Vasut | cfba457 | 2012-09-13 16:50:30 +0200 | [diff] [blame] | 23 | #include <serial.h> |
| 24 | #include <linux/compiler.h> |
Wolfgang Denk | f93ae78 | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 25 | |
Wolfgang Denk | f93ae78 | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 26 | #include <asm/io.h> |
Haavard Skinnemoen | df548d3 | 2006-11-19 18:06:53 +0100 | [diff] [blame] | 27 | #include <asm/arch/clk.h> |
Reinhard Meyer | 329f0f5 | 2010-11-03 16:32:56 +0100 | [diff] [blame] | 28 | #include <asm/arch/hardware.h> |
Wolfgang Denk | f93ae78 | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 29 | |
| 30 | #include "atmel_usart.h" |
| 31 | |
| 32 | DECLARE_GLOBAL_DATA_PTR; |
| 33 | |
Marek Vasut | cfba457 | 2012-09-13 16:50:30 +0200 | [diff] [blame] | 34 | static void atmel_serial_setbrg(void) |
Wolfgang Denk | f93ae78 | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 35 | { |
Reinhard Meyer | 329f0f5 | 2010-11-03 16:32:56 +0100 | [diff] [blame] | 36 | atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE; |
Wolfgang Denk | f93ae78 | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 37 | unsigned long divisor; |
| 38 | unsigned long usart_hz; |
| 39 | |
| 40 | /* |
| 41 | * Master Clock |
| 42 | * Baud Rate = -------------- |
| 43 | * 16 * CD |
| 44 | */ |
Reinhard Meyer | 329f0f5 | 2010-11-03 16:32:56 +0100 | [diff] [blame] | 45 | usart_hz = get_usart_clk_rate(CONFIG_USART_ID); |
Wolfgang Denk | f93ae78 | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 46 | divisor = (usart_hz / 16 + gd->baudrate / 2) / gd->baudrate; |
Andreas Bießmann | 125637c | 2010-09-03 10:28:05 +0200 | [diff] [blame] | 47 | writel(USART3_BF(CD, divisor), &usart->brgr); |
Wolfgang Denk | f93ae78 | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 48 | } |
| 49 | |
Marek Vasut | cfba457 | 2012-09-13 16:50:30 +0200 | [diff] [blame] | 50 | static int atmel_serial_init(void) |
Wolfgang Denk | f93ae78 | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 51 | { |
Reinhard Meyer | 329f0f5 | 2010-11-03 16:32:56 +0100 | [diff] [blame] | 52 | atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE; |
Andreas Bießmann | 125637c | 2010-09-03 10:28:05 +0200 | [diff] [blame] | 53 | |
Xu, Hong | 1f4faed | 2011-08-02 01:05:04 +0000 | [diff] [blame] | 54 | /* |
| 55 | * Just in case: drain transmitter register |
| 56 | * 1000us is enough for baudrate >= 9600 |
| 57 | */ |
| 58 | if (!(readl(&usart->csr) & USART3_BIT(TXEMPTY))) |
| 59 | __udelay(1000); |
| 60 | |
Andreas Bießmann | 125637c | 2010-09-03 10:28:05 +0200 | [diff] [blame] | 61 | writel(USART3_BIT(RSTRX) | USART3_BIT(RSTTX), &usart->cr); |
Wolfgang Denk | f93ae78 | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 62 | |
| 63 | serial_setbrg(); |
| 64 | |
Andreas Bießmann | 125637c | 2010-09-03 10:28:05 +0200 | [diff] [blame] | 65 | writel((USART3_BF(USART_MODE, USART3_USART_MODE_NORMAL) |
Haavard Skinnemoen | df548d3 | 2006-11-19 18:06:53 +0100 | [diff] [blame] | 66 | | USART3_BF(USCLKS, USART3_USCLKS_MCK) |
| 67 | | USART3_BF(CHRL, USART3_CHRL_8) |
| 68 | | USART3_BF(PAR, USART3_PAR_NONE) |
Andreas Bießmann | 125637c | 2010-09-03 10:28:05 +0200 | [diff] [blame] | 69 | | USART3_BF(NBSTOP, USART3_NBSTOP_1)), |
| 70 | &usart->mr); |
Xu, Hong | 1f4faed | 2011-08-02 01:05:04 +0000 | [diff] [blame] | 71 | writel(USART3_BIT(RXEN) | USART3_BIT(TXEN), &usart->cr); |
| 72 | /* 100us is enough for the new settings to be settled */ |
| 73 | __udelay(100); |
Wolfgang Denk | f93ae78 | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | |
Marek Vasut | cfba457 | 2012-09-13 16:50:30 +0200 | [diff] [blame] | 78 | static void atmel_serial_putc(char c) |
Wolfgang Denk | f93ae78 | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 79 | { |
Reinhard Meyer | 329f0f5 | 2010-11-03 16:32:56 +0100 | [diff] [blame] | 80 | atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE; |
Andreas Bießmann | 125637c | 2010-09-03 10:28:05 +0200 | [diff] [blame] | 81 | |
Wolfgang Denk | f93ae78 | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 82 | if (c == '\n') |
| 83 | serial_putc('\r'); |
| 84 | |
Andreas Bießmann | 125637c | 2010-09-03 10:28:05 +0200 | [diff] [blame] | 85 | while (!(readl(&usart->csr) & USART3_BIT(TXRDY))); |
| 86 | writel(c, &usart->thr); |
Wolfgang Denk | f93ae78 | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 87 | } |
| 88 | |
Marek Vasut | cfba457 | 2012-09-13 16:50:30 +0200 | [diff] [blame] | 89 | static int atmel_serial_getc(void) |
Wolfgang Denk | f93ae78 | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 90 | { |
Reinhard Meyer | 329f0f5 | 2010-11-03 16:32:56 +0100 | [diff] [blame] | 91 | atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE; |
Andreas Bießmann | 125637c | 2010-09-03 10:28:05 +0200 | [diff] [blame] | 92 | |
| 93 | while (!(readl(&usart->csr) & USART3_BIT(RXRDY))) |
Jean-Christophe PLAGNIOL-VILLARD | 843a265 | 2009-03-27 23:26:42 +0100 | [diff] [blame] | 94 | WATCHDOG_RESET(); |
Andreas Bießmann | 125637c | 2010-09-03 10:28:05 +0200 | [diff] [blame] | 95 | return readl(&usart->rhr); |
Wolfgang Denk | f93ae78 | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 96 | } |
| 97 | |
Marek Vasut | cfba457 | 2012-09-13 16:50:30 +0200 | [diff] [blame] | 98 | static int atmel_serial_tstc(void) |
Wolfgang Denk | f93ae78 | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 99 | { |
Reinhard Meyer | 329f0f5 | 2010-11-03 16:32:56 +0100 | [diff] [blame] | 100 | atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE; |
Andreas Bießmann | 125637c | 2010-09-03 10:28:05 +0200 | [diff] [blame] | 101 | return (readl(&usart->csr) & USART3_BIT(RXRDY)) != 0; |
Wolfgang Denk | f93ae78 | 2006-10-24 14:31:24 +0200 | [diff] [blame] | 102 | } |
Marek Vasut | cfba457 | 2012-09-13 16:50:30 +0200 | [diff] [blame] | 103 | |
Marek Vasut | cfba457 | 2012-09-13 16:50:30 +0200 | [diff] [blame] | 104 | static struct serial_device atmel_serial_drv = { |
| 105 | .name = "atmel_serial", |
| 106 | .start = atmel_serial_init, |
| 107 | .stop = NULL, |
| 108 | .setbrg = atmel_serial_setbrg, |
| 109 | .putc = atmel_serial_putc, |
Marek Vasut | ec3fd68 | 2012-10-06 14:07:02 +0000 | [diff] [blame] | 110 | .puts = default_serial_puts, |
Marek Vasut | cfba457 | 2012-09-13 16:50:30 +0200 | [diff] [blame] | 111 | .getc = atmel_serial_getc, |
| 112 | .tstc = atmel_serial_tstc, |
| 113 | }; |
| 114 | |
| 115 | void atmel_serial_initialize(void) |
| 116 | { |
| 117 | serial_register(&atmel_serial_drv); |
| 118 | } |
| 119 | |
| 120 | __weak struct serial_device *default_serial_console(void) |
| 121 | { |
| 122 | return &atmel_serial_drv; |
| 123 | } |