source: rtems/c/src/lib/libcpu/arm/at91rm9200/dbgu/dbgu.c @ 010d830

4.115
Last change on this file since 010d830 was 010d830, checked in by Joel Sherrill <joel.sherrill@…>, on Aug 15, 2010 at 11:28:46 PM

2010-08-15 Joel Sherrill <joel.sherrilL@…>

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