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

4.115
Last change on this file since cfaa366 was cfaa366, checked in by Joel Sherrill <joel.sherrill@…>, on 05/03/12 at 17:55:58

General - Remove extraneous blank line in license message

Many files had an extra blank line in the license text
found in the file header. This patch removes that line.

The script that did this also turned off execute permission
when it was turned on incorrectly.

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