forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-11 223293205a7265c8b02882461ba8996650048ade
kernel/drivers/tty/serial/sh-sci.c
....@@ -15,10 +15,6 @@
1515 * Modified to support SH7300 SCIF. Takashi Kusuda (Jun 2003).
1616 * Removed SH7300 support (Jul 2007).
1717 */
18
-#if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
19
-#define SUPPORT_SYSRQ
20
-#endif
21
-
2218 #undef DEBUG
2319
2420 #include <linux/clk.h>
....@@ -54,6 +50,7 @@
5450
5551 #ifdef CONFIG_SUPERH
5652 #include <asm/sh_bios.h>
53
+#include <asm/platform_early.h>
5754 #endif
5855
5956 #include "serial_mctrl_gpio.h"
....@@ -1107,9 +1104,8 @@
11071104 scif_set_rtrg(port, 1);
11081105 }
11091106
1110
-static ssize_t rx_trigger_show(struct device *dev,
1111
- struct device_attribute *attr,
1112
- char *buf)
1107
+static ssize_t rx_fifo_trigger_show(struct device *dev,
1108
+ struct device_attribute *attr, char *buf)
11131109 {
11141110 struct uart_port *port = dev_get_drvdata(dev);
11151111 struct sci_port *sci = to_sci_port(port);
....@@ -1117,10 +1113,9 @@
11171113 return sprintf(buf, "%d\n", sci->rx_trigger);
11181114 }
11191115
1120
-static ssize_t rx_trigger_store(struct device *dev,
1121
- struct device_attribute *attr,
1122
- const char *buf,
1123
- size_t count)
1116
+static ssize_t rx_fifo_trigger_store(struct device *dev,
1117
+ struct device_attribute *attr,
1118
+ const char *buf, size_t count)
11241119 {
11251120 struct uart_port *port = dev_get_drvdata(dev);
11261121 struct sci_port *sci = to_sci_port(port);
....@@ -1138,7 +1133,7 @@
11381133 return count;
11391134 }
11401135
1141
-static DEVICE_ATTR(rx_fifo_trigger, 0644, rx_trigger_show, rx_trigger_store);
1136
+static DEVICE_ATTR_RW(rx_fifo_trigger);
11421137
11431138 static ssize_t rx_fifo_timeout_show(struct device *dev,
11441139 struct device_attribute *attr,
....@@ -1248,12 +1243,22 @@
12481243 return -1;
12491244 }
12501245
1251
-static void sci_rx_dma_release(struct sci_port *s)
1246
+static void sci_dma_rx_chan_invalidate(struct sci_port *s)
1247
+{
1248
+ unsigned int i;
1249
+
1250
+ s->chan_rx = NULL;
1251
+ for (i = 0; i < ARRAY_SIZE(s->cookie_rx); i++)
1252
+ s->cookie_rx[i] = -EINVAL;
1253
+ s->active_rx = 0;
1254
+}
1255
+
1256
+static void sci_dma_rx_release(struct sci_port *s)
12521257 {
12531258 struct dma_chan *chan = s->chan_rx_saved;
12541259
1255
- s->chan_rx_saved = s->chan_rx = NULL;
1256
- s->cookie_rx[0] = s->cookie_rx[1] = -EINVAL;
1260
+ s->chan_rx_saved = NULL;
1261
+ sci_dma_rx_chan_invalidate(s);
12571262 dmaengine_terminate_sync(chan);
12581263 dma_free_coherent(chan->device->dev, s->buf_len_rx * 2, s->rx_buf[0],
12591264 sg_dma_address(&s->sg_rx[0]));
....@@ -1267,6 +1272,20 @@
12671272 ktime_t t = ktime_set(sec, nsec);
12681273
12691274 hrtimer_start(hrt, t, HRTIMER_MODE_REL);
1275
+}
1276
+
1277
+static void sci_dma_rx_reenable_irq(struct sci_port *s)
1278
+{
1279
+ struct uart_port *port = &s->port;
1280
+ u16 scr;
1281
+
1282
+ /* Direct new serial port interrupts back to CPU */
1283
+ scr = serial_port_in(port, SCSCR);
1284
+ if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
1285
+ scr &= ~SCSCR_RDRQE;
1286
+ enable_irq(s->irqs[SCIx_RXI_IRQ]);
1287
+ }
1288
+ serial_port_out(port, SCSCR, scr | SCSCR_RIE);
12701289 }
12711290
12721291 static void sci_dma_rx_complete(void *arg)
....@@ -1318,12 +1337,13 @@
13181337 dev_warn(port->dev, "Failed submitting Rx DMA descriptor\n");
13191338 /* Switch to PIO */
13201339 spin_lock_irqsave(&port->lock, flags);
1321
- s->chan_rx = NULL;
1322
- sci_start_rx(port);
1340
+ dmaengine_terminate_async(chan);
1341
+ sci_dma_rx_chan_invalidate(s);
1342
+ sci_dma_rx_reenable_irq(s);
13231343 spin_unlock_irqrestore(&port->lock, flags);
13241344 }
13251345
1326
-static void sci_tx_dma_release(struct sci_port *s)
1346
+static void sci_dma_tx_release(struct sci_port *s)
13271347 {
13281348 struct dma_chan *chan = s->chan_tx_saved;
13291349
....@@ -1336,7 +1356,7 @@
13361356 dma_release_channel(chan);
13371357 }
13381358
1339
-static int sci_submit_rx(struct sci_port *s, bool port_lock_held)
1359
+static int sci_dma_rx_submit(struct sci_port *s, bool port_lock_held)
13401360 {
13411361 struct dma_chan *chan = s->chan_rx;
13421362 struct uart_port *port = &s->port;
....@@ -1372,17 +1392,14 @@
13721392 spin_lock_irqsave(&port->lock, flags);
13731393 if (i)
13741394 dmaengine_terminate_async(chan);
1375
- for (i = 0; i < 2; i++)
1376
- s->cookie_rx[i] = -EINVAL;
1377
- s->active_rx = 0;
1378
- s->chan_rx = NULL;
1395
+ sci_dma_rx_chan_invalidate(s);
13791396 sci_start_rx(port);
13801397 if (!port_lock_held)
13811398 spin_unlock_irqrestore(&port->lock, flags);
13821399 return -EAGAIN;
13831400 }
13841401
1385
-static void work_fn_tx(struct work_struct *work)
1402
+static void sci_dma_tx_work_fn(struct work_struct *work)
13861403 {
13871404 struct sci_port *s = container_of(work, struct sci_port, work_tx);
13881405 struct dma_async_tx_descriptor *desc;
....@@ -1449,7 +1466,7 @@
14491466 return;
14501467 }
14511468
1452
-static enum hrtimer_restart rx_timer_fn(struct hrtimer *t)
1469
+static enum hrtimer_restart sci_dma_rx_timer_fn(struct hrtimer *t)
14531470 {
14541471 struct sci_port *s = container_of(t, struct sci_port, rx_timer);
14551472 struct dma_chan *chan = s->chan_rx;
....@@ -1459,7 +1476,6 @@
14591476 unsigned long flags;
14601477 unsigned int read;
14611478 int active, count;
1462
- u16 scr;
14631479
14641480 dev_dbg(port->dev, "DMA Rx timed out\n");
14651481
....@@ -1507,15 +1523,9 @@
15071523 }
15081524
15091525 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
1510
- sci_submit_rx(s, true);
1526
+ sci_dma_rx_submit(s, true);
15111527
1512
- /* Direct new serial port interrupts back to CPU */
1513
- scr = serial_port_in(port, SCSCR);
1514
- if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
1515
- scr &= ~SCSCR_RDRQE;
1516
- enable_irq(s->irqs[SCIx_RXI_IRQ]);
1517
- }
1518
- serial_port_out(port, SCSCR, scr | SCSCR_RIE);
1528
+ sci_dma_rx_reenable_irq(s);
15191529
15201530 spin_unlock_irqrestore(&port->lock, flags);
15211531
....@@ -1532,7 +1542,7 @@
15321542 chan = dma_request_slave_channel(port->dev,
15331543 dir == DMA_MEM_TO_DEV ? "tx" : "rx");
15341544 if (!chan) {
1535
- dev_warn(port->dev, "dma_request_slave_channel failed\n");
1545
+ dev_dbg(port->dev, "dma_request_slave_channel failed\n");
15361546 return NULL;
15371547 }
15381548
....@@ -1600,7 +1610,7 @@
16001610 __func__, UART_XMIT_SIZE,
16011611 port->state->xmit.buf, &s->tx_dma_addr);
16021612
1603
- INIT_WORK(&s->work_tx, work_fn_tx);
1613
+ INIT_WORK(&s->work_tx, sci_dma_tx_work_fn);
16041614 s->chan_tx_saved = s->chan_tx = chan;
16051615 }
16061616 }
....@@ -1635,12 +1645,12 @@
16351645 }
16361646
16371647 hrtimer_init(&s->rx_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1638
- s->rx_timer.function = rx_timer_fn;
1648
+ s->rx_timer.function = sci_dma_rx_timer_fn;
16391649
16401650 s->chan_rx_saved = s->chan_rx = chan;
16411651
16421652 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
1643
- sci_submit_rx(s, false);
1653
+ sci_dma_rx_submit(s, false);
16441654 }
16451655 }
16461656
....@@ -1649,9 +1659,9 @@
16491659 struct sci_port *s = to_sci_port(port);
16501660
16511661 if (s->chan_tx_saved)
1652
- sci_tx_dma_release(s);
1662
+ sci_dma_tx_release(s);
16531663 if (s->chan_rx_saved)
1654
- sci_rx_dma_release(s);
1664
+ sci_dma_rx_release(s);
16551665 }
16561666
16571667 static void sci_flush_buffer(struct uart_port *port)
....@@ -1696,7 +1706,7 @@
16961706 disable_irq_nosync(irq);
16971707 scr |= SCSCR_RDRQE;
16981708 } else {
1699
- if (sci_submit_rx(s, false) < 0)
1709
+ if (sci_dma_rx_submit(s, false) < 0)
17001710 goto handle_pio;
17011711
17021712 scr &= ~SCSCR_RIE;
....@@ -1727,7 +1737,7 @@
17271737 * of whether the I_IXOFF is set, otherwise, how is the interrupt
17281738 * to be disabled?
17291739 */
1730
- sci_receive_chars(ptr);
1740
+ sci_receive_chars(port);
17311741
17321742 return IRQ_HANDLED;
17331743 }
....@@ -1787,7 +1797,7 @@
17871797 } else {
17881798 sci_handle_fifo_overrun(port);
17891799 if (!s->chan_rx)
1790
- sci_receive_chars(ptr);
1800
+ sci_receive_chars(port);
17911801 }
17921802
17931803 sci_clear_SCxSR(port, SCxSR_ERROR_CLEAR(port));
....@@ -2106,12 +2116,12 @@
21062116 if (s->autorts) {
21072117 if (sci_get_cts(port))
21082118 mctrl |= TIOCM_CTS;
2109
- } else if (IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(gpios, UART_GPIO_CTS))) {
2119
+ } else if (!mctrl_gpio_to_gpiod(gpios, UART_GPIO_CTS)) {
21102120 mctrl |= TIOCM_CTS;
21112121 }
2112
- if (IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(gpios, UART_GPIO_DSR)))
2122
+ if (!mctrl_gpio_to_gpiod(gpios, UART_GPIO_DSR))
21132123 mctrl |= TIOCM_DSR;
2114
- if (IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(gpios, UART_GPIO_DCD)))
2124
+ if (!mctrl_gpio_to_gpiod(gpios, UART_GPIO_DCD))
21152125 mctrl |= TIOCM_CAR;
21162126
21172127 return mctrl;
....@@ -2382,8 +2392,12 @@
23822392 int best_clk = -1;
23832393 unsigned long flags;
23842394
2385
- if ((termios->c_cflag & CSIZE) == CS7)
2395
+ if ((termios->c_cflag & CSIZE) == CS7) {
23862396 smr_val |= SCSMR_CHR;
2397
+ } else {
2398
+ termios->c_cflag &= ~CSIZE;
2399
+ termios->c_cflag |= CS8;
2400
+ }
23872401 if (termios->c_cflag & PARENB)
23882402 smr_val |= SCSMR_PE;
23892403 if (termios->c_cflag & PARODD)
....@@ -2686,7 +2700,7 @@
26862700 return 0;
26872701
26882702 if (port->dev->of_node || (port->flags & UPF_IOREMAP)) {
2689
- port->membase = ioremap_nocache(port->mapbase, sport->reg_size);
2703
+ port->membase = ioremap(port->mapbase, sport->reg_size);
26902704 if (unlikely(!port->membase)) {
26912705 dev_err(port->dev, "can't remap port#%d\n", port->line);
26922706 return -ENXIO;
....@@ -2893,6 +2907,7 @@
28932907 port->ops = &sci_uart_ops;
28942908 port->iotype = UPIO_MEM;
28952909 port->line = index;
2910
+ port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_SH_SCI_CONSOLE);
28962911
28972912 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
28982913 if (res == NULL)
....@@ -2901,8 +2916,12 @@
29012916 port->mapbase = res->start;
29022917 sci_port->reg_size = resource_size(res);
29032918
2904
- for (i = 0; i < ARRAY_SIZE(sci_port->irqs); ++i)
2905
- sci_port->irqs[i] = platform_get_irq(dev, i);
2919
+ for (i = 0; i < ARRAY_SIZE(sci_port->irqs); ++i) {
2920
+ if (i)
2921
+ sci_port->irqs[i] = platform_get_irq_optional(dev, i);
2922
+ else
2923
+ sci_port->irqs[i] = platform_get_irq(dev, i);
2924
+ }
29062925
29072926 /* The SCI generates several interrupts. They can be muxed together or
29082927 * connected to different interrupt lines. In the muxed case only one
....@@ -3017,12 +3036,9 @@
30173036 unsigned long flags;
30183037 int locked = 1;
30193038
3020
-#if defined(SUPPORT_SYSRQ)
30213039 if (port->sysrq)
30223040 locked = 0;
3023
- else
3024
-#endif
3025
- if (oops_in_progress)
3041
+ else if (oops_in_progress)
30263042 locked = spin_trylock_irqsave(&port->lock, flags);
30273043 else
30283044 spin_lock_irqsave(&port->lock, flags);
....@@ -3093,6 +3109,7 @@
30933109 .data = &sci_uart_driver,
30943110 };
30953111
3112
+#ifdef CONFIG_SUPERH
30963113 static struct console early_serial_console = {
30973114 .name = "early_ttySC",
30983115 .write = serial_console_write,
....@@ -3121,6 +3138,7 @@
31213138 register_console(&early_serial_console);
31223139 return 0;
31233140 }
3141
+#endif
31243142
31253143 #define SCI_CONSOLE (&serial_console)
31263144
....@@ -3157,14 +3175,10 @@
31573175
31583176 sci_cleanup_single(port);
31593177
3160
- if (port->port.fifosize > 1) {
3161
- sysfs_remove_file(&dev->dev.kobj,
3162
- &dev_attr_rx_fifo_trigger.attr);
3163
- }
3164
- if (type == PORT_SCIFA || type == PORT_SCIFB || type == PORT_HSCIF) {
3165
- sysfs_remove_file(&dev->dev.kobj,
3166
- &dev_attr_rx_fifo_timeout.attr);
3167
- }
3178
+ if (port->port.fifosize > 1)
3179
+ device_remove_file(&dev->dev, &dev_attr_rx_fifo_trigger);
3180
+ if (type == PORT_SCIFA || type == PORT_SCIFB || type == PORT_HSCIF)
3181
+ device_remove_file(&dev->dev, &dev_attr_rx_fifo_timeout);
31683182
31693183 return 0;
31703184 }
....@@ -3292,14 +3306,12 @@
32923306 return ret;
32933307
32943308 sciport->gpios = mctrl_gpio_init(&sciport->port, 0);
3295
- if (IS_ERR(sciport->gpios) && PTR_ERR(sciport->gpios) != -ENOSYS)
3309
+ if (IS_ERR(sciport->gpios))
32963310 return PTR_ERR(sciport->gpios);
32973311
32983312 if (sciport->has_rtscts) {
3299
- if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(sciport->gpios,
3300
- UART_GPIO_CTS)) ||
3301
- !IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(sciport->gpios,
3302
- UART_GPIO_RTS))) {
3313
+ if (mctrl_gpio_to_gpiod(sciport->gpios, UART_GPIO_CTS) ||
3314
+ mctrl_gpio_to_gpiod(sciport->gpios, UART_GPIO_RTS)) {
33033315 dev_err(&dev->dev, "Conflicting RTS/CTS config\n");
33043316 return -EINVAL;
33053317 }
....@@ -3327,8 +3339,10 @@
33273339 * the special early probe. We don't have sufficient device state
33283340 * to make it beyond this yet.
33293341 */
3330
- if (is_early_platform_device(dev))
3342
+#ifdef CONFIG_SUPERH
3343
+ if (is_sh_early_platform_device(dev))
33313344 return sci_probe_earlyprintk(dev);
3345
+#endif
33323346
33333347 if (dev->dev.of_node) {
33343348 p = sci_parse_dt(dev, &dev_id);
....@@ -3352,19 +3366,17 @@
33523366 return ret;
33533367
33543368 if (sp->port.fifosize > 1) {
3355
- ret = sysfs_create_file(&dev->dev.kobj,
3356
- &dev_attr_rx_fifo_trigger.attr);
3369
+ ret = device_create_file(&dev->dev, &dev_attr_rx_fifo_trigger);
33573370 if (ret)
33583371 return ret;
33593372 }
33603373 if (sp->port.type == PORT_SCIFA || sp->port.type == PORT_SCIFB ||
33613374 sp->port.type == PORT_HSCIF) {
3362
- ret = sysfs_create_file(&dev->dev.kobj,
3363
- &dev_attr_rx_fifo_timeout.attr);
3375
+ ret = device_create_file(&dev->dev, &dev_attr_rx_fifo_timeout);
33643376 if (ret) {
33653377 if (sp->port.fifosize > 1) {
3366
- sysfs_remove_file(&dev->dev.kobj,
3367
- &dev_attr_rx_fifo_trigger.attr);
3378
+ device_remove_file(&dev->dev,
3379
+ &dev_attr_rx_fifo_trigger);
33683380 }
33693381 return ret;
33703382 }
....@@ -3425,8 +3437,8 @@
34253437 uart_unregister_driver(&sci_uart_driver);
34263438 }
34273439
3428
-#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
3429
-early_platform_init_buffer("earlyprintk", &sci_driver,
3440
+#if defined(CONFIG_SUPERH) && defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
3441
+sh_early_platform_init_buffer("earlyprintk", &sci_driver,
34303442 early_serial_buf, ARRAY_SIZE(early_serial_buf));
34313443 #endif
34323444 #ifdef CONFIG_SERIAL_SH_SCI_EARLYCON
....@@ -3462,6 +3474,12 @@
34623474 {
34633475 return early_console_setup(device, PORT_SCIF);
34643476 }
3477
+static int __init rzscifa_early_console_setup(struct earlycon_device *device,
3478
+ const char *opt)
3479
+{
3480
+ port_cfg.regtype = SCIx_RZ_SCIFA_REGTYPE;
3481
+ return early_console_setup(device, PORT_SCIF);
3482
+}
34653483 static int __init scifa_early_console_setup(struct earlycon_device *device,
34663484 const char *opt)
34673485 {
....@@ -3480,6 +3498,7 @@
34803498
34813499 OF_EARLYCON_DECLARE(sci, "renesas,sci", sci_early_console_setup);
34823500 OF_EARLYCON_DECLARE(scif, "renesas,scif", scif_early_console_setup);
3501
+OF_EARLYCON_DECLARE(scif, "renesas,scif-r7s9210", rzscifa_early_console_setup);
34833502 OF_EARLYCON_DECLARE(scifa, "renesas,scifa", scifa_early_console_setup);
34843503 OF_EARLYCON_DECLARE(scifb, "renesas,scifb", scifb_early_console_setup);
34853504 OF_EARLYCON_DECLARE(hscif, "renesas,hscif", hscif_early_console_setup);