summaryrefslogtreecommitdiffstats
path: root/bsps/aarch64/xilinx-versal/dev/serial/versal-uart-polled.c
blob: 9453dc248bbc39f77435ad0906fb57b3f60be356 (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
/* SPDX-License-Identifier: BSD-2-Clause */

/*
 * Copyright (C) 2021 Gedare Bloom <gedare@rtems.org>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <dev/serial/versal-uart.h>
#include <dev/serial/versal-uart-regs.h>

#include <bspopts.h>

static inline volatile versal_uart *versal_uart_get_regs(
  rtems_termios_device_context *base
)
{
  versal_uart_context *ctx = (versal_uart_context *) base;
  return ctx->regs;
}

/*
 * Make weak and let the user override.
 */
uint32_t versal_uart_input_clock(void) __attribute__ ((weak));

uint32_t versal_uart_input_clock(void)
{
  return VERSAL_CLOCK_UART;
}

int32_t versal_cal_baud_rate(
  uint32_t baudrate,
  uint32_t maxerror,
  uint32_t *ibdiv,
  uint32_t *fbdiv
)
{
  uint32_t calcbaudrate;
  uint32_t bauderror;
  uint32_t percenterror;
  uint32_t fbdivrnd;
  uint32_t inputclk = versal_uart_input_clock();

  /*
   * The baud rate cannot be larger than the UART clock / 16.
   */
  if ((baudrate * 16) > inputclk) {
    return -1;
  }

  /*
   * The UART clock cannot be larger than 16*65535*baudrate.
   * Could maybe use an estimate (inputclk / 2**16) to save a division.
   * This invariant gets checked below, by ensuring ibdiv < 2**16.
   */

  /*
   * The UART clock cannot be more than 5/3 times faster than the LPD_LSBUS_CLK
   *  - TODO?
   */

  /*
   * The baud rate divisor is a 16-bit integer and 6-bit fractional part.
   * It is equal to the UART clock / (16 * baudrate).
   */
  *ibdiv = inputclk / 16 / baudrate;
  if ( *ibdiv > 1<<16 ) {
    return -1;
  }

  /*
   * Find the fractional part. This can overflow with 32-bit arithmetic if
   * inputclk > 536870911, so use 64-bit. Unfortunately, this means we have
   * two 64-bit divisions here.
   */
  fbdivrnd = ((((uint64_t)inputclk / 16) << 7) / baudrate) & 0x1;
  *fbdiv = (((((uint64_t)inputclk / 16) << 6) / baudrate) & 0x3F) + fbdivrnd;

  /*
   * calculate the baudrate and check if the error is too large.
   */
  calcbaudrate = (((uint64_t)inputclk / 16) << 6) / ((*ibdiv << 6) | *fbdiv);

  if (baudrate > calcbaudrate) {
    bauderror = baudrate - calcbaudrate;
  } else {
    bauderror = calcbaudrate - baudrate;
  }

  percenterror = (bauderror * 100) / baudrate;

  if (maxerror < percenterror) {
    return -1;
  }

  return 0;
}

int versal_uart_initialize(rtems_termios_device_context *base)
{
  volatile versal_uart *regs = versal_uart_get_regs(base);
  uint32_t maxerr = 3;
  uint32_t ibauddiv = 0;
  uint32_t fbauddiv = 0;
  int rv;

  versal_uart_reset_tx_flush(base);

  rv = versal_cal_baud_rate(
      VERSAL_UART_DEFAULT_BAUD,
      maxerr,
      &ibauddiv,
      &fbauddiv
  );

  if ( rv < 0 ) {
    return rv;
  }

  /* Line control: 8-bit word length, no parity, no FIFO, 1 stop bit */
  regs->uartlcr_h = VERSAL_UARTLCR_H_WLEN( VERSAL_UARTLCR_H_WLEN_8 )
    | VERSAL_UARTLCR_H_FEN;

  /* Control: receive, transmit, uart enable, no CTS, no RTS, no loopback */
  regs->uartcr = VERSAL_UARTCR_RXE
    | VERSAL_UARTCR_TXE
    | VERSAL_UARTCR_UARTEN;

  regs->uartibrd = VERSAL_UARTIBRD_BAUD_DIVINT(ibauddiv);
  regs->uartfbrd = VERSAL_UARTFBRD_BAUD_DIVFRAC(fbauddiv);

  return 0;
}

int versal_uart_read_polled(rtems_termios_device_context *base)
{
  volatile versal_uart *regs = versal_uart_get_regs(base);

  if ((regs->uartfr & VERSAL_UARTFR_RXFE) != 0) {
    return -1;
  } else {
    return VERSAL_UARTDR_DATA_GET(regs->uartdr);
  }
}

void versal_uart_write_polled(
  rtems_termios_device_context *base,
  char c
)
{
  volatile versal_uart *regs = versal_uart_get_regs(base);

  while ((regs->uartfr & VERSAL_UARTFR_TXFF) != 0) {
    /* Wait while full */
  }

  regs->uartdr = VERSAL_UARTDR_DATA(c);
}

void versal_uart_reset_tx_flush(rtems_termios_device_context *base)
{
  volatile versal_uart *regs = versal_uart_get_regs(base);
  int c = 4;

  while (c-- > 0)
    versal_uart_write_polled(base, '\r');

  while ((regs->uartfr & VERSAL_UARTFR_TXFE) == 0) {
    /* Wait for empty */
  }
  while ((regs->uartfr & VERSAL_UARTFR_BUSY) != 0) {
    /* Wait for empty */
  }
}