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 08/15/10 at 23:28:46

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
RevLine 
[af85485]1/*
2 *  Console driver for AT91RM9200 DBGU port
3 *
[359e537]4 *  This driver uses the shared console driver in
[af85485]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 *
[93f4a906]14 *  http://www.rtems.com/license/LICENSE.
[af85485]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);
[ae3c037]35static ssize_t dbgu_write(int minor, const char *buf, size_t len);
[af85485]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. */
[359e537]41console_fns dbgu_fns =
42{
[af85485]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
[359e537]57/*
[af85485]58 * This is called the first time each device is opened. Since
[359e537]59 * the driver is polled, we don't have to do anything. If the driver
60 * were interrupt driven, we'd enable interrupts here.
[af85485]61 */
[359e537]62static int dbgu_first_open(int major, int minor, void *arg)
[af85485]63{
64    return 0;
65}
66
67
[359e537]68/*
[af85485]69 * This is called the last time each device is closed.  Since
[359e537]70 * the driver is polled, we don't have to do anything. If the driver
71 * were interrupt driven, we'd disable interrupts here.
[af85485]72 */
[359e537]73static int dbgu_last_close(int major, int minor, void *arg)
[af85485]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 */
[359e537]85static int dbgu_read(int minor)
[af85485]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    }
[359e537]102
103    c  = dbgu->rhr & 0xff;
104
[af85485]105    return c;
106}
107
108
[359e537]109/*
110 * Write buffer to UART
[af85485]111 *
112 * return 1 on success, -1 on error
113 */
[ae3c037]114static ssize_t dbgu_write(int minor, const char *buf, size_t len)
[af85485]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        }
[359e537]136
[af85485]137        c = (char) buf[i];
138        dbgu->thr = c;
[359e537]139
[af85485]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    }
[359e537]146
[af85485]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
[9b623ee]182/* This is used for getchark support */
[af85485]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. */
[359e537]189static int dbgu_set_attributes(int minor, const struct termios *t)
[af85485]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/***********************************************************************/
[359e537]200/*
[af85485]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/*
[359e537]211 * Write a character to the console. This is used by printk() and
[af85485]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 ) {
[3b8dfa66]216  dbgu_write_polled(0, c);
217  if ( c == '\n' )
218    dbgu_write_polled(0, '\r');
[af85485]219}
220
[010d830]221BSP_output_char_function_type     BSP_output_char = _BSP_put_char;
[af85485]222
[0e7cdc1e]223int _BSP_poll_char(void)
[9b623ee]224{
225  return dbgu_poll_read(0);
226}
[af85485]227
[9b623ee]228BSP_polling_getchar_function_type BSP_poll_char = _BSP_poll_char;
Note: See TracBrowser for help on using the repository browser.