hc
2023-11-06 e3e12f52b214121840b44c91de5b3e5af5d3eb84
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
// SPDX-License-Identifier: GPL-2.0+
/*
 * 8250_dma.c - DMA Engine API support for 8250.c
 *
 * Copyright (C) 2013 Intel Corporation
 */
#include <linux/tty.h>
#include <linux/tty_flip.h>
#include <linux/serial_reg.h>
#include <linux/dma-mapping.h>
 
#include "8250.h"
 
#ifdef CONFIG_ARCH_ROCKCHIP
#define MAX_TX_BYTES        64
#define MAX_FIFO_SIZE        64
#define UART_RFL_16550A        0x21
#endif
 
static void __dma_tx_complete(void *param)
{
   struct uart_8250_port    *p = param;
   struct uart_8250_dma    *dma = p->dma;
   struct circ_buf        *xmit = &p->port.state->xmit;
   unsigned long    flags;
   int        ret;
 
   dma_sync_single_for_cpu(dma->txchan->device->dev, dma->tx_addr,
               UART_XMIT_SIZE, DMA_TO_DEVICE);
 
   spin_lock_irqsave(&p->port.lock, flags);
 
   dma->tx_running = 0;
 
   xmit->tail += dma->tx_size;
   xmit->tail &= UART_XMIT_SIZE - 1;
   p->port.icount.tx += dma->tx_size;
 
   if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
       uart_write_wakeup(&p->port);
 
   ret = serial8250_tx_dma(p);
   if (ret) {
       p->ier |= UART_IER_THRI;
#ifdef CONFIG_ARCH_ROCKCHIP
       p->ier |= UART_IER_PTIME;
#endif
       serial_port_out(&p->port, UART_IER, p->ier);
   }
 
   spin_unlock_irqrestore(&p->port.lock, flags);
}
 
#ifdef CONFIG_ARCH_ROCKCHIP
 
static void __dma_rx_complete(void *param)
{
   struct uart_8250_port    *p = param;
   struct uart_8250_dma    *dma = p->dma;
   struct tty_port        *tty_port = &p->port.state->port;
   struct dma_tx_state    state;
   unsigned int        count = 0, cur_index = 0;
 
   dmaengine_tx_status(dma->rxchan, dma->rx_cookie, &state);
   cur_index = dma->rx_size - state.residue;
 
   if (cur_index == dma->rx_index)
       return;
   else if (cur_index > dma->rx_index)
       count = cur_index - dma->rx_index;
   else
       count = dma->rx_size - dma->rx_index;
 
   tty_insert_flip_string(tty_port, dma->rx_buf + dma->rx_index, count);
 
   if (cur_index < dma->rx_index) {
       tty_insert_flip_string(tty_port, dma->rx_buf, cur_index);
       count += cur_index;
   }
 
   p->port.icount.rx += count;
   dma->rx_index = cur_index;
}
 
#else
 
static void __dma_rx_complete(void *param)
{
   struct uart_8250_port    *p = param;
   struct uart_8250_dma    *dma = p->dma;
   struct tty_port        *tty_port = &p->port.state->port;
   struct dma_tx_state    state;
   int            count;
 
   dma->rx_running = 0;
   dmaengine_tx_status(dma->rxchan, dma->rx_cookie, &state);
 
   count = dma->rx_size - state.residue;
 
   tty_insert_flip_string(tty_port, dma->rx_buf, count);
   p->port.icount.rx += count;
 
   tty_flip_buffer_push(tty_port);
}
 
#endif
 
