summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/arm/shared/arm-pl011.c
blob: 9d613cf73f980c902df5c7bb2e56dbfbea4f88e8 (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
/*
 * Copyright (c) 2013 embedded brains GmbH.  All rights reserved.
 *
 *  embedded brains GmbH
 *  Dornierstr. 4
 *  82178 Puchheim
 *  Germany
 *  <info@embedded-brains.de>
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rtems.org/license/LICENSE.
 */

#include <bsp/arm-pl011.h>
#include <bsp/arm-pl011-regs.h>

#include <libchip/sersupp.h>

static volatile pl011 *pl011_get_regs(int minor)
{
  const console_tbl *ct = Console_Port_Tbl != NULL ?
    Console_Port_Tbl[minor] : &Console_Configuration_Ports[minor];

  return (volatile pl011 *) ct->ulCtrlPort1;
}

static void pl011_initialize(int minor)
{
  volatile pl011 *regs = pl011_get_regs(minor);

  regs->uartlcr_h = PL011_UARTLCR_H_WLEN(PL011_UARTLCR_H_WLEN_8);
  regs->uartcr = PL011_UARTCR_RXE
    | PL011_UARTCR_TXE
    | PL011_UARTCR_UARTEN;
}

static int pl011_first_open(int major, int minor, void *arg)
{
  rtems_libio_open_close_args_t *oc = (rtems_libio_open_close_args_t *) arg;
  struct rtems_termios_tty *tty = (struct rtems_termios_tty *) oc->iop->data1;
  console_data *cd = &Console_Port_Data[minor];
  const console_tbl *ct = Console_Port_Tbl[minor];

  cd->termios_data = tty;
  rtems_termios_set_initial_baud(tty, (rtems_termios_baud_t) ct->pDeviceParams);

  return 0;
}

static int pl011_last_close(int major, int minor, void *arg)
{
  return 0;
}

static int pl011_read_polled(int minor)
{
  volatile pl011 *regs = pl011_get_regs(minor);

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

static void pl011_write_polled(int minor, char c)
{
  volatile pl011 *regs = pl011_get_regs(minor);

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

  regs->uartdr = PL011_UARTDR_DATA(c);
}

static ssize_t pl011_write_support_polled(
  int minor,
  const char *s,
  size_t n
)
{
  ssize_t i = 0;

  for (i = 0; i < n; ++i) {
    pl011_write_polled(minor, s[i]);
  }

  return n;
}

static int pl011_set_attribues(int minor, const struct termios *term)
{
  return -1;
}

const console_fns arm_pl011_fns = {
  .deviceProbe = libchip_serial_default_probe,
  .deviceFirstOpen = pl011_first_open,
  .deviceLastClose = pl011_last_close,
  .deviceRead = pl011_read_polled,
  .deviceWrite = pl011_write_support_polled,
  .deviceInitialize = pl011_initialize,
  .deviceWritePolled = pl011_write_polled,
  .deviceSetAttributes = pl011_set_attribues,
  .deviceOutputUsesInterrupts = false
};