1 /*
2 * Copyright (c) 2021-2022 Bestechnic (Shanghai) Co., Ltd. All rights reserved.
3 *
4 * This file is dual licensed: you can use it either under the terms of
5 * the GPL, or the BSD license, at your option.
6 * See the LICENSE file in the root of this repository for complete details.
7 */
8
9 #include "uart_bes.h"
10 #include <stdlib.h>
11 #include <string.h>
12 #include "hal_iomux.h"
13 #include "hal_timer.h"
14 #ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO
15 #include "hcs_macro.h"
16 #include "hdf_config_macro.h"
17 #else
18 #include "device_resource_if.h"
19 #endif
20 #include "hal_trace.h"
21 #include "hal_cache.h"
22 #include "hdf_log.h"
23
24
25 #define HDF_UART_TMO OSAL_WAIT_FOREVER
26 #define HDF_LOG_TAG uartDev
27
28 #define UART_FIFO_MAX_BUFFER 2048
29 #define UART_DMA_RING_BUFFER_SIZE 256 // mast be 2^n
30 #ifdef LOSCFG_SOC_SERIES_BES2700
31 #include "hal_location.h"
32 #define MAX_UART_NUMBER 2
33 #define MAX_UART_ID HAL_UART_ID_1
34 static SRAM_BSS_LOC unsigned char g_halUartBuf[UART_DMA_RING_BUFFER_SIZE];
35 static SRAM_BSS_LOC unsigned char g_halUart1Buf[UART_DMA_RING_BUFFER_SIZE];
36 static unsigned char *g_uartKfifoBuffer[MAX_UART_NUMBER] = { NULL, NULL };
37 #elif defined (LOSCFG_SOC_SERIES_BES2600)
38 #define MAX_UART_NUMBER 3
39 #define MAX_UART_ID HAL_UART_ID_2
40 static __SRAMBSS unsigned char g_halUartBuf[UART_DMA_RING_BUFFER_SIZE];
41 static __SRAMBSS unsigned char g_halUart1Buf[UART_DMA_RING_BUFFER_SIZE];
42 static __SRAMBSS unsigned char g_halUart2Buf[UART_DMA_RING_BUFFER_SIZE];
43 static unsigned char *g_uartKfifoBuffer[MAX_UART_NUMBER] = {NULL, NULL, NULL};
44 #endif
45
46 static struct UART_CTX_OBJ g_uartCtx[MAX_UART_NUMBER] = {0};
47 struct HAL_UART_CFG_T g_lowUartCfg = {
48 // used for tgdb cli console
49 .parity = HAL_UART_PARITY_NONE,
50 .stop = HAL_UART_STOP_BITS_1,
51 .data = HAL_UART_DATA_BITS_8,
52 .flow = HAL_UART_FLOW_CONTROL_NONE,
53 .tx_level = HAL_UART_FIFO_LEVEL_7_8,
54 .rx_level = HAL_UART_FIFO_LEVEL_1_8,
55 .baud = 0,
56 .dma_rx = false,
57 .dma_tx = false,
58 .dma_rx_stop_on_err = false,
59 };
60
HalSetUartIomux(enum HAL_UART_ID_T uartId)61 static void HalSetUartIomux(enum HAL_UART_ID_T uartId)
62 {
63 if (uartId > MAX_UART_ID) {
64 HDF_LOGE("%s %d not support!\r\n", __func__, uartId);
65 return;
66 }
67 if (uartId == HAL_UART_ID_0) {
68 hal_iomux_set_uart0();
69 }
70 if (uartId == HAL_UART_ID_1) {
71 hal_iomux_set_uart1();
72 }
73 #ifdef LOSCFG_SOC_SERIES_BES2600
74 if (uartId == HAL_UART_ID_2) {
75 hal_iomux_set_uart2();
76 }
77 #endif
78 }
79
HalUartStartDmaRx(uint32_t uartId)80 static void HalUartStartDmaRx(uint32_t uartId)
81 {
82 if (uartId > MAX_UART_ID) {
83 HDF_LOGE("%s %u not support!\r\n", __func__, uartId);
84 return;
85 }
86 struct HAL_DMA_DESC_T dmaDescRx;
87 uint32_t descCnt;
88 union HAL_UART_IRQ_T mask;
89
90 mask.reg = 0;
91 mask.BE = 0;
92 mask.FE = 0;
93 mask.OE = 0;
94 mask.PE = 0;
95 mask.RT = 1;
96 descCnt = 1;
97 hal_uart_dma_recv_mask(uartId, g_uartCtx[uartId].buffer, UART_DMA_RING_BUFFER_SIZE, &dmaDescRx, &descCnt, &mask);
98 }
99
UartRxHandler(enum HAL_UART_ID_T uartId,union HAL_UART_IRQ_T status)100 static void UartRxHandler(enum HAL_UART_ID_T uartId, union HAL_UART_IRQ_T status)
101 {
102 if (uartId > MAX_UART_ID) {
103 HDF_LOGE("%s %u not support!\r\n", __func__, uartId);
104 return;
105 }
106 if (status.TX != 0) {
107 if (OsalSemPost(&g_uartCtx[uartId].txSem) != HDF_SUCCESS) {
108 HDF_LOGE("%s OsalSemPost txSem failed!\r\n", __func__);
109 return;
110 }
111 }
112
113 if (status.RX != 0 || status.RT != 0) {
114 if (OsalSemPost(&g_uartCtx[uartId].rxSem) != HDF_SUCCESS) {
115 HDF_LOGE("%s OsalSemPost rxSem failed!\r\n", __func__);
116 return;
117 }
118 }
119 }
120
UartDmaRxHandler(uint32_t xferSize,int dmaError,union HAL_UART_IRQ_T status)121 static void UartDmaRxHandler(uint32_t xferSize, int dmaError, union HAL_UART_IRQ_T status)
122 {
123 int32_t ret;
124 uint32_t len = kfifo_put(&g_uartCtx[HAL_UART_ID_0].fifo, g_uartCtx[HAL_UART_ID_0].buffer, xferSize);
125 if (len < xferSize) {
126 HDF_LOGE("%s ringbuf is full have %d need %d\r", __FUNCTION__, (int)len, (int)xferSize);
127 return;
128 }
129
130 ret = memset_s(g_uartCtx[HAL_UART_ID_0].buffer, UART_DMA_RING_BUFFER_SIZE, 0, UART_DMA_RING_BUFFER_SIZE);
131 if (ret != 0) {
132 HDF_LOGE("%s %d:memset_s error\r\n", __func__, __LINE__);
133 return;
134 }
135 OsalSemPost(&g_uartCtx[HAL_UART_ID_0].rxSem);
136 HalUartStartDmaRx(HAL_UART_ID_0);
137 }
138
UartDmaTxHandler(uint32_t xferSize,int dmaError)139 static void UartDmaTxHandler(uint32_t xferSize, int dmaError)
140 {
141 OsalSemPost(&g_uartCtx[HAL_UART_ID_0].txSem);
142 }
143
Uart1DmaRxHandler(uint32_t xferSize,int dmaError,union HAL_UART_IRQ_T status)144 static void Uart1DmaRxHandler(uint32_t xferSize, int dmaError, union HAL_UART_IRQ_T status)
145 {
146 int32_t ret;
147 uint32_t len = kfifo_put(&g_uartCtx[HAL_UART_ID_1].fifo, g_uartCtx[HAL_UART_ID_1].buffer, xferSize);
148 if (len < xferSize) {
149 HDF_LOGE("%s ringbuf is full have %d need %d\r", __FUNCTION__, (int)len, (int)xferSize);
150 return;
151 }
152 ret = memset_s(g_uartCtx[HAL_UART_ID_1].buffer, UART_DMA_RING_BUFFER_SIZE, 0, UART_DMA_RING_BUFFER_SIZE);
153 if (ret != 0) {
154 HDF_LOGE("%s %d:memset_s error\r\n", __func__, __LINE__);
155 return;
156 }
157 OsalSemPost(&g_uartCtx[HAL_UART_ID_1].rxSem);
158 HalUartStartDmaRx(HAL_UART_ID_1);
159 }
160
Uart1DmaTxHandler(uint32_t xferSize,int dmaError)161 static void Uart1DmaTxHandler(uint32_t xferSize, int dmaError)
162 {
163 OsalSemPost(&g_uartCtx[HAL_UART_ID_1].txSem);
164 }
165
166 #ifdef LOSCFG_SOC_SERIES_BES2600
167 /* uart2 */
Uart2DmaRxHandler(uint32_t xferSize,int dmaError,union HAL_UART_IRQ_T status)168 static void Uart2DmaRxHandler(uint32_t xferSize, int dmaError, union HAL_UART_IRQ_T status)
169 {
170 int32_t ret;
171 uint32_t len = kfifo_put(&g_uartCtx[HAL_UART_ID_2].fifo, g_uartCtx[HAL_UART_ID_2].buffer, xferSize);
172 if (len < xferSize) {
173 HDF_LOGE("%s ringbuf is full have %d need %d\r", __FUNCTION__, (int)len, (int)xferSize);
174 return;
175 }
176
177 ret = memset_s(g_uartCtx[HAL_UART_ID_2].buffer, UART_DMA_RING_BUFFER_SIZE, 0, UART_DMA_RING_BUFFER_SIZE);
178 if (ret != 0) {
179 HDF_LOGE("%s %d:memset_s error\r\n", __func__, __LINE__);
180 return;
181 }
182 OsalSemPost(&g_uartCtx[HAL_UART_ID_2].rxSem);
183 HalUartStartDmaRx(HAL_UART_ID_2);
184 }
185
Uart2DmaTxHandler(uint32_t xferSize,int dmaError)186 static void Uart2DmaTxHandler(uint32_t xferSize, int dmaError)
187 {
188 OsalSemPost(&g_uartCtx[HAL_UART_ID_2].txSem);
189 }
190 #endif
191
HalUartStartRx(uint32_t uartId)192 static void HalUartStartRx(uint32_t uartId)
193 {
194 if (uartId > MAX_UART_ID) {
195 HDF_LOGE("%s %d Invalid input \r\n", __FILE__, __LINE__);
196 return;
197 }
198 union HAL_UART_IRQ_T mask;
199 mask.reg = 0;
200 mask.RT = 1;
201 mask.RX = 1;
202
203 hal_uart_irq_set_mask(uartId, mask);
204 hal_uart_irq_set_handler(uartId, UartRxHandler);
205 }
206
HalUartSend(uint32_t uartId,const void * data,uint32_t size,uint32_t timeOut)207 static int32_t HalUartSend(uint32_t uartId, const void *data, uint32_t size, uint32_t timeOut)
208 {
209 int32_t ret;
210 struct HAL_DMA_DESC_T dmaSescTx;
211 uint32_t descCnt;
212 if (data == NULL || size == 0) {
213 HDF_LOGE("%s %d Invalid input \r\n", __FILE__, __LINE__);
214 return HDF_ERR_INVALID_PARAM;
215 }
216
217 if (uartId > MAX_UART_ID) {
218 HDF_LOGE("%s %d Invalid input \r\n", __FILE__, __LINE__);
219 return HDF_ERR_NOT_SUPPORT;
220 }
221 descCnt = 1;
222 #ifdef LOSCFG_SOC_SERIES_BES2700
223 hal_cache_sync_all(HAL_CACHE_ID_D_CACHE);
224 #endif
225 hal_uart_dma_send(uartId, data, size, &dmaSescTx, &descCnt);
226 OsalSemWait(&g_uartCtx[uartId].txSem, timeOut);
227
228 return HDF_SUCCESS;
229 }
230
HalUartRecv(uint8_t uartId,void * data,uint32_t expectSize,uint32_t * recvSize,uint32_t timeOut)231 static int32_t HalUartRecv(uint8_t uartId, void *data, uint32_t expectSize,
232 uint32_t *recvSize, uint32_t timeOut)
233 {
234 uint32_t beginTime;
235 uint32_t nowTime;
236 uint32_t fifoPopLen;
237 uint32_t recvedLen = 0;
238 int32_t expectLen = (int32_t)expectSize;
239
240 if (data == NULL || expectLen == 0 || recvSize == NULL) {
241 HDF_LOGE("%s %d Invalid input \r\n", __FILE__, __LINE__);
242 return HDF_ERR_INVALID_PARAM;
243 }
244
245 if (uartId > MAX_UART_ID) {
246 HDF_LOGE("%s %d Invalid input \r\n", __FILE__, __LINE__);
247 return HDF_ERR_NOT_SUPPORT;
248 }
249 beginTime = TICKS_TO_MS(hal_sys_timer_get());
250 do {
251 fifoPopLen = kfifo_get(&g_uartCtx[uartId].fifo, (uint8_t *)data + recvedLen, expectLen);
252 recvedLen += fifoPopLen;
253 expectLen -= fifoPopLen;
254 if (recvedLen >= expectSize) {
255 break;
256 }
257 /* haven't get any data from fifo */
258 if (recvedLen == 0) {
259 break;
260 }
261 /* if reaches here, it means need to wait for more data come */
262 OsalSemWait(&g_uartCtx[uartId].rxSem, timeOut);
263 /* time out break */
264 nowTime = TICKS_TO_MS(hal_sys_timer_get());
265 if ((uint32_t)(nowTime - beginTime) >= timeOut) {
266 break;
267 }
268 } while (1);
269
270 if (recvSize != NULL) {
271 *recvSize = recvedLen;
272 }
273 return HDF_SUCCESS;
274 }
275
InitUartCtxCfg(struct UartDevice * device)276 static int32_t InitUartCtxCfg(struct UartDevice *device)
277 {
278 int32_t ret;
279 uint32_t uartId;
280 struct HAL_UART_CFG_T *uartCfg = NULL;
281 if (device == NULL) {
282 HDF_LOGE("%s: INVALID PARAM", __func__);
283 return HDF_ERR_INVALID_OBJECT;
284 }
285 uartCfg = &device->config;
286 if (uartCfg == NULL) {
287 HDF_LOGE("%s: INVALID OBJECT", __func__);
288 return HDF_ERR_INVALID_OBJECT;
289 }
290 uartId = device->uartId;
291 if (uartId > MAX_UART_ID) {
292 HDF_LOGE("%s %d NOT SUPPORT \r\n", __FILE__, __LINE__);
293 return HDF_ERR_NOT_SUPPORT;
294 }
295 ret = memset_s(&g_uartCtx[uartId], sizeof(struct UART_CTX_OBJ), 0, sizeof(struct UART_CTX_OBJ));
296 if (ret != 0) {
297 HDF_LOGE("%s %d:memset_s error\r\n", __func__, __LINE__);
298 return HDF_FAILURE;
299 }
300 g_uartCtx[uartId].txDMA = uartCfg->dma_tx;
301 g_uartCtx[uartId].rxDMA = uartCfg->dma_rx;
302
303 if (uartId == HAL_UART_ID_0) {
304 g_uartCtx[uartId].UartDmaRxHandler = UartDmaRxHandler;
305 g_uartCtx[uartId].UartDmaTxHandler = UartDmaTxHandler;
306 g_uartCtx[uartId].buffer = g_halUartBuf;
307 }
308
309 if (uartId == HAL_UART_ID_1) {
310 g_uartCtx[uartId].UartDmaRxHandler = Uart1DmaRxHandler;
311 g_uartCtx[uartId].UartDmaTxHandler = Uart1DmaTxHandler;
312 g_uartCtx[uartId].buffer = g_halUart1Buf;
313 }
314 #ifdef LOSCFG_SOC_SERIES_BES2600
315 if (uartId == HAL_UART_ID_2) {
316 g_uartCtx[uartId].UartDmaRxHandler = Uart2DmaRxHandler;
317 g_uartCtx[uartId].UartDmaTxHandler = Uart2DmaTxHandler;
318 g_uartCtx[uartId].buffer = g_halUart2Buf;
319 }
320 #endif
321 return HDF_SUCCESS;
322 }
323
HalUartHandlerInit(struct UartDevice * device)324 static void HalUartHandlerInit(struct UartDevice *device)
325 {
326 uint32_t uartId;
327 int32_t ret;
328 struct HAL_UART_CFG_T *uartCfg = NULL;
329 if (device == NULL) {
330 HDF_LOGE("%s: INVALID PARAM!\r\n", __func__);
331 return;
332 }
333
334 uartId = device->uartId;
335 if (uartId > MAX_UART_ID) {
336 HDF_LOGE("%s %d NOT SUPPORT!\r\n", __FILE__, __LINE__);
337 return;
338 }
339 ret = InitUartCtxCfg(device);
340 if (ret != HDF_SUCCESS) {
341 HDF_LOGE("%s %d InitUartCtxCfg failed\r\n", __FILE__, __LINE__);
342 return;
343 }
344 if (!g_uartKfifoBuffer[uartId]) {
345 g_uartKfifoBuffer[uartId] = (char *)OsalMemAlloc(UART_FIFO_MAX_BUFFER);
346 if (!g_uartKfifoBuffer[uartId]) {
347 HDF_LOGE("kfifo OsalMemAlloc failed!");
348 return;
349 }
350 kfifo_init(&g_uartCtx[uartId].fifo, g_uartKfifoBuffer[uartId], UART_FIFO_MAX_BUFFER);
351 }
352
353 if (OsalSemInit(&g_uartCtx[uartId].rxSem, 0) != HDF_SUCCESS) {
354 HDF_LOGE("UART rxsem init failed!");
355 return;
356 }
357 if (OsalSemInit(&g_uartCtx[uartId].txSem, 0) != HDF_SUCCESS) {
358 HDF_LOGE("UART txsem init failed!");
359 return;
360 }
361
362 if (g_uartCtx[uartId].rxDMA == true) {
363 HDF_LOGI("uart %u start dma rx\r\n", uartId);
364 hal_uart_irq_set_dma_handler(uartId, g_uartCtx[uartId].UartDmaRxHandler, g_uartCtx[uartId].UartDmaTxHandler);
365 HalUartStartDmaRx(uartId);
366 } else {
367 HalUartStartRx(uartId);
368 }
369 }
370
UartStart(struct UartDevice * device)371 static void UartStart(struct UartDevice *device)
372 {
373 uint32_t uartId;
374 struct HAL_UART_CFG_T *uartCfg = NULL;
375 if (device == NULL) {
376 HDF_LOGE("%s: INVALID PARAM", __func__);
377 return;
378 }
379 uartId = device->uartId;
380 if (uartId > MAX_UART_ID) {
381 HDF_LOGE("%s %d NOT SUPPORT \r\n", __FILE__, __LINE__);
382 return;
383 }
384 uartCfg = &device->config;
385 if (uartCfg == NULL) {
386 HDF_LOGE("%s: INVALID OBJECT", __func__);
387 return;
388 }
389 hal_uart_open(uartId, uartCfg);
390 HalUartHandlerInit(device);
391 }
392
393 /* HdfDriverEntry method definitions */
394 static int32_t UartDriverBind(struct HdfDeviceObject *device);
395 static int32_t UartDriverInit(struct HdfDeviceObject *device);
396 static void UartDriverRelease(struct HdfDeviceObject *device);
397
398 /* HdfDriverEntry definitions */
399 struct HdfDriverEntry g_UartDriverEntry = {
400 .moduleVersion = 1,
401 .moduleName = "HDF_UART_MODULE_HDF",
402 .Bind = UartDriverBind,
403 .Init = UartDriverInit,
404 .Release = UartDriverRelease,
405 };
406
407 /* Initialize HdfDriverEntry */
408 HDF_INIT(g_UartDriverEntry);
409
410 /* UartHostMethod method definitions */
411 static int32_t UartHostDevInit(struct UartHost *host);
412 static int32_t UartHostDevDeinit(struct UartHost *host);
413 static int32_t UartHostDevWrite(struct UartHost *host, uint8_t *data, uint32_t size);
414 static int32_t UartHostDevSetBaud(struct UartHost *host, uint32_t baudRate);
415 static int32_t UartHostDevGetBaud(struct UartHost *host, uint32_t *baudRate);
416 static int32_t UartHostDevRead(struct UartHost *host, uint8_t *data, uint32_t size);
417 static int32_t UartHostDevSetAttribute(struct UartHost *host, struct UartAttribute *attribute);
418 static int32_t UartHostDevGetAttribute(struct UartHost *host, struct UartAttribute *attribute);
419 static int32_t UartHostDevSetTransMode(struct UartHost *host, enum UartTransMode mode);
420
421 /* UartHostMethod definitions */
422 struct UartHostMethod g_uartHostMethod = {
423 .Init = UartHostDevInit,
424 .Deinit = UartHostDevDeinit,
425 .Read = UartHostDevRead,
426 .Write = UartHostDevWrite,
427 .SetBaud = UartHostDevSetBaud,
428 .GetBaud = UartHostDevGetBaud,
429 .SetAttribute = UartHostDevSetAttribute,
430 .GetAttribute = UartHostDevGetAttribute,
431 .SetTransMode = UartHostDevSetTransMode,
432 };
433
InitUartDevice(struct UartHost * host)434 static int InitUartDevice(struct UartHost *host)
435 {
436 HDF_LOGI("%s: Enter", __func__);
437 struct UartDevice *uartDevice = NULL;
438 struct HAL_UART_CFG_T *uartCfg = NULL;
439 struct UartResource *resource = NULL;
440 if (host == NULL || host->priv == NULL) {
441 HDF_LOGE("%s: invalid parameter", __func__);
442 return HDF_ERR_INVALID_PARAM;
443 }
444
445 uartDevice = (struct UartDevice *)host->priv;
446 if (uartDevice == NULL) {
447 HDF_LOGE("%s: INVALID OBJECT", __func__);
448 return HDF_ERR_INVALID_OBJECT;
449 }
450 resource = &uartDevice->resource;
451 if (resource == NULL) {
452 HDF_LOGE("%s: INVALID OBJECT", __func__);
453 return HDF_ERR_INVALID_OBJECT;
454 }
455 uartCfg = &uartDevice->config;
456 if (uartCfg == NULL) {
457 HDF_LOGE("%s: INVALID OBJECT", __func__);
458 return HDF_ERR_INVALID_OBJECT;
459 }
460 uint32_t uartId = resource->num;
461 if (uartId > MAX_UART_ID) {
462 HDF_LOGE("%s %d NOT SUPPORT \r\n", __FILE__, __LINE__);
463 return HDF_ERR_NOT_SUPPORT;
464 }
465 uartCfg->flow = HAL_UART_FLOW_CONTROL_NONE;
466 uartCfg->tx_level = HAL_UART_FIFO_LEVEL_1_8;
467 uartCfg->rx_level = HAL_UART_FIFO_LEVEL_1_2;
468 uartCfg->dma_rx_stop_on_err = false;
469
470 if (!uartDevice->initFlag) {
471 HDF_LOGE("uart %u device init\r\n", uartDevice->uartId);
472 HalSetUartIomux(uartDevice->uartId);
473 UartStart(uartDevice);
474 uartDevice->initFlag = true;
475 }
476 return HDF_SUCCESS;
477 }
478
GetUartDeviceResource(struct UartDevice * device,const struct DeviceResourceNode * resourceNode)479 static uint32_t GetUartDeviceResource(
480 struct UartDevice *device, const struct DeviceResourceNode *resourceNode)
481 {
482 struct DeviceResourceIface *dri = NULL;
483 struct UartResource *resource = NULL;
484 if (device == NULL || resourceNode == NULL) {
485 HDF_LOGE("%s: INVALID PARAM", __func__);
486 return HDF_ERR_INVALID_PARAM;
487 }
488 resource = &device->resource;
489 if (resource == NULL) {
490 HDF_LOGE("%s: INVALID OBJECT", __func__);
491 return HDF_ERR_INVALID_OBJECT;
492 }
493
494 dri = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
495 if (dri == NULL || dri->GetUint32 == NULL) {
496 HDF_LOGE("DeviceResourceIface is invalid");
497 return HDF_ERR_INVALID_PARAM;
498 }
499 if (dri->GetUint32(resourceNode, "num", &resource->num, 0) != HDF_SUCCESS) {
500 HDF_LOGE("uart config read num fail");
501 return HDF_FAILURE;
502 }
503 if (dri->GetUint32(resourceNode, "baudrate", &resource->baudRate, 0) != HDF_SUCCESS) {
504 HDF_LOGE("uart config read baudrate fail");
505 return HDF_FAILURE;
506 }
507 if (dri->GetUint32(resourceNode, "parity", &resource->parity, 0) != HDF_SUCCESS) {
508 HDF_LOGE("uart config read parity fail");
509 return HDF_FAILURE;
510 }
511 if (dri->GetUint32(resourceNode, "stopBit", &resource->stopBit, 0) != HDF_SUCCESS) {
512 HDF_LOGE("uart config read stopBit fail");
513 return HDF_FAILURE;
514 }
515 if (dri->GetUint32(resourceNode, "data", &resource->wLen, 0) != HDF_SUCCESS) {
516 HDF_LOGE("uart config read data fail");
517 return HDF_FAILURE;
518 }
519
520 resource->txDMA = dri->GetBool(resourceNode, "txDMA");
521 resource->rxDMA = dri->GetBool(resourceNode, "rxDMA");
522
523 // copy config
524 device->initFlag = false;
525 device->uartId = resource->num;
526 device->config.baud = resource->baudRate;
527 device->config.parity = resource->parity;
528 device->config.stop = resource->stopBit;
529 device->config.data = resource->wLen;
530 device->config.dma_rx = resource->rxDMA;
531 device->config.dma_tx = resource->txDMA;
532 return HDF_SUCCESS;
533 }
534
AttachUartDevice(struct UartHost * uartHost,struct HdfDeviceObject * device)535 static int32_t AttachUartDevice(struct UartHost *uartHost, struct HdfDeviceObject *device)
536 {
537 int32_t ret;
538 struct UartDevice *uartDevice = NULL;
539 #ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO
540 if (device == NULL || uartHost == NULL) {
541 #else
542 if (uartHost == NULL || device == NULL || device->property == NULL) {
543 #endif
544 HDF_LOGE("%s: property is NULL", __func__);
545 return HDF_ERR_INVALID_PARAM;
546 }
547
548 uartDevice = (struct UartDevice *)OsalMemAlloc(sizeof(struct UartDevice));
549 if (uartDevice == NULL) {
550 HDF_LOGE("%s: OsalMemCalloc uartDevice error", __func__);
551 return HDF_ERR_MALLOC_FAIL;
552 }
553 #ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO
554 ret = GetUartDeviceResource(uartDevice, device->deviceMatchAttr);
555 #else
556 ret = GetUartDeviceResource(uartDevice, device->property);
557 #endif
558 if (ret != HDF_SUCCESS) {
559 (void)OsalMemFree(uartDevice);
560 return HDF_FAILURE;
561 }
562
563 uartHost->priv = uartDevice;
564
565 return InitUartDevice(uartHost);
566 }
567
568 static int32_t UartDriverBind(struct HdfDeviceObject *device)
569 {
570 struct UartHost *devService = NULL;
571 if (device == NULL) {
572 HDF_LOGE("%s: invalid parameter", __func__);
573 return HDF_ERR_INVALID_PARAM;
574 }
575 devService = (struct UartHost *)OsalMemAlloc(sizeof(*devService));
576 if (devService == NULL) {
577 HDF_LOGE("%s: OsalMemCalloc error", __func__);
578 return HDF_ERR_INVALID_OBJECT;
579 }
580 devService->device = device;
581 device->service = &(devService->service);
582 devService->priv = NULL;
583 devService->method = NULL;
584 return HDF_SUCCESS;
585 }
586
587 static void UartDriverRelease(struct HdfDeviceObject *device)
588 {
589 HDF_LOGI("Enter %s:", __func__);
590 uint32_t uartId;
591 struct UartHost *host = NULL;
592 struct UartDevice *uartDevice = NULL;
593 if (device == NULL) {
594 HDF_LOGE("%s: device is NULL", __func__);
595 return;
596 }
597
598 host = UartHostFromDevice(device);
599 if (host == NULL || host->priv == NULL) {
600 HDF_LOGE("%s: host is NULL", __func__);
601 return;
602 }
603
604 uartDevice = (struct UartDevice *)host->priv;
605 if (uartDevice == NULL) {
606 HDF_LOGE("%s: INVALID OBJECT", __func__);
607 return;
608 }
609 uartId = uartDevice->uartId;
610 host->method = NULL;
611
612 OsalSemDestroy(&g_uartCtx[uartId].rxSem);
613 OsalSemDestroy(&g_uartCtx[uartId].txSem);
614 OsalMemFree(uartDevice);
615 OsalMemFree(host);
616 }
617
618 static int32_t UartDriverInit(struct HdfDeviceObject *device)
619 {
620 HDF_LOGI("Enter %s:", __func__);
621 int32_t ret;
622 struct UartHost *host = NULL;
623
624 if (device == NULL) {
625 HDF_LOGE("%s: device is NULL", __func__);
626 return HDF_ERR_INVALID_OBJECT;
627 }
628
629 host = UartHostFromDevice(device);
630 if (host == NULL) {
631 HDF_LOGE("%s: host is NULL", __func__);
632 return HDF_ERR_INVALID_OBJECT;
633 }
634
635 ret = AttachUartDevice(host, device);
636 if (ret != HDF_SUCCESS) {
637 HDF_LOGE("%s: attach error", __func__);
638 return HDF_FAILURE;
639 }
640
641 host->method = &g_uartHostMethod;
642
643 return ret;
644 }
645
646 /* UartHostMethod implementations */
647 static int32_t UartHostDevInit(struct UartHost *host)
648 {
649 HDF_LOGI("%s: Enter\r\n", __func__);
650 if (host == NULL) {
651 HDF_LOGE("%s: invalid parameter", __func__);
652 return HDF_ERR_INVALID_PARAM;
653 }
654
655 return InitUartDevice(host);
656 }
657
658 static int32_t UartHostDevDeinit(struct UartHost *host)
659 {
660 HDF_LOGI("%s: Enter", __func__);
661 uint32_t uartId;
662 struct UartDevice *uartDevice = NULL;
663 if (host == NULL || host->priv == NULL) {
664 HDF_LOGE("%s: invalid parameter", __func__);
665 return HDF_ERR_INVALID_PARAM;
666 }
667
668 uartDevice = (struct UartDevice *)host->priv;
669 if (uartDevice == NULL) {
670 HDF_LOGE("%s: INVALID OBJECT", __func__);
671 return HDF_ERR_INVALID_OBJECT;
672 }
673 uartId = uartDevice->uartId;
674 uartDevice->initFlag = false;
675
676 hal_uart_close(uartId);
677
678 return HDF_SUCCESS;
679 }
680
681 static int32_t UartHostDevWrite(struct UartHost *host, uint8_t *data, uint32_t size)
682 {
683 struct UartDevice *device = NULL;
684 uint32_t uartId;
685
686 if (host == NULL || data == NULL || size == 0 || host->priv == NULL) {
687 HDF_LOGE("%s: invalid parameter", __func__);
688 return HDF_ERR_INVALID_PARAM;
689 }
690
691 device = (struct UartDevice *)host->priv;
692 if (device == NULL) {
693 HDF_LOGE("%s: device is NULL", __func__);
694 return HDF_ERR_INVALID_OBJECT;
695 }
696
697 uartId = device->uartId;
698 if (uartId > MAX_UART_ID) {
699 HDF_LOGE("%s %d NOT SUPPORT \r\n", __FILE__, __LINE__);
700 return HDF_ERR_NOT_SUPPORT;
701 }
702 if (g_uartCtx[uartId].txDMA) {
703 HalUartSend(uartId, data, size, HDF_UART_TMO);
704 } else {
705 for (uint32_t idx = 0; idx < size; idx++) {
706 if (g_uartCtx[uartId].isBlock) {
707 hal_uart_blocked_putc(uartId, data[idx]);
708 } else {
709 hal_uart_putc(uartId, data[idx]);
710 }
711 }
712 }
713
714 return HDF_SUCCESS;
715 }
716
717 static int32_t UartHostDevRead(struct UartHost *host, uint8_t *data, uint32_t size)
718 {
719 uint32_t recvSize;
720 int32_t ret;
721 uint32_t uartId;
722 struct UartDevice *uartDevice = NULL;
723 if (host == NULL || data == NULL || host->priv == NULL) {
724 HDF_LOGE("%s: invalid parameter", __func__);
725 return HDF_ERR_INVALID_PARAM;
726 }
727
728 uartDevice = (struct UartDevice *)host->priv;
729 if (uartDevice == NULL) {
730 HDF_LOGE("%s: device is NULL", __func__);
731 return HDF_ERR_INVALID_OBJECT;
732 }
733
734 uartId = uartDevice->uartId;
735 if (g_uartCtx[uartId].rxDMA) {
736 ret = HalUartRecv(uartId, data, size, &recvSize, HDF_UART_TMO);
737 if (ret != HDF_SUCCESS) {
738 HDF_LOGE("uart %ld recev error\r\n", uartId);
739 return ret;
740 }
741 ret = recvSize;
742 } else {
743 if (g_uartCtx[uartId].isBlock) {
744 data[0] = hal_uart_blocked_getc(uartId);
745 } else {
746 if (hal_uart_readable(uartId) > 0) {
747 data[0] = hal_uart_getc(uartId);
748 }
749 }
750 ret = 1;
751 }
752
753 return ret;
754 }
755
756 static int32_t UartHostDevSetBaud(struct UartHost *host, uint32_t baudRate)
757 {
758 HDF_LOGI("%s: Enter", __func__);
759 struct UartDevice *uartDevice = NULL;
760 struct HAL_UART_CFG_T *uartCfg = NULL;
761 uint32_t uartId;
762 if (host == NULL || host->priv == NULL) {
763 HDF_LOGE("%s: invalid parameter", __func__);
764 return HDF_ERR_INVALID_PARAM;
765 }
766
767 uartDevice = (struct UartDevice *)host->priv;
768 if (uartDevice == NULL) {
769 HDF_LOGE("%s: device is NULL", __func__);
770 return HDF_ERR_INVALID_OBJECT;
771 }
772 uartId = uartDevice->uartId;
773 if (uartId > MAX_UART_ID) {
774 HDF_LOGE("%s %d NOT SUPPORT \r\n", __FILE__, __LINE__);
775 return HDF_ERR_NOT_SUPPORT;
776 }
777 uartCfg = &uartDevice->config;
778 if (uartCfg == NULL) {
779 HDF_LOGE("%s: device config is NULL", __func__);
780 return HDF_ERR_INVALID_OBJECT;
781 }
782 uartCfg->baud = baudRate;
783
784 hal_uart_open(uartId, uartCfg);
785
786 return HDF_SUCCESS;
787 }
788
789 static int32_t UartHostDevGetBaud(struct UartHost *host, uint32_t *baudRate)
790 {
791 HDF_LOGI("%s: Enter", __func__);
792 struct UartDevice *uartDevice = NULL;
793 struct HAL_UART_CFG_T *uartCfg = NULL;
794 uint32_t uartId;
795 if (host == NULL || baudRate == NULL || host->priv == NULL) {
796 HDF_LOGE("%s: invalid parameter", __func__);
797 return HDF_ERR_INVALID_PARAM;
798 }
799 uartDevice = (struct UartDevice *)host->priv;
800 if (uartDevice == NULL) {
801 HDF_LOGE("%s: device is NULL", __func__);
802 return HDF_ERR_INVALID_OBJECT;
803 }
804 uartId = uartDevice->uartId;
805 if (uartId > MAX_UART_ID) {
806 HDF_LOGE("%s %d NOT SUPPORT \r\n", __FILE__, __LINE__);
807 return HDF_ERR_NOT_SUPPORT;
808 }
809 uartCfg = &uartDevice->config;
810 if (uartCfg == NULL) {
811 HDF_LOGE("%s: device is NULL", __func__);
812 return HDF_ERR_INVALID_OBJECT;
813 }
814 baudRate = &uartCfg->baud;
815 if (baudRate == NULL) {
816 HDF_LOGE("%s: baudRate is NULL", __func__);
817 return HDF_ERR_INVALID_OBJECT;
818 }
819 return HDF_SUCCESS;
820 }
821
822 static int32_t SetUartDevConfig(struct UartAttribute *attribute, struct UartDevice *uartDevice)
823 {
824 struct HAL_UART_CFG_T *uartCfg = NULL;
825 uint32_t uartId;
826 if (attribute == NULL || uartDevice == NULL) {
827 HDF_LOGE("%s: invalid parameter", __func__);
828 return HDF_ERR_INVALID_PARAM;
829 }
830 uartId = uartDevice->uartId;
831 uartCfg = &uartDevice->config;
832 if (uartCfg == NULL) {
833 HDF_LOGE("%s: config is NULL", __func__);
834 return HDF_ERR_INVALID_OBJECT;
835 }
836
837 switch (attribute->dataBits) {
838 case UART_ATTR_DATABIT_8:
839 uartCfg->data = HAL_UART_DATA_BITS_8;
840 break;
841 case UART_ATTR_DATABIT_7:
842 uartCfg->data = HAL_UART_DATA_BITS_7;
843 break;
844 case UART_ATTR_DATABIT_6:
845 uartCfg->data = HAL_UART_DATA_BITS_6;
846 break;
847 case UART_ATTR_DATABIT_5:
848 uartCfg->data = HAL_UART_DATA_BITS_5;
849 break;
850 default:
851 uartCfg->data = HAL_UART_DATA_BITS_8;
852 break;
853 }
854
855 uartCfg->parity = attribute->parity;
856
857 switch (attribute->stopBits) {
858 case UART_ATTR_STOPBIT_1:
859 case UART_ATTR_STOPBIT_2:
860 uartCfg->stop = attribute->stopBits;
861 break;
862 default:
863 uartCfg->stop = UART_ATTR_STOPBIT_1;
864 break;
865 }
866
867 if (attribute->rts && attribute->cts) {
868 uartCfg->flow = HAL_UART_FLOW_CONTROL_RTSCTS;
869 } else if (attribute->rts && !attribute->cts) {
870 uartCfg->flow = HAL_UART_FLOW_CONTROL_RTS;
871 } else if (!attribute->rts && attribute->cts) {
872 uartCfg->flow = HAL_UART_FLOW_CONTROL_CTS;
873 } else {
874 uartCfg->flow = HAL_UART_FLOW_CONTROL_NONE;
875 }
876 hal_uart_open(uartId, uartCfg);
877 return HDF_SUCCESS;
878 }
879
880 static int32_t UartHostDevSetAttribute(struct UartHost *host, struct UartAttribute *attribute)
881 {
882 HDF_LOGI("%s: Enter", __func__);
883 struct UartDevice *uartDevice = NULL;
884 int ret;
885 if (host == NULL || attribute == NULL || host->priv == NULL) {
886 HDF_LOGE("%s: invalid parameter", __func__);
887 return HDF_ERR_INVALID_PARAM;
888 }
889
890 uartDevice = (struct UartDevice *)host->priv;
891 if (uartDevice == NULL) {
892 HDF_LOGE("%s: device is NULL", __func__);
893 return HDF_ERR_INVALID_OBJECT;
894 }
895
896 ret = SetUartDevConfig(attribute, uartDevice);
897 if (ret != HDF_SUCCESS) {
898 HDF_LOGE("%s: SetUartDevConfig error", __func__);
899 return HDF_FAILURE;
900 }
901 return HDF_SUCCESS;
902 }
903
904 static int32_t GetUartDevConfig(struct UartAttribute *attribute, struct HAL_UART_CFG_T *uartCfg)
905 {
906 if (attribute == NULL || uartCfg == NULL) {
907 HDF_LOGE("%s: invalid parameter", __func__);
908 return HDF_ERR_INVALID_PARAM;
909 }
910 switch (uartCfg->data) {
911 case HAL_UART_DATA_BITS_8:
912 attribute->dataBits = UART_ATTR_DATABIT_8;
913 break;
914 case HAL_UART_DATA_BITS_7:
915 attribute->dataBits = UART_ATTR_DATABIT_7;
916 break;
917 case HAL_UART_DATA_BITS_6:
918 attribute->dataBits = UART_ATTR_DATABIT_6;
919 break;
920 case HAL_UART_DATA_BITS_5:
921 attribute->dataBits = UART_ATTR_DATABIT_5;
922 break;
923 default:
924 attribute->dataBits = UART_ATTR_DATABIT_8;
925 break;
926 }
927
928 attribute->parity = uartCfg->parity;
929 attribute->stopBits = uartCfg->stop;
930
931 switch (uartCfg->flow) {
932 case HAL_UART_FLOW_CONTROL_NONE:
933 attribute->rts = 0;
934 attribute->cts = 0;
935 break;
936 case HAL_UART_FLOW_CONTROL_CTS:
937 attribute->rts = 0;
938 attribute->cts = 1;
939 break;
940 case HAL_UART_FLOW_CONTROL_RTS:
941 attribute->rts = 1;
942 attribute->cts = 0;
943 break;
944 case HAL_UART_FLOW_CONTROL_RTSCTS:
945 attribute->rts = 1;
946 attribute->cts = 1;
947 break;
948 default:
949 attribute->rts = 0;
950 attribute->cts = 0;
951 break;
952 }
953 return HDF_SUCCESS;
954 }
955
956 static int32_t UartHostDevGetAttribute(struct UartHost *host, struct UartAttribute *attribute)
957 {
958 HDF_LOGI("%s: Enter", __func__);
959 struct UartDevice *uartDevice = NULL;
960 struct HAL_UART_CFG_T *uartCfg = NULL;
961 if (host == NULL || attribute == NULL) {
962 HDF_LOGE("%s: invalid parameter", __func__);
963 return HDF_ERR_INVALID_PARAM;
964 }
965
966 uartDevice = (struct UartDevice *)host->priv;
967 if (uartDevice == NULL) {
968 HDF_LOGE("%s: device is NULL", __func__);
969 return HDF_ERR_INVALID_OBJECT;
970 }
971 uartCfg = &uartDevice->config;
972 if (uartCfg == NULL) {
973 HDF_LOGE("%s: config is NULL", __func__);
974 return HDF_ERR_INVALID_OBJECT;
975 }
976
977 return GetUartDevConfig(attribute, uartCfg);
978 }
979
980 static int32_t UartHostDevSetTransMode(struct UartHost *host, enum UartTransMode mode)
981 {
982 HDF_LOGI("%s: Enter", __func__);
983 struct UartDevice *uartDevice = NULL;
984 uint32_t uartId;
985 if (host == NULL || host->priv == NULL) {
986 HDF_LOGE("%s: invalid parameter", __func__);
987 return HDF_ERR_INVALID_PARAM;
988 }
989
990 uartDevice = (struct UartDevice *)host->priv;
991 if (uartDevice == NULL) {
992 HDF_LOGE("%s: device is NULL", __func__);
993 return HDF_ERR_INVALID_OBJECT;
994 }
995 uartId = uartDevice->uartId;
996 if (uartId > MAX_UART_ID) {
997 HDF_LOGE("%s %d NOT SUPPORT \r\n", __FILE__, __LINE__);
998 return HDF_ERR_NOT_SUPPORT;
999 }
1000 switch (mode) {
1001 case UART_MODE_RD_BLOCK:
1002 g_uartCtx[uartId].isBlock = true;
1003 break;
1004 case UART_MODE_RD_NONBLOCK:
1005 g_uartCtx[uartId].isBlock = false;
1006 break;
1007 case UART_MODE_DMA_RX_EN:
1008 g_uartCtx[uartId].rxDMA = true;
1009 break;
1010 case UART_MODE_DMA_RX_DIS:
1011 g_uartCtx[uartId].rxDMA = false;
1012 break;
1013 case UART_MODE_DMA_TX_EN:
1014 g_uartCtx[uartId].txDMA = true;
1015 break;
1016 case UART_MODE_DMA_TX_DIS:
1017 g_uartCtx[uartId].txDMA = false;
1018 break;
1019 default:
1020 HDF_LOGE("%s: UartTransMode(%d) invalid", __func__, mode);
1021 break;
1022 }
1023 return HDF_SUCCESS;
1024 }
1025