source: rtems/bsps/arm/raspberrypi/console/usart.c @ d7d66d7

Last change on this file since d7d66d7 was d7d66d7, checked in by Sebastian Huber <sebastian.huber@…>, on Apr 19, 2018 at 4:28:01 AM

bsps: Move console drivers to bsps

This patch is a part of the BSP source reorganization.

Update #3285.

  • Property mode set to 100644
File size: 3.5 KB
Line 
1/**
2 * @file
3 *
4 * @ingroup raspberrypi_usart
5 *
6 * @brief USART support.
7 */
8
9/*
10 * Copyright (c) 2013 Alan Cudmore
11 *
12 *  The license and distribution terms for this file may be
13 *  found in the file LICENSE in this distribution or at
14 *
15 *  http://www.rtems.org/license/LICENSE
16 *
17 */
18
19#include <libchip/sersupp.h>
20
21#include <bsp.h>
22#include <bsp/irq.h>
23#include <bsp/usart.h>
24#include <bsp/raspberrypi.h>
25#include <rtems/bspIo.h>
26
27static void usart_delay(uint32_t n)
28{
29 volatile uint32_t i = 0;
30
31 for(i = 0; i < n; i++)
32   ;
33}
34
35#if 0
36/*
37 *  These will be useful when the driver supports interrupt driven IO.
38 */
39static rtems_vector_number usart_get_irq_number(const console_tbl *ct)
40{
41  return ct->ulIntVector;
42}
43
44static uint32_t usart_get_baud(const console_tbl *ct)
45{
46  return ct->ulClock;
47}
48#endif
49
50static void usart_set_baud(int minor, int baud)
51{
52 /*
53  * Nothing for now
54  */
55 return;
56}
57
58static void usart_initialize(int minor)
59{
60  unsigned int gpio_reg;
61
62  /*
63  ** Program GPIO pins for UART 0
64  */
65  gpio_reg = BCM2835_REG(BCM2835_GPIO_GPFSEL1);
66  gpio_reg &= ~(7<<12);    /* gpio14 */
67  gpio_reg |=  (4<<12);    /* alt0   */
68  gpio_reg &= ~(7<<15);    /* gpio15 */
69  gpio_reg |=  (4<<15);    /* alt0   */
70  BCM2835_REG(BCM2835_GPIO_GPFSEL1) = gpio_reg;
71
72  BCM2835_REG(BCM2835_GPIO_GPPUD) = 0;
73  usart_delay(150);
74  BCM2835_REG(BCM2835_GPIO_GPPUDCLK0) = (1<<14)|(1<<15);
75  usart_delay(150);
76  BCM2835_REG(BCM2835_GPIO_GPPUDCLK0) = 0;
77
78  /*
79  ** Init the PL011 UART
80  */
81  BCM2835_REG(BCM2835_UART0_CR)   = 0;
82  BCM2835_REG(BCM2835_UART0_ICR)  = 0x7FF;
83  BCM2835_REG(BCM2835_UART0_IMSC) = 0;
84  BCM2835_REG(BCM2835_UART0_IBRD) = 1;
85  BCM2835_REG(BCM2835_UART0_FBRD) = 40;
86  BCM2835_REG(BCM2835_UART0_LCRH) = 0x70;
87  BCM2835_REG(BCM2835_UART0_RSRECR) =  0;
88
89  BCM2835_REG(BCM2835_UART0_CR)   = 0x301;
90
91  BCM2835_REG(BCM2835_UART0_IMSC) = BCM2835_UART0_IMSC_RX;
92
93  usart_set_baud(minor, 115000);
94}
95
96static int usart_first_open(int major, int minor, void *arg)
97{
98  rtems_libio_open_close_args_t *oc = (rtems_libio_open_close_args_t *) arg;
99  struct rtems_termios_tty *tty = (struct rtems_termios_tty *) oc->iop->data1;
100  const console_tbl *ct = Console_Port_Tbl [minor];
101  console_data *cd = &Console_Port_Data [minor];
102
103  cd->termios_data = tty;
104  rtems_termios_set_initial_baud(tty, ct->ulClock);
105
106  return 0;
107}
108
109static int usart_last_close(int major, int minor, void *arg)
110{
111  return 0;
112}
113
114static int usart_read_polled(int minor)
115{
116  if (minor == 0) {
117    if (((BCM2835_REG(BCM2835_UART0_FR)) & BCM2835_UART0_FR_RXFE) == 0) {
118       return((BCM2835_REG(BCM2835_UART0_DR)) & 0xFF );
119    } else {
120      return -1;
121    }
122  } else {
123    printk("Unknown console minor number: %d\n", minor);
124    return -1;
125  }
126}
127
128static void usart_write_polled(int minor, char c)
129{
130   while (1) {
131     if ((BCM2835_REG(BCM2835_UART0_FR) & BCM2835_UART0_FR_TXFF) == 0)
132       break;
133   }
134   BCM2835_REG(BCM2835_UART0_DR) = c;
135}
136
137static ssize_t usart_write_support_polled(
138  int minor,
139  const char *s,
140  size_t n
141)
142{
143  ssize_t i = 0;
144
145  for (i = 0; i < n; ++i) {
146    usart_write_polled(minor, s [i]);
147  }
148
149  return n;
150}
151
152static int usart_set_attributes(int minor, const struct termios *term)
153{
154  return -1;
155}
156
157const console_fns bcm2835_usart_fns = {
158  .deviceProbe = libchip_serial_default_probe,
159  .deviceFirstOpen = usart_first_open,
160  .deviceLastClose = usart_last_close,
161  .deviceRead = usart_read_polled,
162  .deviceWrite = usart_write_support_polled,
163  .deviceInitialize = usart_initialize,
164  .deviceWritePolled = usart_write_polled,
165  .deviceSetAttributes = usart_set_attributes,
166  .deviceOutputUsesInterrupts = false
167};
Note: See TracBrowser for help on using the repository browser.