blob: ce36e9914510e1ba671d50cf97e773f17f686875 [file] [log] [blame]
Wolfgang Denkf93ae782006-10-24 14:31:24 +02001/*
2 * Copyright (C) 2004-2006 Atmel Corporation
3 *
Andreas Bießmann125637c2010-09-03 10:28:05 +02004 * Modified to support C structur SoC access by
5 * Andreas Bießmann <biessmann@corscience.de>
6 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Wolfgang Denkf93ae782006-10-24 14:31:24 +02008 */
9#include <common.h>
Jean-Christophe PLAGNIOL-VILLARD843a2652009-03-27 23:26:42 +010010#include <watchdog.h>
Marek Vasutcfba4572012-09-13 16:50:30 +020011#include <serial.h>
12#include <linux/compiler.h>
Wolfgang Denkf93ae782006-10-24 14:31:24 +020013
Wolfgang Denkf93ae782006-10-24 14:31:24 +020014#include <asm/io.h>
Haavard Skinnemoendf548d32006-11-19 18:06:53 +010015#include <asm/arch/clk.h>
Reinhard Meyer329f0f52010-11-03 16:32:56 +010016#include <asm/arch/hardware.h>
Wolfgang Denkf93ae782006-10-24 14:31:24 +020017
18#include "atmel_usart.h"
19
20DECLARE_GLOBAL_DATA_PTR;
21
Simon Glass62137fc2014-10-29 13:08:59 -060022static void atmel_serial_setbrg_internal(atmel_usart3_t *usart, int id,
23 int baudrate)
Wolfgang Denkf93ae782006-10-24 14:31:24 +020024{
25 unsigned long divisor;
26 unsigned long usart_hz;
27
28 /*
29 * Master Clock
30 * Baud Rate = --------------
31 * 16 * CD
32 */
Simon Glass62137fc2014-10-29 13:08:59 -060033 usart_hz = get_usart_clk_rate(id);
34 divisor = (usart_hz / 16 + baudrate / 2) / baudrate;
Andreas Bießmann125637c2010-09-03 10:28:05 +020035 writel(USART3_BF(CD, divisor), &usart->brgr);
Wolfgang Denkf93ae782006-10-24 14:31:24 +020036}
37
Simon Glass62137fc2014-10-29 13:08:59 -060038static void atmel_serial_init_internal(atmel_usart3_t *usart)
Wolfgang Denkf93ae782006-10-24 14:31:24 +020039{
Xu, Hong1f4faed2011-08-02 01:05:04 +000040 /*
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ßmann125637c2010-09-03 10:28:05 +020047 writel(USART3_BIT(RSTRX) | USART3_BIT(RSTTX), &usart->cr);
Simon Glass62137fc2014-10-29 13:08:59 -060048}
Wolfgang Denkf93ae782006-10-24 14:31:24 +020049
Simon Glass62137fc2014-10-29 13:08:59 -060050static void atmel_serial_activate(atmel_usart3_t *usart)
51{
Andreas Bießmann125637c2010-09-03 10:28:05 +020052 writel((USART3_BF(USART_MODE, USART3_USART_MODE_NORMAL)
Haavard Skinnemoendf548d32006-11-19 18:06:53 +010053 | USART3_BF(USCLKS, USART3_USCLKS_MCK)
54 | USART3_BF(CHRL, USART3_CHRL_8)
55 | USART3_BF(PAR, USART3_PAR_NONE)
Andreas Bießmann125637c2010-09-03 10:28:05 +020056 | USART3_BF(NBSTOP, USART3_NBSTOP_1)),
57 &usart->mr);
Xu, Hong1f4faed2011-08-02 01:05:04 +000058 writel(USART3_BIT(RXEN) | USART3_BIT(TXEN), &usart->cr);
59 /* 100us is enough for the new settings to be settled */
60 __udelay(100);
Simon Glass62137fc2014-10-29 13:08:59 -060061}
62
63static 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
69static 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 Denkf93ae782006-10-24 14:31:24 +020076
77 return 0;
78}
79
Marek Vasutcfba4572012-09-13 16:50:30 +020080static void atmel_serial_putc(char c)
Wolfgang Denkf93ae782006-10-24 14:31:24 +020081{
Reinhard Meyer329f0f52010-11-03 16:32:56 +010082 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
Andreas Bießmann125637c2010-09-03 10:28:05 +020083
Wolfgang Denkf93ae782006-10-24 14:31:24 +020084 if (c == '\n')
85 serial_putc('\r');
86
Andreas Bießmann125637c2010-09-03 10:28:05 +020087 while (!(readl(&usart->csr) & USART3_BIT(TXRDY)));
88 writel(c, &usart->thr);
Wolfgang Denkf93ae782006-10-24 14:31:24 +020089}
90
Marek Vasutcfba4572012-09-13 16:50:30 +020091static int atmel_serial_getc(void)
Wolfgang Denkf93ae782006-10-24 14:31:24 +020092{
Reinhard Meyer329f0f52010-11-03 16:32:56 +010093 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
Andreas Bießmann125637c2010-09-03 10:28:05 +020094
95 while (!(readl(&usart->csr) & USART3_BIT(RXRDY)))
Jean-Christophe PLAGNIOL-VILLARD843a2652009-03-27 23:26:42 +010096 WATCHDOG_RESET();
Andreas Bießmann125637c2010-09-03 10:28:05 +020097 return readl(&usart->rhr);
Wolfgang Denkf93ae782006-10-24 14:31:24 +020098}
99
Marek Vasutcfba4572012-09-13 16:50:30 +0200100static int atmel_serial_tstc(void)
Wolfgang Denkf93ae782006-10-24 14:31:24 +0200101{
Reinhard Meyer329f0f52010-11-03 16:32:56 +0100102 atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
Andreas Bießmann125637c2010-09-03 10:28:05 +0200103 return (readl(&usart->csr) & USART3_BIT(RXRDY)) != 0;
Wolfgang Denkf93ae782006-10-24 14:31:24 +0200104}
Marek Vasutcfba4572012-09-13 16:50:30 +0200105
Marek Vasutcfba4572012-09-13 16:50:30 +0200106static 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 Vasutec3fd682012-10-06 14:07:02 +0000112 .puts = default_serial_puts,
Marek Vasutcfba4572012-09-13 16:50:30 +0200113 .getc = atmel_serial_getc,
114 .tstc = atmel_serial_tstc,
115};
116
117void 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}