blob: 8f0e3489a0e59287c60a2e3b34cc1fdc69eb91d7 [file] [log] [blame]
Wolfgang Denk5e1d9ec2006-10-24 14:31:24 +02001/*
2 * Copyright (C) 2004-2006 Atmel Corporation
3 *
Andreas Bießmannfb378682010-09-03 10:28:05 +02004 * Modified to support C structur SoC access by
5 * Andreas Bießmann <biessmann@corscience.de>
6 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Wolfgang Denk5e1d9ec2006-10-24 14:31:24 +02008 */
9#include <common.h>
Jean-Christophe PLAGNIOL-VILLARDd5ee38e2009-03-27 23:26:42 +010010#include <watchdog.h>
Marek Vasutff093ed2012-09-13 16:50:30 +020011#include <serial.h>
12#include <linux/compiler.h>
Wolfgang Denk5e1d9ec2006-10-24 14:31:24 +020013
Wolfgang Denk5e1d9ec2006-10-24 14:31:24 +020014#include <asm/io.h>
Haavard Skinnemoen0a2743f2006-11-19 18:06:53 +010015#include <asm/arch/clk.h>
Reinhard Meyer7f619cb2010-11-03 16:32:56 +010016#include <asm/arch/hardware.h>
Wolfgang Denk5e1d9ec2006-10-24 14:31:24 +020017
18#include "atmel_usart.h"
19
20DECLARE_GLOBAL_DATA_PTR;
21
Marek Vasutff093ed2012-09-13 16:50:30 +020022static void atmel_serial_setbrg(void)
Wolfgang Denk5e1d9ec2006-10-24 14:31:24 +020023{
Reinhard Meyer7f619cb2010-11-03 16:32:56 +010024 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
Wolfgang Denk5e1d9ec2006-10-24 14:31:24 +020025 unsigned long divisor;
26 unsigned long usart_hz;
27
28 /*
29 * Master Clock
30 * Baud Rate = --------------
31 * 16 * CD
32 */
Reinhard Meyer7f619cb2010-11-03 16:32:56 +010033 usart_hz = get_usart_clk_rate(CONFIG_USART_ID);
Wolfgang Denk5e1d9ec2006-10-24 14:31:24 +020034 divisor = (usart_hz / 16 + gd->baudrate / 2) / gd->baudrate;
Andreas Bießmannfb378682010-09-03 10:28:05 +020035 writel(USART3_BF(CD, divisor), &usart->brgr);
Wolfgang Denk5e1d9ec2006-10-24 14:31:24 +020036}
37
Marek Vasutff093ed2012-09-13 16:50:30 +020038static int atmel_serial_init(void)
Wolfgang Denk5e1d9ec2006-10-24 14:31:24 +020039{
Reinhard Meyer7f619cb2010-11-03 16:32:56 +010040 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
Andreas Bießmannfb378682010-09-03 10:28:05 +020041
Xu, Hong9db57992011-08-02 01:05:04 +000042 /*
43 * Just in case: drain transmitter register
44 * 1000us is enough for baudrate >= 9600
45 */
46 if (!(readl(&usart->csr) & USART3_BIT(TXEMPTY)))
47 __udelay(1000);
48
Andreas Bießmannfb378682010-09-03 10:28:05 +020049 writel(USART3_BIT(RSTRX) | USART3_BIT(RSTTX), &usart->cr);
Wolfgang Denk5e1d9ec2006-10-24 14:31:24 +020050
51 serial_setbrg();
52
Andreas Bießmannfb378682010-09-03 10:28:05 +020053 writel((USART3_BF(USART_MODE, USART3_USART_MODE_NORMAL)
Haavard Skinnemoen0a2743f2006-11-19 18:06:53 +010054 | USART3_BF(USCLKS, USART3_USCLKS_MCK)
55 | USART3_BF(CHRL, USART3_CHRL_8)
56 | USART3_BF(PAR, USART3_PAR_NONE)
Andreas Bießmannfb378682010-09-03 10:28:05 +020057 | USART3_BF(NBSTOP, USART3_NBSTOP_1)),
58 &usart->mr);
Xu, Hong9db57992011-08-02 01:05:04 +000059 writel(USART3_BIT(RXEN) | USART3_BIT(TXEN), &usart->cr);
60 /* 100us is enough for the new settings to be settled */
61 __udelay(100);
Wolfgang Denk5e1d9ec2006-10-24 14:31:24 +020062
63 return 0;
64}
65
Marek Vasutff093ed2012-09-13 16:50:30 +020066static void atmel_serial_putc(char c)
Wolfgang Denk5e1d9ec2006-10-24 14:31:24 +020067{
Reinhard Meyer7f619cb2010-11-03 16:32:56 +010068 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
Andreas Bießmannfb378682010-09-03 10:28:05 +020069
Wolfgang Denk5e1d9ec2006-10-24 14:31:24 +020070 if (c == '\n')
71 serial_putc('\r');
72
Andreas Bießmannfb378682010-09-03 10:28:05 +020073 while (!(readl(&usart->csr) & USART3_BIT(TXRDY)));
74 writel(c, &usart->thr);
Wolfgang Denk5e1d9ec2006-10-24 14:31:24 +020075}
76
Marek Vasutff093ed2012-09-13 16:50:30 +020077static int atmel_serial_getc(void)
Wolfgang Denk5e1d9ec2006-10-24 14:31:24 +020078{
Reinhard Meyer7f619cb2010-11-03 16:32:56 +010079 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
Andreas Bießmannfb378682010-09-03 10:28:05 +020080
81 while (!(readl(&usart->csr) & USART3_BIT(RXRDY)))
Jean-Christophe PLAGNIOL-VILLARDd5ee38e2009-03-27 23:26:42 +010082 WATCHDOG_RESET();
Andreas Bießmannfb378682010-09-03 10:28:05 +020083 return readl(&usart->rhr);
Wolfgang Denk5e1d9ec2006-10-24 14:31:24 +020084}
85
Marek Vasutff093ed2012-09-13 16:50:30 +020086static int atmel_serial_tstc(void)
Wolfgang Denk5e1d9ec2006-10-24 14:31:24 +020087{
Reinhard Meyer7f619cb2010-11-03 16:32:56 +010088 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
Andreas Bießmannfb378682010-09-03 10:28:05 +020089 return (readl(&usart->csr) & USART3_BIT(RXRDY)) != 0;
Wolfgang Denk5e1d9ec2006-10-24 14:31:24 +020090}
Marek Vasutff093ed2012-09-13 16:50:30 +020091
Marek Vasutff093ed2012-09-13 16:50:30 +020092static struct serial_device atmel_serial_drv = {
93 .name = "atmel_serial",
94 .start = atmel_serial_init,
95 .stop = NULL,
96 .setbrg = atmel_serial_setbrg,
97 .putc = atmel_serial_putc,
Marek Vasutd9c64492012-10-06 14:07:02 +000098 .puts = default_serial_puts,
Marek Vasutff093ed2012-09-13 16:50:30 +020099 .getc = atmel_serial_getc,
100 .tstc = atmel_serial_tstc,
101};
102
103void atmel_serial_initialize(void)
104{
105 serial_register(&atmel_serial_drv);
106}
107
108__weak struct serial_device *default_serial_console(void)
109{
110 return &atmel_serial_drv;
111}