int serial8250_tx_dma(struct uart_8250_port *p)
{
   struct uart_8250_dma        *dma = p->dma;
   struct circ_buf            *xmit = &p->port.state->xmit;
   struct dma_async_tx_descriptor    *desc;
   int ret;
 
   if (dma->tx_running)
       return 0;
 
   if (uart_tx_stopped(&p->port) || uart_circ_empty(xmit)) {
       /* We have been called from __dma_tx_complete() */
       serial8250_rpm_put_tx(p);
       return 0;
   }
 
   dma->tx_size = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
#ifdef CONFIG_ARCH_ROCKCHIP
   if (dma->tx_size < MAX_TX_BYTES) {
       ret = -EBUSY;
       goto err;
   }
#endif
   desc = dmaengine_prep_slave_single(dma->txchan,
                      dma->tx_addr + xmit->tail,
                      dma->tx_size, DMA_MEM_TO_DEV,
                      DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
   if (!desc) {
       ret = -EBUSY;
       goto err;
   }
 
   dma->tx_running = 1;
   desc->callback = __dma_tx_complete;
   desc->callback_param = p;
 
   dma->tx_cookie = dmaengine_submit(desc);
 
   dma_sync_single_for_device(dma->txchan->device->dev, dma->tx_addr,
                  UART_XMIT_SIZE, DMA_TO_DEVICE);
 
   dma_async_issue_pending(dma->txchan);
   if (dma->tx_err) {
       dma->tx_err = 0;
       if (p->ier & UART_IER_THRI) {
           p->ier &= ~UART_IER_THRI;
#ifdef CONFIG_ARCH_ROCKCHIP
           p->ier &= ~UART_IER_PTIME;
#endif
           serial_out(p, UART_IER, p->ier);
       }
   }
   return 0;
err:
   dma->tx_err = 1;
   return ret;
}
 
#ifdef CONFIG_ARCH_ROCKCHIP
 
int serial8250_rx_dma(struct uart_8250_port *p)
{
   unsigned int rfl, i = 0, fcr = 0, cur_index = 0;
   unsigned char buf[MAX_FIFO_SIZE];
   struct uart_port    *port = &p->port;
   struct tty_port        *tty_port = &p->port.state->port;
   struct dma_tx_state    state;
   struct uart_8250_dma    *dma = p->dma;
 
   fcr = UART_FCR_ENABLE_FIFO | UART_FCR_T_TRIG_10 | UART_FCR_R_TRIG_11;
   serial_port_out(port, UART_FCR, fcr);
 
   do {
       dmaengine_tx_status(dma->rxchan, dma->rx_cookie, &state);
       cur_index = dma->rx_size - state.residue;
   } while (cur_index % dma->rxconf.src_maxburst);
 
   rfl = serial_port_in(port, UART_RFL_16550A);
   while (i < rfl)
       buf[i++] = serial_port_in(port, UART_RX);
 
   __dma_rx_complete(p);
 
   tty_insert_flip_string(tty_port, buf, i);
   p->port.icount.rx += i;
   tty_flip_buffer_push(tty_port);
 
   if (fcr)
       serial_port_out(port, UART_FCR, p->fcr);
   return 0;
}
 
int serial8250_start_rx_dma(struct uart_8250_port *p)
{
   struct uart_8250_dma        *dma = p->dma;
   struct dma_async_tx_descriptor    *desc;
 
   desc = dmaengine_prep_dma_cyclic(dma->rxchan, dma->rx_addr,
                    dma->rx_size, dma->rx_size,
                    DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT |
                    DMA_CTRL_ACK);
   if (!desc)
       return -EBUSY;
 
   dma->rx_running = 1;
   desc->callback = NULL;
   desc->callback_param = NULL;
 
   dma->rx_cookie = dmaengine_submit(desc);
   dma_async_issue_pending(dma->rxchan);
   dma->rx_index = 0;
   return 0;
}
 
#else
 
int serial8250_rx_dma(struct uart_8250_port *p)
{
   struct uart_8250_dma        *dma = p->dma;
   struct dma_async_tx_descriptor    *desc;
 
   if (dma->rx_running)
       return 0;
 
   desc = dmaengine_prep_slave_single(dma->rxchan, dma->rx_addr,
                      dma->rx_size, DMA_DEV_TO_MEM,
                      DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
   if (!desc)
       return -EBUSY;
 
   dma->rx_running = 1;
   desc->callback = __dma_rx_complete;
   desc->callback_param = p;
 
   dma->rx_cookie = dmaengine_submit(desc);
 
   dma_async_issue_pending(dma->rxchan);
 
   return 0;
}
 
#endif
 
void serial8250_rx_dma_flush(struct uart_8250_port *p)
{
   struct uart_8250_dma *dma = p->dma;
 
   if (dma->rx_running) {
       dmaengine_pause(dma->rxchan);
       __dma_rx_complete(p);
       dmaengine_terminate_async(dma->rxchan);
   }
}
EXPORT_SYMBOL_GPL(serial8250_rx_dma_flush);
 
int serial8250_request_dma(struct uart_8250_port *p)
{
   struct uart_8250_dma    *dma = p->dma;
   phys_addr_t rx_dma_addr = dma->rx_dma_addr ?
                 dma->rx_dma_addr : p->port.mapbase;
   phys_addr_t tx_dma_addr = dma->tx_dma_addr ?
                 dma->tx_dma_addr : p->port.mapbase;
   dma_cap_mask_t        mask;
   struct dma_slave_caps    caps;
   int            ret;
 
   /* Default slave configuration parameters */
   dma->rxconf.direction        = DMA_DEV_TO_MEM;
   dma->rxconf.src_addr_width    = DMA_SLAVE_BUSWIDTH_1_BYTE;
   dma->rxconf.src_addr        = rx_dma_addr + UART_RX;
#ifdef CONFIG_ARCH_ROCKCHIP
   if ((p->port.fifosize / 4) < 16)
       dma->rxconf.src_maxburst = p->port.fifosize / 4;
   else
       dma->rxconf.src_maxburst = 16;
#endif
 
   dma->txconf.direction        = DMA_MEM_TO_DEV;
   dma->txconf.dst_addr_width    = DMA_SLAVE_BUSWIDTH_1_BYTE;
   dma->txconf.dst_addr        = tx_dma_addr + UART_TX;
#ifdef CONFIG_ARCH_ROCKCHIP
   dma->txconf.dst_maxburst    = 16;
#endif
   dma_cap_zero(mask);
   dma_cap_set(DMA_SLAVE, mask);
 
   /* Get a channel for RX */
   dma->rxchan = dma_request_slave_channel_compat(mask,
                              dma->fn, dma->rx_param,
                              p->port.dev, "rx");
   if (!dma->rxchan)
       return -ENODEV;
 
   /* 8250 rx dma requires dmaengine driver to support pause/terminate */
   ret = dma_get_slave_caps(dma->rxchan, &caps);
   if (ret)
       goto err_rx;
   if (!caps.cmd_pause || !caps.cmd_terminate ||
       caps.residue_granularity == DMA_RESIDUE_GRANULARITY_DESCRIPTOR) {
       ret = -EINVAL;
       goto err_rx;
   }
 
   dmaengine_slave_config(dma->rxchan, &dma->rxconf);
 
   /* RX buffer */
#ifdef CONFIG_ARCH_ROCKCHIP
   if (!dma->rx_size)
       dma->rx_size = PAGE_SIZE * 2;
#else
   if (!dma->rx_size)
       dma->rx_size = PAGE_SIZE;
#endif
 
   dma->rx_buf = dma_alloc_coherent(dma->rxchan->device->dev, dma->rx_size,
                   &dma->rx_addr, GFP_KERNEL);
 
   if (!dma->rx_buf) {
       ret = -ENOMEM;
       goto err_rx;
   }
 
   /* Get a channel for TX */
   dma->txchan = dma_request_slave_channel_compat(mask,
                              dma->fn, dma->tx_param,
                              p->port.dev, "tx");
   if (dma->txchan) {
       dmaengine_slave_config(dma->txchan, &dma->txconf);
 
       /* TX buffer */
       dma->tx_addr = dma_map_single(dma->txchan->device->dev,
                       p->port.state->xmit.buf,
                       UART_XMIT_SIZE,
                       DMA_TO_DEVICE);
       if (dma_mapping_error(dma->txchan->device->dev, dma->tx_addr)) {
           dma_free_coherent(dma->rxchan->device->dev,
                     dma->rx_size, dma->rx_buf,
                     dma->rx_addr);
           dma_release_channel(dma->txchan);
           dma->txchan = NULL;
       }
 
       dev_info_ratelimited(p->port.dev, "got rx and tx dma channels\n");
   } else {
       dev_info_ratelimited(p->port.dev, "got rx dma channels only\n");
   }
 
#ifdef CONFIG_ARCH_ROCKCHIP
   /* start dma for rx*/
   serial8250_start_rx_dma(p);
#endif
   return 0;
 
err_rx:
   dma_release_channel(dma->rxchan);
   return ret;
}
EXPORT_SYMBOL_GPL(serial8250_request_dma);
 
void serial8250_release_dma(struct uart_8250_port *p)
{
   struct uart_8250_dma *dma = p->dma;
 
   if (!dma)
       return;
 
   /* Release RX resources */
   dmaengine_terminate_sync(dma->rxchan);
   dma_free_coherent(dma->rxchan->device->dev, dma->rx_size, dma->rx_buf,
             dma->rx_addr);
   dma_release_channel(dma->rxchan);
   dma->rxchan = NULL;
#ifdef CONFIG_ARCH_ROCKCHIP
   dma->rx_running = 0;
#endif
   /* Release TX resources */
   if (dma->txchan) {
       dmaengine_terminate_all(dma->txchan);
       dma_unmap_single(dma->txchan->device->dev, dma->tx_addr,
                UART_XMIT_SIZE, DMA_TO_DEVICE);
       dma_release_channel(dma->txchan);
       dma->txchan = NULL;
       dma->tx_running = 0;
   }
   dev_dbg_ratelimited(p->port.dev, "dma channels released\n");
}
EXPORT_SYMBOL_GPL(serial8250_release_dma);