blob: 70299766b423774c67e0b2b28fc3009875e890ce [file] [log] [blame]
xf.li2f424182024-08-20 00:47:34 -07001/****************************************************************************/
2/*
3 * zx29_uart.c sanchips
4 *
5 * (C) Copyright 2003-2007, gaowei
6 * (C) Copyright 2003-2007, sanchips
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13/****************************************************************************/
14#include <linux/kernel.h>
15#include <linux/init.h>
16#include <linux/timer.h>
17#include <linux/interrupt.h>
18#include <linux/module.h>
19#include <linux/console.h>
20#include <linux/tty.h>
21#include <linux/tty_flip.h>
22#include <linux/serial.h>
23#include <linux/serial_core.h>
24#include <linux/platform_device.h>
25#include <linux/of.h>
26#include <linux/of_irq.h>
27#include <linux/io.h>
28#include <linux/printk.h>
29#include <linux/clk.h>
30#include <linux/gpio.h>
31#include <linux/delay.h>
32#include <linux/dmaengine.h>
33#include <linux/dma-mapping.h>
34#include <linux/slab.h>
35#include <linux/scatterlist.h>
36#include <linux/device.h>
37#include <linux/ioport.h>
38#include <linux/sched/clock.h>
39#include <linux/soc/zte/spinlock.h>
40
41#if 0
42#include <mach/gpio_def.h>
43#include <mach/irqs.h>
44#include <mach/board.h>
45#include <mach/gpio.h>
46#include <mach/debug.h>
47#include <mach/iomap.h>
48#include <mach/dma.h>
49#include <mach/dma_cfg.h>
50#endif
51//#include <linux/wakelock.h>
52#include <linux/kthread.h>
53#include <linux/semaphore.h>
54
55#include <linux/dma/zx-dma.h>
56//#include "../../dma/zte/zx298501_dma.h"
57
58#include "zx29_uart.h"
59#include <linux/soc/zte/rpmsg.h>
60#include <linux/soc/sc/drv_idle.h>
61#include "pub_debug_info.h"
62//#include <linux/soc/zte/pm/drv_idle.h>
63//#include <mach/pcu.h>
64//#define DEBUG_UART
65
66#ifdef DEBUG_UART
67#pragma GCC optimize("O0")
68#endif
69
70#define UART_WCLK_NAME "uartclk"
71#define UART_APBCLK_NAME "apb_pclk"
72
73#define CONFIG_SERIAL_ZX29_DMA 1
74
75
76extern bool xp2xp_Ap2CpIsApWakeup(void);
77extern int xp2xp_enable_4line(void);
78
79extern signed int zx29_dma_stop(unsigned int channel_id);
80extern signed int zx29_dma_get_transfer_num(unsigned int channel_id);
81
82
83
84char uart_names[5][12] = {
85 "zx29_uart.0",
86 "zx29_uart.1",
87 "zx29_uart.2",
88 "zx29_uart.3",
89 "zx29_uart.4"
90};
91
92#if CONFIG_SERIAL_ZX29_DMA
93#define ZX29_DMA_BUFFER_SIZE PAGE_SIZE
94#define UART_DMA_RX_MAX_COUNT 2
95//#define RX_DMA_TIMEOUT (HZ / 10)//60
96#define RX_DMA_TIMEOUT (HZ / 100)
97#define RX_DMA_WORK 1
98struct zx29_sgbuf {
99 struct scatterlist sg;
100 dma_addr_t dma_addr;
101 char *buf;
102};
103
104struct zx29_dmarx_data {
105 struct dma_chan *chan;
106 struct completion complete;
107 dma_channel_def rx_def[UART_DMA_RX_MAX_COUNT];
108 u32 rx_index;
109 bool use_buf_b;
110 struct zx29_sgbuf sgbuf_a;
111 struct zx29_sgbuf sgbuf_b;
112 dma_cookie_t cookie;
113 bool running;
114 atomic_t count;
115 bool used;
116};
117
118struct zx29_dmatx_data {
119 struct dma_chan *chan;
120 struct completion complete;
121 dma_channel_def tx_def;
122 struct scatterlist sg;
123 char *buf;
124 bool queued;
125 atomic_t count;
126};
xf.li1867bfa2024-08-20 02:32:16 -0700127#define UART_DEBUG_RECORDER_BYTE 0
xf.li2f424182024-08-20 00:47:34 -0700128#define UART_DMA_CYCLE_RX_CONFIG_COUNT 5
129struct zx29_dma_cycle_data{
130 int id;
131 int flg_enter_th;
132 int flg_enter_to;
133 char flg_overrun;
134 char flg_pe;
135 char flg_be;
136 char flg_fe;
137 char from_resume;
138 unsigned long cnt_callback_total;
139 unsigned long cnt_th_total;
140 int cnt_callback;
141 int cnt_th;
142 struct zx29_sgbuf sgbuf[UART_DMA_CYCLE_RX_CONFIG_COUNT];
143 dma_channel_def rxdef[UART_DMA_CYCLE_RX_CONFIG_COUNT];
xf.li1867bfa2024-08-20 02:32:16 -0700144 bool enter_throttle;
145 bool from_unthrottle;
146 bool used;
147 unsigned int cnt_throttle;
148 unsigned int cnt_unthrottle;
xf.li2f424182024-08-20 00:47:34 -0700149};
xf.li1867bfa2024-08-20 02:32:16 -0700150struct zx29_dma_cycle_data uart_dma_cycle[6];
xf.li2f424182024-08-20 00:47:34 -0700151#endif
152
153
154
155#define UART_NUM 5
156int g_uart_overrun[5];
157ktime_t g_hr_interval;
158
159
160int g_cons_id_cmdline;
161EXPORT_SYMBOL(g_cons_id_cmdline);
162
163#ifdef DEBUG_CONSOLE
164#undef DEBUG_CONSOLE
165#endif
166#define DEBUG_CONSOLE g_cons_id_cmdline
167/****************************************************************************/
168
169/* Use device name ttyS, major 4, minor 64-68. This is the usual serial port
170 * name, but it is legally reserved for the 8250 driver. */
171#define SERIAL_zx29_MAJOR TTY_MAJOR
172#define SERIAL_MINOR_START 64
173
174#define UART_PORT_AUTOBAUD_ON 1
175#define UART_PORT_AUTOBAUD_OFF 0
176#define UART_PORT_AUTOBAUD_BYTE 2
177#define UART_AT_SENDOK_NUM 6
178#define UART_AUTOBAUD_LEVEL 5
179#define UART_AUTOBAUD_CHECKBYTE 4
180#define UART_AUTOBAUD_RATE 115200
181#define UART1_AUTOBAUD_RATE 921600
182
183
184unsigned char uart_port_autobaud_buffer[UART_PORT_AUTOBAUD_BYTE] = {0};
185unsigned char uart_port_autobaud_gtflag = 0 ;
186unsigned char uart_port_autobaud_suflag = 0 ;
187unsigned char g_console_open_flag = 1;
188
189
190unsigned char UART_AT_send_ok[UART_AT_SENDOK_NUM] =
191 {
192 0x0d,0x0a,0x4F,0x4B,0x0d,0x0a
193 };
194
195unsigned char UART_baud_check[UART_AUTOBAUD_LEVEL][UART_AUTOBAUD_CHECKBYTE]=
196 {
197 {0x61,0x74,0x41,0x54},{0x06,0x9e,0x06,0x98},{0x1c,0x80,0x1c,0x00},
198 {0xe0,0x00,0xe0,0x00},{0x00,0x00,0x00,0x00},
199 };
200unsigned int UART_baud[UART_AUTOBAUD_LEVEL] =
201 {
202 115200,57600,38400,19200,9600
203 };
204unsigned int UART_termios_cflag[UART_AUTOBAUD_LEVEL] =
205 {
206 B115200,B57600,B38400,B19200,B9600
207 };
208
209#ifdef CONFIG_SERIAL_CORE_CONSOLE
210#define uart_console(port) ((port)->cons && (port)->cons->index == (port)->line)
211#else
212#define uart_console(port) (0)
213#endif
214
215/****************************************************************************/
216/*
217 * Local per-uart structure.
218 */
219struct zx29_uart_port
220{
221 struct uart_port port;
222 unsigned int sigs; /* Local copy of line sigs */
223 unsigned int old_status;
224 unsigned char imr; /* Local interrupt mask reg mirror */
225#if CONFIG_SERIAL_ZX29_DMA
226 unsigned char dmacr; /* DMA reg*/
227#endif
228 bool rts_state;
229 bool autorts; /* hardware flow control */
230 struct clk *wclk; /* uart work clock */
231 struct clk *busclk; /* uart apb clock */
232 bool autobaud;
233 bool autobaud_state;
234 unsigned int baudrate;
235 bool uartwake;
236
237 int irq;
238 int irq_state;
239 int rxd_irq;
240 struct tasklet_struct write_wakeup;
241 bool rxd_wakeup;
242 int rxd_int_depth;
243 bool enter_suspend;
244#if CONFIG_SERIAL_ZX29_DMA
245 /* DMA stuff */
246 bool using_tx_dma;
247 bool using_rx_dma;
248 struct zx29_dmarx_data dmarx;
249 struct zx29_dmatx_data dmatx;
250 struct timer_list rx_dma_timer;
251 struct hrtimer rx_dma_hrtimer;
252 struct task_struct *dma_compl_th;
253 struct semaphore sema;
254 struct semaphore sema_cyclic;
255 bool port_close;
256 bool work_state;
257 size_t pre_pending;
258 struct zx29_sgbuf *sg2tty;
259 size_t sg2tty_len;
260 struct zx29_sgbuf *curr_sg;
261 int enable_ctsrts;
262 int enable_wakeup;
263
264 struct notifier_block wakeup_notifier;
265
266#endif
267 //means application decide close and release DMA &wakelock
268 int app_ctrl;
269 int sleep_state;
270 //if app_ctrl is set or using kernel control sleep,set this flag
271 int uart_power_mode;
272};
273
274
275
276static struct zx29_uart_port zx29_uart_ports[UART_NUM];
277
278#define zx29_MAXPORTS ARRAY_SIZE(zx29_uart_ports)
279typedef struct __UART_STATIC{
280 int cnt;
281 char head[16];
282 unsigned long long s_time;
283 int func_step;
284 unsigned int fr;
285 unsigned int ris;
286}uart_static;
287#define STATIC_UART_ID 0
xf.li1867bfa2024-08-20 02:32:16 -0700288#define UART_STATIC_COUNT 512
289#define UART_STATIC_NUM 4
290uart_static g_uart_static[UART_STATIC_NUM][UART_STATIC_COUNT] = {0};
291int g_uart_static_cnt[UART_STATIC_NUM] = {0};
292void test_uart_static(int uart_id, char *buf, unsigned int cnt, int steps)
xf.li2f424182024-08-20 00:47:34 -0700293{
xf.li1867bfa2024-08-20 02:32:16 -0700294 //if(uart_id != STATIC_UART_ID)
295 // return;
xf.li2f424182024-08-20 00:47:34 -0700296 if(buf){
297 if(cnt >= 16){
xf.li1867bfa2024-08-20 02:32:16 -0700298 memcpy(g_uart_static[uart_id][g_uart_static_cnt[uart_id]].head, buf, 16);
xf.li2f424182024-08-20 00:47:34 -0700299 }else{
xf.li1867bfa2024-08-20 02:32:16 -0700300 memcpy(g_uart_static[uart_id][g_uart_static_cnt[uart_id]].head, buf, cnt);
xf.li2f424182024-08-20 00:47:34 -0700301 }
302 }
xf.li1867bfa2024-08-20 02:32:16 -0700303 g_uart_static[uart_id][g_uart_static_cnt[uart_id]].cnt = cnt;
304 g_uart_static[uart_id][g_uart_static_cnt[uart_id]].s_time = local_clock();
305 g_uart_static[uart_id][g_uart_static_cnt[uart_id]].func_step = steps;
306 g_uart_static[uart_id][g_uart_static_cnt[uart_id]].fr = UART_GET_FR(&zx29_uart_ports[uart_id].port);
307 g_uart_static[uart_id][g_uart_static_cnt[uart_id]].ris = UART_GET_RIS(&zx29_uart_ports[uart_id].port);
xf.li2f424182024-08-20 00:47:34 -0700308
xf.li1867bfa2024-08-20 02:32:16 -0700309 if(++g_uart_static_cnt[uart_id] >= UART_STATIC_COUNT)
310 g_uart_static_cnt[uart_id] = 0;
xf.li2f424182024-08-20 00:47:34 -0700311}
312
313
314
315
316#define zx29_MAXPORTS ARRAY_SIZE(zx29_uart_ports)
317void zx29_uart_stop_rx(struct uart_port *port);
318
319#if CONFIG_SERIAL_ZX29_DMA
320static inline bool zx29_dma_tx_start(struct zx29_uart_port *zup);
321static inline void zx29_dma_tx_stop(struct zx29_uart_port *zup);
322static bool zx29_dma_tx_irq(struct zx29_uart_port *zup);
323static int zx29_uart_dma_tx_chars(struct zx29_uart_port *zup);
324void uart_dma_rx_callback(void *data);
325void uart_dma_rx_callback_use_dma_cyclic(void * data);
326static void zx29_uart_dma_rx_chars(struct zx29_uart_port *zup,
327 //u32 pending, bool use_buf_b,
328 u32 pending, struct zx29_sgbuf *sgbuf,
329 bool readfifo, unsigned long *flags);
330static inline void zx29_dma_rx_stop(struct zx29_uart_port *zup);
331static inline bool zx29_dma_rx_available(struct zx29_uart_port *zup);
332static inline bool zx29_dma_rx_running(struct zx29_uart_port *zup);
333static int zx29_dma_rx_trigger_dma(struct zx29_uart_port *zup);
334static int zx29_dma_rx_trigger_dma_use_dma_cyclic(struct zx29_uart_port *zup);
335
336static void zx29_uart_rx_dma_chars(struct zx29_uart_port *zup, unsigned long *flags);
337dma_peripheral_id uart_get_rx_dma_peripheral_id(struct zx29_uart_port *zup);
338
339#if RX_DMA_WORK
340static void zx29_uart_rx_timeout_chars(struct zx29_uart_port *zup, unsigned long *flags);
341static inline bool zx29_dma_rx_work_scheduled(struct zx29_uart_port *zup);
342
343static void zx29_uart_rt_dma(struct zx29_uart_port *zup, unsigned long *flags);
344static void uart_dma_cycle_deinit(struct zx29_uart_port *zup);
345#endif
346#endif
347
348
349
350/*******************************************************************************
351* Function: uart_wakeup_callback.
352* Description: uart_wakeup_callback.
353* Parameters:
354* Input:val:means wakeup or sleep notify to other device
355*
356* Output:v:means devices been called return result
357*
358* Returns:
359*
360* Others:
361********************************************************************************/
362int uart_wakeup_callback(struct notifier_block * nb, unsigned long val, void * v)
363{
364 int *call_result = (int *)v;
365 unsigned long flags = 0;
366 struct zx29_uart_port *zup = container_of(nb, struct zx29_uart_port, wakeup_notifier);
367
368 if(!zup || zup->port_close){
369 *call_result |= 0;
370 return 0;
371 }
372 struct platform_device *pdev = zup->port.private_data;
373 raw_spin_lock_irqsave(&zup->port.lock, flags);
374 if(val == 1){//wakeup
375 zup->sleep_state = 0;
376 pm_stay_awake(&pdev->dev);
377 zx29_uart_rx_dma_chars(zup, &flags);
378
379 }else{//sleep
380 zup->sleep_state = 1;
381 zx29_uart_stop_rx(&zup->port);
382 pm_relax(&pdev->dev);
383
384 }
385 *call_result |= 0;
386 raw_spin_unlock_irqrestore(&zup->port.lock, flags);
387 return 0;
388}
389
390int zx29_get_sleep_state(int uart_index)
391{
392 if(uart_index < 0 || uart_index > 2){
393 printk("invalid uart index\n");
394 return -1;
395 }
396
397 return zx29_uart_ports[uart_index].sleep_state;
398}
399EXPORT_SYMBOL_GPL(zx29_get_sleep_state);
400
401void zx29_set_sleep_state(int state, int uart_index)
402{
403 if(uart_index < 0 || uart_index > 2){
404 printk("invalid uart index\n");
405 return ;
406 }
407 printk(" uart %d, state change to:%d\n", uart_index, state);
408 zx29_uart_ports[uart_index].sleep_state = (state ? 1: 0);
409}
410EXPORT_SYMBOL_GPL(zx29_set_sleep_state);
411
412static ssize_t sleep_state_show(struct device *_dev,
413 struct device_attribute *attr, char *buf)
414{
415 struct platform_device *pdev = container_of(_dev, struct platform_device, dev);
416 //struct zx29_uart_platdata *pdata = pdev->dev.platform_data;
417
418 return sprintf(buf, "\n wakeup_enable = %d\n",zx29_uart_ports[pdev->id].sleep_state);
419}
420
421static ssize_t sleep_state_store(struct device *_dev,
422 struct device_attribute *attr,
423 const char *buf, size_t count)
424{
425 uint32_t flag = 0;
426 struct platform_device *pdev = container_of(_dev, struct platform_device, dev);
427 struct zx29_uart_platdata *pdata = pdev->dev.platform_data;
428 flag = simple_strtoul(buf, NULL, 16);
429 //pdata->uart_wakeup_enable = flag;
430 zx29_uart_ports[pdev->id].sleep_state = (flag ? 1: 0);
431 return count;
432}
433
434DEVICE_ATTR(sleep_state, S_IRUGO | S_IWUSR, sleep_state_show,
435 sleep_state_store);
436//bool uart_dma_filter_fn (struct dma_chan *chan, void *param)
437//{
438// dma_peripheral_id peri_id = (dma_peripheral_id) param;
439// if (chan->chan_id == (unsigned int)peri_id){
440// printk("uart_dma_filter_fn, peri_id:%d, ok\n", peri_id);
441// return true;
442// }
443// chan->private = param;
444//
445// return false;
446//}
447static void zx29_uart_console_putc(struct uart_port *port, int c);
448void zx29_uart_putc(struct uart_port *port, int c);
449
450#if CONFIG_SERIAL_ZX29_DMA
451void uart_mod_timer(struct zx29_uart_port *zup, unsigned long *flags)
452{
453 unsigned long t_delay = 0;
454 t_delay = msecs_to_jiffies(RX_DMA_TIMEOUT);
455 spin_unlock_irqrestore(&zup->port.lock, *flags);
456 //printk("uart_mod_timer, delay %d jiffies\n", t_delay);
457 mod_timer(&(zup->rx_dma_timer), jiffies + t_delay);
458
459 spin_lock_irqsave(&zup->port.lock, *flags);
460}
461#endif
462/**
463* Show the console_input attribute.
464*/
465static ssize_t console_input_show(struct device *_dev,
466 struct device_attribute *attr, char *buf)
467{
468 return sprintf(buf, "\n console_input = %d\n",g_console_open_flag);
469}
470
471/**
472 * Store the console_input attribure.
473 * 0: disable console input function,only out put log
474 * 1: able console input, can input commands
475 */
476static ssize_t console_input_store(struct device *_dev,
477 struct device_attribute *attr,
478 const char *buf, size_t count)
479{
480 uint32_t flag = 0;
481 flag = simple_strtoul(buf, NULL, 16);
482 g_console_open_flag = flag;
483
484 return count;
485}
486
487DEVICE_ATTR(console_input, S_IRUGO | S_IWUSR, console_input_show,
488 console_input_store);
489
490static ssize_t ctsrts_input_show(struct device *_dev,
491 struct device_attribute *attr, char *buf)
492{
493 struct platform_device *pdev = container_of(_dev, struct platform_device, dev);
494// struct zx29_uart_platdata *pdata = pdev->dev.platform_data;
495 if(pdev->id < 0 || pdev->id >= UART_NUM){
496 printk("ctsrts_input_store, invalid uart id, return error\n");
497 return 0;
498 }
499// return sprintf(buf, "\n ctsrts_input = %d\n",pdata->uart_ctsrtsuse);
500return sprintf(buf, "\n uart %d ctsrts_input = %d\n", pdev->id, zx29_uart_ports[pdev->id].enable_ctsrts);
501
502}
503
504static ssize_t ctsrts_input_store(struct device *_dev,
505 struct device_attribute *attr,
506 const char *buf, size_t count)
507{
508 uint32_t flag = 0;
509 struct platform_device *pdev = container_of(_dev, struct platform_device, dev);
510
511 if(pdev->id != 0){
512 printk("ctsrts_input_store, invalid uart id, only uart support hardware control\n");
513 }
514 flag = simple_strtoul(buf, NULL, 16);
515 zx29_uart_ports[pdev->id].enable_ctsrts = flag;
516
517 return count;
518}
519
520DEVICE_ATTR(ctsrts_input, S_IRUGO | S_IWUSR, ctsrts_input_show,
521 ctsrts_input_store);
522
523static ssize_t wakeup_enable_show(struct device *_dev,
524 struct device_attribute *attr, char *buf)
525{
526 struct platform_device *pdev = container_of(_dev, struct platform_device, dev);
527 //struct zx29_uart_platdata *pdata = pdev->dev.platform_data;
528
529 return sprintf(buf, "\n wakeup_enable = %d\n",1);
530}
531
532static ssize_t wakeup_enable_store(struct device *_dev,
533 struct device_attribute *attr,
534 const char *buf, size_t count)
535{
536 uint32_t flag = 0;
537 struct platform_device *pdev = container_of(_dev, struct platform_device, dev);
538 //struct zx29_uart_platdata *pdata = pdev->dev.platform_data;
539 if(pdev->id != 4){
540 printk("\nctsrts_input_store, invalid uart id, only lp_uart(uart 4) support wakeup\n");
541 }
542 flag = simple_strtoul(buf, NULL, 16);
543 zx29_uart_ports[pdev->id].enable_wakeup = flag;
544
545 return count;
546}
547
548DEVICE_ATTR(wakeup_enable, S_IRUGO | S_IWUSR, wakeup_enable_show,
549 wakeup_enable_store);
550
551static ssize_t app_ctrl_show(struct device *_dev,
552 struct device_attribute *attr, char *buf)
553{
554 struct platform_device *pdev = container_of(_dev, struct platform_device, dev);
555 //struct zx29_uart_platdata *pdata = pdev->dev.platform_data;
556
557 return sprintf(buf, "%d\n",zx29_uart_ports[pdev->id].app_ctrl);
558}
559
560static ssize_t app_ctrl_store(struct device *_dev,
561 struct device_attribute *attr,
562 const char *buf, size_t count)
563{
564 uint32_t flag = 0;
565 struct platform_device *pdev = container_of(_dev, struct platform_device, dev);
566 //struct zx29_uart_platdata *pdata = pdev->dev.platform_data;
567 flag = simple_strtoul(buf, NULL, 16);
568 // pdata->uart_wakeup_enable = flag;
569 zx29_uart_ports[pdev->id].app_ctrl = (flag == 0) ? 0 : 1;
570
571 return count;
572}
573DEVICE_ATTR(app_ctrl, S_IRUGO | S_IWUSR, app_ctrl_show,
574 app_ctrl_store);
575
576int rxd_wake_cnt = 0;
577static ssize_t statics_show(struct device *_dev,
578 struct device_attribute *attr, char *buf)
579{
580 struct platform_device *pdev = container_of(_dev, struct platform_device, dev);
581 //struct zx29_uart_platdata *pdata = pdev->dev.platform_data;
582
583 return sprintf(buf, "\n RX:%u,TX:%u,OE:%u,brk:%u,FE:%u,PE:%u ,rxd_wake_cnt:%d\n",
584 zx29_uart_ports[pdev->id].port.icount.rx,
585 zx29_uart_ports[pdev->id].port.icount.tx,
586 zx29_uart_ports[pdev->id].port.icount.overrun,
587 zx29_uart_ports[pdev->id].port.icount.brk,
588 zx29_uart_ports[pdev->id].port.icount.frame,
589 zx29_uart_ports[pdev->id].port.icount.parity,
590 rxd_wake_cnt
591 );
592}
593DEVICE_ATTR(statics, S_IRUGO, statics_show, NULL);
xf.li6b423c72025-03-14 00:07:42 -0700594
595static unsigned int uart_io_seletc = 0;
596
597
598static ssize_t uart_io_select_show(struct device *_dev,
599struct device_attribute *attr, char *buf)
600{
601struct platform_device *pdev = container_of(_dev, struct platform_device, dev);
602//struct zx29_uart_platdata *pdata = pdev->dev.platform_data;
603
604return sprintf(buf, "%d\n",uart_io_seletc );
605
606}
607
608static ssize_t uart_io_select_store(struct device *_dev,
609struct device_attribute *attr,
610const char *buf, size_t count)
611{
612 uint32_t flag = 0;
613struct platform_device *pdev = container_of(_dev, struct platform_device, dev);
614flag = simple_strtoul(buf, NULL, 16);
615
616if(flag == 1){
617 printk("uart io is 1\n");
618pinctrl_pm_select_default_state(_dev);
619}else if(flag == 0){
620pinctrl_pm_select_sleep_state(_dev);
621}
622else{
623printk("uart io select flag invaild\n");
624}
625
626uart_io_seletc = flag;
627
628
629
630return count;
631}
632
633DEVICE_ATTR(uart_io_select, S_IRUGO | S_IWUSR, uart_io_select_show,
634 uart_io_select_store);
635
xf.li2f424182024-08-20 00:47:34 -0700636#define VEHICLE_USE_ONE_UART_LOG 1
637#if VEHICLE_USE_ONE_UART_LOG
638#define ICP_CORE_ID_PS CORE_PS0
639#define ICP_CORE_ID_CAP 1
640#define ICP_CHANNEL_CONSOLE_UART 7
641#define ICP_MSG_LEN_CONSOLE_UART 2
642#define ICP_BUFFERSIZE_CONSOLE_TOGGLE 16
643#define SYMB_PS_CORE_ID ICP_CORE_ID_PS
644#define SYMB_CAP_CORE_ID ICP_CORE_ID_CAP
645#define SYMB_WHAT_CORE_ID 3
646#define ENABLE_CURRENT_CONSOLE_UART 1
647#define DISABLE_CURRENT_CONSOLE_UART 0
648#define ENABLE_TOGGLE 1
649#define DISABLE_TOGGLE 0
650unsigned char g_core_id_occupy_uart = 0;
651unsigned char g_cap_uart_toggle = 0;
652static irqreturn_t zx29_uart_interrupt(int irq, void *dev_id);
653static void restart_current_cons_uart(void)
654{
655 struct zx29_uart_port *zup = &zx29_uart_ports[DEBUG_CONSOLE];
656 struct uart_port *port = &zup->port;
657 enable_irq(port->irq);
658 g_core_id_occupy_uart = SYMB_CAP_CORE_ID;
659 spin_lock(&zup->port.lock);
660 tasklet_schedule(&zup->write_wakeup);
661 spin_unlock(&zup->port.lock);
662}
663static void forbid_current_cons_uart(void)
664{
665 struct zx29_uart_port *zup = &zx29_uart_ports[DEBUG_CONSOLE];
666 struct uart_port *port = &zup->port;
667 disable_irq(port->irq);
668 g_core_id_occupy_uart = SYMB_PS_CORE_ID;
669}
670static void process_ps2cap_rpmsg(char *arr)
671{
672 if((arr[0] == SYMB_CAP_CORE_ID) && (arr[1] == ENABLE_CURRENT_CONSOLE_UART)){
673 restart_current_cons_uart();
674 }else if((arr[0] == SYMB_CAP_CORE_ID) && (arr[1] == DISABLE_CURRENT_CONSOLE_UART)){
675 printk("current console uart not enable.\n");
676 g_core_id_occupy_uart = SYMB_CAP_CORE_ID;
677 }else if((arr[0] == SYMB_WHAT_CORE_ID) && (arr[1] == SYMB_PS_CORE_ID)){
678 g_core_id_occupy_uart = SYMB_PS_CORE_ID;
679 forbid_current_cons_uart();
680 }else if((arr[0] == SYMB_WHAT_CORE_ID) && (arr[1] == SYMB_CAP_CORE_ID)){
681 g_core_id_occupy_uart = SYMB_CAP_CORE_ID;
682 }
683 else{
684 printk("%s error!!\n",__func__);
685 }
686}
687static void icp_callback_ps2cap(void *buf, unsigned int len)
688{
689 char *arr_ps2cap;
690 if (len==0){
691 printk("%s empty.\n", __func__);
692 return ;
693 }
694 arr_ps2cap = (char *)buf;
695 process_ps2cap_rpmsg(arr_ps2cap);
696}
697static void echo_to_change_other_uart(uint32_t val)
698{
699 int ret;
700 if(val > ENABLE_TOGGLE)
701 {
702 printk("echo para error!!!\n");
703 return;
704 }
705 char arr[2] = {0};
706 arr[0] = SYMB_PS_CORE_ID;
707 arr[1] = val;
708 T_RpMsg_Msg icp_msg;
709 icp_msg.coreID = CORE_PS0;
710 icp_msg.chID = ICP_CHANNEL_CONSOLE_UART;
711 icp_msg.flag = RPMSG_WRITE_INT; /* 1- means send an icp interrupt> */
712 icp_msg.buf = arr;
713 icp_msg.len = ICP_MSG_LEN_CONSOLE_UART;
714 ret = rpmsgWrite(&icp_msg);
715 if(ret == 0){
716 if(val == ENABLE_TOGGLE)
717 g_core_id_occupy_uart = SYMB_PS_CORE_ID;
718 else if(val == DISABLE_TOGGLE)
719 g_core_id_occupy_uart = SYMB_CAP_CORE_ID;
720 }else
721 printk("echo_to_change_ohter_uart fail.\n");
722}
723static ssize_t console_uart_toggle_show(struct device *_dev,
724 struct device_attribute *attr, char *buf)
725{
726 return sprintf(buf, "\n console_uart_toggle_show %d. \n", g_cap_uart_toggle);
727}
728static ssize_t console_uart_toggle_store(struct device *_dev,
729 struct device_attribute *attr,
730 const char *buf, size_t count)
731{
732 uint32_t flag = 0;
733 flag = simple_strtoul(buf, NULL, 16);
734 if(flag == ENABLE_TOGGLE){
735 g_cap_uart_toggle = 1;
736 forbid_current_cons_uart();
737 echo_to_change_other_uart(flag);
738 }else if(flag == DISABLE_TOGGLE){
739 g_cap_uart_toggle = 0;
740 g_core_id_occupy_uart = SYMB_CAP_CORE_ID;
741 }
742 return count;
743}
744DEVICE_ATTR(console_uart_toggle, S_IRUGO | S_IWUSR, console_uart_toggle_show,
745 console_uart_toggle_store);
746static void notify_occupy_uart_coreid_to_other(void)
747{
748 char arr[2] = {0};
749 arr[0] = SYMB_WHAT_CORE_ID;
750 arr[1] = g_core_id_occupy_uart;
751 T_RpMsg_Msg icp_msg;
752 icp_msg.coreID = CORE_AP;
753 icp_msg.chID = ICP_CHANNEL_CONSOLE_UART;
754 icp_msg.flag = RPMSG_WRITE_INT; /* 1- means send an icp interrupt> */
755 icp_msg.buf = arr;
756 icp_msg.len = ICP_MSG_LEN_CONSOLE_UART;
757 rpmsgWrite(&icp_msg);
758}
759static ssize_t coreid_occupy_uart_show(struct device *_dev,
760 struct device_attribute *attr, char *buf)
761{
762 return sprintf(buf, "\n core %d occupy cons uart now! \n",g_core_id_occupy_uart);
763}
764static ssize_t coreid_occupy_uart_store(struct device *_dev,
765 struct device_attribute *attr,
766 const char *buf, size_t count)
767{
768 uint32_t flag = 0;
769 flag = simple_strtoul(buf, NULL, 16);
770 g_core_id_occupy_uart = flag;
771 if(flag == SYMB_CAP_CORE_ID){
772 g_cap_uart_toggle = 0;
773 }else if(SYMB_PS_CORE_ID){
774 g_cap_uart_toggle = 1;
775 }
776 return count;
777}
778DEVICE_ATTR(coreid_occupy_uart, S_IRUGO | S_IWUSR, coreid_occupy_uart_show,
779 coreid_occupy_uart_store);
780#endif
781
782//extern int (*pm_callback_fn)(void);
783#ifdef CONFIG_CPU_IDLE
784typedef int (*pm_callback_fn)(void);
785extern int zx_pm_register_callback(pm_callback_fn enter_cb, pm_callback_fn exit_cb);
786
787extern void disable_irq_nosync(unsigned int irq);
788extern void enable_irq(unsigned int irq);
789
790void uart_rxd_int_disable(struct uart_port *port)
791{
792 struct zx29_uart_port *zup = container_of(port, struct zx29_uart_port, port);
793 zup->rxd_int_depth++;
794}
795EXPORT_SYMBOL(uart_rxd_int_disable);
796
797int uart_0_pm_enter(void)
798{
799 struct zx29_uart_port *zup = &zx29_uart_ports[0];
800
801 //zDrvInt_UnmaskIrq(UART0_RXD_INT);
802 if(zup->irq_state == 0 || zup->imr== 0)
803 return 0;
804
805 //pcu_int_clear(PCU_UART0_RXD_INT);
806 if(!zup->rxd_int_depth){
807 //enable_irq(UART0_RXD_INT);
808 zup->rxd_int_depth++;
809 }
810 return 0;
811}
812
813int uart_0_pm_exit(void)
814{
815
816 return 0;
817}
818#endif
819/****************************************************************************/
820
821static int zx29_sgbuf_init(struct dma_chan *chan, struct zx29_sgbuf *sg,
822 enum dma_data_direction dir)
823{
824 dma_addr_t dma_addr;
825
826 sg->buf = dma_alloc_coherent(chan->device->dev,
827 ZX29_DMA_BUFFER_SIZE, &dma_addr, GFP_KERNEL);
828 if (!sg->buf){
829 printk("zx29_sgbuf_init fail, no mem\n");
830 return -ENOMEM;
831 }
832 sg_init_table(&sg->sg, 1);
833 sg_set_page(&sg->sg, phys_to_page(dma_addr),
834 ZX29_DMA_BUFFER_SIZE, offset_in_page(dma_addr));
835 sg_dma_address(&sg->sg) = dma_addr;
836 sg_dma_len(&sg->sg) = ZX29_DMA_BUFFER_SIZE;
837 sg->dma_addr = dma_addr;
838 return 0;
839}
840
841static void zx29_sgbuf_free(struct dma_chan *chan, struct zx29_sgbuf *sg,
842 enum dma_data_direction dir)
843{
844 if (sg->buf) {
845 dma_free_coherent(chan->device->dev,
846 ZX29_DMA_BUFFER_SIZE, sg->buf,
847 sg_dma_address(&sg->sg));
848 sg->dma_addr = NULL;
849 }
850}
851
852
853/****************************************************************************/
854static unsigned int zx29_uart_tx_empty(struct uart_port *port)
855{
856 return (UART_GET_FR(port)&(UART_FR_TXBUSY|UART_FR_TXFF)) ? 0 : TIOCSER_TEMT;
857}
858
859/****************************************************************************/
860static void zx29_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
861{
862 unsigned int control = 0;
863 struct zx29_uart_port *zup = container_of(port, struct zx29_uart_port, port);
864 zup->sigs = mctrl;
865 control = UART_GET_CR(&zup->port);
866 if(mctrl & TIOCM_DTR)
867 control |= UART_CR_DTR;
868 else
869 control &= ~ UART_CR_DTR;
870
871 if(mctrl & TIOCM_RTS)
872 control |= UART_CR_RTS;
873 else
874 control &= ~UART_CR_RTS;
875
876 if(mctrl & TIOCM_LOOP)
877 control |= UART_CR_LBE;
878 else
879 control &= ~UART_CR_LBE;
880
881 /* We need to disable auto-RTS if we want to turn RTS off */
882 if (zup->autorts) {
883 if (mctrl & TIOCM_RTS)
884 control |= UART_CR_RTSEN;
885 else
886 control &= ~UART_CR_RTSEN;
887 }
888 UART_PUT_CR(port, control);
889}
890
891/****************************************************************************/
892static unsigned int zx29_uart_get_mctrl(struct uart_port *port)
893{
894 struct zx29_uart_port *zup = container_of(port, struct zx29_uart_port, port);
895 unsigned int mctrl = 0;
896 unsigned int uart_flag = 0;
897
898 uart_flag = UART_GET_FR(port);
899
900 mctrl = (uart_flag&UART_FR_CTS) ?TIOCM_CTS : 0;
901 mctrl |= (zup->sigs & TIOCM_RTS);
902 mctrl |= (uart_flag&UART_FR_DCD) ? TIOCM_CD : 0;
903 mctrl |= (uart_flag&UART_FR_DSR) ? TIOCM_DSR : 0;
904 mctrl |= (uart_flag&UART_FR_RI) ? TIOCM_RI : 0;
905
906 return mctrl;
907}
908
909/****************************************************************************/
910static void zx29_uart_start_tx(struct uart_port *port)
911{
912 struct zx29_uart_port *zup = container_of(port, struct zx29_uart_port, port);
913 unsigned int control = 0;
914 unsigned int reg_bak[10] = {0};
915 struct circ_buf *xmit = &zup->port.state->xmit;
916 int count = 0;
917#if VEHICLE_USE_ONE_UART_LOG
918 if((port->line == DEBUG_CONSOLE))
919 {
920 if(g_core_id_occupy_uart == SYMB_PS_CORE_ID){
921 #if 1
922 count = uart_circ_chars_pending(xmit);
923 while(count-- > 0)
924 {
925 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
926 if (uart_circ_empty(xmit))
927 break;
928 }
929 #endif
930 return;
931 }
932 count = uart_circ_chars_pending(xmit);
933 while(count-- > 0)
934 {
935 zx29_uart_console_putc(&zup->port, xmit->buf[xmit->tail]);
936 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
937 zup->port.icount.tx++;
938 if (uart_circ_empty(xmit)){
939 break;
940 }
941 }
942
943 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
944 {
945 spin_lock(&zup->port.lock);
946 tasklet_schedule(&zup->write_wakeup);
947 spin_unlock(&zup->port.lock);
948 return;
949 }
950 return;
951 }
952else
953#endif
954{
955 if(!(UART_GET_RIS(port)&UART_TXIS) && (UART_GET_FR(port) & UART_FR_TXFE))
956 {
957 if(!(UART_GET_RIS(port)&UART_TXIS))
958 {
959 count = uart_circ_chars_pending(xmit);
960 if(count >= zup->port.fifosize)
961 count = 15;//sent data more than TX ifls, TXIS will coming soon
962 if(count != 0){
963 do {
964 zx29_uart_putc(&zup->port, xmit->buf[xmit->tail]);
965 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
966 zup->port.icount.tx++;
967 if (uart_circ_empty(xmit) || (UART_GET_RIS(port)&UART_TXIS))
968 break;
969 } while (--count > 0);
970 }
971 }
972
973 }
974 }
975#if CONFIG_SERIAL_ZX29_DMA
976 if(!uart_console(port))
977 {
978 if (!zx29_dma_tx_start(zup))
979 {
980 zup->imr |= UART_TXIM;
981 UART_PUT_IMSC(port, zup->imr);
982 if(!(UART_GET_RIS(port)&UART_TXIS)){
983 if((UART_GET_FR(port) & UART_FR_TXFF))
984 return;
985 count = uart_circ_chars_pending(xmit);
986 while (count > 0) {
987 UART_PUT_CHAR(&zup->port, xmit->buf[xmit->tail]);
988 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
989 zup->port.icount.tx++;
990 if (uart_circ_empty(xmit) || (UART_GET_RIS(port)&UART_TXIS) ||
991 (UART_GET_FR(port) & UART_FR_TXFF))
992 break;
993 }
994 }
995 }
996 }
997 else
998 {
999 zup->imr |= UART_TXIM;
1000 UART_PUT_IMSC(port, zup->imr);
1001 }
1002#else
1003 zup->imr |= UART_TXIM;
1004 UART_PUT_IMSC(port, zup->imr);
1005#endif
1006}
1007
1008static void uart_write_wakeup_task(unsigned long _port)
1009{
1010 struct uart_port *port = (void *)_port;
1011struct zx29_uart_port *zup = container_of(port, struct zx29_uart_port, port);
1012 struct platform_device *pdev=port->private_data;
1013 //printk("wakeup_task,port:%d, rxd_wakeup:%d\n", port->line, zup->rxd_wakeup);
1014
1015 if(zup->rxd_wakeup){
1016 //rxd wake
1017 printk("wakeup_task,port:%d, rxd_wakeup:%d\n", port->line, zup->rxd_wakeup);
1018 pm_wakeup_dev_event(&pdev->dev, 5000, false);
1019 disable_irq(zup->rxd_irq);
1020 zup->rxd_wakeup = false;
1021 } else {
1022 uart_write_wakeup(port);
1023 }
1024
1025}
1026
1027#if CONFIG_SERIAL_ZX29_DMA
xf.li1867bfa2024-08-20 02:32:16 -07001028#if UART_DEBUG_RECORDER_BYTE
1029#define UART_DRIVER_DEBUG_COUNT (4*1024*1024)
1030u32 cnt_uart_driver_debug = 0;
1031u8 uart_driver_debug[UART_DRIVER_DEBUG_COUNT]={};
1032void uart_debug(char *debug_buf, u32 count){
1033 if(cnt_uart_driver_debug > (UART_DRIVER_DEBUG_COUNT-1)){
1034 cnt_uart_driver_debug = 0;
1035 }else{
1036 memcpy(uart_driver_debug+cnt_uart_driver_debug,debug_buf,count);
1037 cnt_uart_driver_debug = cnt_uart_driver_debug+count;
1038 }
1039}
1040#endif
xf.li2f424182024-08-20 00:47:34 -07001041int dma_complete_thread_use_dma_cyclic(void *ptr)
1042{
1043 unsigned long flags;
1044 struct zx29_uart_port *zup = (struct zx29_uart_port *)ptr;
1045 size_t pending;
1046 int dma_count = 0;
1047 struct device *dev = NULL;
1048 dev = zup->dmarx.chan->device->dev;
1049 int uart_id = zup->port.line;
1050 while(down_interruptible(&zup->sema_cyclic) == 0)
1051 {
xf.li1867bfa2024-08-20 02:32:16 -07001052 if(uart_dma_cycle[zup->port.line].cnt_callback > 0)
1053 uart_id = zup->port.line;
1054 else if(uart_dma_cycle[zup->port.line+3].cnt_callback > 0)
1055 uart_id = zup->port.line + 3;
1056
xf.li2f424182024-08-20 00:47:34 -07001057 if(zup->port_close || !uart_dma_cycle[uart_id].sgbuf[uart_dma_cycle[uart_id].flg_enter_th].dma_addr)
1058 break;
xf.li1867bfa2024-08-20 02:32:16 -07001059
xf.li2f424182024-08-20 00:47:34 -07001060 spin_lock_irqsave(&zup->port.lock, flags);
1061 uart_dma_cycle[uart_id].cnt_th_total++;
1062 uart_dma_cycle[uart_id].cnt_th++;
1063 zup->sg2tty = &uart_dma_cycle[uart_id].sgbuf[uart_dma_cycle[uart_id].flg_enter_th];
1064 zup->sg2tty_len = 4096;
1065 pending = zup->sg2tty_len;
1066 if(uart_dma_cycle[uart_id].flg_be || uart_dma_cycle[uart_id].flg_fe|| uart_dma_cycle[uart_id].flg_pe){
1067 printk("error in uart%d: fe %u ,be %u pe %u.\n",zup->port.line,zup->port.icount.frame,
1068 zup->port.icount.brk,zup->port.icount.parity);
1069 uart_dma_cycle[uart_id].flg_be = 0;
1070 uart_dma_cycle[uart_id].flg_fe = 0;
1071 uart_dma_cycle[uart_id].flg_pe = 0;
1072 }
1073 dma_sync_sg_for_cpu(dev, &zup->sg2tty->sg, 1, DMA_FROM_DEVICE);
1074 spin_unlock_irqrestore(&zup->port.lock, flags);
1075 dma_count = tty_insert_flip_string(&zup->port.state->port,
1076 zup->sg2tty->buf, pending);
xf.li1867bfa2024-08-20 02:32:16 -07001077 test_uart_static(zup->port.line, zup->sg2tty->buf, uart_dma_cycle[zup->port.line].used, 27);
1078#if UART_DEBUG_RECORDER_BYTE
1079 uart_debug(zup->sg2tty->buf, pending);
1080#endif
xf.li2f424182024-08-20 00:47:34 -07001081 tty_flip_buffer_push(&zup->port.state->port);
1082 spin_lock_irqsave(&zup->port.lock, flags);
1083 dma_sync_sg_for_device(dev, &zup->sg2tty->sg, 1, DMA_FROM_DEVICE);
1084 zup->sg2tty = NULL;
1085 zup->sg2tty_len = 0;
1086 zup->port.icount.rx += dma_count;
1087 if (dma_count < pending)
1088 dev_info(zup->port.dev,
1089 "couldn't insert all characters (TTY is full?)\n");
1090 uart_dma_cycle[uart_id].flg_enter_th = (uart_dma_cycle[uart_id].flg_enter_th+1)%UART_DMA_CYCLE_RX_CONFIG_COUNT;
1091 uart_dma_cycle[uart_id].cnt_callback--;
1092 if(!hrtimer_active(&zup->rx_dma_hrtimer))
1093 hrtimer_restart(&zup->rx_dma_hrtimer);
1094 spin_unlock_irqrestore(&zup->port.lock, flags);
1095 }
1096 return 0;
1097}
1098int dma_complete_thread(void *ptr)
1099{
1100 unsigned long flags;
1101 struct zx29_uart_port *zup = (struct zx29_uart_port *)ptr;
1102
1103 size_t pending;
1104 struct dma_tx_state state;
1105 struct zx29_dmarx_data *dmarx = &zup->dmarx;
1106 struct dma_chan *rxchan = dmarx->chan;
1107 bool lastbuf;
1108 int dma_count = 0;
1109 struct zx29_sgbuf *sgbuf = NULL;
1110 struct device *dev = NULL;
1111 dma_peripheral_id rx_id = uart_get_rx_dma_peripheral_id(zup);
1112 dev = zup->dmarx.chan->device->dev;
1113
1114 while(down_interruptible(&zup->sema) == 0)
1115 {
1116 if(zup->port_close)
1117 break;
1118 spin_lock_irqsave(&zup->port.lock, flags);
1119 // tty = zup->port.state->port.tty;
1120 if(!zup->sg2tty)
1121 panic("dma_complete_thread, buffer 2 tty is invalid\n");
1122 // dev = zup->dmarx.chan->device->dev;
1123 pending = zup->sg2tty_len;
1124 if(zx29_dma_rx_running(zup)){
1125
1126 test_uart_static(zup->port.line, NULL, 0, 10);
1127 //uart_mod_timer(zup, &flags);
1128 if(!hrtimer_active(&zup->rx_dma_hrtimer))
1129 hrtimer_forward_now(&zup->rx_dma_hrtimer, g_hr_interval);
1130 }
1131 /* Pick everything from the DMA first */
1132 if (pending) {
1133 /* Sync in buffer */
1134 dma_sync_sg_for_cpu(dev, &zup->sg2tty->sg, 1, DMA_FROM_DEVICE);
1135 //BUG();
1136
1137 spin_unlock_irqrestore(&zup->port.lock, flags);
1138 dma_count = tty_insert_flip_string(&zup->port.state->port,
1139 zup->sg2tty->buf, pending);
1140 test_uart_static(zup->port.line, zup->sg2tty->buf, pending, 11);
1141 tty_flip_buffer_push(&zup->port.state->port);
1142
1143 spin_lock_irqsave(&zup->port.lock, flags);
1144 /* Return buffer to device */
1145 dma_sync_sg_for_device(dev, &zup->sg2tty->sg, 1, DMA_FROM_DEVICE);
1146
1147 zup->sg2tty = NULL;
1148 zup->sg2tty_len = 0;
1149 zup->port.icount.rx += dma_count;
1150
1151 //if(zup->port.line == 0)
1152 //printk("yanming dma_complete_thread, dma2tty:%d\n", dma_count);
1153 if (dma_count < pending){
1154 sc_debug_info_record(MODULE_ID_CAP_UART, "uart%d couldn't insert all characters \n",zup->port.line);
1155 dev_info(zup->port.dev,
1156 "couldn't insert all characters (TTY is full?)\n");
1157 }
1158
1159
1160 }
1161#if 0
1162 zup->work_state = false;
1163 zup->pre_pending = 0;
1164 zup->imr |= UART_RXIM;
1165 UART_PUT_IMSC(&zup->port, zup->imr);
1166#endif
1167 spin_unlock_irqrestore(&zup->port.lock, flags);
1168 }
1169
1170 return 0;
1171}
1172#endif
1173
1174/****************************************************************************/
1175static void zx29_uart_stop_tx(struct uart_port *port)
1176{
1177 struct zx29_uart_port *zup = container_of(port, struct zx29_uart_port, port);
1178 zup->imr &= ~UART_TXIM;
1179 UART_PUT_IMSC(port, zup->imr);
1180#ifdef CONFIG_SERIAL_ZX29_UART_CONSOLE
1181 if((port->line == DEBUG_CONSOLE) && uart_tx_stopped(port))
1182 {
1183 //uart_write_wakeup(port);
1184 tasklet_schedule(&zup->write_wakeup);
1185 }
1186#endif
1187
1188#if CONFIG_SERIAL_ZX29_DMA
1189 zx29_dma_tx_stop(zup);
1190#endif
1191
1192 zx_cpuidle_set_free(IDLE_FLAG_UART);
1193
1194}
1195
1196/****************************************************************************/
1197void zx29_uart_stop_rx(struct uart_port *port)
1198{
1199 struct zx29_uart_port *zup = container_of(port, struct zx29_uart_port, port);
1200
1201 zup->imr &= ~(UART_RXIM|UART_RTIM|UART_FEIM|UART_PEIM|UART_BEIM|UART_OEIM);
1202 UART_PUT_IMSC(port, zup->imr);
1203#if CONFIG_SERIAL_ZX29_DMA
1204 zx29_dma_rx_stop(zup);
1205#endif
1206}
1207
1208/****************************************************************************/
1209static void zx29_uart_break_ctl(struct uart_port *port, int break_state)
1210{
1211 struct zx29_uart_port *zup = container_of(port, struct zx29_uart_port, port);
1212 unsigned long flags;
1213 unsigned int lcr_h;
1214 spin_lock_irqsave(&zup->port.lock, flags);
1215 lcr_h = UART_GET_LCRH(port);
1216 if (break_state == -1)
1217 lcr_h |= UART_LCRH_BRK;
1218 else
1219 lcr_h &= ~UART_LCRH_BRK;
1220 UART_PUT_LCRH(port, lcr_h);
1221 spin_unlock_irqrestore(&zup->port.lock, flags);
1222}
1223
1224/****************************************************************************/
1225static void zx29_uart_enable_ms(struct uart_port *port)
1226{
1227 struct zx29_uart_port *zup = container_of(port, struct zx29_uart_port, port);
1228 zup->imr |= UART_RIMIM|UART_CTSMIM|UART_DCDMIM|UART_DSRMIM;
1229 UART_PUT_IMSC(port, zup->imr);
1230}
1231
1232/****************************************************************************/
1233/*--------------------------------------------------------------------
1234 * Reads up to 256 characters from the FIFO or until it's empty and
1235 * inserts them into the TTY layer. Returns the number of characters
1236 * read from the FIFO.
1237 --------------------------------------------------------------------*/
1238static int zx29_uart_fifo_to_tty(struct zx29_uart_port *zup)
1239{
1240 struct uart_port *port = &zup->port;
1241 u32 status, ch, i = 0;
1242 unsigned int flag, max_count = 256;
1243 int fifotaken = 0;
1244 u8 uart_poll_char[16] ={0};
1245
1246 while (max_count--) {
1247 status = UART_GET_FR(port);
1248 if (status & UART_FR_RXFE)
1249 break;
1250
1251 /* Take chars from the FIFO and update status */
1252 ch = UART_GET_CHAR(port) | UART_DUMMY_DR_RX;
1253
1254#if 0
1255 if(g_console_open_flag == 0 &&
1256 port->line == DEBUG_CONSOLE){
1257 if((ch&0xff) == 't'){
1258 memset(uart_poll_char, 0, sizeof(uart_poll_char));
1259 uart_poll_char[0] = 't';
1260 i = 0;
1261 printk("ch = %c i = %d\n",ch,i);
1262 }else if ((ch&0xff) == 'y' && (i == 1)){
1263 uart_poll_char[1] = 'y';
1264 printk("ch = %c i = %d\n",ch,i);
1265 }else if ((ch&0xff) == 'o' && (i == 2)){
1266 uart_poll_char[2] = 'o';
1267 printk("ch = %c i = %d\n",ch,i);
1268 }else if ((ch&0xff) == 'p' && (i == 3)){
1269 uart_poll_char[3] = 'p';
1270 printk("ch = %c i = %d\n",ch,i);
1271
1272 }else if ((ch&0xff) == 'e' && (i == 4)){
1273 uart_poll_char[4] = 'e';
1274 printk("ch = %c i = %d\n",ch,i);
1275
1276 }else if ((ch&0xff) == 'n' && (i == 5)){
1277 uart_poll_char[5] = 'n';
1278 printk("ch = %c i = %d\n",ch,i);
1279 g_console_open_flag = 1;
1280 printk("ch = %c i = %d,g_console_open_flag:%d\n",ch,i,g_console_open_flag);
1281 }else {
1282 i = 10;
1283 }
1284 i++;
1285 }
1286#endif
1287 flag = TTY_NORMAL;
1288 if(zup->autobaud_state == UART_PORT_AUTOBAUD_ON)
1289 {
1290 if(zup->port.icount.rx < UART_PORT_AUTOBAUD_BYTE)
1291 {
1292 uart_port_autobaud_buffer[zup->port.icount.rx] = ch;
1293 }
1294 else
1295 {
1296 uart_port_autobaud_gtflag = 1 ;
1297 }
1298 }
1299 zup->port.icount.rx++;
1300 if(zup->autobaud_state == UART_PORT_AUTOBAUD_OFF)
1301 {
1302 if(fifotaken < 16){
1303 uart_poll_char[fifotaken] = ch & 0xFF;
1304 }
1305 fifotaken++;
1306
1307 if (unlikely(ch & UART_DR_ERROR)) {
1308 if (ch & UART_DR_BE) {
1309 ch &= ~(UART_DR_FE | UART_DR_PE);
1310 zup->port.icount.brk++;
1311 if (uart_handle_break(&zup->port))
1312 continue;
1313 } else if (ch & UART_DR_PE)
1314 zup->port.icount.parity++;
1315 else if (ch & UART_DR_FE)
1316 zup->port.icount.frame++;
1317 else if (ch & UART_DR_OE){
1318 zup->port.icount.overrun++;
1319 //if(!uart_console(&zup->port))
1320 // BUG_ON(1);
1321 }
1322 ch &= zup->port.read_status_mask;
1323
1324 if (ch & UART_DR_BE)
1325 flag = TTY_BREAK;
1326 else if (ch & UART_DR_PE)
1327 flag = TTY_PARITY;
1328 else if (ch & UART_DR_FE)
1329 flag = TTY_FRAME;
1330 }
1331
1332 if (uart_handle_sysrq_char(&zup->port, ch & 255))
1333 continue;
1334 if(g_console_open_flag || port->line != DEBUG_CONSOLE){
1335 uart_insert_char(&zup->port, ch, UART_DR_OE, ch, flag);
1336 }
1337 }
1338 }
1339
1340 test_uart_static(zup->port.line, uart_poll_char, fifotaken, 3);
1341
1342 return fifotaken;
1343}
1344
1345/****************************************************************************/
1346static void zx29_uart_rx_chars(struct zx29_uart_port *zup)
1347{
1348 unsigned long flags;
1349
1350 //struct tty_struct *tty = zup->port.state->port.tty;
1351
1352 zx29_uart_fifo_to_tty(zup);
1353 spin_unlock(&zup->port.lock);
1354
1355 tty_flip_buffer_push(&zup->port.state->port);
1356
1357#if CONFIG_SERIAL_ZX29_DMA
1358 if(!uart_console(&zup->port)){//console doesn't use dma rcv data
1359 if (zx29_dma_rx_available(zup)) {
1360 if (zx29_dma_rx_trigger_dma(zup)) {
1361 dev_dbg(zup->port.dev, "could not trigger RX DMA job "
1362 "fall back to interrupt mode again\n");
1363 zup->imr |= UART_RXIM;
1364 } else{
1365 zup->imr &= ~UART_RXIM;
1366 }
1367 UART_PUT_IMSC(&zup->port,zup->imr);
1368 }
1369 }
1370#endif
1371RX_END:
1372 spin_lock(&zup->port.lock);
1373
1374}
1375
1376/****************************************************************************/
1377static void zx29_uart_tx_chars(struct zx29_uart_port *zup)
1378{
1379 struct circ_buf *xmit = &zup->port.state->xmit;
1380 unsigned long flags;
1381 int count;
1382
1383 if (zup->port.x_char) {
1384 UART_PUT_CHAR(&zup->port, zup->port.x_char);
1385 zup->port.icount.tx++;
1386 zup->port.x_char = 0;
1387 return;
1388 }
1389 if (uart_circ_empty(xmit) || uart_tx_stopped(&zup->port)) {
1390 zx29_uart_stop_tx(&zup->port);
1391 return;
1392 }
1393#if CONFIG_SERIAL_ZX29_DMA
1394 /* If we are using DMA mode, try to send some characters. */
1395 if(!uart_console(&(zup->port)))
1396 {
1397 if (zx29_dma_tx_irq(zup))
1398 return;
1399 }
1400#endif
1401 count = zup->port.fifosize >> 1;
1402 do {
1403 zx29_uart_putc(&zup->port, xmit->buf[xmit->tail]);
1404 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1405 zup->port.icount.tx++;
1406 if (uart_circ_empty(xmit))
1407 break;
1408 } while (--count > 0);
1409
1410 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1411 {
1412 spin_unlock(&zup->port.lock);
1413 //uart_write_wakeup(&zup->port);
1414 tasklet_schedule(&zup->write_wakeup);
1415 spin_lock(&zup->port.lock);
1416 }
1417
1418 if (uart_circ_empty(xmit))
1419 zx29_uart_stop_tx(&zup->port);
1420}
1421
1422#if CONFIG_SERIAL_ZX29_DMA
1423
1424dma_peripheral_id uart_get_rx_dma_peripheral_id(struct zx29_uart_port *zup)
1425{
1426 struct uart_port *port = &zup->port;
1427 if(port->line < UART0 || port->line > UART4){
1428 printk("get_rx_dma_peripheral_id,fail, invalid port->line:%d\n", port->line);
1429 }
1430 if(port->line == UART0){
1431 return DMA_CH_UART0_RX;
1432 } else if(port->line == UART1){
1433 return DMA_CH_UART1_RX;
1434 }else if(port->line == UART2){
1435 return DMA_CH_UART2_RX;
1436 }
1437// else if(port->line == UART3){
1438// return DMA_CH_UART3_RX;
1439// }else if(port->line == UART4){
1440// return DMA_CH_UART4_RX;
1441// }
1442
1443 return DMA_CH_NUM;
1444}
1445
1446/*
1447 * We received a transmit interrupt without a pending X-char but with
1448 * pending characters.
1449 * Locking: called with port lock held and IRQs disabled.
1450 * Returns:
1451 * false if we want to use PIO to transmit
1452 * true if we queued a DMA buffer
1453 */
1454static bool zx29_dma_tx_irq(struct zx29_uart_port *zup)
1455{
1456 if (!zup->using_tx_dma)
1457 return false;
1458
1459 /*
1460 * If we already have a TX buffer queued, but received a
1461 * TX interrupt, it will be because we've just sent an X-char.
1462 * Ensure the TX DMA is enabled and the TX IRQ is disabled.
1463 */
1464 if (zup->dmatx.queued) {
1465 zup->dmacr |= UART_TXDMAE;
1466 UART_PUT_DMACR(&zup->port, zup->dmacr);
1467 zup->imr &= ~UART_TXIM;
1468 UART_PUT_IMSC(&zup->port,zup->imr);
1469 return true;
1470 }
1471
1472 /*
1473 * We don't have a TX buffer queued, so try to queue one.
1474 * If we successfully queued a buffer, mask the TX IRQ.
1475 */
1476 if (zx29_uart_dma_tx_chars(zup) > 0) {
1477 zup->imr &= ~UART_TXIM;
1478 UART_PUT_IMSC(&zup->port,zup->imr);
1479 return true;
1480 }
1481 return false;
1482}
1483
1484
1485/*
1486 * Stop the DMA transmit (eg, due to received XOFF).
1487 * Locking: called with port lock held and IRQs disabled.
1488 */
1489static inline void zx29_dma_tx_stop(struct zx29_uart_port *zup)
1490{
1491 if (zup->dmatx.queued) {
1492 zup->dmacr &= ~UART_TXDMAE;
1493 UART_PUT_DMACR(&zup->port, zup->dmacr);
1494 }
1495}
1496
1497
1498/*
1499 * Try to start a DMA transmit, or in the case of an XON/OFF
1500 * character queued for send, try to get that character out ASAP.
1501 * Locking: called with port lock held and IRQs disabled.
1502 * Returns:
1503 * false if we want the TX IRQ to be enabled
1504 * true if we have a buffer queued
1505 */
1506static inline bool zx29_dma_tx_start(struct zx29_uart_port *zup)
1507{
1508 u16 dmacr;
1509
1510 if (!zup->using_tx_dma)
1511 return false;
1512
1513 if (!zup->port.x_char) {
1514 /* no X-char, try to push chars out in DMA mode */
1515 bool ret = true;
1516
1517 if (!zup->dmatx.queued) {
1518 if (zx29_uart_dma_tx_chars(zup) > 0) {
1519 zup->imr &= ~UART_TXIM;
1520 ret = true;
1521 } else {
1522 zup->imr |= UART_TXIM;
1523 ret = false;
1524 }
1525 UART_PUT_IMSC(&zup->port,zup->imr);
1526 } else if (!(zup->dmacr & UART_TXDMAE)) {
1527 zup->dmacr |= UART_TXDMAE;
1528 UART_PUT_DMACR(&zup->port, zup->dmacr);
1529 }
1530 return ret;
1531 }
1532
1533 /*
1534 * We have an X-char to send. Disable DMA to prevent it loading
1535 * the TX fifo, and then see if we can stuff it into the FIFO.
1536 */
1537 dmacr = zup->dmacr;
1538 zup->dmacr &= ~UART_TXDMAE;
1539 UART_PUT_DMACR(&zup->port, zup->dmacr);
1540
1541 if (UART_GET_FR(&zup->port) & UART_FR_TXFF) {
1542 /*
1543 * No space in the FIFO, so enable the transmit interrupt
1544 * so we know when there is space. Note that once we've
1545 * loaded the character, we should just re-enable DMA.
1546 */
1547 return false;
1548 }
1549
1550 UART_PUT_CHAR(&zup->port, zup->port.x_char);
1551 //writew(uap->port.x_char, uap->port.membase + UART01x_DR);
1552 zup->port.icount.tx++;
1553 zup->port.x_char = 0;
1554
1555 /* Success - restore the DMA state */
1556 zup->dmacr = dmacr;
1557 UART_PUT_DMACR(&zup->port, zup->dmacr);
1558 //writew(dmacr, uap->port.membase + UART011_DMACR);
1559
1560 return true;
1561}
1562
1563/****************************************************************************/
1564
1565//#if CONFIG_SERIAL_ZX29_DMA
1566/*
1567 * Flush the transmit buffer.
1568 * Locking: called with port lock held and IRQs disabled.
1569 */
1570static void zx29_dma_flush_buffer(struct uart_port *port)
1571{
1572 struct zx29_uart_port *zup = (struct zx29_uart_port *)port;
1573 if (!zup->using_tx_dma)
1574 return;
1575
1576 /* Avoid deadlock with the DMA engine callback */
1577 //dmaengine_terminate_all(zup->dmatx.chan);
1578 if (zup->dmatx.queued) {
1579
1580 //printk(KERN_INFO "zx29_dma_flush_buffer enter[%s][%d] Port[%d]\n",__func__,__LINE__,port->line);
1581 dma_unmap_sg(zup->dmatx.chan->device->dev, &zup->dmatx.sg, 1,
1582 DMA_TO_DEVICE);
1583 zup->dmatx.queued = false;
1584 zup->dmacr &= ~UART_TXDMAE;
1585 UART_PUT_DMACR(&zup->port, zup->dmacr);
1586 }
1587}
1588
1589static int zx29_dma_rx_trigger_dma(struct zx29_uart_port *zup)
1590{
1591 struct dma_chan *rxchan = zup->dmarx.chan;
1592 struct zx29_dmarx_data *dmarx = &zup->dmarx;
1593 struct dma_async_tx_descriptor *desc;
1594 struct zx29_sgbuf *sgbuf;
1595
1596 dma_peripheral_id rx_id = uart_get_rx_dma_peripheral_id(zup);
1597 if (!rxchan)
1598 {
1599 printk("[%s][%d]\n",__func__,__LINE__);
1600 return -EIO;
1601 }
1602
1603 /* Start the RX DMA job */
1604
1605 sgbuf = zup->dmarx.use_buf_b ?
1606 &zup->dmarx.sgbuf_b : &zup->dmarx.sgbuf_a;
1607 /*
1608
1609 sgbuf = zup->dmarx.use_buf_b ?
1610 &zup->dmarx.sgbuf_a : &zup->dmarx.sgbuf_b;
1611 */
1612 zup->dmarx.rx_def[zup->dmarx.rx_index].link_addr=0;
1613 zup->dmarx.rx_def[zup->dmarx.rx_index].dest_addr=(unsigned int)(sgbuf->dma_addr);
1614 zup->dmarx.rx_def[zup->dmarx.rx_index].count=ZX29_DMA_BUFFER_SIZE;//fifo or max buffer?
1615 wmb();
1616
1617 dmaengine_slave_config(rxchan, (struct dma_slave_config*)&zup->dmarx.rx_def[zup->dmarx.rx_index]);
1618 desc = rxchan->device->device_prep_interleaved_dma(rxchan,NULL,0);
1619
1620
1621 /*
1622 * If the DMA engine is busy and cannot prepare a
1623 * channel, no big deal, the driver will fall back
1624 * to interrupt mode as a result of this error code.
1625 */
1626 if (!desc) {
1627 printk(KERN_INFO "!!ERROR DESC !!![%s][%d]Port:[%d]\n",__func__,__LINE__,zup->port.line);
1628 sc_debug_info_record(MODULE_ID_CAP_UART, "uart%d ERROR DESC \n",zup->port.line);
1629 zup->dmarx.running = false;
1630 dmaengine_terminate_all(rxchan);
1631 //zx29_dma_force_stop(rx_id);
1632 return -EBUSY;
1633 }
1634
1635 /* Some data to go along to the callback */
1636 desc->callback = uart_dma_rx_callback;
1637 desc->callback_param = zup;
1638 zup->curr_sg = sgbuf;
1639 wmb();
1640
1641 dmarx->cookie = dmaengine_submit(desc);
1642 dma_async_issue_pending(rxchan);
1643 atomic_inc(&zup->dmarx.count);
1644 zup->dmarx.rx_index = (zup->dmarx.rx_index +1)%UART_DMA_RX_MAX_COUNT;
1645 zup->dmacr |= UART_RXDMAE;
1646 UART_PUT_DMACR(&zup->port, zup->dmacr);
1647 zup->dmarx.running = true;
1648 zup->dmarx.used = true;
1649 zup->imr &= ~(UART_RXIM | UART_RTIM);
1650 UART_PUT_IMSC(&zup->port,zup->imr);
1651
1652
1653 return 0;
1654}
1655static int zx29_dma_rx_trigger_dma_use_dma_cyclic(struct zx29_uart_port *zup)
1656{
1657 struct dma_chan *rxchan = zup->dmarx.chan;
1658 struct zx29_dmarx_data *dmarx = &zup->dmarx;
1659 struct dma_async_tx_descriptor *desc;
1660 dma_peripheral_id rx_id = uart_get_rx_dma_peripheral_id(zup);
1661 int uart_id = zup->port.line;
1662 if (!rxchan)
1663 {
1664 printk("[%s][%d]\n",__func__,__LINE__);
1665 return -EIO;
1666 }
xf.li1867bfa2024-08-20 02:32:16 -07001667
1668 if(uart_dma_cycle[zup->port.line].used)
1669 uart_id = uart_id + 3;
1670
xf.li2f424182024-08-20 00:47:34 -07001671 dmaengine_slave_config(rxchan, (struct dma_slave_config*)&uart_dma_cycle[uart_id].rxdef);
1672 desc = rxchan->device->device_prep_dma_cyclic(rxchan,NULL,(ZX29_DMA_BUFFER_SIZE *5) , ZX29_DMA_BUFFER_SIZE,0,0);
1673 if (!desc) {
1674 printk(KERN_INFO "!!ERROR DESC !!![%s][%d]Port:[%d]\n",__func__,__LINE__,zup->port.line);
1675 zup->dmarx.running = false;
1676 dmaengine_terminate_all(rxchan);
1677 return -EBUSY;
1678 }
1679 desc->callback = uart_dma_rx_callback_use_dma_cyclic;
1680 desc->callback_param = zup;
1681 wmb();
1682 dmarx->cookie = dmaengine_submit(desc);
1683 dma_async_issue_pending(rxchan);
1684 zup->dmacr |= UART_RXDMAE;
1685 UART_PUT_DMACR(&zup->port, zup->dmacr);
1686 uart_dma_cycle[uart_id].flg_enter_th = 0;
xf.li1867bfa2024-08-20 02:32:16 -07001687 if(uart_dma_cycle[zup->port.line].used){
1688 uart_dma_cycle[zup->port.line].used = false;
1689 uart_dma_cycle[zup->port.line+3].used = true;
1690 }else{
1691 uart_dma_cycle[zup->port.line].used = true;
1692 uart_dma_cycle[zup->port.line+3].used = false;
1693 }
1694
xf.li2f424182024-08-20 00:47:34 -07001695 zup->dmarx.running = true;
1696 zup->dmarx.used = true;
1697 zup->imr &= ~(UART_RXIM | UART_RTIM);
1698 UART_PUT_IMSC(&zup->port,zup->imr);
1699 return 0;
1700}
1701
1702void uart_dma_rx_callback(void *data)
1703{
1704 unsigned long flags;
1705
1706 struct zx29_uart_port *zup = (struct zx29_uart_port *)data;
1707 int uart_id = zup->port.line;
1708 struct zx29_dmarx_data *dmarx = &zup->dmarx;
1709 struct dma_chan *rxchan = dmarx->chan;
1710 struct device *dev = NULL;
1711// struct dma_tx_state state;
1712 unsigned int ris_status;
1713
1714 bool lastbuf;
1715 int dma_count = 0;
1716 struct zx29_sgbuf *sgbuf = zup->curr_sg;
1717 size_t pending;
1718
1719 spin_lock_irqsave(&zup->port.lock, flags);
1720 ris_status = UART_GET_RIS(&zup->port);
1721 if(ris_status & (UART_OEIS | UART_BEIS | UART_PEIS | UART_FEIS)){
1722 if(ris_status & UART_OEIS){
1723 zup->port.icount.overrun++;
1724 g_uart_overrun[uart_id] = 4;
1725 test_uart_static(zup->port.line, NULL, 0, 20);
1726 //if(!uart_console(&zup->port))
1727 // BUG_ON(1);
1728 }
1729 if(ris_status & UART_BEIS)
1730 zup->port.icount.brk++;
1731 if(ris_status & UART_PEIS)
1732 zup->port.icount.parity++;
1733 if(ris_status & UART_FEIS)
1734 zup->port.icount.frame++;
1735 UART_PUT_ICR(&zup->port, (UART_OEIS | UART_BEIS | UART_PEIS | UART_FEIS));
1736 }
1737 dma_peripheral_id rx_id = uart_get_rx_dma_peripheral_id(zup);
1738 zx29_dma_stop(rx_id);
1739
1740 dev = zup->dmarx.chan->device->dev;
1741 zup->dmacr &= ~UART_RXDMAE;
1742 UART_PUT_DMACR(&zup->port,zup->dmacr);
1743
1744 //spin_lock_irqsave(&zup->port.lock, flags);
1745 zup->sg2tty = sgbuf;
1746// rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
1747 zup->sg2tty_len = zup->sg2tty->sg.length - zx29_dma_get_transfer_num(rx_id);
1748 //zx29_dma_force_stop(rx_id);
1749 // dmaengine_terminate_all(rxchan);
1750 dmarx->use_buf_b = ! dmarx->use_buf_b;
1751 wmb();
1752 //BUG_ON(pending > ZX29_DMA_BUFFER_SIZE);
1753 /* Then we terminate the transfer - we now know our residue */
1754 //dmaengine_terminate_all(rxchan);
1755
1756 zup->dmarx.running = false;
1757 zup->dmarx.used = false;
1758 test_uart_static(zup->port.line, NULL, 0, 9);
1759 if (zx29_dma_rx_trigger_dma(zup)) {
1760 printk("rx_dma_chars RXDMA start fail\n");
1761 zup->imr |= UART_RXIM;
1762 UART_PUT_IMSC(&zup->port,zup->imr);
1763 }else{
1764 zup->pre_pending = 0;
1765 zup->dmarx.used = true;
1766 zup->work_state = true;
1767 }
1768 spin_unlock_irqrestore(&zup->port.lock, flags);
1769
1770 up(&zup->sema);
1771}
1772
1773void uart_dma_rx_callback_use_dma_cyclic(void *data)
1774{
1775 unsigned long flags;
1776 struct zx29_uart_port *zup = (struct zx29_uart_port *)data;
1777 unsigned int ris_status;
1778 int uart_id = zup->port.line;
1779 spin_lock_irqsave(&zup->port.lock, flags);
xf.li1867bfa2024-08-20 02:32:16 -07001780 if(!uart_dma_cycle[zup->port.line].used)
1781 uart_id = uart_id + 3;
xf.li2f424182024-08-20 00:47:34 -07001782 uart_dma_cycle[uart_id].cnt_callback_total++;
1783 uart_dma_cycle[uart_id].cnt_callback++;
1784 ris_status = UART_GET_RIS(&zup->port);
1785 if(ris_status & (UART_OEIS | UART_BEIS | UART_PEIS | UART_FEIS)){
1786 if(ris_status & UART_OEIS){
1787 zup->port.icount.overrun++;
1788 uart_dma_cycle[uart_id].flg_overrun = 1;
1789 }
1790 if(ris_status & UART_BEIS){
1791 uart_dma_cycle[uart_id].flg_be = 1;
1792 zup->port.icount.brk++;
1793 }
1794 if(ris_status & UART_PEIS){
1795 uart_dma_cycle[uart_id].flg_pe = 1;
1796 zup->port.icount.parity++;
1797 }
1798 if(ris_status & UART_FEIS){
1799 uart_dma_cycle[uart_id].flg_fe = 1;
1800 zup->port.icount.frame++;
1801 }
1802 UART_PUT_ICR(&zup->port, (UART_OEIS | UART_BEIS | UART_PEIS | UART_FEIS));
1803 }
1804 spin_unlock_irqrestore(&zup->port.lock, flags);
1805 test_uart_static(zup->port.line, NULL, 0, 26);
1806 up(&zup->sema_cyclic);
1807}
1808static inline void zx29_dma_rx_stop(struct zx29_uart_port *zup)
1809{
1810 dma_peripheral_id rx_id = uart_get_rx_dma_peripheral_id(zup);
1811 //zx29_dma_force_stop(rx_id);
1812 //dmaengine_terminate_all(zup->dmarx.chan);
1813 /* FIXME. Just disable the DMA enable */
1814 zup->dmacr &= ~UART_RXDMAE;
1815 UART_PUT_DMACR(&zup->port,zup->dmacr);
1816 zx29_dma_stop(rx_id);
1817#if 0
1818 //do we need check data received?
1819 if(zup->pre_pending){
1820 printk("pre_pending :%d\n ", zup->pre_pending);
1821 }
1822#endif
1823 zup->curr_sg = NULL;
1824}
1825
1826static void zx29_dma_remove(struct zx29_uart_port *zup)
1827{
1828 /* TODO: remove the initcall if it has not yet executed */
1829 if (zup->dmatx.chan)
1830 dma_release_channel(zup->dmatx.chan);
1831 if (zup->dmarx.chan)
1832 dma_release_channel(zup->dmarx.chan);
1833}
1834
1835
1836static void zx29_dma_shutdown(struct zx29_uart_port *zup)
1837{
1838 unsigned long flags;
1839 dma_peripheral_id rx_id = uart_get_rx_dma_peripheral_id(zup);
1840
1841 if (!(zup->using_tx_dma || zup->using_rx_dma))
1842 return;
1843 /* Disable RX and TX DMA */
1844 while(UART_GET_FR(&zup->port) & (UART_FR_TXBUSY | UART_FR_TXBUSY))
1845 barrier();
1846
1847 spin_lock_irqsave(&zup->port.lock, flags);
1848 //zx29_dma_force_stop(rx_id);
1849 // dmaengine_terminate_all(zup->dmarx.chan);
1850 zup->dmacr &= ~(UART_DMAONERR | UART_RXDMAE | UART_TXDMAE);
1851 UART_PUT_DMACR(&zup->port,zup->dmacr);
1852 zx29_dma_stop(rx_id);
1853 zup->curr_sg = NULL;
1854 spin_unlock_irqrestore(&zup->port.lock, flags);
1855 if (zup->using_tx_dma) {
1856 /* In theory, this should already be done by zx29_dma_flush_buffer */
1857 dmaengine_terminate_all(zup->dmatx.chan);
1858 if (zup->dmatx.queued) {
1859 dma_unmap_sg(zup->dmatx.chan->device->dev, &zup->dmatx.sg, 1,
1860 DMA_TO_DEVICE);
1861 zup->dmatx.queued = false;
1862 }
1863 if(!zup->dmatx.buf)
1864 kfree(zup->dmatx.buf);
1865 zup->dmatx.buf = NULL;
1866 zup->using_tx_dma = false;
1867 }
1868 if (zup->using_rx_dma) {
1869 //dmaengine_terminate_all(zup->dmarx.chan);
1870 /* Clean up the RX DMA */
1871 if(!zup->uart_power_mode){
1872 zx29_sgbuf_free(zup->dmarx.chan, &zup->dmarx.sgbuf_a, DMA_FROM_DEVICE);
1873 zx29_sgbuf_free(zup->dmarx.chan, &zup->dmarx.sgbuf_b, DMA_FROM_DEVICE);
1874 }else if(zup->uart_power_mode == 1){
1875 uart_dma_cycle_deinit(zup);
1876 }else
1877 printk("uart%d dma shutdown fail.\n",zup->port.line);
1878 zup->using_rx_dma = false;
1879 zup->dmarx.used = false;
1880 zup->dmarx.running = false;
1881 zup->dmarx.use_buf_b = false;
1882 zup->dmarx.rx_index = 0;
1883 }
1884 zup->pre_pending = 0;
1885 zup->work_state = false;
1886
1887}
1888
1889static void zx29_shutdown_channel(struct zx29_uart_port *zup,
1890 unsigned int lcrh)
1891{
1892 unsigned long val;
1893
1894 val = UART_GET_LCRH(&zup->port);
1895 val &= ~(UART_LCRH_BRK | UART_LCRH_FEN);
1896 UART_PUT_LCRH(&zup->port, val);
1897}
1898
1899
1900static inline bool zx29_dma_rx_available(struct zx29_uart_port *zup)
1901{
1902 return zup->using_rx_dma;
1903}
1904
1905static inline bool zx29_dma_rx_running(struct zx29_uart_port *zup)
1906{
1907 return zup->using_rx_dma && zup->dmarx.running;
1908}
1909
1910static inline bool zx29_dma_rx_used(struct zx29_uart_port *zup)
1911{
1912 return zup->using_rx_dma && zup->dmarx.used;
1913}
1914
1915static inline bool zx29_dma_rx_work_scheduled(struct zx29_uart_port *zup)
1916{
1917 return zup->using_rx_dma && zup->work_state;
1918}
1919
1920
1921void uart_dma_tx_callback(void *data)
1922{
1923 struct zx29_uart_port *zup = data;
1924 struct zx29_dmatx_data *dmatx = &zup->dmatx;
1925
1926 unsigned long flags;
1927 u16 dmacr;
1928 spin_lock_irqsave(&zup->port.lock, flags);
1929 if (zup->dmatx.queued)
1930 dma_unmap_sg(dmatx->chan->device->dev, &dmatx->sg, 1,
1931 DMA_TO_DEVICE);
1932
1933 dmacr = zup->dmacr;
1934 zup->dmacr = dmacr & ~UART_TXDMAE;
1935 UART_PUT_DMACR(&zup->port,zup->dmacr);
1936
1937 /*
1938 * If TX DMA was disabled, it means that we've stopped the DMA for
1939 * some reason (eg, XOFF received, or we want to send an X-char.)
1940 *
1941 * Note: we need to be careful here of a potential race between DMA
1942 * and the rest of the driver - if the driver disables TX DMA while
1943 * a TX buffer completing, we must update the tx queued status to
1944 * get further refills (hence we check dmacr).
1945 */
1946 if (!(dmacr & UART_TXDMAE) || uart_tx_stopped(&zup->port) ||
1947 uart_circ_empty(&zup->port.state->xmit)) {
1948 zup->dmatx.queued = false;
1949
1950
1951 zx_cpuidle_set_free(IDLE_FLAG_UART);
1952
1953
1954 spin_unlock_irqrestore(&zup->port.lock, flags);
1955 return;
1956 }
1957
1958 if (zx29_uart_dma_tx_chars(zup) <= 0) {
1959 /*
1960 * We didn't queue a DMA buffer for some reason, but we
1961 * have data pending to be sent. Re-enable the TX IRQ.
1962 */
1963 zup->imr |= UART_TXIM;
1964 UART_PUT_IMSC(&zup->port, zup->imr);
1965 }
1966 spin_unlock_irqrestore(&zup->port.lock, flags);
1967}
1968
1969static int zx29_uart_dma_tx_chars(struct zx29_uart_port *zup)
1970{
1971 struct zx29_dmatx_data *dmatx = &zup->dmatx;
1972 struct dma_chan *tx_chan = dmatx->chan;
1973 struct dma_device *dma_dev = tx_chan->device;
1974 struct dma_async_tx_descriptor *desc;
1975 struct circ_buf *xmit = &zup->port.state->xmit;
1976 unsigned int count;
1977
1978 /*
1979 * Try to avoid the overhead involved in using DMA if the
1980 * transaction fits in the first half of the FIFO, by using
1981 * the standard interrupt handling. This ensures that we
1982 * issue a uart_write_wakeup() at the appropriate time.
1983 */
1984
1985 count = uart_circ_chars_pending(xmit);
1986 if (count < (16 >> 1)) {
1987 zup->dmatx.queued = false;
1988 return 0;
1989 }
1990
1991 if (xmit->tail < xmit->head)
1992 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], count);
1993 else {
1994 size_t first = UART_XMIT_SIZE - xmit->tail;
1995 size_t second ;//= xmit->head;
1996
1997 if (first > count)
1998 first = count;
1999 second = count - first;
2000 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], first);
2001 if (second)
2002 memcpy(&dmatx->buf[first], &xmit->buf[0], second);
2003 }
2004 dmatx->sg.length = count;
2005
2006 if (dma_map_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE) != 1) {
2007 zup->dmatx.queued = false;
2008 dev_dbg(zup->port.dev, "unable to map TX DMA\n");
2009 return -EBUSY;
2010 }
2011
2012
2013 zup->dmatx.tx_def.link_addr=0;
2014 zup->dmatx.tx_def.src_addr=(unsigned int)(dmatx->sg.dma_address);
2015 zup->dmatx.tx_def.count=count;
2016 wmb();
2017 dmaengine_slave_config(tx_chan, (struct dma_slave_config*)&zup->dmatx.tx_def);
2018 desc = tx_chan->device->device_prep_interleaved_dma(tx_chan,NULL,0);
2019
2020 if (!desc) {
2021 printk(KERN_INFO "!!!!!ERROR TX DESC[%s][%d]\n",__func__,__LINE__);
2022 dma_unmap_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE);
2023 zup->dmatx.queued = false;
2024 /*
2025 * If DMA cannot be used right now, we complete this
2026 * transaction via IRQ and let the TTY layer retry.
2027 */
2028 dev_dbg(zup->port.dev, "TX DMA busy\n");
2029 return -EBUSY;
2030 }
2031 desc->callback = (dma_async_tx_callback)uart_dma_tx_callback;
2032 desc->callback_param = (void *)zup;
2033 dmaengine_submit(desc);
2034 dma_async_issue_pending(tx_chan);
2035 atomic_inc(&zup->dmatx.count);
2036 zup->dmacr |= UART_TXDMAE;
2037 UART_PUT_DMACR(&zup->port,zup->dmacr);
2038 zup->dmatx.queued = true;
2039
2040 /*
2041 * Now we know that DMA will fire, so advance the ring buffer
2042 * with the stuff we just dispatched.
2043 */
2044 xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
2045 zup->port.icount.tx += count;
2046
2047 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
2048 //uart_write_wakeup(&zup->port);
2049 tasklet_schedule(&zup->write_wakeup);
2050
2051 return 1;
2052}
2053
2054static void zx29_uart_dma_rx_chars(struct zx29_uart_port *zup,
2055 //u32 pending, bool use_buf_b,
2056 u32 pending, struct zx29_sgbuf *sgbuf,
2057 bool readfifo, unsigned long *flags)
2058{
2059 struct tty_struct *tty = zup->port.state->port.tty;
2060#if 0
2061 struct zx29_sgbuf *sgbuf = use_buf_b ?
2062 &zup->dmarx.sgbuf_b : &zup->dmarx.sgbuf_a;
2063#endif
2064 struct device *dev = zup->dmarx.chan->device->dev;
2065 int dma_count = 0;
2066 u32 fifotaken = 0; /* only used for vdbg() */
2067 //unsigned long flags;
2068
2069 /* Pick everything from the DMA first */
2070 if (pending) {
2071 /* Sync in buffer */
2072
2073 dma_sync_sg_for_cpu(dev, &sgbuf->sg, 1, DMA_FROM_DEVICE);
2074
2075 /*
2076 * First take all chars in the DMA pipe, then look in the FIFO.
2077 * Note that tty_insert_flip_buf() tries to take as many chars
2078 * as it can.
2079 */
2080
2081 spin_unlock_irqrestore(&zup->port.lock, *flags);
2082
2083 dma_count = tty_insert_flip_string(&zup->port.state->port,
2084 sgbuf->buf, pending);
2085
2086 test_uart_static(zup->port.line, sgbuf->buf, pending, 6);
2087 spin_lock_irqsave(&zup->port.lock, *flags);
2088 /* Return buffer to device */
2089 dma_sync_sg_for_device(dev, &sgbuf->sg, 1, DMA_FROM_DEVICE);
2090
2091 zup->port.icount.rx += dma_count;
2092 if (dma_count < pending)
2093 dev_info(zup->port.dev,
2094 "couldn't insert all characters (TTY is full?)\n");
2095 }
2096
2097 /*
2098 * Only continue with trying to read the FIFO if all DMA chars have
2099 * been taken first.
2100 */
2101 //if (dma_count == pending && readfifo) {
2102 if (readfifo) {
2103 /* Clear any error flags */
2104 //UART_PUT_ICR(&zup->port,UART_OEIC | UART_BEIC | UART_PEIC | UART_FEIC);
2105 /*
2106 * If we read all the DMA'd characters, and we had an
2107 * incomplete buffer, that could be due to an rx error, or
2108 * maybe we just timed out. Read any pending chars and check
2109 * the error status.
2110 *
2111 * Error conditions will only occur in the FIFO, these will
2112 * trigger an immediate interrupt and stop the DMA job, so we
2113 * will always find the error in the FIFO, never in the DMA
2114 * buffer.
2115 */
2116 test_uart_static(zup->port.line, NULL, 0, 7);
2117 fifotaken = zx29_uart_fifo_to_tty(zup);
2118 }
2119 if((pending > 0) || (fifotaken > 0)) {
2120 spin_unlock(&zup->port.lock);
2121 tty_flip_buffer_push(&zup->port.state->port);
2122 spin_lock(&zup->port.lock);
2123 }
2124}
2125static void zx29_uart_deal_dma_fifo_rx_chars_cyclic(struct zx29_uart_port *zup,
2126 u32 pending, struct zx29_sgbuf *sgbuf,
2127 unsigned long *flags, char *fifo_buf, int fifo_len)
2128{
2129 struct tty_struct *tty = zup->port.state->port.tty;
2130 struct device *dev = zup->dmarx.chan->device->dev;
2131 int dma_count = 0;
2132 int fifo_count = 0;
2133 u32 fifotaken = 0; /* only used for vdbg() */
2134 if ((pending) && (pending != 4096)) {
2135 dma_sync_sg_for_cpu(dev, &sgbuf->sg, 1, DMA_FROM_DEVICE);
2136 spin_unlock_irqrestore(&zup->port.lock, *flags);
2137 dma_count = tty_insert_flip_string(&zup->port.state->port,
2138 sgbuf->buf, pending);
2139 test_uart_static(zup->port.line, sgbuf->buf, pending, 6);
xf.li1867bfa2024-08-20 02:32:16 -07002140#if UART_DEBUG_RECORDER_BYTE
2141 uart_debug(sgbuf->buf, pending);
2142#endif
xf.li2f424182024-08-20 00:47:34 -07002143 spin_lock_irqsave(&zup->port.lock, *flags);
2144 dma_sync_sg_for_device(dev, &sgbuf->sg, 1, DMA_FROM_DEVICE);
2145 zup->port.icount.rx += dma_count;
2146 if (dma_count < pending)
2147 dev_info(zup->port.dev,
2148 "couldn't insert all characters (TTY is full?)\n");
2149 }
2150 if(fifo_len){
2151 spin_unlock_irqrestore(&zup->port.lock, *flags);
2152 fifo_count = tty_insert_flip_string(&zup->port.state->port,
2153 fifo_buf, fifo_len);
xf.li1867bfa2024-08-20 02:32:16 -07002154 test_uart_static(zup->port.line, fifo_buf, fifo_len, 50);
2155#if UART_DEBUG_RECORDER_BYTE
2156 uart_debug(fifo_buf, fifo_len);
2157#endif
xf.li2f424182024-08-20 00:47:34 -07002158 fifo_buf[0] = '\0';
2159 fifo_buf[1] = '\0';
2160 fifo_buf[2] = '\0';
2161 spin_lock_irqsave(&zup->port.lock, *flags);
2162 }
2163 zup->port.icount.rx += fifo_count;
2164 if(((pending) && (pending != 4096)) || (fifo_len > 0)){
2165 spin_unlock(&zup->port.lock);
2166 tty_flip_buffer_push(&zup->port.state->port);
xf.li1867bfa2024-08-20 02:32:16 -07002167 test_uart_static(zup->port.line, NULL, (fifo_count+dma_count), 51);
xf.li2f424182024-08-20 00:47:34 -07002168 spin_lock(&zup->port.lock);
2169 }
2170}
2171
2172static void zx29_uart_deal_dma_fifo_rx_chars(struct zx29_uart_port *zup,
2173 u32 pending, struct zx29_sgbuf *sgbuf,
2174 unsigned long *flags, char *fifo_buf, int fifo_len)
2175{
2176 struct tty_struct *tty = zup->port.state->port.tty;
2177
2178 struct device *dev = zup->dmarx.chan->device->dev;
2179 int dma_count = 0;
2180 int fifo_count = 0;
2181 u32 fifotaken = 0; /* only used for vdbg() */
2182 if (pending) {
2183 dma_sync_sg_for_cpu(dev, &sgbuf->sg, 1, DMA_FROM_DEVICE);
2184 spin_unlock_irqrestore(&zup->port.lock, *flags);
2185 dma_count = tty_insert_flip_string(&zup->port.state->port,
2186 sgbuf->buf, pending);
2187 test_uart_static(zup->port.line, sgbuf->buf, pending, 6);
2188 spin_lock_irqsave(&zup->port.lock, *flags);
2189 dma_sync_sg_for_device(dev, &sgbuf->sg, 1, DMA_FROM_DEVICE);
2190 spin_unlock_irqrestore(&zup->port.lock, *flags);
2191 zup->port.icount.rx += dma_count;
2192 if (dma_count < pending)
2193 dev_info(zup->port.dev,
2194 "couldn't insert all characters (TTY is full?)\n");
2195 }
2196
2197 if(fifo_len){
2198 //printk("qq >> fifo len %d.\n",fifo_len);
2199 fifo_count = tty_insert_flip_string(&zup->port.state->port,
2200 fifo_buf, fifo_len);
2201 //printk("qq >>fifo count %d,buf is %x %x %x .\n",fifo_count, fifo_buf[0],fifo_buf[1],fifo_buf[2]);
2202 fifo_buf[0] = '\0';
2203 fifo_buf[1] = '\0';
2204 fifo_buf[2] = '\0';
2205 //memset(fifo_buf, '\0', 4);
2206 }
2207
2208 zup->port.icount.rx += fifo_count;
2209 test_uart_static(zup->port.line, fifo_buf, fifo_count, 18);
2210 if(pending > 0 || (fifo_len > 0)){
2211 tty_flip_buffer_push(&zup->port.state->port);
2212 spin_lock_irqsave(&zup->port.lock, *flags);
2213 }
2214}
2215
2216#if 0
2217static void zx29_dma_rx_irq(struct zx29_uart_port *zup, unsigned long *flags)
2218{
2219 struct zx29_dmarx_data *dmarx = &zup->dmarx;
2220 struct dma_chan *rxchan = dmarx->chan;
2221 struct zx29_sgbuf *sgbuf = dmarx->use_buf_b ?
2222 &dmarx->sgbuf_b : &dmarx->sgbuf_a;
2223 size_t pending;
2224 struct dma_tx_state state;
2225 enum dma_status dmastat;
2226 dma_peripheral_id rx_id = uart_get_rx_dma_peripheral_id(zup);
2227
2228 uint32_t ris_status = UART_GET_RIS(&zup->port);
2229 //printk("rx irq\n");
2230 if(ris_status & (UART_OEIS | UART_BEIS | UART_PEIS | UART_FEIS)){
2231 if(ris_status & UART_OEIS){
2232 zup->port.icount.overrun++;
2233 //if(!uart_console(&zup->port))
2234 // BUG_ON(1);
2235 }
2236 if(ris_status & UART_BEIS)
2237 zup->port.icount.brk++;
2238 if(ris_status & UART_PEIS)
2239 zup->port.icount.parity++;
2240 if(ris_status & UART_FEIS)
2241 zup->port.icount.frame++;
2242
2243 UART_PUT_ICR(&zup->port, (UART_OEIS | UART_BEIS | UART_PEIS | UART_FEIS));
2244 }
2245
2246 if(zx29_dma_rx_running(zup)){
2247 /*
2248 * Pause the transfer so we can trust the current counter,
2249 * do this before we pause the block, else we may
2250 * overflow the FIFO.
2251 */
2252 // if(zx29_dma_stop(rx_id))
2253 // printk( "uart%d unable to pause DMA transfer\n", zup->port.line);
2254 //rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
2255 //zx29_dma_force_stop(rx_id);
2256 //dmaengine_terminate_all(rxchan);
2257
2258 //dmastat = zx29_dma_get_status();//Normally,this value is insignificance.
2259
2260 /* Disable RX DMA - incoming data will wait in the FIFO */
2261 zup->dmacr &= ~UART_RXDMAE;
2262 UART_PUT_DMACR(&zup->port,zup->dmacr);
2263 zx29_dma_stop(rx_id);
2264 zup->dmarx.running = false;
2265 zup->dmarx.used = false;
2266 pending = sgbuf->sg.length - zx29_dma_get_transfer_num(rx_id);//state.residue;
2267 BUG_ON(pending > ZX29_DMA_BUFFER_SIZE);
2268 /* Then we terminate the transfer - we now know our residue */
2269 //dmaengine_terminate_all(rxchan);
2270
2271 dmarx->use_buf_b = !dmarx->use_buf_b;
2272 /*
2273 * This will take the chars we have so far and insert
2274 * into the framework.
2275 */
2276 zx29_uart_dma_rx_chars(zup, pending, sgbuf, false, flags);
2277 }
2278
2279 /* Switch buffer & re-trigger DMA job */
2280 if (zx29_dma_rx_trigger_dma(zup)) {
2281 printk("uart%d could not retrigger RX DMA job\n",zup->port.line);
2282 zup->imr |= UART_RXIM;
2283 UART_PUT_IMSC(&zup->port, zup->imr);
2284 }
2285#if RX_DMA_WORK
2286 //printk("add timer\n");
2287 else{
2288 // mod_timer(&(zup->rx_dma_timer), jiffies + msecs_to_jiffies(RX_DMA_TIMEOUT));
2289 uart_mod_timer(zup, flags);
2290 zup->pre_pending = 0;
2291 zup->work_state = true;
2292 }
2293#endif
2294
2295}
2296#endif
2297/****************************************************************************/
2298static void zx29_uart_rx_dma_chars(struct zx29_uart_port *zup, unsigned long *flags)
2299{
2300
2301 struct tty_struct *tty = zup->port.state->port.tty;
2302 //zx29_uart_fifo_to_tty(zup);
2303// spin_unlock(&zup->port.lock);
2304 if (zx29_dma_rx_available(zup)) {
2305 if (zx29_dma_rx_trigger_dma(zup)) {
2306 printk("rx_dma_chars RXDMA start fail\n");
2307 zup->imr |= UART_RXIM | UART_RTIM;
2308 UART_PUT_IMSC(&zup->port,zup->imr);
2309 }
2310#if RX_DMA_WORK
2311//printk("add timer\n");
2312 else{
2313 //mod_timer(&(zup->rx_dma_timer), jiffies + msecs_to_jiffies(RX_DMA_TIMEOUT));
2314 uart_mod_timer(zup, flags);
2315 zup->pre_pending = 0;
2316 zup->work_state = true;
2317 }
2318#endif
2319 }
2320
2321 //tty_flip_buffer_push(tty);
2322 //spin_lock(&zup->port.lock);
2323}
2324
2325
2326/****************************************************************************/
2327static void zx29_uart_rx_timeout_chars(struct zx29_uart_port *zup, unsigned long *flags)
2328{
2329 int rt_cnt = 0;
2330// unsigned long flags;
2331
2332 int fr = UART_GET_FR(&zup->port);
2333 //printk("rx_timeout_chars\n");
2334
2335 rt_cnt = zx29_uart_fifo_to_tty(zup);
2336 if(rt_cnt){
2337 if(g_console_open_flag == 1 || zup->port.line != DEBUG_CONSOLE){
2338 spin_unlock(&zup->port.lock);
2339 tty_flip_buffer_push(&zup->port.state->port);
2340 spin_lock(&zup->port.lock);
2341 }
2342 }
2343}
2344
2345static void zx29_uart_rt_dma(struct zx29_uart_port *zup, unsigned long *flags)
2346{
2347 struct zx29_dmarx_data *dmarx = &zup->dmarx;
2348 struct dma_chan *rxchan = dmarx->chan;
2349 struct zx29_sgbuf *sgbuf = zup->curr_sg;
2350 size_t pending;
2351 struct dma_tx_state state;
2352 enum dma_status dmastat;
2353
2354 dma_peripheral_id rx_id = uart_get_rx_dma_peripheral_id(zup);
2355 uint32_t ris_status = UART_GET_RIS(&zup->port);
2356
2357 //rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
2358 pending = sgbuf->sg.length - zx29_dma_get_transfer_num(rx_id);
2359 //printk("---zx29_uart_rt_dma, pending:%d, residue:%d\n", pending, state.residue);
2360 if(ris_status & (UART_OEIS | UART_BEIS | UART_PEIS | UART_FEIS)){
2361 if(ris_status & UART_OEIS){
2362 zup->port.icount.overrun++;
2363 // if(!uart_console(&zup->port))
2364 //BUG_ON(1);
2365 }
2366 if(ris_status & UART_BEIS)
2367 zup->port.icount.brk++;
2368 if(ris_status & UART_PEIS)
2369 zup->port.icount.parity++;
2370 if(ris_status & UART_FEIS)
2371 zup->port.icount.frame++;
2372
2373 UART_PUT_ICR(&zup->port, (UART_OEIS | UART_BEIS | UART_PEIS | UART_FEIS));
2374 }
2375
2376 if(zx29_dma_rx_running(zup)){
2377 /*
2378 * Pause the transfer so we can trust the current counter,
2379 * do this before we pause the block, else we may
2380 * overflow the FIFO.
2381 */
2382 zup->dmacr &= ~UART_RXDMAE;
2383 UART_PUT_DMACR(&zup->port,zup->dmacr);
2384 zx29_dma_stop(rx_id);
2385 //rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
2386 //printk( "uart%d unable to pause DMA transfer\n", zup->port.line);
2387 //dmastat = rxchan->device->device_tx_status(rxchan,
2388 // dmarx->cookie, &state);
2389 // dmastat = zx29_dma_get_status();//Normally,this value is insignificance.
2390
2391 //zx29_dma_force_stop(rx_id);
2392 //dmaengine_terminate_all(rxchan);
2393
2394 /* Disable RX DMA - incoming data will wait in the FIFO */
2395 zup->dmarx.running = false;
2396 zup->dmarx.used = false;
2397 zup->curr_sg = zup->sg2tty = NULL;
2398 zup->sg2tty_len = 0;
2399 zup->imr |= (UART_RTIM|UART_RXIM);
2400 UART_PUT_IMSC(&zup->port, zup->imr);
2401 pending = sgbuf->sg.length - zx29_dma_get_transfer_num(rx_id);//state.residue;
2402
2403 //printk("---zx29_uart_rt_dma, after stop pending:%d, residue:%d\n", pending, state.residue);
2404 BUG_ON(pending > ZX29_DMA_BUFFER_SIZE);
2405 /* Then we terminate the transfer - we now know our residue */
2406 //dmaengine_terminate_all(rxchan);
2407
2408 dmarx->use_buf_b = !dmarx->use_buf_b;
2409 wmb();
2410 /*
2411 * This will take the chars we have so far and insert
2412 * into the framework.
2413 */
2414 test_uart_static(zup->port.line, NULL, 0, 5);
2415 zx29_uart_dma_rx_chars(zup, pending, sgbuf, true, flags);
2416 }
2417
2418#if 0
2419//printk("rt dma\n");
2420 /* Switch buffer & re-trigger DMA job */
2421 if (zx29_dma_rx_trigger_dma(zup)) {
2422 printk("zx29_dma_rx_trigger_dma fail,uart:%d\n", zup->port.line);
2423 zup->imr |= UART_RXIM;
2424 UART_PUT_IMSC(&zup->port, zup->imr);
2425 }
2426#if RX_DMA_WORK
2427 //printk("add timer\n");
2428 else{
2429 //mod_timer(&(zup->rx_dma_timer), jiffies + msecs_to_jiffies(RX_DMA_TIMEOUT));
2430 uart_mod_timer(zup, flags);
2431 zup->pre_pending = 0;
2432 zup->work_state = true;
2433 zup->dmarx.used = true;
2434 }
2435#endif
2436
2437#endif
2438
2439}
2440char g_fifo_residue_buf[5][4];
2441char g_fifo_residue_all[5][20];
2442unsigned char g_fifo_cnt[5];
2443static void zx29_uart_rx_dma_timeout(struct timer_list *t)
2444{
2445 struct zx29_uart_port *zup = from_timer(zup, t, rx_dma_timer);
2446
2447 struct zx29_dmarx_data *dmarx = &zup->dmarx;
2448 static bool dma_timeout_flag = false;
2449 size_t pending, tmp_len;
2450 uint32_t ris_status = 0;
2451 int cancel_timer = 0;
2452 int sg_idx = (dmarx->use_buf_b ? 1 : 0);
2453
2454 unsigned long flags;
2455 struct zx29_sgbuf *sgbuf = NULL;
2456 int uart_id = zup->port.line;
2457 if(!zx29_dma_rx_running(zup))
2458 //printk("---uart_rx_dma_timeout enter, dma stopped\n");
2459 return;
2460 raw_spin_lock_irqsave(&zup->port.lock, flags);
2461 if(zup->port_close || (zup->curr_sg == NULL)){
2462 raw_spin_unlock_irqrestore(&zup->port.lock, flags);
2463 return;
2464 }
2465 //rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
2466 if(zup->sg2tty) {//dma complete now, later check again
2467 raw_spin_unlock_irqrestore(&zup->port.lock, flags);
2468 test_uart_static(zup->port.line, NULL, 0, 14);
2469 mod_timer(&(zup->rx_dma_timer), jiffies + RX_DMA_TIMEOUT);
2470 return;
2471 }
2472 sgbuf = zup->curr_sg;
2473 dma_peripheral_id rx_id = uart_get_rx_dma_peripheral_id(zup);
2474 pending = sgbuf->sg.length - zx29_dma_get_transfer_num(rx_id);
2475 //pending = sgbuf->sg.length - zx29_dma_get_transfer_num(rx_id);
2476 //printk("---uart_rx_dma_timeout enter,sg.length:%d, pending:%d, state.residue:%d\n", sgbuf->sg.length, pending, state.residue);
2477 if(pending == zup->pre_pending){
2478 int fr = UART_GET_FR(&zup->port);
2479 //if RXBUSY,means data come again
2480
2481 if((fr & UART_FR_RXBUSY)){
2482
2483 uart_mod_timer(zup, &flags);
2484 test_uart_static(zup->port.line, NULL, 0, 12);
2485 goto deal_end;
2486
2487 }
2488
2489 ris_status = UART_GET_RIS(&zup->port);
2490
2491 if(ris_status & (UART_OEIS | UART_BEIS | UART_PEIS | UART_FEIS)){
2492 if(ris_status & UART_OEIS){
2493 zup->port.icount.overrun++;
2494 g_uart_overrun[uart_id] = 1;
2495 test_uart_static(zup->port.line, NULL, 0, 19);
2496 //if(!uart_console(&zup->port))
2497 // BUG_ON(1);
2498 }
2499 if(ris_status & UART_BEIS)
2500 zup->port.icount.brk++;
2501 if(ris_status & UART_PEIS)
2502 zup->port.icount.parity++;
2503 if(ris_status & UART_FEIS)
2504 zup->port.icount.frame++;
2505
2506 UART_PUT_ICR(&zup->port, (UART_OEIS | UART_BEIS | UART_PEIS | UART_FEIS));
2507 }
2508
2509 zup->dmacr &= ~UART_RXDMAE;
2510 UART_PUT_DMACR(&zup->port,zup->dmacr);
2511 zx29_dma_stop(rx_id);
2512 zup->dmarx.running = false;
2513 zup->dmarx.used = false;
2514 tmp_len = sgbuf->sg.length - zx29_dma_get_transfer_num(rx_id);
2515 if(tmp_len != pending){
2516 pending = tmp_len;
2517 }
2518 dmarx->use_buf_b = !dmarx->use_buf_b;
2519 wmb();
2520 if(zup->uart_power_mode){
2521 int i;
2522 for(i= 0;i < 3;i++){
2523 fr = UART_GET_FR(&zup->port);
2524 if((fr & UART_FR_RXFE) == 0){
2525 g_fifo_residue_buf[uart_id][i] = UART_GET_CHAR(&zup->port) | UART_DUMMY_DR_RX;
2526 g_fifo_residue_all[uart_id][g_fifo_cnt[uart_id]++] = g_fifo_residue_buf[uart_id][i];
2527 if(g_fifo_cnt[uart_id] >= 20) g_fifo_cnt[uart_id] = 0;
2528 }
2529 else
2530 break;
2531 }
2532 if(i){
2533 g_fifo_residue_all[uart_id][g_fifo_cnt[uart_id]++]=i;
2534 if(g_fifo_cnt[uart_id] >= 20) g_fifo_cnt[uart_id] = 0;
2535 }
2536
2537 //zup->sg2tty = sgbuf;
2538 //when app ctrl sleep ,always start dma receive
2539 if(zup->sleep_state == 0){
2540 //now start dma again
2541 if (zx29_dma_rx_trigger_dma(zup)) {
2542 printk("rx_dma_chars RXDMA start fail\n");
2543 zup->imr |= UART_RXIM;
2544 UART_PUT_IMSC(&zup->port,zup->imr);
2545 }else{
2546 uart_mod_timer(zup, &flags);
2547 zup->pre_pending = 0;
2548 zup->dmarx.used = true;
2549 zup->work_state = true;
2550 }
2551 }
2552 if(pending || (i > 0)){
2553 test_uart_static(zup->port.line, NULL, 0, 13);
2554 zx29_uart_deal_dma_fifo_rx_chars(zup, pending, sgbuf, &flags, g_fifo_residue_buf[uart_id],i);
2555 }
2556
2557 }else{
2558 //for normal mode, dma start only on rx busy after timeout came
2559 if(pending || (( fr & UART_FR_RXFE) == 0)){
2560 test_uart_static(zup->port.line, NULL, 0, 13);
2561 zx29_uart_dma_rx_chars(zup, pending, sgbuf, true, &flags);
2562 }
2563 zup->imr |= (UART_RTIM|UART_RXIM);
2564 UART_PUT_IMSC(&zup->port, zup->imr);
2565 zup->pre_pending = 0;
2566 zup->work_state = false;
2567 if((UART_GET_RIS(&zup->port) & (UART_RXIS | UART_RTIS)) ||
2568 (UART_GET_FR(&zup->port) & UART_FR_RXBUSY)){
2569 zup->imr &= ~(UART_RXIM);
2570 UART_PUT_IMSC(&zup->port, zup->imr);
2571
2572 if (zx29_dma_rx_trigger_dma(zup)) {
2573 printk("rx_dma_chars RXDMA start fail\n");
2574 zup->imr |= (UART_RTIM|UART_RXIM);
2575 UART_PUT_IMSC(&zup->port,zup->imr);
2576 }else{
2577 uart_mod_timer(zup, &flags);
2578 zup->pre_pending = 0;
2579 zup->dmarx.used = true;
2580 zup->work_state = true;
2581 UART_PUT_ICR(&zup->port,(UART_RTIS|UART_RXIS));
2582 }
2583 }
2584
2585 }
2586deal_end:
2587
2588 raw_spin_unlock_irqrestore(&zup->port.lock, flags);
2589 }else{
2590 raw_spin_unlock_irqrestore(&zup->port.lock, flags);
2591 zup->pre_pending = pending;
2592 mod_timer(&(zup->rx_dma_timer), jiffies + RX_DMA_TIMEOUT);
2593 //uart_mod_timer(zup, &flags);
2594 }
2595
2596
2597}
2598enum hrtimer_restart zx29_uart_rx_dma_hrtimeout(struct hrtimer *t)
2599{
2600 struct zx29_uart_port *zup = from_timer(zup, t, rx_dma_hrtimer);
2601 struct zx29_dmarx_data *dmarx = &zup->dmarx;
2602 static bool dma_timeout_flag = false;
2603 size_t pending, tmp_len;
2604 uint32_t ris_status = 0;
2605 int cancel_timer = 0;
2606 int sg_idx = (dmarx->use_buf_b ? 1 : 0);
2607 int uart_id = zup->port.line;
2608 unsigned long flags;
2609 struct zx29_sgbuf *sgbuf = NULL;
2610 if(!zx29_dma_rx_running(zup))
2611 return HRTIMER_NORESTART;
2612 raw_spin_lock_irqsave(&zup->port.lock, flags);
2613 if(zup->port_close || (zup->curr_sg == NULL)){
2614 raw_spin_unlock_irqrestore(&zup->port.lock, flags);
2615 return HRTIMER_NORESTART;
2616 }
2617 if(zup->sg2tty) {//dma complete now, later check again
2618 raw_spin_unlock_irqrestore(&zup->port.lock, flags);
2619 test_uart_static(zup->port.line, NULL, 0, 14);
2620 hrtimer_forward_now(&zup->rx_dma_hrtimer, g_hr_interval);
2621 return HRTIMER_RESTART;
2622 }
2623 if(zup->enter_suspend){
2624 raw_spin_unlock_irqrestore(&zup->port.lock, flags);
2625 test_uart_static(zup->port.line, NULL, 0, 15);
2626 hrtimer_forward_now(&zup->rx_dma_hrtimer, g_hr_interval);
2627 return HRTIMER_RESTART;
2628 }
2629 sgbuf = zup->curr_sg;
2630 dma_peripheral_id rx_id = uart_get_rx_dma_peripheral_id(zup);
2631 pending = sgbuf->sg.length - zx29_dma_get_transfer_num(rx_id);
2632 if((pending == zup->pre_pending)) {
2633 int fr = UART_GET_FR(&zup->port);
2634 if((fr & UART_FR_RXBUSY)){
2635 hrtimer_forward_now(&zup->rx_dma_hrtimer, g_hr_interval);
2636 test_uart_static(zup->port.line, NULL, 0, 12);
2637 goto deal_end;
2638 }
2639 ris_status = UART_GET_RIS(&zup->port);
2640 if(ris_status & (UART_OEIS | UART_BEIS | UART_PEIS | UART_FEIS)){
2641 if(ris_status & UART_OEIS){
2642 zup->port.icount.overrun++;
2643 g_uart_overrun[uart_id] = 1;
2644 test_uart_static(zup->port.line, NULL, 0, 19);
2645 }
2646 if(ris_status & UART_BEIS)
2647 zup->port.icount.brk++;
2648 if(ris_status & UART_PEIS)
2649 zup->port.icount.parity++;
2650 if(ris_status & UART_FEIS)
2651 zup->port.icount.frame++;
2652 UART_PUT_ICR(&zup->port, (UART_OEIS | UART_BEIS | UART_PEIS | UART_FEIS));
2653 }
2654 zup->dmacr &= ~UART_RXDMAE;
2655 UART_PUT_DMACR(&zup->port,zup->dmacr);
2656 zx29_dma_stop(rx_id);
2657 zup->dmarx.running = false;
2658 zup->dmarx.used = false;
2659 tmp_len = sgbuf->sg.length - zx29_dma_get_transfer_num(rx_id);
2660 if(tmp_len != pending){
2661 pending = tmp_len;
2662 }
2663 dmarx->use_buf_b = !dmarx->use_buf_b;
2664 wmb();
2665 if(zup->uart_power_mode){
2666 int i;
2667 for(i= 0;i < 3;i++){
2668 fr = UART_GET_FR(&zup->port);
2669 if((fr & UART_FR_RXFE) == 0){
2670 g_fifo_residue_buf[uart_id][i] = UART_GET_CHAR(&zup->port) | UART_DUMMY_DR_RX;
2671 g_fifo_residue_all[uart_id][g_fifo_cnt[uart_id]++] = g_fifo_residue_buf[uart_id][i];
2672 if(g_fifo_cnt[uart_id] >= 20) g_fifo_cnt[uart_id] = 0;
2673 }
2674 else
2675 break;
2676 }
2677 if(i){
2678 g_fifo_residue_all[uart_id][g_fifo_cnt[uart_id]++]=i;
2679 if(g_fifo_cnt[uart_id] >= 20) g_fifo_cnt[uart_id] = 0;
2680 }
2681 if(zup->sleep_state == 0){
2682 if (zx29_dma_rx_trigger_dma(zup)) {
2683 printk("rx_dma_chars RXDMA start fail\n");
2684 zup->imr |= UART_RXIM;
2685 UART_PUT_IMSC(&zup->port,zup->imr);
2686 }else{
2687 hrtimer_forward_now(&zup->rx_dma_hrtimer, g_hr_interval);
2688 zup->pre_pending = 0;
2689 zup->dmarx.used = true;
2690 zup->work_state = true;
2691 }
2692 }
2693 if(pending || (i > 0)){
2694 test_uart_static(zup->port.line, NULL, 0, 13);
2695 zx29_uart_deal_dma_fifo_rx_chars(zup, pending, sgbuf, &flags, g_fifo_residue_buf[uart_id],i);
2696 }
2697 }else{
2698 if(pending || (( fr & UART_FR_RXFE) == 0)){
2699 test_uart_static(zup->port.line, NULL, 0, 13);
2700 zx29_uart_dma_rx_chars(zup, pending, sgbuf, true, &flags);
2701 printk("at pending %d.\n",pending);
2702 }
2703 zup->imr |= (UART_RTIM|UART_RXIM);
2704 UART_PUT_IMSC(&zup->port, zup->imr);
2705 zup->pre_pending = 0;
2706 zup->work_state = false;
2707 if((UART_GET_RIS(&zup->port) & (UART_RXIS | UART_RTIS)) ||
2708 (UART_GET_FR(&zup->port) & UART_FR_RXBUSY)){
2709 zup->imr &= ~(UART_RXIM);
2710 UART_PUT_IMSC(&zup->port, zup->imr);
2711 if (zx29_dma_rx_trigger_dma(zup)) {
2712 printk("rx_dma_chars RXDMA start fail\n");
2713 zup->imr |= (UART_RTIM|UART_RXIM);
2714 UART_PUT_IMSC(&zup->port,zup->imr);
2715 }else{
2716 hrtimer_forward_now(&zup->rx_dma_hrtimer, g_hr_interval);
2717 zup->pre_pending = 0;
2718 zup->dmarx.used = true;
2719 zup->work_state = true;
2720 UART_PUT_ICR(&zup->port,(UART_RTIS|UART_RXIS));
2721 }
2722 }
2723 }
2724deal_end:
2725 raw_spin_unlock_irqrestore(&zup->port.lock, flags);
2726 return HRTIMER_RESTART;
2727 }else{
2728 raw_spin_unlock_irqrestore(&zup->port.lock, flags);
2729 zup->pre_pending = pending;
2730 hrtimer_forward_now(&zup->rx_dma_hrtimer, g_hr_interval);
2731 test_uart_static(zup->port.line, NULL, zup->pre_pending, 22);
2732 return HRTIMER_RESTART;
2733 }
2734}
2735enum hrtimer_restart zx29_uart_rx_dma_hrtimeout_cyclic(struct hrtimer *t)
2736{
2737 struct zx29_uart_port *zup = from_timer(zup, t, rx_dma_hrtimer);
2738 struct zx29_dmarx_data *dmarx = &zup->dmarx;
2739 struct dma_chan *rxchan = dmarx->chan;
2740 size_t pending, tmp_len;
2741 uint32_t ris_status = 0;
2742 unsigned long flags;
2743 struct zx29_sgbuf *sgbuf = NULL;
2744 int uart_id = zup->port.line;
xf.li1867bfa2024-08-20 02:32:16 -07002745
2746
xf.li2f424182024-08-20 00:47:34 -07002747 if(!zx29_dma_rx_running(zup))
2748 return HRTIMER_NORESTART;
xf.li1867bfa2024-08-20 02:32:16 -07002749 raw_spin_lock_irqsave(&zup->port.lock, flags);
2750
2751 if((uart_dma_cycle[zup->port.line].cnt_callback > 0) || (uart_dma_cycle[zup->port.line+3].cnt_callback > 0)){
2752 test_uart_static(zup->port.line, NULL, uart_dma_cycle[zup->port.line].used, 46);
2753 raw_spin_unlock_irqrestore(&zup->port.lock, flags);
xf.li2f424182024-08-20 00:47:34 -07002754 return HRTIMER_NORESTART;
2755 }
2756
xf.li1867bfa2024-08-20 02:32:16 -07002757 if(!uart_dma_cycle[zup->port.line].used)
2758 uart_id = uart_id + 3;
2759
xf.li2f424182024-08-20 00:47:34 -07002760 sgbuf = &uart_dma_cycle[uart_id].sgbuf[uart_dma_cycle[uart_id].flg_enter_th];
2761 if(zup->port_close || (sgbuf == NULL)){
xf.li1867bfa2024-08-20 02:32:16 -07002762 raw_spin_unlock_irqrestore(&zup->port.lock, flags);
2763 return HRTIMER_NORESTART;
xf.li2f424182024-08-20 00:47:34 -07002764 }
2765 if(zup->sema_cyclic.count > 0){
2766 printk("uart has th not deal.\n");
xf.li1867bfa2024-08-20 02:32:16 -07002767 //test_uart_static(zup->port.line, NULL, uart_id, 11);
2768 raw_spin_unlock_irqrestore(&zup->port.lock, flags);
2769 hrtimer_forward_now(&zup->rx_dma_hrtimer, g_hr_interval);
xf.li2f424182024-08-20 00:47:34 -07002770 return HRTIMER_RESTART;
2771 }
xf.li1867bfa2024-08-20 02:32:16 -07002772
xf.li2f424182024-08-20 00:47:34 -07002773 if((zup->sg2tty)){//dma not complete now, later check again
2774 printk("dmath_cyclic not end.\n");
xf.li1867bfa2024-08-20 02:32:16 -07002775 raw_spin_unlock_irqrestore(&zup->port.lock, flags);
xf.li2f424182024-08-20 00:47:34 -07002776 test_uart_static(zup->port.line, NULL, 0, 14);
2777 hrtimer_forward_now(&zup->rx_dma_hrtimer, g_hr_interval);
2778 return HRTIMER_RESTART;
2779 }
xf.li1867bfa2024-08-20 02:32:16 -07002780 if(zup->enter_suspend || uart_dma_cycle[uart_id].enter_throttle){
2781 raw_spin_unlock_irqrestore(&zup->port.lock, flags);
xf.li2f424182024-08-20 00:47:34 -07002782 test_uart_static(zup->port.line, NULL, 0, 15);
2783 hrtimer_forward_now(&zup->rx_dma_hrtimer, g_hr_interval);
2784 return HRTIMER_RESTART;
2785 }
2786 dma_peripheral_id rx_id = uart_get_rx_dma_peripheral_id(zup);
2787 pending = sgbuf->sg.length - zx29_dma_get_transfer_num(rx_id);
xf.li1867bfa2024-08-20 02:32:16 -07002788 if(((pending == zup->pre_pending) && pending) || uart_dma_cycle[uart_id].from_resume
2789 || uart_dma_cycle[uart_id].from_unthrottle){
xf.li2f424182024-08-20 00:47:34 -07002790 uart_dma_cycle[uart_id].from_resume = 0;
xf.li1867bfa2024-08-20 02:32:16 -07002791 uart_dma_cycle[uart_id].from_unthrottle = false;
xf.li2f424182024-08-20 00:47:34 -07002792#if 0
2793 if(uart_dma_cycle[uart_id].flg_enter_th == 0)
2794 uart_dma_cycle[uart_id].flg_enter_to = 4;
2795 else
2796 uart_dma_cycle[uart_id].flg_enter_to = uart_dma_cycle[uart_id].flg_enter_th - 1;
2797 struct zx29_sgbuf *sgbuf_tmp = NULL;
2798 sgbuf_tmp = &uart_dma_cycle[uart_id].sgbuf[uart_dma_cycle[uart_id].flg_enter_to];
2799 test_uart_static(zup->port.line, NULL, 0, 61);
2800 if (sgbuf->sg.dma_address != (zx29_dma_cur_dst(rx_id)&0xfffff000)){
2801 if(sgbuf_tmp->sg.dma_address != ((zx29_dma_cur_dst(rx_id)&0xfffff000)-0x1000)){
2802 printk("uart lose dma isr enter self resume.\n");
2803 up(&zup->sema_cyclic);
2804 spin_unlock_irqrestore(&zup->port.lock, flags);
2805 return;
2806 }
2807 }
2808 #endif
2809 int fr = UART_GET_FR(&zup->port);
2810 if((fr & UART_FR_RXBUSY)){
2811 hrtimer_forward_now(&zup->rx_dma_hrtimer, g_hr_interval);
2812 test_uart_static(zup->port.line, NULL, 0, 12);
2813 goto deal_end;
2814 }
2815 ris_status = UART_GET_RIS(&zup->port);
2816 if(ris_status & (UART_OEIS | UART_BEIS | UART_PEIS | UART_FEIS)){
2817 if(ris_status & UART_OEIS){
2818 zup->port.icount.overrun++;
2819 uart_dma_cycle[uart_id].flg_overrun = 1;
2820 }
2821 if(ris_status & UART_BEIS)
2822 zup->port.icount.brk++;
2823 if(ris_status & UART_PEIS)
2824 zup->port.icount.parity++;
2825 if(ris_status & UART_FEIS)
2826 zup->port.icount.frame++;
2827 UART_PUT_ICR(&zup->port, (UART_OEIS | UART_BEIS | UART_PEIS | UART_FEIS));
2828 printk("error in uart%d: fe %u ,be %u pe %u.\n",zup->port.line,zup->port.icount.frame,
2829 zup->port.icount.brk,zup->port.icount.parity);
2830 }
2831 zup->dmacr &= ~UART_RXDMAE;
2832 UART_PUT_DMACR(&zup->port,zup->dmacr);
2833 dmaengine_terminate_all(rxchan);
xf.li2f424182024-08-20 00:47:34 -07002834 zup->dmarx.running = false;
2835 zup->dmarx.used = false;
xf.li1867bfa2024-08-20 02:32:16 -07002836 tmp_len = sgbuf->sg.length - zx29_dma_get_transfer_num(rx_id);
2837 if(tmp_len != pending){
2838 pending = tmp_len;
2839 //test_uart_static(zup->port.line, NULL, tmp_len, 48);
2840 }
xf.li2f424182024-08-20 00:47:34 -07002841 wmb();
2842 int i = 0;
2843 for(i= 0;i < 3;i++){
2844 fr = UART_GET_FR(&zup->port);
2845 if((fr & UART_FR_RXFE) == 0){
xf.li1867bfa2024-08-20 02:32:16 -07002846 g_fifo_residue_buf[zup->port.line][i] = UART_GET_CHAR(&zup->port) | UART_DUMMY_DR_RX;
2847 g_fifo_residue_all[zup->port.line][g_fifo_cnt[zup->port.line]++] = g_fifo_residue_buf[zup->port.line][i];
2848 if(g_fifo_cnt[zup->port.line] >= 20) g_fifo_cnt[zup->port.line] = 0;
xf.li2f424182024-08-20 00:47:34 -07002849 }
2850 else
2851 break;
2852 }
2853 if(i){
xf.li1867bfa2024-08-20 02:32:16 -07002854 g_fifo_residue_all[zup->port.line][g_fifo_cnt[zup->port.line]++]=i;
2855 if(g_fifo_cnt[zup->port.line] >= 20) g_fifo_cnt[zup->port.line] = 0;
xf.li2f424182024-08-20 00:47:34 -07002856 }
2857 if (zx29_dma_rx_trigger_dma_use_dma_cyclic(zup)) {
2858 printk("rx_dma_chars RXDMA start fail\n");
2859 zup->imr |= UART_RXIM;
2860 UART_PUT_IMSC(&zup->port,zup->imr);
2861 }else{
2862 hrtimer_forward_now(&zup->rx_dma_hrtimer, g_hr_interval);
xf.li1867bfa2024-08-20 02:32:16 -07002863 test_uart_static(zup->port.line, NULL, (pending+i), 49);
xf.li2f424182024-08-20 00:47:34 -07002864 zup->pre_pending = 0;
2865 zup->dmarx.used = true;
2866 zup->work_state = true;
2867 }
2868 if((pending && (pending != 4096)) || (i > 0)){
xf.li1867bfa2024-08-20 02:32:16 -07002869 zx29_uart_deal_dma_fifo_rx_chars_cyclic(zup, pending, sgbuf, &flags, g_fifo_residue_buf[zup->port.line],i);
xf.li2f424182024-08-20 00:47:34 -07002870 }
2871 uart_dma_cycle[uart_id].cnt_th = 0;
2872 uart_dma_cycle[uart_id].cnt_callback=0;
2873deal_end:
xf.li1867bfa2024-08-20 02:32:16 -07002874 raw_spin_unlock_irqrestore(&zup->port.lock, flags);
xf.li2f424182024-08-20 00:47:34 -07002875 return HRTIMER_RESTART;
2876 }else{
xf.li1867bfa2024-08-20 02:32:16 -07002877 raw_spin_unlock_irqrestore(&zup->port.lock, flags);
xf.li2f424182024-08-20 00:47:34 -07002878 zup->pre_pending = pending;
2879 hrtimer_forward_now(&zup->rx_dma_hrtimer, g_hr_interval);
2880 test_uart_static(zup->port.line, NULL, zup->pre_pending, 22);
2881 return HRTIMER_RESTART;
2882 }
2883}
2884#endif
2885
2886
2887static void zx29_uart_modem_status(struct zx29_uart_port *zup)
2888{
2889 unsigned int status, delta;
2890
2891 status = UART_GET_FR(&zup->port)& UART_FR_MODEM_ANY;
2892
2893 delta = status ^ zup->old_status;
2894 zup->old_status = status;
2895
2896 if (!delta)
2897 return;
2898
2899 if (delta & UART_FR_DCD)
2900 uart_handle_dcd_change(&zup->port, status & UART_FR_DCD);
2901
2902 if (delta & UART_FR_DSR)
2903 zup->port.icount.dsr++;
2904
2905 if (delta & UART_FR_CTS)
2906 uart_handle_cts_change(&zup->port, status & UART_FR_CTS);
2907
2908 wake_up_interruptible(&zup->port.state->port.delta_msr_wait);
2909}
2910
2911/****************************************************************************/
2912static irqreturn_t zx29_uart_interrupt(int irq, void *dev_id)
2913{
2914 struct uart_port *port = (struct uart_port *)dev_id;
2915 struct zx29_uart_port *zup = container_of(port, struct zx29_uart_port, port);
2916 unsigned long flags;
2917 unsigned int status,ris, pass_counter = 256;
2918 int handled = 0;
2919 int uart_id = zup->port.line;
2920 spin_lock_irqsave(&zup->port.lock, flags);
2921 status = UART_GET_MIS(port) & zup->imr;
2922 ris = UART_GET_RIS(port);
2923 if (status) {
2924 do {
2925 UART_PUT_ICR(port,(status & ~(UART_TXIS|UART_RTIS|UART_RXIS)));
2926 if(uart_console(&zup->port)){
2927 if (status & (UART_RTIS|UART_RXIS))
2928 zx29_uart_rx_chars(zup);
2929 }else{
2930#ifdef CONFIG_CPU_IDLE
2931 zup->rxd_int_depth = 0;
2932#endif
2933 if (status & (UART_RXIS)){
2934#if CONFIG_SERIAL_ZX29_DMA
2935 if(ris & UART_OEIS){
2936 zup->port.icount.overrun++;
2937 g_uart_overrun[uart_id] = 8;
2938 test_uart_static(zup->port.line, NULL, 0, 21);
2939 //if(!uart_console(&zup->port))
2940 // BUG_ON(1);
2941 }
2942 if (zx29_dma_rx_used(zup)){
2943 UART_PUT_ICR(port,UART_RXIS);
2944 if(!(zup->imr & UART_RTIM)){
2945 zup->imr |= UART_RTIM;
2946 UART_PUT_IMSC(port,zup->imr);
2947 }
2948
2949 test_uart_static(port->line, NULL, 0, 8);
2950 uart_mod_timer(zup, &flags);
2951
2952 }else{
2953 test_uart_static(port->line, NULL, 0, 1);
2954
2955 zup->imr &= ~UART_RXIM;
2956 UART_PUT_IMSC(&zup->port,zup->imr);
2957 zx29_uart_rx_dma_chars(zup, &flags);
2958
2959 zup->dmarx.used = true;
2960 //when RX&RT comes both, we trigger dma and add timer,so clear RT,waiting the timer
2961 if(status & (UART_RTIS))
2962 status &= ~UART_RTIS;
2963 }
2964#else
2965 zx29_uart_rx_chars(zup);
2966#endif
2967 }
2968
2969 if (status & (UART_RTIS)){
2970#if CONFIG_SERIAL_ZX29_DMA
2971 if(!zx29_dma_rx_running(zup)){
2972 test_uart_static(port->line, NULL, 0, 2);
2973 zx29_uart_rx_timeout_chars(zup, &flags);
2974 }else{
2975 UART_PUT_ICR(port, UART_RTIS);
2976 test_uart_static(port->line, NULL, 0, 4);
2977 zx29_uart_rt_dma(zup, &flags);
2978 }
2979#else
2980 zx29_uart_rx_chars(zup);
2981#endif
2982 }
2983 }
2984
2985 if (status & (UART_DSRMIS|UART_DCDMIS|UART_CTSMIS|UART_RIMIS))
2986 zx29_uart_modem_status(zup);
2987
2988 if (status & UART_TXIS)
2989 zx29_uart_tx_chars(zup);
2990
2991 if (pass_counter-- == 0)
2992 break;
2993
2994 status = UART_GET_MIS(port);
2995 } while (status != 0);
2996 handled = IRQ_HANDLED;
2997 }
2998 spin_unlock_irqrestore(&zup->port.lock, flags);
2999
3000 return IRQ_RETVAL(handled);
3001}
3002
3003#if CONFIG_SERIAL_ZX29_DMA
3004extern bool zx29_dma_filter_fn(struct dma_chan *chan, void *param);
3005static void uart_dma_init(struct zx29_uart_port *zup)
3006{
3007 int i=0;
3008 struct dma_chan *chan = NULL;
3009
3010 atomic_set(&zup->dmarx.count, 1);
3011 atomic_set(&zup->dmatx.count, 1);
3012#if 1
3013 if(zup->port.line == UART0)
3014 {
3015 zup->dmatx.tx_def.dest_addr = (unsigned int)(ZX_UART0_BASE +zx29_UART_DR);
3016 }
3017 else if(zup->port.line == UART1)
3018 {
3019 zup->dmatx.tx_def.dest_addr = (unsigned int)(ZX_UART1_BASE+zx29_UART_DR);
3020 }
3021 else if(zup->port.line == UART2)
3022 {
3023 zup->dmatx.tx_def.dest_addr = (unsigned int)(ZX_UART2_BASE+zx29_UART_DR);
3024 }
3025
3026 zup->dmatx.tx_def.dma_control.tran_mode = TRAN_MEM_TO_PERI;
3027 zup->dmatx.tx_def.dma_control.irq_mode = DMA_ALL_IRQ_ENABLE;
3028 zup->dmatx.tx_def.dma_control.src_burst_size = DMA_BURST_SIZE_8BIT;
3029 zup->dmatx.tx_def.dma_control.src_burst_len = DMA_BURST_LEN_4;
3030 zup->dmatx.tx_def.dma_control.dest_burst_size = DMA_BURST_SIZE_8BIT;
3031 zup->dmatx.tx_def.dma_control.dest_burst_len = DMA_BURST_LEN_4;
3032
3033 dma_cap_mask_t mask;
3034 dma_cap_zero(mask);
3035 dma_cap_set(DMA_SLAVE, mask);
3036
3037 if(zup->port.line == UART0)
3038 {
3039 chan = dma_request_channel(mask, zx29_dma_filter_fn, (void*)DMA_CH_UART0_TX);
3040 }
3041 else if(zup->port.line == UART1)
3042 {
3043 chan = dma_request_channel(mask, zx29_dma_filter_fn, (void*)DMA_CH_UART1_TX);
3044 }
3045 else if(zup->port.line == UART2)
3046 {
3047 chan = dma_request_channel(mask, zx29_dma_filter_fn, (void*)DMA_CH_UART2_TX);
3048 }
3049 if(!chan){
3050 printk("UART%d DMA TX channel request fail.\n", zup->port.line);
3051 return;
3052 }
3053 zup->dmatx.chan = chan;
3054
3055
3056
3057 for(i=0;i<UART_DMA_RX_MAX_COUNT;i++)
3058 {
3059 if(zup->port.line == UART0)
3060 {
3061 zup->dmarx.rx_def[i].src_addr = ZX_UART0_BASE+zx29_UART_DR;
3062 }
3063 else if(zup->port.line == UART1)
3064 {
3065 zup->dmarx.rx_def[i].src_addr = ZX_UART1_BASE+zx29_UART_DR;
3066 }
3067 else if(zup->port.line == UART2)
3068 {
3069 zup->dmarx.rx_def[i].src_addr = ZX_UART2_BASE+zx29_UART_DR;
3070 }
3071
3072 zup->dmarx.rx_def[i].dma_control.tran_mode = TRAN_PERI_TO_MEM;
3073 zup->dmarx.rx_def[i].dma_control.irq_mode = DMA_ALL_IRQ_ENABLE;
3074 zup->dmarx.rx_def[i].dma_control.src_burst_size = DMA_BURST_SIZE_8BIT;
3075 zup->dmarx.rx_def[i].dma_control.src_burst_len = DMA_BURST_LEN_4;
3076 zup->dmarx.rx_def[i].dma_control.dest_burst_size = DMA_BURST_SIZE_8BIT;
3077 zup->dmarx.rx_def[i].dma_control.dest_burst_len = DMA_BURST_LEN_4;
3078 }
3079
3080 zup->dmarx.rx_index = 0;
3081 chan = NULL;
3082
3083 dma_cap_zero(mask);
3084 dma_cap_set(DMA_SLAVE, mask);
3085
3086 if(zup->port.line == UART0)
3087 {
3088 chan = dma_request_channel(mask, zx29_dma_filter_fn, (void*)DMA_CH_UART0_RX);
3089 }
3090 else if(zup->port.line == UART1)
3091 {
3092 chan = dma_request_channel(mask, zx29_dma_filter_fn, (void*)DMA_CH_UART1_RX);
3093 }
3094 else if(zup->port.line == UART2)
3095 {
3096 chan = dma_request_channel(mask, zx29_dma_filter_fn, (void*)DMA_CH_UART2_RX);
3097 }
3098 if(!chan){
3099 printk("UART%d DMA RX channel request fail.\n", zup->port.line);
3100 return;
3101 }
3102 zup->dmarx.chan = chan;
3103#endif
3104}
3105
3106static int uart_dma_cycle_init(struct zx29_uart_port *zup)
3107{
3108 int ret;
3109 int uart_id = zup->port.line;
3110 uart_dma_cycle[uart_id].id = zup->port.line;
3111 int i,j;
3112 for(i=0;i<UART_DMA_CYCLE_RX_CONFIG_COUNT;i++){
3113 ret = zx29_sgbuf_init(zup->dmarx.chan, &uart_dma_cycle[uart_id].sgbuf[i],DMA_FROM_DEVICE);
3114 if(ret){
3115 printk( "init uart_dma_cycle sgbuf failed,uart: %d,ret:%d\n", zup->port.line, ret);
3116 for(j=0;j<i;j++){
3117 zx29_sgbuf_free(zup->dmarx.chan, &uart_dma_cycle[uart_id].sgbuf[j],DMA_FROM_DEVICE);
3118 }
3119 return -1;
3120 }
xf.li1867bfa2024-08-20 02:32:16 -07003121
3122 ret = zx29_sgbuf_init(zup->dmarx.chan, &uart_dma_cycle[uart_id+3].sgbuf[i],DMA_FROM_DEVICE);
3123 if(ret){
3124 printk( "init uart_dma_cycle sgbuf failed,uart: %d,ret:%d\n", (zup->port.line+3), ret);
3125 for(j=0;j<i;j++){
3126 zx29_sgbuf_free(zup->dmarx.chan, &uart_dma_cycle[uart_id+3].sgbuf[j],DMA_FROM_DEVICE);
3127 }
3128 return -1;
3129 }
xf.li2f424182024-08-20 00:47:34 -07003130 }
3131 for(i=0;i<UART_DMA_CYCLE_RX_CONFIG_COUNT;i++){
xf.li1867bfa2024-08-20 02:32:16 -07003132 if(zup->port.line == UART0){
xf.li2f424182024-08-20 00:47:34 -07003133 uart_dma_cycle[uart_id].rxdef[i].src_addr = ZX_UART0_BASE+zx29_UART_DR;
xf.li1867bfa2024-08-20 02:32:16 -07003134 uart_dma_cycle[uart_id].used = false;
3135 uart_dma_cycle[uart_id+3].rxdef[i].src_addr = ZX_UART0_BASE+zx29_UART_DR;
3136 uart_dma_cycle[uart_id+3].used = false;
3137 }
3138 else if(zup->port.line == UART1){
xf.li2f424182024-08-20 00:47:34 -07003139 uart_dma_cycle[uart_id].rxdef[i].src_addr = ZX_UART1_BASE+zx29_UART_DR;
xf.li1867bfa2024-08-20 02:32:16 -07003140 uart_dma_cycle[uart_id].used = false;
3141 uart_dma_cycle[uart_id+3].rxdef[i].src_addr = ZX_UART1_BASE+zx29_UART_DR;
3142 uart_dma_cycle[uart_id+3].used = false;
3143 }
3144 else if(zup->port.line == UART2){
xf.li2f424182024-08-20 00:47:34 -07003145 uart_dma_cycle[uart_id].rxdef[i].src_addr = ZX_UART2_BASE+zx29_UART_DR;
xf.li1867bfa2024-08-20 02:32:16 -07003146 uart_dma_cycle[uart_id].used = false;
3147 uart_dma_cycle[uart_id+3].rxdef[i].src_addr = ZX_UART2_BASE+zx29_UART_DR;
3148 uart_dma_cycle[uart_id+3].used = false;
xf.li2f424182024-08-20 00:47:34 -07003149 }
3150 uart_dma_cycle[uart_id].rxdef[i].dest_addr = (unsigned int)(uart_dma_cycle[uart_id].sgbuf[i].dma_addr);
3151 uart_dma_cycle[uart_id].rxdef[i].dma_control.tran_mode = TRAN_PERI_TO_MEM;
3152 uart_dma_cycle[uart_id].rxdef[i].dma_control.src_burst_len = DMA_BURST_LEN_4;
3153 uart_dma_cycle[uart_id].rxdef[i].count = ZX29_DMA_BUFFER_SIZE;
3154 uart_dma_cycle[uart_id].rxdef[i].dma_control.src_burst_size = DMA_BURST_SIZE_8BIT;
3155 uart_dma_cycle[uart_id].rxdef[i].dma_control.dest_burst_size = DMA_BURST_SIZE_8BIT;
3156 uart_dma_cycle[uart_id].rxdef[i].dma_control.dest_burst_len = DMA_BURST_LEN_4;
3157 uart_dma_cycle[uart_id].rxdef[i].dma_control.irq_mode = DMA_ALL_IRQ_ENABLE;
xf.li1867bfa2024-08-20 02:32:16 -07003158 uart_dma_cycle[uart_id].rxdef[i].link_addr = 1;
3159
3160 uart_dma_cycle[uart_id+3].rxdef[i].dest_addr = (unsigned int)(uart_dma_cycle[uart_id+3].sgbuf[i].dma_addr);
3161 uart_dma_cycle[uart_id+3].rxdef[i].dma_control.tran_mode = TRAN_PERI_TO_MEM;
3162 uart_dma_cycle[uart_id+3].rxdef[i].dma_control.src_burst_len = DMA_BURST_LEN_4;
3163 uart_dma_cycle[uart_id+3].rxdef[i].count = ZX29_DMA_BUFFER_SIZE;
3164 uart_dma_cycle[uart_id+3].rxdef[i].dma_control.src_burst_size = DMA_BURST_SIZE_8BIT;
3165 uart_dma_cycle[uart_id+3].rxdef[i].dma_control.dest_burst_size = DMA_BURST_SIZE_8BIT;
3166 uart_dma_cycle[uart_id+3].rxdef[i].dma_control.dest_burst_len = DMA_BURST_LEN_4;
3167 uart_dma_cycle[uart_id+3].rxdef[i].dma_control.irq_mode = DMA_ALL_IRQ_ENABLE;
3168 uart_dma_cycle[uart_id+3].rxdef[i].link_addr = 1;
3169
xf.li2f424182024-08-20 00:47:34 -07003170 }
3171 return 0;
3172}
3173static void uart_dma_cycle_deinit(struct zx29_uart_port *zup)
3174{
3175 int i;
3176 int uart_id = zup->port.line;
3177 for(i=0;i<UART_DMA_CYCLE_RX_CONFIG_COUNT;i++){
3178 zx29_sgbuf_free(zup->dmarx.chan, &uart_dma_cycle[uart_id].sgbuf[i],DMA_FROM_DEVICE);
xf.li1867bfa2024-08-20 02:32:16 -07003179 zx29_sgbuf_free(zup->dmarx.chan, &uart_dma_cycle[uart_id+3].sgbuf[i],DMA_FROM_DEVICE);
xf.li2f424182024-08-20 00:47:34 -07003180 }
3181 memset(uart_dma_cycle[uart_id].rxdef, 0, sizeof(uart_dma_cycle[uart_id].rxdef));
xf.li1867bfa2024-08-20 02:32:16 -07003182 memset(uart_dma_cycle[uart_id+3].rxdef, 0, sizeof(uart_dma_cycle[uart_id+3].rxdef));
xf.li2f424182024-08-20 00:47:34 -07003183}
3184static void uart_dma_startup(struct zx29_uart_port *zup)
3185{
3186 int ret = 0;
3187 if (!zup->dmatx.chan)
3188 {
3189 printk("tx_chan is error[%s][%d]\n",__func__,__LINE__);
3190 return;
3191 }
3192
3193 zup->dmatx.buf = kmalloc(ZX29_DMA_BUFFER_SIZE, GFP_KERNEL | __GFP_DMA);
3194 if (!zup->dmatx.buf) {
3195 printk("tx_buf is error[%s][%d]\n",__func__,__LINE__);
3196 return;
3197 }
3198
3199 sg_init_one(&zup->dmatx.sg, zup->dmatx.buf, ZX29_DMA_BUFFER_SIZE);
3200
3201 /* The DMA buffer is now the FIFO the TTY subsystem can use */
3202 zup->port.fifosize = 16;//ZX29_DMA_BUFFER_SIZE;
3203 zup->using_tx_dma = true;
3204
3205 if(!zup->uart_power_mode)
3206 {
3207 if (!zup->dmarx.chan)
3208 {
3209 printk(KERN_INFO "[%s][%d]uart_%d rx_chan is error\n",__func__,__LINE__, zup->port.line);
3210 goto skip_rx;
3211 }
3212
3213 /* Allocate and map DMA RX buffers */
3214 ret = zx29_sgbuf_init(zup->dmarx.chan, &zup->dmarx.sgbuf_a,
3215 DMA_FROM_DEVICE);
3216 if (ret) {
3217 printk(KERN_INFO "[%s][%d] uart_%d rx_buf_a is error\n",__func__,__LINE__, zup->port.line);
3218 //dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
3219 // "RX buffer A", ret);
3220 goto skip_rx;
3221 }
3222
3223 ret = zx29_sgbuf_init(zup->dmarx.chan, &zup->dmarx.sgbuf_b,
3224 DMA_FROM_DEVICE);
3225 if (ret) {
3226 printk( "failed to init DMA uart: %d RX buffer B ,ret:%d\n", zup->port.line, ret);
3227 zx29_sgbuf_free(zup->dmarx.chan, &zup->dmarx.sgbuf_a,
3228 DMA_FROM_DEVICE);
3229 goto skip_rx;
3230 }
3231
3232 zup->using_rx_dma = true;
3233 zup->sg2tty = NULL;
3234 zup->sg2tty_len = 0;
3235 zup->curr_sg = NULL;
3236#if RX_DMA_WORK
3237 timer_setup(&(zup->rx_dma_timer), zx29_uart_rx_dma_timeout, 0);
3238 hrtimer_init(&zup->rx_dma_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3239 zup->rx_dma_hrtimer.function = zx29_uart_rx_dma_hrtimeout;
3240 g_hr_interval = ktime_set(0, 1500000);
3241 zup->dmarx.running = false;
3242 zup->dmarx.used = false;
3243 zup->dmarx.use_buf_b = false;
3244 zup->dmarx.rx_index = 0;
3245
3246 zup->pre_pending = 0;
3247 zup->work_state = false;
3248
3249 zup->dma_compl_th = kthread_run(dma_complete_thread, zup, "uart_dma_compl");
3250 BUG_ON(IS_ERR(zup->dma_compl_th));
3251#endif
3252
3253skip_rx:
3254
3255 /* Turn on DMA error (RX/TX will be enabled on demand) */
3256 printk("uart_dma_startup, port:%d, ret:%d\n", zup->port.line,ret );
3257 zup->dmacr &= ~UART_DMAONERR;
3258 //zup->dmacr |= UART_DMAONERR;
3259 UART_PUT_DMACR(&zup->port, zup->dmacr);
3260 if(zup->uart_power_mode){
3261 if (zup->using_rx_dma) {
3262 //printk(KERN_INFO "[%s][%d]\n",__func__,__LINE__);
3263 if (zx29_dma_rx_trigger_dma(zup)){
3264 dev_dbg(zup->port.dev, "could not trigger initial "
3265 "RX DMA job, fall back to interrupt mode\n");
3266 }else{
3267 mod_timer(&(zup->rx_dma_timer), jiffies + RX_DMA_TIMEOUT);
3268 zup->pre_pending = 0;
3269 zup->work_state = true;
3270 }
3271 }
3272 }
3273 }
3274 else if(zup->uart_power_mode == 1)
3275 {
3276 ret = uart_dma_cycle_init(zup);
3277 if(ret){
3278 printk("uart%d dma cycle init failed,ret %d.\n",zup->port.line,ret);
3279 return;
3280 }
3281 zup->using_rx_dma = true;
3282 zup->sg2tty = NULL;
3283 zup->sg2tty_len = 0;
3284 zup->curr_sg = NULL;
3285#if RX_DMA_WORK
3286 hrtimer_init(&zup->rx_dma_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3287 zup->rx_dma_hrtimer.function = zx29_uart_rx_dma_hrtimeout_cyclic;
3288 g_hr_interval = ktime_set(0, 1500000);
3289 zup->dmarx.running = false;
3290 zup->dmarx.used = false;
3291 zup->dmarx.use_buf_b = false;
3292 zup->dmarx.rx_index = 0;
3293 zup->pre_pending = 0;
3294 zup->work_state = false;
3295 sema_init(&zup->sema_cyclic, 0);
3296 zup->dma_compl_th = kthread_run(dma_complete_thread_use_dma_cyclic, zup, "uart_dma_th_cyc");
3297 BUG_ON(IS_ERR(zup->dma_compl_th));
3298#endif
3299 printk("uart_dma_startup, port:%d, ret:%d\n", zup->port.line,ret );
3300 zup->dmacr &= ~UART_DMAONERR;
3301 UART_PUT_DMACR(&zup->port, zup->dmacr);
3302 if(zup->uart_power_mode){
3303 if (zup->using_rx_dma) {
3304 if (zx29_dma_rx_trigger_dma_use_dma_cyclic(zup)){
3305 dev_dbg(zup->port.dev, "could not trigger initial "
3306 "RX DMA job, fall back to interrupt mode\n");
3307 }else{
3308 hrtimer_start(&zup->rx_dma_hrtimer, g_hr_interval, HRTIMER_MODE_REL);
3309 //mod_timer(&(zup->rx_dma_timer), jiffies + RX_DMA_TIMEOUT);
3310 zup->pre_pending = 0;
3311 zup->work_state = true;
3312 }
3313 }
3314 }
3315 }else
3316 printk("uart%d power mode set error,dma dont startup.\n",zup->port.line);
3317}
3318
3319
3320#endif
3321
3322static irqreturn_t zx29_uart_rxd_irq(int irq, void *dev_id)
3323{
3324 struct zx29_uart_port *zup = (struct zx29_uart_port *)dev_id;
3325
3326 rxd_wake_cnt++;
3327 zup->rxd_wakeup = true;
3328 tasklet_schedule(&zup->write_wakeup);
3329 zup->rxd_int_depth = 0;
3330 return IRQ_HANDLED;//IRQ_RETVAL(retval);
3331}
3332
3333/****************************************************************************/
3334static int zx29_uart_startup(struct uart_port *port)
3335{
3336 struct zx29_uart_port *zup = container_of(port, struct zx29_uart_port, port);
3337 unsigned long flags = 0;
3338 unsigned long control = 0;
3339 int retval = 0;
3340 struct platform_device *pdev=port->private_data;
3341// struct zx29_uart_platdata *pdata = pdev->dev.platform_data;
3342 int i = 0,j = 0,iflag = 0;
3343 struct tty_struct *tty = port->state->port.tty;
3344 unsigned int ibrd, fbrd,lcr_h, old_cr;
3345 int ret=0;
3346 printk("-----zx29_uart_startup, port:%d\n", port->line);
3347 #if 0//def CONFIG_ARCH_ZX297520V3_WATCH
3348
3349 if(port->line == 0)
3350 {
3351 gpio_free(pdata->uart_txd.gpionum);
3352 gpio_free(pdata->uart_rxd.gpionum);
3353 //printk("gpio_free err err!\n");
3354 }
3355
3356 wmb();
3357
3358 #endif
3359 if(DEBUG_CONSOLE != pdev->id){
3360 char temp_buf[TASK_COMM_LEN]= {0};
3361 int th_ctrl = 0;
3362 if((strlen(get_task_comm(temp_buf,get_current())) > 0) && (strcmp(get_task_comm(temp_buf,get_current()),"at_ctl") != 0))
3363 th_ctrl = 1;
3364
3365 //app ctrl or kernel ctrl set this
3366 int kernel_ctrl = xp2xp_enable_4line();
3367 zup->uart_power_mode = (kernel_ctrl | zup->app_ctrl | th_ctrl);
3368 printk("zx29_uart%d open task is %s,power_mode is %d.\n",pdev->id, get_task_comm(temp_buf,get_current()),zup->uart_power_mode);
3369 if(zup->uart_power_mode){
3370 //pm_stay_awake(&pdev->dev);
3371 }
3372 }
3373 //when open, clear last statistic info
3374 port->icount.brk = port->icount.buf_overrun = port->icount.frame = 0;
3375 port->icount.overrun = port->icount.parity = port->icount.rng = 0;
3376 port->icount.rx = port->icount.tx = 0;
3377 /*
3378 *enable uart clock
3379 *if uart is used for console, don't need do these, these was done before
3380 */
3381 if (DEBUG_CONSOLE != port->line) {
3382 /* config uart apb_clk */
3383 clk_prepare_enable(zup->busclk);
3384 /* enable uart work clock */
3385 clk_prepare_enable(zup->wclk);
3386 }
3387
3388 /* Clear all pending error and receive interrupts */
3389 UART_PUT_ICR(port, 0xfff);
3390
3391 /* Allocate the IRQ */
3392 retval = request_irq(port->irq, zx29_uart_interrupt, 0, "uart-zx29", zup);
3393 if (retval){
3394 printk("[UART]unable to attach zx29 UART %d "
3395 "interrupt vector=%d\n", port->line, port->irq);
3396 return retval;
3397 }
3398
3399 /* set interrupt fifo level RX:1/2 Full, TX:1/2 Full */
3400#if 0//CONFIG_SERIAL_ZX29_DMA
3401 UART_PUT_IFLS(port, UART_IFLS_RX2_8|UART_IFLS_TX6_8);
3402#else
3403 UART_PUT_IFLS(port, UART_IFLS_RX2_8|UART_IFLS_TX4_8);
3404#endif
3405
3406#if 0
3407 /* Provoke TX FIFO interrupt into asserting. */
3408 control = UART_CR_UARTEN | UART_CR_TXE | UART_CR_LBE;
3409 UART_PUT_CR(port, control);
3410 UART_PUT_FBRD(port, 0);
3411 UART_PUT_IBRD(port, 1);
3412 UART_PUT_LCRH(port, 0);
3413 UART_PUT_CHAR(port, 0);
3414 while (UART_GET_FR(port) & UART_FR_TXBUSY)
3415 barrier();
3416#endif
3417 control = UART_CR_UARTEN | UART_CR_RXE | UART_CR_TXE;
3418 //console & lp_uart don't need dma
3419 if ((DEBUG_CONSOLE != port->line) && (port->line != 4)) {
3420#if CONFIG_SERIAL_ZX29_DMA
3421 UART_PUT_DMACR(port, UART_TXDMAE | UART_RXDMAE);
3422 uart_dma_startup(zup);
3423#endif
3424 }
3425
3426 tasklet_init(&zup->write_wakeup, uart_write_wakeup_task, (unsigned long) port);
3427 if((pdev->id == 0) && (zup->irq_state == 0) && (zup->uart_power_mode == 0)){
3428 ret = request_irq(zup->rxd_irq,
3429 zx29_uart_rxd_irq,
3430 0,
3431 "uart0_rxd_wake",
3432 zup);
3433 if(ret<0){
3434 panic("request uart0 rxd wake irq fail\n");
3435 }
3436 printk("--------rxd wake up interrupt ok\n");
3437 enable_irq_wake(zup->rxd_irq);
3438 zup->irq_state = 1;
3439 zup->rxd_int_depth = 1;
3440 }
3441#if 0
3442 /*configure gpio pin to UART*/
3443 if((pdata->uart_use)/*&&(port->line == UART0 )*/)
3444 {
3445 retval=gpio_request(pdata->uart_rxd.gpionum,pdata->uart_rxd.gpioname);
3446 if(retval)
3447 BUG();
3448 retval=gpio_request(pdata->uart_txd.gpionum,pdata->uart_txd.gpioname);
3449 if(retval)
3450 BUG();
3451 /*uart rxd*/
3452 zx29_gpio_config(pdata->uart_rxd.gpionum, pdata->uart_rxd.gpiofnc);
3453 if(pdata->uart_rxd.gpionum == ZX29_GPIO_121 ) {
3454 //pull up gpio121
3455 *(volatile unsigned int *)0xf843c82c |= 0xf0;
3456 }
3457 /*uart txd*/
3458 zx29_gpio_config(pdata->uart_txd.gpionum, pdata->uart_txd.gpiofnc);
3459#ifdef CONFIG_ARCH_ZX297520V3
3460 if((pdev->id != DEBUG_CONSOLE) && (pdata->uart_wakeup_enable == 1) && (zup->irq_state == 0)){
3461 zup->irq = platform_get_irq_byname(pdev, "zx29_uart_rxd_wakeup");
3462 printk(KERN_INFO"zx29_uart_startup,irq:%d,%s.%d\n",zup->irq,pdata->uart_cts.gpioname,zup->irq_state);
3463 if(zup->irq >= 0){
3464
3465 pcu_int_set_type(PCU_UART0_RXD_INT, IRQF_TRIGGER_FALLING);
3466 pcu_int_clear(PCU_UART0_RXD_INT);
3467 ret = request_irq(zup->irq, zx29_uart_rxd_irq,
3468 IRQF_ONESHOT , "uart_rxd_irq",
3469 zup);
3470 printk(KERN_INFO"zx29_uart_startup, retval:%d\n",ret);
3471 irq_set_irq_wake(zup->irq,1);
3472#ifdef CONFIG_CPU_IDLE
3473 zup->rxd_int_depth = rxd_wake_cnt = 0;
3474 zx_pm_register_callback(uart_0_pm_enter, uart_0_pm_exit);
3475 disable_irq_nosync(UART0_RXD_INT);
3476#endif
3477 zup->irq_state = 1;
3478 }else{
3479 printk("uart_startup, request wake irq fail:%d\n",zup->irq);
3480 }
3481 }
3482#endif
3483 if(pdata->uart_ctsrtsuse)
3484 {
3485 retval=gpio_request(pdata->uart_cts.gpionum,pdata->uart_cts.gpioname);
3486 if(retval)
3487 BUG();
3488
3489 retval=gpio_request(pdata->uart_rts.gpionum,pdata->uart_rts.gpioname);
3490 if(retval)
3491 BUG();
3492/*uart cts*/
3493 zx29_gpio_config(pdata->uart_cts.gpionum, pdata->uart_cts.gpiofnc);
3494/*uart rts*/
3495 zx29_gpio_config(pdata->uart_rts.gpionum, pdata->uart_rts.gpiofnc);
3496
3497 control |= (UART_CR_RTSEN |UART_CR_CTSEN );
3498 control |= UART_CR_RTS; //wl write1 for allow send
3499 }
3500 zup->autobaud = pdata->uart_abauduse ;
3501 }
3502#if 0
3503 if((pdata->uart_use)&&(port->line == UART1 ))
3504 {
3505 retval=gpio_request(pdata->uart_rx.gpionum,pdata->uart_rx.gpioname);
3506 if(retval)
3507 BUG();
3508 retval=gpio_request(pdata->uart_txd.gpionum,pdata->uart_tx.gpioname);
3509 if(retval)
3510 BUG();
3511/*uart rxd*/
3512 zx29_gpio_config(pdata->uart_rxd.gpionum, pdata->uart_rxd.gpiofnc);
3513/*uart txd*/
3514 zx29_gpio_config(pdata->uart_txdnum, pdata->uart_txdfnc);
3515
3516 if(pdata->uart_ctsrtsuse)
3517 {
3518 retval=gpio_request(pdata->uart_ctsnum,"uart1_cts");
3519 if(retval)
3520 BUG();
3521 retval=gpio_request(pdata->uart_rtsnum,"uart1_rts");
3522 if(retval)
3523 BUG();
3524/*uart cts*/
3525 zx29_gpio_config(pdata->uart_ctsnum, pdata->uart_ctsfnc);
3526/*uart rts*/
3527 zx29_gpio_config(pdata->uart_rtsnum, pdata->uart_rtsfnc);
3528
3529 control |= (UART_CR_RTSEN |UART_CR_CTSEN );
3530 control |= UART_CR_RTS; //wl write1 for allow send
3531 }
3532 zup->autobaud = pdata->uart_abauduse;
3533 }
3534 if((pdata->uart_use)&&(port->line == UART2 ))
3535 {
3536 retval=gpio_request(pdata->uart_rxdnum,"uart2_rxd");
3537 if(retval)
3538 BUG();
3539 retval=gpio_request(pdata->uart_txdnum,"uart2_txd");
3540 if(retval)
3541 BUG();
3542
3543/*uart rxd*/
3544 zx29_gpio_config(pdata->uart_rxdnum, pdata->uart_rxdfnc);
3545 if(pdata->uart_rxdnum == ZX29_GPIO_121 ) {
3546 //pull up gpio121
3547 *(volatile unsigned int *)0xf843c82c |= 0xf0;
3548 }
3549/*uart txd*/
3550 zx29_gpio_config(pdata->uart_txdnum, pdata->uart_txdfnc);
3551
3552 if(pdata->uart_ctsrtsuse)
3553 {
3554 retval=gpio_request(pdata->uart_ctsnum,"uart2_cts");
3555 if(retval)
3556 BUG();
3557
3558 retval=gpio_request(pdata->uart_rtsnum,"uart2_rts");
3559 if(retval)
3560 BUG();
3561/*uart cts*/
3562 zx29_gpio_config(pdata->uart_ctsnum, pdata->uart_ctsfnc);
3563/*uart rts*/
3564 zx29_gpio_config(pdata->uart_rtsnum, pdata->uart_rtsfnc);
3565
3566 control |= (UART_CR_RTSEN |UART_CR_CTSEN );
3567 control |= UART_CR_RTS; //wl write1 for allow send
3568 }
3569 zup->autobaud = pdata->uart_abauduse ;
3570 }
3571#endif
3572#endif
3573 zup->autobaud_state = UART_PORT_AUTOBAUD_OFF;
3574 UART_PUT_CR(port, control);
3575
3576 /*
3577 * Finally, enable interrupts, only timeouts when using DMA
3578 * if initial RX DMA job failed, start in interrupt mode
3579 * as well.
3580 */
3581 spin_lock_irqsave(&zup->port.lock, flags);
3582 /* Clear out any spuriously appearing RX interrupts */
3583 UART_PUT_ICR(port, (UART_RTIS | UART_RXIS));
3584 //when dma not running,set UART_RTIM | UART_RXIM
3585 if(!zx29_dma_rx_running(zup)){
3586 zup->imr = UART_RTIM | UART_RXIM;
3587 UART_PUT_IMSC(port, zup->imr);
3588 }
3589#if CONFIG_SERIAL_ZX29_DMA
3590 zup->port_close = false;
3591#endif
3592 spin_unlock_irqrestore(&zup->port.lock, flags);
3593
3594
3595 return 0;
3596}
3597
3598/****************************************************************************/
3599static void zx29_uart_shutdown(struct uart_port *port)
3600{
3601 printk("zx29_uart%d_shutdown.\n",port->line);
3602 struct zx29_uart_port *zup = container_of(port, struct zx29_uart_port, port);
3603 unsigned long flags;
3604 uint32_t val;
3605 int retval = 0;
3606 struct platform_device *pdev=port->private_data;
3607 //struct zx29_uart_platdata *pdata = pdev->dev.platform_data;
3608#if CONFIG_SERIAL_ZX29_DMA
3609 zup->port_close = true;
xf.li1867bfa2024-08-20 02:32:16 -07003610 if(zup->uart_power_mode)
3611 up(&zup->sema_cyclic);
3612 else
xf.li2f424182024-08-20 00:47:34 -07003613 up(&zup->sema);
3614#endif
3615 int ret;
3616 tasklet_kill(&zup->write_wakeup);
3617#if RX_DMA_WORK
3618 if(zx29_dma_rx_work_scheduled(zup)){
3619 ret = del_timer_sync(&(zup->rx_dma_timer));
3620 ret = hrtimer_cancel(&zup->rx_dma_hrtimer);
3621 zup->work_state = 0;
3622 }
3623#endif
3624 /* Disable and clear all interrupts now */
3625 spin_lock_irqsave(&port->lock, flags);
3626 zup->imr = 0;
3627 UART_PUT_IMSC(port, zup->imr);
3628 UART_PUT_ICR(port, 0xFFFF);
3629
3630 spin_unlock_irqrestore(&port->lock, flags);
3631#if CONFIG_SERIAL_ZX29_DMA
3632 zx29_dma_shutdown(zup);
3633#endif
3634 /* Free the interrupt */
3635 free_irq(zup->port.irq, zup);
3636
3637 /* Disable UART transmitter and receiver */
3638 zup->autorts = false;
3639 val = UART_GET_CR(port);
3640 if (val & UART_CR_RTS) {
3641 zup->rts_state = true;
3642 val = UART_CR_RTS;
3643 } else
3644 zup->rts_state = false;
3645 val = UART_CR_UARTEN | UART_CR_TXE;
3646 UART_PUT_CR(port, val);
3647
3648 /* disable break condition and fifos */
3649 val = UART_GET_LCRH(port);
3650 val &= ~(UART_LCRH_BRK | UART_LCRH_FEN);
3651 UART_PUT_LCRH(port, val);
3652 if(zup->uart_power_mode){
3653 //pm_relax(&pdev->dev);
3654 zup->app_ctrl = 0;
3655 zup->uart_power_mode = 0;
3656 }
3657
3658 if((pdev->id == 0) && (zup->irq_state == 1) && (zup->uart_power_mode == 0)){
3659 free_irq(zup->rxd_irq, zup);
3660 disable_irq_wake(zup->rxd_irq);
3661 zup->irq_state = 0;
3662 }
3663
3664#if 0
3665 if(pdata->uart_use)
3666 {
3667 if(pdata->uart_ctsrtsuse)
3668 {
3669 gpio_free(pdata->uart_cts.gpionum);
3670 gpio_free(pdata->uart_rts.gpionum);
3671 }
3672#ifdef CONFIG_ARCH_ZX297520V3
3673 if((pdev->id != DEBUG_CONSOLE) && (pdata->uart_wakeup_enable == 1) && (zup->irq_state == 1)){
3674 printk(KERN_INFO"zx29_uart_shutdown,irq:%d,%s\n",zup->irq,pdata->uart_cts.gpioname);
3675 if(zup->irq){
3676 free_irq(zup->irq, zup);
3677 pcu_int_clear(PCU_UART0_RXD_INT);
3678 irq_set_irq_wake(zup->irq, 0);
3679 zup->irq_state = 0;
3680 zup->rxd_int_depth = 0;
3681 }
3682 }
3683#endif
3684 gpio_free(pdata->uart_rxd.gpionum);
3685 gpio_free(pdata->uart_txd.gpionum);
3686
3687#ifdef CONFIG_ARCH_ZX297520V3_WATCH
3688 if(port->line == 0)
3689 {
3690 retval = gpio_request(pdata->uart_txd.gpionum, pdata->uart_txd.gpioname);
3691 if(retval)
3692 {
3693 BUG();
3694 }
3695 zx29_gpio_config(pdata->uart_txd.gpionum, GPIO30_GPIO30);
3696 gpio_direction_input(pdata->uart_txd.gpionum);
3697
3698 retval = gpio_request(pdata->uart_rxd.gpionum, pdata->uart_rxd.gpioname);
3699 if(retval)
3700 {
3701 BUG();
3702 }
3703 zx29_gpio_config(pdata->uart_rxd.gpionum, GPIO29_GPIO29);
3704 gpio_direction_input(pdata->uart_rxd.gpionum);
3705 }
3706#endif
3707
3708 }
3709#endif
3710 /* Shutdown uart clock */
3711}
3712
3713/****************************************************************************/
3714static void zx29_uart_set_termios(struct uart_port *port, struct ktermios *termios,
3715 struct ktermios *old)
3716{
3717 struct zx29_uart_port *zup = container_of(port, struct zx29_uart_port, port);
3718 unsigned int lcr_h, old_cr;
3719 unsigned long flags;
3720 unsigned int baud, ibrd, fbrd,j;
3721
3722 //temple change,using setting from cmm script
3723 //if(port->line == DEBUG_CONSOLE)
3724 //return;
3725
3726 /* Set baud rate */
3727 /* Ask the core to calculate the divisor for us. */
3728 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
3729 printk("uart port %d baud is %d.\n",port->line,baud);
3730
3731 //this should not hapend
3732 if(baud == 0)
3733 BUG_ON(1);
3734 zup->baudrate = baud;
3735 ibrd = port->uartclk / (baud<<4);
3736 fbrd = ((port->uartclk % (baud<<4) )*8 + baud)/(2*baud);
3737 UART_PUT_FBRD(port, fbrd);
3738 UART_PUT_IBRD(port, ibrd);
3739
3740printk("-------zx29_uart_set_termios,line:%d, new baud:%d, uartclk:%d,ibrd:%d, fbrd:%d \n", port->line,
3741 baud, port->uartclk, ibrd, fbrd);
3742
3743 switch (termios->c_cflag & CSIZE) {
3744 case CS5:
3745 lcr_h = UART_LCRH_WLEN_5;
3746 break;
3747 case CS6:
3748 lcr_h = UART_LCRH_WLEN_6;
3749 break;
3750 case CS7:
3751 lcr_h = UART_LCRH_WLEN_7;
3752 break;
3753 default: // CS8
3754 lcr_h = UART_LCRH_WLEN_8;
3755 break;
3756 }
3757 if (termios->c_cflag & CSTOPB)
3758 lcr_h |= UART_LCRH_STP2;
3759 if (termios->c_cflag & PARENB) {
3760 lcr_h |= UART_LCRH_PEN;
3761 if (!(termios->c_cflag & PARODD))
3762 lcr_h |= UART_LCRH_EPS;
3763 }
3764 if (port->fifosize > 1)
3765 lcr_h |= UART_LCRH_FEN;
3766
3767 spin_lock_irqsave(&port->lock, flags);
3768
3769 /*
3770 * Update the per-port timeout.
3771 */
3772 uart_update_timeout(port, termios->c_cflag, baud);
3773
3774 port->read_status_mask = UART_DR_OE | 255;
3775 if (termios->c_iflag & INPCK)
3776 port->read_status_mask |= UART_DR_FE | UART_DR_PE;
3777 if (termios->c_iflag & (BRKINT | PARMRK))
3778 port->read_status_mask |= UART_DR_BE;
3779
3780 /*
3781 * Characters to ignore
3782 */
3783 port->ignore_status_mask = 0;
3784 if (termios->c_iflag & IGNPAR)
3785 port->ignore_status_mask |= UART_DR_FE | UART_DR_PE;
3786 if (termios->c_iflag & IGNBRK) {
3787 port->ignore_status_mask |= UART_DR_BE;
3788 /*
3789 * If we're ignoring parity and break indicators,
3790 * ignore overruns too (for real raw support).
3791 */
3792 if (termios->c_iflag & IGNPAR)
3793 port->ignore_status_mask |= UART_DR_OE;
3794 }
3795
3796 /*
3797 * Ignore all characters if CREAD is not set.
3798 */
3799 if ((termios->c_cflag & CREAD) == 0)
3800 port->ignore_status_mask |= UART_DUMMY_DR_RX;
3801
3802 if (UART_ENABLE_MS(port, termios->c_cflag))
3803 zx29_uart_enable_ms(port);
3804
3805 /* first, disable everything */
3806 old_cr = UART_GET_CR(port);
3807 UART_PUT_CR(port, 0);
3808
3809 if (termios->c_cflag & CRTSCTS) {
3810 if (old_cr & UART_CR_RTS)
3811 old_cr |= UART_CR_RTSEN;
3812
3813 old_cr |= UART_CR_CTSEN;
3814 port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS;
3815 zup->autorts = true;
3816 } else {
3817 old_cr &= ~(UART_CR_CTSEN | UART_CR_RTSEN);
3818 zup->autorts = false;
3819 port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS);
3820 }
3821
3822 /*
3823 * ----------v----------v----------v----------v-----
3824 * NOTE: MUST BE WRITTEN AFTER UARTLCR_M & UARTLCR_L
3825 * ----------^----------^----------^----------^-----
3826 */
3827 UART_PUT_LCRH(port, lcr_h);
3828 UART_PUT_CR(port, old_cr);
3829
3830 spin_unlock_irqrestore(&port->lock, flags);
3831 if( zup->autobaud_state == UART_PORT_AUTOBAUD_ON)
3832 {
3833 msleep(50);
3834 zup->port.icount.rx = 0;
3835
3836 for( j = 0; j<UART_AT_SENDOK_NUM; j++)
3837 {
3838 while (UART_GET_FR(port) & UART_FR_TXFF)
3839 barrier();
3840 UART_PUT_CHAR(&zup->port, UART_AT_send_ok[j]);
3841 }
3842
3843 zup->autobaud_state = UART_PORT_AUTOBAUD_OFF;
3844 }
3845}
3846
3847/****************************************************************************/
3848static const char *zx29_uart_type(struct uart_port *port)
3849{
3850 return (port->type == PORT_ZX29) ? "zx29_UART" : NULL;
3851}
3852
3853/****************************************************************************/
3854
3855static int zx29_uart_request_port(struct uart_port *port)
3856{
3857 /* UARTs always present */
3858// return request_mem_region(port->mapbase, SZ_4K, "uart-zx29")!= NULL ? 0 : -EBUSY;
3859 return 0;
3860}
3861
3862/****************************************************************************/
3863static void zx29_uart_config_port(struct uart_port *port, int flags)
3864{
3865 if (flags & UART_CONFIG_TYPE) {
3866 port->type = PORT_ZX29;
3867 zx29_uart_request_port(port);
3868 }
3869}
3870
3871/****************************************************************************/
3872
3873static void zx29_uart_release_port(struct uart_port *port)
3874{
3875// release_mem_region(port->mapbase, SZ_4K);
3876}
3877
3878/****************************************************************************/
3879
3880static int zx29_uart_verify_port(struct uart_port *port, struct serial_struct *ser)
3881{
3882 if ((ser->type != PORT_UNKNOWN) && (ser->type != PORT_ZX29))
3883 return -EINVAL;
3884 return 0;
3885}
3886
3887
3888void zx29_uart_putc(struct uart_port *port, int c)
3889{
3890 while (UART_GET_FR(port) & UART_FR_TXFF)
3891 barrier();
3892 UART_PUT_CHAR(port, c);
3893}
3894
3895
3896#ifdef CONFIG_CONSOLE_POLL
3897/****************************************************************************/
3898static int zx29_get_poll_char(struct uart_port *port)
3899{
3900 if (UART_GET_FR(port) & UART_FR_RXFE)
3901 return NO_POLL_CHAR;
3902
3903 return UART_PUT_CHAR(port);
3904}
3905
3906/****************************************************************************/
3907static void zx29_put_poll_char(struct uart_port *port, unsigned char ch)
3908{
3909 while (UART_GET_FR(port) & UART_FR_TXFF)
3910 barrier();
3911 UART_PUT_CHAR(port, ch);
3912}
3913#endif /* CONFIG_CONSOLE_POLL */
xf.li1867bfa2024-08-20 02:32:16 -07003914extern int tty_buffer_space_avail(struct tty_port *port);
xf.li2f424182024-08-20 00:47:34 -07003915static void zx29_uart_throttle_rx(struct uart_port *port)
3916{
xf.li1867bfa2024-08-20 02:32:16 -07003917 test_uart_static(port->line, NULL, 0, 80);
xf.li2f424182024-08-20 00:47:34 -07003918 unsigned long flags;
xf.li1867bfa2024-08-20 02:32:16 -07003919 int uart_id = port->line;
3920
xf.li2f424182024-08-20 00:47:34 -07003921 struct zx29_uart_port *zup = container_of(port, struct zx29_uart_port, port);
3922 dma_peripheral_id rx_id = uart_get_rx_dma_peripheral_id(zup);
xf.li1867bfa2024-08-20 02:32:16 -07003923 spin_lock_irqsave(&port->lock, flags);
3924 if(!uart_dma_cycle[port->line].used)
3925 uart_id = uart_id + 3;
3926 if(!tty_buffer_space_avail(&port->state->port)){
3927 zx29_dma_stop(DMA_CH_UART0_RX);
3928 uart_dma_cycle[uart_id].enter_throttle = true;
3929 uart_dma_cycle[uart_id].cnt_throttle++;
3930 }
3931 spin_unlock_irqrestore(&port->lock, flags);
3932 #if 0
xf.li2f424182024-08-20 00:47:34 -07003933 while(zx29_dma_get_transfer_num(rx_id) != 4096)
3934 msleep(1);
3935 spin_lock_irqsave(&port->lock, flags);
3936 zup->dmacr &= ~UART_RXDMAE;
3937 UART_PUT_DMACR(&zup->port,zup->dmacr);
3938 zx29_dma_stop(rx_id);
3939 zup->dmarx.running = false;
3940 zup->dmarx.used = false;
xf.li1867bfa2024-08-20 02:32:16 -07003941 uart_dma_cycle[port->line].enter_throttle = true;
xf.li2f424182024-08-20 00:47:34 -07003942 spin_unlock_irqrestore(&port->lock, flags);
xf.li1867bfa2024-08-20 02:32:16 -07003943 #endif
xf.li2f424182024-08-20 00:47:34 -07003944}
3945static void zx29_uart_unthrottle_rx(struct uart_port *port)
3946{
xf.li1867bfa2024-08-20 02:32:16 -07003947 test_uart_static(port->line, NULL, 0, 81);
xf.li2f424182024-08-20 00:47:34 -07003948 struct zx29_uart_port *zup = container_of(port, struct zx29_uart_port, port);
3949 unsigned long flags;
xf.li1867bfa2024-08-20 02:32:16 -07003950 int uart_id = port->line;
xf.li2f424182024-08-20 00:47:34 -07003951 spin_lock_irqsave(&port->lock, flags);
xf.li1867bfa2024-08-20 02:32:16 -07003952 if(!uart_dma_cycle[port->line].used)
3953 uart_id = uart_id + 3;
3954 uart_dma_cycle[uart_id].enter_throttle = false;
3955 uart_dma_cycle[uart_id].from_unthrottle = true;
3956 uart_dma_cycle[uart_id].cnt_unthrottle++;
3957 #if 0
3958 if (zx29_dma_rx_trigger_dma_use_dma_cyclic(zup)) {
xf.li2f424182024-08-20 00:47:34 -07003959 printk("rx_dma_chars RXDMA start fail\n");
3960 zup->imr |= (UART_RTIM|UART_RXIM);
3961 UART_PUT_IMSC(&zup->port,zup->imr);
3962 }else{
xf.li1867bfa2024-08-20 02:32:16 -07003963 hrtimer_forward_now(&zup->rx_dma_hrtimer, g_hr_interval);
xf.li2f424182024-08-20 00:47:34 -07003964 zup->pre_pending = 0;
3965 zup->dmarx.used = true;
3966 zup->work_state = true;
3967 UART_PUT_ICR(&zup->port,(UART_RTIS|UART_RXIS));
3968 }
xf.li1867bfa2024-08-20 02:32:16 -07003969 #endif
xf.li2f424182024-08-20 00:47:34 -07003970 spin_unlock_irqrestore(&port->lock, flags);
3971}
3972
3973/****************************************************************************/
3974/*
3975 * Define the basic serial functions we support.
3976 */
3977static const struct uart_ops zx29_uart_ops = {
3978 .tx_empty = zx29_uart_tx_empty,
3979 .set_mctrl = zx29_uart_set_mctrl,
3980 .get_mctrl = zx29_uart_get_mctrl,
3981 .start_tx = zx29_uart_start_tx,
3982 .stop_tx = zx29_uart_stop_tx,
3983 .stop_rx = zx29_uart_stop_rx,
3984 .throttle = zx29_uart_throttle_rx,
3985 .unthrottle = zx29_uart_unthrottle_rx,
3986 .enable_ms = zx29_uart_enable_ms,
3987 .break_ctl = zx29_uart_break_ctl,
3988 .startup = zx29_uart_startup,
3989 .shutdown = zx29_uart_shutdown,
3990 .set_termios = zx29_uart_set_termios,
3991#if CONFIG_SERIAL_ZX29_DMA
3992 .flush_buffer = zx29_dma_flush_buffer,
3993#endif
3994 .type = zx29_uart_type,
3995 .request_port = zx29_uart_request_port,
3996 .release_port = zx29_uart_release_port,
3997 .config_port = zx29_uart_config_port,
3998 .verify_port = zx29_uart_verify_port,
3999#ifdef CONFIG_CONSOLE_POLL
4000 .poll_get_char = zx29_get_poll_char,
4001 .poll_put_char = zx29_put_poll_char,
4002#endif
4003
4004};
4005
4006
4007/****************************************************************************/
4008static int zx29_init_ports(struct zx29_uart_port *zx29_port,
4009 struct platform_device *pdev)
4010{
4011 int ret = 0;
4012 struct uart_port *port=&zx29_port->port;
4013 unsigned int offset=(unsigned int)(pdev->id);
4014 struct device_node *np = pdev->dev.of_node;
4015 unsigned int baud, ibrd, fbrd;
xf.li1867bfa2024-08-20 02:32:16 -07004016
xf.li2f424182024-08-20 00:47:34 -07004017 struct resource *regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4018 //struct resource *irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
4019
4020 if(!regs){
4021 dev_err(&pdev->dev, "zx29_init_ports, get resource fail,\n");
4022 return -ENODEV;
4023 }
4024/*get apb clock*/
4025 zx29_port->busclk = devm_clk_get(&pdev->dev, UART_APBCLK_NAME);
4026 if (IS_ERR(zx29_port->busclk)) {
4027 ret = PTR_ERR(zx29_port->busclk);
4028 printk("failed to get zx29_port->busclk: %d\n", ret);
4029 return ret;
4030 }
4031
4032 /*get work clock*/
4033 zx29_port->wclk = devm_clk_get(&pdev->dev, UART_WCLK_NAME);
4034
4035 if (IS_ERR(zx29_port->wclk)) {
4036 ret = PTR_ERR(zx29_port->wclk);
4037 printk("failed to get zx29_port->wclk: %d\n", ret);
4038 return ret;
4039 }
xf.li2f424182024-08-20 00:47:34 -07004040 if(offset == 0){
4041 clk_set_rate(zx29_port->wclk, 104 * 1000000);
4042 }
4043 port->line = offset;
4044 port->type = PORT_ZX29;
4045 port->fifosize = UART_TXFIFO_SIZE;
4046 //port->iotype = UPIO_MEM;
4047 //port->irq = irq->start;
4048 port->irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
4049 if(pdev->id == 0){
4050 zx29_port->rxd_irq = irq_of_parse_and_map(pdev->dev.of_node, 1);
4051 }
4052 //port->membase = devm_ioremap_nocache(&pdev->dev, regs->start,
4053 // resource_size(regs));
4054 port->membase = devm_platform_ioremap_resource(pdev, 0);
4055
4056 if (!port->membase)
4057 return -ENODEV;
4058 port->mapbase = regs->start;
4059 port->mapsize = resource_size(regs);
4060
4061 //port->flags = UPF_BOOT_AUTOCONF;
4062 port->ops = &zx29_uart_ops;
4063 port->uartclk = clk_get_rate(zx29_port->wclk);
4064
4065 port->private_data = pdev;
4066 //here is temple def
4067 if(port->uartclk == 0){
4068 printk("---zx29_init_ports, uartclk hard set to 26M\n");
xf.li2f424182024-08-20 00:47:34 -07004069 port->uartclk = 26000000;
4070 }
4071 printk("---zx29_init_ports, line:%d, irq:%d, membase:%08x, uartclk:%d\n", port->line, port->irq, port->membase, port->uartclk);
4072 /*
4073 * just configure clock,
4074 * actually pin configuration is needed, but now gpio driver is not OK
4075 * use bootloader default configuration
4076 */
4077 if(DEBUG_CONSOLE == pdev->id){
4078 /* config uart apb_clk */
4079 clk_prepare_enable(zx29_port->busclk);
4080
4081 /* enable uart work clock */
4082 clk_prepare_enable(zx29_port->wclk);
4083 }
4084 return 0;
4085}
4086
4087
4088#ifdef CONFIG_SERIAL_ZX29_UART_CONSOLE
4089
4090#if VEHICLE_USE_ONE_UART_LOG
4091static void zx29_uart_console_putc(struct uart_port *port, int c)
4092{
4093 if(g_core_id_occupy_uart == SYMB_PS_CORE_ID)
4094 return;
4095 int ret = soft_spin_lock_printf(UART_SFLOCK);
4096 if(ret)
4097 return;
4098 while (UART_GET_FR(port) & UART_FR_TXFF)
4099 barrier();
4100 UART_PUT_CHAR(port, c);
4101 soft_spin_unlock(UART_SFLOCK);
4102}
4103#else
4104static void zx29_uart_console_putc(struct uart_port *port, int c)
4105{
4106 while (UART_GET_FR(port) & UART_FR_TXFF)
4107 barrier();
4108 UART_PUT_CHAR(port, c);
4109}
4110
4111#endif
4112
4113
4114
4115/****************************************************************************/
4116static void zx29_uart_console_write(struct console *co, const char *s, unsigned int count)
4117{
4118 struct uart_port *port = &zx29_uart_ports[co->index].port;
4119
4120 //spin_lock(&port->lock);
4121 for (; (count); count--, s++) {
4122 zx29_uart_console_putc(port, *s);
4123 if (*s == '\n')
4124 zx29_uart_console_putc(port, '\r');
4125 }
4126
4127 //spin_unlock(&port->lock);
4128}
4129
4130/***************************************************************************
4131 * If the port was already initialised (eg, by a boot loader),
4132 * try to determine the current setup.
4133 ****************************************************************************/
4134static void __init zx29_console_get_options(struct uart_port *port, int *baud,
4135 int *parity, int *bits)
4136{
4137 if (UART_GET_CR(port) & UART_CR_UARTEN) {
4138 unsigned int lcr_h, ibrd, fbrd;
4139
4140 lcr_h = UART_GET_LCRH(port);
4141 *parity = 'n';
4142 if (lcr_h & UART_LCRH_PEN) {
4143 if (lcr_h & UART_LCRH_EPS)
4144 *parity = 'e';
4145 else
4146 *parity = 'o';
4147 }
4148 if ((lcr_h & 0x60) == UART_LCRH_WLEN_7)
4149 *bits = 7;
4150 else
4151 *bits = 8;
4152
4153 ibrd = UART_GET_IBRD(port);
4154 fbrd = UART_GET_FBRD(port);
4155
4156 *baud = port->uartclk * 8 / (16*8 * ibrd + 2*fbrd-1);
4157 }
4158}
4159
4160/****************************************************************************/
4161static int __init zx29_uart_console_setup(struct console *co, char *options)
4162{
4163 printk("zx29_uart_console_setup.\n");
4164 struct uart_port *port;
4165 int baud = 115200;
4166 int bits = 8;
4167 int parity = 'n';
4168 int flow = 'n';
4169 unsigned int uart_cr = 0;
4170
4171 if ((co->index < 0) || (co->index >= zx29_MAXPORTS))
4172 co->index = CONFIG_UART_CONSOLE_ID;
4173
4174 port = &zx29_uart_ports[co->index].port;
4175 if (port->membase == NULL)
4176 return -ENODEV;
4177
4178 uart_cr = UART_GET_CR(port);
4179 uart_cr |= UART_CR_UARTEN | UART_CR_TXE;
4180 UART_PUT_CR(port,uart_cr);
4181
4182 if (options)
4183 uart_parse_options(options, &baud, &parity, &bits, &flow);
4184 else
4185 zx29_console_get_options(port, &baud, &parity, &bits);
4186
4187 return uart_set_options(port, co, baud, parity, bits, flow);
4188}
4189
4190/****************************************************************************/
4191
4192static struct uart_driver zx29_uart_driver;
4193int zx29_get_console_index(void)
4194{
4195#if 0
4196 int dev_cnt = zx29_device_table_num;
4197 int idx = 0;
4198 struct platform_device *pdev = NULL;
4199 for(idx = 0; idx < dev_cnt; idx++)
4200 {
4201 pdev = zx29_device_table[idx];
4202 if(strcmp(pdev->name,"zx29_uart") == 0 && pdev->id == CONFIG_UART_CONSOLE_ID)
4203 return idx;
4204 }
4205#endif
4206 return CONFIG_UART_CONSOLE_ID;
4207 //return -1;
4208}
4209static struct console zx29_uart_console = {
4210 .name = "ttyS",
4211 .write = zx29_uart_console_write,
4212 .device = uart_console_device,
4213 .setup = zx29_uart_console_setup,
4214 .flags = CON_PRINTBUFFER,
4215 .index = -1,
4216 .data = &zx29_uart_driver,
4217};
4218static int __init zx29_uart_console_init(void)
4219{
4220 int console_dev_id = zx29_get_console_index();
4221
4222 if(console_dev_id < 0){
4223 printk("console init fail, uart config fail, console_dev_id is: %d", console_dev_id);
4224 return -1;
4225 }
4226 //zx29_init_ports(&zx29_uart_ports[DEBUG_CONSOLE], zx29_device_table[console_dev_id]);
4227 register_console(&zx29_uart_console);
4228 pr_info("[UART]register_console: zx29 console registered!\n");
4229
4230 return 0;
4231}
4232
4233//console_initcall(zx29_uart_console_init);
4234
4235#define zx29_UART_CONSOLE (&zx29_uart_console)
4236
4237
4238static void zx29_uart_early_write(struct console *con, const char *s, unsigned n)
4239{
4240 struct earlycon_device *dev = con->data;
4241
4242 uart_console_write(&dev->port, s, n, zx29_uart_console_putc);
4243}
4244
4245static int __init zx29_uart_early_console_setup(struct earlycon_device *device,
4246 const char *opt)
4247{
4248 if (!device->port.membase)
4249 return -ENODEV;
4250
4251 device->con->write = zx29_uart_early_write;
4252
4253 return 0;
4254}
4255
4256OF_EARLYCON_DECLARE(zx29_uart, "zxic,zx29-uart", zx29_uart_early_console_setup);
4257#else
4258#define zx29_UART_CONSOLE NULL
4259#endif /* CONFIG_zx29_UART_CONSOLE */
4260static void zx29_uart_pin_ctrl(struct platform_device *pdev)
4261{
4262 struct pinctrl *pin_ctrl;
4263 struct pinctrl_state *state0;
4264 pin_ctrl = devm_pinctrl_get(&pdev->dev);
4265 switch(pdev->id){
4266 case 0:
4267 printk("zx29_uart %d use default pinctrl.",pdev->id);
4268 break;
4269 case 1:
4270 printk("zx29_uart %d use default pinctrl.",pdev->id);
4271 break;
4272
4273 case 2:
4274 if(IS_ERR(pin_ctrl)){
4275 dev_warn(&pdev->dev, "fail to get uart2 pins.");
4276 pin_ctrl = NULL;
4277 return;
4278 }
4279 state0 = pinctrl_lookup_state(pin_ctrl, "default");
4280 if(IS_ERR(state0)){
4281 dev_err(&pdev->dev, "uart2 pinstate get fail.\n");
4282 }
4283 if(pinctrl_select_state(pin_ctrl, state0)){
4284 dev_err(&pdev->dev, "uart2 select pinstate fail.\n");
4285 }
4286 break;
4287 }
4288}
4289
4290/*
4291 * Define the zx29 UART driver structure.
4292 */
4293static struct uart_driver zx29_uart_driver = {
4294 .owner = THIS_MODULE,
4295 .driver_name = "zx29_uart",
4296 .dev_name = "ttyS",
4297 .major = SERIAL_zx29_MAJOR,
4298 .minor = SERIAL_MINOR_START,
4299 .nr = zx29_MAXPORTS,
4300 .cons = zx29_UART_CONSOLE,
4301};
4302
4303unsigned char uart_wakelock_name[zx29_MAXPORTS][20]={{0}};
4304/****************************************************************************/
4305
4306
4307#ifdef CONFIG_PM_SLEEP
4308static int zx29_uart_suspend(struct device *dev)
4309{
4310 int ret = 0;
4311 struct zx29_uart_port *zup = dev_get_drvdata(dev);
4312 unsigned int flags;
4313 if (!zup)
4314 return -EINVAL;
4315 if(zup->port.line == UART1)
4316 return 0;
4317#if 1
4318 pinctrl_pm_select_sleep_state(dev);
4319#endif
4320
4321 printk("zx29_uart%d suspend.\n",zup->port.line);
4322
4323 raw_spin_lock_irqsave(&zup->port.lock, flags);
4324 zup->enter_suspend = 1;
4325 if(zup->port.line == UART2){
4326 zx29_dma_stop(DMA_CH_UART2_RX);
4327 zx29_dma_stop(DMA_CH_UART2_TX);
4328 raw_spin_unlock_irqrestore(&zup->port.lock, flags);
4329 return 0;
4330 }
4331
4332 zx29_dma_stop(DMA_CH_UART0_RX);
4333 zx29_dma_stop(DMA_CH_UART0_TX);
4334 raw_spin_unlock_irqrestore(&zup->port.lock, flags);
4335#if 0
4336 ret = irq_set_irq_type(unsigned int irq, unsigned int type);
4337
4338#endif
4339 //pcu_int_clear(PCU_UART0_RXD_INT);
4340 if(zup->irq_state && (zup->rxd_int_depth == 0)){
4341 struct irq_data *data_rxd;
4342 data_rxd= irq_get_irq_data(zup->rxd_irq);
4343 if(data_rxd){
4344 if(irqd_irq_disabled(data_rxd))
4345 enable_irq(zup->rxd_irq);
4346 }
4347 zup->rxd_int_depth = 1;
4348 }
4349
4350 return 0;
4351}
4352
4353static int zx29_uart_resume(struct device *dev)
4354{
4355 struct zx29_uart_port *zup = dev_get_drvdata(dev);
4356
4357 if (!zup)
4358 return -EINVAL;
4359 int uart_id = zup->port.line;
xf.li1867bfa2024-08-20 02:32:16 -07004360 if(!uart_dma_cycle[zup->port.line].used)
4361 uart_id = uart_id + 3;
xf.li2f424182024-08-20 00:47:34 -07004362 if(zup->port.line == UART1)
4363 return 0;
4364#if 1
4365 pinctrl_pm_select_default_state(dev);
4366#endif
4367
4368 printk("zx29_uart%d resume.\n",zup->port.line);
4369
4370 zup->enter_suspend = 0;
4371 uart_dma_cycle[uart_id].from_resume = 1;
4372 return 0;
4373}
4374
4375
4376#endif
4377
4378static SIMPLE_DEV_PM_OPS(zx29_uart_dev_pm_ops, zx29_uart_suspend, zx29_uart_resume);
4379
4380
4381
4382static int zx29_uart_probe(struct platform_device *pdev)
4383{
4384 int ret=0;
4385 int error;
4386 char wakelock_name[20];
4387 struct device_node *np = pdev->dev.of_node;
4388 ret = of_alias_get_id(np, "uart");
4389
4390 if(ret < 0){
4391 printk("-----zx29_uart_probe,of_alias_get_id fail ret:%d\n", ret);
4392 return -ENODEV;
4393 }
4394 pdev->id = ret;
4395 printk("-----zx29_uart_probe,ret:%d\n", ret);
4396 // struct zx29_uart_platdata *pdata = pdev->dev.platform_data;
4397 struct zx29_uart_port *port = &zx29_uart_ports[pdev->id];
4398
4399
4400#if 0//def CONFIG_SERIAL_ZX29_UART_CONSOLE
4401 if(DEBUG_CONSOLE != pdev->id){
4402 ret = zx29_init_ports(port,pdev);
4403 }
4404#else
4405 ret = zx29_init_ports(port,pdev);
4406
4407#endif
4408 if(ret < 0){
4409 printk("-----zx29_uart_probe,zx29_init_ports fail ret:%d\n", ret);
4410 }
4411 zx29_uart_pin_ctrl(pdev);
4412#if CONFIG_SERIAL_ZX29_DMA
4413 if((DEBUG_CONSOLE != pdev->id ) && (pdev->id != 4))
4414 {
4415 uart_dma_init(port);
4416 printk(KERN_INFO "[%s][%d]UART_%d DMA is OPENED\n",__func__,__LINE__,pdev->id);
4417 }
4418#endif
4419 ret = 0;
4420 ret=uart_add_one_port(&zx29_uart_driver, &port->port);
4421
4422 if(ret)
4423 {
4424#if CONFIG_SERIAL_ZX29_DMA
4425 zx29_dma_remove(port);
4426#endif
4427 return ret;
4428 }
4429#if CONFIG_SERIAL_ZX29_DMA
4430 sema_init(&port->sema, 0);
4431#endif
4432
4433 platform_set_drvdata(pdev, port);
4434
4435 if(pdev->id == DEBUG_CONSOLE){
4436 //g_console_open_flag = pdata->uart_input_enable ? pdata->uart_input_enable : 0;
4437 error = device_create_file(&pdev->dev, &dev_attr_console_input);
4438#if VEHICLE_USE_ONE_UART_LOG
4439 error = device_create_file(&pdev->dev, &dev_attr_console_uart_toggle);
4440 error = device_create_file(&pdev->dev, &dev_attr_coreid_occupy_uart);
4441 int ret;
4442 ret = rpmsgCreateChannel(CORE_PS0, ICP_CHANNEL_CONSOLE_UART, ICP_BUFFERSIZE_CONSOLE_TOGGLE);
4443 if(ret){
4444 printk("linux5 request icp channel for uart fail %d.\n",ret);
4445 }
4446 rpmsgRegCallBack(CORE_PS0, ICP_CHANNEL_CONSOLE_UART, icp_callback_ps2cap);
4447#endif
4448 }
4449
4450 if(pdev->id != DEBUG_CONSOLE){
4451 error = device_create_file(&pdev->dev, &dev_attr_ctsrts_input);
4452 error = device_create_file(&pdev->dev, &dev_attr_wakeup_enable);
4453 error = device_create_file(&pdev->dev, &dev_attr_sleep_state);
4454 error = device_create_file(&pdev->dev, &dev_attr_app_ctrl);
4455
4456 }
xf.li6b423c72025-03-14 00:07:42 -07004457 if(pdev->id == 2){
4458 error = device_create_file(&pdev->dev, &dev_attr_uart_io_select);
4459 }
xf.li2f424182024-08-20 00:47:34 -07004460 error = device_create_file(&pdev->dev, &dev_attr_statics);
4461 device_init_wakeup(&pdev->dev, true);
4462/*
4463 strcpy(wakelock_name, "uart_wakelock_x");
4464 wakelock_name[14] = '0' + pdev->id;
4465 strcpy(uart_wakelock_name[pdev->id], wakelock_name);
4466 wake_lock_init(&(port->port.port_wakelock),WAKE_LOCK_SUSPEND,uart_wakelock_name[pdev->id]);
4467*/
4468
4469 printk(KERN_INFO "TSP zx29 UART_%d probe OK\n",pdev->id);
4470 return 0;
4471}
4472
4473/****************************************************************************/
4474static int /*__devexit*/ zx29_uart_remove(struct platform_device *pdev)
4475{
4476 struct uart_port *port = NULL;
4477#if CONFIG_SERIAL_ZX29_DMA
4478 struct zx29_uart_port *zup = container_of(port, struct zx29_uart_port, port);
4479#endif
4480 int i;
4481 if(pdev->id == DEBUG_CONSOLE){
4482 device_remove_file(&pdev->dev, &dev_attr_console_input);
4483 }
4484
4485 if(pdev->id != DEBUG_CONSOLE){
4486 device_remove_file(&pdev->dev, &dev_attr_ctsrts_input);
4487 device_remove_file(&pdev->dev, &dev_attr_wakeup_enable);
4488 }
4489
4490 for (i = 0; (i < zx29_MAXPORTS); i++) {
4491 port = &zx29_uart_ports[i].port;
4492 if (port){
4493 uart_remove_one_port(&zx29_uart_driver, port);
4494
4495
4496#if CONFIG_SERIAL_ZX29_DMA
4497 zup=container_of(port,struct zx29_uart_port,port);
4498 zx29_dma_remove(zup);
4499
4500#endif
4501 }
4502 }
4503 return 0;
4504}
4505
4506static const struct of_device_id zx29_uart_of_match[] = {
4507 { .compatible = "zxic,zx29-uart"},
4508 {},
4509};
4510MODULE_DEVICE_TABLE(of, zx29_uart_of_match);
4511
4512static struct platform_driver zx29_uart_platform_driver = {
4513 .probe = zx29_uart_probe,
4514 //.remove = __devexit_p(zx29_uart_remove),
4515 .remove = zx29_uart_remove,
4516 .driver = {
4517 .name = "zx29_uart",
4518 .pm = &zx29_uart_dev_pm_ops,
4519 .of_match_table = of_match_ptr(zx29_uart_of_match),
4520 .owner = THIS_MODULE,
4521 },
4522};
4523
4524static int __init zx29_uart_init(void)
4525{
4526 int rc;
4527
4528 rc = uart_register_driver(&zx29_uart_driver);
4529 if (rc)
4530 return rc;
4531 rc = platform_driver_register(&zx29_uart_platform_driver);
4532 if (rc){
4533 uart_unregister_driver(&zx29_uart_driver);
4534 return rc;
4535 }
4536
4537 printk(KERN_INFO "zx29 UART driver registered\n");
4538
4539 return 0;
4540}
4541
4542static void __exit zx29_uart_exit(void)
4543{
4544#ifdef CONFIG_SERIAL_ZX29_UART_CONSOLE
4545 unregister_console(&zx29_uart_console);
4546#endif
4547 platform_driver_unregister(&zx29_uart_platform_driver);
4548 uart_unregister_driver(&zx29_uart_driver);
4549}
4550//arch_initcall(zx29_uart_init);
4551
4552//subsys_initcall(zx29_uart_init);
4553module_init(zx29_uart_init);
4554module_exit(zx29_uart_exit);
4555