wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Simple serial driver for Cogent motherboard serial ports |
| 3 | * for use during boot |
| 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Masahiro Yamada | 0298793 | 2013-11-11 10:30:20 +0900 | [diff] [blame] | 7 | #include "serial.h" |
Marek Vasut | d25c39d | 2012-09-13 12:29:31 +0200 | [diff] [blame] | 8 | #include <serial.h> |
| 9 | #include <linux/compiler.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 10 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 11 | DECLARE_GLOBAL_DATA_PTR; |
| 12 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 13 | #if (CMA_MB_CAPS & CMA_MB_CAP_SERPAR) |
| 14 | |
| 15 | #if (defined(CONFIG_8xx) && defined(CONFIG_8xx_CONS_NONE)) || \ |
Masahiro Yamada | 58dac32 | 2014-03-05 17:40:10 +0900 | [diff] [blame] | 16 | (defined(CONFIG_MPC8260) && defined(CONFIG_CONS_NONE)) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 17 | |
| 18 | #if CONFIG_CONS_INDEX == 1 |
| 19 | #define CMA_MB_SERIAL_BASE CMA_MB_SERIALA_BASE |
| 20 | #elif CONFIG_CONS_INDEX == 2 |
| 21 | #define CMA_MB_SERIAL_BASE CMA_MB_SERIALB_BASE |
| 22 | #elif CONFIG_CONS_INDEX == 3 && (CMA_MB_CAPS & CMA_MB_CAP_SER2) |
| 23 | #define CMA_MB_SERIAL_BASE CMA_MB_SER2A_BASE |
| 24 | #elif CONFIG_CONS_INDEX == 4 && (CMA_MB_CAPS & CMA_MB_CAP_SER2) |
| 25 | #define CMA_MB_SERIAL_BASE CMA_MB_SER2B_BASE |
| 26 | #else |
| 27 | #error CONFIG_CONS_INDEX must be configured for Cogent motherboard serial |
| 28 | #endif |
| 29 | |
Marek Vasut | d25c39d | 2012-09-13 12:29:31 +0200 | [diff] [blame] | 30 | static int cogent_serial_init(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 31 | { |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 32 | cma_mb_serial *mbsp = (cma_mb_serial *) CMA_MB_SERIAL_BASE; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 33 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 34 | cma_mb_reg_write (&mbsp->ser_ier, 0x00); /* turn off interrupts */ |
| 35 | serial_setbrg (); |
| 36 | cma_mb_reg_write (&mbsp->ser_lcr, 0x03); /* 8 data, 1 stop, no parity */ |
| 37 | cma_mb_reg_write (&mbsp->ser_mcr, 0x03); /* RTS/DTR */ |
| 38 | cma_mb_reg_write (&mbsp->ser_fcr, 0x07); /* Clear & enable FIFOs */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 39 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 40 | return (0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Marek Vasut | d25c39d | 2012-09-13 12:29:31 +0200 | [diff] [blame] | 43 | static void cogent_serial_setbrg(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 44 | { |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 45 | cma_mb_serial *mbsp = (cma_mb_serial *) CMA_MB_SERIAL_BASE; |
| 46 | unsigned int divisor; |
| 47 | unsigned char lcr; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 48 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 49 | if ((divisor = br_to_div (gd->baudrate)) == 0) |
| 50 | divisor = DEFDIV; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 51 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 52 | lcr = cma_mb_reg_read (&mbsp->ser_lcr); |
| 53 | cma_mb_reg_write (&mbsp->ser_lcr, lcr | 0x80); /* Access baud rate(set DLAB) */ |
| 54 | cma_mb_reg_write (&mbsp->ser_brl, divisor & 0xff); |
| 55 | cma_mb_reg_write (&mbsp->ser_brh, (divisor >> 8) & 0xff); |
| 56 | cma_mb_reg_write (&mbsp->ser_lcr, lcr); /* unset DLAB */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Marek Vasut | d25c39d | 2012-09-13 12:29:31 +0200 | [diff] [blame] | 59 | static void cogent_serial_putc(const char c) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 60 | { |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 61 | cma_mb_serial *mbsp = (cma_mb_serial *) CMA_MB_SERIAL_BASE; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 62 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 63 | if (c == '\n') |
| 64 | serial_putc ('\r'); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 65 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 66 | while ((cma_mb_reg_read (&mbsp->ser_lsr) & LSR_THRE) == 0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 67 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 68 | cma_mb_reg_write (&mbsp->ser_thr, c); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Marek Vasut | d25c39d | 2012-09-13 12:29:31 +0200 | [diff] [blame] | 71 | static int cogent_serial_getc(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 72 | { |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 73 | cma_mb_serial *mbsp = (cma_mb_serial *) CMA_MB_SERIAL_BASE; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 74 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 75 | while ((cma_mb_reg_read (&mbsp->ser_lsr) & LSR_DR) == 0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 76 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 77 | return ((int) cma_mb_reg_read (&mbsp->ser_rhr) & 0x7f); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Marek Vasut | d25c39d | 2012-09-13 12:29:31 +0200 | [diff] [blame] | 80 | static int cogent_serial_tstc(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 81 | { |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 82 | cma_mb_serial *mbsp = (cma_mb_serial *) CMA_MB_SERIAL_BASE; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 83 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 84 | return ((cma_mb_reg_read (&mbsp->ser_lsr) & LSR_DR) != 0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Marek Vasut | d25c39d | 2012-09-13 12:29:31 +0200 | [diff] [blame] | 87 | static struct serial_device cogent_serial_drv = { |
| 88 | .name = "cogent_serial", |
| 89 | .start = cogent_serial_init, |
| 90 | .stop = NULL, |
| 91 | .setbrg = cogent_serial_setbrg, |
| 92 | .putc = cogent_serial_putc, |
Marek Vasut | ec3fd68 | 2012-10-06 14:07:02 +0000 | [diff] [blame] | 93 | .puts = default_serial_puts, |
Marek Vasut | d25c39d | 2012-09-13 12:29:31 +0200 | [diff] [blame] | 94 | .getc = cogent_serial_getc, |
| 95 | .tstc = cogent_serial_tstc, |
| 96 | }; |
| 97 | |
| 98 | void cogent_serial_initialize(void) |
| 99 | { |
| 100 | serial_register(&cogent_serial_drv); |
| 101 | } |
| 102 | |
| 103 | __weak struct serial_device *default_serial_console(void) |
| 104 | { |
| 105 | return &cogent_serial_drv; |
| 106 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 107 | #endif /* CONS_NONE */ |
| 108 | |
Jon Loeliger | fcec2eb | 2007-07-09 18:19:09 -0500 | [diff] [blame] | 109 | #if defined(CONFIG_CMD_KGDB) && \ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 110 | defined(CONFIG_KGDB_NONE) |
| 111 | |
| 112 | #if CONFIG_KGDB_INDEX == CONFIG_CONS_INDEX |
| 113 | #error Console and kgdb are on the same serial port - this is not supported |
| 114 | #endif |
| 115 | |
| 116 | #if CONFIG_KGDB_INDEX == 1 |
| 117 | #define CMA_MB_KGDB_SER_BASE CMA_MB_SERIALA_BASE |
| 118 | #elif CONFIG_KGDB_INDEX == 2 |
| 119 | #define CMA_MB_KGDB_SER_BASE CMA_MB_SERIALB_BASE |
| 120 | #elif CONFIG_KGDB_INDEX == 3 && (CMA_MB_CAPS & CMA_MB_CAP_SER2) |
| 121 | #define CMA_MB_KGDB_SER_BASE CMA_MB_SER2A_BASE |
| 122 | #elif CONFIG_KGDB_INDEX == 4 && (CMA_MB_CAPS & CMA_MB_CAP_SER2) |
| 123 | #define CMA_MB_KGDB_SER_BASE CMA_MB_SER2B_BASE |
| 124 | #else |
| 125 | #error CONFIG_KGDB_INDEX must be configured for Cogent motherboard serial |
| 126 | #endif |
| 127 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 128 | void kgdb_serial_init (void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 129 | { |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 130 | cma_mb_serial *mbsp = (cma_mb_serial *) CMA_MB_KGDB_SER_BASE; |
| 131 | unsigned int divisor; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 132 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 133 | if ((divisor = br_to_div (CONFIG_KGDB_BAUDRATE)) == 0) |
| 134 | divisor = DEFDIV; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 135 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 136 | cma_mb_reg_write (&mbsp->ser_ier, 0x00); /* turn off interrupts */ |
| 137 | cma_mb_reg_write (&mbsp->ser_lcr, 0x80); /* Access baud rate(set DLAB) */ |
| 138 | cma_mb_reg_write (&mbsp->ser_brl, divisor & 0xff); |
| 139 | cma_mb_reg_write (&mbsp->ser_brh, (divisor >> 8) & 0xff); |
| 140 | cma_mb_reg_write (&mbsp->ser_lcr, 0x03); /* 8 data, 1 stop, no parity */ |
| 141 | cma_mb_reg_write (&mbsp->ser_mcr, 0x03); /* RTS/DTR */ |
| 142 | cma_mb_reg_write (&mbsp->ser_fcr, 0x07); /* Clear & enable FIFOs */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 143 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 144 | printf ("[on cma10x serial port B] "); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 147 | void putDebugChar (int c) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 148 | { |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 149 | cma_mb_serial *mbsp = (cma_mb_serial *) CMA_MB_KGDB_SER_BASE; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 150 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 151 | while ((cma_mb_reg_read (&mbsp->ser_lsr) & LSR_THRE) == 0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 152 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 153 | cma_mb_reg_write (&mbsp->ser_thr, c & 0xff); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 156 | void putDebugStr (const char *str) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 157 | { |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 158 | while (*str != '\0') { |
| 159 | if (*str == '\n') |
| 160 | putDebugChar ('\r'); |
| 161 | putDebugChar (*str++); |
| 162 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 165 | int getDebugChar (void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 166 | { |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 167 | cma_mb_serial *mbsp = (cma_mb_serial *) CMA_MB_KGDB_SER_BASE; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 168 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 169 | while ((cma_mb_reg_read (&mbsp->ser_lsr) & LSR_DR) == 0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 170 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 171 | return ((int) cma_mb_reg_read (&mbsp->ser_rhr) & 0x7f); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 174 | void kgdb_interruptible (int yes) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 175 | { |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 176 | cma_mb_serial *mbsp = (cma_mb_serial *) CMA_MB_KGDB_SER_BASE; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 177 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 178 | if (yes == 1) { |
| 179 | printf ("kgdb: turning serial ints on\n"); |
| 180 | cma_mb_reg_write (&mbsp->ser_ier, 0xf); |
| 181 | } else { |
| 182 | printf ("kgdb: turning serial ints off\n"); |
| 183 | cma_mb_reg_write (&mbsp->ser_ier, 0x0); |
| 184 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | #endif /* KGDB && KGDB_NONE */ |
| 188 | |
| 189 | #endif /* CAPS & SERPAR */ |