sh: Fix receive FIFO level register of SH4A
[platform/kernel/u-boot.git] / drivers / serial / serial_sh.c
1 /*
2  * SuperH SCIF device driver.
3  * Copyright (c) 2007,2008 Nobuhiro Iwamatsu
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 #include <common.h>
21 #include <asm/processor.h>
22
23 #ifdef CFG_SCIF_CONSOLE
24
25 #if defined (CONFIG_CONS_SCIF0)
26 #define SCIF_BASE       SCIF0_BASE
27 #elif defined (CONFIG_CONS_SCIF1)
28 #define SCIF_BASE       SCIF1_BASE
29 #else
30 #error "Default SCIF doesn't set....."
31 #endif
32
33 /* Base register */
34 #define SCSMR   (vu_short *)(SCIF_BASE + 0x0)
35 #define SCBRR   (vu_char  *)(SCIF_BASE + 0x4)
36 #define SCSCR   (vu_short *)(SCIF_BASE + 0x8)
37 #define SCFCR   (vu_short *)(SCIF_BASE + 0x18)
38 #define SCFDR   (vu_short *)(SCIF_BASE + 0x1C)
39 #ifdef CONFIG_CPU_SH7720 /* SH7720 specific */
40 # define SCFSR  (vu_short *)(SCIF_BASE + 0x14) /* SCSSR */
41 # define SCFTDR (vu_char  *)(SCIF_BASE + 0x20)
42 # define SCFRDR (vu_char  *)(SCIF_BASE + 0x24)
43 #else
44 # define SCFTDR (vu_char  *)(SCIF_BASE + 0xC)
45 # define SCFSR  (vu_short *)(SCIF_BASE + 0x10)
46 # define SCFRDR (vu_char  *)(SCIF_BASE + 0x14)
47 #endif
48
49 #if defined(CONFIG_CPU_SH7780) || \
50         defined(CONFIG_CPU_SH7785)
51 # define SCRFDR (vu_short *)(SCIF_BASE + 0x20)
52 # define SCSPTR (vu_short *)(SCIF_BASE + 0x24)
53 # define SCLSR   (vu_short *)(SCIF_BASE + 0x28)
54 # define SCRER  (vu_short *)(SCIF_BASE + 0x2C)
55 # define LSR_ORER       1
56 # define FIFOLEVEL_MASK 0xFF
57 #elif defined(CONFIG_CPU_SH7750) || \
58         defined(CONFIG_CPU_SH7722)
59 # define SCSPTR         (vu_short *)(SCIF_BASE + 0x20)
60 # define SCLSR  (vu_short *)(SCIF_BASE + 0x24)
61 # define LSR_ORER       1
62 # define FIFOLEVEL_MASK 0x1F
63 #elif defined(CONFIG_CPU_SH7720)
64 # define SCLSR   (vu_short *)(SCIF_BASE + 0x24)
65 # define LSR_ORER       0x0200
66 # define FIFOLEVEL_MASK 0x1F
67 #elif defined(CONFIG_CPU_SH7710)
68         defined(CONFIG_CPU_SH7712)
69 # define SCLSR  SCFSR   /* SCSSR */
70 # define LSR_ORER       1
71 # define FIFOLEVEL_MASK 0x1F
72 #endif
73
74 /* SCBRR register value setting */
75 #if defined(CONFIG_CPU_SH7720)
76 # define SCBRR_VALUE(bps, clk) (((clk*2)+16*bps)/(32*bps)-1)
77 #else   /* Generic SuperH */
78 # define SCBRR_VALUE(bps, clk) ((clk+16*bps)/(32*bps)-1)
79 #endif
80
81 #define SCR_RE          (1 << 4)
82 #define SCR_TE          (1 << 5)
83 #define FCR_RFRST       (1 << 1) /* RFCL */
84 #define FCR_TFRST       (1 << 2) /* TFCL */
85 #define FSR_DR          (1 << 0)
86 #define FSR_RDF         (1 << 1)
87 #define FSR_FER         (1 << 3)
88 #define FSR_BRK         (1 << 4)
89 #define FSR_FER         (1 << 3)
90 #define FSR_TEND        (1 << 6)
91 #define FSR_ER          (1 << 7)
92
93 /*----------------------------------------------------------------------*/
94
95 void serial_setbrg (void)
96 {
97         DECLARE_GLOBAL_DATA_PTR;
98         *SCBRR = SCBRR_VALUE(gd->baudrate,CONFIG_SYS_CLK_FREQ);
99 }
100
101 int serial_init (void)
102 {
103         *SCSCR = (SCR_RE | SCR_TE);
104         *SCSMR = 0 ;
105         *SCSMR = 0;
106         *SCFCR = (FCR_RFRST | FCR_TFRST);
107         *SCFCR;
108         *SCFCR = 0;
109
110         serial_setbrg();
111         return 0;
112 }
113
114 static int serial_tx_fifo_level (void)
115 {
116         return (*SCFDR >> 8) & FIFOLEVEL_MASK;
117 }
118
119 static int serial_rx_fifo_level (void)
120 {
121 #if defined(CONFIG_SH4A)
122         return (*SCRFDR >> 0) & FIFOLEVEL_MASK;
123 #else
124         return (*SCFDR >> 0) & FIFOLEVEL_MASK;
125 #endif
126 }
127
128 void serial_raw_putc (const char c)
129 {
130         unsigned int fsr_bits_to_clear;
131
132         while (1) {
133                 if (*SCFSR & FSR_TEND) {                /* Tx fifo is empty */
134                         fsr_bits_to_clear = FSR_TEND;
135                         break;
136                 }
137         }
138
139         *SCFTDR = c;
140         if (fsr_bits_to_clear != 0)
141                 *SCFSR &= ~fsr_bits_to_clear;
142 }
143
144 void serial_putc (const char c)
145 {
146         if (c == '\n')
147                 serial_raw_putc ('\r');
148         serial_raw_putc (c);
149 }
150
151 void serial_puts (const char *s)
152 {
153         char c;
154         while ((c = *s++) != 0)
155                 serial_putc (c);
156 }
157
158 int serial_tstc (void)
159 {
160         return serial_rx_fifo_level() ? 1 : 0;
161 }
162
163 #define FSR_ERR_CLEAR   0x0063
164 #define RDRF_CLEAR      0x00fc
165 void handle_error( void ){
166
167         (void)*SCFSR ;
168         *SCFSR = FSR_ERR_CLEAR ;
169         (void)*SCLSR ;
170         *SCLSR = 0x00 ;
171 }
172
173 int serial_getc_check( void ){
174         unsigned short status;
175
176         status = *SCFSR ;
177
178         if (status & (FSR_FER | FSR_FER | FSR_ER | FSR_BRK))
179                 handle_error();
180         if( *SCLSR & LSR_ORER )
181                 handle_error();
182         return (status & ( FSR_DR | FSR_RDF ));
183 }
184
185 int serial_getc (void)
186 {
187         unsigned short status ;
188         char ch;
189         while(!serial_getc_check());
190
191         ch = *SCFRDR;
192         status =  *SCFSR ;
193
194         *SCFSR = RDRF_CLEAR ;
195
196         if (status & (FSR_FER | FSR_FER | FSR_ER | FSR_BRK))
197                 handle_error();
198
199         if( *SCLSR & LSR_ORER )
200                 handle_error();
201
202         return ch ;
203 }
204
205 #endif  /* CFG_SCIF_CONSOLE */