summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libcpu/arm/at91rm9200/usart/usart.c
blob: 2a4737c3a880eeb781fcc779680080e7f50eb869 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
 *  Driver for AT91RM9200 USART ports
 *
 * COPYRIGHT (c) 2006-2009.
 * NCB - Sistemas Embarcados Ltda. (Brazil)
 * Fernando Nicodemos <fgnicodemos@terra.com.br>
 *
 * and
 *
 * COPYRIGHT (c) 1989-2009.
 * On-Line Applications Research Corporation (OAR).
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.com/license/LICENSE.
*/

#include <bsp.h>
#include <rtems/libio.h>
#include <termios.h>

#include <at91rm9200.h>
#include <at91rm9200_usart.h>
#include <at91rm9200_pmc.h>
#include <rtems/bspIo.h>
#include <libchip/serial.h>
#include <libchip/sersupp.h>

/* static function prototypes */
static int     usart_first_open(int major, int minor, void *arg);
static int     usart_last_close(int major, int minor, void *arg);
static int     usart_read_polled(int minor);
static ssize_t usart_write_polled_support(int minor, const char *buf, size_t len);
static void    usart_init(int minor);
static void    usart_write_polled(int minor, char c);
static int     usart_set_attributes(int minor, const struct termios *t);
at91rm9200_usart_regs_t *usart_get_base(int minor);

/* Pointers to functions for handling the UART polled. */
console_fns usart_polling_fns = {
  libchip_serial_default_probe,       /* deviceProbe */
  usart_first_open,                   /* deviceFirstOpen */
  usart_last_close,                   /* deviceLastClose */
  usart_read_polled,                  /* deviceRead */
  usart_write_polled_support,         /* deviceWrite */
  usart_init,                         /* deviceInitialize */
  usart_write_polled,                 /* deviceWritePolled */
  usart_set_attributes,               /* deviceSetAttributes */
  FALSE                 /* TRUE if interrupt driven, FALSE if not. */
};

at91rm9200_usart_regs_t *usart_get_base(int minor)
{
  console_tbl *console_entry;
  at91rm9200_usart_regs_t *port;

  console_entry = BSP_get_uart_from_minor(minor);

  if (console_entry == NULL)
    return 0;

  port = (at91rm9200_usart_regs_t *) console_entry->ulCtrlPort1;
  //printk( "minor=%d entry=%p port=%p\n", minor, console_entry, port );

  return port;
}

/*
 * Functions called via callbacks (i.e. the ones in uart_fns
 */

/*
 * This is called the first time each device is opened. Since
 * the driver is polled, we don't have to do anything. If the driver
 * were interrupt driven, we'd enable interrupts here.
 */
static int usart_first_open(int major, int minor, void *arg)
{
  at91rm9200_usart_regs_t *usart;

  usart = usart_get_base(minor);
  if ( !usart )
    return -1;

  /* XXX port isn't being initialized or enabled */

  /* XXX I hope this is enough */
  usart->cr = (US_CR_RXEN | US_CR_TXEN);
  return 0;
}

/*
 * This is called the last time each device is closed.  Since
 * the driver is polled, we don't have to do anything. If the driver
 * were interrupt driven, we'd disable interrupts here.
 */
static int usart_last_close(int major, int minor, void *arg)
{
  at91rm9200_usart_regs_t *usart;

  usart = usart_get_base(minor);
  if ( !usart )
    return -1;

  return 0;
}

/*
 * Read one character from UART.
 *
 * return -1 if there's no data, otherwise return
 * the character in lowest 8 bits of returned int.
 */
static int usart_read_polled(int minor)
{
  at91rm9200_usart_regs_t *usart;

  usart = usart_get_base(minor);
  if ( !usart )
    return -1;

  /* if nothing ready return -1 */
  if ( (usart->sr & US_IER_RXRDY) == 0 )
    return -1;

  return usart->rhr;
}


/*
 *  Write character out
 */
static void usart_write_polled(int minor, char c)
{
  at91rm9200_usart_regs_t *usart;

  usart = usart_get_base(minor);
  if ( !usart )
    return;

  /* delay until TX empty */
  while ( (usart->sr & US_IER_TXEMPTY) == 0 )
    ;

  usart->thr = c;
}

/*
 * Write buffer to UART
 *
 * return 1 on success, -1 on error
 */
static ssize_t usart_write_polled_support(int minor, const char *buf, size_t len)
{
  at91rm9200_usart_regs_t *usart;
  int nwrite=0;

  /*
   *  Verify the minor number
   */
  usart = usart_get_base(minor);
  if ( !usart )
    return -1;

  /*
   * poll each byte in the string out of the port.
   */
  while (nwrite < len) {
    usart_write_polled(minor, *buf++);
    nwrite++;
  }

  /*
   * return the number of bytes written.
   */
  return nwrite;

  return 1;
}


/* Set up the UART. */
static void usart_init(int minor)
{
  at91rm9200_usart_regs_t *usart;

  usart = usart_get_base(minor);
  if ( !usart )
    return;

}


/* This is for setting baud rate, bits, etc. */
static int usart_set_attributes(int minor, const struct termios *t)
{
  uint32_t      brgr;
  uint32_t      mode, baud, baud_requested;
  at91rm9200_usart_regs_t *usart;

  usart = usart_get_base(minor);
  if ( !usart )
    return -1;

  /* Get current mode register */
  mode = usart->mr & ~(US_MR_USMODE | US_MR_USCLKS | US_MR_CHRL
			| US_MR_PAR | US_MR_NBSTOP);

  /* Byte size */
  switch (t->c_cflag & CSIZE){
  case CS5:
    mode |= US_MR_CHRL_5;
    break;
  case CS6:
    mode |= US_MR_CHRL_6;
    break;
  case CS7:
    mode |= US_MR_CHRL_7;
    break;
  default:
    mode |= US_MR_CHRL_8;
    break;
  }

  /* Stop bits */
  if (t->c_cflag & CSTOPB){
	mode |= US_MR_NBSTOP_2; /* 2 stop bits */
  } else
    mode |= US_MR_NBSTOP_1;     /* 1 stop bits */

  /* Parity */
  if (t->c_cflag & PARENB){
      /* Mark or Space parity */
      if (t->c_cflag & PARODD){
	mode |= US_MR_PAR_ODD;
      } else
	mode |= US_MR_PAR_EVEN;
   } else
	mode |= US_MR_PAR_NONE;

  baud_requested = t->c_cflag & CBAUD;

  /* If not, set the dbgu console baud as USART baud default */
  if (!baud_requested)
    baud_requested = BSP_get_baud();

  baud = rtems_termios_baud_to_number(baud_requested);

  brgr = (at91rm9200_get_mck() / 16) / baud;

	if (brgr > 65535){    /* BRGR is 16-bit, so switch to slower clock */
		brgr /= 8;
		mode |= US_MR_USCLKS_MCK_DIV8;
	}

  usart->mr = mode;
  usart->brgr = brgr;
  return 0;
}

/*
 * The following functions are not used by TERMIOS, but other RTEMS
 * functions use them instead.
 */

/*
 * Read from UART. This is used in the exit code, and can't
 * rely on interrupts.
 */
int usart_poll_read(int minor)
{
  return usart_read_polled(minor);
}