wdenk | 0db5bca | 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 | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 20 | * Foundation, |
wdenk | 0db5bca | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 21 | */ |
| 22 | |
| 23 | /* |
| 24 | * File: serial.c |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 25 | * |
| 26 | * Discription: Serial interface driver for SCI1 and SCI2. |
Wolfgang Denk | 53677ef | 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 | 0db5bca | 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 | b57c652 | 2012-09-13 01:26:42 +0200 | [diff] [blame] | 36 | #include <serial.h> |
| 37 | #include <linux/compiler.h> |
wdenk | 0db5bca | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 38 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 39 | DECLARE_GLOBAL_DATA_PTR; |
wdenk | 0db5bca | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 40 | |
| 41 | /* |
Marek Vasut | b57c652 | 2012-09-13 01:26:42 +0200 | [diff] [blame] | 42 | * Local functions |
wdenk | 0db5bca | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 43 | */ |
| 44 | |
Marek Vasut | b57c652 | 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 | 0db5bca | 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 | b57c652 | 2012-09-13 01:26:42 +0200 | [diff] [blame] | 69 | static int mpc5xx_serial_init(void) |
wdenk | 0db5bca | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 70 | { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 71 | volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR; |
wdenk | 0db5bca | 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 | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 78 | immr->im_qsmcm.qsmcm_scc1r1 = SCI_TE | SCI_RE; |
wdenk | 0db5bca | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 79 | #else |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 80 | immr->im_qsmcm.qsmcm_scc2r1 = SCI_M_10; |
wdenk | 0db5bca | 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 | b57c652 | 2012-09-13 01:26:42 +0200 | [diff] [blame] | 86 | static void mpc5xx_serial_putc(const char c) |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 87 | { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 88 | volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 89 | |
wdenk | 0db5bca | 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 | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 93 | immr->im_qsmcm.qsmcm_sc1dr = (short)c; |
wdenk | 0db5bca | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 94 | #else |
| 95 | immr->im_qsmcm.qsmcm_sc2dr = (short)c; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 96 | #endif |
wdenk | 0db5bca | 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 | b57c652 | 2012-09-13 01:26:42 +0200 | [diff] [blame] | 108 | static int mpc5xx_serial_getc(void) |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 109 | { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 110 | volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR; |
wdenk | 0db5bca | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 111 | volatile short status; |
| 112 | unsigned char tmp; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 113 | |
wdenk | 0db5bca | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 114 | /* New data ? */ |
| 115 | do { |
| 116 | #if defined(CONFIG_5xx_CONS_SCI1) |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 117 | status = immr->im_qsmcm.qsmcm_sc1sr; |
wdenk | 0db5bca | 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 | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 123 | reset_5xx_watchdog (immr); |
wdenk | 0db5bca | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 124 | #endif |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 125 | } while ((status & SCI_RDRF) == 0); |
| 126 | |
wdenk | 0db5bca | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 127 | /* Read data */ |
| 128 | #if defined(CONFIG_5xx_CONS_SCI1) |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 129 | tmp = (unsigned char)(immr->im_qsmcm.qsmcm_sc1dr & SCI_SCXDR_MK); |
wdenk | 0db5bca | 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 | b57c652 | 2012-09-13 01:26:42 +0200 | [diff] [blame] | 136 | static int mpc5xx_serial_tstc(void) |
wdenk | 0db5bca | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 137 | { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 138 | volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 139 | short status; |
wdenk | 0db5bca | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 140 | |
| 141 | /* New data character ? */ |
| 142 | #if defined(CONFIG_5xx_CONS_SCI1) |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 143 | status = immr->im_qsmcm.qsmcm_sc1sr; |
wdenk | 0db5bca | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 144 | #else |
| 145 | status = immr->im_qsmcm.qsmcm_sc2sr; |
| 146 | #endif |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 147 | return (status & SCI_RDRF); |
wdenk | 0db5bca | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Marek Vasut | b57c652 | 2012-09-13 01:26:42 +0200 | [diff] [blame] | 150 | static void mpc5xx_serial_setbrg(void) |
wdenk | 0db5bca | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 151 | { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 152 | volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR; |
wdenk | 0db5bca | 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 | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 158 | immr->im_qsmcm.qsmcm_scc1r0 = (scxbr & SCI_SCXBR_MK); |
wdenk | 0db5bca | 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 | b57c652 | 2012-09-13 01:26:42 +0200 | [diff] [blame] | 164 | static struct serial_device mpc5xx_serial_drv = { |
| 165 | .name = "mpc5xx_serial", |
| 166 | .start = mpc5xx_serial_init, |
| 167 | .stop = NULL, |
| 168 | .setbrg = mpc5xx_serial_setbrg, |
| 169 | .putc = mpc5xx_serial_putc, |
Marek Vasut | ec3fd68 | 2012-10-06 14:07:02 +0000 | [diff] [blame] | 170 | .puts = default_serial_puts, |
Marek Vasut | b57c652 | 2012-09-13 01:26:42 +0200 | [diff] [blame] | 171 | .getc = mpc5xx_serial_getc, |
| 172 | .tstc = mpc5xx_serial_tstc, |
| 173 | }; |
| 174 | |
| 175 | void mpc5xx_serial_initialize(void) |
wdenk | 0db5bca | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 176 | { |
Marek Vasut | b57c652 | 2012-09-13 01:26:42 +0200 | [diff] [blame] | 177 | serial_register(&mpc5xx_serial_drv); |
wdenk | 0db5bca | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 178 | } |
Marek Vasut | b57c652 | 2012-09-13 01:26:42 +0200 | [diff] [blame] | 179 | |
| 180 | __weak struct serial_device *default_serial_console(void) |
| 181 | { |
| 182 | return &mpc5xx_serial_drv; |
| 183 | } |