wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2002 |
| 3 | * Wolfgang Denk, DENX Software Engineering, <wd@denx.de> |
| 4 | * |
| 5 | * (C) Copyright 2002 |
| 6 | * Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 7 | * Marius Groeger <mgroeger@sysgo.de> |
| 8 | * |
| 9 | * (C) Copyright 2002 |
| 10 | * Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 11 | * Alex Zuepke <azu@sysgo.de> |
| 12 | * |
| 13 | * Copyright (C) 1999 2000 2001 Erik Mouw (J.A.K.Mouw@its.tudelft.nl) |
| 14 | * |
| 15 | * This program is free software; you can redistribute it and/or modify |
| 16 | * it under the terms of the GNU General Public License as published by |
| 17 | * the Free Software Foundation; either version 2 of the License, or |
| 18 | * (at your option) any later version. |
| 19 | * |
| 20 | * This program is distributed in the hope that it will be useful, |
| 21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 | * GNU General Public License for more details. |
| 24 | * |
| 25 | * You should have received a copy of the GNU General Public License |
| 26 | * along with this program; if not, write to the Free Software |
| 27 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 28 | * |
| 29 | */ |
| 30 | |
| 31 | #include <common.h> |
| 32 | #include <SA-1100.h> |
Marek Vasut | a30a422 | 2012-09-14 22:39:44 +0200 | [diff] [blame] | 33 | #include <serial.h> |
| 34 | #include <linux/compiler.h> |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 35 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 36 | DECLARE_GLOBAL_DATA_PTR; |
| 37 | |
Marek Vasut | a30a422 | 2012-09-14 22:39:44 +0200 | [diff] [blame] | 38 | static void sa1100_serial_setbrg(void) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 39 | { |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 40 | unsigned int reg = 0; |
| 41 | |
| 42 | if (gd->baudrate == 1200) |
| 43 | reg = 191; |
| 44 | else if (gd->baudrate == 9600) |
| 45 | reg = 23; |
| 46 | else if (gd->baudrate == 19200) |
| 47 | reg = 11; |
| 48 | else if (gd->baudrate == 38400) |
| 49 | reg = 5; |
| 50 | else if (gd->baudrate == 57600) |
| 51 | reg = 3; |
| 52 | else if (gd->baudrate == 115200) |
| 53 | reg = 1; |
| 54 | else |
| 55 | hang (); |
| 56 | |
| 57 | #ifdef CONFIG_SERIAL1 |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 58 | /* SA1110 uart function */ |
| 59 | Ser1SDCR0 |= SDCR0_SUS; |
| 60 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 61 | /* Wait until port is ready ... */ |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 62 | while(Ser1UTSR1 & UTSR1_TBY) {} |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 63 | |
| 64 | /* init serial serial 1 */ |
| 65 | Ser1UTCR3 = 0x00; |
| 66 | Ser1UTSR0 = 0xff; |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 67 | Ser1UTCR0 = ( UTCR0_1StpBit | UTCR0_8BitData ); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 68 | Ser1UTCR1 = 0; |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 69 | Ser1UTCR2 = (u32)reg; |
| 70 | Ser1UTCR3 = ( UTCR3_RXE | UTCR3_TXE ); |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 71 | #elif defined(CONFIG_SERIAL3) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 72 | /* Wait until port is ready ... */ |
| 73 | while (Ser3UTSR1 & UTSR1_TBY) { |
| 74 | } |
| 75 | |
| 76 | /* init serial serial 3 */ |
| 77 | Ser3UTCR3 = 0x00; |
| 78 | Ser3UTSR0 = 0xff; |
| 79 | Ser3UTCR0 = (UTCR0_1StpBit | UTCR0_8BitData); |
| 80 | Ser3UTCR1 = 0; |
| 81 | Ser3UTCR2 = (u32) reg; |
| 82 | Ser3UTCR3 = (UTCR3_RXE | UTCR3_TXE); |
| 83 | #else |
| 84 | #error "Bad: you didn't configured serial ..." |
| 85 | #endif |
| 86 | } |
| 87 | |
| 88 | |
| 89 | /* |
| 90 | * Initialise the serial port with the given baudrate. The settings |
| 91 | * are always 8 data bits, no parity, 1 stop bit, no start bits. |
| 92 | * |
| 93 | */ |
Marek Vasut | a30a422 | 2012-09-14 22:39:44 +0200 | [diff] [blame] | 94 | static int sa1100_serial_init(void) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 95 | { |
| 96 | serial_setbrg (); |
| 97 | |
| 98 | return (0); |
| 99 | } |
| 100 | |
| 101 | |
| 102 | /* |
| 103 | * Output a single byte to the serial port. |
| 104 | */ |
Marek Vasut | a30a422 | 2012-09-14 22:39:44 +0200 | [diff] [blame] | 105 | static void sa1100_serial_putc(const char c) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 106 | { |
| 107 | #ifdef CONFIG_SERIAL1 |
| 108 | /* wait for room in the tx FIFO on SERIAL1 */ |
| 109 | while ((Ser1UTSR0 & UTSR0_TFS) == 0); |
| 110 | |
| 111 | Ser1UTDR = c; |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 112 | #elif defined(CONFIG_SERIAL3) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 113 | /* wait for room in the tx FIFO on SERIAL3 */ |
| 114 | while ((Ser3UTSR0 & UTSR0_TFS) == 0); |
| 115 | |
| 116 | Ser3UTDR = c; |
| 117 | #endif |
| 118 | |
| 119 | /* If \n, also do \r */ |
| 120 | if (c == '\n') |
| 121 | serial_putc ('\r'); |
| 122 | } |
| 123 | |
| 124 | /* |
| 125 | * Read a single byte from the serial port. Returns 1 on success, 0 |
| 126 | * otherwise. When the function is succesfull, the character read is |
| 127 | * written into its argument c. |
| 128 | */ |
Marek Vasut | a30a422 | 2012-09-14 22:39:44 +0200 | [diff] [blame] | 129 | static int sa1100_serial_tstc(void) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 130 | { |
| 131 | #ifdef CONFIG_SERIAL1 |
| 132 | return Ser1UTSR1 & UTSR1_RNE; |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 133 | #elif defined(CONFIG_SERIAL3) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 134 | return Ser3UTSR1 & UTSR1_RNE; |
| 135 | #endif |
| 136 | } |
| 137 | |
| 138 | /* |
| 139 | * Read a single byte from the serial port. Returns 1 on success, 0 |
| 140 | * otherwise. When the function is succesfull, the character read is |
| 141 | * written into its argument c. |
| 142 | */ |
Marek Vasut | a30a422 | 2012-09-14 22:39:44 +0200 | [diff] [blame] | 143 | static int sa1100_serial_getc(void) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 144 | { |
| 145 | #ifdef CONFIG_SERIAL1 |
| 146 | while (!(Ser1UTSR1 & UTSR1_RNE)); |
| 147 | |
| 148 | return (char) Ser1UTDR & 0xff; |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 149 | #elif defined(CONFIG_SERIAL3) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 150 | while (!(Ser3UTSR1 & UTSR1_RNE)); |
| 151 | |
| 152 | return (char) Ser3UTDR & 0xff; |
| 153 | #endif |
| 154 | } |
| 155 | |
Marek Vasut | a30a422 | 2012-09-14 22:39:44 +0200 | [diff] [blame] | 156 | static struct serial_device sa1100_serial_drv = { |
| 157 | .name = "sa1100_serial", |
| 158 | .start = sa1100_serial_init, |
| 159 | .stop = NULL, |
| 160 | .setbrg = sa1100_serial_setbrg, |
| 161 | .putc = sa1100_serial_putc, |
Marek Vasut | ec3fd68 | 2012-10-06 14:07:02 +0000 | [diff] [blame] | 162 | .puts = default_serial_puts, |
Marek Vasut | a30a422 | 2012-09-14 22:39:44 +0200 | [diff] [blame] | 163 | .getc = sa1100_serial_getc, |
| 164 | .tstc = sa1100_serial_tstc, |
| 165 | }; |
| 166 | |
| 167 | void sa1100_serial_initialize(void) |
| 168 | { |
| 169 | serial_register(&sa1100_serial_drv); |
| 170 | } |
| 171 | |
| 172 | __weak struct serial_device *default_serial_console(void) |
| 173 | { |
| 174 | return &sa1100_serial_drv; |
| 175 | } |