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