Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 1 | /* |
| 2 | * SuperH SCIF device driver. |
Nobuhiro Iwamatsu | 30bf52c | 2008-01-17 15:53:52 +0900 | [diff] [blame] | 3 | * Copyright (c) 2007,2008 Nobuhiro Iwamatsu |
Wolfgang Denk | 0a5c214 | 2007-12-27 01:52:50 +0100 | [diff] [blame] | 4 | * |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 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 | |
Nobuhiro Iwamatsu | fcabccc | 2008-08-22 17:48:51 +0900 | [diff] [blame] | 23 | #if defined(CONFIG_CONS_SCIF0) |
| 24 | # define SCIF_BASE SCIF0_BASE |
| 25 | #elif defined(CONFIG_CONS_SCIF1) |
| 26 | # define SCIF_BASE SCIF1_BASE |
| 27 | #elif defined(CONFIG_CONS_SCIF2) |
| 28 | # define SCIF_BASE SCIF2_BASE |
| 29 | #elif defined(CONFIG_CONS_SCIF3) |
| 30 | # define SCIF_BASE SCIF3_BASE |
| 31 | #elif defined(CONFIG_CONS_SCIF4) |
| 32 | # define SCIF_BASE SCIF4_BASE |
| 33 | #elif defined(CONFIG_CONS_SCIF5) |
| 34 | # define SCIF_BASE SCIF5_BASE |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 35 | #else |
Nobuhiro Iwamatsu | fcabccc | 2008-08-22 17:48:51 +0900 | [diff] [blame] | 36 | # error "Default SCIF doesn't set....." |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 37 | #endif |
| 38 | |
Nobuhiro Iwamatsu | fb3205b | 2008-01-15 23:25:25 +0900 | [diff] [blame] | 39 | /* Base register */ |
| 40 | #define SCSMR (vu_short *)(SCIF_BASE + 0x0) |
| 41 | #define SCBRR (vu_char *)(SCIF_BASE + 0x4) |
| 42 | #define SCSCR (vu_short *)(SCIF_BASE + 0x8) |
| 43 | #define SCFCR (vu_short *)(SCIF_BASE + 0x18) |
| 44 | #define SCFDR (vu_short *)(SCIF_BASE + 0x1C) |
Nobuhiro Iwamatsu | fcabccc | 2008-08-22 17:48:51 +0900 | [diff] [blame] | 45 | #if defined(CONFIG_CPU_SH7720) || \ |
| 46 | (defined(CONFIG_CPU_SH7723) && defined(CONFIG_SCIF_A)) |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 47 | # define SCFSR (vu_short *)(SCIF_BASE + 0x14) /* SCSSR */ |
Nobuhiro Iwamatsu | 1b36beb | 2008-03-06 14:05:53 +0900 | [diff] [blame] | 48 | # define SCFTDR (vu_char *)(SCIF_BASE + 0x20) |
| 49 | # define SCFRDR (vu_char *)(SCIF_BASE + 0x24) |
Yoshihiro Shimoda | e625834 | 2008-01-09 14:30:02 +0900 | [diff] [blame] | 50 | #else |
Nobuhiro Iwamatsu | 1b36beb | 2008-03-06 14:05:53 +0900 | [diff] [blame] | 51 | # define SCFTDR (vu_char *)(SCIF_BASE + 0xC) |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 52 | # define SCFSR (vu_short *)(SCIF_BASE + 0x10) |
Nobuhiro Iwamatsu | 1b36beb | 2008-03-06 14:05:53 +0900 | [diff] [blame] | 53 | # define SCFRDR (vu_char *)(SCIF_BASE + 0x14) |
Yoshihiro Shimoda | e625834 | 2008-01-09 14:30:02 +0900 | [diff] [blame] | 54 | #endif |
| 55 | |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 56 | #if defined(CONFIG_CPU_SH7780) || \ |
Yusuke Goda | 54cda8b | 2008-03-05 14:23:26 +0900 | [diff] [blame] | 57 | defined(CONFIG_CPU_SH7785) |
Nobuhiro Iwamatsu | 1b36beb | 2008-03-06 14:05:53 +0900 | [diff] [blame] | 58 | # define SCRFDR (vu_short *)(SCIF_BASE + 0x20) |
| 59 | # define SCSPTR (vu_short *)(SCIF_BASE + 0x24) |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 60 | # define SCLSR (vu_short *)(SCIF_BASE + 0x28) |
Nobuhiro Iwamatsu | 1b36beb | 2008-03-06 14:05:53 +0900 | [diff] [blame] | 61 | # define SCRER (vu_short *)(SCIF_BASE + 0x2C) |
| 62 | # define LSR_ORER 1 |
| 63 | # define FIFOLEVEL_MASK 0xFF |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 64 | #elif defined(CONFIG_CPU_SH7763) |
Nobuhiro Iwamatsu | fcabccc | 2008-08-22 17:48:51 +0900 | [diff] [blame] | 65 | # if defined(CONFIG_CONS_SCIF2) |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 66 | # define SCSPTR (vu_short *)(SCIF_BASE + 0x20) |
| 67 | # define SCLSR (vu_short *)(SCIF_BASE + 0x24) |
| 68 | # define LSR_ORER 1 |
| 69 | # define FIFOLEVEL_MASK 0x1F |
| 70 | # else |
| 71 | # define SCRFDR (vu_short *)(SCIF_BASE + 0x20) |
| 72 | # define SCSPTR (vu_short *)(SCIF_BASE + 0x24) |
| 73 | # define SCLSR (vu_short *)(SCIF_BASE + 0x28) |
| 74 | # define SCRER (vu_short *)(SCIF_BASE + 0x2C) |
| 75 | # define LSR_ORER 1 |
| 76 | # define FIFOLEVEL_MASK 0xFF |
| 77 | # endif |
Nobuhiro Iwamatsu | fcabccc | 2008-08-22 17:48:51 +0900 | [diff] [blame] | 78 | #elif defined(CONFIG_CPU_SH7723) |
Nobuhiro Iwamatsu | 0123357 | 2008-09-17 11:45:26 +0900 | [diff] [blame^] | 79 | # if defined(CONFIG_SCIF_A) |
Nobuhiro Iwamatsu | fcabccc | 2008-08-22 17:48:51 +0900 | [diff] [blame] | 80 | # define SCLSR SCFSR |
| 81 | # define LSR_ORER 0x0200 |
| 82 | # define FIFOLEVEL_MASK 0x3F |
| 83 | #else |
| 84 | # define SCLSR (vu_short *)(SCIF_BASE + 0x24) |
| 85 | # define LSR_ORER 1 |
| 86 | # define FIFOLEVEL_MASK 0x1F |
| 87 | #endif |
Yusuke Goda | 54cda8b | 2008-03-05 14:23:26 +0900 | [diff] [blame] | 88 | #elif defined(CONFIG_CPU_SH7750) || \ |
Nobuhiro Iwamatsu | 53b3143 | 2008-03-12 12:10:28 +0900 | [diff] [blame] | 89 | defined(CONFIG_CPU_SH7751) || \ |
Nobuhiro Iwamatsu | 8e7130f | 2008-07-03 23:11:02 +0900 | [diff] [blame] | 90 | defined(CONFIG_CPU_SH7722) || \ |
| 91 | defined(CONFIG_CPU_SH7203) |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 92 | # define SCSPTR (vu_short *)(SCIF_BASE + 0x20) |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 93 | # define SCLSR (vu_short *)(SCIF_BASE + 0x24) |
Nobuhiro Iwamatsu | 1b36beb | 2008-03-06 14:05:53 +0900 | [diff] [blame] | 94 | # define LSR_ORER 1 |
| 95 | # define FIFOLEVEL_MASK 0x1F |
Yusuke Goda | 54cda8b | 2008-03-05 14:23:26 +0900 | [diff] [blame] | 96 | #elif defined(CONFIG_CPU_SH7720) |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 97 | # define SCLSR (vu_short *)(SCIF_BASE + 0x24) |
Nobuhiro Iwamatsu | 1b36beb | 2008-03-06 14:05:53 +0900 | [diff] [blame] | 98 | # define LSR_ORER 0x0200 |
| 99 | # define FIFOLEVEL_MASK 0x1F |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 100 | #elif defined(CONFIG_CPU_SH7710) || \ |
Yusuke Goda | 54cda8b | 2008-03-05 14:23:26 +0900 | [diff] [blame] | 101 | defined(CONFIG_CPU_SH7712) |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 102 | # define SCLSR SCFSR /* SCSSR */ |
Nobuhiro Iwamatsu | 1b36beb | 2008-03-06 14:05:53 +0900 | [diff] [blame] | 103 | # define LSR_ORER 1 |
| 104 | # define FIFOLEVEL_MASK 0x1F |
Nobuhiro Iwamatsu | fb3205b | 2008-01-15 23:25:25 +0900 | [diff] [blame] | 105 | #endif |
Nobuhiro Iwamatsu | 30bf52c | 2008-01-17 15:53:52 +0900 | [diff] [blame] | 106 | |
Yusuke Goda | 54cda8b | 2008-03-05 14:23:26 +0900 | [diff] [blame] | 107 | /* SCBRR register value setting */ |
Nobuhiro Iwamatsu | 30bf52c | 2008-01-17 15:53:52 +0900 | [diff] [blame] | 108 | #if defined(CONFIG_CPU_SH7720) |
Nobuhiro Iwamatsu | 1b36beb | 2008-03-06 14:05:53 +0900 | [diff] [blame] | 109 | # define SCBRR_VALUE(bps, clk) (((clk*2)+16*bps)/(32*bps)-1) |
Nobuhiro Iwamatsu | fcabccc | 2008-08-22 17:48:51 +0900 | [diff] [blame] | 110 | #elif defined(CONFIG_CPU_SH7723) && defined(CONFIG_SCIF_A) |
| 111 | /* SH7723 SCIFA use bus clock. So clock *2 */ |
| 112 | # define SCBRR_VALUE(bps, clk) (((clk*2*2)+16*bps)/(32*bps)-1) |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 113 | #else /* Generic SuperH */ |
Nobuhiro Iwamatsu | 1b36beb | 2008-03-06 14:05:53 +0900 | [diff] [blame] | 114 | # define SCBRR_VALUE(bps, clk) ((clk+16*bps)/(32*bps)-1) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 115 | #endif |
| 116 | |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 117 | #define SCR_RE (1 << 4) |
| 118 | #define SCR_TE (1 << 5) |
| 119 | #define FCR_RFRST (1 << 1) /* RFCL */ |
| 120 | #define FCR_TFRST (1 << 2) /* TFCL */ |
| 121 | #define FSR_DR (1 << 0) |
| 122 | #define FSR_RDF (1 << 1) |
| 123 | #define FSR_FER (1 << 3) |
| 124 | #define FSR_BRK (1 << 4) |
| 125 | #define FSR_FER (1 << 3) |
| 126 | #define FSR_TEND (1 << 6) |
| 127 | #define FSR_ER (1 << 7) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 128 | |
| 129 | /*----------------------------------------------------------------------*/ |
| 130 | |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 131 | void serial_setbrg(void) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 132 | { |
| 133 | DECLARE_GLOBAL_DATA_PTR; |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 134 | *SCBRR = SCBRR_VALUE(gd->baudrate, CONFIG_SYS_CLK_FREQ); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 135 | } |
| 136 | |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 137 | int serial_init(void) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 138 | { |
| 139 | *SCSCR = (SCR_RE | SCR_TE); |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 140 | *SCSMR = 0; |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 141 | *SCSMR = 0; |
| 142 | *SCFCR = (FCR_RFRST | FCR_TFRST); |
| 143 | *SCFCR; |
| 144 | *SCFCR = 0; |
| 145 | |
| 146 | serial_setbrg(); |
| 147 | return 0; |
| 148 | } |
| 149 | |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 150 | static int serial_rx_fifo_level(void) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 151 | { |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 152 | #if defined(SCRFDR) |
Nobuhiro Iwamatsu | 1b36beb | 2008-03-06 14:05:53 +0900 | [diff] [blame] | 153 | return (*SCRFDR >> 0) & FIFOLEVEL_MASK; |
| 154 | #else |
| 155 | return (*SCFDR >> 0) & FIFOLEVEL_MASK; |
| 156 | #endif |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 157 | } |
| 158 | |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 159 | void serial_raw_putc(const char c) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 160 | { |
| 161 | unsigned int fsr_bits_to_clear; |
| 162 | |
| 163 | while (1) { |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 164 | if (*SCFSR & FSR_TEND) { /* Tx fifo is empty */ |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 165 | fsr_bits_to_clear = FSR_TEND; |
| 166 | break; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | *SCFTDR = c; |
| 171 | if (fsr_bits_to_clear != 0) |
| 172 | *SCFSR &= ~fsr_bits_to_clear; |
| 173 | } |
| 174 | |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 175 | void serial_putc(const char c) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 176 | { |
| 177 | if (c == '\n') |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 178 | serial_raw_putc('\r'); |
| 179 | serial_raw_putc(c); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 180 | } |
| 181 | |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 182 | void serial_puts(const char *s) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 183 | { |
| 184 | char c; |
| 185 | while ((c = *s++) != 0) |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 186 | serial_putc(c); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 187 | } |
| 188 | |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 189 | int serial_tstc(void) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 190 | { |
Nobuhiro Iwamatsu | fcabccc | 2008-08-22 17:48:51 +0900 | [diff] [blame] | 191 | return serial_rx_fifo_level() ? 1 : 0; |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 192 | } |
| 193 | |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 194 | #define FSR_ERR_CLEAR 0x0063 |
| 195 | #define RDRF_CLEAR 0x00fc |
| 196 | void handle_error(void) |
| 197 | { |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 198 | |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 199 | (void)*SCFSR; |
| 200 | *SCFSR = FSR_ERR_CLEAR; |
| 201 | (void)*SCLSR; |
| 202 | *SCLSR = 0x00; |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 203 | } |
| 204 | |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 205 | int serial_getc_check(void) |
| 206 | { |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 207 | unsigned short status; |
| 208 | |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 209 | status = *SCFSR; |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 210 | |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 211 | if (status & (FSR_FER | FSR_ER | FSR_BRK)) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 212 | handle_error(); |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 213 | if (*SCLSR & LSR_ORER) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 214 | handle_error(); |
Nobuhiro Iwamatsu | fcabccc | 2008-08-22 17:48:51 +0900 | [diff] [blame] | 215 | return status & (FSR_DR | FSR_RDF); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 216 | } |
| 217 | |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 218 | int serial_getc(void) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 219 | { |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 220 | unsigned short status; |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 221 | char ch; |
Nobuhiro Iwamatsu | fcabccc | 2008-08-22 17:48:51 +0900 | [diff] [blame] | 222 | |
| 223 | while (!serial_getc_check()) |
| 224 | ; |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 225 | |
| 226 | ch = *SCFRDR; |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 227 | status = *SCFSR; |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 228 | |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 229 | *SCFSR = RDRF_CLEAR; |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 230 | |
| 231 | if (status & (FSR_FER | FSR_FER | FSR_ER | FSR_BRK)) |
| 232 | handle_error(); |
| 233 | |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 234 | if (*SCLSR & LSR_ORER) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 235 | handle_error(); |
| 236 | |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 237 | return ch; |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 238 | } |