blob: 45b6f8afd432426da2b6e8e20209f5673aaf1206 [file] [log] [blame]
wdenk9f664dd2004-06-09 21:50:45 +00001/*
2 * cpu/mc9328/serial.c
3 *
4 * (c) Copyright 2004
5 * Techware Information Technology, Inc.
6 * http://www.techware.com.tw/
7 *
8 * Ming-Len Wu <minglen_wu@techware.com.tw>
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23 * MA 02111-1307 USA
24 */
25
26
27#include <common.h>
28#include <mc9328.h>
29
30#if defined(CONFIG_UART1)
31/* GPIO PORT B */
32
33#define reg_GIUS MX1_GIUS_C
34#define reg_GPR MX1_GPR_B
35#define GPIO_MASK 0xFFFFE1FF
36#define UART_BASE 0x00206000
37
38
39#elif defined (CONFIG_UART2)
40/* GPIO PORT C */
41
42#define reg_GIUS MX1_GIUS_C
43#define reg_GPR MX1_GPR_C
44#define GPIO_MASK 0x0FFFFFFF
45#define UART_BASE 0x207000
46
47#endif
48
49#define reg_URXD (*((volatile u32 *)(UART_BASE+0x00)))
50#define reg_UTXD (*((volatile u32 *)(UART_BASE+0x40)))
51#define reg_UCR1 (*((volatile u32 *)(UART_BASE+0x80)))
52#define reg_UCR2 (*((volatile u32 *)(UART_BASE+0x84)))
53#define reg_UCR3 (*((volatile u32 *)(UART_BASE+0x88)))
54#define reg_UCR4 (*((volatile u32 *)(UART_BASE+0x8C)))
55#define reg_UFCR (*((volatile u32 *)(UART_BASE+0x90)))
56#define reg_USR1 (*((volatile u32 *)(UART_BASE+0x94)))
57#define reg_USR2 (*((volatile u32 *)(UART_BASE+0x98)))
58#define reg_UESC (*((volatile u32 *)(UART_BASE+0x9C)))
59#define reg_UTIM (*((volatile u32 *)(UART_BASE+0xA0)))
60#define reg_UBIR (*((volatile u32 *)(UART_BASE+0xA4)))
61#define reg_UBMR (*((volatile u32 *)(UART_BASE+0xA8)))
62#define reg_UBRC (*((volatile u32 *)(UART_BASE+0xAC)))
63
64#define TXFE_MASK 0x4000 /* Tx buffer empty */
65#define RDR_MASK 0x0001 /* receive data ready */
66
67
68void serial_setbrg (void) {
69
70/* config I/O pins for UART */
71
72 reg_GIUS &= GPIO_MASK;
73 reg_GPR &= GPIO_MASK;
74
75/* config UART */
76
77 reg_UCR1 = 5;
78 reg_UCR2 = 0x4027;
79 reg_UCR4 = 1;
80 reg_UFCR = 0xA81;
81
82 reg_UBIR = 0xF;
83 reg_UBMR = 0x8A;
84 reg_UBRC = 8;
85}
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 */
94
95int serial_init (void) {
96 serial_setbrg ();
97
98 return (0);
99}
100
101
102
103/*
104 * Read a single byte from the serial port. Returns 1 on success, 0
105 * otherwise. When the function is succesfull, the character read is
106 * written into its argument c.
107 */
108int serial_getc (void) {
109
110 while (!(reg_USR2 & RDR_MASK)) ; /* wait until RDR bit set */
111
112 return (u8)reg_URXD;
113}
114
115
116/*
117 * Output a single byte to the serial port.
118 */
119void serial_putc (const char c) {
120
121 while (!(reg_USR2 & TXFE_MASK)); /* wait until TXFE bit set */
122
123 reg_UTXD = (u16) c;
124
125 if (c == '\n') { /* carriage return ? append line-feed */
126 while (!(reg_USR2 & TXFE_MASK)); /* wait until TXFE bit set */
127 reg_UTXD = '\r';
128 }
129
130}
131
132
133/*
134 * Test whether a character is in the RX buffer
135 */
136int serial_tstc (void) {
137 return reg_USR2 & RDR_MASK;
138}
139
140
141void serial_puts (const char *s) {
142 while (*s) {
143 serial_putc (*s++);
144 }
145}
146