Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 1 | /* |
| 2 | * SuperH SCIF device driver. |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame^] | 3 | * Copyright (C) 2007,2008,2010 Nobuhiro Iwamatsu |
| 4 | * Copyright (C) 2002 - 2008 Paul Mundt |
Wolfgang Denk | 0a5c214 | 2007-12-27 01:52:50 +0100 | [diff] [blame] | 5 | * |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | |
| 21 | #include <common.h> |
Jean-Christophe PLAGNIOL-VILLARD | b27a8e3 | 2009-01-11 16:35:16 +0100 | [diff] [blame] | 22 | #include <asm/io.h> |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 23 | #include <asm/processor.h> |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame^] | 24 | #include "serial_sh.h" |
John Rigby | 0d21ed0 | 2010-12-20 18:27:51 -0700 | [diff] [blame] | 25 | |
Nobuhiro Iwamatsu | fcabccc | 2008-08-22 17:48:51 +0900 | [diff] [blame] | 26 | #if defined(CONFIG_CONS_SCIF0) |
| 27 | # define SCIF_BASE SCIF0_BASE |
| 28 | #elif defined(CONFIG_CONS_SCIF1) |
| 29 | # define SCIF_BASE SCIF1_BASE |
| 30 | #elif defined(CONFIG_CONS_SCIF2) |
| 31 | # define SCIF_BASE SCIF2_BASE |
| 32 | #elif defined(CONFIG_CONS_SCIF3) |
| 33 | # define SCIF_BASE SCIF3_BASE |
| 34 | #elif defined(CONFIG_CONS_SCIF4) |
| 35 | # define SCIF_BASE SCIF4_BASE |
| 36 | #elif defined(CONFIG_CONS_SCIF5) |
| 37 | # define SCIF_BASE SCIF5_BASE |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 38 | #else |
Nobuhiro Iwamatsu | fcabccc | 2008-08-22 17:48:51 +0900 | [diff] [blame] | 39 | # error "Default SCIF doesn't set....." |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 40 | #endif |
| 41 | |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame^] | 42 | #if defined(CONFIG_SCIF_A) |
| 43 | #define SCIF_BASE_PORT PORT_SCIFA |
Yoshihiro Shimoda | e625834 | 2008-01-09 14:30:02 +0900 | [diff] [blame] | 44 | #else |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame^] | 45 | #define SCIF_BASE_PORT PORT_SCIF |
Yoshihiro Shimoda | e625834 | 2008-01-09 14:30:02 +0900 | [diff] [blame] | 46 | #endif |
| 47 | |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame^] | 48 | static struct uart_port sh_sci = { |
| 49 | .membase = (unsigned char*)SCIF_BASE, |
| 50 | .mapbase = SCIF_BASE, |
| 51 | .type = SCIF_BASE_PORT, |
| 52 | }; |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 53 | |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 54 | void serial_setbrg(void) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 55 | { |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame^] | 56 | DECLARE_GLOBAL_DATA_PTR; |
| 57 | sci_out(&sh_sci, SCBRR, SCBRR_VALUE(gd->baudrate, CONFIG_SYS_CLK_FREQ)); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 58 | } |
| 59 | |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 60 | int serial_init(void) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 61 | { |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame^] | 62 | sci_out(&sh_sci, SCSCR , SCSCR_INIT(&sh_sci)); |
| 63 | sci_out(&sh_sci, SCSCR , SCSCR_INIT(&sh_sci)); |
| 64 | sci_out(&sh_sci, SCSMR, 0); |
| 65 | sci_out(&sh_sci, SCSMR, 0); |
| 66 | sci_out(&sh_sci, SCFCR, SCFCR_RFRST|SCFCR_TFRST); |
| 67 | sci_in(&sh_sci, SCFCR); |
| 68 | sci_out(&sh_sci, SCFCR, 0); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 69 | |
| 70 | serial_setbrg(); |
| 71 | return 0; |
| 72 | } |
| 73 | |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame^] | 74 | #if defined(CONFIG_CPU_SH7760) || \ |
| 75 | defined(CONFIG_CPU_SH7780) || \ |
| 76 | defined(CONFIG_CPU_SH7785) || \ |
| 77 | defined(CONFIG_CPU_SH7786) |
| 78 | static int scif_rxfill(struct uart_port *port) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 79 | { |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame^] | 80 | return sci_in(port, SCRFDR) & 0xff; |
| 81 | } |
| 82 | #elif defined(CONFIG_CPU_SH7763) |
| 83 | static int scif_rxfill(struct uart_port *port) |
| 84 | { |
| 85 | if ((port->mapbase == 0xffe00000) || |
| 86 | (port->mapbase == 0xffe08000)) { |
| 87 | /* SCIF0/1*/ |
| 88 | return sci_in(port, SCRFDR) & 0xff; |
| 89 | } else { |
| 90 | /* SCIF2 */ |
| 91 | return sci_in(port, SCFDR) & SCIF2_RFDC_MASK; |
| 92 | } |
| 93 | } |
| 94 | #elif defined(CONFIG_ARCH_SH7372) |
| 95 | static int scif_rxfill(struct uart_port *port) |
| 96 | { |
| 97 | if (port->type == PORT_SCIFA) |
| 98 | return sci_in(port, SCFDR) & SCIF_RFDC_MASK; |
| 99 | else |
| 100 | return sci_in(port, SCRFDR); |
| 101 | } |
Nobuhiro Iwamatsu | 1b36beb | 2008-03-06 14:05:53 +0900 | [diff] [blame] | 102 | #else |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame^] | 103 | static int scif_rxfill(struct uart_port *port) |
| 104 | { |
| 105 | return sci_in(port, SCFDR) & SCIF_RFDC_MASK; |
| 106 | } |
Nobuhiro Iwamatsu | 1b36beb | 2008-03-06 14:05:53 +0900 | [diff] [blame] | 107 | #endif |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame^] | 108 | |
| 109 | static int serial_rx_fifo_level(void) |
| 110 | { |
| 111 | return scif_rxfill(&sh_sci); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 112 | } |
| 113 | |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 114 | void serial_raw_putc(const char c) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 115 | { |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 116 | while (1) { |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame^] | 117 | /* Tx fifo is empty */ |
| 118 | if (sci_in(&sh_sci, SCxSR) & SCxSR_TEND(&sh_sci)) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 119 | break; |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 120 | } |
| 121 | |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame^] | 122 | sci_out(&sh_sci, SCxTDR, c); |
| 123 | sci_out(&sh_sci, SCxSR, sci_in(&sh_sci, SCxSR) & ~SCxSR_TEND(&sh_sci)); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 124 | } |
| 125 | |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 126 | void serial_putc(const char c) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 127 | { |
| 128 | if (c == '\n') |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 129 | serial_raw_putc('\r'); |
| 130 | serial_raw_putc(c); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 131 | } |
| 132 | |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 133 | void serial_puts(const char *s) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 134 | { |
| 135 | char c; |
| 136 | while ((c = *s++) != 0) |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 137 | serial_putc(c); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 138 | } |
| 139 | |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 140 | int serial_tstc(void) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 141 | { |
Nobuhiro Iwamatsu | fcabccc | 2008-08-22 17:48:51 +0900 | [diff] [blame] | 142 | return serial_rx_fifo_level() ? 1 : 0; |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 143 | } |
| 144 | |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 145 | void handle_error(void) |
| 146 | { |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame^] | 147 | sci_in(&sh_sci, SCxSR); |
| 148 | sci_out(&sh_sci, SCxSR, SCxSR_ERROR_CLEAR(&sh_sci)); |
| 149 | sci_in(&sh_sci, SCLSR); |
| 150 | sci_out(&sh_sci, SCLSR, 0x00); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 151 | } |
| 152 | |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 153 | int serial_getc_check(void) |
| 154 | { |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 155 | unsigned short status; |
| 156 | |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame^] | 157 | status = sci_in(&sh_sci, SCxSR); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 158 | |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame^] | 159 | if (status & SCIF_ERRORS) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 160 | handle_error(); |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame^] | 161 | if (sci_in(&sh_sci, SCLSR) & SCxSR_ORER(&sh_sci)) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 162 | handle_error(); |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame^] | 163 | return status & (SCIF_DR | SCxSR_RDxF(&sh_sci)); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 164 | } |
| 165 | |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 166 | int serial_getc(void) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 167 | { |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 168 | unsigned short status; |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 169 | char ch; |
Nobuhiro Iwamatsu | fcabccc | 2008-08-22 17:48:51 +0900 | [diff] [blame] | 170 | |
| 171 | while (!serial_getc_check()) |
| 172 | ; |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 173 | |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame^] | 174 | ch = sci_in(&sh_sci, SCxRDR); |
| 175 | status = sci_in(&sh_sci, SCxSR); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 176 | |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame^] | 177 | sci_out(&sh_sci, SCxSR, SCxSR_RDxF_CLEAR(&sh_sci)); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 178 | |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame^] | 179 | if (status & SCIF_ERRORS) |
| 180 | handle_error(); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 181 | |
Nobuhiro Iwamatsu | a5579ca | 2010-10-26 03:55:15 +0900 | [diff] [blame^] | 182 | if (sci_in(&sh_sci, SCLSR) & SCxSR_ORER(&sh_sci)) |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 183 | handle_error(); |
Nobuhiro Iwamatsu | 6564b1a | 2008-06-06 16:16:08 +0900 | [diff] [blame] | 184 | return ch; |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 185 | } |