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