blob: 30336f262cf8ee6b0ba85c6a285ecdb77ce3d280 [file] [log] [blame]
wdenkf5300ab2003-09-12 15:35:15 +00001/*
2 * (C) Copyright 2003
3 * Martin Krause, TQ-Systems GmbH, <martin.krause@tqs.de>
4 *
Peter Tyser84ad6882010-04-12 22:28:11 -05005 * Based on arch/arm/cpu/arm920t/serial.c, by Gary Jennejohn
Detlev Zundel792a09e2009-05-13 10:54:10 +02006 * (C) Copyright 2002 Gary Jennejohn, DENX Software Engineering, <garyj@denx.de>
wdenkf5300ab2003-09-12 15:35:15 +00007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
24#include <common.h>
kevin.morfitt@fearnside-systems.co.ukac678042009-11-17 18:30:34 +090025#include <asm/arch/s3c24x0_cpu.h>
wdenkf5300ab2003-09-12 15:35:15 +000026#include "rs485.h"
27
28static void rs485_setbrg (void);
29static void rs485_cfgio (void);
30static void set_rs485re(unsigned char rs485re_state);
31static void set_rs485de(unsigned char rs485de_state);
32static void rs485_setbrg (void);
33#ifdef NOT_USED
34static void trab_rs485_disable_tx(void);
35static void trab_rs485_disable_rx(void);
36#endif
37
38#define UART_NR S3C24X0_UART1
39
40/* CPLD-Register for controlling TRAB hardware functions */
41#define CPLD_RS485_RE ((volatile unsigned long *)0x04028000)
42
43static void rs485_setbrg (void)
44{
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090045 struct s3c24x0_uart * const uart = s3c24x0_get_base_uart(UART_NR);
wdenkf5300ab2003-09-12 15:35:15 +000046 int i;
47 unsigned int reg = 0;
48
49 /* value is calculated so : (int)(PCLK/16./baudrate) -1 */
wdenka0ff7f22003-10-09 13:16:55 +000050 /* reg = (33000000 / (16 * gd->baudrate)) - 1; */
51 reg = (33000000 / (16 * 38400)) - 1;
wdenkf5300ab2003-09-12 15:35:15 +000052
53 /* FIFO enable, Tx/Rx FIFO clear */
C Naumand9abba82010-10-26 23:04:31 +090054 uart->ufcon = 0x07;
55 uart->umcon = 0x0;
wdenkf5300ab2003-09-12 15:35:15 +000056 /* Normal,No parity,1 stop,8 bit */
C Naumand9abba82010-10-26 23:04:31 +090057 uart->ulcon = 0x3;
wdenkf5300ab2003-09-12 15:35:15 +000058 /*
59 * tx=level,rx=edge,disable timeout int.,enable rx error int.,
60 * normal,interrupt or polling
61 */
C Naumand9abba82010-10-26 23:04:31 +090062 uart->ucon = 0x245;
63 uart->ubrdiv = reg;
wdenkf5300ab2003-09-12 15:35:15 +000064
65 for (i = 0; i < 100; i++);
66}
67
68static void rs485_cfgio (void)
69{
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090070 struct s3c24x0_gpio * const gpio = s3c24x0_get_base_gpio();
wdenkf5300ab2003-09-12 15:35:15 +000071
C Naumand9abba82010-10-26 23:04:31 +090072 gpio->pfcon &= ~(0x3 << 2);
73 gpio->pfcon |= (0x2 << 2); /* configure GPF1 as RXD1 */
wdenkf5300ab2003-09-12 15:35:15 +000074
C Naumand9abba82010-10-26 23:04:31 +090075 gpio->pfcon &= ~(0x3 << 6);
76 gpio->pfcon |= (0x2 << 6); /* configure GPF3 as TXD1 */
wdenkf5300ab2003-09-12 15:35:15 +000077
C Naumand9abba82010-10-26 23:04:31 +090078 gpio->pfup |= (1 << 1); /* disable pullup on GPF1 */
79 gpio->pfup |= (1 << 3); /* disable pullup on GPF3 */
wdenkf5300ab2003-09-12 15:35:15 +000080
C Naumand9abba82010-10-26 23:04:31 +090081 gpio->pacon &= ~(1 << 11); /* set GPA11 (RS485_DE) to output */
wdenkf5300ab2003-09-12 15:35:15 +000082}
83
84/*
85 * Initialise the rs485 port with the given baudrate. The settings
86 * are always 8 data bits, no parity, 1 stop bit, no start bits.
87 *
88 */
89int rs485_init (void)
90{
wdenka0ff7f22003-10-09 13:16:55 +000091 rs485_cfgio ();
92 rs485_setbrg ();
wdenkf5300ab2003-09-12 15:35:15 +000093
94 return (0);
95}
96
97/*
98 * Read a single byte from the rs485 port. Returns 1 on success, 0
99 * otherwise. When the function is succesfull, the character read is
100 * written into its argument c.
101 */
102int rs485_getc (void)
103{
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900104 struct s3c24x0_uart * const uart = s3c24x0_get_base_uart(UART_NR);
wdenkf5300ab2003-09-12 15:35:15 +0000105
106 /* wait for character to arrive */
C Naumand9abba82010-10-26 23:04:31 +0900107 while (!(uart->utrstat & 0x1))
108 ;
wdenkf5300ab2003-09-12 15:35:15 +0000109
C Naumand9abba82010-10-26 23:04:31 +0900110 return uart->urxh & 0xff;
wdenkf5300ab2003-09-12 15:35:15 +0000111}
112
113/*
114 * Output a single byte to the rs485 port.
115 */
116void rs485_putc (const char c)
117{
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900118 struct s3c24x0_uart * const uart = s3c24x0_get_base_uart(UART_NR);
wdenkf5300ab2003-09-12 15:35:15 +0000119
120 /* wait for room in the tx FIFO */
C Naumand9abba82010-10-26 23:04:31 +0900121 while (!(uart->utrstat & 0x2))
122 ;
wdenkf5300ab2003-09-12 15:35:15 +0000123
C Naumand9abba82010-10-26 23:04:31 +0900124 uart->utxh = c;
wdenkf5300ab2003-09-12 15:35:15 +0000125
126 /* If \n, also do \r */
127 if (c == '\n')
128 rs485_putc ('\r');
129}
130
131/*
132 * Test whether a character is in the RX buffer
133 */
134int rs485_tstc (void)
135{
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900136 struct s3c24x0_uart * const uart = s3c24x0_get_base_uart(UART_NR);
wdenkf5300ab2003-09-12 15:35:15 +0000137
C Naumand9abba82010-10-26 23:04:31 +0900138 return uart->utrstat & 0x1;
wdenkf5300ab2003-09-12 15:35:15 +0000139}
140
141void rs485_puts (const char *s)
142{
143 while (*s) {
144 rs485_putc (*s++);
145 }
146}
147
148
149/*
150 * State table:
151 * RE DE Result
152 * 1 1 XMIT
153 * 0 0 RCV
154 * 1 0 Shutdown
155 */
156
157/* function that controls the receiver enable for the rs485 */
158/* rs485re_state reflects the level (0/1) of the RE pin */
159
160static void set_rs485re(unsigned char rs485re_state)
161{
162 if(rs485re_state)
163 *CPLD_RS485_RE = 0x010000;
164 else
165 *CPLD_RS485_RE = 0x0;
166}
167
168/* function that controls the sender enable for the rs485 */
169/* rs485de_state reflects the level (0/1) of the DE pin */
170
171static void set_rs485de(unsigned char rs485de_state)
172{
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900173 struct s3c24x0_gpio * const gpio = s3c24x0_get_base_gpio();
wdenkf5300ab2003-09-12 15:35:15 +0000174
wdenka0ff7f22003-10-09 13:16:55 +0000175 /* This is on PORT A bit 11 */
176 if(rs485de_state)
C Naumand9abba82010-10-26 23:04:31 +0900177 gpio->padat |= (1 << 11);
wdenka0ff7f22003-10-09 13:16:55 +0000178 else
C Naumand9abba82010-10-26 23:04:31 +0900179 gpio->padat &= ~(1 << 11);
wdenkf5300ab2003-09-12 15:35:15 +0000180}
181
182
183void trab_rs485_enable_tx(void)
184{
185 set_rs485de(1);
186 set_rs485re(1);
187}
188
189void trab_rs485_enable_rx(void)
190{
191 set_rs485re(0);
192 set_rs485de(0);
193}
194
195#ifdef NOT_USED
196static void trab_rs485_disable_tx(void)
197{
198 set_rs485de(0);
199}
200
201static void trab_rs485_disable_rx(void)
202{
203 set_rs485re(1);
204}
205#endif