Rafal Jaworowski | 8993e54 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 1 | /* |
Detlev Zundel | 57ae8a5 | 2010-01-21 17:55:58 +0100 | [diff] [blame] | 2 | * (C) Copyright 2000 - 2010 |
Rafal Jaworowski | 8993e54 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (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, |
| 21 | * MA 02111-1307 USA |
| 22 | * |
| 23 | * Based ont the MPC5200 PSC driver. |
| 24 | * Adapted for MPC512x by Jan Wrobel <wrr@semihalf.com> |
| 25 | */ |
| 26 | |
| 27 | /* |
| 28 | * Minimal serial functions needed to use one of the PSC ports |
| 29 | * as serial console interface. |
| 30 | */ |
| 31 | |
| 32 | #include <common.h> |
Mike Frysinger | 6c768ca | 2011-04-29 18:03:29 +0000 | [diff] [blame^] | 33 | #include <linux/compiler.h> |
Wolfgang Denk | 843efb1 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 34 | #include <asm/io.h> |
| 35 | #include <asm/processor.h> |
Anatolij Gustschin | e3b28e6 | 2010-04-24 19:27:05 +0200 | [diff] [blame] | 36 | #include <serial.h> |
Rafal Jaworowski | 8993e54 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 37 | |
| 38 | DECLARE_GLOBAL_DATA_PTR; |
| 39 | |
Anatolij Gustschin | e3b28e6 | 2010-04-24 19:27:05 +0200 | [diff] [blame] | 40 | #if defined(CONFIG_PSC_CONSOLE) || defined(CONFIG_SERIAL_MULTI) |
Rafal Jaworowski | 8993e54 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 41 | |
| 42 | static void fifo_init (volatile psc512x_t *psc) |
| 43 | { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 44 | volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR; |
Anatolij Gustschin | e3b28e6 | 2010-04-24 19:27:05 +0200 | [diff] [blame] | 45 | u32 tfsize, rfsize; |
Rafal Jaworowski | 8993e54 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 46 | |
| 47 | /* reset Rx & Tx fifo slice */ |
Wolfgang Denk | 843efb1 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 48 | out_be32(&psc->rfcmd, PSC_FIFO_RESET_SLICE); |
| 49 | out_be32(&psc->tfcmd, PSC_FIFO_RESET_SLICE); |
Rafal Jaworowski | 8993e54 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 50 | |
| 51 | /* disable Tx & Rx FIFO interrupts */ |
Wolfgang Denk | 843efb1 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 52 | out_be32(&psc->rfintmask, 0); |
| 53 | out_be32(&psc->tfintmask, 0); |
Rafal Jaworowski | 8993e54 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 54 | |
Anatolij Gustschin | e3b28e6 | 2010-04-24 19:27:05 +0200 | [diff] [blame] | 55 | #if defined(CONFIG_SERIAL_MULTI) |
| 56 | switch (((u32)psc & 0xf00) >> 8) { |
| 57 | case 0: |
| 58 | tfsize = FIFOC_PSC0_TX_SIZE | (FIFOC_PSC0_TX_ADDR << 16); |
| 59 | rfsize = FIFOC_PSC0_RX_SIZE | (FIFOC_PSC0_RX_ADDR << 16); |
| 60 | break; |
| 61 | case 1: |
| 62 | tfsize = FIFOC_PSC1_TX_SIZE | (FIFOC_PSC1_TX_ADDR << 16); |
| 63 | rfsize = FIFOC_PSC1_RX_SIZE | (FIFOC_PSC1_RX_ADDR << 16); |
| 64 | break; |
| 65 | case 2: |
| 66 | tfsize = FIFOC_PSC2_TX_SIZE | (FIFOC_PSC2_TX_ADDR << 16); |
| 67 | rfsize = FIFOC_PSC2_RX_SIZE | (FIFOC_PSC2_RX_ADDR << 16); |
| 68 | break; |
| 69 | case 3: |
| 70 | tfsize = FIFOC_PSC3_TX_SIZE | (FIFOC_PSC3_TX_ADDR << 16); |
| 71 | rfsize = FIFOC_PSC3_RX_SIZE | (FIFOC_PSC3_RX_ADDR << 16); |
| 72 | break; |
| 73 | case 4: |
| 74 | tfsize = FIFOC_PSC4_TX_SIZE | (FIFOC_PSC4_TX_ADDR << 16); |
| 75 | rfsize = FIFOC_PSC4_RX_SIZE | (FIFOC_PSC4_RX_ADDR << 16); |
| 76 | break; |
| 77 | case 5: |
| 78 | tfsize = FIFOC_PSC5_TX_SIZE | (FIFOC_PSC5_TX_ADDR << 16); |
| 79 | rfsize = FIFOC_PSC5_RX_SIZE | (FIFOC_PSC5_RX_ADDR << 16); |
| 80 | break; |
| 81 | case 6: |
| 82 | tfsize = FIFOC_PSC6_TX_SIZE | (FIFOC_PSC6_TX_ADDR << 16); |
| 83 | rfsize = FIFOC_PSC6_RX_SIZE | (FIFOC_PSC6_RX_ADDR << 16); |
| 84 | break; |
| 85 | case 7: |
| 86 | tfsize = FIFOC_PSC7_TX_SIZE | (FIFOC_PSC7_TX_ADDR << 16); |
| 87 | rfsize = FIFOC_PSC7_RX_SIZE | (FIFOC_PSC7_RX_ADDR << 16); |
| 88 | break; |
| 89 | case 8: |
| 90 | tfsize = FIFOC_PSC8_TX_SIZE | (FIFOC_PSC8_TX_ADDR << 16); |
| 91 | rfsize = FIFOC_PSC8_RX_SIZE | (FIFOC_PSC8_RX_ADDR << 16); |
| 92 | break; |
| 93 | case 9: |
| 94 | tfsize = FIFOC_PSC9_TX_SIZE | (FIFOC_PSC9_TX_ADDR << 16); |
| 95 | rfsize = FIFOC_PSC9_RX_SIZE | (FIFOC_PSC9_RX_ADDR << 16); |
| 96 | break; |
| 97 | case 10: |
| 98 | tfsize = FIFOC_PSC10_TX_SIZE | (FIFOC_PSC10_TX_ADDR << 16); |
| 99 | rfsize = FIFOC_PSC10_RX_SIZE | (FIFOC_PSC10_RX_ADDR << 16); |
| 100 | break; |
| 101 | case 11: |
| 102 | tfsize = FIFOC_PSC11_TX_SIZE | (FIFOC_PSC11_TX_ADDR << 16); |
| 103 | rfsize = FIFOC_PSC11_RX_SIZE | (FIFOC_PSC11_RX_ADDR << 16); |
| 104 | break; |
| 105 | default: |
| 106 | return; |
| 107 | } |
| 108 | #else |
| 109 | tfsize = CONSOLE_FIFO_TX_SIZE | (CONSOLE_FIFO_TX_ADDR << 16); |
| 110 | rfsize = CONSOLE_FIFO_RX_SIZE | (CONSOLE_FIFO_RX_ADDR << 16); |
| 111 | #endif |
| 112 | out_be32(&psc->tfsize, tfsize); |
| 113 | out_be32(&psc->rfsize, rfsize); |
Rafal Jaworowski | 8993e54 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 114 | |
| 115 | /* enable Tx & Rx FIFO slice */ |
Wolfgang Denk | 843efb1 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 116 | out_be32(&psc->rfcmd, PSC_FIFO_ENABLE_SLICE); |
| 117 | out_be32(&psc->tfcmd, PSC_FIFO_ENABLE_SLICE); |
Rafal Jaworowski | 8993e54 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 118 | |
Wolfgang Denk | 843efb1 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 119 | out_be32(&im->fifoc.fifoc_cmd, FIFOC_DISABLE_CLOCK_GATE); |
Rafal Jaworowski | 8993e54 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 120 | __asm__ volatile ("sync"); |
| 121 | } |
| 122 | |
Anatolij Gustschin | e3b28e6 | 2010-04-24 19:27:05 +0200 | [diff] [blame] | 123 | void serial_setbrg_dev(unsigned int idx) |
Stefan Roese | ba4feae | 2009-06-02 16:53:16 +0200 | [diff] [blame] | 124 | { |
| 125 | volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR; |
Anatolij Gustschin | e3b28e6 | 2010-04-24 19:27:05 +0200 | [diff] [blame] | 126 | volatile psc512x_t *psc = (psc512x_t *) &im->psc[idx]; |
Stefan Roese | ba4feae | 2009-06-02 16:53:16 +0200 | [diff] [blame] | 127 | unsigned long baseclk, div; |
Anatolij Gustschin | e3b28e6 | 2010-04-24 19:27:05 +0200 | [diff] [blame] | 128 | unsigned long baudrate; |
| 129 | char buf[16]; |
| 130 | char *br_env; |
Stefan Roese | ba4feae | 2009-06-02 16:53:16 +0200 | [diff] [blame] | 131 | |
Anatolij Gustschin | e3b28e6 | 2010-04-24 19:27:05 +0200 | [diff] [blame] | 132 | baudrate = gd->baudrate; |
| 133 | if (idx != CONFIG_PSC_CONSOLE) { |
| 134 | /* Allows setting baudrate for other serial devices |
| 135 | * on PSCx using environment. If not specified, use |
| 136 | * the same baudrate as for console. |
| 137 | */ |
| 138 | sprintf(buf, "psc%d_baudrate", idx); |
| 139 | br_env = getenv(buf); |
| 140 | if (br_env) |
| 141 | baudrate = simple_strtoul(br_env, NULL, 10); |
| 142 | |
| 143 | debug("%s: idx %d, baudrate %d\n", __func__, idx, baudrate); |
| 144 | } |
| 145 | |
| 146 | /* calculate divisor for setting PSC CTUR and CTLR registers */ |
Stefan Roese | ba4feae | 2009-06-02 16:53:16 +0200 | [diff] [blame] | 147 | baseclk = (gd->ips_clk + 8) / 16; |
Anatolij Gustschin | e3b28e6 | 2010-04-24 19:27:05 +0200 | [diff] [blame] | 148 | div = (baseclk + (baudrate / 2)) / baudrate; |
Stefan Roese | ba4feae | 2009-06-02 16:53:16 +0200 | [diff] [blame] | 149 | |
| 150 | out_8(&psc->ctur, (div >> 8) & 0xff); |
| 151 | out_8(&psc->ctlr, div & 0xff); /* set baudrate */ |
| 152 | } |
| 153 | |
Anatolij Gustschin | e3b28e6 | 2010-04-24 19:27:05 +0200 | [diff] [blame] | 154 | int serial_init_dev(unsigned int idx) |
Rafal Jaworowski | 8993e54 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 155 | { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 156 | volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR; |
Anatolij Gustschin | e3b28e6 | 2010-04-24 19:27:05 +0200 | [diff] [blame] | 157 | volatile psc512x_t *psc = (psc512x_t *) &im->psc[idx]; |
| 158 | #if defined(CONFIG_SERIAL_MULTI) |
| 159 | u32 reg; |
| 160 | |
| 161 | reg = in_be32(&im->clk.sccr[0]); |
| 162 | out_be32(&im->clk.sccr[0], reg | CLOCK_SCCR1_PSC_EN(idx)); |
| 163 | #endif |
Rafal Jaworowski | 8993e54 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 164 | |
| 165 | fifo_init (psc); |
| 166 | |
| 167 | /* set MR register to point to MR1 */ |
Wolfgang Denk | 843efb1 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 168 | out_8(&psc->command, PSC_SEL_MODE_REG_1); |
Rafal Jaworowski | 8993e54 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 169 | |
| 170 | /* disable Tx/Rx */ |
Wolfgang Denk | 843efb1 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 171 | out_8(&psc->command, PSC_TX_DISABLE | PSC_RX_DISABLE); |
Rafal Jaworowski | 8993e54 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 172 | |
| 173 | /* choose the prescaler by 16 for the Tx/Rx clock generation */ |
Wolfgang Denk | 843efb1 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 174 | out_be16(&psc->psc_clock_select, 0xdd00); |
Rafal Jaworowski | 8993e54 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 175 | |
| 176 | /* switch to UART mode */ |
Wolfgang Denk | 843efb1 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 177 | out_be32(&psc->sicr, 0); |
Rafal Jaworowski | 8993e54 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 178 | |
| 179 | /* mode register points to mr1 */ |
| 180 | /* configure parity, bit length and so on in mode register 1*/ |
Wolfgang Denk | 843efb1 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 181 | out_8(&psc->mode, PSC_MODE_8_BITS | PSC_MODE_PARNONE); |
Rafal Jaworowski | 8993e54 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 182 | /* now, mode register points to mr2 */ |
Wolfgang Denk | 843efb1 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 183 | out_8(&psc->mode, PSC_MODE_1_STOPBIT); |
Rafal Jaworowski | 8993e54 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 184 | |
Rafal Jaworowski | 8993e54 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 185 | /* set baudrate */ |
Anatolij Gustschin | e3b28e6 | 2010-04-24 19:27:05 +0200 | [diff] [blame] | 186 | serial_setbrg_dev(idx); |
Rafal Jaworowski | 8993e54 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 187 | |
| 188 | /* disable all interrupts */ |
Wolfgang Denk | 843efb1 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 189 | out_be16(&psc->psc_imr, 0); |
Rafal Jaworowski | 8993e54 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 190 | |
| 191 | /* reset and enable Rx/Tx */ |
Wolfgang Denk | 843efb1 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 192 | out_8(&psc->command, PSC_RST_RX); |
| 193 | out_8(&psc->command, PSC_RST_TX); |
| 194 | out_8(&psc->command, PSC_RX_ENABLE | PSC_TX_ENABLE); |
Rafal Jaworowski | 8993e54 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 195 | |
| 196 | return 0; |
| 197 | } |
| 198 | |
Anatolij Gustschin | e3b28e6 | 2010-04-24 19:27:05 +0200 | [diff] [blame] | 199 | int serial_uninit_dev(unsigned int idx) |
| 200 | { |
| 201 | volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR; |
| 202 | volatile psc512x_t *psc = (psc512x_t *) &im->psc[idx]; |
| 203 | u32 reg; |
| 204 | |
| 205 | out_8(&psc->command, PSC_RX_DISABLE | PSC_TX_DISABLE); |
| 206 | reg = in_be32(&im->clk.sccr[0]); |
| 207 | reg &= ~CLOCK_SCCR1_PSC_EN(idx); |
| 208 | out_be32(&im->clk.sccr[0], reg); |
| 209 | |
| 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | void serial_putc_dev(unsigned int idx, const char c) |
Rafal Jaworowski | 8993e54 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 214 | { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 215 | volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR; |
Anatolij Gustschin | e3b28e6 | 2010-04-24 19:27:05 +0200 | [diff] [blame] | 216 | volatile psc512x_t *psc = (psc512x_t *) &im->psc[idx]; |
Rafal Jaworowski | 8993e54 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 217 | |
| 218 | if (c == '\n') |
Anatolij Gustschin | e3b28e6 | 2010-04-24 19:27:05 +0200 | [diff] [blame] | 219 | serial_putc_dev(idx, '\r'); |
Rafal Jaworowski | 8993e54 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 220 | |
| 221 | /* Wait for last character to go. */ |
Wolfgang Denk | 843efb1 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 222 | while (!(in_be16(&psc->psc_status) & PSC_SR_TXEMP)) |
Rafal Jaworowski | 8993e54 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 223 | ; |
| 224 | |
Detlev Zundel | 57ae8a5 | 2010-01-21 17:55:58 +0100 | [diff] [blame] | 225 | out_8(&psc->tfdata_8, c); |
Rafal Jaworowski | 8993e54 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 226 | } |
| 227 | |
Anatolij Gustschin | e3b28e6 | 2010-04-24 19:27:05 +0200 | [diff] [blame] | 228 | void serial_putc_raw_dev(unsigned int idx, const char c) |
Rafal Jaworowski | 8993e54 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 229 | { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 230 | volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR; |
Anatolij Gustschin | e3b28e6 | 2010-04-24 19:27:05 +0200 | [diff] [blame] | 231 | volatile psc512x_t *psc = (psc512x_t *) &im->psc[idx]; |
Rafal Jaworowski | 8993e54 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 232 | |
| 233 | /* Wait for last character to go. */ |
Wolfgang Denk | 843efb1 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 234 | while (!(in_be16(&psc->psc_status) & PSC_SR_TXEMP)) |
Rafal Jaworowski | 8993e54 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 235 | ; |
| 236 | |
Detlev Zundel | 57ae8a5 | 2010-01-21 17:55:58 +0100 | [diff] [blame] | 237 | out_8(&psc->tfdata_8, c); |
Rafal Jaworowski | 8993e54 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 238 | } |
| 239 | |
Anatolij Gustschin | e3b28e6 | 2010-04-24 19:27:05 +0200 | [diff] [blame] | 240 | void serial_puts_dev(unsigned int idx, const char *s) |
Rafal Jaworowski | 8993e54 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 241 | { |
Anatolij Gustschin | e3b28e6 | 2010-04-24 19:27:05 +0200 | [diff] [blame] | 242 | while (*s) |
| 243 | serial_putc_dev(idx, *s++); |
Rafal Jaworowski | 8993e54 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 244 | } |
| 245 | |
Anatolij Gustschin | e3b28e6 | 2010-04-24 19:27:05 +0200 | [diff] [blame] | 246 | int serial_getc_dev(unsigned int idx) |
Rafal Jaworowski | 8993e54 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 247 | { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 248 | volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR; |
Anatolij Gustschin | e3b28e6 | 2010-04-24 19:27:05 +0200 | [diff] [blame] | 249 | volatile psc512x_t *psc = (psc512x_t *) &im->psc[idx]; |
Rafal Jaworowski | 8993e54 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 250 | |
| 251 | /* Wait for a character to arrive. */ |
Wolfgang Denk | 843efb1 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 252 | while (in_be32(&psc->rfstat) & PSC_FIFO_EMPTY) |
Rafal Jaworowski | 8993e54 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 253 | ; |
| 254 | |
Detlev Zundel | 57ae8a5 | 2010-01-21 17:55:58 +0100 | [diff] [blame] | 255 | return in_8(&psc->rfdata_8); |
Rafal Jaworowski | 8993e54 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 256 | } |
| 257 | |
Anatolij Gustschin | e3b28e6 | 2010-04-24 19:27:05 +0200 | [diff] [blame] | 258 | int serial_tstc_dev(unsigned int idx) |
Rafal Jaworowski | 8993e54 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 259 | { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 260 | volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR; |
Anatolij Gustschin | e3b28e6 | 2010-04-24 19:27:05 +0200 | [diff] [blame] | 261 | volatile psc512x_t *psc = (psc512x_t *) &im->psc[idx]; |
Rafal Jaworowski | 8993e54 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 262 | |
Wolfgang Denk | 843efb1 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 263 | return !(in_be32(&psc->rfstat) & PSC_FIFO_EMPTY); |
Rafal Jaworowski | 8993e54 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 264 | } |
| 265 | |
Anatolij Gustschin | e3b28e6 | 2010-04-24 19:27:05 +0200 | [diff] [blame] | 266 | void serial_setrts_dev(unsigned int idx, int s) |
Rafal Jaworowski | 8993e54 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 267 | { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 268 | volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR; |
Anatolij Gustschin | e3b28e6 | 2010-04-24 19:27:05 +0200 | [diff] [blame] | 269 | volatile psc512x_t *psc = (psc512x_t *) &im->psc[idx]; |
Rafal Jaworowski | 8993e54 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 270 | |
| 271 | if (s) { |
| 272 | /* Assert RTS (become LOW) */ |
Wolfgang Denk | 843efb1 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 273 | out_8(&psc->op1, 0x1); |
Rafal Jaworowski | 8993e54 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 274 | } |
| 275 | else { |
| 276 | /* Negate RTS (become HIGH) */ |
Wolfgang Denk | 843efb1 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 277 | out_8(&psc->op0, 0x1); |
Rafal Jaworowski | 8993e54 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | |
Anatolij Gustschin | e3b28e6 | 2010-04-24 19:27:05 +0200 | [diff] [blame] | 281 | int serial_getcts_dev(unsigned int idx) |
Rafal Jaworowski | 8993e54 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 282 | { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 283 | volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR; |
Anatolij Gustschin | e3b28e6 | 2010-04-24 19:27:05 +0200 | [diff] [blame] | 284 | volatile psc512x_t *psc = (psc512x_t *) &im->psc[idx]; |
Rafal Jaworowski | 8993e54 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 285 | |
Wolfgang Denk | 843efb1 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 286 | return (in_8(&psc->ip) & 0x1) ? 0 : 1; |
Rafal Jaworowski | 8993e54 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 287 | } |
Anatolij Gustschin | e3b28e6 | 2010-04-24 19:27:05 +0200 | [diff] [blame] | 288 | #endif /* CONFIG_PSC_CONSOLE || CONFIG_SERIAL_MULTI */ |
| 289 | |
| 290 | #if defined(CONFIG_SERIAL_MULTI) |
| 291 | |
| 292 | #define DECLARE_PSC_SERIAL_FUNCTIONS(port) \ |
| 293 | int serial##port##_init(void) \ |
| 294 | { \ |
| 295 | return serial_init_dev(port); \ |
| 296 | } \ |
| 297 | int serial##port##_uninit(void) \ |
| 298 | { \ |
| 299 | return serial_uninit_dev(port); \ |
| 300 | } \ |
| 301 | void serial##port##_setbrg(void) \ |
| 302 | { \ |
| 303 | serial_setbrg_dev(port); \ |
| 304 | } \ |
| 305 | int serial##port##_getc(void) \ |
| 306 | { \ |
| 307 | return serial_getc_dev(port); \ |
| 308 | } \ |
| 309 | int serial##port##_tstc(void) \ |
| 310 | { \ |
| 311 | return serial_tstc_dev(port); \ |
| 312 | } \ |
| 313 | void serial##port##_putc(const char c) \ |
| 314 | { \ |
| 315 | serial_putc_dev(port, c); \ |
| 316 | } \ |
| 317 | void serial##port##_puts(const char *s) \ |
| 318 | { \ |
| 319 | serial_puts_dev(port, s); \ |
| 320 | } |
| 321 | |
| 322 | #define INIT_PSC_SERIAL_STRUCTURE(port, name, bus) { \ |
| 323 | name, \ |
| 324 | bus, \ |
| 325 | serial##port##_init, \ |
| 326 | serial##port##_uninit, \ |
| 327 | serial##port##_setbrg, \ |
| 328 | serial##port##_getc, \ |
| 329 | serial##port##_tstc, \ |
| 330 | serial##port##_putc, \ |
| 331 | serial##port##_puts, \ |
| 332 | } |
| 333 | |
| 334 | #if defined(CONFIG_SYS_PSC1) |
| 335 | DECLARE_PSC_SERIAL_FUNCTIONS(1); |
| 336 | struct serial_device serial1_device = |
| 337 | INIT_PSC_SERIAL_STRUCTURE(1, "psc1", "UART1"); |
| 338 | #endif |
| 339 | |
| 340 | #if defined(CONFIG_SYS_PSC3) |
| 341 | DECLARE_PSC_SERIAL_FUNCTIONS(3); |
| 342 | struct serial_device serial3_device = |
| 343 | INIT_PSC_SERIAL_STRUCTURE(3, "psc3", "UART3"); |
| 344 | #endif |
| 345 | |
| 346 | #if defined(CONFIG_SYS_PSC4) |
| 347 | DECLARE_PSC_SERIAL_FUNCTIONS(4); |
| 348 | struct serial_device serial4_device = |
| 349 | INIT_PSC_SERIAL_STRUCTURE(4, "psc4", "UART4"); |
| 350 | #endif |
| 351 | |
| 352 | #if defined(CONFIG_SYS_PSC6) |
| 353 | DECLARE_PSC_SERIAL_FUNCTIONS(6); |
| 354 | struct serial_device serial6_device = |
| 355 | INIT_PSC_SERIAL_STRUCTURE(6, "psc6", "UART6"); |
| 356 | #endif |
| 357 | |
Mike Frysinger | 6c768ca | 2011-04-29 18:03:29 +0000 | [diff] [blame^] | 358 | __weak struct serial_device *default_serial_console(void) |
| 359 | { |
| 360 | #if (CONFIG_PSC_CONSOLE == 3) |
| 361 | return &serial3_device; |
| 362 | #elif (CONFIG_PSC_CONSOLE == 6) |
| 363 | return &serial6_device; |
| 364 | #else |
| 365 | #error "invalid CONFIG_PSC_CONSOLE" |
| 366 | #endif |
| 367 | } |
| 368 | |
Anatolij Gustschin | e3b28e6 | 2010-04-24 19:27:05 +0200 | [diff] [blame] | 369 | #else |
| 370 | |
| 371 | void serial_setbrg(void) |
| 372 | { |
| 373 | serial_setbrg_dev(CONFIG_PSC_CONSOLE); |
| 374 | } |
| 375 | |
| 376 | int serial_init(void) |
| 377 | { |
| 378 | return serial_init_dev(CONFIG_PSC_CONSOLE); |
| 379 | } |
| 380 | |
| 381 | void serial_putc(const char c) |
| 382 | { |
| 383 | serial_putc_dev(CONFIG_PSC_CONSOLE, c); |
| 384 | } |
| 385 | |
| 386 | void serial_putc_raw(const char c) |
| 387 | { |
| 388 | serial_putc_raw_dev(CONFIG_PSC_CONSOLE, c); |
| 389 | } |
| 390 | |
| 391 | void serial_puts(const char *s) |
| 392 | { |
| 393 | serial_puts_dev(CONFIG_PSC_CONSOLE, s); |
| 394 | } |
| 395 | |
| 396 | int serial_getc(void) |
| 397 | { |
| 398 | return serial_getc_dev(CONFIG_PSC_CONSOLE); |
| 399 | } |
| 400 | |
| 401 | int serial_tstc(void) |
| 402 | { |
| 403 | return serial_tstc_dev(CONFIG_PSC_CONSOLE); |
| 404 | } |
| 405 | |
| 406 | void serial_setrts(int s) |
| 407 | { |
| 408 | return serial_setrts_dev(CONFIG_PSC_CONSOLE, s); |
| 409 | } |
| 410 | |
| 411 | int serial_getcts(void) |
| 412 | { |
| 413 | return serial_getcts_dev(CONFIG_PSC_CONSOLE); |
| 414 | } |
Rafal Jaworowski | 8993e54 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 415 | #endif /* CONFIG_PSC_CONSOLE */ |
Anatolij Gustschin | 8e234e3 | 2010-04-24 19:27:06 +0200 | [diff] [blame] | 416 | |
| 417 | #if defined(CONFIG_SERIAL_MULTI) |
| 418 | #include <stdio_dev.h> |
| 419 | /* |
| 420 | * Routines for communication with serial devices over PSC |
| 421 | */ |
| 422 | /* Bitfield for initialized PSCs */ |
| 423 | static unsigned int initialized; |
| 424 | |
| 425 | struct stdio_dev *open_port(int num, int baudrate) |
| 426 | { |
| 427 | struct stdio_dev *port; |
| 428 | char env_var[16]; |
| 429 | char env_val[10]; |
| 430 | char name[7]; |
| 431 | |
| 432 | if (num < 0 || num > 11) |
| 433 | return NULL; |
| 434 | |
| 435 | sprintf(name, "psc%d", num); |
| 436 | port = stdio_get_by_name(name); |
| 437 | if (!port) |
| 438 | return NULL; |
| 439 | |
| 440 | if (!test_bit(num, &initialized)) { |
| 441 | sprintf(env_var, "psc%d_baudrate", num); |
| 442 | sprintf(env_val, "%d", baudrate); |
| 443 | setenv(env_var, env_val); |
| 444 | |
| 445 | if (port->start()) |
| 446 | return NULL; |
| 447 | |
| 448 | set_bit(num, &initialized); |
| 449 | } |
| 450 | |
| 451 | return port; |
| 452 | } |
| 453 | |
| 454 | int close_port(int num) |
| 455 | { |
| 456 | struct stdio_dev *port; |
| 457 | int ret; |
| 458 | char name[7]; |
| 459 | |
| 460 | if (num < 0 || num > 11) |
| 461 | return -1; |
| 462 | |
| 463 | sprintf(name, "psc%d", num); |
| 464 | port = stdio_get_by_name(name); |
| 465 | if (!port) |
| 466 | return -1; |
| 467 | |
| 468 | ret = port->stop(); |
| 469 | clear_bit(num, &initialized); |
| 470 | |
| 471 | return ret; |
| 472 | } |
| 473 | |
| 474 | int write_port(struct stdio_dev *port, char *buf) |
| 475 | { |
| 476 | if (!port || !buf) |
| 477 | return -1; |
| 478 | |
| 479 | port->puts(buf); |
| 480 | |
| 481 | return 0; |
| 482 | } |
| 483 | |
| 484 | int read_port(struct stdio_dev *port, char *buf, int size) |
| 485 | { |
| 486 | int cnt = 0; |
| 487 | |
| 488 | if (!port || !buf) |
| 489 | return -1; |
| 490 | |
| 491 | if (!size) |
| 492 | return 0; |
| 493 | |
| 494 | while (port->tstc()) { |
| 495 | buf[cnt++] = port->getc(); |
| 496 | if (cnt > size) |
| 497 | break; |
| 498 | } |
| 499 | |
| 500 | return cnt; |
| 501 | } |
| 502 | #endif /* CONFIG_SERIAL_MULTI */ |