wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2003 |
| 3 | * Martin Winistoerfer, martinwinistoerfer@gmx.ch. |
| 4 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * File: serial.c |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 10 | * |
| 11 | * Discription: Serial interface driver for SCI1 and SCI2. |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 12 | * Since this code will be called from ROM use |
| 13 | * only non-static local variables. |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 14 | * |
| 15 | */ |
| 16 | |
| 17 | #include <common.h> |
| 18 | #include <watchdog.h> |
| 19 | #include <command.h> |
| 20 | #include <mpc5xx.h> |
Marek Vasut | 58698e7 | 2012-09-13 01:26:42 +0200 | [diff] [blame] | 21 | #include <serial.h> |
| 22 | #include <linux/compiler.h> |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 23 | |
Wolfgang Denk | 6405a15 | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 24 | DECLARE_GLOBAL_DATA_PTR; |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 25 | |
| 26 | /* |
Marek Vasut | 58698e7 | 2012-09-13 01:26:42 +0200 | [diff] [blame] | 27 | * Local functions |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 28 | */ |
| 29 | |
Marek Vasut | 58698e7 | 2012-09-13 01:26:42 +0200 | [diff] [blame] | 30 | static int ready_to_send(void) |
| 31 | { |
| 32 | volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR; |
| 33 | volatile short status; |
| 34 | |
| 35 | do { |
| 36 | #if defined(CONFIG_5xx_CONS_SCI1) |
| 37 | status = immr->im_qsmcm.qsmcm_sc1sr; |
| 38 | #else |
| 39 | status = immr->im_qsmcm.qsmcm_sc2sr; |
| 40 | #endif |
| 41 | |
| 42 | #if defined(CONFIG_WATCHDOG) |
| 43 | reset_5xx_watchdog (immr); |
| 44 | #endif |
| 45 | } while ((status & SCI_TDRE) == 0); |
| 46 | return 1; |
| 47 | |
| 48 | } |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 49 | |
| 50 | /* |
| 51 | * Minimal global serial functions needed to use one of the SCI modules. |
| 52 | */ |
| 53 | |
Marek Vasut | 58698e7 | 2012-09-13 01:26:42 +0200 | [diff] [blame] | 54 | static int mpc5xx_serial_init(void) |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 55 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 56 | volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR; |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 57 | |
| 58 | serial_setbrg(); |
| 59 | |
| 60 | #if defined(CONFIG_5xx_CONS_SCI1) |
| 61 | /* 10-Bit, 1 start bit, 8 data bit, no parity, 1 stop bit */ |
| 62 | immr->im_qsmcm.qsmcm_scc1r1 = SCI_M_10; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 63 | immr->im_qsmcm.qsmcm_scc1r1 = SCI_TE | SCI_RE; |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 64 | #else |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 65 | immr->im_qsmcm.qsmcm_scc2r1 = SCI_M_10; |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 66 | immr->im_qsmcm.qsmcm_scc2r1 = SCI_TE | SCI_RE; |
| 67 | #endif |
| 68 | return 0; |
| 69 | } |
| 70 | |
Marek Vasut | 58698e7 | 2012-09-13 01:26:42 +0200 | [diff] [blame] | 71 | static void mpc5xx_serial_putc(const char c) |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 72 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 73 | volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 74 | |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 75 | /* Test for completition */ |
| 76 | if(ready_to_send()) { |
| 77 | #if defined(CONFIG_5xx_CONS_SCI1) |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 78 | immr->im_qsmcm.qsmcm_sc1dr = (short)c; |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 79 | #else |
| 80 | immr->im_qsmcm.qsmcm_sc2dr = (short)c; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 81 | #endif |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 82 | if(c == '\n') { |
| 83 | if(ready_to_send()); |
| 84 | #if defined(CONFIG_5xx_CONS_SCI1) |
| 85 | immr->im_qsmcm.qsmcm_sc1dr = (short)'\r'; |
| 86 | #else |
| 87 | immr->im_qsmcm.qsmcm_sc2dr = (short)'\r'; |
| 88 | #endif |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
Marek Vasut | 58698e7 | 2012-09-13 01:26:42 +0200 | [diff] [blame] | 93 | static int mpc5xx_serial_getc(void) |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 94 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 95 | volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR; |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 96 | volatile short status; |
| 97 | unsigned char tmp; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 98 | |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 99 | /* New data ? */ |
| 100 | do { |
| 101 | #if defined(CONFIG_5xx_CONS_SCI1) |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 102 | status = immr->im_qsmcm.qsmcm_sc1sr; |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 103 | #else |
| 104 | status = immr->im_qsmcm.qsmcm_sc2sr; |
| 105 | #endif |
| 106 | |
| 107 | #if defined(CONFIG_WATCHDOG) |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 108 | reset_5xx_watchdog (immr); |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 109 | #endif |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 110 | } while ((status & SCI_RDRF) == 0); |
| 111 | |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 112 | /* Read data */ |
| 113 | #if defined(CONFIG_5xx_CONS_SCI1) |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 114 | tmp = (unsigned char)(immr->im_qsmcm.qsmcm_sc1dr & SCI_SCXDR_MK); |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 115 | #else |
| 116 | tmp = (unsigned char)( immr->im_qsmcm.qsmcm_sc2dr & SCI_SCXDR_MK); |
| 117 | #endif |
| 118 | return tmp; |
| 119 | } |
| 120 | |
Marek Vasut | 58698e7 | 2012-09-13 01:26:42 +0200 | [diff] [blame] | 121 | static int mpc5xx_serial_tstc(void) |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 122 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 123 | volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 124 | short status; |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 125 | |
| 126 | /* New data character ? */ |
| 127 | #if defined(CONFIG_5xx_CONS_SCI1) |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 128 | status = immr->im_qsmcm.qsmcm_sc1sr; |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 129 | #else |
| 130 | status = immr->im_qsmcm.qsmcm_sc2sr; |
| 131 | #endif |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 132 | return (status & SCI_RDRF); |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Marek Vasut | 58698e7 | 2012-09-13 01:26:42 +0200 | [diff] [blame] | 135 | static void mpc5xx_serial_setbrg(void) |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 136 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 137 | volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR; |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 138 | short scxbr; |
| 139 | |
| 140 | /* Set baudrate */ |
| 141 | scxbr = (gd->cpu_clk / (32 * gd->baudrate)); |
| 142 | #if defined(CONFIG_5xx_CONS_SCI1) |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 143 | immr->im_qsmcm.qsmcm_scc1r0 = (scxbr & SCI_SCXBR_MK); |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 144 | #else |
| 145 | immr->im_qsmcm.qsmcm_scc2r0 = (scxbr & SCI_SCXBR_MK); |
| 146 | #endif |
| 147 | } |
| 148 | |
Marek Vasut | 58698e7 | 2012-09-13 01:26:42 +0200 | [diff] [blame] | 149 | static struct serial_device mpc5xx_serial_drv = { |
| 150 | .name = "mpc5xx_serial", |
| 151 | .start = mpc5xx_serial_init, |
| 152 | .stop = NULL, |
| 153 | .setbrg = mpc5xx_serial_setbrg, |
| 154 | .putc = mpc5xx_serial_putc, |
Marek Vasut | d9c6449 | 2012-10-06 14:07:02 +0000 | [diff] [blame] | 155 | .puts = default_serial_puts, |
Marek Vasut | 58698e7 | 2012-09-13 01:26:42 +0200 | [diff] [blame] | 156 | .getc = mpc5xx_serial_getc, |
| 157 | .tstc = mpc5xx_serial_tstc, |
| 158 | }; |
| 159 | |
| 160 | void mpc5xx_serial_initialize(void) |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 161 | { |
Marek Vasut | 58698e7 | 2012-09-13 01:26:42 +0200 | [diff] [blame] | 162 | serial_register(&mpc5xx_serial_drv); |
| 163 | } |
wdenk | 359733b | 2003-03-31 17:27:09 +0000 | [diff] [blame] | 164 | |
Marek Vasut | 58698e7 | 2012-09-13 01:26:42 +0200 | [diff] [blame] | 165 | __weak struct serial_device *default_serial_console(void) |
| 166 | { |
| 167 | return &mpc5xx_serial_drv; |
| 168 | } |