[98eb7e78] | 1 | /** |
---|
| 2 | * @file |
---|
| 3 | * |
---|
| 4 | * @ingroup raspberrypi_usart |
---|
| 5 | * |
---|
| 6 | * @brief USART support. |
---|
| 7 | */ |
---|
| 8 | |
---|
[c32b1ef] | 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 | * |
---|
[c499856] | 15 | * http://www.rtems.org/license/LICENSE |
---|
[c32b1ef] | 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> |
---|
[b83c23e6] | 25 | #include <rtems/bspIo.h> |
---|
[c32b1ef] | 26 | |
---|
| 27 | static void usart_delay(uint32_t n) |
---|
| 28 | { |
---|
[ddc8165] | 29 | volatile uint32_t i = 0; |
---|
| 30 | |
---|
| 31 | for(i = 0; i < n; i++) |
---|
| 32 | ; |
---|
[c32b1ef] | 33 | } |
---|
| 34 | |
---|
[ddc8165] | 35 | #if 0 |
---|
| 36 | /* |
---|
| 37 | * These will be useful when the driver supports interrupt driven IO. |
---|
| 38 | */ |
---|
[c32b1ef] | 39 | static rtems_vector_number usart_get_irq_number(const console_tbl *ct) |
---|
| 40 | { |
---|
[ddc8165] | 41 | return ct->ulIntVector; |
---|
[c32b1ef] | 42 | } |
---|
| 43 | |
---|
| 44 | static uint32_t usart_get_baud(const console_tbl *ct) |
---|
| 45 | { |
---|
[ddc8165] | 46 | return ct->ulClock; |
---|
[c32b1ef] | 47 | } |
---|
[ddc8165] | 48 | #endif |
---|
[c32b1ef] | 49 | |
---|
| 50 | static void usart_set_baud(int minor, int baud) |
---|
| 51 | { |
---|
[ddc8165] | 52 | /* |
---|
| 53 | * Nothing for now |
---|
| 54 | */ |
---|
| 55 | return; |
---|
[c32b1ef] | 56 | } |
---|
| 57 | |
---|
| 58 | static void usart_initialize(int minor) |
---|
| 59 | { |
---|
[ddc8165] | 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); |
---|
[c32b1ef] | 94 | } |
---|
| 95 | |
---|
| 96 | static 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 | |
---|
| 109 | static int usart_last_close(int major, int minor, void *arg) |
---|
| 110 | { |
---|
| 111 | return 0; |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | static int usart_read_polled(int minor) |
---|
| 115 | { |
---|
[ddc8165] | 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 { |
---|
[c32b1ef] | 120 | return -1; |
---|
[ddc8165] | 121 | } |
---|
| 122 | } else { |
---|
| 123 | printk("Unknown console minor number: %d\n", minor); |
---|
| 124 | return -1; |
---|
| 125 | } |
---|
[c32b1ef] | 126 | } |
---|
| 127 | |
---|
| 128 | static void usart_write_polled(int minor, char c) |
---|
| 129 | { |
---|
[ddc8165] | 130 | while (1) { |
---|
| 131 | if ((BCM2835_REG(BCM2835_UART0_FR) & BCM2835_UART0_FR_TXFF) == 0) |
---|
| 132 | break; |
---|
[c32b1ef] | 133 | } |
---|
| 134 | BCM2835_REG(BCM2835_UART0_DR) = c; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | static 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 | |
---|
[ddc8165] | 145 | for (i = 0; i < n; ++i) { |
---|
[c32b1ef] | 146 | usart_write_polled(minor, s [i]); |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | return n; |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | static int usart_set_attributes(int minor, const struct termios *term) |
---|
| 153 | { |
---|
| 154 | return -1; |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | const 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 | }; |
---|