1 /*
2 * Copyright (c) 2016-2022 Arm Limited
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "uart_pl011_drv.h"
18
19 #include <stddef.h>
20 #include "cmsis_compiler.h"
21
22 #define FREQ_IRLPBAUD16_MIN (1420000u) /* 1.42 MHz */
23 #define FREQ_IRLPBAUD16_MAX (2120000u) /* 2.12 MHz */
24 #define SAMPLING_FACTOR (16u)
25 #define UART_PL011_FBRD_WIDTH (6u)
26
27 /**
28 * \brief UART PL011 register map structure
29 */
30 struct _uart_pl011_reg_map_t {
31 volatile uint32_t uartdr; /*!< Offset: 0x000 (R/W) Data register */
32 union {
33 volatile uint32_t uartrsr;
34 /*!< Offset: 0x004 (R/ ) Receive status register */
35 volatile uint32_t uartecr;
36 /*!< Offset: 0x004 ( /W) Error clear register */
37 };
38 volatile uint32_t reserved_0[4]; /*!< Offset: 0x008-0x014 Reserved */
39 volatile uint32_t uartfr; /*!< Offset: 0x018 (R/ ) Flag register */
40 volatile uint32_t reserved_1; /*!< Offset: 0x01C Reserved */
41 volatile uint32_t uartilpr;
42 /*!< Offset: 0x020 (R/W) IrDA low-power counter register */
43 volatile uint32_t uartibrd;
44 /*!< Offset: 0x024 (R/W) Integer baud rate register */
45 volatile uint32_t uartfbrd;
46 /*!< Offset: 0x028 (R/W) Fractional baud rate register */
47 volatile uint32_t uartlcr_h;
48 /*!< Offset: 0x02C (R/W) Line control register */
49 volatile uint32_t uartcr;
50 /*!< Offset: 0x030 (R/W) Control register */
51 volatile uint32_t uartifls;
52 /*!< Offset: 0x034 (R/W) Interrupt FIFO level select register */
53 volatile uint32_t uartimsc;
54 /*!< Offset: 0x038 (R/W) Interrupt mask set/clear register */
55 volatile uint32_t uartris;
56 /*!< Offset: 0x03C (R/ ) Raw interrupt status register */
57 volatile uint32_t uartmis;
58 /*!< Offset: 0x040 (R/ ) Masked interrupt status register */
59 volatile uint32_t uarticr;
60 /*!< Offset: 0x044 ( /W) Interrupt clear register */
61 volatile uint32_t uartdmacr;
62 /*!< Offset: 0x048 (R/W) DMA control register */
63 volatile uint32_t reserved_2[13]; /*!< Offset: 0x04C-0x07C Reserved */
64 volatile uint32_t reserved_3[4];
65 /*!< Offset: 0x080-0x08C Reserved for test purposes */
66 volatile uint32_t reserved_4[976]; /*!< Offset: 0x090-0xFCC Reserved */
67 volatile uint32_t reserved_5[4];
68 /*!< Offset: 0xFD0-0xFDC Reserved for future ID expansion */
69 volatile uint32_t uartperiphid0;
70 /*!< Offset: 0xFE0 (R/ ) UARTPeriphID0 register */
71 volatile uint32_t uartperiphid1;
72 /*!< Offset: 0xFE4 (R/ ) UARTPeriphID1 register */
73 volatile uint32_t uartperiphid2;
74 /*!< Offset: 0xFE8 (R/ ) UARTPeriphID2 register */
75 volatile uint32_t uartperiphid3;
76 /*!< Offset: 0xFEC (R/ ) UARTPeriphID3 register */
77 volatile uint32_t uartpcellid0;
78 /*!< Offset: 0xFF0 (R/ ) UARTPCellID0 register */
79 volatile uint32_t uartpcellid1;
80 /*!< Offset: 0xFF4 (R/ ) UARTPCellID1 register */
81 volatile uint32_t uartpcellid2;
82 /*!< Offset: 0xFF8 (R/ ) UARTPCellID2 register */
83 volatile uint32_t uartpcellid3;
84 /*!< Offset: 0xFFC (R/ ) UARTPCellID3 register */
85 };
86
87 #define UART_PL011_UARTFR_CTS_MASK ( \
88 0x1u<<UART_PL011_UARTFR_CTS_OFF)
89 #define UART_PL011_UARTFR_DSR_MASK ( \
90 0x1u<<UART_PL011_UARTFR_DSR_OFF)
91 #define UART_PL011_UARTFR_DCD_MASK ( \
92 0x1u<<UART_PL011_UARTFR_DCD_OFF)
93 #define UART_PL011_UARTFR_BUSYBIT ( \
94 0x1u<<UART_PL011_UARTFR_BUSYBIT_OFF)
95 #define UART_PL011_UARTFR_RX_FIFO_EMPTY ( \
96 0x1u<<UART_PL011_UARTFR_RX_FIFO_EMPTY_OFF)
97 #define UART_PL011_UARTFR_TX_FIFO_FULL ( \
98 0x1u<<UART_PL011_UARTFR_TX_FIFO_FULL_OFF)
99 #define UART_PL011_UARTFR_RI_MASK ( \
100 0x1u<<UART_PL011_UARTFR_RI_OFF)
101
102 #define UART_PL011_UARTLCR_H_BRK_MASK ( \
103 0x1u<<UART_PL011_UARTLCR_H_BRK_OFF)
104 #define UART_PL011_UARTLCR_H_PARITY_MASK ( \
105 0x1u<<UART_PL011_UARTLCR_H_PEN_OFF \
106 | 0x1u<<UART_PL011_UARTLCR_H_EPS_OFF \
107 | 0x1u<<UART_PL011_UARTLCR_H_SPS_OFF)
108 #define UART_PL011_UARTLCR_H_STOPBIT_MASK ( \
109 0x1u<<UART_PL011_UARTLCR_H_STP2_OFF)
110 #define UART_PL011_UARTLCR_H_FEN_MASK ( \
111 0x1u<<UART_PL011_UARTLCR_H_FEN_OFF)
112 #define UART_PL011_UARTLCR_H_WLEN_MASK ( \
113 0x3u<<UART_PL011_UARTLCR_H_WLEN_OFF)
114 #define UART_PL011_FORMAT_MASK ( \
115 UART_PL011_UARTLCR_H_PARITY_MASK \
116 | UART_PL011_UARTLCR_H_STOPBIT_MASK \
117 | UART_PL011_UARTLCR_H_WLEN_MASK)
118
119 #define UART_PL011_UARTCR_EN_MASK ( \
120 0x1u<<UART_PL011_UARTCR_UARTEN_OFF)
121 #define UART_PL011_UARTCR_SIREN_MASK ( \
122 0x1u<<UART_PL011_UARTCR_SIREN_OFF)
123 #define UART_PL011_UARTCR_SIRLP_MASK ( \
124 0x1u<<UART_PL011_UARTCR_SIRLP_OFF)
125 #define UART_PL011_UARTCR_LBE_MASK ( \
126 0x1u<<UART_PL011_UARTCR_LBE_OFF)
127 #define UART_PL011_UARTCR_TX_EN_MASK ( \
128 0x1u<<UART_PL011_UARTCR_TXE_OFF)
129 #define UART_PL011_UARTCR_RX_EN_MASK ( \
130 0x1u<<UART_PL011_UARTCR_RXE_OFF)
131 #define UART_PL011_UARTCR_DTR_MASK ( \
132 0x1u<<UART_PL011_UARTCR_DTR_OFF)
133 #define UART_PL011_UARTCR_RTS_MASK ( \
134 0x1u<<UART_PL011_UARTCR_RTS_OFF)
135 #define UART_PL011_UARTCR_OUT1_MASK ( \
136 0x1u<<UART_PL011_UARTCR_OUT1_OFF)
137 #define UART_PL011_UARTCR_OUT2_MASK ( \
138 0x1u<<UART_PL011_UARTCR_OUT2_OFF)
139 #define UART_PL011_UARTCR_RTSE_MASK ( \
140 0x1u<<UART_PL011_UARTCR_RTSE_OFF)
141 #define UART_PL011_UARTCR_CTSE_MASK ( \
142 0x1u<<UART_PL011_UARTCR_CTSE_OFF)
143
144 #define UART_PL011_UARTIFLS_TX_FIFO_LVL_MASK ( \
145 0x7u<<UART_PL011_UARTIFLS_TX_OFF)
146 #define UART_PL011_UARTIFLS_RX_FIFO_LVL_MASK ( \
147 0x7u<<UART_PL011_UARTIFLS_RX_OFF)
148
149 #define UART_PL011_UARTDMACR_RX_MASK ( \
150 0x1u<<UART_PL011_UARTDMACR_RXEN_OFF \
151 | 0x1u<<UART_PL011_UARTDMACR_ON_ERR_OFF)
152 #define UART_PL011_UARTDMACR_TX_MASK ( \
153 0x1u<<UART_PL011_UARTDMACR_TXEN_OFF)
154
155 /* Default register values of UART PL011 */
156 #define UART_PL011_DATA_REG_RESET_VALUE (0x0u)
157 #define UART_PL011_ECR_REG_CLEAR_VALUE (0xFFu)
158 #define UART_PL011_ILPR_REG_RESET_VALUE (0x0u)
159 #define UART_PL011_IBRD_REG_RESET_VALUE (0x0u)
160 #define UART_PL011_FBRD_REG_RESET_VALUE (0x0u)
161 #define UART_PL011_LCR_H_REG_RESET_VALUE (0x0u)
162 #define UART_PL011_CR_REG_RESET_VALUE (0x0300u)
163 #define UART_PL011_IFLS_REG_RESET_VALUE (0x12u)
164 #define UART_PL011_IMSC_REG_RESET_VALUE (0x0u)
165 #define UART_PL011_ICR_REG_CLEAR_VALUE (0x7FFu)
166 #define UART_PL011_DMACR_REG_RESET_VALUE (0x0u)
167
_uart_pl011_enable(struct _uart_pl011_reg_map_t * p_uart)168 static void _uart_pl011_enable(struct _uart_pl011_reg_map_t* p_uart)
169 {
170 p_uart->uartcr |= UART_PL011_UARTCR_EN_MASK;
171 }
172
_uart_pl011_disable(struct _uart_pl011_reg_map_t * p_uart)173 static void _uart_pl011_disable(struct _uart_pl011_reg_map_t* p_uart)
174 {
175 p_uart->uartcr &= ~UART_PL011_UARTCR_EN_MASK;
176 }
177
_uart_pl011_is_enabled(struct _uart_pl011_reg_map_t * p_uart)178 static bool _uart_pl011_is_enabled(struct _uart_pl011_reg_map_t* p_uart)
179 {
180 return (bool)(p_uart->uartcr & UART_PL011_UARTCR_EN_MASK);
181 }
182
_uart_pl011_enable_fifo(struct _uart_pl011_reg_map_t * p_uart)183 static void _uart_pl011_enable_fifo(struct _uart_pl011_reg_map_t* p_uart)
184 {
185 p_uart->uartlcr_h |= UART_PL011_UARTLCR_H_FEN_MASK;
186 }
187
_uart_pl011_disable_fifo(struct _uart_pl011_reg_map_t * p_uart)188 static void _uart_pl011_disable_fifo(struct _uart_pl011_reg_map_t* p_uart)
189 {
190 p_uart->uartlcr_h &= ~UART_PL011_UARTLCR_H_FEN_MASK;
191 }
192
_uart_pl011_is_fifo_enabled(struct _uart_pl011_reg_map_t * p_uart)193 static bool _uart_pl011_is_fifo_enabled(struct _uart_pl011_reg_map_t* p_uart)
194 {
195 return (bool)(p_uart->uartlcr_h & UART_PL011_UARTLCR_H_FEN_MASK);
196 }
197
_uart_pl011_is_busy(struct _uart_pl011_reg_map_t * p_uart)198 static bool _uart_pl011_is_busy(struct _uart_pl011_reg_map_t* p_uart)
199 {
200 return (bool)(p_uart->uartfr & UART_PL011_UARTFR_BUSYBIT);
201 }
202
_uart_pl011_set_baudrate(struct _uart_pl011_reg_map_t * p_uart,uint32_t clk,uint32_t baudrate)203 static enum uart_pl011_error_t _uart_pl011_set_baudrate(
204 struct _uart_pl011_reg_map_t* p_uart,
205 uint32_t clk, uint32_t baudrate)
206 {
207 /* Avoiding float calculations, bauddiv is left shifted by 6 */
208 uint64_t bauddiv = (((uint64_t)clk)<<UART_PL011_FBRD_WIDTH)
209 /(SAMPLING_FACTOR*baudrate);
210
211 /* Valid bauddiv value
212 * uart_clk (min) >= 16 x baud_rate (max)
213 * uart_clk (max) <= 16 x 65535 x baud_rate (min)
214 */
215 if((bauddiv < (1u<<UART_PL011_FBRD_WIDTH))
216 || (bauddiv > (65535u<<UART_PL011_FBRD_WIDTH))) {
217 return UART_PL011_ERR_INVALID_BAUD;
218 }
219
220 p_uart->uartibrd = (uint32_t)(bauddiv >> UART_PL011_FBRD_WIDTH);
221 p_uart->uartfbrd = (uint32_t)(bauddiv &
222 ((1u << UART_PL011_FBRD_WIDTH) - 1u));
223
224 __DMB();
225
226 /* In order to internally update the contents of uartibrd or uartfbrd, a
227 * uartlcr_h write must always be performed at the end
228 * ARM DDI 0183F, Pg 3-13
229 */
230 p_uart->uartlcr_h = p_uart->uartlcr_h;
231
232 return UART_PL011_ERR_NONE;
233 }
234
_uart_pl011_set_format(struct _uart_pl011_reg_map_t * p_uart,enum uart_pl011_wlen_t word_len,enum uart_pl011_parity_t parity,enum uart_pl011_stopbit_t stop_bits)235 static void _uart_pl011_set_format(struct _uart_pl011_reg_map_t* p_uart,
236 enum uart_pl011_wlen_t word_len,
237 enum uart_pl011_parity_t parity,
238 enum uart_pl011_stopbit_t stop_bits)
239 {
240 uint32_t ctrl_reg = p_uart->uartlcr_h & ~(UART_PL011_FORMAT_MASK);
241
242 /* Making sure other bit are not changed */
243 word_len &= UART_PL011_UARTLCR_H_WLEN_MASK;
244 parity &= UART_PL011_UARTLCR_H_PARITY_MASK;
245 stop_bits &= UART_PL011_UARTLCR_H_STOPBIT_MASK;
246
247 p_uart->uartlcr_h = ctrl_reg | word_len | parity | stop_bits;
248
249 }
250
_uart_pl011_set_cr_bit(struct _uart_pl011_reg_map_t * p_uart,uint32_t mask)251 static void _uart_pl011_set_cr_bit(struct _uart_pl011_reg_map_t* p_uart,
252 uint32_t mask)
253 {
254 bool uart_enabled = _uart_pl011_is_enabled(p_uart);
255 bool fifo_enabled = _uart_pl011_is_fifo_enabled(p_uart);
256
257 /* UART must be disabled before any Control Register or
258 * Line Control Register are reprogrammed */
259 _uart_pl011_disable(p_uart);
260
261 /* Flush the transmit FIFO by disabling bit 4 (FEN) in
262 * the line control register (UARTCLR_H) */
263 _uart_pl011_disable_fifo(p_uart);
264
265 p_uart->uartcr |= (mask);
266
267 /* Enabling the FIFOs if previously enabled */
268 if(fifo_enabled) {
269 _uart_pl011_enable_fifo(p_uart);
270 }
271
272 /* Enabling the UART if previously enabled */
273 if(uart_enabled) {
274 _uart_pl011_enable(p_uart);
275 }
276 }
277
_uart_pl011_clear_cr_bit(struct _uart_pl011_reg_map_t * p_uart,uint32_t mask)278 static void _uart_pl011_clear_cr_bit(struct _uart_pl011_reg_map_t* p_uart,
279 uint32_t mask)
280 {
281 bool uart_enabled = _uart_pl011_is_enabled(p_uart);
282 bool fifo_enabled = _uart_pl011_is_fifo_enabled(p_uart);
283
284 /* UART must be disabled before any Control Register or
285 * Line Control Register are reprogrammed */
286 _uart_pl011_disable(p_uart);
287
288 /* Flush the transmit FIFO by disabling bit 4 (FEN) in
289 * the line control register (UARTCLR_H) */
290 _uart_pl011_disable_fifo(p_uart);
291
292 p_uart->uartcr &= ~(mask);
293
294 /* Enabling the FIFOs if previously enabled */
295 if(fifo_enabled) {
296 _uart_pl011_enable_fifo(p_uart);
297 }
298
299 /* Enabling the UART if previously enabled */
300 if(uart_enabled) {
301 _uart_pl011_enable(p_uart);
302 }
303 }
304
_uart_pl011_set_lcr_h_bit(struct _uart_pl011_reg_map_t * p_uart,uint32_t mask)305 static void _uart_pl011_set_lcr_h_bit(struct _uart_pl011_reg_map_t* p_uart,
306 uint32_t mask)
307 {
308 bool uart_enabled = _uart_pl011_is_enabled(p_uart);
309
310 /* UART must be disabled before any Control Register or
311 * Line Control Register are reprogrammed */
312 _uart_pl011_disable(p_uart);
313
314 p_uart->uartlcr_h |= (mask);
315
316 /* Enabling the UART if previously enabled */
317 if(uart_enabled) {
318 _uart_pl011_enable(p_uart);
319 }
320 }
321
_uart_pl011_clear_lcr_h_bit(struct _uart_pl011_reg_map_t * p_uart,uint32_t mask)322 static void _uart_pl011_clear_lcr_h_bit(struct _uart_pl011_reg_map_t* p_uart,
323 uint32_t mask)
324 {
325 bool uart_enabled = _uart_pl011_is_enabled(p_uart);
326
327 /* UART must be disabled before any Control Register or
328 * Line Control Register are reprogrammed */
329 _uart_pl011_disable(p_uart);
330
331 p_uart->uartlcr_h &= ~(mask);
332
333 /* Enabling the UART if previously enabled */
334 if(uart_enabled) {
335 _uart_pl011_enable(p_uart);
336 }
337 }
338
_uart_pl011_reset_regs(struct _uart_pl011_reg_map_t * p_uart)339 static void _uart_pl011_reset_regs(struct _uart_pl011_reg_map_t* p_uart)
340 {
341 /* Restore the default value of UART registers, the registers which
342 * are not listed below are Read-Only */
343
344 /* Will disable the UART */
345 p_uart->uartcr = UART_PL011_CR_REG_RESET_VALUE;
346 p_uart->uartdr = UART_PL011_DATA_REG_RESET_VALUE;
347 /* Clear all the errors */
348 p_uart->uartecr = UART_PL011_ECR_REG_CLEAR_VALUE;
349 p_uart->uartilpr = UART_PL011_ILPR_REG_RESET_VALUE;
350 p_uart->uartibrd = UART_PL011_IBRD_REG_RESET_VALUE;
351 p_uart->uartfbrd = UART_PL011_FBRD_REG_RESET_VALUE;
352 p_uart->uartlcr_h = UART_PL011_LCR_H_REG_RESET_VALUE;
353 p_uart->uartifls = UART_PL011_IFLS_REG_RESET_VALUE;
354 p_uart->uartimsc = UART_PL011_IMSC_REG_RESET_VALUE;
355 /* Clear all the interrupts */
356 p_uart->uarticr = UART_PL011_ICR_REG_CLEAR_VALUE;
357 p_uart->uartdmacr = UART_PL011_DMACR_REG_RESET_VALUE;
358 }
359
uart_pl011_init(struct uart_pl011_dev_t * dev,uint32_t uart_clk)360 enum uart_pl011_error_t uart_pl011_init(struct uart_pl011_dev_t* dev,
361 uint32_t uart_clk)
362 {
363 enum uart_pl011_error_t err;
364
365 struct _uart_pl011_reg_map_t* p_uart =
366 (struct _uart_pl011_reg_map_t*)dev->cfg->base;
367
368 uint32_t def_baud = dev->cfg->def_baudrate;
369
370 if(uart_clk == 0) {
371 return UART_PL011_ERR_INVALID_ARG;
372 }
373
374 if(def_baud == 0) {
375 return UART_PL011_ERR_INVALID_BAUD;
376 }
377
378 /* Updating the system clock */
379 dev->data->uart_clk = uart_clk;
380
381 /* Setting the default baudrate */
382 err = _uart_pl011_set_baudrate(p_uart, uart_clk, def_baud);
383
384 if(err != UART_PL011_ERR_NONE) {
385 return err;
386 }
387
388 /* Setting the default character format */
389 _uart_pl011_set_format(p_uart, dev->cfg->def_wlen,
390 dev->cfg->def_parity,
391 dev->cfg->def_stopbit);
392
393 /* Enabling the FIFOs */
394 _uart_pl011_enable_fifo(p_uart);
395
396 dev->data->state = UART_PL011_INITIALIZED;
397
398 return UART_PL011_ERR_NONE;
399 }
400
uart_pl011_uninit(struct uart_pl011_dev_t * dev)401 void uart_pl011_uninit(struct uart_pl011_dev_t* dev)
402 {
403 struct _uart_pl011_reg_map_t* p_uart =
404 (struct _uart_pl011_reg_map_t*)dev->cfg->base;
405
406 while(_uart_pl011_is_busy(p_uart));
407
408 /* Disable and restore the default configuration of the peripheral */
409 _uart_pl011_reset_regs(p_uart);
410
411 dev->data->state = UART_PL011_UNINITIALIZED;
412
413 return;
414 }
415
uart_pl011_get_state(struct uart_pl011_dev_t * dev)416 enum uart_pl011_state_t uart_pl011_get_state(struct uart_pl011_dev_t* dev)
417 {
418 return dev->data->state;
419 }
420
uart_pl011_set_baudrate(struct uart_pl011_dev_t * dev,uint32_t baudrate)421 enum uart_pl011_error_t uart_pl011_set_baudrate(
422 struct uart_pl011_dev_t* dev, uint32_t baudrate)
423 {
424 enum uart_pl011_error_t err = UART_PL011_ERR_NONE;
425
426 struct _uart_pl011_reg_map_t* p_uart =
427 (struct _uart_pl011_reg_map_t*)dev->cfg->base;
428
429 bool uart_enabled = _uart_pl011_is_enabled(p_uart);
430
431 if(uart_pl011_get_state(dev) != UART_PL011_INITIALIZED) {
432 return UART_PL011_ERR_NOT_INIT;
433 }
434
435 if(baudrate == 0) {
436 return UART_PL011_ERR_INVALID_BAUD;
437 }
438
439 /* UART must be disabled before any Control Register or
440 * Line Control Register are reprogrammed */
441 _uart_pl011_disable(p_uart);
442
443 /* If baudrate is not valid ie UART_PL011_ERR_NONE is not returned then
444 * the UART will continue to function at the old baudrate */
445 err = _uart_pl011_set_baudrate(p_uart, dev->data->uart_clk, baudrate);
446
447 if(err == UART_PL011_ERR_NONE) {
448 dev->data->baudrate = baudrate;
449 }
450
451 if(uart_enabled) {
452 _uart_pl011_enable(p_uart);
453 }
454
455 return err;
456 }
457
uart_pl011_get_baudrate(struct uart_pl011_dev_t * dev)458 uint32_t uart_pl011_get_baudrate(struct uart_pl011_dev_t* dev)
459 {
460 return dev->data->baudrate;
461 }
462
uart_pl011_enable_intr(struct uart_pl011_dev_t * dev,enum uart_pl011_intr_t mask)463 void uart_pl011_enable_intr(struct uart_pl011_dev_t* dev,
464 enum uart_pl011_intr_t mask)
465 {
466 struct _uart_pl011_reg_map_t* p_uart =
467 (struct _uart_pl011_reg_map_t*)dev->cfg->base;
468
469 p_uart->uartimsc |= (uint32_t)(mask);
470
471 return;
472 }
473
uart_pl011_disable_intr(struct uart_pl011_dev_t * dev,enum uart_pl011_intr_t mask)474 void uart_pl011_disable_intr(struct uart_pl011_dev_t* dev,
475 enum uart_pl011_intr_t mask)
476 {
477 struct _uart_pl011_reg_map_t* p_uart =
478 (struct _uart_pl011_reg_map_t*)dev->cfg->base;
479
480 p_uart->uartimsc &= (uint32_t)(~mask);
481
482 return;
483 }
484
uart_pl011_clear_intr(struct uart_pl011_dev_t * dev,enum uart_pl011_intr_t mask)485 void uart_pl011_clear_intr(struct uart_pl011_dev_t* dev,
486 enum uart_pl011_intr_t mask)
487 {
488 struct _uart_pl011_reg_map_t* p_uart =
489 (struct _uart_pl011_reg_map_t*)dev->cfg->base;
490
491 p_uart->uarticr = (uint32_t)mask;
492
493 return;
494 }
495
496
uart_pl011_get_masked_intr_status(struct uart_pl011_dev_t * dev)497 enum uart_pl011_intr_t uart_pl011_get_masked_intr_status(
498 struct uart_pl011_dev_t* dev)
499 {
500 struct _uart_pl011_reg_map_t* p_uart =
501 (struct _uart_pl011_reg_map_t*)dev->cfg->base;
502
503 return (enum uart_pl011_intr_t)(p_uart->uartmis);
504
505 }
506
uart_pl011_get_raw_intr_status(struct uart_pl011_dev_t * dev)507 enum uart_pl011_intr_t uart_pl011_get_raw_intr_status(
508 struct uart_pl011_dev_t* dev)
509 {
510 struct _uart_pl011_reg_map_t* p_uart =
511 (struct _uart_pl011_reg_map_t*)dev->cfg->base;
512
513 return (enum uart_pl011_intr_t)(p_uart->uartris);
514 }
515
uart_pl011_set_rx_fifo_lvl(struct uart_pl011_dev_t * dev,enum uart_pl011_rx_fifo_lvl_t rx_lvl)516 void uart_pl011_set_rx_fifo_lvl(struct uart_pl011_dev_t* dev,
517 enum uart_pl011_rx_fifo_lvl_t rx_lvl)
518 {
519 uint32_t fifo_lvl;
520
521 struct _uart_pl011_reg_map_t* p_uart =
522 (struct _uart_pl011_reg_map_t*)dev->cfg->base;
523
524 /* Check if rx_lvl have valid values */
525 rx_lvl &= UART_PL011_UARTIFLS_RX_FIFO_LVL_MASK;
526
527 fifo_lvl = p_uart->uartifls
528 & ~(UART_PL011_UARTIFLS_RX_FIFO_LVL_MASK);
529 p_uart->uartifls = fifo_lvl | rx_lvl;
530
531 return;
532 }
533
uart_pl011_set_tx_fifo_lvl(struct uart_pl011_dev_t * dev,enum uart_pl011_tx_fifo_lvl_t tx_lvl)534 void uart_pl011_set_tx_fifo_lvl(struct uart_pl011_dev_t* dev,
535 enum uart_pl011_tx_fifo_lvl_t tx_lvl)
536 {
537 uint32_t fifo_lvl;
538
539 struct _uart_pl011_reg_map_t* p_uart =
540 (struct _uart_pl011_reg_map_t*)dev->cfg->base;
541 /* Check if tx_lvl have valid values */
542 tx_lvl &= UART_PL011_UARTIFLS_TX_FIFO_LVL_MASK;
543
544 fifo_lvl = p_uart->uartifls
545 & ~(UART_PL011_UARTIFLS_TX_FIFO_LVL_MASK);
546 p_uart->uartifls = fifo_lvl | tx_lvl;
547
548 return;
549 }
550
uart_pl011_set_tx_dma(struct uart_pl011_dev_t * dev,enum uart_pl011_tx_dma_t enable)551 void uart_pl011_set_tx_dma(struct uart_pl011_dev_t* dev,
552 enum uart_pl011_tx_dma_t enable)
553 {
554 uint32_t dma_cr;
555
556 struct _uart_pl011_reg_map_t* p_uart =
557 (struct _uart_pl011_reg_map_t*)dev->cfg->base;
558
559 enable &= UART_PL011_UARTDMACR_TX_MASK;
560
561 dma_cr = p_uart->uartdmacr
562 & ~(UART_PL011_UARTDMACR_TX_MASK);
563
564 p_uart->uartdmacr = dma_cr | enable;
565
566 return;
567 }
568
uart_pl011_set_rx_dma(struct uart_pl011_dev_t * dev,enum uart_pl011_rx_dma_t enable)569 void uart_pl011_set_rx_dma(struct uart_pl011_dev_t* dev,
570 enum uart_pl011_rx_dma_t enable)
571 {
572 uint32_t dma_cr;
573
574 struct _uart_pl011_reg_map_t* p_uart =
575 (struct _uart_pl011_reg_map_t*)dev->cfg->base;
576
577 enable &= UART_PL011_UARTDMACR_RX_MASK;
578
579 dma_cr = p_uart->uartdmacr
580 & ~(UART_PL011_UARTDMACR_RX_MASK);
581
582 p_uart->uartdmacr = dma_cr | enable;
583
584 return;
585 }
586
uart_pl011_is_readable(struct uart_pl011_dev_t * dev)587 bool uart_pl011_is_readable(struct uart_pl011_dev_t* dev)
588 {
589 struct _uart_pl011_reg_map_t* p_uart =
590 (struct _uart_pl011_reg_map_t*)dev->cfg->base;
591
592 if( (uart_pl011_get_state(dev) == UART_PL011_INITIALIZED) &&
593 /* UART is initialized */
594 (p_uart->uartcr & UART_PL011_UARTCR_EN_MASK) &&
595 /* UART is enabled */
596 (p_uart->uartcr & UART_PL011_UARTCR_RX_EN_MASK) &&
597 /* Receive is enabled */
598 ((p_uart->uartfr & UART_PL011_UARTFR_RX_FIFO_EMPTY) == 0)) {
599 /* Receive Fifo is not empty */
600 return true;
601 }
602
603 return false;
604
605 }
606
uart_pl011_read(struct uart_pl011_dev_t * dev,uint8_t * byte)607 enum uart_pl011_error_t uart_pl011_read(
608 struct uart_pl011_dev_t* dev, uint8_t* byte)
609 {
610 struct _uart_pl011_reg_map_t* p_uart =
611 (struct _uart_pl011_reg_map_t*)dev->cfg->base;
612
613 *byte = p_uart->uartdr;
614
615 return (enum uart_pl011_error_t)(p_uart->uartrsr
616 & UART_PL011_RX_ERR_MASK);
617 }
618
uart_pl011_is_writable(struct uart_pl011_dev_t * dev)619 bool uart_pl011_is_writable(struct uart_pl011_dev_t* dev)
620 {
621 struct _uart_pl011_reg_map_t* p_uart =
622 (struct _uart_pl011_reg_map_t*)dev->cfg->base;
623
624 if( (uart_pl011_get_state(dev) == UART_PL011_INITIALIZED) &&
625 /* UART is initialized */
626 (p_uart->uartcr & UART_PL011_UARTCR_EN_MASK) &&
627 /* UART is enabled */
628 (p_uart->uartcr & UART_PL011_UARTCR_TX_EN_MASK) &&
629 /* Transmit is enabled */
630 ((p_uart->uartfr & UART_PL011_UARTFR_TX_FIFO_FULL) == 0)) {
631 /* Transmit Fifo is not full */
632 return true;
633 }
634 return false;
635
636 }
637
uart_pl011_write(struct uart_pl011_dev_t * dev,uint8_t byte)638 void uart_pl011_write(struct uart_pl011_dev_t* dev, uint8_t byte)
639 {
640 struct _uart_pl011_reg_map_t* p_uart =
641 (struct _uart_pl011_reg_map_t*)dev->cfg->base;
642
643 p_uart->uartdr = byte;
644
645 return;
646 }
647
uart_pl011_set_format(struct uart_pl011_dev_t * dev,enum uart_pl011_wlen_t word_len,enum uart_pl011_parity_t parity,enum uart_pl011_stopbit_t stop_bits)648 enum uart_pl011_error_t uart_pl011_set_format(struct uart_pl011_dev_t* dev,
649 enum uart_pl011_wlen_t word_len,
650 enum uart_pl011_parity_t parity,
651 enum uart_pl011_stopbit_t stop_bits)
652 {
653 struct _uart_pl011_reg_map_t* p_uart =
654 (struct _uart_pl011_reg_map_t*)dev->cfg->base;
655
656 bool uart_enabled = _uart_pl011_is_enabled(p_uart);
657
658 if(uart_pl011_get_state(dev) != UART_PL011_INITIALIZED) {
659 return UART_PL011_ERR_NOT_INIT;
660 }
661
662 /* UART must be disabled before any Control Register or
663 * Line Control Register are reprogrammed */
664 _uart_pl011_disable(p_uart);
665
666 _uart_pl011_set_format(p_uart, word_len, parity, stop_bits);
667
668 /* Enabling the UART if previously enabled */
669 if(uart_enabled) {
670 _uart_pl011_enable(p_uart);
671 }
672
673 return UART_PL011_ERR_NONE;
674 }
675
uart_pl011_enable_fifo(struct uart_pl011_dev_t * dev)676 void uart_pl011_enable_fifo(struct uart_pl011_dev_t* dev)
677 {
678 struct _uart_pl011_reg_map_t* p_uart =
679 (struct _uart_pl011_reg_map_t*)dev->cfg->base;
680
681 _uart_pl011_set_lcr_h_bit(p_uart, UART_PL011_UARTLCR_H_FEN_MASK);
682
683 return;
684 }
685
uart_pl011_disable_fifo(struct uart_pl011_dev_t * dev)686 void uart_pl011_disable_fifo(struct uart_pl011_dev_t* dev)
687 {
688 struct _uart_pl011_reg_map_t* p_uart =
689 (struct _uart_pl011_reg_map_t*)dev->cfg->base;
690
691 _uart_pl011_clear_lcr_h_bit(p_uart, UART_PL011_UARTLCR_H_FEN_MASK);
692
693 return;
694 }
695
uart_pl011_enable_break(struct uart_pl011_dev_t * dev)696 void uart_pl011_enable_break(struct uart_pl011_dev_t* dev)
697 {
698 struct _uart_pl011_reg_map_t* p_uart =
699 (struct _uart_pl011_reg_map_t*)dev->cfg->base;
700
701 _uart_pl011_set_lcr_h_bit(p_uart, UART_PL011_UARTLCR_H_BRK_MASK);
702
703 return;
704 }
705
uart_pl011_disable_break(struct uart_pl011_dev_t * dev)706 void uart_pl011_disable_break(struct uart_pl011_dev_t* dev)
707 {
708 struct _uart_pl011_reg_map_t* p_uart =
709 (struct _uart_pl011_reg_map_t*)dev->cfg->base;
710
711 _uart_pl011_clear_lcr_h_bit(p_uart, UART_PL011_UARTLCR_H_BRK_MASK);
712
713 return;
714 }
715
uart_pl011_enable_cts_flowcontrol(struct uart_pl011_dev_t * dev)716 void uart_pl011_enable_cts_flowcontrol(struct uart_pl011_dev_t* dev)
717 {
718 struct _uart_pl011_reg_map_t* p_uart =
719 (struct _uart_pl011_reg_map_t*)dev->cfg->base;
720
721 _uart_pl011_set_cr_bit(p_uart, UART_PL011_UARTCR_CTSE_MASK);
722
723 return;
724 }
725
uart_pl011_disable_cts_flowcontrol(struct uart_pl011_dev_t * dev)726 void uart_pl011_disable_cts_flowcontrol(struct uart_pl011_dev_t* dev)
727 {
728 struct _uart_pl011_reg_map_t* p_uart =
729 (struct _uart_pl011_reg_map_t*)dev->cfg->base;
730
731 _uart_pl011_clear_cr_bit(p_uart, UART_PL011_UARTCR_CTSE_MASK);
732
733 return;
734 }
735
uart_pl011_enable_rts_flowcontrol(struct uart_pl011_dev_t * dev)736 void uart_pl011_enable_rts_flowcontrol(struct uart_pl011_dev_t* dev)
737 {
738 struct _uart_pl011_reg_map_t* p_uart =
739 (struct _uart_pl011_reg_map_t*)dev->cfg->base;
740
741 _uart_pl011_set_cr_bit(p_uart, UART_PL011_UARTCR_RTSE_MASK);
742
743 return;
744 }
745
uart_pl011_disable_rts_flowcontrol(struct uart_pl011_dev_t * dev)746 void uart_pl011_disable_rts_flowcontrol(struct uart_pl011_dev_t* dev)
747 {
748 struct _uart_pl011_reg_map_t* p_uart =
749 (struct _uart_pl011_reg_map_t*)dev->cfg->base;
750
751 _uart_pl011_clear_cr_bit(p_uart, UART_PL011_UARTCR_RTSE_MASK);
752
753 return;
754 }
755
uart_pl011_enable_ri(struct uart_pl011_dev_t * dev)756 void uart_pl011_enable_ri(struct uart_pl011_dev_t* dev)
757 {
758 struct _uart_pl011_reg_map_t* p_uart =
759 (struct _uart_pl011_reg_map_t*)dev->cfg->base;
760
761 _uart_pl011_set_cr_bit(p_uart, UART_PL011_UARTCR_OUT2_MASK);
762
763 return;
764 }
765
uart_pl011_disable_ri(struct uart_pl011_dev_t * dev)766 void uart_pl011_disable_ri(struct uart_pl011_dev_t* dev)
767 {
768 struct _uart_pl011_reg_map_t* p_uart =
769 (struct _uart_pl011_reg_map_t*)dev->cfg->base;
770
771 _uart_pl011_clear_cr_bit(p_uart, UART_PL011_UARTCR_OUT2_MASK);
772
773 return;
774 }
775
uart_pl011_enable_dcd(struct uart_pl011_dev_t * dev)776 void uart_pl011_enable_dcd(struct uart_pl011_dev_t* dev)
777 {
778 struct _uart_pl011_reg_map_t* p_uart =
779 (struct _uart_pl011_reg_map_t*)dev->cfg->base;
780
781 _uart_pl011_set_cr_bit(p_uart, UART_PL011_UARTCR_OUT1_MASK);
782
783 return;
784 }
785
uart_pl011_disable_dcd(struct uart_pl011_dev_t * dev)786 void uart_pl011_disable_dcd(struct uart_pl011_dev_t* dev)
787 {
788 struct _uart_pl011_reg_map_t* p_uart =
789 (struct _uart_pl011_reg_map_t*)dev->cfg->base;
790
791 _uart_pl011_clear_cr_bit(p_uart, UART_PL011_UARTCR_OUT1_MASK);
792
793 return;
794 }
795
uart_pl011_set_rts(struct uart_pl011_dev_t * dev)796 void uart_pl011_set_rts(struct uart_pl011_dev_t* dev)
797 {
798 struct _uart_pl011_reg_map_t* p_uart =
799 (struct _uart_pl011_reg_map_t*)dev->cfg->base;
800
801 _uart_pl011_set_cr_bit(p_uart, UART_PL011_UARTCR_RTS_MASK);
802
803 return;
804 }
805
uart_pl011_clear_rts(struct uart_pl011_dev_t * dev)806 void uart_pl011_clear_rts(struct uart_pl011_dev_t* dev)
807 {
808 struct _uart_pl011_reg_map_t* p_uart =
809 (struct _uart_pl011_reg_map_t*)dev->cfg->base;
810
811 _uart_pl011_clear_cr_bit(p_uart, UART_PL011_UARTCR_RTS_MASK);
812
813 return;
814 }
815
uart_pl011_set_dtr(struct uart_pl011_dev_t * dev)816 void uart_pl011_set_dtr(struct uart_pl011_dev_t* dev)
817 {
818 struct _uart_pl011_reg_map_t* p_uart =
819 (struct _uart_pl011_reg_map_t*)dev->cfg->base;
820
821 _uart_pl011_set_cr_bit(p_uart, UART_PL011_UARTCR_DTR_MASK);
822
823 return;
824 }
825
uart_pl011_clear_dtr(struct uart_pl011_dev_t * dev)826 void uart_pl011_clear_dtr(struct uart_pl011_dev_t* dev)
827 {
828 struct _uart_pl011_reg_map_t* p_uart =
829 (struct _uart_pl011_reg_map_t*)dev->cfg->base;
830
831 _uart_pl011_clear_cr_bit(p_uart, UART_PL011_UARTCR_DTR_MASK);
832
833 return;
834 }
835
uart_pl011_enable_receive(struct uart_pl011_dev_t * dev)836 void uart_pl011_enable_receive(struct uart_pl011_dev_t* dev)
837 {
838 struct _uart_pl011_reg_map_t* p_uart =
839 (struct _uart_pl011_reg_map_t*)dev->cfg->base;
840
841 _uart_pl011_set_cr_bit(p_uart, UART_PL011_UARTCR_RX_EN_MASK);
842
843 return;
844 }
845
uart_pl011_disable_receive(struct uart_pl011_dev_t * dev)846 void uart_pl011_disable_receive(struct uart_pl011_dev_t* dev)
847 {
848 struct _uart_pl011_reg_map_t* p_uart =
849 (struct _uart_pl011_reg_map_t*)dev->cfg->base;
850
851 _uart_pl011_clear_cr_bit(p_uart, UART_PL011_UARTCR_RX_EN_MASK);
852
853 return;
854 }
855
uart_pl011_enable_transmit(struct uart_pl011_dev_t * dev)856 void uart_pl011_enable_transmit(struct uart_pl011_dev_t* dev)
857 {
858 struct _uart_pl011_reg_map_t* p_uart =
859 (struct _uart_pl011_reg_map_t*)dev->cfg->base;
860
861 _uart_pl011_set_cr_bit(p_uart, UART_PL011_UARTCR_TX_EN_MASK);
862
863 return;
864 }
865
uart_pl011_disable_transmit(struct uart_pl011_dev_t * dev)866 void uart_pl011_disable_transmit(struct uart_pl011_dev_t* dev)
867 {
868 struct _uart_pl011_reg_map_t* p_uart =
869 (struct _uart_pl011_reg_map_t*)dev->cfg->base;
870
871 _uart_pl011_clear_cr_bit(p_uart, UART_PL011_UARTCR_TX_EN_MASK);
872
873 return;
874 }
875
uart_pl011_set_loopback(struct uart_pl011_dev_t * dev)876 void uart_pl011_set_loopback(struct uart_pl011_dev_t* dev)
877 {
878 struct _uart_pl011_reg_map_t* p_uart =
879 (struct _uart_pl011_reg_map_t*)dev->cfg->base;
880
881 _uart_pl011_set_cr_bit(p_uart, UART_PL011_UARTCR_LBE_MASK);
882
883 return;
884 }
885
uart_pl011_clear_loopback(struct uart_pl011_dev_t * dev)886 void uart_pl011_clear_loopback(struct uart_pl011_dev_t* dev)
887 {
888 struct _uart_pl011_reg_map_t* p_uart =
889 (struct _uart_pl011_reg_map_t*)dev->cfg->base;
890
891 _uart_pl011_clear_cr_bit(p_uart, UART_PL011_UARTCR_LBE_MASK);
892
893 return;
894 }
895
uart_pl011_enable_sirlp(struct uart_pl011_dev_t * dev)896 void uart_pl011_enable_sirlp(struct uart_pl011_dev_t* dev)
897 {
898 struct _uart_pl011_reg_map_t* p_uart =
899 (struct _uart_pl011_reg_map_t*)dev->cfg->base;
900
901 _uart_pl011_set_cr_bit(p_uart,
902 UART_PL011_UARTCR_SIREN_MASK | UART_PL011_UARTCR_SIRLP_MASK);
903
904 return;
905 }
906
uart_pl011_disable_sirlp(struct uart_pl011_dev_t * dev)907 void uart_pl011_disable_sirlp(struct uart_pl011_dev_t* dev)
908 {
909 struct _uart_pl011_reg_map_t* p_uart =
910 (struct _uart_pl011_reg_map_t*)dev->cfg->base;
911
912 _uart_pl011_clear_cr_bit(p_uart,
913 UART_PL011_UARTCR_SIREN_MASK | UART_PL011_UARTCR_SIRLP_MASK);
914
915 return;
916 }
917
uart_pl011_enable_sir(struct uart_pl011_dev_t * dev)918 void uart_pl011_enable_sir(struct uart_pl011_dev_t* dev)
919 {
920 struct _uart_pl011_reg_map_t* p_uart =
921 (struct _uart_pl011_reg_map_t*)dev->cfg->base;
922
923 _uart_pl011_set_cr_bit(p_uart, UART_PL011_UARTCR_SIREN_MASK);
924
925 return;
926 }
927
uart_pl011_disable_sir(struct uart_pl011_dev_t * dev)928 void uart_pl011_disable_sir(struct uart_pl011_dev_t* dev)
929 {
930 struct _uart_pl011_reg_map_t* p_uart =
931 (struct _uart_pl011_reg_map_t*)dev->cfg->base;
932
933 _uart_pl011_clear_cr_bit(p_uart, UART_PL011_UARTCR_SIREN_MASK);
934
935 return;
936 }
937
uart_pl011_enable(struct uart_pl011_dev_t * dev)938 void uart_pl011_enable(struct uart_pl011_dev_t* dev)
939 {
940 struct _uart_pl011_reg_map_t* p_uart =
941 (struct _uart_pl011_reg_map_t*)dev->cfg->base;
942
943 _uart_pl011_enable(p_uart);
944
945 return;
946 }
947
uart_pl011_disable(struct uart_pl011_dev_t * dev)948 void uart_pl011_disable(struct uart_pl011_dev_t* dev)
949 {
950 struct _uart_pl011_reg_map_t* p_uart =
951 (struct _uart_pl011_reg_map_t*)dev->cfg->base;
952
953 _uart_pl011_disable(p_uart);
954
955 return;
956 }
957
uart_pl011_get_cts_status(struct uart_pl011_dev_t * dev)958 bool uart_pl011_get_cts_status(struct uart_pl011_dev_t* dev)
959 {
960 struct _uart_pl011_reg_map_t* p_uart =
961 (struct _uart_pl011_reg_map_t*)dev->cfg->base;
962 return (bool)(p_uart->uartfr & UART_PL011_UARTFR_CTS_MASK);
963
964 }
965
uart_pl011_get_dsr_status(struct uart_pl011_dev_t * dev)966 bool uart_pl011_get_dsr_status(struct uart_pl011_dev_t* dev)
967 {
968 struct _uart_pl011_reg_map_t* p_uart =
969 (struct _uart_pl011_reg_map_t*)dev->cfg->base;
970 return (bool)(p_uart->uartfr & UART_PL011_UARTFR_DSR_MASK);
971
972 }
973
uart_pl011_get_dcd_status(struct uart_pl011_dev_t * dev)974 bool uart_pl011_get_dcd_status(struct uart_pl011_dev_t* dev)
975 {
976 struct _uart_pl011_reg_map_t* p_uart =
977 (struct _uart_pl011_reg_map_t*)dev->cfg->base;
978 return (bool)(p_uart->uartfr & UART_PL011_UARTFR_DCD_MASK);
979
980 }
981
uart_pl011_get_ri_status(struct uart_pl011_dev_t * dev)982 bool uart_pl011_get_ri_status(struct uart_pl011_dev_t* dev)
983 {
984 struct _uart_pl011_reg_map_t* p_uart =
985 (struct _uart_pl011_reg_map_t*)dev->cfg->base;
986 return (bool)(p_uart->uartfr & UART_PL011_UARTFR_RI_MASK);
987
988 }
989
uart_pl011_set_sirlp_divisor(struct uart_pl011_dev_t * dev,uint32_t value)990 enum uart_pl011_error_t uart_pl011_set_sirlp_divisor(
991 struct uart_pl011_dev_t* dev, uint32_t value)
992 {
993 struct _uart_pl011_reg_map_t* p_uart =
994 (struct _uart_pl011_reg_map_t*)dev->cfg->base;
995
996 uint32_t irlp_baud16_clk;
997
998 if(uart_pl011_get_state(dev) != UART_PL011_INITIALIZED) {
999 return UART_PL011_ERR_NOT_INIT;
1000 }
1001
1002 if(value == 0) {
1003 return UART_PL011_ERR_INVALID_ARG;
1004 }
1005
1006 irlp_baud16_clk = dev->data->uart_clk/value;
1007
1008 /* Chose the divisor so that 1.42MHz < FIrLPBaud16 < 2.12MHz, that
1009 * results in a low-power pulse duration of 1.41–2.11μs (three times
1010 * the period of IrLPBaud16). ARM DDI0183F Pg 3-9 */
1011 if(irlp_baud16_clk < FREQ_IRLPBAUD16_MIN ||
1012 irlp_baud16_clk > FREQ_IRLPBAUD16_MAX) {
1013 return UART_PL011_ERR_INVALID_ARG;
1014 }
1015
1016 p_uart->uartilpr = value;
1017
1018 return UART_PL011_ERR_NONE;
1019 }
1020