1 | /* |
---|
2 | * Console driver for AT91RM9200 DBGU port |
---|
3 | * |
---|
4 | * This driver uses the shared console driver in |
---|
5 | * ...../libbsp/shared/console.c |
---|
6 | * |
---|
7 | * Copyright (c) 2003 by Cogent Computer Systems |
---|
8 | * Written by Mike Kelly <mike@cogcomp.com> |
---|
9 | * and Jay Monkman <jtm@lopingdog.com> |
---|
10 | * |
---|
11 | * The license and distribution terms for this file may be |
---|
12 | * found in the file LICENSE in this distribution or at |
---|
13 | * http://www.rtems.org/license/LICENSE. |
---|
14 | */ |
---|
15 | #include <bsp.h> |
---|
16 | #include <rtems/libio.h> |
---|
17 | #include <termios.h> |
---|
18 | |
---|
19 | #include <at91rm9200.h> |
---|
20 | #include <at91rm9200_dbgu.h> |
---|
21 | #include <at91rm9200_pmc.h> |
---|
22 | #include <rtems/bspIo.h> |
---|
23 | #include <libchip/serial.h> |
---|
24 | #include <libchip/sersupp.h> |
---|
25 | |
---|
26 | volatile int dbg_dly; |
---|
27 | |
---|
28 | /* static function prototypes */ |
---|
29 | static int dbgu_first_open(int major, int minor, void *arg); |
---|
30 | static int dbgu_last_close(int major, int minor, void *arg); |
---|
31 | static int dbgu_read(int minor); |
---|
32 | static ssize_t dbgu_write(int minor, const char *buf, size_t len); |
---|
33 | static void dbgu_init(int minor); |
---|
34 | static void dbgu_write_polled(int minor, char c); |
---|
35 | static int dbgu_set_attributes(int minor, const struct termios *t); |
---|
36 | |
---|
37 | /* Pointers to functions for handling the UART. */ |
---|
38 | const console_fns dbgu_fns = |
---|
39 | { |
---|
40 | libchip_serial_default_probe, |
---|
41 | dbgu_first_open, |
---|
42 | dbgu_last_close, |
---|
43 | dbgu_read, |
---|
44 | dbgu_write, |
---|
45 | dbgu_init, |
---|
46 | dbgu_write_polled, /* not used in this driver */ |
---|
47 | dbgu_set_attributes, |
---|
48 | FALSE /* TRUE if interrupt driven, FALSE if not. */ |
---|
49 | }; |
---|
50 | /*********************************************************************/ |
---|
51 | /* Functions called via callbacks (i.e. the ones in uart_fns */ |
---|
52 | /*********************************************************************/ |
---|
53 | |
---|
54 | /* |
---|
55 | * This is called the first time each device is opened. Since |
---|
56 | * the driver is polled, we don't have to do anything. If the driver |
---|
57 | * were interrupt driven, we'd enable interrupts here. |
---|
58 | */ |
---|
59 | static int dbgu_first_open(int major, int minor, void *arg) |
---|
60 | { |
---|
61 | return 0; |
---|
62 | } |
---|
63 | |
---|
64 | |
---|
65 | /* |
---|
66 | * This is called the last time each device is closed. Since |
---|
67 | * the driver is polled, we don't have to do anything. If the driver |
---|
68 | * were interrupt driven, we'd disable interrupts here. |
---|
69 | */ |
---|
70 | static int dbgu_last_close(int major, int minor, void *arg) |
---|
71 | { |
---|
72 | return 0; |
---|
73 | } |
---|
74 | |
---|
75 | |
---|
76 | /* |
---|
77 | * Read one character from UART. |
---|
78 | * |
---|
79 | * return -1 if there's no data, otherwise return |
---|
80 | * the character in lowest 8 bits of returned int. |
---|
81 | */ |
---|
82 | static int dbgu_read(int minor) |
---|
83 | { |
---|
84 | char c; |
---|
85 | console_tbl *console_entry; |
---|
86 | at91rm9200_dbgu_regs_t *dbgu; |
---|
87 | |
---|
88 | console_entry = BSP_get_uart_from_minor(minor); |
---|
89 | |
---|
90 | if (console_entry == NULL) { |
---|
91 | return -1; |
---|
92 | } |
---|
93 | |
---|
94 | dbgu = (at91rm9200_dbgu_regs_t *)console_entry->ulCtrlPort1; |
---|
95 | |
---|
96 | if (!(dbgu->sr & DBGU_INT_RXRDY)) { |
---|
97 | return -1; |
---|
98 | } |
---|
99 | |
---|
100 | c = dbgu->rhr & 0xff; |
---|
101 | |
---|
102 | return c; |
---|
103 | } |
---|
104 | |
---|
105 | |
---|
106 | /* |
---|
107 | * Write buffer to UART |
---|
108 | * |
---|
109 | * return 1 on success, -1 on error |
---|
110 | */ |
---|
111 | static ssize_t dbgu_write(int minor, const char *buf, size_t len) |
---|
112 | { |
---|
113 | int i, x; |
---|
114 | char c; |
---|
115 | console_tbl *console_entry; |
---|
116 | at91rm9200_dbgu_regs_t *dbgu; |
---|
117 | |
---|
118 | console_entry = BSP_get_uart_from_minor(minor); |
---|
119 | |
---|
120 | if (console_entry == NULL) { |
---|
121 | return -1; |
---|
122 | } |
---|
123 | |
---|
124 | dbgu = (at91rm9200_dbgu_regs_t *)console_entry->ulCtrlPort1; |
---|
125 | |
---|
126 | for (i = 0; i < len; i++) { |
---|
127 | /* Wait for fifo to have room */ |
---|
128 | while(1) { |
---|
129 | if (dbgu->sr & DBGU_INT_TXRDY) { |
---|
130 | break; |
---|
131 | } |
---|
132 | } |
---|
133 | |
---|
134 | c = (char) buf[i]; |
---|
135 | dbgu->thr = c; |
---|
136 | |
---|
137 | /* the TXRDY flag does not seem to update right away (is this true?) */ |
---|
138 | /* so we wait a bit before continuing */ |
---|
139 | for (x = 0; x < 100; x++) { |
---|
140 | dbg_dly++; /* using a global so this doesn't get optimized out */ |
---|
141 | } |
---|
142 | } |
---|
143 | |
---|
144 | return 1; |
---|
145 | } |
---|
146 | |
---|
147 | |
---|
148 | /* Set up the UART. */ |
---|
149 | static void dbgu_init(int minor) |
---|
150 | { |
---|
151 | console_tbl *console_entry; |
---|
152 | at91rm9200_dbgu_regs_t *dbgu; |
---|
153 | |
---|
154 | console_entry = BSP_get_uart_from_minor(minor); |
---|
155 | |
---|
156 | if (console_entry == NULL) { |
---|
157 | return; |
---|
158 | } |
---|
159 | |
---|
160 | dbgu = (at91rm9200_dbgu_regs_t *)console_entry->ulCtrlPort1; |
---|
161 | |
---|
162 | /* Clear error bits, and reset */ |
---|
163 | dbgu->cr = (DBGU_CR_RSTSTA | DBGU_CR_RSTTX | DBGU_CR_RSTRX); |
---|
164 | |
---|
165 | /* Clear pending interrupts */ |
---|
166 | dbgu->idr = DBGU_INT_ALL; |
---|
167 | dbgu->imr = 0; |
---|
168 | |
---|
169 | /* Set port to no parity, no loopback */ |
---|
170 | dbgu->mr = DBGU_MR_PAR_NONE | DBGU_MR_CHMODE_NORM; |
---|
171 | |
---|
172 | /* Set the baud rate */ |
---|
173 | dbgu->brgr = (at91rm9200_get_mck() / 16) / BSP_get_baud(); |
---|
174 | |
---|
175 | /* Enable the DBGU */ |
---|
176 | dbgu->cr = (DBGU_CR_TXEN | DBGU_CR_RXEN); |
---|
177 | } |
---|
178 | |
---|
179 | /* This is used for getchark support */ |
---|
180 | static void dbgu_write_polled(int minor, char c) |
---|
181 | { |
---|
182 | dbgu_write(minor, &c, 1); |
---|
183 | } |
---|
184 | |
---|
185 | /* This is for setting baud rate, bits, etc. */ |
---|
186 | static int dbgu_set_attributes(int minor, const struct termios *t) |
---|
187 | { |
---|
188 | return 0; |
---|
189 | } |
---|
190 | |
---|
191 | /***********************************************************************/ |
---|
192 | /* |
---|
193 | * The following functions are not used by TERMIOS, but other RTEMS |
---|
194 | * functions use them instead. |
---|
195 | */ |
---|
196 | /***********************************************************************/ |
---|
197 | /* |
---|
198 | * Read from UART. This is used in the exit code, and can't |
---|
199 | * rely on interrupts. |
---|
200 | */ |
---|
201 | static int dbgu_poll_read(int minor) |
---|
202 | { |
---|
203 | return dbgu_read(minor); |
---|
204 | } |
---|
205 | |
---|
206 | |
---|
207 | /* |
---|
208 | * Write a character to the console. This is used by printk() and |
---|
209 | * maybe other low level functions. It should not use interrupts or any |
---|
210 | * RTEMS system calls. It needs to be very simple |
---|
211 | */ |
---|
212 | static void _BSP_put_char( char c ) { |
---|
213 | dbgu_write_polled(0, c); |
---|
214 | if ( c == '\n' ) |
---|
215 | dbgu_write_polled(0, '\r'); |
---|
216 | } |
---|
217 | |
---|
218 | BSP_output_char_function_type BSP_output_char = _BSP_put_char; |
---|
219 | |
---|
220 | static int _BSP_poll_char(void) |
---|
221 | { |
---|
222 | return dbgu_poll_read(0); |
---|
223 | } |
---|
224 | |
---|
225 | BSP_polling_getchar_function_type BSP_poll_char = _BSP_poll_char; |
---|