wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (INCA) ASC UART support |
| 3 | */ |
| 4 | |
wdenk | b02744a | 2003-04-05 00:53:31 +0000 | [diff] [blame] | 5 | #include <config.h> |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 6 | #include <common.h> |
| 7 | #include <asm/inca-ip.h> |
Marek Vasut | 2a9d935 | 2012-09-13 01:20:07 +0200 | [diff] [blame] | 8 | #include <serial.h> |
| 9 | #include <linux/compiler.h> |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 10 | #include "asc_serial.h" |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 11 | |
wdenk | b02744a | 2003-04-05 00:53:31 +0000 | [diff] [blame] | 12 | |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 13 | #define SET_BIT(reg, mask) reg |= (mask) |
| 14 | #define CLEAR_BIT(reg, mask) reg &= (~mask) |
| 15 | #define CLEAR_BITS(reg, mask) CLEAR_BIT(reg, mask) |
| 16 | #define SET_BITS(reg, mask) SET_BIT(reg, mask) |
| 17 | #define SET_BITFIELD(reg, mask, off, val) {reg &= (~mask); reg |= (val << off);} |
| 18 | |
| 19 | extern uint incaip_get_fpiclk(void); |
| 20 | |
| 21 | static int serial_setopt (void); |
| 22 | |
| 23 | /* pointer to ASC register base address */ |
| 24 | static volatile incaAsc_t *pAsc = (incaAsc_t *)INCA_IP_ASC; |
| 25 | |
| 26 | /****************************************************************************** |
| 27 | * |
| 28 | * serial_init - initialize a INCAASC channel |
| 29 | * |
| 30 | * This routine initializes the number of data bits, parity |
| 31 | * and set the selected baud rate. Interrupts are disabled. |
| 32 | * Set the modem control signals if the option is selected. |
| 33 | * |
| 34 | * RETURNS: N/A |
| 35 | */ |
| 36 | |
Marek Vasut | 2a9d935 | 2012-09-13 01:20:07 +0200 | [diff] [blame] | 37 | static int asc_serial_init(void) |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 38 | { |
| 39 | /* we have to set PMU.EN13 bit to enable an ASC device*/ |
| 40 | INCAASC_PMU_ENABLE(13); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 41 | |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 42 | /* and we have to set CLC register*/ |
| 43 | CLEAR_BIT(pAsc->asc_clc, ASCCLC_DISS); |
| 44 | SET_BITFIELD(pAsc->asc_clc, ASCCLC_RMCMASK, ASCCLC_RMCOFFSET, 0x0001); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 45 | |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 46 | /* initialy we are in async mode */ |
| 47 | pAsc->asc_con = ASCCON_M_8ASYNC; |
| 48 | |
| 49 | /* select input port */ |
| 50 | pAsc->asc_pisel = (CONSOLE_TTY & 0x1); |
| 51 | |
| 52 | /* TXFIFO's filling level */ |
| 53 | SET_BITFIELD(pAsc->asc_txfcon, ASCTXFCON_TXFITLMASK, |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 54 | ASCTXFCON_TXFITLOFF, INCAASC_TXFIFO_FL); |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 55 | /* enable TXFIFO */ |
| 56 | SET_BIT(pAsc->asc_txfcon, ASCTXFCON_TXFEN); |
| 57 | |
| 58 | /* RXFIFO's filling level */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 59 | SET_BITFIELD(pAsc->asc_txfcon, ASCRXFCON_RXFITLMASK, |
| 60 | ASCRXFCON_RXFITLOFF, INCAASC_RXFIFO_FL); |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 61 | /* enable RXFIFO */ |
| 62 | SET_BIT(pAsc->asc_rxfcon, ASCRXFCON_RXFEN); |
| 63 | |
| 64 | /* enable error signals */ |
| 65 | SET_BIT(pAsc->asc_con, ASCCON_FEN); |
| 66 | SET_BIT(pAsc->asc_con, ASCCON_OEN); |
| 67 | |
| 68 | /* acknowledge ASC interrupts */ |
| 69 | ASC_INTERRUPTS_CLEAR(INCAASC_IRQ_LINE_ALL); |
| 70 | |
| 71 | /* disable ASC interrupts */ |
| 72 | ASC_INTERRUPTS_DISABLE(INCAASC_IRQ_LINE_ALL); |
| 73 | |
| 74 | /* set FIFOs into the transparent mode */ |
| 75 | SET_BIT(pAsc->asc_txfcon, ASCTXFCON_TXTMEN); |
| 76 | SET_BIT(pAsc->asc_rxfcon, ASCRXFCON_RXTMEN); |
| 77 | |
| 78 | /* set baud rate */ |
| 79 | serial_setbrg(); |
| 80 | |
| 81 | /* set the options */ |
| 82 | serial_setopt(); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 83 | |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 84 | return 0; |
| 85 | } |
| 86 | |
Marek Vasut | 2a9d935 | 2012-09-13 01:20:07 +0200 | [diff] [blame] | 87 | static void asc_serial_setbrg(void) |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 88 | { |
| 89 | ulong uiReloadValue, fdv; |
| 90 | ulong f_ASC; |
| 91 | |
| 92 | f_ASC = incaip_get_fpiclk(); |
| 93 | |
| 94 | #ifndef INCAASC_USE_FDV |
| 95 | fdv = 2; |
| 96 | uiReloadValue = (f_ASC / (fdv * 16 * CONFIG_BAUDRATE)) - 1; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 97 | #else |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 98 | fdv = INCAASC_FDV_HIGH_BAUDRATE; |
| 99 | uiReloadValue = (f_ASC / (8192 * CONFIG_BAUDRATE / fdv)) - 1; |
| 100 | #endif /* INCAASC_USE_FDV */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 101 | |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 102 | if ( (uiReloadValue < 0) || (uiReloadValue > 8191) ) |
| 103 | { |
| 104 | #ifndef INCAASC_USE_FDV |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 105 | fdv = 3; |
| 106 | uiReloadValue = (f_ASC / (fdv * 16 * CONFIG_BAUDRATE)) - 1; |
| 107 | #else |
| 108 | fdv = INCAASC_FDV_LOW_BAUDRATE; |
| 109 | uiReloadValue = (f_ASC / (8192 * CONFIG_BAUDRATE / fdv)) - 1; |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 110 | #endif /* INCAASC_USE_FDV */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 111 | |
| 112 | if ( (uiReloadValue < 0) || (uiReloadValue > 8191) ) |
| 113 | { |
| 114 | return; /* can't impossibly generate that baud rate */ |
| 115 | } |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | /* Disable Baud Rate Generator; BG should only be written when R=0 */ |
| 119 | CLEAR_BIT(pAsc->asc_con, ASCCON_R); |
| 120 | |
| 121 | #ifndef INCAASC_USE_FDV |
| 122 | /* |
| 123 | * Disable Fractional Divider (FDE) |
| 124 | * Divide clock by reload-value + constant (BRS) |
| 125 | */ |
| 126 | /* FDE = 0 */ |
| 127 | CLEAR_BIT(pAsc->asc_con, ASCCON_FDE); |
| 128 | |
| 129 | if ( fdv == 2 ) |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 130 | CLEAR_BIT(pAsc->asc_con, ASCCON_BRS); /* BRS = 0 */ |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 131 | else |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 132 | SET_BIT(pAsc->asc_con, ASCCON_BRS); /* BRS = 1 */ |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 133 | |
| 134 | #else /* INCAASC_USE_FDV */ |
| 135 | |
| 136 | /* Enable Fractional Divider */ |
| 137 | SET_BIT(pAsc->asc_con, ASCCON_FDE); /* FDE = 1 */ |
| 138 | |
| 139 | /* Set fractional divider value */ |
| 140 | pAsc->asc_fdv = fdv & ASCFDV_VALUE_MASK; |
| 141 | |
| 142 | #endif /* INCAASC_USE_FDV */ |
| 143 | |
| 144 | /* Set reload value in BG */ |
| 145 | pAsc->asc_bg = uiReloadValue; |
| 146 | |
| 147 | /* Enable Baud Rate Generator */ |
| 148 | SET_BIT(pAsc->asc_con, ASCCON_R); /* R = 1 */ |
| 149 | } |
| 150 | |
| 151 | /******************************************************************************* |
| 152 | * |
| 153 | * serial_setopt - set the serial options |
| 154 | * |
| 155 | * Set the channel operating mode to that specified. Following options |
| 156 | * are supported: CREAD, CSIZE, PARENB, and PARODD. |
| 157 | * |
| 158 | * Note, this routine disables the transmitter. The calling routine |
| 159 | * may have to re-enable it. |
| 160 | * |
| 161 | * RETURNS: |
| 162 | * Returns 0 to indicate success, otherwise -1 is returned |
| 163 | */ |
| 164 | |
| 165 | static int serial_setopt (void) |
| 166 | { |
| 167 | ulong con; |
| 168 | |
| 169 | switch ( ASC_OPTIONS & ASCOPT_CSIZE ) |
| 170 | { |
| 171 | /* 7-bit-data */ |
| 172 | case ASCOPT_CS7: |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 173 | con = ASCCON_M_7ASYNCPAR; /* 7-bit-data and parity bit */ |
| 174 | break; |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 175 | |
| 176 | /* 8-bit-data */ |
| 177 | case ASCOPT_CS8: |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 178 | if ( ASC_OPTIONS & ASCOPT_PARENB ) |
| 179 | con = ASCCON_M_8ASYNCPAR; /* 8-bit-data and parity bit */ |
| 180 | else |
| 181 | con = ASCCON_M_8ASYNC; /* 8-bit-data no parity */ |
| 182 | break; |
| 183 | |
| 184 | /* |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 185 | * only 7 and 8-bit frames are supported |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 186 | * if we don't use IOCTL extensions |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 187 | */ |
| 188 | default: |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 189 | return -1; |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | if ( ASC_OPTIONS & ASCOPT_STOPB ) |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 193 | SET_BIT(con, ASCCON_STP); /* 2 stop bits */ |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 194 | else |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 195 | CLEAR_BIT(con, ASCCON_STP); /* 1 stop bit */ |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 196 | |
| 197 | if ( ASC_OPTIONS & ASCOPT_PARENB ) |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 198 | SET_BIT(con, ASCCON_PEN); /* enable parity checking */ |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 199 | else |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 200 | CLEAR_BIT(con, ASCCON_PEN); /* disable parity checking */ |
| 201 | |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 202 | if ( ASC_OPTIONS & ASCOPT_PARODD ) |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 203 | SET_BIT(con, ASCCON_ODD); /* odd parity */ |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 204 | else |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 205 | CLEAR_BIT(con, ASCCON_ODD); /* even parity */ |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 206 | |
| 207 | if ( ASC_OPTIONS & ASCOPT_CREAD ) |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 208 | SET_BIT(pAsc->asc_whbcon, ASCWHBCON_SETREN); /* Receiver enable */ |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 209 | |
| 210 | pAsc->asc_con |= con; |
| 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
Marek Vasut | 2a9d935 | 2012-09-13 01:20:07 +0200 | [diff] [blame] | 215 | static void asc_serial_putc(const char c) |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 216 | { |
| 217 | uint txFl = 0; |
| 218 | |
| 219 | if (c == '\n') serial_putc ('\r'); |
| 220 | |
| 221 | /* check do we have a free space in the TX FIFO */ |
| 222 | /* get current filling level */ |
| 223 | do |
| 224 | { |
| 225 | txFl = ( pAsc->asc_fstat & ASCFSTAT_TXFFLMASK ) >> ASCFSTAT_TXFFLOFF; |
| 226 | } |
| 227 | while ( txFl == INCAASC_TXFIFO_FULL ); |
| 228 | |
| 229 | pAsc->asc_tbuf = c; /* write char to Transmit Buffer Register */ |
wdenk | b02744a | 2003-04-05 00:53:31 +0000 | [diff] [blame] | 230 | |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 231 | /* check for errors */ |
| 232 | if ( pAsc->asc_con & ASCCON_OE ) |
| 233 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 234 | SET_BIT(pAsc->asc_whbcon, ASCWHBCON_CLROE); |
| 235 | return; |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | |
Marek Vasut | 2a9d935 | 2012-09-13 01:20:07 +0200 | [diff] [blame] | 239 | static int asc_serial_getc(void) |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 240 | { |
| 241 | ulong symbol_mask; |
| 242 | char c; |
| 243 | |
| 244 | while (!serial_tstc()); |
| 245 | |
| 246 | symbol_mask = |
| 247 | ((ASC_OPTIONS & ASCOPT_CSIZE) == ASCOPT_CS7) ? (0x7f) : (0xff); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 248 | |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 249 | c = (char)(pAsc->asc_rbuf & symbol_mask); |
| 250 | |
| 251 | return c; |
| 252 | } |
| 253 | |
Marek Vasut | 2a9d935 | 2012-09-13 01:20:07 +0200 | [diff] [blame] | 254 | static int asc_serial_tstc(void) |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 255 | { |
| 256 | int res = 1; |
| 257 | |
| 258 | if ( (pAsc->asc_fstat & ASCFSTAT_RXFFLMASK) == 0 ) |
| 259 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 260 | res = 0; |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 261 | } |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 262 | else if ( pAsc->asc_con & ASCCON_FE ) |
| 263 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 264 | SET_BIT(pAsc->asc_whbcon, ASCWHBCON_CLRFE); |
| 265 | res = 0; |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 266 | } |
| 267 | else if ( pAsc->asc_con & ASCCON_PE ) |
| 268 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 269 | SET_BIT(pAsc->asc_whbcon, ASCWHBCON_CLRPE); |
| 270 | res = 0; |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 271 | } |
| 272 | else if ( pAsc->asc_con & ASCCON_OE ) |
| 273 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 274 | SET_BIT(pAsc->asc_whbcon, ASCWHBCON_CLROE); |
| 275 | res = 0; |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | return res; |
| 279 | } |
Marek Vasut | 2a9d935 | 2012-09-13 01:20:07 +0200 | [diff] [blame] | 280 | |
Marek Vasut | 2a9d935 | 2012-09-13 01:20:07 +0200 | [diff] [blame] | 281 | static struct serial_device asc_serial_drv = { |
| 282 | .name = "asc_serial", |
| 283 | .start = asc_serial_init, |
| 284 | .stop = NULL, |
| 285 | .setbrg = asc_serial_setbrg, |
| 286 | .putc = asc_serial_putc, |
Marek Vasut | d9c6449 | 2012-10-06 14:07:02 +0000 | [diff] [blame] | 287 | .puts = default_serial_puts, |
Marek Vasut | 2a9d935 | 2012-09-13 01:20:07 +0200 | [diff] [blame] | 288 | .getc = asc_serial_getc, |
| 289 | .tstc = asc_serial_tstc, |
| 290 | }; |
| 291 | |
| 292 | void asc_serial_initialize(void) |
| 293 | { |
| 294 | serial_register(&asc_serial_drv); |
| 295 | } |
| 296 | |
| 297 | __weak struct serial_device *default_serial_console(void) |
| 298 | { |
| 299 | return &asc_serial_drv; |
| 300 | } |