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 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame^] | 15 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | #include <common.h> |
| 19 | #include <SA-1100.h> |
Marek Vasut | a30a422 | 2012-09-14 22:39:44 +0200 | [diff] [blame] | 20 | #include <serial.h> |
| 21 | #include <linux/compiler.h> |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 22 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 23 | DECLARE_GLOBAL_DATA_PTR; |
| 24 | |
Marek Vasut | a30a422 | 2012-09-14 22:39:44 +0200 | [diff] [blame] | 25 | static void sa1100_serial_setbrg(void) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 26 | { |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 27 | unsigned int reg = 0; |
| 28 | |
| 29 | if (gd->baudrate == 1200) |
| 30 | reg = 191; |
| 31 | else if (gd->baudrate == 9600) |
| 32 | reg = 23; |
| 33 | else if (gd->baudrate == 19200) |
| 34 | reg = 11; |
| 35 | else if (gd->baudrate == 38400) |
| 36 | reg = 5; |
| 37 | else if (gd->baudrate == 57600) |
| 38 | reg = 3; |
| 39 | else if (gd->baudrate == 115200) |
| 40 | reg = 1; |
| 41 | else |
| 42 | hang (); |
| 43 | |
| 44 | #ifdef CONFIG_SERIAL1 |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 45 | /* SA1110 uart function */ |
| 46 | Ser1SDCR0 |= SDCR0_SUS; |
| 47 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 48 | /* Wait until port is ready ... */ |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 49 | while(Ser1UTSR1 & UTSR1_TBY) {} |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 50 | |
| 51 | /* init serial serial 1 */ |
| 52 | Ser1UTCR3 = 0x00; |
| 53 | Ser1UTSR0 = 0xff; |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 54 | Ser1UTCR0 = ( UTCR0_1StpBit | UTCR0_8BitData ); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 55 | Ser1UTCR1 = 0; |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 56 | Ser1UTCR2 = (u32)reg; |
| 57 | Ser1UTCR3 = ( UTCR3_RXE | UTCR3_TXE ); |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 58 | #elif defined(CONFIG_SERIAL3) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 59 | /* Wait until port is ready ... */ |
| 60 | while (Ser3UTSR1 & UTSR1_TBY) { |
| 61 | } |
| 62 | |
| 63 | /* init serial serial 3 */ |
| 64 | Ser3UTCR3 = 0x00; |
| 65 | Ser3UTSR0 = 0xff; |
| 66 | Ser3UTCR0 = (UTCR0_1StpBit | UTCR0_8BitData); |
| 67 | Ser3UTCR1 = 0; |
| 68 | Ser3UTCR2 = (u32) reg; |
| 69 | Ser3UTCR3 = (UTCR3_RXE | UTCR3_TXE); |
| 70 | #else |
| 71 | #error "Bad: you didn't configured serial ..." |
| 72 | #endif |
| 73 | } |
| 74 | |
| 75 | |
| 76 | /* |
| 77 | * Initialise the serial port with the given baudrate. The settings |
| 78 | * are always 8 data bits, no parity, 1 stop bit, no start bits. |
| 79 | * |
| 80 | */ |
Marek Vasut | a30a422 | 2012-09-14 22:39:44 +0200 | [diff] [blame] | 81 | static int sa1100_serial_init(void) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 82 | { |
| 83 | serial_setbrg (); |
| 84 | |
| 85 | return (0); |
| 86 | } |
| 87 | |
| 88 | |
| 89 | /* |
| 90 | * Output a single byte to the serial port. |
| 91 | */ |
Marek Vasut | a30a422 | 2012-09-14 22:39:44 +0200 | [diff] [blame] | 92 | static void sa1100_serial_putc(const char c) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 93 | { |
| 94 | #ifdef CONFIG_SERIAL1 |
| 95 | /* wait for room in the tx FIFO on SERIAL1 */ |
| 96 | while ((Ser1UTSR0 & UTSR0_TFS) == 0); |
| 97 | |
| 98 | Ser1UTDR = c; |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 99 | #elif defined(CONFIG_SERIAL3) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 100 | /* wait for room in the tx FIFO on SERIAL3 */ |
| 101 | while ((Ser3UTSR0 & UTSR0_TFS) == 0); |
| 102 | |
| 103 | Ser3UTDR = c; |
| 104 | #endif |
| 105 | |
| 106 | /* If \n, also do \r */ |
| 107 | if (c == '\n') |
| 108 | serial_putc ('\r'); |
| 109 | } |
| 110 | |
| 111 | /* |
| 112 | * Read a single byte from the serial port. Returns 1 on success, 0 |
| 113 | * otherwise. When the function is succesfull, the character read is |
| 114 | * written into its argument c. |
| 115 | */ |
Marek Vasut | a30a422 | 2012-09-14 22:39:44 +0200 | [diff] [blame] | 116 | static int sa1100_serial_tstc(void) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 117 | { |
| 118 | #ifdef CONFIG_SERIAL1 |
| 119 | return Ser1UTSR1 & UTSR1_RNE; |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 120 | #elif defined(CONFIG_SERIAL3) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 121 | return Ser3UTSR1 & UTSR1_RNE; |
| 122 | #endif |
| 123 | } |
| 124 | |
| 125 | /* |
| 126 | * Read a single byte from the serial port. Returns 1 on success, 0 |
| 127 | * otherwise. When the function is succesfull, the character read is |
| 128 | * written into its argument c. |
| 129 | */ |
Marek Vasut | a30a422 | 2012-09-14 22:39:44 +0200 | [diff] [blame] | 130 | static int sa1100_serial_getc(void) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 131 | { |
| 132 | #ifdef CONFIG_SERIAL1 |
| 133 | while (!(Ser1UTSR1 & UTSR1_RNE)); |
| 134 | |
| 135 | return (char) Ser1UTDR & 0xff; |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 136 | #elif defined(CONFIG_SERIAL3) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 137 | while (!(Ser3UTSR1 & UTSR1_RNE)); |
| 138 | |
| 139 | return (char) Ser3UTDR & 0xff; |
| 140 | #endif |
| 141 | } |
| 142 | |
Marek Vasut | a30a422 | 2012-09-14 22:39:44 +0200 | [diff] [blame] | 143 | static struct serial_device sa1100_serial_drv = { |
| 144 | .name = "sa1100_serial", |
| 145 | .start = sa1100_serial_init, |
| 146 | .stop = NULL, |
| 147 | .setbrg = sa1100_serial_setbrg, |
| 148 | .putc = sa1100_serial_putc, |
Marek Vasut | ec3fd68 | 2012-10-06 14:07:02 +0000 | [diff] [blame] | 149 | .puts = default_serial_puts, |
Marek Vasut | a30a422 | 2012-09-14 22:39:44 +0200 | [diff] [blame] | 150 | .getc = sa1100_serial_getc, |
| 151 | .tstc = sa1100_serial_tstc, |
| 152 | }; |
| 153 | |
| 154 | void sa1100_serial_initialize(void) |
| 155 | { |
| 156 | serial_register(&sa1100_serial_drv); |
| 157 | } |
| 158 | |
| 159 | __weak struct serial_device *default_serial_console(void) |
| 160 | { |
| 161 | return &sa1100_serial_drv; |
| 162 | } |