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> |
| 7 | #include <board/cogent/serial.h> |
| 8 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 9 | DECLARE_GLOBAL_DATA_PTR; |
| 10 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 11 | #if (CMA_MB_CAPS & CMA_MB_CAP_SERPAR) |
| 12 | |
| 13 | #if (defined(CONFIG_8xx) && defined(CONFIG_8xx_CONS_NONE)) || \ |
| 14 | (defined(CONFIG_8260) && defined(CONFIG_CONS_NONE)) |
| 15 | |
| 16 | #if CONFIG_CONS_INDEX == 1 |
| 17 | #define CMA_MB_SERIAL_BASE CMA_MB_SERIALA_BASE |
| 18 | #elif CONFIG_CONS_INDEX == 2 |
| 19 | #define CMA_MB_SERIAL_BASE CMA_MB_SERIALB_BASE |
| 20 | #elif CONFIG_CONS_INDEX == 3 && (CMA_MB_CAPS & CMA_MB_CAP_SER2) |
| 21 | #define CMA_MB_SERIAL_BASE CMA_MB_SER2A_BASE |
| 22 | #elif CONFIG_CONS_INDEX == 4 && (CMA_MB_CAPS & CMA_MB_CAP_SER2) |
| 23 | #define CMA_MB_SERIAL_BASE CMA_MB_SER2B_BASE |
| 24 | #else |
| 25 | #error CONFIG_CONS_INDEX must be configured for Cogent motherboard serial |
| 26 | #endif |
| 27 | |
| 28 | int serial_init (void) |
| 29 | { |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 30 | cma_mb_serial *mbsp = (cma_mb_serial *) CMA_MB_SERIAL_BASE; |
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_reg_write (&mbsp->ser_ier, 0x00); /* turn off interrupts */ |
| 33 | serial_setbrg (); |
| 34 | cma_mb_reg_write (&mbsp->ser_lcr, 0x03); /* 8 data, 1 stop, no parity */ |
| 35 | cma_mb_reg_write (&mbsp->ser_mcr, 0x03); /* RTS/DTR */ |
| 36 | cma_mb_reg_write (&mbsp->ser_fcr, 0x07); /* Clear & enable FIFOs */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 37 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 38 | return (0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 41 | void serial_setbrg (void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 42 | { |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 43 | cma_mb_serial *mbsp = (cma_mb_serial *) CMA_MB_SERIAL_BASE; |
| 44 | unsigned int divisor; |
| 45 | unsigned char lcr; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 46 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 47 | if ((divisor = br_to_div (gd->baudrate)) == 0) |
| 48 | divisor = DEFDIV; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 49 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 50 | lcr = cma_mb_reg_read (&mbsp->ser_lcr); |
| 51 | cma_mb_reg_write (&mbsp->ser_lcr, lcr | 0x80); /* Access baud rate(set DLAB) */ |
| 52 | cma_mb_reg_write (&mbsp->ser_brl, divisor & 0xff); |
| 53 | cma_mb_reg_write (&mbsp->ser_brh, (divisor >> 8) & 0xff); |
| 54 | cma_mb_reg_write (&mbsp->ser_lcr, lcr); /* unset DLAB */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 57 | void serial_putc (const char c) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 58 | { |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 59 | cma_mb_serial *mbsp = (cma_mb_serial *) CMA_MB_SERIAL_BASE; |
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 | if (c == '\n') |
| 62 | serial_putc ('\r'); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 63 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 64 | while ((cma_mb_reg_read (&mbsp->ser_lsr) & LSR_THRE) == 0); |
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 | cma_mb_reg_write (&mbsp->ser_thr, c); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 69 | void serial_puts (const char *s) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 70 | { |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 71 | while (*s != '\0') |
| 72 | serial_putc (*s++); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 75 | int serial_getc (void) |
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 | cma_mb_serial *mbsp = (cma_mb_serial *) CMA_MB_SERIAL_BASE; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 78 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 79 | while ((cma_mb_reg_read (&mbsp->ser_lsr) & LSR_DR) == 0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 80 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 81 | return ((int) cma_mb_reg_read (&mbsp->ser_rhr) & 0x7f); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 84 | int serial_tstc (void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 85 | { |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 86 | cma_mb_serial *mbsp = (cma_mb_serial *) CMA_MB_SERIAL_BASE; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 87 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 88 | return ((cma_mb_reg_read (&mbsp->ser_lsr) & LSR_DR) != 0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | #endif /* CONS_NONE */ |
| 92 | |
Jon Loeliger | fcec2eb | 2007-07-09 18:19:09 -0500 | [diff] [blame^] | 93 | #if defined(CONFIG_CMD_KGDB) && \ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 94 | defined(CONFIG_KGDB_NONE) |
| 95 | |
| 96 | #if CONFIG_KGDB_INDEX == CONFIG_CONS_INDEX |
| 97 | #error Console and kgdb are on the same serial port - this is not supported |
| 98 | #endif |
| 99 | |
| 100 | #if CONFIG_KGDB_INDEX == 1 |
| 101 | #define CMA_MB_KGDB_SER_BASE CMA_MB_SERIALA_BASE |
| 102 | #elif CONFIG_KGDB_INDEX == 2 |
| 103 | #define CMA_MB_KGDB_SER_BASE CMA_MB_SERIALB_BASE |
| 104 | #elif CONFIG_KGDB_INDEX == 3 && (CMA_MB_CAPS & CMA_MB_CAP_SER2) |
| 105 | #define CMA_MB_KGDB_SER_BASE CMA_MB_SER2A_BASE |
| 106 | #elif CONFIG_KGDB_INDEX == 4 && (CMA_MB_CAPS & CMA_MB_CAP_SER2) |
| 107 | #define CMA_MB_KGDB_SER_BASE CMA_MB_SER2B_BASE |
| 108 | #else |
| 109 | #error CONFIG_KGDB_INDEX must be configured for Cogent motherboard serial |
| 110 | #endif |
| 111 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 112 | void kgdb_serial_init (void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 113 | { |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 114 | cma_mb_serial *mbsp = (cma_mb_serial *) CMA_MB_KGDB_SER_BASE; |
| 115 | unsigned int divisor; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 116 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 117 | if ((divisor = br_to_div (CONFIG_KGDB_BAUDRATE)) == 0) |
| 118 | divisor = DEFDIV; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 119 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 120 | cma_mb_reg_write (&mbsp->ser_ier, 0x00); /* turn off interrupts */ |
| 121 | cma_mb_reg_write (&mbsp->ser_lcr, 0x80); /* Access baud rate(set DLAB) */ |
| 122 | cma_mb_reg_write (&mbsp->ser_brl, divisor & 0xff); |
| 123 | cma_mb_reg_write (&mbsp->ser_brh, (divisor >> 8) & 0xff); |
| 124 | cma_mb_reg_write (&mbsp->ser_lcr, 0x03); /* 8 data, 1 stop, no parity */ |
| 125 | cma_mb_reg_write (&mbsp->ser_mcr, 0x03); /* RTS/DTR */ |
| 126 | cma_mb_reg_write (&mbsp->ser_fcr, 0x07); /* Clear & enable FIFOs */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 127 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 128 | printf ("[on cma10x serial port B] "); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 131 | void putDebugChar (int c) |
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 | cma_mb_serial *mbsp = (cma_mb_serial *) CMA_MB_KGDB_SER_BASE; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 134 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 135 | while ((cma_mb_reg_read (&mbsp->ser_lsr) & LSR_THRE) == 0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 136 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 137 | cma_mb_reg_write (&mbsp->ser_thr, c & 0xff); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 140 | void putDebugStr (const char *str) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 141 | { |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 142 | while (*str != '\0') { |
| 143 | if (*str == '\n') |
| 144 | putDebugChar ('\r'); |
| 145 | putDebugChar (*str++); |
| 146 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 149 | int getDebugChar (void) |
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 | cma_mb_serial *mbsp = (cma_mb_serial *) CMA_MB_KGDB_SER_BASE; |
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 | while ((cma_mb_reg_read (&mbsp->ser_lsr) & LSR_DR) == 0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 154 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 155 | return ((int) cma_mb_reg_read (&mbsp->ser_rhr) & 0x7f); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 158 | void kgdb_interruptible (int yes) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 159 | { |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 160 | cma_mb_serial *mbsp = (cma_mb_serial *) CMA_MB_KGDB_SER_BASE; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 161 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 162 | if (yes == 1) { |
| 163 | printf ("kgdb: turning serial ints on\n"); |
| 164 | cma_mb_reg_write (&mbsp->ser_ier, 0xf); |
| 165 | } else { |
| 166 | printf ("kgdb: turning serial ints off\n"); |
| 167 | cma_mb_reg_write (&mbsp->ser_ier, 0x0); |
| 168 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | #endif /* KGDB && KGDB_NONE */ |
| 172 | |
| 173 | #endif /* CAPS & SERPAR */ |