wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (c) 2004 Sascha Hauer <sascha@saschahauer.de> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | */ |
| 19 | |
| 20 | #include <common.h> |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 21 | #include <asm/arch/imx-regs.h> |
Marek Vasut | 9784bde | 2012-09-14 22:34:51 +0200 | [diff] [blame] | 22 | #include <serial.h> |
| 23 | #include <linux/compiler.h> |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 24 | |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 25 | #if defined CONFIG_IMX_SERIAL1 |
| 26 | #define UART_BASE IMX_UART1_BASE |
| 27 | #elif defined CONFIG_IMX_SERIAL2 |
| 28 | #define UART_BASE IMX_UART2_BASE |
| 29 | #else |
| 30 | #error "define CONFIG_IMX_SERIAL1, CONFIG_IMX_SERIAL2 or CONFIG_IMX_SERIAL_NONE" |
| 31 | #endif |
| 32 | |
| 33 | struct imx_serial { |
| 34 | volatile uint32_t urxd[16]; |
| 35 | volatile uint32_t utxd[16]; |
| 36 | volatile uint32_t ucr1; |
| 37 | volatile uint32_t ucr2; |
| 38 | volatile uint32_t ucr3; |
| 39 | volatile uint32_t ucr4; |
| 40 | volatile uint32_t ufcr; |
| 41 | volatile uint32_t usr1; |
| 42 | volatile uint32_t usr2; |
| 43 | volatile uint32_t uesc; |
| 44 | volatile uint32_t utim; |
| 45 | volatile uint32_t ubir; |
| 46 | volatile uint32_t ubmr; |
| 47 | volatile uint32_t ubrc; |
| 48 | volatile uint32_t bipr[4]; |
| 49 | volatile uint32_t bmpr[4]; |
| 50 | volatile uint32_t uts; |
| 51 | }; |
| 52 | |
Andrew Dyer | 3207699 | 2008-09-12 02:20:46 +0200 | [diff] [blame] | 53 | DECLARE_GLOBAL_DATA_PTR; |
| 54 | |
Marek Vasut | 9784bde | 2012-09-14 22:34:51 +0200 | [diff] [blame] | 55 | static void imx_serial_setbrg(void) |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 56 | { |
| 57 | serial_init(); |
| 58 | } |
| 59 | |
| 60 | extern void imx_gpio_mode(int gpio_mode); |
| 61 | |
| 62 | /* |
| 63 | * Initialise the serial port with the given baudrate. The settings |
| 64 | * are always 8 data bits, no parity, 1 stop bit, no start bits. |
| 65 | * |
| 66 | */ |
Marek Vasut | 9784bde | 2012-09-14 22:34:51 +0200 | [diff] [blame] | 67 | static int imx_serial_init(void) |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 68 | { |
| 69 | volatile struct imx_serial* base = (struct imx_serial *)UART_BASE; |
Andrew Dyer | 3207699 | 2008-09-12 02:20:46 +0200 | [diff] [blame] | 70 | unsigned int ufcr_rfdiv; |
| 71 | unsigned int refclk; |
| 72 | |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 73 | #ifdef CONFIG_IMX_SERIAL1 |
| 74 | imx_gpio_mode(PC11_PF_UART1_TXD); |
| 75 | imx_gpio_mode(PC12_PF_UART1_RXD); |
| 76 | #else |
| 77 | imx_gpio_mode(PB30_PF_UART2_TXD); |
| 78 | imx_gpio_mode(PB31_PF_UART2_RXD); |
| 79 | #endif |
| 80 | |
| 81 | /* Disable UART */ |
| 82 | base->ucr1 &= ~UCR1_UARTEN; |
| 83 | |
| 84 | /* Set to default POR state */ |
| 85 | |
| 86 | base->ucr1 = 0x00000004; |
| 87 | base->ucr2 = 0x00000000; |
| 88 | base->ucr3 = 0x00000000; |
| 89 | base->ucr4 = 0x00008040; |
| 90 | base->uesc = 0x0000002B; |
| 91 | base->utim = 0x00000000; |
| 92 | base->ubir = 0x00000000; |
| 93 | base->ubmr = 0x00000000; |
| 94 | base->uts = 0x00000000; |
| 95 | /* Set clocks */ |
| 96 | base->ucr4 |= UCR4_REF16; |
| 97 | |
| 98 | /* Configure FIFOs */ |
| 99 | base->ufcr = 0xa81; |
| 100 | |
Andrew Dyer | 3207699 | 2008-09-12 02:20:46 +0200 | [diff] [blame] | 101 | /* set the baud rate. |
| 102 | * |
| 103 | * baud * 16 x |
| 104 | * --------- = - |
| 105 | * refclk y |
| 106 | * |
| 107 | * x - 1 = UBIR |
| 108 | * y - 1 = UBMR |
| 109 | * |
| 110 | * each register is 16 bits wide. refclk max is 96 MHz |
| 111 | * |
| 112 | */ |
| 113 | |
| 114 | ufcr_rfdiv = ((base->ufcr) & UFCR_RFDIV) >> 7; |
| 115 | if (ufcr_rfdiv == 6) |
| 116 | ufcr_rfdiv = 7; |
| 117 | else |
| 118 | ufcr_rfdiv = 6 - ufcr_rfdiv; |
| 119 | |
| 120 | refclk = get_PERCLK1(); |
| 121 | refclk /= ufcr_rfdiv; |
| 122 | |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 123 | /* Set the numerator value minus one of the BRM ratio */ |
Andrew Dyer | 3207699 | 2008-09-12 02:20:46 +0200 | [diff] [blame] | 124 | base->ubir = (gd->baudrate / 100) - 1; |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 125 | |
| 126 | /* Set the denominator value minus one of the BRM ratio */ |
Andrew Dyer | 3207699 | 2008-09-12 02:20:46 +0200 | [diff] [blame] | 127 | base->ubmr = (refclk/(16 * 100)) - 1; |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 128 | |
| 129 | /* Set to 8N1 */ |
| 130 | base->ucr2 &= ~UCR2_PREN; |
| 131 | base->ucr2 |= UCR2_WS; |
| 132 | base->ucr2 &= ~UCR2_STPB; |
| 133 | |
| 134 | /* Ignore RTS */ |
| 135 | base->ucr2 |= UCR2_IRTS; |
| 136 | |
| 137 | /* Enable UART */ |
| 138 | base->ucr1 |= UCR1_UARTEN | UCR1_UARTCLKEN; |
| 139 | |
| 140 | /* Enable FIFOs */ |
| 141 | base->ucr2 |= UCR2_SRST | UCR2_RXEN | UCR2_TXEN; |
| 142 | |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 143 | /* Clear status flags */ |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 144 | base->usr2 |= USR2_ADET | |
Andrew Dyer | 3207699 | 2008-09-12 02:20:46 +0200 | [diff] [blame] | 145 | USR2_DTRF | |
| 146 | USR2_IDLE | |
| 147 | USR2_IRINT | |
| 148 | USR2_WAKE | |
| 149 | USR2_RTSF | |
| 150 | USR2_BRCD | |
| 151 | USR2_ORE; |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 152 | |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 153 | /* Clear status flags */ |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 154 | base->usr1 |= USR1_PARITYERR | |
Andrew Dyer | 3207699 | 2008-09-12 02:20:46 +0200 | [diff] [blame] | 155 | USR1_RTSD | |
| 156 | USR1_ESCF | |
| 157 | USR1_FRAMERR | |
| 158 | USR1_AIRINT | |
| 159 | USR1_AWAKE; |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 160 | return (0); |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | * Read a single byte from the serial port. Returns 1 on success, 0 |
| 165 | * otherwise. When the function is successful, the character read is |
| 166 | * written into its argument c. |
| 167 | */ |
Marek Vasut | 9784bde | 2012-09-14 22:34:51 +0200 | [diff] [blame] | 168 | static int imx_serial_getc(void) |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 169 | { |
| 170 | volatile struct imx_serial* base = (struct imx_serial *)UART_BASE; |
| 171 | unsigned char ch; |
| 172 | |
| 173 | while(base->uts & UTS_RXEMPTY); |
| 174 | |
| 175 | ch = (char)base->urxd[0]; |
| 176 | |
| 177 | return ch; |
| 178 | } |
| 179 | |
| 180 | #ifdef CONFIG_HWFLOW |
| 181 | static int hwflow = 0; /* turned off by default */ |
| 182 | int hwflow_onoff(int on) |
| 183 | { |
| 184 | } |
| 185 | #endif |
| 186 | |
| 187 | /* |
| 188 | * Output a single byte to the serial port. |
| 189 | */ |
Marek Vasut | 9784bde | 2012-09-14 22:34:51 +0200 | [diff] [blame] | 190 | static void imx_serial_putc(const char c) |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 191 | { |
| 192 | volatile struct imx_serial* base = (struct imx_serial *)UART_BASE; |
| 193 | |
| 194 | /* Wait for Tx FIFO not full */ |
| 195 | while (base->uts & UTS_TXFULL); |
| 196 | |
| 197 | base->utxd[0] = c; |
| 198 | |
| 199 | /* If \n, also do \r */ |
| 200 | if (c == '\n') |
| 201 | serial_putc ('\r'); |
| 202 | } |
| 203 | |
| 204 | /* |
| 205 | * Test whether a character is in the RX buffer |
| 206 | */ |
Marek Vasut | 9784bde | 2012-09-14 22:34:51 +0200 | [diff] [blame] | 207 | static int imx_serial_tstc(void) |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 208 | { |
| 209 | volatile struct imx_serial* base = (struct imx_serial *)UART_BASE; |
| 210 | |
| 211 | /* If receive fifo is empty, return false */ |
| 212 | if (base->uts & UTS_RXEMPTY) |
| 213 | return 0; |
| 214 | return 1; |
| 215 | } |
| 216 | |
Marek Vasut | 9784bde | 2012-09-14 22:34:51 +0200 | [diff] [blame] | 217 | static void imx_serial_puts(const char *s) |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 218 | { |
| 219 | while (*s) { |
| 220 | serial_putc (*s++); |
| 221 | } |
| 222 | } |
Marek Vasut | 9784bde | 2012-09-14 22:34:51 +0200 | [diff] [blame] | 223 | |
Marek Vasut | 9784bde | 2012-09-14 22:34:51 +0200 | [diff] [blame] | 224 | static struct serial_device imx_serial_drv = { |
| 225 | .name = "imx_serial", |
| 226 | .start = imx_serial_init, |
| 227 | .stop = NULL, |
| 228 | .setbrg = imx_serial_setbrg, |
| 229 | .putc = imx_serial_putc, |
| 230 | .puts = imx_serial_puts, |
| 231 | .getc = imx_serial_getc, |
| 232 | .tstc = imx_serial_tstc, |
| 233 | }; |
| 234 | |
| 235 | void imx_serial_initialize(void) |
| 236 | { |
| 237 | serial_register(&imx_serial_drv); |
| 238 | } |
| 239 | |
| 240 | __weak struct serial_device *default_serial_console(void) |
| 241 | { |
| 242 | return &imx_serial_drv; |
| 243 | } |