wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2003 |
| 3 | * Martin Winistoerfer, martinwinistoerfer@gmx.ch. |
| 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 |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 20 | * Foundation, |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 21 | */ |
| 22 | |
| 23 | /* |
| 24 | * File: serial.c |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 25 | * |
| 26 | * Discription: Serial interface driver for SCI1 and SCI2. |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 27 | * Since this code will be called from ROM use |
| 28 | * only non-static local variables. |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 29 | * |
| 30 | */ |
| 31 | |
| 32 | #include <common.h> |
| 33 | #include <watchdog.h> |
| 34 | #include <command.h> |
| 35 | #include <mpc5xx.h> |
Marek Vasut | 58698e7 | 2012-09-13 01:26:42 +0200 | [diff] [blame^] | 36 | #include <serial.h> |
| 37 | #include <linux/compiler.h> |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 38 | |
Wolfgang Denk | 6405a15 | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 39 | DECLARE_GLOBAL_DATA_PTR; |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 40 | |
| 41 | /* |
Marek Vasut | 58698e7 | 2012-09-13 01:26:42 +0200 | [diff] [blame^] | 42 | * Local functions |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 43 | */ |
| 44 | |
Marek Vasut | 58698e7 | 2012-09-13 01:26:42 +0200 | [diff] [blame^] | 45 | static int ready_to_send(void) |
| 46 | { |
| 47 | volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR; |
| 48 | volatile short status; |
| 49 | |
| 50 | do { |
| 51 | #if defined(CONFIG_5xx_CONS_SCI1) |
| 52 | status = immr->im_qsmcm.qsmcm_sc1sr; |
| 53 | #else |
| 54 | status = immr->im_qsmcm.qsmcm_sc2sr; |
| 55 | #endif |
| 56 | |
| 57 | #if defined(CONFIG_WATCHDOG) |
| 58 | reset_5xx_watchdog (immr); |
| 59 | #endif |
| 60 | } while ((status & SCI_TDRE) == 0); |
| 61 | return 1; |
| 62 | |
| 63 | } |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 64 | |
| 65 | /* |
| 66 | * Minimal global serial functions needed to use one of the SCI modules. |
| 67 | */ |
| 68 | |
Marek Vasut | 58698e7 | 2012-09-13 01:26:42 +0200 | [diff] [blame^] | 69 | static int mpc5xx_serial_init(void) |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 70 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 71 | volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR; |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 72 | |
| 73 | serial_setbrg(); |
| 74 | |
| 75 | #if defined(CONFIG_5xx_CONS_SCI1) |
| 76 | /* 10-Bit, 1 start bit, 8 data bit, no parity, 1 stop bit */ |
| 77 | immr->im_qsmcm.qsmcm_scc1r1 = SCI_M_10; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 78 | immr->im_qsmcm.qsmcm_scc1r1 = SCI_TE | SCI_RE; |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 79 | #else |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 80 | immr->im_qsmcm.qsmcm_scc2r1 = SCI_M_10; |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 81 | immr->im_qsmcm.qsmcm_scc2r1 = SCI_TE | SCI_RE; |
| 82 | #endif |
| 83 | return 0; |
| 84 | } |
| 85 | |
Marek Vasut | 58698e7 | 2012-09-13 01:26:42 +0200 | [diff] [blame^] | 86 | static void mpc5xx_serial_putc(const char c) |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 87 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 88 | volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 89 | |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 90 | /* Test for completition */ |
| 91 | if(ready_to_send()) { |
| 92 | #if defined(CONFIG_5xx_CONS_SCI1) |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 93 | immr->im_qsmcm.qsmcm_sc1dr = (short)c; |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 94 | #else |
| 95 | immr->im_qsmcm.qsmcm_sc2dr = (short)c; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 96 | #endif |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 97 | if(c == '\n') { |
| 98 | if(ready_to_send()); |
| 99 | #if defined(CONFIG_5xx_CONS_SCI1) |
| 100 | immr->im_qsmcm.qsmcm_sc1dr = (short)'\r'; |
| 101 | #else |
| 102 | immr->im_qsmcm.qsmcm_sc2dr = (short)'\r'; |
| 103 | #endif |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
Marek Vasut | 58698e7 | 2012-09-13 01:26:42 +0200 | [diff] [blame^] | 108 | static int mpc5xx_serial_getc(void) |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 109 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 110 | volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR; |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 111 | volatile short status; |
| 112 | unsigned char tmp; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 113 | |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 114 | /* New data ? */ |
| 115 | do { |
| 116 | #if defined(CONFIG_5xx_CONS_SCI1) |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 117 | status = immr->im_qsmcm.qsmcm_sc1sr; |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 118 | #else |
| 119 | status = immr->im_qsmcm.qsmcm_sc2sr; |
| 120 | #endif |
| 121 | |
| 122 | #if defined(CONFIG_WATCHDOG) |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 123 | reset_5xx_watchdog (immr); |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 124 | #endif |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 125 | } while ((status & SCI_RDRF) == 0); |
| 126 | |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 127 | /* Read data */ |
| 128 | #if defined(CONFIG_5xx_CONS_SCI1) |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 129 | tmp = (unsigned char)(immr->im_qsmcm.qsmcm_sc1dr & SCI_SCXDR_MK); |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 130 | #else |
| 131 | tmp = (unsigned char)( immr->im_qsmcm.qsmcm_sc2dr & SCI_SCXDR_MK); |
| 132 | #endif |
| 133 | return tmp; |
| 134 | } |
| 135 | |
Marek Vasut | 58698e7 | 2012-09-13 01:26:42 +0200 | [diff] [blame^] | 136 | static int mpc5xx_serial_tstc(void) |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 137 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 138 | volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 139 | short status; |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 140 | |
| 141 | /* New data character ? */ |
| 142 | #if defined(CONFIG_5xx_CONS_SCI1) |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 143 | status = immr->im_qsmcm.qsmcm_sc1sr; |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 144 | #else |
| 145 | status = immr->im_qsmcm.qsmcm_sc2sr; |
| 146 | #endif |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 147 | return (status & SCI_RDRF); |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Marek Vasut | 58698e7 | 2012-09-13 01:26:42 +0200 | [diff] [blame^] | 150 | static void mpc5xx_serial_setbrg(void) |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 151 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 152 | volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR; |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 153 | short scxbr; |
| 154 | |
| 155 | /* Set baudrate */ |
| 156 | scxbr = (gd->cpu_clk / (32 * gd->baudrate)); |
| 157 | #if defined(CONFIG_5xx_CONS_SCI1) |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 158 | immr->im_qsmcm.qsmcm_scc1r0 = (scxbr & SCI_SCXBR_MK); |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 159 | #else |
| 160 | immr->im_qsmcm.qsmcm_scc2r0 = (scxbr & SCI_SCXBR_MK); |
| 161 | #endif |
| 162 | } |
| 163 | |
Marek Vasut | 58698e7 | 2012-09-13 01:26:42 +0200 | [diff] [blame^] | 164 | static void mpc5xx_serial_puts(const char *s) |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 165 | { |
| 166 | while (*s) { |
| 167 | serial_putc(*s); |
| 168 | ++s; |
| 169 | } |
| 170 | } |
| 171 | |
Marek Vasut | 58698e7 | 2012-09-13 01:26:42 +0200 | [diff] [blame^] | 172 | #ifdef CONFIG_SERIAL_MULTI |
| 173 | static struct serial_device mpc5xx_serial_drv = { |
| 174 | .name = "mpc5xx_serial", |
| 175 | .start = mpc5xx_serial_init, |
| 176 | .stop = NULL, |
| 177 | .setbrg = mpc5xx_serial_setbrg, |
| 178 | .putc = mpc5xx_serial_putc, |
| 179 | .puts = mpc5xx_serial_puts, |
| 180 | .getc = mpc5xx_serial_getc, |
| 181 | .tstc = mpc5xx_serial_tstc, |
| 182 | }; |
| 183 | |
| 184 | void mpc5xx_serial_initialize(void) |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 185 | { |
Marek Vasut | 58698e7 | 2012-09-13 01:26:42 +0200 | [diff] [blame^] | 186 | serial_register(&mpc5xx_serial_drv); |
| 187 | } |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 188 | |
Marek Vasut | 58698e7 | 2012-09-13 01:26:42 +0200 | [diff] [blame^] | 189 | __weak struct serial_device *default_serial_console(void) |
| 190 | { |
| 191 | return &mpc5xx_serial_drv; |
| 192 | } |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 193 | #else |
Marek Vasut | 58698e7 | 2012-09-13 01:26:42 +0200 | [diff] [blame^] | 194 | int serial_init(void) |
| 195 | { |
| 196 | return mpc5xx_serial_init(); |
| 197 | } |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 198 | |
Marek Vasut | 58698e7 | 2012-09-13 01:26:42 +0200 | [diff] [blame^] | 199 | void serial_setbrg(void) |
| 200 | { |
| 201 | mpc5xx_serial_setbrg(); |
| 202 | } |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 203 | |
Marek Vasut | 58698e7 | 2012-09-13 01:26:42 +0200 | [diff] [blame^] | 204 | void serial_putc(const char c) |
| 205 | { |
| 206 | mpc5xx_serial_putc(c); |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 207 | } |
Marek Vasut | 58698e7 | 2012-09-13 01:26:42 +0200 | [diff] [blame^] | 208 | |
| 209 | void serial_puts(const char *s) |
| 210 | { |
| 211 | mpc5xx_serial_puts(s); |
| 212 | } |
| 213 | |
| 214 | int serial_getc(void) |
| 215 | { |
| 216 | return mpc5xx_serial_getc(); |
| 217 | } |
| 218 | |
| 219 | int serial_tstc(void) |
| 220 | { |
| 221 | return mpc5xx_serial_tstc(); |
| 222 | } |
| 223 | #endif |