hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
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>
....@@ -35,6 +31,7 @@
3531 #include <linux/ioport.h>
3632 #include <linux/ktime.h>
3733 #include <linux/major.h>
34
+#include <linux/minmax.h>
3835 #include <linux/module.h>
3936 #include <linux/mm.h>
4037 #include <linux/of.h>
....@@ -54,6 +51,7 @@
5451
5552 #ifdef CONFIG_SUPERH
5653 #include <asm/sh_bios.h>
54
+#include <asm/platform_early.h>
5755 #endif
5856
5957 #include "serial_mctrl_gpio.h"
....@@ -1107,9 +1105,8 @@
11071105 scif_set_rtrg(port, 1);
11081106 }
11091107
1110
-static ssize_t rx_trigger_show(struct device *dev,
1111
- struct device_attribute *attr,
1112
- char *buf)
1108
+static ssize_t rx_fifo_trigger_show(struct device *dev,
1109
+ struct device_attribute *attr, char *buf)
11131110 {
11141111 struct uart_port *port = dev_get_drvdata(dev);
11151112 struct sci_port *sci = to_sci_port(port);
....@@ -1117,10 +1114,9 @@
11171114 return sprintf(buf, "%d\n", sci->rx_trigger);
11181115 }
11191116
1120
-static ssize_t rx_trigger_store(struct device *dev,
1121
- struct device_attribute *attr,
1122
- const char *buf,
1123
- size_t count)
1117
+static ssize_t rx_fifo_trigger_store(struct device *dev,
1118
+ struct device_attribute *attr,
1119
+ const char *buf, size_t count)
11241120 {
11251121 struct uart_port *port = dev_get_drvdata(dev);
11261122 struct sci_port *sci = to_sci_port(port);
....@@ -1138,7 +1134,7 @@
11381134 return count;
11391135 }
11401136
1141
-static DEVICE_ATTR(rx_fifo_trigger, 0644, rx_trigger_show, rx_trigger_store);
1137
+static DEVICE_ATTR_RW(rx_fifo_trigger);
11421138
11431139 static ssize_t rx_fifo_timeout_show(struct device *dev,
11441140 struct device_attribute *attr,
....@@ -1248,12 +1244,22 @@
12481244 return -1;
12491245 }
12501246
1251
-static void sci_rx_dma_release(struct sci_port *s)
1247
+static void sci_dma_rx_chan_invalidate(struct sci_port *s)
1248
+{
1249
+ unsigned int i;
1250
+
1251
+ s->chan_rx = NULL;
1252
+ for (i = 0; i < ARRAY_SIZE(s->cookie_rx); i++)
1253
+ s->cookie_rx[i] = -EINVAL;
1254
+ s->active_rx = 0;
1255
+}
1256
+
1257
+static void sci_dma_rx_release(struct sci_port *s)
12521258 {
12531259 struct dma_chan *chan = s->chan_rx_saved;
12541260
1255
- s->chan_rx_saved = s->chan_rx = NULL;
1256
- s->cookie_rx[0] = s->cookie_rx[1] = -EINVAL;
1261
+ s->chan_rx_saved = NULL;
1262
+ sci_dma_rx_chan_invalidate(s);
12571263 dmaengine_terminate_sync(chan);
12581264 dma_free_coherent(chan->device->dev, s->buf_len_rx * 2, s->rx_buf[0],
12591265 sg_dma_address(&s->sg_rx[0]));
....@@ -1267,6 +1273,20 @@
12671273 ktime_t t = ktime_set(sec, nsec);
12681274
12691275 hrtimer_start(hrt, t, HRTIMER_MODE_REL);
1276
+}
1277
+
1278
+static void sci_dma_rx_reenable_irq(struct sci_port *s)
1279
+{
1280
+ struct uart_port *port = &s->port;
1281
+ u16 scr;
1282
+
1283
+ /* Direct new serial port interrupts back to CPU */
1284
+ scr = serial_port_in(port, SCSCR);
1285
+ if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
1286
+ scr &= ~SCSCR_RDRQE;
1287
+ enable_irq(s->irqs[SCIx_RXI_IRQ]);
1288
+ }
1289
+ serial_port_out(port, SCSCR, scr | SCSCR_RIE);
12701290 }
12711291
12721292 static void sci_dma_rx_complete(void *arg)
....@@ -1318,12 +1338,13 @@
13181338 dev_warn(port->dev, "Failed submitting Rx DMA descriptor\n");
13191339 /* Switch to PIO */
13201340 spin_lock_irqsave(&port->lock, flags);
1321
- s->chan_rx = NULL;
1322
- sci_start_rx(port);
1341
+ dmaengine_terminate_async(chan);
1342
+ sci_dma_rx_chan_invalidate(s);
1343
+ sci_dma_rx_reenable_irq(s);
13231344 spin_unlock_irqrestore(&port->lock, flags);
13241345 }
13251346
1326
-static void sci_tx_dma_release(struct sci_port *s)
1347
+static void sci_dma_tx_release(struct sci_port *s)
13271348 {
13281349 struct dma_chan *chan = s->chan_tx_saved;
13291350
....@@ -1336,7 +1357,7 @@
13361357 dma_release_channel(chan);
13371358 }
13381359
1339
-static int sci_submit_rx(struct sci_port *s, bool port_lock_held)
1360
+static int sci_dma_rx_submit(struct sci_port *s, bool port_lock_held)
13401361 {
13411362 struct dma_chan *chan = s->chan_rx;
13421363 struct uart_port *port = &s->port;
....@@ -1372,17 +1393,14 @@
13721393 spin_lock_irqsave(&port->lock, flags);
13731394 if (i)
13741395 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;
1396
+ sci_dma_rx_chan_invalidate(s);
13791397 sci_start_rx(port);
13801398 if (!port_lock_held)
13811399 spin_unlock_irqrestore(&port->lock, flags);
13821400 return -EAGAIN;
13831401 }
13841402
1385
-static void work_fn_tx(struct work_struct *work)
1403
+static void sci_dma_tx_work_fn(struct work_struct *work)
13861404 {
13871405 struct sci_port *s = container_of(work, struct sci_port, work_tx);
13881406 struct dma_async_tx_descriptor *desc;
....@@ -1449,7 +1467,7 @@
14491467 return;
14501468 }
14511469
1452
-static enum hrtimer_restart rx_timer_fn(struct hrtimer *t)
1470
+static enum hrtimer_restart sci_dma_rx_timer_fn(struct hrtimer *t)
14531471 {
14541472 struct sci_port *s = container_of(t, struct sci_port, rx_timer);
14551473 struct dma_chan *chan = s->chan_rx;
....@@ -1459,7 +1477,6 @@
14591477 unsigned long flags;
14601478 unsigned int read;
14611479 int active, count;
1462
- u16 scr;
14631480
14641481 dev_dbg(port->dev, "DMA Rx timed out\n");
14651482
....@@ -1507,15 +1524,9 @@
15071524 }
15081525
15091526 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
1510
- sci_submit_rx(s, true);
1527
+ sci_dma_rx_submit(s, true);
15111528
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);
1529
+ sci_dma_rx_reenable_irq(s);
15191530
15201531 spin_unlock_irqrestore(&port->lock, flags);
15211532
....@@ -1532,7 +1543,7 @@
15321543 chan = dma_request_slave_channel(port->dev,
15331544 dir == DMA_MEM_TO_DEV ? "tx" : "rx");
15341545 if (!chan) {
1535
- dev_warn(port->dev, "dma_request_slave_channel failed\n");
1546
+ dev_dbg(port->dev, "dma_request_slave_channel failed\n");
15361547 return NULL;
15371548 }
15381549
....@@ -1600,7 +1611,7 @@
16001611 __func__, UART_XMIT_SIZE,
16011612 port->state->xmit.buf, &s->tx_dma_addr);
16021613
1603
- INIT_WORK(&s->work_tx, work_fn_tx);
1614
+ INIT_WORK(&s->work_tx, sci_dma_tx_work_fn);
16041615 s->chan_tx_saved = s->chan_tx = chan;
16051616 }
16061617 }
....@@ -1635,12 +1646,12 @@
16351646 }
16361647
16371648 hrtimer_init(&s->rx_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1638
- s->rx_timer.function = rx_timer_fn;
1649
+ s->rx_timer.function = sci_dma_rx_timer_fn;
16391650
16401651 s->chan_rx_saved = s->chan_rx = chan;
16411652
16421653 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
1643
- sci_submit_rx(s, false);
1654
+ sci_dma_rx_submit(s, false);
16441655 }
16451656 }
16461657
....@@ -1649,9 +1660,9 @@
16491660 struct sci_port *s = to_sci_port(port);
16501661
16511662 if (s->chan_tx_saved)
1652
- sci_tx_dma_release(s);
1663
+ sci_dma_tx_release(s);
16531664 if (s->chan_rx_saved)
1654
- sci_rx_dma_release(s);
1665
+ sci_dma_rx_release(s);
16551666 }
16561667
16571668 static void sci_flush_buffer(struct uart_port *port)
....@@ -1696,7 +1707,7 @@
16961707 disable_irq_nosync(irq);
16971708 scr |= SCSCR_RDRQE;
16981709 } else {
1699
- if (sci_submit_rx(s, false) < 0)
1710
+ if (sci_dma_rx_submit(s, false) < 0)
17001711 goto handle_pio;
17011712
17021713 scr &= ~SCSCR_RIE;
....@@ -1727,7 +1738,7 @@
17271738 * of whether the I_IXOFF is set, otherwise, how is the interrupt
17281739 * to be disabled?
17291740 */
1730
- sci_receive_chars(ptr);
1741
+ sci_receive_chars(port);
17311742
17321743 return IRQ_HANDLED;
17331744 }
....@@ -1787,7 +1798,7 @@
17871798 } else {
17881799 sci_handle_fifo_overrun(port);
17891800 if (!s->chan_rx)
1790
- sci_receive_chars(ptr);
1801
+ sci_receive_chars(port);
17911802 }
17921803
17931804 sci_clear_SCxSR(port, SCxSR_ERROR_CLEAR(port));
....@@ -2106,12 +2117,12 @@
21062117 if (s->autorts) {
21072118 if (sci_get_cts(port))
21082119 mctrl |= TIOCM_CTS;
2109
- } else if (IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(gpios, UART_GPIO_CTS))) {
2120
+ } else if (!mctrl_gpio_to_gpiod(gpios, UART_GPIO_CTS)) {
21102121 mctrl |= TIOCM_CTS;
21112122 }
2112
- if (IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(gpios, UART_GPIO_DSR)))
2123
+ if (!mctrl_gpio_to_gpiod(gpios, UART_GPIO_DSR))
21132124 mctrl |= TIOCM_DSR;
2114
- if (IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(gpios, UART_GPIO_DCD)))
2125
+ if (!mctrl_gpio_to_gpiod(gpios, UART_GPIO_DCD))
21152126 mctrl |= TIOCM_CAR;
21162127
21172128 return mctrl;
....@@ -2382,8 +2393,12 @@
23822393 int best_clk = -1;
23832394 unsigned long flags;
23842395
2385
- if ((termios->c_cflag & CSIZE) == CS7)
2396
+ if ((termios->c_cflag & CSIZE) == CS7) {
23862397 smr_val |= SCSMR_CHR;
2398
+ } else {
2399
+ termios->c_cflag &= ~CSIZE;
2400
+ termios->c_cflag |= CS8;
2401
+ }
23872402 if (termios->c_cflag & PARENB)
23882403 smr_val |= SCSMR_PE;
23892404 if (termios->c_cflag & PARODD)
....@@ -2686,7 +2701,7 @@
26862701 return 0;
26872702
26882703 if (port->dev->of_node || (port->flags & UPF_IOREMAP)) {
2689
- port->membase = ioremap_nocache(port->mapbase, sport->reg_size);
2704
+ port->membase = ioremap(port->mapbase, sport->reg_size);
26902705 if (unlikely(!port->membase)) {
26912706 dev_err(port->dev, "can't remap port#%d\n", port->line);
26922707 return -ENXIO;
....@@ -2893,6 +2908,7 @@
28932908 port->ops = &sci_uart_ops;
28942909 port->iotype = UPIO_MEM;
28952910 port->line = index;
2911
+ port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_SH_SCI_CONSOLE);
28962912
28972913 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
28982914 if (res == NULL)
....@@ -2901,8 +2917,19 @@
29012917 port->mapbase = res->start;
29022918 sci_port->reg_size = resource_size(res);
29032919
2904
- for (i = 0; i < ARRAY_SIZE(sci_port->irqs); ++i)
2905
- sci_port->irqs[i] = platform_get_irq(dev, i);
2920
+ for (i = 0; i < ARRAY_SIZE(sci_port->irqs); ++i) {
2921
+ if (i)
2922
+ sci_port->irqs[i] = platform_get_irq_optional(dev, i);
2923
+ else
2924
+ sci_port->irqs[i] = platform_get_irq(dev, i);
2925
+ }
2926
+
2927
+ /*
2928
+ * The fourth interrupt on SCI port is transmit end interrupt, so
2929
+ * shuffle the interrupts.
2930
+ */
2931
+ if (p->type == PORT_SCI)
2932
+ swap(sci_port->irqs[SCIx_BRI_IRQ], sci_port->irqs[SCIx_TEI_IRQ]);
29062933
29072934 /* The SCI generates several interrupts. They can be muxed together or
29082935 * connected to different interrupt lines. In the muxed case only one
....@@ -2969,7 +2996,7 @@
29692996 port->flags = UPF_FIXED_PORT | UPF_BOOT_AUTOCONF | p->flags;
29702997 port->fifosize = sci_port->params->fifosize;
29712998
2972
- if (port->type == PORT_SCI) {
2999
+ if (port->type == PORT_SCI && !dev->dev.of_node) {
29733000 if (sci_port->reg_size >= 0x20)
29743001 port->regshift = 2;
29753002 else
....@@ -3017,12 +3044,9 @@
30173044 unsigned long flags;
30183045 int locked = 1;
30193046
3020
-#if defined(SUPPORT_SYSRQ)
30213047 if (port->sysrq)
30223048 locked = 0;
3023
- else
3024
-#endif
3025
- if (oops_in_progress)
3049
+ else if (oops_in_progress)
30263050 locked = spin_trylock_irqsave(&port->lock, flags);
30273051 else
30283052 spin_lock_irqsave(&port->lock, flags);
....@@ -3093,6 +3117,7 @@
30933117 .data = &sci_uart_driver,
30943118 };
30953119
3120
+#ifdef CONFIG_SUPERH
30963121 static struct console early_serial_console = {
30973122 .name = "early_ttySC",
30983123 .write = serial_console_write,
....@@ -3121,6 +3146,7 @@
31213146 register_console(&early_serial_console);
31223147 return 0;
31233148 }
3149
+#endif
31243150
31253151 #define SCI_CONSOLE (&serial_console)
31263152
....@@ -3157,14 +3183,10 @@
31573183
31583184 sci_cleanup_single(port);
31593185
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
- }
3186
+ if (port->port.fifosize > 1)
3187
+ device_remove_file(&dev->dev, &dev_attr_rx_fifo_trigger);
3188
+ if (type == PORT_SCIFA || type == PORT_SCIFB || type == PORT_HSCIF)
3189
+ device_remove_file(&dev->dev, &dev_attr_rx_fifo_timeout);
31683190
31693191 return 0;
31703192 }
....@@ -3292,14 +3314,12 @@
32923314 return ret;
32933315
32943316 sciport->gpios = mctrl_gpio_init(&sciport->port, 0);
3295
- if (IS_ERR(sciport->gpios) && PTR_ERR(sciport->gpios) != -ENOSYS)
3317
+ if (IS_ERR(sciport->gpios))
32963318 return PTR_ERR(sciport->gpios);
32973319
32983320 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))) {
3321
+ if (mctrl_gpio_to_gpiod(sciport->gpios, UART_GPIO_CTS) ||
3322
+ mctrl_gpio_to_gpiod(sciport->gpios, UART_GPIO_RTS)) {
33033323 dev_err(&dev->dev, "Conflicting RTS/CTS config\n");
33043324 return -EINVAL;
33053325 }
....@@ -3327,8 +3347,10 @@
33273347 * the special early probe. We don't have sufficient device state
33283348 * to make it beyond this yet.
33293349 */
3330
- if (is_early_platform_device(dev))
3350
+#ifdef CONFIG_SUPERH
3351
+ if (is_sh_early_platform_device(dev))
33313352 return sci_probe_earlyprintk(dev);
3353
+#endif
33323354
33333355 if (dev->dev.of_node) {
33343356 p = sci_parse_dt(dev, &dev_id);
....@@ -3352,19 +3374,17 @@
33523374 return ret;
33533375
33543376 if (sp->port.fifosize > 1) {
3355
- ret = sysfs_create_file(&dev->dev.kobj,
3356
- &dev_attr_rx_fifo_trigger.attr);
3377
+ ret = device_create_file(&dev->dev, &dev_attr_rx_fifo_trigger);
33573378 if (ret)
33583379 return ret;
33593380 }
33603381 if (sp->port.type == PORT_SCIFA || sp->port.type == PORT_SCIFB ||
33613382 sp->port.type == PORT_HSCIF) {
3362
- ret = sysfs_create_file(&dev->dev.kobj,
3363
- &dev_attr_rx_fifo_timeout.attr);
3383
+ ret = device_create_file(&dev->dev, &dev_attr_rx_fifo_timeout);
33643384 if (ret) {
33653385 if (sp->port.fifosize > 1) {
3366
- sysfs_remove_file(&dev->dev.kobj,
3367
- &dev_attr_rx_fifo_trigger.attr);
3386
+ device_remove_file(&dev->dev,
3387
+ &dev_attr_rx_fifo_trigger);
33683388 }
33693389 return ret;
33703390 }
....@@ -3425,8 +3445,8 @@
34253445 uart_unregister_driver(&sci_uart_driver);
34263446 }
34273447
3428
-#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
3429
-early_platform_init_buffer("earlyprintk", &sci_driver,
3448
+#if defined(CONFIG_SUPERH) && defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
3449
+sh_early_platform_init_buffer("earlyprintk", &sci_driver,
34303450 early_serial_buf, ARRAY_SIZE(early_serial_buf));
34313451 #endif
34323452 #ifdef CONFIG_SERIAL_SH_SCI_EARLYCON
....@@ -3462,6 +3482,12 @@
34623482 {
34633483 return early_console_setup(device, PORT_SCIF);
34643484 }
3485
+static int __init rzscifa_early_console_setup(struct earlycon_device *device,
3486
+ const char *opt)
3487
+{
3488
+ port_cfg.regtype = SCIx_RZ_SCIFA_REGTYPE;
3489
+ return early_console_setup(device, PORT_SCIF);
3490
+}
34653491 static int __init scifa_early_console_setup(struct earlycon_device *device,
34663492 const char *opt)
34673493 {
....@@ -3480,6 +3506,7 @@
34803506
34813507 OF_EARLYCON_DECLARE(sci, "renesas,sci", sci_early_console_setup);
34823508 OF_EARLYCON_DECLARE(scif, "renesas,scif", scif_early_console_setup);
3509
+OF_EARLYCON_DECLARE(scif, "renesas,scif-r7s9210", rzscifa_early_console_setup);
34833510 OF_EARLYCON_DECLARE(scifa, "renesas,scifa", scifa_early_console_setup);
34843511 OF_EARLYCON_DECLARE(scifb, "renesas,scifb", scifb_early_console_setup);
34853512 OF_EARLYCON_DECLARE(hscif, "renesas,hscif", hscif_early_console_setup);