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