aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/tty
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2013-09-03 14:38:36 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2013-09-03 14:38:36 -0400
commit2f01ea908bcf838e815c0124b579513dbda3b8c8 (patch)
treef42db47c1695daaf369f08a21b2267d4a8ece756 /drivers/tty
parent751144271f4b63d5de9005ea4e5e6e5c7c6fd629 (diff)
parent2d1d3f3ae985ec5676fb56ff2c7acad2e1c4e6eb (diff)
Merge tag 'tty-3.12-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/tty
Pull tty/serial driver patches from Greg KH: "Here's the big tty/serial driver pull request for 3.12-rc1. Lots of n_tty reworks to resolve some very long-standing issues, removing the 3-4 different locks that were taken for every character. This code has been beaten on for a long time in linux-next with no reported regressions. Other than that, a range of serial and tty driver updates and revisions. Full details in the shortlog" * tag 'tty-3.12-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/tty: (226 commits) hvc_xen: Remove unnecessary __GFP_ZERO from kzalloc serial: imx: initialize the local variable tty: ar933x_uart: add device tree support and binding documentation tty: ar933x_uart: allow to build the driver as a module ARM: dts: msm: Update uartdm compatible strings devicetree: serial: Document msm_serial bindings serial: unify serial bindings into a single dir serial: fsl-imx-uart: Cleanup duplicate device tree binding tty: ar933x_uart: use config_enabled() macro to clean up ifdefs tty: ar933x_uart: remove superfluous assignment of ar933x_uart_driver.nr tty: ar933x_uart: use the clk API to get the uart clock tty: serial: cpm_uart: Adding proper request of GPIO used by cpm_uart driver serial: sirf: fix the amount of serial ports serial: sirf: define macro for some magic numbers of USP serial: icom: move array overflow checks earlier TTY: amiserial, remove unnecessary platform_set_drvdata() serial: st-asc: remove unnecessary platform_set_drvdata() msm_serial: Send more than 1 character on the console w/ UARTDM msm_serial: Add support for non-GSBI UARTDM devices msm_serial: Switch clock consumer strings and simplify code ...
Diffstat (limited to 'drivers/tty')
-rw-r--r--drivers/tty/amiserial.c2
-rw-r--r--drivers/tty/hvc/hvc_console.c11
-rw-r--r--drivers/tty/hvc/hvc_console.h3
-rw-r--r--drivers/tty/hvc/hvc_iucv.c64
-rw-r--r--drivers/tty/hvc/hvc_xen.c6
-rw-r--r--drivers/tty/n_gsm.c29
-rw-r--r--drivers/tty/n_tty.c1414
-rw-r--r--drivers/tty/pty.c18
-rw-r--r--drivers/tty/serial/8250/8250_core.c2
-rw-r--r--drivers/tty/serial/8250/8250_dw.c34
-rw-r--r--drivers/tty/serial/8250/8250_early.c2
-rw-r--r--drivers/tty/serial/8250/8250_em.c27
-rw-r--r--drivers/tty/serial/8250/8250_pci.c15
-rw-r--r--drivers/tty/serial/8250/Kconfig2
-rw-r--r--drivers/tty/serial/Kconfig33
-rw-r--r--drivers/tty/serial/Makefile1
-rw-r--r--drivers/tty/serial/altera_jtaguart.c5
-rw-r--r--drivers/tty/serial/altera_uart.c4
-rw-r--r--drivers/tty/serial/amba-pl010.c2
-rw-r--r--drivers/tty/serial/amba-pl011.c18
-rw-r--r--drivers/tty/serial/apbuart.c2
-rw-r--r--drivers/tty/serial/ar933x_uart.c111
-rw-r--r--drivers/tty/serial/arc_uart.c35
-rw-r--r--drivers/tty/serial/atmel_serial.c857
-rw-r--r--drivers/tty/serial/bcm63xx_uart.c3
-rw-r--r--drivers/tty/serial/bfin_sport_uart.c14
-rw-r--r--drivers/tty/serial/bfin_uart.c21
-rw-r--r--drivers/tty/serial/clps711x.c11
-rw-r--r--drivers/tty/serial/cpm_uart/cpm_uart_core.c28
-rw-r--r--drivers/tty/serial/efm32-uart.c29
-rw-r--r--drivers/tty/serial/fsl_lpuart.c7
-rw-r--r--drivers/tty/serial/icom.c103
-rw-r--r--drivers/tty/serial/ifx6x60.c2
-rw-r--r--drivers/tty/serial/imx.c520
-rw-r--r--drivers/tty/serial/ioc4_serial.c4
-rw-r--r--drivers/tty/serial/lantiq.c7
-rw-r--r--drivers/tty/serial/lpc32xx_hs.c7
-rw-r--r--drivers/tty/serial/m32r_sio.c3
-rw-r--r--drivers/tty/serial/max3100.c2
-rw-r--r--drivers/tty/serial/max310x.c991
-rw-r--r--drivers/tty/serial/mcf.c5
-rw-r--r--drivers/tty/serial/mfd.c14
-rw-r--r--drivers/tty/serial/mpsc.c15
-rw-r--r--drivers/tty/serial/mrst_max3110.c4
-rw-r--r--drivers/tty/serial/msm_serial.c276
-rw-r--r--drivers/tty/serial/msm_serial.h19
-rw-r--r--drivers/tty/serial/msm_serial_hs.c2
-rw-r--r--drivers/tty/serial/mxs-auart.c16
-rw-r--r--drivers/tty/serial/netx-serial.c8
-rw-r--r--drivers/tty/serial/nwpserial.c3
-rw-r--r--drivers/tty/serial/omap-serial.c214
-rw-r--r--drivers/tty/serial/pch_uart.c84
-rw-r--r--drivers/tty/serial/pmac_zilog.c1
-rw-r--r--drivers/tty/serial/pnx8xxx_uart.c5
-rw-r--r--drivers/tty/serial/pxa.c33
-rw-r--r--drivers/tty/serial/rp2.c2
-rw-r--r--drivers/tty/serial/sa1100.c5
-rw-r--r--drivers/tty/serial/samsung.c9
-rw-r--r--drivers/tty/serial/samsung.h3
-rw-r--r--drivers/tty/serial/sc26xx.c2
-rw-r--r--drivers/tty/serial/sccnxp.c339
-rw-r--r--drivers/tty/serial/serial-tegra.c16
-rw-r--r--drivers/tty/serial/serial_core.c4
-rw-r--r--drivers/tty/serial/serial_txx9.c2
-rw-r--r--drivers/tty/serial/sh-sci.c4
-rw-r--r--drivers/tty/serial/sirfsoc_uart.c1195
-rw-r--r--drivers/tty/serial/sirfsoc_uart.h501
-rw-r--r--drivers/tty/serial/st-asc.c932
-rw-r--r--drivers/tty/serial/timbuart.c4
-rw-r--r--drivers/tty/serial/vr41xx_siu.c2
-rw-r--r--drivers/tty/serial/vt8500_serial.c3
-rw-r--r--drivers/tty/synclink.c130
-rw-r--r--drivers/tty/tty_buffer.c417
-rw-r--r--drivers/tty/tty_io.c33
-rw-r--r--drivers/tty/tty_ioctl.c90
-rw-r--r--drivers/tty/tty_ldisc.c461
-rw-r--r--drivers/tty/vt/keyboard.c21
-rw-r--r--drivers/tty/vt/selection.c8
-rw-r--r--drivers/tty/vt/vt.c8
79 files changed, 6422 insertions, 2917 deletions
diff --git a/drivers/tty/amiserial.c b/drivers/tty/amiserial.c
index 083710e02367..2b86f8e0fb58 100644
--- a/drivers/tty/amiserial.c
+++ b/drivers/tty/amiserial.c
@@ -1785,8 +1785,6 @@ static int __exit amiga_serial_remove(struct platform_device *pdev)
1785 free_irq(IRQ_AMIGA_TBE, state); 1785 free_irq(IRQ_AMIGA_TBE, state);
1786 free_irq(IRQ_AMIGA_RBF, state); 1786 free_irq(IRQ_AMIGA_RBF, state);
1787 1787
1788 platform_set_drvdata(pdev, NULL);
1789
1790 return error; 1788 return error;
1791} 1789}
1792 1790
diff --git a/drivers/tty/hvc/hvc_console.c b/drivers/tty/hvc/hvc_console.c
index eb255e807c06..9eba119bcdd3 100644
--- a/drivers/tty/hvc/hvc_console.c
+++ b/drivers/tty/hvc/hvc_console.c
@@ -361,7 +361,12 @@ static int hvc_open(struct tty_struct *tty, struct file * filp)
361 tty->driver_data = NULL; 361 tty->driver_data = NULL;
362 tty_port_put(&hp->port); 362 tty_port_put(&hp->port);
363 printk(KERN_ERR "hvc_open: request_irq failed with rc %d.\n", rc); 363 printk(KERN_ERR "hvc_open: request_irq failed with rc %d.\n", rc);
364 } 364 } else
365 /* We are ready... raise DTR/RTS */
366 if (C_BAUD(tty))
367 if (hp->ops->dtr_rts)
368 hp->ops->dtr_rts(hp, 1);
369
365 /* Force wakeup of the polling thread */ 370 /* Force wakeup of the polling thread */
366 hvc_kick(); 371 hvc_kick();
367 372
@@ -393,6 +398,10 @@ static void hvc_close(struct tty_struct *tty, struct file * filp)
393 /* We are done with the tty pointer now. */ 398 /* We are done with the tty pointer now. */
394 tty_port_tty_set(&hp->port, NULL); 399 tty_port_tty_set(&hp->port, NULL);
395 400
401 if (C_HUPCL(tty))
402 if (hp->ops->dtr_rts)
403 hp->ops->dtr_rts(hp, 0);
404
396 if (hp->ops->notifier_del) 405 if (hp->ops->notifier_del)
397 hp->ops->notifier_del(hp, hp->data); 406 hp->ops->notifier_del(hp, hp->data);
398 407
diff --git a/drivers/tty/hvc/hvc_console.h b/drivers/tty/hvc/hvc_console.h
index 674d23cb919a..913101980827 100644
--- a/drivers/tty/hvc/hvc_console.h
+++ b/drivers/tty/hvc/hvc_console.h
@@ -75,6 +75,9 @@ struct hv_ops {
75 /* tiocmget/set implementation */ 75 /* tiocmget/set implementation */
76 int (*tiocmget)(struct hvc_struct *hp); 76 int (*tiocmget)(struct hvc_struct *hp);
77 int (*tiocmset)(struct hvc_struct *hp, unsigned int set, unsigned int clear); 77 int (*tiocmset)(struct hvc_struct *hp, unsigned int set, unsigned int clear);
78
79 /* Callbacks to handle tty ports */
80 void (*dtr_rts)(struct hvc_struct *hp, int raise);
78}; 81};
79 82
80/* Register a vterm and a slot index for use as a console (console_init) */ 83/* Register a vterm and a slot index for use as a console (console_init) */
diff --git a/drivers/tty/hvc/hvc_iucv.c b/drivers/tty/hvc/hvc_iucv.c
index 9d47f50c2755..fd17a9b804b8 100644
--- a/drivers/tty/hvc/hvc_iucv.c
+++ b/drivers/tty/hvc/hvc_iucv.c
@@ -656,21 +656,64 @@ static void hvc_iucv_notifier_hangup(struct hvc_struct *hp, int id)
656} 656}
657 657
658/** 658/**
659 * hvc_iucv_dtr_rts() - HVC notifier for handling DTR/RTS
660 * @hp: Pointer the HVC device (struct hvc_struct)
661 * @raise: Non-zero to raise or zero to lower DTR/RTS lines
662 *
663 * This routine notifies the HVC back-end to raise or lower DTR/RTS
664 * lines. Raising DTR/RTS is ignored. Lowering DTR/RTS indicates to
665 * drop the IUCV connection (similar to hang up the modem).
666 */
667static void hvc_iucv_dtr_rts(struct hvc_struct *hp, int raise)
668{
669 struct hvc_iucv_private *priv;
670 struct iucv_path *path;
671
672 /* Raising the DTR/RTS is ignored as IUCV connections can be
673 * established at any times.
674 */
675 if (raise)
676 return;
677
678 priv = hvc_iucv_get_private(hp->vtermno);
679 if (!priv)
680 return;
681
682 /* Lowering the DTR/RTS lines disconnects an established IUCV
683 * connection.
684 */
685 flush_sndbuf_sync(priv);
686
687 spin_lock_bh(&priv->lock);
688 path = priv->path; /* save reference to IUCV path */
689 priv->path = NULL;
690 priv->iucv_state = IUCV_DISCONN;
691 spin_unlock_bh(&priv->lock);
692
693 /* Sever IUCV path outside of priv->lock due to lock ordering of:
694 * priv->lock <--> iucv_table_lock */
695 if (path) {
696 iucv_path_sever(path, NULL);
697 iucv_path_free(path);
698 }
699}
700
701/**
659 * hvc_iucv_notifier_del() - HVC notifier for closing a TTY for the last time. 702 * hvc_iucv_notifier_del() - HVC notifier for closing a TTY for the last time.
660 * @hp: Pointer to the HVC device (struct hvc_struct) 703 * @hp: Pointer to the HVC device (struct hvc_struct)
661 * @id: Additional data (originally passed to hvc_alloc): 704 * @id: Additional data (originally passed to hvc_alloc):
662 * the index of an struct hvc_iucv_private instance. 705 * the index of an struct hvc_iucv_private instance.
663 * 706 *
664 * This routine notifies the HVC back-end that the last tty device fd has been 707 * This routine notifies the HVC back-end that the last tty device fd has been
665 * closed. The function calls hvc_iucv_cleanup() to clean up the struct 708 * closed. The function cleans up tty resources. The clean-up of the IUCV
666 * hvc_iucv_private instance. 709 * connection is done in hvc_iucv_dtr_rts() and depends on the HUPCL termios
710 * control setting.
667 * 711 *
668 * Locking: struct hvc_iucv_private->lock 712 * Locking: struct hvc_iucv_private->lock
669 */ 713 */
670static void hvc_iucv_notifier_del(struct hvc_struct *hp, int id) 714static void hvc_iucv_notifier_del(struct hvc_struct *hp, int id)
671{ 715{
672 struct hvc_iucv_private *priv; 716 struct hvc_iucv_private *priv;
673 struct iucv_path *path;
674 717
675 priv = hvc_iucv_get_private(id); 718 priv = hvc_iucv_get_private(id);
676 if (!priv) 719 if (!priv)
@@ -679,17 +722,11 @@ static void hvc_iucv_notifier_del(struct hvc_struct *hp, int id)
679 flush_sndbuf_sync(priv); 722 flush_sndbuf_sync(priv);
680 723
681 spin_lock_bh(&priv->lock); 724 spin_lock_bh(&priv->lock);
682 path = priv->path; /* save reference to IUCV path */ 725 destroy_tty_buffer_list(&priv->tty_outqueue);
683 priv->path = NULL; 726 destroy_tty_buffer_list(&priv->tty_inqueue);
684 hvc_iucv_cleanup(priv); 727 priv->tty_state = TTY_CLOSED;
728 priv->sndbuf_len = 0;
685 spin_unlock_bh(&priv->lock); 729 spin_unlock_bh(&priv->lock);
686
687 /* sever IUCV path outside of priv->lock due to lock ordering of:
688 * priv->lock <--> iucv_table_lock */
689 if (path) {
690 iucv_path_sever(path, NULL);
691 iucv_path_free(path);
692 }
693} 730}
694 731
695/** 732/**
@@ -931,6 +968,7 @@ static const struct hv_ops hvc_iucv_ops = {
931 .notifier_add = hvc_iucv_notifier_add, 968 .notifier_add = hvc_iucv_notifier_add,
932 .notifier_del = hvc_iucv_notifier_del, 969 .notifier_del = hvc_iucv_notifier_del,
933 .notifier_hangup = hvc_iucv_notifier_hangup, 970 .notifier_hangup = hvc_iucv_notifier_hangup,
971 .dtr_rts = hvc_iucv_dtr_rts,
934}; 972};
935 973
936/* Suspend / resume device operations */ 974/* Suspend / resume device operations */
diff --git a/drivers/tty/hvc/hvc_xen.c b/drivers/tty/hvc/hvc_xen.c
index 682210d778bd..e61c36cbb866 100644
--- a/drivers/tty/hvc/hvc_xen.c
+++ b/drivers/tty/hvc/hvc_xen.c
@@ -208,7 +208,7 @@ static int xen_hvm_console_init(void)
208 208
209 info = vtermno_to_xencons(HVC_COOKIE); 209 info = vtermno_to_xencons(HVC_COOKIE);
210 if (!info) { 210 if (!info) {
211 info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL | __GFP_ZERO); 211 info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL);
212 if (!info) 212 if (!info)
213 return -ENOMEM; 213 return -ENOMEM;
214 } else if (info->intf != NULL) { 214 } else if (info->intf != NULL) {
@@ -257,7 +257,7 @@ static int xen_pv_console_init(void)
257 257
258 info = vtermno_to_xencons(HVC_COOKIE); 258 info = vtermno_to_xencons(HVC_COOKIE);
259 if (!info) { 259 if (!info) {
260 info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL | __GFP_ZERO); 260 info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL);
261 if (!info) 261 if (!info)
262 return -ENOMEM; 262 return -ENOMEM;
263 } else if (info->intf != NULL) { 263 } else if (info->intf != NULL) {
@@ -284,7 +284,7 @@ static int xen_initial_domain_console_init(void)
284 284
285 info = vtermno_to_xencons(HVC_COOKIE); 285 info = vtermno_to_xencons(HVC_COOKIE);
286 if (!info) { 286 if (!info) {
287 info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL | __GFP_ZERO); 287 info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL);
288 if (!info) 288 if (!info)
289 return -ENOMEM; 289 return -ENOMEM;
290 } 290 }
diff --git a/drivers/tty/n_gsm.c b/drivers/tty/n_gsm.c
index 642239015b46..c0f76da55304 100644
--- a/drivers/tty/n_gsm.c
+++ b/drivers/tty/n_gsm.c
@@ -807,7 +807,7 @@ static int gsm_dlci_data_output(struct gsm_mux *gsm, struct gsm_dlci *dlci)
807 int h = dlci->adaption - 1; 807 int h = dlci->adaption - 1;
808 808
809 total_size = 0; 809 total_size = 0;
810 while(1) { 810 while (1) {
811 len = kfifo_len(dlci->fifo); 811 len = kfifo_len(dlci->fifo);
812 if (len == 0) 812 if (len == 0)
813 return total_size; 813 return total_size;
@@ -827,8 +827,8 @@ static int gsm_dlci_data_output(struct gsm_mux *gsm, struct gsm_dlci *dlci)
827 switch (dlci->adaption) { 827 switch (dlci->adaption) {
828 case 1: /* Unstructured */ 828 case 1: /* Unstructured */
829 break; 829 break;
830 case 2: /* Unstructed with modem bits. Always one byte as we never 830 case 2: /* Unstructed with modem bits.
831 send inline break data */ 831 Always one byte as we never send inline break data */
832 *dp++ = gsm_encode_modem(dlci); 832 *dp++ = gsm_encode_modem(dlci);
833 break; 833 break;
834 } 834 }
@@ -968,7 +968,7 @@ static void gsm_dlci_data_kick(struct gsm_dlci *dlci)
968 unsigned long flags; 968 unsigned long flags;
969 int sweep; 969 int sweep;
970 970
971 if (dlci->constipated) 971 if (dlci->constipated)
972 return; 972 return;
973 973
974 spin_lock_irqsave(&dlci->gsm->tx_lock, flags); 974 spin_lock_irqsave(&dlci->gsm->tx_lock, flags);
@@ -981,7 +981,7 @@ static void gsm_dlci_data_kick(struct gsm_dlci *dlci)
981 gsm_dlci_data_output(dlci->gsm, dlci); 981 gsm_dlci_data_output(dlci->gsm, dlci);
982 } 982 }
983 if (sweep) 983 if (sweep)
984 gsm_dlci_data_sweep(dlci->gsm); 984 gsm_dlci_data_sweep(dlci->gsm);
985 spin_unlock_irqrestore(&dlci->gsm->tx_lock, flags); 985 spin_unlock_irqrestore(&dlci->gsm->tx_lock, flags);
986} 986}
987 987
@@ -1138,7 +1138,7 @@ static void gsm_control_modem(struct gsm_mux *gsm, u8 *data, int clen)
1138static void gsm_control_rls(struct gsm_mux *gsm, u8 *data, int clen) 1138static void gsm_control_rls(struct gsm_mux *gsm, u8 *data, int clen)
1139{ 1139{
1140 struct tty_port *port; 1140 struct tty_port *port;
1141 unsigned int addr = 0 ; 1141 unsigned int addr = 0;
1142 u8 bits; 1142 u8 bits;
1143 int len = clen; 1143 int len = clen;
1144 u8 *dp = data; 1144 u8 *dp = data;
@@ -1740,10 +1740,11 @@ static void gsm_queue(struct gsm_mux *gsm)
1740 1740
1741 if ((gsm->control & ~PF) == UI) 1741 if ((gsm->control & ~PF) == UI)
1742 gsm->fcs = gsm_fcs_add_block(gsm->fcs, gsm->buf, gsm->len); 1742 gsm->fcs = gsm_fcs_add_block(gsm->fcs, gsm->buf, gsm->len);
1743 if (gsm->encoding == 0){ 1743 if (gsm->encoding == 0) {
1744 /* WARNING: gsm->received_fcs is used for gsm->encoding = 0 only. 1744 /* WARNING: gsm->received_fcs is used for
1745 In this case it contain the last piece of data 1745 gsm->encoding = 0 only.
1746 required to generate final CRC */ 1746 In this case it contain the last piece of data
1747 required to generate final CRC */
1747 gsm->fcs = gsm_fcs_add(gsm->fcs, gsm->received_fcs); 1748 gsm->fcs = gsm_fcs_add(gsm->fcs, gsm->received_fcs);
1748 } 1749 }
1749 if (gsm->fcs != GOOD_FCS) { 1750 if (gsm->fcs != GOOD_FCS) {
@@ -2904,9 +2905,11 @@ static int gsmtty_install(struct tty_driver *driver, struct tty_struct *tty)
2904 gsm = gsm_mux[mux]; 2905 gsm = gsm_mux[mux];
2905 if (gsm->dead) 2906 if (gsm->dead)
2906 return -EL2HLT; 2907 return -EL2HLT;
2907 /* If DLCI 0 is not yet fully open return an error. This is ok from a locking 2908 /* If DLCI 0 is not yet fully open return an error.
2908 perspective as we don't have to worry about this if DLCI0 is lost */ 2909 This is ok from a locking
2909 if (gsm->dlci[0] && gsm->dlci[0]->state != DLCI_OPEN) 2910 perspective as we don't have to worry about this
2911 if DLCI0 is lost */
2912 if (gsm->dlci[0] && gsm->dlci[0]->state != DLCI_OPEN)
2910 return -EL2NSYNC; 2913 return -EL2NSYNC;
2911 dlci = gsm->dlci[line]; 2914 dlci = gsm->dlci[line];
2912 if (dlci == NULL) { 2915 if (dlci == NULL) {
diff --git a/drivers/tty/n_tty.c b/drivers/tty/n_tty.c
index 4bf0fc0843d7..c9a9ddd1d0bc 100644
--- a/drivers/tty/n_tty.c
+++ b/drivers/tty/n_tty.c
@@ -50,6 +50,7 @@
50#include <linux/uaccess.h> 50#include <linux/uaccess.h>
51#include <linux/module.h> 51#include <linux/module.h>
52#include <linux/ratelimit.h> 52#include <linux/ratelimit.h>
53#include <linux/vmalloc.h>
53 54
54 55
55/* number of characters left in xmit buffer before select has we have room */ 56/* number of characters left in xmit buffer before select has we have room */
@@ -74,37 +75,81 @@
74#define ECHO_OP_SET_CANON_COL 0x81 75#define ECHO_OP_SET_CANON_COL 0x81
75#define ECHO_OP_ERASE_TAB 0x82 76#define ECHO_OP_ERASE_TAB 0x82
76 77
78#define ECHO_COMMIT_WATERMARK 256
79#define ECHO_BLOCK 256
80#define ECHO_DISCARD_WATERMARK N_TTY_BUF_SIZE - (ECHO_BLOCK + 32)
81
82
83#undef N_TTY_TRACE
84#ifdef N_TTY_TRACE
85# define n_tty_trace(f, args...) trace_printk(f, ##args)
86#else
87# define n_tty_trace(f, args...)
88#endif
89
77struct n_tty_data { 90struct n_tty_data {
78 unsigned int column; 91 /* producer-published */
92 size_t read_head;
93 size_t canon_head;
94 size_t echo_head;
95 size_t echo_commit;
96 DECLARE_BITMAP(char_map, 256);
97
98 /* private to n_tty_receive_overrun (single-threaded) */
79 unsigned long overrun_time; 99 unsigned long overrun_time;
80 int num_overrun; 100 int num_overrun;
81 101
102 /* non-atomic */
103 bool no_room;
104
105 /* must hold exclusive termios_rwsem to reset these */
82 unsigned char lnext:1, erasing:1, raw:1, real_raw:1, icanon:1; 106 unsigned char lnext:1, erasing:1, raw:1, real_raw:1, icanon:1;
83 unsigned char echo_overrun:1;
84 107
85 DECLARE_BITMAP(process_char_map, 256); 108 /* shared by producer and consumer */
109 char read_buf[N_TTY_BUF_SIZE];
86 DECLARE_BITMAP(read_flags, N_TTY_BUF_SIZE); 110 DECLARE_BITMAP(read_flags, N_TTY_BUF_SIZE);
111 unsigned char echo_buf[N_TTY_BUF_SIZE];
87 112
88 char *read_buf;
89 int read_head;
90 int read_tail;
91 int read_cnt;
92 int minimum_to_wake; 113 int minimum_to_wake;
93 114
94 unsigned char *echo_buf; 115 /* consumer-published */
95 unsigned int echo_pos; 116 size_t read_tail;
96 unsigned int echo_cnt; 117 size_t line_start;
97 118
98 int canon_data; 119 /* protected by output lock */
99 unsigned long canon_head; 120 unsigned int column;
100 unsigned int canon_column; 121 unsigned int canon_column;
122 size_t echo_tail;
101 123
102 struct mutex atomic_read_lock; 124 struct mutex atomic_read_lock;
103 struct mutex output_lock; 125 struct mutex output_lock;
104 struct mutex echo_lock;
105 raw_spinlock_t read_lock;
106}; 126};
107 127
128static inline size_t read_cnt(struct n_tty_data *ldata)
129{
130 return ldata->read_head - ldata->read_tail;
131}
132
133static inline unsigned char read_buf(struct n_tty_data *ldata, size_t i)
134{
135 return ldata->read_buf[i & (N_TTY_BUF_SIZE - 1)];
136}
137
138static inline unsigned char *read_buf_addr(struct n_tty_data *ldata, size_t i)
139{
140 return &ldata->read_buf[i & (N_TTY_BUF_SIZE - 1)];
141}
142
143static inline unsigned char echo_buf(struct n_tty_data *ldata, size_t i)
144{
145 return ldata->echo_buf[i & (N_TTY_BUF_SIZE - 1)];
146}
147
148static inline unsigned char *echo_buf_addr(struct n_tty_data *ldata, size_t i)
149{
150 return &ldata->echo_buf[i & (N_TTY_BUF_SIZE - 1)];
151}
152
108static inline int tty_put_user(struct tty_struct *tty, unsigned char x, 153static inline int tty_put_user(struct tty_struct *tty, unsigned char x,
109 unsigned char __user *ptr) 154 unsigned char __user *ptr)
110{ 155{
@@ -114,33 +159,18 @@ static inline int tty_put_user(struct tty_struct *tty, unsigned char x,
114 return put_user(x, ptr); 159 return put_user(x, ptr);
115} 160}
116 161
117/** 162static int receive_room(struct tty_struct *tty)
118 * n_tty_set_room - receive space
119 * @tty: terminal
120 *
121 * Updates tty->receive_room to reflect the currently available space
122 * in the input buffer, and re-schedules the flip buffer work if space
123 * just became available.
124 *
125 * Locks: Concurrent update is protected with read_lock
126 */
127
128static int set_room(struct tty_struct *tty)
129{ 163{
130 struct n_tty_data *ldata = tty->disc_data; 164 struct n_tty_data *ldata = tty->disc_data;
131 int left; 165 int left;
132 int old_left;
133 unsigned long flags;
134
135 raw_spin_lock_irqsave(&ldata->read_lock, flags);
136 166
137 if (I_PARMRK(tty)) { 167 if (I_PARMRK(tty)) {
138 /* Multiply read_cnt by 3, since each byte might take up to 168 /* Multiply read_cnt by 3, since each byte might take up to
139 * three times as many spaces when PARMRK is set (depending on 169 * three times as many spaces when PARMRK is set (depending on
140 * its flags, e.g. parity error). */ 170 * its flags, e.g. parity error). */
141 left = N_TTY_BUF_SIZE - ldata->read_cnt * 3 - 1; 171 left = N_TTY_BUF_SIZE - read_cnt(ldata) * 3 - 1;
142 } else 172 } else
143 left = N_TTY_BUF_SIZE - ldata->read_cnt - 1; 173 left = N_TTY_BUF_SIZE - read_cnt(ldata) - 1;
144 174
145 /* 175 /*
146 * If we are doing input canonicalization, and there are no 176 * If we are doing input canonicalization, and there are no
@@ -149,19 +179,31 @@ static int set_room(struct tty_struct *tty)
149 * characters will be beeped. 179 * characters will be beeped.
150 */ 180 */
151 if (left <= 0) 181 if (left <= 0)
152 left = ldata->icanon && !ldata->canon_data; 182 left = ldata->icanon && ldata->canon_head == ldata->read_tail;
153 old_left = tty->receive_room;
154 tty->receive_room = left;
155 183
156 raw_spin_unlock_irqrestore(&ldata->read_lock, flags); 184 return left;
157
158 return left && !old_left;
159} 185}
160 186
187/**
188 * n_tty_set_room - receive space
189 * @tty: terminal
190 *
191 * Re-schedules the flip buffer work if space just became available.
192 *
193 * Caller holds exclusive termios_rwsem
194 * or
195 * n_tty_read()/consumer path:
196 * holds non-exclusive termios_rwsem
197 */
198
161static void n_tty_set_room(struct tty_struct *tty) 199static void n_tty_set_room(struct tty_struct *tty)
162{ 200{
201 struct n_tty_data *ldata = tty->disc_data;
202
163 /* Did this open up the receive buffer? We may need to flip */ 203 /* Did this open up the receive buffer? We may need to flip */
164 if (set_room(tty)) { 204 if (unlikely(ldata->no_room) && receive_room(tty)) {
205 ldata->no_room = 0;
206
165 WARN_RATELIMIT(tty->port->itty == NULL, 207 WARN_RATELIMIT(tty->port->itty == NULL,
166 "scheduling with invalid itty\n"); 208 "scheduling with invalid itty\n");
167 /* see if ldisc has been killed - if so, this means that 209 /* see if ldisc has been killed - if so, this means that
@@ -170,17 +212,93 @@ static void n_tty_set_room(struct tty_struct *tty)
170 */ 212 */
171 WARN_RATELIMIT(test_bit(TTY_LDISC_HALTED, &tty->flags), 213 WARN_RATELIMIT(test_bit(TTY_LDISC_HALTED, &tty->flags),
172 "scheduling buffer work for halted ldisc\n"); 214 "scheduling buffer work for halted ldisc\n");
173 schedule_work(&tty->port->buf.work); 215 queue_work(system_unbound_wq, &tty->port->buf.work);
174 } 216 }
175} 217}
176 218
177static void put_tty_queue_nolock(unsigned char c, struct n_tty_data *ldata) 219static ssize_t chars_in_buffer(struct tty_struct *tty)
220{
221 struct n_tty_data *ldata = tty->disc_data;
222 ssize_t n = 0;
223
224 if (!ldata->icanon)
225 n = read_cnt(ldata);
226 else
227 n = ldata->canon_head - ldata->read_tail;
228 return n;
229}
230
231/**
232 * n_tty_write_wakeup - asynchronous I/O notifier
233 * @tty: tty device
234 *
235 * Required for the ptys, serial driver etc. since processes
236 * that attach themselves to the master and rely on ASYNC
237 * IO must be woken up
238 */
239
240static void n_tty_write_wakeup(struct tty_struct *tty)
178{ 241{
179 if (ldata->read_cnt < N_TTY_BUF_SIZE) { 242 if (tty->fasync && test_and_clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags))
180 ldata->read_buf[ldata->read_head] = c; 243 kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
181 ldata->read_head = (ldata->read_head + 1) & (N_TTY_BUF_SIZE-1); 244}
182 ldata->read_cnt++; 245
246static void n_tty_check_throttle(struct tty_struct *tty)
247{
248 if (tty->driver->type == TTY_DRIVER_TYPE_PTY)
249 return;
250 /*
251 * Check the remaining room for the input canonicalization
252 * mode. We don't want to throttle the driver if we're in
253 * canonical mode and don't have a newline yet!
254 */
255 while (1) {
256 int throttled;
257 tty_set_flow_change(tty, TTY_THROTTLE_SAFE);
258 if (receive_room(tty) >= TTY_THRESHOLD_THROTTLE)
259 break;
260 throttled = tty_throttle_safe(tty);
261 if (!throttled)
262 break;
263 }
264 __tty_set_flow_change(tty, 0);
265}
266
267static void n_tty_check_unthrottle(struct tty_struct *tty)
268{
269 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
270 tty->link->ldisc->ops->write_wakeup == n_tty_write_wakeup) {
271 if (chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE)
272 return;
273 if (!tty->count)
274 return;
275 n_tty_set_room(tty);
276 n_tty_write_wakeup(tty->link);
277 wake_up_interruptible_poll(&tty->link->write_wait, POLLOUT);
278 return;
279 }
280
281 /* If there is enough space in the read buffer now, let the
282 * low-level driver know. We use chars_in_buffer() to
283 * check the buffer, as it now knows about canonical mode.
284 * Otherwise, if the driver is throttled and the line is
285 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
286 * we won't get any more characters.
287 */
288
289 while (1) {
290 int unthrottled;
291 tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE);
292 if (chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE)
293 break;
294 if (!tty->count)
295 break;
296 n_tty_set_room(tty);
297 unthrottled = tty_unthrottle_safe(tty);
298 if (!unthrottled)
299 break;
183 } 300 }
301 __tty_set_flow_change(tty, 0);
184} 302}
185 303
186/** 304/**
@@ -188,21 +306,19 @@ static void put_tty_queue_nolock(unsigned char c, struct n_tty_data *ldata)
188 * @c: character 306 * @c: character
189 * @ldata: n_tty data 307 * @ldata: n_tty data
190 * 308 *
191 * Add a character to the tty read_buf queue. This is done under the 309 * Add a character to the tty read_buf queue.
192 * read_lock to serialize character addition and also to protect us 310 *
193 * against parallel reads or flushes 311 * n_tty_receive_buf()/producer path:
312 * caller holds non-exclusive termios_rwsem
313 * modifies read_head
314 *
315 * read_head is only considered 'published' if canonical mode is
316 * not active.
194 */ 317 */
195 318
196static void put_tty_queue(unsigned char c, struct n_tty_data *ldata) 319static inline void put_tty_queue(unsigned char c, struct n_tty_data *ldata)
197{ 320{
198 unsigned long flags; 321 *read_buf_addr(ldata, ldata->read_head++) = c;
199 /*
200 * The problem of stomping on the buffers ends here.
201 * Why didn't anyone see this one coming? --AJK
202 */
203 raw_spin_lock_irqsave(&ldata->read_lock, flags);
204 put_tty_queue_nolock(c, ldata);
205 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
206} 322}
207 323
208/** 324/**
@@ -212,22 +328,17 @@ static void put_tty_queue(unsigned char c, struct n_tty_data *ldata)
212 * Reset the read buffer counters and clear the flags. 328 * Reset the read buffer counters and clear the flags.
213 * Called from n_tty_open() and n_tty_flush_buffer(). 329 * Called from n_tty_open() and n_tty_flush_buffer().
214 * 330 *
215 * Locking: tty_read_lock for read fields. 331 * Locking: caller holds exclusive termios_rwsem
332 * (or locking is not required)
216 */ 333 */
217 334
218static void reset_buffer_flags(struct n_tty_data *ldata) 335static void reset_buffer_flags(struct n_tty_data *ldata)
219{ 336{
220 unsigned long flags; 337 ldata->read_head = ldata->canon_head = ldata->read_tail = 0;
338 ldata->echo_head = ldata->echo_tail = ldata->echo_commit = 0;
339 ldata->line_start = 0;
221 340
222 raw_spin_lock_irqsave(&ldata->read_lock, flags); 341 ldata->erasing = 0;
223 ldata->read_head = ldata->read_tail = ldata->read_cnt = 0;
224 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
225
226 mutex_lock(&ldata->echo_lock);
227 ldata->echo_pos = ldata->echo_cnt = ldata->echo_overrun = 0;
228 mutex_unlock(&ldata->echo_lock);
229
230 ldata->canon_head = ldata->canon_data = ldata->erasing = 0;
231 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE); 342 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
232} 343}
233 344
@@ -251,16 +362,21 @@ static void n_tty_packet_mode_flush(struct tty_struct *tty)
251 * buffer flushed (eg at hangup) or when the N_TTY line discipline 362 * buffer flushed (eg at hangup) or when the N_TTY line discipline
252 * internally has to clean the pending queue (for example some signals). 363 * internally has to clean the pending queue (for example some signals).
253 * 364 *
254 * Locking: ctrl_lock, read_lock. 365 * Holds termios_rwsem to exclude producer/consumer while
366 * buffer indices are reset.
367 *
368 * Locking: ctrl_lock, exclusive termios_rwsem
255 */ 369 */
256 370
257static void n_tty_flush_buffer(struct tty_struct *tty) 371static void n_tty_flush_buffer(struct tty_struct *tty)
258{ 372{
373 down_write(&tty->termios_rwsem);
259 reset_buffer_flags(tty->disc_data); 374 reset_buffer_flags(tty->disc_data);
260 n_tty_set_room(tty); 375 n_tty_set_room(tty);
261 376
262 if (tty->link) 377 if (tty->link)
263 n_tty_packet_mode_flush(tty); 378 n_tty_packet_mode_flush(tty);
379 up_write(&tty->termios_rwsem);
264} 380}
265 381
266/** 382/**
@@ -270,24 +386,18 @@ static void n_tty_flush_buffer(struct tty_struct *tty)
270 * Report the number of characters buffered to be delivered to user 386 * Report the number of characters buffered to be delivered to user
271 * at this instant in time. 387 * at this instant in time.
272 * 388 *
273 * Locking: read_lock 389 * Locking: exclusive termios_rwsem
274 */ 390 */
275 391
276static ssize_t n_tty_chars_in_buffer(struct tty_struct *tty) 392static ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
277{ 393{
278 struct n_tty_data *ldata = tty->disc_data; 394 ssize_t n;
279 unsigned long flags;
280 ssize_t n = 0;
281 395
282 raw_spin_lock_irqsave(&ldata->read_lock, flags); 396 WARN_ONCE(1, "%s is deprecated and scheduled for removal.", __func__);
283 if (!ldata->icanon) { 397
284 n = ldata->read_cnt; 398 down_write(&tty->termios_rwsem);
285 } else if (ldata->canon_data) { 399 n = chars_in_buffer(tty);
286 n = (ldata->canon_head > ldata->read_tail) ? 400 up_write(&tty->termios_rwsem);
287 ldata->canon_head - ldata->read_tail :
288 ldata->canon_head + (N_TTY_BUF_SIZE - ldata->read_tail);
289 }
290 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
291 return n; 401 return n;
292} 402}
293 403
@@ -532,33 +642,23 @@ break_out:
532 * are prioritized. Also, when control characters are echoed with a 642 * are prioritized. Also, when control characters are echoed with a
533 * prefixed "^", the pair is treated atomically and thus not separated. 643 * prefixed "^", the pair is treated atomically and thus not separated.
534 * 644 *
535 * Locking: output_lock to protect column state and space left, 645 * Locking: callers must hold output_lock
536 * echo_lock to protect the echo buffer
537 */ 646 */
538 647
539static void process_echoes(struct tty_struct *tty) 648static size_t __process_echoes(struct tty_struct *tty)
540{ 649{
541 struct n_tty_data *ldata = tty->disc_data; 650 struct n_tty_data *ldata = tty->disc_data;
542 int space, nr; 651 int space, old_space;
652 size_t tail;
543 unsigned char c; 653 unsigned char c;
544 unsigned char *cp, *buf_end;
545
546 if (!ldata->echo_cnt)
547 return;
548
549 mutex_lock(&ldata->output_lock);
550 mutex_lock(&ldata->echo_lock);
551 654
552 space = tty_write_room(tty); 655 old_space = space = tty_write_room(tty);
553 656
554 buf_end = ldata->echo_buf + N_TTY_BUF_SIZE; 657 tail = ldata->echo_tail;
555 cp = ldata->echo_buf + ldata->echo_pos; 658 while (ldata->echo_commit != tail) {
556 nr = ldata->echo_cnt; 659 c = echo_buf(ldata, tail);
557 while (nr > 0) {
558 c = *cp;
559 if (c == ECHO_OP_START) { 660 if (c == ECHO_OP_START) {
560 unsigned char op; 661 unsigned char op;
561 unsigned char *opp;
562 int no_space_left = 0; 662 int no_space_left = 0;
563 663
564 /* 664 /*
@@ -566,18 +666,13 @@ static void process_echoes(struct tty_struct *tty)
566 * operation, get the next byte, which is either the 666 * operation, get the next byte, which is either the
567 * op code or a control character value. 667 * op code or a control character value.
568 */ 668 */
569 opp = cp + 1; 669 op = echo_buf(ldata, tail + 1);
570 if (opp == buf_end)
571 opp -= N_TTY_BUF_SIZE;
572 op = *opp;
573 670
574 switch (op) { 671 switch (op) {
575 unsigned int num_chars, num_bs; 672 unsigned int num_chars, num_bs;
576 673
577 case ECHO_OP_ERASE_TAB: 674 case ECHO_OP_ERASE_TAB:
578 if (++opp == buf_end) 675 num_chars = echo_buf(ldata, tail + 2);
579 opp -= N_TTY_BUF_SIZE;
580 num_chars = *opp;
581 676
582 /* 677 /*
583 * Determine how many columns to go back 678 * Determine how many columns to go back
@@ -603,21 +698,18 @@ static void process_echoes(struct tty_struct *tty)
603 if (ldata->column > 0) 698 if (ldata->column > 0)
604 ldata->column--; 699 ldata->column--;
605 } 700 }
606 cp += 3; 701 tail += 3;
607 nr -= 3;
608 break; 702 break;
609 703
610 case ECHO_OP_SET_CANON_COL: 704 case ECHO_OP_SET_CANON_COL:
611 ldata->canon_column = ldata->column; 705 ldata->canon_column = ldata->column;
612 cp += 2; 706 tail += 2;
613 nr -= 2;
614 break; 707 break;
615 708
616 case ECHO_OP_MOVE_BACK_COL: 709 case ECHO_OP_MOVE_BACK_COL:
617 if (ldata->column > 0) 710 if (ldata->column > 0)
618 ldata->column--; 711 ldata->column--;
619 cp += 2; 712 tail += 2;
620 nr -= 2;
621 break; 713 break;
622 714
623 case ECHO_OP_START: 715 case ECHO_OP_START:
@@ -629,8 +721,7 @@ static void process_echoes(struct tty_struct *tty)
629 tty_put_char(tty, ECHO_OP_START); 721 tty_put_char(tty, ECHO_OP_START);
630 ldata->column++; 722 ldata->column++;
631 space--; 723 space--;
632 cp += 2; 724 tail += 2;
633 nr -= 2;
634 break; 725 break;
635 726
636 default: 727 default:
@@ -651,8 +742,7 @@ static void process_echoes(struct tty_struct *tty)
651 tty_put_char(tty, op ^ 0100); 742 tty_put_char(tty, op ^ 0100);
652 ldata->column += 2; 743 ldata->column += 2;
653 space -= 2; 744 space -= 2;
654 cp += 2; 745 tail += 2;
655 nr -= 2;
656 } 746 }
657 747
658 if (no_space_left) 748 if (no_space_left)
@@ -669,80 +759,92 @@ static void process_echoes(struct tty_struct *tty)
669 tty_put_char(tty, c); 759 tty_put_char(tty, c);
670 space -= 1; 760 space -= 1;
671 } 761 }
672 cp += 1; 762 tail += 1;
673 nr -= 1;
674 } 763 }
675
676 /* When end of circular buffer reached, wrap around */
677 if (cp >= buf_end)
678 cp -= N_TTY_BUF_SIZE;
679 } 764 }
680 765
681 if (nr == 0) { 766 /* If the echo buffer is nearly full (so that the possibility exists
682 ldata->echo_pos = 0; 767 * of echo overrun before the next commit), then discard enough
683 ldata->echo_cnt = 0; 768 * data at the tail to prevent a subsequent overrun */
684 ldata->echo_overrun = 0; 769 while (ldata->echo_commit - tail >= ECHO_DISCARD_WATERMARK) {
685 } else { 770 if (echo_buf(ldata, tail == ECHO_OP_START)) {
686 int num_processed = ldata->echo_cnt - nr; 771 if (echo_buf(ldata, tail) == ECHO_OP_ERASE_TAB)
687 ldata->echo_pos += num_processed; 772 tail += 3;
688 ldata->echo_pos &= N_TTY_BUF_SIZE - 1; 773 else
689 ldata->echo_cnt = nr; 774 tail += 2;
690 if (num_processed > 0) 775 } else
691 ldata->echo_overrun = 0; 776 tail++;
692 } 777 }
693 778
694 mutex_unlock(&ldata->echo_lock); 779 ldata->echo_tail = tail;
780 return old_space - space;
781}
782
783static void commit_echoes(struct tty_struct *tty)
784{
785 struct n_tty_data *ldata = tty->disc_data;
786 size_t nr, old, echoed;
787 size_t head;
788
789 head = ldata->echo_head;
790 old = ldata->echo_commit - ldata->echo_tail;
791
792 /* Process committed echoes if the accumulated # of bytes
793 * is over the threshold (and try again each time another
794 * block is accumulated) */
795 nr = head - ldata->echo_tail;
796 if (nr < ECHO_COMMIT_WATERMARK || (nr % ECHO_BLOCK > old % ECHO_BLOCK))
797 return;
798
799 mutex_lock(&ldata->output_lock);
800 ldata->echo_commit = head;
801 echoed = __process_echoes(tty);
802 mutex_unlock(&ldata->output_lock);
803
804 if (echoed && tty->ops->flush_chars)
805 tty->ops->flush_chars(tty);
806}
807
808static void process_echoes(struct tty_struct *tty)
809{
810 struct n_tty_data *ldata = tty->disc_data;
811 size_t echoed;
812
813 if (!L_ECHO(tty) || ldata->echo_commit == ldata->echo_tail)
814 return;
815
816 mutex_lock(&ldata->output_lock);
817 echoed = __process_echoes(tty);
695 mutex_unlock(&ldata->output_lock); 818 mutex_unlock(&ldata->output_lock);
696 819
697 if (tty->ops->flush_chars) 820 if (echoed && tty->ops->flush_chars)
698 tty->ops->flush_chars(tty); 821 tty->ops->flush_chars(tty);
699} 822}
700 823
824static void flush_echoes(struct tty_struct *tty)
825{
826 struct n_tty_data *ldata = tty->disc_data;
827
828 if (!L_ECHO(tty) || ldata->echo_commit == ldata->echo_head)
829 return;
830
831 mutex_lock(&ldata->output_lock);
832 ldata->echo_commit = ldata->echo_head;
833 __process_echoes(tty);
834 mutex_unlock(&ldata->output_lock);
835}
836
701/** 837/**
702 * add_echo_byte - add a byte to the echo buffer 838 * add_echo_byte - add a byte to the echo buffer
703 * @c: unicode byte to echo 839 * @c: unicode byte to echo
704 * @ldata: n_tty data 840 * @ldata: n_tty data
705 * 841 *
706 * Add a character or operation byte to the echo buffer. 842 * Add a character or operation byte to the echo buffer.
707 *
708 * Should be called under the echo lock to protect the echo buffer.
709 */ 843 */
710 844
711static void add_echo_byte(unsigned char c, struct n_tty_data *ldata) 845static inline void add_echo_byte(unsigned char c, struct n_tty_data *ldata)
712{ 846{
713 int new_byte_pos; 847 *echo_buf_addr(ldata, ldata->echo_head++) = c;
714
715 if (ldata->echo_cnt == N_TTY_BUF_SIZE) {
716 /* Circular buffer is already at capacity */
717 new_byte_pos = ldata->echo_pos;
718
719 /*
720 * Since the buffer start position needs to be advanced,
721 * be sure to step by a whole operation byte group.
722 */
723 if (ldata->echo_buf[ldata->echo_pos] == ECHO_OP_START) {
724 if (ldata->echo_buf[(ldata->echo_pos + 1) &
725 (N_TTY_BUF_SIZE - 1)] ==
726 ECHO_OP_ERASE_TAB) {
727 ldata->echo_pos += 3;
728 ldata->echo_cnt -= 2;
729 } else {
730 ldata->echo_pos += 2;
731 ldata->echo_cnt -= 1;
732 }
733 } else {
734 ldata->echo_pos++;
735 }
736 ldata->echo_pos &= N_TTY_BUF_SIZE - 1;
737
738 ldata->echo_overrun = 1;
739 } else {
740 new_byte_pos = ldata->echo_pos + ldata->echo_cnt;
741 new_byte_pos &= N_TTY_BUF_SIZE - 1;
742 ldata->echo_cnt++;
743 }
744
745 ldata->echo_buf[new_byte_pos] = c;
746} 848}
747 849
748/** 850/**
@@ -750,16 +852,12 @@ static void add_echo_byte(unsigned char c, struct n_tty_data *ldata)
750 * @ldata: n_tty data 852 * @ldata: n_tty data
751 * 853 *
752 * Add an operation to the echo buffer to move back one column. 854 * Add an operation to the echo buffer to move back one column.
753 *
754 * Locking: echo_lock to protect the echo buffer
755 */ 855 */
756 856
757static void echo_move_back_col(struct n_tty_data *ldata) 857static void echo_move_back_col(struct n_tty_data *ldata)
758{ 858{
759 mutex_lock(&ldata->echo_lock);
760 add_echo_byte(ECHO_OP_START, ldata); 859 add_echo_byte(ECHO_OP_START, ldata);
761 add_echo_byte(ECHO_OP_MOVE_BACK_COL, ldata); 860 add_echo_byte(ECHO_OP_MOVE_BACK_COL, ldata);
762 mutex_unlock(&ldata->echo_lock);
763} 861}
764 862
765/** 863/**
@@ -768,16 +866,12 @@ static void echo_move_back_col(struct n_tty_data *ldata)
768 * 866 *
769 * Add an operation to the echo buffer to set the canon column 867 * Add an operation to the echo buffer to set the canon column
770 * to the current column. 868 * to the current column.
771 *
772 * Locking: echo_lock to protect the echo buffer
773 */ 869 */
774 870
775static void echo_set_canon_col(struct n_tty_data *ldata) 871static void echo_set_canon_col(struct n_tty_data *ldata)
776{ 872{
777 mutex_lock(&ldata->echo_lock);
778 add_echo_byte(ECHO_OP_START, ldata); 873 add_echo_byte(ECHO_OP_START, ldata);
779 add_echo_byte(ECHO_OP_SET_CANON_COL, ldata); 874 add_echo_byte(ECHO_OP_SET_CANON_COL, ldata);
780 mutex_unlock(&ldata->echo_lock);
781} 875}
782 876
783/** 877/**
@@ -793,15 +887,11 @@ static void echo_set_canon_col(struct n_tty_data *ldata)
793 * of input. This information will be used later, along with 887 * of input. This information will be used later, along with
794 * canon column (if applicable), to go back the correct number 888 * canon column (if applicable), to go back the correct number
795 * of columns. 889 * of columns.
796 *
797 * Locking: echo_lock to protect the echo buffer
798 */ 890 */
799 891
800static void echo_erase_tab(unsigned int num_chars, int after_tab, 892static void echo_erase_tab(unsigned int num_chars, int after_tab,
801 struct n_tty_data *ldata) 893 struct n_tty_data *ldata)
802{ 894{
803 mutex_lock(&ldata->echo_lock);
804
805 add_echo_byte(ECHO_OP_START, ldata); 895 add_echo_byte(ECHO_OP_START, ldata);
806 add_echo_byte(ECHO_OP_ERASE_TAB, ldata); 896 add_echo_byte(ECHO_OP_ERASE_TAB, ldata);
807 897
@@ -813,8 +903,6 @@ static void echo_erase_tab(unsigned int num_chars, int after_tab,
813 num_chars |= 0x80; 903 num_chars |= 0x80;
814 904
815 add_echo_byte(num_chars, ldata); 905 add_echo_byte(num_chars, ldata);
816
817 mutex_unlock(&ldata->echo_lock);
818} 906}
819 907
820/** 908/**
@@ -826,20 +914,16 @@ static void echo_erase_tab(unsigned int num_chars, int after_tab,
826 * L_ECHO(tty) is true. Called from the driver receive_buf path. 914 * L_ECHO(tty) is true. Called from the driver receive_buf path.
827 * 915 *
828 * This variant does not treat control characters specially. 916 * This variant does not treat control characters specially.
829 *
830 * Locking: echo_lock to protect the echo buffer
831 */ 917 */
832 918
833static void echo_char_raw(unsigned char c, struct n_tty_data *ldata) 919static void echo_char_raw(unsigned char c, struct n_tty_data *ldata)
834{ 920{
835 mutex_lock(&ldata->echo_lock);
836 if (c == ECHO_OP_START) { 921 if (c == ECHO_OP_START) {
837 add_echo_byte(ECHO_OP_START, ldata); 922 add_echo_byte(ECHO_OP_START, ldata);
838 add_echo_byte(ECHO_OP_START, ldata); 923 add_echo_byte(ECHO_OP_START, ldata);
839 } else { 924 } else {
840 add_echo_byte(c, ldata); 925 add_echo_byte(c, ldata);
841 } 926 }
842 mutex_unlock(&ldata->echo_lock);
843} 927}
844 928
845/** 929/**
@@ -852,16 +936,12 @@ static void echo_char_raw(unsigned char c, struct n_tty_data *ldata)
852 * 936 *
853 * This variant tags control characters to be echoed as "^X" 937 * This variant tags control characters to be echoed as "^X"
854 * (where X is the letter representing the control char). 938 * (where X is the letter representing the control char).
855 *
856 * Locking: echo_lock to protect the echo buffer
857 */ 939 */
858 940
859static void echo_char(unsigned char c, struct tty_struct *tty) 941static void echo_char(unsigned char c, struct tty_struct *tty)
860{ 942{
861 struct n_tty_data *ldata = tty->disc_data; 943 struct n_tty_data *ldata = tty->disc_data;
862 944
863 mutex_lock(&ldata->echo_lock);
864
865 if (c == ECHO_OP_START) { 945 if (c == ECHO_OP_START) {
866 add_echo_byte(ECHO_OP_START, ldata); 946 add_echo_byte(ECHO_OP_START, ldata);
867 add_echo_byte(ECHO_OP_START, ldata); 947 add_echo_byte(ECHO_OP_START, ldata);
@@ -870,8 +950,6 @@ static void echo_char(unsigned char c, struct tty_struct *tty)
870 add_echo_byte(ECHO_OP_START, ldata); 950 add_echo_byte(ECHO_OP_START, ldata);
871 add_echo_byte(c, ldata); 951 add_echo_byte(c, ldata);
872 } 952 }
873
874 mutex_unlock(&ldata->echo_lock);
875} 953}
876 954
877/** 955/**
@@ -896,17 +974,22 @@ static inline void finish_erasing(struct n_tty_data *ldata)
896 * present in the stream from the driver layer. Handles the complexities 974 * present in the stream from the driver layer. Handles the complexities
897 * of UTF-8 multibyte symbols. 975 * of UTF-8 multibyte symbols.
898 * 976 *
899 * Locking: read_lock for tty buffers 977 * n_tty_receive_buf()/producer path:
978 * caller holds non-exclusive termios_rwsem
979 * modifies read_head
980 *
981 * Modifying the read_head is not considered a publish in this context
982 * because canonical mode is active -- only canon_head publishes
900 */ 983 */
901 984
902static void eraser(unsigned char c, struct tty_struct *tty) 985static void eraser(unsigned char c, struct tty_struct *tty)
903{ 986{
904 struct n_tty_data *ldata = tty->disc_data; 987 struct n_tty_data *ldata = tty->disc_data;
905 enum { ERASE, WERASE, KILL } kill_type; 988 enum { ERASE, WERASE, KILL } kill_type;
906 int head, seen_alnums, cnt; 989 size_t head;
907 unsigned long flags; 990 size_t cnt;
991 int seen_alnums;
908 992
909 /* FIXME: locking needed ? */
910 if (ldata->read_head == ldata->canon_head) { 993 if (ldata->read_head == ldata->canon_head) {
911 /* process_output('\a', tty); */ /* what do you think? */ 994 /* process_output('\a', tty); */ /* what do you think? */
912 return; 995 return;
@@ -917,19 +1000,11 @@ static void eraser(unsigned char c, struct tty_struct *tty)
917 kill_type = WERASE; 1000 kill_type = WERASE;
918 else { 1001 else {
919 if (!L_ECHO(tty)) { 1002 if (!L_ECHO(tty)) {
920 raw_spin_lock_irqsave(&ldata->read_lock, flags);
921 ldata->read_cnt -= ((ldata->read_head - ldata->canon_head) &
922 (N_TTY_BUF_SIZE - 1));
923 ldata->read_head = ldata->canon_head; 1003 ldata->read_head = ldata->canon_head;
924 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
925 return; 1004 return;
926 } 1005 }
927 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) { 1006 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
928 raw_spin_lock_irqsave(&ldata->read_lock, flags);
929 ldata->read_cnt -= ((ldata->read_head - ldata->canon_head) &
930 (N_TTY_BUF_SIZE - 1));
931 ldata->read_head = ldata->canon_head; 1007 ldata->read_head = ldata->canon_head;
932 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
933 finish_erasing(ldata); 1008 finish_erasing(ldata);
934 echo_char(KILL_CHAR(tty), tty); 1009 echo_char(KILL_CHAR(tty), tty);
935 /* Add a newline if ECHOK is on and ECHOKE is off. */ 1010 /* Add a newline if ECHOK is on and ECHOKE is off. */
@@ -941,14 +1016,13 @@ static void eraser(unsigned char c, struct tty_struct *tty)
941 } 1016 }
942 1017
943 seen_alnums = 0; 1018 seen_alnums = 0;
944 /* FIXME: Locking ?? */
945 while (ldata->read_head != ldata->canon_head) { 1019 while (ldata->read_head != ldata->canon_head) {
946 head = ldata->read_head; 1020 head = ldata->read_head;
947 1021
948 /* erase a single possibly multibyte character */ 1022 /* erase a single possibly multibyte character */
949 do { 1023 do {
950 head = (head - 1) & (N_TTY_BUF_SIZE-1); 1024 head--;
951 c = ldata->read_buf[head]; 1025 c = read_buf(ldata, head);
952 } while (is_continuation(c, tty) && head != ldata->canon_head); 1026 } while (is_continuation(c, tty) && head != ldata->canon_head);
953 1027
954 /* do not partially erase */ 1028 /* do not partially erase */
@@ -962,11 +1036,8 @@ static void eraser(unsigned char c, struct tty_struct *tty)
962 else if (seen_alnums) 1036 else if (seen_alnums)
963 break; 1037 break;
964 } 1038 }
965 cnt = (ldata->read_head - head) & (N_TTY_BUF_SIZE-1); 1039 cnt = ldata->read_head - head;
966 raw_spin_lock_irqsave(&ldata->read_lock, flags);
967 ldata->read_head = head; 1040 ldata->read_head = head;
968 ldata->read_cnt -= cnt;
969 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
970 if (L_ECHO(tty)) { 1041 if (L_ECHO(tty)) {
971 if (L_ECHOPRT(tty)) { 1042 if (L_ECHOPRT(tty)) {
972 if (!ldata->erasing) { 1043 if (!ldata->erasing) {
@@ -976,9 +1047,8 @@ static void eraser(unsigned char c, struct tty_struct *tty)
976 /* if cnt > 1, output a multi-byte character */ 1047 /* if cnt > 1, output a multi-byte character */
977 echo_char(c, tty); 1048 echo_char(c, tty);
978 while (--cnt > 0) { 1049 while (--cnt > 0) {
979 head = (head+1) & (N_TTY_BUF_SIZE-1); 1050 head++;
980 echo_char_raw(ldata->read_buf[head], 1051 echo_char_raw(read_buf(ldata, head), ldata);
981 ldata);
982 echo_move_back_col(ldata); 1052 echo_move_back_col(ldata);
983 } 1053 }
984 } else if (kill_type == ERASE && !L_ECHOE(tty)) { 1054 } else if (kill_type == ERASE && !L_ECHOE(tty)) {
@@ -986,7 +1056,7 @@ static void eraser(unsigned char c, struct tty_struct *tty)
986 } else if (c == '\t') { 1056 } else if (c == '\t') {
987 unsigned int num_chars = 0; 1057 unsigned int num_chars = 0;
988 int after_tab = 0; 1058 int after_tab = 0;
989 unsigned long tail = ldata->read_head; 1059 size_t tail = ldata->read_head;
990 1060
991 /* 1061 /*
992 * Count the columns used for characters 1062 * Count the columns used for characters
@@ -996,8 +1066,8 @@ static void eraser(unsigned char c, struct tty_struct *tty)
996 * number of columns. 1066 * number of columns.
997 */ 1067 */
998 while (tail != ldata->canon_head) { 1068 while (tail != ldata->canon_head) {
999 tail = (tail-1) & (N_TTY_BUF_SIZE-1); 1069 tail--;
1000 c = ldata->read_buf[tail]; 1070 c = read_buf(ldata, tail);
1001 if (c == '\t') { 1071 if (c == '\t') {
1002 after_tab = 1; 1072 after_tab = 1;
1003 break; 1073 break;
@@ -1040,7 +1110,7 @@ static void eraser(unsigned char c, struct tty_struct *tty)
1040 * Locking: ctrl_lock 1110 * Locking: ctrl_lock
1041 */ 1111 */
1042 1112
1043static inline void isig(int sig, struct tty_struct *tty) 1113static void isig(int sig, struct tty_struct *tty)
1044{ 1114{
1045 struct pid *tty_pgrp = tty_get_pgrp(tty); 1115 struct pid *tty_pgrp = tty_get_pgrp(tty);
1046 if (tty_pgrp) { 1116 if (tty_pgrp) {
@@ -1056,10 +1126,14 @@ static inline void isig(int sig, struct tty_struct *tty)
1056 * An RS232 break event has been hit in the incoming bitstream. This 1126 * An RS232 break event has been hit in the incoming bitstream. This
1057 * can cause a variety of events depending upon the termios settings. 1127 * can cause a variety of events depending upon the termios settings.
1058 * 1128 *
1059 * Called from the receive_buf path so single threaded. 1129 * n_tty_receive_buf()/producer path:
1130 * caller holds non-exclusive termios_rwsem
1131 * publishes read_head via put_tty_queue()
1132 *
1133 * Note: may get exclusive termios_rwsem if flushing input buffer
1060 */ 1134 */
1061 1135
1062static inline void n_tty_receive_break(struct tty_struct *tty) 1136static void n_tty_receive_break(struct tty_struct *tty)
1063{ 1137{
1064 struct n_tty_data *ldata = tty->disc_data; 1138 struct n_tty_data *ldata = tty->disc_data;
1065 1139
@@ -1068,8 +1142,11 @@ static inline void n_tty_receive_break(struct tty_struct *tty)
1068 if (I_BRKINT(tty)) { 1142 if (I_BRKINT(tty)) {
1069 isig(SIGINT, tty); 1143 isig(SIGINT, tty);
1070 if (!L_NOFLSH(tty)) { 1144 if (!L_NOFLSH(tty)) {
1145 /* flushing needs exclusive termios_rwsem */
1146 up_read(&tty->termios_rwsem);
1071 n_tty_flush_buffer(tty); 1147 n_tty_flush_buffer(tty);
1072 tty_driver_flush_buffer(tty); 1148 tty_driver_flush_buffer(tty);
1149 down_read(&tty->termios_rwsem);
1073 } 1150 }
1074 return; 1151 return;
1075 } 1152 }
@@ -1094,7 +1171,7 @@ static inline void n_tty_receive_break(struct tty_struct *tty)
1094 * private. 1171 * private.
1095 */ 1172 */
1096 1173
1097static inline void n_tty_receive_overrun(struct tty_struct *tty) 1174static void n_tty_receive_overrun(struct tty_struct *tty)
1098{ 1175{
1099 struct n_tty_data *ldata = tty->disc_data; 1176 struct n_tty_data *ldata = tty->disc_data;
1100 char buf[64]; 1177 char buf[64];
@@ -1116,10 +1193,13 @@ static inline void n_tty_receive_overrun(struct tty_struct *tty)
1116 * @c: character 1193 * @c: character
1117 * 1194 *
1118 * Process a parity error and queue the right data to indicate 1195 * Process a parity error and queue the right data to indicate
1119 * the error case if necessary. Locking as per n_tty_receive_buf. 1196 * the error case if necessary.
1197 *
1198 * n_tty_receive_buf()/producer path:
1199 * caller holds non-exclusive termios_rwsem
1200 * publishes read_head via put_tty_queue()
1120 */ 1201 */
1121static inline void n_tty_receive_parity_error(struct tty_struct *tty, 1202static void n_tty_receive_parity_error(struct tty_struct *tty, unsigned char c)
1122 unsigned char c)
1123{ 1203{
1124 struct n_tty_data *ldata = tty->disc_data; 1204 struct n_tty_data *ldata = tty->disc_data;
1125 1205
@@ -1136,6 +1216,26 @@ static inline void n_tty_receive_parity_error(struct tty_struct *tty,
1136 wake_up_interruptible(&tty->read_wait); 1216 wake_up_interruptible(&tty->read_wait);
1137} 1217}
1138 1218
1219static void
1220n_tty_receive_signal_char(struct tty_struct *tty, int signal, unsigned char c)
1221{
1222 if (!L_NOFLSH(tty)) {
1223 /* flushing needs exclusive termios_rwsem */
1224 up_read(&tty->termios_rwsem);
1225 n_tty_flush_buffer(tty);
1226 tty_driver_flush_buffer(tty);
1227 down_read(&tty->termios_rwsem);
1228 }
1229 if (I_IXON(tty))
1230 start_tty(tty);
1231 if (L_ECHO(tty)) {
1232 echo_char(c, tty);
1233 commit_echoes(tty);
1234 }
1235 isig(signal, tty);
1236 return;
1237}
1238
1139/** 1239/**
1140 * n_tty_receive_char - perform processing 1240 * n_tty_receive_char - perform processing
1141 * @tty: terminal device 1241 * @tty: terminal device
@@ -1144,117 +1244,54 @@ static inline void n_tty_receive_parity_error(struct tty_struct *tty,
1144 * Process an individual character of input received from the driver. 1244 * Process an individual character of input received from the driver.
1145 * This is serialized with respect to itself by the rules for the 1245 * This is serialized with respect to itself by the rules for the
1146 * driver above. 1246 * driver above.
1247 *
1248 * n_tty_receive_buf()/producer path:
1249 * caller holds non-exclusive termios_rwsem
1250 * publishes canon_head if canonical mode is active
1251 * otherwise, publishes read_head via put_tty_queue()
1252 *
1253 * Returns 1 if LNEXT was received, else returns 0
1147 */ 1254 */
1148 1255
1149static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c) 1256static int
1257n_tty_receive_char_special(struct tty_struct *tty, unsigned char c)
1150{ 1258{
1151 struct n_tty_data *ldata = tty->disc_data; 1259 struct n_tty_data *ldata = tty->disc_data;
1152 unsigned long flags;
1153 int parmrk; 1260 int parmrk;
1154 1261
1155 if (ldata->raw) {
1156 put_tty_queue(c, ldata);
1157 return;
1158 }
1159
1160 if (I_ISTRIP(tty))
1161 c &= 0x7f;
1162 if (I_IUCLC(tty) && L_IEXTEN(tty))
1163 c = tolower(c);
1164
1165 if (L_EXTPROC(tty)) {
1166 put_tty_queue(c, ldata);
1167 return;
1168 }
1169
1170 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) &&
1171 I_IXANY(tty) && c != START_CHAR(tty) && c != STOP_CHAR(tty) &&
1172 c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) && c != SUSP_CHAR(tty)) {
1173 start_tty(tty);
1174 process_echoes(tty);
1175 }
1176
1177 if (tty->closing) {
1178 if (I_IXON(tty)) {
1179 if (c == START_CHAR(tty)) {
1180 start_tty(tty);
1181 process_echoes(tty);
1182 } else if (c == STOP_CHAR(tty))
1183 stop_tty(tty);
1184 }
1185 return;
1186 }
1187
1188 /*
1189 * If the previous character was LNEXT, or we know that this
1190 * character is not one of the characters that we'll have to
1191 * handle specially, do shortcut processing to speed things
1192 * up.
1193 */
1194 if (!test_bit(c, ldata->process_char_map) || ldata->lnext) {
1195 ldata->lnext = 0;
1196 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
1197 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
1198 /* beep if no space */
1199 if (L_ECHO(tty))
1200 process_output('\a', tty);
1201 return;
1202 }
1203 if (L_ECHO(tty)) {
1204 finish_erasing(ldata);
1205 /* Record the column of first canon char. */
1206 if (ldata->canon_head == ldata->read_head)
1207 echo_set_canon_col(ldata);
1208 echo_char(c, tty);
1209 process_echoes(tty);
1210 }
1211 if (parmrk)
1212 put_tty_queue(c, ldata);
1213 put_tty_queue(c, ldata);
1214 return;
1215 }
1216
1217 if (I_IXON(tty)) { 1262 if (I_IXON(tty)) {
1218 if (c == START_CHAR(tty)) { 1263 if (c == START_CHAR(tty)) {
1219 start_tty(tty); 1264 start_tty(tty);
1220 process_echoes(tty); 1265 commit_echoes(tty);
1221 return; 1266 return 0;
1222 } 1267 }
1223 if (c == STOP_CHAR(tty)) { 1268 if (c == STOP_CHAR(tty)) {
1224 stop_tty(tty); 1269 stop_tty(tty);
1225 return; 1270 return 0;
1226 } 1271 }
1227 } 1272 }
1228 1273
1229 if (L_ISIG(tty)) { 1274 if (L_ISIG(tty)) {
1230 int signal; 1275 if (c == INTR_CHAR(tty)) {
1231 signal = SIGINT; 1276 n_tty_receive_signal_char(tty, SIGINT, c);
1232 if (c == INTR_CHAR(tty)) 1277 return 0;
1233 goto send_signal; 1278 } else if (c == QUIT_CHAR(tty)) {
1234 signal = SIGQUIT; 1279 n_tty_receive_signal_char(tty, SIGQUIT, c);
1235 if (c == QUIT_CHAR(tty)) 1280 return 0;
1236 goto send_signal; 1281 } else if (c == SUSP_CHAR(tty)) {
1237 signal = SIGTSTP; 1282 n_tty_receive_signal_char(tty, SIGTSTP, c);
1238 if (c == SUSP_CHAR(tty)) { 1283 return 0;
1239send_signal:
1240 if (!L_NOFLSH(tty)) {
1241 n_tty_flush_buffer(tty);
1242 tty_driver_flush_buffer(tty);
1243 }
1244 if (I_IXON(tty))
1245 start_tty(tty);
1246 if (L_ECHO(tty)) {
1247 echo_char(c, tty);
1248 process_echoes(tty);
1249 }
1250 isig(signal, tty);
1251 return;
1252 } 1284 }
1253 } 1285 }
1254 1286
1287 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && I_IXANY(tty)) {
1288 start_tty(tty);
1289 process_echoes(tty);
1290 }
1291
1255 if (c == '\r') { 1292 if (c == '\r') {
1256 if (I_IGNCR(tty)) 1293 if (I_IGNCR(tty))
1257 return; 1294 return 0;
1258 if (I_ICRNL(tty)) 1295 if (I_ICRNL(tty))
1259 c = '\n'; 1296 c = '\n';
1260 } else if (c == '\n' && I_INLCR(tty)) 1297 } else if (c == '\n' && I_INLCR(tty))
@@ -1264,8 +1301,8 @@ send_signal:
1264 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) || 1301 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
1265 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) { 1302 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
1266 eraser(c, tty); 1303 eraser(c, tty);
1267 process_echoes(tty); 1304 commit_echoes(tty);
1268 return; 1305 return 0;
1269 } 1306 }
1270 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) { 1307 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
1271 ldata->lnext = 1; 1308 ldata->lnext = 1;
@@ -1274,42 +1311,32 @@ send_signal:
1274 if (L_ECHOCTL(tty)) { 1311 if (L_ECHOCTL(tty)) {
1275 echo_char_raw('^', ldata); 1312 echo_char_raw('^', ldata);
1276 echo_char_raw('\b', ldata); 1313 echo_char_raw('\b', ldata);
1277 process_echoes(tty); 1314 commit_echoes(tty);
1278 } 1315 }
1279 } 1316 }
1280 return; 1317 return 1;
1281 } 1318 }
1282 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) && 1319 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) && L_IEXTEN(tty)) {
1283 L_IEXTEN(tty)) { 1320 size_t tail = ldata->canon_head;
1284 unsigned long tail = ldata->canon_head;
1285 1321
1286 finish_erasing(ldata); 1322 finish_erasing(ldata);
1287 echo_char(c, tty); 1323 echo_char(c, tty);
1288 echo_char_raw('\n', ldata); 1324 echo_char_raw('\n', ldata);
1289 while (tail != ldata->read_head) { 1325 while (tail != ldata->read_head) {
1290 echo_char(ldata->read_buf[tail], tty); 1326 echo_char(read_buf(ldata, tail), tty);
1291 tail = (tail+1) & (N_TTY_BUF_SIZE-1); 1327 tail++;
1292 } 1328 }
1293 process_echoes(tty); 1329 commit_echoes(tty);
1294 return; 1330 return 0;
1295 } 1331 }
1296 if (c == '\n') { 1332 if (c == '\n') {
1297 if (ldata->read_cnt >= N_TTY_BUF_SIZE) {
1298 if (L_ECHO(tty))
1299 process_output('\a', tty);
1300 return;
1301 }
1302 if (L_ECHO(tty) || L_ECHONL(tty)) { 1333 if (L_ECHO(tty) || L_ECHONL(tty)) {
1303 echo_char_raw('\n', ldata); 1334 echo_char_raw('\n', ldata);
1304 process_echoes(tty); 1335 commit_echoes(tty);
1305 } 1336 }
1306 goto handle_newline; 1337 goto handle_newline;
1307 } 1338 }
1308 if (c == EOF_CHAR(tty)) { 1339 if (c == EOF_CHAR(tty)) {
1309 if (ldata->read_cnt >= N_TTY_BUF_SIZE)
1310 return;
1311 if (ldata->canon_head != ldata->read_head)
1312 set_bit(TTY_PUSH, &tty->flags);
1313 c = __DISABLED_CHAR; 1340 c = __DISABLED_CHAR;
1314 goto handle_newline; 1341 goto handle_newline;
1315 } 1342 }
@@ -1317,11 +1344,6 @@ send_signal:
1317 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) { 1344 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
1318 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) 1345 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty))
1319 ? 1 : 0; 1346 ? 1 : 0;
1320 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk)) {
1321 if (L_ECHO(tty))
1322 process_output('\a', tty);
1323 return;
1324 }
1325 /* 1347 /*
1326 * XXX are EOL_CHAR and EOL2_CHAR echoed?!? 1348 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
1327 */ 1349 */
@@ -1330,7 +1352,7 @@ send_signal:
1330 if (ldata->canon_head == ldata->read_head) 1352 if (ldata->canon_head == ldata->read_head)
1331 echo_set_canon_col(ldata); 1353 echo_set_canon_col(ldata);
1332 echo_char(c, tty); 1354 echo_char(c, tty);
1333 process_echoes(tty); 1355 commit_echoes(tty);
1334 } 1356 }
1335 /* 1357 /*
1336 * XXX does PARMRK doubling happen for 1358 * XXX does PARMRK doubling happen for
@@ -1340,26 +1362,17 @@ send_signal:
1340 put_tty_queue(c, ldata); 1362 put_tty_queue(c, ldata);
1341 1363
1342handle_newline: 1364handle_newline:
1343 raw_spin_lock_irqsave(&ldata->read_lock, flags); 1365 set_bit(ldata->read_head & (N_TTY_BUF_SIZE - 1), ldata->read_flags);
1344 set_bit(ldata->read_head, ldata->read_flags); 1366 put_tty_queue(c, ldata);
1345 put_tty_queue_nolock(c, ldata);
1346 ldata->canon_head = ldata->read_head; 1367 ldata->canon_head = ldata->read_head;
1347 ldata->canon_data++;
1348 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
1349 kill_fasync(&tty->fasync, SIGIO, POLL_IN); 1368 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1350 if (waitqueue_active(&tty->read_wait)) 1369 if (waitqueue_active(&tty->read_wait))
1351 wake_up_interruptible(&tty->read_wait); 1370 wake_up_interruptible(&tty->read_wait);
1352 return; 1371 return 0;
1353 } 1372 }
1354 } 1373 }
1355 1374
1356 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0; 1375 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
1357 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
1358 /* beep if no space */
1359 if (L_ECHO(tty))
1360 process_output('\a', tty);
1361 return;
1362 }
1363 if (L_ECHO(tty)) { 1376 if (L_ECHO(tty)) {
1364 finish_erasing(ldata); 1377 finish_erasing(ldata);
1365 if (c == '\n') 1378 if (c == '\n')
@@ -1370,29 +1383,123 @@ handle_newline:
1370 echo_set_canon_col(ldata); 1383 echo_set_canon_col(ldata);
1371 echo_char(c, tty); 1384 echo_char(c, tty);
1372 } 1385 }
1373 process_echoes(tty); 1386 commit_echoes(tty);
1374 } 1387 }
1375 1388
1376 if (parmrk) 1389 if (parmrk)
1377 put_tty_queue(c, ldata); 1390 put_tty_queue(c, ldata);
1378 1391
1379 put_tty_queue(c, ldata); 1392 put_tty_queue(c, ldata);
1393 return 0;
1380} 1394}
1381 1395
1396static inline void
1397n_tty_receive_char_inline(struct tty_struct *tty, unsigned char c)
1398{
1399 struct n_tty_data *ldata = tty->disc_data;
1400 int parmrk;
1382 1401
1383/** 1402 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && I_IXANY(tty)) {
1384 * n_tty_write_wakeup - asynchronous I/O notifier 1403 start_tty(tty);
1385 * @tty: tty device 1404 process_echoes(tty);
1386 * 1405 }
1387 * Required for the ptys, serial driver etc. since processes 1406 if (L_ECHO(tty)) {
1388 * that attach themselves to the master and rely on ASYNC 1407 finish_erasing(ldata);
1389 * IO must be woken up 1408 /* Record the column of first canon char. */
1390 */ 1409 if (ldata->canon_head == ldata->read_head)
1410 echo_set_canon_col(ldata);
1411 echo_char(c, tty);
1412 commit_echoes(tty);
1413 }
1414 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
1415 if (parmrk)
1416 put_tty_queue(c, ldata);
1417 put_tty_queue(c, ldata);
1418}
1391 1419
1392static void n_tty_write_wakeup(struct tty_struct *tty) 1420static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
1393{ 1421{
1394 if (tty->fasync && test_and_clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) 1422 n_tty_receive_char_inline(tty, c);
1395 kill_fasync(&tty->fasync, SIGIO, POLL_OUT); 1423}
1424
1425static inline void
1426n_tty_receive_char_fast(struct tty_struct *tty, unsigned char c)
1427{
1428 struct n_tty_data *ldata = tty->disc_data;
1429
1430 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && I_IXANY(tty)) {
1431 start_tty(tty);
1432 process_echoes(tty);
1433 }
1434 if (L_ECHO(tty)) {
1435 finish_erasing(ldata);
1436 /* Record the column of first canon char. */
1437 if (ldata->canon_head == ldata->read_head)
1438 echo_set_canon_col(ldata);
1439 echo_char(c, tty);
1440 commit_echoes(tty);
1441 }
1442 put_tty_queue(c, ldata);
1443}
1444
1445static inline void
1446n_tty_receive_char_closing(struct tty_struct *tty, unsigned char c)
1447{
1448 if (I_ISTRIP(tty))
1449 c &= 0x7f;
1450 if (I_IUCLC(tty) && L_IEXTEN(tty))
1451 c = tolower(c);
1452
1453 if (I_IXON(tty)) {
1454 if (c == STOP_CHAR(tty))
1455 stop_tty(tty);
1456 else if (c == START_CHAR(tty) ||
1457 (tty->stopped && !tty->flow_stopped && I_IXANY(tty) &&
1458 c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) &&
1459 c != SUSP_CHAR(tty))) {
1460 start_tty(tty);
1461 process_echoes(tty);
1462 }
1463 }
1464}
1465
1466static void
1467n_tty_receive_char_flagged(struct tty_struct *tty, unsigned char c, char flag)
1468{
1469 char buf[64];
1470
1471 switch (flag) {
1472 case TTY_BREAK:
1473 n_tty_receive_break(tty);
1474 break;
1475 case TTY_PARITY:
1476 case TTY_FRAME:
1477 n_tty_receive_parity_error(tty, c);
1478 break;
1479 case TTY_OVERRUN:
1480 n_tty_receive_overrun(tty);
1481 break;
1482 default:
1483 printk(KERN_ERR "%s: unknown flag %d\n",
1484 tty_name(tty, buf), flag);
1485 break;
1486 }
1487}
1488
1489static void
1490n_tty_receive_char_lnext(struct tty_struct *tty, unsigned char c, char flag)
1491{
1492 struct n_tty_data *ldata = tty->disc_data;
1493
1494 ldata->lnext = 0;
1495 if (likely(flag == TTY_NORMAL)) {
1496 if (I_ISTRIP(tty))
1497 c &= 0x7f;
1498 if (I_IUCLC(tty) && L_IEXTEN(tty))
1499 c = tolower(c);
1500 n_tty_receive_char(tty, c);
1501 } else
1502 n_tty_receive_char_flagged(tty, c, flag);
1396} 1503}
1397 1504
1398/** 1505/**
@@ -1406,86 +1513,220 @@ static void n_tty_write_wakeup(struct tty_struct *tty)
1406 * been received. This function must be called from soft contexts 1513 * been received. This function must be called from soft contexts
1407 * not from interrupt context. The driver is responsible for making 1514 * not from interrupt context. The driver is responsible for making
1408 * calls one at a time and in order (or using flush_to_ldisc) 1515 * calls one at a time and in order (or using flush_to_ldisc)
1516 *
1517 * n_tty_receive_buf()/producer path:
1518 * claims non-exclusive termios_rwsem
1519 * publishes read_head and canon_head
1409 */ 1520 */
1410 1521
1411static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp, 1522static void
1412 char *fp, int count) 1523n_tty_receive_buf_real_raw(struct tty_struct *tty, const unsigned char *cp,
1524 char *fp, int count)
1413{ 1525{
1414 struct n_tty_data *ldata = tty->disc_data; 1526 struct n_tty_data *ldata = tty->disc_data;
1415 const unsigned char *p; 1527 size_t n, head;
1416 char *f, flags = TTY_NORMAL; 1528
1417 int i; 1529 head = ldata->read_head & (N_TTY_BUF_SIZE - 1);
1418 char buf[64]; 1530 n = N_TTY_BUF_SIZE - max(read_cnt(ldata), head);
1419 unsigned long cpuflags; 1531 n = min_t(size_t, count, n);
1420 1532 memcpy(read_buf_addr(ldata, head), cp, n);
1421 if (ldata->real_raw) { 1533 ldata->read_head += n;
1422 raw_spin_lock_irqsave(&ldata->read_lock, cpuflags); 1534 cp += n;
1423 i = min(N_TTY_BUF_SIZE - ldata->read_cnt, 1535 count -= n;
1424 N_TTY_BUF_SIZE - ldata->read_head); 1536
1425 i = min(count, i); 1537 head = ldata->read_head & (N_TTY_BUF_SIZE - 1);
1426 memcpy(ldata->read_buf + ldata->read_head, cp, i); 1538 n = N_TTY_BUF_SIZE - max(read_cnt(ldata), head);
1427 ldata->read_head = (ldata->read_head + i) & (N_TTY_BUF_SIZE-1); 1539 n = min_t(size_t, count, n);
1428 ldata->read_cnt += i; 1540 memcpy(read_buf_addr(ldata, head), cp, n);
1429 cp += i; 1541 ldata->read_head += n;
1430 count -= i; 1542}
1431 1543
1432 i = min(N_TTY_BUF_SIZE - ldata->read_cnt, 1544static void
1433 N_TTY_BUF_SIZE - ldata->read_head); 1545n_tty_receive_buf_raw(struct tty_struct *tty, const unsigned char *cp,
1434 i = min(count, i); 1546 char *fp, int count)
1435 memcpy(ldata->read_buf + ldata->read_head, cp, i); 1547{
1436 ldata->read_head = (ldata->read_head + i) & (N_TTY_BUF_SIZE-1); 1548 struct n_tty_data *ldata = tty->disc_data;
1437 ldata->read_cnt += i; 1549 char flag = TTY_NORMAL;
1438 raw_spin_unlock_irqrestore(&ldata->read_lock, cpuflags); 1550
1439 } else { 1551 while (count--) {
1440 for (i = count, p = cp, f = fp; i; i--, p++) { 1552 if (fp)
1441 if (f) 1553 flag = *fp++;
1442 flags = *f++; 1554 if (likely(flag == TTY_NORMAL))
1443 switch (flags) { 1555 put_tty_queue(*cp++, ldata);
1444 case TTY_NORMAL: 1556 else
1445 n_tty_receive_char(tty, *p); 1557 n_tty_receive_char_flagged(tty, *cp++, flag);
1446 break; 1558 }
1447 case TTY_BREAK: 1559}
1448 n_tty_receive_break(tty); 1560
1449 break; 1561static void
1450 case TTY_PARITY: 1562n_tty_receive_buf_closing(struct tty_struct *tty, const unsigned char *cp,
1451 case TTY_FRAME: 1563 char *fp, int count)
1452 n_tty_receive_parity_error(tty, *p); 1564{
1453 break; 1565 char flag = TTY_NORMAL;
1454 case TTY_OVERRUN: 1566
1455 n_tty_receive_overrun(tty); 1567 while (count--) {
1456 break; 1568 if (fp)
1457 default: 1569 flag = *fp++;
1458 printk(KERN_ERR "%s: unknown flag %d\n", 1570 if (likely(flag == TTY_NORMAL))
1459 tty_name(tty, buf), flags); 1571 n_tty_receive_char_closing(tty, *cp++);
1460 break; 1572 else
1573 n_tty_receive_char_flagged(tty, *cp++, flag);
1574 }
1575}
1576
1577static void
1578n_tty_receive_buf_standard(struct tty_struct *tty, const unsigned char *cp,
1579 char *fp, int count)
1580{
1581 struct n_tty_data *ldata = tty->disc_data;
1582 char flag = TTY_NORMAL;
1583
1584 while (count--) {
1585 if (fp)
1586 flag = *fp++;
1587 if (likely(flag == TTY_NORMAL)) {
1588 unsigned char c = *cp++;
1589
1590 if (I_ISTRIP(tty))
1591 c &= 0x7f;
1592 if (I_IUCLC(tty) && L_IEXTEN(tty))
1593 c = tolower(c);
1594 if (L_EXTPROC(tty)) {
1595 put_tty_queue(c, ldata);
1596 continue;
1461 } 1597 }
1598 if (!test_bit(c, ldata->char_map))
1599 n_tty_receive_char_inline(tty, c);
1600 else if (n_tty_receive_char_special(tty, c) && count) {
1601 if (fp)
1602 flag = *fp++;
1603 n_tty_receive_char_lnext(tty, *cp++, flag);
1604 count--;
1605 }
1606 } else
1607 n_tty_receive_char_flagged(tty, *cp++, flag);
1608 }
1609}
1610
1611static void
1612n_tty_receive_buf_fast(struct tty_struct *tty, const unsigned char *cp,
1613 char *fp, int count)
1614{
1615 struct n_tty_data *ldata = tty->disc_data;
1616 char flag = TTY_NORMAL;
1617
1618 while (count--) {
1619 if (fp)
1620 flag = *fp++;
1621 if (likely(flag == TTY_NORMAL)) {
1622 unsigned char c = *cp++;
1623
1624 if (!test_bit(c, ldata->char_map))
1625 n_tty_receive_char_fast(tty, c);
1626 else if (n_tty_receive_char_special(tty, c) && count) {
1627 if (fp)
1628 flag = *fp++;
1629 n_tty_receive_char_lnext(tty, *cp++, flag);
1630 count--;
1631 }
1632 } else
1633 n_tty_receive_char_flagged(tty, *cp++, flag);
1634 }
1635}
1636
1637static void __receive_buf(struct tty_struct *tty, const unsigned char *cp,
1638 char *fp, int count)
1639{
1640 struct n_tty_data *ldata = tty->disc_data;
1641 bool preops = I_ISTRIP(tty) || (I_IUCLC(tty) && L_IEXTEN(tty));
1642
1643 if (ldata->real_raw)
1644 n_tty_receive_buf_real_raw(tty, cp, fp, count);
1645 else if (ldata->raw || (L_EXTPROC(tty) && !preops))
1646 n_tty_receive_buf_raw(tty, cp, fp, count);
1647 else if (tty->closing && !L_EXTPROC(tty))
1648 n_tty_receive_buf_closing(tty, cp, fp, count);
1649 else {
1650 if (ldata->lnext) {
1651 char flag = TTY_NORMAL;
1652
1653 if (fp)
1654 flag = *fp++;
1655 n_tty_receive_char_lnext(tty, *cp++, flag);
1656 count--;
1462 } 1657 }
1658
1659 if (!preops && !I_PARMRK(tty))
1660 n_tty_receive_buf_fast(tty, cp, fp, count);
1661 else
1662 n_tty_receive_buf_standard(tty, cp, fp, count);
1663
1664 flush_echoes(tty);
1463 if (tty->ops->flush_chars) 1665 if (tty->ops->flush_chars)
1464 tty->ops->flush_chars(tty); 1666 tty->ops->flush_chars(tty);
1465 } 1667 }
1466 1668
1467 set_room(tty); 1669 if ((!ldata->icanon && (read_cnt(ldata) >= ldata->minimum_to_wake)) ||
1468
1469 if ((!ldata->icanon && (ldata->read_cnt >= ldata->minimum_to_wake)) ||
1470 L_EXTPROC(tty)) { 1670 L_EXTPROC(tty)) {
1471 kill_fasync(&tty->fasync, SIGIO, POLL_IN); 1671 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1472 if (waitqueue_active(&tty->read_wait)) 1672 if (waitqueue_active(&tty->read_wait))
1473 wake_up_interruptible(&tty->read_wait); 1673 wake_up_interruptible(&tty->read_wait);
1474 } 1674 }
1675}
1676
1677static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1678 char *fp, int count)
1679{
1680 int room, n;
1681
1682 down_read(&tty->termios_rwsem);
1475 1683
1476 /*
1477 * Check the remaining room for the input canonicalization
1478 * mode. We don't want to throttle the driver if we're in
1479 * canonical mode and don't have a newline yet!
1480 */
1481 while (1) { 1684 while (1) {
1482 tty_set_flow_change(tty, TTY_THROTTLE_SAFE); 1685 room = receive_room(tty);
1483 if (tty->receive_room >= TTY_THRESHOLD_THROTTLE) 1686 n = min(count, room);
1687 if (!n)
1484 break; 1688 break;
1485 if (!tty_throttle_safe(tty)) 1689 __receive_buf(tty, cp, fp, n);
1690 cp += n;
1691 if (fp)
1692 fp += n;
1693 count -= n;
1694 }
1695
1696 tty->receive_room = room;
1697 n_tty_check_throttle(tty);
1698 up_read(&tty->termios_rwsem);
1699}
1700
1701static int n_tty_receive_buf2(struct tty_struct *tty, const unsigned char *cp,
1702 char *fp, int count)
1703{
1704 struct n_tty_data *ldata = tty->disc_data;
1705 int room, n, rcvd = 0;
1706
1707 down_read(&tty->termios_rwsem);
1708
1709 while (1) {
1710 room = receive_room(tty);
1711 n = min(count, room);
1712 if (!n) {
1713 if (!room)
1714 ldata->no_room = 1;
1486 break; 1715 break;
1716 }
1717 __receive_buf(tty, cp, fp, n);
1718 cp += n;
1719 if (fp)
1720 fp += n;
1721 count -= n;
1722 rcvd += n;
1487 } 1723 }
1488 __tty_set_flow_change(tty, 0); 1724
1725 tty->receive_room = room;
1726 n_tty_check_throttle(tty);
1727 up_read(&tty->termios_rwsem);
1728
1729 return rcvd;
1489} 1730}
1490 1731
1491int is_ignored(int sig) 1732int is_ignored(int sig)
@@ -1505,7 +1746,7 @@ int is_ignored(int sig)
1505 * guaranteed that this function will not be re-entered or in progress 1746 * guaranteed that this function will not be re-entered or in progress
1506 * when the ldisc is closed. 1747 * when the ldisc is closed.
1507 * 1748 *
1508 * Locking: Caller holds tty->termios_mutex 1749 * Locking: Caller holds tty->termios_rwsem
1509 */ 1750 */
1510 1751
1511static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old) 1752static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
@@ -1517,12 +1758,13 @@ static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
1517 canon_change = (old->c_lflag ^ tty->termios.c_lflag) & ICANON; 1758 canon_change = (old->c_lflag ^ tty->termios.c_lflag) & ICANON;
1518 if (canon_change) { 1759 if (canon_change) {
1519 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE); 1760 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
1761 ldata->line_start = 0;
1520 ldata->canon_head = ldata->read_tail; 1762 ldata->canon_head = ldata->read_tail;
1521 ldata->canon_data = 0;
1522 ldata->erasing = 0; 1763 ldata->erasing = 0;
1764 ldata->lnext = 0;
1523 } 1765 }
1524 1766
1525 if (canon_change && !L_ICANON(tty) && ldata->read_cnt) 1767 if (canon_change && !L_ICANON(tty) && read_cnt(ldata))
1526 wake_up_interruptible(&tty->read_wait); 1768 wake_up_interruptible(&tty->read_wait);
1527 1769
1528 ldata->icanon = (L_ICANON(tty) != 0); 1770 ldata->icanon = (L_ICANON(tty) != 0);
@@ -1531,41 +1773,38 @@ static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
1531 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) || 1773 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
1532 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) || 1774 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
1533 I_PARMRK(tty)) { 1775 I_PARMRK(tty)) {
1534 bitmap_zero(ldata->process_char_map, 256); 1776 bitmap_zero(ldata->char_map, 256);
1535 1777
1536 if (I_IGNCR(tty) || I_ICRNL(tty)) 1778 if (I_IGNCR(tty) || I_ICRNL(tty))
1537 set_bit('\r', ldata->process_char_map); 1779 set_bit('\r', ldata->char_map);
1538 if (I_INLCR(tty)) 1780 if (I_INLCR(tty))
1539 set_bit('\n', ldata->process_char_map); 1781 set_bit('\n', ldata->char_map);
1540 1782
1541 if (L_ICANON(tty)) { 1783 if (L_ICANON(tty)) {
1542 set_bit(ERASE_CHAR(tty), ldata->process_char_map); 1784 set_bit(ERASE_CHAR(tty), ldata->char_map);
1543 set_bit(KILL_CHAR(tty), ldata->process_char_map); 1785 set_bit(KILL_CHAR(tty), ldata->char_map);
1544 set_bit(EOF_CHAR(tty), ldata->process_char_map); 1786 set_bit(EOF_CHAR(tty), ldata->char_map);
1545 set_bit('\n', ldata->process_char_map); 1787 set_bit('\n', ldata->char_map);
1546 set_bit(EOL_CHAR(tty), ldata->process_char_map); 1788 set_bit(EOL_CHAR(tty), ldata->char_map);
1547 if (L_IEXTEN(tty)) { 1789 if (L_IEXTEN(tty)) {
1548 set_bit(WERASE_CHAR(tty), 1790 set_bit(WERASE_CHAR(tty), ldata->char_map);
1549 ldata->process_char_map); 1791 set_bit(LNEXT_CHAR(tty), ldata->char_map);
1550 set_bit(LNEXT_CHAR(tty), 1792 set_bit(EOL2_CHAR(tty), ldata->char_map);
1551 ldata->process_char_map);
1552 set_bit(EOL2_CHAR(tty),
1553 ldata->process_char_map);
1554 if (L_ECHO(tty)) 1793 if (L_ECHO(tty))
1555 set_bit(REPRINT_CHAR(tty), 1794 set_bit(REPRINT_CHAR(tty),
1556 ldata->process_char_map); 1795 ldata->char_map);
1557 } 1796 }
1558 } 1797 }
1559 if (I_IXON(tty)) { 1798 if (I_IXON(tty)) {
1560 set_bit(START_CHAR(tty), ldata->process_char_map); 1799 set_bit(START_CHAR(tty), ldata->char_map);
1561 set_bit(STOP_CHAR(tty), ldata->process_char_map); 1800 set_bit(STOP_CHAR(tty), ldata->char_map);
1562 } 1801 }
1563 if (L_ISIG(tty)) { 1802 if (L_ISIG(tty)) {
1564 set_bit(INTR_CHAR(tty), ldata->process_char_map); 1803 set_bit(INTR_CHAR(tty), ldata->char_map);
1565 set_bit(QUIT_CHAR(tty), ldata->process_char_map); 1804 set_bit(QUIT_CHAR(tty), ldata->char_map);
1566 set_bit(SUSP_CHAR(tty), ldata->process_char_map); 1805 set_bit(SUSP_CHAR(tty), ldata->char_map);
1567 } 1806 }
1568 clear_bit(__DISABLED_CHAR, ldata->process_char_map); 1807 clear_bit(__DISABLED_CHAR, ldata->char_map);
1569 ldata->raw = 0; 1808 ldata->raw = 0;
1570 ldata->real_raw = 0; 1809 ldata->real_raw = 0;
1571 } else { 1810 } else {
@@ -1608,9 +1847,7 @@ static void n_tty_close(struct tty_struct *tty)
1608 if (tty->link) 1847 if (tty->link)
1609 n_tty_packet_mode_flush(tty); 1848 n_tty_packet_mode_flush(tty);
1610 1849
1611 kfree(ldata->read_buf); 1850 vfree(ldata);
1612 kfree(ldata->echo_buf);
1613 kfree(ldata);
1614 tty->disc_data = NULL; 1851 tty->disc_data = NULL;
1615} 1852}
1616 1853
@@ -1628,26 +1865,23 @@ static int n_tty_open(struct tty_struct *tty)
1628{ 1865{
1629 struct n_tty_data *ldata; 1866 struct n_tty_data *ldata;
1630 1867
1631 ldata = kzalloc(sizeof(*ldata), GFP_KERNEL); 1868 /* Currently a malloc failure here can panic */
1869 ldata = vmalloc(sizeof(*ldata));
1632 if (!ldata) 1870 if (!ldata)
1633 goto err; 1871 goto err;
1634 1872
1635 ldata->overrun_time = jiffies; 1873 ldata->overrun_time = jiffies;
1636 mutex_init(&ldata->atomic_read_lock); 1874 mutex_init(&ldata->atomic_read_lock);
1637 mutex_init(&ldata->output_lock); 1875 mutex_init(&ldata->output_lock);
1638 mutex_init(&ldata->echo_lock);
1639 raw_spin_lock_init(&ldata->read_lock);
1640
1641 /* These are ugly. Currently a malloc failure here can panic */
1642 ldata->read_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1643 ldata->echo_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1644 if (!ldata->read_buf || !ldata->echo_buf)
1645 goto err_free_bufs;
1646 1876
1647 tty->disc_data = ldata; 1877 tty->disc_data = ldata;
1648 reset_buffer_flags(tty->disc_data); 1878 reset_buffer_flags(tty->disc_data);
1649 ldata->column = 0; 1879 ldata->column = 0;
1880 ldata->canon_column = 0;
1650 ldata->minimum_to_wake = 1; 1881 ldata->minimum_to_wake = 1;
1882 ldata->num_overrun = 0;
1883 ldata->no_room = 0;
1884 ldata->lnext = 0;
1651 tty->closing = 0; 1885 tty->closing = 0;
1652 /* indicate buffer work may resume */ 1886 /* indicate buffer work may resume */
1653 clear_bit(TTY_LDISC_HALTED, &tty->flags); 1887 clear_bit(TTY_LDISC_HALTED, &tty->flags);
@@ -1655,10 +1889,6 @@ static int n_tty_open(struct tty_struct *tty)
1655 tty_unthrottle(tty); 1889 tty_unthrottle(tty);
1656 1890
1657 return 0; 1891 return 0;
1658err_free_bufs:
1659 kfree(ldata->read_buf);
1660 kfree(ldata->echo_buf);
1661 kfree(ldata);
1662err: 1892err:
1663 return -ENOMEM; 1893 return -ENOMEM;
1664} 1894}
@@ -1667,11 +1897,10 @@ static inline int input_available_p(struct tty_struct *tty, int amt)
1667{ 1897{
1668 struct n_tty_data *ldata = tty->disc_data; 1898 struct n_tty_data *ldata = tty->disc_data;
1669 1899
1670 tty_flush_to_ldisc(tty);
1671 if (ldata->icanon && !L_EXTPROC(tty)) { 1900 if (ldata->icanon && !L_EXTPROC(tty)) {
1672 if (ldata->canon_data) 1901 if (ldata->canon_head != ldata->read_tail)
1673 return 1; 1902 return 1;
1674 } else if (ldata->read_cnt >= (amt ? amt : 1)) 1903 } else if (read_cnt(ldata) >= (amt ? amt : 1))
1675 return 1; 1904 return 1;
1676 1905
1677 return 0; 1906 return 0;
@@ -1692,6 +1921,9 @@ static inline int input_available_p(struct tty_struct *tty, int amt)
1692 * 1921 *
1693 * Called under the ldata->atomic_read_lock sem 1922 * Called under the ldata->atomic_read_lock sem
1694 * 1923 *
1924 * n_tty_read()/consumer path:
1925 * caller holds non-exclusive termios_rwsem
1926 * read_tail published
1695 */ 1927 */
1696 1928
1697static int copy_from_read_buf(struct tty_struct *tty, 1929static int copy_from_read_buf(struct tty_struct *tty,
@@ -1702,34 +1934,114 @@ static int copy_from_read_buf(struct tty_struct *tty,
1702 struct n_tty_data *ldata = tty->disc_data; 1934 struct n_tty_data *ldata = tty->disc_data;
1703 int retval; 1935 int retval;
1704 size_t n; 1936 size_t n;
1705 unsigned long flags;
1706 bool is_eof; 1937 bool is_eof;
1938 size_t tail = ldata->read_tail & (N_TTY_BUF_SIZE - 1);
1707 1939
1708 retval = 0; 1940 retval = 0;
1709 raw_spin_lock_irqsave(&ldata->read_lock, flags); 1941 n = min(read_cnt(ldata), N_TTY_BUF_SIZE - tail);
1710 n = min(ldata->read_cnt, N_TTY_BUF_SIZE - ldata->read_tail);
1711 n = min(*nr, n); 1942 n = min(*nr, n);
1712 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
1713 if (n) { 1943 if (n) {
1714 retval = copy_to_user(*b, &ldata->read_buf[ldata->read_tail], n); 1944 retval = copy_to_user(*b, read_buf_addr(ldata, tail), n);
1715 n -= retval; 1945 n -= retval;
1716 is_eof = n == 1 && 1946 is_eof = n == 1 && read_buf(ldata, tail) == EOF_CHAR(tty);
1717 ldata->read_buf[ldata->read_tail] == EOF_CHAR(tty); 1947 tty_audit_add_data(tty, read_buf_addr(ldata, tail), n,
1718 tty_audit_add_data(tty, &ldata->read_buf[ldata->read_tail], n,
1719 ldata->icanon); 1948 ldata->icanon);
1720 raw_spin_lock_irqsave(&ldata->read_lock, flags); 1949 ldata->read_tail += n;
1721 ldata->read_tail = (ldata->read_tail + n) & (N_TTY_BUF_SIZE-1);
1722 ldata->read_cnt -= n;
1723 /* Turn single EOF into zero-length read */ 1950 /* Turn single EOF into zero-length read */
1724 if (L_EXTPROC(tty) && ldata->icanon && is_eof && !ldata->read_cnt) 1951 if (L_EXTPROC(tty) && ldata->icanon && is_eof && !read_cnt(ldata))
1725 n = 0; 1952 n = 0;
1726 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
1727 *b += n; 1953 *b += n;
1728 *nr -= n; 1954 *nr -= n;
1729 } 1955 }
1730 return retval; 1956 return retval;
1731} 1957}
1732 1958
1959/**
1960 * canon_copy_from_read_buf - copy read data in canonical mode
1961 * @tty: terminal device
1962 * @b: user data
1963 * @nr: size of data
1964 *
1965 * Helper function for n_tty_read. It is only called when ICANON is on;
1966 * it copies one line of input up to and including the line-delimiting
1967 * character into the user-space buffer.
1968 *
1969 * Called under the atomic_read_lock mutex
1970 *
1971 * n_tty_read()/consumer path:
1972 * caller holds non-exclusive termios_rwsem
1973 * read_tail published
1974 */
1975
1976static int canon_copy_from_read_buf(struct tty_struct *tty,
1977 unsigned char __user **b,
1978 size_t *nr)
1979{
1980 struct n_tty_data *ldata = tty->disc_data;
1981 size_t n, size, more, c;
1982 size_t eol;
1983 size_t tail;
1984 int ret, found = 0;
1985 bool eof_push = 0;
1986
1987 /* N.B. avoid overrun if nr == 0 */
1988 n = min(*nr, read_cnt(ldata));
1989 if (!n)
1990 return 0;
1991
1992 tail = ldata->read_tail & (N_TTY_BUF_SIZE - 1);
1993 size = min_t(size_t, tail + n, N_TTY_BUF_SIZE);
1994
1995 n_tty_trace("%s: nr:%zu tail:%zu n:%zu size:%zu\n",
1996 __func__, *nr, tail, n, size);
1997
1998 eol = find_next_bit(ldata->read_flags, size, tail);
1999 more = n - (size - tail);
2000 if (eol == N_TTY_BUF_SIZE && more) {
2001 /* scan wrapped without finding set bit */
2002 eol = find_next_bit(ldata->read_flags, more, 0);
2003 if (eol != more)
2004 found = 1;
2005 } else if (eol != size)
2006 found = 1;
2007
2008 size = N_TTY_BUF_SIZE - tail;
2009 n = (found + eol + size) & (N_TTY_BUF_SIZE - 1);
2010 c = n;
2011
2012 if (found && read_buf(ldata, eol) == __DISABLED_CHAR) {
2013 n--;
2014 eof_push = !n && ldata->read_tail != ldata->line_start;
2015 }
2016
2017 n_tty_trace("%s: eol:%zu found:%d n:%zu c:%zu size:%zu more:%zu\n",
2018 __func__, eol, found, n, c, size, more);
2019
2020 if (n > size) {
2021 ret = copy_to_user(*b, read_buf_addr(ldata, tail), size);
2022 if (ret)
2023 return -EFAULT;
2024 ret = copy_to_user(*b + size, ldata->read_buf, n - size);
2025 } else
2026 ret = copy_to_user(*b, read_buf_addr(ldata, tail), n);
2027
2028 if (ret)
2029 return -EFAULT;
2030 *b += n;
2031 *nr -= n;
2032
2033 if (found)
2034 clear_bit(eol, ldata->read_flags);
2035 smp_mb__after_clear_bit();
2036 ldata->read_tail += c;
2037
2038 if (found) {
2039 ldata->line_start = ldata->read_tail;
2040 tty_audit_push(tty);
2041 }
2042 return eof_push ? -EAGAIN : 0;
2043}
2044
1733extern ssize_t redirected_tty_write(struct file *, const char __user *, 2045extern ssize_t redirected_tty_write(struct file *, const char __user *,
1734 size_t, loff_t *); 2046 size_t, loff_t *);
1735 2047
@@ -1787,6 +2099,10 @@ static int job_control(struct tty_struct *tty, struct file *file)
1787 * a hangup. Always called in user context, may sleep. 2099 * a hangup. Always called in user context, may sleep.
1788 * 2100 *
1789 * This code must be sure never to sleep through a hangup. 2101 * This code must be sure never to sleep through a hangup.
2102 *
2103 * n_tty_read()/consumer path:
2104 * claims non-exclusive termios_rwsem
2105 * publishes read_tail
1790 */ 2106 */
1791 2107
1792static ssize_t n_tty_read(struct tty_struct *tty, struct file *file, 2108static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
@@ -1798,16 +2114,27 @@ static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
1798 int c; 2114 int c;
1799 int minimum, time; 2115 int minimum, time;
1800 ssize_t retval = 0; 2116 ssize_t retval = 0;
1801 ssize_t size;
1802 long timeout; 2117 long timeout;
1803 unsigned long flags; 2118 unsigned long flags;
1804 int packet; 2119 int packet;
1805 2120
1806do_it_again:
1807 c = job_control(tty, file); 2121 c = job_control(tty, file);
1808 if (c < 0) 2122 if (c < 0)
1809 return c; 2123 return c;
1810 2124
2125 /*
2126 * Internal serialization of reads.
2127 */
2128 if (file->f_flags & O_NONBLOCK) {
2129 if (!mutex_trylock(&ldata->atomic_read_lock))
2130 return -EAGAIN;
2131 } else {
2132 if (mutex_lock_interruptible(&ldata->atomic_read_lock))
2133 return -ERESTARTSYS;
2134 }
2135
2136 down_read(&tty->termios_rwsem);
2137
1811 minimum = time = 0; 2138 minimum = time = 0;
1812 timeout = MAX_SCHEDULE_TIMEOUT; 2139 timeout = MAX_SCHEDULE_TIMEOUT;
1813 if (!ldata->icanon) { 2140 if (!ldata->icanon) {
@@ -1825,16 +2152,6 @@ do_it_again:
1825 } 2152 }
1826 } 2153 }
1827 2154
1828 /*
1829 * Internal serialization of reads.
1830 */
1831 if (file->f_flags & O_NONBLOCK) {
1832 if (!mutex_trylock(&ldata->atomic_read_lock))
1833 return -EAGAIN;
1834 } else {
1835 if (mutex_lock_interruptible(&ldata->atomic_read_lock))
1836 return -ERESTARTSYS;
1837 }
1838 packet = tty->packet; 2155 packet = tty->packet;
1839 2156
1840 add_wait_queue(&tty->read_wait, &wait); 2157 add_wait_queue(&tty->read_wait, &wait);
@@ -1883,7 +2200,11 @@ do_it_again:
1883 break; 2200 break;
1884 } 2201 }
1885 n_tty_set_room(tty); 2202 n_tty_set_room(tty);
2203 up_read(&tty->termios_rwsem);
2204
1886 timeout = schedule_timeout(timeout); 2205 timeout = schedule_timeout(timeout);
2206
2207 down_read(&tty->termios_rwsem);
1887 continue; 2208 continue;
1888 } 2209 }
1889 __set_current_state(TASK_RUNNING); 2210 __set_current_state(TASK_RUNNING);
@@ -1899,45 +2220,11 @@ do_it_again:
1899 } 2220 }
1900 2221
1901 if (ldata->icanon && !L_EXTPROC(tty)) { 2222 if (ldata->icanon && !L_EXTPROC(tty)) {
1902 /* N.B. avoid overrun if nr == 0 */ 2223 retval = canon_copy_from_read_buf(tty, &b, &nr);
1903 raw_spin_lock_irqsave(&ldata->read_lock, flags); 2224 if (retval == -EAGAIN) {
1904 while (nr && ldata->read_cnt) { 2225 retval = 0;
1905 int eol; 2226 continue;
1906 2227 } else if (retval)
1907 eol = test_and_clear_bit(ldata->read_tail,
1908 ldata->read_flags);
1909 c = ldata->read_buf[ldata->read_tail];
1910 ldata->read_tail = ((ldata->read_tail+1) &
1911 (N_TTY_BUF_SIZE-1));
1912 ldata->read_cnt--;
1913 if (eol) {
1914 /* this test should be redundant:
1915 * we shouldn't be reading data if
1916 * canon_data is 0
1917 */
1918 if (--ldata->canon_data < 0)
1919 ldata->canon_data = 0;
1920 }
1921 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
1922
1923 if (!eol || (c != __DISABLED_CHAR)) {
1924 if (tty_put_user(tty, c, b++)) {
1925 retval = -EFAULT;
1926 b--;
1927 raw_spin_lock_irqsave(&ldata->read_lock, flags);
1928 break;
1929 }
1930 nr--;
1931 }
1932 if (eol) {
1933 tty_audit_push(tty);
1934 raw_spin_lock_irqsave(&ldata->read_lock, flags);
1935 break;
1936 }
1937 raw_spin_lock_irqsave(&ldata->read_lock, flags);
1938 }
1939 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
1940 if (retval)
1941 break; 2228 break;
1942 } else { 2229 } else {
1943 int uncopied; 2230 int uncopied;
@@ -1951,24 +2238,7 @@ do_it_again:
1951 } 2238 }
1952 } 2239 }
1953 2240
1954 /* If there is enough space in the read buffer now, let the 2241 n_tty_check_unthrottle(tty);
1955 * low-level driver know. We use n_tty_chars_in_buffer() to
1956 * check the buffer, as it now knows about canonical mode.
1957 * Otherwise, if the driver is throttled and the line is
1958 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
1959 * we won't get any more characters.
1960 */
1961 while (1) {
1962 tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE);
1963 if (n_tty_chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE)
1964 break;
1965 if (!tty->count)
1966 break;
1967 n_tty_set_room(tty);
1968 if (!tty_unthrottle_safe(tty))
1969 break;
1970 }
1971 __tty_set_flow_change(tty, 0);
1972 2242
1973 if (b - buf >= minimum) 2243 if (b - buf >= minimum)
1974 break; 2244 break;
@@ -1982,15 +2252,11 @@ do_it_again:
1982 ldata->minimum_to_wake = minimum; 2252 ldata->minimum_to_wake = minimum;
1983 2253
1984 __set_current_state(TASK_RUNNING); 2254 __set_current_state(TASK_RUNNING);
1985 size = b - buf; 2255 if (b - buf)
1986 if (size) { 2256 retval = b - buf;
1987 retval = size;
1988 if (nr)
1989 clear_bit(TTY_PUSH, &tty->flags);
1990 } else if (test_and_clear_bit(TTY_PUSH, &tty->flags))
1991 goto do_it_again;
1992 2257
1993 n_tty_set_room(tty); 2258 n_tty_set_room(tty);
2259 up_read(&tty->termios_rwsem);
1994 return retval; 2260 return retval;
1995} 2261}
1996 2262
@@ -2031,6 +2297,8 @@ static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
2031 return retval; 2297 return retval;
2032 } 2298 }
2033 2299
2300 down_read(&tty->termios_rwsem);
2301
2034 /* Write out any echoed characters that are still pending */ 2302 /* Write out any echoed characters that are still pending */
2035 process_echoes(tty); 2303 process_echoes(tty);
2036 2304
@@ -2084,13 +2352,18 @@ static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
2084 retval = -EAGAIN; 2352 retval = -EAGAIN;
2085 break; 2353 break;
2086 } 2354 }
2355 up_read(&tty->termios_rwsem);
2356
2087 schedule(); 2357 schedule();
2358
2359 down_read(&tty->termios_rwsem);
2088 } 2360 }
2089break_out: 2361break_out:
2090 __set_current_state(TASK_RUNNING); 2362 __set_current_state(TASK_RUNNING);
2091 remove_wait_queue(&tty->write_wait, &wait); 2363 remove_wait_queue(&tty->write_wait, &wait);
2092 if (b - buf != nr && tty->fasync) 2364 if (b - buf != nr && tty->fasync)
2093 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); 2365 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
2366 up_read(&tty->termios_rwsem);
2094 return (b - buf) ? b - buf : retval; 2367 return (b - buf) ? b - buf : retval;
2095} 2368}
2096 2369
@@ -2139,19 +2412,19 @@ static unsigned int n_tty_poll(struct tty_struct *tty, struct file *file,
2139 2412
2140static unsigned long inq_canon(struct n_tty_data *ldata) 2413static unsigned long inq_canon(struct n_tty_data *ldata)
2141{ 2414{
2142 int nr, head, tail; 2415 size_t nr, head, tail;
2143 2416
2144 if (!ldata->canon_data) 2417 if (ldata->canon_head == ldata->read_tail)
2145 return 0; 2418 return 0;
2146 head = ldata->canon_head; 2419 head = ldata->canon_head;
2147 tail = ldata->read_tail; 2420 tail = ldata->read_tail;
2148 nr = (head - tail) & (N_TTY_BUF_SIZE-1); 2421 nr = head - tail;
2149 /* Skip EOF-chars.. */ 2422 /* Skip EOF-chars.. */
2150 while (head != tail) { 2423 while (head != tail) {
2151 if (test_bit(tail, ldata->read_flags) && 2424 if (test_bit(tail & (N_TTY_BUF_SIZE - 1), ldata->read_flags) &&
2152 ldata->read_buf[tail] == __DISABLED_CHAR) 2425 read_buf(ldata, tail) == __DISABLED_CHAR)
2153 nr--; 2426 nr--;
2154 tail = (tail+1) & (N_TTY_BUF_SIZE-1); 2427 tail++;
2155 } 2428 }
2156 return nr; 2429 return nr;
2157} 2430}
@@ -2166,10 +2439,12 @@ static int n_tty_ioctl(struct tty_struct *tty, struct file *file,
2166 case TIOCOUTQ: 2439 case TIOCOUTQ:
2167 return put_user(tty_chars_in_buffer(tty), (int __user *) arg); 2440 return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
2168 case TIOCINQ: 2441 case TIOCINQ:
2169 /* FIXME: Locking */ 2442 down_write(&tty->termios_rwsem);
2170 retval = ldata->read_cnt;
2171 if (L_ICANON(tty)) 2443 if (L_ICANON(tty))
2172 retval = inq_canon(ldata); 2444 retval = inq_canon(ldata);
2445 else
2446 retval = read_cnt(ldata);
2447 up_write(&tty->termios_rwsem);
2173 return put_user(retval, (unsigned int __user *) arg); 2448 return put_user(retval, (unsigned int __user *) arg);
2174 default: 2449 default:
2175 return n_tty_ioctl_helper(tty, file, cmd, arg); 2450 return n_tty_ioctl_helper(tty, file, cmd, arg);
@@ -2203,6 +2478,7 @@ struct tty_ldisc_ops tty_ldisc_N_TTY = {
2203 .receive_buf = n_tty_receive_buf, 2478 .receive_buf = n_tty_receive_buf,
2204 .write_wakeup = n_tty_write_wakeup, 2479 .write_wakeup = n_tty_write_wakeup,
2205 .fasync = n_tty_fasync, 2480 .fasync = n_tty_fasync,
2481 .receive_buf2 = n_tty_receive_buf2,
2206}; 2482};
2207 2483
2208/** 2484/**
diff --git a/drivers/tty/pty.c b/drivers/tty/pty.c
index abfd99089781..25c9bc783722 100644
--- a/drivers/tty/pty.c
+++ b/drivers/tty/pty.c
@@ -89,17 +89,13 @@ static void pty_unthrottle(struct tty_struct *tty)
89 * pty_space - report space left for writing 89 * pty_space - report space left for writing
90 * @to: tty we are writing into 90 * @to: tty we are writing into
91 * 91 *
92 * The tty buffers allow 64K but we sneak a peak and clip at 8K this 92 * Limit the buffer space used by ptys to 8k.
93 * allows a lot of overspill room for echo and other fun messes to
94 * be handled properly
95 */ 93 */
96 94
97static int pty_space(struct tty_struct *to) 95static int pty_space(struct tty_struct *to)
98{ 96{
99 int n = 8192 - to->port->buf.memory_used; 97 int n = tty_buffer_space_avail(to->port);
100 if (n < 0) 98 return min(n, 8192);
101 return 0;
102 return n;
103} 99}
104 100
105/** 101/**
@@ -125,10 +121,8 @@ static int pty_write(struct tty_struct *tty, const unsigned char *buf, int c)
125 /* Stuff the data into the input queue of the other end */ 121 /* Stuff the data into the input queue of the other end */
126 c = tty_insert_flip_string(to->port, buf, c); 122 c = tty_insert_flip_string(to->port, buf, c);
127 /* And shovel */ 123 /* And shovel */
128 if (c) { 124 if (c)
129 tty_flip_buffer_push(to->port); 125 tty_flip_buffer_push(to->port);
130 tty_wakeup(tty);
131 }
132 } 126 }
133 return c; 127 return c;
134} 128}
@@ -287,7 +281,7 @@ static int pty_resize(struct tty_struct *tty, struct winsize *ws)
287 struct tty_struct *pty = tty->link; 281 struct tty_struct *pty = tty->link;
288 282
289 /* For a PTY we need to lock the tty side */ 283 /* For a PTY we need to lock the tty side */
290 mutex_lock(&tty->termios_mutex); 284 mutex_lock(&tty->winsize_mutex);
291 if (!memcmp(ws, &tty->winsize, sizeof(*ws))) 285 if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
292 goto done; 286 goto done;
293 287
@@ -314,7 +308,7 @@ static int pty_resize(struct tty_struct *tty, struct winsize *ws)
314 tty->winsize = *ws; 308 tty->winsize = *ws;
315 pty->winsize = *ws; /* Never used so will go away soon */ 309 pty->winsize = *ws; /* Never used so will go away soon */
316done: 310done:
317 mutex_unlock(&tty->termios_mutex); 311 mutex_unlock(&tty->winsize_mutex);
318 return 0; 312 return 0;
319} 313}
320 314
diff --git a/drivers/tty/serial/8250/8250_core.c b/drivers/tty/serial/8250/8250_core.c
index 86c00b1c5583..570df9d2a5d2 100644
--- a/drivers/tty/serial/8250/8250_core.c
+++ b/drivers/tty/serial/8250/8250_core.c
@@ -3062,7 +3062,7 @@ void serial8250_resume_port(int line)
3062 */ 3062 */
3063static int serial8250_probe(struct platform_device *dev) 3063static int serial8250_probe(struct platform_device *dev)
3064{ 3064{
3065 struct plat_serial8250_port *p = dev->dev.platform_data; 3065 struct plat_serial8250_port *p = dev_get_platdata(&dev->dev);
3066 struct uart_8250_port uart; 3066 struct uart_8250_port uart;
3067 int ret, i, irqflag = 0; 3067 int ret, i, irqflag = 0;
3068 3068
diff --git a/drivers/tty/serial/8250/8250_dw.c b/drivers/tty/serial/8250/8250_dw.c
index 76a8daadff47..daf710f5c3fc 100644
--- a/drivers/tty/serial/8250/8250_dw.c
+++ b/drivers/tty/serial/8250/8250_dw.c
@@ -57,11 +57,25 @@
57 57
58struct dw8250_data { 58struct dw8250_data {
59 int last_lcr; 59 int last_lcr;
60 int last_mcr;
60 int line; 61 int line;
61 struct clk *clk; 62 struct clk *clk;
62 u8 usr_reg; 63 u8 usr_reg;
63}; 64};
64 65
66static inline int dw8250_modify_msr(struct uart_port *p, int offset, int value)
67{
68 struct dw8250_data *d = p->private_data;
69
70 /* If reading MSR, report CTS asserted when auto-CTS/RTS enabled */
71 if (offset == UART_MSR && d->last_mcr & UART_MCR_AFE) {
72 value |= UART_MSR_CTS;
73 value &= ~UART_MSR_DCTS;
74 }
75
76 return value;
77}
78
65static void dw8250_serial_out(struct uart_port *p, int offset, int value) 79static void dw8250_serial_out(struct uart_port *p, int offset, int value)
66{ 80{
67 struct dw8250_data *d = p->private_data; 81 struct dw8250_data *d = p->private_data;
@@ -69,15 +83,17 @@ static void dw8250_serial_out(struct uart_port *p, int offset, int value)
69 if (offset == UART_LCR) 83 if (offset == UART_LCR)
70 d->last_lcr = value; 84 d->last_lcr = value;
71 85
72 offset <<= p->regshift; 86 if (offset == UART_MCR)
73 writeb(value, p->membase + offset); 87 d->last_mcr = value;
88
89 writeb(value, p->membase + (offset << p->regshift));
74} 90}
75 91
76static unsigned int dw8250_serial_in(struct uart_port *p, int offset) 92static unsigned int dw8250_serial_in(struct uart_port *p, int offset)
77{ 93{
78 offset <<= p->regshift; 94 unsigned int value = readb(p->membase + (offset << p->regshift));
79 95
80 return readb(p->membase + offset); 96 return dw8250_modify_msr(p, offset, value);
81} 97}
82 98
83/* Read Back (rb) version to ensure register access ording. */ 99/* Read Back (rb) version to ensure register access ording. */
@@ -94,15 +110,17 @@ static void dw8250_serial_out32(struct uart_port *p, int offset, int value)
94 if (offset == UART_LCR) 110 if (offset == UART_LCR)
95 d->last_lcr = value; 111 d->last_lcr = value;
96 112
97 offset <<= p->regshift; 113 if (offset == UART_MCR)
98 writel(value, p->membase + offset); 114 d->last_mcr = value;
115
116 writel(value, p->membase + (offset << p->regshift));
99} 117}
100 118
101static unsigned int dw8250_serial_in32(struct uart_port *p, int offset) 119static unsigned int dw8250_serial_in32(struct uart_port *p, int offset)
102{ 120{
103 offset <<= p->regshift; 121 unsigned int value = readl(p->membase + (offset << p->regshift));
104 122
105 return readl(p->membase + offset); 123 return dw8250_modify_msr(p, offset, value);
106} 124}
107 125
108static int dw8250_handle_irq(struct uart_port *p) 126static int dw8250_handle_irq(struct uart_port *p)
diff --git a/drivers/tty/serial/8250/8250_early.c b/drivers/tty/serial/8250/8250_early.c
index 946ddd2b3a54..c100d6343d50 100644
--- a/drivers/tty/serial/8250/8250_early.c
+++ b/drivers/tty/serial/8250/8250_early.c
@@ -194,7 +194,7 @@ static int __init parse_options(struct early_serial8250_device *device,
194 options++; 194 options++;
195 device->baud = simple_strtoul(options, NULL, 0); 195 device->baud = simple_strtoul(options, NULL, 0);
196 length = min(strcspn(options, " ") + 1, 196 length = min(strcspn(options, " ") + 1,
197 sizeof(device->options)); 197 (size_t)(sizeof(device->options)));
198 strlcpy(device->options, options, length); 198 strlcpy(device->options, options, length);
199 } else { 199 } else {
200 device->baud = probe_baud(port); 200 device->baud = probe_baud(port);
diff --git a/drivers/tty/serial/8250/8250_em.c b/drivers/tty/serial/8250/8250_em.c
index 916cc19fbbda..5f3bba12c159 100644
--- a/drivers/tty/serial/8250/8250_em.c
+++ b/drivers/tty/serial/8250/8250_em.c
@@ -95,25 +95,23 @@ static int serial8250_em_probe(struct platform_device *pdev)
95 struct resource *irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0); 95 struct resource *irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
96 struct serial8250_em_priv *priv; 96 struct serial8250_em_priv *priv;
97 struct uart_8250_port up; 97 struct uart_8250_port up;
98 int ret = -EINVAL; 98 int ret;
99 99
100 if (!regs || !irq) { 100 if (!regs || !irq) {
101 dev_err(&pdev->dev, "missing registers or irq\n"); 101 dev_err(&pdev->dev, "missing registers or irq\n");
102 goto err0; 102 return -EINVAL;
103 } 103 }
104 104
105 priv = kzalloc(sizeof(*priv), GFP_KERNEL); 105 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
106 if (!priv) { 106 if (!priv) {
107 dev_err(&pdev->dev, "unable to allocate private data\n"); 107 dev_err(&pdev->dev, "unable to allocate private data\n");
108 ret = -ENOMEM; 108 return -ENOMEM;
109 goto err0;
110 } 109 }
111 110
112 priv->sclk = clk_get(&pdev->dev, "sclk"); 111 priv->sclk = devm_clk_get(&pdev->dev, "sclk");
113 if (IS_ERR(priv->sclk)) { 112 if (IS_ERR(priv->sclk)) {
114 dev_err(&pdev->dev, "unable to get clock\n"); 113 dev_err(&pdev->dev, "unable to get clock\n");
115 ret = PTR_ERR(priv->sclk); 114 return PTR_ERR(priv->sclk);
116 goto err1;
117 } 115 }
118 116
119 memset(&up, 0, sizeof(up)); 117 memset(&up, 0, sizeof(up));
@@ -136,20 +134,13 @@ static int serial8250_em_probe(struct platform_device *pdev)
136 ret = serial8250_register_8250_port(&up); 134 ret = serial8250_register_8250_port(&up);
137 if (ret < 0) { 135 if (ret < 0) {
138 dev_err(&pdev->dev, "unable to register 8250 port\n"); 136 dev_err(&pdev->dev, "unable to register 8250 port\n");
139 goto err2; 137 clk_disable(priv->sclk);
138 return ret;
140 } 139 }
141 140
142 priv->line = ret; 141 priv->line = ret;
143 platform_set_drvdata(pdev, priv); 142 platform_set_drvdata(pdev, priv);
144 return 0; 143 return 0;
145
146 err2:
147 clk_disable(priv->sclk);
148 clk_put(priv->sclk);
149 err1:
150 kfree(priv);
151 err0:
152 return ret;
153} 144}
154 145
155static int serial8250_em_remove(struct platform_device *pdev) 146static int serial8250_em_remove(struct platform_device *pdev)
@@ -158,8 +149,6 @@ static int serial8250_em_remove(struct platform_device *pdev)
158 149
159 serial8250_unregister_port(priv->line); 150 serial8250_unregister_port(priv->line);
160 clk_disable(priv->sclk); 151 clk_disable(priv->sclk);
161 clk_put(priv->sclk);
162 kfree(priv);
163 return 0; 152 return 0;
164} 153}
165 154
diff --git a/drivers/tty/serial/8250/8250_pci.c b/drivers/tty/serial/8250/8250_pci.c
index c52948b368d8..c810da7c7a88 100644
--- a/drivers/tty/serial/8250/8250_pci.c
+++ b/drivers/tty/serial/8250/8250_pci.c
@@ -1565,6 +1565,7 @@ pci_wch_ch353_setup(struct serial_private *priv,
1565#define PCI_DEVICE_ID_COMMTECH_4228PCIE 0x0021 1565#define PCI_DEVICE_ID_COMMTECH_4228PCIE 0x0021
1566#define PCI_DEVICE_ID_COMMTECH_4222PCIE 0x0022 1566#define PCI_DEVICE_ID_COMMTECH_4222PCIE 0x0022
1567#define PCI_DEVICE_ID_BROADCOM_TRUMANAGE 0x160a 1567#define PCI_DEVICE_ID_BROADCOM_TRUMANAGE 0x160a
1568#define PCI_DEVICE_ID_AMCC_ADDIDATA_APCI7800 0x818e
1568 1569
1569#define PCI_VENDOR_ID_SUNIX 0x1fd4 1570#define PCI_VENDOR_ID_SUNIX 0x1fd4
1570#define PCI_DEVICE_ID_SUNIX_1999 0x1999 1571#define PCI_DEVICE_ID_SUNIX_1999 0x1999
@@ -1587,8 +1588,8 @@ static struct pci_serial_quirk pci_serial_quirks[] __refdata = {
1587 * ADDI-DATA GmbH communication cards <info@addi-data.com> 1588 * ADDI-DATA GmbH communication cards <info@addi-data.com>
1588 */ 1589 */
1589 { 1590 {
1590 .vendor = PCI_VENDOR_ID_ADDIDATA_OLD, 1591 .vendor = PCI_VENDOR_ID_AMCC,
1591 .device = PCI_DEVICE_ID_ADDIDATA_APCI7800, 1592 .device = PCI_DEVICE_ID_AMCC_ADDIDATA_APCI7800,
1592 .subvendor = PCI_ANY_ID, 1593 .subvendor = PCI_ANY_ID,
1593 .subdevice = PCI_ANY_ID, 1594 .subdevice = PCI_ANY_ID,
1594 .setup = addidata_apci7800_setup, 1595 .setup = addidata_apci7800_setup,
@@ -4697,8 +4698,8 @@ static struct pci_device_id serial_pci_tbl[] = {
4697 0, 4698 0,
4698 pbn_b0_1_115200 }, 4699 pbn_b0_1_115200 },
4699 4700
4700 { PCI_VENDOR_ID_ADDIDATA_OLD, 4701 { PCI_VENDOR_ID_AMCC,
4701 PCI_DEVICE_ID_ADDIDATA_APCI7800, 4702 PCI_DEVICE_ID_AMCC_ADDIDATA_APCI7800,
4702 PCI_ANY_ID, 4703 PCI_ANY_ID,
4703 PCI_ANY_ID, 4704 PCI_ANY_ID,
4704 0, 4705 0,
@@ -4797,6 +4798,12 @@ static struct pci_device_id serial_pci_tbl[] = {
4797 PCI_VENDOR_ID_IBM, 0x0299, 4798 PCI_VENDOR_ID_IBM, 0x0299,
4798 0, 0, pbn_b0_bt_2_115200 }, 4799 0, 0, pbn_b0_bt_2_115200 },
4799 4800
4801 /*
4802 * other NetMos 9835 devices are most likely handled by the
4803 * parport_serial driver, check drivers/parport/parport_serial.c
4804 * before adding them here.
4805 */
4806
4800 { PCI_VENDOR_ID_NETMOS, PCI_DEVICE_ID_NETMOS_9901, 4807 { PCI_VENDOR_ID_NETMOS, PCI_DEVICE_ID_NETMOS_9901,
4801 0xA000, 0x1000, 4808 0xA000, 0x1000,
4802 0, 0, pbn_b0_1_115200 }, 4809 0, 0, pbn_b0_1_115200 },
diff --git a/drivers/tty/serial/8250/Kconfig b/drivers/tty/serial/8250/Kconfig
index a1ba94d64885..f3b306efaa59 100644
--- a/drivers/tty/serial/8250/Kconfig
+++ b/drivers/tty/serial/8250/Kconfig
@@ -116,6 +116,8 @@ config SERIAL_8250_PCI
116 This builds standard PCI serial support. You may be able to 116 This builds standard PCI serial support. You may be able to
117 disable this feature if you only need legacy serial support. 117 disable this feature if you only need legacy serial support.
118 Saves about 9K. 118 Saves about 9K.
119 Note that serial ports on NetMos 9835 Multi-I/O cards are handled
120 by the parport_serial driver, enabled with CONFIG_PARPORT_SERIAL.
119 121
120config SERIAL_8250_HP300 122config SERIAL_8250_HP300
121 tristate 123 tristate
diff --git a/drivers/tty/serial/Kconfig b/drivers/tty/serial/Kconfig
index 1456673bcca0..cc4c8682b47b 100644
--- a/drivers/tty/serial/Kconfig
+++ b/drivers/tty/serial/Kconfig
@@ -291,13 +291,13 @@ config SERIAL_MAX3100
291 291
292config SERIAL_MAX310X 292config SERIAL_MAX310X
293 bool "MAX310X support" 293 bool "MAX310X support"
294 depends on SPI 294 depends on SPI_MASTER
295 select SERIAL_CORE 295 select SERIAL_CORE
296 select REGMAP_SPI if SPI 296 select REGMAP_SPI if SPI_MASTER
297 default n 297 default n
298 help 298 help
299 This selects support for an advanced UART from Maxim (Dallas). 299 This selects support for an advanced UART from Maxim (Dallas).
300 Supported ICs are MAX3107, MAX3108. 300 Supported ICs are MAX3107, MAX3108, MAX3109, MAX14830.
301 Each IC contains 128 words each of receive and transmit FIFO 301 Each IC contains 128 words each of receive and transmit FIFO
302 that can be controlled through I2C or high-speed SPI. 302 that can be controlled through I2C or high-speed SPI.
303 303
@@ -1401,13 +1401,16 @@ config SERIAL_XILINX_PS_UART_CONSOLE
1401 Enable a Xilinx PS UART port to be the system console. 1401 Enable a Xilinx PS UART port to be the system console.
1402 1402
1403config SERIAL_AR933X 1403config SERIAL_AR933X
1404 bool "AR933X serial port support" 1404 tristate "AR933X serial port support"
1405 depends on SOC_AR933X 1405 depends on HAVE_CLK && SOC_AR933X
1406 select SERIAL_CORE 1406 select SERIAL_CORE
1407 help 1407 help
1408 If you have an Atheros AR933X SOC based board and want to use the 1408 If you have an Atheros AR933X SOC based board and want to use the
1409 built-in UART of the SoC, say Y to this option. 1409 built-in UART of the SoC, say Y to this option.
1410 1410
1411 To compile this driver as a module, choose M here: the
1412 module will be called ar933x_uart.
1413
1411config SERIAL_AR933X_CONSOLE 1414config SERIAL_AR933X_CONSOLE
1412 bool "Console on AR933X serial port" 1415 bool "Console on AR933X serial port"
1413 depends on SERIAL_AR933X=y 1416 depends on SERIAL_AR933X=y
@@ -1424,8 +1427,8 @@ config SERIAL_AR933X_NR_UARTS
1424 to support. 1427 to support.
1425 1428
1426config SERIAL_EFM32_UART 1429config SERIAL_EFM32_UART
1427 tristate "EFM32 UART/USART port." 1430 tristate "EFM32 UART/USART port"
1428 depends on ARCH_EFM32 1431 depends on ARM && (ARCH_EFM32 || COMPILE_TEST)
1429 select SERIAL_CORE 1432 select SERIAL_CORE
1430 help 1433 help
1431 This driver support the USART and UART ports on 1434 This driver support the USART and UART ports on
@@ -1497,6 +1500,22 @@ config SERIAL_FSL_LPUART_CONSOLE
1497 If you have enabled the lpuart serial port on the Freescale SoCs, 1500 If you have enabled the lpuart serial port on the Freescale SoCs,
1498 you can make it the console by answering Y to this option. 1501 you can make it the console by answering Y to this option.
1499 1502
1503config SERIAL_ST_ASC
1504 tristate "ST ASC serial port support"
1505 select SERIAL_CORE
1506 help
1507 This driver is for the on-chip Asychronous Serial Controller on
1508 STMicroelectronics STi SoCs.
1509 ASC is embedded in ST COMMS IP block. It supports Rx & Tx functionality.
1510 It support all industry standard baud rates.
1511
1512 If unsure, say N.
1513
1514config SERIAL_ST_ASC_CONSOLE
1515 bool "Support for console on ST ASC"
1516 depends on SERIAL_ST_ASC=y
1517 select SERIAL_CORE_CONSOLE
1518
1500endmenu 1519endmenu
1501 1520
1502endif # TTY 1521endif # TTY
diff --git a/drivers/tty/serial/Makefile b/drivers/tty/serial/Makefile
index cf650f0cd6e4..47b679c547e9 100644
--- a/drivers/tty/serial/Makefile
+++ b/drivers/tty/serial/Makefile
@@ -65,6 +65,7 @@ obj-$(CONFIG_SERIAL_KGDB_NMI) += kgdb_nmi.o
65obj-$(CONFIG_SERIAL_KS8695) += serial_ks8695.o 65obj-$(CONFIG_SERIAL_KS8695) += serial_ks8695.o
66obj-$(CONFIG_SERIAL_OMAP) += omap-serial.o 66obj-$(CONFIG_SERIAL_OMAP) += omap-serial.o
67obj-$(CONFIG_SERIAL_ALTERA_UART) += altera_uart.o 67obj-$(CONFIG_SERIAL_ALTERA_UART) += altera_uart.o
68obj-$(CONFIG_SERIAL_ST_ASC) += st-asc.o
68obj-$(CONFIG_KGDB_SERIAL_CONSOLE) += kgdboc.o 69obj-$(CONFIG_KGDB_SERIAL_CONSOLE) += kgdboc.o
69obj-$(CONFIG_SERIAL_QE) += ucc_uart.o 70obj-$(CONFIG_SERIAL_QE) += ucc_uart.o
70obj-$(CONFIG_SERIAL_TIMBERDALE) += timbuart.o 71obj-$(CONFIG_SERIAL_TIMBERDALE) += timbuart.o
diff --git a/drivers/tty/serial/altera_jtaguart.c b/drivers/tty/serial/altera_jtaguart.c
index c6bdb943726b..18e038fbdcdc 100644
--- a/drivers/tty/serial/altera_jtaguart.c
+++ b/drivers/tty/serial/altera_jtaguart.c
@@ -139,7 +139,9 @@ static void altera_jtaguart_rx_chars(struct altera_jtaguart *pp)
139 uart_insert_char(port, 0, 0, ch, flag); 139 uart_insert_char(port, 0, 0, ch, flag);
140 } 140 }
141 141
142 spin_unlock(&port->lock);
142 tty_flip_buffer_push(&port->state->port); 143 tty_flip_buffer_push(&port->state->port);
144 spin_lock(&port->lock);
143} 145}
144 146
145static void altera_jtaguart_tx_chars(struct altera_jtaguart *pp) 147static void altera_jtaguart_tx_chars(struct altera_jtaguart *pp)
@@ -408,7 +410,8 @@ static struct uart_driver altera_jtaguart_driver = {
408 410
409static int altera_jtaguart_probe(struct platform_device *pdev) 411static int altera_jtaguart_probe(struct platform_device *pdev)
410{ 412{
411 struct altera_jtaguart_platform_uart *platp = pdev->dev.platform_data; 413 struct altera_jtaguart_platform_uart *platp =
414 dev_get_platdata(&pdev->dev);
412 struct uart_port *port; 415 struct uart_port *port;
413 struct resource *res_irq, *res_mem; 416 struct resource *res_irq, *res_mem;
414 int i = pdev->id; 417 int i = pdev->id;
diff --git a/drivers/tty/serial/altera_uart.c b/drivers/tty/serial/altera_uart.c
index 1d46966e2a65..6431472aeb1f 100644
--- a/drivers/tty/serial/altera_uart.c
+++ b/drivers/tty/serial/altera_uart.c
@@ -231,7 +231,9 @@ static void altera_uart_rx_chars(struct altera_uart *pp)
231 flag); 231 flag);
232 } 232 }
233 233
234 spin_unlock(&port->lock);
234 tty_flip_buffer_push(&port->state->port); 235 tty_flip_buffer_push(&port->state->port);
236 spin_lock(&port->lock);
235} 237}
236 238
237static void altera_uart_tx_chars(struct altera_uart *pp) 239static void altera_uart_tx_chars(struct altera_uart *pp)
@@ -534,7 +536,7 @@ static int altera_uart_get_of_uartclk(struct platform_device *pdev,
534 536
535static int altera_uart_probe(struct platform_device *pdev) 537static int altera_uart_probe(struct platform_device *pdev)
536{ 538{
537 struct altera_uart_platform_uart *platp = pdev->dev.platform_data; 539 struct altera_uart_platform_uart *platp = dev_get_platdata(&pdev->dev);
538 struct uart_port *port; 540 struct uart_port *port;
539 struct resource *res_mem; 541 struct resource *res_mem;
540 struct resource *res_irq; 542 struct resource *res_irq;
diff --git a/drivers/tty/serial/amba-pl010.c b/drivers/tty/serial/amba-pl010.c
index c36840519527..8b90f0b6dfdf 100644
--- a/drivers/tty/serial/amba-pl010.c
+++ b/drivers/tty/serial/amba-pl010.c
@@ -721,7 +721,7 @@ static int pl010_probe(struct amba_device *dev, const struct amba_id *id)
721 uap->port.flags = UPF_BOOT_AUTOCONF; 721 uap->port.flags = UPF_BOOT_AUTOCONF;
722 uap->port.line = i; 722 uap->port.line = i;
723 uap->dev = dev; 723 uap->dev = dev;
724 uap->data = dev->dev.platform_data; 724 uap->data = dev_get_platdata(&dev->dev);
725 725
726 amba_ports[i] = uap; 726 amba_ports[i] = uap;
727 727
diff --git a/drivers/tty/serial/amba-pl011.c b/drivers/tty/serial/amba-pl011.c
index 28b35ad9c6cd..aaa22867e656 100644
--- a/drivers/tty/serial/amba-pl011.c
+++ b/drivers/tty/serial/amba-pl011.c
@@ -265,7 +265,7 @@ static void pl011_sgbuf_free(struct dma_chan *chan, struct pl011_sgbuf *sg,
265static void pl011_dma_probe_initcall(struct device *dev, struct uart_amba_port *uap) 265static void pl011_dma_probe_initcall(struct device *dev, struct uart_amba_port *uap)
266{ 266{
267 /* DMA is the sole user of the platform data right now */ 267 /* DMA is the sole user of the platform data right now */
268 struct amba_pl011_data *plat = uap->port.dev->platform_data; 268 struct amba_pl011_data *plat = dev_get_platdata(uap->port.dev);
269 struct dma_slave_config tx_conf = { 269 struct dma_slave_config tx_conf = {
270 .dst_addr = uap->port.mapbase + UART01x_DR, 270 .dst_addr = uap->port.mapbase + UART01x_DR,
271 .dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE, 271 .dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
@@ -677,6 +677,8 @@ static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
677 * Locking: called with port lock held and IRQs disabled. 677 * Locking: called with port lock held and IRQs disabled.
678 */ 678 */
679static void pl011_dma_flush_buffer(struct uart_port *port) 679static void pl011_dma_flush_buffer(struct uart_port *port)
680__releases(&uap->port.lock)
681__acquires(&uap->port.lock)
680{ 682{
681 struct uart_amba_port *uap = (struct uart_amba_port *)port; 683 struct uart_amba_port *uap = (struct uart_amba_port *)port;
682 684
@@ -1198,6 +1200,8 @@ static void pl011_enable_ms(struct uart_port *port)
1198} 1200}
1199 1201
1200static void pl011_rx_chars(struct uart_amba_port *uap) 1202static void pl011_rx_chars(struct uart_amba_port *uap)
1203__releases(&uap->port.lock)
1204__acquires(&uap->port.lock)
1201{ 1205{
1202 pl011_fifo_to_tty(uap); 1206 pl011_fifo_to_tty(uap);
1203 1207
@@ -1497,10 +1501,10 @@ static int pl011_hwinit(struct uart_port *port)
1497 uap->im = readw(uap->port.membase + UART011_IMSC); 1501 uap->im = readw(uap->port.membase + UART011_IMSC);
1498 writew(UART011_RTIM | UART011_RXIM, uap->port.membase + UART011_IMSC); 1502 writew(UART011_RTIM | UART011_RXIM, uap->port.membase + UART011_IMSC);
1499 1503
1500 if (uap->port.dev->platform_data) { 1504 if (dev_get_platdata(uap->port.dev)) {
1501 struct amba_pl011_data *plat; 1505 struct amba_pl011_data *plat;
1502 1506
1503 plat = uap->port.dev->platform_data; 1507 plat = dev_get_platdata(uap->port.dev);
1504 if (plat->init) 1508 if (plat->init)
1505 plat->init(); 1509 plat->init();
1506 } 1510 }
@@ -1645,10 +1649,10 @@ static void pl011_shutdown(struct uart_port *port)
1645 /* Optionally let pins go into sleep states */ 1649 /* Optionally let pins go into sleep states */
1646 pinctrl_pm_select_sleep_state(port->dev); 1650 pinctrl_pm_select_sleep_state(port->dev);
1647 1651
1648 if (uap->port.dev->platform_data) { 1652 if (dev_get_platdata(uap->port.dev)) {
1649 struct amba_pl011_data *plat; 1653 struct amba_pl011_data *plat;
1650 1654
1651 plat = uap->port.dev->platform_data; 1655 plat = dev_get_platdata(uap->port.dev);
1652 if (plat->exit) 1656 if (plat->exit)
1653 plat->exit(); 1657 plat->exit();
1654 } 1658 }
@@ -2002,10 +2006,10 @@ static int __init pl011_console_setup(struct console *co, char *options)
2002 if (ret) 2006 if (ret)
2003 return ret; 2007 return ret;
2004 2008
2005 if (uap->port.dev->platform_data) { 2009 if (dev_get_platdata(uap->port.dev)) {
2006 struct amba_pl011_data *plat; 2010 struct amba_pl011_data *plat;
2007 2011
2008 plat = uap->port.dev->platform_data; 2012 plat = dev_get_platdata(uap->port.dev);
2009 if (plat->init) 2013 if (plat->init)
2010 plat->init(); 2014 plat->init();
2011 } 2015 }
diff --git a/drivers/tty/serial/apbuart.c b/drivers/tty/serial/apbuart.c
index 6331464d9101..de11ab8ffd91 100644
--- a/drivers/tty/serial/apbuart.c
+++ b/drivers/tty/serial/apbuart.c
@@ -125,7 +125,9 @@ static void apbuart_rx_chars(struct uart_port *port)
125 status = UART_GET_STATUS(port); 125 status = UART_GET_STATUS(port);
126 } 126 }
127 127
128 spin_unlock(&port->lock);
128 tty_flip_buffer_push(&port->state->port); 129 tty_flip_buffer_push(&port->state->port);
130 spin_lock(&port->lock);
129} 131}
130 132
131static void apbuart_tx_chars(struct uart_port *port) 133static void apbuart_tx_chars(struct uart_port *port)
diff --git a/drivers/tty/serial/ar933x_uart.c b/drivers/tty/serial/ar933x_uart.c
index 27f20c57abed..acd03af7cd52 100644
--- a/drivers/tty/serial/ar933x_uart.c
+++ b/drivers/tty/serial/ar933x_uart.c
@@ -17,6 +17,8 @@
17#include <linux/sysrq.h> 17#include <linux/sysrq.h>
18#include <linux/delay.h> 18#include <linux/delay.h>
19#include <linux/platform_device.h> 19#include <linux/platform_device.h>
20#include <linux/of.h>
21#include <linux/of_platform.h>
20#include <linux/tty.h> 22#include <linux/tty.h>
21#include <linux/tty_flip.h> 23#include <linux/tty_flip.h>
22#include <linux/serial_core.h> 24#include <linux/serial_core.h>
@@ -24,11 +26,11 @@
24#include <linux/slab.h> 26#include <linux/slab.h>
25#include <linux/io.h> 27#include <linux/io.h>
26#include <linux/irq.h> 28#include <linux/irq.h>
29#include <linux/clk.h>
27 30
28#include <asm/div64.h> 31#include <asm/div64.h>
29 32
30#include <asm/mach-ath79/ar933x_uart.h> 33#include <asm/mach-ath79/ar933x_uart.h>
31#include <asm/mach-ath79/ar933x_uart_platform.h>
32 34
33#define DRIVER_NAME "ar933x-uart" 35#define DRIVER_NAME "ar933x-uart"
34 36
@@ -47,8 +49,14 @@ struct ar933x_uart_port {
47 unsigned int ier; /* shadow Interrupt Enable Register */ 49 unsigned int ier; /* shadow Interrupt Enable Register */
48 unsigned int min_baud; 50 unsigned int min_baud;
49 unsigned int max_baud; 51 unsigned int max_baud;
52 struct clk *clk;
50}; 53};
51 54
55static inline bool ar933x_uart_console_enabled(void)
56{
57 return config_enabled(CONFIG_SERIAL_AR933X_CONSOLE);
58}
59
52static inline unsigned int ar933x_uart_read(struct ar933x_uart_port *up, 60static inline unsigned int ar933x_uart_read(struct ar933x_uart_port *up,
53 int offset) 61 int offset)
54{ 62{
@@ -322,7 +330,9 @@ static void ar933x_uart_rx_chars(struct ar933x_uart_port *up)
322 tty_insert_flip_char(port, ch, TTY_NORMAL); 330 tty_insert_flip_char(port, ch, TTY_NORMAL);
323 } while (max_count-- > 0); 331 } while (max_count-- > 0);
324 332
333 spin_unlock(&up->port.lock);
325 tty_flip_buffer_push(port); 334 tty_flip_buffer_push(port);
335 spin_lock(&up->port.lock);
326} 336}
327 337
328static void ar933x_uart_tx_chars(struct ar933x_uart_port *up) 338static void ar933x_uart_tx_chars(struct ar933x_uart_port *up)
@@ -497,8 +507,6 @@ static struct uart_ops ar933x_uart_ops = {
497 .verify_port = ar933x_uart_verify_port, 507 .verify_port = ar933x_uart_verify_port,
498}; 508};
499 509
500#ifdef CONFIG_SERIAL_AR933X_CONSOLE
501
502static struct ar933x_uart_port * 510static struct ar933x_uart_port *
503ar933x_console_ports[CONFIG_SERIAL_AR933X_NR_UARTS]; 511ar933x_console_ports[CONFIG_SERIAL_AR933X_NR_UARTS];
504 512
@@ -597,80 +605,88 @@ static struct console ar933x_uart_console = {
597 605
598static void ar933x_uart_add_console_port(struct ar933x_uart_port *up) 606static void ar933x_uart_add_console_port(struct ar933x_uart_port *up)
599{ 607{
608 if (!ar933x_uart_console_enabled())
609 return;
610
600 ar933x_console_ports[up->port.line] = up; 611 ar933x_console_ports[up->port.line] = up;
601} 612}
602 613
603#define AR933X_SERIAL_CONSOLE (&ar933x_uart_console)
604
605#else
606
607static inline void ar933x_uart_add_console_port(struct ar933x_uart_port *up) {}
608
609#define AR933X_SERIAL_CONSOLE NULL
610
611#endif /* CONFIG_SERIAL_AR933X_CONSOLE */
612
613static struct uart_driver ar933x_uart_driver = { 614static struct uart_driver ar933x_uart_driver = {
614 .owner = THIS_MODULE, 615 .owner = THIS_MODULE,
615 .driver_name = DRIVER_NAME, 616 .driver_name = DRIVER_NAME,
616 .dev_name = "ttyATH", 617 .dev_name = "ttyATH",
617 .nr = CONFIG_SERIAL_AR933X_NR_UARTS, 618 .nr = CONFIG_SERIAL_AR933X_NR_UARTS,
618 .cons = AR933X_SERIAL_CONSOLE, 619 .cons = NULL, /* filled in runtime */
619}; 620};
620 621
621static int ar933x_uart_probe(struct platform_device *pdev) 622static int ar933x_uart_probe(struct platform_device *pdev)
622{ 623{
623 struct ar933x_uart_platform_data *pdata;
624 struct ar933x_uart_port *up; 624 struct ar933x_uart_port *up;
625 struct uart_port *port; 625 struct uart_port *port;
626 struct resource *mem_res; 626 struct resource *mem_res;
627 struct resource *irq_res; 627 struct resource *irq_res;
628 struct device_node *np;
628 unsigned int baud; 629 unsigned int baud;
629 int id; 630 int id;
630 int ret; 631 int ret;
631 632
632 pdata = pdev->dev.platform_data; 633 np = pdev->dev.of_node;
633 if (!pdata) 634 if (config_enabled(CONFIG_OF) && np) {
634 return -EINVAL; 635 id = of_alias_get_id(np, "serial");
635 636 if (id < 0) {
636 id = pdev->id; 637 dev_err(&pdev->dev, "unable to get alias id, err=%d\n",
637 if (id == -1) 638 id);
638 id = 0; 639 return id;
640 }
641 } else {
642 id = pdev->id;
643 if (id == -1)
644 id = 0;
645 }
639 646
640 if (id > CONFIG_SERIAL_AR933X_NR_UARTS) 647 if (id > CONFIG_SERIAL_AR933X_NR_UARTS)
641 return -EINVAL; 648 return -EINVAL;
642 649
643 mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
644 if (!mem_res) {
645 dev_err(&pdev->dev, "no MEM resource\n");
646 return -EINVAL;
647 }
648
649 irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0); 650 irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
650 if (!irq_res) { 651 if (!irq_res) {
651 dev_err(&pdev->dev, "no IRQ resource\n"); 652 dev_err(&pdev->dev, "no IRQ resource\n");
652 return -EINVAL; 653 return -EINVAL;
653 } 654 }
654 655
655 up = kzalloc(sizeof(struct ar933x_uart_port), GFP_KERNEL); 656 up = devm_kzalloc(&pdev->dev, sizeof(struct ar933x_uart_port),
657 GFP_KERNEL);
656 if (!up) 658 if (!up)
657 return -ENOMEM; 659 return -ENOMEM;
658 660
661 up->clk = devm_clk_get(&pdev->dev, "uart");
662 if (IS_ERR(up->clk)) {
663 dev_err(&pdev->dev, "unable to get UART clock\n");
664 return PTR_ERR(up->clk);
665 }
666
659 port = &up->port; 667 port = &up->port;
660 port->mapbase = mem_res->start;
661 668
662 port->membase = ioremap(mem_res->start, AR933X_UART_REGS_SIZE); 669 mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
663 if (!port->membase) { 670 port->membase = devm_ioremap_resource(&pdev->dev, mem_res);
664 ret = -ENOMEM; 671 if (IS_ERR(port->membase))
665 goto err_free_up; 672 return PTR_ERR(port->membase);
673
674 ret = clk_prepare_enable(up->clk);
675 if (ret)
676 return ret;
677
678 port->uartclk = clk_get_rate(up->clk);
679 if (!port->uartclk) {
680 ret = -EINVAL;
681 goto err_disable_clk;
666 } 682 }
667 683
684 port->mapbase = mem_res->start;
668 port->line = id; 685 port->line = id;
669 port->irq = irq_res->start; 686 port->irq = irq_res->start;
670 port->dev = &pdev->dev; 687 port->dev = &pdev->dev;
671 port->type = PORT_AR933X; 688 port->type = PORT_AR933X;
672 port->iotype = UPIO_MEM32; 689 port->iotype = UPIO_MEM32;
673 port->uartclk = pdata->uartclk;
674 690
675 port->regshift = 2; 691 port->regshift = 2;
676 port->fifosize = AR933X_UART_FIFO_SIZE; 692 port->fifosize = AR933X_UART_FIFO_SIZE;
@@ -686,15 +702,13 @@ static int ar933x_uart_probe(struct platform_device *pdev)
686 702
687 ret = uart_add_one_port(&ar933x_uart_driver, &up->port); 703 ret = uart_add_one_port(&ar933x_uart_driver, &up->port);
688 if (ret) 704 if (ret)
689 goto err_unmap; 705 goto err_disable_clk;
690 706
691 platform_set_drvdata(pdev, up); 707 platform_set_drvdata(pdev, up);
692 return 0; 708 return 0;
693 709
694err_unmap: 710err_disable_clk:
695 iounmap(up->port.membase); 711 clk_disable_unprepare(up->clk);
696err_free_up:
697 kfree(up);
698 return ret; 712 return ret;
699} 713}
700 714
@@ -703,23 +717,30 @@ static int ar933x_uart_remove(struct platform_device *pdev)
703 struct ar933x_uart_port *up; 717 struct ar933x_uart_port *up;
704 718
705 up = platform_get_drvdata(pdev); 719 up = platform_get_drvdata(pdev);
706 platform_set_drvdata(pdev, NULL);
707 720
708 if (up) { 721 if (up) {
709 uart_remove_one_port(&ar933x_uart_driver, &up->port); 722 uart_remove_one_port(&ar933x_uart_driver, &up->port);
710 iounmap(up->port.membase); 723 clk_disable_unprepare(up->clk);
711 kfree(up);
712 } 724 }
713 725
714 return 0; 726 return 0;
715} 727}
716 728
729#ifdef CONFIG_OF
730static const struct of_device_id ar933x_uart_of_ids[] = {
731 { .compatible = "qca,ar9330-uart" },
732 {},
733};
734MODULE_DEVICE_TABLE(of, ar933x_uart_of_ids);
735#endif
736
717static struct platform_driver ar933x_uart_platform_driver = { 737static struct platform_driver ar933x_uart_platform_driver = {
718 .probe = ar933x_uart_probe, 738 .probe = ar933x_uart_probe,
719 .remove = ar933x_uart_remove, 739 .remove = ar933x_uart_remove,
720 .driver = { 740 .driver = {
721 .name = DRIVER_NAME, 741 .name = DRIVER_NAME,
722 .owner = THIS_MODULE, 742 .owner = THIS_MODULE,
743 .of_match_table = of_match_ptr(ar933x_uart_of_ids),
723 }, 744 },
724}; 745};
725 746
@@ -727,7 +748,9 @@ static int __init ar933x_uart_init(void)
727{ 748{
728 int ret; 749 int ret;
729 750
730 ar933x_uart_driver.nr = CONFIG_SERIAL_AR933X_NR_UARTS; 751 if (ar933x_uart_console_enabled())
752 ar933x_uart_driver.cons = &ar933x_uart_console;
753
731 ret = uart_register_driver(&ar933x_uart_driver); 754 ret = uart_register_driver(&ar933x_uart_driver);
732 if (ret) 755 if (ret)
733 goto err_out; 756 goto err_out;
diff --git a/drivers/tty/serial/arc_uart.c b/drivers/tty/serial/arc_uart.c
index 22f280aa4f2c..569872f4c9b8 100644
--- a/drivers/tty/serial/arc_uart.c
+++ b/drivers/tty/serial/arc_uart.c
@@ -209,9 +209,9 @@ static void arc_serial_start_tx(struct uart_port *port)
209 arc_serial_tx_chars(uart); 209 arc_serial_tx_chars(uart);
210} 210}
211 211
212static void arc_serial_rx_chars(struct arc_uart_port *uart) 212static void arc_serial_rx_chars(struct arc_uart_port *uart, unsigned int status)
213{ 213{
214 unsigned int status, ch, flg = 0; 214 unsigned int ch, flg = 0;
215 215
216 /* 216 /*
217 * UART has 4 deep RX-FIFO. Driver's recongnition of this fact 217 * UART has 4 deep RX-FIFO. Driver's recongnition of this fact
@@ -222,11 +222,11 @@ static void arc_serial_rx_chars(struct arc_uart_port *uart)
222 * before RX-EMPTY=0, implies some sort of buffering going on in the 222 * before RX-EMPTY=0, implies some sort of buffering going on in the
223 * controller, which is indeed the Rx-FIFO. 223 * controller, which is indeed the Rx-FIFO.
224 */ 224 */
225 while (!((status = UART_GET_STATUS(uart)) & RXEMPTY)) { 225 do {
226 226 /*
227 ch = UART_GET_DATA(uart); 227 * This could be an Rx Intr for err (no data),
228 uart->port.icount.rx++; 228 * so check err and clear that Intr first
229 229 */
230 if (unlikely(status & (RXOERR | RXFERR))) { 230 if (unlikely(status & (RXOERR | RXFERR))) {
231 if (status & RXOERR) { 231 if (status & RXOERR) {
232 uart->port.icount.overrun++; 232 uart->port.icount.overrun++;
@@ -242,14 +242,19 @@ static void arc_serial_rx_chars(struct arc_uart_port *uart)
242 } else 242 } else
243 flg = TTY_NORMAL; 243 flg = TTY_NORMAL;
244 244
245 if (unlikely(uart_handle_sysrq_char(&uart->port, ch))) 245 if (status & RXEMPTY)
246 goto done; 246 continue;
247 247
248 uart_insert_char(&uart->port, status, RXOERR, ch, flg); 248 ch = UART_GET_DATA(uart);
249 uart->port.icount.rx++;
250
251 if (!(uart_handle_sysrq_char(&uart->port, ch)))
252 uart_insert_char(&uart->port, status, RXOERR, ch, flg);
249 253
250done: 254 spin_unlock(&uart->port.lock);
251 tty_flip_buffer_push(&uart->port.state->port); 255 tty_flip_buffer_push(&uart->port.state->port);
252 } 256 spin_lock(&uart->port.lock);
257 } while (!((status = UART_GET_STATUS(uart)) & RXEMPTY));
253} 258}
254 259
255/* 260/*
@@ -292,11 +297,11 @@ static irqreturn_t arc_serial_isr(int irq, void *dev_id)
292 * notifications from the UART Controller. 297 * notifications from the UART Controller.
293 * To demultiplex between the two, we check the relevant bits 298 * To demultiplex between the two, we check the relevant bits
294 */ 299 */
295 if ((status & RXIENB) && !(status & RXEMPTY)) { 300 if (status & RXIENB) {
296 301
297 /* already in ISR, no need of xx_irqsave */ 302 /* already in ISR, no need of xx_irqsave */
298 spin_lock(&uart->port.lock); 303 spin_lock(&uart->port.lock);
299 arc_serial_rx_chars(uart); 304 arc_serial_rx_chars(uart, status);
300 spin_unlock(&uart->port.lock); 305 spin_unlock(&uart->port.lock);
301 } 306 }
302 307
@@ -528,7 +533,7 @@ arc_uart_init_one(struct platform_device *pdev, int dev_id)
528 unsigned long *plat_data; 533 unsigned long *plat_data;
529 struct arc_uart_port *uart = &arc_uart_ports[dev_id]; 534 struct arc_uart_port *uart = &arc_uart_ports[dev_id];
530 535
531 plat_data = ((unsigned long *)(pdev->dev.platform_data)); 536 plat_data = (unsigned long *)dev_get_platdata(&pdev->dev);
532 if (!plat_data) 537 if (!plat_data)
533 return -ENODEV; 538 return -ENODEV;
534 539
diff --git a/drivers/tty/serial/atmel_serial.c b/drivers/tty/serial/atmel_serial.c
index 691265faebbe..d067285a2d20 100644
--- a/drivers/tty/serial/atmel_serial.c
+++ b/drivers/tty/serial/atmel_serial.c
@@ -39,8 +39,8 @@
39#include <linux/atmel_pdc.h> 39#include <linux/atmel_pdc.h>
40#include <linux/atmel_serial.h> 40#include <linux/atmel_serial.h>
41#include <linux/uaccess.h> 41#include <linux/uaccess.h>
42#include <linux/pinctrl/consumer.h>
43#include <linux/platform_data/atmel.h> 42#include <linux/platform_data/atmel.h>
43#include <linux/timer.h>
44 44
45#include <asm/io.h> 45#include <asm/io.h>
46#include <asm/ioctls.h> 46#include <asm/ioctls.h>
@@ -98,6 +98,7 @@ static void atmel_stop_rx(struct uart_port *port);
98#define UART_PUT_BRGR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_BRGR) 98#define UART_PUT_BRGR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_BRGR)
99#define UART_PUT_RTOR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_RTOR) 99#define UART_PUT_RTOR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_RTOR)
100#define UART_PUT_TTGR(port, v) __raw_writel(v, (port)->membase + ATMEL_US_TTGR) 100#define UART_PUT_TTGR(port, v) __raw_writel(v, (port)->membase + ATMEL_US_TTGR)
101#define UART_GET_IP_NAME(port) __raw_readl((port)->membase + ATMEL_US_NAME)
101 102
102 /* PDC registers */ 103 /* PDC registers */
103#define UART_PUT_PTCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_PTCR) 104#define UART_PUT_PTCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_PTCR)
@@ -140,13 +141,25 @@ struct atmel_uart_port {
140 u32 backup_imr; /* IMR saved during suspend */ 141 u32 backup_imr; /* IMR saved during suspend */
141 int break_active; /* break being received */ 142 int break_active; /* break being received */
142 143
143 short use_dma_rx; /* enable PDC receiver */ 144 bool use_dma_rx; /* enable DMA receiver */
145 bool use_pdc_rx; /* enable PDC receiver */
144 short pdc_rx_idx; /* current PDC RX buffer */ 146 short pdc_rx_idx; /* current PDC RX buffer */
145 struct atmel_dma_buffer pdc_rx[2]; /* PDC receier */ 147 struct atmel_dma_buffer pdc_rx[2]; /* PDC receier */
146 148
147 short use_dma_tx; /* enable PDC transmitter */ 149 bool use_dma_tx; /* enable DMA transmitter */
150 bool use_pdc_tx; /* enable PDC transmitter */
148 struct atmel_dma_buffer pdc_tx; /* PDC transmitter */ 151 struct atmel_dma_buffer pdc_tx; /* PDC transmitter */
149 152
153 spinlock_t lock_tx; /* port lock */
154 spinlock_t lock_rx; /* port lock */
155 struct dma_chan *chan_tx;
156 struct dma_chan *chan_rx;
157 struct dma_async_tx_descriptor *desc_tx;
158 struct dma_async_tx_descriptor *desc_rx;
159 dma_cookie_t cookie_tx;
160 dma_cookie_t cookie_rx;
161 struct scatterlist sg_tx;
162 struct scatterlist sg_rx;
150 struct tasklet_struct tasklet; 163 struct tasklet_struct tasklet;
151 unsigned int irq_status; 164 unsigned int irq_status;
152 unsigned int irq_status_prev; 165 unsigned int irq_status_prev;
@@ -155,6 +168,14 @@ struct atmel_uart_port {
155 168
156 struct serial_rs485 rs485; /* rs485 settings */ 169 struct serial_rs485 rs485; /* rs485 settings */
157 unsigned int tx_done_mask; 170 unsigned int tx_done_mask;
171 bool is_usart; /* usart or uart */
172 struct timer_list uart_timer; /* uart timer */
173 int (*prepare_rx)(struct uart_port *port);
174 int (*prepare_tx)(struct uart_port *port);
175 void (*schedule_rx)(struct uart_port *port);
176 void (*schedule_tx)(struct uart_port *port);
177 void (*release_rx)(struct uart_port *port);
178 void (*release_tx)(struct uart_port *port);
158}; 179};
159 180
160static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART]; 181static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART];
@@ -181,31 +202,45 @@ to_atmel_uart_port(struct uart_port *uart)
181} 202}
182 203
183#ifdef CONFIG_SERIAL_ATMEL_PDC 204#ifdef CONFIG_SERIAL_ATMEL_PDC
184static bool atmel_use_dma_rx(struct uart_port *port) 205static bool atmel_use_pdc_rx(struct uart_port *port)
185{ 206{
186 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 207 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
187 208
188 return atmel_port->use_dma_rx; 209 return atmel_port->use_pdc_rx;
189} 210}
190 211
191static bool atmel_use_dma_tx(struct uart_port *port) 212static bool atmel_use_pdc_tx(struct uart_port *port)
192{ 213{
193 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 214 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
194 215
195 return atmel_port->use_dma_tx; 216 return atmel_port->use_pdc_tx;
196} 217}
197#else 218#else
198static bool atmel_use_dma_rx(struct uart_port *port) 219static bool atmel_use_pdc_rx(struct uart_port *port)
199{ 220{
200 return false; 221 return false;
201} 222}
202 223
203static bool atmel_use_dma_tx(struct uart_port *port) 224static bool atmel_use_pdc_tx(struct uart_port *port)
204{ 225{
205 return false; 226 return false;
206} 227}
207#endif 228#endif
208 229
230static bool atmel_use_dma_tx(struct uart_port *port)
231{
232 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
233
234 return atmel_port->use_dma_tx;
235}
236
237static bool atmel_use_dma_rx(struct uart_port *port)
238{
239 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
240
241 return atmel_port->use_dma_rx;
242}
243
209/* Enable or disable the rs485 support */ 244/* Enable or disable the rs485 support */
210void atmel_config_rs485(struct uart_port *port, struct serial_rs485 *rs485conf) 245void atmel_config_rs485(struct uart_port *port, struct serial_rs485 *rs485conf)
211{ 246{
@@ -233,7 +268,7 @@ void atmel_config_rs485(struct uart_port *port, struct serial_rs485 *rs485conf)
233 mode |= ATMEL_US_USMODE_RS485; 268 mode |= ATMEL_US_USMODE_RS485;
234 } else { 269 } else {
235 dev_dbg(port->dev, "Setting UART to RS232\n"); 270 dev_dbg(port->dev, "Setting UART to RS232\n");
236 if (atmel_use_dma_tx(port)) 271 if (atmel_use_pdc_tx(port))
237 atmel_port->tx_done_mask = ATMEL_US_ENDTX | 272 atmel_port->tx_done_mask = ATMEL_US_ENDTX |
238 ATMEL_US_TXBUFE; 273 ATMEL_US_TXBUFE;
239 else 274 else
@@ -345,7 +380,7 @@ static void atmel_stop_tx(struct uart_port *port)
345{ 380{
346 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 381 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
347 382
348 if (atmel_use_dma_tx(port)) { 383 if (atmel_use_pdc_tx(port)) {
349 /* disable PDC transmit */ 384 /* disable PDC transmit */
350 UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS); 385 UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
351 } 386 }
@@ -364,7 +399,7 @@ static void atmel_start_tx(struct uart_port *port)
364{ 399{
365 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 400 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
366 401
367 if (atmel_use_dma_tx(port)) { 402 if (atmel_use_pdc_tx(port)) {
368 if (UART_GET_PTSR(port) & ATMEL_PDC_TXTEN) 403 if (UART_GET_PTSR(port) & ATMEL_PDC_TXTEN)
369 /* The transmitter is already running. Yes, we 404 /* The transmitter is already running. Yes, we
370 really need this.*/ 405 really need this.*/
@@ -390,7 +425,7 @@ static void atmel_start_rx(struct uart_port *port)
390 425
391 UART_PUT_CR(port, ATMEL_US_RXEN); 426 UART_PUT_CR(port, ATMEL_US_RXEN);
392 427
393 if (atmel_use_dma_rx(port)) { 428 if (atmel_use_pdc_rx(port)) {
394 /* enable PDC controller */ 429 /* enable PDC controller */
395 UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT | 430 UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
396 port->read_status_mask); 431 port->read_status_mask);
@@ -407,7 +442,7 @@ static void atmel_stop_rx(struct uart_port *port)
407{ 442{
408 UART_PUT_CR(port, ATMEL_US_RXDIS); 443 UART_PUT_CR(port, ATMEL_US_RXDIS);
409 444
410 if (atmel_use_dma_rx(port)) { 445 if (atmel_use_pdc_rx(port)) {
411 /* disable PDC receive */ 446 /* disable PDC receive */
412 UART_PUT_PTCR(port, ATMEL_PDC_RXTDIS); 447 UART_PUT_PTCR(port, ATMEL_PDC_RXTDIS);
413 UART_PUT_IDR(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT | 448 UART_PUT_IDR(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
@@ -564,6 +599,372 @@ static void atmel_tx_chars(struct uart_port *port)
564 UART_PUT_IER(port, atmel_port->tx_done_mask); 599 UART_PUT_IER(port, atmel_port->tx_done_mask);
565} 600}
566 601
602static void atmel_complete_tx_dma(void *arg)
603{
604 struct atmel_uart_port *atmel_port = arg;
605 struct uart_port *port = &atmel_port->uart;
606 struct circ_buf *xmit = &port->state->xmit;
607 struct dma_chan *chan = atmel_port->chan_tx;
608 unsigned long flags;
609
610 spin_lock_irqsave(&port->lock, flags);
611
612 if (chan)
613 dmaengine_terminate_all(chan);
614 xmit->tail += sg_dma_len(&atmel_port->sg_tx);
615 xmit->tail &= UART_XMIT_SIZE - 1;
616
617 port->icount.tx += sg_dma_len(&atmel_port->sg_tx);
618
619 spin_lock_irq(&atmel_port->lock_tx);
620 async_tx_ack(atmel_port->desc_tx);
621 atmel_port->cookie_tx = -EINVAL;
622 atmel_port->desc_tx = NULL;
623 spin_unlock_irq(&atmel_port->lock_tx);
624
625 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
626 uart_write_wakeup(port);
627
628 /* Do we really need this? */
629 if (!uart_circ_empty(xmit))
630 tasklet_schedule(&atmel_port->tasklet);
631
632 spin_unlock_irqrestore(&port->lock, flags);
633}
634
635static void atmel_release_tx_dma(struct uart_port *port)
636{
637 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
638 struct dma_chan *chan = atmel_port->chan_tx;
639
640 if (chan) {
641 dmaengine_terminate_all(chan);
642 dma_release_channel(chan);
643 dma_unmap_sg(port->dev, &atmel_port->sg_tx, 1,
644 DMA_MEM_TO_DEV);
645 }
646
647 atmel_port->desc_tx = NULL;
648 atmel_port->chan_tx = NULL;
649 atmel_port->cookie_tx = -EINVAL;
650}
651
652/*
653 * Called from tasklet with TXRDY interrupt is disabled.
654 */
655static void atmel_tx_dma(struct uart_port *port)
656{
657 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
658 struct circ_buf *xmit = &port->state->xmit;
659 struct dma_chan *chan = atmel_port->chan_tx;
660 struct dma_async_tx_descriptor *desc;
661 struct scatterlist *sg = &atmel_port->sg_tx;
662
663 /* Make sure we have an idle channel */
664 if (atmel_port->desc_tx != NULL)
665 return;
666
667 if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
668 /*
669 * DMA is idle now.
670 * Port xmit buffer is already mapped,
671 * and it is one page... Just adjust
672 * offsets and lengths. Since it is a circular buffer,
673 * we have to transmit till the end, and then the rest.
674 * Take the port lock to get a
675 * consistent xmit buffer state.
676 */
677 sg->offset = xmit->tail & (UART_XMIT_SIZE - 1);
678 sg_dma_address(sg) = (sg_dma_address(sg) &
679 ~(UART_XMIT_SIZE - 1))
680 + sg->offset;
681 sg_dma_len(sg) = CIRC_CNT_TO_END(xmit->head,
682 xmit->tail,
683 UART_XMIT_SIZE);
684 BUG_ON(!sg_dma_len(sg));
685
686 desc = dmaengine_prep_slave_sg(chan,
687 sg,
688 1,
689 DMA_MEM_TO_DEV,
690 DMA_PREP_INTERRUPT |
691 DMA_CTRL_ACK);
692 if (!desc) {
693 dev_err(port->dev, "Failed to send via dma!\n");
694 return;
695 }
696
697 dma_sync_sg_for_device(port->dev, sg, 1, DMA_MEM_TO_DEV);
698
699 atmel_port->desc_tx = desc;
700 desc->callback = atmel_complete_tx_dma;
701 desc->callback_param = atmel_port;
702 atmel_port->cookie_tx = dmaengine_submit(desc);
703
704 } else {
705 if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
706 /* DMA done, stop TX, start RX for RS485 */
707 atmel_start_rx(port);
708 }
709 }
710
711 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
712 uart_write_wakeup(port);
713}
714
715static int atmel_prepare_tx_dma(struct uart_port *port)
716{
717 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
718 dma_cap_mask_t mask;
719 struct dma_slave_config config;
720 int ret, nent;
721
722 dma_cap_zero(mask);
723 dma_cap_set(DMA_SLAVE, mask);
724
725 atmel_port->chan_tx = dma_request_slave_channel(port->dev, "tx");
726 if (atmel_port->chan_tx == NULL)
727 goto chan_err;
728 dev_info(port->dev, "using %s for tx DMA transfers\n",
729 dma_chan_name(atmel_port->chan_tx));
730
731 spin_lock_init(&atmel_port->lock_tx);
732 sg_init_table(&atmel_port->sg_tx, 1);
733 /* UART circular tx buffer is an aligned page. */
734 BUG_ON((int)port->state->xmit.buf & ~PAGE_MASK);
735 sg_set_page(&atmel_port->sg_tx,
736 virt_to_page(port->state->xmit.buf),
737 UART_XMIT_SIZE,
738 (int)port->state->xmit.buf & ~PAGE_MASK);
739 nent = dma_map_sg(port->dev,
740 &atmel_port->sg_tx,
741 1,
742 DMA_MEM_TO_DEV);
743
744 if (!nent) {
745 dev_dbg(port->dev, "need to release resource of dma\n");
746 goto chan_err;
747 } else {
748 dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__,
749 sg_dma_len(&atmel_port->sg_tx),
750 port->state->xmit.buf,
751 sg_dma_address(&atmel_port->sg_tx));
752 }
753
754 /* Configure the slave DMA */
755 memset(&config, 0, sizeof(config));
756 config.direction = DMA_MEM_TO_DEV;
757 config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
758 config.dst_addr = port->mapbase + ATMEL_US_THR;
759
760 ret = dmaengine_device_control(atmel_port->chan_tx,
761 DMA_SLAVE_CONFIG,
762 (unsigned long)&config);
763 if (ret) {
764 dev_err(port->dev, "DMA tx slave configuration failed\n");
765 goto chan_err;
766 }
767
768 return 0;
769
770chan_err:
771 dev_err(port->dev, "TX channel not available, switch to pio\n");
772 atmel_port->use_dma_tx = 0;
773 if (atmel_port->chan_tx)
774 atmel_release_tx_dma(port);
775 return -EINVAL;
776}
777
778static void atmel_flip_buffer_rx_dma(struct uart_port *port,
779 char *buf, size_t count)
780{
781 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
782 struct tty_port *tport = &port->state->port;
783
784 dma_sync_sg_for_cpu(port->dev,
785 &atmel_port->sg_rx,
786 1,
787 DMA_DEV_TO_MEM);
788
789 tty_insert_flip_string(tport, buf, count);
790
791 dma_sync_sg_for_device(port->dev,
792 &atmel_port->sg_rx,
793 1,
794 DMA_DEV_TO_MEM);
795 /*
796 * Drop the lock here since it might end up calling
797 * uart_start(), which takes the lock.
798 */
799 spin_unlock(&port->lock);
800 tty_flip_buffer_push(tport);
801 spin_lock(&port->lock);
802}
803
804static void atmel_complete_rx_dma(void *arg)
805{
806 struct uart_port *port = arg;
807 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
808
809 tasklet_schedule(&atmel_port->tasklet);
810}
811
812static void atmel_release_rx_dma(struct uart_port *port)
813{
814 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
815 struct dma_chan *chan = atmel_port->chan_rx;
816
817 if (chan) {
818 dmaengine_terminate_all(chan);
819 dma_release_channel(chan);
820 dma_unmap_sg(port->dev, &atmel_port->sg_rx, 1,
821 DMA_DEV_TO_MEM);
822 }
823
824 atmel_port->desc_rx = NULL;
825 atmel_port->chan_rx = NULL;
826 atmel_port->cookie_rx = -EINVAL;
827
828 if (!atmel_port->is_usart)
829 del_timer_sync(&atmel_port->uart_timer);
830}
831
832static void atmel_rx_from_dma(struct uart_port *port)
833{
834 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
835 struct circ_buf *ring = &atmel_port->rx_ring;
836 struct dma_chan *chan = atmel_port->chan_rx;
837 struct dma_tx_state state;
838 enum dma_status dmastat;
839 size_t pending, count;
840
841
842 /* Reset the UART timeout early so that we don't miss one */
843 UART_PUT_CR(port, ATMEL_US_STTTO);
844 dmastat = dmaengine_tx_status(chan,
845 atmel_port->cookie_rx,
846 &state);
847 /* Restart a new tasklet if DMA status is error */
848 if (dmastat == DMA_ERROR) {
849 dev_dbg(port->dev, "Get residue error, restart tasklet\n");
850 UART_PUT_IER(port, ATMEL_US_TIMEOUT);
851 tasklet_schedule(&atmel_port->tasklet);
852 return;
853 }
854 /* current transfer size should no larger than dma buffer */
855 pending = sg_dma_len(&atmel_port->sg_rx) - state.residue;
856 BUG_ON(pending > sg_dma_len(&atmel_port->sg_rx));
857
858 /*
859 * This will take the chars we have so far,
860 * ring->head will record the transfer size, only new bytes come
861 * will insert into the framework.
862 */
863 if (pending > ring->head) {
864 count = pending - ring->head;
865
866 atmel_flip_buffer_rx_dma(port, ring->buf + ring->head, count);
867
868 ring->head += count;
869 if (ring->head == sg_dma_len(&atmel_port->sg_rx))
870 ring->head = 0;
871
872 port->icount.rx += count;
873 }
874
875 UART_PUT_IER(port, ATMEL_US_TIMEOUT);
876}
877
878static int atmel_prepare_rx_dma(struct uart_port *port)
879{
880 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
881 struct dma_async_tx_descriptor *desc;
882 dma_cap_mask_t mask;
883 struct dma_slave_config config;
884 struct circ_buf *ring;
885 int ret, nent;
886
887 ring = &atmel_port->rx_ring;
888
889 dma_cap_zero(mask);
890 dma_cap_set(DMA_CYCLIC, mask);
891
892 atmel_port->chan_rx = dma_request_slave_channel(port->dev, "rx");
893 if (atmel_port->chan_rx == NULL)
894 goto chan_err;
895 dev_info(port->dev, "using %s for rx DMA transfers\n",
896 dma_chan_name(atmel_port->chan_rx));
897
898 spin_lock_init(&atmel_port->lock_rx);
899 sg_init_table(&atmel_port->sg_rx, 1);
900 /* UART circular rx buffer is an aligned page. */
901 BUG_ON((int)port->state->xmit.buf & ~PAGE_MASK);
902 sg_set_page(&atmel_port->sg_rx,
903 virt_to_page(ring->buf),
904 ATMEL_SERIAL_RINGSIZE,
905 (int)ring->buf & ~PAGE_MASK);
906 nent = dma_map_sg(port->dev,
907 &atmel_port->sg_rx,
908 1,
909 DMA_DEV_TO_MEM);
910
911 if (!nent) {
912 dev_dbg(port->dev, "need to release resource of dma\n");
913 goto chan_err;
914 } else {
915 dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__,
916 sg_dma_len(&atmel_port->sg_rx),
917 ring->buf,
918 sg_dma_address(&atmel_port->sg_rx));
919 }
920
921 /* Configure the slave DMA */
922 memset(&config, 0, sizeof(config));
923 config.direction = DMA_DEV_TO_MEM;
924 config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
925 config.src_addr = port->mapbase + ATMEL_US_RHR;
926
927 ret = dmaengine_device_control(atmel_port->chan_rx,
928 DMA_SLAVE_CONFIG,
929 (unsigned long)&config);
930 if (ret) {
931 dev_err(port->dev, "DMA rx slave configuration failed\n");
932 goto chan_err;
933 }
934 /*
935 * Prepare a cyclic dma transfer, assign 2 descriptors,
936 * each one is half ring buffer size
937 */
938 desc = dmaengine_prep_dma_cyclic(atmel_port->chan_rx,
939 sg_dma_address(&atmel_port->sg_rx),
940 sg_dma_len(&atmel_port->sg_rx),
941 sg_dma_len(&atmel_port->sg_rx)/2,
942 DMA_DEV_TO_MEM,
943 DMA_PREP_INTERRUPT);
944 desc->callback = atmel_complete_rx_dma;
945 desc->callback_param = port;
946 atmel_port->desc_rx = desc;
947 atmel_port->cookie_rx = dmaengine_submit(desc);
948
949 return 0;
950
951chan_err:
952 dev_err(port->dev, "RX channel not available, switch to pio\n");
953 atmel_port->use_dma_rx = 0;
954 if (atmel_port->chan_rx)
955 atmel_release_rx_dma(port);
956 return -EINVAL;
957}
958
959static void atmel_uart_timer_callback(unsigned long data)
960{
961 struct uart_port *port = (void *)data;
962 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
963
964 tasklet_schedule(&atmel_port->tasklet);
965 mod_timer(&atmel_port->uart_timer, jiffies + uart_poll_timeout(port));
966}
967
567/* 968/*
568 * receive interrupt handler. 969 * receive interrupt handler.
569 */ 970 */
@@ -572,7 +973,7 @@ atmel_handle_receive(struct uart_port *port, unsigned int pending)
572{ 973{
573 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 974 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
574 975
575 if (atmel_use_dma_rx(port)) { 976 if (atmel_use_pdc_rx(port)) {
576 /* 977 /*
577 * PDC receive. Just schedule the tasklet and let it 978 * PDC receive. Just schedule the tasklet and let it
578 * figure out the details. 979 * figure out the details.
@@ -591,6 +992,13 @@ atmel_handle_receive(struct uart_port *port, unsigned int pending)
591 atmel_pdc_rxerr(port, pending); 992 atmel_pdc_rxerr(port, pending);
592 } 993 }
593 994
995 if (atmel_use_dma_rx(port)) {
996 if (pending & ATMEL_US_TIMEOUT) {
997 UART_PUT_IDR(port, ATMEL_US_TIMEOUT);
998 tasklet_schedule(&atmel_port->tasklet);
999 }
1000 }
1001
594 /* Interrupt receive */ 1002 /* Interrupt receive */
595 if (pending & ATMEL_US_RXRDY) 1003 if (pending & ATMEL_US_RXRDY)
596 atmel_rx_chars(port); 1004 atmel_rx_chars(port);
@@ -658,10 +1066,21 @@ static irqreturn_t atmel_interrupt(int irq, void *dev_id)
658 return pass_counter ? IRQ_HANDLED : IRQ_NONE; 1066 return pass_counter ? IRQ_HANDLED : IRQ_NONE;
659} 1067}
660 1068
1069static void atmel_release_tx_pdc(struct uart_port *port)
1070{
1071 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1072 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1073
1074 dma_unmap_single(port->dev,
1075 pdc->dma_addr,
1076 pdc->dma_size,
1077 DMA_TO_DEVICE);
1078}
1079
661/* 1080/*
662 * Called from tasklet with ENDTX and TXBUFE interrupts disabled. 1081 * Called from tasklet with ENDTX and TXBUFE interrupts disabled.
663 */ 1082 */
664static void atmel_tx_dma(struct uart_port *port) 1083static void atmel_tx_pdc(struct uart_port *port)
665{ 1084{
666 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1085 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
667 struct circ_buf *xmit = &port->state->xmit; 1086 struct circ_buf *xmit = &port->state->xmit;
@@ -710,6 +1129,23 @@ static void atmel_tx_dma(struct uart_port *port)
710 uart_write_wakeup(port); 1129 uart_write_wakeup(port);
711} 1130}
712 1131
1132static int atmel_prepare_tx_pdc(struct uart_port *port)
1133{
1134 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1135 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1136 struct circ_buf *xmit = &port->state->xmit;
1137
1138 pdc->buf = xmit->buf;
1139 pdc->dma_addr = dma_map_single(port->dev,
1140 pdc->buf,
1141 UART_XMIT_SIZE,
1142 DMA_TO_DEVICE);
1143 pdc->dma_size = UART_XMIT_SIZE;
1144 pdc->ofs = 0;
1145
1146 return 0;
1147}
1148
713static void atmel_rx_from_ring(struct uart_port *port) 1149static void atmel_rx_from_ring(struct uart_port *port)
714{ 1150{
715 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1151 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
@@ -778,7 +1214,26 @@ static void atmel_rx_from_ring(struct uart_port *port)
778 spin_lock(&port->lock); 1214 spin_lock(&port->lock);
779} 1215}
780 1216
781static void atmel_rx_from_dma(struct uart_port *port) 1217static void atmel_release_rx_pdc(struct uart_port *port)
1218{
1219 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1220 int i;
1221
1222 for (i = 0; i < 2; i++) {
1223 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1224
1225 dma_unmap_single(port->dev,
1226 pdc->dma_addr,
1227 pdc->dma_size,
1228 DMA_FROM_DEVICE);
1229 kfree(pdc->buf);
1230 }
1231
1232 if (!atmel_port->is_usart)
1233 del_timer_sync(&atmel_port->uart_timer);
1234}
1235
1236static void atmel_rx_from_pdc(struct uart_port *port)
782{ 1237{
783 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1238 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
784 struct tty_port *tport = &port->state->port; 1239 struct tty_port *tport = &port->state->port;
@@ -855,6 +1310,45 @@ static void atmel_rx_from_dma(struct uart_port *port)
855 UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT); 1310 UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
856} 1311}
857 1312
1313static int atmel_prepare_rx_pdc(struct uart_port *port)
1314{
1315 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1316 int i;
1317
1318 for (i = 0; i < 2; i++) {
1319 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1320
1321 pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL);
1322 if (pdc->buf == NULL) {
1323 if (i != 0) {
1324 dma_unmap_single(port->dev,
1325 atmel_port->pdc_rx[0].dma_addr,
1326 PDC_BUFFER_SIZE,
1327 DMA_FROM_DEVICE);
1328 kfree(atmel_port->pdc_rx[0].buf);
1329 }
1330 atmel_port->use_pdc_rx = 0;
1331 return -ENOMEM;
1332 }
1333 pdc->dma_addr = dma_map_single(port->dev,
1334 pdc->buf,
1335 PDC_BUFFER_SIZE,
1336 DMA_FROM_DEVICE);
1337 pdc->dma_size = PDC_BUFFER_SIZE;
1338 pdc->ofs = 0;
1339 }
1340
1341 atmel_port->pdc_rx_idx = 0;
1342
1343 UART_PUT_RPR(port, atmel_port->pdc_rx[0].dma_addr);
1344 UART_PUT_RCR(port, PDC_BUFFER_SIZE);
1345
1346 UART_PUT_RNPR(port, atmel_port->pdc_rx[1].dma_addr);
1347 UART_PUT_RNCR(port, PDC_BUFFER_SIZE);
1348
1349 return 0;
1350}
1351
858/* 1352/*
859 * tasklet handling tty stuff outside the interrupt handler. 1353 * tasklet handling tty stuff outside the interrupt handler.
860 */ 1354 */
@@ -868,10 +1362,7 @@ static void atmel_tasklet_func(unsigned long data)
868 /* The interrupt handler does not take the lock */ 1362 /* The interrupt handler does not take the lock */
869 spin_lock(&port->lock); 1363 spin_lock(&port->lock);
870 1364
871 if (atmel_use_dma_tx(port)) 1365 atmel_port->schedule_tx(port);
872 atmel_tx_dma(port);
873 else
874 atmel_tx_chars(port);
875 1366
876 status = atmel_port->irq_status; 1367 status = atmel_port->irq_status;
877 status_change = status ^ atmel_port->irq_status_prev; 1368 status_change = status ^ atmel_port->irq_status_prev;
@@ -893,19 +1384,152 @@ static void atmel_tasklet_func(unsigned long data)
893 atmel_port->irq_status_prev = status; 1384 atmel_port->irq_status_prev = status;
894 } 1385 }
895 1386
896 if (atmel_use_dma_rx(port)) 1387 atmel_port->schedule_rx(port);
897 atmel_rx_from_dma(port);
898 else
899 atmel_rx_from_ring(port);
900 1388
901 spin_unlock(&port->lock); 1389 spin_unlock(&port->lock);
902} 1390}
903 1391
1392static int atmel_init_property(struct atmel_uart_port *atmel_port,
1393 struct platform_device *pdev)
1394{
1395 struct device_node *np = pdev->dev.of_node;
1396 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
1397
1398 if (np) {
1399 /* DMA/PDC usage specification */
1400 if (of_get_property(np, "atmel,use-dma-rx", NULL)) {
1401 if (of_get_property(np, "dmas", NULL)) {
1402 atmel_port->use_dma_rx = true;
1403 atmel_port->use_pdc_rx = false;
1404 } else {
1405 atmel_port->use_dma_rx = false;
1406 atmel_port->use_pdc_rx = true;
1407 }
1408 } else {
1409 atmel_port->use_dma_rx = false;
1410 atmel_port->use_pdc_rx = false;
1411 }
1412
1413 if (of_get_property(np, "atmel,use-dma-tx", NULL)) {
1414 if (of_get_property(np, "dmas", NULL)) {
1415 atmel_port->use_dma_tx = true;
1416 atmel_port->use_pdc_tx = false;
1417 } else {
1418 atmel_port->use_dma_tx = false;
1419 atmel_port->use_pdc_tx = true;
1420 }
1421 } else {
1422 atmel_port->use_dma_tx = false;
1423 atmel_port->use_pdc_tx = false;
1424 }
1425
1426 } else {
1427 atmel_port->use_pdc_rx = pdata->use_dma_rx;
1428 atmel_port->use_pdc_tx = pdata->use_dma_tx;
1429 atmel_port->use_dma_rx = false;
1430 atmel_port->use_dma_tx = false;
1431 }
1432
1433 return 0;
1434}
1435
1436static void atmel_init_rs485(struct atmel_uart_port *atmel_port,
1437 struct platform_device *pdev)
1438{
1439 struct device_node *np = pdev->dev.of_node;
1440 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
1441
1442 if (np) {
1443 u32 rs485_delay[2];
1444 /* rs485 properties */
1445 if (of_property_read_u32_array(np, "rs485-rts-delay",
1446 rs485_delay, 2) == 0) {
1447 struct serial_rs485 *rs485conf = &atmel_port->rs485;
1448
1449 rs485conf->delay_rts_before_send = rs485_delay[0];
1450 rs485conf->delay_rts_after_send = rs485_delay[1];
1451 rs485conf->flags = 0;
1452
1453 if (of_get_property(np, "rs485-rx-during-tx", NULL))
1454 rs485conf->flags |= SER_RS485_RX_DURING_TX;
1455
1456 if (of_get_property(np, "linux,rs485-enabled-at-boot-time",
1457 NULL))
1458 rs485conf->flags |= SER_RS485_ENABLED;
1459 }
1460 } else {
1461 atmel_port->rs485 = pdata->rs485;
1462 }
1463
1464}
1465
1466static void atmel_set_ops(struct uart_port *port)
1467{
1468 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1469
1470 if (atmel_use_dma_rx(port)) {
1471 atmel_port->prepare_rx = &atmel_prepare_rx_dma;
1472 atmel_port->schedule_rx = &atmel_rx_from_dma;
1473 atmel_port->release_rx = &atmel_release_rx_dma;
1474 } else if (atmel_use_pdc_rx(port)) {
1475 atmel_port->prepare_rx = &atmel_prepare_rx_pdc;
1476 atmel_port->schedule_rx = &atmel_rx_from_pdc;
1477 atmel_port->release_rx = &atmel_release_rx_pdc;
1478 } else {
1479 atmel_port->prepare_rx = NULL;
1480 atmel_port->schedule_rx = &atmel_rx_from_ring;
1481 atmel_port->release_rx = NULL;
1482 }
1483
1484 if (atmel_use_dma_tx(port)) {
1485 atmel_port->prepare_tx = &atmel_prepare_tx_dma;
1486 atmel_port->schedule_tx = &atmel_tx_dma;
1487 atmel_port->release_tx = &atmel_release_tx_dma;
1488 } else if (atmel_use_pdc_tx(port)) {
1489 atmel_port->prepare_tx = &atmel_prepare_tx_pdc;
1490 atmel_port->schedule_tx = &atmel_tx_pdc;
1491 atmel_port->release_tx = &atmel_release_tx_pdc;
1492 } else {
1493 atmel_port->prepare_tx = NULL;
1494 atmel_port->schedule_tx = &atmel_tx_chars;
1495 atmel_port->release_tx = NULL;
1496 }
1497}
1498
1499/*
1500 * Get ip name usart or uart
1501 */
1502static int atmel_get_ip_name(struct uart_port *port)
1503{
1504 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1505 int name = UART_GET_IP_NAME(port);
1506 int usart, uart;
1507 /* usart and uart ascii */
1508 usart = 0x55534152;
1509 uart = 0x44424755;
1510
1511 atmel_port->is_usart = false;
1512
1513 if (name == usart) {
1514 dev_dbg(port->dev, "This is usart\n");
1515 atmel_port->is_usart = true;
1516 } else if (name == uart) {
1517 dev_dbg(port->dev, "This is uart\n");
1518 atmel_port->is_usart = false;
1519 } else {
1520 dev_err(port->dev, "Not supported ip name, set to uart\n");
1521 return -EINVAL;
1522 }
1523
1524 return 0;
1525}
1526
904/* 1527/*
905 * Perform initialization and enable port for reception 1528 * Perform initialization and enable port for reception
906 */ 1529 */
907static int atmel_startup(struct uart_port *port) 1530static int atmel_startup(struct uart_port *port)
908{ 1531{
1532 struct platform_device *pdev = to_platform_device(port->dev);
909 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1533 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
910 struct tty_struct *tty = port->state->port.tty; 1534 struct tty_struct *tty = port->state->port.tty;
911 int retval; 1535 int retval;
@@ -930,53 +1554,19 @@ static int atmel_startup(struct uart_port *port)
930 /* 1554 /*
931 * Initialize DMA (if necessary) 1555 * Initialize DMA (if necessary)
932 */ 1556 */
933 if (atmel_use_dma_rx(port)) { 1557 atmel_init_property(atmel_port, pdev);
934 int i;
935
936 for (i = 0; i < 2; i++) {
937 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
938
939 pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL);
940 if (pdc->buf == NULL) {
941 if (i != 0) {
942 dma_unmap_single(port->dev,
943 atmel_port->pdc_rx[0].dma_addr,
944 PDC_BUFFER_SIZE,
945 DMA_FROM_DEVICE);
946 kfree(atmel_port->pdc_rx[0].buf);
947 }
948 free_irq(port->irq, port);
949 return -ENOMEM;
950 }
951 pdc->dma_addr = dma_map_single(port->dev,
952 pdc->buf,
953 PDC_BUFFER_SIZE,
954 DMA_FROM_DEVICE);
955 pdc->dma_size = PDC_BUFFER_SIZE;
956 pdc->ofs = 0;
957 }
958 1558
959 atmel_port->pdc_rx_idx = 0; 1559 if (atmel_port->prepare_rx) {
960 1560 retval = atmel_port->prepare_rx(port);
961 UART_PUT_RPR(port, atmel_port->pdc_rx[0].dma_addr); 1561 if (retval < 0)
962 UART_PUT_RCR(port, PDC_BUFFER_SIZE); 1562 atmel_set_ops(port);
963
964 UART_PUT_RNPR(port, atmel_port->pdc_rx[1].dma_addr);
965 UART_PUT_RNCR(port, PDC_BUFFER_SIZE);
966 } 1563 }
967 if (atmel_use_dma_tx(port)) {
968 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
969 struct circ_buf *xmit = &port->state->xmit;
970 1564
971 pdc->buf = xmit->buf; 1565 if (atmel_port->prepare_tx) {
972 pdc->dma_addr = dma_map_single(port->dev, 1566 retval = atmel_port->prepare_tx(port);
973 pdc->buf, 1567 if (retval < 0)
974 UART_XMIT_SIZE, 1568 atmel_set_ops(port);
975 DMA_TO_DEVICE);
976 pdc->dma_size = UART_XMIT_SIZE;
977 pdc->ofs = 0;
978 } 1569 }
979
980 /* 1570 /*
981 * If there is a specific "open" function (to register 1571 * If there is a specific "open" function (to register
982 * control line interrupts) 1572 * control line interrupts)
@@ -1000,14 +1590,38 @@ static int atmel_startup(struct uart_port *port)
1000 /* enable xmit & rcvr */ 1590 /* enable xmit & rcvr */
1001 UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN); 1591 UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
1002 1592
1003 if (atmel_use_dma_rx(port)) { 1593 if (atmel_use_pdc_rx(port)) {
1004 /* set UART timeout */ 1594 /* set UART timeout */
1005 UART_PUT_RTOR(port, PDC_RX_TIMEOUT); 1595 if (!atmel_port->is_usart) {
1006 UART_PUT_CR(port, ATMEL_US_STTTO); 1596 setup_timer(&atmel_port->uart_timer,
1007 1597 atmel_uart_timer_callback,
1008 UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT); 1598 (unsigned long)port);
1599 mod_timer(&atmel_port->uart_timer,
1600 jiffies + uart_poll_timeout(port));
1601 /* set USART timeout */
1602 } else {
1603 UART_PUT_RTOR(port, PDC_RX_TIMEOUT);
1604 UART_PUT_CR(port, ATMEL_US_STTTO);
1605
1606 UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
1607 }
1009 /* enable PDC controller */ 1608 /* enable PDC controller */
1010 UART_PUT_PTCR(port, ATMEL_PDC_RXTEN); 1609 UART_PUT_PTCR(port, ATMEL_PDC_RXTEN);
1610 } else if (atmel_use_dma_rx(port)) {
1611 /* set UART timeout */
1612 if (!atmel_port->is_usart) {
1613 setup_timer(&atmel_port->uart_timer,
1614 atmel_uart_timer_callback,
1615 (unsigned long)port);
1616 mod_timer(&atmel_port->uart_timer,
1617 jiffies + uart_poll_timeout(port));
1618 /* set USART timeout */
1619 } else {
1620 UART_PUT_RTOR(port, PDC_RX_TIMEOUT);
1621 UART_PUT_CR(port, ATMEL_US_STTTO);
1622
1623 UART_PUT_IER(port, ATMEL_US_TIMEOUT);
1624 }
1011 } else { 1625 } else {
1012 /* enable receive only */ 1626 /* enable receive only */
1013 UART_PUT_IER(port, ATMEL_US_RXRDY); 1627 UART_PUT_IER(port, ATMEL_US_RXRDY);
@@ -1031,27 +1645,10 @@ static void atmel_shutdown(struct uart_port *port)
1031 /* 1645 /*
1032 * Shut-down the DMA. 1646 * Shut-down the DMA.
1033 */ 1647 */
1034 if (atmel_use_dma_rx(port)) { 1648 if (atmel_port->release_rx)
1035 int i; 1649 atmel_port->release_rx(port);
1036 1650 if (atmel_port->release_tx)
1037 for (i = 0; i < 2; i++) { 1651 atmel_port->release_tx(port);
1038 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1039
1040 dma_unmap_single(port->dev,
1041 pdc->dma_addr,
1042 pdc->dma_size,
1043 DMA_FROM_DEVICE);
1044 kfree(pdc->buf);
1045 }
1046 }
1047 if (atmel_use_dma_tx(port)) {
1048 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1049
1050 dma_unmap_single(port->dev,
1051 pdc->dma_addr,
1052 pdc->dma_size,
1053 DMA_TO_DEVICE);
1054 }
1055 1652
1056 /* 1653 /*
1057 * Disable all interrupts, port and break condition. 1654 * Disable all interrupts, port and break condition.
@@ -1080,7 +1677,7 @@ static void atmel_flush_buffer(struct uart_port *port)
1080{ 1677{
1081 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1678 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1082 1679
1083 if (atmel_use_dma_tx(port)) { 1680 if (atmel_use_pdc_tx(port)) {
1084 UART_PUT_TCR(port, 0); 1681 UART_PUT_TCR(port, 0);
1085 atmel_port->pdc_tx.ofs = 0; 1682 atmel_port->pdc_tx.ofs = 0;
1086 } 1683 }
@@ -1193,7 +1790,7 @@ static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
1193 if (termios->c_iflag & (BRKINT | PARMRK)) 1790 if (termios->c_iflag & (BRKINT | PARMRK))
1194 port->read_status_mask |= ATMEL_US_RXBRK; 1791 port->read_status_mask |= ATMEL_US_RXBRK;
1195 1792
1196 if (atmel_use_dma_rx(port)) 1793 if (atmel_use_pdc_rx(port))
1197 /* need to enable error interrupts */ 1794 /* need to enable error interrupts */
1198 UART_PUT_IER(port, port->read_status_mask); 1795 UART_PUT_IER(port, port->read_status_mask);
1199 1796
@@ -1423,38 +2020,6 @@ static struct uart_ops atmel_pops = {
1423#endif 2020#endif
1424}; 2021};
1425 2022
1426static void atmel_of_init_port(struct atmel_uart_port *atmel_port,
1427 struct device_node *np)
1428{
1429 u32 rs485_delay[2];
1430
1431 /* DMA/PDC usage specification */
1432 if (of_get_property(np, "atmel,use-dma-rx", NULL))
1433 atmel_port->use_dma_rx = 1;
1434 else
1435 atmel_port->use_dma_rx = 0;
1436 if (of_get_property(np, "atmel,use-dma-tx", NULL))
1437 atmel_port->use_dma_tx = 1;
1438 else
1439 atmel_port->use_dma_tx = 0;
1440
1441 /* rs485 properties */
1442 if (of_property_read_u32_array(np, "rs485-rts-delay",
1443 rs485_delay, 2) == 0) {
1444 struct serial_rs485 *rs485conf = &atmel_port->rs485;
1445
1446 rs485conf->delay_rts_before_send = rs485_delay[0];
1447 rs485conf->delay_rts_after_send = rs485_delay[1];
1448 rs485conf->flags = 0;
1449
1450 if (of_get_property(np, "rs485-rx-during-tx", NULL))
1451 rs485conf->flags |= SER_RS485_RX_DURING_TX;
1452
1453 if (of_get_property(np, "linux,rs485-enabled-at-boot-time", NULL))
1454 rs485conf->flags |= SER_RS485_ENABLED;
1455 }
1456}
1457
1458/* 2023/*
1459 * Configure the port from the platform device resource info. 2024 * Configure the port from the platform device resource info.
1460 */ 2025 */
@@ -1463,15 +2028,12 @@ static int atmel_init_port(struct atmel_uart_port *atmel_port,
1463{ 2028{
1464 int ret; 2029 int ret;
1465 struct uart_port *port = &atmel_port->uart; 2030 struct uart_port *port = &atmel_port->uart;
1466 struct atmel_uart_data *pdata = pdev->dev.platform_data; 2031 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
1467 2032
1468 if (pdev->dev.of_node) { 2033 if (!atmel_init_property(atmel_port, pdev))
1469 atmel_of_init_port(atmel_port, pdev->dev.of_node); 2034 atmel_set_ops(port);
1470 } else { 2035
1471 atmel_port->use_dma_rx = pdata->use_dma_rx; 2036 atmel_init_rs485(atmel_port, pdev);
1472 atmel_port->use_dma_tx = pdata->use_dma_tx;
1473 atmel_port->rs485 = pdata->rs485;
1474 }
1475 2037
1476 port->iotype = UPIO_MEM; 2038 port->iotype = UPIO_MEM;
1477 port->flags = UPF_BOOT_AUTOCONF; 2039 port->flags = UPF_BOOT_AUTOCONF;
@@ -1516,7 +2078,7 @@ static int atmel_init_port(struct atmel_uart_port *atmel_port,
1516 /* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */ 2078 /* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */
1517 if (atmel_port->rs485.flags & SER_RS485_ENABLED) 2079 if (atmel_port->rs485.flags & SER_RS485_ENABLED)
1518 atmel_port->tx_done_mask = ATMEL_US_TXEMPTY; 2080 atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
1519 else if (atmel_use_dma_tx(port)) { 2081 else if (atmel_use_pdc_tx(port)) {
1520 port->fifosize = PDC_BUFFER_SIZE; 2082 port->fifosize = PDC_BUFFER_SIZE;
1521 atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE; 2083 atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE;
1522 } else { 2084 } else {
@@ -1664,7 +2226,7 @@ static int __init atmel_console_init(void)
1664 int ret; 2226 int ret;
1665 if (atmel_default_console_device) { 2227 if (atmel_default_console_device) {
1666 struct atmel_uart_data *pdata = 2228 struct atmel_uart_data *pdata =
1667 atmel_default_console_device->dev.platform_data; 2229 dev_get_platdata(&atmel_default_console_device->dev);
1668 int id = pdata->num; 2230 int id = pdata->num;
1669 struct atmel_uart_port *port = &atmel_ports[id]; 2231 struct atmel_uart_port *port = &atmel_ports[id];
1670 2232
@@ -1772,10 +2334,9 @@ static int atmel_serial_probe(struct platform_device *pdev)
1772{ 2334{
1773 struct atmel_uart_port *port; 2335 struct atmel_uart_port *port;
1774 struct device_node *np = pdev->dev.of_node; 2336 struct device_node *np = pdev->dev.of_node;
1775 struct atmel_uart_data *pdata = pdev->dev.platform_data; 2337 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
1776 void *data; 2338 void *data;
1777 int ret = -ENODEV; 2339 int ret = -ENODEV;
1778 struct pinctrl *pinctrl;
1779 2340
1780 BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1)); 2341 BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1));
1781 2342
@@ -1809,13 +2370,7 @@ static int atmel_serial_probe(struct platform_device *pdev)
1809 if (ret) 2370 if (ret)
1810 goto err; 2371 goto err;
1811 2372
1812 pinctrl = devm_pinctrl_get_select_default(&pdev->dev); 2373 if (!atmel_use_pdc_rx(&port->uart)) {
1813 if (IS_ERR(pinctrl)) {
1814 ret = PTR_ERR(pinctrl);
1815 goto err;
1816 }
1817
1818 if (!atmel_use_dma_rx(&port->uart)) {
1819 ret = -ENOMEM; 2374 ret = -ENOMEM;
1820 data = kmalloc(sizeof(struct atmel_uart_char) 2375 data = kmalloc(sizeof(struct atmel_uart_char)
1821 * ATMEL_SERIAL_RINGSIZE, GFP_KERNEL); 2376 * ATMEL_SERIAL_RINGSIZE, GFP_KERNEL);
@@ -1847,6 +2402,13 @@ static int atmel_serial_probe(struct platform_device *pdev)
1847 UART_PUT_CR(&port->uart, ATMEL_US_RTSEN); 2402 UART_PUT_CR(&port->uart, ATMEL_US_RTSEN);
1848 } 2403 }
1849 2404
2405 /*
2406 * Get port name of usart or uart
2407 */
2408 ret = atmel_get_ip_name(&port->uart);
2409 if (ret < 0)
2410 goto err_add_port;
2411
1850 return 0; 2412 return 0;
1851 2413
1852err_add_port: 2414err_add_port:
@@ -1868,7 +2430,6 @@ static int atmel_serial_remove(struct platform_device *pdev)
1868 int ret = 0; 2430 int ret = 0;
1869 2431
1870 device_init_wakeup(&pdev->dev, 0); 2432 device_init_wakeup(&pdev->dev, 0);
1871 platform_set_drvdata(pdev, NULL);
1872 2433
1873 ret = uart_remove_one_port(&atmel_uart, port); 2434 ret = uart_remove_one_port(&atmel_uart, port);
1874 2435
diff --git a/drivers/tty/serial/bcm63xx_uart.c b/drivers/tty/serial/bcm63xx_uart.c
index 6fa2ae77fffd..649d5129c4b4 100644
--- a/drivers/tty/serial/bcm63xx_uart.c
+++ b/drivers/tty/serial/bcm63xx_uart.c
@@ -302,7 +302,9 @@ static void bcm_uart_do_rx(struct uart_port *port)
302 302
303 } while (--max_count); 303 } while (--max_count);
304 304
305 spin_unlock(&port->lock);
305 tty_flip_buffer_push(tty_port); 306 tty_flip_buffer_push(tty_port);
307 spin_lock(&port->lock);
306} 308}
307 309
308/* 310/*
@@ -852,7 +854,6 @@ static int bcm_uart_remove(struct platform_device *pdev)
852 854
853 port = platform_get_drvdata(pdev); 855 port = platform_get_drvdata(pdev);
854 uart_remove_one_port(&bcm_uart_driver, port); 856 uart_remove_one_port(&bcm_uart_driver, port);
855 platform_set_drvdata(pdev, NULL);
856 /* mark port as free */ 857 /* mark port as free */
857 ports[pdev->id].membase = 0; 858 ports[pdev->id].membase = 0;
858 return 0; 859 return 0;
diff --git a/drivers/tty/serial/bfin_sport_uart.c b/drivers/tty/serial/bfin_sport_uart.c
index 487c173b0f72..87636cc61a21 100644
--- a/drivers/tty/serial/bfin_sport_uart.c
+++ b/drivers/tty/serial/bfin_sport_uart.c
@@ -161,11 +161,12 @@ static irqreturn_t sport_uart_rx_irq(int irq, void *dev_id)
161 if (!uart_handle_sysrq_char(&up->port, ch)) 161 if (!uart_handle_sysrq_char(&up->port, ch))
162 tty_insert_flip_char(port, ch, TTY_NORMAL); 162 tty_insert_flip_char(port, ch, TTY_NORMAL);
163 } 163 }
164 /* XXX this won't deadlock with lowlat? */
165 tty_flip_buffer_push(port);
166 164
167 spin_unlock(&up->port.lock); 165 spin_unlock(&up->port.lock);
168 166
167 /* XXX this won't deadlock with lowlat? */
168 tty_flip_buffer_push(port);
169
169 return IRQ_HANDLED; 170 return IRQ_HANDLED;
170} 171}
171 172
@@ -766,7 +767,8 @@ static int sport_uart_probe(struct platform_device *pdev)
766 } 767 }
767 768
768 ret = peripheral_request_list( 769 ret = peripheral_request_list(
769 (unsigned short *)pdev->dev.platform_data, DRV_NAME); 770 (unsigned short *)dev_get_platdata(&pdev->dev),
771 DRV_NAME);
770 if (ret) { 772 if (ret) {
771 dev_err(&pdev->dev, 773 dev_err(&pdev->dev,
772 "Fail to request SPORT peripherals\n"); 774 "Fail to request SPORT peripherals\n");
@@ -843,7 +845,7 @@ out_error_unmap:
843 iounmap(sport->port.membase); 845 iounmap(sport->port.membase);
844out_error_free_peripherals: 846out_error_free_peripherals:
845 peripheral_free_list( 847 peripheral_free_list(
846 (unsigned short *)pdev->dev.platform_data); 848 (unsigned short *)dev_get_platdata(&pdev->dev));
847out_error_free_mem: 849out_error_free_mem:
848 kfree(sport); 850 kfree(sport);
849 bfin_sport_uart_ports[pdev->id] = NULL; 851 bfin_sport_uart_ports[pdev->id] = NULL;
@@ -863,7 +865,7 @@ static int sport_uart_remove(struct platform_device *pdev)
863 uart_remove_one_port(&sport_uart_reg, &sport->port); 865 uart_remove_one_port(&sport_uart_reg, &sport->port);
864 iounmap(sport->port.membase); 866 iounmap(sport->port.membase);
865 peripheral_free_list( 867 peripheral_free_list(
866 (unsigned short *)pdev->dev.platform_data); 868 (unsigned short *)dev_get_platdata(&pdev->dev));
867 kfree(sport); 869 kfree(sport);
868 bfin_sport_uart_ports[pdev->id] = NULL; 870 bfin_sport_uart_ports[pdev->id] = NULL;
869 } 871 }
@@ -883,7 +885,7 @@ static struct platform_driver sport_uart_driver = {
883}; 885};
884 886
885#ifdef CONFIG_SERIAL_BFIN_SPORT_CONSOLE 887#ifdef CONFIG_SERIAL_BFIN_SPORT_CONSOLE
886static __initdata struct early_platform_driver early_sport_uart_driver = { 888static struct early_platform_driver early_sport_uart_driver __initdata = {
887 .class_str = CLASS_BFIN_SPORT_CONSOLE, 889 .class_str = CLASS_BFIN_SPORT_CONSOLE,
888 .pdrv = &sport_uart_driver, 890 .pdrv = &sport_uart_driver,
889 .requested_id = EARLY_PLATFORM_ID_UNSET, 891 .requested_id = EARLY_PLATFORM_ID_UNSET,
diff --git a/drivers/tty/serial/bfin_uart.c b/drivers/tty/serial/bfin_uart.c
index 26a3be7ced7d..3c75e8e04028 100644
--- a/drivers/tty/serial/bfin_uart.c
+++ b/drivers/tty/serial/bfin_uart.c
@@ -41,10 +41,6 @@
41# undef CONFIG_EARLY_PRINTK 41# undef CONFIG_EARLY_PRINTK
42#endif 42#endif
43 43
44#ifdef CONFIG_SERIAL_BFIN_MODULE
45# undef CONFIG_EARLY_PRINTK
46#endif
47
48/* UART name and device definitions */ 44/* UART name and device definitions */
49#define BFIN_SERIAL_DEV_NAME "ttyBF" 45#define BFIN_SERIAL_DEV_NAME "ttyBF"
50#define BFIN_SERIAL_MAJOR 204 46#define BFIN_SERIAL_MAJOR 204
@@ -1180,7 +1176,7 @@ bfin_earlyprintk_console_write(struct console *co, const char *s, unsigned int c
1180 * don't let the common infrastructure play with things. (see calls to setup 1176 * don't let the common infrastructure play with things. (see calls to setup
1181 * & earlysetup in ./kernel/printk.c:register_console() 1177 * & earlysetup in ./kernel/printk.c:register_console()
1182 */ 1178 */
1183static struct __initdata console bfin_early_serial_console = { 1179static struct console bfin_early_serial_console __initdata = {
1184 .name = "early_BFuart", 1180 .name = "early_BFuart",
1185 .write = bfin_earlyprintk_console_write, 1181 .write = bfin_earlyprintk_console_write,
1186 .device = uart_console_device, 1182 .device = uart_console_device,
@@ -1244,7 +1240,8 @@ static int bfin_serial_probe(struct platform_device *pdev)
1244 */ 1240 */
1245#endif 1241#endif
1246 ret = peripheral_request_list( 1242 ret = peripheral_request_list(
1247 (unsigned short *)pdev->dev.platform_data, DRIVER_NAME); 1243 (unsigned short *)dev_get_platdata(&pdev->dev),
1244 DRIVER_NAME);
1248 if (ret) { 1245 if (ret) {
1249 dev_err(&pdev->dev, 1246 dev_err(&pdev->dev,
1250 "fail to request bfin serial peripherals\n"); 1247 "fail to request bfin serial peripherals\n");
@@ -1362,7 +1359,7 @@ out_error_unmap:
1362 iounmap(uart->port.membase); 1359 iounmap(uart->port.membase);
1363out_error_free_peripherals: 1360out_error_free_peripherals:
1364 peripheral_free_list( 1361 peripheral_free_list(
1365 (unsigned short *)pdev->dev.platform_data); 1362 (unsigned short *)dev_get_platdata(&pdev->dev));
1366out_error_free_mem: 1363out_error_free_mem:
1367 kfree(uart); 1364 kfree(uart);
1368 bfin_serial_ports[pdev->id] = NULL; 1365 bfin_serial_ports[pdev->id] = NULL;
@@ -1381,7 +1378,7 @@ static int bfin_serial_remove(struct platform_device *pdev)
1381 uart_remove_one_port(&bfin_serial_reg, &uart->port); 1378 uart_remove_one_port(&bfin_serial_reg, &uart->port);
1382 iounmap(uart->port.membase); 1379 iounmap(uart->port.membase);
1383 peripheral_free_list( 1380 peripheral_free_list(
1384 (unsigned short *)pdev->dev.platform_data); 1381 (unsigned short *)dev_get_platdata(&pdev->dev));
1385 kfree(uart); 1382 kfree(uart);
1386 bfin_serial_ports[pdev->id] = NULL; 1383 bfin_serial_ports[pdev->id] = NULL;
1387 } 1384 }
@@ -1401,7 +1398,7 @@ static struct platform_driver bfin_serial_driver = {
1401}; 1398};
1402 1399
1403#if defined(CONFIG_SERIAL_BFIN_CONSOLE) 1400#if defined(CONFIG_SERIAL_BFIN_CONSOLE)
1404static __initdata struct early_platform_driver early_bfin_serial_driver = { 1401static struct early_platform_driver early_bfin_serial_driver __initdata = {
1405 .class_str = CLASS_BFIN_CONSOLE, 1402 .class_str = CLASS_BFIN_CONSOLE,
1406 .pdrv = &bfin_serial_driver, 1403 .pdrv = &bfin_serial_driver,
1407 .requested_id = EARLY_PLATFORM_ID_UNSET, 1404 .requested_id = EARLY_PLATFORM_ID_UNSET,
@@ -1436,7 +1433,7 @@ static int bfin_earlyprintk_probe(struct platform_device *pdev)
1436 } 1433 }
1437 1434
1438 ret = peripheral_request_list( 1435 ret = peripheral_request_list(
1439 (unsigned short *)pdev->dev.platform_data, DRIVER_NAME); 1436 (unsigned short *)dev_get_platdata(&pdev->dev), DRIVER_NAME);
1440 if (ret) { 1437 if (ret) {
1441 dev_err(&pdev->dev, 1438 dev_err(&pdev->dev,
1442 "fail to request bfin serial peripherals\n"); 1439 "fail to request bfin serial peripherals\n");
@@ -1467,7 +1464,7 @@ static int bfin_earlyprintk_probe(struct platform_device *pdev)
1467 1464
1468out_error_free_peripherals: 1465out_error_free_peripherals:
1469 peripheral_free_list( 1466 peripheral_free_list(
1470 (unsigned short *)pdev->dev.platform_data); 1467 (unsigned short *)dev_get_platdata(&pdev->dev));
1471 1468
1472 return ret; 1469 return ret;
1473} 1470}
@@ -1480,7 +1477,7 @@ static struct platform_driver bfin_earlyprintk_driver = {
1480 }, 1477 },
1481}; 1478};
1482 1479
1483static __initdata struct early_platform_driver early_bfin_earlyprintk_driver = { 1480static struct early_platform_driver early_bfin_earlyprintk_driver __initdata = {
1484 .class_str = CLASS_BFIN_EARLYPRINTK, 1481 .class_str = CLASS_BFIN_EARLYPRINTK,
1485 .pdrv = &bfin_earlyprintk_driver, 1482 .pdrv = &bfin_earlyprintk_driver,
1486 .requested_id = EARLY_PLATFORM_ID_UNSET, 1483 .requested_id = EARLY_PLATFORM_ID_UNSET,
diff --git a/drivers/tty/serial/clps711x.c b/drivers/tty/serial/clps711x.c
index bfb17968c8db..7e4e4088471c 100644
--- a/drivers/tty/serial/clps711x.c
+++ b/drivers/tty/serial/clps711x.c
@@ -438,8 +438,7 @@ static int uart_clps711x_probe(struct platform_device *pdev)
438 s->uart_clk = devm_clk_get(&pdev->dev, "uart"); 438 s->uart_clk = devm_clk_get(&pdev->dev, "uart");
439 if (IS_ERR(s->uart_clk)) { 439 if (IS_ERR(s->uart_clk)) {
440 dev_err(&pdev->dev, "Can't get UART clocks\n"); 440 dev_err(&pdev->dev, "Can't get UART clocks\n");
441 ret = PTR_ERR(s->uart_clk); 441 return PTR_ERR(s->uart_clk);
442 goto err_out;
443 } 442 }
444 443
445 s->uart.owner = THIS_MODULE; 444 s->uart.owner = THIS_MODULE;
@@ -461,7 +460,7 @@ static int uart_clps711x_probe(struct platform_device *pdev)
461 if (ret) { 460 if (ret) {
462 dev_err(&pdev->dev, "Registering UART driver failed\n"); 461 dev_err(&pdev->dev, "Registering UART driver failed\n");
463 devm_clk_put(&pdev->dev, s->uart_clk); 462 devm_clk_put(&pdev->dev, s->uart_clk);
464 goto err_out; 463 return ret;
465 } 464 }
466 465
467 for (i = 0; i < UART_CLPS711X_NR; i++) { 466 for (i = 0; i < UART_CLPS711X_NR; i++) {
@@ -478,11 +477,6 @@ static int uart_clps711x_probe(struct platform_device *pdev)
478 } 477 }
479 478
480 return 0; 479 return 0;
481
482err_out:
483 platform_set_drvdata(pdev, NULL);
484
485 return ret;
486} 480}
487 481
488static int uart_clps711x_remove(struct platform_device *pdev) 482static int uart_clps711x_remove(struct platform_device *pdev)
@@ -495,7 +489,6 @@ static int uart_clps711x_remove(struct platform_device *pdev)
495 489
496 devm_clk_put(&pdev->dev, s->uart_clk); 490 devm_clk_put(&pdev->dev, s->uart_clk);
497 uart_unregister_driver(&s->uart); 491 uart_unregister_driver(&s->uart);
498 platform_set_drvdata(pdev, NULL);
499 492
500 return 0; 493 return 0;
501} 494}
diff --git a/drivers/tty/serial/cpm_uart/cpm_uart_core.c b/drivers/tty/serial/cpm_uart/cpm_uart_core.c
index f7672cae5321..1a535f70dc41 100644
--- a/drivers/tty/serial/cpm_uart/cpm_uart_core.c
+++ b/drivers/tty/serial/cpm_uart/cpm_uart_core.c
@@ -1213,8 +1213,32 @@ static int cpm_uart_init_port(struct device_node *np,
1213 goto out_pram; 1213 goto out_pram;
1214 } 1214 }
1215 1215
1216 for (i = 0; i < NUM_GPIOS; i++) 1216 for (i = 0; i < NUM_GPIOS; i++) {
1217 pinfo->gpios[i] = of_get_gpio(np, i); 1217 int gpio;
1218
1219 pinfo->gpios[i] = -1;
1220
1221 gpio = of_get_gpio(np, i);
1222
1223 if (gpio_is_valid(gpio)) {
1224 ret = gpio_request(gpio, "cpm_uart");
1225 if (ret) {
1226 pr_err("can't request gpio #%d: %d\n", i, ret);
1227 continue;
1228 }
1229 if (i == GPIO_RTS || i == GPIO_DTR)
1230 ret = gpio_direction_output(gpio, 0);
1231 else
1232 ret = gpio_direction_input(gpio);
1233 if (ret) {
1234 pr_err("can't set direction for gpio #%d: %d\n",
1235 i, ret);
1236 gpio_free(gpio);
1237 continue;
1238 }
1239 pinfo->gpios[i] = gpio;
1240 }
1241 }
1218 1242
1219#ifdef CONFIG_PPC_EARLY_DEBUG_CPM 1243#ifdef CONFIG_PPC_EARLY_DEBUG_CPM
1220 udbg_putc = NULL; 1244 udbg_putc = NULL;
diff --git a/drivers/tty/serial/efm32-uart.c b/drivers/tty/serial/efm32-uart.c
index 7d199c8e1a75..0eb5b5673ede 100644
--- a/drivers/tty/serial/efm32-uart.c
+++ b/drivers/tty/serial/efm32-uart.c
@@ -268,10 +268,10 @@ static irqreturn_t efm32_uart_rxirq(int irq, void *data)
268 handled = IRQ_HANDLED; 268 handled = IRQ_HANDLED;
269 } 269 }
270 270
271 tty_flip_buffer_push(tport);
272
273 spin_unlock(&port->lock); 271 spin_unlock(&port->lock);
274 272
273 tty_flip_buffer_push(tport);
274
275 return handled; 275 return handled;
276} 276}
277 277
@@ -698,6 +698,7 @@ static int efm32_uart_probe(struct platform_device *pdev)
698{ 698{
699 struct efm32_uart_port *efm_port; 699 struct efm32_uart_port *efm_port;
700 struct resource *res; 700 struct resource *res;
701 unsigned int line;
701 int ret; 702 int ret;
702 703
703 efm_port = kzalloc(sizeof(*efm_port), GFP_KERNEL); 704 efm_port = kzalloc(sizeof(*efm_port), GFP_KERNEL);
@@ -750,18 +751,21 @@ static int efm32_uart_probe(struct platform_device *pdev)
750 751
751 if (pdata) 752 if (pdata)
752 efm_port->pdata = *pdata; 753 efm_port->pdata = *pdata;
753 } 754 } else if (ret < 0)
755 goto err_probe_dt;
756
757 line = efm_port->port.line;
754 758
755 if (efm_port->port.line >= 0 && 759 if (line >= 0 && line < ARRAY_SIZE(efm32_uart_ports))
756 efm_port->port.line < ARRAY_SIZE(efm32_uart_ports)) 760 efm32_uart_ports[line] = efm_port;
757 efm32_uart_ports[efm_port->port.line] = efm_port;
758 761
759 ret = uart_add_one_port(&efm32_uart_reg, &efm_port->port); 762 ret = uart_add_one_port(&efm32_uart_reg, &efm_port->port);
760 if (ret) { 763 if (ret) {
761 dev_dbg(&pdev->dev, "failed to add port: %d\n", ret); 764 dev_dbg(&pdev->dev, "failed to add port: %d\n", ret);
762 765
763 if (pdev->id >= 0 && pdev->id < ARRAY_SIZE(efm32_uart_ports)) 766 if (line >= 0 && line < ARRAY_SIZE(efm32_uart_ports))
764 efm32_uart_ports[pdev->id] = NULL; 767 efm32_uart_ports[line] = NULL;
768err_probe_dt:
765err_get_rxirq: 769err_get_rxirq:
766err_too_small: 770err_too_small:
767err_get_base: 771err_get_base:
@@ -777,20 +781,19 @@ err_get_base:
777static int efm32_uart_remove(struct platform_device *pdev) 781static int efm32_uart_remove(struct platform_device *pdev)
778{ 782{
779 struct efm32_uart_port *efm_port = platform_get_drvdata(pdev); 783 struct efm32_uart_port *efm_port = platform_get_drvdata(pdev);
780 784 unsigned int line = efm_port->port.line;
781 platform_set_drvdata(pdev, NULL);
782 785
783 uart_remove_one_port(&efm32_uart_reg, &efm_port->port); 786 uart_remove_one_port(&efm32_uart_reg, &efm_port->port);
784 787
785 if (pdev->id >= 0 && pdev->id < ARRAY_SIZE(efm32_uart_ports)) 788 if (line >= 0 && line < ARRAY_SIZE(efm32_uart_ports))
786 efm32_uart_ports[pdev->id] = NULL; 789 efm32_uart_ports[line] = NULL;
787 790
788 kfree(efm_port); 791 kfree(efm_port);
789 792
790 return 0; 793 return 0;
791} 794}
792 795
793static struct of_device_id efm32_uart_dt_ids[] = { 796static const struct of_device_id efm32_uart_dt_ids[] = {
794 { 797 {
795 .compatible = "efm32,uart", 798 .compatible = "efm32,uart",
796 }, { 799 }, {
diff --git a/drivers/tty/serial/fsl_lpuart.c b/drivers/tty/serial/fsl_lpuart.c
index 263cfaabe9e2..8978dc9a58b7 100644
--- a/drivers/tty/serial/fsl_lpuart.c
+++ b/drivers/tty/serial/fsl_lpuart.c
@@ -342,8 +342,10 @@ static void lpuart_break_ctl(struct uart_port *port, int break_state)
342static void lpuart_setup_watermark(struct lpuart_port *sport) 342static void lpuart_setup_watermark(struct lpuart_port *sport)
343{ 343{
344 unsigned char val, cr2; 344 unsigned char val, cr2;
345 unsigned char cr2_saved;
345 346
346 cr2 = readb(sport->port.membase + UARTCR2); 347 cr2 = readb(sport->port.membase + UARTCR2);
348 cr2_saved = cr2;
347 cr2 &= ~(UARTCR2_TIE | UARTCR2_TCIE | UARTCR2_TE | 349 cr2 &= ~(UARTCR2_TIE | UARTCR2_TCIE | UARTCR2_TE |
348 UARTCR2_RIE | UARTCR2_RE); 350 UARTCR2_RIE | UARTCR2_RE);
349 writeb(cr2, sport->port.membase + UARTCR2); 351 writeb(cr2, sport->port.membase + UARTCR2);
@@ -366,6 +368,9 @@ static void lpuart_setup_watermark(struct lpuart_port *sport)
366 368
367 writeb(2, sport->port.membase + UARTTWFIFO); 369 writeb(2, sport->port.membase + UARTTWFIFO);
368 writeb(1, sport->port.membase + UARTRWFIFO); 370 writeb(1, sport->port.membase + UARTRWFIFO);
371
372 /* Restore cr2 */
373 writeb(cr2_saved, sport->port.membase + UARTCR2);
369} 374}
370 375
371static int lpuart_startup(struct uart_port *port) 376static int lpuart_startup(struct uart_port *port)
@@ -858,7 +863,7 @@ static int __init lpuart_serial_init(void)
858 if (ret) 863 if (ret)
859 uart_unregister_driver(&lpuart_reg); 864 uart_unregister_driver(&lpuart_reg);
860 865
861 return 0; 866 return ret;
862} 867}
863 868
864static void __exit lpuart_serial_exit(void) 869static void __exit lpuart_serial_exit(void)
diff --git a/drivers/tty/serial/icom.c b/drivers/tty/serial/icom.c
index 18ed5aebb166..d98e43348970 100644
--- a/drivers/tty/serial/icom.c
+++ b/drivers/tty/serial/icom.c
@@ -105,7 +105,7 @@ static const struct pci_device_id icom_pci_table[] = {
105 {} 105 {}
106}; 106};
107 107
108struct lookup_proc_table start_proc[4] = { 108static struct lookup_proc_table start_proc[4] = {
109 {NULL, ICOM_CONTROL_START_A}, 109 {NULL, ICOM_CONTROL_START_A},
110 {NULL, ICOM_CONTROL_START_B}, 110 {NULL, ICOM_CONTROL_START_B},
111 {NULL, ICOM_CONTROL_START_C}, 111 {NULL, ICOM_CONTROL_START_C},
@@ -113,14 +113,14 @@ struct lookup_proc_table start_proc[4] = {
113}; 113};
114 114
115 115
116struct lookup_proc_table stop_proc[4] = { 116static struct lookup_proc_table stop_proc[4] = {
117 {NULL, ICOM_CONTROL_STOP_A}, 117 {NULL, ICOM_CONTROL_STOP_A},
118 {NULL, ICOM_CONTROL_STOP_B}, 118 {NULL, ICOM_CONTROL_STOP_B},
119 {NULL, ICOM_CONTROL_STOP_C}, 119 {NULL, ICOM_CONTROL_STOP_C},
120 {NULL, ICOM_CONTROL_STOP_D} 120 {NULL, ICOM_CONTROL_STOP_D}
121}; 121};
122 122
123struct lookup_int_table int_mask_tbl[4] = { 123static struct lookup_int_table int_mask_tbl[4] = {
124 {NULL, ICOM_INT_MASK_PRC_A}, 124 {NULL, ICOM_INT_MASK_PRC_A},
125 {NULL, ICOM_INT_MASK_PRC_B}, 125 {NULL, ICOM_INT_MASK_PRC_B},
126 {NULL, ICOM_INT_MASK_PRC_C}, 126 {NULL, ICOM_INT_MASK_PRC_C},
@@ -297,25 +297,25 @@ static void stop_processor(struct icom_port *icom_port)
297 spin_lock_irqsave(&icom_lock, flags); 297 spin_lock_irqsave(&icom_lock, flags);
298 298
299 port = icom_port->port; 299 port = icom_port->port;
300 if (port >= ARRAY_SIZE(stop_proc)) {
301 dev_err(&icom_port->adapter->pci_dev->dev,
302 "Invalid port assignment\n");
303 goto unlock;
304 }
305
300 if (port == 0 || port == 1) 306 if (port == 0 || port == 1)
301 stop_proc[port].global_control_reg = &icom_port->global_reg->control; 307 stop_proc[port].global_control_reg = &icom_port->global_reg->control;
302 else 308 else
303 stop_proc[port].global_control_reg = &icom_port->global_reg->control_2; 309 stop_proc[port].global_control_reg = &icom_port->global_reg->control_2;
304 310
311 temp = readl(stop_proc[port].global_control_reg);
312 temp = (temp & ~start_proc[port].processor_id) | stop_proc[port].processor_id;
313 writel(temp, stop_proc[port].global_control_reg);
305 314
306 if (port < 4) { 315 /* write flush */
307 temp = readl(stop_proc[port].global_control_reg); 316 readl(stop_proc[port].global_control_reg);
308 temp =
309 (temp & ~start_proc[port].processor_id) | stop_proc[port].processor_id;
310 writel(temp, stop_proc[port].global_control_reg);
311
312 /* write flush */
313 readl(stop_proc[port].global_control_reg);
314 } else {
315 dev_err(&icom_port->adapter->pci_dev->dev,
316 "Invalid port assignment\n");
317 }
318 317
318unlock:
319 spin_unlock_irqrestore(&icom_lock, flags); 319 spin_unlock_irqrestore(&icom_lock, flags);
320} 320}
321 321
@@ -328,23 +328,25 @@ static void start_processor(struct icom_port *icom_port)
328 spin_lock_irqsave(&icom_lock, flags); 328 spin_lock_irqsave(&icom_lock, flags);
329 329
330 port = icom_port->port; 330 port = icom_port->port;
331 if (port >= ARRAY_SIZE(start_proc)) {
332 dev_err(&icom_port->adapter->pci_dev->dev,
333 "Invalid port assignment\n");
334 goto unlock;
335 }
336
331 if (port == 0 || port == 1) 337 if (port == 0 || port == 1)
332 start_proc[port].global_control_reg = &icom_port->global_reg->control; 338 start_proc[port].global_control_reg = &icom_port->global_reg->control;
333 else 339 else
334 start_proc[port].global_control_reg = &icom_port->global_reg->control_2; 340 start_proc[port].global_control_reg = &icom_port->global_reg->control_2;
335 if (port < 4) {
336 temp = readl(start_proc[port].global_control_reg);
337 temp =
338 (temp & ~stop_proc[port].processor_id) | start_proc[port].processor_id;
339 writel(temp, start_proc[port].global_control_reg);
340 341
341 /* write flush */ 342 temp = readl(start_proc[port].global_control_reg);
342 readl(start_proc[port].global_control_reg); 343 temp = (temp & ~stop_proc[port].processor_id) | start_proc[port].processor_id;
343 } else { 344 writel(temp, start_proc[port].global_control_reg);
344 dev_err(&icom_port->adapter->pci_dev->dev, 345
345 "Invalid port assignment\n"); 346 /* write flush */
346 } 347 readl(start_proc[port].global_control_reg);
347 348
349unlock:
348 spin_unlock_irqrestore(&icom_lock, flags); 350 spin_unlock_irqrestore(&icom_lock, flags);
349} 351}
350 352
@@ -557,6 +559,12 @@ static int startup(struct icom_port *icom_port)
557 */ 559 */
558 spin_lock_irqsave(&icom_lock, flags); 560 spin_lock_irqsave(&icom_lock, flags);
559 port = icom_port->port; 561 port = icom_port->port;
562 if (port >= ARRAY_SIZE(int_mask_tbl)) {
563 dev_err(&icom_port->adapter->pci_dev->dev,
564 "Invalid port assignment\n");
565 goto unlock;
566 }
567
560 if (port == 0 || port == 1) 568 if (port == 0 || port == 1)
561 int_mask_tbl[port].global_int_mask = &icom_port->global_reg->int_mask; 569 int_mask_tbl[port].global_int_mask = &icom_port->global_reg->int_mask;
562 else 570 else
@@ -566,17 +574,14 @@ static int startup(struct icom_port *icom_port)
566 writew(0x00FF, icom_port->int_reg); 574 writew(0x00FF, icom_port->int_reg);
567 else 575 else
568 writew(0x3F00, icom_port->int_reg); 576 writew(0x3F00, icom_port->int_reg);
569 if (port < 4) {
570 temp = readl(int_mask_tbl[port].global_int_mask);
571 writel(temp & ~int_mask_tbl[port].processor_id, int_mask_tbl[port].global_int_mask);
572 577
573 /* write flush */ 578 temp = readl(int_mask_tbl[port].global_int_mask);
574 readl(int_mask_tbl[port].global_int_mask); 579 writel(temp & ~int_mask_tbl[port].processor_id, int_mask_tbl[port].global_int_mask);
575 } else {
576 dev_err(&icom_port->adapter->pci_dev->dev,
577 "Invalid port assignment\n");
578 }
579 580
581 /* write flush */
582 readl(int_mask_tbl[port].global_int_mask);
583
584unlock:
580 spin_unlock_irqrestore(&icom_lock, flags); 585 spin_unlock_irqrestore(&icom_lock, flags);
581 return 0; 586 return 0;
582} 587}
@@ -595,21 +600,23 @@ static void shutdown(struct icom_port *icom_port)
595 * disable all interrupts 600 * disable all interrupts
596 */ 601 */
597 port = icom_port->port; 602 port = icom_port->port;
603 if (port >= ARRAY_SIZE(int_mask_tbl)) {
604 dev_err(&icom_port->adapter->pci_dev->dev,
605 "Invalid port assignment\n");
606 goto unlock;
607 }
598 if (port == 0 || port == 1) 608 if (port == 0 || port == 1)
599 int_mask_tbl[port].global_int_mask = &icom_port->global_reg->int_mask; 609 int_mask_tbl[port].global_int_mask = &icom_port->global_reg->int_mask;
600 else 610 else
601 int_mask_tbl[port].global_int_mask = &icom_port->global_reg->int_mask_2; 611 int_mask_tbl[port].global_int_mask = &icom_port->global_reg->int_mask_2;
602 612
603 if (port < 4) { 613 temp = readl(int_mask_tbl[port].global_int_mask);
604 temp = readl(int_mask_tbl[port].global_int_mask); 614 writel(temp | int_mask_tbl[port].processor_id, int_mask_tbl[port].global_int_mask);
605 writel(temp | int_mask_tbl[port].processor_id, int_mask_tbl[port].global_int_mask);
606 615
607 /* write flush */ 616 /* write flush */
608 readl(int_mask_tbl[port].global_int_mask); 617 readl(int_mask_tbl[port].global_int_mask);
609 } else { 618
610 dev_err(&icom_port->adapter->pci_dev->dev, 619unlock:
611 "Invalid port assignment\n");
612 }
613 spin_unlock_irqrestore(&icom_lock, flags); 620 spin_unlock_irqrestore(&icom_lock, flags);
614 621
615 /* 622 /*
@@ -834,7 +841,10 @@ ignore_char:
834 status = cpu_to_le16(icom_port->statStg->rcv[rcv_buff].flags); 841 status = cpu_to_le16(icom_port->statStg->rcv[rcv_buff].flags);
835 } 842 }
836 icom_port->next_rcv = rcv_buff; 843 icom_port->next_rcv = rcv_buff;
844
845 spin_unlock(&icom_port->uart_port.lock);
837 tty_flip_buffer_push(port); 846 tty_flip_buffer_push(port);
847 spin_lock(&icom_port->uart_port.lock);
838} 848}
839 849
840static void process_interrupt(u16 port_int_reg, 850static void process_interrupt(u16 port_int_reg,
@@ -1087,8 +1097,7 @@ static void icom_close(struct uart_port *port)
1087 1097
1088 /* stop receiver */ 1098 /* stop receiver */
1089 cmdReg = readb(&ICOM_PORT->dram->CmdReg); 1099 cmdReg = readb(&ICOM_PORT->dram->CmdReg);
1090 writeb(cmdReg & (unsigned char) ~CMD_RCV_ENABLE, 1100 writeb(cmdReg & ~CMD_RCV_ENABLE, &ICOM_PORT->dram->CmdReg);
1091 &ICOM_PORT->dram->CmdReg);
1092 1101
1093 shutdown(ICOM_PORT); 1102 shutdown(ICOM_PORT);
1094 1103
@@ -1567,7 +1576,7 @@ static int icom_probe(struct pci_dev *dev,
1567 icom_port->uart_port.type = PORT_ICOM; 1576 icom_port->uart_port.type = PORT_ICOM;
1568 icom_port->uart_port.iotype = UPIO_MEM; 1577 icom_port->uart_port.iotype = UPIO_MEM;
1569 icom_port->uart_port.membase = 1578 icom_port->uart_port.membase =
1570 (char *) icom_adapter->base_addr_pci; 1579 (unsigned char __iomem *)icom_adapter->base_addr_pci;
1571 icom_port->uart_port.fifosize = 16; 1580 icom_port->uart_port.fifosize = 16;
1572 icom_port->uart_port.ops = &icom_ops; 1581 icom_port->uart_port.ops = &icom_ops;
1573 icom_port->uart_port.line = 1582 icom_port->uart_port.line =
diff --git a/drivers/tty/serial/ifx6x60.c b/drivers/tty/serial/ifx6x60.c
index 8b1534c424af..af286e6713eb 100644
--- a/drivers/tty/serial/ifx6x60.c
+++ b/drivers/tty/serial/ifx6x60.c
@@ -1008,7 +1008,7 @@ static int ifx_spi_spi_probe(struct spi_device *spi)
1008 return -ENODEV; 1008 return -ENODEV;
1009 } 1009 }
1010 1010
1011 pl_data = (struct ifx_modem_platform_data *)spi->dev.platform_data; 1011 pl_data = (struct ifx_modem_platform_data *)dev_get_platdata(&spi->dev);
1012 if (!pl_data) { 1012 if (!pl_data) {
1013 dev_err(&spi->dev, "missing platform data!"); 1013 dev_err(&spi->dev, "missing platform data!");
1014 return -ENODEV; 1014 return -ENODEV;
diff --git a/drivers/tty/serial/imx.c b/drivers/tty/serial/imx.c
index 415cec62073f..a0ebbc9ce5cd 100644
--- a/drivers/tty/serial/imx.c
+++ b/drivers/tty/serial/imx.c
@@ -47,11 +47,12 @@
47#include <linux/slab.h> 47#include <linux/slab.h>
48#include <linux/of.h> 48#include <linux/of.h>
49#include <linux/of_device.h> 49#include <linux/of_device.h>
50#include <linux/pinctrl/consumer.h>
51#include <linux/io.h> 50#include <linux/io.h>
51#include <linux/dma-mapping.h>
52 52
53#include <asm/irq.h> 53#include <asm/irq.h>
54#include <linux/platform_data/serial-imx.h> 54#include <linux/platform_data/serial-imx.h>
55#include <linux/platform_data/dma-imx.h>
55 56
56/* Register definitions */ 57/* Register definitions */
57#define URXD0 0x0 /* Receiver Register */ 58#define URXD0 0x0 /* Receiver Register */
@@ -83,6 +84,7 @@
83#define UCR1_ADBR (1<<14) /* Auto detect baud rate */ 84#define UCR1_ADBR (1<<14) /* Auto detect baud rate */
84#define UCR1_TRDYEN (1<<13) /* Transmitter ready interrupt enable */ 85#define UCR1_TRDYEN (1<<13) /* Transmitter ready interrupt enable */
85#define UCR1_IDEN (1<<12) /* Idle condition interrupt */ 86#define UCR1_IDEN (1<<12) /* Idle condition interrupt */
87#define UCR1_ICD_REG(x) (((x) & 3) << 10) /* idle condition detect */
86#define UCR1_RRDYEN (1<<9) /* Recv ready interrupt enable */ 88#define UCR1_RRDYEN (1<<9) /* Recv ready interrupt enable */
87#define UCR1_RDMAEN (1<<8) /* Recv ready DMA enable */ 89#define UCR1_RDMAEN (1<<8) /* Recv ready DMA enable */
88#define UCR1_IREN (1<<7) /* Infrared interface enable */ 90#define UCR1_IREN (1<<7) /* Infrared interface enable */
@@ -91,6 +93,7 @@
91#define UCR1_SNDBRK (1<<4) /* Send break */ 93#define UCR1_SNDBRK (1<<4) /* Send break */
92#define UCR1_TDMAEN (1<<3) /* Transmitter ready DMA enable */ 94#define UCR1_TDMAEN (1<<3) /* Transmitter ready DMA enable */
93#define IMX1_UCR1_UARTCLKEN (1<<2) /* UART clock enabled, i.mx1 only */ 95#define IMX1_UCR1_UARTCLKEN (1<<2) /* UART clock enabled, i.mx1 only */
96#define UCR1_ATDMAEN (1<<2) /* Aging DMA Timer Enable */
94#define UCR1_DOZE (1<<1) /* Doze */ 97#define UCR1_DOZE (1<<1) /* Doze */
95#define UCR1_UARTEN (1<<0) /* UART enabled */ 98#define UCR1_UARTEN (1<<0) /* UART enabled */
96#define UCR2_ESCI (1<<15) /* Escape seq interrupt enable */ 99#define UCR2_ESCI (1<<15) /* Escape seq interrupt enable */
@@ -126,6 +129,7 @@
126#define UCR4_ENIRI (1<<8) /* Serial infrared interrupt enable */ 129#define UCR4_ENIRI (1<<8) /* Serial infrared interrupt enable */
127#define UCR4_WKEN (1<<7) /* Wake interrupt enable */ 130#define UCR4_WKEN (1<<7) /* Wake interrupt enable */
128#define UCR4_REF16 (1<<6) /* Ref freq 16 MHz */ 131#define UCR4_REF16 (1<<6) /* Ref freq 16 MHz */
132#define UCR4_IDDMAEN (1<<6) /* DMA IDLE Condition Detected */
129#define UCR4_IRSC (1<<5) /* IR special case */ 133#define UCR4_IRSC (1<<5) /* IR special case */
130#define UCR4_TCEN (1<<3) /* Transmit complete interrupt enable */ 134#define UCR4_TCEN (1<<3) /* Transmit complete interrupt enable */
131#define UCR4_BKEN (1<<2) /* Break condition interrupt enable */ 135#define UCR4_BKEN (1<<2) /* Break condition interrupt enable */
@@ -187,6 +191,7 @@
187enum imx_uart_type { 191enum imx_uart_type {
188 IMX1_UART, 192 IMX1_UART,
189 IMX21_UART, 193 IMX21_UART,
194 IMX6Q_UART,
190}; 195};
191 196
192/* device type dependent stuff */ 197/* device type dependent stuff */
@@ -209,6 +214,19 @@ struct imx_port {
209 struct clk *clk_ipg; 214 struct clk *clk_ipg;
210 struct clk *clk_per; 215 struct clk *clk_per;
211 const struct imx_uart_data *devdata; 216 const struct imx_uart_data *devdata;
217
218 /* DMA fields */
219 unsigned int dma_is_inited:1;
220 unsigned int dma_is_enabled:1;
221 unsigned int dma_is_rxing:1;
222 unsigned int dma_is_txing:1;
223 struct dma_chan *dma_chan_rx, *dma_chan_tx;
224 struct scatterlist rx_sgl, tx_sgl[2];
225 void *rx_buf;
226 unsigned int rx_bytes, tx_bytes;
227 struct work_struct tsk_dma_rx, tsk_dma_tx;
228 unsigned int dma_tx_nents;
229 wait_queue_head_t dma_wait;
212}; 230};
213 231
214struct imx_port_ucrs { 232struct imx_port_ucrs {
@@ -232,6 +250,10 @@ static struct imx_uart_data imx_uart_devdata[] = {
232 .uts_reg = IMX21_UTS, 250 .uts_reg = IMX21_UTS,
233 .devtype = IMX21_UART, 251 .devtype = IMX21_UART,
234 }, 252 },
253 [IMX6Q_UART] = {
254 .uts_reg = IMX21_UTS,
255 .devtype = IMX6Q_UART,
256 },
235}; 257};
236 258
237static struct platform_device_id imx_uart_devtype[] = { 259static struct platform_device_id imx_uart_devtype[] = {
@@ -242,12 +264,16 @@ static struct platform_device_id imx_uart_devtype[] = {
242 .name = "imx21-uart", 264 .name = "imx21-uart",
243 .driver_data = (kernel_ulong_t) &imx_uart_devdata[IMX21_UART], 265 .driver_data = (kernel_ulong_t) &imx_uart_devdata[IMX21_UART],
244 }, { 266 }, {
267 .name = "imx6q-uart",
268 .driver_data = (kernel_ulong_t) &imx_uart_devdata[IMX6Q_UART],
269 }, {
245 /* sentinel */ 270 /* sentinel */
246 } 271 }
247}; 272};
248MODULE_DEVICE_TABLE(platform, imx_uart_devtype); 273MODULE_DEVICE_TABLE(platform, imx_uart_devtype);
249 274
250static struct of_device_id imx_uart_dt_ids[] = { 275static struct of_device_id imx_uart_dt_ids[] = {
276 { .compatible = "fsl,imx6q-uart", .data = &imx_uart_devdata[IMX6Q_UART], },
251 { .compatible = "fsl,imx1-uart", .data = &imx_uart_devdata[IMX1_UART], }, 277 { .compatible = "fsl,imx1-uart", .data = &imx_uart_devdata[IMX1_UART], },
252 { .compatible = "fsl,imx21-uart", .data = &imx_uart_devdata[IMX21_UART], }, 278 { .compatible = "fsl,imx21-uart", .data = &imx_uart_devdata[IMX21_UART], },
253 { /* sentinel */ } 279 { /* sentinel */ }
@@ -269,6 +295,10 @@ static inline int is_imx21_uart(struct imx_port *sport)
269 return sport->devdata->devtype == IMX21_UART; 295 return sport->devdata->devtype == IMX21_UART;
270} 296}
271 297
298static inline int is_imx6q_uart(struct imx_port *sport)
299{
300 return sport->devdata->devtype == IMX6Q_UART;
301}
272/* 302/*
273 * Save and restore functions for UCR1, UCR2 and UCR3 registers 303 * Save and restore functions for UCR1, UCR2 and UCR3 registers
274 */ 304 */
@@ -387,6 +417,13 @@ static void imx_stop_tx(struct uart_port *port)
387 return; 417 return;
388 } 418 }
389 419
420 /*
421 * We are maybe in the SMP context, so if the DMA TX thread is running
422 * on other cpu, we have to wait for it to finish.
423 */
424 if (sport->dma_is_enabled && sport->dma_is_txing)
425 return;
426
390 temp = readl(sport->port.membase + UCR1); 427 temp = readl(sport->port.membase + UCR1);
391 writel(temp & ~UCR1_TXMPTYEN, sport->port.membase + UCR1); 428 writel(temp & ~UCR1_TXMPTYEN, sport->port.membase + UCR1);
392} 429}
@@ -399,6 +436,13 @@ static void imx_stop_rx(struct uart_port *port)
399 struct imx_port *sport = (struct imx_port *)port; 436 struct imx_port *sport = (struct imx_port *)port;
400 unsigned long temp; 437 unsigned long temp;
401 438
439 /*
440 * We are maybe in the SMP context, so if the DMA TX thread is running
441 * on other cpu, we have to wait for it to finish.
442 */
443 if (sport->dma_is_enabled && sport->dma_is_rxing)
444 return;
445
402 temp = readl(sport->port.membase + UCR2); 446 temp = readl(sport->port.membase + UCR2);
403 writel(temp & ~UCR2_RXEN, sport->port.membase + UCR2); 447 writel(temp & ~UCR2_RXEN, sport->port.membase + UCR2);
404} 448}
@@ -434,6 +478,95 @@ static inline void imx_transmit_buffer(struct imx_port *sport)
434 imx_stop_tx(&sport->port); 478 imx_stop_tx(&sport->port);
435} 479}
436 480
481static void dma_tx_callback(void *data)
482{
483 struct imx_port *sport = data;
484 struct scatterlist *sgl = &sport->tx_sgl[0];
485 struct circ_buf *xmit = &sport->port.state->xmit;
486 unsigned long flags;
487
488 dma_unmap_sg(sport->port.dev, sgl, sport->dma_tx_nents, DMA_TO_DEVICE);
489
490 sport->dma_is_txing = 0;
491
492 /* update the stat */
493 spin_lock_irqsave(&sport->port.lock, flags);
494 xmit->tail = (xmit->tail + sport->tx_bytes) & (UART_XMIT_SIZE - 1);
495 sport->port.icount.tx += sport->tx_bytes;
496 spin_unlock_irqrestore(&sport->port.lock, flags);
497
498 dev_dbg(sport->port.dev, "we finish the TX DMA.\n");
499
500 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
501 uart_write_wakeup(&sport->port);
502
503 if (waitqueue_active(&sport->dma_wait)) {
504 wake_up(&sport->dma_wait);
505 dev_dbg(sport->port.dev, "exit in %s.\n", __func__);
506 return;
507 }
508
509 schedule_work(&sport->tsk_dma_tx);
510}
511
512static void dma_tx_work(struct work_struct *w)
513{
514 struct imx_port *sport = container_of(w, struct imx_port, tsk_dma_tx);
515 struct circ_buf *xmit = &sport->port.state->xmit;
516 struct scatterlist *sgl = sport->tx_sgl;
517 struct dma_async_tx_descriptor *desc;
518 struct dma_chan *chan = sport->dma_chan_tx;
519 struct device *dev = sport->port.dev;
520 enum dma_status status;
521 unsigned long flags;
522 int ret;
523
524 status = chan->device->device_tx_status(chan, (dma_cookie_t)0, NULL);
525 if (DMA_IN_PROGRESS == status)
526 return;
527
528 spin_lock_irqsave(&sport->port.lock, flags);
529 sport->tx_bytes = uart_circ_chars_pending(xmit);
530 if (sport->tx_bytes == 0) {
531 spin_unlock_irqrestore(&sport->port.lock, flags);
532 return;
533 }
534
535 if (xmit->tail > xmit->head) {
536 sport->dma_tx_nents = 2;
537 sg_init_table(sgl, 2);
538 sg_set_buf(sgl, xmit->buf + xmit->tail,
539 UART_XMIT_SIZE - xmit->tail);
540 sg_set_buf(sgl + 1, xmit->buf, xmit->head);
541 } else {
542 sport->dma_tx_nents = 1;
543 sg_init_one(sgl, xmit->buf + xmit->tail, sport->tx_bytes);
544 }
545 spin_unlock_irqrestore(&sport->port.lock, flags);
546
547 ret = dma_map_sg(dev, sgl, sport->dma_tx_nents, DMA_TO_DEVICE);
548 if (ret == 0) {
549 dev_err(dev, "DMA mapping error for TX.\n");
550 return;
551 }
552 desc = dmaengine_prep_slave_sg(chan, sgl, sport->dma_tx_nents,
553 DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT);
554 if (!desc) {
555 dev_err(dev, "We cannot prepare for the TX slave dma!\n");
556 return;
557 }
558 desc->callback = dma_tx_callback;
559 desc->callback_param = sport;
560
561 dev_dbg(dev, "TX: prepare to send %lu bytes by DMA.\n",
562 uart_circ_chars_pending(xmit));
563 /* fire it */
564 sport->dma_is_txing = 1;
565 dmaengine_submit(desc);
566 dma_async_issue_pending(chan);
567 return;
568}
569
437/* 570/*
438 * interrupts disabled on entry 571 * interrupts disabled on entry
439 */ 572 */
@@ -460,8 +593,10 @@ static void imx_start_tx(struct uart_port *port)
460 temp |= UCR4_OREN; 593 temp |= UCR4_OREN;
461 writel(temp, sport->port.membase + UCR4); 594 writel(temp, sport->port.membase + UCR4);
462 595
463 temp = readl(sport->port.membase + UCR1); 596 if (!sport->dma_is_enabled) {
464 writel(temp | UCR1_TXMPTYEN, sport->port.membase + UCR1); 597 temp = readl(sport->port.membase + UCR1);
598 writel(temp | UCR1_TXMPTYEN, sport->port.membase + UCR1);
599 }
465 600
466 if (USE_IRDA(sport)) { 601 if (USE_IRDA(sport)) {
467 temp = readl(sport->port.membase + UCR1); 602 temp = readl(sport->port.membase + UCR1);
@@ -473,6 +608,15 @@ static void imx_start_tx(struct uart_port *port)
473 writel(temp, sport->port.membase + UCR4); 608 writel(temp, sport->port.membase + UCR4);
474 } 609 }
475 610
611 if (sport->dma_is_enabled) {
612 /*
613 * We may in the interrupt context, so arise a work_struct to
614 * do the real job.
615 */
616 schedule_work(&sport->tsk_dma_tx);
617 return;
618 }
619
476 if (readl(sport->port.membase + uts_reg(sport)) & UTS_TXEMPTY) 620 if (readl(sport->port.membase + uts_reg(sport)) & UTS_TXEMPTY)
477 imx_transmit_buffer(sport); 621 imx_transmit_buffer(sport);
478} 622}
@@ -588,6 +732,28 @@ out:
588 return IRQ_HANDLED; 732 return IRQ_HANDLED;
589} 733}
590 734
735/*
736 * If the RXFIFO is filled with some data, and then we
737 * arise a DMA operation to receive them.
738 */
739static void imx_dma_rxint(struct imx_port *sport)
740{
741 unsigned long temp;
742
743 temp = readl(sport->port.membase + USR2);
744 if ((temp & USR2_RDR) && !sport->dma_is_rxing) {
745 sport->dma_is_rxing = 1;
746
747 /* disable the `Recerver Ready Interrrupt` */
748 temp = readl(sport->port.membase + UCR1);
749 temp &= ~(UCR1_RRDYEN);
750 writel(temp, sport->port.membase + UCR1);
751
752 /* tell the DMA to receive the data. */
753 schedule_work(&sport->tsk_dma_rx);
754 }
755}
756
591static irqreturn_t imx_int(int irq, void *dev_id) 757static irqreturn_t imx_int(int irq, void *dev_id)
592{ 758{
593 struct imx_port *sport = dev_id; 759 struct imx_port *sport = dev_id;
@@ -596,8 +762,12 @@ static irqreturn_t imx_int(int irq, void *dev_id)
596 762
597 sts = readl(sport->port.membase + USR1); 763 sts = readl(sport->port.membase + USR1);
598 764
599 if (sts & USR1_RRDY) 765 if (sts & USR1_RRDY) {
600 imx_rxint(irq, dev_id); 766 if (sport->dma_is_enabled)
767 imx_dma_rxint(sport);
768 else
769 imx_rxint(irq, dev_id);
770 }
601 771
602 if (sts & USR1_TRDY && 772 if (sts & USR1_TRDY &&
603 readl(sport->port.membase + UCR1) & UCR1_TXMPTYEN) 773 readl(sport->port.membase + UCR1) & UCR1_TXMPTYEN)
@@ -654,7 +824,8 @@ static void imx_set_mctrl(struct uart_port *port, unsigned int mctrl)
654 temp = readl(sport->port.membase + UCR2) & ~UCR2_CTS; 824 temp = readl(sport->port.membase + UCR2) & ~UCR2_CTS;
655 825
656 if (mctrl & TIOCM_RTS) 826 if (mctrl & TIOCM_RTS)
657 temp |= UCR2_CTS; 827 if (!sport->dma_is_enabled)
828 temp |= UCR2_CTS;
658 829
659 writel(temp, sport->port.membase + UCR2); 830 writel(temp, sport->port.membase + UCR2);
660} 831}
@@ -693,6 +864,226 @@ static int imx_setup_ufcr(struct imx_port *sport, unsigned int mode)
693 return 0; 864 return 0;
694} 865}
695 866
867#define RX_BUF_SIZE (PAGE_SIZE)
868static int start_rx_dma(struct imx_port *sport);
869static void dma_rx_work(struct work_struct *w)
870{
871 struct imx_port *sport = container_of(w, struct imx_port, tsk_dma_rx);
872 struct tty_port *port = &sport->port.state->port;
873
874 if (sport->rx_bytes) {
875 tty_insert_flip_string(port, sport->rx_buf, sport->rx_bytes);
876 tty_flip_buffer_push(port);
877 sport->rx_bytes = 0;
878 }
879
880 if (sport->dma_is_rxing)
881 start_rx_dma(sport);
882}
883
884static void imx_rx_dma_done(struct imx_port *sport)
885{
886 unsigned long temp;
887
888 /* Enable this interrupt when the RXFIFO is empty. */
889 temp = readl(sport->port.membase + UCR1);
890 temp |= UCR1_RRDYEN;
891 writel(temp, sport->port.membase + UCR1);
892
893 sport->dma_is_rxing = 0;
894
895 /* Is the shutdown waiting for us? */
896 if (waitqueue_active(&sport->dma_wait))
897 wake_up(&sport->dma_wait);
898}
899
900/*
901 * There are three kinds of RX DMA interrupts(such as in the MX6Q):
902 * [1] the RX DMA buffer is full.
903 * [2] the Aging timer expires(wait for 8 bytes long)
904 * [3] the Idle Condition Detect(enabled the UCR4_IDDMAEN).
905 *
906 * The [2] is trigger when a character was been sitting in the FIFO
907 * meanwhile [3] can wait for 32 bytes long when the RX line is
908 * on IDLE state and RxFIFO is empty.
909 */
910static void dma_rx_callback(void *data)
911{
912 struct imx_port *sport = data;
913 struct dma_chan *chan = sport->dma_chan_rx;
914 struct scatterlist *sgl = &sport->rx_sgl;
915 struct dma_tx_state state;
916 enum dma_status status;
917 unsigned int count;
918
919 /* unmap it first */
920 dma_unmap_sg(sport->port.dev, sgl, 1, DMA_FROM_DEVICE);
921
922 status = chan->device->device_tx_status(chan, (dma_cookie_t)0, &state);
923 count = RX_BUF_SIZE - state.residue;
924 dev_dbg(sport->port.dev, "We get %d bytes.\n", count);
925
926 if (count) {
927 sport->rx_bytes = count;
928 schedule_work(&sport->tsk_dma_rx);
929 } else
930 imx_rx_dma_done(sport);
931}
932
933static int start_rx_dma(struct imx_port *sport)
934{
935 struct scatterlist *sgl = &sport->rx_sgl;
936 struct dma_chan *chan = sport->dma_chan_rx;
937 struct device *dev = sport->port.dev;
938 struct dma_async_tx_descriptor *desc;
939 int ret;
940
941 sg_init_one(sgl, sport->rx_buf, RX_BUF_SIZE);
942 ret = dma_map_sg(dev, sgl, 1, DMA_FROM_DEVICE);
943 if (ret == 0) {
944 dev_err(dev, "DMA mapping error for RX.\n");
945 return -EINVAL;
946 }
947 desc = dmaengine_prep_slave_sg(chan, sgl, 1, DMA_DEV_TO_MEM,
948 DMA_PREP_INTERRUPT);
949 if (!desc) {
950 dev_err(dev, "We cannot prepare for the RX slave dma!\n");
951 return -EINVAL;
952 }
953 desc->callback = dma_rx_callback;
954 desc->callback_param = sport;
955
956 dev_dbg(dev, "RX: prepare for the DMA.\n");
957 dmaengine_submit(desc);
958 dma_async_issue_pending(chan);
959 return 0;
960}
961
962static void imx_uart_dma_exit(struct imx_port *sport)
963{
964 if (sport->dma_chan_rx) {
965 dma_release_channel(sport->dma_chan_rx);
966 sport->dma_chan_rx = NULL;
967
968 kfree(sport->rx_buf);
969 sport->rx_buf = NULL;
970 }
971
972 if (sport->dma_chan_tx) {
973 dma_release_channel(sport->dma_chan_tx);
974 sport->dma_chan_tx = NULL;
975 }
976
977 sport->dma_is_inited = 0;
978}
979
980static int imx_uart_dma_init(struct imx_port *sport)
981{
982 struct dma_slave_config slave_config = {};
983 struct device *dev = sport->port.dev;
984 int ret;
985
986 /* Prepare for RX : */
987 sport->dma_chan_rx = dma_request_slave_channel(dev, "rx");
988 if (!sport->dma_chan_rx) {
989 dev_dbg(dev, "cannot get the DMA channel.\n");
990 ret = -EINVAL;
991 goto err;
992 }
993
994 slave_config.direction = DMA_DEV_TO_MEM;
995 slave_config.src_addr = sport->port.mapbase + URXD0;
996 slave_config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
997 slave_config.src_maxburst = RXTL;
998 ret = dmaengine_slave_config(sport->dma_chan_rx, &slave_config);
999 if (ret) {
1000 dev_err(dev, "error in RX dma configuration.\n");
1001 goto err;
1002 }
1003
1004 sport->rx_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
1005 if (!sport->rx_buf) {
1006 dev_err(dev, "cannot alloc DMA buffer.\n");
1007 ret = -ENOMEM;
1008 goto err;
1009 }
1010 sport->rx_bytes = 0;
1011
1012 /* Prepare for TX : */
1013 sport->dma_chan_tx = dma_request_slave_channel(dev, "tx");
1014 if (!sport->dma_chan_tx) {
1015 dev_err(dev, "cannot get the TX DMA channel!\n");
1016 ret = -EINVAL;
1017 goto err;
1018 }
1019
1020 slave_config.direction = DMA_MEM_TO_DEV;
1021 slave_config.dst_addr = sport->port.mapbase + URTX0;
1022 slave_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1023 slave_config.dst_maxburst = TXTL;
1024 ret = dmaengine_slave_config(sport->dma_chan_tx, &slave_config);
1025 if (ret) {
1026 dev_err(dev, "error in TX dma configuration.");
1027 goto err;
1028 }
1029
1030 sport->dma_is_inited = 1;
1031
1032 return 0;
1033err:
1034 imx_uart_dma_exit(sport);
1035 return ret;
1036}
1037
1038static void imx_enable_dma(struct imx_port *sport)
1039{
1040 unsigned long temp;
1041 struct tty_port *port = &sport->port.state->port;
1042
1043 port->low_latency = 1;
1044 INIT_WORK(&sport->tsk_dma_tx, dma_tx_work);
1045 INIT_WORK(&sport->tsk_dma_rx, dma_rx_work);
1046 init_waitqueue_head(&sport->dma_wait);
1047
1048 /* set UCR1 */
1049 temp = readl(sport->port.membase + UCR1);
1050 temp |= UCR1_RDMAEN | UCR1_TDMAEN | UCR1_ATDMAEN |
1051 /* wait for 32 idle frames for IDDMA interrupt */
1052 UCR1_ICD_REG(3);
1053 writel(temp, sport->port.membase + UCR1);
1054
1055 /* set UCR4 */
1056 temp = readl(sport->port.membase + UCR4);
1057 temp |= UCR4_IDDMAEN;
1058 writel(temp, sport->port.membase + UCR4);
1059
1060 sport->dma_is_enabled = 1;
1061}
1062
1063static void imx_disable_dma(struct imx_port *sport)
1064{
1065 unsigned long temp;
1066 struct tty_port *port = &sport->port.state->port;
1067
1068 /* clear UCR1 */
1069 temp = readl(sport->port.membase + UCR1);
1070 temp &= ~(UCR1_RDMAEN | UCR1_TDMAEN | UCR1_ATDMAEN);
1071 writel(temp, sport->port.membase + UCR1);
1072
1073 /* clear UCR2 */
1074 temp = readl(sport->port.membase + UCR2);
1075 temp &= ~(UCR2_CTSC | UCR2_CTS);
1076 writel(temp, sport->port.membase + UCR2);
1077
1078 /* clear UCR4 */
1079 temp = readl(sport->port.membase + UCR4);
1080 temp &= ~UCR4_IDDMAEN;
1081 writel(temp, sport->port.membase + UCR4);
1082
1083 sport->dma_is_enabled = 0;
1084 port->low_latency = 0;
1085}
1086
696/* half the RX buffer size */ 1087/* half the RX buffer size */
697#define CTSTL 16 1088#define CTSTL 16
698 1089
@@ -702,15 +1093,13 @@ static int imx_startup(struct uart_port *port)
702 int retval; 1093 int retval;
703 unsigned long flags, temp; 1094 unsigned long flags, temp;
704 1095
705 if (!uart_console(port)) { 1096 retval = clk_prepare_enable(sport->clk_per);
706 retval = clk_prepare_enable(sport->clk_per); 1097 if (retval)
707 if (retval) 1098 goto error_out1;
708 goto error_out1; 1099 retval = clk_prepare_enable(sport->clk_ipg);
709 retval = clk_prepare_enable(sport->clk_ipg); 1100 if (retval) {
710 if (retval) { 1101 clk_disable_unprepare(sport->clk_per);
711 clk_disable_unprepare(sport->clk_per); 1102 goto error_out1;
712 goto error_out1;
713 }
714 } 1103 }
715 1104
716 imx_setup_ufcr(sport, 0); 1105 imx_setup_ufcr(sport, 0);
@@ -803,7 +1192,7 @@ static int imx_startup(struct uart_port *port)
803 } 1192 }
804 } 1193 }
805 1194
806 if (is_imx21_uart(sport)) { 1195 if (!is_imx1_uart(sport)) {
807 temp = readl(sport->port.membase + UCR3); 1196 temp = readl(sport->port.membase + UCR3);
808 temp |= IMX21_UCR3_RXDMUXSEL; 1197 temp |= IMX21_UCR3_RXDMUXSEL;
809 writel(temp, sport->port.membase + UCR3); 1198 writel(temp, sport->port.membase + UCR3);
@@ -833,7 +1222,7 @@ static int imx_startup(struct uart_port *port)
833 1222
834 if (USE_IRDA(sport)) { 1223 if (USE_IRDA(sport)) {
835 struct imxuart_platform_data *pdata; 1224 struct imxuart_platform_data *pdata;
836 pdata = sport->port.dev->platform_data; 1225 pdata = dev_get_platdata(sport->port.dev);
837 sport->irda_inv_rx = pdata->irda_inv_rx; 1226 sport->irda_inv_rx = pdata->irda_inv_rx;
838 sport->irda_inv_tx = pdata->irda_inv_tx; 1227 sport->irda_inv_tx = pdata->irda_inv_tx;
839 sport->trcv_delay = pdata->transceiver_delay; 1228 sport->trcv_delay = pdata->transceiver_delay;
@@ -859,6 +1248,15 @@ static void imx_shutdown(struct uart_port *port)
859 unsigned long temp; 1248 unsigned long temp;
860 unsigned long flags; 1249 unsigned long flags;
861 1250
1251 if (sport->dma_is_enabled) {
1252 /* We have to wait for the DMA to finish. */
1253 wait_event(sport->dma_wait,
1254 !sport->dma_is_rxing && !sport->dma_is_txing);
1255 imx_stop_rx(port);
1256 imx_disable_dma(sport);
1257 imx_uart_dma_exit(sport);
1258 }
1259
862 spin_lock_irqsave(&sport->port.lock, flags); 1260 spin_lock_irqsave(&sport->port.lock, flags);
863 temp = readl(sport->port.membase + UCR2); 1261 temp = readl(sport->port.membase + UCR2);
864 temp &= ~(UCR2_TXEN); 1262 temp &= ~(UCR2_TXEN);
@@ -867,7 +1265,7 @@ static void imx_shutdown(struct uart_port *port)
867 1265
868 if (USE_IRDA(sport)) { 1266 if (USE_IRDA(sport)) {
869 struct imxuart_platform_data *pdata; 1267 struct imxuart_platform_data *pdata;
870 pdata = sport->port.dev->platform_data; 1268 pdata = dev_get_platdata(sport->port.dev);
871 if (pdata->irda_enable) 1269 if (pdata->irda_enable)
872 pdata->irda_enable(0); 1270 pdata->irda_enable(0);
873 } 1271 }
@@ -901,10 +1299,8 @@ static void imx_shutdown(struct uart_port *port)
901 writel(temp, sport->port.membase + UCR1); 1299 writel(temp, sport->port.membase + UCR1);
902 spin_unlock_irqrestore(&sport->port.lock, flags); 1300 spin_unlock_irqrestore(&sport->port.lock, flags);
903 1301
904 if (!uart_console(&sport->port)) { 1302 clk_disable_unprepare(sport->clk_per);
905 clk_disable_unprepare(sport->clk_per); 1303 clk_disable_unprepare(sport->clk_ipg);
906 clk_disable_unprepare(sport->clk_ipg);
907 }
908} 1304}
909 1305
910static void 1306static void
@@ -947,6 +1343,11 @@ imx_set_termios(struct uart_port *port, struct ktermios *termios,
947 if (sport->have_rtscts) { 1343 if (sport->have_rtscts) {
948 ucr2 &= ~UCR2_IRTS; 1344 ucr2 &= ~UCR2_IRTS;
949 ucr2 |= UCR2_CTSC; 1345 ucr2 |= UCR2_CTSC;
1346
1347 /* Can we enable the DMA support? */
1348 if (is_imx6q_uart(sport) && !uart_console(port)
1349 && !sport->dma_is_inited)
1350 imx_uart_dma_init(sport);
950 } else { 1351 } else {
951 termios->c_cflag &= ~CRTSCTS; 1352 termios->c_cflag &= ~CRTSCTS;
952 } 1353 }
@@ -1020,6 +1421,11 @@ imx_set_termios(struct uart_port *port, struct ktermios *termios,
1020 */ 1421 */
1021 div = 1; 1422 div = 1;
1022 } else { 1423 } else {
1424 /* custom-baudrate handling */
1425 div = sport->port.uartclk / (baud * 16);
1426 if (baud == 38400 && quot != div)
1427 baud = sport->port.uartclk / (quot * 16);
1428
1023 div = sport->port.uartclk / (baud * 16); 1429 div = sport->port.uartclk / (baud * 16);
1024 if (div > 7) 1430 if (div > 7)
1025 div = 7; 1431 div = 7;
@@ -1048,7 +1454,7 @@ imx_set_termios(struct uart_port *port, struct ktermios *termios,
1048 writel(num, sport->port.membase + UBIR); 1454 writel(num, sport->port.membase + UBIR);
1049 writel(denom, sport->port.membase + UBMR); 1455 writel(denom, sport->port.membase + UBMR);
1050 1456
1051 if (is_imx21_uart(sport)) 1457 if (!is_imx1_uart(sport))
1052 writel(sport->port.uartclk / div / 1000, 1458 writel(sport->port.uartclk / div / 1000,
1053 sport->port.membase + IMX21_ONEMS); 1459 sport->port.membase + IMX21_ONEMS);
1054 1460
@@ -1060,6 +1466,8 @@ imx_set_termios(struct uart_port *port, struct ktermios *termios,
1060 if (UART_ENABLE_MS(&sport->port, termios->c_cflag)) 1466 if (UART_ENABLE_MS(&sport->port, termios->c_cflag))
1061 imx_enable_ms(&sport->port); 1467 imx_enable_ms(&sport->port);
1062 1468
1469 if (sport->dma_is_inited && !sport->dma_is_enabled)
1470 imx_enable_dma(sport);
1063 spin_unlock_irqrestore(&sport->port.lock, flags); 1471 spin_unlock_irqrestore(&sport->port.lock, flags);
1064} 1472}
1065 1473
@@ -1251,6 +1659,16 @@ imx_console_write(struct console *co, const char *s, unsigned int count)
1251 unsigned int ucr1; 1659 unsigned int ucr1;
1252 unsigned long flags = 0; 1660 unsigned long flags = 0;
1253 int locked = 1; 1661 int locked = 1;
1662 int retval;
1663
1664 retval = clk_enable(sport->clk_per);
1665 if (retval)
1666 return;
1667 retval = clk_enable(sport->clk_ipg);
1668 if (retval) {
1669 clk_disable(sport->clk_per);
1670 return;
1671 }
1254 1672
1255 if (sport->port.sysrq) 1673 if (sport->port.sysrq)
1256 locked = 0; 1674 locked = 0;
@@ -1286,6 +1704,9 @@ imx_console_write(struct console *co, const char *s, unsigned int count)
1286 1704
1287 if (locked) 1705 if (locked)
1288 spin_unlock_irqrestore(&sport->port.lock, flags); 1706 spin_unlock_irqrestore(&sport->port.lock, flags);
1707
1708 clk_disable(sport->clk_ipg);
1709 clk_disable(sport->clk_per);
1289} 1710}
1290 1711
1291/* 1712/*
@@ -1359,6 +1780,7 @@ imx_console_setup(struct console *co, char *options)
1359 int bits = 8; 1780 int bits = 8;
1360 int parity = 'n'; 1781 int parity = 'n';
1361 int flow = 'n'; 1782 int flow = 'n';
1783 int retval;
1362 1784
1363 /* 1785 /*
1364 * Check whether an invalid uart number has been specified, and 1786 * Check whether an invalid uart number has been specified, and
@@ -1371,6 +1793,11 @@ imx_console_setup(struct console *co, char *options)
1371 if (sport == NULL) 1793 if (sport == NULL)
1372 return -ENODEV; 1794 return -ENODEV;
1373 1795
1796 /* For setting the registers, we only need to enable the ipg clock. */
1797 retval = clk_prepare_enable(sport->clk_ipg);
1798 if (retval)
1799 goto error_console;
1800
1374 if (options) 1801 if (options)
1375 uart_parse_options(options, &baud, &parity, &bits, &flow); 1802 uart_parse_options(options, &baud, &parity, &bits, &flow);
1376 else 1803 else
@@ -1378,7 +1805,20 @@ imx_console_setup(struct console *co, char *options)
1378 1805
1379 imx_setup_ufcr(sport, 0); 1806 imx_setup_ufcr(sport, 0);
1380 1807
1381 return uart_set_options(&sport->port, co, baud, parity, bits, flow); 1808 retval = uart_set_options(&sport->port, co, baud, parity, bits, flow);
1809
1810 clk_disable(sport->clk_ipg);
1811 if (retval) {
1812 clk_unprepare(sport->clk_ipg);
1813 goto error_console;
1814 }
1815
1816 retval = clk_prepare(sport->clk_per);
1817 if (retval)
1818 clk_disable_unprepare(sport->clk_ipg);
1819
1820error_console:
1821 return retval;
1382} 1822}
1383 1823
1384static struct uart_driver imx_reg; 1824static struct uart_driver imx_reg;
@@ -1472,6 +1912,9 @@ static int serial_imx_probe_dt(struct imx_port *sport,
1472 1912
1473 sport->devdata = of_id->data; 1913 sport->devdata = of_id->data;
1474 1914
1915 if (of_device_is_stdout_path(np))
1916 add_preferred_console(imx_reg.cons->name, sport->port.line, 0);
1917
1475 return 0; 1918 return 0;
1476} 1919}
1477#else 1920#else
@@ -1485,7 +1928,7 @@ static inline int serial_imx_probe_dt(struct imx_port *sport,
1485static void serial_imx_probe_pdata(struct imx_port *sport, 1928static void serial_imx_probe_pdata(struct imx_port *sport,
1486 struct platform_device *pdev) 1929 struct platform_device *pdev)
1487{ 1930{
1488 struct imxuart_platform_data *pdata = pdev->dev.platform_data; 1931 struct imxuart_platform_data *pdata = dev_get_platdata(&pdev->dev);
1489 1932
1490 sport->port.line = pdev->id; 1933 sport->port.line = pdev->id;
1491 sport->devdata = (struct imx_uart_data *) pdev->id_entry->driver_data; 1934 sport->devdata = (struct imx_uart_data *) pdev->id_entry->driver_data;
@@ -1507,7 +1950,6 @@ static int serial_imx_probe(struct platform_device *pdev)
1507 void __iomem *base; 1950 void __iomem *base;
1508 int ret = 0; 1951 int ret = 0;
1509 struct resource *res; 1952 struct resource *res;
1510 struct pinctrl *pinctrl;
1511 1953
1512 sport = devm_kzalloc(&pdev->dev, sizeof(*sport), GFP_KERNEL); 1954 sport = devm_kzalloc(&pdev->dev, sizeof(*sport), GFP_KERNEL);
1513 if (!sport) 1955 if (!sport)
@@ -1543,13 +1985,6 @@ static int serial_imx_probe(struct platform_device *pdev)
1543 sport->timer.function = imx_timeout; 1985 sport->timer.function = imx_timeout;
1544 sport->timer.data = (unsigned long)sport; 1986 sport->timer.data = (unsigned long)sport;
1545 1987
1546 pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
1547 if (IS_ERR(pinctrl)) {
1548 ret = PTR_ERR(pinctrl);
1549 dev_err(&pdev->dev, "failed to get default pinctrl: %d\n", ret);
1550 return ret;
1551 }
1552
1553 sport->clk_ipg = devm_clk_get(&pdev->dev, "ipg"); 1988 sport->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
1554 if (IS_ERR(sport->clk_ipg)) { 1989 if (IS_ERR(sport->clk_ipg)) {
1555 ret = PTR_ERR(sport->clk_ipg); 1990 ret = PTR_ERR(sport->clk_ipg);
@@ -1564,18 +1999,15 @@ static int serial_imx_probe(struct platform_device *pdev)
1564 return ret; 1999 return ret;
1565 } 2000 }
1566 2001
1567 clk_prepare_enable(sport->clk_per);
1568 clk_prepare_enable(sport->clk_ipg);
1569
1570 sport->port.uartclk = clk_get_rate(sport->clk_per); 2002 sport->port.uartclk = clk_get_rate(sport->clk_per);
1571 2003
1572 imx_ports[sport->port.line] = sport; 2004 imx_ports[sport->port.line] = sport;
1573 2005
1574 pdata = pdev->dev.platform_data; 2006 pdata = dev_get_platdata(&pdev->dev);
1575 if (pdata && pdata->init) { 2007 if (pdata && pdata->init) {
1576 ret = pdata->init(pdev); 2008 ret = pdata->init(pdev);
1577 if (ret) 2009 if (ret)
1578 goto clkput; 2010 return ret;
1579 } 2011 }
1580 2012
1581 ret = uart_add_one_port(&imx_reg, &sport->port); 2013 ret = uart_add_one_port(&imx_reg, &sport->port);
@@ -1583,18 +2015,10 @@ static int serial_imx_probe(struct platform_device *pdev)
1583 goto deinit; 2015 goto deinit;
1584 platform_set_drvdata(pdev, sport); 2016 platform_set_drvdata(pdev, sport);
1585 2017
1586 if (!uart_console(&sport->port)) {
1587 clk_disable_unprepare(sport->clk_per);
1588 clk_disable_unprepare(sport->clk_ipg);
1589 }
1590
1591 return 0; 2018 return 0;
1592deinit: 2019deinit:
1593 if (pdata && pdata->exit) 2020 if (pdata && pdata->exit)
1594 pdata->exit(pdev); 2021 pdata->exit(pdev);
1595clkput:
1596 clk_disable_unprepare(sport->clk_per);
1597 clk_disable_unprepare(sport->clk_ipg);
1598 return ret; 2022 return ret;
1599} 2023}
1600 2024
@@ -1603,9 +2027,7 @@ static int serial_imx_remove(struct platform_device *pdev)
1603 struct imxuart_platform_data *pdata; 2027 struct imxuart_platform_data *pdata;
1604 struct imx_port *sport = platform_get_drvdata(pdev); 2028 struct imx_port *sport = platform_get_drvdata(pdev);
1605 2029
1606 pdata = pdev->dev.platform_data; 2030 pdata = dev_get_platdata(&pdev->dev);
1607
1608 platform_set_drvdata(pdev, NULL);
1609 2031
1610 uart_remove_one_port(&imx_reg, &sport->port); 2032 uart_remove_one_port(&imx_reg, &sport->port);
1611 2033
diff --git a/drivers/tty/serial/ioc4_serial.c b/drivers/tty/serial/ioc4_serial.c
index e2520abcb1c4..1274499850fc 100644
--- a/drivers/tty/serial/ioc4_serial.c
+++ b/drivers/tty/serial/ioc4_serial.c
@@ -297,7 +297,7 @@ struct ioc4_serial {
297 struct ioc4_uartregs uart_1; 297 struct ioc4_uartregs uart_1;
298 struct ioc4_uartregs uart_2; 298 struct ioc4_uartregs uart_2;
299 struct ioc4_uartregs uart_3; 299 struct ioc4_uartregs uart_3;
300} ioc4_serial; 300};
301 301
302/* UART clock speed */ 302/* UART clock speed */
303#define IOC4_SER_XIN_CLK_66 66666667 303#define IOC4_SER_XIN_CLK_66 66666667
@@ -2767,7 +2767,7 @@ ioc4_serial_core_attach(struct pci_dev *pdev, int port_type)
2767 * called per card found from IOC4 master module. 2767 * called per card found from IOC4 master module.
2768 * @idd: Master module data for this IOC4 2768 * @idd: Master module data for this IOC4
2769 */ 2769 */
2770int 2770static int
2771ioc4_serial_attach_one(struct ioc4_driver_data *idd) 2771ioc4_serial_attach_one(struct ioc4_driver_data *idd)
2772{ 2772{
2773 unsigned long tmp_addr1; 2773 unsigned long tmp_addr1;
diff --git a/drivers/tty/serial/lantiq.c b/drivers/tty/serial/lantiq.c
index 15733da757c6..88d01e0bb0c8 100644
--- a/drivers/tty/serial/lantiq.c
+++ b/drivers/tty/serial/lantiq.c
@@ -318,7 +318,7 @@ lqasc_startup(struct uart_port *port)
318 struct ltq_uart_port *ltq_port = to_ltq_uart_port(port); 318 struct ltq_uart_port *ltq_port = to_ltq_uart_port(port);
319 int retval; 319 int retval;
320 320
321 if (ltq_port->clk) 321 if (!IS_ERR(ltq_port->clk))
322 clk_enable(ltq_port->clk); 322 clk_enable(ltq_port->clk);
323 port->uartclk = clk_get_rate(ltq_port->fpiclk); 323 port->uartclk = clk_get_rate(ltq_port->fpiclk);
324 324
@@ -386,7 +386,7 @@ lqasc_shutdown(struct uart_port *port)
386 port->membase + LTQ_ASC_RXFCON); 386 port->membase + LTQ_ASC_RXFCON);
387 ltq_w32_mask(ASCTXFCON_TXFEN, ASCTXFCON_TXFFLU, 387 ltq_w32_mask(ASCTXFCON_TXFEN, ASCTXFCON_TXFFLU,
388 port->membase + LTQ_ASC_TXFCON); 388 port->membase + LTQ_ASC_TXFCON);
389 if (ltq_port->clk) 389 if (!IS_ERR(ltq_port->clk))
390 clk_disable(ltq_port->clk); 390 clk_disable(ltq_port->clk);
391} 391}
392 392
@@ -636,6 +636,9 @@ lqasc_console_setup(struct console *co, char *options)
636 636
637 port = &ltq_port->port; 637 port = &ltq_port->port;
638 638
639 if (!IS_ERR(ltq_port->clk))
640 clk_enable(ltq_port->clk);
641
639 port->uartclk = clk_get_rate(ltq_port->fpiclk); 642 port->uartclk = clk_get_rate(ltq_port->fpiclk);
640 643
641 if (options) 644 if (options)
diff --git a/drivers/tty/serial/lpc32xx_hs.c b/drivers/tty/serial/lpc32xx_hs.c
index dffea6b2cd7d..701644f06820 100644
--- a/drivers/tty/serial/lpc32xx_hs.c
+++ b/drivers/tty/serial/lpc32xx_hs.c
@@ -279,7 +279,10 @@ static void __serial_lpc32xx_rx(struct uart_port *port)
279 279
280 tmp = readl(LPC32XX_HSUART_FIFO(port->membase)); 280 tmp = readl(LPC32XX_HSUART_FIFO(port->membase));
281 } 281 }
282
283 spin_unlock(&port->lock);
282 tty_flip_buffer_push(tport); 284 tty_flip_buffer_push(tport);
285 spin_lock(&port->lock);
283} 286}
284 287
285static void __serial_lpc32xx_tx(struct uart_port *port) 288static void __serial_lpc32xx_tx(struct uart_port *port)
@@ -351,10 +354,8 @@ static irqreturn_t serial_lpc32xx_interrupt(int irq, void *dev_id)
351 } 354 }
352 355
353 /* Data received? */ 356 /* Data received? */
354 if (status & (LPC32XX_HSU_RX_TIMEOUT_INT | LPC32XX_HSU_RX_TRIG_INT)) { 357 if (status & (LPC32XX_HSU_RX_TIMEOUT_INT | LPC32XX_HSU_RX_TRIG_INT))
355 __serial_lpc32xx_rx(port); 358 __serial_lpc32xx_rx(port);
356 tty_flip_buffer_push(tport);
357 }
358 359
359 /* Transmit data request? */ 360 /* Transmit data request? */
360 if ((status & LPC32XX_HSU_TX_INT) && (!uart_tx_stopped(port))) { 361 if ((status & LPC32XX_HSU_TX_INT) && (!uart_tx_stopped(port))) {
diff --git a/drivers/tty/serial/m32r_sio.c b/drivers/tty/serial/m32r_sio.c
index bb1afa0922e1..9cd9b4eba9fc 100644
--- a/drivers/tty/serial/m32r_sio.c
+++ b/drivers/tty/serial/m32r_sio.c
@@ -368,7 +368,10 @@ static void receive_chars(struct uart_sio_port *up, int *status)
368 ignore_char: 368 ignore_char:
369 *status = serial_in(up, UART_LSR); 369 *status = serial_in(up, UART_LSR);
370 } while ((*status & UART_LSR_DR) && (max_count-- > 0)); 370 } while ((*status & UART_LSR_DR) && (max_count-- > 0));
371
372 spin_unlock(&up->port.lock);
371 tty_flip_buffer_push(port); 373 tty_flip_buffer_push(port);
374 spin_lock(&up->port.lock);
372} 375}
373 376
374static void transmit_chars(struct uart_sio_port *up) 377static void transmit_chars(struct uart_sio_port *up)
diff --git a/drivers/tty/serial/max3100.c b/drivers/tty/serial/max3100.c
index 35866d5872ad..79f9a9eff545 100644
--- a/drivers/tty/serial/max3100.c
+++ b/drivers/tty/serial/max3100.c
@@ -779,7 +779,7 @@ static int max3100_probe(struct spi_device *spi)
779 max3100s[i]->irq = spi->irq; 779 max3100s[i]->irq = spi->irq;
780 spin_lock_init(&max3100s[i]->conf_lock); 780 spin_lock_init(&max3100s[i]->conf_lock);
781 spi_set_drvdata(spi, max3100s[i]); 781 spi_set_drvdata(spi, max3100s[i]);
782 pdata = spi->dev.platform_data; 782 pdata = dev_get_platdata(&spi->dev);
783 max3100s[i]->crystal = pdata->crystal; 783 max3100s[i]->crystal = pdata->crystal;
784 max3100s[i]->loopback = pdata->loopback; 784 max3100s[i]->loopback = pdata->loopback;
785 max3100s[i]->poll_time = pdata->poll_time * HZ / 1000; 785 max3100s[i]->poll_time = pdata->poll_time * HZ / 1000;
diff --git a/drivers/tty/serial/max310x.c b/drivers/tty/serial/max310x.c
index 8941e6418942..b2e707aa603a 100644
--- a/drivers/tty/serial/max310x.c
+++ b/drivers/tty/serial/max310x.c
@@ -1,7 +1,7 @@
1/* 1/*
2 * Maxim (Dallas) MAX3107/8 serial driver 2 * Maxim (Dallas) MAX3107/8/9, MAX14830 serial driver
3 * 3 *
4 * Copyright (C) 2012 Alexander Shiyan <shc_work@mail.ru> 4 * Copyright (C) 2012-2013 Alexander Shiyan <shc_work@mail.ru>
5 * 5 *
6 * Based on max3100.c, by Christian Pellegrin <chripell@evolware.org> 6 * Based on max3100.c, by Christian Pellegrin <chripell@evolware.org>
7 * Based on max3110.c, by Feng Tang <feng.tang@intel.com> 7 * Based on max3110.c, by Feng Tang <feng.tang@intel.com>
@@ -13,11 +13,10 @@
13 * (at your option) any later version. 13 * (at your option) any later version.
14 */ 14 */
15 15
16/* TODO: MAX3109 support (Dual) */
17/* TODO: MAX14830 support (Quad) */
18
19#include <linux/module.h> 16#include <linux/module.h>
17#include <linux/delay.h>
20#include <linux/device.h> 18#include <linux/device.h>
19#include <linux/bitops.h>
21#include <linux/serial_core.h> 20#include <linux/serial_core.h>
22#include <linux/serial.h> 21#include <linux/serial.h>
23#include <linux/tty.h> 22#include <linux/tty.h>
@@ -25,8 +24,10 @@
25#include <linux/regmap.h> 24#include <linux/regmap.h>
26#include <linux/gpio.h> 25#include <linux/gpio.h>
27#include <linux/spi/spi.h> 26#include <linux/spi/spi.h>
27
28#include <linux/platform_data/max310x.h> 28#include <linux/platform_data/max310x.h>
29 29
30#define MAX310X_NAME "max310x"
30#define MAX310X_MAJOR 204 31#define MAX310X_MAJOR 204
31#define MAX310X_MINOR 209 32#define MAX310X_MINOR 209
32 33
@@ -37,7 +38,8 @@
37#define MAX310X_IRQSTS_REG (0x02) /* IRQ status */ 38#define MAX310X_IRQSTS_REG (0x02) /* IRQ status */
38#define MAX310X_LSR_IRQEN_REG (0x03) /* LSR IRQ enable */ 39#define MAX310X_LSR_IRQEN_REG (0x03) /* LSR IRQ enable */
39#define MAX310X_LSR_IRQSTS_REG (0x04) /* LSR IRQ status */ 40#define MAX310X_LSR_IRQSTS_REG (0x04) /* LSR IRQ status */
40#define MAX310X_SPCHR_IRQEN_REG (0x05) /* Special char IRQ enable */ 41#define MAX310X_REG_05 (0x05)
42#define MAX310X_SPCHR_IRQEN_REG MAX310X_REG_05 /* Special char IRQ en */
41#define MAX310X_SPCHR_IRQSTS_REG (0x06) /* Special char IRQ status */ 43#define MAX310X_SPCHR_IRQSTS_REG (0x06) /* Special char IRQ status */
42#define MAX310X_STS_IRQEN_REG (0x07) /* Status IRQ enable */ 44#define MAX310X_STS_IRQEN_REG (0x07) /* Status IRQ enable */
43#define MAX310X_STS_IRQSTS_REG (0x08) /* Status IRQ status */ 45#define MAX310X_STS_IRQSTS_REG (0x08) /* Status IRQ status */
@@ -63,8 +65,15 @@
63#define MAX310X_BRGDIVLSB_REG (0x1c) /* Baud rate divisor LSB */ 65#define MAX310X_BRGDIVLSB_REG (0x1c) /* Baud rate divisor LSB */
64#define MAX310X_BRGDIVMSB_REG (0x1d) /* Baud rate divisor MSB */ 66#define MAX310X_BRGDIVMSB_REG (0x1d) /* Baud rate divisor MSB */
65#define MAX310X_CLKSRC_REG (0x1e) /* Clock source */ 67#define MAX310X_CLKSRC_REG (0x1e) /* Clock source */
66/* Only present in MAX3107 */ 68#define MAX310X_REG_1F (0x1f)
67#define MAX3107_REVID_REG (0x1f) /* Revision identification */ 69
70#define MAX310X_REVID_REG MAX310X_REG_1F /* Revision ID */
71
72#define MAX310X_GLOBALIRQ_REG MAX310X_REG_1F /* Global IRQ (RO) */
73#define MAX310X_GLOBALCMD_REG MAX310X_REG_1F /* Global Command (WO) */
74
75/* Extended registers */
76#define MAX310X_REVID_EXTREG MAX310X_REG_05 /* Revision ID */
68 77
69/* IRQ register bits */ 78/* IRQ register bits */
70#define MAX310X_IRQ_LSR_BIT (1 << 0) /* LSR interrupt */ 79#define MAX310X_IRQ_LSR_BIT (1 << 0) /* LSR interrupt */
@@ -246,58 +255,210 @@
246#define MAX310X_CLKSRC_EXTCLK_BIT (1 << 4) /* External clock enable */ 255#define MAX310X_CLKSRC_EXTCLK_BIT (1 << 4) /* External clock enable */
247#define MAX310X_CLKSRC_CLK2RTS_BIT (1 << 7) /* Baud clk to RTS pin */ 256#define MAX310X_CLKSRC_CLK2RTS_BIT (1 << 7) /* Baud clk to RTS pin */
248 257
258/* Global commands */
259#define MAX310X_EXTREG_ENBL (0xce)
260#define MAX310X_EXTREG_DSBL (0xcd)
261
249/* Misc definitions */ 262/* Misc definitions */
250#define MAX310X_FIFO_SIZE (128) 263#define MAX310X_FIFO_SIZE (128)
264#define MAX310x_REV_MASK (0xfc)
251 265
252/* MAX3107 specific */ 266/* MAX3107 specific */
253#define MAX3107_REV_ID (0xa0) 267#define MAX3107_REV_ID (0xa0)
254#define MAX3107_REV_MASK (0xfe) 268
255 269/* MAX3109 specific */
256/* IRQ status bits definitions */ 270#define MAX3109_REV_ID (0xc0)
257#define MAX310X_IRQ_TX (MAX310X_IRQ_TXFIFO_BIT | \ 271
258 MAX310X_IRQ_TXEMPTY_BIT) 272/* MAX14830 specific */
259#define MAX310X_IRQ_RX (MAX310X_IRQ_RXFIFO_BIT | \ 273#define MAX14830_BRGCFG_CLKDIS_BIT (1 << 6) /* Clock Disable */
260 MAX310X_IRQ_RXEMPTY_BIT) 274#define MAX14830_REV_ID (0xb0)
261 275
262/* Supported chip types */ 276struct max310x_devtype {
263enum { 277 char name[9];
264 MAX310X_TYPE_MAX3107 = 3107, 278 int nr;
265 MAX310X_TYPE_MAX3108 = 3108, 279 int (*detect)(struct device *);
280 void (*power)(struct uart_port *, int);
266}; 281};
267 282
268struct max310x_port { 283struct max310x_one {
269 struct uart_driver uart;
270 struct uart_port port; 284 struct uart_port port;
285 struct work_struct tx_work;
286};
271 287
272 const char *name; 288struct max310x_port {
273 int uartclk; 289 struct uart_driver uart;
274 290 struct max310x_devtype *devtype;
275 unsigned int nr_gpio; 291 struct regmap *regmap;
292 struct regmap_config regcfg;
293 struct mutex mutex;
294 struct max310x_pdata *pdata;
295 int gpio_used;
276#ifdef CONFIG_GPIOLIB 296#ifdef CONFIG_GPIOLIB
277 struct gpio_chip gpio; 297 struct gpio_chip gpio;
278#endif 298#endif
299 struct max310x_one p[0];
300};
279 301
280 struct regmap *regmap; 302static u8 max310x_port_read(struct uart_port *port, u8 reg)
281 struct regmap_config regcfg; 303{
304 struct max310x_port *s = dev_get_drvdata(port->dev);
305 unsigned int val = 0;
282 306
283 struct workqueue_struct *wq; 307 regmap_read(s->regmap, port->iobase + reg, &val);
284 struct work_struct tx_work;
285 308
286 struct mutex max310x_mutex; 309 return val;
310}
287 311
288 struct max310x_pdata *pdata; 312static void max310x_port_write(struct uart_port *port, u8 reg, u8 val)
313{
314 struct max310x_port *s = dev_get_drvdata(port->dev);
315
316 regmap_write(s->regmap, port->iobase + reg, val);
317}
318
319static void max310x_port_update(struct uart_port *port, u8 reg, u8 mask, u8 val)
320{
321 struct max310x_port *s = dev_get_drvdata(port->dev);
322
323 regmap_update_bits(s->regmap, port->iobase + reg, mask, val);
324}
325
326static int max3107_detect(struct device *dev)
327{
328 struct max310x_port *s = dev_get_drvdata(dev);
329 unsigned int val = 0;
330 int ret;
331
332 ret = regmap_read(s->regmap, MAX310X_REVID_REG, &val);
333 if (ret)
334 return ret;
335
336 if (((val & MAX310x_REV_MASK) != MAX3107_REV_ID)) {
337 dev_err(dev,
338 "%s ID 0x%02x does not match\n", s->devtype->name, val);
339 return -ENODEV;
340 }
341
342 return 0;
343}
344
345static int max3108_detect(struct device *dev)
346{
347 struct max310x_port *s = dev_get_drvdata(dev);
348 unsigned int val = 0;
349 int ret;
350
351 /* MAX3108 have not REV ID register, we just check default value
352 * from clocksource register to make sure everything works.
353 */
354 ret = regmap_read(s->regmap, MAX310X_CLKSRC_REG, &val);
355 if (ret)
356 return ret;
357
358 if (val != (MAX310X_CLKSRC_EXTCLK_BIT | MAX310X_CLKSRC_PLLBYP_BIT)) {
359 dev_err(dev, "%s not present\n", s->devtype->name);
360 return -ENODEV;
361 }
362
363 return 0;
364}
365
366static int max3109_detect(struct device *dev)
367{
368 struct max310x_port *s = dev_get_drvdata(dev);
369 unsigned int val = 0;
370 int ret;
371
372 ret = regmap_read(s->regmap, MAX310X_REVID_REG, &val);
373 if (ret)
374 return ret;
375
376 if (((val & MAX310x_REV_MASK) != MAX3109_REV_ID)) {
377 dev_err(dev,
378 "%s ID 0x%02x does not match\n", s->devtype->name, val);
379 return -ENODEV;
380 }
381
382 return 0;
383}
384
385static void max310x_power(struct uart_port *port, int on)
386{
387 max310x_port_update(port, MAX310X_MODE1_REG,
388 MAX310X_MODE1_FORCESLEEP_BIT,
389 on ? 0 : MAX310X_MODE1_FORCESLEEP_BIT);
390 if (on)
391 msleep(50);
392}
393
394static int max14830_detect(struct device *dev)
395{
396 struct max310x_port *s = dev_get_drvdata(dev);
397 unsigned int val = 0;
398 int ret;
399
400 ret = regmap_write(s->regmap, MAX310X_GLOBALCMD_REG,
401 MAX310X_EXTREG_ENBL);
402 if (ret)
403 return ret;
404
405 regmap_read(s->regmap, MAX310X_REVID_EXTREG, &val);
406 regmap_write(s->regmap, MAX310X_GLOBALCMD_REG, MAX310X_EXTREG_DSBL);
407 if (((val & MAX310x_REV_MASK) != MAX14830_REV_ID)) {
408 dev_err(dev,
409 "%s ID 0x%02x does not match\n", s->devtype->name, val);
410 return -ENODEV;
411 }
412
413 return 0;
414}
415
416static void max14830_power(struct uart_port *port, int on)
417{
418 max310x_port_update(port, MAX310X_BRGCFG_REG,
419 MAX14830_BRGCFG_CLKDIS_BIT,
420 on ? 0 : MAX14830_BRGCFG_CLKDIS_BIT);
421 if (on)
422 msleep(50);
423}
424
425static const struct max310x_devtype max3107_devtype = {
426 .name = "MAX3107",
427 .nr = 1,
428 .detect = max3107_detect,
429 .power = max310x_power,
430};
431
432static const struct max310x_devtype max3108_devtype = {
433 .name = "MAX3108",
434 .nr = 1,
435 .detect = max3108_detect,
436 .power = max310x_power,
437};
438
439static const struct max310x_devtype max3109_devtype = {
440 .name = "MAX3109",
441 .nr = 2,
442 .detect = max3109_detect,
443 .power = max310x_power,
444};
445
446static const struct max310x_devtype max14830_devtype = {
447 .name = "MAX14830",
448 .nr = 4,
449 .detect = max14830_detect,
450 .power = max14830_power,
289}; 451};
290 452
291static bool max3107_8_reg_writeable(struct device *dev, unsigned int reg) 453static bool max310x_reg_writeable(struct device *dev, unsigned int reg)
292{ 454{
293 switch (reg) { 455 switch (reg & 0x1f) {
294 case MAX310X_IRQSTS_REG: 456 case MAX310X_IRQSTS_REG:
295 case MAX310X_LSR_IRQSTS_REG: 457 case MAX310X_LSR_IRQSTS_REG:
296 case MAX310X_SPCHR_IRQSTS_REG: 458 case MAX310X_SPCHR_IRQSTS_REG:
297 case MAX310X_STS_IRQSTS_REG: 459 case MAX310X_STS_IRQSTS_REG:
298 case MAX310X_TXFIFOLVL_REG: 460 case MAX310X_TXFIFOLVL_REG:
299 case MAX310X_RXFIFOLVL_REG: 461 case MAX310X_RXFIFOLVL_REG:
300 case MAX3107_REVID_REG: /* Only available on MAX3107 */
301 return false; 462 return false;
302 default: 463 default:
303 break; 464 break;
@@ -308,7 +469,7 @@ static bool max3107_8_reg_writeable(struct device *dev, unsigned int reg)
308 469
309static bool max310x_reg_volatile(struct device *dev, unsigned int reg) 470static bool max310x_reg_volatile(struct device *dev, unsigned int reg)
310{ 471{
311 switch (reg) { 472 switch (reg & 0x1f) {
312 case MAX310X_RHR_REG: 473 case MAX310X_RHR_REG:
313 case MAX310X_IRQSTS_REG: 474 case MAX310X_IRQSTS_REG:
314 case MAX310X_LSR_IRQSTS_REG: 475 case MAX310X_LSR_IRQSTS_REG:
@@ -317,6 +478,9 @@ static bool max310x_reg_volatile(struct device *dev, unsigned int reg)
317 case MAX310X_TXFIFOLVL_REG: 478 case MAX310X_TXFIFOLVL_REG:
318 case MAX310X_RXFIFOLVL_REG: 479 case MAX310X_RXFIFOLVL_REG:
319 case MAX310X_GPIODATA_REG: 480 case MAX310X_GPIODATA_REG:
481 case MAX310X_BRGDIVLSB_REG:
482 case MAX310X_REG_05:
483 case MAX310X_REG_1F:
320 return true; 484 return true;
321 default: 485 default:
322 break; 486 break;
@@ -327,7 +491,7 @@ static bool max310x_reg_volatile(struct device *dev, unsigned int reg)
327 491
328static bool max310x_reg_precious(struct device *dev, unsigned int reg) 492static bool max310x_reg_precious(struct device *dev, unsigned int reg)
329{ 493{
330 switch (reg) { 494 switch (reg & 0x1f) {
331 case MAX310X_RHR_REG: 495 case MAX310X_RHR_REG:
332 case MAX310X_IRQSTS_REG: 496 case MAX310X_IRQSTS_REG:
333 case MAX310X_SPCHR_IRQSTS_REG: 497 case MAX310X_SPCHR_IRQSTS_REG:
@@ -340,42 +504,25 @@ static bool max310x_reg_precious(struct device *dev, unsigned int reg)
340 return false; 504 return false;
341} 505}
342 506
343static void max310x_set_baud(struct max310x_port *s, int baud) 507static void max310x_set_baud(struct uart_port *port, int baud)
344{ 508{
345 unsigned int mode = 0, div = s->uartclk / baud; 509 unsigned int mode = 0, div = port->uartclk / baud;
346 510
347 if (!(div / 16)) { 511 if (!(div / 16)) {
348 /* Mode x2 */ 512 /* Mode x2 */
349 mode = MAX310X_BRGCFG_2XMODE_BIT; 513 mode = MAX310X_BRGCFG_2XMODE_BIT;
350 div = (s->uartclk * 2) / baud; 514 div = (port->uartclk * 2) / baud;
351 } 515 }
352 516
353 if (!(div / 16)) { 517 if (!(div / 16)) {
354 /* Mode x4 */ 518 /* Mode x4 */
355 mode = MAX310X_BRGCFG_4XMODE_BIT; 519 mode = MAX310X_BRGCFG_4XMODE_BIT;
356 div = (s->uartclk * 4) / baud; 520 div = (port->uartclk * 4) / baud;
357 } 521 }
358 522
359 regmap_write(s->regmap, MAX310X_BRGDIVMSB_REG, 523 max310x_port_write(port, MAX310X_BRGDIVMSB_REG, (div / 16) >> 8);
360 ((div / 16) >> 8) & 0xff); 524 max310x_port_write(port, MAX310X_BRGDIVLSB_REG, div / 16);
361 regmap_write(s->regmap, MAX310X_BRGDIVLSB_REG, (div / 16) & 0xff); 525 max310x_port_write(port, MAX310X_BRGCFG_REG, (div % 16) | mode);
362 regmap_write(s->regmap, MAX310X_BRGCFG_REG, (div % 16) | mode);
363}
364
365static void max310x_wait_pll(struct max310x_port *s)
366{
367 int tryes = 1000;
368
369 /* Wait for PLL only if crystal is used */
370 if (!(s->pdata->driver_flags & MAX310X_EXT_CLK)) {
371 unsigned int sts = 0;
372
373 while (tryes--) {
374 regmap_read(s->regmap, MAX310X_STS_IRQSTS_REG, &sts);
375 if (sts & MAX310X_STS_CLKREADY_BIT)
376 break;
377 }
378 }
379} 526}
380 527
381static int max310x_update_best_err(unsigned long f, long *besterr) 528static int max310x_update_best_err(unsigned long f, long *besterr)
@@ -449,49 +596,49 @@ static int max310x_set_ref_clk(struct max310x_port *s)
449 596
450 regmap_write(s->regmap, MAX310X_CLKSRC_REG, clksrc); 597 regmap_write(s->regmap, MAX310X_CLKSRC_REG, clksrc);
451 598
452 if (pllcfg) 599 /* Wait for crystal */
453 max310x_wait_pll(s); 600 if (pllcfg && !(s->pdata->driver_flags & MAX310X_EXT_CLK))
454 601 msleep(10);
455 dev_dbg(s->port.dev, "Reference clock set to %lu Hz\n", bestfreq);
456 602
457 return (int)bestfreq; 603 return (int)bestfreq;
458} 604}
459 605
460static void max310x_handle_rx(struct max310x_port *s, unsigned int rxlen) 606static void max310x_handle_rx(struct uart_port *port, unsigned int rxlen)
461{ 607{
462 unsigned int sts = 0, ch = 0, flag; 608 unsigned int sts, ch, flag;
463 609
464 if (unlikely(rxlen >= MAX310X_FIFO_SIZE)) { 610 if (unlikely(rxlen >= port->fifosize)) {
465 dev_warn(s->port.dev, "Possible RX FIFO overrun %d\n", rxlen); 611 dev_warn_ratelimited(port->dev,
612 "Port %i: Possible RX FIFO overrun\n",
613 port->line);
614 port->icount.buf_overrun++;
466 /* Ensure sanity of RX level */ 615 /* Ensure sanity of RX level */
467 rxlen = MAX310X_FIFO_SIZE; 616 rxlen = port->fifosize;
468 } 617 }
469 618
470 dev_dbg(s->port.dev, "RX Len = %u\n", rxlen);
471
472 while (rxlen--) { 619 while (rxlen--) {
473 regmap_read(s->regmap, MAX310X_RHR_REG, &ch); 620 ch = max310x_port_read(port, MAX310X_RHR_REG);
474 regmap_read(s->regmap, MAX310X_LSR_IRQSTS_REG, &sts); 621 sts = max310x_port_read(port, MAX310X_LSR_IRQSTS_REG);
475 622
476 sts &= MAX310X_LSR_RXPAR_BIT | MAX310X_LSR_FRERR_BIT | 623 sts &= MAX310X_LSR_RXPAR_BIT | MAX310X_LSR_FRERR_BIT |
477 MAX310X_LSR_RXOVR_BIT | MAX310X_LSR_RXBRK_BIT; 624 MAX310X_LSR_RXOVR_BIT | MAX310X_LSR_RXBRK_BIT;
478 625
479 s->port.icount.rx++; 626 port->icount.rx++;
480 flag = TTY_NORMAL; 627 flag = TTY_NORMAL;
481 628
482 if (unlikely(sts)) { 629 if (unlikely(sts)) {
483 if (sts & MAX310X_LSR_RXBRK_BIT) { 630 if (sts & MAX310X_LSR_RXBRK_BIT) {
484 s->port.icount.brk++; 631 port->icount.brk++;
485 if (uart_handle_break(&s->port)) 632 if (uart_handle_break(port))
486 continue; 633 continue;
487 } else if (sts & MAX310X_LSR_RXPAR_BIT) 634 } else if (sts & MAX310X_LSR_RXPAR_BIT)
488 s->port.icount.parity++; 635 port->icount.parity++;
489 else if (sts & MAX310X_LSR_FRERR_BIT) 636 else if (sts & MAX310X_LSR_FRERR_BIT)
490 s->port.icount.frame++; 637 port->icount.frame++;
491 else if (sts & MAX310X_LSR_RXOVR_BIT) 638 else if (sts & MAX310X_LSR_RXOVR_BIT)
492 s->port.icount.overrun++; 639 port->icount.overrun++;
493 640
494 sts &= s->port.read_status_mask; 641 sts &= port->read_status_mask;
495 if (sts & MAX310X_LSR_RXBRK_BIT) 642 if (sts & MAX310X_LSR_RXBRK_BIT)
496 flag = TTY_BREAK; 643 flag = TTY_BREAK;
497 else if (sts & MAX310X_LSR_RXPAR_BIT) 644 else if (sts & MAX310X_LSR_RXPAR_BIT)
@@ -502,129 +649,129 @@ static void max310x_handle_rx(struct max310x_port *s, unsigned int rxlen)
502 flag = TTY_OVERRUN; 649 flag = TTY_OVERRUN;
503 } 650 }
504 651
505 if (uart_handle_sysrq_char(s->port, ch)) 652 if (uart_handle_sysrq_char(port, ch))
506 continue; 653 continue;
507 654
508 if (sts & s->port.ignore_status_mask) 655 if (sts & port->ignore_status_mask)
509 continue; 656 continue;
510 657
511 uart_insert_char(&s->port, sts, MAX310X_LSR_RXOVR_BIT, 658 uart_insert_char(port, sts, MAX310X_LSR_RXOVR_BIT, ch, flag);
512 ch, flag);
513 } 659 }
514 660
515 tty_flip_buffer_push(&s->port.state->port); 661 tty_flip_buffer_push(&port->state->port);
516} 662}
517 663
518static void max310x_handle_tx(struct max310x_port *s) 664static void max310x_handle_tx(struct uart_port *port)
519{ 665{
520 struct circ_buf *xmit = &s->port.state->xmit; 666 struct circ_buf *xmit = &port->state->xmit;
521 unsigned int txlen = 0, to_send; 667 unsigned int txlen, to_send;
522 668
523 if (unlikely(s->port.x_char)) { 669 if (unlikely(port->x_char)) {
524 regmap_write(s->regmap, MAX310X_THR_REG, s->port.x_char); 670 max310x_port_write(port, MAX310X_THR_REG, port->x_char);
525 s->port.icount.tx++; 671 port->icount.tx++;
526 s->port.x_char = 0; 672 port->x_char = 0;
527 return; 673 return;
528 } 674 }
529 675
530 if (uart_circ_empty(xmit) || uart_tx_stopped(&s->port)) 676 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
531 return; 677 return;
532 678
533 /* Get length of data pending in circular buffer */ 679 /* Get length of data pending in circular buffer */
534 to_send = uart_circ_chars_pending(xmit); 680 to_send = uart_circ_chars_pending(xmit);
535 if (likely(to_send)) { 681 if (likely(to_send)) {
536 /* Limit to size of TX FIFO */ 682 /* Limit to size of TX FIFO */
537 regmap_read(s->regmap, MAX310X_TXFIFOLVL_REG, &txlen); 683 txlen = max310x_port_read(port, MAX310X_TXFIFOLVL_REG);
538 txlen = MAX310X_FIFO_SIZE - txlen; 684 txlen = port->fifosize - txlen;
539 to_send = (to_send > txlen) ? txlen : to_send; 685 to_send = (to_send > txlen) ? txlen : to_send;
540 686
541 dev_dbg(s->port.dev, "TX Len = %u\n", to_send);
542
543 /* Add data to send */ 687 /* Add data to send */
544 s->port.icount.tx += to_send; 688 port->icount.tx += to_send;
545 while (to_send--) { 689 while (to_send--) {
546 regmap_write(s->regmap, MAX310X_THR_REG, 690 max310x_port_write(port, MAX310X_THR_REG,
547 xmit->buf[xmit->tail]); 691 xmit->buf[xmit->tail]);
548 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); 692 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
549 }; 693 };
550 } 694 }
551 695
552 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 696 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
553 uart_write_wakeup(&s->port); 697 uart_write_wakeup(port);
554} 698}
555 699
556static irqreturn_t max310x_ist(int irq, void *dev_id) 700static void max310x_port_irq(struct max310x_port *s, int portno)
557{ 701{
558 struct max310x_port *s = (struct max310x_port *)dev_id; 702 struct uart_port *port = &s->p[portno].port;
559 unsigned int ists = 0, lsr = 0, rxlen = 0;
560 703
561 mutex_lock(&s->max310x_mutex); 704 do {
705 unsigned int ists, lsr, rxlen;
562 706
563 for (;;) {
564 /* Read IRQ status & RX FIFO level */ 707 /* Read IRQ status & RX FIFO level */
565 regmap_read(s->regmap, MAX310X_IRQSTS_REG, &ists); 708 ists = max310x_port_read(port, MAX310X_IRQSTS_REG);
566 regmap_read(s->regmap, MAX310X_LSR_IRQSTS_REG, &lsr); 709 rxlen = max310x_port_read(port, MAX310X_RXFIFOLVL_REG);
567 regmap_read(s->regmap, MAX310X_RXFIFOLVL_REG, &rxlen); 710 if (!ists && !rxlen)
568 if (!ists && !(lsr & MAX310X_LSR_RXTO_BIT) && !rxlen)
569 break; 711 break;
570 712
571 dev_dbg(s->port.dev, "IRQ status: 0x%02x\n", ists); 713 if (ists & MAX310X_IRQ_CTS_BIT) {
572 714 lsr = max310x_port_read(port, MAX310X_LSR_IRQSTS_REG);
573 if (rxlen) 715 uart_handle_cts_change(port,
574 max310x_handle_rx(s, rxlen);
575 if (ists & MAX310X_IRQ_TX)
576 max310x_handle_tx(s);
577 if (ists & MAX310X_IRQ_CTS_BIT)
578 uart_handle_cts_change(&s->port,
579 !!(lsr & MAX310X_LSR_CTS_BIT)); 716 !!(lsr & MAX310X_LSR_CTS_BIT));
580 } 717 }
718 if (rxlen)
719 max310x_handle_rx(port, rxlen);
720 if (ists & MAX310X_IRQ_TXEMPTY_BIT) {
721 mutex_lock(&s->mutex);
722 max310x_handle_tx(port);
723 mutex_unlock(&s->mutex);
724 }
725 } while (1);
726}
727
728static irqreturn_t max310x_ist(int irq, void *dev_id)
729{
730 struct max310x_port *s = (struct max310x_port *)dev_id;
581 731
582 mutex_unlock(&s->max310x_mutex); 732 if (s->uart.nr > 1) {
733 do {
734 unsigned int val = ~0;
735
736 WARN_ON_ONCE(regmap_read(s->regmap,
737 MAX310X_GLOBALIRQ_REG, &val));
738 val = ((1 << s->uart.nr) - 1) & ~val;
739 if (!val)
740 break;
741 max310x_port_irq(s, fls(val) - 1);
742 } while (1);
743 } else
744 max310x_port_irq(s, 0);
583 745
584 return IRQ_HANDLED; 746 return IRQ_HANDLED;
585} 747}
586 748
587static void max310x_wq_proc(struct work_struct *ws) 749static void max310x_wq_proc(struct work_struct *ws)
588{ 750{
589 struct max310x_port *s = container_of(ws, struct max310x_port, tx_work); 751 struct max310x_one *one = container_of(ws, struct max310x_one, tx_work);
752 struct max310x_port *s = dev_get_drvdata(one->port.dev);
590 753
591 mutex_lock(&s->max310x_mutex); 754 mutex_lock(&s->mutex);
592 max310x_handle_tx(s); 755 max310x_handle_tx(&one->port);
593 mutex_unlock(&s->max310x_mutex); 756 mutex_unlock(&s->mutex);
594} 757}
595 758
596static void max310x_start_tx(struct uart_port *port) 759static void max310x_start_tx(struct uart_port *port)
597{ 760{
598 struct max310x_port *s = container_of(port, struct max310x_port, port); 761 struct max310x_one *one = container_of(port, struct max310x_one, port);
599
600 queue_work(s->wq, &s->tx_work);
601}
602
603static void max310x_stop_tx(struct uart_port *port)
604{
605 /* Do nothing */
606}
607 762
608static void max310x_stop_rx(struct uart_port *port) 763 if (!work_pending(&one->tx_work))
609{ 764 schedule_work(&one->tx_work);
610 /* Do nothing */
611} 765}
612 766
613static unsigned int max310x_tx_empty(struct uart_port *port) 767static unsigned int max310x_tx_empty(struct uart_port *port)
614{ 768{
615 unsigned int val = 0; 769 unsigned int lvl, sts;
616 struct max310x_port *s = container_of(port, struct max310x_port, port);
617 770
618 mutex_lock(&s->max310x_mutex); 771 lvl = max310x_port_read(port, MAX310X_TXFIFOLVL_REG);
619 regmap_read(s->regmap, MAX310X_TXFIFOLVL_REG, &val); 772 sts = max310x_port_read(port, MAX310X_IRQSTS_REG);
620 mutex_unlock(&s->max310x_mutex);
621 773
622 return val ? 0 : TIOCSER_TEMT; 774 return ((sts & MAX310X_IRQ_TXEMPTY_BIT) && !lvl) ? TIOCSER_TEMT : 0;
623}
624
625static void max310x_enable_ms(struct uart_port *port)
626{
627 /* Modem status not supported */
628} 775}
629 776
630static unsigned int max310x_get_mctrl(struct uart_port *port) 777static unsigned int max310x_get_mctrl(struct uart_port *port)
@@ -644,28 +791,20 @@ static void max310x_set_mctrl(struct uart_port *port, unsigned int mctrl)
644 791
645static void max310x_break_ctl(struct uart_port *port, int break_state) 792static void max310x_break_ctl(struct uart_port *port, int break_state)
646{ 793{
647 struct max310x_port *s = container_of(port, struct max310x_port, port); 794 max310x_port_update(port, MAX310X_LCR_REG,
648 795 MAX310X_LCR_TXBREAK_BIT,
649 mutex_lock(&s->max310x_mutex); 796 break_state ? MAX310X_LCR_TXBREAK_BIT : 0);
650 regmap_update_bits(s->regmap, MAX310X_LCR_REG,
651 MAX310X_LCR_TXBREAK_BIT,
652 break_state ? MAX310X_LCR_TXBREAK_BIT : 0);
653 mutex_unlock(&s->max310x_mutex);
654} 797}
655 798
656static void max310x_set_termios(struct uart_port *port, 799static void max310x_set_termios(struct uart_port *port,
657 struct ktermios *termios, 800 struct ktermios *termios,
658 struct ktermios *old) 801 struct ktermios *old)
659{ 802{
660 struct max310x_port *s = container_of(port, struct max310x_port, port);
661 unsigned int lcr, flow = 0; 803 unsigned int lcr, flow = 0;
662 int baud; 804 int baud;
663 805
664 mutex_lock(&s->max310x_mutex);
665
666 /* Mask termios capabilities we don't support */ 806 /* Mask termios capabilities we don't support */
667 termios->c_cflag &= ~CMSPAR; 807 termios->c_cflag &= ~CMSPAR;
668 termios->c_iflag &= ~IXANY;
669 808
670 /* Word size */ 809 /* Word size */
671 switch (termios->c_cflag & CSIZE) { 810 switch (termios->c_cflag & CSIZE) {
@@ -696,7 +835,7 @@ static void max310x_set_termios(struct uart_port *port,
696 lcr |= MAX310X_LCR_STOPLEN_BIT; /* 2 stops */ 835 lcr |= MAX310X_LCR_STOPLEN_BIT; /* 2 stops */
697 836
698 /* Update LCR register */ 837 /* Update LCR register */
699 regmap_write(s->regmap, MAX310X_LCR_REG, lcr); 838 max310x_port_write(port, MAX310X_LCR_REG, lcr);
700 839
701 /* Set read status mask */ 840 /* Set read status mask */
702 port->read_status_mask = MAX310X_LSR_RXOVR_BIT; 841 port->read_status_mask = MAX310X_LSR_RXOVR_BIT;
@@ -717,8 +856,8 @@ static void max310x_set_termios(struct uart_port *port,
717 MAX310X_LSR_RXBRK_BIT; 856 MAX310X_LSR_RXBRK_BIT;
718 857
719 /* Configure flow control */ 858 /* Configure flow control */
720 regmap_write(s->regmap, MAX310X_XON1_REG, termios->c_cc[VSTART]); 859 max310x_port_write(port, MAX310X_XON1_REG, termios->c_cc[VSTART]);
721 regmap_write(s->regmap, MAX310X_XOFF1_REG, termios->c_cc[VSTOP]); 860 max310x_port_write(port, MAX310X_XOFF1_REG, termios->c_cc[VSTOP]);
722 if (termios->c_cflag & CRTSCTS) 861 if (termios->c_cflag & CRTSCTS)
723 flow |= MAX310X_FLOWCTRL_AUTOCTS_BIT | 862 flow |= MAX310X_FLOWCTRL_AUTOCTS_BIT |
724 MAX310X_FLOWCTRL_AUTORTS_BIT; 863 MAX310X_FLOWCTRL_AUTORTS_BIT;
@@ -728,7 +867,7 @@ static void max310x_set_termios(struct uart_port *port,
728 if (termios->c_iflag & IXOFF) 867 if (termios->c_iflag & IXOFF)
729 flow |= MAX310X_FLOWCTRL_SWFLOW1_BIT | 868 flow |= MAX310X_FLOWCTRL_SWFLOW1_BIT |
730 MAX310X_FLOWCTRL_SWFLOWEN_BIT; 869 MAX310X_FLOWCTRL_SWFLOWEN_BIT;
731 regmap_write(s->regmap, MAX310X_FLOWCTRL_REG, flow); 870 max310x_port_write(port, MAX310X_FLOWCTRL_REG, flow);
732 871
733 /* Get baud rate generator configuration */ 872 /* Get baud rate generator configuration */
734 baud = uart_get_baud_rate(port, termios, old, 873 baud = uart_get_baud_rate(port, termios, old,
@@ -736,36 +875,30 @@ static void max310x_set_termios(struct uart_port *port,
736 port->uartclk / 4); 875 port->uartclk / 4);
737 876
738 /* Setup baudrate generator */ 877 /* Setup baudrate generator */
739 max310x_set_baud(s, baud); 878 max310x_set_baud(port, baud);
740 879
741 /* Update timeout according to new baud rate */ 880 /* Update timeout according to new baud rate */
742 uart_update_timeout(port, termios->c_cflag, baud); 881 uart_update_timeout(port, termios->c_cflag, baud);
743
744 mutex_unlock(&s->max310x_mutex);
745} 882}
746 883
747static int max310x_startup(struct uart_port *port) 884static int max310x_startup(struct uart_port *port)
748{ 885{
749 unsigned int val, line = port->line; 886 unsigned int val, line = port->line;
750 struct max310x_port *s = container_of(port, struct max310x_port, port); 887 struct max310x_port *s = dev_get_drvdata(port->dev);
751
752 if (s->pdata->suspend)
753 s->pdata->suspend(0);
754 888
755 mutex_lock(&s->max310x_mutex); 889 s->devtype->power(port, 1);
756 890
757 /* Configure baud rate, 9600 as default */ 891 /* Configure baud rate, 9600 as default */
758 max310x_set_baud(s, 9600); 892 max310x_set_baud(port, 9600);
759 893
760 /* Configure LCR register, 8N1 mode by default */ 894 /* Configure LCR register, 8N1 mode by default */
761 val = MAX310X_LCR_WORD_LEN_8; 895 max310x_port_write(port, MAX310X_LCR_REG, MAX310X_LCR_WORD_LEN_8);
762 regmap_write(s->regmap, MAX310X_LCR_REG, val);
763 896
764 /* Configure MODE1 register */ 897 /* Configure MODE1 register */
765 regmap_update_bits(s->regmap, MAX310X_MODE1_REG, 898 max310x_port_update(port, MAX310X_MODE1_REG,
766 MAX310X_MODE1_TRNSCVCTRL_BIT, 899 MAX310X_MODE1_TRNSCVCTRL_BIT,
767 (s->pdata->uart_flags[line] & MAX310X_AUTO_DIR_CTRL) 900 (s->pdata->uart_flags[line] & MAX310X_AUTO_DIR_CTRL)
768 ? MAX310X_MODE1_TRNSCVCTRL_BIT : 0); 901 ? MAX310X_MODE1_TRNSCVCTRL_BIT : 0);
769 902
770 /* Configure MODE2 register */ 903 /* Configure MODE2 register */
771 val = MAX310X_MODE2_RXEMPTINV_BIT; 904 val = MAX310X_MODE2_RXEMPTINV_BIT;
@@ -776,63 +909,40 @@ static int max310x_startup(struct uart_port *port)
776 909
777 /* Reset FIFOs */ 910 /* Reset FIFOs */
778 val |= MAX310X_MODE2_FIFORST_BIT; 911 val |= MAX310X_MODE2_FIFORST_BIT;
779 regmap_write(s->regmap, MAX310X_MODE2_REG, val); 912 max310x_port_write(port, MAX310X_MODE2_REG, val);
780 913 max310x_port_update(port, MAX310X_MODE2_REG,
781 /* Configure FIFO trigger level register */ 914 MAX310X_MODE2_FIFORST_BIT, 0);
782 /* RX FIFO trigger for 16 words, TX FIFO trigger for 64 words */
783 val = MAX310X_FIFOTRIGLVL_RX(16) | MAX310X_FIFOTRIGLVL_TX(64);
784 regmap_write(s->regmap, MAX310X_FIFOTRIGLVL_REG, val);
785 915
786 /* Configure flow control levels */ 916 /* Configure flow control levels */
787 /* Flow control halt level 96, resume level 48 */ 917 /* Flow control halt level 96, resume level 48 */
788 val = MAX310X_FLOWLVL_RES(48) | MAX310X_FLOWLVL_HALT(96); 918 max310x_port_write(port, MAX310X_FLOWLVL_REG,
789 regmap_write(s->regmap, MAX310X_FLOWLVL_REG, val); 919 MAX310X_FLOWLVL_RES(48) | MAX310X_FLOWLVL_HALT(96));
790
791 /* Clear timeout register */
792 regmap_write(s->regmap, MAX310X_RXTO_REG, 0);
793 920
794 /* Configure LSR interrupt enable register */ 921 /* Clear IRQ status register */
795 /* Enable RX timeout interrupt */ 922 max310x_port_read(port, MAX310X_IRQSTS_REG);
796 val = MAX310X_LSR_RXTO_BIT;
797 regmap_write(s->regmap, MAX310X_LSR_IRQEN_REG, val);
798 923
799 /* Clear FIFO reset */ 924 /* Enable RX, TX, CTS change interrupts */
800 regmap_update_bits(s->regmap, MAX310X_MODE2_REG, 925 val = MAX310X_IRQ_RXEMPTY_BIT | MAX310X_IRQ_TXEMPTY_BIT;
801 MAX310X_MODE2_FIFORST_BIT, 0); 926 max310x_port_write(port, MAX310X_IRQEN_REG, val | MAX310X_IRQ_CTS_BIT);
802
803 /* Clear IRQ status register by reading it */
804 regmap_read(s->regmap, MAX310X_IRQSTS_REG, &val);
805
806 /* Configure interrupt enable register */
807 /* Enable CTS change interrupt */
808 val = MAX310X_IRQ_CTS_BIT;
809 /* Enable RX, TX interrupts */
810 val |= MAX310X_IRQ_RX | MAX310X_IRQ_TX;
811 regmap_write(s->regmap, MAX310X_IRQEN_REG, val);
812
813 mutex_unlock(&s->max310x_mutex);
814 927
815 return 0; 928 return 0;
816} 929}
817 930
818static void max310x_shutdown(struct uart_port *port) 931static void max310x_shutdown(struct uart_port *port)
819{ 932{
820 struct max310x_port *s = container_of(port, struct max310x_port, port); 933 struct max310x_port *s = dev_get_drvdata(port->dev);
821 934
822 /* Disable all interrupts */ 935 /* Disable all interrupts */
823 mutex_lock(&s->max310x_mutex); 936 max310x_port_write(port, MAX310X_IRQEN_REG, 0);
824 regmap_write(s->regmap, MAX310X_IRQEN_REG, 0);
825 mutex_unlock(&s->max310x_mutex);
826 937
827 if (s->pdata->suspend) 938 s->devtype->power(port, 0);
828 s->pdata->suspend(1);
829} 939}
830 940
831static const char *max310x_type(struct uart_port *port) 941static const char *max310x_type(struct uart_port *port)
832{ 942{
833 struct max310x_port *s = container_of(port, struct max310x_port, port); 943 struct max310x_port *s = dev_get_drvdata(port->dev);
834 944
835 return (port->type == PORT_MAX310X) ? s->name : NULL; 945 return (port->type == PORT_MAX310X) ? s->devtype->name : NULL;
836} 946}
837 947
838static int max310x_request_port(struct uart_port *port) 948static int max310x_request_port(struct uart_port *port)
@@ -841,134 +951,99 @@ static int max310x_request_port(struct uart_port *port)
841 return 0; 951 return 0;
842} 952}
843 953
844static void max310x_release_port(struct uart_port *port)
845{
846 /* Do nothing */
847}
848
849static void max310x_config_port(struct uart_port *port, int flags) 954static void max310x_config_port(struct uart_port *port, int flags)
850{ 955{
851 if (flags & UART_CONFIG_TYPE) 956 if (flags & UART_CONFIG_TYPE)
852 port->type = PORT_MAX310X; 957 port->type = PORT_MAX310X;
853} 958}
854 959
855static int max310x_verify_port(struct uart_port *port, struct serial_struct *ser) 960static int max310x_verify_port(struct uart_port *port, struct serial_struct *s)
856{ 961{
857 if ((ser->type == PORT_UNKNOWN) || (ser->type == PORT_MAX310X)) 962 if ((s->type != PORT_UNKNOWN) && (s->type != PORT_MAX310X))
858 return 0; 963 return -EINVAL;
859 if (ser->irq == port->irq) 964 if (s->irq != port->irq)
860 return 0; 965 return -EINVAL;
861 966
862 return -EINVAL; 967 return 0;
863} 968}
864 969
865static struct uart_ops max310x_ops = { 970static void max310x_null_void(struct uart_port *port)
971{
972 /* Do nothing */
973}
974
975static const struct uart_ops max310x_ops = {
866 .tx_empty = max310x_tx_empty, 976 .tx_empty = max310x_tx_empty,
867 .set_mctrl = max310x_set_mctrl, 977 .set_mctrl = max310x_set_mctrl,
868 .get_mctrl = max310x_get_mctrl, 978 .get_mctrl = max310x_get_mctrl,
869 .stop_tx = max310x_stop_tx, 979 .stop_tx = max310x_null_void,
870 .start_tx = max310x_start_tx, 980 .start_tx = max310x_start_tx,
871 .stop_rx = max310x_stop_rx, 981 .stop_rx = max310x_null_void,
872 .enable_ms = max310x_enable_ms, 982 .enable_ms = max310x_null_void,
873 .break_ctl = max310x_break_ctl, 983 .break_ctl = max310x_break_ctl,
874 .startup = max310x_startup, 984 .startup = max310x_startup,
875 .shutdown = max310x_shutdown, 985 .shutdown = max310x_shutdown,
876 .set_termios = max310x_set_termios, 986 .set_termios = max310x_set_termios,
877 .type = max310x_type, 987 .type = max310x_type,
878 .request_port = max310x_request_port, 988 .request_port = max310x_request_port,
879 .release_port = max310x_release_port, 989 .release_port = max310x_null_void,
880 .config_port = max310x_config_port, 990 .config_port = max310x_config_port,
881 .verify_port = max310x_verify_port, 991 .verify_port = max310x_verify_port,
882}; 992};
883 993
884#ifdef CONFIG_PM_SLEEP 994static int __maybe_unused max310x_suspend(struct device *dev)
885
886static int max310x_suspend(struct device *dev)
887{ 995{
888 int ret;
889 struct max310x_port *s = dev_get_drvdata(dev); 996 struct max310x_port *s = dev_get_drvdata(dev);
997 int i;
890 998
891 dev_dbg(dev, "Suspend\n"); 999 for (i = 0; i < s->uart.nr; i++) {
892 1000 uart_suspend_port(&s->uart, &s->p[i].port);
893 ret = uart_suspend_port(&s->uart, &s->port); 1001 s->devtype->power(&s->p[i].port, 0);
894 1002 }
895 mutex_lock(&s->max310x_mutex);
896
897 /* Enable sleep mode */
898 regmap_update_bits(s->regmap, MAX310X_MODE1_REG,
899 MAX310X_MODE1_FORCESLEEP_BIT,
900 MAX310X_MODE1_FORCESLEEP_BIT);
901
902 mutex_unlock(&s->max310x_mutex);
903
904 if (s->pdata->suspend)
905 s->pdata->suspend(1);
906 1003
907 return ret; 1004 return 0;
908} 1005}
909 1006
910static int max310x_resume(struct device *dev) 1007static int __maybe_unused max310x_resume(struct device *dev)
911{ 1008{
912 struct max310x_port *s = dev_get_drvdata(dev); 1009 struct max310x_port *s = dev_get_drvdata(dev);
1010 int i;
913 1011
914 dev_dbg(dev, "Resume\n"); 1012 for (i = 0; i < s->uart.nr; i++) {
915 1013 s->devtype->power(&s->p[i].port, 1);
916 if (s->pdata->suspend) 1014 uart_resume_port(&s->uart, &s->p[i].port);
917 s->pdata->suspend(0); 1015 }
918
919 mutex_lock(&s->max310x_mutex);
920
921 /* Disable sleep mode */
922 regmap_update_bits(s->regmap, MAX310X_MODE1_REG,
923 MAX310X_MODE1_FORCESLEEP_BIT,
924 0);
925
926 max310x_wait_pll(s);
927
928 mutex_unlock(&s->max310x_mutex);
929 1016
930 return uart_resume_port(&s->uart, &s->port); 1017 return 0;
931} 1018}
932 1019
933static SIMPLE_DEV_PM_OPS(max310x_pm_ops, max310x_suspend, max310x_resume);
934#define MAX310X_PM_OPS (&max310x_pm_ops)
935
936#else
937#define MAX310X_PM_OPS NULL
938#endif
939
940#ifdef CONFIG_GPIOLIB 1020#ifdef CONFIG_GPIOLIB
941static int max310x_gpio_get(struct gpio_chip *chip, unsigned offset) 1021static int max310x_gpio_get(struct gpio_chip *chip, unsigned offset)
942{ 1022{
943 unsigned int val = 0; 1023 unsigned int val;
944 struct max310x_port *s = container_of(chip, struct max310x_port, gpio); 1024 struct max310x_port *s = container_of(chip, struct max310x_port, gpio);
1025 struct uart_port *port = &s->p[offset / 4].port;
945 1026
946 mutex_lock(&s->max310x_mutex); 1027 val = max310x_port_read(port, MAX310X_GPIODATA_REG);
947 regmap_read(s->regmap, MAX310X_GPIODATA_REG, &val);
948 mutex_unlock(&s->max310x_mutex);
949 1028
950 return !!((val >> 4) & (1 << offset)); 1029 return !!((val >> 4) & (1 << (offset % 4)));
951} 1030}
952 1031
953static void max310x_gpio_set(struct gpio_chip *chip, unsigned offset, int value) 1032static void max310x_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
954{ 1033{
955 struct max310x_port *s = container_of(chip, struct max310x_port, gpio); 1034 struct max310x_port *s = container_of(chip, struct max310x_port, gpio);
1035 struct uart_port *port = &s->p[offset / 4].port;
956 1036
957 mutex_lock(&s->max310x_mutex); 1037 max310x_port_update(port, MAX310X_GPIODATA_REG, 1 << (offset % 4),
958 regmap_update_bits(s->regmap, MAX310X_GPIODATA_REG, 1 << offset, value ? 1038 value ? 1 << (offset % 4) : 0);
959 1 << offset : 0);
960 mutex_unlock(&s->max310x_mutex);
961} 1039}
962 1040
963static int max310x_gpio_direction_input(struct gpio_chip *chip, unsigned offset) 1041static int max310x_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
964{ 1042{
965 struct max310x_port *s = container_of(chip, struct max310x_port, gpio); 1043 struct max310x_port *s = container_of(chip, struct max310x_port, gpio);
1044 struct uart_port *port = &s->p[offset / 4].port;
966 1045
967 mutex_lock(&s->max310x_mutex); 1046 max310x_port_update(port, MAX310X_GPIOCFG_REG, 1 << (offset % 4), 0);
968
969 regmap_update_bits(s->regmap, MAX310X_GPIOCFG_REG, 1 << offset, 0);
970
971 mutex_unlock(&s->max310x_mutex);
972 1047
973 return 0; 1048 return 0;
974} 1049}
@@ -977,74 +1052,42 @@ static int max310x_gpio_direction_output(struct gpio_chip *chip,
977 unsigned offset, int value) 1052 unsigned offset, int value)
978{ 1053{
979 struct max310x_port *s = container_of(chip, struct max310x_port, gpio); 1054 struct max310x_port *s = container_of(chip, struct max310x_port, gpio);
1055 struct uart_port *port = &s->p[offset / 4].port;
980 1056
981 mutex_lock(&s->max310x_mutex); 1057 max310x_port_update(port, MAX310X_GPIODATA_REG, 1 << (offset % 4),
982 1058 value ? 1 << (offset % 4) : 0);
983 regmap_update_bits(s->regmap, MAX310X_GPIOCFG_REG, 1 << offset, 1059 max310x_port_update(port, MAX310X_GPIOCFG_REG, 1 << (offset % 4),
984 1 << offset); 1060 1 << (offset % 4));
985 regmap_update_bits(s->regmap, MAX310X_GPIODATA_REG, 1 << offset, value ?
986 1 << offset : 0);
987
988 mutex_unlock(&s->max310x_mutex);
989 1061
990 return 0; 1062 return 0;
991} 1063}
992#endif 1064#endif
993 1065
994/* Generic platform data */ 1066static int max310x_probe(struct device *dev, int is_spi,
995static struct max310x_pdata generic_plat_data = { 1067 struct max310x_devtype *devtype, int irq)
996 .driver_flags = MAX310X_EXT_CLK,
997 .uart_flags[0] = MAX310X_ECHO_SUPRESS,
998 .frequency = 26000000,
999};
1000
1001static int max310x_probe(struct spi_device *spi)
1002{ 1068{
1003 struct max310x_port *s; 1069 struct max310x_port *s;
1004 struct device *dev = &spi->dev; 1070 struct max310x_pdata *pdata = dev_get_platdata(dev);
1005 int chiptype = spi_get_device_id(spi)->driver_data; 1071 int i, ret, uartclk;
1006 struct max310x_pdata *pdata = dev->platform_data;
1007 unsigned int val = 0;
1008 int ret;
1009 1072
1010 /* Check for IRQ */ 1073 /* Check for IRQ */
1011 if (spi->irq <= 0) { 1074 if (irq <= 0) {
1012 dev_err(dev, "No IRQ specified\n"); 1075 dev_err(dev, "No IRQ specified\n");
1013 return -ENOTSUPP; 1076 return -ENOTSUPP;
1014 } 1077 }
1015 1078
1079 if (!pdata) {
1080 dev_err(dev, "No platform data supplied\n");
1081 return -EINVAL;
1082 }
1083
1016 /* Alloc port structure */ 1084 /* Alloc port structure */
1017 s = devm_kzalloc(dev, sizeof(struct max310x_port), GFP_KERNEL); 1085 s = devm_kzalloc(dev, sizeof(*s) +
1086 sizeof(struct max310x_one) * devtype->nr, GFP_KERNEL);
1018 if (!s) { 1087 if (!s) {
1019 dev_err(dev, "Error allocating port structure\n"); 1088 dev_err(dev, "Error allocating port structure\n");
1020 return -ENOMEM; 1089 return -ENOMEM;
1021 } 1090 }
1022 dev_set_drvdata(dev, s);
1023
1024 if (!pdata) {
1025 dev_warn(dev, "No platform data supplied, using defaults\n");
1026 pdata = &generic_plat_data;
1027 }
1028 s->pdata = pdata;
1029
1030 /* Individual chip settings */
1031 switch (chiptype) {
1032 case MAX310X_TYPE_MAX3107:
1033 s->name = "MAX3107";
1034 s->nr_gpio = 4;
1035 s->uart.nr = 1;
1036 s->regcfg.max_register = 0x1f;
1037 break;
1038 case MAX310X_TYPE_MAX3108:
1039 s->name = "MAX3108";
1040 s->nr_gpio = 4;
1041 s->uart.nr = 1;
1042 s->regcfg.max_register = 0x1e;
1043 break;
1044 default:
1045 dev_err(dev, "Unsupported chip type %i\n", chiptype);
1046 return -ENOTSUPP;
1047 }
1048 1091
1049 /* Check input frequency */ 1092 /* Check input frequency */
1050 if ((pdata->driver_flags & MAX310X_EXT_CLK) && 1093 if ((pdata->driver_flags & MAX310X_EXT_CLK) &&
@@ -1055,13 +1098,11 @@ static int max310x_probe(struct spi_device *spi)
1055 ((pdata->frequency < 1000000) || (pdata->frequency > 4000000))) 1098 ((pdata->frequency < 1000000) || (pdata->frequency > 4000000)))
1056 goto err_freq; 1099 goto err_freq;
1057 1100
1058 mutex_init(&s->max310x_mutex); 1101 s->pdata = pdata;
1102 s->devtype = devtype;
1103 dev_set_drvdata(dev, s);
1059 1104
1060 /* Setup SPI bus */ 1105 mutex_init(&s->mutex);
1061 spi->mode = SPI_MODE_0;
1062 spi->bits_per_word = 8;
1063 spi->max_speed_hz = 26000000;
1064 spi_setup(spi);
1065 1106
1066 /* Setup regmap */ 1107 /* Setup regmap */
1067 s->regcfg.reg_bits = 8; 1108 s->regcfg.reg_bits = 8;
@@ -1069,109 +1110,100 @@ static int max310x_probe(struct spi_device *spi)
1069 s->regcfg.read_flag_mask = 0x00; 1110 s->regcfg.read_flag_mask = 0x00;
1070 s->regcfg.write_flag_mask = 0x80; 1111 s->regcfg.write_flag_mask = 0x80;
1071 s->regcfg.cache_type = REGCACHE_RBTREE; 1112 s->regcfg.cache_type = REGCACHE_RBTREE;
1072 s->regcfg.writeable_reg = max3107_8_reg_writeable; 1113 s->regcfg.writeable_reg = max310x_reg_writeable;
1073 s->regcfg.volatile_reg = max310x_reg_volatile; 1114 s->regcfg.volatile_reg = max310x_reg_volatile;
1074 s->regcfg.precious_reg = max310x_reg_precious; 1115 s->regcfg.precious_reg = max310x_reg_precious;
1075 s->regmap = devm_regmap_init_spi(spi, &s->regcfg); 1116 s->regcfg.max_register = devtype->nr * 0x20 - 1;
1117
1118 if (IS_ENABLED(CONFIG_SPI_MASTER) && is_spi) {
1119 struct spi_device *spi = to_spi_device(dev);
1120
1121 s->regmap = devm_regmap_init_spi(spi, &s->regcfg);
1122 } else
1123 return -ENOTSUPP;
1124
1076 if (IS_ERR(s->regmap)) { 1125 if (IS_ERR(s->regmap)) {
1077 ret = PTR_ERR(s->regmap);
1078 dev_err(dev, "Failed to initialize register map\n"); 1126 dev_err(dev, "Failed to initialize register map\n");
1079 goto err_out; 1127 return PTR_ERR(s->regmap);
1080 }
1081
1082 /* Reset chip & check SPI function */
1083 ret = regmap_write(s->regmap, MAX310X_MODE2_REG, MAX310X_MODE2_RST_BIT);
1084 if (ret) {
1085 dev_err(dev, "SPI transfer failed\n");
1086 goto err_out;
1087 }
1088 /* Clear chip reset */
1089 regmap_write(s->regmap, MAX310X_MODE2_REG, 0);
1090
1091 switch (chiptype) {
1092 case MAX310X_TYPE_MAX3107:
1093 /* Check REV ID to ensure we are talking to what we expect */
1094 regmap_read(s->regmap, MAX3107_REVID_REG, &val);
1095 if (((val & MAX3107_REV_MASK) != MAX3107_REV_ID)) {
1096 dev_err(dev, "%s ID 0x%02x does not match\n",
1097 s->name, val);
1098 ret = -ENODEV;
1099 goto err_out;
1100 }
1101 break;
1102 case MAX310X_TYPE_MAX3108:
1103 /* MAX3108 have not REV ID register, we just check default value
1104 * from clocksource register to make sure everything works.
1105 */
1106 regmap_read(s->regmap, MAX310X_CLKSRC_REG, &val);
1107 if (val != (MAX310X_CLKSRC_EXTCLK_BIT |
1108 MAX310X_CLKSRC_PLLBYP_BIT)) {
1109 dev_err(dev, "%s not present\n", s->name);
1110 ret = -ENODEV;
1111 goto err_out;
1112 }
1113 break;
1114 } 1128 }
1115 1129
1116 /* Board specific configure */ 1130 /* Board specific configure */
1117 if (pdata->init) 1131 if (s->pdata->init)
1118 pdata->init(); 1132 s->pdata->init();
1119 if (pdata->suspend) 1133
1120 pdata->suspend(0); 1134 /* Check device to ensure we are talking to what we expect */
1121 1135 ret = devtype->detect(dev);
1122 /* Calculate referecne clock */ 1136 if (ret)
1123 s->uartclk = max310x_set_ref_clk(s); 1137 return ret;
1124 1138
1125 /* Disable all interrupts */ 1139 for (i = 0; i < devtype->nr; i++) {
1126 regmap_write(s->regmap, MAX310X_IRQEN_REG, 0); 1140 unsigned int offs = i << 5;
1127 1141
1128 /* Setup MODE1 register */ 1142 /* Reset port */
1129 val = MAX310X_MODE1_IRQSEL_BIT; /* Enable IRQ pin */ 1143 regmap_write(s->regmap, MAX310X_MODE2_REG + offs,
1130 if (pdata->driver_flags & MAX310X_AUTOSLEEP) 1144 MAX310X_MODE2_RST_BIT);
1131 val = MAX310X_MODE1_AUTOSLEEP_BIT; 1145 /* Clear port reset */
1132 regmap_write(s->regmap, MAX310X_MODE1_REG, val); 1146 regmap_write(s->regmap, MAX310X_MODE2_REG + offs, 0);
1133 1147
1134 /* Setup interrupt */ 1148 /* Wait for port startup */
1135 ret = devm_request_threaded_irq(dev, spi->irq, NULL, max310x_ist, 1149 do {
1136 IRQF_TRIGGER_FALLING | IRQF_ONESHOT, 1150 regmap_read(s->regmap,
1137 dev_name(dev), s); 1151 MAX310X_BRGDIVLSB_REG + offs, &ret);
1138 if (ret) { 1152 } while (ret != 0x01);
1139 dev_err(dev, "Unable to reguest IRQ %i\n", spi->irq); 1153
1140 goto err_out; 1154 regmap_update_bits(s->regmap, MAX310X_MODE1_REG + offs,
1155 MAX310X_MODE1_AUTOSLEEP_BIT,
1156 MAX310X_MODE1_AUTOSLEEP_BIT);
1141 } 1157 }
1142 1158
1159 uartclk = max310x_set_ref_clk(s);
1160 dev_dbg(dev, "Reference clock set to %i Hz\n", uartclk);
1161
1143 /* Register UART driver */ 1162 /* Register UART driver */
1144 s->uart.owner = THIS_MODULE; 1163 s->uart.owner = THIS_MODULE;
1145 s->uart.driver_name = dev_name(dev);
1146 s->uart.dev_name = "ttyMAX"; 1164 s->uart.dev_name = "ttyMAX";
1147 s->uart.major = MAX310X_MAJOR; 1165 s->uart.major = MAX310X_MAJOR;
1148 s->uart.minor = MAX310X_MINOR; 1166 s->uart.minor = MAX310X_MINOR;
1167 s->uart.nr = devtype->nr;
1149 ret = uart_register_driver(&s->uart); 1168 ret = uart_register_driver(&s->uart);
1150 if (ret) { 1169 if (ret) {
1151 dev_err(dev, "Registering UART driver failed\n"); 1170 dev_err(dev, "Registering UART driver failed\n");
1152 goto err_out; 1171 return ret;
1153 } 1172 }
1154 1173
1155 /* Initialize workqueue for start TX */ 1174 for (i = 0; i < devtype->nr; i++) {
1156 s->wq = create_freezable_workqueue(dev_name(dev)); 1175 /* Initialize port data */
1157 INIT_WORK(&s->tx_work, max310x_wq_proc); 1176 s->p[i].port.line = i;
1158 1177 s->p[i].port.dev = dev;
1159 /* Initialize UART port data */ 1178 s->p[i].port.irq = irq;
1160 s->port.line = 0; 1179 s->p[i].port.type = PORT_MAX310X;
1161 s->port.dev = dev; 1180 s->p[i].port.fifosize = MAX310X_FIFO_SIZE;
1162 s->port.irq = spi->irq; 1181 s->p[i].port.flags = UPF_SKIP_TEST | UPF_FIXED_TYPE |
1163 s->port.type = PORT_MAX310X; 1182 UPF_LOW_LATENCY;
1164 s->port.fifosize = MAX310X_FIFO_SIZE; 1183 s->p[i].port.iotype = UPIO_PORT;
1165 s->port.flags = UPF_SKIP_TEST | UPF_FIXED_TYPE; 1184 s->p[i].port.iobase = i * 0x20;
1166 s->port.iotype = UPIO_PORT; 1185 s->p[i].port.membase = (void __iomem *)~0;
1167 s->port.membase = (void __iomem *)0xffffffff; /* Bogus value */ 1186 s->p[i].port.uartclk = uartclk;
1168 s->port.uartclk = s->uartclk; 1187 s->p[i].port.ops = &max310x_ops;
1169 s->port.ops = &max310x_ops; 1188 /* Disable all interrupts */
1170 uart_add_one_port(&s->uart, &s->port); 1189 max310x_port_write(&s->p[i].port, MAX310X_IRQEN_REG, 0);
1190 /* Clear IRQ status register */
1191 max310x_port_read(&s->p[i].port, MAX310X_IRQSTS_REG);
1192 /* Enable IRQ pin */
1193 max310x_port_update(&s->p[i].port, MAX310X_MODE1_REG,
1194 MAX310X_MODE1_IRQSEL_BIT,
1195 MAX310X_MODE1_IRQSEL_BIT);
1196 /* Initialize queue for start TX */
1197 INIT_WORK(&s->p[i].tx_work, max310x_wq_proc);
1198 /* Register port */
1199 uart_add_one_port(&s->uart, &s->p[i].port);
1200 /* Go to suspend mode */
1201 devtype->power(&s->p[i].port, 0);
1202 }
1171 1203
1172#ifdef CONFIG_GPIOLIB 1204#ifdef CONFIG_GPIOLIB
1173 /* Setup GPIO cotroller */ 1205 /* Setup GPIO cotroller */
1174 if (pdata->gpio_base) { 1206 if (s->pdata->gpio_base) {
1175 s->gpio.owner = THIS_MODULE; 1207 s->gpio.owner = THIS_MODULE;
1176 s->gpio.dev = dev; 1208 s->gpio.dev = dev;
1177 s->gpio.label = dev_name(dev); 1209 s->gpio.label = dev_name(dev);
@@ -1179,86 +1211,107 @@ static int max310x_probe(struct spi_device *spi)
1179 s->gpio.get = max310x_gpio_get; 1211 s->gpio.get = max310x_gpio_get;
1180 s->gpio.direction_output= max310x_gpio_direction_output; 1212 s->gpio.direction_output= max310x_gpio_direction_output;
1181 s->gpio.set = max310x_gpio_set; 1213 s->gpio.set = max310x_gpio_set;
1182 s->gpio.base = pdata->gpio_base; 1214 s->gpio.base = s->pdata->gpio_base;
1183 s->gpio.ngpio = s->nr_gpio; 1215 s->gpio.ngpio = devtype->nr * 4;
1184 s->gpio.can_sleep = 1; 1216 s->gpio.can_sleep = 1;
1185 if (gpiochip_add(&s->gpio)) { 1217 if (!gpiochip_add(&s->gpio))
1186 /* Indicate that we should not call gpiochip_remove */ 1218 s->gpio_used = 1;
1187 s->gpio.base = 0;
1188 }
1189 } else 1219 } else
1190 dev_info(dev, "GPIO support not enabled\n"); 1220 dev_info(dev, "GPIO support not enabled\n");
1191#endif 1221#endif
1192 1222
1193 /* Go to suspend mode */ 1223 /* Setup interrupt */
1194 if (pdata->suspend) 1224 ret = devm_request_threaded_irq(dev, irq, NULL, max310x_ist,
1195 pdata->suspend(1); 1225 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
1226 dev_name(dev), s);
1227 if (ret) {
1228 dev_err(dev, "Unable to reguest IRQ %i\n", irq);
1229#ifdef CONFIG_GPIOLIB
1230 if (s->gpio_used)
1231 WARN_ON(gpiochip_remove(&s->gpio));
1232#endif
1233 }
1196 1234
1197 return 0; 1235 return ret;
1198 1236
1199err_freq: 1237err_freq:
1200 dev_err(dev, "Frequency parameter incorrect\n"); 1238 dev_err(dev, "Frequency parameter incorrect\n");
1201 ret = -EINVAL; 1239 return -EINVAL;
1202
1203err_out:
1204 dev_set_drvdata(dev, NULL);
1205
1206 return ret;
1207} 1240}
1208 1241
1209static int max310x_remove(struct spi_device *spi) 1242static int max310x_remove(struct device *dev)
1210{ 1243{
1211 struct device *dev = &spi->dev;
1212 struct max310x_port *s = dev_get_drvdata(dev); 1244 struct max310x_port *s = dev_get_drvdata(dev);
1213 int ret = 0; 1245 int i, ret = 0;
1214
1215 dev_dbg(dev, "Removing port\n");
1216
1217 devm_free_irq(dev, s->port.irq, s);
1218
1219 destroy_workqueue(s->wq);
1220 1246
1221 uart_remove_one_port(&s->uart, &s->port); 1247 for (i = 0; i < s->uart.nr; i++) {
1248 cancel_work_sync(&s->p[i].tx_work);
1249 uart_remove_one_port(&s->uart, &s->p[i].port);
1250 s->devtype->power(&s->p[i].port, 0);
1251 }
1222 1252
1223 uart_unregister_driver(&s->uart); 1253 uart_unregister_driver(&s->uart);
1224 1254
1225#ifdef CONFIG_GPIOLIB 1255#ifdef CONFIG_GPIOLIB
1226 if (s->pdata->gpio_base) { 1256 if (s->gpio_used)
1227 ret = gpiochip_remove(&s->gpio); 1257 ret = gpiochip_remove(&s->gpio);
1228 if (ret)
1229 dev_err(dev, "Failed to remove gpio chip: %d\n", ret);
1230 }
1231#endif 1258#endif
1232 1259
1233 dev_set_drvdata(dev, NULL);
1234
1235 if (s->pdata->suspend)
1236 s->pdata->suspend(1);
1237 if (s->pdata->exit) 1260 if (s->pdata->exit)
1238 s->pdata->exit(); 1261 s->pdata->exit();
1239 1262
1240 return ret; 1263 return ret;
1241} 1264}
1242 1265
1266#ifdef CONFIG_SPI_MASTER
1267static int max310x_spi_probe(struct spi_device *spi)
1268{
1269 struct max310x_devtype *devtype =
1270 (struct max310x_devtype *)spi_get_device_id(spi)->driver_data;
1271 int ret;
1272
1273 /* Setup SPI bus */
1274 spi->bits_per_word = 8;
1275 spi->mode = spi->mode ? : SPI_MODE_0;
1276 spi->max_speed_hz = spi->max_speed_hz ? : 26000000;
1277 ret = spi_setup(spi);
1278 if (ret) {
1279 dev_err(&spi->dev, "SPI setup failed\n");
1280 return ret;
1281 }
1282
1283 return max310x_probe(&spi->dev, 1, devtype, spi->irq);
1284}
1285
1286static int max310x_spi_remove(struct spi_device *spi)
1287{
1288 return max310x_remove(&spi->dev);
1289}
1290
1291static SIMPLE_DEV_PM_OPS(max310x_pm_ops, max310x_suspend, max310x_resume);
1292
1243static const struct spi_device_id max310x_id_table[] = { 1293static const struct spi_device_id max310x_id_table[] = {
1244 { "max3107", MAX310X_TYPE_MAX3107 }, 1294 { "max3107", (kernel_ulong_t)&max3107_devtype, },
1245 { "max3108", MAX310X_TYPE_MAX3108 }, 1295 { "max3108", (kernel_ulong_t)&max3108_devtype, },
1296 { "max3109", (kernel_ulong_t)&max3109_devtype, },
1297 { "max14830", (kernel_ulong_t)&max14830_devtype, },
1246 { } 1298 { }
1247}; 1299};
1248MODULE_DEVICE_TABLE(spi, max310x_id_table); 1300MODULE_DEVICE_TABLE(spi, max310x_id_table);
1249 1301
1250static struct spi_driver max310x_driver = { 1302static struct spi_driver max310x_uart_driver = {
1251 .driver = { 1303 .driver = {
1252 .name = "max310x", 1304 .name = MAX310X_NAME,
1253 .owner = THIS_MODULE, 1305 .owner = THIS_MODULE,
1254 .pm = MAX310X_PM_OPS, 1306 .pm = &max310x_pm_ops,
1255 }, 1307 },
1256 .probe = max310x_probe, 1308 .probe = max310x_spi_probe,
1257 .remove = max310x_remove, 1309 .remove = max310x_spi_remove,
1258 .id_table = max310x_id_table, 1310 .id_table = max310x_id_table,
1259}; 1311};
1260module_spi_driver(max310x_driver); 1312module_spi_driver(max310x_uart_driver);
1313#endif
1261 1314
1262MODULE_LICENSE("GPL v2"); 1315MODULE_LICENSE("GPL");
1263MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>"); 1316MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
1264MODULE_DESCRIPTION("MAX310X serial driver"); 1317MODULE_DESCRIPTION("MAX310X serial driver");
diff --git a/drivers/tty/serial/mcf.c b/drivers/tty/serial/mcf.c
index 65be0c00c4bf..0edfaf8cd269 100644
--- a/drivers/tty/serial/mcf.c
+++ b/drivers/tty/serial/mcf.c
@@ -24,6 +24,7 @@
24#include <linux/serial_core.h> 24#include <linux/serial_core.h>
25#include <linux/io.h> 25#include <linux/io.h>
26#include <linux/uaccess.h> 26#include <linux/uaccess.h>
27#include <linux/platform_device.h>
27#include <asm/coldfire.h> 28#include <asm/coldfire.h>
28#include <asm/mcfsim.h> 29#include <asm/mcfsim.h>
29#include <asm/mcfuart.h> 30#include <asm/mcfuart.h>
@@ -324,7 +325,9 @@ static void mcf_rx_chars(struct mcf_uart *pp)
324 uart_insert_char(port, status, MCFUART_USR_RXOVERRUN, ch, flag); 325 uart_insert_char(port, status, MCFUART_USR_RXOVERRUN, ch, flag);
325 } 326 }
326 327
328 spin_unlock(&port->lock);
327 tty_flip_buffer_push(&port->state->port); 329 tty_flip_buffer_push(&port->state->port);
330 spin_lock(&port->lock);
328} 331}
329 332
330/****************************************************************************/ 333/****************************************************************************/
@@ -644,7 +647,7 @@ static struct uart_driver mcf_driver = {
644 647
645static int mcf_probe(struct platform_device *pdev) 648static int mcf_probe(struct platform_device *pdev)
646{ 649{
647 struct mcf_platform_uart *platp = pdev->dev.platform_data; 650 struct mcf_platform_uart *platp = dev_get_platdata(&pdev->dev);
648 struct uart_port *port; 651 struct uart_port *port;
649 int i; 652 int i;
650 653
diff --git a/drivers/tty/serial/mfd.c b/drivers/tty/serial/mfd.c
index 4a82267af83f..d3db042f649e 100644
--- a/drivers/tty/serial/mfd.c
+++ b/drivers/tty/serial/mfd.c
@@ -386,7 +386,7 @@ static void serial_hsu_stop_tx(struct uart_port *port)
386 386
387/* This is always called in spinlock protected mode, so 387/* This is always called in spinlock protected mode, so
388 * modify timeout timer is safe here */ 388 * modify timeout timer is safe here */
389void hsu_dma_rx(struct uart_hsu_port *up, u32 int_sts) 389void hsu_dma_rx(struct uart_hsu_port *up, u32 int_sts, unsigned long *flags)
390{ 390{
391 struct hsu_dma_buffer *dbuf = &up->rxbuf; 391 struct hsu_dma_buffer *dbuf = &up->rxbuf;
392 struct hsu_dma_chan *chan = up->rxc; 392 struct hsu_dma_chan *chan = up->rxc;
@@ -438,7 +438,9 @@ void hsu_dma_rx(struct uart_hsu_port *up, u32 int_sts)
438 | (0x1 << 16) 438 | (0x1 << 16)
439 | (0x1 << 24) /* timeout bit, see HSU Errata 1 */ 439 | (0x1 << 24) /* timeout bit, see HSU Errata 1 */
440 ); 440 );
441 spin_unlock_irqrestore(&up->port.lock, *flags);
441 tty_flip_buffer_push(tport); 442 tty_flip_buffer_push(tport);
443 spin_lock_irqsave(&up->port.lock, *flags);
442 444
443 chan_writel(chan, HSU_CH_CR, 0x3); 445 chan_writel(chan, HSU_CH_CR, 0x3);
444 446
@@ -459,7 +461,8 @@ static void serial_hsu_stop_rx(struct uart_port *port)
459 } 461 }
460} 462}
461 463
462static inline void receive_chars(struct uart_hsu_port *up, int *status) 464static inline void receive_chars(struct uart_hsu_port *up, int *status,
465 unsigned long *flags)
463{ 466{
464 unsigned int ch, flag; 467 unsigned int ch, flag;
465 unsigned int max_count = 256; 468 unsigned int max_count = 256;
@@ -519,7 +522,10 @@ static inline void receive_chars(struct uart_hsu_port *up, int *status)
519 ignore_char: 522 ignore_char:
520 *status = serial_in(up, UART_LSR); 523 *status = serial_in(up, UART_LSR);
521 } while ((*status & UART_LSR_DR) && max_count--); 524 } while ((*status & UART_LSR_DR) && max_count--);
525
526 spin_unlock_irqrestore(&up->port.lock, *flags);
522 tty_flip_buffer_push(&up->port.state->port); 527 tty_flip_buffer_push(&up->port.state->port);
528 spin_lock_irqsave(&up->port.lock, *flags);
523} 529}
524 530
525static void transmit_chars(struct uart_hsu_port *up) 531static void transmit_chars(struct uart_hsu_port *up)
@@ -613,7 +619,7 @@ static irqreturn_t port_irq(int irq, void *dev_id)
613 619
614 lsr = serial_in(up, UART_LSR); 620 lsr = serial_in(up, UART_LSR);
615 if (lsr & UART_LSR_DR) 621 if (lsr & UART_LSR_DR)
616 receive_chars(up, &lsr); 622 receive_chars(up, &lsr, &flags);
617 check_modem_status(up); 623 check_modem_status(up);
618 624
619 /* lsr will be renewed during the receive_chars */ 625 /* lsr will be renewed during the receive_chars */
@@ -643,7 +649,7 @@ static inline void dma_chan_irq(struct hsu_dma_chan *chan)
643 649
644 /* Rx channel */ 650 /* Rx channel */
645 if (chan->dirt == DMA_FROM_DEVICE) 651 if (chan->dirt == DMA_FROM_DEVICE)
646 hsu_dma_rx(up, int_sts); 652 hsu_dma_rx(up, int_sts, &flags);
647 653
648 /* Tx channel */ 654 /* Tx channel */
649 if (chan->dirt == DMA_TO_DEVICE) { 655 if (chan->dirt == DMA_TO_DEVICE) {
diff --git a/drivers/tty/serial/mpsc.c b/drivers/tty/serial/mpsc.c
index bc24f4931670..8d702677acc5 100644
--- a/drivers/tty/serial/mpsc.c
+++ b/drivers/tty/serial/mpsc.c
@@ -934,7 +934,7 @@ static int serial_polled;
934 ****************************************************************************** 934 ******************************************************************************
935 */ 935 */
936 936
937static int mpsc_rx_intr(struct mpsc_port_info *pi) 937static int mpsc_rx_intr(struct mpsc_port_info *pi, unsigned long *flags)
938{ 938{
939 struct mpsc_rx_desc *rxre; 939 struct mpsc_rx_desc *rxre;
940 struct tty_port *port = &pi->port.state->port; 940 struct tty_port *port = &pi->port.state->port;
@@ -969,8 +969,11 @@ static int mpsc_rx_intr(struct mpsc_port_info *pi)
969#endif 969#endif
970 /* Following use of tty struct directly is deprecated */ 970 /* Following use of tty struct directly is deprecated */
971 if (tty_buffer_request_room(port, bytes_in) < bytes_in) { 971 if (tty_buffer_request_room(port, bytes_in) < bytes_in) {
972 if (port->low_latency) 972 if (port->low_latency) {
973 spin_unlock_irqrestore(&pi->port.lock, *flags);
973 tty_flip_buffer_push(port); 974 tty_flip_buffer_push(port);
975 spin_lock_irqsave(&pi->port.lock, *flags);
976 }
974 /* 977 /*
975 * If this failed then we will throw away the bytes 978 * If this failed then we will throw away the bytes
976 * but must do so to clear interrupts. 979 * but must do so to clear interrupts.
@@ -1080,7 +1083,9 @@ next_frame:
1080 if ((readl(pi->sdma_base + SDMA_SDCM) & SDMA_SDCM_ERD) == 0) 1083 if ((readl(pi->sdma_base + SDMA_SDCM) & SDMA_SDCM_ERD) == 0)
1081 mpsc_start_rx(pi); 1084 mpsc_start_rx(pi);
1082 1085
1086 spin_unlock_irqrestore(&pi->port.lock, *flags);
1083 tty_flip_buffer_push(port); 1087 tty_flip_buffer_push(port);
1088 spin_lock_irqsave(&pi->port.lock, *flags);
1084 return rc; 1089 return rc;
1085} 1090}
1086 1091
@@ -1222,7 +1227,7 @@ static irqreturn_t mpsc_sdma_intr(int irq, void *dev_id)
1222 1227
1223 spin_lock_irqsave(&pi->port.lock, iflags); 1228 spin_lock_irqsave(&pi->port.lock, iflags);
1224 mpsc_sdma_intr_ack(pi); 1229 mpsc_sdma_intr_ack(pi);
1225 if (mpsc_rx_intr(pi)) 1230 if (mpsc_rx_intr(pi, &iflags))
1226 rc = IRQ_HANDLED; 1231 rc = IRQ_HANDLED;
1227 if (mpsc_tx_intr(pi)) 1232 if (mpsc_tx_intr(pi))
1228 rc = IRQ_HANDLED; 1233 rc = IRQ_HANDLED;
@@ -1884,7 +1889,7 @@ static int mpsc_shared_drv_probe(struct platform_device *dev)
1884 if (dev->id == 0) { 1889 if (dev->id == 0) {
1885 if (!(rc = mpsc_shared_map_regs(dev))) { 1890 if (!(rc = mpsc_shared_map_regs(dev))) {
1886 pdata = (struct mpsc_shared_pdata *) 1891 pdata = (struct mpsc_shared_pdata *)
1887 dev->dev.platform_data; 1892 dev_get_platdata(&dev->dev);
1888 1893
1889 mpsc_shared_regs.MPSC_MRR_m = pdata->mrr_val; 1894 mpsc_shared_regs.MPSC_MRR_m = pdata->mrr_val;
1890 mpsc_shared_regs.MPSC_RCRR_m= pdata->rcrr_val; 1895 mpsc_shared_regs.MPSC_RCRR_m= pdata->rcrr_val;
@@ -2025,7 +2030,7 @@ static void mpsc_drv_get_platform_data(struct mpsc_port_info *pi,
2025{ 2030{
2026 struct mpsc_pdata *pdata; 2031 struct mpsc_pdata *pdata;
2027 2032
2028 pdata = (struct mpsc_pdata *)pd->dev.platform_data; 2033 pdata = (struct mpsc_pdata *)dev_get_platdata(&pd->dev);
2029 2034
2030 pi->port.uartclk = pdata->brg_clk_freq; 2035 pi->port.uartclk = pdata->brg_clk_freq;
2031 pi->port.iotype = UPIO_MEM; 2036 pi->port.iotype = UPIO_MEM;
diff --git a/drivers/tty/serial/mrst_max3110.c b/drivers/tty/serial/mrst_max3110.c
index 9b6ef20420c0..a67e7081f001 100644
--- a/drivers/tty/serial/mrst_max3110.c
+++ b/drivers/tty/serial/mrst_max3110.c
@@ -713,7 +713,7 @@ static void serial_m3110_enable_ms(struct uart_port *port)
713{ 713{
714} 714}
715 715
716struct uart_ops serial_m3110_ops = { 716static struct uart_ops serial_m3110_ops = {
717 .tx_empty = serial_m3110_tx_empty, 717 .tx_empty = serial_m3110_tx_empty,
718 .set_mctrl = serial_m3110_set_mctrl, 718 .set_mctrl = serial_m3110_set_mctrl,
719 .get_mctrl = serial_m3110_get_mctrl, 719 .get_mctrl = serial_m3110_get_mctrl,
@@ -844,7 +844,7 @@ static int serial_m3110_probe(struct spi_device *spi)
844 pmax = max; 844 pmax = max;
845 845
846 /* Give membase a psudo value to pass serial_core's check */ 846 /* Give membase a psudo value to pass serial_core's check */
847 max->port.membase = (void *)0xff110000; 847 max->port.membase = (unsigned char __iomem *)0xff110000;
848 uart_add_one_port(&serial_m3110_reg, &max->port); 848 uart_add_one_port(&serial_m3110_reg, &max->port);
849 849
850 return 0; 850 return 0;
diff --git a/drivers/tty/serial/msm_serial.c b/drivers/tty/serial/msm_serial.c
index 2c6cfb3cf032..b5d779cd3c2b 100644
--- a/drivers/tty/serial/msm_serial.c
+++ b/drivers/tty/serial/msm_serial.c
@@ -45,16 +45,19 @@ struct msm_port {
45 struct clk *clk; 45 struct clk *clk;
46 struct clk *pclk; 46 struct clk *pclk;
47 unsigned int imr; 47 unsigned int imr;
48 unsigned int *gsbi_base; 48 void __iomem *gsbi_base;
49 int is_uartdm; 49 int is_uartdm;
50 unsigned int old_snap_state; 50 unsigned int old_snap_state;
51}; 51};
52 52
53static inline void wait_for_xmitr(struct uart_port *port, int bits) 53static inline void wait_for_xmitr(struct uart_port *port)
54{ 54{
55 if (!(msm_read(port, UART_SR) & UART_SR_TX_EMPTY)) 55 while (!(msm_read(port, UART_SR) & UART_SR_TX_EMPTY)) {
56 while ((msm_read(port, UART_ISR) & bits) != bits) 56 if (msm_read(port, UART_ISR) & UART_ISR_TX_READY)
57 cpu_relax(); 57 break;
58 udelay(1);
59 }
60 msm_write(port, UART_CR_CMD_RESET_TX_READY, UART_CR);
58} 61}
59 62
60static void msm_stop_tx(struct uart_port *port) 63static void msm_stop_tx(struct uart_port *port)
@@ -137,7 +140,10 @@ static void handle_rx_dm(struct uart_port *port, unsigned int misr)
137 count -= 4; 140 count -= 4;
138 } 141 }
139 142
143 spin_unlock(&port->lock);
140 tty_flip_buffer_push(tport); 144 tty_flip_buffer_push(tport);
145 spin_lock(&port->lock);
146
141 if (misr & (UART_IMR_RXSTALE)) 147 if (misr & (UART_IMR_RXSTALE))
142 msm_write(port, UART_CR_CMD_RESET_STALE_INT, UART_CR); 148 msm_write(port, UART_CR_CMD_RESET_STALE_INT, UART_CR);
143 msm_write(port, 0xFFFFFF, UARTDM_DMRX); 149 msm_write(port, 0xFFFFFF, UARTDM_DMRX);
@@ -189,52 +195,69 @@ static void handle_rx(struct uart_port *port)
189 tty_insert_flip_char(tport, c, flag); 195 tty_insert_flip_char(tport, c, flag);
190 } 196 }
191 197
198 spin_unlock(&port->lock);
192 tty_flip_buffer_push(tport); 199 tty_flip_buffer_push(tport);
200 spin_lock(&port->lock);
193} 201}
194 202
195static void reset_dm_count(struct uart_port *port) 203static void reset_dm_count(struct uart_port *port, int count)
196{ 204{
197 wait_for_xmitr(port, UART_ISR_TX_READY); 205 wait_for_xmitr(port);
198 msm_write(port, 1, UARTDM_NCF_TX); 206 msm_write(port, count, UARTDM_NCF_TX);
207 msm_read(port, UARTDM_NCF_TX);
199} 208}
200 209
201static void handle_tx(struct uart_port *port) 210static void handle_tx(struct uart_port *port)
202{ 211{
203 struct circ_buf *xmit = &port->state->xmit; 212 struct circ_buf *xmit = &port->state->xmit;
204 struct msm_port *msm_port = UART_TO_MSM(port); 213 struct msm_port *msm_port = UART_TO_MSM(port);
205 int sent_tx; 214 unsigned int tx_count, num_chars;
215 unsigned int tf_pointer = 0;
216
217 tx_count = uart_circ_chars_pending(xmit);
218 tx_count = min3(tx_count, (unsigned int)UART_XMIT_SIZE - xmit->tail,
219 port->fifosize);
206 220
207 if (port->x_char) { 221 if (port->x_char) {
208 if (msm_port->is_uartdm) 222 if (msm_port->is_uartdm)
209 reset_dm_count(port); 223 reset_dm_count(port, tx_count + 1);
210 224
211 msm_write(port, port->x_char, 225 msm_write(port, port->x_char,
212 msm_port->is_uartdm ? UARTDM_TF : UART_TF); 226 msm_port->is_uartdm ? UARTDM_TF : UART_TF);
213 port->icount.tx++; 227 port->icount.tx++;
214 port->x_char = 0; 228 port->x_char = 0;
229 } else if (tx_count && msm_port->is_uartdm) {
230 reset_dm_count(port, tx_count);
215 } 231 }
216 232
217 if (msm_port->is_uartdm) 233 while (tf_pointer < tx_count) {
218 reset_dm_count(port); 234 int i;
235 char buf[4] = { 0 };
236 unsigned int *bf = (unsigned int *)&buf;
219 237
220 while (msm_read(port, UART_SR) & UART_SR_TX_READY) { 238 if (!(msm_read(port, UART_SR) & UART_SR_TX_READY))
221 if (uart_circ_empty(xmit)) {
222 /* disable tx interrupts */
223 msm_port->imr &= ~UART_IMR_TXLEV;
224 msm_write(port, msm_port->imr, UART_IMR);
225 break; 239 break;
226 }
227 msm_write(port, xmit->buf[xmit->tail],
228 msm_port->is_uartdm ? UARTDM_TF : UART_TF);
229 240
230 if (msm_port->is_uartdm) 241 if (msm_port->is_uartdm)
231 reset_dm_count(port); 242 num_chars = min(tx_count - tf_pointer,
243 (unsigned int)sizeof(buf));
244 else
245 num_chars = 1;
232 246
233 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); 247 for (i = 0; i < num_chars; i++) {
234 port->icount.tx++; 248 buf[i] = xmit->buf[xmit->tail + i];
235 sent_tx = 1; 249 port->icount.tx++;
250 }
251
252 msm_write(port, *bf, msm_port->is_uartdm ? UARTDM_TF : UART_TF);
253 xmit->tail = (xmit->tail + num_chars) & (UART_XMIT_SIZE - 1);
254 tf_pointer += num_chars;
236 } 255 }
237 256
257 /* disable tx interrupts if nothing more to send */
258 if (uart_circ_empty(xmit))
259 msm_stop_tx(port);
260
238 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 261 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
239 uart_write_wakeup(port); 262 uart_write_wakeup(port);
240} 263}
@@ -295,7 +318,7 @@ static void msm_reset(struct uart_port *port)
295 msm_write(port, UART_CR_CMD_SET_RFR, UART_CR); 318 msm_write(port, UART_CR_CMD_SET_RFR, UART_CR);
296} 319}
297 320
298void msm_set_mctrl(struct uart_port *port, unsigned int mctrl) 321static void msm_set_mctrl(struct uart_port *port, unsigned int mctrl)
299{ 322{
300 unsigned int mr; 323 unsigned int mr;
301 mr = msm_read(port, UART_MR1); 324 mr = msm_read(port, UART_MR1);
@@ -318,70 +341,60 @@ static void msm_break_ctl(struct uart_port *port, int break_ctl)
318 msm_write(port, UART_CR_CMD_STOP_BREAK, UART_CR); 341 msm_write(port, UART_CR_CMD_STOP_BREAK, UART_CR);
319} 342}
320 343
344struct msm_baud_map {
345 u16 divisor;
346 u8 code;
347 u8 rxstale;
348};
349
350static const struct msm_baud_map *
351msm_find_best_baud(struct uart_port *port, unsigned int baud)
352{
353 unsigned int i, divisor;
354 const struct msm_baud_map *entry;
355 static const struct msm_baud_map table[] = {
356 { 1536, 0x00, 1 },
357 { 768, 0x11, 1 },
358 { 384, 0x22, 1 },
359 { 192, 0x33, 1 },
360 { 96, 0x44, 1 },
361 { 48, 0x55, 1 },
362 { 32, 0x66, 1 },
363 { 24, 0x77, 1 },
364 { 16, 0x88, 1 },
365 { 12, 0x99, 6 },
366 { 8, 0xaa, 6 },
367 { 6, 0xbb, 6 },
368 { 4, 0xcc, 6 },
369 { 3, 0xdd, 8 },
370 { 2, 0xee, 16 },
371 { 1, 0xff, 31 },
372 };
373
374 divisor = uart_get_divisor(port, baud);
375
376 for (i = 0, entry = table; i < ARRAY_SIZE(table); i++, entry++)
377 if (entry->divisor <= divisor)
378 break;
379
380 return entry; /* Default to smallest divider */
381}
382
321static int msm_set_baud_rate(struct uart_port *port, unsigned int baud) 383static int msm_set_baud_rate(struct uart_port *port, unsigned int baud)
322{ 384{
323 unsigned int baud_code, rxstale, watermark; 385 unsigned int rxstale, watermark;
324 struct msm_port *msm_port = UART_TO_MSM(port); 386 struct msm_port *msm_port = UART_TO_MSM(port);
387 const struct msm_baud_map *entry;
325 388
326 switch (baud) { 389 entry = msm_find_best_baud(port, baud);
327 case 300:
328 baud_code = UART_CSR_300;
329 rxstale = 1;
330 break;
331 case 600:
332 baud_code = UART_CSR_600;
333 rxstale = 1;
334 break;
335 case 1200:
336 baud_code = UART_CSR_1200;
337 rxstale = 1;
338 break;
339 case 2400:
340 baud_code = UART_CSR_2400;
341 rxstale = 1;
342 break;
343 case 4800:
344 baud_code = UART_CSR_4800;
345 rxstale = 1;
346 break;
347 case 9600:
348 baud_code = UART_CSR_9600;
349 rxstale = 2;
350 break;
351 case 14400:
352 baud_code = UART_CSR_14400;
353 rxstale = 3;
354 break;
355 case 19200:
356 baud_code = UART_CSR_19200;
357 rxstale = 4;
358 break;
359 case 28800:
360 baud_code = UART_CSR_28800;
361 rxstale = 6;
362 break;
363 case 38400:
364 baud_code = UART_CSR_38400;
365 rxstale = 8;
366 break;
367 case 57600:
368 baud_code = UART_CSR_57600;
369 rxstale = 16;
370 break;
371 case 115200:
372 default:
373 baud_code = UART_CSR_115200;
374 baud = 115200;
375 rxstale = 31;
376 break;
377 }
378 390
379 if (msm_port->is_uartdm) 391 if (msm_port->is_uartdm)
380 msm_write(port, UART_CR_CMD_RESET_RX, UART_CR); 392 msm_write(port, UART_CR_CMD_RESET_RX, UART_CR);
381 393
382 msm_write(port, baud_code, UART_CSR); 394 msm_write(port, entry->code, UART_CSR);
383 395
384 /* RX stale watermark */ 396 /* RX stale watermark */
397 rxstale = entry->rxstale;
385 watermark = UART_IPR_STALE_LSB & rxstale; 398 watermark = UART_IPR_STALE_LSB & rxstale;
386 watermark |= UART_IPR_RXSTALE_LAST; 399 watermark |= UART_IPR_RXSTALE_LAST;
387 watermark |= UART_IPR_STALE_TIMEOUT_MSB & (rxstale << 2); 400 watermark |= UART_IPR_STALE_TIMEOUT_MSB & (rxstale << 2);
@@ -409,8 +422,7 @@ static void msm_init_clock(struct uart_port *port)
409 struct msm_port *msm_port = UART_TO_MSM(port); 422 struct msm_port *msm_port = UART_TO_MSM(port);
410 423
411 clk_prepare_enable(msm_port->clk); 424 clk_prepare_enable(msm_port->clk);
412 if (!IS_ERR(msm_port->pclk)) 425 clk_prepare_enable(msm_port->pclk);
413 clk_prepare_enable(msm_port->pclk);
414 msm_serial_set_mnd_regs(port); 426 msm_serial_set_mnd_regs(port);
415} 427}
416 428
@@ -589,12 +601,10 @@ static void msm_release_port(struct uart_port *port)
589 port->membase = NULL; 601 port->membase = NULL;
590 602
591 if (msm_port->gsbi_base) { 603 if (msm_port->gsbi_base) {
592 iowrite32(GSBI_PROTOCOL_IDLE, msm_port->gsbi_base + 604 writel_relaxed(GSBI_PROTOCOL_IDLE,
593 GSBI_CONTROL); 605 msm_port->gsbi_base + GSBI_CONTROL);
594
595 gsbi_resource = platform_get_resource(pdev,
596 IORESOURCE_MEM, 1);
597 606
607 gsbi_resource = platform_get_resource(pdev, IORESOURCE_MEM, 1);
598 if (unlikely(!gsbi_resource)) 608 if (unlikely(!gsbi_resource))
599 return; 609 return;
600 610
@@ -637,7 +647,7 @@ static int msm_request_port(struct uart_port *port)
637 if (!request_mem_region(gsbi_resource->start, size, 647 if (!request_mem_region(gsbi_resource->start, size,
638 "msm_serial")) { 648 "msm_serial")) {
639 ret = -EBUSY; 649 ret = -EBUSY;
640 goto fail_release_port; 650 goto fail_release_port_membase;
641 } 651 }
642 652
643 msm_port->gsbi_base = ioremap(gsbi_resource->start, size); 653 msm_port->gsbi_base = ioremap(gsbi_resource->start, size);
@@ -651,6 +661,8 @@ static int msm_request_port(struct uart_port *port)
651 661
652fail_release_gsbi: 662fail_release_gsbi:
653 release_mem_region(gsbi_resource->start, size); 663 release_mem_region(gsbi_resource->start, size);
664fail_release_port_membase:
665 iounmap(port->membase);
654fail_release_port: 666fail_release_port:
655 release_mem_region(port->mapbase, size); 667 release_mem_region(port->mapbase, size);
656 return ret; 668 return ret;
@@ -666,10 +678,9 @@ static void msm_config_port(struct uart_port *port, int flags)
666 if (ret) 678 if (ret)
667 return; 679 return;
668 } 680 }
669 681 if (msm_port->gsbi_base)
670 if (msm_port->is_uartdm) 682 writel_relaxed(GSBI_PROTOCOL_UART,
671 iowrite32(GSBI_PROTOCOL_UART, msm_port->gsbi_base + 683 msm_port->gsbi_base + GSBI_CONTROL);
672 GSBI_CONTROL);
673} 684}
674 685
675static int msm_verify_port(struct uart_port *port, struct serial_struct *ser) 686static int msm_verify_port(struct uart_port *port, struct serial_struct *ser)
@@ -689,13 +700,11 @@ static void msm_power(struct uart_port *port, unsigned int state,
689 switch (state) { 700 switch (state) {
690 case 0: 701 case 0:
691 clk_prepare_enable(msm_port->clk); 702 clk_prepare_enable(msm_port->clk);
692 if (!IS_ERR(msm_port->pclk)) 703 clk_prepare_enable(msm_port->pclk);
693 clk_prepare_enable(msm_port->pclk);
694 break; 704 break;
695 case 3: 705 case 3:
696 clk_disable_unprepare(msm_port->clk); 706 clk_disable_unprepare(msm_port->clk);
697 if (!IS_ERR(msm_port->pclk)) 707 clk_disable_unprepare(msm_port->pclk);
698 clk_disable_unprepare(msm_port->pclk);
699 break; 708 break;
700 default: 709 default:
701 printk(KERN_ERR "msm_serial: Unknown PM state %d\n", state); 710 printk(KERN_ERR "msm_serial: Unknown PM state %d\n", state);
@@ -760,32 +769,63 @@ static inline struct uart_port *get_port_from_line(unsigned int line)
760} 769}
761 770
762#ifdef CONFIG_SERIAL_MSM_CONSOLE 771#ifdef CONFIG_SERIAL_MSM_CONSOLE
763
764static void msm_console_putchar(struct uart_port *port, int c)
765{
766 struct msm_port *msm_port = UART_TO_MSM(port);
767
768 if (msm_port->is_uartdm)
769 reset_dm_count(port);
770
771 while (!(msm_read(port, UART_SR) & UART_SR_TX_READY))
772 ;
773 msm_write(port, c, msm_port->is_uartdm ? UARTDM_TF : UART_TF);
774}
775
776static void msm_console_write(struct console *co, const char *s, 772static void msm_console_write(struct console *co, const char *s,
777 unsigned int count) 773 unsigned int count)
778{ 774{
775 int i;
779 struct uart_port *port; 776 struct uart_port *port;
780 struct msm_port *msm_port; 777 struct msm_port *msm_port;
778 int num_newlines = 0;
779 bool replaced = false;
781 780
782 BUG_ON(co->index < 0 || co->index >= UART_NR); 781 BUG_ON(co->index < 0 || co->index >= UART_NR);
783 782
784 port = get_port_from_line(co->index); 783 port = get_port_from_line(co->index);
785 msm_port = UART_TO_MSM(port); 784 msm_port = UART_TO_MSM(port);
786 785
786 /* Account for newlines that will get a carriage return added */
787 for (i = 0; i < count; i++)
788 if (s[i] == '\n')
789 num_newlines++;
790 count += num_newlines;
791
787 spin_lock(&port->lock); 792 spin_lock(&port->lock);
788 uart_console_write(port, s, count, msm_console_putchar); 793 if (msm_port->is_uartdm)
794 reset_dm_count(port, count);
795
796 i = 0;
797 while (i < count) {
798 int j;
799 unsigned int num_chars;
800 char buf[4] = { 0 };
801 unsigned int *bf = (unsigned int *)&buf;
802
803 if (msm_port->is_uartdm)
804 num_chars = min(count - i, (unsigned int)sizeof(buf));
805 else
806 num_chars = 1;
807
808 for (j = 0; j < num_chars; j++) {
809 char c = *s;
810
811 if (c == '\n' && !replaced) {
812 buf[j] = '\r';
813 j++;
814 replaced = true;
815 }
816 if (j < num_chars) {
817 buf[j] = c;
818 s++;
819 replaced = false;
820 }
821 }
822
823 while (!(msm_read(port, UART_SR) & UART_SR_TX_READY))
824 cpu_relax();
825
826 msm_write(port, *bf, msm_port->is_uartdm ? UARTDM_TF : UART_TF);
827 i += num_chars;
828 }
789 spin_unlock(&port->lock); 829 spin_unlock(&port->lock);
790} 830}
791 831
@@ -859,6 +899,11 @@ static struct uart_driver msm_uart_driver = {
859 899
860static atomic_t msm_uart_next_id = ATOMIC_INIT(0); 900static atomic_t msm_uart_next_id = ATOMIC_INIT(0);
861 901
902static const struct of_device_id msm_uartdm_table[] = {
903 { .compatible = "qcom,msm-uartdm" },
904 { }
905};
906
862static int __init msm_serial_probe(struct platform_device *pdev) 907static int __init msm_serial_probe(struct platform_device *pdev)
863{ 908{
864 struct msm_port *msm_port; 909 struct msm_port *msm_port;
@@ -878,23 +923,17 @@ static int __init msm_serial_probe(struct platform_device *pdev)
878 port->dev = &pdev->dev; 923 port->dev = &pdev->dev;
879 msm_port = UART_TO_MSM(port); 924 msm_port = UART_TO_MSM(port);
880 925
881 if (platform_get_resource(pdev, IORESOURCE_MEM, 1)) 926 if (of_match_device(msm_uartdm_table, &pdev->dev))
882 msm_port->is_uartdm = 1; 927 msm_port->is_uartdm = 1;
883 else 928 else
884 msm_port->is_uartdm = 0; 929 msm_port->is_uartdm = 0;
885 930
886 if (msm_port->is_uartdm) { 931 msm_port->clk = devm_clk_get(&pdev->dev, "core");
887 msm_port->clk = devm_clk_get(&pdev->dev, "gsbi_uart_clk");
888 msm_port->pclk = devm_clk_get(&pdev->dev, "gsbi_pclk");
889 } else {
890 msm_port->clk = devm_clk_get(&pdev->dev, "uart_clk");
891 msm_port->pclk = ERR_PTR(-ENOENT);
892 }
893
894 if (IS_ERR(msm_port->clk)) 932 if (IS_ERR(msm_port->clk))
895 return PTR_ERR(msm_port->clk); 933 return PTR_ERR(msm_port->clk);
896 934
897 if (msm_port->is_uartdm) { 935 if (msm_port->is_uartdm) {
936 msm_port->pclk = devm_clk_get(&pdev->dev, "iface");
898 if (IS_ERR(msm_port->pclk)) 937 if (IS_ERR(msm_port->pclk))
899 return PTR_ERR(msm_port->pclk); 938 return PTR_ERR(msm_port->pclk);
900 939
@@ -931,6 +970,7 @@ static int msm_serial_remove(struct platform_device *pdev)
931 970
932static struct of_device_id msm_match_table[] = { 971static struct of_device_id msm_match_table[] = {
933 { .compatible = "qcom,msm-uart" }, 972 { .compatible = "qcom,msm-uart" },
973 { .compatible = "qcom,msm-uartdm" },
934 {} 974 {}
935}; 975};
936 976
diff --git a/drivers/tty/serial/msm_serial.h b/drivers/tty/serial/msm_serial.h
index e4acef5de77e..469fda50ac63 100644
--- a/drivers/tty/serial/msm_serial.h
+++ b/drivers/tty/serial/msm_serial.h
@@ -38,19 +38,7 @@
38#define UART_MR2_PARITY_MODE_SPACE 0x3 38#define UART_MR2_PARITY_MODE_SPACE 0x3
39#define UART_MR2_PARITY_MODE 0x3 39#define UART_MR2_PARITY_MODE 0x3
40 40
41#define UART_CSR 0x0008 41#define UART_CSR 0x0008
42#define UART_CSR_115200 0xFF
43#define UART_CSR_57600 0xEE
44#define UART_CSR_38400 0xDD
45#define UART_CSR_28800 0xCC
46#define UART_CSR_19200 0xBB
47#define UART_CSR_14400 0xAA
48#define UART_CSR_9600 0x99
49#define UART_CSR_4800 0x77
50#define UART_CSR_2400 0x55
51#define UART_CSR_1200 0x44
52#define UART_CSR_600 0x33
53#define UART_CSR_300 0x22
54 42
55#define UART_TF 0x000C 43#define UART_TF 0x000C
56#define UARTDM_TF 0x0070 44#define UARTDM_TF 0x0070
@@ -71,6 +59,7 @@
71#define UART_CR_CMD_RESET_RFR (14 << 4) 59#define UART_CR_CMD_RESET_RFR (14 << 4)
72#define UART_CR_CMD_PROTECTION_EN (16 << 4) 60#define UART_CR_CMD_PROTECTION_EN (16 << 4)
73#define UART_CR_CMD_STALE_EVENT_ENABLE (80 << 4) 61#define UART_CR_CMD_STALE_EVENT_ENABLE (80 << 4)
62#define UART_CR_CMD_RESET_TX_READY (3 << 8)
74#define UART_CR_TX_DISABLE (1 << 3) 63#define UART_CR_TX_DISABLE (1 << 3)
75#define UART_CR_TX_ENABLE (1 << 2) 64#define UART_CR_TX_ENABLE (1 << 2)
76#define UART_CR_RX_DISABLE (1 << 1) 65#define UART_CR_RX_DISABLE (1 << 1)
@@ -151,6 +140,7 @@ static inline void msm_serial_set_mnd_regs_tcxo(struct uart_port *port)
151 msm_write(port, 0xF1, UART_NREG); 140 msm_write(port, 0xF1, UART_NREG);
152 msm_write(port, 0x0F, UART_DREG); 141 msm_write(port, 0x0F, UART_DREG);
153 msm_write(port, 0x1A, UART_MNDREG); 142 msm_write(port, 0x1A, UART_MNDREG);
143 port->uartclk = 1843200;
154} 144}
155 145
156/* 146/*
@@ -162,6 +152,7 @@ static inline void msm_serial_set_mnd_regs_tcxoby4(struct uart_port *port)
162 msm_write(port, 0xF6, UART_NREG); 152 msm_write(port, 0xF6, UART_NREG);
163 msm_write(port, 0x0F, UART_DREG); 153 msm_write(port, 0x0F, UART_DREG);
164 msm_write(port, 0x0A, UART_MNDREG); 154 msm_write(port, 0x0A, UART_MNDREG);
155 port->uartclk = 1843200;
165} 156}
166 157
167static inline 158static inline
@@ -169,7 +160,7 @@ void msm_serial_set_mnd_regs_from_uartclk(struct uart_port *port)
169{ 160{
170 if (port->uartclk == 19200000) 161 if (port->uartclk == 19200000)
171 msm_serial_set_mnd_regs_tcxo(port); 162 msm_serial_set_mnd_regs_tcxo(port);
172 else 163 else if (port->uartclk == 4800000)
173 msm_serial_set_mnd_regs_tcxoby4(port); 164 msm_serial_set_mnd_regs_tcxoby4(port);
174} 165}
175 166
diff --git a/drivers/tty/serial/msm_serial_hs.c b/drivers/tty/serial/msm_serial_hs.c
index 4ca2f64861e6..48e94961a9e5 100644
--- a/drivers/tty/serial/msm_serial_hs.c
+++ b/drivers/tty/serial/msm_serial_hs.c
@@ -1618,7 +1618,7 @@ static int msm_hs_probe(struct platform_device *pdev)
1618 struct msm_hs_port *msm_uport; 1618 struct msm_hs_port *msm_uport;
1619 struct resource *resource; 1619 struct resource *resource;
1620 const struct msm_serial_hs_platform_data *pdata = 1620 const struct msm_serial_hs_platform_data *pdata =
1621 pdev->dev.platform_data; 1621 dev_get_platdata(&pdev->dev);
1622 1622
1623 if (pdev->id < 0 || pdev->id >= UARTDM_NR) { 1623 if (pdev->id < 0 || pdev->id >= UARTDM_NR) {
1624 printk(KERN_ERR "Invalid plaform device ID = %d\n", pdev->id); 1624 printk(KERN_ERR "Invalid plaform device ID = %d\n", pdev->id);
diff --git a/drivers/tty/serial/mxs-auart.c b/drivers/tty/serial/mxs-auart.c
index f85b8e6d0346..10e9d70b5c40 100644
--- a/drivers/tty/serial/mxs-auart.c
+++ b/drivers/tty/serial/mxs-auart.c
@@ -32,7 +32,6 @@
32#include <linux/clk.h> 32#include <linux/clk.h>
33#include <linux/delay.h> 33#include <linux/delay.h>
34#include <linux/io.h> 34#include <linux/io.h>
35#include <linux/pinctrl/consumer.h>
36#include <linux/of_device.h> 35#include <linux/of_device.h>
37#include <linux/dma-mapping.h> 36#include <linux/dma-mapping.h>
38#include <linux/dmaengine.h> 37#include <linux/dmaengine.h>
@@ -134,10 +133,10 @@ enum mxs_auart_type {
134struct mxs_auart_port { 133struct mxs_auart_port {
135 struct uart_port port; 134 struct uart_port port;
136 135
137#define MXS_AUART_DMA_CONFIG 0x1
138#define MXS_AUART_DMA_ENABLED 0x2 136#define MXS_AUART_DMA_ENABLED 0x2
139#define MXS_AUART_DMA_TX_SYNC 2 /* bit 2 */ 137#define MXS_AUART_DMA_TX_SYNC 2 /* bit 2 */
140#define MXS_AUART_DMA_RX_READY 3 /* bit 3 */ 138#define MXS_AUART_DMA_RX_READY 3 /* bit 3 */
139#define MXS_AUART_RTSCTS 4 /* bit 4 */
141 unsigned long flags; 140 unsigned long flags;
142 unsigned int ctrl; 141 unsigned int ctrl;
143 enum mxs_auart_type devtype; 142 enum mxs_auart_type devtype;
@@ -640,7 +639,8 @@ static void mxs_auart_settermios(struct uart_port *u,
640 * we can only implement the DMA support for auart 639 * we can only implement the DMA support for auart
641 * in mx28. 640 * in mx28.
642 */ 641 */
643 if (is_imx28_auart(s) && (s->flags & MXS_AUART_DMA_CONFIG)) { 642 if (is_imx28_auart(s)
643 && test_bit(MXS_AUART_RTSCTS, &s->flags)) {
644 if (!mxs_auart_dma_init(s)) 644 if (!mxs_auart_dma_init(s))
645 /* enable DMA tranfer */ 645 /* enable DMA tranfer */
646 ctrl2 |= AUART_CTRL2_TXDMAE | AUART_CTRL2_RXDMAE 646 ctrl2 |= AUART_CTRL2_TXDMAE | AUART_CTRL2_RXDMAE
@@ -1008,7 +1008,8 @@ static int serial_mxs_probe_dt(struct mxs_auart_port *s,
1008 } 1008 }
1009 s->port.line = ret; 1009 s->port.line = ret;
1010 1010
1011 s->flags |= MXS_AUART_DMA_CONFIG; 1011 if (of_get_property(np, "fsl,uart-has-rtscts", NULL))
1012 set_bit(MXS_AUART_RTSCTS, &s->flags);
1012 1013
1013 return 0; 1014 return 0;
1014} 1015}
@@ -1021,7 +1022,6 @@ static int mxs_auart_probe(struct platform_device *pdev)
1021 u32 version; 1022 u32 version;
1022 int ret = 0; 1023 int ret = 0;
1023 struct resource *r; 1024 struct resource *r;
1024 struct pinctrl *pinctrl;
1025 1025
1026 s = kzalloc(sizeof(struct mxs_auart_port), GFP_KERNEL); 1026 s = kzalloc(sizeof(struct mxs_auart_port), GFP_KERNEL);
1027 if (!s) { 1027 if (!s) {
@@ -1035,12 +1035,6 @@ static int mxs_auart_probe(struct platform_device *pdev)
1035 else if (ret < 0) 1035 else if (ret < 0)
1036 goto out_free; 1036 goto out_free;
1037 1037
1038 pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
1039 if (IS_ERR(pinctrl)) {
1040 ret = PTR_ERR(pinctrl);
1041 goto out_free;
1042 }
1043
1044 if (of_id) { 1038 if (of_id) {
1045 pdev->id_entry = of_id->data; 1039 pdev->id_entry = of_id->data;
1046 s->devtype = pdev->id_entry->driver_data; 1040 s->devtype = pdev->id_entry->driver_data;
diff --git a/drivers/tty/serial/netx-serial.c b/drivers/tty/serial/netx-serial.c
index b9a40ed70be2..0a4dd70d29eb 100644
--- a/drivers/tty/serial/netx-serial.c
+++ b/drivers/tty/serial/netx-serial.c
@@ -196,7 +196,7 @@ static void netx_txint(struct uart_port *port)
196 uart_write_wakeup(port); 196 uart_write_wakeup(port);
197} 197}
198 198
199static void netx_rxint(struct uart_port *port) 199static void netx_rxint(struct uart_port *port, unsigned long *flags)
200{ 200{
201 unsigned char rx, flg, status; 201 unsigned char rx, flg, status;
202 202
@@ -236,7 +236,9 @@ static void netx_rxint(struct uart_port *port)
236 uart_insert_char(port, status, SR_OE, rx, flg); 236 uart_insert_char(port, status, SR_OE, rx, flg);
237 } 237 }
238 238
239 spin_unlock_irqrestore(&port->lock, *flags);
239 tty_flip_buffer_push(&port->state->port); 240 tty_flip_buffer_push(&port->state->port);
241 spin_lock_irqsave(&port->lock, *flags);
240} 242}
241 243
242static irqreturn_t netx_int(int irq, void *dev_id) 244static irqreturn_t netx_int(int irq, void *dev_id)
@@ -250,7 +252,7 @@ static irqreturn_t netx_int(int irq, void *dev_id)
250 status = readl(port->membase + UART_IIR) & IIR_MASK; 252 status = readl(port->membase + UART_IIR) & IIR_MASK;
251 while (status) { 253 while (status) {
252 if (status & IIR_RIS) 254 if (status & IIR_RIS)
253 netx_rxint(port); 255 netx_rxint(port, &flags);
254 if (status & IIR_TIS) 256 if (status & IIR_TIS)
255 netx_txint(port); 257 netx_txint(port);
256 if (status & IIR_MIS) { 258 if (status & IIR_MIS) {
@@ -693,8 +695,6 @@ static int serial_netx_remove(struct platform_device *pdev)
693{ 695{
694 struct netx_port *sport = platform_get_drvdata(pdev); 696 struct netx_port *sport = platform_get_drvdata(pdev);
695 697
696 platform_set_drvdata(pdev, NULL);
697
698 if (sport) 698 if (sport)
699 uart_remove_one_port(&netx_reg, &sport->port); 699 uart_remove_one_port(&netx_reg, &sport->port);
700 700
diff --git a/drivers/tty/serial/nwpserial.c b/drivers/tty/serial/nwpserial.c
index 549c70a2a63e..693bc6c2561e 100644
--- a/drivers/tty/serial/nwpserial.c
+++ b/drivers/tty/serial/nwpserial.c
@@ -149,7 +149,10 @@ static irqreturn_t nwpserial_interrupt(int irq, void *dev_id)
149 tty_insert_flip_char(port, ch, TTY_NORMAL); 149 tty_insert_flip_char(port, ch, TTY_NORMAL);
150 } while (dcr_read(up->dcr_host, UART_LSR) & UART_LSR_DR); 150 } while (dcr_read(up->dcr_host, UART_LSR) & UART_LSR_DR);
151 151
152 spin_unlock(&up->port.lock);
152 tty_flip_buffer_push(port); 153 tty_flip_buffer_push(port);
154 spin_lock(&up->port.lock);
155
153 ret = IRQ_HANDLED; 156 ret = IRQ_HANDLED;
154 157
155 /* clear interrupt */ 158 /* clear interrupt */
diff --git a/drivers/tty/serial/omap-serial.c b/drivers/tty/serial/omap-serial.c
index b6d172873076..816d1a23f9d0 100644
--- a/drivers/tty/serial/omap-serial.c
+++ b/drivers/tty/serial/omap-serial.c
@@ -40,9 +40,11 @@
40#include <linux/pm_runtime.h> 40#include <linux/pm_runtime.h>
41#include <linux/of.h> 41#include <linux/of.h>
42#include <linux/gpio.h> 42#include <linux/gpio.h>
43#include <linux/pinctrl/consumer.h> 43#include <linux/of_gpio.h>
44#include <linux/platform_data/serial-omap.h> 44#include <linux/platform_data/serial-omap.h>
45 45
46#include <dt-bindings/gpio/gpio.h>
47
46#define OMAP_MAX_HSUART_PORTS 6 48#define OMAP_MAX_HSUART_PORTS 6
47 49
48#define UART_BUILD_REVISION(x, y) (((x) << 8) | (y)) 50#define UART_BUILD_REVISION(x, y) (((x) << 8) | (y))
@@ -52,6 +54,11 @@
52#define OMAP_UART_REV_52 0x0502 54#define OMAP_UART_REV_52 0x0502
53#define OMAP_UART_REV_63 0x0603 55#define OMAP_UART_REV_63 0x0603
54 56
57#define OMAP_UART_TX_WAKEUP_EN BIT(7)
58
59/* Feature flags */
60#define OMAP_UART_WER_HAS_TX_WAKEUP BIT(0)
61
55#define UART_ERRATA_i202_MDR1_ACCESS BIT(0) 62#define UART_ERRATA_i202_MDR1_ACCESS BIT(0)
56#define UART_ERRATA_i291_DMA_FORCEIDLE BIT(1) 63#define UART_ERRATA_i291_DMA_FORCEIDLE BIT(1)
57 64
@@ -137,6 +144,7 @@ struct uart_omap_port {
137 unsigned char dlh; 144 unsigned char dlh;
138 unsigned char mdr1; 145 unsigned char mdr1;
139 unsigned char scr; 146 unsigned char scr;
147 unsigned char wer;
140 148
141 int use_dma; 149 int use_dma;
142 /* 150 /*
@@ -151,16 +159,19 @@ struct uart_omap_port {
151 int context_loss_cnt; 159 int context_loss_cnt;
152 u32 errata; 160 u32 errata;
153 u8 wakeups_enabled; 161 u8 wakeups_enabled;
162 u32 features;
154 163
155 int DTR_gpio; 164 int DTR_gpio;
156 int DTR_inverted; 165 int DTR_inverted;
157 int DTR_active; 166 int DTR_active;
158 167
168 struct serial_rs485 rs485;
169 int rts_gpio;
170
159 struct pm_qos_request pm_qos_request; 171 struct pm_qos_request pm_qos_request;
160 u32 latency; 172 u32 latency;
161 u32 calc_latency; 173 u32 calc_latency;
162 struct work_struct qos_work; 174 struct work_struct qos_work;
163 struct pinctrl *pins;
164 bool is_suspending; 175 bool is_suspending;
165}; 176};
166 177
@@ -195,7 +206,7 @@ static inline void serial_omap_clear_fifos(struct uart_omap_port *up)
195 206
196static int serial_omap_get_context_loss_count(struct uart_omap_port *up) 207static int serial_omap_get_context_loss_count(struct uart_omap_port *up)
197{ 208{
198 struct omap_uart_port_info *pdata = up->dev->platform_data; 209 struct omap_uart_port_info *pdata = dev_get_platdata(up->dev);
199 210
200 if (!pdata || !pdata->get_context_loss_count) 211 if (!pdata || !pdata->get_context_loss_count)
201 return -EINVAL; 212 return -EINVAL;
@@ -205,7 +216,7 @@ static int serial_omap_get_context_loss_count(struct uart_omap_port *up)
205 216
206static void serial_omap_enable_wakeup(struct uart_omap_port *up, bool enable) 217static void serial_omap_enable_wakeup(struct uart_omap_port *up, bool enable)
207{ 218{
208 struct omap_uart_port_info *pdata = up->dev->platform_data; 219 struct omap_uart_port_info *pdata = dev_get_platdata(up->dev);
209 220
210 if (!pdata || !pdata->enable_wakeup) 221 if (!pdata || !pdata->enable_wakeup)
211 return; 222 return;
@@ -272,13 +283,42 @@ static void serial_omap_enable_ms(struct uart_port *port)
272static void serial_omap_stop_tx(struct uart_port *port) 283static void serial_omap_stop_tx(struct uart_port *port)
273{ 284{
274 struct uart_omap_port *up = to_uart_omap_port(port); 285 struct uart_omap_port *up = to_uart_omap_port(port);
286 struct circ_buf *xmit = &up->port.state->xmit;
287 int res;
275 288
276 pm_runtime_get_sync(up->dev); 289 pm_runtime_get_sync(up->dev);
290
291 /* handle rs485 */
292 if (up->rs485.flags & SER_RS485_ENABLED) {
293 /* do nothing if current tx not yet completed */
294 res = serial_in(up, UART_LSR) & UART_LSR_TEMT;
295 if (!res)
296 return;
297
298 /* if there's no more data to send, turn off rts */
299 if (uart_circ_empty(xmit)) {
300 /* if rts not already disabled */
301 res = (up->rs485.flags & SER_RS485_RTS_AFTER_SEND) ? 1 : 0;
302 if (gpio_get_value(up->rts_gpio) != res) {
303 if (up->rs485.delay_rts_after_send > 0) {
304 mdelay(up->rs485.delay_rts_after_send);
305 }
306 gpio_set_value(up->rts_gpio, res);
307 }
308 }
309 }
310
277 if (up->ier & UART_IER_THRI) { 311 if (up->ier & UART_IER_THRI) {
278 up->ier &= ~UART_IER_THRI; 312 up->ier &= ~UART_IER_THRI;
279 serial_out(up, UART_IER, up->ier); 313 serial_out(up, UART_IER, up->ier);
280 } 314 }
281 315
316 if ((up->rs485.flags & SER_RS485_ENABLED) &&
317 !(up->rs485.flags & SER_RS485_RX_DURING_TX)) {
318 up->ier = UART_IER_RLSI | UART_IER_RDI;
319 serial_out(up, UART_IER, up->ier);
320 }
321
282 pm_runtime_mark_last_busy(up->dev); 322 pm_runtime_mark_last_busy(up->dev);
283 pm_runtime_put_autosuspend(up->dev); 323 pm_runtime_put_autosuspend(up->dev);
284} 324}
@@ -340,8 +380,26 @@ static inline void serial_omap_enable_ier_thri(struct uart_omap_port *up)
340static void serial_omap_start_tx(struct uart_port *port) 380static void serial_omap_start_tx(struct uart_port *port)
341{ 381{
342 struct uart_omap_port *up = to_uart_omap_port(port); 382 struct uart_omap_port *up = to_uart_omap_port(port);
383 int res;
343 384
344 pm_runtime_get_sync(up->dev); 385 pm_runtime_get_sync(up->dev);
386
387 /* handle rs485 */
388 if (up->rs485.flags & SER_RS485_ENABLED) {
389 /* if rts not already enabled */
390 res = (up->rs485.flags & SER_RS485_RTS_ON_SEND) ? 1 : 0;
391 if (gpio_get_value(up->rts_gpio) != res) {
392 gpio_set_value(up->rts_gpio, res);
393 if (up->rs485.delay_rts_before_send > 0) {
394 mdelay(up->rs485.delay_rts_before_send);
395 }
396 }
397 }
398
399 if ((up->rs485.flags & SER_RS485_ENABLED) &&
400 !(up->rs485.flags & SER_RS485_RX_DURING_TX))
401 serial_omap_stop_rx(port);
402
345 serial_omap_enable_ier_thri(up); 403 serial_omap_enable_ier_thri(up);
346 pm_runtime_mark_last_busy(up->dev); 404 pm_runtime_mark_last_busy(up->dev);
347 pm_runtime_put_autosuspend(up->dev); 405 pm_runtime_put_autosuspend(up->dev);
@@ -683,7 +741,11 @@ static int serial_omap_startup(struct uart_port *port)
683 serial_out(up, UART_IER, up->ier); 741 serial_out(up, UART_IER, up->ier);
684 742
685 /* Enable module level wake up */ 743 /* Enable module level wake up */
686 serial_out(up, UART_OMAP_WER, OMAP_UART_WER_MOD_WKUP); 744 up->wer = OMAP_UART_WER_MOD_WKUP;
745 if (up->features & OMAP_UART_WER_HAS_TX_WAKEUP)
746 up->wer |= OMAP_UART_TX_WAKEUP_EN;
747
748 serial_out(up, UART_OMAP_WER, up->wer);
687 749
688 pm_runtime_mark_last_busy(up->dev); 750 pm_runtime_mark_last_busy(up->dev);
689 pm_runtime_put_autosuspend(up->dev); 751 pm_runtime_put_autosuspend(up->dev);
@@ -1254,6 +1316,76 @@ static inline void serial_omap_add_console_port(struct uart_omap_port *up)
1254 1316
1255#endif 1317#endif
1256 1318
1319/* Enable or disable the rs485 support */
1320static void
1321serial_omap_config_rs485(struct uart_port *port, struct serial_rs485 *rs485conf)
1322{
1323 struct uart_omap_port *up = to_uart_omap_port(port);
1324 unsigned long flags;
1325 unsigned int mode;
1326 int val;
1327
1328 pm_runtime_get_sync(up->dev);
1329 spin_lock_irqsave(&up->port.lock, flags);
1330
1331 /* Disable interrupts from this port */
1332 mode = up->ier;
1333 up->ier = 0;
1334 serial_out(up, UART_IER, 0);
1335
1336 /* store new config */
1337 up->rs485 = *rs485conf;
1338
1339 /*
1340 * Just as a precaution, only allow rs485
1341 * to be enabled if the gpio pin is valid
1342 */
1343 if (gpio_is_valid(up->rts_gpio)) {
1344 /* enable / disable rts */
1345 val = (up->rs485.flags & SER_RS485_ENABLED) ?
1346 SER_RS485_RTS_AFTER_SEND : SER_RS485_RTS_ON_SEND;
1347 val = (up->rs485.flags & val) ? 1 : 0;
1348 gpio_set_value(up->rts_gpio, val);
1349 } else
1350 up->rs485.flags &= ~SER_RS485_ENABLED;
1351
1352 /* Enable interrupts */
1353 up->ier = mode;
1354 serial_out(up, UART_IER, up->ier);
1355
1356 spin_unlock_irqrestore(&up->port.lock, flags);
1357 pm_runtime_mark_last_busy(up->dev);
1358 pm_runtime_put_autosuspend(up->dev);
1359}
1360
1361static int
1362serial_omap_ioctl(struct uart_port *port, unsigned int cmd, unsigned long arg)
1363{
1364 struct serial_rs485 rs485conf;
1365
1366 switch (cmd) {
1367 case TIOCSRS485:
1368 if (copy_from_user(&rs485conf, (struct serial_rs485 *) arg,
1369 sizeof(rs485conf)))
1370 return -EFAULT;
1371
1372 serial_omap_config_rs485(port, &rs485conf);
1373 break;
1374
1375 case TIOCGRS485:
1376 if (copy_to_user((struct serial_rs485 *) arg,
1377 &(to_uart_omap_port(port)->rs485),
1378 sizeof(rs485conf)))
1379 return -EFAULT;
1380 break;
1381
1382 default:
1383 return -ENOIOCTLCMD;
1384 }
1385 return 0;
1386}
1387
1388
1257static struct uart_ops serial_omap_pops = { 1389static struct uart_ops serial_omap_pops = {
1258 .tx_empty = serial_omap_tx_empty, 1390 .tx_empty = serial_omap_tx_empty,
1259 .set_mctrl = serial_omap_set_mctrl, 1391 .set_mctrl = serial_omap_set_mctrl,
@@ -1275,6 +1407,7 @@ static struct uart_ops serial_omap_pops = {
1275 .request_port = serial_omap_request_port, 1407 .request_port = serial_omap_request_port,
1276 .config_port = serial_omap_config_port, 1408 .config_port = serial_omap_config_port,
1277 .verify_port = serial_omap_verify_port, 1409 .verify_port = serial_omap_verify_port,
1410 .ioctl = serial_omap_ioctl,
1278#ifdef CONFIG_CONSOLE_POLL 1411#ifdef CONFIG_CONSOLE_POLL
1279 .poll_put_char = serial_omap_poll_put_char, 1412 .poll_put_char = serial_omap_poll_put_char,
1280 .poll_get_char = serial_omap_poll_get_char, 1413 .poll_get_char = serial_omap_poll_get_char,
@@ -1334,7 +1467,7 @@ static void omap_serial_fill_features_erratas(struct uart_omap_port *up)
1334 u32 mvr, scheme; 1467 u32 mvr, scheme;
1335 u16 revision, major, minor; 1468 u16 revision, major, minor;
1336 1469
1337 mvr = serial_in(up, UART_OMAP_MVER); 1470 mvr = readl(up->port.membase + (UART_OMAP_MVER << up->port.regshift));
1338 1471
1339 /* Check revision register scheme */ 1472 /* Check revision register scheme */
1340 scheme = mvr >> OMAP_UART_MVR_SCHEME_SHIFT; 1473 scheme = mvr >> OMAP_UART_MVR_SCHEME_SHIFT;
@@ -1373,9 +1506,11 @@ static void omap_serial_fill_features_erratas(struct uart_omap_port *up)
1373 case OMAP_UART_REV_52: 1506 case OMAP_UART_REV_52:
1374 up->errata |= (UART_ERRATA_i202_MDR1_ACCESS | 1507 up->errata |= (UART_ERRATA_i202_MDR1_ACCESS |
1375 UART_ERRATA_i291_DMA_FORCEIDLE); 1508 UART_ERRATA_i291_DMA_FORCEIDLE);
1509 up->features |= OMAP_UART_WER_HAS_TX_WAKEUP;
1376 break; 1510 break;
1377 case OMAP_UART_REV_63: 1511 case OMAP_UART_REV_63:
1378 up->errata |= UART_ERRATA_i202_MDR1_ACCESS; 1512 up->errata |= UART_ERRATA_i202_MDR1_ACCESS;
1513 up->features |= OMAP_UART_WER_HAS_TX_WAKEUP;
1379 break; 1514 break;
1380 default: 1515 default:
1381 break; 1516 break;
@@ -1395,15 +1530,64 @@ static struct omap_uart_port_info *of_get_uart_port_info(struct device *dev)
1395 return omap_up_info; 1530 return omap_up_info;
1396} 1531}
1397 1532
1533static int serial_omap_probe_rs485(struct uart_omap_port *up,
1534 struct device_node *np)
1535{
1536 struct serial_rs485 *rs485conf = &up->rs485;
1537 u32 rs485_delay[2];
1538 enum of_gpio_flags flags;
1539 int ret;
1540
1541 rs485conf->flags = 0;
1542 up->rts_gpio = -EINVAL;
1543
1544 if (!np)
1545 return 0;
1546
1547 if (of_property_read_bool(np, "rs485-rts-active-high"))
1548 rs485conf->flags |= SER_RS485_RTS_ON_SEND;
1549 else
1550 rs485conf->flags |= SER_RS485_RTS_AFTER_SEND;
1551
1552 /* check for tx enable gpio */
1553 up->rts_gpio = of_get_named_gpio_flags(np, "rts-gpio", 0, &flags);
1554 if (gpio_is_valid(up->rts_gpio)) {
1555 ret = gpio_request(up->rts_gpio, "omap-serial");
1556 if (ret < 0)
1557 return ret;
1558 ret = gpio_direction_output(up->rts_gpio,
1559 flags & SER_RS485_RTS_AFTER_SEND);
1560 if (ret < 0)
1561 return ret;
1562 } else
1563 up->rts_gpio = -EINVAL;
1564
1565 if (of_property_read_u32_array(np, "rs485-rts-delay",
1566 rs485_delay, 2) == 0) {
1567 rs485conf->delay_rts_before_send = rs485_delay[0];
1568 rs485conf->delay_rts_after_send = rs485_delay[1];
1569 }
1570
1571 if (of_property_read_bool(np, "rs485-rx-during-tx"))
1572 rs485conf->flags |= SER_RS485_RX_DURING_TX;
1573
1574 if (of_property_read_bool(np, "linux,rs485-enabled-at-boot-time"))
1575 rs485conf->flags |= SER_RS485_ENABLED;
1576
1577 return 0;
1578}
1579
1398static int serial_omap_probe(struct platform_device *pdev) 1580static int serial_omap_probe(struct platform_device *pdev)
1399{ 1581{
1400 struct uart_omap_port *up; 1582 struct uart_omap_port *up;
1401 struct resource *mem, *irq; 1583 struct resource *mem, *irq;
1402 struct omap_uart_port_info *omap_up_info = pdev->dev.platform_data; 1584 struct omap_uart_port_info *omap_up_info = dev_get_platdata(&pdev->dev);
1403 int ret; 1585 int ret;
1404 1586
1405 if (pdev->dev.of_node) 1587 if (pdev->dev.of_node) {
1406 omap_up_info = of_get_uart_port_info(&pdev->dev); 1588 omap_up_info = of_get_uart_port_info(&pdev->dev);
1589 pdev->dev.platform_data = omap_up_info;
1590 }
1407 1591
1408 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1592 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1409 if (!mem) { 1593 if (!mem) {
@@ -1468,12 +1652,9 @@ static int serial_omap_probe(struct platform_device *pdev)
1468 goto err_port_line; 1652 goto err_port_line;
1469 } 1653 }
1470 1654
1471 up->pins = devm_pinctrl_get_select_default(&pdev->dev); 1655 ret = serial_omap_probe_rs485(up, pdev->dev.of_node);
1472 if (IS_ERR(up->pins)) { 1656 if (ret < 0)
1473 dev_warn(&pdev->dev, "did not get pins for uart%i error: %li\n", 1657 goto err_rs485;
1474 up->port.line, PTR_ERR(up->pins));
1475 up->pins = NULL;
1476 }
1477 1658
1478 sprintf(up->name, "OMAP UART%d", up->port.line); 1659 sprintf(up->name, "OMAP UART%d", up->port.line);
1479 up->port.mapbase = mem->start; 1660 up->port.mapbase = mem->start;
@@ -1501,7 +1682,6 @@ static int serial_omap_probe(struct platform_device *pdev)
1501 INIT_WORK(&up->qos_work, serial_omap_uart_qos_work); 1682 INIT_WORK(&up->qos_work, serial_omap_uart_qos_work);
1502 1683
1503 platform_set_drvdata(pdev, up); 1684 platform_set_drvdata(pdev, up);
1504 pm_runtime_enable(&pdev->dev);
1505 if (omap_up_info->autosuspend_timeout == 0) 1685 if (omap_up_info->autosuspend_timeout == 0)
1506 omap_up_info->autosuspend_timeout = -1; 1686 omap_up_info->autosuspend_timeout = -1;
1507 device_init_wakeup(up->dev, true); 1687 device_init_wakeup(up->dev, true);
@@ -1510,6 +1690,8 @@ static int serial_omap_probe(struct platform_device *pdev)
1510 omap_up_info->autosuspend_timeout); 1690 omap_up_info->autosuspend_timeout);
1511 1691
1512 pm_runtime_irq_safe(&pdev->dev); 1692 pm_runtime_irq_safe(&pdev->dev);
1693 pm_runtime_enable(&pdev->dev);
1694
1513 pm_runtime_get_sync(&pdev->dev); 1695 pm_runtime_get_sync(&pdev->dev);
1514 1696
1515 omap_serial_fill_features_erratas(up); 1697 omap_serial_fill_features_erratas(up);
@@ -1529,6 +1711,7 @@ err_add_port:
1529 pm_runtime_put(&pdev->dev); 1711 pm_runtime_put(&pdev->dev);
1530 pm_runtime_disable(&pdev->dev); 1712 pm_runtime_disable(&pdev->dev);
1531err_ioremap: 1713err_ioremap:
1714err_rs485:
1532err_port_line: 1715err_port_line:
1533 dev_err(&pdev->dev, "[UART%d]: failure [%s]: %d\n", 1716 dev_err(&pdev->dev, "[UART%d]: failure [%s]: %d\n",
1534 pdev->id, __func__, ret); 1717 pdev->id, __func__, ret);
@@ -1609,6 +1792,7 @@ static void serial_omap_restore_context(struct uart_omap_port *up)
1609 serial_omap_mdr1_errataset(up, up->mdr1); 1792 serial_omap_mdr1_errataset(up, up->mdr1);
1610 else 1793 else
1611 serial_out(up, UART_OMAP_MDR1, up->mdr1); 1794 serial_out(up, UART_OMAP_MDR1, up->mdr1);
1795 serial_out(up, UART_OMAP_WER, up->wer);
1612} 1796}
1613 1797
1614static int serial_omap_runtime_suspend(struct device *dev) 1798static int serial_omap_runtime_suspend(struct device *dev)
diff --git a/drivers/tty/serial/pch_uart.c b/drivers/tty/serial/pch_uart.c
index 572d48189de9..52379e56a31e 100644
--- a/drivers/tty/serial/pch_uart.c
+++ b/drivers/tty/serial/pch_uart.c
@@ -232,7 +232,7 @@ struct eg20t_port {
232 unsigned int iobase; 232 unsigned int iobase;
233 struct pci_dev *pdev; 233 struct pci_dev *pdev;
234 int fifo_size; 234 int fifo_size;
235 int uartclk; 235 unsigned int uartclk;
236 int start_tx; 236 int start_tx;
237 int start_rx; 237 int start_rx;
238 int tx_empty; 238 int tx_empty;
@@ -373,35 +373,62 @@ static const struct file_operations port_regs_ops = {
373}; 373};
374#endif /* CONFIG_DEBUG_FS */ 374#endif /* CONFIG_DEBUG_FS */
375 375
376static struct dmi_system_id pch_uart_dmi_table[] = {
377 {
378 .ident = "CM-iTC",
379 {
380 DMI_MATCH(DMI_BOARD_NAME, "CM-iTC"),
381 },
382 (void *)CMITC_UARTCLK,
383 },
384 {
385 .ident = "FRI2",
386 {
387 DMI_MATCH(DMI_BIOS_VERSION, "FRI2"),
388 },
389 (void *)FRI2_64_UARTCLK,
390 },
391 {
392 .ident = "Fish River Island II",
393 {
394 DMI_MATCH(DMI_PRODUCT_NAME, "Fish River Island II"),
395 },
396 (void *)FRI2_48_UARTCLK,
397 },
398 {
399 .ident = "COMe-mTT",
400 {
401 DMI_MATCH(DMI_BOARD_NAME, "COMe-mTT"),
402 },
403 (void *)NTC1_UARTCLK,
404 },
405 {
406 .ident = "nanoETXexpress-TT",
407 {
408 DMI_MATCH(DMI_BOARD_NAME, "nanoETXexpress-TT"),
409 },
410 (void *)NTC1_UARTCLK,
411 },
412 {
413 .ident = "MinnowBoard",
414 {
415 DMI_MATCH(DMI_BOARD_NAME, "MinnowBoard"),
416 },
417 (void *)MINNOW_UARTCLK,
418 },
419};
420
376/* Return UART clock, checking for board specific clocks. */ 421/* Return UART clock, checking for board specific clocks. */
377static int pch_uart_get_uartclk(void) 422static unsigned int pch_uart_get_uartclk(void)
378{ 423{
379 const char *cmp; 424 const struct dmi_system_id *d;
380 425
381 if (user_uartclk) 426 if (user_uartclk)
382 return user_uartclk; 427 return user_uartclk;
383 428
384 cmp = dmi_get_system_info(DMI_BOARD_NAME); 429 d = dmi_first_match(pch_uart_dmi_table);
385 if (cmp && strstr(cmp, "CM-iTC")) 430 if (d)
386 return CMITC_UARTCLK; 431 return (unsigned long)d->driver_data;
387
388 cmp = dmi_get_system_info(DMI_BIOS_VERSION);
389 if (cmp && strnstr(cmp, "FRI2", 4))
390 return FRI2_64_UARTCLK;
391
392 cmp = dmi_get_system_info(DMI_PRODUCT_NAME);
393 if (cmp && strstr(cmp, "Fish River Island II"))
394 return FRI2_48_UARTCLK;
395
396 /* Kontron COMe-mTT10 (nanoETXexpress-TT) */
397 cmp = dmi_get_system_info(DMI_BOARD_NAME);
398 if (cmp && (strstr(cmp, "COMe-mTT") ||
399 strstr(cmp, "nanoETXexpress-TT")))
400 return NTC1_UARTCLK;
401
402 cmp = dmi_get_system_info(DMI_BOARD_NAME);
403 if (cmp && strstr(cmp, "MinnowBoard"))
404 return MINNOW_UARTCLK;
405 432
406 return DEFAULT_UARTCLK; 433 return DEFAULT_UARTCLK;
407} 434}
@@ -422,7 +449,7 @@ static void pch_uart_hal_disable_interrupt(struct eg20t_port *priv,
422 iowrite8(ier, priv->membase + UART_IER); 449 iowrite8(ier, priv->membase + UART_IER);
423} 450}
424 451
425static int pch_uart_hal_set_line(struct eg20t_port *priv, int baud, 452static int pch_uart_hal_set_line(struct eg20t_port *priv, unsigned int baud,
426 unsigned int parity, unsigned int bits, 453 unsigned int parity, unsigned int bits,
427 unsigned int stb) 454 unsigned int stb)
428{ 455{
@@ -457,7 +484,7 @@ static int pch_uart_hal_set_line(struct eg20t_port *priv, int baud,
457 lcr |= bits; 484 lcr |= bits;
458 lcr |= stb; 485 lcr |= stb;
459 486
460 dev_dbg(priv->port.dev, "%s:baud = %d, div = %04x, lcr = %02x (%lu)\n", 487 dev_dbg(priv->port.dev, "%s:baud = %u, div = %04x, lcr = %02x (%lu)\n",
461 __func__, baud, div, lcr, jiffies); 488 __func__, baud, div, lcr, jiffies);
462 iowrite8(PCH_UART_LCR_DLAB, priv->membase + UART_LCR); 489 iowrite8(PCH_UART_LCR_DLAB, priv->membase + UART_LCR);
463 iowrite8(dll, priv->membase + PCH_UART_DLL); 490 iowrite8(dll, priv->membase + PCH_UART_DLL);
@@ -1363,9 +1390,8 @@ static void pch_uart_shutdown(struct uart_port *port)
1363static void pch_uart_set_termios(struct uart_port *port, 1390static void pch_uart_set_termios(struct uart_port *port,
1364 struct ktermios *termios, struct ktermios *old) 1391 struct ktermios *termios, struct ktermios *old)
1365{ 1392{
1366 int baud;
1367 int rtn; 1393 int rtn;
1368 unsigned int parity, bits, stb; 1394 unsigned int baud, parity, bits, stb;
1369 struct eg20t_port *priv; 1395 struct eg20t_port *priv;
1370 unsigned long flags; 1396 unsigned long flags;
1371 1397
@@ -1498,6 +1524,7 @@ static int pch_uart_verify_port(struct uart_port *port,
1498 return 0; 1524 return 0;
1499} 1525}
1500 1526
1527#if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_PCH_UART_CONSOLE)
1501/* 1528/*
1502 * Wait for transmitter & holding register to empty 1529 * Wait for transmitter & holding register to empty
1503 */ 1530 */
@@ -1528,6 +1555,7 @@ static void wait_for_xmitr(struct eg20t_port *up, int bits)
1528 } 1555 }
1529 } 1556 }
1530} 1557}
1558#endif /* CONFIG_CONSOLE_POLL || CONFIG_SERIAL_PCH_UART_CONSOLE */
1531 1559
1532#ifdef CONFIG_CONSOLE_POLL 1560#ifdef CONFIG_CONSOLE_POLL
1533/* 1561/*
diff --git a/drivers/tty/serial/pmac_zilog.c b/drivers/tty/serial/pmac_zilog.c
index b1785f58b6e3..f87f1a0c8c6e 100644
--- a/drivers/tty/serial/pmac_zilog.c
+++ b/drivers/tty/serial/pmac_zilog.c
@@ -1798,7 +1798,6 @@ static int __exit pmz_detach(struct platform_device *pdev)
1798 1798
1799 uart_remove_one_port(&pmz_uart_reg, &uap->port); 1799 uart_remove_one_port(&pmz_uart_reg, &uap->port);
1800 1800
1801 platform_set_drvdata(pdev, NULL);
1802 uap->port.dev = NULL; 1801 uap->port.dev = NULL;
1803 1802
1804 return 0; 1803 return 0;
diff --git a/drivers/tty/serial/pnx8xxx_uart.c b/drivers/tty/serial/pnx8xxx_uart.c
index 7e277a5384a7..de6c05c63683 100644
--- a/drivers/tty/serial/pnx8xxx_uart.c
+++ b/drivers/tty/serial/pnx8xxx_uart.c
@@ -237,7 +237,10 @@ static void pnx8xxx_rx_chars(struct pnx8xxx_port *sport)
237 status = FIFO_TO_SM(serial_in(sport, PNX8XXX_FIFO)) | 237 status = FIFO_TO_SM(serial_in(sport, PNX8XXX_FIFO)) |
238 ISTAT_TO_SM(serial_in(sport, PNX8XXX_ISTAT)); 238 ISTAT_TO_SM(serial_in(sport, PNX8XXX_ISTAT));
239 } 239 }
240
241 spin_unlock(&sport->port.lock);
240 tty_flip_buffer_push(&sport->port.state->port); 242 tty_flip_buffer_push(&sport->port.state->port);
243 spin_lock(&sport->port.lock);
241} 244}
242 245
243static void pnx8xxx_tx_chars(struct pnx8xxx_port *sport) 246static void pnx8xxx_tx_chars(struct pnx8xxx_port *sport)
@@ -801,8 +804,6 @@ static int pnx8xxx_serial_remove(struct platform_device *pdev)
801{ 804{
802 struct pnx8xxx_port *sport = platform_get_drvdata(pdev); 805 struct pnx8xxx_port *sport = platform_get_drvdata(pdev);
803 806
804 platform_set_drvdata(pdev, NULL);
805
806 if (sport) 807 if (sport)
807 uart_remove_one_port(&pnx8xxx_reg, &sport->port); 808 uart_remove_one_port(&pnx8xxx_reg, &sport->port);
808 809
diff --git a/drivers/tty/serial/pxa.c b/drivers/tty/serial/pxa.c
index 05f504e0c271..f9f20f383760 100644
--- a/drivers/tty/serial/pxa.c
+++ b/drivers/tty/serial/pxa.c
@@ -332,31 +332,6 @@ static void serial_pxa_break_ctl(struct uart_port *port, int break_state)
332 spin_unlock_irqrestore(&up->port.lock, flags); 332 spin_unlock_irqrestore(&up->port.lock, flags);
333} 333}
334 334
335#if 0
336static void serial_pxa_dma_init(struct pxa_uart *up)
337{
338 up->rxdma =
339 pxa_request_dma(up->name, DMA_PRIO_LOW, pxa_receive_dma, up);
340 if (up->rxdma < 0)
341 goto out;
342 up->txdma =
343 pxa_request_dma(up->name, DMA_PRIO_LOW, pxa_transmit_dma, up);
344 if (up->txdma < 0)
345 goto err_txdma;
346 up->dmadesc = kmalloc(4 * sizeof(pxa_dma_desc), GFP_KERNEL);
347 if (!up->dmadesc)
348 goto err_alloc;
349
350 /* ... */
351err_alloc:
352 pxa_free_dma(up->txdma);
353err_rxdma:
354 pxa_free_dma(up->rxdma);
355out:
356 return;
357}
358#endif
359
360static int serial_pxa_startup(struct uart_port *port) 335static int serial_pxa_startup(struct uart_port *port)
361{ 336{
362 struct uart_pxa_port *up = (struct uart_pxa_port *)port; 337 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
@@ -790,7 +765,7 @@ static struct console serial_pxa_console = {
790#define PXA_CONSOLE NULL 765#define PXA_CONSOLE NULL
791#endif 766#endif
792 767
793struct uart_ops serial_pxa_pops = { 768static struct uart_ops serial_pxa_pops = {
794 .tx_empty = serial_pxa_tx_empty, 769 .tx_empty = serial_pxa_tx_empty,
795 .set_mctrl = serial_pxa_set_mctrl, 770 .set_mctrl = serial_pxa_set_mctrl,
796 .get_mctrl = serial_pxa_get_mctrl, 771 .get_mctrl = serial_pxa_get_mctrl,
@@ -945,8 +920,6 @@ static int serial_pxa_remove(struct platform_device *dev)
945{ 920{
946 struct uart_pxa_port *sport = platform_get_drvdata(dev); 921 struct uart_pxa_port *sport = platform_get_drvdata(dev);
947 922
948 platform_set_drvdata(dev, NULL);
949
950 uart_remove_one_port(&serial_pxa_reg, &sport->port); 923 uart_remove_one_port(&serial_pxa_reg, &sport->port);
951 924
952 clk_unprepare(sport->clk); 925 clk_unprepare(sport->clk);
@@ -970,7 +943,7 @@ static struct platform_driver serial_pxa_driver = {
970 }, 943 },
971}; 944};
972 945
973int __init serial_pxa_init(void) 946static int __init serial_pxa_init(void)
974{ 947{
975 int ret; 948 int ret;
976 949
@@ -985,7 +958,7 @@ int __init serial_pxa_init(void)
985 return ret; 958 return ret;
986} 959}
987 960
988void __exit serial_pxa_exit(void) 961static void __exit serial_pxa_exit(void)
989{ 962{
990 platform_driver_unregister(&serial_pxa_driver); 963 platform_driver_unregister(&serial_pxa_driver);
991 uart_unregister_driver(&serial_pxa_reg); 964 uart_unregister_driver(&serial_pxa_reg);
diff --git a/drivers/tty/serial/rp2.c b/drivers/tty/serial/rp2.c
index a314a943f124..328d6deb6b08 100644
--- a/drivers/tty/serial/rp2.c
+++ b/drivers/tty/serial/rp2.c
@@ -427,7 +427,9 @@ static void rp2_rx_chars(struct rp2_uart_port *up)
427 up->port.icount.rx++; 427 up->port.icount.rx++;
428 } 428 }
429 429
430 spin_unlock(&up->port.lock);
430 tty_flip_buffer_push(port); 431 tty_flip_buffer_push(port);
432 spin_lock(&up->port.lock);
431} 433}
432 434
433static void rp2_tx_chars(struct rp2_uart_port *up) 435static void rp2_tx_chars(struct rp2_uart_port *up)
diff --git a/drivers/tty/serial/sa1100.c b/drivers/tty/serial/sa1100.c
index af6b3e3ad24d..ba25722a7131 100644
--- a/drivers/tty/serial/sa1100.c
+++ b/drivers/tty/serial/sa1100.c
@@ -232,7 +232,10 @@ sa1100_rx_chars(struct sa1100_port *sport)
232 status = UTSR1_TO_SM(UART_GET_UTSR1(sport)) | 232 status = UTSR1_TO_SM(UART_GET_UTSR1(sport)) |
233 UTSR0_TO_SM(UART_GET_UTSR0(sport)); 233 UTSR0_TO_SM(UART_GET_UTSR0(sport));
234 } 234 }
235
236 spin_unlock(&sport->port.lock);
235 tty_flip_buffer_push(&sport->port.state->port); 237 tty_flip_buffer_push(&sport->port.state->port);
238 spin_lock(&sport->port.lock);
236} 239}
237 240
238static void sa1100_tx_chars(struct sa1100_port *sport) 241static void sa1100_tx_chars(struct sa1100_port *sport)
@@ -864,8 +867,6 @@ static int sa1100_serial_remove(struct platform_device *pdev)
864{ 867{
865 struct sa1100_port *sport = platform_get_drvdata(pdev); 868 struct sa1100_port *sport = platform_get_drvdata(pdev);
866 869
867 platform_set_drvdata(pdev, NULL);
868
869 if (sport) 870 if (sport)
870 uart_remove_one_port(&sa1100_reg, &sport->port); 871 uart_remove_one_port(&sa1100_reg, &sport->port);
871 872
diff --git a/drivers/tty/serial/samsung.c b/drivers/tty/serial/samsung.c
index 376079b9bd75..f3dfa19a1cb8 100644
--- a/drivers/tty/serial/samsung.c
+++ b/drivers/tty/serial/samsung.c
@@ -249,6 +249,8 @@ s3c24xx_serial_rx_chars(int irq, void *dev_id)
249 ufcon |= S3C2410_UFCON_RESETRX; 249 ufcon |= S3C2410_UFCON_RESETRX;
250 wr_regl(port, S3C2410_UFCON, ufcon); 250 wr_regl(port, S3C2410_UFCON, ufcon);
251 rx_enabled(port) = 1; 251 rx_enabled(port) = 1;
252 spin_unlock_irqrestore(&port->lock,
253 flags);
252 goto out; 254 goto out;
253 } 255 }
254 continue; 256 continue;
@@ -297,10 +299,11 @@ s3c24xx_serial_rx_chars(int irq, void *dev_id)
297 ignore_char: 299 ignore_char:
298 continue; 300 continue;
299 } 301 }
302
303 spin_unlock_irqrestore(&port->lock, flags);
300 tty_flip_buffer_push(&port->state->port); 304 tty_flip_buffer_push(&port->state->port);
301 305
302 out: 306 out:
303 spin_unlock_irqrestore(&port->lock, flags);
304 return IRQ_HANDLED; 307 return IRQ_HANDLED;
305} 308}
306 309
@@ -1250,8 +1253,8 @@ static int s3c24xx_serial_probe(struct platform_device *pdev)
1250 1253
1251 ourport->baudclk = ERR_PTR(-EINVAL); 1254 ourport->baudclk = ERR_PTR(-EINVAL);
1252 ourport->info = ourport->drv_data->info; 1255 ourport->info = ourport->drv_data->info;
1253 ourport->cfg = (pdev->dev.platform_data) ? 1256 ourport->cfg = (dev_get_platdata(&pdev->dev)) ?
1254 (struct s3c2410_uartcfg *)pdev->dev.platform_data : 1257 (struct s3c2410_uartcfg *)dev_get_platdata(&pdev->dev) :
1255 ourport->drv_data->def_cfg; 1258 ourport->drv_data->def_cfg;
1256 1259
1257 ourport->port.fifosize = (ourport->info->fifosize) ? 1260 ourport->port.fifosize = (ourport->info->fifosize) ?
diff --git a/drivers/tty/serial/samsung.h b/drivers/tty/serial/samsung.h
index 00a499ecd385..aaa617a6c499 100644
--- a/drivers/tty/serial/samsung.h
+++ b/drivers/tty/serial/samsung.h
@@ -68,7 +68,8 @@ struct s3c24xx_uart_port {
68/* register access controls */ 68/* register access controls */
69 69
70#define portaddr(port, reg) ((port)->membase + (reg)) 70#define portaddr(port, reg) ((port)->membase + (reg))
71#define portaddrl(port, reg) ((unsigned long *)((port)->membase + (reg))) 71#define portaddrl(port, reg) \
72 ((unsigned long *)(unsigned long)((port)->membase + (reg)))
72 73
73#define rd_regb(port, reg) (__raw_readb(portaddr(port, reg))) 74#define rd_regb(port, reg) (__raw_readb(portaddr(port, reg)))
74#define rd_regl(port, reg) (__raw_readl(portaddr(port, reg))) 75#define rd_regl(port, reg) (__raw_readl(portaddr(port, reg)))
diff --git a/drivers/tty/serial/sc26xx.c b/drivers/tty/serial/sc26xx.c
index 4b1434d53e9d..887b4f770749 100644
--- a/drivers/tty/serial/sc26xx.c
+++ b/drivers/tty/serial/sc26xx.c
@@ -637,7 +637,7 @@ static int sc26xx_probe(struct platform_device *dev)
637{ 637{
638 struct resource *res; 638 struct resource *res;
639 struct uart_sc26xx_port *up; 639 struct uart_sc26xx_port *up;
640 unsigned int *sc26xx_data = dev->dev.platform_data; 640 unsigned int *sc26xx_data = dev_get_platdata(&dev->dev);
641 int err; 641 int err;
642 642
643 res = platform_get_resource(dev, IORESOURCE_MEM, 0); 643 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
diff --git a/drivers/tty/serial/sccnxp.c b/drivers/tty/serial/sccnxp.c
index c77304155410..49e9bbfe6cab 100644
--- a/drivers/tty/serial/sccnxp.c
+++ b/drivers/tty/serial/sccnxp.c
@@ -15,6 +15,7 @@
15#define SUPPORT_SYSRQ 15#define SUPPORT_SYSRQ
16#endif 16#endif
17 17
18#include <linux/clk.h>
18#include <linux/err.h> 19#include <linux/err.h>
19#include <linux/module.h> 20#include <linux/module.h>
20#include <linux/device.h> 21#include <linux/device.h>
@@ -94,16 +95,17 @@
94#define MCTRL_IBIT(cfg, sig) ((((cfg) >> (sig)) & 0xf) - LINE_IP0) 95#define MCTRL_IBIT(cfg, sig) ((((cfg) >> (sig)) & 0xf) - LINE_IP0)
95#define MCTRL_OBIT(cfg, sig) ((((cfg) >> (sig)) & 0xf) - LINE_OP0) 96#define MCTRL_OBIT(cfg, sig) ((((cfg) >> (sig)) & 0xf) - LINE_OP0)
96 97
97/* Supported chip types */ 98#define SCCNXP_HAVE_IO 0x00000001
98enum { 99#define SCCNXP_HAVE_MR0 0x00000002
99 SCCNXP_TYPE_SC2681 = 2681, 100
100 SCCNXP_TYPE_SC2691 = 2691, 101struct sccnxp_chip {
101 SCCNXP_TYPE_SC2692 = 2692, 102 const char *name;
102 SCCNXP_TYPE_SC2891 = 2891, 103 unsigned int nr;
103 SCCNXP_TYPE_SC2892 = 2892, 104 unsigned long freq_min;
104 SCCNXP_TYPE_SC28202 = 28202, 105 unsigned long freq_std;
105 SCCNXP_TYPE_SC68681 = 68681, 106 unsigned long freq_max;
106 SCCNXP_TYPE_SC68692 = 68692, 107 unsigned int flags;
108 unsigned int fifosize;
107}; 109};
108 110
109struct sccnxp_port { 111struct sccnxp_port {
@@ -111,16 +113,10 @@ struct sccnxp_port {
111 struct uart_port port[SCCNXP_MAX_UARTS]; 113 struct uart_port port[SCCNXP_MAX_UARTS];
112 bool opened[SCCNXP_MAX_UARTS]; 114 bool opened[SCCNXP_MAX_UARTS];
113 115
114 const char *name;
115 int irq; 116 int irq;
116
117 u8 imr; 117 u8 imr;
118 u8 addr_mask;
119 int freq_std;
120 118
121 int flags; 119 struct sccnxp_chip *chip;
122#define SCCNXP_HAVE_IO 0x00000001
123#define SCCNXP_HAVE_MR0 0x00000002
124 120
125#ifdef CONFIG_SERIAL_SCCNXP_CONSOLE 121#ifdef CONFIG_SERIAL_SCCNXP_CONSOLE
126 struct console console; 122 struct console console;
@@ -136,29 +132,94 @@ struct sccnxp_port {
136 struct regulator *regulator; 132 struct regulator *regulator;
137}; 133};
138 134
139static inline u8 sccnxp_raw_read(void __iomem *base, u8 reg, u8 shift) 135static const struct sccnxp_chip sc2681 = {
140{ 136 .name = "SC2681",
141 return readb(base + (reg << shift)); 137 .nr = 2,
142} 138 .freq_min = 1000000,
139 .freq_std = 3686400,
140 .freq_max = 4000000,
141 .flags = SCCNXP_HAVE_IO,
142 .fifosize = 3,
143};
143 144
144static inline void sccnxp_raw_write(void __iomem *base, u8 reg, u8 shift, u8 v) 145static const struct sccnxp_chip sc2691 = {
145{ 146 .name = "SC2691",
146 writeb(v, base + (reg << shift)); 147 .nr = 1,
147} 148 .freq_min = 1000000,
149 .freq_std = 3686400,
150 .freq_max = 4000000,
151 .flags = 0,
152 .fifosize = 3,
153};
154
155static const struct sccnxp_chip sc2692 = {
156 .name = "SC2692",
157 .nr = 2,
158 .freq_min = 1000000,
159 .freq_std = 3686400,
160 .freq_max = 4000000,
161 .flags = SCCNXP_HAVE_IO,
162 .fifosize = 3,
163};
164
165static const struct sccnxp_chip sc2891 = {
166 .name = "SC2891",
167 .nr = 1,
168 .freq_min = 100000,
169 .freq_std = 3686400,
170 .freq_max = 8000000,
171 .flags = SCCNXP_HAVE_IO | SCCNXP_HAVE_MR0,
172 .fifosize = 16,
173};
174
175static const struct sccnxp_chip sc2892 = {
176 .name = "SC2892",
177 .nr = 2,
178 .freq_min = 100000,
179 .freq_std = 3686400,
180 .freq_max = 8000000,
181 .flags = SCCNXP_HAVE_IO | SCCNXP_HAVE_MR0,
182 .fifosize = 16,
183};
184
185static const struct sccnxp_chip sc28202 = {
186 .name = "SC28202",
187 .nr = 2,
188 .freq_min = 1000000,
189 .freq_std = 14745600,
190 .freq_max = 50000000,
191 .flags = SCCNXP_HAVE_IO | SCCNXP_HAVE_MR0,
192 .fifosize = 256,
193};
194
195static const struct sccnxp_chip sc68681 = {
196 .name = "SC68681",
197 .nr = 2,
198 .freq_min = 1000000,
199 .freq_std = 3686400,
200 .freq_max = 4000000,
201 .flags = SCCNXP_HAVE_IO,
202 .fifosize = 3,
203};
204
205static const struct sccnxp_chip sc68692 = {
206 .name = "SC68692",
207 .nr = 2,
208 .freq_min = 1000000,
209 .freq_std = 3686400,
210 .freq_max = 4000000,
211 .flags = SCCNXP_HAVE_IO,
212 .fifosize = 3,
213};
148 214
149static inline u8 sccnxp_read(struct uart_port *port, u8 reg) 215static inline u8 sccnxp_read(struct uart_port *port, u8 reg)
150{ 216{
151 struct sccnxp_port *s = dev_get_drvdata(port->dev); 217 return readb(port->membase + (reg << port->regshift));
152
153 return sccnxp_raw_read(port->membase, reg & s->addr_mask,
154 port->regshift);
155} 218}
156 219
157static inline void sccnxp_write(struct uart_port *port, u8 reg, u8 v) 220static inline void sccnxp_write(struct uart_port *port, u8 reg, u8 v)
158{ 221{
159 struct sccnxp_port *s = dev_get_drvdata(port->dev); 222 writeb(v, port->membase + (reg << port->regshift));
160
161 sccnxp_raw_write(port->membase, reg & s->addr_mask, port->regshift, v);
162} 223}
163 224
164static inline u8 sccnxp_port_read(struct uart_port *port, u8 reg) 225static inline u8 sccnxp_port_read(struct uart_port *port, u8 reg)
@@ -224,13 +285,14 @@ static int sccnxp_set_baud(struct uart_port *port, int baud)
224{ 285{
225 struct sccnxp_port *s = dev_get_drvdata(port->dev); 286 struct sccnxp_port *s = dev_get_drvdata(port->dev);
226 int div_std, tmp_baud, bestbaud = baud, besterr = -1; 287 int div_std, tmp_baud, bestbaud = baud, besterr = -1;
288 struct sccnxp_chip *chip = s->chip;
227 u8 i, acr = 0, csr = 0, mr0 = 0; 289 u8 i, acr = 0, csr = 0, mr0 = 0;
228 290
229 /* Find best baud from table */ 291 /* Find best baud from table */
230 for (i = 0; baud_std[i].baud && besterr; i++) { 292 for (i = 0; baud_std[i].baud && besterr; i++) {
231 if (baud_std[i].mr0 && !(s->flags & SCCNXP_HAVE_MR0)) 293 if (baud_std[i].mr0 && !(chip->flags & SCCNXP_HAVE_MR0))
232 continue; 294 continue;
233 div_std = DIV_ROUND_CLOSEST(s->freq_std, baud_std[i].baud); 295 div_std = DIV_ROUND_CLOSEST(chip->freq_std, baud_std[i].baud);
234 tmp_baud = DIV_ROUND_CLOSEST(port->uartclk, div_std); 296 tmp_baud = DIV_ROUND_CLOSEST(port->uartclk, div_std);
235 if (!sccnxp_update_best_err(baud, tmp_baud, &besterr)) { 297 if (!sccnxp_update_best_err(baud, tmp_baud, &besterr)) {
236 acr = baud_std[i].acr; 298 acr = baud_std[i].acr;
@@ -240,7 +302,7 @@ static int sccnxp_set_baud(struct uart_port *port, int baud)
240 } 302 }
241 } 303 }
242 304
243 if (s->flags & SCCNXP_HAVE_MR0) { 305 if (chip->flags & SCCNXP_HAVE_MR0) {
244 /* Enable FIFO, set half level for TX */ 306 /* Enable FIFO, set half level for TX */
245 mr0 |= MR0_FIFO | MR0_TXLVL; 307 mr0 |= MR0_FIFO | MR0_TXLVL;
246 /* Update MR0 */ 308 /* Update MR0 */
@@ -363,7 +425,7 @@ static void sccnxp_handle_tx(struct uart_port *port)
363 sccnxp_disable_irq(port, IMR_TXRDY); 425 sccnxp_disable_irq(port, IMR_TXRDY);
364 426
365 /* Set direction to input */ 427 /* Set direction to input */
366 if (s->flags & SCCNXP_HAVE_IO) 428 if (s->chip->flags & SCCNXP_HAVE_IO)
367 sccnxp_set_bit(port, DIR_OP, 0); 429 sccnxp_set_bit(port, DIR_OP, 0);
368 } 430 }
369 return; 431 return;
@@ -437,7 +499,7 @@ static void sccnxp_start_tx(struct uart_port *port)
437 spin_lock_irqsave(&s->lock, flags); 499 spin_lock_irqsave(&s->lock, flags);
438 500
439 /* Set direction to output */ 501 /* Set direction to output */
440 if (s->flags & SCCNXP_HAVE_IO) 502 if (s->chip->flags & SCCNXP_HAVE_IO)
441 sccnxp_set_bit(port, DIR_OP, 1); 503 sccnxp_set_bit(port, DIR_OP, 1);
442 504
443 sccnxp_enable_irq(port, IMR_TXRDY); 505 sccnxp_enable_irq(port, IMR_TXRDY);
@@ -483,7 +545,7 @@ static void sccnxp_set_mctrl(struct uart_port *port, unsigned int mctrl)
483 struct sccnxp_port *s = dev_get_drvdata(port->dev); 545 struct sccnxp_port *s = dev_get_drvdata(port->dev);
484 unsigned long flags; 546 unsigned long flags;
485 547
486 if (!(s->flags & SCCNXP_HAVE_IO)) 548 if (!(s->chip->flags & SCCNXP_HAVE_IO))
487 return; 549 return;
488 550
489 spin_lock_irqsave(&s->lock, flags); 551 spin_lock_irqsave(&s->lock, flags);
@@ -501,7 +563,7 @@ static unsigned int sccnxp_get_mctrl(struct uart_port *port)
501 struct sccnxp_port *s = dev_get_drvdata(port->dev); 563 struct sccnxp_port *s = dev_get_drvdata(port->dev);
502 unsigned int mctrl = TIOCM_DSR | TIOCM_CTS | TIOCM_CAR; 564 unsigned int mctrl = TIOCM_DSR | TIOCM_CTS | TIOCM_CAR;
503 565
504 if (!(s->flags & SCCNXP_HAVE_IO)) 566 if (!(s->chip->flags & SCCNXP_HAVE_IO))
505 return mctrl; 567 return mctrl;
506 568
507 spin_lock_irqsave(&s->lock, flags); 569 spin_lock_irqsave(&s->lock, flags);
@@ -617,7 +679,7 @@ static void sccnxp_set_termios(struct uart_port *port,
617 679
618 /* Setup baudrate */ 680 /* Setup baudrate */
619 baud = uart_get_baud_rate(port, termios, old, 50, 681 baud = uart_get_baud_rate(port, termios, old, 50,
620 (s->flags & SCCNXP_HAVE_MR0) ? 682 (s->chip->flags & SCCNXP_HAVE_MR0) ?
621 230400 : 38400); 683 230400 : 38400);
622 baud = sccnxp_set_baud(port, baud); 684 baud = sccnxp_set_baud(port, baud);
623 685
@@ -641,7 +703,7 @@ static int sccnxp_startup(struct uart_port *port)
641 703
642 spin_lock_irqsave(&s->lock, flags); 704 spin_lock_irqsave(&s->lock, flags);
643 705
644 if (s->flags & SCCNXP_HAVE_IO) { 706 if (s->chip->flags & SCCNXP_HAVE_IO) {
645 /* Outputs are controlled manually */ 707 /* Outputs are controlled manually */
646 sccnxp_write(port, SCCNXP_OPCR_REG, 0); 708 sccnxp_write(port, SCCNXP_OPCR_REG, 0);
647 } 709 }
@@ -681,7 +743,7 @@ static void sccnxp_shutdown(struct uart_port *port)
681 sccnxp_port_write(port, SCCNXP_CR_REG, CR_RX_DISABLE | CR_TX_DISABLE); 743 sccnxp_port_write(port, SCCNXP_CR_REG, CR_RX_DISABLE | CR_TX_DISABLE);
682 744
683 /* Leave direction to input */ 745 /* Leave direction to input */
684 if (s->flags & SCCNXP_HAVE_IO) 746 if (s->chip->flags & SCCNXP_HAVE_IO)
685 sccnxp_set_bit(port, DIR_OP, 0); 747 sccnxp_set_bit(port, DIR_OP, 0);
686 748
687 spin_unlock_irqrestore(&s->lock, flags); 749 spin_unlock_irqrestore(&s->lock, flags);
@@ -691,7 +753,7 @@ static const char *sccnxp_type(struct uart_port *port)
691{ 753{
692 struct sccnxp_port *s = dev_get_drvdata(port->dev); 754 struct sccnxp_port *s = dev_get_drvdata(port->dev);
693 755
694 return (port->type == PORT_SC26XX) ? s->name : NULL; 756 return (port->type == PORT_SC26XX) ? s->chip->name : NULL;
695} 757}
696 758
697static void sccnxp_release_port(struct uart_port *port) 759static void sccnxp_release_port(struct uart_port *port)
@@ -778,19 +840,31 @@ static int sccnxp_console_setup(struct console *co, char *options)
778} 840}
779#endif 841#endif
780 842
843static const struct platform_device_id sccnxp_id_table[] = {
844 { .name = "sc2681", .driver_data = (kernel_ulong_t)&sc2681, },
845 { .name = "sc2691", .driver_data = (kernel_ulong_t)&sc2691, },
846 { .name = "sc2692", .driver_data = (kernel_ulong_t)&sc2692, },
847 { .name = "sc2891", .driver_data = (kernel_ulong_t)&sc2891, },
848 { .name = "sc2892", .driver_data = (kernel_ulong_t)&sc2892, },
849 { .name = "sc28202", .driver_data = (kernel_ulong_t)&sc28202, },
850 { .name = "sc68681", .driver_data = (kernel_ulong_t)&sc68681, },
851 { .name = "sc68692", .driver_data = (kernel_ulong_t)&sc68692, },
852 { }
853};
854MODULE_DEVICE_TABLE(platform, sccnxp_id_table);
855
781static int sccnxp_probe(struct platform_device *pdev) 856static int sccnxp_probe(struct platform_device *pdev)
782{ 857{
783 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 858 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
784 int chiptype = pdev->id_entry->driver_data;
785 struct sccnxp_pdata *pdata = dev_get_platdata(&pdev->dev); 859 struct sccnxp_pdata *pdata = dev_get_platdata(&pdev->dev);
786 int i, ret, fifosize, freq_min, freq_max; 860 int i, ret, uartclk;
787 struct sccnxp_port *s; 861 struct sccnxp_port *s;
788 void __iomem *membase; 862 void __iomem *membase;
863 struct clk *clk;
789 864
790 if (!res) { 865 membase = devm_ioremap_resource(&pdev->dev, res);
791 dev_err(&pdev->dev, "Missing memory resource data\n"); 866 if (IS_ERR(membase))
792 return -EADDRNOTAVAIL; 867 return PTR_ERR(membase);
793 }
794 868
795 s = devm_kzalloc(&pdev->dev, sizeof(struct sccnxp_port), GFP_KERNEL); 869 s = devm_kzalloc(&pdev->dev, sizeof(struct sccnxp_port), GFP_KERNEL);
796 if (!s) { 870 if (!s) {
@@ -801,99 +875,38 @@ static int sccnxp_probe(struct platform_device *pdev)
801 875
802 spin_lock_init(&s->lock); 876 spin_lock_init(&s->lock);
803 877
804 /* Individual chip settings */ 878 s->chip = (struct sccnxp_chip *)pdev->id_entry->driver_data;
805 switch (chiptype) { 879
806 case SCCNXP_TYPE_SC2681: 880 s->regulator = devm_regulator_get(&pdev->dev, "vcc");
807 s->name = "SC2681"; 881 if (!IS_ERR(s->regulator)) {
808 s->uart.nr = 2; 882 ret = regulator_enable(s->regulator);
809 s->freq_std = 3686400; 883 if (ret) {
810 s->addr_mask = 0x0f; 884 dev_err(&pdev->dev,
811 s->flags = SCCNXP_HAVE_IO; 885 "Failed to enable regulator: %i\n", ret);
812 fifosize = 3; 886 return ret;
813 freq_min = 1000000; 887 }
814 freq_max = 4000000; 888 } else if (PTR_ERR(s->regulator) == -EPROBE_DEFER)
815 break; 889 return -EPROBE_DEFER;
816 case SCCNXP_TYPE_SC2691: 890
817 s->name = "SC2691"; 891 clk = devm_clk_get(&pdev->dev, NULL);
818 s->uart.nr = 1; 892 if (IS_ERR(clk)) {
819 s->freq_std = 3686400; 893 if (PTR_ERR(clk) == -EPROBE_DEFER) {
820 s->addr_mask = 0x07; 894 ret = -EPROBE_DEFER;
821 s->flags = 0; 895 goto err_out;
822 fifosize = 3; 896 }
823 freq_min = 1000000; 897 dev_notice(&pdev->dev, "Using default clock frequency\n");
824 freq_max = 4000000; 898 uartclk = s->chip->freq_std;
825 break; 899 } else
826 case SCCNXP_TYPE_SC2692: 900 uartclk = clk_get_rate(clk);
827 s->name = "SC2692"; 901
828 s->uart.nr = 2; 902 /* Check input frequency */
829 s->freq_std = 3686400; 903 if ((uartclk < s->chip->freq_min) || (uartclk > s->chip->freq_max)) {
830 s->addr_mask = 0x0f; 904 dev_err(&pdev->dev, "Frequency out of bounds\n");
831 s->flags = SCCNXP_HAVE_IO; 905 ret = -EINVAL;
832 fifosize = 3;
833 freq_min = 1000000;
834 freq_max = 4000000;
835 break;
836 case SCCNXP_TYPE_SC2891:
837 s->name = "SC2891";
838 s->uart.nr = 1;
839 s->freq_std = 3686400;
840 s->addr_mask = 0x0f;
841 s->flags = SCCNXP_HAVE_IO | SCCNXP_HAVE_MR0;
842 fifosize = 16;
843 freq_min = 100000;
844 freq_max = 8000000;
845 break;
846 case SCCNXP_TYPE_SC2892:
847 s->name = "SC2892";
848 s->uart.nr = 2;
849 s->freq_std = 3686400;
850 s->addr_mask = 0x0f;
851 s->flags = SCCNXP_HAVE_IO | SCCNXP_HAVE_MR0;
852 fifosize = 16;
853 freq_min = 100000;
854 freq_max = 8000000;
855 break;
856 case SCCNXP_TYPE_SC28202:
857 s->name = "SC28202";
858 s->uart.nr = 2;
859 s->freq_std = 14745600;
860 s->addr_mask = 0x7f;
861 s->flags = SCCNXP_HAVE_IO | SCCNXP_HAVE_MR0;
862 fifosize = 256;
863 freq_min = 1000000;
864 freq_max = 50000000;
865 break;
866 case SCCNXP_TYPE_SC68681:
867 s->name = "SC68681";
868 s->uart.nr = 2;
869 s->freq_std = 3686400;
870 s->addr_mask = 0x0f;
871 s->flags = SCCNXP_HAVE_IO;
872 fifosize = 3;
873 freq_min = 1000000;
874 freq_max = 4000000;
875 break;
876 case SCCNXP_TYPE_SC68692:
877 s->name = "SC68692";
878 s->uart.nr = 2;
879 s->freq_std = 3686400;
880 s->addr_mask = 0x0f;
881 s->flags = SCCNXP_HAVE_IO;
882 fifosize = 3;
883 freq_min = 1000000;
884 freq_max = 4000000;
885 break;
886 default:
887 dev_err(&pdev->dev, "Unsupported chip type %i\n", chiptype);
888 ret = -ENOTSUPP;
889 goto err_out; 906 goto err_out;
890 } 907 }
891 908
892 if (!pdata) { 909 if (pdata)
893 dev_warn(&pdev->dev,
894 "No platform data supplied, using defaults\n");
895 s->pdata.frequency = s->freq_std;
896 } else
897 memcpy(&s->pdata, pdata, sizeof(struct sccnxp_pdata)); 910 memcpy(&s->pdata, pdata, sizeof(struct sccnxp_pdata));
898 911
899 if (s->pdata.poll_time_us) { 912 if (s->pdata.poll_time_us) {
@@ -911,34 +924,11 @@ static int sccnxp_probe(struct platform_device *pdev)
911 } 924 }
912 } 925 }
913 926
914 /* Check input frequency */
915 if ((s->pdata.frequency < freq_min) ||
916 (s->pdata.frequency > freq_max)) {
917 dev_err(&pdev->dev, "Frequency out of bounds\n");
918 ret = -EINVAL;
919 goto err_out;
920 }
921
922 s->regulator = devm_regulator_get(&pdev->dev, "VCC");
923 if (!IS_ERR(s->regulator)) {
924 ret = regulator_enable(s->regulator);
925 if (ret) {
926 dev_err(&pdev->dev,
927 "Failed to enable regulator: %i\n", ret);
928 return ret;
929 }
930 }
931
932 membase = devm_ioremap_resource(&pdev->dev, res);
933 if (IS_ERR(membase)) {
934 ret = PTR_ERR(membase);
935 goto err_out;
936 }
937
938 s->uart.owner = THIS_MODULE; 927 s->uart.owner = THIS_MODULE;
939 s->uart.dev_name = "ttySC"; 928 s->uart.dev_name = "ttySC";
940 s->uart.major = SCCNXP_MAJOR; 929 s->uart.major = SCCNXP_MAJOR;
941 s->uart.minor = SCCNXP_MINOR; 930 s->uart.minor = SCCNXP_MINOR;
931 s->uart.nr = s->chip->nr;
942#ifdef CONFIG_SERIAL_SCCNXP_CONSOLE 932#ifdef CONFIG_SERIAL_SCCNXP_CONSOLE
943 s->uart.cons = &s->console; 933 s->uart.cons = &s->console;
944 s->uart.cons->device = uart_console_device; 934 s->uart.cons->device = uart_console_device;
@@ -960,17 +950,17 @@ static int sccnxp_probe(struct platform_device *pdev)
960 s->port[i].dev = &pdev->dev; 950 s->port[i].dev = &pdev->dev;
961 s->port[i].irq = s->irq; 951 s->port[i].irq = s->irq;
962 s->port[i].type = PORT_SC26XX; 952 s->port[i].type = PORT_SC26XX;
963 s->port[i].fifosize = fifosize; 953 s->port[i].fifosize = s->chip->fifosize;
964 s->port[i].flags = UPF_SKIP_TEST | UPF_FIXED_TYPE; 954 s->port[i].flags = UPF_SKIP_TEST | UPF_FIXED_TYPE;
965 s->port[i].iotype = UPIO_MEM; 955 s->port[i].iotype = UPIO_MEM;
966 s->port[i].mapbase = res->start; 956 s->port[i].mapbase = res->start;
967 s->port[i].membase = membase; 957 s->port[i].membase = membase;
968 s->port[i].regshift = s->pdata.reg_shift; 958 s->port[i].regshift = s->pdata.reg_shift;
969 s->port[i].uartclk = s->pdata.frequency; 959 s->port[i].uartclk = uartclk;
970 s->port[i].ops = &sccnxp_ops; 960 s->port[i].ops = &sccnxp_ops;
971 uart_add_one_port(&s->uart, &s->port[i]); 961 uart_add_one_port(&s->uart, &s->port[i]);
972 /* Set direction to input */ 962 /* Set direction to input */
973 if (s->flags & SCCNXP_HAVE_IO) 963 if (s->chip->flags & SCCNXP_HAVE_IO)
974 sccnxp_set_bit(&s->port[i], DIR_OP, 0); 964 sccnxp_set_bit(&s->port[i], DIR_OP, 0);
975 } 965 }
976 966
@@ -997,7 +987,8 @@ static int sccnxp_probe(struct platform_device *pdev)
997 } 987 }
998 988
999err_out: 989err_out:
1000 platform_set_drvdata(pdev, NULL); 990 if (!IS_ERR(s->regulator))
991 return regulator_disable(s->regulator);
1001 992
1002 return ret; 993 return ret;
1003} 994}
@@ -1016,7 +1007,6 @@ static int sccnxp_remove(struct platform_device *pdev)
1016 uart_remove_one_port(&s->uart, &s->port[i]); 1007 uart_remove_one_port(&s->uart, &s->port[i]);
1017 1008
1018 uart_unregister_driver(&s->uart); 1009 uart_unregister_driver(&s->uart);
1019 platform_set_drvdata(pdev, NULL);
1020 1010
1021 if (!IS_ERR(s->regulator)) 1011 if (!IS_ERR(s->regulator))
1022 return regulator_disable(s->regulator); 1012 return regulator_disable(s->regulator);
@@ -1024,19 +1014,6 @@ static int sccnxp_remove(struct platform_device *pdev)
1024 return 0; 1014 return 0;
1025} 1015}
1026 1016
1027static const struct platform_device_id sccnxp_id_table[] = {
1028 { "sc2681", SCCNXP_TYPE_SC2681 },
1029 { "sc2691", SCCNXP_TYPE_SC2691 },
1030 { "sc2692", SCCNXP_TYPE_SC2692 },
1031 { "sc2891", SCCNXP_TYPE_SC2891 },
1032 { "sc2892", SCCNXP_TYPE_SC2892 },
1033 { "sc28202", SCCNXP_TYPE_SC28202 },
1034 { "sc68681", SCCNXP_TYPE_SC68681 },
1035 { "sc68692", SCCNXP_TYPE_SC68692 },
1036 { },
1037};
1038MODULE_DEVICE_TABLE(platform, sccnxp_id_table);
1039
1040static struct platform_driver sccnxp_uart_driver = { 1017static struct platform_driver sccnxp_uart_driver = {
1041 .driver = { 1018 .driver = {
1042 .name = SCCNXP_NAME, 1019 .name = SCCNXP_NAME,
diff --git a/drivers/tty/serial/serial-tegra.c b/drivers/tty/serial/serial-tegra.c
index ee7c8123c374..d0d972f7e43e 100644
--- a/drivers/tty/serial/serial-tegra.c
+++ b/drivers/tty/serial/serial-tegra.c
@@ -571,7 +571,9 @@ static void tegra_uart_rx_dma_complete(void *args)
571 571
572 tegra_uart_handle_rx_pio(tup, port); 572 tegra_uart_handle_rx_pio(tup, port);
573 if (tty) { 573 if (tty) {
574 spin_unlock_irqrestore(&u->lock, flags);
574 tty_flip_buffer_push(port); 575 tty_flip_buffer_push(port);
576 spin_lock_irqsave(&u->lock, flags);
575 tty_kref_put(tty); 577 tty_kref_put(tty);
576 } 578 }
577 tegra_uart_start_rx_dma(tup); 579 tegra_uart_start_rx_dma(tup);
@@ -583,11 +585,13 @@ static void tegra_uart_rx_dma_complete(void *args)
583 spin_unlock_irqrestore(&u->lock, flags); 585 spin_unlock_irqrestore(&u->lock, flags);
584} 586}
585 587
586static void tegra_uart_handle_rx_dma(struct tegra_uart_port *tup) 588static void tegra_uart_handle_rx_dma(struct tegra_uart_port *tup,
589 unsigned long *flags)
587{ 590{
588 struct dma_tx_state state; 591 struct dma_tx_state state;
589 struct tty_struct *tty = tty_port_tty_get(&tup->uport.state->port); 592 struct tty_struct *tty = tty_port_tty_get(&tup->uport.state->port);
590 struct tty_port *port = &tup->uport.state->port; 593 struct tty_port *port = &tup->uport.state->port;
594 struct uart_port *u = &tup->uport;
591 int count; 595 int count;
592 596
593 /* Deactivate flow control to stop sender */ 597 /* Deactivate flow control to stop sender */
@@ -604,7 +608,9 @@ static void tegra_uart_handle_rx_dma(struct tegra_uart_port *tup)
604 608
605 tegra_uart_handle_rx_pio(tup, port); 609 tegra_uart_handle_rx_pio(tup, port);
606 if (tty) { 610 if (tty) {
611 spin_unlock_irqrestore(&u->lock, *flags);
607 tty_flip_buffer_push(port); 612 tty_flip_buffer_push(port);
613 spin_lock_irqsave(&u->lock, *flags);
608 tty_kref_put(tty); 614 tty_kref_put(tty);
609 } 615 }
610 tegra_uart_start_rx_dma(tup); 616 tegra_uart_start_rx_dma(tup);
@@ -671,7 +677,7 @@ static irqreturn_t tegra_uart_isr(int irq, void *data)
671 iir = tegra_uart_read(tup, UART_IIR); 677 iir = tegra_uart_read(tup, UART_IIR);
672 if (iir & UART_IIR_NO_INT) { 678 if (iir & UART_IIR_NO_INT) {
673 if (is_rx_int) { 679 if (is_rx_int) {
674 tegra_uart_handle_rx_dma(tup); 680 tegra_uart_handle_rx_dma(tup, &flags);
675 if (tup->rx_in_progress) { 681 if (tup->rx_in_progress) {
676 ier = tup->ier_shadow; 682 ier = tup->ier_shadow;
677 ier |= (UART_IER_RLSI | UART_IER_RTOIE | 683 ier |= (UART_IER_RLSI | UART_IER_RTOIE |
@@ -1206,7 +1212,7 @@ static struct uart_driver tegra_uart_driver = {
1206 .owner = THIS_MODULE, 1212 .owner = THIS_MODULE,
1207 .driver_name = "tegra_hsuart", 1213 .driver_name = "tegra_hsuart",
1208 .dev_name = "ttyTHS", 1214 .dev_name = "ttyTHS",
1209 .cons = 0, 1215 .cons = NULL,
1210 .nr = TEGRA_UART_MAXIMUM, 1216 .nr = TEGRA_UART_MAXIMUM,
1211}; 1217};
1212 1218
@@ -1237,13 +1243,13 @@ static int tegra_uart_parse_dt(struct platform_device *pdev,
1237 return 0; 1243 return 0;
1238} 1244}
1239 1245
1240struct tegra_uart_chip_data tegra20_uart_chip_data = { 1246static struct tegra_uart_chip_data tegra20_uart_chip_data = {
1241 .tx_fifo_full_status = false, 1247 .tx_fifo_full_status = false,
1242 .allow_txfifo_reset_fifo_mode = true, 1248 .allow_txfifo_reset_fifo_mode = true,
1243 .support_clk_src_div = false, 1249 .support_clk_src_div = false,
1244}; 1250};
1245 1251
1246struct tegra_uart_chip_data tegra30_uart_chip_data = { 1252static struct tegra_uart_chip_data tegra30_uart_chip_data = {
1247 .tx_fifo_full_status = true, 1253 .tx_fifo_full_status = true,
1248 .allow_txfifo_reset_fifo_mode = false, 1254 .allow_txfifo_reset_fifo_mode = false,
1249 .support_clk_src_div = true, 1255 .support_clk_src_div = true,
diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c
index 28cdd2829139..0f02351c9239 100644
--- a/drivers/tty/serial/serial_core.c
+++ b/drivers/tty/serial/serial_core.c
@@ -2095,12 +2095,12 @@ uart_report_port(struct uart_driver *drv, struct uart_port *port)
2095 break; 2095 break;
2096 } 2096 }
2097 2097
2098 printk(KERN_INFO "%s%s%s%d at %s (irq = %d) is a %s\n", 2098 printk(KERN_INFO "%s%s%s%d at %s (irq = %d, base_baud = %d) is a %s\n",
2099 port->dev ? dev_name(port->dev) : "", 2099 port->dev ? dev_name(port->dev) : "",
2100 port->dev ? ": " : "", 2100 port->dev ? ": " : "",
2101 drv->dev_name, 2101 drv->dev_name,
2102 drv->tty_driver->name_base + port->line, 2102 drv->tty_driver->name_base + port->line,
2103 address, port->irq, uart_type(port)); 2103 address, port->irq, port->uartclk / 16, uart_type(port));
2104} 2104}
2105 2105
2106static void 2106static void
diff --git a/drivers/tty/serial/serial_txx9.c b/drivers/tty/serial/serial_txx9.c
index fe48a0c2b4ca..440a962412da 100644
--- a/drivers/tty/serial/serial_txx9.c
+++ b/drivers/tty/serial/serial_txx9.c
@@ -1097,7 +1097,7 @@ static void serial_txx9_unregister_port(int line)
1097 */ 1097 */
1098static int serial_txx9_probe(struct platform_device *dev) 1098static int serial_txx9_probe(struct platform_device *dev)
1099{ 1099{
1100 struct uart_port *p = dev->dev.platform_data; 1100 struct uart_port *p = dev_get_platdata(&dev->dev);
1101 struct uart_port port; 1101 struct uart_port port;
1102 int ret, i; 1102 int ret, i;
1103 1103
diff --git a/drivers/tty/serial/sh-sci.c b/drivers/tty/serial/sh-sci.c
index 7477e0ea5cdb..537750261aaa 100644
--- a/drivers/tty/serial/sh-sci.c
+++ b/drivers/tty/serial/sh-sci.c
@@ -2380,7 +2380,7 @@ static char early_serial_buf[32];
2380 2380
2381static int sci_probe_earlyprintk(struct platform_device *pdev) 2381static int sci_probe_earlyprintk(struct platform_device *pdev)
2382{ 2382{
2383 struct plat_sci_port *cfg = pdev->dev.platform_data; 2383 struct plat_sci_port *cfg = dev_get_platdata(&pdev->dev);
2384 2384
2385 if (early_serial_console.data) 2385 if (early_serial_console.data)
2386 return -EEXIST; 2386 return -EEXIST;
@@ -2469,7 +2469,7 @@ static int sci_probe_single(struct platform_device *dev,
2469 2469
2470static int sci_probe(struct platform_device *dev) 2470static int sci_probe(struct platform_device *dev)
2471{ 2471{
2472 struct plat_sci_port *p = dev->dev.platform_data; 2472 struct plat_sci_port *p = dev_get_platdata(&dev->dev);
2473 struct sci_port *sp = &sci_ports[dev->id]; 2473 struct sci_port *sp = &sci_ports[dev->id];
2474 int ret; 2474 int ret;
2475 2475
diff --git a/drivers/tty/serial/sirfsoc_uart.c b/drivers/tty/serial/sirfsoc_uart.c
index 1fd564b8194b..61c1ad03db5b 100644
--- a/drivers/tty/serial/sirfsoc_uart.c
+++ b/drivers/tty/serial/sirfsoc_uart.c
@@ -20,9 +20,13 @@
20#include <linux/of.h> 20#include <linux/of.h>
21#include <linux/slab.h> 21#include <linux/slab.h>
22#include <linux/io.h> 22#include <linux/io.h>
23#include <linux/of_gpio.h>
24#include <linux/dmaengine.h>
25#include <linux/dma-direction.h>
26#include <linux/dma-mapping.h>
27#include <linux/sirfsoc_dma.h>
23#include <asm/irq.h> 28#include <asm/irq.h>
24#include <asm/mach/irq.h> 29#include <asm/mach/irq.h>
25#include <linux/pinctrl/consumer.h>
26 30
27#include "sirfsoc_uart.h" 31#include "sirfsoc_uart.h"
28 32
@@ -32,6 +36,9 @@ static unsigned int
32sirfsoc_uart_pio_rx_chars(struct uart_port *port, unsigned int max_rx_count); 36sirfsoc_uart_pio_rx_chars(struct uart_port *port, unsigned int max_rx_count);
33static struct uart_driver sirfsoc_uart_drv; 37static struct uart_driver sirfsoc_uart_drv;
34 38
39static void sirfsoc_uart_tx_dma_complete_callback(void *param);
40static void sirfsoc_uart_start_next_rx_dma(struct uart_port *port);
41static void sirfsoc_uart_rx_dma_complete_callback(void *param);
35static const struct sirfsoc_baudrate_to_regv baudrate_to_regv[] = { 42static const struct sirfsoc_baudrate_to_regv baudrate_to_regv[] = {
36 {4000000, 2359296}, 43 {4000000, 2359296},
37 {3500000, 1310721}, 44 {3500000, 1310721},
@@ -89,6 +96,13 @@ static struct sirfsoc_uart_port sirfsoc_uart_ports[SIRFSOC_UART_NR] = {
89 .line = 4, 96 .line = 4,
90 }, 97 },
91 }, 98 },
99 [5] = {
100 .port = {
101 .iotype = UPIO_MEM,
102 .flags = UPF_BOOT_AUTOCONF,
103 .line = 5,
104 },
105 },
92}; 106};
93 107
94static inline struct sirfsoc_uart_port *to_sirfport(struct uart_port *port) 108static inline struct sirfsoc_uart_port *to_sirfport(struct uart_port *port)
@@ -99,21 +113,28 @@ static inline struct sirfsoc_uart_port *to_sirfport(struct uart_port *port)
99static inline unsigned int sirfsoc_uart_tx_empty(struct uart_port *port) 113static inline unsigned int sirfsoc_uart_tx_empty(struct uart_port *port)
100{ 114{
101 unsigned long reg; 115 unsigned long reg;
102 reg = rd_regl(port, SIRFUART_TX_FIFO_STATUS); 116 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
103 if (reg & SIRFUART_FIFOEMPTY_MASK(port)) 117 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
104 return TIOCSER_TEMT; 118 struct sirfsoc_fifo_status *ufifo_st = &sirfport->uart_reg->fifo_status;
105 else 119 reg = rd_regl(port, ureg->sirfsoc_tx_fifo_status);
106 return 0; 120
121 return (reg & ufifo_st->ff_empty(port->line)) ? TIOCSER_TEMT : 0;
107} 122}
108 123
109static unsigned int sirfsoc_uart_get_mctrl(struct uart_port *port) 124static unsigned int sirfsoc_uart_get_mctrl(struct uart_port *port)
110{ 125{
111 struct sirfsoc_uart_port *sirfport = to_sirfport(port); 126 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
112 if (!(sirfport->ms_enabled)) { 127 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
128 if (!sirfport->hw_flow_ctrl || !sirfport->ms_enabled)
113 goto cts_asserted; 129 goto cts_asserted;
114 } else if (sirfport->hw_flow_ctrl) { 130 if (sirfport->uart_reg->uart_type == SIRF_REAL_UART) {
115 if (!(rd_regl(port, SIRFUART_AFC_CTRL) & 131 if (!(rd_regl(port, ureg->sirfsoc_afc_ctrl) &
116 SIRFUART_CTS_IN_STATUS)) 132 SIRFUART_AFC_CTS_STATUS))
133 goto cts_asserted;
134 else
135 goto cts_deasserted;
136 } else {
137 if (!gpio_get_value(sirfport->cts_gpio))
117 goto cts_asserted; 138 goto cts_asserted;
118 else 139 else
119 goto cts_deasserted; 140 goto cts_deasserted;
@@ -127,89 +148,276 @@ cts_asserted:
127static void sirfsoc_uart_set_mctrl(struct uart_port *port, unsigned int mctrl) 148static void sirfsoc_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
128{ 149{
129 struct sirfsoc_uart_port *sirfport = to_sirfport(port); 150 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
151 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
130 unsigned int assert = mctrl & TIOCM_RTS; 152 unsigned int assert = mctrl & TIOCM_RTS;
131 unsigned int val = assert ? SIRFUART_AFC_CTRL_RX_THD : 0x0; 153 unsigned int val = assert ? SIRFUART_AFC_CTRL_RX_THD : 0x0;
132 unsigned int current_val; 154 unsigned int current_val;
133 if (sirfport->hw_flow_ctrl) { 155
134 current_val = rd_regl(port, SIRFUART_AFC_CTRL) & ~0xFF; 156 if (!sirfport->hw_flow_ctrl || !sirfport->ms_enabled)
157 return;
158 if (sirfport->uart_reg->uart_type == SIRF_REAL_UART) {
159 current_val = rd_regl(port, ureg->sirfsoc_afc_ctrl) & ~0xFF;
135 val |= current_val; 160 val |= current_val;
136 wr_regl(port, SIRFUART_AFC_CTRL, val); 161 wr_regl(port, ureg->sirfsoc_afc_ctrl, val);
162 } else {
163 if (!val)
164 gpio_set_value(sirfport->rts_gpio, 1);
165 else
166 gpio_set_value(sirfport->rts_gpio, 0);
137 } 167 }
138} 168}
139 169
140static void sirfsoc_uart_stop_tx(struct uart_port *port) 170static void sirfsoc_uart_stop_tx(struct uart_port *port)
141{ 171{
142 unsigned int regv; 172 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
143 regv = rd_regl(port, SIRFUART_INT_EN); 173 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
144 wr_regl(port, SIRFUART_INT_EN, regv & ~SIRFUART_TX_INT_EN); 174 struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
175
176 if (IS_DMA_CHAN_VALID(sirfport->tx_dma_no)) {
177 if (sirfport->tx_dma_state == TX_DMA_RUNNING) {
178 dmaengine_pause(sirfport->tx_dma_chan);
179 sirfport->tx_dma_state = TX_DMA_PAUSE;
180 } else {
181 if (!sirfport->is_marco)
182 wr_regl(port, ureg->sirfsoc_int_en_reg,
183 rd_regl(port, ureg->sirfsoc_int_en_reg) &
184 ~uint_en->sirfsoc_txfifo_empty_en);
185 else
186 wr_regl(port, SIRFUART_INT_EN_CLR,
187 uint_en->sirfsoc_txfifo_empty_en);
188 }
189 } else {
190 if (!sirfport->is_marco)
191 wr_regl(port, ureg->sirfsoc_int_en_reg,
192 rd_regl(port, ureg->sirfsoc_int_en_reg) &
193 ~uint_en->sirfsoc_txfifo_empty_en);
194 else
195 wr_regl(port, SIRFUART_INT_EN_CLR,
196 uint_en->sirfsoc_txfifo_empty_en);
197 }
145} 198}
146 199
147void sirfsoc_uart_start_tx(struct uart_port *port) 200static void sirfsoc_uart_tx_with_dma(struct sirfsoc_uart_port *sirfport)
201{
202 struct uart_port *port = &sirfport->port;
203 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
204 struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
205 struct circ_buf *xmit = &port->state->xmit;
206 unsigned long tran_size;
207 unsigned long tran_start;
208 unsigned long pio_tx_size;
209
210 tran_size = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
211 tran_start = (unsigned long)(xmit->buf + xmit->tail);
212 if (uart_circ_empty(xmit) || uart_tx_stopped(port) ||
213 !tran_size)
214 return;
215 if (sirfport->tx_dma_state == TX_DMA_PAUSE) {
216 dmaengine_resume(sirfport->tx_dma_chan);
217 return;
218 }
219 if (sirfport->tx_dma_state == TX_DMA_RUNNING)
220 return;
221 if (!sirfport->is_marco)
222 wr_regl(port, ureg->sirfsoc_int_en_reg,
223 rd_regl(port, ureg->sirfsoc_int_en_reg)&
224 ~(uint_en->sirfsoc_txfifo_empty_en));
225 else
226 wr_regl(port, SIRFUART_INT_EN_CLR,
227 uint_en->sirfsoc_txfifo_empty_en);
228 /*
229 * DMA requires buffer address and buffer length are both aligned with
230 * 4 bytes, so we use PIO for
231 * 1. if address is not aligned with 4bytes, use PIO for the first 1~3
232 * bytes, and move to DMA for the left part aligned with 4bytes
233 * 2. if buffer length is not aligned with 4bytes, use DMA for aligned
234 * part first, move to PIO for the left 1~3 bytes
235 */
236 if (tran_size < 4 || BYTES_TO_ALIGN(tran_start)) {
237 wr_regl(port, ureg->sirfsoc_tx_fifo_op, SIRFUART_FIFO_STOP);
238 wr_regl(port, ureg->sirfsoc_tx_dma_io_ctrl,
239 rd_regl(port, ureg->sirfsoc_tx_dma_io_ctrl)|
240 SIRFUART_IO_MODE);
241 if (BYTES_TO_ALIGN(tran_start)) {
242 pio_tx_size = sirfsoc_uart_pio_tx_chars(sirfport,
243 BYTES_TO_ALIGN(tran_start));
244 tran_size -= pio_tx_size;
245 }
246 if (tran_size < 4)
247 sirfsoc_uart_pio_tx_chars(sirfport, tran_size);
248 if (!sirfport->is_marco)
249 wr_regl(port, ureg->sirfsoc_int_en_reg,
250 rd_regl(port, ureg->sirfsoc_int_en_reg)|
251 uint_en->sirfsoc_txfifo_empty_en);
252 else
253 wr_regl(port, ureg->sirfsoc_int_en_reg,
254 uint_en->sirfsoc_txfifo_empty_en);
255 wr_regl(port, ureg->sirfsoc_tx_fifo_op, SIRFUART_FIFO_START);
256 } else {
257 /* tx transfer mode switch into dma mode */
258 wr_regl(port, ureg->sirfsoc_tx_fifo_op, SIRFUART_FIFO_STOP);
259 wr_regl(port, ureg->sirfsoc_tx_dma_io_ctrl,
260 rd_regl(port, ureg->sirfsoc_tx_dma_io_ctrl)&
261 ~SIRFUART_IO_MODE);
262 wr_regl(port, ureg->sirfsoc_tx_fifo_op, SIRFUART_FIFO_START);
263 tran_size &= ~(0x3);
264
265 sirfport->tx_dma_addr = dma_map_single(port->dev,
266 xmit->buf + xmit->tail,
267 tran_size, DMA_TO_DEVICE);
268 sirfport->tx_dma_desc = dmaengine_prep_slave_single(
269 sirfport->tx_dma_chan, sirfport->tx_dma_addr,
270 tran_size, DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT);
271 if (!sirfport->tx_dma_desc) {
272 dev_err(port->dev, "DMA prep slave single fail\n");
273 return;
274 }
275 sirfport->tx_dma_desc->callback =
276 sirfsoc_uart_tx_dma_complete_callback;
277 sirfport->tx_dma_desc->callback_param = (void *)sirfport;
278 sirfport->transfer_size = tran_size;
279
280 dmaengine_submit(sirfport->tx_dma_desc);
281 dma_async_issue_pending(sirfport->tx_dma_chan);
282 sirfport->tx_dma_state = TX_DMA_RUNNING;
283 }
284}
285
286static void sirfsoc_uart_start_tx(struct uart_port *port)
148{ 287{
149 struct sirfsoc_uart_port *sirfport = to_sirfport(port); 288 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
150 unsigned long regv; 289 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
151 sirfsoc_uart_pio_tx_chars(sirfport, 1); 290 struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
152 wr_regl(port, SIRFUART_TX_FIFO_OP, SIRFUART_TX_FIFO_START); 291 if (IS_DMA_CHAN_VALID(sirfport->tx_dma_no))
153 regv = rd_regl(port, SIRFUART_INT_EN); 292 sirfsoc_uart_tx_with_dma(sirfport);
154 wr_regl(port, SIRFUART_INT_EN, regv | SIRFUART_TX_INT_EN); 293 else {
294 sirfsoc_uart_pio_tx_chars(sirfport, 1);
295 wr_regl(port, ureg->sirfsoc_tx_fifo_op, SIRFUART_FIFO_START);
296 if (!sirfport->is_marco)
297 wr_regl(port, ureg->sirfsoc_int_en_reg,
298 rd_regl(port, ureg->sirfsoc_int_en_reg)|
299 uint_en->sirfsoc_txfifo_empty_en);
300 else
301 wr_regl(port, ureg->sirfsoc_int_en_reg,
302 uint_en->sirfsoc_txfifo_empty_en);
303 }
155} 304}
156 305
157static void sirfsoc_uart_stop_rx(struct uart_port *port) 306static void sirfsoc_uart_stop_rx(struct uart_port *port)
158{ 307{
159 unsigned long regv; 308 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
160 wr_regl(port, SIRFUART_RX_FIFO_OP, 0); 309 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
161 regv = rd_regl(port, SIRFUART_INT_EN); 310 struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
162 wr_regl(port, SIRFUART_INT_EN, regv & ~SIRFUART_RX_IO_INT_EN); 311
312 wr_regl(port, ureg->sirfsoc_rx_fifo_op, 0);
313 if (IS_DMA_CHAN_VALID(sirfport->rx_dma_no)) {
314 if (!sirfport->is_marco)
315 wr_regl(port, ureg->sirfsoc_int_en_reg,
316 rd_regl(port, ureg->sirfsoc_int_en_reg) &
317 ~(SIRFUART_RX_DMA_INT_EN(port, uint_en) |
318 uint_en->sirfsoc_rx_done_en));
319 else
320 wr_regl(port, SIRFUART_INT_EN_CLR,
321 SIRFUART_RX_DMA_INT_EN(port, uint_en)|
322 uint_en->sirfsoc_rx_done_en);
323 dmaengine_terminate_all(sirfport->rx_dma_chan);
324 } else {
325 if (!sirfport->is_marco)
326 wr_regl(port, ureg->sirfsoc_int_en_reg,
327 rd_regl(port, ureg->sirfsoc_int_en_reg)&
328 ~(SIRFUART_RX_IO_INT_EN(port, uint_en)));
329 else
330 wr_regl(port, SIRFUART_INT_EN_CLR,
331 SIRFUART_RX_IO_INT_EN(port, uint_en));
332 }
163} 333}
164 334
165static void sirfsoc_uart_disable_ms(struct uart_port *port) 335static void sirfsoc_uart_disable_ms(struct uart_port *port)
166{ 336{
167 struct sirfsoc_uart_port *sirfport = to_sirfport(port); 337 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
168 unsigned long reg; 338 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
169 sirfport->ms_enabled = 0; 339 struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
340
170 if (!sirfport->hw_flow_ctrl) 341 if (!sirfport->hw_flow_ctrl)
171 return; 342 return;
172 reg = rd_regl(port, SIRFUART_AFC_CTRL); 343 sirfport->ms_enabled = false;
173 wr_regl(port, SIRFUART_AFC_CTRL, reg & ~0x3FF); 344 if (sirfport->uart_reg->uart_type == SIRF_REAL_UART) {
174 reg = rd_regl(port, SIRFUART_INT_EN); 345 wr_regl(port, ureg->sirfsoc_afc_ctrl,
175 wr_regl(port, SIRFUART_INT_EN, reg & ~SIRFUART_CTS_INT_EN); 346 rd_regl(port, ureg->sirfsoc_afc_ctrl) & ~0x3FF);
347 if (!sirfport->is_marco)
348 wr_regl(port, ureg->sirfsoc_int_en_reg,
349 rd_regl(port, ureg->sirfsoc_int_en_reg)&
350 ~uint_en->sirfsoc_cts_en);
351 else
352 wr_regl(port, SIRFUART_INT_EN_CLR,
353 uint_en->sirfsoc_cts_en);
354 } else
355 disable_irq(gpio_to_irq(sirfport->cts_gpio));
356}
357
358static irqreturn_t sirfsoc_uart_usp_cts_handler(int irq, void *dev_id)
359{
360 struct sirfsoc_uart_port *sirfport = (struct sirfsoc_uart_port *)dev_id;
361 struct uart_port *port = &sirfport->port;
362 if (gpio_is_valid(sirfport->cts_gpio) && sirfport->ms_enabled)
363 uart_handle_cts_change(port,
364 !gpio_get_value(sirfport->cts_gpio));
365 return IRQ_HANDLED;
176} 366}
177 367
178static void sirfsoc_uart_enable_ms(struct uart_port *port) 368static void sirfsoc_uart_enable_ms(struct uart_port *port)
179{ 369{
180 struct sirfsoc_uart_port *sirfport = to_sirfport(port); 370 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
181 unsigned long reg; 371 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
182 unsigned long flg; 372 struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
373
183 if (!sirfport->hw_flow_ctrl) 374 if (!sirfport->hw_flow_ctrl)
184 return; 375 return;
185 flg = SIRFUART_AFC_RX_EN | SIRFUART_AFC_TX_EN; 376 sirfport->ms_enabled = true;
186 reg = rd_regl(port, SIRFUART_AFC_CTRL); 377 if (sirfport->uart_reg->uart_type == SIRF_REAL_UART) {
187 wr_regl(port, SIRFUART_AFC_CTRL, reg | flg); 378 wr_regl(port, ureg->sirfsoc_afc_ctrl,
188 reg = rd_regl(port, SIRFUART_INT_EN); 379 rd_regl(port, ureg->sirfsoc_afc_ctrl) |
189 wr_regl(port, SIRFUART_INT_EN, reg | SIRFUART_CTS_INT_EN); 380 SIRFUART_AFC_TX_EN | SIRFUART_AFC_RX_EN);
190 uart_handle_cts_change(port, 381 if (!sirfport->is_marco)
191 !(rd_regl(port, SIRFUART_AFC_CTRL) & SIRFUART_CTS_IN_STATUS)); 382 wr_regl(port, ureg->sirfsoc_int_en_reg,
192 sirfport->ms_enabled = 1; 383 rd_regl(port, ureg->sirfsoc_int_en_reg)
384 | uint_en->sirfsoc_cts_en);
385 else
386 wr_regl(port, ureg->sirfsoc_int_en_reg,
387 uint_en->sirfsoc_cts_en);
388 } else
389 enable_irq(gpio_to_irq(sirfport->cts_gpio));
193} 390}
194 391
195static void sirfsoc_uart_break_ctl(struct uart_port *port, int break_state) 392static void sirfsoc_uart_break_ctl(struct uart_port *port, int break_state)
196{ 393{
197 unsigned long ulcon = rd_regl(port, SIRFUART_LINE_CTRL); 394 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
198 if (break_state) 395 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
199 ulcon |= SIRFUART_SET_BREAK; 396 if (sirfport->uart_reg->uart_type == SIRF_REAL_UART) {
200 else 397 unsigned long ulcon = rd_regl(port, ureg->sirfsoc_line_ctrl);
201 ulcon &= ~SIRFUART_SET_BREAK; 398 if (break_state)
202 wr_regl(port, SIRFUART_LINE_CTRL, ulcon); 399 ulcon |= SIRFUART_SET_BREAK;
400 else
401 ulcon &= ~SIRFUART_SET_BREAK;
402 wr_regl(port, ureg->sirfsoc_line_ctrl, ulcon);
403 }
203} 404}
204 405
205static unsigned int 406static unsigned int
206sirfsoc_uart_pio_rx_chars(struct uart_port *port, unsigned int max_rx_count) 407sirfsoc_uart_pio_rx_chars(struct uart_port *port, unsigned int max_rx_count)
207{ 408{
409 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
410 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
411 struct sirfsoc_fifo_status *ufifo_st = &sirfport->uart_reg->fifo_status;
208 unsigned int ch, rx_count = 0; 412 unsigned int ch, rx_count = 0;
209 413 struct tty_struct *tty;
210 while (!(rd_regl(port, SIRFUART_RX_FIFO_STATUS) & 414 tty = tty_port_tty_get(&port->state->port);
211 SIRFUART_FIFOEMPTY_MASK(port))) { 415 if (!tty)
212 ch = rd_regl(port, SIRFUART_RX_FIFO_DATA) | SIRFUART_DUMMY_READ; 416 return -ENODEV;
417 while (!(rd_regl(port, ureg->sirfsoc_rx_fifo_status) &
418 ufifo_st->ff_empty(port->line))) {
419 ch = rd_regl(port, ureg->sirfsoc_rx_fifo_data) |
420 SIRFUART_DUMMY_READ;
213 if (unlikely(uart_handle_sysrq_char(port, ch))) 421 if (unlikely(uart_handle_sysrq_char(port, ch)))
214 continue; 422 continue;
215 uart_insert_char(port, 0, 0, ch, TTY_NORMAL); 423 uart_insert_char(port, 0, 0, ch, TTY_NORMAL);
@@ -218,8 +426,12 @@ sirfsoc_uart_pio_rx_chars(struct uart_port *port, unsigned int max_rx_count)
218 break; 426 break;
219 } 427 }
220 428
429 sirfport->rx_io_count += rx_count;
221 port->icount.rx += rx_count; 430 port->icount.rx += rx_count;
431
432 spin_unlock(&port->lock);
222 tty_flip_buffer_push(&port->state->port); 433 tty_flip_buffer_push(&port->state->port);
434 spin_lock(&port->lock);
223 435
224 return rx_count; 436 return rx_count;
225} 437}
@@ -228,13 +440,16 @@ static unsigned int
228sirfsoc_uart_pio_tx_chars(struct sirfsoc_uart_port *sirfport, int count) 440sirfsoc_uart_pio_tx_chars(struct sirfsoc_uart_port *sirfport, int count)
229{ 441{
230 struct uart_port *port = &sirfport->port; 442 struct uart_port *port = &sirfport->port;
443 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
444 struct sirfsoc_fifo_status *ufifo_st = &sirfport->uart_reg->fifo_status;
231 struct circ_buf *xmit = &port->state->xmit; 445 struct circ_buf *xmit = &port->state->xmit;
232 unsigned int num_tx = 0; 446 unsigned int num_tx = 0;
233 while (!uart_circ_empty(xmit) && 447 while (!uart_circ_empty(xmit) &&
234 !(rd_regl(port, SIRFUART_TX_FIFO_STATUS) & 448 !(rd_regl(port, ureg->sirfsoc_tx_fifo_status) &
235 SIRFUART_FIFOFULL_MASK(port)) && 449 ufifo_st->ff_full(port->line)) &&
236 count--) { 450 count--) {
237 wr_regl(port, SIRFUART_TX_FIFO_DATA, xmit->buf[xmit->tail]); 451 wr_regl(port, ureg->sirfsoc_tx_fifo_data,
452 xmit->buf[xmit->tail]);
238 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); 453 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
239 port->icount.tx++; 454 port->icount.tx++;
240 num_tx++; 455 num_tx++;
@@ -244,6 +459,166 @@ sirfsoc_uart_pio_tx_chars(struct sirfsoc_uart_port *sirfport, int count)
244 return num_tx; 459 return num_tx;
245} 460}
246 461
462static void sirfsoc_uart_tx_dma_complete_callback(void *param)
463{
464 struct sirfsoc_uart_port *sirfport = (struct sirfsoc_uart_port *)param;
465 struct uart_port *port = &sirfport->port;
466 struct circ_buf *xmit = &port->state->xmit;
467 unsigned long flags;
468
469 xmit->tail = (xmit->tail + sirfport->transfer_size) &
470 (UART_XMIT_SIZE - 1);
471 port->icount.tx += sirfport->transfer_size;
472 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
473 uart_write_wakeup(port);
474 if (sirfport->tx_dma_addr)
475 dma_unmap_single(port->dev, sirfport->tx_dma_addr,
476 sirfport->transfer_size, DMA_TO_DEVICE);
477 spin_lock_irqsave(&sirfport->tx_lock, flags);
478 sirfport->tx_dma_state = TX_DMA_IDLE;
479 sirfsoc_uart_tx_with_dma(sirfport);
480 spin_unlock_irqrestore(&sirfport->tx_lock, flags);
481}
482
483static void sirfsoc_uart_insert_rx_buf_to_tty(
484 struct sirfsoc_uart_port *sirfport, int count)
485{
486 struct uart_port *port = &sirfport->port;
487 struct tty_port *tport = &port->state->port;
488 int inserted;
489
490 inserted = tty_insert_flip_string(tport,
491 sirfport->rx_dma_items[sirfport->rx_completed].xmit.buf, count);
492 port->icount.rx += inserted;
493 tty_flip_buffer_push(tport);
494}
495
496static void sirfsoc_rx_submit_one_dma_desc(struct uart_port *port, int index)
497{
498 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
499
500 sirfport->rx_dma_items[index].xmit.tail =
501 sirfport->rx_dma_items[index].xmit.head = 0;
502 sirfport->rx_dma_items[index].desc =
503 dmaengine_prep_slave_single(sirfport->rx_dma_chan,
504 sirfport->rx_dma_items[index].dma_addr, SIRFSOC_RX_DMA_BUF_SIZE,
505 DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT);
506 if (!sirfport->rx_dma_items[index].desc) {
507 dev_err(port->dev, "DMA slave single fail\n");
508 return;
509 }
510 sirfport->rx_dma_items[index].desc->callback =
511 sirfsoc_uart_rx_dma_complete_callback;
512 sirfport->rx_dma_items[index].desc->callback_param = sirfport;
513 sirfport->rx_dma_items[index].cookie =
514 dmaengine_submit(sirfport->rx_dma_items[index].desc);
515 dma_async_issue_pending(sirfport->rx_dma_chan);
516}
517
518static void sirfsoc_rx_tmo_process_tl(unsigned long param)
519{
520 struct sirfsoc_uart_port *sirfport = (struct sirfsoc_uart_port *)param;
521 struct uart_port *port = &sirfport->port;
522 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
523 struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
524 struct sirfsoc_int_status *uint_st = &sirfport->uart_reg->uart_int_st;
525 unsigned int count;
526 unsigned long flags;
527
528 spin_lock_irqsave(&sirfport->rx_lock, flags);
529 while (sirfport->rx_completed != sirfport->rx_issued) {
530 sirfsoc_uart_insert_rx_buf_to_tty(sirfport,
531 SIRFSOC_RX_DMA_BUF_SIZE);
532 sirfsoc_rx_submit_one_dma_desc(port, sirfport->rx_completed++);
533 sirfport->rx_completed %= SIRFSOC_RX_LOOP_BUF_CNT;
534 }
535 count = CIRC_CNT(sirfport->rx_dma_items[sirfport->rx_issued].xmit.head,
536 sirfport->rx_dma_items[sirfport->rx_issued].xmit.tail,
537 SIRFSOC_RX_DMA_BUF_SIZE);
538 if (count > 0)
539 sirfsoc_uart_insert_rx_buf_to_tty(sirfport, count);
540 wr_regl(port, ureg->sirfsoc_rx_dma_io_ctrl,
541 rd_regl(port, ureg->sirfsoc_rx_dma_io_ctrl) |
542 SIRFUART_IO_MODE);
543 sirfsoc_uart_pio_rx_chars(port, 4 - sirfport->rx_io_count);
544 spin_unlock_irqrestore(&sirfport->rx_lock, flags);
545 if (sirfport->rx_io_count == 4) {
546 spin_lock_irqsave(&sirfport->rx_lock, flags);
547 sirfport->rx_io_count = 0;
548 wr_regl(port, ureg->sirfsoc_int_st_reg,
549 uint_st->sirfsoc_rx_done);
550 if (!sirfport->is_marco)
551 wr_regl(port, ureg->sirfsoc_int_en_reg,
552 rd_regl(port, ureg->sirfsoc_int_en_reg) &
553 ~(uint_en->sirfsoc_rx_done_en));
554 else
555 wr_regl(port, SIRFUART_INT_EN_CLR,
556 uint_en->sirfsoc_rx_done_en);
557 spin_unlock_irqrestore(&sirfport->rx_lock, flags);
558
559 sirfsoc_uart_start_next_rx_dma(port);
560 } else {
561 spin_lock_irqsave(&sirfport->rx_lock, flags);
562 wr_regl(port, ureg->sirfsoc_int_st_reg,
563 uint_st->sirfsoc_rx_done);
564 if (!sirfport->is_marco)
565 wr_regl(port, ureg->sirfsoc_int_en_reg,
566 rd_regl(port, ureg->sirfsoc_int_en_reg) |
567 (uint_en->sirfsoc_rx_done_en));
568 else
569 wr_regl(port, ureg->sirfsoc_int_en_reg,
570 uint_en->sirfsoc_rx_done_en);
571 spin_unlock_irqrestore(&sirfport->rx_lock, flags);
572 }
573}
574
575static void sirfsoc_uart_handle_rx_tmo(struct sirfsoc_uart_port *sirfport)
576{
577 struct uart_port *port = &sirfport->port;
578 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
579 struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
580 struct dma_tx_state tx_state;
581 spin_lock(&sirfport->rx_lock);
582
583 dmaengine_tx_status(sirfport->rx_dma_chan,
584 sirfport->rx_dma_items[sirfport->rx_issued].cookie, &tx_state);
585 dmaengine_terminate_all(sirfport->rx_dma_chan);
586 sirfport->rx_dma_items[sirfport->rx_issued].xmit.head =
587 SIRFSOC_RX_DMA_BUF_SIZE - tx_state.residue;
588 if (!sirfport->is_marco)
589 wr_regl(port, ureg->sirfsoc_int_en_reg,
590 rd_regl(port, ureg->sirfsoc_int_en_reg) &
591 ~(uint_en->sirfsoc_rx_timeout_en));
592 else
593 wr_regl(port, SIRFUART_INT_EN_CLR,
594 uint_en->sirfsoc_rx_timeout_en);
595 spin_unlock(&sirfport->rx_lock);
596 tasklet_schedule(&sirfport->rx_tmo_process_tasklet);
597}
598
599static void sirfsoc_uart_handle_rx_done(struct sirfsoc_uart_port *sirfport)
600{
601 struct uart_port *port = &sirfport->port;
602 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
603 struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
604 struct sirfsoc_int_status *uint_st = &sirfport->uart_reg->uart_int_st;
605
606 sirfsoc_uart_pio_rx_chars(port, 4 - sirfport->rx_io_count);
607 if (sirfport->rx_io_count == 4) {
608 sirfport->rx_io_count = 0;
609 if (!sirfport->is_marco)
610 wr_regl(port, ureg->sirfsoc_int_en_reg,
611 rd_regl(port, ureg->sirfsoc_int_en_reg) &
612 ~(uint_en->sirfsoc_rx_done_en));
613 else
614 wr_regl(port, SIRFUART_INT_EN_CLR,
615 uint_en->sirfsoc_rx_done_en);
616 wr_regl(port, ureg->sirfsoc_int_st_reg,
617 uint_st->sirfsoc_rx_timeout);
618 sirfsoc_uart_start_next_rx_dma(port);
619 }
620}
621
247static irqreturn_t sirfsoc_uart_isr(int irq, void *dev_id) 622static irqreturn_t sirfsoc_uart_isr(int irq, void *dev_id)
248{ 623{
249 unsigned long intr_status; 624 unsigned long intr_status;
@@ -251,79 +626,191 @@ static irqreturn_t sirfsoc_uart_isr(int irq, void *dev_id)
251 unsigned long flag = TTY_NORMAL; 626 unsigned long flag = TTY_NORMAL;
252 struct sirfsoc_uart_port *sirfport = (struct sirfsoc_uart_port *)dev_id; 627 struct sirfsoc_uart_port *sirfport = (struct sirfsoc_uart_port *)dev_id;
253 struct uart_port *port = &sirfport->port; 628 struct uart_port *port = &sirfport->port;
629 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
630 struct sirfsoc_fifo_status *ufifo_st = &sirfport->uart_reg->fifo_status;
631 struct sirfsoc_int_status *uint_st = &sirfport->uart_reg->uart_int_st;
632 struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
254 struct uart_state *state = port->state; 633 struct uart_state *state = port->state;
255 struct circ_buf *xmit = &port->state->xmit; 634 struct circ_buf *xmit = &port->state->xmit;
256 spin_lock(&port->lock); 635 spin_lock(&port->lock);
257 intr_status = rd_regl(port, SIRFUART_INT_STATUS); 636 intr_status = rd_regl(port, ureg->sirfsoc_int_st_reg);
258 wr_regl(port, SIRFUART_INT_STATUS, intr_status); 637 wr_regl(port, ureg->sirfsoc_int_st_reg, intr_status);
259 intr_status &= rd_regl(port, SIRFUART_INT_EN); 638 intr_status &= rd_regl(port, ureg->sirfsoc_int_en_reg);
260 if (unlikely(intr_status & (SIRFUART_ERR_INT_STAT))) { 639 if (unlikely(intr_status & (SIRFUART_ERR_INT_STAT(port, uint_st)))) {
261 if (intr_status & SIRFUART_RXD_BREAK) { 640 if (intr_status & uint_st->sirfsoc_rxd_brk) {
641 port->icount.brk++;
262 if (uart_handle_break(port)) 642 if (uart_handle_break(port))
263 goto recv_char; 643 goto recv_char;
264 uart_insert_char(port, intr_status,
265 SIRFUART_RX_OFLOW, 0, TTY_BREAK);
266 spin_unlock(&port->lock);
267 return IRQ_HANDLED;
268 } 644 }
269 if (intr_status & SIRFUART_RX_OFLOW) 645 if (intr_status & uint_st->sirfsoc_rx_oflow)
270 port->icount.overrun++; 646 port->icount.overrun++;
271 if (intr_status & SIRFUART_FRM_ERR) { 647 if (intr_status & uint_st->sirfsoc_frm_err) {
272 port->icount.frame++; 648 port->icount.frame++;
273 flag = TTY_FRAME; 649 flag = TTY_FRAME;
274 } 650 }
275 if (intr_status & SIRFUART_PARITY_ERR) 651 if (intr_status & uint_st->sirfsoc_parity_err)
276 flag = TTY_PARITY; 652 flag = TTY_PARITY;
277 wr_regl(port, SIRFUART_RX_FIFO_OP, SIRFUART_RX_FIFO_RESET); 653 wr_regl(port, ureg->sirfsoc_rx_fifo_op, SIRFUART_FIFO_RESET);
278 wr_regl(port, SIRFUART_RX_FIFO_OP, 0); 654 wr_regl(port, ureg->sirfsoc_rx_fifo_op, 0);
279 wr_regl(port, SIRFUART_RX_FIFO_OP, SIRFUART_RX_FIFO_START); 655 wr_regl(port, ureg->sirfsoc_rx_fifo_op, SIRFUART_FIFO_START);
280 intr_status &= port->read_status_mask; 656 intr_status &= port->read_status_mask;
281 uart_insert_char(port, intr_status, 657 uart_insert_char(port, intr_status,
282 SIRFUART_RX_OFLOW_INT, 0, flag); 658 uint_en->sirfsoc_rx_oflow_en, 0, flag);
659 tty_flip_buffer_push(&state->port);
283 } 660 }
284recv_char: 661recv_char:
285 if (intr_status & SIRFUART_CTS_INT_EN) { 662 if ((sirfport->uart_reg->uart_type == SIRF_REAL_UART) &&
286 cts_status = !(rd_regl(port, SIRFUART_AFC_CTRL) & 663 (intr_status & SIRFUART_CTS_INT_ST(uint_st)) &&
287 SIRFUART_CTS_IN_STATUS); 664 !sirfport->tx_dma_state) {
288 if (cts_status != 0) { 665 cts_status = rd_regl(port, ureg->sirfsoc_afc_ctrl) &
289 uart_handle_cts_change(port, 1); 666 SIRFUART_AFC_CTS_STATUS;
290 } else { 667 if (cts_status != 0)
291 uart_handle_cts_change(port, 0); 668 cts_status = 0;
292 wake_up_interruptible(&state->port.delta_msr_wait); 669 else
293 } 670 cts_status = 1;
671 uart_handle_cts_change(port, cts_status);
672 wake_up_interruptible(&state->port.delta_msr_wait);
294 } 673 }
295 if (intr_status & SIRFUART_RX_IO_INT_EN) 674 if (IS_DMA_CHAN_VALID(sirfport->rx_dma_no)) {
296 sirfsoc_uart_pio_rx_chars(port, SIRFSOC_UART_IO_RX_MAX_CNT); 675 if (intr_status & uint_st->sirfsoc_rx_timeout)
297 if (intr_status & SIRFUART_TX_INT_EN) { 676 sirfsoc_uart_handle_rx_tmo(sirfport);
298 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) { 677 if (intr_status & uint_st->sirfsoc_rx_done)
299 spin_unlock(&port->lock); 678 sirfsoc_uart_handle_rx_done(sirfport);
300 return IRQ_HANDLED; 679 } else {
301 } else { 680 if (intr_status & SIRFUART_RX_IO_INT_ST(uint_st))
302 sirfsoc_uart_pio_tx_chars(sirfport, 681 sirfsoc_uart_pio_rx_chars(port,
682 SIRFSOC_UART_IO_RX_MAX_CNT);
683 }
684 if (intr_status & uint_st->sirfsoc_txfifo_empty) {
685 if (IS_DMA_CHAN_VALID(sirfport->tx_dma_no))
686 sirfsoc_uart_tx_with_dma(sirfport);
687 else {
688 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
689 spin_unlock(&port->lock);
690 return IRQ_HANDLED;
691 } else {
692 sirfsoc_uart_pio_tx_chars(sirfport,
303 SIRFSOC_UART_IO_TX_REASONABLE_CNT); 693 SIRFSOC_UART_IO_TX_REASONABLE_CNT);
304 if ((uart_circ_empty(xmit)) && 694 if ((uart_circ_empty(xmit)) &&
305 (rd_regl(port, SIRFUART_TX_FIFO_STATUS) & 695 (rd_regl(port, ureg->sirfsoc_tx_fifo_status) &
306 SIRFUART_FIFOEMPTY_MASK(port))) 696 ufifo_st->ff_empty(port->line)))
307 sirfsoc_uart_stop_tx(port); 697 sirfsoc_uart_stop_tx(port);
698 }
308 } 699 }
309 } 700 }
310 spin_unlock(&port->lock); 701 spin_unlock(&port->lock);
311 return IRQ_HANDLED; 702 return IRQ_HANDLED;
312} 703}
313 704
705static void sirfsoc_uart_rx_dma_complete_tl(unsigned long param)
706{
707 struct sirfsoc_uart_port *sirfport = (struct sirfsoc_uart_port *)param;
708 struct uart_port *port = &sirfport->port;
709 unsigned long flags;
710 spin_lock_irqsave(&sirfport->rx_lock, flags);
711 while (sirfport->rx_completed != sirfport->rx_issued) {
712 sirfsoc_uart_insert_rx_buf_to_tty(sirfport,
713 SIRFSOC_RX_DMA_BUF_SIZE);
714 sirfsoc_rx_submit_one_dma_desc(port, sirfport->rx_completed++);
715 sirfport->rx_completed %= SIRFSOC_RX_LOOP_BUF_CNT;
716 }
717 spin_unlock_irqrestore(&sirfport->rx_lock, flags);
718}
719
720static void sirfsoc_uart_rx_dma_complete_callback(void *param)
721{
722 struct sirfsoc_uart_port *sirfport = (struct sirfsoc_uart_port *)param;
723 spin_lock(&sirfport->rx_lock);
724 sirfport->rx_issued++;
725 sirfport->rx_issued %= SIRFSOC_RX_LOOP_BUF_CNT;
726 spin_unlock(&sirfport->rx_lock);
727 tasklet_schedule(&sirfport->rx_dma_complete_tasklet);
728}
729
730/* submit rx dma task into dmaengine */
731static void sirfsoc_uart_start_next_rx_dma(struct uart_port *port)
732{
733 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
734 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
735 struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
736 unsigned long flags;
737 int i;
738 spin_lock_irqsave(&sirfport->rx_lock, flags);
739 sirfport->rx_io_count = 0;
740 wr_regl(port, ureg->sirfsoc_rx_dma_io_ctrl,
741 rd_regl(port, ureg->sirfsoc_rx_dma_io_ctrl) &
742 ~SIRFUART_IO_MODE);
743 spin_unlock_irqrestore(&sirfport->rx_lock, flags);
744 for (i = 0; i < SIRFSOC_RX_LOOP_BUF_CNT; i++)
745 sirfsoc_rx_submit_one_dma_desc(port, i);
746 sirfport->rx_completed = sirfport->rx_issued = 0;
747 spin_lock_irqsave(&sirfport->rx_lock, flags);
748 if (!sirfport->is_marco)
749 wr_regl(port, ureg->sirfsoc_int_en_reg,
750 rd_regl(port, ureg->sirfsoc_int_en_reg) |
751 SIRFUART_RX_DMA_INT_EN(port, uint_en));
752 else
753 wr_regl(port, ureg->sirfsoc_int_en_reg,
754 SIRFUART_RX_DMA_INT_EN(port, uint_en));
755 spin_unlock_irqrestore(&sirfport->rx_lock, flags);
756}
757
314static void sirfsoc_uart_start_rx(struct uart_port *port) 758static void sirfsoc_uart_start_rx(struct uart_port *port)
315{ 759{
316 unsigned long regv; 760 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
317 regv = rd_regl(port, SIRFUART_INT_EN); 761 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
318 wr_regl(port, SIRFUART_INT_EN, regv | SIRFUART_RX_IO_INT_EN); 762 struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
319 wr_regl(port, SIRFUART_RX_FIFO_OP, SIRFUART_RX_FIFO_RESET); 763
320 wr_regl(port, SIRFUART_RX_FIFO_OP, 0); 764 sirfport->rx_io_count = 0;
321 wr_regl(port, SIRFUART_RX_FIFO_OP, SIRFUART_RX_FIFO_START); 765 wr_regl(port, ureg->sirfsoc_rx_fifo_op, SIRFUART_FIFO_RESET);
766 wr_regl(port, ureg->sirfsoc_rx_fifo_op, 0);
767 wr_regl(port, ureg->sirfsoc_rx_fifo_op, SIRFUART_FIFO_START);
768 if (IS_DMA_CHAN_VALID(sirfport->rx_dma_no))
769 sirfsoc_uart_start_next_rx_dma(port);
770 else {
771 if (!sirfport->is_marco)
772 wr_regl(port, ureg->sirfsoc_int_en_reg,
773 rd_regl(port, ureg->sirfsoc_int_en_reg) |
774 SIRFUART_RX_IO_INT_EN(port, uint_en));
775 else
776 wr_regl(port, ureg->sirfsoc_int_en_reg,
777 SIRFUART_RX_IO_INT_EN(port, uint_en));
778 }
779}
780
781static unsigned int
782sirfsoc_usp_calc_sample_div(unsigned long set_rate,
783 unsigned long ioclk_rate, unsigned long *sample_reg)
784{
785 unsigned long min_delta = ~0UL;
786 unsigned short sample_div;
787 unsigned long ioclk_div = 0;
788 unsigned long temp_delta;
789
790 for (sample_div = SIRF_MIN_SAMPLE_DIV;
791 sample_div <= SIRF_MAX_SAMPLE_DIV; sample_div++) {
792 temp_delta = ioclk_rate -
793 (ioclk_rate + (set_rate * sample_div) / 2)
794 / (set_rate * sample_div) * set_rate * sample_div;
795
796 temp_delta = (temp_delta > 0) ? temp_delta : -temp_delta;
797 if (temp_delta < min_delta) {
798 ioclk_div = (2 * ioclk_rate /
799 (set_rate * sample_div) + 1) / 2 - 1;
800 if (ioclk_div > SIRF_IOCLK_DIV_MAX)
801 continue;
802 min_delta = temp_delta;
803 *sample_reg = sample_div;
804 if (!temp_delta)
805 break;
806 }
807 }
808 return ioclk_div;
322} 809}
323 810
324static unsigned int 811static unsigned int
325sirfsoc_calc_sample_div(unsigned long baud_rate, 812sirfsoc_uart_calc_sample_div(unsigned long baud_rate,
326 unsigned long ioclk_rate, unsigned long *setted_baud) 813 unsigned long ioclk_rate, unsigned long *set_baud)
327{ 814{
328 unsigned long min_delta = ~0UL; 815 unsigned long min_delta = ~0UL;
329 unsigned short sample_div; 816 unsigned short sample_div;
@@ -346,7 +833,7 @@ sirfsoc_calc_sample_div(unsigned long baud_rate,
346 regv = regv & (~SIRF_SAMPLE_DIV_MASK); 833 regv = regv & (~SIRF_SAMPLE_DIV_MASK);
347 regv = regv | (sample_div << SIRF_SAMPLE_DIV_SHIFT); 834 regv = regv | (sample_div << SIRF_SAMPLE_DIV_SHIFT);
348 min_delta = temp_delta; 835 min_delta = temp_delta;
349 *setted_baud = baud_tmp; 836 *set_baud = baud_tmp;
350 } 837 }
351 } 838 }
352 return regv; 839 return regv;
@@ -357,63 +844,93 @@ static void sirfsoc_uart_set_termios(struct uart_port *port,
357 struct ktermios *old) 844 struct ktermios *old)
358{ 845{
359 struct sirfsoc_uart_port *sirfport = to_sirfport(port); 846 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
847 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
848 struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
360 unsigned long config_reg = 0; 849 unsigned long config_reg = 0;
361 unsigned long baud_rate; 850 unsigned long baud_rate;
362 unsigned long setted_baud; 851 unsigned long set_baud;
363 unsigned long flags; 852 unsigned long flags;
364 unsigned long ic; 853 unsigned long ic;
365 unsigned int clk_div_reg = 0; 854 unsigned int clk_div_reg = 0;
366 unsigned long temp_reg_val; 855 unsigned long txfifo_op_reg, ioclk_rate;
367 unsigned long rx_time_out; 856 unsigned long rx_time_out;
368 int threshold_div; 857 int threshold_div;
369 int temp; 858 u32 data_bit_len, stop_bit_len, len_val;
859 unsigned long sample_div_reg = 0xf;
860 ioclk_rate = port->uartclk;
370 861
371 switch (termios->c_cflag & CSIZE) { 862 switch (termios->c_cflag & CSIZE) {
372 default: 863 default:
373 case CS8: 864 case CS8:
865 data_bit_len = 8;
374 config_reg |= SIRFUART_DATA_BIT_LEN_8; 866 config_reg |= SIRFUART_DATA_BIT_LEN_8;
375 break; 867 break;
376 case CS7: 868 case CS7:
869 data_bit_len = 7;
377 config_reg |= SIRFUART_DATA_BIT_LEN_7; 870 config_reg |= SIRFUART_DATA_BIT_LEN_7;
378 break; 871 break;
379 case CS6: 872 case CS6:
873 data_bit_len = 6;
380 config_reg |= SIRFUART_DATA_BIT_LEN_6; 874 config_reg |= SIRFUART_DATA_BIT_LEN_6;
381 break; 875 break;
382 case CS5: 876 case CS5:
877 data_bit_len = 5;
383 config_reg |= SIRFUART_DATA_BIT_LEN_5; 878 config_reg |= SIRFUART_DATA_BIT_LEN_5;
384 break; 879 break;
385 } 880 }
386 if (termios->c_cflag & CSTOPB) 881 if (termios->c_cflag & CSTOPB) {
387 config_reg |= SIRFUART_STOP_BIT_LEN_2; 882 config_reg |= SIRFUART_STOP_BIT_LEN_2;
388 baud_rate = uart_get_baud_rate(port, termios, old, 0, 4000000); 883 stop_bit_len = 2;
884 } else
885 stop_bit_len = 1;
886
389 spin_lock_irqsave(&port->lock, flags); 887 spin_lock_irqsave(&port->lock, flags);
390 port->read_status_mask = SIRFUART_RX_OFLOW_INT; 888 port->read_status_mask = uint_en->sirfsoc_rx_oflow_en;
391 port->ignore_status_mask = 0; 889 port->ignore_status_mask = 0;
392 /* read flags */ 890 if (sirfport->uart_reg->uart_type == SIRF_REAL_UART) {
393 if (termios->c_iflag & INPCK) 891 if (termios->c_iflag & INPCK)
394 port->read_status_mask |= 892 port->read_status_mask |= uint_en->sirfsoc_frm_err_en |
395 SIRFUART_FRM_ERR_INT | SIRFUART_PARITY_ERR_INT; 893 uint_en->sirfsoc_parity_err_en;
894 } else {
895 if (termios->c_iflag & INPCK)
896 port->read_status_mask |= uint_en->sirfsoc_frm_err_en;
897 }
396 if (termios->c_iflag & (BRKINT | PARMRK)) 898 if (termios->c_iflag & (BRKINT | PARMRK))
397 port->read_status_mask |= SIRFUART_RXD_BREAK_INT; 899 port->read_status_mask |= uint_en->sirfsoc_rxd_brk_en;
398 /* ignore flags */ 900 if (sirfport->uart_reg->uart_type == SIRF_REAL_UART) {
399 if (termios->c_iflag & IGNPAR) 901 if (termios->c_iflag & IGNPAR)
902 port->ignore_status_mask |=
903 uint_en->sirfsoc_frm_err_en |
904 uint_en->sirfsoc_parity_err_en;
905 if (termios->c_cflag & PARENB) {
906 if (termios->c_cflag & CMSPAR) {
907 if (termios->c_cflag & PARODD)
908 config_reg |= SIRFUART_STICK_BIT_MARK;
909 else
910 config_reg |= SIRFUART_STICK_BIT_SPACE;
911 } else if (termios->c_cflag & PARODD) {
912 config_reg |= SIRFUART_STICK_BIT_ODD;
913 } else {
914 config_reg |= SIRFUART_STICK_BIT_EVEN;
915 }
916 }
917 } else {
918 if (termios->c_iflag & IGNPAR)
919 port->ignore_status_mask |=
920 uint_en->sirfsoc_frm_err_en;
921 if (termios->c_cflag & PARENB)
922 dev_warn(port->dev,
923 "USP-UART not support parity err\n");
924 }
925 if (termios->c_iflag & IGNBRK) {
400 port->ignore_status_mask |= 926 port->ignore_status_mask |=
401 SIRFUART_FRM_ERR_INT | SIRFUART_PARITY_ERR_INT; 927 uint_en->sirfsoc_rxd_brk_en;
928 if (termios->c_iflag & IGNPAR)
929 port->ignore_status_mask |=
930 uint_en->sirfsoc_rx_oflow_en;
931 }
402 if ((termios->c_cflag & CREAD) == 0) 932 if ((termios->c_cflag & CREAD) == 0)
403 port->ignore_status_mask |= SIRFUART_DUMMY_READ; 933 port->ignore_status_mask |= SIRFUART_DUMMY_READ;
404 /* enable parity if PARENB is set*/
405 if (termios->c_cflag & PARENB) {
406 if (termios->c_cflag & CMSPAR) {
407 if (termios->c_cflag & PARODD)
408 config_reg |= SIRFUART_STICK_BIT_MARK;
409 else
410 config_reg |= SIRFUART_STICK_BIT_SPACE;
411 } else if (termios->c_cflag & PARODD) {
412 config_reg |= SIRFUART_STICK_BIT_ODD;
413 } else {
414 config_reg |= SIRFUART_STICK_BIT_EVEN;
415 }
416 }
417 /* Hardware Flow Control Settings */ 934 /* Hardware Flow Control Settings */
418 if (UART_ENABLE_MS(port, termios->c_cflag)) { 935 if (UART_ENABLE_MS(port, termios->c_cflag)) {
419 if (!sirfport->ms_enabled) 936 if (!sirfport->ms_enabled)
@@ -422,75 +939,184 @@ static void sirfsoc_uart_set_termios(struct uart_port *port,
422 if (sirfport->ms_enabled) 939 if (sirfport->ms_enabled)
423 sirfsoc_uart_disable_ms(port); 940 sirfsoc_uart_disable_ms(port);
424 } 941 }
425 942 baud_rate = uart_get_baud_rate(port, termios, old, 0, 4000000);
426 if (port->uartclk == 150000000) { 943 if (ioclk_rate == 150000000) {
427 /* common rate: fast calculation */
428 for (ic = 0; ic < SIRF_BAUD_RATE_SUPPORT_NR; ic++) 944 for (ic = 0; ic < SIRF_BAUD_RATE_SUPPORT_NR; ic++)
429 if (baud_rate == baudrate_to_regv[ic].baud_rate) 945 if (baud_rate == baudrate_to_regv[ic].baud_rate)
430 clk_div_reg = baudrate_to_regv[ic].reg_val; 946 clk_div_reg = baudrate_to_regv[ic].reg_val;
431 } 947 }
432 948 set_baud = baud_rate;
433 setted_baud = baud_rate; 949 if (sirfport->uart_reg->uart_type == SIRF_REAL_UART) {
434 /* arbitary rate setting */ 950 if (unlikely(clk_div_reg == 0))
435 if (unlikely(clk_div_reg == 0)) 951 clk_div_reg = sirfsoc_uart_calc_sample_div(baud_rate,
436 clk_div_reg = sirfsoc_calc_sample_div(baud_rate, port->uartclk, 952 ioclk_rate, &set_baud);
437 &setted_baud); 953 wr_regl(port, ureg->sirfsoc_divisor, clk_div_reg);
438 wr_regl(port, SIRFUART_DIVISOR, clk_div_reg); 954 } else {
439 955 clk_div_reg = sirfsoc_usp_calc_sample_div(baud_rate,
956 ioclk_rate, &sample_div_reg);
957 sample_div_reg--;
958 set_baud = ((ioclk_rate / (clk_div_reg+1) - 1) /
959 (sample_div_reg + 1));
960 /* setting usp mode 2 */
961 len_val = ((1 << SIRFSOC_USP_MODE2_RXD_DELAY_OFFSET) |
962 (1 << SIRFSOC_USP_MODE2_TXD_DELAY_OFFSET));
963 len_val |= ((clk_div_reg & SIRFSOC_USP_MODE2_CLK_DIVISOR_MASK)
964 << SIRFSOC_USP_MODE2_CLK_DIVISOR_OFFSET);
965 wr_regl(port, ureg->sirfsoc_mode2, len_val);
966 }
440 if (tty_termios_baud_rate(termios)) 967 if (tty_termios_baud_rate(termios))
441 tty_termios_encode_baud_rate(termios, setted_baud, setted_baud); 968 tty_termios_encode_baud_rate(termios, set_baud, set_baud);
442 969 /* set receive timeout && data bits len */
443 /* set receive timeout */ 970 rx_time_out = SIRFSOC_UART_RX_TIMEOUT(set_baud, 20000);
444 rx_time_out = SIRFSOC_UART_RX_TIMEOUT(baud_rate, 20000); 971 rx_time_out = SIRFUART_RECV_TIMEOUT_VALUE(rx_time_out);
445 rx_time_out = (rx_time_out > 0xFFFF) ? 0xFFFF : rx_time_out; 972 txfifo_op_reg = rd_regl(port, ureg->sirfsoc_tx_fifo_op);
446 config_reg |= SIRFUART_RECV_TIMEOUT(rx_time_out); 973 wr_regl(port, ureg->sirfsoc_rx_fifo_op, SIRFUART_FIFO_STOP);
447 temp_reg_val = rd_regl(port, SIRFUART_TX_FIFO_OP); 974 wr_regl(port, ureg->sirfsoc_tx_fifo_op,
448 wr_regl(port, SIRFUART_RX_FIFO_OP, 0); 975 (txfifo_op_reg & ~SIRFUART_FIFO_START));
449 wr_regl(port, SIRFUART_TX_FIFO_OP, 976 if (sirfport->uart_reg->uart_type == SIRF_REAL_UART) {
450 temp_reg_val & ~SIRFUART_TX_FIFO_START); 977 config_reg |= SIRFUART_RECV_TIMEOUT(port, rx_time_out);
451 wr_regl(port, SIRFUART_TX_DMA_IO_CTRL, SIRFUART_TX_MODE_IO); 978 wr_regl(port, ureg->sirfsoc_line_ctrl, config_reg);
452 wr_regl(port, SIRFUART_RX_DMA_IO_CTRL, SIRFUART_RX_MODE_IO); 979 } else {
453 wr_regl(port, SIRFUART_LINE_CTRL, config_reg); 980 /*tx frame ctrl*/
454 981 len_val = (data_bit_len - 1) << SIRFSOC_USP_TX_DATA_LEN_OFFSET;
982 len_val |= (data_bit_len + 1 + stop_bit_len - 1) <<
983 SIRFSOC_USP_TX_FRAME_LEN_OFFSET;
984 len_val |= ((data_bit_len - 1) <<
985 SIRFSOC_USP_TX_SHIFTER_LEN_OFFSET);
986 len_val |= (((clk_div_reg & 0xc00) >> 10) <<
987 SIRFSOC_USP_TX_CLK_DIVISOR_OFFSET);
988 wr_regl(port, ureg->sirfsoc_tx_frame_ctrl, len_val);
989 /*rx frame ctrl*/
990 len_val = (data_bit_len - 1) << SIRFSOC_USP_RX_DATA_LEN_OFFSET;
991 len_val |= (data_bit_len + 1 + stop_bit_len - 1) <<
992 SIRFSOC_USP_RX_FRAME_LEN_OFFSET;
993 len_val |= (data_bit_len - 1) <<
994 SIRFSOC_USP_RX_SHIFTER_LEN_OFFSET;
995 len_val |= (((clk_div_reg & 0xf000) >> 12) <<
996 SIRFSOC_USP_RX_CLK_DIVISOR_OFFSET);
997 wr_regl(port, ureg->sirfsoc_rx_frame_ctrl, len_val);
998 /*async param*/
999 wr_regl(port, ureg->sirfsoc_async_param_reg,
1000 (SIRFUART_RECV_TIMEOUT(port, rx_time_out)) |
1001 (sample_div_reg & SIRFSOC_USP_ASYNC_DIV2_MASK) <<
1002 SIRFSOC_USP_ASYNC_DIV2_OFFSET);
1003 }
1004 if (IS_DMA_CHAN_VALID(sirfport->tx_dma_no))
1005 wr_regl(port, ureg->sirfsoc_tx_dma_io_ctrl, SIRFUART_DMA_MODE);
1006 else
1007 wr_regl(port, ureg->sirfsoc_tx_dma_io_ctrl, SIRFUART_IO_MODE);
1008 if (IS_DMA_CHAN_VALID(sirfport->rx_dma_no))
1009 wr_regl(port, ureg->sirfsoc_rx_dma_io_ctrl, SIRFUART_DMA_MODE);
1010 else
1011 wr_regl(port, ureg->sirfsoc_rx_dma_io_ctrl, SIRFUART_IO_MODE);
455 /* Reset Rx/Tx FIFO Threshold level for proper baudrate */ 1012 /* Reset Rx/Tx FIFO Threshold level for proper baudrate */
456 if (baud_rate < 1000000) 1013 if (set_baud < 1000000)
457 threshold_div = 1; 1014 threshold_div = 1;
458 else 1015 else
459 threshold_div = 2; 1016 threshold_div = 2;
460 temp = port->line == 1 ? 16 : 64; 1017 wr_regl(port, ureg->sirfsoc_tx_fifo_ctrl,
461 wr_regl(port, SIRFUART_TX_FIFO_CTRL, temp / threshold_div); 1018 SIRFUART_FIFO_THD(port) / threshold_div);
462 wr_regl(port, SIRFUART_RX_FIFO_CTRL, temp / threshold_div); 1019 wr_regl(port, ureg->sirfsoc_rx_fifo_ctrl,
463 temp_reg_val |= SIRFUART_TX_FIFO_START; 1020 SIRFUART_FIFO_THD(port) / threshold_div);
464 wr_regl(port, SIRFUART_TX_FIFO_OP, temp_reg_val); 1021 txfifo_op_reg |= SIRFUART_FIFO_START;
465 uart_update_timeout(port, termios->c_cflag, baud_rate); 1022 wr_regl(port, ureg->sirfsoc_tx_fifo_op, txfifo_op_reg);
1023 uart_update_timeout(port, termios->c_cflag, set_baud);
466 sirfsoc_uart_start_rx(port); 1024 sirfsoc_uart_start_rx(port);
467 wr_regl(port, SIRFUART_TX_RX_EN, SIRFUART_TX_EN | SIRFUART_RX_EN); 1025 wr_regl(port, ureg->sirfsoc_tx_rx_en, SIRFUART_TX_EN | SIRFUART_RX_EN);
468 spin_unlock_irqrestore(&port->lock, flags); 1026 spin_unlock_irqrestore(&port->lock, flags);
469} 1027}
470 1028
471static void startup_uart_controller(struct uart_port *port) 1029static unsigned int sirfsoc_uart_init_tx_dma(struct uart_port *port)
472{ 1030{
473 unsigned long temp_regv; 1031 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
474 int temp; 1032 dma_cap_mask_t dma_mask;
475 temp_regv = rd_regl(port, SIRFUART_TX_DMA_IO_CTRL); 1033 struct dma_slave_config tx_slv_cfg = {
476 wr_regl(port, SIRFUART_TX_DMA_IO_CTRL, temp_regv | SIRFUART_TX_MODE_IO); 1034 .dst_maxburst = 2,
477 temp_regv = rd_regl(port, SIRFUART_RX_DMA_IO_CTRL); 1035 };
478 wr_regl(port, SIRFUART_RX_DMA_IO_CTRL, temp_regv | SIRFUART_RX_MODE_IO); 1036
479 wr_regl(port, SIRFUART_TX_DMA_IO_LEN, 0); 1037 dma_cap_zero(dma_mask);
480 wr_regl(port, SIRFUART_RX_DMA_IO_LEN, 0); 1038 dma_cap_set(DMA_SLAVE, dma_mask);
481 wr_regl(port, SIRFUART_TX_RX_EN, SIRFUART_RX_EN | SIRFUART_TX_EN); 1039 sirfport->tx_dma_chan = dma_request_channel(dma_mask,
482 wr_regl(port, SIRFUART_TX_FIFO_OP, SIRFUART_TX_FIFO_RESET); 1040 (dma_filter_fn)sirfsoc_dma_filter_id,
483 wr_regl(port, SIRFUART_TX_FIFO_OP, 0); 1041 (void *)sirfport->tx_dma_no);
484 wr_regl(port, SIRFUART_RX_FIFO_OP, SIRFUART_RX_FIFO_RESET); 1042 if (!sirfport->tx_dma_chan) {
485 wr_regl(port, SIRFUART_RX_FIFO_OP, 0); 1043 dev_err(port->dev, "Uart Request Dma Channel Fail %d\n",
486 temp = port->line == 1 ? 16 : 64; 1044 sirfport->tx_dma_no);
487 wr_regl(port, SIRFUART_TX_FIFO_CTRL, temp); 1045 return -EPROBE_DEFER;
488 wr_regl(port, SIRFUART_RX_FIFO_CTRL, temp); 1046 }
1047 dmaengine_slave_config(sirfport->tx_dma_chan, &tx_slv_cfg);
1048
1049 return 0;
1050}
1051
1052static unsigned int sirfsoc_uart_init_rx_dma(struct uart_port *port)
1053{
1054 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
1055 dma_cap_mask_t dma_mask;
1056 int ret;
1057 int i, j;
1058 struct dma_slave_config slv_cfg = {
1059 .src_maxburst = 2,
1060 };
1061
1062 dma_cap_zero(dma_mask);
1063 dma_cap_set(DMA_SLAVE, dma_mask);
1064 sirfport->rx_dma_chan = dma_request_channel(dma_mask,
1065 (dma_filter_fn)sirfsoc_dma_filter_id,
1066 (void *)sirfport->rx_dma_no);
1067 if (!sirfport->rx_dma_chan) {
1068 dev_err(port->dev, "Uart Request Dma Channel Fail %d\n",
1069 sirfport->rx_dma_no);
1070 ret = -EPROBE_DEFER;
1071 goto request_err;
1072 }
1073 for (i = 0; i < SIRFSOC_RX_LOOP_BUF_CNT; i++) {
1074 sirfport->rx_dma_items[i].xmit.buf =
1075 dma_alloc_coherent(port->dev, SIRFSOC_RX_DMA_BUF_SIZE,
1076 &sirfport->rx_dma_items[i].dma_addr, GFP_KERNEL);
1077 if (!sirfport->rx_dma_items[i].xmit.buf) {
1078 dev_err(port->dev, "Uart alloc bufa failed\n");
1079 ret = -ENOMEM;
1080 goto alloc_coherent_err;
1081 }
1082 sirfport->rx_dma_items[i].xmit.head =
1083 sirfport->rx_dma_items[i].xmit.tail = 0;
1084 }
1085 dmaengine_slave_config(sirfport->rx_dma_chan, &slv_cfg);
1086
1087 return 0;
1088alloc_coherent_err:
1089 for (j = 0; j < i; j++)
1090 dma_free_coherent(port->dev, SIRFSOC_RX_DMA_BUF_SIZE,
1091 sirfport->rx_dma_items[j].xmit.buf,
1092 sirfport->rx_dma_items[j].dma_addr);
1093 dma_release_channel(sirfport->rx_dma_chan);
1094request_err:
1095 return ret;
1096}
1097
1098static void sirfsoc_uart_uninit_tx_dma(struct sirfsoc_uart_port *sirfport)
1099{
1100 dmaengine_terminate_all(sirfport->tx_dma_chan);
1101 dma_release_channel(sirfport->tx_dma_chan);
1102}
1103
1104static void sirfsoc_uart_uninit_rx_dma(struct sirfsoc_uart_port *sirfport)
1105{
1106 int i;
1107 struct uart_port *port = &sirfport->port;
1108 dmaengine_terminate_all(sirfport->rx_dma_chan);
1109 dma_release_channel(sirfport->rx_dma_chan);
1110 for (i = 0; i < SIRFSOC_RX_LOOP_BUF_CNT; i++)
1111 dma_free_coherent(port->dev, SIRFSOC_RX_DMA_BUF_SIZE,
1112 sirfport->rx_dma_items[i].xmit.buf,
1113 sirfport->rx_dma_items[i].dma_addr);
489} 1114}
490 1115
491static int sirfsoc_uart_startup(struct uart_port *port) 1116static int sirfsoc_uart_startup(struct uart_port *port)
492{ 1117{
493 struct sirfsoc_uart_port *sirfport = to_sirfport(port); 1118 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
1119 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
494 unsigned int index = port->line; 1120 unsigned int index = port->line;
495 int ret; 1121 int ret;
496 set_irq_flags(port->irq, IRQF_VALID | IRQF_NOAUTOEN); 1122 set_irq_flags(port->irq, IRQF_VALID | IRQF_NOAUTOEN);
@@ -504,8 +1130,64 @@ static int sirfsoc_uart_startup(struct uart_port *port)
504 index, port->irq); 1130 index, port->irq);
505 goto irq_err; 1131 goto irq_err;
506 } 1132 }
507 startup_uart_controller(port); 1133
1134 /* initial hardware settings */
1135 wr_regl(port, ureg->sirfsoc_tx_dma_io_ctrl,
1136 rd_regl(port, ureg->sirfsoc_tx_dma_io_ctrl) |
1137 SIRFUART_IO_MODE);
1138 wr_regl(port, ureg->sirfsoc_rx_dma_io_ctrl,
1139 rd_regl(port, ureg->sirfsoc_rx_dma_io_ctrl) |
1140 SIRFUART_IO_MODE);
1141 wr_regl(port, ureg->sirfsoc_tx_dma_io_len, 0);
1142 wr_regl(port, ureg->sirfsoc_rx_dma_io_len, 0);
1143 wr_regl(port, ureg->sirfsoc_tx_rx_en, SIRFUART_RX_EN | SIRFUART_TX_EN);
1144 if (sirfport->uart_reg->uart_type == SIRF_USP_UART)
1145 wr_regl(port, ureg->sirfsoc_mode1,
1146 SIRFSOC_USP_ENDIAN_CTRL_LSBF |
1147 SIRFSOC_USP_EN);
1148 wr_regl(port, ureg->sirfsoc_tx_fifo_op, SIRFUART_FIFO_RESET);
1149 wr_regl(port, ureg->sirfsoc_tx_fifo_op, 0);
1150 wr_regl(port, ureg->sirfsoc_rx_fifo_op, SIRFUART_FIFO_RESET);
1151 wr_regl(port, ureg->sirfsoc_rx_fifo_op, 0);
1152 wr_regl(port, ureg->sirfsoc_tx_fifo_ctrl, SIRFUART_FIFO_THD(port));
1153 wr_regl(port, ureg->sirfsoc_rx_fifo_ctrl, SIRFUART_FIFO_THD(port));
1154
1155 if (IS_DMA_CHAN_VALID(sirfport->rx_dma_no)) {
1156 ret = sirfsoc_uart_init_rx_dma(port);
1157 if (ret)
1158 goto init_rx_err;
1159 wr_regl(port, ureg->sirfsoc_rx_fifo_level_chk,
1160 SIRFUART_RX_FIFO_CHK_SC(port->line, 0x4) |
1161 SIRFUART_RX_FIFO_CHK_LC(port->line, 0xe) |
1162 SIRFUART_RX_FIFO_CHK_HC(port->line, 0x1b));
1163 }
1164 if (IS_DMA_CHAN_VALID(sirfport->tx_dma_no)) {
1165 sirfsoc_uart_init_tx_dma(port);
1166 sirfport->tx_dma_state = TX_DMA_IDLE;
1167 wr_regl(port, ureg->sirfsoc_tx_fifo_level_chk,
1168 SIRFUART_TX_FIFO_CHK_SC(port->line, 0x1b) |
1169 SIRFUART_TX_FIFO_CHK_LC(port->line, 0xe) |
1170 SIRFUART_TX_FIFO_CHK_HC(port->line, 0x4));
1171 }
1172 sirfport->ms_enabled = false;
1173 if (sirfport->uart_reg->uart_type == SIRF_USP_UART &&
1174 sirfport->hw_flow_ctrl) {
1175 set_irq_flags(gpio_to_irq(sirfport->cts_gpio),
1176 IRQF_VALID | IRQF_NOAUTOEN);
1177 ret = request_irq(gpio_to_irq(sirfport->cts_gpio),
1178 sirfsoc_uart_usp_cts_handler, IRQF_TRIGGER_FALLING |
1179 IRQF_TRIGGER_RISING, "usp_cts_irq", sirfport);
1180 if (ret != 0) {
1181 dev_err(port->dev, "UART-USP:request gpio irq fail\n");
1182 goto init_rx_err;
1183 }
1184 }
1185
508 enable_irq(port->irq); 1186 enable_irq(port->irq);
1187
1188 return 0;
1189init_rx_err:
1190 free_irq(port->irq, sirfport);
509irq_err: 1191irq_err:
510 return ret; 1192 return ret;
511} 1193}
@@ -513,11 +1195,25 @@ irq_err:
513static void sirfsoc_uart_shutdown(struct uart_port *port) 1195static void sirfsoc_uart_shutdown(struct uart_port *port)
514{ 1196{
515 struct sirfsoc_uart_port *sirfport = to_sirfport(port); 1197 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
516 wr_regl(port, SIRFUART_INT_EN, 0); 1198 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
1199 if (!sirfport->is_marco)
1200 wr_regl(port, ureg->sirfsoc_int_en_reg, 0);
1201 else
1202 wr_regl(port, SIRFUART_INT_EN_CLR, ~0UL);
1203
517 free_irq(port->irq, sirfport); 1204 free_irq(port->irq, sirfport);
518 if (sirfport->ms_enabled) { 1205 if (sirfport->ms_enabled)
519 sirfsoc_uart_disable_ms(port); 1206 sirfsoc_uart_disable_ms(port);
520 sirfport->ms_enabled = 0; 1207 if (sirfport->uart_reg->uart_type == SIRF_USP_UART &&
1208 sirfport->hw_flow_ctrl) {
1209 gpio_set_value(sirfport->rts_gpio, 1);
1210 free_irq(gpio_to_irq(sirfport->cts_gpio), sirfport);
1211 }
1212 if (IS_DMA_CHAN_VALID(sirfport->rx_dma_no))
1213 sirfsoc_uart_uninit_rx_dma(sirfport);
1214 if (IS_DMA_CHAN_VALID(sirfport->tx_dma_no)) {
1215 sirfsoc_uart_uninit_tx_dma(sirfport);
1216 sirfport->tx_dma_state = TX_DMA_IDLE;
521 } 1217 }
522} 1218}
523 1219
@@ -528,9 +1224,11 @@ static const char *sirfsoc_uart_type(struct uart_port *port)
528 1224
529static int sirfsoc_uart_request_port(struct uart_port *port) 1225static int sirfsoc_uart_request_port(struct uart_port *port)
530{ 1226{
1227 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
1228 struct sirfsoc_uart_param *uart_param = &sirfport->uart_reg->uart_param;
531 void *ret; 1229 void *ret;
532 ret = request_mem_region(port->mapbase, 1230 ret = request_mem_region(port->mapbase,
533 SIRFUART_MAP_SIZE, SIRFUART_PORT_NAME); 1231 SIRFUART_MAP_SIZE, uart_param->port_name);
534 return ret ? 0 : -EBUSY; 1232 return ret ? 0 : -EBUSY;
535} 1233}
536 1234
@@ -566,32 +1264,45 @@ static struct uart_ops sirfsoc_uart_ops = {
566}; 1264};
567 1265
568#ifdef CONFIG_SERIAL_SIRFSOC_CONSOLE 1266#ifdef CONFIG_SERIAL_SIRFSOC_CONSOLE
569static int __init sirfsoc_uart_console_setup(struct console *co, char *options) 1267static int __init
1268sirfsoc_uart_console_setup(struct console *co, char *options)
570{ 1269{
571 unsigned int baud = 115200; 1270 unsigned int baud = 115200;
572 unsigned int bits = 8; 1271 unsigned int bits = 8;
573 unsigned int parity = 'n'; 1272 unsigned int parity = 'n';
574 unsigned int flow = 'n'; 1273 unsigned int flow = 'n';
575 struct uart_port *port = &sirfsoc_uart_ports[co->index].port; 1274 struct uart_port *port = &sirfsoc_uart_ports[co->index].port;
576 1275 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
1276 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
577 if (co->index < 0 || co->index >= SIRFSOC_UART_NR) 1277 if (co->index < 0 || co->index >= SIRFSOC_UART_NR)
578 return -EINVAL; 1278 return -EINVAL;
579 1279
580 if (!port->mapbase) 1280 if (!port->mapbase)
581 return -ENODEV; 1281 return -ENODEV;
582 1282
1283 /* enable usp in mode1 register */
1284 if (sirfport->uart_reg->uart_type == SIRF_USP_UART)
1285 wr_regl(port, ureg->sirfsoc_mode1, SIRFSOC_USP_EN |
1286 SIRFSOC_USP_ENDIAN_CTRL_LSBF);
583 if (options) 1287 if (options)
584 uart_parse_options(options, &baud, &parity, &bits, &flow); 1288 uart_parse_options(options, &baud, &parity, &bits, &flow);
585 port->cons = co; 1289 port->cons = co;
1290
1291 /* default console tx/rx transfer using io mode */
1292 sirfport->rx_dma_no = UNVALID_DMA_CHAN;
1293 sirfport->tx_dma_no = UNVALID_DMA_CHAN;
586 return uart_set_options(port, co, baud, parity, bits, flow); 1294 return uart_set_options(port, co, baud, parity, bits, flow);
587} 1295}
588 1296
589static void sirfsoc_uart_console_putchar(struct uart_port *port, int ch) 1297static void sirfsoc_uart_console_putchar(struct uart_port *port, int ch)
590{ 1298{
1299 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
1300 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
1301 struct sirfsoc_fifo_status *ufifo_st = &sirfport->uart_reg->fifo_status;
591 while (rd_regl(port, 1302 while (rd_regl(port,
592 SIRFUART_TX_FIFO_STATUS) & SIRFUART_FIFOFULL_MASK(port)) 1303 ureg->sirfsoc_tx_fifo_status) & ufifo_st->ff_full(port->line))
593 cpu_relax(); 1304 cpu_relax();
594 wr_regb(port, SIRFUART_TX_FIFO_DATA, ch); 1305 wr_regb(port, ureg->sirfsoc_tx_fifo_data, ch);
595} 1306}
596 1307
597static void sirfsoc_uart_console_write(struct console *co, const char *s, 1308static void sirfsoc_uart_console_write(struct console *co, const char *s,
@@ -633,27 +1344,99 @@ static struct uart_driver sirfsoc_uart_drv = {
633#endif 1344#endif
634}; 1345};
635 1346
636int sirfsoc_uart_probe(struct platform_device *pdev) 1347static struct of_device_id sirfsoc_uart_ids[] = {
1348 { .compatible = "sirf,prima2-uart", .data = &sirfsoc_uart,},
1349 { .compatible = "sirf,marco-uart", .data = &sirfsoc_uart},
1350 { .compatible = "sirf,prima2-usp-uart", .data = &sirfsoc_usp},
1351 {}
1352};
1353MODULE_DEVICE_TABLE(of, sirfsoc_uart_ids);
1354
1355static int sirfsoc_uart_probe(struct platform_device *pdev)
637{ 1356{
638 struct sirfsoc_uart_port *sirfport; 1357 struct sirfsoc_uart_port *sirfport;
639 struct uart_port *port; 1358 struct uart_port *port;
640 struct resource *res; 1359 struct resource *res;
641 int ret; 1360 int ret;
1361 const struct of_device_id *match;
642 1362
1363 match = of_match_node(sirfsoc_uart_ids, pdev->dev.of_node);
643 if (of_property_read_u32(pdev->dev.of_node, "cell-index", &pdev->id)) { 1364 if (of_property_read_u32(pdev->dev.of_node, "cell-index", &pdev->id)) {
644 dev_err(&pdev->dev, 1365 dev_err(&pdev->dev,
645 "Unable to find cell-index in uart node.\n"); 1366 "Unable to find cell-index in uart node.\n");
646 ret = -EFAULT; 1367 ret = -EFAULT;
647 goto err; 1368 goto err;
648 } 1369 }
649 1370 if (of_device_is_compatible(pdev->dev.of_node, "sirf,prima2-usp-uart"))
1371 pdev->id += ((struct sirfsoc_uart_register *)
1372 match->data)->uart_param.register_uart_nr;
650 sirfport = &sirfsoc_uart_ports[pdev->id]; 1373 sirfport = &sirfsoc_uart_ports[pdev->id];
651 port = &sirfport->port; 1374 port = &sirfport->port;
652 port->dev = &pdev->dev; 1375 port->dev = &pdev->dev;
653 port->private_data = sirfport; 1376 port->private_data = sirfport;
1377 sirfport->uart_reg = (struct sirfsoc_uart_register *)match->data;
1378
1379 sirfport->hw_flow_ctrl = of_property_read_bool(pdev->dev.of_node,
1380 "sirf,uart-has-rtscts");
1381 if (of_device_is_compatible(pdev->dev.of_node, "sirf,prima2-uart")) {
1382 sirfport->uart_reg->uart_type = SIRF_REAL_UART;
1383 if (of_property_read_u32(pdev->dev.of_node,
1384 "sirf,uart-dma-rx-channel",
1385 &sirfport->rx_dma_no))
1386 sirfport->rx_dma_no = UNVALID_DMA_CHAN;
1387 if (of_property_read_u32(pdev->dev.of_node,
1388 "sirf,uart-dma-tx-channel",
1389 &sirfport->tx_dma_no))
1390 sirfport->tx_dma_no = UNVALID_DMA_CHAN;
1391 }
1392 if (of_device_is_compatible(pdev->dev.of_node, "sirf,prima2-usp-uart")) {
1393 sirfport->uart_reg->uart_type = SIRF_USP_UART;
1394 if (of_property_read_u32(pdev->dev.of_node,
1395 "sirf,usp-dma-rx-channel",
1396 &sirfport->rx_dma_no))
1397 sirfport->rx_dma_no = UNVALID_DMA_CHAN;
1398 if (of_property_read_u32(pdev->dev.of_node,
1399 "sirf,usp-dma-tx-channel",
1400 &sirfport->tx_dma_no))
1401 sirfport->tx_dma_no = UNVALID_DMA_CHAN;
1402 if (!sirfport->hw_flow_ctrl)
1403 goto usp_no_flow_control;
1404 if (of_find_property(pdev->dev.of_node, "cts-gpios", NULL))
1405 sirfport->cts_gpio = of_get_named_gpio(
1406 pdev->dev.of_node, "cts-gpios", 0);
1407 else
1408 sirfport->cts_gpio = -1;
1409 if (of_find_property(pdev->dev.of_node, "rts-gpios", NULL))
1410 sirfport->rts_gpio = of_get_named_gpio(
1411 pdev->dev.of_node, "rts-gpios", 0);
1412 else
1413 sirfport->rts_gpio = -1;
654 1414
655 if (of_find_property(pdev->dev.of_node, "hw_flow_ctrl", NULL)) 1415 if ((!gpio_is_valid(sirfport->cts_gpio) ||
656 sirfport->hw_flow_ctrl = 1; 1416 !gpio_is_valid(sirfport->rts_gpio))) {
1417 ret = -EINVAL;
1418 dev_err(&pdev->dev,
1419 "Usp flow control must have cts and rts gpio");
1420 goto err;
1421 }
1422 ret = devm_gpio_request(&pdev->dev, sirfport->cts_gpio,
1423 "usp-cts-gpio");
1424 if (ret) {
1425 dev_err(&pdev->dev, "Unable request cts gpio");
1426 goto err;
1427 }
1428 gpio_direction_input(sirfport->cts_gpio);
1429 ret = devm_gpio_request(&pdev->dev, sirfport->rts_gpio,
1430 "usp-rts-gpio");
1431 if (ret) {
1432 dev_err(&pdev->dev, "Unable request rts gpio");
1433 goto err;
1434 }
1435 gpio_direction_output(sirfport->rts_gpio, 1);
1436 }
1437usp_no_flow_control:
1438 if (of_device_is_compatible(pdev->dev.of_node, "sirf,marco-uart"))
1439 sirfport->is_marco = true;
657 1440
658 if (of_property_read_u32(pdev->dev.of_node, 1441 if (of_property_read_u32(pdev->dev.of_node,
659 "fifosize", 1442 "fifosize",
@@ -670,6 +1453,12 @@ int sirfsoc_uart_probe(struct platform_device *pdev)
670 ret = -EFAULT; 1453 ret = -EFAULT;
671 goto err; 1454 goto err;
672 } 1455 }
1456 spin_lock_init(&sirfport->rx_lock);
1457 spin_lock_init(&sirfport->tx_lock);
1458 tasklet_init(&sirfport->rx_dma_complete_tasklet,
1459 sirfsoc_uart_rx_dma_complete_tl, (unsigned long)sirfport);
1460 tasklet_init(&sirfport->rx_tmo_process_tasklet,
1461 sirfsoc_rx_tmo_process_tl, (unsigned long)sirfport);
673 port->mapbase = res->start; 1462 port->mapbase = res->start;
674 port->membase = devm_ioremap(&pdev->dev, res->start, resource_size(res)); 1463 port->membase = devm_ioremap(&pdev->dev, res->start, resource_size(res));
675 if (!port->membase) { 1464 if (!port->membase) {
@@ -685,18 +1474,10 @@ int sirfsoc_uart_probe(struct platform_device *pdev)
685 } 1474 }
686 port->irq = res->start; 1475 port->irq = res->start;
687 1476
688 if (sirfport->hw_flow_ctrl) {
689 sirfport->p = pinctrl_get_select_default(&pdev->dev);
690 if (IS_ERR(sirfport->p)) {
691 ret = PTR_ERR(sirfport->p);
692 goto err;
693 }
694 }
695
696 sirfport->clk = clk_get(&pdev->dev, NULL); 1477 sirfport->clk = clk_get(&pdev->dev, NULL);
697 if (IS_ERR(sirfport->clk)) { 1478 if (IS_ERR(sirfport->clk)) {
698 ret = PTR_ERR(sirfport->clk); 1479 ret = PTR_ERR(sirfport->clk);
699 goto clk_err; 1480 goto err;
700 } 1481 }
701 clk_prepare_enable(sirfport->clk); 1482 clk_prepare_enable(sirfport->clk);
702 port->uartclk = clk_get_rate(sirfport->clk); 1483 port->uartclk = clk_get_rate(sirfport->clk);
@@ -716,10 +1497,6 @@ int sirfsoc_uart_probe(struct platform_device *pdev)
716port_err: 1497port_err:
717 clk_disable_unprepare(sirfport->clk); 1498 clk_disable_unprepare(sirfport->clk);
718 clk_put(sirfport->clk); 1499 clk_put(sirfport->clk);
719clk_err:
720 platform_set_drvdata(pdev, NULL);
721 if (sirfport->hw_flow_ctrl)
722 pinctrl_put(sirfport->p);
723err: 1500err:
724 return ret; 1501 return ret;
725} 1502}
@@ -728,9 +1505,6 @@ static int sirfsoc_uart_remove(struct platform_device *pdev)
728{ 1505{
729 struct sirfsoc_uart_port *sirfport = platform_get_drvdata(pdev); 1506 struct sirfsoc_uart_port *sirfport = platform_get_drvdata(pdev);
730 struct uart_port *port = &sirfport->port; 1507 struct uart_port *port = &sirfport->port;
731 platform_set_drvdata(pdev, NULL);
732 if (sirfport->hw_flow_ctrl)
733 pinctrl_put(sirfport->p);
734 clk_disable_unprepare(sirfport->clk); 1508 clk_disable_unprepare(sirfport->clk);
735 clk_put(sirfport->clk); 1509 clk_put(sirfport->clk);
736 uart_remove_one_port(&sirfsoc_uart_drv, port); 1510 uart_remove_one_port(&sirfsoc_uart_drv, port);
@@ -754,13 +1528,6 @@ static int sirfsoc_uart_resume(struct platform_device *pdev)
754 return 0; 1528 return 0;
755} 1529}
756 1530
757static struct of_device_id sirfsoc_uart_ids[] = {
758 { .compatible = "sirf,prima2-uart", },
759 { .compatible = "sirf,marco-uart", },
760 {}
761};
762MODULE_DEVICE_TABLE(of, sirfsoc_uart_ids);
763
764static struct platform_driver sirfsoc_uart_driver = { 1531static struct platform_driver sirfsoc_uart_driver = {
765 .probe = sirfsoc_uart_probe, 1532 .probe = sirfsoc_uart_probe,
766 .remove = sirfsoc_uart_remove, 1533 .remove = sirfsoc_uart_remove,
diff --git a/drivers/tty/serial/sirfsoc_uart.h b/drivers/tty/serial/sirfsoc_uart.h
index 85328ba0c4e3..fb8d0a002607 100644
--- a/drivers/tty/serial/sirfsoc_uart.h
+++ b/drivers/tty/serial/sirfsoc_uart.h
@@ -6,31 +6,260 @@
6 * Licensed under GPLv2 or later. 6 * Licensed under GPLv2 or later.
7 */ 7 */
8#include <linux/bitops.h> 8#include <linux/bitops.h>
9struct sirfsoc_uart_param {
10 const char *uart_name;
11 const char *port_name;
12 u32 uart_nr;
13 u32 register_uart_nr;
14};
15
16struct sirfsoc_register {
17 /* hardware uart specific */
18 u32 sirfsoc_line_ctrl;
19 u32 sirfsoc_divisor;
20 /* uart - usp common */
21 u32 sirfsoc_tx_rx_en;
22 u32 sirfsoc_int_en_reg;
23 u32 sirfsoc_int_st_reg;
24 u32 sirfsoc_tx_dma_io_ctrl;
25 u32 sirfsoc_tx_dma_io_len;
26 u32 sirfsoc_tx_fifo_ctrl;
27 u32 sirfsoc_tx_fifo_level_chk;
28 u32 sirfsoc_tx_fifo_op;
29 u32 sirfsoc_tx_fifo_status;
30 u32 sirfsoc_tx_fifo_data;
31 u32 sirfsoc_rx_dma_io_ctrl;
32 u32 sirfsoc_rx_dma_io_len;
33 u32 sirfsoc_rx_fifo_ctrl;
34 u32 sirfsoc_rx_fifo_level_chk;
35 u32 sirfsoc_rx_fifo_op;
36 u32 sirfsoc_rx_fifo_status;
37 u32 sirfsoc_rx_fifo_data;
38 u32 sirfsoc_afc_ctrl;
39 u32 sirfsoc_swh_dma_io;
40 /* hardware usp specific */
41 u32 sirfsoc_mode1;
42 u32 sirfsoc_mode2;
43 u32 sirfsoc_tx_frame_ctrl;
44 u32 sirfsoc_rx_frame_ctrl;
45 u32 sirfsoc_async_param_reg;
46};
47
48typedef u32 (*fifo_full_mask)(int line);
49typedef u32 (*fifo_empty_mask)(int line);
50
51struct sirfsoc_fifo_status {
52 fifo_full_mask ff_full;
53 fifo_empty_mask ff_empty;
54};
9 55
10/* UART Register Offset Define */ 56struct sirfsoc_int_en {
11#define SIRFUART_LINE_CTRL 0x0040 57 u32 sirfsoc_rx_done_en;
12#define SIRFUART_TX_RX_EN 0x004c 58 u32 sirfsoc_tx_done_en;
13#define SIRFUART_DIVISOR 0x0050 59 u32 sirfsoc_rx_oflow_en;
14#define SIRFUART_INT_EN 0x0054 60 u32 sirfsoc_tx_allout_en;
15#define SIRFUART_INT_STATUS 0x0058 61 u32 sirfsoc_rx_io_dma_en;
16#define SIRFUART_TX_DMA_IO_CTRL 0x0100 62 u32 sirfsoc_tx_io_dma_en;
17#define SIRFUART_TX_DMA_IO_LEN 0x0104 63 u32 sirfsoc_rxfifo_full_en;
18#define SIRFUART_TX_FIFO_CTRL 0x0108 64 u32 sirfsoc_txfifo_empty_en;
19#define SIRFUART_TX_FIFO_LEVEL_CHK 0x010C 65 u32 sirfsoc_rxfifo_thd_en;
20#define SIRFUART_TX_FIFO_OP 0x0110 66 u32 sirfsoc_txfifo_thd_en;
21#define SIRFUART_TX_FIFO_STATUS 0x0114 67 u32 sirfsoc_frm_err_en;
22#define SIRFUART_TX_FIFO_DATA 0x0118 68 u32 sirfsoc_rxd_brk_en;
23#define SIRFUART_RX_DMA_IO_CTRL 0x0120 69 u32 sirfsoc_rx_timeout_en;
24#define SIRFUART_RX_DMA_IO_LEN 0x0124 70 u32 sirfsoc_parity_err_en;
25#define SIRFUART_RX_FIFO_CTRL 0x0128 71 u32 sirfsoc_cts_en;
26#define SIRFUART_RX_FIFO_LEVEL_CHK 0x012C 72 u32 sirfsoc_rts_en;
27#define SIRFUART_RX_FIFO_OP 0x0130 73};
28#define SIRFUART_RX_FIFO_STATUS 0x0134 74
29#define SIRFUART_RX_FIFO_DATA 0x0138 75struct sirfsoc_int_status {
30#define SIRFUART_AFC_CTRL 0x0140 76 u32 sirfsoc_rx_done;
31#define SIRFUART_SWH_DMA_IO 0x0148 77 u32 sirfsoc_tx_done;
32 78 u32 sirfsoc_rx_oflow;
33/* UART Line Control Register */ 79 u32 sirfsoc_tx_allout;
80 u32 sirfsoc_rx_io_dma;
81 u32 sirfsoc_tx_io_dma;
82 u32 sirfsoc_rxfifo_full;
83 u32 sirfsoc_txfifo_empty;
84 u32 sirfsoc_rxfifo_thd;
85 u32 sirfsoc_txfifo_thd;
86 u32 sirfsoc_frm_err;
87 u32 sirfsoc_rxd_brk;
88 u32 sirfsoc_rx_timeout;
89 u32 sirfsoc_parity_err;
90 u32 sirfsoc_cts;
91 u32 sirfsoc_rts;
92};
93
94enum sirfsoc_uart_type {
95 SIRF_REAL_UART,
96 SIRF_USP_UART,
97};
98
99struct sirfsoc_uart_register {
100 struct sirfsoc_register uart_reg;
101 struct sirfsoc_int_en uart_int_en;
102 struct sirfsoc_int_status uart_int_st;
103 struct sirfsoc_fifo_status fifo_status;
104 struct sirfsoc_uart_param uart_param;
105 enum sirfsoc_uart_type uart_type;
106};
107
108u32 usp_ff_full(int line)
109{
110 return 0x80;
111}
112u32 usp_ff_empty(int line)
113{
114 return 0x100;
115}
116u32 uart_ff_full(int line)
117{
118 return (line == 1) ? (0x20) : (0x80);
119}
120u32 uart_ff_empty(int line)
121{
122 return (line == 1) ? (0x40) : (0x100);
123}
124struct sirfsoc_uart_register sirfsoc_usp = {
125 .uart_reg = {
126 .sirfsoc_mode1 = 0x0000,
127 .sirfsoc_mode2 = 0x0004,
128 .sirfsoc_tx_frame_ctrl = 0x0008,
129 .sirfsoc_rx_frame_ctrl = 0x000c,
130 .sirfsoc_tx_rx_en = 0x0010,
131 .sirfsoc_int_en_reg = 0x0014,
132 .sirfsoc_int_st_reg = 0x0018,
133 .sirfsoc_async_param_reg = 0x0024,
134 .sirfsoc_tx_dma_io_ctrl = 0x0100,
135 .sirfsoc_tx_dma_io_len = 0x0104,
136 .sirfsoc_tx_fifo_ctrl = 0x0108,
137 .sirfsoc_tx_fifo_level_chk = 0x010c,
138 .sirfsoc_tx_fifo_op = 0x0110,
139 .sirfsoc_tx_fifo_status = 0x0114,
140 .sirfsoc_tx_fifo_data = 0x0118,
141 .sirfsoc_rx_dma_io_ctrl = 0x0120,
142 .sirfsoc_rx_dma_io_len = 0x0124,
143 .sirfsoc_rx_fifo_ctrl = 0x0128,
144 .sirfsoc_rx_fifo_level_chk = 0x012c,
145 .sirfsoc_rx_fifo_op = 0x0130,
146 .sirfsoc_rx_fifo_status = 0x0134,
147 .sirfsoc_rx_fifo_data = 0x0138,
148 },
149 .uart_int_en = {
150 .sirfsoc_rx_done_en = BIT(0),
151 .sirfsoc_tx_done_en = BIT(1),
152 .sirfsoc_rx_oflow_en = BIT(2),
153 .sirfsoc_tx_allout_en = BIT(3),
154 .sirfsoc_rx_io_dma_en = BIT(4),
155 .sirfsoc_tx_io_dma_en = BIT(5),
156 .sirfsoc_rxfifo_full_en = BIT(6),
157 .sirfsoc_txfifo_empty_en = BIT(7),
158 .sirfsoc_rxfifo_thd_en = BIT(8),
159 .sirfsoc_txfifo_thd_en = BIT(9),
160 .sirfsoc_frm_err_en = BIT(10),
161 .sirfsoc_rx_timeout_en = BIT(11),
162 .sirfsoc_rxd_brk_en = BIT(15),
163 },
164 .uart_int_st = {
165 .sirfsoc_rx_done = BIT(0),
166 .sirfsoc_tx_done = BIT(1),
167 .sirfsoc_rx_oflow = BIT(2),
168 .sirfsoc_tx_allout = BIT(3),
169 .sirfsoc_rx_io_dma = BIT(4),
170 .sirfsoc_tx_io_dma = BIT(5),
171 .sirfsoc_rxfifo_full = BIT(6),
172 .sirfsoc_txfifo_empty = BIT(7),
173 .sirfsoc_rxfifo_thd = BIT(8),
174 .sirfsoc_txfifo_thd = BIT(9),
175 .sirfsoc_frm_err = BIT(10),
176 .sirfsoc_rx_timeout = BIT(11),
177 .sirfsoc_rxd_brk = BIT(15),
178 },
179 .fifo_status = {
180 .ff_full = usp_ff_full,
181 .ff_empty = usp_ff_empty,
182 },
183 .uart_param = {
184 .uart_name = "ttySiRF",
185 .port_name = "sirfsoc-uart",
186 .uart_nr = 2,
187 .register_uart_nr = 3,
188 },
189};
190
191struct sirfsoc_uart_register sirfsoc_uart = {
192 .uart_reg = {
193 .sirfsoc_line_ctrl = 0x0040,
194 .sirfsoc_tx_rx_en = 0x004c,
195 .sirfsoc_divisor = 0x0050,
196 .sirfsoc_int_en_reg = 0x0054,
197 .sirfsoc_int_st_reg = 0x0058,
198 .sirfsoc_tx_dma_io_ctrl = 0x0100,
199 .sirfsoc_tx_dma_io_len = 0x0104,
200 .sirfsoc_tx_fifo_ctrl = 0x0108,
201 .sirfsoc_tx_fifo_level_chk = 0x010c,
202 .sirfsoc_tx_fifo_op = 0x0110,
203 .sirfsoc_tx_fifo_status = 0x0114,
204 .sirfsoc_tx_fifo_data = 0x0118,
205 .sirfsoc_rx_dma_io_ctrl = 0x0120,
206 .sirfsoc_rx_dma_io_len = 0x0124,
207 .sirfsoc_rx_fifo_ctrl = 0x0128,
208 .sirfsoc_rx_fifo_level_chk = 0x012c,
209 .sirfsoc_rx_fifo_op = 0x0130,
210 .sirfsoc_rx_fifo_status = 0x0134,
211 .sirfsoc_rx_fifo_data = 0x0138,
212 .sirfsoc_afc_ctrl = 0x0140,
213 .sirfsoc_swh_dma_io = 0x0148,
214 },
215 .uart_int_en = {
216 .sirfsoc_rx_done_en = BIT(0),
217 .sirfsoc_tx_done_en = BIT(1),
218 .sirfsoc_rx_oflow_en = BIT(2),
219 .sirfsoc_tx_allout_en = BIT(3),
220 .sirfsoc_rx_io_dma_en = BIT(4),
221 .sirfsoc_tx_io_dma_en = BIT(5),
222 .sirfsoc_rxfifo_full_en = BIT(6),
223 .sirfsoc_txfifo_empty_en = BIT(7),
224 .sirfsoc_rxfifo_thd_en = BIT(8),
225 .sirfsoc_txfifo_thd_en = BIT(9),
226 .sirfsoc_frm_err_en = BIT(10),
227 .sirfsoc_rxd_brk_en = BIT(11),
228 .sirfsoc_rx_timeout_en = BIT(12),
229 .sirfsoc_parity_err_en = BIT(13),
230 .sirfsoc_cts_en = BIT(14),
231 .sirfsoc_rts_en = BIT(15),
232 },
233 .uart_int_st = {
234 .sirfsoc_rx_done = BIT(0),
235 .sirfsoc_tx_done = BIT(1),
236 .sirfsoc_rx_oflow = BIT(2),
237 .sirfsoc_tx_allout = BIT(3),
238 .sirfsoc_rx_io_dma = BIT(4),
239 .sirfsoc_tx_io_dma = BIT(5),
240 .sirfsoc_rxfifo_full = BIT(6),
241 .sirfsoc_txfifo_empty = BIT(7),
242 .sirfsoc_rxfifo_thd = BIT(8),
243 .sirfsoc_txfifo_thd = BIT(9),
244 .sirfsoc_frm_err = BIT(10),
245 .sirfsoc_rxd_brk = BIT(11),
246 .sirfsoc_rx_timeout = BIT(12),
247 .sirfsoc_parity_err = BIT(13),
248 .sirfsoc_cts = BIT(14),
249 .sirfsoc_rts = BIT(15),
250 },
251 .fifo_status = {
252 .ff_full = uart_ff_full,
253 .ff_empty = uart_ff_empty,
254 },
255 .uart_param = {
256 .uart_name = "ttySiRF",
257 .port_name = "sirfsoc_uart",
258 .uart_nr = 3,
259 .register_uart_nr = 0,
260 },
261};
262/* uart io ctrl */
34#define SIRFUART_DATA_BIT_LEN_MASK 0x3 263#define SIRFUART_DATA_BIT_LEN_MASK 0x3
35#define SIRFUART_DATA_BIT_LEN_5 BIT(0) 264#define SIRFUART_DATA_BIT_LEN_5 BIT(0)
36#define SIRFUART_DATA_BIT_LEN_6 1 265#define SIRFUART_DATA_BIT_LEN_6 1
@@ -50,96 +279,93 @@
50#define SIRFUART_LOOP_BACK BIT(7) 279#define SIRFUART_LOOP_BACK BIT(7)
51#define SIRFUART_PARITY_MASK (7 << 3) 280#define SIRFUART_PARITY_MASK (7 << 3)
52#define SIRFUART_DUMMY_READ BIT(16) 281#define SIRFUART_DUMMY_READ BIT(16)
53 282#define SIRFUART_AFC_CTRL_RX_THD 0x70
54#define SIRFSOC_UART_RX_TIMEOUT(br, to) (((br) * (((to) + 999) / 1000)) / 1000)
55#define SIRFUART_RECV_TIMEOUT_MASK (0xFFFF << 16)
56#define SIRFUART_RECV_TIMEOUT(x) (((x) & 0xFFFF) << 16)
57
58/* UART Auto Flow Control */
59#define SIRFUART_AFC_RX_THD_MASK 0x000000FF
60#define SIRFUART_AFC_RX_EN BIT(8) 283#define SIRFUART_AFC_RX_EN BIT(8)
61#define SIRFUART_AFC_TX_EN BIT(9) 284#define SIRFUART_AFC_TX_EN BIT(9)
62#define SIRFUART_CTS_CTRL BIT(10) 285#define SIRFUART_AFC_CTS_CTRL BIT(10)
63#define SIRFUART_RTS_CTRL BIT(11) 286#define SIRFUART_AFC_RTS_CTRL BIT(11)
64#define SIRFUART_CTS_IN_STATUS BIT(12) 287#define SIRFUART_AFC_CTS_STATUS BIT(12)
65#define SIRFUART_RTS_OUT_STATUS BIT(13) 288#define SIRFUART_AFC_RTS_STATUS BIT(13)
66
67/* UART Interrupt Enable Register */
68#define SIRFUART_RX_DONE_INT BIT(0)
69#define SIRFUART_TX_DONE_INT BIT(1)
70#define SIRFUART_RX_OFLOW_INT BIT(2)
71#define SIRFUART_TX_ALLOUT_INT BIT(3)
72#define SIRFUART_RX_IO_DMA_INT BIT(4)
73#define SIRFUART_TX_IO_DMA_INT BIT(5)
74#define SIRFUART_RXFIFO_FULL_INT BIT(6)
75#define SIRFUART_TXFIFO_EMPTY_INT BIT(7)
76#define SIRFUART_RXFIFO_THD_INT BIT(8)
77#define SIRFUART_TXFIFO_THD_INT BIT(9)
78#define SIRFUART_FRM_ERR_INT BIT(10)
79#define SIRFUART_RXD_BREAK_INT BIT(11)
80#define SIRFUART_RX_TIMEOUT_INT BIT(12)
81#define SIRFUART_PARITY_ERR_INT BIT(13)
82#define SIRFUART_CTS_INT_EN BIT(14)
83#define SIRFUART_RTS_INT_EN BIT(15)
84
85/* UART Interrupt Status Register */
86#define SIRFUART_RX_DONE BIT(0)
87#define SIRFUART_TX_DONE BIT(1)
88#define SIRFUART_RX_OFLOW BIT(2)
89#define SIRFUART_TX_ALL_EMPTY BIT(3)
90#define SIRFUART_DMA_IO_RX_DONE BIT(4)
91#define SIRFUART_DMA_IO_TX_DONE BIT(5)
92#define SIRFUART_RXFIFO_FULL BIT(6)
93#define SIRFUART_TXFIFO_EMPTY BIT(7)
94#define SIRFUART_RXFIFO_THD_REACH BIT(8)
95#define SIRFUART_TXFIFO_THD_REACH BIT(9)
96#define SIRFUART_FRM_ERR BIT(10)
97#define SIRFUART_RXD_BREAK BIT(11)
98#define SIRFUART_RX_TIMEOUT BIT(12)
99#define SIRFUART_PARITY_ERR BIT(13)
100#define SIRFUART_CTS_CHANGE BIT(14)
101#define SIRFUART_RTS_CHANGE BIT(15)
102#define SIRFUART_PLUG_IN BIT(16)
103
104#define SIRFUART_ERR_INT_STAT \
105 (SIRFUART_RX_OFLOW | \
106 SIRFUART_FRM_ERR | \
107 SIRFUART_RXD_BREAK | \
108 SIRFUART_PARITY_ERR)
109#define SIRFUART_ERR_INT_EN \
110 (SIRFUART_RX_OFLOW_INT | \
111 SIRFUART_FRM_ERR_INT | \
112 SIRFUART_RXD_BREAK_INT | \
113 SIRFUART_PARITY_ERR_INT)
114#define SIRFUART_TX_INT_EN SIRFUART_TXFIFO_EMPTY_INT
115#define SIRFUART_RX_IO_INT_EN \
116 (SIRFUART_RX_TIMEOUT_INT | \
117 SIRFUART_RXFIFO_THD_INT | \
118 SIRFUART_RXFIFO_FULL_INT | \
119 SIRFUART_ERR_INT_EN)
120
121/* UART FIFO Register */ 289/* UART FIFO Register */
122#define SIRFUART_TX_FIFO_STOP 0x0 290#define SIRFUART_FIFO_STOP 0x0
123#define SIRFUART_TX_FIFO_RESET 0x1 291#define SIRFUART_FIFO_RESET BIT(0)
124#define SIRFUART_TX_FIFO_START 0x2 292#define SIRFUART_FIFO_START BIT(1)
125#define SIRFUART_RX_FIFO_STOP 0x0
126#define SIRFUART_RX_FIFO_RESET 0x1
127#define SIRFUART_RX_FIFO_START 0x2
128#define SIRFUART_TX_MODE_DMA 0
129#define SIRFUART_TX_MODE_IO 1
130#define SIRFUART_RX_MODE_DMA 0
131#define SIRFUART_RX_MODE_IO 1
132
133#define SIRFUART_RX_EN 0x1
134#define SIRFUART_TX_EN 0x2
135 293
294#define SIRFUART_RX_EN BIT(0)
295#define SIRFUART_TX_EN BIT(1)
296
297#define SIRFUART_IO_MODE BIT(0)
298#define SIRFUART_DMA_MODE 0x0
299
300/* Macro Specific*/
301#define SIRFUART_INT_EN_CLR 0x0060
302/* Baud Rate Calculation */
303#define SIRF_MIN_SAMPLE_DIV 0xf
304#define SIRF_MAX_SAMPLE_DIV 0x3f
305#define SIRF_IOCLK_DIV_MAX 0xffff
306#define SIRF_SAMPLE_DIV_SHIFT 16
307#define SIRF_IOCLK_DIV_MASK 0xffff
308#define SIRF_SAMPLE_DIV_MASK 0x3f0000
309#define SIRF_BAUD_RATE_SUPPORT_NR 18
310
311/* USP SPEC */
312#define SIRFSOC_USP_ENDIAN_CTRL_LSBF BIT(4)
313#define SIRFSOC_USP_EN BIT(5)
314#define SIRFSOC_USP_MODE2_RXD_DELAY_OFFSET 0
315#define SIRFSOC_USP_MODE2_TXD_DELAY_OFFSET 8
316#define SIRFSOC_USP_MODE2_CLK_DIVISOR_MASK 0x3ff
317#define SIRFSOC_USP_MODE2_CLK_DIVISOR_OFFSET 21
318#define SIRFSOC_USP_TX_DATA_LEN_OFFSET 0
319#define SIRFSOC_USP_TX_SYNC_LEN_OFFSET 8
320#define SIRFSOC_USP_TX_FRAME_LEN_OFFSET 16
321#define SIRFSOC_USP_TX_SHIFTER_LEN_OFFSET 24
322#define SIRFSOC_USP_TX_CLK_DIVISOR_OFFSET 30
323#define SIRFSOC_USP_RX_DATA_LEN_OFFSET 0
324#define SIRFSOC_USP_RX_FRAME_LEN_OFFSET 8
325#define SIRFSOC_USP_RX_SHIFTER_LEN_OFFSET 16
326#define SIRFSOC_USP_RX_CLK_DIVISOR_OFFSET 24
327#define SIRFSOC_USP_ASYNC_DIV2_MASK 0x3f
328#define SIRFSOC_USP_ASYNC_DIV2_OFFSET 16
329
330/* USP-UART Common */
331#define SIRFSOC_UART_RX_TIMEOUT(br, to) (((br) * (((to) + 999) / 1000)) / 1000)
332#define SIRFUART_RECV_TIMEOUT_VALUE(x) \
333 (((x) > 0xFFFF) ? 0xFFFF : ((x) & 0xFFFF))
334#define SIRFUART_RECV_TIMEOUT(port, x) \
335 (((port)->line > 2) ? (x & 0xFFFF) : ((x) & 0xFFFF) << 16)
336
337#define SIRFUART_FIFO_THD(port) ((port->line) == 1 ? 16 : 64)
338#define SIRFUART_ERR_INT_STAT(port, unit_st) \
339 (uint_st->sirfsoc_rx_oflow | \
340 uint_st->sirfsoc_frm_err | \
341 uint_st->sirfsoc_rxd_brk | \
342 ((port->line > 2) ? 0 : uint_st->sirfsoc_parity_err))
343#define SIRFUART_RX_IO_INT_EN(port, uint_en) \
344 (uint_en->sirfsoc_rx_timeout_en |\
345 uint_en->sirfsoc_rxfifo_thd_en |\
346 uint_en->sirfsoc_rxfifo_full_en |\
347 uint_en->sirfsoc_frm_err_en |\
348 uint_en->sirfsoc_rx_oflow_en |\
349 uint_en->sirfsoc_rxd_brk_en |\
350 ((port->line > 2) ? 0 : uint_en->sirfsoc_parity_err_en))
351#define SIRFUART_RX_IO_INT_ST(uint_st) \
352 (uint_st->sirfsoc_rx_timeout |\
353 uint_st->sirfsoc_rxfifo_thd |\
354 uint_st->sirfsoc_rxfifo_full)
355#define SIRFUART_CTS_INT_ST(uint_st) (uint_st->sirfsoc_cts)
356#define SIRFUART_RX_DMA_INT_EN(port, uint_en) \
357 (uint_en->sirfsoc_rx_timeout_en |\
358 uint_en->sirfsoc_frm_err_en |\
359 uint_en->sirfsoc_rx_oflow_en |\
360 uint_en->sirfsoc_rxd_brk_en |\
361 ((port->line > 2) ? 0 : uint_en->sirfsoc_parity_err_en))
136/* Generic Definitions */ 362/* Generic Definitions */
137#define SIRFSOC_UART_NAME "ttySiRF" 363#define SIRFSOC_UART_NAME "ttySiRF"
138#define SIRFSOC_UART_MAJOR 0 364#define SIRFSOC_UART_MAJOR 0
139#define SIRFSOC_UART_MINOR 0 365#define SIRFSOC_UART_MINOR 0
140#define SIRFUART_PORT_NAME "sirfsoc-uart" 366#define SIRFUART_PORT_NAME "sirfsoc-uart"
141#define SIRFUART_MAP_SIZE 0x200 367#define SIRFUART_MAP_SIZE 0x200
142#define SIRFSOC_UART_NR 5 368#define SIRFSOC_UART_NR 6
143#define SIRFSOC_PORT_TYPE 0xa5 369#define SIRFSOC_PORT_TYPE 0xa5
144 370
145/* Baud Rate Calculation */ 371/* Baud Rate Calculation */
@@ -151,19 +377,80 @@
151#define SIRF_SAMPLE_DIV_MASK 0x3f0000 377#define SIRF_SAMPLE_DIV_MASK 0x3f0000
152#define SIRF_BAUD_RATE_SUPPORT_NR 18 378#define SIRF_BAUD_RATE_SUPPORT_NR 18
153 379
380/* Uart Common Use Macro*/
381#define SIRFSOC_RX_DMA_BUF_SIZE 256
382#define BYTES_TO_ALIGN(dma_addr) ((unsigned long)(dma_addr) & 0x3)
383#define LOOP_DMA_BUFA_FILL 1
384#define LOOP_DMA_BUFB_FILL 2
385#define TX_TRAN_PIO 1
386#define TX_TRAN_DMA 2
387/* Uart Fifo Level Chk */
388#define SIRFUART_TX_FIFO_SC_OFFSET 0
389#define SIRFUART_TX_FIFO_LC_OFFSET 10
390#define SIRFUART_TX_FIFO_HC_OFFSET 20
391#define SIRFUART_TX_FIFO_CHK_SC(line, value) ((((line) == 1) ? (value & 0x3) :\
392 (value & 0x1f)) << SIRFUART_TX_FIFO_SC_OFFSET)
393#define SIRFUART_TX_FIFO_CHK_LC(line, value) ((((line) == 1) ? (value & 0x3) :\
394 (value & 0x1f)) << SIRFUART_TX_FIFO_LC_OFFSET)
395#define SIRFUART_TX_FIFO_CHK_HC(line, value) ((((line) == 1) ? (value & 0x3) :\
396 (value & 0x1f)) << SIRFUART_TX_FIFO_HC_OFFSET)
397
398#define SIRFUART_RX_FIFO_CHK_SC SIRFUART_TX_FIFO_CHK_SC
399#define SIRFUART_RX_FIFO_CHK_LC SIRFUART_TX_FIFO_CHK_LC
400#define SIRFUART_RX_FIFO_CHK_HC SIRFUART_TX_FIFO_CHK_HC
401/* Indicate how many buffers used */
402#define SIRFSOC_RX_LOOP_BUF_CNT 2
403
404/* Indicate if DMA channel valid */
405#define IS_DMA_CHAN_VALID(x) ((x) != -1)
406#define UNVALID_DMA_CHAN -1
154/* For Fast Baud Rate Calculation */ 407/* For Fast Baud Rate Calculation */
155struct sirfsoc_baudrate_to_regv { 408struct sirfsoc_baudrate_to_regv {
156 unsigned int baud_rate; 409 unsigned int baud_rate;
157 unsigned int reg_val; 410 unsigned int reg_val;
158}; 411};
159 412
413enum sirfsoc_tx_state {
414 TX_DMA_IDLE,
415 TX_DMA_RUNNING,
416 TX_DMA_PAUSE,
417};
418
419struct sirfsoc_loop_buffer {
420 struct circ_buf xmit;
421 dma_cookie_t cookie;
422 struct dma_async_tx_descriptor *desc;
423 dma_addr_t dma_addr;
424};
425
160struct sirfsoc_uart_port { 426struct sirfsoc_uart_port {
161 unsigned char hw_flow_ctrl; 427 bool hw_flow_ctrl;
162 unsigned char ms_enabled; 428 bool ms_enabled;
163 429
164 struct uart_port port; 430 struct uart_port port;
165 struct pinctrl *p;
166 struct clk *clk; 431 struct clk *clk;
432 /* for SiRFmarco, there are SET/CLR for UART_INT_EN */
433 bool is_marco;
434 struct sirfsoc_uart_register *uart_reg;
435 int rx_dma_no;
436 int tx_dma_no;
437 struct dma_chan *rx_dma_chan;
438 struct dma_chan *tx_dma_chan;
439 dma_addr_t tx_dma_addr;
440 struct dma_async_tx_descriptor *tx_dma_desc;
441 spinlock_t rx_lock;
442 spinlock_t tx_lock;
443 struct tasklet_struct rx_dma_complete_tasklet;
444 struct tasklet_struct rx_tmo_process_tasklet;
445 unsigned int rx_io_count;
446 unsigned long transfer_size;
447 enum sirfsoc_tx_state tx_dma_state;
448 unsigned int cts_gpio;
449 unsigned int rts_gpio;
450
451 struct sirfsoc_loop_buffer rx_dma_items[SIRFSOC_RX_LOOP_BUF_CNT];
452 int rx_completed;
453 int rx_issued;
167}; 454};
168 455
169/* Hardware Flow Control */ 456/* Hardware Flow Control */
diff --git a/drivers/tty/serial/st-asc.c b/drivers/tty/serial/st-asc.c
new file mode 100644
index 000000000000..21e6e84c0df8
--- /dev/null
+++ b/drivers/tty/serial/st-asc.c
@@ -0,0 +1,932 @@
1/*
2 * st-asc.c: ST Asynchronous serial controller (ASC) driver
3 *
4 * Copyright (C) 2003-2013 STMicroelectronics (R&D) Limited
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 */
12
13#if defined(CONFIG_SERIAL_ST_ASC_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
14#define SUPPORT_SYSRQ
15#endif
16
17#include <linux/module.h>
18#include <linux/serial.h>
19#include <linux/console.h>
20#include <linux/sysrq.h>
21#include <linux/platform_device.h>
22#include <linux/io.h>
23#include <linux/irq.h>
24#include <linux/tty.h>
25#include <linux/tty_flip.h>
26#include <linux/delay.h>
27#include <linux/spinlock.h>
28#include <linux/pm_runtime.h>
29#include <linux/of.h>
30#include <linux/of_platform.h>
31#include <linux/serial_core.h>
32#include <linux/clk.h>
33
34#define DRIVER_NAME "st-asc"
35#define ASC_SERIAL_NAME "ttyAS"
36#define ASC_FIFO_SIZE 16
37#define ASC_MAX_PORTS 8
38
39struct asc_port {
40 struct uart_port port;
41 struct clk *clk;
42 unsigned int hw_flow_control:1;
43 unsigned int force_m1:1;
44};
45
46static struct asc_port asc_ports[ASC_MAX_PORTS];
47static struct uart_driver asc_uart_driver;
48
49/*---- UART Register definitions ------------------------------*/
50
51/* Register offsets */
52
53#define ASC_BAUDRATE 0x00
54#define ASC_TXBUF 0x04
55#define ASC_RXBUF 0x08
56#define ASC_CTL 0x0C
57#define ASC_INTEN 0x10
58#define ASC_STA 0x14
59#define ASC_GUARDTIME 0x18
60#define ASC_TIMEOUT 0x1C
61#define ASC_TXRESET 0x20
62#define ASC_RXRESET 0x24
63#define ASC_RETRIES 0x28
64
65/* ASC_RXBUF */
66#define ASC_RXBUF_PE 0x100
67#define ASC_RXBUF_FE 0x200
68/**
69 * Some of status comes from higher bits of the character and some come from
70 * the status register. Combining both of them in to single status using dummy
71 * bits.
72 */
73#define ASC_RXBUF_DUMMY_RX 0x10000
74#define ASC_RXBUF_DUMMY_BE 0x20000
75#define ASC_RXBUF_DUMMY_OE 0x40000
76
77/* ASC_CTL */
78
79#define ASC_CTL_MODE_MSK 0x0007
80#define ASC_CTL_MODE_8BIT 0x0001
81#define ASC_CTL_MODE_7BIT_PAR 0x0003
82#define ASC_CTL_MODE_9BIT 0x0004
83#define ASC_CTL_MODE_8BIT_WKUP 0x0005
84#define ASC_CTL_MODE_8BIT_PAR 0x0007
85#define ASC_CTL_STOP_MSK 0x0018
86#define ASC_CTL_STOP_HALFBIT 0x0000
87#define ASC_CTL_STOP_1BIT 0x0008
88#define ASC_CTL_STOP_1_HALFBIT 0x0010
89#define ASC_CTL_STOP_2BIT 0x0018
90#define ASC_CTL_PARITYODD 0x0020
91#define ASC_CTL_LOOPBACK 0x0040
92#define ASC_CTL_RUN 0x0080
93#define ASC_CTL_RXENABLE 0x0100
94#define ASC_CTL_SCENABLE 0x0200
95#define ASC_CTL_FIFOENABLE 0x0400
96#define ASC_CTL_CTSENABLE 0x0800
97#define ASC_CTL_BAUDMODE 0x1000
98
99/* ASC_GUARDTIME */
100
101#define ASC_GUARDTIME_MSK 0x00FF
102
103/* ASC_INTEN */
104
105#define ASC_INTEN_RBE 0x0001
106#define ASC_INTEN_TE 0x0002
107#define ASC_INTEN_THE 0x0004
108#define ASC_INTEN_PE 0x0008
109#define ASC_INTEN_FE 0x0010
110#define ASC_INTEN_OE 0x0020
111#define ASC_INTEN_TNE 0x0040
112#define ASC_INTEN_TOI 0x0080
113#define ASC_INTEN_RHF 0x0100
114
115/* ASC_RETRIES */
116
117#define ASC_RETRIES_MSK 0x00FF
118
119/* ASC_RXBUF */
120
121#define ASC_RXBUF_MSK 0x03FF
122
123/* ASC_STA */
124
125#define ASC_STA_RBF 0x0001
126#define ASC_STA_TE 0x0002
127#define ASC_STA_THE 0x0004
128#define ASC_STA_PE 0x0008
129#define ASC_STA_FE 0x0010
130#define ASC_STA_OE 0x0020
131#define ASC_STA_TNE 0x0040
132#define ASC_STA_TOI 0x0080
133#define ASC_STA_RHF 0x0100
134#define ASC_STA_TF 0x0200
135#define ASC_STA_NKD 0x0400
136
137/* ASC_TIMEOUT */
138
139#define ASC_TIMEOUT_MSK 0x00FF
140
141/* ASC_TXBUF */
142
143#define ASC_TXBUF_MSK 0x01FF
144
145/*---- Inline function definitions ---------------------------*/
146
147static inline struct asc_port *to_asc_port(struct uart_port *port)
148{
149 return container_of(port, struct asc_port, port);
150}
151
152static inline u32 asc_in(struct uart_port *port, u32 offset)
153{
154 return readl(port->membase + offset);
155}
156
157static inline void asc_out(struct uart_port *port, u32 offset, u32 value)
158{
159 writel(value, port->membase + offset);
160}
161
162/*
163 * Some simple utility functions to enable and disable interrupts.
164 * Note that these need to be called with interrupts disabled.
165 */
166static inline void asc_disable_tx_interrupts(struct uart_port *port)
167{
168 u32 intenable = asc_in(port, ASC_INTEN) & ~ASC_INTEN_THE;
169 asc_out(port, ASC_INTEN, intenable);
170 (void)asc_in(port, ASC_INTEN); /* Defeat bus write posting */
171}
172
173static inline void asc_enable_tx_interrupts(struct uart_port *port)
174{
175 u32 intenable = asc_in(port, ASC_INTEN) | ASC_INTEN_THE;
176 asc_out(port, ASC_INTEN, intenable);
177}
178
179static inline void asc_disable_rx_interrupts(struct uart_port *port)
180{
181 u32 intenable = asc_in(port, ASC_INTEN) & ~ASC_INTEN_RBE;
182 asc_out(port, ASC_INTEN, intenable);
183 (void)asc_in(port, ASC_INTEN); /* Defeat bus write posting */
184}
185
186static inline void asc_enable_rx_interrupts(struct uart_port *port)
187{
188 u32 intenable = asc_in(port, ASC_INTEN) | ASC_INTEN_RBE;
189 asc_out(port, ASC_INTEN, intenable);
190}
191
192static inline u32 asc_txfifo_is_empty(struct uart_port *port)
193{
194 return asc_in(port, ASC_STA) & ASC_STA_TE;
195}
196
197static inline int asc_txfifo_is_full(struct uart_port *port)
198{
199 return asc_in(port, ASC_STA) & ASC_STA_TF;
200}
201
202static inline const char *asc_port_name(struct uart_port *port)
203{
204 return to_platform_device(port->dev)->name;
205}
206
207/*----------------------------------------------------------------------*/
208
209/*
210 * This section contains code to support the use of the ASC as a
211 * generic serial port.
212 */
213
214static inline unsigned asc_hw_txroom(struct uart_port *port)
215{
216 u32 status = asc_in(port, ASC_STA);
217
218 if (status & ASC_STA_THE)
219 return port->fifosize / 2;
220 else if (!(status & ASC_STA_TF))
221 return 1;
222
223 return 0;
224}
225
226/*
227 * Start transmitting chars.
228 * This is called from both interrupt and task level.
229 * Either way interrupts are disabled.
230 */
231static void asc_transmit_chars(struct uart_port *port)
232{
233 struct circ_buf *xmit = &port->state->xmit;
234 int txroom;
235 unsigned char c;
236
237 txroom = asc_hw_txroom(port);
238
239 if ((txroom != 0) && port->x_char) {
240 c = port->x_char;
241 port->x_char = 0;
242 asc_out(port, ASC_TXBUF, c);
243 port->icount.tx++;
244 txroom = asc_hw_txroom(port);
245 }
246
247 if (uart_tx_stopped(port)) {
248 /*
249 * We should try and stop the hardware here, but I
250 * don't think the ASC has any way to do that.
251 */
252 asc_disable_tx_interrupts(port);
253 return;
254 }
255
256 if (uart_circ_empty(xmit)) {
257 asc_disable_tx_interrupts(port);
258 return;
259 }
260
261 if (txroom == 0)
262 return;
263
264 do {
265 c = xmit->buf[xmit->tail];
266 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
267 asc_out(port, ASC_TXBUF, c);
268 port->icount.tx++;
269 txroom--;
270 } while ((txroom > 0) && (!uart_circ_empty(xmit)));
271
272 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
273 uart_write_wakeup(port);
274
275 if (uart_circ_empty(xmit))
276 asc_disable_tx_interrupts(port);
277}
278
279static void asc_receive_chars(struct uart_port *port)
280{
281 struct tty_port *tport = &port->state->port;
282 unsigned long status;
283 unsigned long c = 0;
284 char flag;
285
286 if (port->irq_wake)
287 pm_wakeup_event(tport->tty->dev, 0);
288
289 while ((status = asc_in(port, ASC_STA)) & ASC_STA_RBF) {
290 c = asc_in(port, ASC_RXBUF) | ASC_RXBUF_DUMMY_RX;
291 flag = TTY_NORMAL;
292 port->icount.rx++;
293
294 if ((c & (ASC_RXBUF_FE | ASC_RXBUF_PE)) ||
295 status & ASC_STA_OE) {
296
297 if (c & ASC_RXBUF_FE) {
298 if (c == ASC_RXBUF_FE) {
299 port->icount.brk++;
300 if (uart_handle_break(port))
301 continue;
302 c |= ASC_RXBUF_DUMMY_BE;
303 } else {
304 port->icount.frame++;
305 }
306 } else if (c & ASC_RXBUF_PE) {
307 port->icount.parity++;
308 }
309 /*
310 * Reading any data from the RX FIFO clears the
311 * overflow error condition.
312 */
313 if (status & ASC_STA_OE) {
314 port->icount.overrun++;
315 c |= ASC_RXBUF_DUMMY_OE;
316 }
317
318 c &= port->read_status_mask;
319
320 if (c & ASC_RXBUF_DUMMY_BE)
321 flag = TTY_BREAK;
322 else if (c & ASC_RXBUF_PE)
323 flag = TTY_PARITY;
324 else if (c & ASC_RXBUF_FE)
325 flag = TTY_FRAME;
326 }
327
328 if (uart_handle_sysrq_char(port, c))
329 continue;
330
331 uart_insert_char(port, c, ASC_RXBUF_DUMMY_OE, c & 0xff, flag);
332 }
333
334 /* Tell the rest of the system the news. New characters! */
335 tty_flip_buffer_push(tport);
336}
337
338static irqreturn_t asc_interrupt(int irq, void *ptr)
339{
340 struct uart_port *port = ptr;
341 u32 status;
342
343 spin_lock(&port->lock);
344
345 status = asc_in(port, ASC_STA);
346
347 if (status & ASC_STA_RBF) {
348 /* Receive FIFO not empty */
349 asc_receive_chars(port);
350 }
351
352 if ((status & ASC_STA_THE) &&
353 (asc_in(port, ASC_INTEN) & ASC_INTEN_THE)) {
354 /* Transmitter FIFO at least half empty */
355 asc_transmit_chars(port);
356 }
357
358 spin_unlock(&port->lock);
359
360 return IRQ_HANDLED;
361}
362
363/*----------------------------------------------------------------------*/
364
365/*
366 * UART Functions
367 */
368
369static unsigned int asc_tx_empty(struct uart_port *port)
370{
371 return asc_txfifo_is_empty(port) ? TIOCSER_TEMT : 0;
372}
373
374static void asc_set_mctrl(struct uart_port *port, unsigned int mctrl)
375{
376 /*
377 * This routine is used for seting signals of: DTR, DCD, CTS/RTS
378 * We use ASC's hardware for CTS/RTS, so don't need any for that.
379 * Some boards have DTR and DCD implemented using PIO pins,
380 * code to do this should be hooked in here.
381 */
382}
383
384static unsigned int asc_get_mctrl(struct uart_port *port)
385{
386 /*
387 * This routine is used for geting signals of: DTR, DCD, DSR, RI,
388 * and CTS/RTS
389 */
390 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
391}
392
393/* There are probably characters waiting to be transmitted. */
394static void asc_start_tx(struct uart_port *port)
395{
396 struct circ_buf *xmit = &port->state->xmit;
397
398 if (!uart_circ_empty(xmit))
399 asc_enable_tx_interrupts(port);
400}
401
402/* Transmit stop */
403static void asc_stop_tx(struct uart_port *port)
404{
405 asc_disable_tx_interrupts(port);
406}
407
408/* Receive stop */
409static void asc_stop_rx(struct uart_port *port)
410{
411 asc_disable_rx_interrupts(port);
412}
413
414/* Force modem status interrupts on */
415static void asc_enable_ms(struct uart_port *port)
416{
417 /* Nothing here yet .. */
418}
419
420/* Handle breaks - ignored by us */
421static void asc_break_ctl(struct uart_port *port, int break_state)
422{
423 /* Nothing here yet .. */
424}
425
426/*
427 * Enable port for reception.
428 */
429static int asc_startup(struct uart_port *port)
430{
431 if (request_irq(port->irq, asc_interrupt, IRQF_NO_SUSPEND,
432 asc_port_name(port), port)) {
433 dev_err(port->dev, "cannot allocate irq.\n");
434 return -ENODEV;
435 }
436
437 asc_transmit_chars(port);
438 asc_enable_rx_interrupts(port);
439
440 return 0;
441}
442
443static void asc_shutdown(struct uart_port *port)
444{
445 asc_disable_tx_interrupts(port);
446 asc_disable_rx_interrupts(port);
447 free_irq(port->irq, port);
448}
449
450static void asc_pm(struct uart_port *port, unsigned int state,
451 unsigned int oldstate)
452{
453 struct asc_port *ascport = to_asc_port(port);
454 unsigned long flags = 0;
455 u32 ctl;
456
457 switch (state) {
458 case UART_PM_STATE_ON:
459 clk_prepare_enable(ascport->clk);
460 break;
461 case UART_PM_STATE_OFF:
462 /*
463 * Disable the ASC baud rate generator, which is as close as
464 * we can come to turning it off. Note this is not called with
465 * the port spinlock held.
466 */
467 spin_lock_irqsave(&port->lock, flags);
468 ctl = asc_in(port, ASC_CTL) & ~ASC_CTL_RUN;
469 asc_out(port, ASC_CTL, ctl);
470 spin_unlock_irqrestore(&port->lock, flags);
471 clk_disable_unprepare(ascport->clk);
472 break;
473 }
474}
475
476static void asc_set_termios(struct uart_port *port, struct ktermios *termios,
477 struct ktermios *old)
478{
479 struct asc_port *ascport = to_asc_port(port);
480 unsigned int baud;
481 u32 ctrl_val;
482 tcflag_t cflag;
483 unsigned long flags;
484
485 /* Update termios to reflect hardware capabilities */
486 termios->c_cflag &= ~(CMSPAR |
487 (ascport->hw_flow_control ? 0 : CRTSCTS));
488
489 port->uartclk = clk_get_rate(ascport->clk);
490
491 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
492 cflag = termios->c_cflag;
493
494 spin_lock_irqsave(&port->lock, flags);
495
496 /* read control register */
497 ctrl_val = asc_in(port, ASC_CTL);
498
499 /* stop serial port and reset value */
500 asc_out(port, ASC_CTL, (ctrl_val & ~ASC_CTL_RUN));
501 ctrl_val = ASC_CTL_RXENABLE | ASC_CTL_FIFOENABLE;
502
503 /* reset fifo rx & tx */
504 asc_out(port, ASC_TXRESET, 1);
505 asc_out(port, ASC_RXRESET, 1);
506
507 /* set character length */
508 if ((cflag & CSIZE) == CS7) {
509 ctrl_val |= ASC_CTL_MODE_7BIT_PAR;
510 } else {
511 ctrl_val |= (cflag & PARENB) ? ASC_CTL_MODE_8BIT_PAR :
512 ASC_CTL_MODE_8BIT;
513 }
514
515 /* set stop bit */
516 ctrl_val |= (cflag & CSTOPB) ? ASC_CTL_STOP_2BIT : ASC_CTL_STOP_1BIT;
517
518 /* odd parity */
519 if (cflag & PARODD)
520 ctrl_val |= ASC_CTL_PARITYODD;
521
522 /* hardware flow control */
523 if ((cflag & CRTSCTS))
524 ctrl_val |= ASC_CTL_CTSENABLE;
525
526 if ((baud < 19200) && !ascport->force_m1) {
527 asc_out(port, ASC_BAUDRATE, (port->uartclk / (16 * baud)));
528 } else {
529 /*
530 * MODE 1: recommended for high bit rates (above 19.2K)
531 *
532 * baudrate * 16 * 2^16
533 * ASCBaudRate = ------------------------
534 * inputclock
535 *
536 * However to keep the maths inside 32bits we divide top and
537 * bottom by 64. The +1 is to avoid a divide by zero if the
538 * input clock rate is something unexpected.
539 */
540 u32 counter = (baud * 16384) / ((port->uartclk / 64) + 1);
541 asc_out(port, ASC_BAUDRATE, counter);
542 ctrl_val |= ASC_CTL_BAUDMODE;
543 }
544
545 uart_update_timeout(port, cflag, baud);
546
547 ascport->port.read_status_mask = ASC_RXBUF_DUMMY_OE;
548 if (termios->c_iflag & INPCK)
549 ascport->port.read_status_mask |= ASC_RXBUF_FE | ASC_RXBUF_PE;
550 if (termios->c_iflag & (BRKINT | PARMRK))
551 ascport->port.read_status_mask |= ASC_RXBUF_DUMMY_BE;
552
553 /*
554 * Characters to ignore
555 */
556 ascport->port.ignore_status_mask = 0;
557 if (termios->c_iflag & IGNPAR)
558 ascport->port.ignore_status_mask |= ASC_RXBUF_FE | ASC_RXBUF_PE;
559 if (termios->c_iflag & IGNBRK) {
560 ascport->port.ignore_status_mask |= ASC_RXBUF_DUMMY_BE;
561 /*
562 * If we're ignoring parity and break indicators,
563 * ignore overruns too (for real raw support).
564 */
565 if (termios->c_iflag & IGNPAR)
566 ascport->port.ignore_status_mask |= ASC_RXBUF_DUMMY_OE;
567 }
568
569 /*
570 * Ignore all characters if CREAD is not set.
571 */
572 if (!(termios->c_cflag & CREAD))
573 ascport->port.ignore_status_mask |= ASC_RXBUF_DUMMY_RX;
574
575 /* Set the timeout */
576 asc_out(port, ASC_TIMEOUT, 20);
577
578 /* write final value and enable port */
579 asc_out(port, ASC_CTL, (ctrl_val | ASC_CTL_RUN));
580
581 spin_unlock_irqrestore(&port->lock, flags);
582}
583
584static const char *asc_type(struct uart_port *port)
585{
586 return (port->type == PORT_ASC) ? DRIVER_NAME : NULL;
587}
588
589static void asc_release_port(struct uart_port *port)
590{
591}
592
593static int asc_request_port(struct uart_port *port)
594{
595 return 0;
596}
597
598/*
599 * Called when the port is opened, and UPF_BOOT_AUTOCONF flag is set
600 * Set type field if successful
601 */
602static void asc_config_port(struct uart_port *port, int flags)
603{
604 if ((flags & UART_CONFIG_TYPE))
605 port->type = PORT_ASC;
606}
607
608static int
609asc_verify_port(struct uart_port *port, struct serial_struct *ser)
610{
611 /* No user changeable parameters */
612 return -EINVAL;
613}
614
615#ifdef CONFIG_CONSOLE_POLL
616/*
617 * Console polling routines for writing and reading from the uart while
618 * in an interrupt or debug context (i.e. kgdb).
619 */
620
621static int asc_get_poll_char(struct uart_port *port)
622{
623 if (!(asc_in(port, ASC_STA) & ASC_STA_RBF))
624 return NO_POLL_CHAR;
625
626 return asc_in(port, ASC_RXBUF);
627}
628
629static void asc_put_poll_char(struct uart_port *port, unsigned char c)
630{
631 while (asc_txfifo_is_full(port))
632 cpu_relax();
633 asc_out(port, ASC_TXBUF, c);
634}
635
636#endif /* CONFIG_CONSOLE_POLL */
637
638/*---------------------------------------------------------------------*/
639
640static struct uart_ops asc_uart_ops = {
641 .tx_empty = asc_tx_empty,
642 .set_mctrl = asc_set_mctrl,
643 .get_mctrl = asc_get_mctrl,
644 .start_tx = asc_start_tx,
645 .stop_tx = asc_stop_tx,
646 .stop_rx = asc_stop_rx,
647 .enable_ms = asc_enable_ms,
648 .break_ctl = asc_break_ctl,
649 .startup = asc_startup,
650 .shutdown = asc_shutdown,
651 .set_termios = asc_set_termios,
652 .type = asc_type,
653 .release_port = asc_release_port,
654 .request_port = asc_request_port,
655 .config_port = asc_config_port,
656 .verify_port = asc_verify_port,
657 .pm = asc_pm,
658#ifdef CONFIG_CONSOLE_POLL
659 .poll_get_char = asc_get_poll_char,
660 .poll_put_char = asc_put_poll_char,
661#endif /* CONFIG_CONSOLE_POLL */
662};
663
664static int asc_init_port(struct asc_port *ascport,
665 struct platform_device *pdev)
666{
667 struct uart_port *port = &ascport->port;
668 struct resource *res;
669
670 port->iotype = UPIO_MEM;
671 port->flags = UPF_BOOT_AUTOCONF;
672 port->ops = &asc_uart_ops;
673 port->fifosize = ASC_FIFO_SIZE;
674 port->dev = &pdev->dev;
675 port->irq = platform_get_irq(pdev, 0);
676
677 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
678 port->membase = devm_ioremap_resource(&pdev->dev, res);
679 if (IS_ERR(port->membase))
680 return PTR_ERR(port->membase);
681 port->mapbase = res->start;
682
683 spin_lock_init(&port->lock);
684
685 ascport->clk = devm_clk_get(&pdev->dev, NULL);
686
687 if (WARN_ON(IS_ERR(ascport->clk)))
688 return -EINVAL;
689 /* ensure that clk rate is correct by enabling the clk */
690 clk_prepare_enable(ascport->clk);
691 ascport->port.uartclk = clk_get_rate(ascport->clk);
692 WARN_ON(ascport->port.uartclk == 0);
693 clk_disable_unprepare(ascport->clk);
694
695 return 0;
696}
697
698static struct asc_port *asc_of_get_asc_port(struct platform_device *pdev)
699{
700 struct device_node *np = pdev->dev.of_node;
701 int id;
702
703 if (!np)
704 return NULL;
705
706 id = of_alias_get_id(np, ASC_SERIAL_NAME);
707
708 if (id < 0)
709 id = 0;
710
711 if (WARN_ON(id >= ASC_MAX_PORTS))
712 return NULL;
713
714 asc_ports[id].hw_flow_control = of_property_read_bool(np,
715 "st,hw-flow-control");
716 asc_ports[id].force_m1 = of_property_read_bool(np, "st,force_m1");
717 asc_ports[id].port.line = id;
718 return &asc_ports[id];
719}
720
721#ifdef CONFIG_OF
722static struct of_device_id asc_match[] = {
723 { .compatible = "st,asc", },
724 {},
725};
726
727MODULE_DEVICE_TABLE(of, asc_match);
728#endif
729
730static int asc_serial_probe(struct platform_device *pdev)
731{
732 int ret;
733 struct asc_port *ascport;
734
735 ascport = asc_of_get_asc_port(pdev);
736 if (!ascport)
737 return -ENODEV;
738
739 ret = asc_init_port(ascport, pdev);
740 if (ret)
741 return ret;
742
743 ret = uart_add_one_port(&asc_uart_driver, &ascport->port);
744 if (ret)
745 return ret;
746
747 platform_set_drvdata(pdev, &ascport->port);
748
749 return 0;
750}
751
752static int asc_serial_remove(struct platform_device *pdev)
753{
754 struct uart_port *port = platform_get_drvdata(pdev);
755
756 return uart_remove_one_port(&asc_uart_driver, port);
757}
758
759#ifdef CONFIG_PM_SLEEP
760static int asc_serial_suspend(struct device *dev)
761{
762 struct platform_device *pdev = to_platform_device(dev);
763 struct uart_port *port = platform_get_drvdata(pdev);
764
765 return uart_suspend_port(&asc_uart_driver, port);
766}
767
768static int asc_serial_resume(struct device *dev)
769{
770 struct platform_device *pdev = to_platform_device(dev);
771 struct uart_port *port = platform_get_drvdata(pdev);
772
773 return uart_resume_port(&asc_uart_driver, port);
774}
775
776#endif /* CONFIG_PM_SLEEP */
777
778/*----------------------------------------------------------------------*/
779
780#ifdef CONFIG_SERIAL_ST_ASC_CONSOLE
781static void asc_console_putchar(struct uart_port *port, int ch)
782{
783 unsigned int timeout = 1000000;
784
785 /* Wait for upto 1 second in case flow control is stopping us. */
786 while (--timeout && asc_txfifo_is_full(port))
787 udelay(1);
788
789 asc_out(port, ASC_TXBUF, ch);
790}
791
792/*
793 * Print a string to the serial port trying not to disturb
794 * any possible real use of the port...
795 */
796
797static void asc_console_write(struct console *co, const char *s, unsigned count)
798{
799 struct uart_port *port = &asc_ports[co->index].port;
800 unsigned long flags;
801 unsigned long timeout = 1000000;
802 int locked = 1;
803 u32 intenable;
804
805 local_irq_save(flags);
806 if (port->sysrq)
807 locked = 0; /* asc_interrupt has already claimed the lock */
808 else if (oops_in_progress)
809 locked = spin_trylock(&port->lock);
810 else
811 spin_lock(&port->lock);
812
813 /*
814 * Disable interrupts so we don't get the IRQ line bouncing
815 * up and down while interrupts are disabled.
816 */
817 intenable = asc_in(port, ASC_INTEN);
818 asc_out(port, ASC_INTEN, 0);
819 (void)asc_in(port, ASC_INTEN); /* Defeat bus write posting */
820
821 uart_console_write(port, s, count, asc_console_putchar);
822
823 while (--timeout && !asc_txfifo_is_empty(port))
824 udelay(1);
825
826 asc_out(port, ASC_INTEN, intenable);
827
828 if (locked)
829 spin_unlock(&port->lock);
830 local_irq_restore(flags);
831}
832
833static int asc_console_setup(struct console *co, char *options)
834{
835 struct asc_port *ascport;
836 int baud = 9600;
837 int bits = 8;
838 int parity = 'n';
839 int flow = 'n';
840
841 if (co->index >= ASC_MAX_PORTS)
842 return -ENODEV;
843
844 ascport = &asc_ports[co->index];
845
846 /*
847 * This driver does not support early console initialization
848 * (use ARM early printk support instead), so we only expect
849 * this to be called during the uart port registration when the
850 * driver gets probed and the port should be mapped at that point.
851 */
852 BUG_ON(ascport->port.mapbase == 0 || ascport->port.membase == NULL);
853
854 if (options)
855 uart_parse_options(options, &baud, &parity, &bits, &flow);
856
857 return uart_set_options(&ascport->port, co, baud, parity, bits, flow);
858}
859
860static struct console asc_console = {
861 .name = ASC_SERIAL_NAME,
862 .device = uart_console_device,
863 .write = asc_console_write,
864 .setup = asc_console_setup,
865 .flags = CON_PRINTBUFFER,
866 .index = -1,
867 .data = &asc_uart_driver,
868};
869
870#define ASC_SERIAL_CONSOLE (&asc_console)
871
872#else
873#define ASC_SERIAL_CONSOLE NULL
874#endif /* CONFIG_SERIAL_ST_ASC_CONSOLE */
875
876static struct uart_driver asc_uart_driver = {
877 .owner = THIS_MODULE,
878 .driver_name = DRIVER_NAME,
879 .dev_name = ASC_SERIAL_NAME,
880 .major = 0,
881 .minor = 0,
882 .nr = ASC_MAX_PORTS,
883 .cons = ASC_SERIAL_CONSOLE,
884};
885
886static const struct dev_pm_ops asc_serial_pm_ops = {
887 SET_SYSTEM_SLEEP_PM_OPS(asc_serial_suspend, asc_serial_resume)
888};
889
890static struct platform_driver asc_serial_driver = {
891 .probe = asc_serial_probe,
892 .remove = asc_serial_remove,
893 .driver = {
894 .name = DRIVER_NAME,
895 .pm = &asc_serial_pm_ops,
896 .owner = THIS_MODULE,
897 .of_match_table = of_match_ptr(asc_match),
898 },
899};
900
901static int __init asc_init(void)
902{
903 int ret;
904 static char banner[] __initdata =
905 KERN_INFO "STMicroelectronics ASC driver initialized\n";
906
907 printk(banner);
908
909 ret = uart_register_driver(&asc_uart_driver);
910 if (ret)
911 return ret;
912
913 ret = platform_driver_register(&asc_serial_driver);
914 if (ret)
915 uart_unregister_driver(&asc_uart_driver);
916
917 return ret;
918}
919
920static void __exit asc_exit(void)
921{
922 platform_driver_unregister(&asc_serial_driver);
923 uart_unregister_driver(&asc_uart_driver);
924}
925
926module_init(asc_init);
927module_exit(asc_exit);
928
929MODULE_ALIAS("platform:" DRIVER_NAME);
930MODULE_AUTHOR("STMicroelectronics (R&D) Limited");
931MODULE_DESCRIPTION("STMicroelectronics ASC serial port driver");
932MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/timbuart.c b/drivers/tty/serial/timbuart.c
index 6818410a2bea..f87097acd8ab 100644
--- a/drivers/tty/serial/timbuart.c
+++ b/drivers/tty/serial/timbuart.c
@@ -162,7 +162,7 @@ static void timbuart_handle_tx_port(struct uart_port *port, u32 isr, u32 *ier)
162 dev_dbg(port->dev, "%s - leaving\n", __func__); 162 dev_dbg(port->dev, "%s - leaving\n", __func__);
163} 163}
164 164
165void timbuart_handle_rx_port(struct uart_port *port, u32 isr, u32 *ier) 165static void timbuart_handle_rx_port(struct uart_port *port, u32 isr, u32 *ier)
166{ 166{
167 if (isr & RXFLAGS) { 167 if (isr & RXFLAGS) {
168 /* Some RX status is set */ 168 /* Some RX status is set */
@@ -184,7 +184,7 @@ void timbuart_handle_rx_port(struct uart_port *port, u32 isr, u32 *ier)
184 dev_dbg(port->dev, "%s - leaving\n", __func__); 184 dev_dbg(port->dev, "%s - leaving\n", __func__);
185} 185}
186 186
187void timbuart_tasklet(unsigned long arg) 187static void timbuart_tasklet(unsigned long arg)
188{ 188{
189 struct timbuart_port *uart = (struct timbuart_port *)arg; 189 struct timbuart_port *uart = (struct timbuart_port *)arg;
190 u32 isr, ier = 0; 190 u32 isr, ier = 0;
diff --git a/drivers/tty/serial/vr41xx_siu.c b/drivers/tty/serial/vr41xx_siu.c
index f655997f44af..a63c14bc9a24 100644
--- a/drivers/tty/serial/vr41xx_siu.c
+++ b/drivers/tty/serial/vr41xx_siu.c
@@ -705,7 +705,7 @@ static int siu_init_ports(struct platform_device *pdev)
705{ 705{
706 struct uart_port *port; 706 struct uart_port *port;
707 struct resource *res; 707 struct resource *res;
708 int *type = pdev->dev.platform_data; 708 int *type = dev_get_platdata(&pdev->dev);
709 int i; 709 int i;
710 710
711 if (!type) 711 if (!type)
diff --git a/drivers/tty/serial/vt8500_serial.c b/drivers/tty/serial/vt8500_serial.c
index 48af43de3467..93b697a0de65 100644
--- a/drivers/tty/serial/vt8500_serial.c
+++ b/drivers/tty/serial/vt8500_serial.c
@@ -170,7 +170,9 @@ static void handle_rx(struct uart_port *port)
170 tty_insert_flip_char(tport, c, flag); 170 tty_insert_flip_char(tport, c, flag);
171 } 171 }
172 172
173 spin_unlock(&port->lock);
173 tty_flip_buffer_push(tport); 174 tty_flip_buffer_push(tport);
175 spin_lock(&port->lock);
174} 176}
175 177
176static void handle_tx(struct uart_port *port) 178static void handle_tx(struct uart_port *port)
@@ -630,7 +632,6 @@ static int vt8500_serial_remove(struct platform_device *pdev)
630{ 632{
631 struct vt8500_port *vt8500_port = platform_get_drvdata(pdev); 633 struct vt8500_port *vt8500_port = platform_get_drvdata(pdev);
632 634
633 platform_set_drvdata(pdev, NULL);
634 clk_disable_unprepare(vt8500_port->clk); 635 clk_disable_unprepare(vt8500_port->clk);
635 uart_remove_one_port(&vt8500_uart_driver, &vt8500_port->uart); 636 uart_remove_one_port(&vt8500_uart_driver, &vt8500_port->uart);
636 637
diff --git a/drivers/tty/synclink.c b/drivers/tty/synclink.c
index 8eaf1ab8addb..e1ce141bad5e 100644
--- a/drivers/tty/synclink.c
+++ b/drivers/tty/synclink.c
@@ -577,22 +577,22 @@ struct mgsl_struct {
577 577
578#define SICR_RXC_ACTIVE BIT15 578#define SICR_RXC_ACTIVE BIT15
579#define SICR_RXC_INACTIVE BIT14 579#define SICR_RXC_INACTIVE BIT14
580#define SICR_RXC (BIT15+BIT14) 580#define SICR_RXC (BIT15|BIT14)
581#define SICR_TXC_ACTIVE BIT13 581#define SICR_TXC_ACTIVE BIT13
582#define SICR_TXC_INACTIVE BIT12 582#define SICR_TXC_INACTIVE BIT12
583#define SICR_TXC (BIT13+BIT12) 583#define SICR_TXC (BIT13|BIT12)
584#define SICR_RI_ACTIVE BIT11 584#define SICR_RI_ACTIVE BIT11
585#define SICR_RI_INACTIVE BIT10 585#define SICR_RI_INACTIVE BIT10
586#define SICR_RI (BIT11+BIT10) 586#define SICR_RI (BIT11|BIT10)
587#define SICR_DSR_ACTIVE BIT9 587#define SICR_DSR_ACTIVE BIT9
588#define SICR_DSR_INACTIVE BIT8 588#define SICR_DSR_INACTIVE BIT8
589#define SICR_DSR (BIT9+BIT8) 589#define SICR_DSR (BIT9|BIT8)
590#define SICR_DCD_ACTIVE BIT7 590#define SICR_DCD_ACTIVE BIT7
591#define SICR_DCD_INACTIVE BIT6 591#define SICR_DCD_INACTIVE BIT6
592#define SICR_DCD (BIT7+BIT6) 592#define SICR_DCD (BIT7|BIT6)
593#define SICR_CTS_ACTIVE BIT5 593#define SICR_CTS_ACTIVE BIT5
594#define SICR_CTS_INACTIVE BIT4 594#define SICR_CTS_INACTIVE BIT4
595#define SICR_CTS (BIT5+BIT4) 595#define SICR_CTS (BIT5|BIT4)
596#define SICR_RCC_UNDERFLOW BIT3 596#define SICR_RCC_UNDERFLOW BIT3
597#define SICR_DPLL_NO_SYNC BIT2 597#define SICR_DPLL_NO_SYNC BIT2
598#define SICR_BRG1_ZERO BIT1 598#define SICR_BRG1_ZERO BIT1
@@ -1161,7 +1161,7 @@ static void mgsl_isr_receive_status( struct mgsl_struct *info )
1161{ 1161{
1162 u16 status = usc_InReg( info, RCSR ); 1162 u16 status = usc_InReg( info, RCSR );
1163 1163
1164 if ( debug_level >= DEBUG_LEVEL_ISR ) 1164 if ( debug_level >= DEBUG_LEVEL_ISR )
1165 printk("%s(%d):mgsl_isr_receive_status status=%04X\n", 1165 printk("%s(%d):mgsl_isr_receive_status status=%04X\n",
1166 __FILE__,__LINE__,status); 1166 __FILE__,__LINE__,status);
1167 1167
@@ -1181,7 +1181,7 @@ static void mgsl_isr_receive_status( struct mgsl_struct *info )
1181 (usc_InReg(info, RICR) & ~RXSTATUS_ABORT_RECEIVED)); 1181 (usc_InReg(info, RICR) & ~RXSTATUS_ABORT_RECEIVED));
1182 } 1182 }
1183 1183
1184 if (status & (RXSTATUS_EXITED_HUNT + RXSTATUS_IDLE_RECEIVED)) { 1184 if (status & (RXSTATUS_EXITED_HUNT | RXSTATUS_IDLE_RECEIVED)) {
1185 if (status & RXSTATUS_EXITED_HUNT) 1185 if (status & RXSTATUS_EXITED_HUNT)
1186 info->icount.exithunt++; 1186 info->icount.exithunt++;
1187 if (status & RXSTATUS_IDLE_RECEIVED) 1187 if (status & RXSTATUS_IDLE_RECEIVED)
@@ -1463,21 +1463,21 @@ static void mgsl_isr_receive_data( struct mgsl_struct *info )
1463 1463
1464 /* get the status of the received byte */ 1464 /* get the status of the received byte */
1465 status = usc_InReg(info, RCSR); 1465 status = usc_InReg(info, RCSR);
1466 if ( status & (RXSTATUS_FRAMING_ERROR + RXSTATUS_PARITY_ERROR + 1466 if ( status & (RXSTATUS_FRAMING_ERROR | RXSTATUS_PARITY_ERROR |
1467 RXSTATUS_OVERRUN + RXSTATUS_BREAK_RECEIVED) ) 1467 RXSTATUS_OVERRUN | RXSTATUS_BREAK_RECEIVED) )
1468 usc_UnlatchRxstatusBits(info,RXSTATUS_ALL); 1468 usc_UnlatchRxstatusBits(info,RXSTATUS_ALL);
1469 1469
1470 icount->rx++; 1470 icount->rx++;
1471 1471
1472 flag = 0; 1472 flag = 0;
1473 if ( status & (RXSTATUS_FRAMING_ERROR + RXSTATUS_PARITY_ERROR + 1473 if ( status & (RXSTATUS_FRAMING_ERROR | RXSTATUS_PARITY_ERROR |
1474 RXSTATUS_OVERRUN + RXSTATUS_BREAK_RECEIVED) ) { 1474 RXSTATUS_OVERRUN | RXSTATUS_BREAK_RECEIVED) ) {
1475 printk("rxerr=%04X\n",status); 1475 printk("rxerr=%04X\n",status);
1476 /* update error statistics */ 1476 /* update error statistics */
1477 if ( status & RXSTATUS_BREAK_RECEIVED ) { 1477 if ( status & RXSTATUS_BREAK_RECEIVED ) {
1478 status &= ~(RXSTATUS_FRAMING_ERROR + RXSTATUS_PARITY_ERROR); 1478 status &= ~(RXSTATUS_FRAMING_ERROR | RXSTATUS_PARITY_ERROR);
1479 icount->brk++; 1479 icount->brk++;
1480 } else if (status & RXSTATUS_PARITY_ERROR) 1480 } else if (status & RXSTATUS_PARITY_ERROR)
1481 icount->parity++; 1481 icount->parity++;
1482 else if (status & RXSTATUS_FRAMING_ERROR) 1482 else if (status & RXSTATUS_FRAMING_ERROR)
1483 icount->frame++; 1483 icount->frame++;
@@ -1488,7 +1488,7 @@ static void mgsl_isr_receive_data( struct mgsl_struct *info )
1488 icount->overrun++; 1488 icount->overrun++;
1489 } 1489 }
1490 1490
1491 /* discard char if tty control flags say so */ 1491 /* discard char if tty control flags say so */
1492 if (status & info->ignore_status_mask) 1492 if (status & info->ignore_status_mask)
1493 continue; 1493 continue;
1494 1494
@@ -1545,8 +1545,8 @@ static void mgsl_isr_misc( struct mgsl_struct *info )
1545 usc_EnableReceiver(info,DISABLE_UNCONDITIONAL); 1545 usc_EnableReceiver(info,DISABLE_UNCONDITIONAL);
1546 usc_DmaCmd(info, DmaCmd_ResetRxChannel); 1546 usc_DmaCmd(info, DmaCmd_ResetRxChannel);
1547 usc_UnlatchRxstatusBits(info, RXSTATUS_ALL); 1547 usc_UnlatchRxstatusBits(info, RXSTATUS_ALL);
1548 usc_ClearIrqPendingBits(info, RECEIVE_DATA + RECEIVE_STATUS); 1548 usc_ClearIrqPendingBits(info, RECEIVE_DATA | RECEIVE_STATUS);
1549 usc_DisableInterrupts(info, RECEIVE_DATA + RECEIVE_STATUS); 1549 usc_DisableInterrupts(info, RECEIVE_DATA | RECEIVE_STATUS);
1550 1550
1551 /* schedule BH handler to restart receiver */ 1551 /* schedule BH handler to restart receiver */
1552 info->pending_bh |= BH_RECEIVE; 1552 info->pending_bh |= BH_RECEIVE;
@@ -1595,7 +1595,7 @@ static void mgsl_isr_receive_dma( struct mgsl_struct *info )
1595 u16 status; 1595 u16 status;
1596 1596
1597 /* clear interrupt pending and IUS bit for Rx DMA IRQ */ 1597 /* clear interrupt pending and IUS bit for Rx DMA IRQ */
1598 usc_OutDmaReg( info, CDIR, BIT9+BIT1 ); 1598 usc_OutDmaReg( info, CDIR, BIT9 | BIT1 );
1599 1599
1600 /* Read the receive DMA status to identify interrupt type. */ 1600 /* Read the receive DMA status to identify interrupt type. */
1601 /* This also clears the status bits. */ 1601 /* This also clears the status bits. */
@@ -1639,7 +1639,7 @@ static void mgsl_isr_transmit_dma( struct mgsl_struct *info )
1639 u16 status; 1639 u16 status;
1640 1640
1641 /* clear interrupt pending and IUS bit for Tx DMA IRQ */ 1641 /* clear interrupt pending and IUS bit for Tx DMA IRQ */
1642 usc_OutDmaReg(info, CDIR, BIT8+BIT0 ); 1642 usc_OutDmaReg(info, CDIR, BIT8 | BIT0 );
1643 1643
1644 /* Read the transmit DMA status to identify interrupt type. */ 1644 /* Read the transmit DMA status to identify interrupt type. */
1645 /* This also clears the status bits. */ 1645 /* This also clears the status bits. */
@@ -1832,8 +1832,8 @@ static void shutdown(struct mgsl_struct * info)
1832 usc_DisableMasterIrqBit(info); 1832 usc_DisableMasterIrqBit(info);
1833 usc_stop_receiver(info); 1833 usc_stop_receiver(info);
1834 usc_stop_transmitter(info); 1834 usc_stop_transmitter(info);
1835 usc_DisableInterrupts(info,RECEIVE_DATA + RECEIVE_STATUS + 1835 usc_DisableInterrupts(info,RECEIVE_DATA | RECEIVE_STATUS |
1836 TRANSMIT_DATA + TRANSMIT_STATUS + IO_PIN + MISC ); 1836 TRANSMIT_DATA | TRANSMIT_STATUS | IO_PIN | MISC );
1837 usc_DisableDmaInterrupts(info,DICR_MASTER + DICR_TRANSMIT + DICR_RECEIVE); 1837 usc_DisableDmaInterrupts(info,DICR_MASTER + DICR_TRANSMIT + DICR_RECEIVE);
1838 1838
1839 /* Disable DMAEN (Port 7, Bit 14) */ 1839 /* Disable DMAEN (Port 7, Bit 14) */
@@ -1886,7 +1886,7 @@ static void mgsl_program_hw(struct mgsl_struct *info)
1886 info->ri_chkcount = 0; 1886 info->ri_chkcount = 0;
1887 info->dsr_chkcount = 0; 1887 info->dsr_chkcount = 0;
1888 1888
1889 usc_EnableStatusIrqs(info,SICR_CTS+SICR_DSR+SICR_DCD+SICR_RI); 1889 usc_EnableStatusIrqs(info,SICR_CTS+SICR_DSR+SICR_DCD+SICR_RI);
1890 usc_EnableInterrupts(info, IO_PIN); 1890 usc_EnableInterrupts(info, IO_PIN);
1891 usc_get_serial_signals(info); 1891 usc_get_serial_signals(info);
1892 1892
@@ -2773,7 +2773,7 @@ static int mgsl_wait_event(struct mgsl_struct * info, int __user * mask_ptr)
2773 if (!waitqueue_active(&info->event_wait_q)) { 2773 if (!waitqueue_active(&info->event_wait_q)) {
2774 /* disable enable exit hunt mode/idle rcvd IRQs */ 2774 /* disable enable exit hunt mode/idle rcvd IRQs */
2775 usc_OutReg(info, RICR, usc_InReg(info,RICR) & 2775 usc_OutReg(info, RICR, usc_InReg(info,RICR) &
2776 ~(RXSTATUS_EXITED_HUNT + RXSTATUS_IDLE_RECEIVED)); 2776 ~(RXSTATUS_EXITED_HUNT | RXSTATUS_IDLE_RECEIVED));
2777 } 2777 }
2778 spin_unlock_irqrestore(&info->irq_spinlock,flags); 2778 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2779 } 2779 }
@@ -3092,7 +3092,7 @@ static void mgsl_close(struct tty_struct *tty, struct file * filp)
3092 printk("%s(%d):mgsl_close(%s) entry, count=%d\n", 3092 printk("%s(%d):mgsl_close(%s) entry, count=%d\n",
3093 __FILE__,__LINE__, info->device_name, info->port.count); 3093 __FILE__,__LINE__, info->device_name, info->port.count);
3094 3094
3095 if (tty_port_close_start(&info->port, tty, filp) == 0) 3095 if (tty_port_close_start(&info->port, tty, filp) == 0)
3096 goto cleanup; 3096 goto cleanup;
3097 3097
3098 mutex_lock(&info->port.mutex); 3098 mutex_lock(&info->port.mutex);
@@ -4297,7 +4297,7 @@ static struct mgsl_struct* mgsl_allocate_device(void)
4297 spin_lock_init(&info->irq_spinlock); 4297 spin_lock_init(&info->irq_spinlock);
4298 spin_lock_init(&info->netlock); 4298 spin_lock_init(&info->netlock);
4299 memcpy(&info->params,&default_params,sizeof(MGSL_PARAMS)); 4299 memcpy(&info->params,&default_params,sizeof(MGSL_PARAMS));
4300 info->idle_mode = HDLC_TXIDLE_FLAGS; 4300 info->idle_mode = HDLC_TXIDLE_FLAGS;
4301 info->num_tx_dma_buffers = 1; 4301 info->num_tx_dma_buffers = 1;
4302 info->num_tx_holding_buffers = 0; 4302 info->num_tx_holding_buffers = 0;
4303 } 4303 }
@@ -4722,7 +4722,7 @@ static void usc_set_sdlc_mode( struct mgsl_struct *info )
4722 else if ( info->params.flags & HDLC_FLAG_UNDERRUN_FLAG ) 4722 else if ( info->params.flags & HDLC_FLAG_UNDERRUN_FLAG )
4723 RegValue |= BIT15; 4723 RegValue |= BIT15;
4724 else if ( info->params.flags & HDLC_FLAG_UNDERRUN_CRC ) 4724 else if ( info->params.flags & HDLC_FLAG_UNDERRUN_CRC )
4725 RegValue |= BIT15 + BIT14; 4725 RegValue |= BIT15 | BIT14;
4726 } 4726 }
4727 4727
4728 if ( info->params.preamble != HDLC_PREAMBLE_PATTERN_NONE ) 4728 if ( info->params.preamble != HDLC_PREAMBLE_PATTERN_NONE )
@@ -4763,11 +4763,11 @@ static void usc_set_sdlc_mode( struct mgsl_struct *info )
4763 switch ( info->params.encoding ) { 4763 switch ( info->params.encoding ) {
4764 case HDLC_ENCODING_NRZB: RegValue |= BIT13; break; 4764 case HDLC_ENCODING_NRZB: RegValue |= BIT13; break;
4765 case HDLC_ENCODING_NRZI_MARK: RegValue |= BIT14; break; 4765 case HDLC_ENCODING_NRZI_MARK: RegValue |= BIT14; break;
4766 case HDLC_ENCODING_NRZI_SPACE: RegValue |= BIT14 + BIT13; break; 4766 case HDLC_ENCODING_NRZI_SPACE: RegValue |= BIT14 | BIT13; break;
4767 case HDLC_ENCODING_BIPHASE_MARK: RegValue |= BIT15; break; 4767 case HDLC_ENCODING_BIPHASE_MARK: RegValue |= BIT15; break;
4768 case HDLC_ENCODING_BIPHASE_SPACE: RegValue |= BIT15 + BIT13; break; 4768 case HDLC_ENCODING_BIPHASE_SPACE: RegValue |= BIT15 | BIT13; break;
4769 case HDLC_ENCODING_BIPHASE_LEVEL: RegValue |= BIT15 + BIT14; break; 4769 case HDLC_ENCODING_BIPHASE_LEVEL: RegValue |= BIT15 | BIT14; break;
4770 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: RegValue |= BIT15 + BIT14 + BIT13; break; 4770 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: RegValue |= BIT15 | BIT14 | BIT13; break;
4771 } 4771 }
4772 4772
4773 if ( (info->params.crc_type & HDLC_CRC_MASK) == HDLC_CRC_16_CCITT ) 4773 if ( (info->params.crc_type & HDLC_CRC_MASK) == HDLC_CRC_16_CCITT )
@@ -4838,15 +4838,15 @@ static void usc_set_sdlc_mode( struct mgsl_struct *info )
4838 switch ( info->params.encoding ) { 4838 switch ( info->params.encoding ) {
4839 case HDLC_ENCODING_NRZB: RegValue |= BIT13; break; 4839 case HDLC_ENCODING_NRZB: RegValue |= BIT13; break;
4840 case HDLC_ENCODING_NRZI_MARK: RegValue |= BIT14; break; 4840 case HDLC_ENCODING_NRZI_MARK: RegValue |= BIT14; break;
4841 case HDLC_ENCODING_NRZI_SPACE: RegValue |= BIT14 + BIT13; break; 4841 case HDLC_ENCODING_NRZI_SPACE: RegValue |= BIT14 | BIT13; break;
4842 case HDLC_ENCODING_BIPHASE_MARK: RegValue |= BIT15; break; 4842 case HDLC_ENCODING_BIPHASE_MARK: RegValue |= BIT15; break;
4843 case HDLC_ENCODING_BIPHASE_SPACE: RegValue |= BIT15 + BIT13; break; 4843 case HDLC_ENCODING_BIPHASE_SPACE: RegValue |= BIT15 | BIT13; break;
4844 case HDLC_ENCODING_BIPHASE_LEVEL: RegValue |= BIT15 + BIT14; break; 4844 case HDLC_ENCODING_BIPHASE_LEVEL: RegValue |= BIT15 | BIT14; break;
4845 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: RegValue |= BIT15 + BIT14 + BIT13; break; 4845 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: RegValue |= BIT15 | BIT14 | BIT13; break;
4846 } 4846 }
4847 4847
4848 if ( (info->params.crc_type & HDLC_CRC_MASK) == HDLC_CRC_16_CCITT ) 4848 if ( (info->params.crc_type & HDLC_CRC_MASK) == HDLC_CRC_16_CCITT )
4849 RegValue |= BIT9 + BIT8; 4849 RegValue |= BIT9 | BIT8;
4850 else if ( (info->params.crc_type & HDLC_CRC_MASK) == HDLC_CRC_32_CCITT ) 4850 else if ( (info->params.crc_type & HDLC_CRC_MASK) == HDLC_CRC_32_CCITT )
4851 RegValue |= ( BIT12 | BIT10 | BIT9 | BIT8); 4851 RegValue |= ( BIT12 | BIT10 | BIT9 | BIT8);
4852 4852
@@ -4957,7 +4957,7 @@ static void usc_set_sdlc_mode( struct mgsl_struct *info )
4957 4957
4958 RegValue = 0x0000; 4958 RegValue = 0x0000;
4959 4959
4960 if ( info->params.flags & (HDLC_FLAG_RXC_DPLL + HDLC_FLAG_TXC_DPLL) ) { 4960 if ( info->params.flags & (HDLC_FLAG_RXC_DPLL | HDLC_FLAG_TXC_DPLL) ) {
4961 u32 XtalSpeed; 4961 u32 XtalSpeed;
4962 u32 DpllDivisor; 4962 u32 DpllDivisor;
4963 u16 Tc; 4963 u16 Tc;
@@ -5019,7 +5019,7 @@ static void usc_set_sdlc_mode( struct mgsl_struct *info )
5019 case HDLC_ENCODING_BIPHASE_MARK: 5019 case HDLC_ENCODING_BIPHASE_MARK:
5020 case HDLC_ENCODING_BIPHASE_SPACE: RegValue |= BIT9; break; 5020 case HDLC_ENCODING_BIPHASE_SPACE: RegValue |= BIT9; break;
5021 case HDLC_ENCODING_BIPHASE_LEVEL: 5021 case HDLC_ENCODING_BIPHASE_LEVEL:
5022 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: RegValue |= BIT9 + BIT8; break; 5022 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: RegValue |= BIT9 | BIT8; break;
5023 } 5023 }
5024 } 5024 }
5025 5025
@@ -5056,8 +5056,8 @@ static void usc_set_sdlc_mode( struct mgsl_struct *info )
5056 /* enable Master Interrupt Enable bit (MIE) */ 5056 /* enable Master Interrupt Enable bit (MIE) */
5057 usc_EnableMasterIrqBit( info ); 5057 usc_EnableMasterIrqBit( info );
5058 5058
5059 usc_ClearIrqPendingBits( info, RECEIVE_STATUS + RECEIVE_DATA + 5059 usc_ClearIrqPendingBits( info, RECEIVE_STATUS | RECEIVE_DATA |
5060 TRANSMIT_STATUS + TRANSMIT_DATA + MISC); 5060 TRANSMIT_STATUS | TRANSMIT_DATA | MISC);
5061 5061
5062 /* arm RCC underflow interrupt */ 5062 /* arm RCC underflow interrupt */
5063 usc_OutReg(info, SICR, (u16)(usc_InReg(info,SICR) | BIT3)); 5063 usc_OutReg(info, SICR, (u16)(usc_InReg(info,SICR) | BIT3));
@@ -5175,14 +5175,14 @@ static void usc_set_sdlc_mode( struct mgsl_struct *info )
5175 switch ( info->params.preamble_length ) { 5175 switch ( info->params.preamble_length ) {
5176 case HDLC_PREAMBLE_LENGTH_16BITS: RegValue |= BIT10; break; 5176 case HDLC_PREAMBLE_LENGTH_16BITS: RegValue |= BIT10; break;
5177 case HDLC_PREAMBLE_LENGTH_32BITS: RegValue |= BIT11; break; 5177 case HDLC_PREAMBLE_LENGTH_32BITS: RegValue |= BIT11; break;
5178 case HDLC_PREAMBLE_LENGTH_64BITS: RegValue |= BIT11 + BIT10; break; 5178 case HDLC_PREAMBLE_LENGTH_64BITS: RegValue |= BIT11 | BIT10; break;
5179 } 5179 }
5180 5180
5181 switch ( info->params.preamble ) { 5181 switch ( info->params.preamble ) {
5182 case HDLC_PREAMBLE_PATTERN_FLAGS: RegValue |= BIT8 + BIT12; break; 5182 case HDLC_PREAMBLE_PATTERN_FLAGS: RegValue |= BIT8 | BIT12; break;
5183 case HDLC_PREAMBLE_PATTERN_ONES: RegValue |= BIT8; break; 5183 case HDLC_PREAMBLE_PATTERN_ONES: RegValue |= BIT8; break;
5184 case HDLC_PREAMBLE_PATTERN_10: RegValue |= BIT9; break; 5184 case HDLC_PREAMBLE_PATTERN_10: RegValue |= BIT9; break;
5185 case HDLC_PREAMBLE_PATTERN_01: RegValue |= BIT9 + BIT8; break; 5185 case HDLC_PREAMBLE_PATTERN_01: RegValue |= BIT9 | BIT8; break;
5186 } 5186 }
5187 5187
5188 usc_OutReg( info, CCR, RegValue ); 5188 usc_OutReg( info, CCR, RegValue );
@@ -5221,7 +5221,7 @@ static void usc_enable_loopback(struct mgsl_struct *info, int enable)
5221{ 5221{
5222 if (enable) { 5222 if (enable) {
5223 /* blank external TXD output */ 5223 /* blank external TXD output */
5224 usc_OutReg(info,IOCR,usc_InReg(info,IOCR) | (BIT7+BIT6)); 5224 usc_OutReg(info,IOCR,usc_InReg(info,IOCR) | (BIT7 | BIT6));
5225 5225
5226 /* Clock mode Control Register (CMCR) 5226 /* Clock mode Control Register (CMCR)
5227 * 5227 *
@@ -5260,7 +5260,7 @@ static void usc_enable_loopback(struct mgsl_struct *info, int enable)
5260 outw( 0x0300, info->io_base + CCAR ); 5260 outw( 0x0300, info->io_base + CCAR );
5261 } else { 5261 } else {
5262 /* enable external TXD output */ 5262 /* enable external TXD output */
5263 usc_OutReg(info,IOCR,usc_InReg(info,IOCR) & ~(BIT7+BIT6)); 5263 usc_OutReg(info,IOCR,usc_InReg(info,IOCR) & ~(BIT7 | BIT6));
5264 5264
5265 /* clear Internal Data loopback mode */ 5265 /* clear Internal Data loopback mode */
5266 info->loopback_bits = 0; 5266 info->loopback_bits = 0;
@@ -5447,13 +5447,13 @@ static void usc_process_rxoverrun_sync( struct mgsl_struct *info )
5447 usc_OutDmaReg( info, NRARU, (u16)(phys_addr >> 16) ); 5447 usc_OutDmaReg( info, NRARU, (u16)(phys_addr >> 16) );
5448 5448
5449 usc_UnlatchRxstatusBits( info, RXSTATUS_ALL ); 5449 usc_UnlatchRxstatusBits( info, RXSTATUS_ALL );
5450 usc_ClearIrqPendingBits( info, RECEIVE_DATA + RECEIVE_STATUS ); 5450 usc_ClearIrqPendingBits( info, RECEIVE_DATA | RECEIVE_STATUS );
5451 usc_EnableInterrupts( info, RECEIVE_STATUS ); 5451 usc_EnableInterrupts( info, RECEIVE_STATUS );
5452 5452
5453 /* 1. Arm End of Buffer (EOB) Receive DMA Interrupt (BIT2 of RDIAR) */ 5453 /* 1. Arm End of Buffer (EOB) Receive DMA Interrupt (BIT2 of RDIAR) */
5454 /* 2. Enable Receive DMA Interrupts (BIT1 of DICR) */ 5454 /* 2. Enable Receive DMA Interrupts (BIT1 of DICR) */
5455 5455
5456 usc_OutDmaReg( info, RDIAR, BIT3 + BIT2 ); 5456 usc_OutDmaReg( info, RDIAR, BIT3 | BIT2 );
5457 usc_OutDmaReg( info, DICR, (u16)(usc_InDmaReg(info,DICR) | BIT1) ); 5457 usc_OutDmaReg( info, DICR, (u16)(usc_InDmaReg(info,DICR) | BIT1) );
5458 usc_DmaCmd( info, DmaCmd_InitRxChannel ); 5458 usc_DmaCmd( info, DmaCmd_InitRxChannel );
5459 if ( info->params.flags & HDLC_FLAG_AUTO_DCD ) 5459 if ( info->params.flags & HDLC_FLAG_AUTO_DCD )
@@ -5488,8 +5488,8 @@ static void usc_stop_receiver( struct mgsl_struct *info )
5488 usc_DmaCmd( info, DmaCmd_ResetRxChannel ); 5488 usc_DmaCmd( info, DmaCmd_ResetRxChannel );
5489 5489
5490 usc_UnlatchRxstatusBits( info, RXSTATUS_ALL ); 5490 usc_UnlatchRxstatusBits( info, RXSTATUS_ALL );
5491 usc_ClearIrqPendingBits( info, RECEIVE_DATA + RECEIVE_STATUS ); 5491 usc_ClearIrqPendingBits( info, RECEIVE_DATA | RECEIVE_STATUS );
5492 usc_DisableInterrupts( info, RECEIVE_DATA + RECEIVE_STATUS ); 5492 usc_DisableInterrupts( info, RECEIVE_DATA | RECEIVE_STATUS );
5493 5493
5494 usc_EnableReceiver(info,DISABLE_UNCONDITIONAL); 5494 usc_EnableReceiver(info,DISABLE_UNCONDITIONAL);
5495 5495
@@ -5536,13 +5536,13 @@ static void usc_start_receiver( struct mgsl_struct *info )
5536 usc_OutDmaReg( info, NRARU, (u16)(phys_addr >> 16) ); 5536 usc_OutDmaReg( info, NRARU, (u16)(phys_addr >> 16) );
5537 5537
5538 usc_UnlatchRxstatusBits( info, RXSTATUS_ALL ); 5538 usc_UnlatchRxstatusBits( info, RXSTATUS_ALL );
5539 usc_ClearIrqPendingBits( info, RECEIVE_DATA + RECEIVE_STATUS ); 5539 usc_ClearIrqPendingBits( info, RECEIVE_DATA | RECEIVE_STATUS );
5540 usc_EnableInterrupts( info, RECEIVE_STATUS ); 5540 usc_EnableInterrupts( info, RECEIVE_STATUS );
5541 5541
5542 /* 1. Arm End of Buffer (EOB) Receive DMA Interrupt (BIT2 of RDIAR) */ 5542 /* 1. Arm End of Buffer (EOB) Receive DMA Interrupt (BIT2 of RDIAR) */
5543 /* 2. Enable Receive DMA Interrupts (BIT1 of DICR) */ 5543 /* 2. Enable Receive DMA Interrupts (BIT1 of DICR) */
5544 5544
5545 usc_OutDmaReg( info, RDIAR, BIT3 + BIT2 ); 5545 usc_OutDmaReg( info, RDIAR, BIT3 | BIT2 );
5546 usc_OutDmaReg( info, DICR, (u16)(usc_InDmaReg(info,DICR) | BIT1) ); 5546 usc_OutDmaReg( info, DICR, (u16)(usc_InDmaReg(info,DICR) | BIT1) );
5547 usc_DmaCmd( info, DmaCmd_InitRxChannel ); 5547 usc_DmaCmd( info, DmaCmd_InitRxChannel );
5548 if ( info->params.flags & HDLC_FLAG_AUTO_DCD ) 5548 if ( info->params.flags & HDLC_FLAG_AUTO_DCD )
@@ -5551,7 +5551,7 @@ static void usc_start_receiver( struct mgsl_struct *info )
5551 usc_EnableReceiver(info,ENABLE_UNCONDITIONAL); 5551 usc_EnableReceiver(info,ENABLE_UNCONDITIONAL);
5552 } else { 5552 } else {
5553 usc_UnlatchRxstatusBits(info, RXSTATUS_ALL); 5553 usc_UnlatchRxstatusBits(info, RXSTATUS_ALL);
5554 usc_ClearIrqPendingBits(info, RECEIVE_DATA + RECEIVE_STATUS); 5554 usc_ClearIrqPendingBits(info, RECEIVE_DATA | RECEIVE_STATUS);
5555 usc_EnableInterrupts(info, RECEIVE_DATA); 5555 usc_EnableInterrupts(info, RECEIVE_DATA);
5556 5556
5557 usc_RTCmd( info, RTCmd_PurgeRxFifo ); 5557 usc_RTCmd( info, RTCmd_PurgeRxFifo );
@@ -5925,7 +5925,7 @@ static void usc_set_async_mode( struct mgsl_struct *info )
5925 RegValue = 0; 5925 RegValue = 0;
5926 5926
5927 if ( info->params.data_bits != 8 ) 5927 if ( info->params.data_bits != 8 )
5928 RegValue |= BIT4+BIT3+BIT2; 5928 RegValue |= BIT4 | BIT3 | BIT2;
5929 5929
5930 if ( info->params.parity != ASYNC_PARITY_NONE ) { 5930 if ( info->params.parity != ASYNC_PARITY_NONE ) {
5931 RegValue |= BIT5; 5931 RegValue |= BIT5;
@@ -5982,7 +5982,7 @@ static void usc_set_async_mode( struct mgsl_struct *info )
5982 RegValue = 0; 5982 RegValue = 0;
5983 5983
5984 if ( info->params.data_bits != 8 ) 5984 if ( info->params.data_bits != 8 )
5985 RegValue |= BIT4+BIT3+BIT2; 5985 RegValue |= BIT4 | BIT3 | BIT2;
5986 5986
5987 if ( info->params.parity != ASYNC_PARITY_NONE ) { 5987 if ( info->params.parity != ASYNC_PARITY_NONE ) {
5988 RegValue |= BIT5; 5988 RegValue |= BIT5;
@@ -6129,7 +6129,7 @@ static void usc_loopback_frame( struct mgsl_struct *info )
6129 6129
6130 /* WAIT FOR RECEIVE COMPLETE */ 6130 /* WAIT FOR RECEIVE COMPLETE */
6131 for (i=0 ; i<1000 ; i++) 6131 for (i=0 ; i<1000 ; i++)
6132 if (usc_InReg( info, RCSR ) & (BIT8 + BIT4 + BIT3 + BIT1)) 6132 if (usc_InReg( info, RCSR ) & (BIT8 | BIT4 | BIT3 | BIT1))
6133 break; 6133 break;
6134 6134
6135 /* clear Internal Data loopback mode */ 6135 /* clear Internal Data loopback mode */
@@ -6579,8 +6579,8 @@ static bool mgsl_get_rx_frame(struct mgsl_struct *info)
6579 6579
6580 status = info->rx_buffer_list[EndIndex].status; 6580 status = info->rx_buffer_list[EndIndex].status;
6581 6581
6582 if ( status & (RXSTATUS_SHORT_FRAME + RXSTATUS_OVERRUN + 6582 if ( status & (RXSTATUS_SHORT_FRAME | RXSTATUS_OVERRUN |
6583 RXSTATUS_CRC_ERROR + RXSTATUS_ABORT) ) { 6583 RXSTATUS_CRC_ERROR | RXSTATUS_ABORT) ) {
6584 if ( status & RXSTATUS_SHORT_FRAME ) 6584 if ( status & RXSTATUS_SHORT_FRAME )
6585 info->icount.rxshort++; 6585 info->icount.rxshort++;
6586 else if ( status & RXSTATUS_ABORT ) 6586 else if ( status & RXSTATUS_ABORT )
@@ -6762,8 +6762,8 @@ static bool mgsl_get_raw_rx_frame(struct mgsl_struct *info)
6762 6762
6763 status = info->rx_buffer_list[CurrentIndex].status; 6763 status = info->rx_buffer_list[CurrentIndex].status;
6764 6764
6765 if ( status & (RXSTATUS_SHORT_FRAME + RXSTATUS_OVERRUN + 6765 if ( status & (RXSTATUS_SHORT_FRAME | RXSTATUS_OVERRUN |
6766 RXSTATUS_CRC_ERROR + RXSTATUS_ABORT) ) { 6766 RXSTATUS_CRC_ERROR | RXSTATUS_ABORT) ) {
6767 if ( status & RXSTATUS_SHORT_FRAME ) 6767 if ( status & RXSTATUS_SHORT_FRAME )
6768 info->icount.rxshort++; 6768 info->icount.rxshort++;
6769 else if ( status & RXSTATUS_ABORT ) 6769 else if ( status & RXSTATUS_ABORT )
@@ -6899,7 +6899,7 @@ static void mgsl_load_tx_dma_buffer(struct mgsl_struct *info,
6899 /* set CMR:13 to start transmit when 6899 /* set CMR:13 to start transmit when
6900 * next GoAhead (abort) is received 6900 * next GoAhead (abort) is received
6901 */ 6901 */
6902 info->cmr_value |= BIT13; 6902 info->cmr_value |= BIT13;
6903 } 6903 }
6904 6904
6905 /* begin loading the frame in the next available tx dma 6905 /* begin loading the frame in the next available tx dma
@@ -7278,7 +7278,7 @@ static bool mgsl_dma_test( struct mgsl_struct *info )
7278 7278
7279 spin_unlock_irqrestore(&info->irq_spinlock,flags); 7279 spin_unlock_irqrestore(&info->irq_spinlock,flags);
7280 7280
7281 7281
7282 /******************************/ 7282 /******************************/
7283 /* WAIT FOR TRANSMIT COMPLETE */ 7283 /* WAIT FOR TRANSMIT COMPLETE */
7284 /******************************/ 7284 /******************************/
@@ -7292,7 +7292,7 @@ static bool mgsl_dma_test( struct mgsl_struct *info )
7292 status = usc_InReg( info, TCSR ); 7292 status = usc_InReg( info, TCSR );
7293 spin_unlock_irqrestore(&info->irq_spinlock,flags); 7293 spin_unlock_irqrestore(&info->irq_spinlock,flags);
7294 7294
7295 while ( !(status & (BIT6+BIT5+BIT4+BIT2+BIT1)) ) { 7295 while ( !(status & (BIT6 | BIT5 | BIT4 | BIT2 | BIT1)) ) {
7296 if (time_after(jiffies, EndTime)) { 7296 if (time_after(jiffies, EndTime)) {
7297 rc = false; 7297 rc = false;
7298 break; 7298 break;
@@ -7307,7 +7307,7 @@ static bool mgsl_dma_test( struct mgsl_struct *info )
7307 7307
7308 if ( rc ){ 7308 if ( rc ){
7309 /* CHECK FOR TRANSMIT ERRORS */ 7309 /* CHECK FOR TRANSMIT ERRORS */
7310 if ( status & (BIT5 + BIT1) ) 7310 if ( status & (BIT5 | BIT1) )
7311 rc = false; 7311 rc = false;
7312 } 7312 }
7313 7313
@@ -7333,7 +7333,7 @@ static bool mgsl_dma_test( struct mgsl_struct *info )
7333 /* CHECK FOR RECEIVE ERRORS */ 7333 /* CHECK FOR RECEIVE ERRORS */
7334 status = info->rx_buffer_list[0].status; 7334 status = info->rx_buffer_list[0].status;
7335 7335
7336 if ( status & (BIT8 + BIT3 + BIT1) ) { 7336 if ( status & (BIT8 | BIT3 | BIT1) ) {
7337 /* receive error has occurred */ 7337 /* receive error has occurred */
7338 rc = false; 7338 rc = false;
7339 } else { 7339 } else {
@@ -7605,7 +7605,7 @@ static void usc_loopmode_send_done( struct mgsl_struct * info )
7605{ 7605{
7606 info->loopmode_send_done_requested = false; 7606 info->loopmode_send_done_requested = false;
7607 /* clear CMR:13 to 0 to start echoing RxData to TxData */ 7607 /* clear CMR:13 to 0 to start echoing RxData to TxData */
7608 info->cmr_value &= ~BIT13; 7608 info->cmr_value &= ~BIT13;
7609 usc_OutReg(info, CMR, info->cmr_value); 7609 usc_OutReg(info, CMR, info->cmr_value);
7610} 7610}
7611 7611
diff --git a/drivers/tty/tty_buffer.c b/drivers/tty/tty_buffer.c
index 9121c1f7aeef..c043136fbe51 100644
--- a/drivers/tty/tty_buffer.c
+++ b/drivers/tty/tty_buffer.c
@@ -18,31 +18,118 @@
18#include <linux/module.h> 18#include <linux/module.h>
19#include <linux/ratelimit.h> 19#include <linux/ratelimit.h>
20 20
21
22#define MIN_TTYB_SIZE 256
23#define TTYB_ALIGN_MASK 255
24
25/*
26 * Byte threshold to limit memory consumption for flip buffers.
27 * The actual memory limit is > 2x this amount.
28 */
29#define TTYB_MEM_LIMIT 65536
30
31/*
32 * We default to dicing tty buffer allocations to this many characters
33 * in order to avoid multiple page allocations. We know the size of
34 * tty_buffer itself but it must also be taken into account that the
35 * the buffer is 256 byte aligned. See tty_buffer_find for the allocation
36 * logic this must match
37 */
38
39#define TTY_BUFFER_PAGE (((PAGE_SIZE - sizeof(struct tty_buffer)) / 2) & ~0xFF)
40
41
42/**
43 * tty_buffer_lock_exclusive - gain exclusive access to buffer
44 * tty_buffer_unlock_exclusive - release exclusive access
45 *
46 * @port - tty_port owning the flip buffer
47 *
48 * Guarantees safe use of the line discipline's receive_buf() method by
49 * excluding the buffer work and any pending flush from using the flip
50 * buffer. Data can continue to be added concurrently to the flip buffer
51 * from the driver side.
52 *
53 * On release, the buffer work is restarted if there is data in the
54 * flip buffer
55 */
56
57void tty_buffer_lock_exclusive(struct tty_port *port)
58{
59 struct tty_bufhead *buf = &port->buf;
60
61 atomic_inc(&buf->priority);
62 mutex_lock(&buf->lock);
63}
64
65void tty_buffer_unlock_exclusive(struct tty_port *port)
66{
67 struct tty_bufhead *buf = &port->buf;
68 int restart;
69
70 restart = buf->head->commit != buf->head->read;
71
72 atomic_dec(&buf->priority);
73 mutex_unlock(&buf->lock);
74 if (restart)
75 queue_work(system_unbound_wq, &buf->work);
76}
77
78/**
79 * tty_buffer_space_avail - return unused buffer space
80 * @port - tty_port owning the flip buffer
81 *
82 * Returns the # of bytes which can be written by the driver without
83 * reaching the buffer limit.
84 *
85 * Note: this does not guarantee that memory is available to write
86 * the returned # of bytes (use tty_prepare_flip_string_xxx() to
87 * pre-allocate if memory guarantee is required).
88 */
89
90int tty_buffer_space_avail(struct tty_port *port)
91{
92 int space = TTYB_MEM_LIMIT - atomic_read(&port->buf.memory_used);
93 return max(space, 0);
94}
95
96static void tty_buffer_reset(struct tty_buffer *p, size_t size)
97{
98 p->used = 0;
99 p->size = size;
100 p->next = NULL;
101 p->commit = 0;
102 p->read = 0;
103}
104
21/** 105/**
22 * tty_buffer_free_all - free buffers used by a tty 106 * tty_buffer_free_all - free buffers used by a tty
23 * @tty: tty to free from 107 * @tty: tty to free from
24 * 108 *
25 * Remove all the buffers pending on a tty whether queued with data 109 * Remove all the buffers pending on a tty whether queued with data
26 * or in the free ring. Must be called when the tty is no longer in use 110 * or in the free ring. Must be called when the tty is no longer in use
27 *
28 * Locking: none
29 */ 111 */
30 112
31void tty_buffer_free_all(struct tty_port *port) 113void tty_buffer_free_all(struct tty_port *port)
32{ 114{
33 struct tty_bufhead *buf = &port->buf; 115 struct tty_bufhead *buf = &port->buf;
34 struct tty_buffer *thead; 116 struct tty_buffer *p, *next;
117 struct llist_node *llist;
35 118
36 while ((thead = buf->head) != NULL) { 119 while ((p = buf->head) != NULL) {
37 buf->head = thead->next; 120 buf->head = p->next;
38 kfree(thead); 121 if (p->size > 0)
39 } 122 kfree(p);
40 while ((thead = buf->free) != NULL) {
41 buf->free = thead->next;
42 kfree(thead);
43 } 123 }
44 buf->tail = NULL; 124 llist = llist_del_all(&buf->free);
45 buf->memory_used = 0; 125 llist_for_each_entry_safe(p, next, llist, free)
126 kfree(p);
127
128 tty_buffer_reset(&buf->sentinel, 0);
129 buf->head = &buf->sentinel;
130 buf->tail = &buf->sentinel;
131
132 atomic_set(&buf->memory_used, 0);
46} 133}
47 134
48/** 135/**
@@ -51,29 +138,39 @@ void tty_buffer_free_all(struct tty_port *port)
51 * @size: desired size (characters) 138 * @size: desired size (characters)
52 * 139 *
53 * Allocate a new tty buffer to hold the desired number of characters. 140 * Allocate a new tty buffer to hold the desired number of characters.
141 * We round our buffers off in 256 character chunks to get better
142 * allocation behaviour.
54 * Return NULL if out of memory or the allocation would exceed the 143 * Return NULL if out of memory or the allocation would exceed the
55 * per device queue 144 * per device queue
56 *
57 * Locking: Caller must hold tty->buf.lock
58 */ 145 */
59 146
60static struct tty_buffer *tty_buffer_alloc(struct tty_port *port, size_t size) 147static struct tty_buffer *tty_buffer_alloc(struct tty_port *port, size_t size)
61{ 148{
149 struct llist_node *free;
62 struct tty_buffer *p; 150 struct tty_buffer *p;
63 151
64 if (port->buf.memory_used + size > 65536) 152 /* Round the buffer size out */
153 size = __ALIGN_MASK(size, TTYB_ALIGN_MASK);
154
155 if (size <= MIN_TTYB_SIZE) {
156 free = llist_del_first(&port->buf.free);
157 if (free) {
158 p = llist_entry(free, struct tty_buffer, free);
159 goto found;
160 }
161 }
162
163 /* Should possibly check if this fails for the largest buffer we
164 have queued and recycle that ? */
165 if (atomic_read(&port->buf.memory_used) > TTYB_MEM_LIMIT)
65 return NULL; 166 return NULL;
66 p = kmalloc(sizeof(struct tty_buffer) + 2 * size, GFP_ATOMIC); 167 p = kmalloc(sizeof(struct tty_buffer) + 2 * size, GFP_ATOMIC);
67 if (p == NULL) 168 if (p == NULL)
68 return NULL; 169 return NULL;
69 p->used = 0; 170
70 p->size = size; 171found:
71 p->next = NULL; 172 tty_buffer_reset(p, size);
72 p->commit = 0; 173 atomic_add(size, &port->buf.memory_used);
73 p->read = 0;
74 p->char_buf_ptr = (char *)(p->data);
75 p->flag_buf_ptr = (unsigned char *)p->char_buf_ptr + size;
76 port->buf.memory_used += size;
77 return p; 174 return p;
78} 175}
79 176
@@ -84,8 +181,6 @@ static struct tty_buffer *tty_buffer_alloc(struct tty_port *port, size_t size)
84 * 181 *
85 * Free a tty buffer, or add it to the free list according to our 182 * Free a tty buffer, or add it to the free list according to our
86 * internal strategy 183 * internal strategy
87 *
88 * Locking: Caller must hold tty->buf.lock
89 */ 184 */
90 185
91static void tty_buffer_free(struct tty_port *port, struct tty_buffer *b) 186static void tty_buffer_free(struct tty_port *port, struct tty_buffer *b)
@@ -93,41 +188,12 @@ static void tty_buffer_free(struct tty_port *port, struct tty_buffer *b)
93 struct tty_bufhead *buf = &port->buf; 188 struct tty_bufhead *buf = &port->buf;
94 189
95 /* Dumb strategy for now - should keep some stats */ 190 /* Dumb strategy for now - should keep some stats */
96 buf->memory_used -= b->size; 191 WARN_ON(atomic_sub_return(b->size, &buf->memory_used) < 0);
97 WARN_ON(buf->memory_used < 0);
98 192
99 if (b->size >= 512) 193 if (b->size > MIN_TTYB_SIZE)
100 kfree(b); 194 kfree(b);
101 else { 195 else if (b->size > 0)
102 b->next = buf->free; 196 llist_add(&b->free, &buf->free);
103 buf->free = b;
104 }
105}
106
107/**
108 * __tty_buffer_flush - flush full tty buffers
109 * @tty: tty to flush
110 *
111 * flush all the buffers containing receive data. Caller must
112 * hold the buffer lock and must have ensured no parallel flush to
113 * ldisc is running.
114 *
115 * Locking: Caller must hold tty->buf.lock
116 */
117
118static void __tty_buffer_flush(struct tty_port *port)
119{
120 struct tty_bufhead *buf = &port->buf;
121 struct tty_buffer *thead;
122
123 if (unlikely(buf->head == NULL))
124 return;
125 while ((thead = buf->head->next) != NULL) {
126 tty_buffer_free(port, buf->head);
127 buf->head = thead;
128 }
129 WARN_ON(buf->head != buf->tail);
130 buf->head->read = buf->head->commit;
131} 197}
132 198
133/** 199/**
@@ -138,65 +204,28 @@ static void __tty_buffer_flush(struct tty_port *port)
138 * being processed by flush_to_ldisc then we defer the processing 204 * being processed by flush_to_ldisc then we defer the processing
139 * to that function 205 * to that function
140 * 206 *
141 * Locking: none 207 * Locking: takes buffer lock to ensure single-threaded flip buffer
208 * 'consumer'
142 */ 209 */
143 210
144void tty_buffer_flush(struct tty_struct *tty) 211void tty_buffer_flush(struct tty_struct *tty)
145{ 212{
146 struct tty_port *port = tty->port; 213 struct tty_port *port = tty->port;
147 struct tty_bufhead *buf = &port->buf; 214 struct tty_bufhead *buf = &port->buf;
148 unsigned long flags; 215 struct tty_buffer *next;
149
150 spin_lock_irqsave(&buf->lock, flags);
151
152 /* If the data is being pushed to the tty layer then we can't
153 process it here. Instead set a flag and the flush_to_ldisc
154 path will process the flush request before it exits */
155 if (test_bit(TTYP_FLUSHING, &port->iflags)) {
156 set_bit(TTYP_FLUSHPENDING, &port->iflags);
157 spin_unlock_irqrestore(&buf->lock, flags);
158 wait_event(tty->read_wait,
159 test_bit(TTYP_FLUSHPENDING, &port->iflags) == 0);
160 return;
161 } else
162 __tty_buffer_flush(port);
163 spin_unlock_irqrestore(&buf->lock, flags);
164}
165 216
166/** 217 atomic_inc(&buf->priority);
167 * tty_buffer_find - find a free tty buffer
168 * @tty: tty owning the buffer
169 * @size: characters wanted
170 *
171 * Locate an existing suitable tty buffer or if we are lacking one then
172 * allocate a new one. We round our buffers off in 256 character chunks
173 * to get better allocation behaviour.
174 *
175 * Locking: Caller must hold tty->buf.lock
176 */
177 218
178static struct tty_buffer *tty_buffer_find(struct tty_port *port, size_t size) 219 mutex_lock(&buf->lock);
179{ 220 while ((next = buf->head->next) != NULL) {
180 struct tty_buffer **tbh = &port->buf.free; 221 tty_buffer_free(port, buf->head);
181 while ((*tbh) != NULL) { 222 buf->head = next;
182 struct tty_buffer *t = *tbh;
183 if (t->size >= size) {
184 *tbh = t->next;
185 t->next = NULL;
186 t->used = 0;
187 t->commit = 0;
188 t->read = 0;
189 port->buf.memory_used += t->size;
190 return t;
191 }
192 tbh = &((*tbh)->next);
193 } 223 }
194 /* Round the buffer size out */ 224 buf->head->read = buf->head->commit;
195 size = (size + 0xFF) & ~0xFF; 225 atomic_dec(&buf->priority);
196 return tty_buffer_alloc(port, size); 226 mutex_unlock(&buf->lock);
197 /* Should possibly check if this fails for the largest buffer we
198 have queued and recycle that ? */
199} 227}
228
200/** 229/**
201 * tty_buffer_request_room - grow tty buffer if needed 230 * tty_buffer_request_room - grow tty buffer if needed
202 * @tty: tty structure 231 * @tty: tty structure
@@ -204,38 +233,26 @@ static struct tty_buffer *tty_buffer_find(struct tty_port *port, size_t size)
204 * 233 *
205 * Make at least size bytes of linear space available for the tty 234 * Make at least size bytes of linear space available for the tty
206 * buffer. If we fail return the size we managed to find. 235 * buffer. If we fail return the size we managed to find.
207 *
208 * Locking: Takes port->buf.lock
209 */ 236 */
210int tty_buffer_request_room(struct tty_port *port, size_t size) 237int tty_buffer_request_room(struct tty_port *port, size_t size)
211{ 238{
212 struct tty_bufhead *buf = &port->buf; 239 struct tty_bufhead *buf = &port->buf;
213 struct tty_buffer *b, *n; 240 struct tty_buffer *b, *n;
214 int left; 241 int left;
215 unsigned long flags; 242
216 spin_lock_irqsave(&buf->lock, flags);
217 /* OPTIMISATION: We could keep a per tty "zero" sized buffer to
218 remove this conditional if its worth it. This would be invisible
219 to the callers */
220 b = buf->tail; 243 b = buf->tail;
221 if (b != NULL) 244 left = b->size - b->used;
222 left = b->size - b->used;
223 else
224 left = 0;
225 245
226 if (left < size) { 246 if (left < size) {
227 /* This is the slow path - looking for new buffers to use */ 247 /* This is the slow path - looking for new buffers to use */
228 if ((n = tty_buffer_find(port, size)) != NULL) { 248 if ((n = tty_buffer_alloc(port, size)) != NULL) {
229 if (b != NULL) {
230 b->next = n;
231 b->commit = b->used;
232 } else
233 buf->head = n;
234 buf->tail = n; 249 buf->tail = n;
250 b->commit = b->used;
251 smp_mb();
252 b->next = n;
235 } else 253 } else
236 size = left; 254 size = left;
237 } 255 }
238 spin_unlock_irqrestore(&buf->lock, flags);
239 return size; 256 return size;
240} 257}
241EXPORT_SYMBOL_GPL(tty_buffer_request_room); 258EXPORT_SYMBOL_GPL(tty_buffer_request_room);
@@ -249,8 +266,6 @@ EXPORT_SYMBOL_GPL(tty_buffer_request_room);
249 * 266 *
250 * Queue a series of bytes to the tty buffering. All the characters 267 * Queue a series of bytes to the tty buffering. All the characters
251 * passed are marked with the supplied flag. Returns the number added. 268 * passed are marked with the supplied flag. Returns the number added.
252 *
253 * Locking: Called functions may take port->buf.lock
254 */ 269 */
255 270
256int tty_insert_flip_string_fixed_flag(struct tty_port *port, 271int tty_insert_flip_string_fixed_flag(struct tty_port *port,
@@ -261,12 +276,10 @@ int tty_insert_flip_string_fixed_flag(struct tty_port *port,
261 int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE); 276 int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE);
262 int space = tty_buffer_request_room(port, goal); 277 int space = tty_buffer_request_room(port, goal);
263 struct tty_buffer *tb = port->buf.tail; 278 struct tty_buffer *tb = port->buf.tail;
264 /* If there is no space then tb may be NULL */ 279 if (unlikely(space == 0))
265 if (unlikely(space == 0)) {
266 break; 280 break;
267 } 281 memcpy(char_buf_ptr(tb, tb->used), chars, space);
268 memcpy(tb->char_buf_ptr + tb->used, chars, space); 282 memset(flag_buf_ptr(tb, tb->used), flag, space);
269 memset(tb->flag_buf_ptr + tb->used, flag, space);
270 tb->used += space; 283 tb->used += space;
271 copied += space; 284 copied += space;
272 chars += space; 285 chars += space;
@@ -287,8 +300,6 @@ EXPORT_SYMBOL(tty_insert_flip_string_fixed_flag);
287 * Queue a series of bytes to the tty buffering. For each character 300 * Queue a series of bytes to the tty buffering. For each character
288 * the flags array indicates the status of the character. Returns the 301 * the flags array indicates the status of the character. Returns the
289 * number added. 302 * number added.
290 *
291 * Locking: Called functions may take port->buf.lock
292 */ 303 */
293 304
294int tty_insert_flip_string_flags(struct tty_port *port, 305int tty_insert_flip_string_flags(struct tty_port *port,
@@ -299,12 +310,10 @@ int tty_insert_flip_string_flags(struct tty_port *port,
299 int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE); 310 int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE);
300 int space = tty_buffer_request_room(port, goal); 311 int space = tty_buffer_request_room(port, goal);
301 struct tty_buffer *tb = port->buf.tail; 312 struct tty_buffer *tb = port->buf.tail;
302 /* If there is no space then tb may be NULL */ 313 if (unlikely(space == 0))
303 if (unlikely(space == 0)) {
304 break; 314 break;
305 } 315 memcpy(char_buf_ptr(tb, tb->used), chars, space);
306 memcpy(tb->char_buf_ptr + tb->used, chars, space); 316 memcpy(flag_buf_ptr(tb, tb->used), flags, space);
307 memcpy(tb->flag_buf_ptr + tb->used, flags, space);
308 tb->used += space; 317 tb->used += space;
309 copied += space; 318 copied += space;
310 chars += space; 319 chars += space;
@@ -325,20 +334,14 @@ EXPORT_SYMBOL(tty_insert_flip_string_flags);
325 * processing by the line discipline. 334 * processing by the line discipline.
326 * Note that this function can only be used when the low_latency flag 335 * Note that this function can only be used when the low_latency flag
327 * is unset. Otherwise the workqueue won't be flushed. 336 * is unset. Otherwise the workqueue won't be flushed.
328 *
329 * Locking: Takes port->buf.lock
330 */ 337 */
331 338
332void tty_schedule_flip(struct tty_port *port) 339void tty_schedule_flip(struct tty_port *port)
333{ 340{
334 struct tty_bufhead *buf = &port->buf; 341 struct tty_bufhead *buf = &port->buf;
335 unsigned long flags;
336 WARN_ON(port->low_latency); 342 WARN_ON(port->low_latency);
337 343
338 spin_lock_irqsave(&buf->lock, flags); 344 buf->tail->commit = buf->tail->used;
339 if (buf->tail != NULL)
340 buf->tail->commit = buf->tail->used;
341 spin_unlock_irqrestore(&buf->lock, flags);
342 schedule_work(&buf->work); 345 schedule_work(&buf->work);
343} 346}
344EXPORT_SYMBOL(tty_schedule_flip); 347EXPORT_SYMBOL(tty_schedule_flip);
@@ -354,8 +357,6 @@ EXPORT_SYMBOL(tty_schedule_flip);
354 * accounted for as ready for normal characters. This is used for drivers 357 * accounted for as ready for normal characters. This is used for drivers
355 * that need their own block copy routines into the buffer. There is no 358 * that need their own block copy routines into the buffer. There is no
356 * guarantee the buffer is a DMA target! 359 * guarantee the buffer is a DMA target!
357 *
358 * Locking: May call functions taking port->buf.lock
359 */ 360 */
360 361
361int tty_prepare_flip_string(struct tty_port *port, unsigned char **chars, 362int tty_prepare_flip_string(struct tty_port *port, unsigned char **chars,
@@ -364,8 +365,8 @@ int tty_prepare_flip_string(struct tty_port *port, unsigned char **chars,
364 int space = tty_buffer_request_room(port, size); 365 int space = tty_buffer_request_room(port, size);
365 if (likely(space)) { 366 if (likely(space)) {
366 struct tty_buffer *tb = port->buf.tail; 367 struct tty_buffer *tb = port->buf.tail;
367 *chars = tb->char_buf_ptr + tb->used; 368 *chars = char_buf_ptr(tb, tb->used);
368 memset(tb->flag_buf_ptr + tb->used, TTY_NORMAL, space); 369 memset(flag_buf_ptr(tb, tb->used), TTY_NORMAL, space);
369 tb->used += space; 370 tb->used += space;
370 } 371 }
371 return space; 372 return space;
@@ -384,8 +385,6 @@ EXPORT_SYMBOL_GPL(tty_prepare_flip_string);
384 * accounted for as ready for characters. This is used for drivers 385 * accounted for as ready for characters. This is used for drivers
385 * that need their own block copy routines into the buffer. There is no 386 * that need their own block copy routines into the buffer. There is no
386 * guarantee the buffer is a DMA target! 387 * guarantee the buffer is a DMA target!
387 *
388 * Locking: May call functions taking port->buf.lock
389 */ 388 */
390 389
391int tty_prepare_flip_string_flags(struct tty_port *port, 390int tty_prepare_flip_string_flags(struct tty_port *port,
@@ -394,8 +393,8 @@ int tty_prepare_flip_string_flags(struct tty_port *port,
394 int space = tty_buffer_request_room(port, size); 393 int space = tty_buffer_request_room(port, size);
395 if (likely(space)) { 394 if (likely(space)) {
396 struct tty_buffer *tb = port->buf.tail; 395 struct tty_buffer *tb = port->buf.tail;
397 *chars = tb->char_buf_ptr + tb->used; 396 *chars = char_buf_ptr(tb, tb->used);
398 *flags = tb->flag_buf_ptr + tb->used; 397 *flags = flag_buf_ptr(tb, tb->used);
399 tb->used += space; 398 tb->used += space;
400 } 399 }
401 return space; 400 return space;
@@ -403,6 +402,23 @@ int tty_prepare_flip_string_flags(struct tty_port *port,
403EXPORT_SYMBOL_GPL(tty_prepare_flip_string_flags); 402EXPORT_SYMBOL_GPL(tty_prepare_flip_string_flags);
404 403
405 404
405static int
406receive_buf(struct tty_struct *tty, struct tty_buffer *head, int count)
407{
408 struct tty_ldisc *disc = tty->ldisc;
409 unsigned char *p = char_buf_ptr(head, head->read);
410 char *f = flag_buf_ptr(head, head->read);
411
412 if (disc->ops->receive_buf2)
413 count = disc->ops->receive_buf2(tty, p, f, count);
414 else {
415 count = min_t(int, count, tty->receive_room);
416 if (count)
417 disc->ops->receive_buf(tty, p, f, count);
418 }
419 head->read += count;
420 return count;
421}
406 422
407/** 423/**
408 * flush_to_ldisc 424 * flush_to_ldisc
@@ -411,9 +427,10 @@ EXPORT_SYMBOL_GPL(tty_prepare_flip_string_flags);
411 * This routine is called out of the software interrupt to flush data 427 * This routine is called out of the software interrupt to flush data
412 * from the buffer chain to the line discipline. 428 * from the buffer chain to the line discipline.
413 * 429 *
414 * Locking: holds tty->buf.lock to guard buffer list. Drops the lock 430 * The receive_buf method is single threaded for each tty instance.
415 * while invoking the line discipline receive_buf method. The 431 *
416 * receive_buf method is single threaded for each tty instance. 432 * Locking: takes buffer lock to ensure single-threaded flip buffer
433 * 'consumer'
417 */ 434 */
418 435
419static void flush_to_ldisc(struct work_struct *work) 436static void flush_to_ldisc(struct work_struct *work)
@@ -421,7 +438,6 @@ static void flush_to_ldisc(struct work_struct *work)
421 struct tty_port *port = container_of(work, struct tty_port, buf.work); 438 struct tty_port *port = container_of(work, struct tty_port, buf.work);
422 struct tty_bufhead *buf = &port->buf; 439 struct tty_bufhead *buf = &port->buf;
423 struct tty_struct *tty; 440 struct tty_struct *tty;
424 unsigned long flags;
425 struct tty_ldisc *disc; 441 struct tty_ldisc *disc;
426 442
427 tty = port->itty; 443 tty = port->itty;
@@ -429,52 +445,34 @@ static void flush_to_ldisc(struct work_struct *work)
429 return; 445 return;
430 446
431 disc = tty_ldisc_ref(tty); 447 disc = tty_ldisc_ref(tty);
432 if (disc == NULL) /* !TTY_LDISC */ 448 if (disc == NULL)
433 return; 449 return;
434 450
435 spin_lock_irqsave(&buf->lock, flags); 451 mutex_lock(&buf->lock);
436 452
437 if (!test_and_set_bit(TTYP_FLUSHING, &port->iflags)) { 453 while (1) {
438 struct tty_buffer *head; 454 struct tty_buffer *head = buf->head;
439 while ((head = buf->head) != NULL) { 455 int count;
440 int count; 456
441 char *char_buf; 457 /* Ldisc or user is trying to gain exclusive access */
442 unsigned char *flag_buf; 458 if (atomic_read(&buf->priority))
443 459 break;
444 count = head->commit - head->read; 460
445 if (!count) { 461 count = head->commit - head->read;
446 if (head->next == NULL) 462 if (!count) {
447 break; 463 if (head->next == NULL)
448 buf->head = head->next;
449 tty_buffer_free(port, head);
450 continue;
451 }
452 if (!tty->receive_room)
453 break;
454 if (count > tty->receive_room)
455 count = tty->receive_room;
456 char_buf = head->char_buf_ptr + head->read;
457 flag_buf = head->flag_buf_ptr + head->read;
458 head->read += count;
459 spin_unlock_irqrestore(&buf->lock, flags);
460 disc->ops->receive_buf(tty, char_buf,
461 flag_buf, count);
462 spin_lock_irqsave(&buf->lock, flags);
463 /* Ldisc or user is trying to flush the buffers.
464 We may have a deferred request to flush the
465 input buffer, if so pull the chain under the lock
466 and empty the queue */
467 if (test_bit(TTYP_FLUSHPENDING, &port->iflags)) {
468 __tty_buffer_flush(port);
469 clear_bit(TTYP_FLUSHPENDING, &port->iflags);
470 wake_up(&tty->read_wait);
471 break; 464 break;
472 } 465 buf->head = head->next;
466 tty_buffer_free(port, head);
467 continue;
473 } 468 }
474 clear_bit(TTYP_FLUSHING, &port->iflags); 469
470 count = receive_buf(tty, head, count);
471 if (!count)
472 break;
475 } 473 }
476 474
477 spin_unlock_irqrestore(&buf->lock, flags); 475 mutex_unlock(&buf->lock);
478 476
479 tty_ldisc_deref(disc); 477 tty_ldisc_deref(disc);
480} 478}
@@ -503,19 +501,13 @@ void tty_flush_to_ldisc(struct tty_struct *tty)
503 * 501 *
504 * In the event of the queue being busy for flipping the work will be 502 * In the event of the queue being busy for flipping the work will be
505 * held off and retried later. 503 * held off and retried later.
506 *
507 * Locking: tty buffer lock. Driver locks in low latency mode.
508 */ 504 */
509 505
510void tty_flip_buffer_push(struct tty_port *port) 506void tty_flip_buffer_push(struct tty_port *port)
511{ 507{
512 struct tty_bufhead *buf = &port->buf; 508 struct tty_bufhead *buf = &port->buf;
513 unsigned long flags;
514 509
515 spin_lock_irqsave(&buf->lock, flags); 510 buf->tail->commit = buf->tail->used;
516 if (buf->tail != NULL)
517 buf->tail->commit = buf->tail->used;
518 spin_unlock_irqrestore(&buf->lock, flags);
519 511
520 if (port->low_latency) 512 if (port->low_latency)
521 flush_to_ldisc(&buf->work); 513 flush_to_ldisc(&buf->work);
@@ -530,19 +522,18 @@ EXPORT_SYMBOL(tty_flip_buffer_push);
530 * 522 *
531 * Set up the initial state of the buffer management for a tty device. 523 * Set up the initial state of the buffer management for a tty device.
532 * Must be called before the other tty buffer functions are used. 524 * Must be called before the other tty buffer functions are used.
533 *
534 * Locking: none
535 */ 525 */
536 526
537void tty_buffer_init(struct tty_port *port) 527void tty_buffer_init(struct tty_port *port)
538{ 528{
539 struct tty_bufhead *buf = &port->buf; 529 struct tty_bufhead *buf = &port->buf;
540 530
541 spin_lock_init(&buf->lock); 531 mutex_init(&buf->lock);
542 buf->head = NULL; 532 tty_buffer_reset(&buf->sentinel, 0);
543 buf->tail = NULL; 533 buf->head = &buf->sentinel;
544 buf->free = NULL; 534 buf->tail = &buf->sentinel;
545 buf->memory_used = 0; 535 init_llist_head(&buf->free);
536 atomic_set(&buf->memory_used, 0);
537 atomic_set(&buf->priority, 0);
546 INIT_WORK(&buf->work, flush_to_ldisc); 538 INIT_WORK(&buf->work, flush_to_ldisc);
547} 539}
548
diff --git a/drivers/tty/tty_io.c b/drivers/tty/tty_io.c
index 366af832794b..a9355ce1c6d5 100644
--- a/drivers/tty/tty_io.c
+++ b/drivers/tty/tty_io.c
@@ -603,8 +603,8 @@ static int tty_signal_session_leader(struct tty_struct *tty, int exit_session)
603 * BTM 603 * BTM
604 * redirect lock for undoing redirection 604 * redirect lock for undoing redirection
605 * file list lock for manipulating list of ttys 605 * file list lock for manipulating list of ttys
606 * tty_ldisc_lock from called functions 606 * tty_ldiscs_lock from called functions
607 * termios_mutex resetting termios data 607 * termios_rwsem resetting termios data
608 * tasklist_lock to walk task list for hangup event 608 * tasklist_lock to walk task list for hangup event
609 * ->siglock to protect ->signal/->sighand 609 * ->siglock to protect ->signal/->sighand
610 */ 610 */
@@ -629,6 +629,11 @@ static void __tty_hangup(struct tty_struct *tty, int exit_session)
629 629
630 tty_lock(tty); 630 tty_lock(tty);
631 631
632 if (test_bit(TTY_HUPPED, &tty->flags)) {
633 tty_unlock(tty);
634 return;
635 }
636
632 /* some functions below drop BTM, so we need this bit */ 637 /* some functions below drop BTM, so we need this bit */
633 set_bit(TTY_HUPPING, &tty->flags); 638 set_bit(TTY_HUPPING, &tty->flags);
634 639
@@ -664,7 +669,6 @@ static void __tty_hangup(struct tty_struct *tty, int exit_session)
664 669
665 spin_lock_irq(&tty->ctrl_lock); 670 spin_lock_irq(&tty->ctrl_lock);
666 clear_bit(TTY_THROTTLED, &tty->flags); 671 clear_bit(TTY_THROTTLED, &tty->flags);
667 clear_bit(TTY_PUSH, &tty->flags);
668 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); 672 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
669 put_pid(tty->session); 673 put_pid(tty->session);
670 put_pid(tty->pgrp); 674 put_pid(tty->pgrp);
@@ -1388,8 +1392,7 @@ static int tty_reopen(struct tty_struct *tty)
1388 struct tty_driver *driver = tty->driver; 1392 struct tty_driver *driver = tty->driver;
1389 1393
1390 if (test_bit(TTY_CLOSING, &tty->flags) || 1394 if (test_bit(TTY_CLOSING, &tty->flags) ||
1391 test_bit(TTY_HUPPING, &tty->flags) || 1395 test_bit(TTY_HUPPING, &tty->flags))
1392 test_bit(TTY_LDISC_CHANGING, &tty->flags))
1393 return -EIO; 1396 return -EIO;
1394 1397
1395 if (driver->type == TTY_DRIVER_TYPE_PTY && 1398 if (driver->type == TTY_DRIVER_TYPE_PTY &&
@@ -1405,7 +1408,7 @@ static int tty_reopen(struct tty_struct *tty)
1405 } 1408 }
1406 tty->count++; 1409 tty->count++;
1407 1410
1408 WARN_ON(!test_bit(TTY_LDISC, &tty->flags)); 1411 WARN_ON(!tty->ldisc);
1409 1412
1410 return 0; 1413 return 0;
1411} 1414}
@@ -2202,7 +2205,7 @@ static int tty_fasync(int fd, struct file *filp, int on)
2202 * FIXME: does not honour flow control ?? 2205 * FIXME: does not honour flow control ??
2203 * 2206 *
2204 * Locking: 2207 * Locking:
2205 * Called functions take tty_ldisc_lock 2208 * Called functions take tty_ldiscs_lock
2206 * current->signal->tty check is safe without locks 2209 * current->signal->tty check is safe without locks
2207 * 2210 *
2208 * FIXME: may race normal receive processing 2211 * FIXME: may race normal receive processing
@@ -2231,7 +2234,7 @@ static int tiocsti(struct tty_struct *tty, char __user *p)
2231 * 2234 *
2232 * Copies the kernel idea of the window size into the user buffer. 2235 * Copies the kernel idea of the window size into the user buffer.
2233 * 2236 *
2234 * Locking: tty->termios_mutex is taken to ensure the winsize data 2237 * Locking: tty->winsize_mutex is taken to ensure the winsize data
2235 * is consistent. 2238 * is consistent.
2236 */ 2239 */
2237 2240
@@ -2239,9 +2242,9 @@ static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg)
2239{ 2242{
2240 int err; 2243 int err;
2241 2244
2242 mutex_lock(&tty->termios_mutex); 2245 mutex_lock(&tty->winsize_mutex);
2243 err = copy_to_user(arg, &tty->winsize, sizeof(*arg)); 2246 err = copy_to_user(arg, &tty->winsize, sizeof(*arg));
2244 mutex_unlock(&tty->termios_mutex); 2247 mutex_unlock(&tty->winsize_mutex);
2245 2248
2246 return err ? -EFAULT: 0; 2249 return err ? -EFAULT: 0;
2247} 2250}
@@ -2262,7 +2265,7 @@ int tty_do_resize(struct tty_struct *tty, struct winsize *ws)
2262 unsigned long flags; 2265 unsigned long flags;
2263 2266
2264 /* Lock the tty */ 2267 /* Lock the tty */
2265 mutex_lock(&tty->termios_mutex); 2268 mutex_lock(&tty->winsize_mutex);
2266 if (!memcmp(ws, &tty->winsize, sizeof(*ws))) 2269 if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
2267 goto done; 2270 goto done;
2268 /* Get the PID values and reference them so we can 2271 /* Get the PID values and reference them so we can
@@ -2277,7 +2280,7 @@ int tty_do_resize(struct tty_struct *tty, struct winsize *ws)
2277 2280
2278 tty->winsize = *ws; 2281 tty->winsize = *ws;
2279done: 2282done:
2280 mutex_unlock(&tty->termios_mutex); 2283 mutex_unlock(&tty->winsize_mutex);
2281 return 0; 2284 return 0;
2282} 2285}
2283EXPORT_SYMBOL(tty_do_resize); 2286EXPORT_SYMBOL(tty_do_resize);
@@ -3016,8 +3019,10 @@ void initialize_tty_struct(struct tty_struct *tty,
3016 tty->session = NULL; 3019 tty->session = NULL;
3017 tty->pgrp = NULL; 3020 tty->pgrp = NULL;
3018 mutex_init(&tty->legacy_mutex); 3021 mutex_init(&tty->legacy_mutex);
3019 mutex_init(&tty->termios_mutex); 3022 mutex_init(&tty->throttle_mutex);
3020 mutex_init(&tty->ldisc_mutex); 3023 init_rwsem(&tty->termios_rwsem);
3024 mutex_init(&tty->winsize_mutex);
3025 init_ldsem(&tty->ldisc_sem);
3021 init_waitqueue_head(&tty->write_wait); 3026 init_waitqueue_head(&tty->write_wait);
3022 init_waitqueue_head(&tty->read_wait); 3027 init_waitqueue_head(&tty->read_wait);
3023 INIT_WORK(&tty->hangup_work, do_tty_hangup); 3028 INIT_WORK(&tty->hangup_work, do_tty_hangup);
diff --git a/drivers/tty/tty_ioctl.c b/drivers/tty/tty_ioctl.c
index 3500d4114147..03ba081c5772 100644
--- a/drivers/tty/tty_ioctl.c
+++ b/drivers/tty/tty_ioctl.c
@@ -94,20 +94,20 @@ EXPORT_SYMBOL(tty_driver_flush_buffer);
94 * @tty: terminal 94 * @tty: terminal
95 * 95 *
96 * Indicate that a tty should stop transmitting data down the stack. 96 * Indicate that a tty should stop transmitting data down the stack.
97 * Takes the termios mutex to protect against parallel throttle/unthrottle 97 * Takes the termios rwsem to protect against parallel throttle/unthrottle
98 * and also to ensure the driver can consistently reference its own 98 * and also to ensure the driver can consistently reference its own
99 * termios data at this point when implementing software flow control. 99 * termios data at this point when implementing software flow control.
100 */ 100 */
101 101
102void tty_throttle(struct tty_struct *tty) 102void tty_throttle(struct tty_struct *tty)
103{ 103{
104 mutex_lock(&tty->termios_mutex); 104 down_write(&tty->termios_rwsem);
105 /* check TTY_THROTTLED first so it indicates our state */ 105 /* check TTY_THROTTLED first so it indicates our state */
106 if (!test_and_set_bit(TTY_THROTTLED, &tty->flags) && 106 if (!test_and_set_bit(TTY_THROTTLED, &tty->flags) &&
107 tty->ops->throttle) 107 tty->ops->throttle)
108 tty->ops->throttle(tty); 108 tty->ops->throttle(tty);
109 tty->flow_change = 0; 109 tty->flow_change = 0;
110 mutex_unlock(&tty->termios_mutex); 110 up_write(&tty->termios_rwsem);
111} 111}
112EXPORT_SYMBOL(tty_throttle); 112EXPORT_SYMBOL(tty_throttle);
113 113
@@ -116,7 +116,7 @@ EXPORT_SYMBOL(tty_throttle);
116 * @tty: terminal 116 * @tty: terminal
117 * 117 *
118 * Indicate that a tty may continue transmitting data down the stack. 118 * Indicate that a tty may continue transmitting data down the stack.
119 * Takes the termios mutex to protect against parallel throttle/unthrottle 119 * Takes the termios rwsem to protect against parallel throttle/unthrottle
120 * and also to ensure the driver can consistently reference its own 120 * and also to ensure the driver can consistently reference its own
121 * termios data at this point when implementing software flow control. 121 * termios data at this point when implementing software flow control.
122 * 122 *
@@ -126,12 +126,12 @@ EXPORT_SYMBOL(tty_throttle);
126 126
127void tty_unthrottle(struct tty_struct *tty) 127void tty_unthrottle(struct tty_struct *tty)
128{ 128{
129 mutex_lock(&tty->termios_mutex); 129 down_write(&tty->termios_rwsem);
130 if (test_and_clear_bit(TTY_THROTTLED, &tty->flags) && 130 if (test_and_clear_bit(TTY_THROTTLED, &tty->flags) &&
131 tty->ops->unthrottle) 131 tty->ops->unthrottle)
132 tty->ops->unthrottle(tty); 132 tty->ops->unthrottle(tty);
133 tty->flow_change = 0; 133 tty->flow_change = 0;
134 mutex_unlock(&tty->termios_mutex); 134 up_write(&tty->termios_rwsem);
135} 135}
136EXPORT_SYMBOL(tty_unthrottle); 136EXPORT_SYMBOL(tty_unthrottle);
137 137
@@ -151,7 +151,7 @@ int tty_throttle_safe(struct tty_struct *tty)
151{ 151{
152 int ret = 0; 152 int ret = 0;
153 153
154 mutex_lock(&tty->termios_mutex); 154 mutex_lock(&tty->throttle_mutex);
155 if (!test_bit(TTY_THROTTLED, &tty->flags)) { 155 if (!test_bit(TTY_THROTTLED, &tty->flags)) {
156 if (tty->flow_change != TTY_THROTTLE_SAFE) 156 if (tty->flow_change != TTY_THROTTLE_SAFE)
157 ret = 1; 157 ret = 1;
@@ -161,7 +161,7 @@ int tty_throttle_safe(struct tty_struct *tty)
161 tty->ops->throttle(tty); 161 tty->ops->throttle(tty);
162 } 162 }
163 } 163 }
164 mutex_unlock(&tty->termios_mutex); 164 mutex_unlock(&tty->throttle_mutex);
165 165
166 return ret; 166 return ret;
167} 167}
@@ -182,7 +182,7 @@ int tty_unthrottle_safe(struct tty_struct *tty)
182{ 182{
183 int ret = 0; 183 int ret = 0;
184 184
185 mutex_lock(&tty->termios_mutex); 185 mutex_lock(&tty->throttle_mutex);
186 if (test_bit(TTY_THROTTLED, &tty->flags)) { 186 if (test_bit(TTY_THROTTLED, &tty->flags)) {
187 if (tty->flow_change != TTY_UNTHROTTLE_SAFE) 187 if (tty->flow_change != TTY_UNTHROTTLE_SAFE)
188 ret = 1; 188 ret = 1;
@@ -192,7 +192,7 @@ int tty_unthrottle_safe(struct tty_struct *tty)
192 tty->ops->unthrottle(tty); 192 tty->ops->unthrottle(tty);
193 } 193 }
194 } 194 }
195 mutex_unlock(&tty->termios_mutex); 195 mutex_unlock(&tty->throttle_mutex);
196 196
197 return ret; 197 return ret;
198} 198}
@@ -468,7 +468,7 @@ EXPORT_SYMBOL_GPL(tty_termios_encode_baud_rate);
468 * @obad: output baud rate 468 * @obad: output baud rate
469 * 469 *
470 * Update the current termios data for the tty with the new speed 470 * Update the current termios data for the tty with the new speed
471 * settings. The caller must hold the termios_mutex for the tty in 471 * settings. The caller must hold the termios_rwsem for the tty in
472 * question. 472 * question.
473 */ 473 */
474 474
@@ -528,7 +528,7 @@ EXPORT_SYMBOL(tty_termios_hw_change);
528 * is a bit of layering violation here with n_tty in terms of the 528 * is a bit of layering violation here with n_tty in terms of the
529 * internal knowledge of this function. 529 * internal knowledge of this function.
530 * 530 *
531 * Locking: termios_mutex 531 * Locking: termios_rwsem
532 */ 532 */
533 533
534int tty_set_termios(struct tty_struct *tty, struct ktermios *new_termios) 534int tty_set_termios(struct tty_struct *tty, struct ktermios *new_termios)
@@ -544,7 +544,7 @@ int tty_set_termios(struct tty_struct *tty, struct ktermios *new_termios)
544 544
545 /* FIXME: we need to decide on some locking/ordering semantics 545 /* FIXME: we need to decide on some locking/ordering semantics
546 for the set_termios notification eventually */ 546 for the set_termios notification eventually */
547 mutex_lock(&tty->termios_mutex); 547 down_write(&tty->termios_rwsem);
548 old_termios = tty->termios; 548 old_termios = tty->termios;
549 tty->termios = *new_termios; 549 tty->termios = *new_termios;
550 unset_locked_termios(&tty->termios, &old_termios, &tty->termios_locked); 550 unset_locked_termios(&tty->termios, &old_termios, &tty->termios_locked);
@@ -586,7 +586,7 @@ int tty_set_termios(struct tty_struct *tty, struct ktermios *new_termios)
586 (ld->ops->set_termios)(tty, &old_termios); 586 (ld->ops->set_termios)(tty, &old_termios);
587 tty_ldisc_deref(ld); 587 tty_ldisc_deref(ld);
588 } 588 }
589 mutex_unlock(&tty->termios_mutex); 589 up_write(&tty->termios_rwsem);
590 return 0; 590 return 0;
591} 591}
592EXPORT_SYMBOL_GPL(tty_set_termios); 592EXPORT_SYMBOL_GPL(tty_set_termios);
@@ -601,7 +601,7 @@ EXPORT_SYMBOL_GPL(tty_set_termios);
601 * functions before using tty_set_termios to do the actual changes. 601 * functions before using tty_set_termios to do the actual changes.
602 * 602 *
603 * Locking: 603 * Locking:
604 * Called functions take ldisc and termios_mutex locks 604 * Called functions take ldisc and termios_rwsem locks
605 */ 605 */
606 606
607static int set_termios(struct tty_struct *tty, void __user *arg, int opt) 607static int set_termios(struct tty_struct *tty, void __user *arg, int opt)
@@ -613,9 +613,9 @@ static int set_termios(struct tty_struct *tty, void __user *arg, int opt)
613 if (retval) 613 if (retval)
614 return retval; 614 return retval;
615 615
616 mutex_lock(&tty->termios_mutex); 616 down_read(&tty->termios_rwsem);
617 tmp_termios = tty->termios; 617 tmp_termios = tty->termios;
618 mutex_unlock(&tty->termios_mutex); 618 up_read(&tty->termios_rwsem);
619 619
620 if (opt & TERMIOS_TERMIO) { 620 if (opt & TERMIOS_TERMIO) {
621 if (user_termio_to_kernel_termios(&tmp_termios, 621 if (user_termio_to_kernel_termios(&tmp_termios,
@@ -667,16 +667,16 @@ static int set_termios(struct tty_struct *tty, void __user *arg, int opt)
667 667
668static void copy_termios(struct tty_struct *tty, struct ktermios *kterm) 668static void copy_termios(struct tty_struct *tty, struct ktermios *kterm)
669{ 669{
670 mutex_lock(&tty->termios_mutex); 670 down_read(&tty->termios_rwsem);
671 *kterm = tty->termios; 671 *kterm = tty->termios;
672 mutex_unlock(&tty->termios_mutex); 672 up_read(&tty->termios_rwsem);
673} 673}
674 674
675static void copy_termios_locked(struct tty_struct *tty, struct ktermios *kterm) 675static void copy_termios_locked(struct tty_struct *tty, struct ktermios *kterm)
676{ 676{
677 mutex_lock(&tty->termios_mutex); 677 down_read(&tty->termios_rwsem);
678 *kterm = tty->termios_locked; 678 *kterm = tty->termios_locked;
679 mutex_unlock(&tty->termios_mutex); 679 up_read(&tty->termios_rwsem);
680} 680}
681 681
682static int get_termio(struct tty_struct *tty, struct termio __user *termio) 682static int get_termio(struct tty_struct *tty, struct termio __user *termio)
@@ -723,10 +723,10 @@ static int set_termiox(struct tty_struct *tty, void __user *arg, int opt)
723 return -ERESTARTSYS; 723 return -ERESTARTSYS;
724 } 724 }
725 725
726 mutex_lock(&tty->termios_mutex); 726 down_write(&tty->termios_rwsem);
727 if (tty->ops->set_termiox) 727 if (tty->ops->set_termiox)
728 tty->ops->set_termiox(tty, &tnew); 728 tty->ops->set_termiox(tty, &tnew);
729 mutex_unlock(&tty->termios_mutex); 729 up_write(&tty->termios_rwsem);
730 return 0; 730 return 0;
731} 731}
732 732
@@ -761,13 +761,13 @@ static int get_sgttyb(struct tty_struct *tty, struct sgttyb __user *sgttyb)
761{ 761{
762 struct sgttyb tmp; 762 struct sgttyb tmp;
763 763
764 mutex_lock(&tty->termios_mutex); 764 down_read(&tty->termios_rwsem);
765 tmp.sg_ispeed = tty->termios.c_ispeed; 765 tmp.sg_ispeed = tty->termios.c_ispeed;
766 tmp.sg_ospeed = tty->termios.c_ospeed; 766 tmp.sg_ospeed = tty->termios.c_ospeed;
767 tmp.sg_erase = tty->termios.c_cc[VERASE]; 767 tmp.sg_erase = tty->termios.c_cc[VERASE];
768 tmp.sg_kill = tty->termios.c_cc[VKILL]; 768 tmp.sg_kill = tty->termios.c_cc[VKILL];
769 tmp.sg_flags = get_sgflags(tty); 769 tmp.sg_flags = get_sgflags(tty);
770 mutex_unlock(&tty->termios_mutex); 770 up_read(&tty->termios_rwsem);
771 771
772 return copy_to_user(sgttyb, &tmp, sizeof(tmp)) ? -EFAULT : 0; 772 return copy_to_user(sgttyb, &tmp, sizeof(tmp)) ? -EFAULT : 0;
773} 773}
@@ -806,7 +806,7 @@ static void set_sgflags(struct ktermios *termios, int flags)
806 * Updates a terminal from the legacy BSD style terminal information 806 * Updates a terminal from the legacy BSD style terminal information
807 * structure. 807 * structure.
808 * 808 *
809 * Locking: termios_mutex 809 * Locking: termios_rwsem
810 */ 810 */
811 811
812static int set_sgttyb(struct tty_struct *tty, struct sgttyb __user *sgttyb) 812static int set_sgttyb(struct tty_struct *tty, struct sgttyb __user *sgttyb)
@@ -822,7 +822,7 @@ static int set_sgttyb(struct tty_struct *tty, struct sgttyb __user *sgttyb)
822 if (copy_from_user(&tmp, sgttyb, sizeof(tmp))) 822 if (copy_from_user(&tmp, sgttyb, sizeof(tmp)))
823 return -EFAULT; 823 return -EFAULT;
824 824
825 mutex_lock(&tty->termios_mutex); 825 down_write(&tty->termios_rwsem);
826 termios = tty->termios; 826 termios = tty->termios;
827 termios.c_cc[VERASE] = tmp.sg_erase; 827 termios.c_cc[VERASE] = tmp.sg_erase;
828 termios.c_cc[VKILL] = tmp.sg_kill; 828 termios.c_cc[VKILL] = tmp.sg_kill;
@@ -832,7 +832,7 @@ static int set_sgttyb(struct tty_struct *tty, struct sgttyb __user *sgttyb)
832 tty_termios_encode_baud_rate(&termios, termios.c_ispeed, 832 tty_termios_encode_baud_rate(&termios, termios.c_ispeed,
833 termios.c_ospeed); 833 termios.c_ospeed);
834#endif 834#endif
835 mutex_unlock(&tty->termios_mutex); 835 up_write(&tty->termios_rwsem);
836 tty_set_termios(tty, &termios); 836 tty_set_termios(tty, &termios);
837 return 0; 837 return 0;
838} 838}
@@ -843,14 +843,14 @@ static int get_tchars(struct tty_struct *tty, struct tchars __user *tchars)
843{ 843{
844 struct tchars tmp; 844 struct tchars tmp;
845 845
846 mutex_lock(&tty->termios_mutex); 846 down_read(&tty->termios_rwsem);
847 tmp.t_intrc = tty->termios.c_cc[VINTR]; 847 tmp.t_intrc = tty->termios.c_cc[VINTR];
848 tmp.t_quitc = tty->termios.c_cc[VQUIT]; 848 tmp.t_quitc = tty->termios.c_cc[VQUIT];
849 tmp.t_startc = tty->termios.c_cc[VSTART]; 849 tmp.t_startc = tty->termios.c_cc[VSTART];
850 tmp.t_stopc = tty->termios.c_cc[VSTOP]; 850 tmp.t_stopc = tty->termios.c_cc[VSTOP];
851 tmp.t_eofc = tty->termios.c_cc[VEOF]; 851 tmp.t_eofc = tty->termios.c_cc[VEOF];
852 tmp.t_brkc = tty->termios.c_cc[VEOL2]; /* what is brkc anyway? */ 852 tmp.t_brkc = tty->termios.c_cc[VEOL2]; /* what is brkc anyway? */
853 mutex_unlock(&tty->termios_mutex); 853 up_read(&tty->termios_rwsem);
854 return copy_to_user(tchars, &tmp, sizeof(tmp)) ? -EFAULT : 0; 854 return copy_to_user(tchars, &tmp, sizeof(tmp)) ? -EFAULT : 0;
855} 855}
856 856
@@ -860,14 +860,14 @@ static int set_tchars(struct tty_struct *tty, struct tchars __user *tchars)
860 860
861 if (copy_from_user(&tmp, tchars, sizeof(tmp))) 861 if (copy_from_user(&tmp, tchars, sizeof(tmp)))
862 return -EFAULT; 862 return -EFAULT;
863 mutex_lock(&tty->termios_mutex); 863 down_write(&tty->termios_rwsem);
864 tty->termios.c_cc[VINTR] = tmp.t_intrc; 864 tty->termios.c_cc[VINTR] = tmp.t_intrc;
865 tty->termios.c_cc[VQUIT] = tmp.t_quitc; 865 tty->termios.c_cc[VQUIT] = tmp.t_quitc;
866 tty->termios.c_cc[VSTART] = tmp.t_startc; 866 tty->termios.c_cc[VSTART] = tmp.t_startc;
867 tty->termios.c_cc[VSTOP] = tmp.t_stopc; 867 tty->termios.c_cc[VSTOP] = tmp.t_stopc;
868 tty->termios.c_cc[VEOF] = tmp.t_eofc; 868 tty->termios.c_cc[VEOF] = tmp.t_eofc;
869 tty->termios.c_cc[VEOL2] = tmp.t_brkc; /* what is brkc anyway? */ 869 tty->termios.c_cc[VEOL2] = tmp.t_brkc; /* what is brkc anyway? */
870 mutex_unlock(&tty->termios_mutex); 870 up_write(&tty->termios_rwsem);
871 return 0; 871 return 0;
872} 872}
873#endif 873#endif
@@ -877,7 +877,7 @@ static int get_ltchars(struct tty_struct *tty, struct ltchars __user *ltchars)
877{ 877{
878 struct ltchars tmp; 878 struct ltchars tmp;
879 879
880 mutex_lock(&tty->termios_mutex); 880 down_read(&tty->termios_rwsem);
881 tmp.t_suspc = tty->termios.c_cc[VSUSP]; 881 tmp.t_suspc = tty->termios.c_cc[VSUSP];
882 /* what is dsuspc anyway? */ 882 /* what is dsuspc anyway? */
883 tmp.t_dsuspc = tty->termios.c_cc[VSUSP]; 883 tmp.t_dsuspc = tty->termios.c_cc[VSUSP];
@@ -886,7 +886,7 @@ static int get_ltchars(struct tty_struct *tty, struct ltchars __user *ltchars)
886 tmp.t_flushc = tty->termios.c_cc[VEOL2]; 886 tmp.t_flushc = tty->termios.c_cc[VEOL2];
887 tmp.t_werasc = tty->termios.c_cc[VWERASE]; 887 tmp.t_werasc = tty->termios.c_cc[VWERASE];
888 tmp.t_lnextc = tty->termios.c_cc[VLNEXT]; 888 tmp.t_lnextc = tty->termios.c_cc[VLNEXT];
889 mutex_unlock(&tty->termios_mutex); 889 up_read(&tty->termios_rwsem);
890 return copy_to_user(ltchars, &tmp, sizeof(tmp)) ? -EFAULT : 0; 890 return copy_to_user(ltchars, &tmp, sizeof(tmp)) ? -EFAULT : 0;
891} 891}
892 892
@@ -897,7 +897,7 @@ static int set_ltchars(struct tty_struct *tty, struct ltchars __user *ltchars)
897 if (copy_from_user(&tmp, ltchars, sizeof(tmp))) 897 if (copy_from_user(&tmp, ltchars, sizeof(tmp)))
898 return -EFAULT; 898 return -EFAULT;
899 899
900 mutex_lock(&tty->termios_mutex); 900 down_write(&tty->termios_rwsem);
901 tty->termios.c_cc[VSUSP] = tmp.t_suspc; 901 tty->termios.c_cc[VSUSP] = tmp.t_suspc;
902 /* what is dsuspc anyway? */ 902 /* what is dsuspc anyway? */
903 tty->termios.c_cc[VEOL2] = tmp.t_dsuspc; 903 tty->termios.c_cc[VEOL2] = tmp.t_dsuspc;
@@ -906,7 +906,7 @@ static int set_ltchars(struct tty_struct *tty, struct ltchars __user *ltchars)
906 tty->termios.c_cc[VEOL2] = tmp.t_flushc; 906 tty->termios.c_cc[VEOL2] = tmp.t_flushc;
907 tty->termios.c_cc[VWERASE] = tmp.t_werasc; 907 tty->termios.c_cc[VWERASE] = tmp.t_werasc;
908 tty->termios.c_cc[VLNEXT] = tmp.t_lnextc; 908 tty->termios.c_cc[VLNEXT] = tmp.t_lnextc;
909 mutex_unlock(&tty->termios_mutex); 909 up_write(&tty->termios_rwsem);
910 return 0; 910 return 0;
911} 911}
912#endif 912#endif
@@ -946,7 +946,7 @@ static int send_prio_char(struct tty_struct *tty, char ch)
946 * @arg: enable/disable CLOCAL 946 * @arg: enable/disable CLOCAL
947 * 947 *
948 * Perform a change to the CLOCAL state and call into the driver 948 * Perform a change to the CLOCAL state and call into the driver
949 * layer to make it visible. All done with the termios mutex 949 * layer to make it visible. All done with the termios rwsem
950 */ 950 */
951 951
952static int tty_change_softcar(struct tty_struct *tty, int arg) 952static int tty_change_softcar(struct tty_struct *tty, int arg)
@@ -955,7 +955,7 @@ static int tty_change_softcar(struct tty_struct *tty, int arg)
955 int bit = arg ? CLOCAL : 0; 955 int bit = arg ? CLOCAL : 0;
956 struct ktermios old; 956 struct ktermios old;
957 957
958 mutex_lock(&tty->termios_mutex); 958 down_write(&tty->termios_rwsem);
959 old = tty->termios; 959 old = tty->termios;
960 tty->termios.c_cflag &= ~CLOCAL; 960 tty->termios.c_cflag &= ~CLOCAL;
961 tty->termios.c_cflag |= bit; 961 tty->termios.c_cflag |= bit;
@@ -963,7 +963,7 @@ static int tty_change_softcar(struct tty_struct *tty, int arg)
963 tty->ops->set_termios(tty, &old); 963 tty->ops->set_termios(tty, &old);
964 if ((tty->termios.c_cflag & CLOCAL) != bit) 964 if ((tty->termios.c_cflag & CLOCAL) != bit)
965 ret = -EINVAL; 965 ret = -EINVAL;
966 mutex_unlock(&tty->termios_mutex); 966 up_write(&tty->termios_rwsem);
967 return ret; 967 return ret;
968} 968}
969 969
@@ -1066,9 +1066,9 @@ int tty_mode_ioctl(struct tty_struct *tty, struct file *file,
1066 if (user_termios_to_kernel_termios(&kterm, 1066 if (user_termios_to_kernel_termios(&kterm,
1067 (struct termios __user *) arg)) 1067 (struct termios __user *) arg))
1068 return -EFAULT; 1068 return -EFAULT;
1069 mutex_lock(&real_tty->termios_mutex); 1069 down_write(&real_tty->termios_rwsem);
1070 real_tty->termios_locked = kterm; 1070 real_tty->termios_locked = kterm;
1071 mutex_unlock(&real_tty->termios_mutex); 1071 up_write(&real_tty->termios_rwsem);
1072 return 0; 1072 return 0;
1073#else 1073#else
1074 case TIOCGLCKTRMIOS: 1074 case TIOCGLCKTRMIOS:
@@ -1083,9 +1083,9 @@ int tty_mode_ioctl(struct tty_struct *tty, struct file *file,
1083 if (user_termios_to_kernel_termios_1(&kterm, 1083 if (user_termios_to_kernel_termios_1(&kterm,
1084 (struct termios __user *) arg)) 1084 (struct termios __user *) arg))
1085 return -EFAULT; 1085 return -EFAULT;
1086 mutex_lock(&real_tty->termios_mutex); 1086 down_write(&real_tty->termios_rwsem);
1087 real_tty->termios_locked = kterm; 1087 real_tty->termios_locked = kterm;
1088 mutex_unlock(&real_tty->termios_mutex); 1088 up_write(&real_tty->termios_rwsem);
1089 return ret; 1089 return ret;
1090#endif 1090#endif
1091#ifdef TCGETX 1091#ifdef TCGETX
@@ -1093,9 +1093,9 @@ int tty_mode_ioctl(struct tty_struct *tty, struct file *file,
1093 struct termiox ktermx; 1093 struct termiox ktermx;
1094 if (real_tty->termiox == NULL) 1094 if (real_tty->termiox == NULL)
1095 return -EINVAL; 1095 return -EINVAL;
1096 mutex_lock(&real_tty->termios_mutex); 1096 down_read(&real_tty->termios_rwsem);
1097 memcpy(&ktermx, real_tty->termiox, sizeof(struct termiox)); 1097 memcpy(&ktermx, real_tty->termiox, sizeof(struct termiox));
1098 mutex_unlock(&real_tty->termios_mutex); 1098 up_read(&real_tty->termios_rwsem);
1099 if (copy_to_user(p, &ktermx, sizeof(struct termiox))) 1099 if (copy_to_user(p, &ktermx, sizeof(struct termiox)))
1100 ret = -EFAULT; 1100 ret = -EFAULT;
1101 return ret; 1101 return ret;
diff --git a/drivers/tty/tty_ldisc.c b/drivers/tty/tty_ldisc.c
index 1afe192bef6a..6458e11e8e9d 100644
--- a/drivers/tty/tty_ldisc.c
+++ b/drivers/tty/tty_ldisc.c
@@ -31,14 +31,20 @@
31#define tty_ldisc_debug(tty, f, args...) 31#define tty_ldisc_debug(tty, f, args...)
32#endif 32#endif
33 33
34/* lockdep nested classes for tty->ldisc_sem */
35enum {
36 LDISC_SEM_NORMAL,
37 LDISC_SEM_OTHER,
38};
39
40
34/* 41/*
35 * This guards the refcounted line discipline lists. The lock 42 * This guards the refcounted line discipline lists. The lock
36 * must be taken with irqs off because there are hangup path 43 * must be taken with irqs off because there are hangup path
37 * callers who will do ldisc lookups and cannot sleep. 44 * callers who will do ldisc lookups and cannot sleep.
38 */ 45 */
39 46
40static DEFINE_RAW_SPINLOCK(tty_ldisc_lock); 47static DEFINE_RAW_SPINLOCK(tty_ldiscs_lock);
41static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_wait);
42/* Line disc dispatch table */ 48/* Line disc dispatch table */
43static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS]; 49static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS];
44 50
@@ -52,7 +58,7 @@ static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS];
52 * from this point onwards. 58 * from this point onwards.
53 * 59 *
54 * Locking: 60 * Locking:
55 * takes tty_ldisc_lock to guard against ldisc races 61 * takes tty_ldiscs_lock to guard against ldisc races
56 */ 62 */
57 63
58int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc) 64int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc)
@@ -63,11 +69,11 @@ int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc)
63 if (disc < N_TTY || disc >= NR_LDISCS) 69 if (disc < N_TTY || disc >= NR_LDISCS)
64 return -EINVAL; 70 return -EINVAL;
65 71
66 raw_spin_lock_irqsave(&tty_ldisc_lock, flags); 72 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
67 tty_ldiscs[disc] = new_ldisc; 73 tty_ldiscs[disc] = new_ldisc;
68 new_ldisc->num = disc; 74 new_ldisc->num = disc;
69 new_ldisc->refcount = 0; 75 new_ldisc->refcount = 0;
70 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags); 76 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
71 77
72 return ret; 78 return ret;
73} 79}
@@ -82,7 +88,7 @@ EXPORT_SYMBOL(tty_register_ldisc);
82 * currently in use. 88 * currently in use.
83 * 89 *
84 * Locking: 90 * Locking:
85 * takes tty_ldisc_lock to guard against ldisc races 91 * takes tty_ldiscs_lock to guard against ldisc races
86 */ 92 */
87 93
88int tty_unregister_ldisc(int disc) 94int tty_unregister_ldisc(int disc)
@@ -93,12 +99,12 @@ int tty_unregister_ldisc(int disc)
93 if (disc < N_TTY || disc >= NR_LDISCS) 99 if (disc < N_TTY || disc >= NR_LDISCS)
94 return -EINVAL; 100 return -EINVAL;
95 101
96 raw_spin_lock_irqsave(&tty_ldisc_lock, flags); 102 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
97 if (tty_ldiscs[disc]->refcount) 103 if (tty_ldiscs[disc]->refcount)
98 ret = -EBUSY; 104 ret = -EBUSY;
99 else 105 else
100 tty_ldiscs[disc] = NULL; 106 tty_ldiscs[disc] = NULL;
101 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags); 107 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
102 108
103 return ret; 109 return ret;
104} 110}
@@ -109,7 +115,7 @@ static struct tty_ldisc_ops *get_ldops(int disc)
109 unsigned long flags; 115 unsigned long flags;
110 struct tty_ldisc_ops *ldops, *ret; 116 struct tty_ldisc_ops *ldops, *ret;
111 117
112 raw_spin_lock_irqsave(&tty_ldisc_lock, flags); 118 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
113 ret = ERR_PTR(-EINVAL); 119 ret = ERR_PTR(-EINVAL);
114 ldops = tty_ldiscs[disc]; 120 ldops = tty_ldiscs[disc];
115 if (ldops) { 121 if (ldops) {
@@ -119,7 +125,7 @@ static struct tty_ldisc_ops *get_ldops(int disc)
119 ret = ldops; 125 ret = ldops;
120 } 126 }
121 } 127 }
122 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags); 128 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
123 return ret; 129 return ret;
124} 130}
125 131
@@ -127,10 +133,10 @@ static void put_ldops(struct tty_ldisc_ops *ldops)
127{ 133{
128 unsigned long flags; 134 unsigned long flags;
129 135
130 raw_spin_lock_irqsave(&tty_ldisc_lock, flags); 136 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
131 ldops->refcount--; 137 ldops->refcount--;
132 module_put(ldops->owner); 138 module_put(ldops->owner);
133 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags); 139 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
134} 140}
135 141
136/** 142/**
@@ -143,10 +149,10 @@ static void put_ldops(struct tty_ldisc_ops *ldops)
143 * available 149 * available
144 * 150 *
145 * Locking: 151 * Locking:
146 * takes tty_ldisc_lock to guard against ldisc races 152 * takes tty_ldiscs_lock to guard against ldisc races
147 */ 153 */
148 154
149static struct tty_ldisc *tty_ldisc_get(int disc) 155static struct tty_ldisc *tty_ldisc_get(struct tty_struct *tty, int disc)
150{ 156{
151 struct tty_ldisc *ld; 157 struct tty_ldisc *ld;
152 struct tty_ldisc_ops *ldops; 158 struct tty_ldisc_ops *ldops;
@@ -173,8 +179,7 @@ static struct tty_ldisc *tty_ldisc_get(int disc)
173 } 179 }
174 180
175 ld->ops = ldops; 181 ld->ops = ldops;
176 atomic_set(&ld->users, 1); 182 ld->tty = tty;
177 init_waitqueue_head(&ld->wq_idle);
178 183
179 return ld; 184 return ld;
180} 185}
@@ -186,20 +191,11 @@ static struct tty_ldisc *tty_ldisc_get(int disc)
186 */ 191 */
187static inline void tty_ldisc_put(struct tty_ldisc *ld) 192static inline void tty_ldisc_put(struct tty_ldisc *ld)
188{ 193{
189 unsigned long flags;
190
191 if (WARN_ON_ONCE(!ld)) 194 if (WARN_ON_ONCE(!ld))
192 return; 195 return;
193 196
194 raw_spin_lock_irqsave(&tty_ldisc_lock, flags); 197 put_ldops(ld->ops);
195
196 /* unreleased reader reference(s) will cause this WARN */
197 WARN_ON(!atomic_dec_and_test(&ld->users));
198
199 ld->ops->refcount--;
200 module_put(ld->ops->owner);
201 kfree(ld); 198 kfree(ld);
202 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
203} 199}
204 200
205static void *tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos) 201static void *tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos)
@@ -251,34 +247,6 @@ const struct file_operations tty_ldiscs_proc_fops = {
251}; 247};
252 248
253/** 249/**
254 * tty_ldisc_try - internal helper
255 * @tty: the tty
256 *
257 * Make a single attempt to grab and bump the refcount on
258 * the tty ldisc. Return 0 on failure or 1 on success. This is
259 * used to implement both the waiting and non waiting versions
260 * of tty_ldisc_ref
261 *
262 * Locking: takes tty_ldisc_lock
263 */
264
265static struct tty_ldisc *tty_ldisc_try(struct tty_struct *tty)
266{
267 unsigned long flags;
268 struct tty_ldisc *ld;
269
270 /* FIXME: this allows reference acquire after TTY_LDISC is cleared */
271 raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
272 ld = NULL;
273 if (test_bit(TTY_LDISC, &tty->flags) && tty->ldisc) {
274 ld = tty->ldisc;
275 atomic_inc(&ld->users);
276 }
277 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
278 return ld;
279}
280
281/**
282 * tty_ldisc_ref_wait - wait for the tty ldisc 250 * tty_ldisc_ref_wait - wait for the tty ldisc
283 * @tty: tty device 251 * @tty: tty device
284 * 252 *
@@ -291,16 +259,15 @@ static struct tty_ldisc *tty_ldisc_try(struct tty_struct *tty)
291 * against a discipline change, such as an existing ldisc reference 259 * against a discipline change, such as an existing ldisc reference
292 * (which we check for) 260 * (which we check for)
293 * 261 *
294 * Locking: call functions take tty_ldisc_lock 262 * Note: only callable from a file_operations routine (which
263 * guarantees tty->ldisc != NULL when the lock is acquired).
295 */ 264 */
296 265
297struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty) 266struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
298{ 267{
299 struct tty_ldisc *ld; 268 ldsem_down_read(&tty->ldisc_sem, MAX_SCHEDULE_TIMEOUT);
300 269 WARN_ON(!tty->ldisc);
301 /* wait_event is a macro */ 270 return tty->ldisc;
302 wait_event(tty_ldisc_wait, (ld = tty_ldisc_try(tty)) != NULL);
303 return ld;
304} 271}
305EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait); 272EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
306 273
@@ -311,13 +278,18 @@ EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
311 * Dereference the line discipline for the terminal and take a 278 * Dereference the line discipline for the terminal and take a
312 * reference to it. If the line discipline is in flux then 279 * reference to it. If the line discipline is in flux then
313 * return NULL. Can be called from IRQ and timer functions. 280 * return NULL. Can be called from IRQ and timer functions.
314 *
315 * Locking: called functions take tty_ldisc_lock
316 */ 281 */
317 282
318struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty) 283struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
319{ 284{
320 return tty_ldisc_try(tty); 285 struct tty_ldisc *ld = NULL;
286
287 if (ldsem_down_read_trylock(&tty->ldisc_sem)) {
288 ld = tty->ldisc;
289 if (!ld)
290 ldsem_up_read(&tty->ldisc_sem);
291 }
292 return ld;
321} 293}
322EXPORT_SYMBOL_GPL(tty_ldisc_ref); 294EXPORT_SYMBOL_GPL(tty_ldisc_ref);
323 295
@@ -327,48 +299,91 @@ EXPORT_SYMBOL_GPL(tty_ldisc_ref);
327 * 299 *
328 * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May 300 * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May
329 * be called in IRQ context. 301 * be called in IRQ context.
330 *
331 * Locking: takes tty_ldisc_lock
332 */ 302 */
333 303
334void tty_ldisc_deref(struct tty_ldisc *ld) 304void tty_ldisc_deref(struct tty_ldisc *ld)
335{ 305{
336 unsigned long flags; 306 ldsem_up_read(&ld->tty->ldisc_sem);
307}
308EXPORT_SYMBOL_GPL(tty_ldisc_deref);
337 309
338 if (WARN_ON_ONCE(!ld))
339 return;
340 310
341 raw_spin_lock_irqsave(&tty_ldisc_lock, flags); 311static inline int __lockfunc
342 /* 312tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout)
343 * WARNs if one-too-many reader references were released 313{
344 * - the last reference must be released with tty_ldisc_put 314 return ldsem_down_write(&tty->ldisc_sem, timeout);
345 */ 315}
346 WARN_ON(atomic_dec_and_test(&ld->users));
347 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
348 316
349 if (waitqueue_active(&ld->wq_idle)) 317static inline int __lockfunc
350 wake_up(&ld->wq_idle); 318tty_ldisc_lock_nested(struct tty_struct *tty, unsigned long timeout)
319{
320 return ldsem_down_write_nested(&tty->ldisc_sem,
321 LDISC_SEM_OTHER, timeout);
351} 322}
352EXPORT_SYMBOL_GPL(tty_ldisc_deref);
353 323
354/** 324static inline void tty_ldisc_unlock(struct tty_struct *tty)
355 * tty_ldisc_enable - allow ldisc use 325{
356 * @tty: terminal to activate ldisc on 326 return ldsem_up_write(&tty->ldisc_sem);
357 * 327}
358 * Set the TTY_LDISC flag when the line discipline can be called
359 * again. Do necessary wakeups for existing sleepers. Clear the LDISC
360 * changing flag to indicate any ldisc change is now over.
361 *
362 * Note: nobody should set the TTY_LDISC bit except via this function.
363 * Clearing directly is allowed.
364 */
365 328
366static void tty_ldisc_enable(struct tty_struct *tty) 329static int __lockfunc
330tty_ldisc_lock_pair_timeout(struct tty_struct *tty, struct tty_struct *tty2,
331 unsigned long timeout)
332{
333 int ret;
334
335 if (tty < tty2) {
336 ret = tty_ldisc_lock(tty, timeout);
337 if (ret) {
338 ret = tty_ldisc_lock_nested(tty2, timeout);
339 if (!ret)
340 tty_ldisc_unlock(tty);
341 }
342 } else {
343 /* if this is possible, it has lots of implications */
344 WARN_ON_ONCE(tty == tty2);
345 if (tty2 && tty != tty2) {
346 ret = tty_ldisc_lock(tty2, timeout);
347 if (ret) {
348 ret = tty_ldisc_lock_nested(tty, timeout);
349 if (!ret)
350 tty_ldisc_unlock(tty2);
351 }
352 } else
353 ret = tty_ldisc_lock(tty, timeout);
354 }
355
356 if (!ret)
357 return -EBUSY;
358
359 set_bit(TTY_LDISC_HALTED, &tty->flags);
360 if (tty2)
361 set_bit(TTY_LDISC_HALTED, &tty2->flags);
362 return 0;
363}
364
365static void __lockfunc
366tty_ldisc_lock_pair(struct tty_struct *tty, struct tty_struct *tty2)
367{
368 tty_ldisc_lock_pair_timeout(tty, tty2, MAX_SCHEDULE_TIMEOUT);
369}
370
371static void __lockfunc tty_ldisc_unlock_pair(struct tty_struct *tty,
372 struct tty_struct *tty2)
373{
374 tty_ldisc_unlock(tty);
375 if (tty2)
376 tty_ldisc_unlock(tty2);
377}
378
379static void __lockfunc tty_ldisc_enable_pair(struct tty_struct *tty,
380 struct tty_struct *tty2)
367{ 381{
368 clear_bit(TTY_LDISC_HALTED, &tty->flags); 382 clear_bit(TTY_LDISC_HALTED, &tty->flags);
369 set_bit(TTY_LDISC, &tty->flags); 383 if (tty2)
370 clear_bit(TTY_LDISC_CHANGING, &tty->flags); 384 clear_bit(TTY_LDISC_HALTED, &tty2->flags);
371 wake_up(&tty_ldisc_wait); 385
386 tty_ldisc_unlock_pair(tty, tty2);
372} 387}
373 388
374/** 389/**
@@ -400,14 +415,14 @@ EXPORT_SYMBOL_GPL(tty_ldisc_flush);
400 * they are not on hot paths so a little discipline won't do 415 * they are not on hot paths so a little discipline won't do
401 * any harm. 416 * any harm.
402 * 417 *
403 * Locking: takes termios_mutex 418 * Locking: takes termios_rwsem
404 */ 419 */
405 420
406static void tty_set_termios_ldisc(struct tty_struct *tty, int num) 421static void tty_set_termios_ldisc(struct tty_struct *tty, int num)
407{ 422{
408 mutex_lock(&tty->termios_mutex); 423 down_write(&tty->termios_rwsem);
409 tty->termios.c_line = num; 424 tty->termios.c_line = num;
410 mutex_unlock(&tty->termios_mutex); 425 up_write(&tty->termios_rwsem);
411} 426}
412 427
413/** 428/**
@@ -468,14 +483,14 @@ static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old)
468 int r; 483 int r;
469 484
470 /* There is an outstanding reference here so this is safe */ 485 /* There is an outstanding reference here so this is safe */
471 old = tty_ldisc_get(old->ops->num); 486 old = tty_ldisc_get(tty, old->ops->num);
472 WARN_ON(IS_ERR(old)); 487 WARN_ON(IS_ERR(old));
473 tty->ldisc = old; 488 tty->ldisc = old;
474 tty_set_termios_ldisc(tty, old->ops->num); 489 tty_set_termios_ldisc(tty, old->ops->num);
475 if (tty_ldisc_open(tty, old) < 0) { 490 if (tty_ldisc_open(tty, old) < 0) {
476 tty_ldisc_put(old); 491 tty_ldisc_put(old);
477 /* This driver is always present */ 492 /* This driver is always present */
478 new_ldisc = tty_ldisc_get(N_TTY); 493 new_ldisc = tty_ldisc_get(tty, N_TTY);
479 if (IS_ERR(new_ldisc)) 494 if (IS_ERR(new_ldisc))
480 panic("n_tty: get"); 495 panic("n_tty: get");
481 tty->ldisc = new_ldisc; 496 tty->ldisc = new_ldisc;
@@ -489,101 +504,6 @@ static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old)
489} 504}
490 505
491/** 506/**
492 * tty_ldisc_wait_idle - wait for the ldisc to become idle
493 * @tty: tty to wait for
494 * @timeout: for how long to wait at most
495 *
496 * Wait for the line discipline to become idle. The discipline must
497 * have been halted for this to guarantee it remains idle.
498 */
499static int tty_ldisc_wait_idle(struct tty_struct *tty, long timeout)
500{
501 long ret;
502 ret = wait_event_timeout(tty->ldisc->wq_idle,
503 atomic_read(&tty->ldisc->users) == 1, timeout);
504 return ret > 0 ? 0 : -EBUSY;
505}
506
507/**
508 * tty_ldisc_halt - shut down the line discipline
509 * @tty: tty device
510 * @o_tty: paired pty device (can be NULL)
511 * @timeout: # of jiffies to wait for ldisc refs to be released
512 *
513 * Shut down the line discipline and work queue for this tty device and
514 * its paired pty (if exists). Clearing the TTY_LDISC flag ensures
515 * no further references can be obtained, while waiting for existing
516 * references to be released ensures no more data is fed to the ldisc.
517 *
518 * You need to do a 'flush_scheduled_work()' (outside the ldisc_mutex)
519 * in order to make sure any currently executing ldisc work is also
520 * flushed.
521 */
522
523static int tty_ldisc_halt(struct tty_struct *tty, struct tty_struct *o_tty,
524 long timeout)
525{
526 int retval;
527
528 clear_bit(TTY_LDISC, &tty->flags);
529 if (o_tty)
530 clear_bit(TTY_LDISC, &o_tty->flags);
531
532 retval = tty_ldisc_wait_idle(tty, timeout);
533 if (!retval && o_tty)
534 retval = tty_ldisc_wait_idle(o_tty, timeout);
535 if (retval)
536 return retval;
537
538 set_bit(TTY_LDISC_HALTED, &tty->flags);
539 if (o_tty)
540 set_bit(TTY_LDISC_HALTED, &o_tty->flags);
541
542 return 0;
543}
544
545/**
546 * tty_ldisc_hangup_halt - halt the line discipline for hangup
547 * @tty: tty being hung up
548 *
549 * Shut down the line discipline and work queue for the tty device
550 * being hungup. Clear the TTY_LDISC flag to ensure no further
551 * references can be obtained and wait for remaining references to be
552 * released to ensure no more data is fed to this ldisc.
553 * Caller must hold legacy and ->ldisc_mutex.
554 *
555 * NB: tty_set_ldisc() is prevented from changing the ldisc concurrently
556 * with this function by checking the TTY_HUPPING flag.
557 */
558static bool tty_ldisc_hangup_halt(struct tty_struct *tty)
559{
560 char cur_n[TASK_COMM_LEN], tty_n[64];
561 long timeout = 3 * HZ;
562
563 clear_bit(TTY_LDISC, &tty->flags);
564
565 if (tty->ldisc) { /* Not yet closed */
566 tty_unlock(tty);
567
568 while (tty_ldisc_wait_idle(tty, timeout) == -EBUSY) {
569 timeout = MAX_SCHEDULE_TIMEOUT;
570 printk_ratelimited(KERN_WARNING
571 "%s: waiting (%s) for %s took too long, but we keep waiting...\n",
572 __func__, get_task_comm(cur_n, current),
573 tty_name(tty, tty_n));
574 }
575
576 set_bit(TTY_LDISC_HALTED, &tty->flags);
577
578 /* must reacquire both locks and preserve lock order */
579 mutex_unlock(&tty->ldisc_mutex);
580 tty_lock(tty);
581 mutex_lock(&tty->ldisc_mutex);
582 }
583 return !!tty->ldisc;
584}
585
586/**
587 * tty_set_ldisc - set line discipline 507 * tty_set_ldisc - set line discipline
588 * @tty: the terminal to set 508 * @tty: the terminal to set
589 * @ldisc: the line discipline 509 * @ldisc: the line discipline
@@ -592,110 +512,49 @@ static bool tty_ldisc_hangup_halt(struct tty_struct *tty)
592 * context. The ldisc change logic has to protect itself against any 512 * context. The ldisc change logic has to protect itself against any
593 * overlapping ldisc change (including on the other end of pty pairs), 513 * overlapping ldisc change (including on the other end of pty pairs),
594 * the close of one side of a tty/pty pair, and eventually hangup. 514 * the close of one side of a tty/pty pair, and eventually hangup.
595 *
596 * Locking: takes tty_ldisc_lock, termios_mutex
597 */ 515 */
598 516
599int tty_set_ldisc(struct tty_struct *tty, int ldisc) 517int tty_set_ldisc(struct tty_struct *tty, int ldisc)
600{ 518{
601 int retval; 519 int retval;
602 struct tty_ldisc *o_ldisc, *new_ldisc; 520 struct tty_ldisc *old_ldisc, *new_ldisc;
603 struct tty_struct *o_tty; 521 struct tty_struct *o_tty = tty->link;
604 522
605 new_ldisc = tty_ldisc_get(ldisc); 523 new_ldisc = tty_ldisc_get(tty, ldisc);
606 if (IS_ERR(new_ldisc)) 524 if (IS_ERR(new_ldisc))
607 return PTR_ERR(new_ldisc); 525 return PTR_ERR(new_ldisc);
608 526
609 tty_lock(tty); 527 retval = tty_ldisc_lock_pair_timeout(tty, o_tty, 5 * HZ);
610 /* 528 if (retval) {
611 * We need to look at the tty locking here for pty/tty pairs 529 tty_ldisc_put(new_ldisc);
612 * when both sides try to change in parallel. 530 return retval;
613 */ 531 }
614
615 o_tty = tty->link; /* o_tty is the pty side or NULL */
616
617 532
618 /* 533 /*
619 * Check the no-op case 534 * Check the no-op case
620 */ 535 */
621 536
622 if (tty->ldisc->ops->num == ldisc) { 537 if (tty->ldisc->ops->num == ldisc) {
623 tty_unlock(tty); 538 tty_ldisc_enable_pair(tty, o_tty);
624 tty_ldisc_put(new_ldisc); 539 tty_ldisc_put(new_ldisc);
625 return 0; 540 return 0;
626 } 541 }
627 542
628 mutex_lock(&tty->ldisc_mutex); 543 old_ldisc = tty->ldisc;
629
630 /*
631 * We could be midstream of another ldisc change which has
632 * dropped the lock during processing. If so we need to wait.
633 */
634
635 while (test_bit(TTY_LDISC_CHANGING, &tty->flags)) {
636 mutex_unlock(&tty->ldisc_mutex);
637 tty_unlock(tty);
638 wait_event(tty_ldisc_wait,
639 test_bit(TTY_LDISC_CHANGING, &tty->flags) == 0);
640 tty_lock(tty);
641 mutex_lock(&tty->ldisc_mutex);
642 }
643
644 set_bit(TTY_LDISC_CHANGING, &tty->flags);
645
646 /*
647 * No more input please, we are switching. The new ldisc
648 * will update this value in the ldisc open function
649 */
650
651 tty->receive_room = 0;
652
653 o_ldisc = tty->ldisc;
654
655 tty_unlock(tty);
656 /*
657 * Make sure we don't change while someone holds a
658 * reference to the line discipline. The TTY_LDISC bit
659 * prevents anyone taking a reference once it is clear.
660 * We need the lock to avoid racing reference takers.
661 *
662 * We must clear the TTY_LDISC bit here to avoid a livelock
663 * with a userspace app continually trying to use the tty in
664 * parallel to the change and re-referencing the tty.
665 */
666
667 retval = tty_ldisc_halt(tty, o_tty, 5 * HZ);
668
669 /*
670 * Wait for hangup to complete, if pending.
671 * We must drop the mutex here in case a hangup is also in process.
672 */
673
674 mutex_unlock(&tty->ldisc_mutex);
675
676 flush_work(&tty->hangup_work);
677
678 tty_lock(tty); 544 tty_lock(tty);
679 mutex_lock(&tty->ldisc_mutex);
680
681 /* handle wait idle failure locked */
682 if (retval) {
683 tty_ldisc_put(new_ldisc);
684 goto enable;
685 }
686 545
687 if (test_bit(TTY_HUPPING, &tty->flags)) { 546 if (test_bit(TTY_HUPPING, &tty->flags) ||
547 test_bit(TTY_HUPPED, &tty->flags)) {
688 /* We were raced by the hangup method. It will have stomped 548 /* We were raced by the hangup method. It will have stomped
689 the ldisc data and closed the ldisc down */ 549 the ldisc data and closed the ldisc down */
690 clear_bit(TTY_LDISC_CHANGING, &tty->flags); 550 tty_ldisc_enable_pair(tty, o_tty);
691 mutex_unlock(&tty->ldisc_mutex);
692 tty_ldisc_put(new_ldisc); 551 tty_ldisc_put(new_ldisc);
693 tty_unlock(tty); 552 tty_unlock(tty);
694 return -EIO; 553 return -EIO;
695 } 554 }
696 555
697 /* Shutdown the current discipline. */ 556 /* Shutdown the old discipline. */
698 tty_ldisc_close(tty, o_ldisc); 557 tty_ldisc_close(tty, old_ldisc);
699 558
700 /* Now set up the new line discipline. */ 559 /* Now set up the new line discipline. */
701 tty->ldisc = new_ldisc; 560 tty->ldisc = new_ldisc;
@@ -705,26 +564,24 @@ int tty_set_ldisc(struct tty_struct *tty, int ldisc)
705 if (retval < 0) { 564 if (retval < 0) {
706 /* Back to the old one or N_TTY if we can't */ 565 /* Back to the old one or N_TTY if we can't */
707 tty_ldisc_put(new_ldisc); 566 tty_ldisc_put(new_ldisc);
708 tty_ldisc_restore(tty, o_ldisc); 567 tty_ldisc_restore(tty, old_ldisc);
709 } 568 }
710 569
711 /* At this point we hold a reference to the new ldisc and a 570 if (tty->ldisc->ops->num != old_ldisc->ops->num && tty->ops->set_ldisc)
712 a reference to the old ldisc. If we ended up flipping back
713 to the existing ldisc we have two references to it */
714
715 if (tty->ldisc->ops->num != o_ldisc->ops->num && tty->ops->set_ldisc)
716 tty->ops->set_ldisc(tty); 571 tty->ops->set_ldisc(tty);
717 572
718 tty_ldisc_put(o_ldisc); 573 /* At this point we hold a reference to the new ldisc and a
574 reference to the old ldisc, or we hold two references to
575 the old ldisc (if it was restored as part of error cleanup
576 above). In either case, releasing a single reference from
577 the old ldisc is correct. */
578
579 tty_ldisc_put(old_ldisc);
719 580
720enable:
721 /* 581 /*
722 * Allow ldisc referencing to occur again 582 * Allow ldisc referencing to occur again
723 */ 583 */
724 584 tty_ldisc_enable_pair(tty, o_tty);
725 tty_ldisc_enable(tty);
726 if (o_tty)
727 tty_ldisc_enable(o_tty);
728 585
729 /* Restart the work queue in case no characters kick it off. Safe if 586 /* Restart the work queue in case no characters kick it off. Safe if
730 already running */ 587 already running */
@@ -732,7 +589,6 @@ enable:
732 if (o_tty) 589 if (o_tty)
733 schedule_work(&o_tty->port->buf.work); 590 schedule_work(&o_tty->port->buf.work);
734 591
735 mutex_unlock(&tty->ldisc_mutex);
736 tty_unlock(tty); 592 tty_unlock(tty);
737 return retval; 593 return retval;
738} 594}
@@ -746,11 +602,11 @@ enable:
746 602
747static void tty_reset_termios(struct tty_struct *tty) 603static void tty_reset_termios(struct tty_struct *tty)
748{ 604{
749 mutex_lock(&tty->termios_mutex); 605 down_write(&tty->termios_rwsem);
750 tty->termios = tty->driver->init_termios; 606 tty->termios = tty->driver->init_termios;
751 tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios); 607 tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios);
752 tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios); 608 tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios);
753 mutex_unlock(&tty->termios_mutex); 609 up_write(&tty->termios_rwsem);
754} 610}
755 611
756 612
@@ -765,7 +621,7 @@ static void tty_reset_termios(struct tty_struct *tty)
765 621
766static int tty_ldisc_reinit(struct tty_struct *tty, int ldisc) 622static int tty_ldisc_reinit(struct tty_struct *tty, int ldisc)
767{ 623{
768 struct tty_ldisc *ld = tty_ldisc_get(ldisc); 624 struct tty_ldisc *ld = tty_ldisc_get(tty, ldisc);
769 625
770 if (IS_ERR(ld)) 626 if (IS_ERR(ld))
771 return -1; 627 return -1;
@@ -804,14 +660,8 @@ void tty_ldisc_hangup(struct tty_struct *tty)
804 660
805 tty_ldisc_debug(tty, "closing ldisc: %p\n", tty->ldisc); 661 tty_ldisc_debug(tty, "closing ldisc: %p\n", tty->ldisc);
806 662
807 /*
808 * FIXME! What are the locking issues here? This may me overdoing
809 * things... This question is especially important now that we've
810 * removed the irqlock.
811 */
812 ld = tty_ldisc_ref(tty); 663 ld = tty_ldisc_ref(tty);
813 if (ld != NULL) { 664 if (ld != NULL) {
814 /* We may have no line discipline at this point */
815 if (ld->ops->flush_buffer) 665 if (ld->ops->flush_buffer)
816 ld->ops->flush_buffer(tty); 666 ld->ops->flush_buffer(tty);
817 tty_driver_flush_buffer(tty); 667 tty_driver_flush_buffer(tty);
@@ -822,21 +672,22 @@ void tty_ldisc_hangup(struct tty_struct *tty)
822 ld->ops->hangup(tty); 672 ld->ops->hangup(tty);
823 tty_ldisc_deref(ld); 673 tty_ldisc_deref(ld);
824 } 674 }
825 /* 675
826 * FIXME: Once we trust the LDISC code better we can wait here for
827 * ldisc completion and fix the driver call race
828 */
829 wake_up_interruptible_poll(&tty->write_wait, POLLOUT); 676 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
830 wake_up_interruptible_poll(&tty->read_wait, POLLIN); 677 wake_up_interruptible_poll(&tty->read_wait, POLLIN);
678
679 tty_unlock(tty);
680
831 /* 681 /*
832 * Shutdown the current line discipline, and reset it to 682 * Shutdown the current line discipline, and reset it to
833 * N_TTY if need be. 683 * N_TTY if need be.
834 * 684 *
835 * Avoid racing set_ldisc or tty_ldisc_release 685 * Avoid racing set_ldisc or tty_ldisc_release
836 */ 686 */
837 mutex_lock(&tty->ldisc_mutex); 687 tty_ldisc_lock_pair(tty, tty->link);
688 tty_lock(tty);
838 689
839 if (tty_ldisc_hangup_halt(tty)) { 690 if (tty->ldisc) {
840 691
841 /* At this point we have a halted ldisc; we want to close it and 692 /* At this point we have a halted ldisc; we want to close it and
842 reopen a new ldisc. We could defer the reopen to the next 693 reopen a new ldisc. We could defer the reopen to the next
@@ -855,9 +706,8 @@ void tty_ldisc_hangup(struct tty_struct *tty)
855 BUG_ON(tty_ldisc_reinit(tty, N_TTY)); 706 BUG_ON(tty_ldisc_reinit(tty, N_TTY));
856 WARN_ON(tty_ldisc_open(tty, tty->ldisc)); 707 WARN_ON(tty_ldisc_open(tty, tty->ldisc));
857 } 708 }
858 tty_ldisc_enable(tty);
859 } 709 }
860 mutex_unlock(&tty->ldisc_mutex); 710 tty_ldisc_enable_pair(tty, tty->link);
861 if (reset) 711 if (reset)
862 tty_reset_termios(tty); 712 tty_reset_termios(tty);
863 713
@@ -889,15 +739,12 @@ int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty)
889 tty_ldisc_close(tty, ld); 739 tty_ldisc_close(tty, ld);
890 return retval; 740 return retval;
891 } 741 }
892 tty_ldisc_enable(o_tty);
893 } 742 }
894 tty_ldisc_enable(tty);
895 return 0; 743 return 0;
896} 744}
897 745
898static void tty_ldisc_kill(struct tty_struct *tty) 746static void tty_ldisc_kill(struct tty_struct *tty)
899{ 747{
900 mutex_lock(&tty->ldisc_mutex);
901 /* 748 /*
902 * Now kill off the ldisc 749 * Now kill off the ldisc
903 */ 750 */
@@ -908,7 +755,6 @@ static void tty_ldisc_kill(struct tty_struct *tty)
908 755
909 /* Ensure the next open requests the N_TTY ldisc */ 756 /* Ensure the next open requests the N_TTY ldisc */
910 tty_set_termios_ldisc(tty, N_TTY); 757 tty_set_termios_ldisc(tty, N_TTY);
911 mutex_unlock(&tty->ldisc_mutex);
912} 758}
913 759
914/** 760/**
@@ -930,15 +776,16 @@ void tty_ldisc_release(struct tty_struct *tty, struct tty_struct *o_tty)
930 776
931 tty_ldisc_debug(tty, "closing ldisc: %p\n", tty->ldisc); 777 tty_ldisc_debug(tty, "closing ldisc: %p\n", tty->ldisc);
932 778
933 tty_ldisc_halt(tty, o_tty, MAX_SCHEDULE_TIMEOUT); 779 tty_ldisc_lock_pair(tty, o_tty);
934
935 tty_lock_pair(tty, o_tty); 780 tty_lock_pair(tty, o_tty);
936 /* This will need doing differently if we need to lock */ 781
937 tty_ldisc_kill(tty); 782 tty_ldisc_kill(tty);
938 if (o_tty) 783 if (o_tty)
939 tty_ldisc_kill(o_tty); 784 tty_ldisc_kill(o_tty);
940 785
941 tty_unlock_pair(tty, o_tty); 786 tty_unlock_pair(tty, o_tty);
787 tty_ldisc_unlock_pair(tty, o_tty);
788
942 /* And the memory resources remaining (buffers, termios) will be 789 /* And the memory resources remaining (buffers, termios) will be
943 disposed of when the kref hits zero */ 790 disposed of when the kref hits zero */
944 791
@@ -955,7 +802,7 @@ void tty_ldisc_release(struct tty_struct *tty, struct tty_struct *o_tty)
955 802
956void tty_ldisc_init(struct tty_struct *tty) 803void tty_ldisc_init(struct tty_struct *tty)
957{ 804{
958 struct tty_ldisc *ld = tty_ldisc_get(N_TTY); 805 struct tty_ldisc *ld = tty_ldisc_get(tty, N_TTY);
959 if (IS_ERR(ld)) 806 if (IS_ERR(ld))
960 panic("n_tty: init_tty"); 807 panic("n_tty: init_tty");
961 tty->ldisc = ld; 808 tty->ldisc = ld;
diff --git a/drivers/tty/vt/keyboard.c b/drivers/tty/vt/keyboard.c
index a9af1b9ae160..d0e3a4497707 100644
--- a/drivers/tty/vt/keyboard.c
+++ b/drivers/tty/vt/keyboard.c
@@ -132,12 +132,6 @@ static int shift_state = 0;
132static unsigned char ledstate = 0xff; /* undefined */ 132static unsigned char ledstate = 0xff; /* undefined */
133static unsigned char ledioctl; 133static unsigned char ledioctl;
134 134
135static struct ledptr {
136 unsigned int *addr;
137 unsigned int mask;
138 unsigned char valid:1;
139} ledptrs[3];
140
141/* 135/*
142 * Notifier list for console keyboard events 136 * Notifier list for console keyboard events
143 */ 137 */
@@ -994,24 +988,11 @@ void setledstate(struct kbd_struct *kbd, unsigned int led)
994static inline unsigned char getleds(void) 988static inline unsigned char getleds(void)
995{ 989{
996 struct kbd_struct *kbd = kbd_table + fg_console; 990 struct kbd_struct *kbd = kbd_table + fg_console;
997 unsigned char leds;
998 int i;
999 991
1000 if (kbd->ledmode == LED_SHOW_IOCTL) 992 if (kbd->ledmode == LED_SHOW_IOCTL)
1001 return ledioctl; 993 return ledioctl;
1002 994
1003 leds = kbd->ledflagstate; 995 return kbd->ledflagstate;
1004
1005 if (kbd->ledmode == LED_SHOW_MEM) {
1006 for (i = 0; i < 3; i++)
1007 if (ledptrs[i].valid) {
1008 if (*ledptrs[i].addr & ledptrs[i].mask)
1009 leds |= (1 << i);
1010 else
1011 leds &= ~(1 << i);
1012 }
1013 }
1014 return leds;
1015} 996}
1016 997
1017static int kbd_update_leds_helper(struct input_handle *handle, void *data) 998static int kbd_update_leds_helper(struct input_handle *handle, void *data)
diff --git a/drivers/tty/vt/selection.c b/drivers/tty/vt/selection.c
index 60b7b6926059..ea27804d87af 100644
--- a/drivers/tty/vt/selection.c
+++ b/drivers/tty/vt/selection.c
@@ -24,6 +24,7 @@
24#include <linux/selection.h> 24#include <linux/selection.h>
25#include <linux/tiocl.h> 25#include <linux/tiocl.h>
26#include <linux/console.h> 26#include <linux/console.h>
27#include <linux/tty_flip.h>
27 28
28/* Don't take this from <ctype.h>: 011-015 on the screen aren't spaces */ 29/* Don't take this from <ctype.h>: 011-015 on the screen aren't spaces */
29#define isspace(c) ((c) == ' ') 30#define isspace(c) ((c) == ' ')
@@ -346,8 +347,8 @@ int paste_selection(struct tty_struct *tty)
346 console_unlock(); 347 console_unlock();
347 348
348 ld = tty_ldisc_ref_wait(tty); 349 ld = tty_ldisc_ref_wait(tty);
350 tty_buffer_lock_exclusive(&vc->port);
349 351
350 /* FIXME: this is completely unsafe */
351 add_wait_queue(&vc->paste_wait, &wait); 352 add_wait_queue(&vc->paste_wait, &wait);
352 while (sel_buffer && sel_buffer_lth > pasted) { 353 while (sel_buffer && sel_buffer_lth > pasted) {
353 set_current_state(TASK_INTERRUPTIBLE); 354 set_current_state(TASK_INTERRUPTIBLE);
@@ -356,13 +357,14 @@ int paste_selection(struct tty_struct *tty)
356 continue; 357 continue;
357 } 358 }
358 count = sel_buffer_lth - pasted; 359 count = sel_buffer_lth - pasted;
359 count = min(count, tty->receive_room); 360 count = tty_ldisc_receive_buf(ld, sel_buffer + pasted, NULL,
360 ld->ops->receive_buf(tty, sel_buffer + pasted, NULL, count); 361 count);
361 pasted += count; 362 pasted += count;
362 } 363 }
363 remove_wait_queue(&vc->paste_wait, &wait); 364 remove_wait_queue(&vc->paste_wait, &wait);
364 __set_current_state(TASK_RUNNING); 365 __set_current_state(TASK_RUNNING);
365 366
367 tty_buffer_unlock_exclusive(&vc->port);
366 tty_ldisc_deref(ld); 368 tty_ldisc_deref(ld);
367 return 0; 369 return 0;
368} 370}
diff --git a/drivers/tty/vt/vt.c b/drivers/tty/vt/vt.c
index c677829baa8b..9a8e8c5a0c73 100644
--- a/drivers/tty/vt/vt.c
+++ b/drivers/tty/vt/vt.c
@@ -828,7 +828,7 @@ static inline int resize_screen(struct vc_data *vc, int width, int height,
828 * If the caller passes a tty structure then update the termios winsize 828 * If the caller passes a tty structure then update the termios winsize
829 * information and perform any necessary signal handling. 829 * information and perform any necessary signal handling.
830 * 830 *
831 * Caller must hold the console semaphore. Takes the termios mutex and 831 * Caller must hold the console semaphore. Takes the termios rwsem and
832 * ctrl_lock of the tty IFF a tty is passed. 832 * ctrl_lock of the tty IFF a tty is passed.
833 */ 833 */
834 834
@@ -972,7 +972,7 @@ int vc_resize(struct vc_data *vc, unsigned int cols, unsigned int rows)
972 * the actual work. 972 * the actual work.
973 * 973 *
974 * Takes the console sem and the called methods then take the tty 974 * Takes the console sem and the called methods then take the tty
975 * termios_mutex and the tty ctrl_lock in that order. 975 * termios_rwsem and the tty ctrl_lock in that order.
976 */ 976 */
977static int vt_resize(struct tty_struct *tty, struct winsize *ws) 977static int vt_resize(struct tty_struct *tty, struct winsize *ws)
978{ 978{
@@ -2809,8 +2809,10 @@ static void con_shutdown(struct tty_struct *tty)
2809 console_unlock(); 2809 console_unlock();
2810} 2810}
2811 2811
2812static int default_color = 7; /* white */
2812static int default_italic_color = 2; // green (ASCII) 2813static int default_italic_color = 2; // green (ASCII)
2813static int default_underline_color = 3; // cyan (ASCII) 2814static int default_underline_color = 3; // cyan (ASCII)
2815module_param_named(color, default_color, int, S_IRUGO | S_IWUSR);
2814module_param_named(italic, default_italic_color, int, S_IRUGO | S_IWUSR); 2816module_param_named(italic, default_italic_color, int, S_IRUGO | S_IWUSR);
2815module_param_named(underline, default_underline_color, int, S_IRUGO | S_IWUSR); 2817module_param_named(underline, default_underline_color, int, S_IRUGO | S_IWUSR);
2816 2818
@@ -2832,7 +2834,7 @@ static void vc_init(struct vc_data *vc, unsigned int rows,
2832 vc->vc_palette[k++] = default_grn[j] ; 2834 vc->vc_palette[k++] = default_grn[j] ;
2833 vc->vc_palette[k++] = default_blu[j] ; 2835 vc->vc_palette[k++] = default_blu[j] ;
2834 } 2836 }
2835 vc->vc_def_color = 0x07; /* white */ 2837 vc->vc_def_color = default_color;
2836 vc->vc_ulcolor = default_underline_color; 2838 vc->vc_ulcolor = default_underline_color;
2837 vc->vc_itcolor = default_italic_color; 2839 vc->vc_itcolor = default_italic_color;
2838 vc->vc_halfcolor = 0x08; /* grey */ 2840 vc->vc_halfcolor = 0x08; /* grey */