summaryrefslogtreecommitdiffstats
path: root/cfe/cfe/arch/mips/board/bcm63xx_ram/src/dev_bcm63xx_uart.c
blob: 3b2ce63746cff760c3d444b5fc8f47d496155017 (plain)
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
#include "cfe.h"
#include "lib_types.h"
#include "lib_malloc.h"
#include "lib_printf.h"
#include "cfe_iocb.h"
#include "cfe_device.h"
#include "cfe_ioctl.h"

#include "sbmips.h"
#include "bsp_config.h"

#include "bcm_hwdefs.h"
#include "bcm_map.h"

static void bcm63xx_uart_probe(cfe_driver_t *drv,
                               unsigned long probe_a, unsigned long probe_b,
                               void *probe_ptr);

static int bcm63xx_uart_open(cfe_devctx_t *ctx);
static int bcm63xx_uart_read(cfe_devctx_t *ctx,iocb_buffer_t *buffer);
static int bcm63xx_uart_inpstat(cfe_devctx_t *ctx,iocb_inpstat_t *inpstat);
static int bcm63xx_uart_write(cfe_devctx_t *ctx,iocb_buffer_t *buffer);
static int bcm63xx_uart_ioctl(cfe_devctx_t *ctx,iocb_buffer_t *buffer);
static int bcm63xx_uart_close(cfe_devctx_t *ctx);

const static cfe_devdisp_t bcm63xx_uart_dispatch = {
    bcm63xx_uart_open,
    bcm63xx_uart_read,
    bcm63xx_uart_inpstat,
    bcm63xx_uart_write,
    bcm63xx_uart_ioctl,
    bcm63xx_uart_close,
    NULL,
    NULL
};


const cfe_driver_t bcm63xx_uart = {
    "BCM63xx DUART",
    "uart",
    CFE_DEV_SERIAL,
    &bcm63xx_uart_dispatch,
    bcm63xx_uart_probe
};


typedef struct bcm63xx_uart_s {
	int      baudrate;
} bcm63xx_uart_t;


static void bcm63xx_set_baudrate( bcm63xx_uart_t * softc )
{
    uint32_t baudwd;

    baudwd = (FPERIPH / softc->baudrate) / 16;
    if( baudwd & 0x1 ) {
        baudwd = baudwd / 2;
    } else {
        baudwd = baudwd / 2 - 1;
    }
    UART->baudword = baudwd;
}


static void bcm63xx_uart_probe(cfe_driver_t *drv,
                               unsigned long probe_a, unsigned long probe_b,
                               void *probe_ptr)
{
	bcm63xx_uart_t * softc;
	char descr[80];

	/* enable the transmitter interrupt? */

    /*
     * probe_a is the DUART base address.
     * probe_b is the channel-number-within-duart (0 or 1)
     * probe_ptr is unused.
     */
    softc = (bcm63xx_uart_t *) KMALLOC(sizeof(bcm63xx_uart_t),0);
    if (softc) {
		xsprintf( descr, "%s channel %d", drv->drv_description, probe_b );
		cfe_attach( drv, softc, NULL, descr );
	}
}

static int bcm63xx_uart_open(cfe_devctx_t *ctx)
{
	bcm63xx_uart_t * softc = ctx->dev_softc;

	/* Enable the UART clock */
	softc->baudrate = CFG_SERIAL_BAUD_RATE;
	bcm63xx_set_baudrate( softc );

	UART->control = BRGEN | TXEN | RXEN;
    UART->config = BITS8SYM | ONESTOP;
    UART->fifoctl = RSTTXFIFOS | RSTRXFIFOS;
    UART->intMask  = 0;

	return 0;
}


static int bcm63xx_uart_read(cfe_devctx_t *ctx,iocb_buffer_t *buffer)
{
	unsigned char * bptr;
	int blen;
	uint32_t status;
	char inval;

	bptr = buffer->buf_ptr;
	blen = buffer->buf_length;

    while (blen > 0) {
		status = UART->intStatus;
        if(status & (RXOVFERR | RXPARERR | RXFRAMERR | RXBRK)) {
            /* RX over flow */
            if(status & RXOVFERR) {
                /* reset RX FIFO to clr interrupt */
                UART->fifoctl |= RSTRXFIFOS;
            }

            /* other errors just read the bad character to clear the bit */
            inval = UART->Data;
        }
        else if(status & RXFIFONE) {
			*bptr++ = UART->Data;
			blen--;
        }
        else
            break;
    }

	buffer->buf_retlen = buffer->buf_length - blen;
	return 0;
}


static int bcm63xx_uart_inpstat(cfe_devctx_t *ctx,iocb_inpstat_t *inpstat)
{
	inpstat->inp_status = UART->intStatus & RXFIFONE;
	return 0;
}


static int bcm63xx_uart_write(cfe_devctx_t *ctx,iocb_buffer_t *buffer)
{
	unsigned char * bptr;
	int blen;
	uint32_t status;

	bptr = buffer->buf_ptr;
	blen = buffer->buf_length;

	status = 0;
	while( (blen > 0) && !status ) {
		/* Wait for the buffer to empty before we write the next character */
		/* FIXME - The serial port should be able to accept more than one  */
		/*         character at a time.  Why doesn't it work though?       */
		do {
			status = UART->intStatus & TXFIFOEMT;
		} while( !status );
		UART->Data = *bptr;
		bptr++;
		blen--;

		status  = UART->intStatus & (TXOVFERR|TXUNDERR);
	}

	if( status ) {
		/* Reset TX FIFO */
        UART->fifoctl |= RSTTXFIFOS;
		blen++;
	}

	buffer->buf_retlen = buffer->buf_length - blen;
	return 0;
}


static int bcm63xx_uart_ioctl(cfe_devctx_t *ctx,iocb_buffer_t *buffer)
{
	bcm63xx_uart_t * softc = ctx->dev_softc;
	unsigned int * info = (unsigned int *) buffer->buf_ptr;

    switch ((int)buffer->buf_ioctlcmd) {
	case IOCTL_SERIAL_GETSPEED:
	    *info = softc->baudrate;
	    break;
	case IOCTL_SERIAL_SETSPEED:
	    softc->baudrate = *info;
		bcm63xx_set_baudrate( softc );
	    break;
	case IOCTL_SERIAL_GETFLOW:
    	*info = SERIAL_FLOW_NONE;
	    break;
	case IOCTL_SERIAL_SETFLOW:
	    break;
	default:
	    return -1;
	}

	return 0;
}


static int bcm63xx_uart_close(cfe_devctx_t *ctx)
{
	/* Turn off the UART clock. */
	return 0;
}