LCOV - code coverage report
Current view: top level - drivers/tty/serial - amba-pl011.c (source / functions) Hit Total Coverage
Test: Real Lines: 315 855 36.8 %
Date: 2020-10-17 15:46:16 Functions: 3 82 3.7 %
Legend: Neither, QEMU, Real, Both Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : // SPDX-License-Identifier: GPL-2.0+
       2                 :            : /*
       3                 :            :  *  Driver for AMBA serial ports
       4                 :            :  *
       5                 :            :  *  Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
       6                 :            :  *
       7                 :            :  *  Copyright 1999 ARM Limited
       8                 :            :  *  Copyright (C) 2000 Deep Blue Solutions Ltd.
       9                 :            :  *  Copyright (C) 2010 ST-Ericsson SA
      10                 :            :  *
      11                 :            :  * This is a generic driver for ARM AMBA-type serial ports.  They
      12                 :            :  * have a lot of 16550-like features, but are not register compatible.
      13                 :            :  * Note that although they do have CTS, DCD and DSR inputs, they do
      14                 :            :  * not have an RI input, nor do they have DTR or RTS outputs.  If
      15                 :            :  * required, these have to be supplied via some other means (eg, GPIO)
      16                 :            :  * and hooked into this driver.
      17                 :            :  */
      18                 :            : 
      19                 :            : 
      20                 :            : #if defined(CONFIG_SERIAL_AMBA_PL011_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
      21                 :            : #define SUPPORT_SYSRQ
      22                 :            : #endif
      23                 :            : 
      24                 :            : #include <linux/module.h>
      25                 :            : #include <linux/ioport.h>
      26                 :            : #include <linux/init.h>
      27                 :            : #include <linux/console.h>
      28                 :            : #include <linux/sysrq.h>
      29                 :            : #include <linux/device.h>
      30                 :            : #include <linux/tty.h>
      31                 :            : #include <linux/tty_flip.h>
      32                 :            : #include <linux/serial_core.h>
      33                 :            : #include <linux/serial.h>
      34                 :            : #include <linux/amba/bus.h>
      35                 :            : #include <linux/amba/serial.h>
      36                 :            : #include <linux/clk.h>
      37                 :            : #include <linux/slab.h>
      38                 :            : #include <linux/dmaengine.h>
      39                 :            : #include <linux/dma-mapping.h>
      40                 :            : #include <linux/scatterlist.h>
      41                 :            : #include <linux/delay.h>
      42                 :            : #include <linux/types.h>
      43                 :            : #include <linux/of.h>
      44                 :            : #include <linux/of_device.h>
      45                 :            : #include <linux/pinctrl/consumer.h>
      46                 :            : #include <linux/sizes.h>
      47                 :            : #include <linux/io.h>
      48                 :            : #include <linux/acpi.h>
      49                 :            : 
      50                 :            : #include "amba-pl011.h"
      51                 :            : 
      52                 :            : #define UART_NR                 14
      53                 :            : 
      54                 :            : #define SERIAL_AMBA_MAJOR       204
      55                 :            : #define SERIAL_AMBA_MINOR       64
      56                 :            : #define SERIAL_AMBA_NR          UART_NR
      57                 :            : 
      58                 :            : #define AMBA_ISR_PASS_LIMIT     256
      59                 :            : 
      60                 :            : #define UART_DR_ERROR           (UART011_DR_OE|UART011_DR_BE|UART011_DR_PE|UART011_DR_FE)
      61                 :            : #define UART_DUMMY_DR_RX        (1 << 16)
      62                 :            : 
      63                 :            : static u16 pl011_std_offsets[REG_ARRAY_SIZE] = {
      64                 :            :         [REG_DR] = UART01x_DR,
      65                 :            :         [REG_FR] = UART01x_FR,
      66                 :            :         [REG_LCRH_RX] = UART011_LCRH,
      67                 :            :         [REG_LCRH_TX] = UART011_LCRH,
      68                 :            :         [REG_IBRD] = UART011_IBRD,
      69                 :            :         [REG_FBRD] = UART011_FBRD,
      70                 :            :         [REG_CR] = UART011_CR,
      71                 :            :         [REG_IFLS] = UART011_IFLS,
      72                 :            :         [REG_IMSC] = UART011_IMSC,
      73                 :            :         [REG_RIS] = UART011_RIS,
      74                 :            :         [REG_MIS] = UART011_MIS,
      75                 :            :         [REG_ICR] = UART011_ICR,
      76                 :            :         [REG_DMACR] = UART011_DMACR,
      77                 :            : };
      78                 :            : 
      79                 :            : /* There is by now at least one vendor with differing details, so handle it */
      80                 :            : struct vendor_data {
      81                 :            :         const u16               *reg_offset;
      82                 :            :         unsigned int            ifls;
      83                 :            :         unsigned int            fr_busy;
      84                 :            :         unsigned int            fr_dsr;
      85                 :            :         unsigned int            fr_cts;
      86                 :            :         unsigned int            fr_ri;
      87                 :            :         unsigned int            inv_fr;
      88                 :            :         bool                    access_32b;
      89                 :            :         bool                    oversampling;
      90                 :            :         bool                    dma_threshold;
      91                 :            :         bool                    cts_event_workaround;
      92                 :            :         bool                    always_enabled;
      93                 :            :         bool                    fixed_options;
      94                 :            : 
      95                 :            :         unsigned int (*get_fifosize)(struct amba_device *dev);
      96                 :            : };
      97                 :            : 
      98                 :          3 : static unsigned int get_fifosize_arm(struct amba_device *dev)
      99                 :            : {
     100                 :          3 :         return amba_rev(dev) < 3 ? 16 : 32;
     101                 :            : }
     102                 :            : 
     103                 :            : static struct vendor_data vendor_arm = {
     104                 :            :         .reg_offset             = pl011_std_offsets,
     105                 :            :         .ifls                   = UART011_IFLS_RX4_8|UART011_IFLS_TX4_8,
     106                 :            :         .fr_busy                = UART01x_FR_BUSY,
     107                 :            :         .fr_dsr                 = UART01x_FR_DSR,
     108                 :            :         .fr_cts                 = UART01x_FR_CTS,
     109                 :            :         .fr_ri                  = UART011_FR_RI,
     110                 :            :         .oversampling           = false,
     111                 :            :         .dma_threshold          = false,
     112                 :            :         .cts_event_workaround   = false,
     113                 :            :         .always_enabled         = false,
     114                 :            :         .fixed_options          = false,
     115                 :            :         .get_fifosize           = get_fifosize_arm,
     116                 :            : };
     117                 :            : 
     118                 :            : static const struct vendor_data vendor_sbsa = {
     119                 :            :         .reg_offset             = pl011_std_offsets,
     120                 :            :         .fr_busy                = UART01x_FR_BUSY,
     121                 :            :         .fr_dsr                 = UART01x_FR_DSR,
     122                 :            :         .fr_cts                 = UART01x_FR_CTS,
     123                 :            :         .fr_ri                  = UART011_FR_RI,
     124                 :            :         .access_32b             = true,
     125                 :            :         .oversampling           = false,
     126                 :            :         .dma_threshold          = false,
     127                 :            :         .cts_event_workaround   = false,
     128                 :            :         .always_enabled         = true,
     129                 :            :         .fixed_options          = true,
     130                 :            : };
     131                 :            : 
     132                 :            : #ifdef CONFIG_ACPI_SPCR_TABLE
     133                 :            : static const struct vendor_data vendor_qdt_qdf2400_e44 = {
     134                 :            :         .reg_offset             = pl011_std_offsets,
     135                 :            :         .fr_busy                = UART011_FR_TXFE,
     136                 :            :         .fr_dsr                 = UART01x_FR_DSR,
     137                 :            :         .fr_cts                 = UART01x_FR_CTS,
     138                 :            :         .fr_ri                  = UART011_FR_RI,
     139                 :            :         .inv_fr                 = UART011_FR_TXFE,
     140                 :            :         .access_32b             = true,
     141                 :            :         .oversampling           = false,
     142                 :            :         .dma_threshold          = false,
     143                 :            :         .cts_event_workaround   = false,
     144                 :            :         .always_enabled         = true,
     145                 :            :         .fixed_options          = true,
     146                 :            : };
     147                 :            : #endif
     148                 :            : 
     149                 :            : static u16 pl011_st_offsets[REG_ARRAY_SIZE] = {
     150                 :            :         [REG_DR] = UART01x_DR,
     151                 :            :         [REG_ST_DMAWM] = ST_UART011_DMAWM,
     152                 :            :         [REG_ST_TIMEOUT] = ST_UART011_TIMEOUT,
     153                 :            :         [REG_FR] = UART01x_FR,
     154                 :            :         [REG_LCRH_RX] = ST_UART011_LCRH_RX,
     155                 :            :         [REG_LCRH_TX] = ST_UART011_LCRH_TX,
     156                 :            :         [REG_IBRD] = UART011_IBRD,
     157                 :            :         [REG_FBRD] = UART011_FBRD,
     158                 :            :         [REG_CR] = UART011_CR,
     159                 :            :         [REG_IFLS] = UART011_IFLS,
     160                 :            :         [REG_IMSC] = UART011_IMSC,
     161                 :            :         [REG_RIS] = UART011_RIS,
     162                 :            :         [REG_MIS] = UART011_MIS,
     163                 :            :         [REG_ICR] = UART011_ICR,
     164                 :            :         [REG_DMACR] = UART011_DMACR,
     165                 :            :         [REG_ST_XFCR] = ST_UART011_XFCR,
     166                 :            :         [REG_ST_XON1] = ST_UART011_XON1,
     167                 :            :         [REG_ST_XON2] = ST_UART011_XON2,
     168                 :            :         [REG_ST_XOFF1] = ST_UART011_XOFF1,
     169                 :            :         [REG_ST_XOFF2] = ST_UART011_XOFF2,
     170                 :            :         [REG_ST_ITCR] = ST_UART011_ITCR,
     171                 :            :         [REG_ST_ITIP] = ST_UART011_ITIP,
     172                 :            :         [REG_ST_ABCR] = ST_UART011_ABCR,
     173                 :            :         [REG_ST_ABIMSC] = ST_UART011_ABIMSC,
     174                 :            : };
     175                 :            : 
     176                 :          0 : static unsigned int get_fifosize_st(struct amba_device *dev)
     177                 :            : {
     178                 :          0 :         return 64;
     179                 :            : }
     180                 :            : 
     181                 :            : static struct vendor_data vendor_st = {
     182                 :            :         .reg_offset             = pl011_st_offsets,
     183                 :            :         .ifls                   = UART011_IFLS_RX_HALF|UART011_IFLS_TX_HALF,
     184                 :            :         .fr_busy                = UART01x_FR_BUSY,
     185                 :            :         .fr_dsr                 = UART01x_FR_DSR,
     186                 :            :         .fr_cts                 = UART01x_FR_CTS,
     187                 :            :         .fr_ri                  = UART011_FR_RI,
     188                 :            :         .oversampling           = true,
     189                 :            :         .dma_threshold          = true,
     190                 :            :         .cts_event_workaround   = true,
     191                 :            :         .always_enabled         = false,
     192                 :            :         .fixed_options          = false,
     193                 :            :         .get_fifosize           = get_fifosize_st,
     194                 :            : };
     195                 :            : 
     196                 :            : static const u16 pl011_zte_offsets[REG_ARRAY_SIZE] = {
     197                 :            :         [REG_DR] = ZX_UART011_DR,
     198                 :            :         [REG_FR] = ZX_UART011_FR,
     199                 :            :         [REG_LCRH_RX] = ZX_UART011_LCRH,
     200                 :            :         [REG_LCRH_TX] = ZX_UART011_LCRH,
     201                 :            :         [REG_IBRD] = ZX_UART011_IBRD,
     202                 :            :         [REG_FBRD] = ZX_UART011_FBRD,
     203                 :            :         [REG_CR] = ZX_UART011_CR,
     204                 :            :         [REG_IFLS] = ZX_UART011_IFLS,
     205                 :            :         [REG_IMSC] = ZX_UART011_IMSC,
     206                 :            :         [REG_RIS] = ZX_UART011_RIS,
     207                 :            :         [REG_MIS] = ZX_UART011_MIS,
     208                 :            :         [REG_ICR] = ZX_UART011_ICR,
     209                 :            :         [REG_DMACR] = ZX_UART011_DMACR,
     210                 :            : };
     211                 :            : 
     212                 :          0 : static unsigned int get_fifosize_zte(struct amba_device *dev)
     213                 :            : {
     214                 :          0 :         return 16;
     215                 :            : }
     216                 :            : 
     217                 :            : static struct vendor_data vendor_zte = {
     218                 :            :         .reg_offset             = pl011_zte_offsets,
     219                 :            :         .access_32b             = true,
     220                 :            :         .ifls                   = UART011_IFLS_RX4_8|UART011_IFLS_TX4_8,
     221                 :            :         .fr_busy                = ZX_UART01x_FR_BUSY,
     222                 :            :         .fr_dsr                 = ZX_UART01x_FR_DSR,
     223                 :            :         .fr_cts                 = ZX_UART01x_FR_CTS,
     224                 :            :         .fr_ri                  = ZX_UART011_FR_RI,
     225                 :            :         .get_fifosize           = get_fifosize_zte,
     226                 :            : };
     227                 :            : 
     228                 :            : /* Deals with DMA transactions */
     229                 :            : 
     230                 :            : struct pl011_sgbuf {
     231                 :            :         struct scatterlist sg;
     232                 :            :         char *buf;
     233                 :            : };
     234                 :            : 
     235                 :            : struct pl011_dmarx_data {
     236                 :            :         struct dma_chan         *chan;
     237                 :            :         struct completion       complete;
     238                 :            :         bool                    use_buf_b;
     239                 :            :         struct pl011_sgbuf      sgbuf_a;
     240                 :            :         struct pl011_sgbuf      sgbuf_b;
     241                 :            :         dma_cookie_t            cookie;
     242                 :            :         bool                    running;
     243                 :            :         struct timer_list       timer;
     244                 :            :         unsigned int last_residue;
     245                 :            :         unsigned long last_jiffies;
     246                 :            :         bool auto_poll_rate;
     247                 :            :         unsigned int poll_rate;
     248                 :            :         unsigned int poll_timeout;
     249                 :            : };
     250                 :            : 
     251                 :            : struct pl011_dmatx_data {
     252                 :            :         struct dma_chan         *chan;
     253                 :            :         struct scatterlist      sg;
     254                 :            :         char                    *buf;
     255                 :            :         bool                    queued;
     256                 :            : };
     257                 :            : 
     258                 :            : /*
     259                 :            :  * We wrap our port structure around the generic uart_port.
     260                 :            :  */
     261                 :            : struct uart_amba_port {
     262                 :            :         struct uart_port        port;
     263                 :            :         const u16               *reg_offset;
     264                 :            :         struct clk              *clk;
     265                 :            :         const struct vendor_data *vendor;
     266                 :            :         unsigned int            dmacr;          /* dma control reg */
     267                 :            :         unsigned int            im;             /* interrupt mask */
     268                 :            :         unsigned int            old_status;
     269                 :            :         unsigned int            fifosize;       /* vendor-specific */
     270                 :            :         unsigned int            old_cr;         /* state during shutdown */
     271                 :            :         unsigned int            fixed_baud;     /* vendor-set fixed baud rate */
     272                 :            :         char                    type[12];
     273                 :            :         bool                    irq_locked;     /* in irq, unreleased lock */
     274                 :            : #ifdef CONFIG_DMA_ENGINE
     275                 :            :         /* DMA stuff */
     276                 :            :         bool                    using_tx_dma;
     277                 :            :         bool                    using_rx_dma;
     278                 :            :         struct pl011_dmarx_data dmarx;
     279                 :            :         struct pl011_dmatx_data dmatx;
     280                 :            :         bool                    dma_probed;
     281                 :            : #endif
     282                 :            : };
     283                 :            : 
     284                 :            : static unsigned int pl011_reg_to_offset(const struct uart_amba_port *uap,
     285                 :            :         unsigned int reg)
     286                 :            : {
     287                 :          3 :         return uap->reg_offset[reg];
     288                 :            : }
     289                 :            : 
     290                 :            : static unsigned int pl011_read(const struct uart_amba_port *uap,
     291                 :            :         unsigned int reg)
     292                 :            : {
     293                 :          3 :         void __iomem *addr = uap->port.membase + pl011_reg_to_offset(uap, reg);
     294                 :            : 
     295                 :          3 :         return (uap->port.iotype == UPIO_MEM32) ?
     296                 :          3 :                 readl_relaxed(addr) : readw_relaxed(addr);
     297                 :            : }
     298                 :            : 
     299                 :            : static void pl011_write(unsigned int val, const struct uart_amba_port *uap,
     300                 :            :         unsigned int reg)
     301                 :            : {
     302                 :          3 :         void __iomem *addr = uap->port.membase + pl011_reg_to_offset(uap, reg);
     303                 :            : 
     304                 :          3 :         if (uap->port.iotype == UPIO_MEM32)
     305                 :            :                 writel_relaxed(val, addr);
     306                 :            :         else
     307                 :          3 :                 writew_relaxed(val, addr);
     308                 :            : }
     309                 :            : 
     310                 :            : /*
     311                 :            :  * Reads up to 256 characters from the FIFO or until it's empty and
     312                 :            :  * inserts them into the TTY layer. Returns the number of characters
     313                 :            :  * read from the FIFO.
     314                 :            :  */
     315                 :          1 : static int pl011_fifo_to_tty(struct uart_amba_port *uap)
     316                 :            : {
     317                 :            :         u16 status;
     318                 :            :         unsigned int ch, flag, fifotaken;
     319                 :            : 
     320                 :          1 :         for (fifotaken = 0; fifotaken != 256; fifotaken++) {
     321                 :            :                 status = pl011_read(uap, REG_FR);
     322                 :          1 :                 if (status & UART01x_FR_RXFE)
     323                 :            :                         break;
     324                 :            : 
     325                 :            :                 /* Take chars from the FIFO and update status */
     326                 :          1 :                 ch = pl011_read(uap, REG_DR) | UART_DUMMY_DR_RX;
     327                 :            :                 flag = TTY_NORMAL;
     328                 :          1 :                 uap->port.icount.rx++;
     329                 :            : 
     330                 :          1 :                 if (unlikely(ch & UART_DR_ERROR)) {
     331                 :          0 :                         if (ch & UART011_DR_BE) {
     332                 :          0 :                                 ch &= ~(UART011_DR_FE | UART011_DR_PE);
     333                 :          0 :                                 uap->port.icount.brk++;
     334                 :          0 :                                 if (uart_handle_break(&uap->port))
     335                 :          0 :                                         continue;
     336                 :          0 :                         } else if (ch & UART011_DR_PE)
     337                 :          0 :                                 uap->port.icount.parity++;
     338                 :          0 :                         else if (ch & UART011_DR_FE)
     339                 :          0 :                                 uap->port.icount.frame++;
     340                 :          0 :                         if (ch & UART011_DR_OE)
     341                 :          0 :                                 uap->port.icount.overrun++;
     342                 :            : 
     343                 :          0 :                         ch &= uap->port.read_status_mask;
     344                 :            : 
     345                 :          0 :                         if (ch & UART011_DR_BE)
     346                 :            :                                 flag = TTY_BREAK;
     347                 :          0 :                         else if (ch & UART011_DR_PE)
     348                 :            :                                 flag = TTY_PARITY;
     349                 :          0 :                         else if (ch & UART011_DR_FE)
     350                 :            :                                 flag = TTY_FRAME;
     351                 :            :                 }
     352                 :            : 
     353                 :          1 :                 if (uart_handle_sysrq_char(&uap->port, ch & 255))
     354                 :          0 :                         continue;
     355                 :            : 
     356                 :          1 :                 uart_insert_char(&uap->port, ch, UART011_DR_OE, ch, flag);
     357                 :            :         }
     358                 :            : 
     359                 :          1 :         return fifotaken;
     360                 :            : }
     361                 :            : 
     362                 :            : 
     363                 :            : /*
     364                 :            :  * All the DMA operation mode stuff goes inside this ifdef.
     365                 :            :  * This assumes that you have a generic DMA device interface,
     366                 :            :  * no custom DMA interfaces are supported.
     367                 :            :  */
     368                 :            : #ifdef CONFIG_DMA_ENGINE
     369                 :            : 
     370                 :            : #define PL011_DMA_BUFFER_SIZE PAGE_SIZE
     371                 :            : 
     372                 :          0 : static int pl011_sgbuf_init(struct dma_chan *chan, struct pl011_sgbuf *sg,
     373                 :            :         enum dma_data_direction dir)
     374                 :            : {
     375                 :            :         dma_addr_t dma_addr;
     376                 :            : 
     377                 :          0 :         sg->buf = dma_alloc_coherent(chan->device->dev,
     378                 :            :                 PL011_DMA_BUFFER_SIZE, &dma_addr, GFP_KERNEL);
     379                 :          0 :         if (!sg->buf)
     380                 :            :                 return -ENOMEM;
     381                 :            : 
     382                 :          0 :         sg_init_table(&sg->sg, 1);
     383                 :          0 :         sg_set_page(&sg->sg, phys_to_page(dma_addr),
     384                 :            :                 PL011_DMA_BUFFER_SIZE, offset_in_page(dma_addr));
     385                 :          0 :         sg_dma_address(&sg->sg) = dma_addr;
     386                 :            :         sg_dma_len(&sg->sg) = PL011_DMA_BUFFER_SIZE;
     387                 :            : 
     388                 :          0 :         return 0;
     389                 :            : }
     390                 :            : 
     391                 :          0 : static void pl011_sgbuf_free(struct dma_chan *chan, struct pl011_sgbuf *sg,
     392                 :            :         enum dma_data_direction dir)
     393                 :            : {
     394                 :          0 :         if (sg->buf) {
     395                 :          0 :                 dma_free_coherent(chan->device->dev,
     396                 :            :                         PL011_DMA_BUFFER_SIZE, sg->buf,
     397                 :            :                         sg_dma_address(&sg->sg));
     398                 :            :         }
     399                 :          0 : }
     400                 :            : 
     401                 :          3 : static void pl011_dma_probe(struct uart_amba_port *uap)
     402                 :            : {
     403                 :            :         /* DMA is the sole user of the platform data right now */
     404                 :          3 :         struct amba_pl011_data *plat = dev_get_platdata(uap->port.dev);
     405                 :            :         struct device *dev = uap->port.dev;
     406                 :          3 :         struct dma_slave_config tx_conf = {
     407                 :          3 :                 .dst_addr = uap->port.mapbase +
     408                 :            :                                  pl011_reg_to_offset(uap, REG_DR),
     409                 :            :                 .dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
     410                 :            :                 .direction = DMA_MEM_TO_DEV,
     411                 :          3 :                 .dst_maxburst = uap->fifosize >> 1,
     412                 :            :                 .device_fc = false,
     413                 :            :         };
     414                 :            :         struct dma_chan *chan;
     415                 :            :         dma_cap_mask_t mask;
     416                 :            : 
     417                 :          3 :         uap->dma_probed = true;
     418                 :          3 :         chan = dma_request_slave_channel_reason(dev, "tx");
     419                 :          3 :         if (IS_ERR(chan)) {
     420                 :          3 :                 if (PTR_ERR(chan) == -EPROBE_DEFER) {
     421                 :          0 :                         uap->dma_probed = false;
     422                 :          3 :                         return;
     423                 :            :                 }
     424                 :            : 
     425                 :            :                 /* We need platform data */
     426                 :          3 :                 if (!plat || !plat->dma_filter) {
     427                 :          3 :                         dev_info(uap->port.dev, "no DMA platform data\n");
     428                 :          3 :                         return;
     429                 :            :                 }
     430                 :            : 
     431                 :            :                 /* Try to acquire a generic DMA engine slave TX channel */
     432                 :            :                 dma_cap_zero(mask);
     433                 :            :                 dma_cap_set(DMA_SLAVE, mask);
     434                 :            : 
     435                 :          0 :                 chan = dma_request_channel(mask, plat->dma_filter,
     436                 :            :                                                 plat->dma_tx_param);
     437                 :          0 :                 if (!chan) {
     438                 :          0 :                         dev_err(uap->port.dev, "no TX DMA channel!\n");
     439                 :          0 :                         return;
     440                 :            :                 }
     441                 :            :         }
     442                 :            : 
     443                 :            :         dmaengine_slave_config(chan, &tx_conf);
     444                 :          0 :         uap->dmatx.chan = chan;
     445                 :            : 
     446                 :          0 :         dev_info(uap->port.dev, "DMA channel TX %s\n",
     447                 :            :                  dma_chan_name(uap->dmatx.chan));
     448                 :            : 
     449                 :            :         /* Optionally make use of an RX channel as well */
     450                 :          0 :         chan = dma_request_slave_channel(dev, "rx");
     451                 :            : 
     452                 :          0 :         if (!chan && plat && plat->dma_rx_param) {
     453                 :          0 :                 chan = dma_request_channel(mask, plat->dma_filter, plat->dma_rx_param);
     454                 :            : 
     455                 :          0 :                 if (!chan) {
     456                 :          0 :                         dev_err(uap->port.dev, "no RX DMA channel!\n");
     457                 :          0 :                         return;
     458                 :            :                 }
     459                 :            :         }
     460                 :            : 
     461                 :          0 :         if (chan) {
     462                 :          0 :                 struct dma_slave_config rx_conf = {
     463                 :          0 :                         .src_addr = uap->port.mapbase +
     464                 :            :                                 pl011_reg_to_offset(uap, REG_DR),
     465                 :            :                         .src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
     466                 :            :                         .direction = DMA_DEV_TO_MEM,
     467                 :          0 :                         .src_maxburst = uap->fifosize >> 2,
     468                 :            :                         .device_fc = false,
     469                 :            :                 };
     470                 :            :                 struct dma_slave_caps caps;
     471                 :            : 
     472                 :            :                 /*
     473                 :            :                  * Some DMA controllers provide information on their capabilities.
     474                 :            :                  * If the controller does, check for suitable residue processing
     475                 :            :                  * otherwise assime all is well.
     476                 :            :                  */
     477                 :          0 :                 if (0 == dma_get_slave_caps(chan, &caps)) {
     478                 :          0 :                         if (caps.residue_granularity ==
     479                 :            :                                         DMA_RESIDUE_GRANULARITY_DESCRIPTOR) {
     480                 :          0 :                                 dma_release_channel(chan);
     481                 :          0 :                                 dev_info(uap->port.dev,
     482                 :            :                                         "RX DMA disabled - no residue processing\n");
     483                 :          0 :                                 return;
     484                 :            :                         }
     485                 :            :                 }
     486                 :            :                 dmaengine_slave_config(chan, &rx_conf);
     487                 :          0 :                 uap->dmarx.chan = chan;
     488                 :            : 
     489                 :          0 :                 uap->dmarx.auto_poll_rate = false;
     490                 :          0 :                 if (plat && plat->dma_rx_poll_enable) {
     491                 :            :                         /* Set poll rate if specified. */
     492                 :          0 :                         if (plat->dma_rx_poll_rate) {
     493                 :            :                                 uap->dmarx.auto_poll_rate = false;
     494                 :          0 :                                 uap->dmarx.poll_rate = plat->dma_rx_poll_rate;
     495                 :            :                         } else {
     496                 :            :                                 /*
     497                 :            :                                  * 100 ms defaults to poll rate if not
     498                 :            :                                  * specified. This will be adjusted with
     499                 :            :                                  * the baud rate at set_termios.
     500                 :            :                                  */
     501                 :          0 :                                 uap->dmarx.auto_poll_rate = true;
     502                 :          0 :                                 uap->dmarx.poll_rate =  100;
     503                 :            :                         }
     504                 :            :                         /* 3 secs defaults poll_timeout if not specified. */
     505                 :          0 :                         if (plat->dma_rx_poll_timeout)
     506                 :          0 :                                 uap->dmarx.poll_timeout =
     507                 :            :                                         plat->dma_rx_poll_timeout;
     508                 :            :                         else
     509                 :          0 :                                 uap->dmarx.poll_timeout = 3000;
     510                 :          0 :                 } else if (!plat && dev->of_node) {
     511                 :          0 :                         uap->dmarx.auto_poll_rate = of_property_read_bool(
     512                 :            :                                                 dev->of_node, "auto-poll");
     513                 :          0 :                         if (uap->dmarx.auto_poll_rate) {
     514                 :            :                                 u32 x;
     515                 :            : 
     516                 :          0 :                                 if (0 == of_property_read_u32(dev->of_node,
     517                 :            :                                                 "poll-rate-ms", &x))
     518                 :          0 :                                         uap->dmarx.poll_rate = x;
     519                 :            :                                 else
     520                 :          0 :                                         uap->dmarx.poll_rate = 100;
     521                 :          0 :                                 if (0 == of_property_read_u32(dev->of_node,
     522                 :            :                                                 "poll-timeout-ms", &x))
     523                 :          0 :                                         uap->dmarx.poll_timeout = x;
     524                 :            :                                 else
     525                 :          0 :                                         uap->dmarx.poll_timeout = 3000;
     526                 :            :                         }
     527                 :            :                 }
     528                 :          0 :                 dev_info(uap->port.dev, "DMA channel RX %s\n",
     529                 :            :                          dma_chan_name(uap->dmarx.chan));
     530                 :            :         }
     531                 :            : }
     532                 :            : 
     533                 :          0 : static void pl011_dma_remove(struct uart_amba_port *uap)
     534                 :            : {
     535                 :          0 :         if (uap->dmatx.chan)
     536                 :          0 :                 dma_release_channel(uap->dmatx.chan);
     537                 :          0 :         if (uap->dmarx.chan)
     538                 :          0 :                 dma_release_channel(uap->dmarx.chan);
     539                 :          0 : }
     540                 :            : 
     541                 :            : /* Forward declare these for the refill routine */
     542                 :            : static int pl011_dma_tx_refill(struct uart_amba_port *uap);
     543                 :            : static void pl011_start_tx_pio(struct uart_amba_port *uap);
     544                 :            : 
     545                 :            : /*
     546                 :            :  * The current DMA TX buffer has been sent.
     547                 :            :  * Try to queue up another DMA buffer.
     548                 :            :  */
     549                 :          0 : static void pl011_dma_tx_callback(void *data)
     550                 :            : {
     551                 :            :         struct uart_amba_port *uap = data;
     552                 :            :         struct pl011_dmatx_data *dmatx = &uap->dmatx;
     553                 :            :         unsigned long flags;
     554                 :            :         u16 dmacr;
     555                 :            : 
     556                 :          0 :         spin_lock_irqsave(&uap->port.lock, flags);
     557                 :          0 :         if (uap->dmatx.queued)
     558                 :          0 :                 dma_unmap_sg(dmatx->chan->device->dev, &dmatx->sg, 1,
     559                 :            :                              DMA_TO_DEVICE);
     560                 :            : 
     561                 :          0 :         dmacr = uap->dmacr;
     562                 :          0 :         uap->dmacr = dmacr & ~UART011_TXDMAE;
     563                 :            :         pl011_write(uap->dmacr, uap, REG_DMACR);
     564                 :            : 
     565                 :            :         /*
     566                 :            :          * If TX DMA was disabled, it means that we've stopped the DMA for
     567                 :            :          * some reason (eg, XOFF received, or we want to send an X-char.)
     568                 :            :          *
     569                 :            :          * Note: we need to be careful here of a potential race between DMA
     570                 :            :          * and the rest of the driver - if the driver disables TX DMA while
     571                 :            :          * a TX buffer completing, we must update the tx queued status to
     572                 :            :          * get further refills (hence we check dmacr).
     573                 :            :          */
     574                 :          0 :         if (!(dmacr & UART011_TXDMAE) || uart_tx_stopped(&uap->port) ||
     575                 :          0 :             uart_circ_empty(&uap->port.state->xmit)) {
     576                 :          0 :                 uap->dmatx.queued = false;
     577                 :            :                 spin_unlock_irqrestore(&uap->port.lock, flags);
     578                 :          0 :                 return;
     579                 :            :         }
     580                 :            : 
     581                 :          0 :         if (pl011_dma_tx_refill(uap) <= 0)
     582                 :            :                 /*
     583                 :            :                  * We didn't queue a DMA buffer for some reason, but we
     584                 :            :                  * have data pending to be sent.  Re-enable the TX IRQ.
     585                 :            :                  */
     586                 :          0 :                 pl011_start_tx_pio(uap);
     587                 :            : 
     588                 :            :         spin_unlock_irqrestore(&uap->port.lock, flags);
     589                 :            : }
     590                 :            : 
     591                 :            : /*
     592                 :            :  * Try to refill the TX DMA buffer.
     593                 :            :  * Locking: called with port lock held and IRQs disabled.
     594                 :            :  * Returns:
     595                 :            :  *   1 if we queued up a TX DMA buffer.
     596                 :            :  *   0 if we didn't want to handle this by DMA
     597                 :            :  *  <0 on error
     598                 :            :  */
     599                 :          0 : static int pl011_dma_tx_refill(struct uart_amba_port *uap)
     600                 :            : {
     601                 :            :         struct pl011_dmatx_data *dmatx = &uap->dmatx;
     602                 :          0 :         struct dma_chan *chan = dmatx->chan;
     603                 :          0 :         struct dma_device *dma_dev = chan->device;
     604                 :            :         struct dma_async_tx_descriptor *desc;
     605                 :          0 :         struct circ_buf *xmit = &uap->port.state->xmit;
     606                 :            :         unsigned int count;
     607                 :            : 
     608                 :            :         /*
     609                 :            :          * Try to avoid the overhead involved in using DMA if the
     610                 :            :          * transaction fits in the first half of the FIFO, by using
     611                 :            :          * the standard interrupt handling.  This ensures that we
     612                 :            :          * issue a uart_write_wakeup() at the appropriate time.
     613                 :            :          */
     614                 :          0 :         count = uart_circ_chars_pending(xmit);
     615                 :          0 :         if (count < (uap->fifosize >> 1)) {
     616                 :          0 :                 uap->dmatx.queued = false;
     617                 :          0 :                 return 0;
     618                 :            :         }
     619                 :            : 
     620                 :            :         /*
     621                 :            :          * Bodge: don't send the last character by DMA, as this
     622                 :            :          * will prevent XON from notifying us to restart DMA.
     623                 :            :          */
     624                 :          0 :         count -= 1;
     625                 :            : 
     626                 :            :         /* Else proceed to copy the TX chars to the DMA buffer and fire DMA */
     627                 :          0 :         if (count > PL011_DMA_BUFFER_SIZE)
     628                 :            :                 count = PL011_DMA_BUFFER_SIZE;
     629                 :            : 
     630                 :          0 :         if (xmit->tail < xmit->head)
     631                 :          0 :                 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], count);
     632                 :            :         else {
     633                 :          0 :                 size_t first = UART_XMIT_SIZE - xmit->tail;
     634                 :            :                 size_t second;
     635                 :            : 
     636                 :          0 :                 if (first > count)
     637                 :            :                         first = count;
     638                 :          0 :                 second = count - first;
     639                 :            : 
     640                 :          0 :                 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], first);
     641                 :          0 :                 if (second)
     642                 :          0 :                         memcpy(&dmatx->buf[first], &xmit->buf[0], second);
     643                 :            :         }
     644                 :            : 
     645                 :          0 :         dmatx->sg.length = count;
     646                 :            : 
     647                 :          0 :         if (dma_map_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE) != 1) {
     648                 :          0 :                 uap->dmatx.queued = false;
     649                 :            :                 dev_dbg(uap->port.dev, "unable to map TX DMA\n");
     650                 :          0 :                 return -EBUSY;
     651                 :            :         }
     652                 :            : 
     653                 :            :         desc = dmaengine_prep_slave_sg(chan, &dmatx->sg, 1, DMA_MEM_TO_DEV,
     654                 :            :                                              DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
     655                 :          0 :         if (!desc) {
     656                 :          0 :                 dma_unmap_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE);
     657                 :          0 :                 uap->dmatx.queued = false;
     658                 :            :                 /*
     659                 :            :                  * If DMA cannot be used right now, we complete this
     660                 :            :                  * transaction via IRQ and let the TTY layer retry.
     661                 :            :                  */
     662                 :            :                 dev_dbg(uap->port.dev, "TX DMA busy\n");
     663                 :          0 :                 return -EBUSY;
     664                 :            :         }
     665                 :            : 
     666                 :            :         /* Some data to go along to the callback */
     667                 :          0 :         desc->callback = pl011_dma_tx_callback;
     668                 :          0 :         desc->callback_param = uap;
     669                 :            : 
     670                 :            :         /* All errors should happen at prepare time */
     671                 :            :         dmaengine_submit(desc);
     672                 :            : 
     673                 :            :         /* Fire the DMA transaction */
     674                 :          0 :         dma_dev->device_issue_pending(chan);
     675                 :            : 
     676                 :          0 :         uap->dmacr |= UART011_TXDMAE;
     677                 :            :         pl011_write(uap->dmacr, uap, REG_DMACR);
     678                 :          0 :         uap->dmatx.queued = true;
     679                 :            : 
     680                 :            :         /*
     681                 :            :          * Now we know that DMA will fire, so advance the ring buffer
     682                 :            :          * with the stuff we just dispatched.
     683                 :            :          */
     684                 :          0 :         xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
     685                 :          0 :         uap->port.icount.tx += count;
     686                 :            : 
     687                 :          0 :         if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
     688                 :          0 :                 uart_write_wakeup(&uap->port);
     689                 :            : 
     690                 :            :         return 1;
     691                 :            : }
     692                 :            : 
     693                 :            : /*
     694                 :            :  * We received a transmit interrupt without a pending X-char but with
     695                 :            :  * pending characters.
     696                 :            :  * Locking: called with port lock held and IRQs disabled.
     697                 :            :  * Returns:
     698                 :            :  *   false if we want to use PIO to transmit
     699                 :            :  *   true if we queued a DMA buffer
     700                 :            :  */
     701                 :          3 : static bool pl011_dma_tx_irq(struct uart_amba_port *uap)
     702                 :            : {
     703                 :          3 :         if (!uap->using_tx_dma)
     704                 :            :                 return false;
     705                 :            : 
     706                 :            :         /*
     707                 :            :          * If we already have a TX buffer queued, but received a
     708                 :            :          * TX interrupt, it will be because we've just sent an X-char.
     709                 :            :          * Ensure the TX DMA is enabled and the TX IRQ is disabled.
     710                 :            :          */
     711                 :          0 :         if (uap->dmatx.queued) {
     712                 :          0 :                 uap->dmacr |= UART011_TXDMAE;
     713                 :            :                 pl011_write(uap->dmacr, uap, REG_DMACR);
     714                 :          0 :                 uap->im &= ~UART011_TXIM;
     715                 :            :                 pl011_write(uap->im, uap, REG_IMSC);
     716                 :            :                 return true;
     717                 :            :         }
     718                 :            : 
     719                 :            :         /*
     720                 :            :          * We don't have a TX buffer queued, so try to queue one.
     721                 :            :          * If we successfully queued a buffer, mask the TX IRQ.
     722                 :            :          */
     723                 :          0 :         if (pl011_dma_tx_refill(uap) > 0) {
     724                 :          0 :                 uap->im &= ~UART011_TXIM;
     725                 :            :                 pl011_write(uap->im, uap, REG_IMSC);
     726                 :            :                 return true;
     727                 :            :         }
     728                 :            :         return false;
     729                 :            : }
     730                 :            : 
     731                 :            : /*
     732                 :            :  * Stop the DMA transmit (eg, due to received XOFF).
     733                 :            :  * Locking: called with port lock held and IRQs disabled.
     734                 :            :  */
     735                 :            : static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
     736                 :            : {
     737                 :          3 :         if (uap->dmatx.queued) {
     738                 :          0 :                 uap->dmacr &= ~UART011_TXDMAE;
     739                 :            :                 pl011_write(uap->dmacr, uap, REG_DMACR);
     740                 :            :         }
     741                 :            : }
     742                 :            : 
     743                 :            : /*
     744                 :            :  * Try to start a DMA transmit, or in the case of an XON/OFF
     745                 :            :  * character queued for send, try to get that character out ASAP.
     746                 :            :  * Locking: called with port lock held and IRQs disabled.
     747                 :            :  * Returns:
     748                 :            :  *   false if we want the TX IRQ to be enabled
     749                 :            :  *   true if we have a buffer queued
     750                 :            :  */
     751                 :          3 : static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
     752                 :            : {
     753                 :            :         u16 dmacr;
     754                 :            : 
     755                 :          3 :         if (!uap->using_tx_dma)
     756                 :            :                 return false;
     757                 :            : 
     758                 :          0 :         if (!uap->port.x_char) {
     759                 :            :                 /* no X-char, try to push chars out in DMA mode */
     760                 :            :                 bool ret = true;
     761                 :            : 
     762                 :          0 :                 if (!uap->dmatx.queued) {
     763                 :          0 :                         if (pl011_dma_tx_refill(uap) > 0) {
     764                 :          0 :                                 uap->im &= ~UART011_TXIM;
     765                 :            :                                 pl011_write(uap->im, uap, REG_IMSC);
     766                 :            :                         } else
     767                 :            :                                 ret = false;
     768                 :          0 :                 } else if (!(uap->dmacr & UART011_TXDMAE)) {
     769                 :          0 :                         uap->dmacr |= UART011_TXDMAE;
     770                 :            :                         pl011_write(uap->dmacr, uap, REG_DMACR);
     771                 :            :                 }
     772                 :          0 :                 return ret;
     773                 :            :         }
     774                 :            : 
     775                 :            :         /*
     776                 :            :          * We have an X-char to send.  Disable DMA to prevent it loading
     777                 :            :          * the TX fifo, and then see if we can stuff it into the FIFO.
     778                 :            :          */
     779                 :          0 :         dmacr = uap->dmacr;
     780                 :          0 :         uap->dmacr &= ~UART011_TXDMAE;
     781                 :            :         pl011_write(uap->dmacr, uap, REG_DMACR);
     782                 :            : 
     783                 :          0 :         if (pl011_read(uap, REG_FR) & UART01x_FR_TXFF) {
     784                 :            :                 /*
     785                 :            :                  * No space in the FIFO, so enable the transmit interrupt
     786                 :            :                  * so we know when there is space.  Note that once we've
     787                 :            :                  * loaded the character, we should just re-enable DMA.
     788                 :            :                  */
     789                 :            :                 return false;
     790                 :            :         }
     791                 :            : 
     792                 :          0 :         pl011_write(uap->port.x_char, uap, REG_DR);
     793                 :          0 :         uap->port.icount.tx++;
     794                 :          0 :         uap->port.x_char = 0;
     795                 :            : 
     796                 :            :         /* Success - restore the DMA state */
     797                 :          0 :         uap->dmacr = dmacr;
     798                 :            :         pl011_write(dmacr, uap, REG_DMACR);
     799                 :            : 
     800                 :            :         return true;
     801                 :            : }
     802                 :            : 
     803                 :            : /*
     804                 :            :  * Flush the transmit buffer.
     805                 :            :  * Locking: called with port lock held and IRQs disabled.
     806                 :            :  */
     807                 :          3 : static void pl011_dma_flush_buffer(struct uart_port *port)
     808                 :            : __releases(&uap->port.lock)
     809                 :            : __acquires(&uap->port.lock)
     810                 :            : {
     811                 :            :         struct uart_amba_port *uap =
     812                 :            :             container_of(port, struct uart_amba_port, port);
     813                 :            : 
     814                 :          3 :         if (!uap->using_tx_dma)
     815                 :          3 :                 return;
     816                 :            : 
     817                 :          0 :         uap->irq_locked = 0;
     818                 :          0 :         dmaengine_terminate_async(uap->dmatx.chan);
     819                 :            : 
     820                 :          0 :         if (uap->dmatx.queued) {
     821                 :          0 :                 dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
     822                 :            :                              DMA_TO_DEVICE);
     823                 :          0 :                 uap->dmatx.queued = false;
     824                 :          0 :                 uap->dmacr &= ~UART011_TXDMAE;
     825                 :            :                 pl011_write(uap->dmacr, uap, REG_DMACR);
     826                 :            :         }
     827                 :            : }
     828                 :            : 
     829                 :            : static void pl011_dma_rx_callback(void *data);
     830                 :            : 
     831                 :          0 : static int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
     832                 :            : {
     833                 :          0 :         struct dma_chan *rxchan = uap->dmarx.chan;
     834                 :            :         struct pl011_dmarx_data *dmarx = &uap->dmarx;
     835                 :            :         struct dma_async_tx_descriptor *desc;
     836                 :            :         struct pl011_sgbuf *sgbuf;
     837                 :            : 
     838                 :          0 :         if (!rxchan)
     839                 :            :                 return -EIO;
     840                 :            : 
     841                 :            :         /* Start the RX DMA job */
     842                 :          0 :         sgbuf = uap->dmarx.use_buf_b ?
     843                 :          0 :                 &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
     844                 :          0 :         desc = dmaengine_prep_slave_sg(rxchan, &sgbuf->sg, 1,
     845                 :            :                                         DMA_DEV_TO_MEM,
     846                 :            :                                         DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
     847                 :            :         /*
     848                 :            :          * If the DMA engine is busy and cannot prepare a
     849                 :            :          * channel, no big deal, the driver will fall back
     850                 :            :          * to interrupt mode as a result of this error code.
     851                 :            :          */
     852                 :          0 :         if (!desc) {
     853                 :          0 :                 uap->dmarx.running = false;
     854                 :            :                 dmaengine_terminate_all(rxchan);
     855                 :            :                 return -EBUSY;
     856                 :            :         }
     857                 :            : 
     858                 :            :         /* Some data to go along to the callback */
     859                 :          0 :         desc->callback = pl011_dma_rx_callback;
     860                 :          0 :         desc->callback_param = uap;
     861                 :          0 :         dmarx->cookie = dmaengine_submit(desc);
     862                 :            :         dma_async_issue_pending(rxchan);
     863                 :            : 
     864                 :          0 :         uap->dmacr |= UART011_RXDMAE;
     865                 :            :         pl011_write(uap->dmacr, uap, REG_DMACR);
     866                 :          0 :         uap->dmarx.running = true;
     867                 :            : 
     868                 :          0 :         uap->im &= ~UART011_RXIM;
     869                 :            :         pl011_write(uap->im, uap, REG_IMSC);
     870                 :            : 
     871                 :            :         return 0;
     872                 :            : }
     873                 :            : 
     874                 :            : /*
     875                 :            :  * This is called when either the DMA job is complete, or
     876                 :            :  * the FIFO timeout interrupt occurred. This must be called
     877                 :            :  * with the port spinlock uap->port.lock held.
     878                 :            :  */
     879                 :          0 : static void pl011_dma_rx_chars(struct uart_amba_port *uap,
     880                 :            :                                u32 pending, bool use_buf_b,
     881                 :            :                                bool readfifo)
     882                 :            : {
     883                 :          0 :         struct tty_port *port = &uap->port.state->port;
     884                 :            :         struct pl011_sgbuf *sgbuf = use_buf_b ?
     885                 :          0 :                 &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
     886                 :            :         int dma_count = 0;
     887                 :            :         u32 fifotaken = 0; /* only used for vdbg() */
     888                 :            : 
     889                 :            :         struct pl011_dmarx_data *dmarx = &uap->dmarx;
     890                 :            :         int dmataken = 0;
     891                 :            : 
     892                 :          0 :         if (uap->dmarx.poll_rate) {
     893                 :            :                 /* The data can be taken by polling */
     894                 :          0 :                 dmataken = sgbuf->sg.length - dmarx->last_residue;
     895                 :            :                 /* Recalculate the pending size */
     896                 :          0 :                 if (pending >= dmataken)
     897                 :          0 :                         pending -= dmataken;
     898                 :            :         }
     899                 :            : 
     900                 :            :         /* Pick the remain data from the DMA */
     901                 :          0 :         if (pending) {
     902                 :            : 
     903                 :            :                 /*
     904                 :            :                  * First take all chars in the DMA pipe, then look in the FIFO.
     905                 :            :                  * Note that tty_insert_flip_buf() tries to take as many chars
     906                 :            :                  * as it can.
     907                 :            :                  */
     908                 :          0 :                 dma_count = tty_insert_flip_string(port, sgbuf->buf + dmataken,
     909                 :            :                                 pending);
     910                 :            : 
     911                 :          0 :                 uap->port.icount.rx += dma_count;
     912                 :          0 :                 if (dma_count < pending)
     913                 :          0 :                         dev_warn(uap->port.dev,
     914                 :            :                                  "couldn't insert all characters (TTY is full?)\n");
     915                 :            :         }
     916                 :            : 
     917                 :            :         /* Reset the last_residue for Rx DMA poll */
     918                 :          0 :         if (uap->dmarx.poll_rate)
     919                 :          0 :                 dmarx->last_residue = sgbuf->sg.length;
     920                 :            : 
     921                 :            :         /*
     922                 :            :          * Only continue with trying to read the FIFO if all DMA chars have
     923                 :            :          * been taken first.
     924                 :            :          */
     925                 :          0 :         if (dma_count == pending && readfifo) {
     926                 :            :                 /* Clear any error flags */
     927                 :            :                 pl011_write(UART011_OEIS | UART011_BEIS | UART011_PEIS |
     928                 :            :                             UART011_FEIS, uap, REG_ICR);
     929                 :            : 
     930                 :            :                 /*
     931                 :            :                  * If we read all the DMA'd characters, and we had an
     932                 :            :                  * incomplete buffer, that could be due to an rx error, or
     933                 :            :                  * maybe we just timed out. Read any pending chars and check
     934                 :            :                  * the error status.
     935                 :            :                  *
     936                 :            :                  * Error conditions will only occur in the FIFO, these will
     937                 :            :                  * trigger an immediate interrupt and stop the DMA job, so we
     938                 :            :                  * will always find the error in the FIFO, never in the DMA
     939                 :            :                  * buffer.
     940                 :            :                  */
     941                 :          0 :                 fifotaken = pl011_fifo_to_tty(uap);
     942                 :            :         }
     943                 :            : 
     944                 :          0 :         uap->irq_locked = 0;
     945                 :            :         spin_unlock(&uap->port.lock);
     946                 :            :         dev_vdbg(uap->port.dev,
     947                 :            :                  "Took %d chars from DMA buffer and %d chars from the FIFO\n",
     948                 :            :                  dma_count, fifotaken);
     949                 :          0 :         tty_flip_buffer_push(port);
     950                 :            :         spin_lock(&uap->port.lock);
     951                 :          0 : }
     952                 :            : 
     953                 :          0 : static void pl011_dma_rx_irq(struct uart_amba_port *uap)
     954                 :            : {
     955                 :            :         struct pl011_dmarx_data *dmarx = &uap->dmarx;
     956                 :          0 :         struct dma_chan *rxchan = dmarx->chan;
     957                 :          0 :         struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
     958                 :          0 :                 &dmarx->sgbuf_b : &dmarx->sgbuf_a;
     959                 :            :         size_t pending;
     960                 :            :         struct dma_tx_state state;
     961                 :            :         enum dma_status dmastat;
     962                 :            : 
     963                 :            :         /*
     964                 :            :          * Pause the transfer so we can trust the current counter,
     965                 :            :          * do this before we pause the PL011 block, else we may
     966                 :            :          * overflow the FIFO.
     967                 :            :          */
     968                 :          0 :         if (dmaengine_pause(rxchan))
     969                 :          0 :                 dev_err(uap->port.dev, "unable to pause DMA transfer\n");
     970                 :          0 :         dmastat = rxchan->device->device_tx_status(rxchan,
     971                 :            :                                                    dmarx->cookie, &state);
     972                 :          0 :         if (dmastat != DMA_PAUSED)
     973                 :          0 :                 dev_err(uap->port.dev, "unable to pause DMA transfer\n");
     974                 :            : 
     975                 :            :         /* Disable RX DMA - incoming data will wait in the FIFO */
     976                 :          0 :         uap->dmacr &= ~UART011_RXDMAE;
     977                 :            :         pl011_write(uap->dmacr, uap, REG_DMACR);
     978                 :          0 :         uap->dmarx.running = false;
     979                 :            : 
     980                 :          0 :         pending = sgbuf->sg.length - state.residue;
     981                 :          0 :         BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
     982                 :            :         /* Then we terminate the transfer - we now know our residue */
     983                 :            :         dmaengine_terminate_all(rxchan);
     984                 :            : 
     985                 :            :         /*
     986                 :            :          * This will take the chars we have so far and insert
     987                 :            :          * into the framework.
     988                 :            :          */
     989                 :          0 :         pl011_dma_rx_chars(uap, pending, dmarx->use_buf_b, true);
     990                 :            : 
     991                 :            :         /* Switch buffer & re-trigger DMA job */
     992                 :          0 :         dmarx->use_buf_b = !dmarx->use_buf_b;
     993                 :          0 :         if (pl011_dma_rx_trigger_dma(uap)) {
     994                 :            :                 dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
     995                 :            :                         "fall back to interrupt mode\n");
     996                 :          0 :                 uap->im |= UART011_RXIM;
     997                 :            :                 pl011_write(uap->im, uap, REG_IMSC);
     998                 :            :         }
     999                 :          0 : }
    1000                 :            : 
    1001                 :          0 : static void pl011_dma_rx_callback(void *data)
    1002                 :            : {
    1003                 :            :         struct uart_amba_port *uap = data;
    1004                 :            :         struct pl011_dmarx_data *dmarx = &uap->dmarx;
    1005                 :          0 :         struct dma_chan *rxchan = dmarx->chan;
    1006                 :          0 :         bool lastbuf = dmarx->use_buf_b;
    1007                 :            :         struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
    1008                 :          0 :                 &dmarx->sgbuf_b : &dmarx->sgbuf_a;
    1009                 :            :         size_t pending;
    1010                 :            :         struct dma_tx_state state;
    1011                 :            :         int ret;
    1012                 :            : 
    1013                 :            :         /*
    1014                 :            :          * This completion interrupt occurs typically when the
    1015                 :            :          * RX buffer is totally stuffed but no timeout has yet
    1016                 :            :          * occurred. When that happens, we just want the RX
    1017                 :            :          * routine to flush out the secondary DMA buffer while
    1018                 :            :          * we immediately trigger the next DMA job.
    1019                 :            :          */
    1020                 :            :         spin_lock_irq(&uap->port.lock);
    1021                 :            :         /*
    1022                 :            :          * Rx data can be taken by the UART interrupts during
    1023                 :            :          * the DMA irq handler. So we check the residue here.
    1024                 :            :          */
    1025                 :          0 :         rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
    1026                 :          0 :         pending = sgbuf->sg.length - state.residue;
    1027                 :          0 :         BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
    1028                 :            :         /* Then we terminate the transfer - we now know our residue */
    1029                 :            :         dmaengine_terminate_all(rxchan);
    1030                 :            : 
    1031                 :          0 :         uap->dmarx.running = false;
    1032                 :          0 :         dmarx->use_buf_b = !lastbuf;
    1033                 :          0 :         ret = pl011_dma_rx_trigger_dma(uap);
    1034                 :            : 
    1035                 :          0 :         pl011_dma_rx_chars(uap, pending, lastbuf, false);
    1036                 :            :         spin_unlock_irq(&uap->port.lock);
    1037                 :            :         /*
    1038                 :            :          * Do this check after we picked the DMA chars so we don't
    1039                 :            :          * get some IRQ immediately from RX.
    1040                 :            :          */
    1041                 :          0 :         if (ret) {
    1042                 :            :                 dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
    1043                 :            :                         "fall back to interrupt mode\n");
    1044                 :          0 :                 uap->im |= UART011_RXIM;
    1045                 :            :                 pl011_write(uap->im, uap, REG_IMSC);
    1046                 :            :         }
    1047                 :          0 : }
    1048                 :            : 
    1049                 :            : /*
    1050                 :            :  * Stop accepting received characters, when we're shutting down or
    1051                 :            :  * suspending this port.
    1052                 :            :  * Locking: called with port lock held and IRQs disabled.
    1053                 :            :  */
    1054                 :            : static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
    1055                 :            : {
    1056                 :            :         /* FIXME.  Just disable the DMA enable */
    1057                 :          0 :         uap->dmacr &= ~UART011_RXDMAE;
    1058                 :            :         pl011_write(uap->dmacr, uap, REG_DMACR);
    1059                 :            : }
    1060                 :            : 
    1061                 :            : /*
    1062                 :            :  * Timer handler for Rx DMA polling.
    1063                 :            :  * Every polling, It checks the residue in the dma buffer and transfer
    1064                 :            :  * data to the tty. Also, last_residue is updated for the next polling.
    1065                 :            :  */
    1066                 :          0 : static void pl011_dma_rx_poll(struct timer_list *t)
    1067                 :            : {
    1068                 :            :         struct uart_amba_port *uap = from_timer(uap, t, dmarx.timer);
    1069                 :          0 :         struct tty_port *port = &uap->port.state->port;
    1070                 :            :         struct pl011_dmarx_data *dmarx = &uap->dmarx;
    1071                 :          0 :         struct dma_chan *rxchan = uap->dmarx.chan;
    1072                 :            :         unsigned long flags = 0;
    1073                 :            :         unsigned int dmataken = 0;
    1074                 :            :         unsigned int size = 0;
    1075                 :            :         struct pl011_sgbuf *sgbuf;
    1076                 :            :         int dma_count;
    1077                 :            :         struct dma_tx_state state;
    1078                 :            : 
    1079                 :          0 :         sgbuf = dmarx->use_buf_b ? &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
    1080                 :          0 :         rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
    1081                 :          0 :         if (likely(state.residue < dmarx->last_residue)) {
    1082                 :          0 :                 dmataken = sgbuf->sg.length - dmarx->last_residue;
    1083                 :          0 :                 size = dmarx->last_residue - state.residue;
    1084                 :          0 :                 dma_count = tty_insert_flip_string(port, sgbuf->buf + dmataken,
    1085                 :            :                                 size);
    1086                 :          0 :                 if (dma_count == size)
    1087                 :          0 :                         dmarx->last_residue =  state.residue;
    1088                 :          0 :                 dmarx->last_jiffies = jiffies;
    1089                 :            :         }
    1090                 :          0 :         tty_flip_buffer_push(port);
    1091                 :            : 
    1092                 :            :         /*
    1093                 :            :          * If no data is received in poll_timeout, the driver will fall back
    1094                 :            :          * to interrupt mode. We will retrigger DMA at the first interrupt.
    1095                 :            :          */
    1096                 :          0 :         if (jiffies_to_msecs(jiffies - dmarx->last_jiffies)
    1097                 :          0 :                         > uap->dmarx.poll_timeout) {
    1098                 :            : 
    1099                 :          0 :                 spin_lock_irqsave(&uap->port.lock, flags);
    1100                 :            :                 pl011_dma_rx_stop(uap);
    1101                 :          0 :                 uap->im |= UART011_RXIM;
    1102                 :            :                 pl011_write(uap->im, uap, REG_IMSC);
    1103                 :            :                 spin_unlock_irqrestore(&uap->port.lock, flags);
    1104                 :            : 
    1105                 :          0 :                 uap->dmarx.running = false;
    1106                 :            :                 dmaengine_terminate_all(rxchan);
    1107                 :          0 :                 del_timer(&uap->dmarx.timer);
    1108                 :            :         } else {
    1109                 :          0 :                 mod_timer(&uap->dmarx.timer,
    1110                 :          0 :                         jiffies + msecs_to_jiffies(uap->dmarx.poll_rate));
    1111                 :            :         }
    1112                 :          0 : }
    1113                 :            : 
    1114                 :          3 : static void pl011_dma_startup(struct uart_amba_port *uap)
    1115                 :            : {
    1116                 :            :         int ret;
    1117                 :            : 
    1118                 :          3 :         if (!uap->dma_probed)
    1119                 :          3 :                 pl011_dma_probe(uap);
    1120                 :            : 
    1121                 :          3 :         if (!uap->dmatx.chan)
    1122                 :            :                 return;
    1123                 :            : 
    1124                 :          0 :         uap->dmatx.buf = kmalloc(PL011_DMA_BUFFER_SIZE, GFP_KERNEL | __GFP_DMA);
    1125                 :          0 :         if (!uap->dmatx.buf) {
    1126                 :          0 :                 dev_err(uap->port.dev, "no memory for DMA TX buffer\n");
    1127                 :          0 :                 uap->port.fifosize = uap->fifosize;
    1128                 :          0 :                 return;
    1129                 :            :         }
    1130                 :            : 
    1131                 :          0 :         sg_init_one(&uap->dmatx.sg, uap->dmatx.buf, PL011_DMA_BUFFER_SIZE);
    1132                 :            : 
    1133                 :            :         /* The DMA buffer is now the FIFO the TTY subsystem can use */
    1134                 :          0 :         uap->port.fifosize = PL011_DMA_BUFFER_SIZE;
    1135                 :          0 :         uap->using_tx_dma = true;
    1136                 :            : 
    1137                 :          0 :         if (!uap->dmarx.chan)
    1138                 :            :                 goto skip_rx;
    1139                 :            : 
    1140                 :            :         /* Allocate and map DMA RX buffers */
    1141                 :          0 :         ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
    1142                 :            :                                DMA_FROM_DEVICE);
    1143                 :          0 :         if (ret) {
    1144                 :          0 :                 dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
    1145                 :            :                         "RX buffer A", ret);
    1146                 :          0 :                 goto skip_rx;
    1147                 :            :         }
    1148                 :            : 
    1149                 :          0 :         ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_b,
    1150                 :            :                                DMA_FROM_DEVICE);
    1151                 :          0 :         if (ret) {
    1152                 :          0 :                 dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
    1153                 :            :                         "RX buffer B", ret);
    1154                 :          0 :                 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
    1155                 :            :                                  DMA_FROM_DEVICE);
    1156                 :          0 :                 goto skip_rx;
    1157                 :            :         }
    1158                 :            : 
    1159                 :          0 :         uap->using_rx_dma = true;
    1160                 :            : 
    1161                 :            : skip_rx:
    1162                 :            :         /* Turn on DMA error (RX/TX will be enabled on demand) */
    1163                 :          0 :         uap->dmacr |= UART011_DMAONERR;
    1164                 :            :         pl011_write(uap->dmacr, uap, REG_DMACR);
    1165                 :            : 
    1166                 :            :         /*
    1167                 :            :          * ST Micro variants has some specific dma burst threshold
    1168                 :            :          * compensation. Set this to 16 bytes, so burst will only
    1169                 :            :          * be issued above/below 16 bytes.
    1170                 :            :          */
    1171                 :          0 :         if (uap->vendor->dma_threshold)
    1172                 :            :                 pl011_write(ST_UART011_DMAWM_RX_16 | ST_UART011_DMAWM_TX_16,
    1173                 :            :                             uap, REG_ST_DMAWM);
    1174                 :            : 
    1175                 :          0 :         if (uap->using_rx_dma) {
    1176                 :          0 :                 if (pl011_dma_rx_trigger_dma(uap))
    1177                 :            :                         dev_dbg(uap->port.dev, "could not trigger initial "
    1178                 :            :                                 "RX DMA job, fall back to interrupt mode\n");
    1179                 :          0 :                 if (uap->dmarx.poll_rate) {
    1180                 :          0 :                         timer_setup(&uap->dmarx.timer, pl011_dma_rx_poll, 0);
    1181                 :          0 :                         mod_timer(&uap->dmarx.timer,
    1182                 :            :                                 jiffies +
    1183                 :          0 :                                 msecs_to_jiffies(uap->dmarx.poll_rate));
    1184                 :          0 :                         uap->dmarx.last_residue = PL011_DMA_BUFFER_SIZE;
    1185                 :          0 :                         uap->dmarx.last_jiffies = jiffies;
    1186                 :            :                 }
    1187                 :            :         }
    1188                 :            : }
    1189                 :            : 
    1190                 :          3 : static void pl011_dma_shutdown(struct uart_amba_port *uap)
    1191                 :            : {
    1192                 :          3 :         if (!(uap->using_tx_dma || uap->using_rx_dma))
    1193                 :          3 :                 return;
    1194                 :            : 
    1195                 :            :         /* Disable RX and TX DMA */
    1196                 :          0 :         while (pl011_read(uap, REG_FR) & uap->vendor->fr_busy)
    1197                 :          0 :                 cpu_relax();
    1198                 :            : 
    1199                 :            :         spin_lock_irq(&uap->port.lock);
    1200                 :          0 :         uap->dmacr &= ~(UART011_DMAONERR | UART011_RXDMAE | UART011_TXDMAE);
    1201                 :            :         pl011_write(uap->dmacr, uap, REG_DMACR);
    1202                 :            :         spin_unlock_irq(&uap->port.lock);
    1203                 :            : 
    1204                 :          0 :         if (uap->using_tx_dma) {
    1205                 :            :                 /* In theory, this should already be done by pl011_dma_flush_buffer */
    1206                 :          0 :                 dmaengine_terminate_all(uap->dmatx.chan);
    1207                 :          0 :                 if (uap->dmatx.queued) {
    1208                 :          0 :                         dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
    1209                 :            :                                      DMA_TO_DEVICE);
    1210                 :          0 :                         uap->dmatx.queued = false;
    1211                 :            :                 }
    1212                 :            : 
    1213                 :          0 :                 kfree(uap->dmatx.buf);
    1214                 :          0 :                 uap->using_tx_dma = false;
    1215                 :            :         }
    1216                 :            : 
    1217                 :          0 :         if (uap->using_rx_dma) {
    1218                 :          0 :                 dmaengine_terminate_all(uap->dmarx.chan);
    1219                 :            :                 /* Clean up the RX DMA */
    1220                 :          0 :                 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a, DMA_FROM_DEVICE);
    1221                 :          0 :                 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_b, DMA_FROM_DEVICE);
    1222                 :          0 :                 if (uap->dmarx.poll_rate)
    1223                 :          0 :                         del_timer_sync(&uap->dmarx.timer);
    1224                 :          0 :                 uap->using_rx_dma = false;
    1225                 :            :         }
    1226                 :            : }
    1227                 :            : 
    1228                 :            : static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
    1229                 :            : {
    1230                 :          1 :         return uap->using_rx_dma;
    1231                 :            : }
    1232                 :            : 
    1233                 :            : static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
    1234                 :            : {
    1235                 :          3 :         return uap->using_rx_dma && uap->dmarx.running;
    1236                 :            : }
    1237                 :            : 
    1238                 :            : #else
    1239                 :            : /* Blank functions if the DMA engine is not available */
    1240                 :            : static inline void pl011_dma_probe(struct uart_amba_port *uap)
    1241                 :            : {
    1242                 :            : }
    1243                 :            : 
    1244                 :            : static inline void pl011_dma_remove(struct uart_amba_port *uap)
    1245                 :            : {
    1246                 :            : }
    1247                 :            : 
    1248                 :            : static inline void pl011_dma_startup(struct uart_amba_port *uap)
    1249                 :            : {
    1250                 :            : }
    1251                 :            : 
    1252                 :            : static inline void pl011_dma_shutdown(struct uart_amba_port *uap)
    1253                 :            : {
    1254                 :            : }
    1255                 :            : 
    1256                 :            : static inline bool pl011_dma_tx_irq(struct uart_amba_port *uap)
    1257                 :            : {
    1258                 :            :         return false;
    1259                 :            : }
    1260                 :            : 
    1261                 :            : static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
    1262                 :            : {
    1263                 :            : }
    1264                 :            : 
    1265                 :            : static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
    1266                 :            : {
    1267                 :            :         return false;
    1268                 :            : }
    1269                 :            : 
    1270                 :            : static inline void pl011_dma_rx_irq(struct uart_amba_port *uap)
    1271                 :            : {
    1272                 :            : }
    1273                 :            : 
    1274                 :            : static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
    1275                 :            : {
    1276                 :            : }
    1277                 :            : 
    1278                 :            : static inline int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
    1279                 :            : {
    1280                 :            :         return -EIO;
    1281                 :            : }
    1282                 :            : 
    1283                 :            : static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
    1284                 :            : {
    1285                 :            :         return false;
    1286                 :            : }
    1287                 :            : 
    1288                 :            : static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
    1289                 :            : {
    1290                 :            :         return false;
    1291                 :            : }
    1292                 :            : 
    1293                 :            : #define pl011_dma_flush_buffer  NULL
    1294                 :            : #endif
    1295                 :            : 
    1296                 :          3 : static void pl011_stop_tx(struct uart_port *port)
    1297                 :            : {
    1298                 :            :         struct uart_amba_port *uap =
    1299                 :            :             container_of(port, struct uart_amba_port, port);
    1300                 :            : 
    1301                 :          3 :         uap->im &= ~UART011_TXIM;
    1302                 :            :         pl011_write(uap->im, uap, REG_IMSC);
    1303                 :            :         pl011_dma_tx_stop(uap);
    1304                 :          3 : }
    1305                 :            : 
    1306                 :            : static bool pl011_tx_chars(struct uart_amba_port *uap, bool from_irq);
    1307                 :            : 
    1308                 :            : /* Start TX with programmed I/O only (no DMA) */
    1309                 :          3 : static void pl011_start_tx_pio(struct uart_amba_port *uap)
    1310                 :            : {
    1311                 :          3 :         if (pl011_tx_chars(uap, false)) {
    1312                 :          2 :                 uap->im |= UART011_TXIM;
    1313                 :            :                 pl011_write(uap->im, uap, REG_IMSC);
    1314                 :            :         }
    1315                 :          3 : }
    1316                 :            : 
    1317                 :          3 : static void pl011_start_tx(struct uart_port *port)
    1318                 :            : {
    1319                 :            :         struct uart_amba_port *uap =
    1320                 :            :             container_of(port, struct uart_amba_port, port);
    1321                 :            : 
    1322                 :          3 :         if (!pl011_dma_tx_start(uap))
    1323                 :          3 :                 pl011_start_tx_pio(uap);
    1324                 :          3 : }
    1325                 :            : 
    1326                 :          0 : static void pl011_throttle(struct uart_port *port)
    1327                 :            : {
    1328                 :            :         struct uart_amba_port *uap =
    1329                 :            :             container_of(port, struct uart_amba_port, port);
    1330                 :            :         unsigned long flags;
    1331                 :            : 
    1332                 :          0 :         spin_lock_irqsave(&uap->port.lock, flags);
    1333                 :          0 :         uap->im &= ~(UART011_RTIM | UART011_RXIM);
    1334                 :            :         pl011_write(uap->im, uap, REG_IMSC);
    1335                 :            :         spin_unlock_irqrestore(&uap->port.lock, flags);
    1336                 :          0 : }
    1337                 :            : 
    1338                 :          0 : static void pl011_unthrottle(struct uart_port *port)
    1339                 :            : {
    1340                 :            :         struct uart_amba_port *uap =
    1341                 :            :             container_of(port, struct uart_amba_port, port);
    1342                 :            :         unsigned long flags;
    1343                 :            : 
    1344                 :          0 :         spin_lock_irqsave(&uap->port.lock, flags);
    1345                 :          0 :         uap->im |= UART011_RTIM;
    1346                 :          0 :         if (!pl011_dma_rx_running(uap))
    1347                 :          0 :             uap->im |= UART011_RXIM;
    1348                 :          0 :         pl011_write(uap->im, uap, REG_IMSC);
    1349                 :            :         spin_unlock_irqrestore(&uap->port.lock, flags);
    1350                 :          0 : }
    1351                 :            : 
    1352                 :          0 : static void pl011_stop_rx(struct uart_port *port)
    1353                 :            : {
    1354                 :            :         struct uart_amba_port *uap =
    1355                 :            :             container_of(port, struct uart_amba_port, port);
    1356                 :            : 
    1357                 :          0 :         uap->im &= ~(UART011_RXIM|UART011_RTIM|UART011_FEIM|
    1358                 :            :                      UART011_PEIM|UART011_BEIM|UART011_OEIM);
    1359                 :            :         pl011_write(uap->im, uap, REG_IMSC);
    1360                 :            : 
    1361                 :            :         pl011_dma_rx_stop(uap);
    1362                 :          0 : }
    1363                 :            : 
    1364                 :          0 : static void pl011_enable_ms(struct uart_port *port)
    1365                 :            : {
    1366                 :            :         struct uart_amba_port *uap =
    1367                 :            :             container_of(port, struct uart_amba_port, port);
    1368                 :            : 
    1369                 :          0 :         uap->im |= UART011_RIMIM|UART011_CTSMIM|UART011_DCDMIM|UART011_DSRMIM;
    1370                 :            :         pl011_write(uap->im, uap, REG_IMSC);
    1371                 :          0 : }
    1372                 :            : 
    1373                 :          1 : static void pl011_rx_chars(struct uart_amba_port *uap)
    1374                 :            : __releases(&uap->port.lock)
    1375                 :            : __acquires(&uap->port.lock)
    1376                 :            : {
    1377                 :          1 :         pl011_fifo_to_tty(uap);
    1378                 :            : 
    1379                 :          1 :         uap->irq_locked = 0;
    1380                 :            :         spin_unlock(&uap->port.lock);
    1381                 :          1 :         tty_flip_buffer_push(&uap->port.state->port);
    1382                 :            :         /*
    1383                 :            :          * If we were temporarily out of DMA mode for a while,
    1384                 :            :          * attempt to switch back to DMA mode again.
    1385                 :            :          */
    1386                 :          1 :         if (pl011_dma_rx_available(uap)) {
    1387                 :          0 :                 if (pl011_dma_rx_trigger_dma(uap)) {
    1388                 :            :                         dev_dbg(uap->port.dev, "could not trigger RX DMA job "
    1389                 :            :                                 "fall back to interrupt mode again\n");
    1390                 :          0 :                         uap->im |= UART011_RXIM;
    1391                 :            :                         pl011_write(uap->im, uap, REG_IMSC);
    1392                 :            :                 } else {
    1393                 :            : #ifdef CONFIG_DMA_ENGINE
    1394                 :            :                         /* Start Rx DMA poll */
    1395                 :          0 :                         if (uap->dmarx.poll_rate) {
    1396                 :          0 :                                 uap->dmarx.last_jiffies = jiffies;
    1397                 :          0 :                                 uap->dmarx.last_residue      = PL011_DMA_BUFFER_SIZE;
    1398                 :          0 :                                 mod_timer(&uap->dmarx.timer,
    1399                 :            :                                         jiffies +
    1400                 :            :                                         msecs_to_jiffies(uap->dmarx.poll_rate));
    1401                 :            :                         }
    1402                 :            : #endif
    1403                 :            :                 }
    1404                 :            :         }
    1405                 :            :         spin_lock(&uap->port.lock);
    1406                 :          1 : }
    1407                 :            : 
    1408                 :          3 : static bool pl011_tx_char(struct uart_amba_port *uap, unsigned char c,
    1409                 :            :                           bool from_irq)
    1410                 :            : {
    1411                 :          3 :         if (unlikely(!from_irq) &&
    1412                 :          3 :             pl011_read(uap, REG_FR) & UART01x_FR_TXFF)
    1413                 :            :                 return false; /* unable to transmit character */
    1414                 :            : 
    1415                 :          3 :         pl011_write(c, uap, REG_DR);
    1416                 :          3 :         mb();
    1417                 :          3 :         uap->port.icount.tx++;
    1418                 :            : 
    1419                 :          3 :         return true;
    1420                 :            : }
    1421                 :            : 
    1422                 :            : /* Returns true if tx interrupts have to be (kept) enabled  */
    1423                 :          3 : static bool pl011_tx_chars(struct uart_amba_port *uap, bool from_irq)
    1424                 :            : {
    1425                 :          3 :         struct circ_buf *xmit = &uap->port.state->xmit;
    1426                 :          3 :         int count = uap->fifosize >> 1;
    1427                 :            : 
    1428                 :          3 :         if (uap->port.x_char) {
    1429                 :          0 :                 if (!pl011_tx_char(uap, uap->port.x_char, from_irq))
    1430                 :            :                         return true;
    1431                 :          0 :                 uap->port.x_char = 0;
    1432                 :          0 :                 --count;
    1433                 :            :         }
    1434                 :          3 :         if (uart_circ_empty(xmit) || uart_tx_stopped(&uap->port)) {
    1435                 :          3 :                 pl011_stop_tx(&uap->port);
    1436                 :          3 :                 return false;
    1437                 :            :         }
    1438                 :            : 
    1439                 :            :         /* If we are using DMA mode, try to send some characters. */
    1440                 :          3 :         if (pl011_dma_tx_irq(uap))
    1441                 :            :                 return true;
    1442                 :            : 
    1443                 :            :         do {
    1444                 :          3 :                 if (likely(from_irq) && count-- == 0)
    1445                 :            :                         break;
    1446                 :            : 
    1447                 :          3 :                 if (likely(from_irq) && count == 0 &&
    1448                 :          2 :                     pl011_read(uap, REG_FR) & UART01x_FR_TXFF)
    1449                 :            :                         break;
    1450                 :            : 
    1451                 :          3 :                 if (!pl011_tx_char(uap, xmit->buf[xmit->tail], from_irq))
    1452                 :            :                         break;
    1453                 :            : 
    1454                 :          3 :                 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
    1455                 :          3 :         } while (!uart_circ_empty(xmit));
    1456                 :            : 
    1457                 :          3 :         if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
    1458                 :          3 :                 uart_write_wakeup(&uap->port);
    1459                 :            : 
    1460                 :          3 :         if (uart_circ_empty(xmit)) {
    1461                 :          3 :                 pl011_stop_tx(&uap->port);
    1462                 :          3 :                 return false;
    1463                 :            :         }
    1464                 :            :         return true;
    1465                 :            : }
    1466                 :            : 
    1467                 :          0 : static void pl011_modem_status(struct uart_amba_port *uap)
    1468                 :            : {
    1469                 :            :         unsigned int status, delta;
    1470                 :            : 
    1471                 :          0 :         status = pl011_read(uap, REG_FR) & UART01x_FR_MODEM_ANY;
    1472                 :            : 
    1473                 :          0 :         delta = status ^ uap->old_status;
    1474                 :          0 :         uap->old_status = status;
    1475                 :            : 
    1476                 :          0 :         if (!delta)
    1477                 :          0 :                 return;
    1478                 :            : 
    1479                 :          0 :         if (delta & UART01x_FR_DCD)
    1480                 :          0 :                 uart_handle_dcd_change(&uap->port, status & UART01x_FR_DCD);
    1481                 :            : 
    1482                 :          0 :         if (delta & uap->vendor->fr_dsr)
    1483                 :          0 :                 uap->port.icount.dsr++;
    1484                 :            : 
    1485                 :          0 :         if (delta & uap->vendor->fr_cts)
    1486                 :          0 :                 uart_handle_cts_change(&uap->port,
    1487                 :            :                                        status & uap->vendor->fr_cts);
    1488                 :            : 
    1489                 :          0 :         wake_up_interruptible(&uap->port.state->port.delta_msr_wait);
    1490                 :            : }
    1491                 :            : 
    1492                 :          3 : static void check_apply_cts_event_workaround(struct uart_amba_port *uap)
    1493                 :            : {
    1494                 :            :         unsigned int dummy_read;
    1495                 :            : 
    1496                 :          3 :         if (!uap->vendor->cts_event_workaround)
    1497                 :          3 :                 return;
    1498                 :            : 
    1499                 :            :         /* workaround to make sure that all bits are unlocked.. */
    1500                 :            :         pl011_write(0x00, uap, REG_ICR);
    1501                 :            : 
    1502                 :            :         /*
    1503                 :            :          * WA: introduce 26ns(1 uart clk) delay before W1C;
    1504                 :            :          * single apb access will incur 2 pclk(133.12Mhz) delay,
    1505                 :            :          * so add 2 dummy reads
    1506                 :            :          */
    1507                 :            :         dummy_read = pl011_read(uap, REG_ICR);
    1508                 :            :         dummy_read = pl011_read(uap, REG_ICR);
    1509                 :            : }
    1510                 :            : 
    1511                 :          3 : static irqreturn_t pl011_int(int irq, void *dev_id)
    1512                 :            : {
    1513                 :            :         struct uart_amba_port *uap = dev_id;
    1514                 :            :         unsigned long flags;
    1515                 :            :         unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT;
    1516                 :            :         int handled = 0;
    1517                 :            : 
    1518                 :          3 :         spin_lock_irqsave(&uap->port.lock, flags);
    1519                 :          3 :         uap->irq_locked = 1;
    1520                 :          3 :         status = pl011_read(uap, REG_RIS) & uap->im;
    1521                 :          3 :         if (status) {
    1522                 :            :                 do {
    1523                 :          3 :                         check_apply_cts_event_workaround(uap);
    1524                 :            : 
    1525                 :          3 :                         pl011_write(status & ~(UART011_TXIS|UART011_RTIS|
    1526                 :            :                                                UART011_RXIS),
    1527                 :            :                                     uap, REG_ICR);
    1528                 :            : 
    1529                 :          3 :                         if (status & (UART011_RTIS|UART011_RXIS)) {
    1530                 :          1 :                                 if (pl011_dma_rx_running(uap))
    1531                 :          0 :                                         pl011_dma_rx_irq(uap);
    1532                 :            :                                 else
    1533                 :          1 :                                         pl011_rx_chars(uap);
    1534                 :            :                         }
    1535                 :          3 :                         if (status & (UART011_DSRMIS|UART011_DCDMIS|
    1536                 :            :                                       UART011_CTSMIS|UART011_RIMIS))
    1537                 :          0 :                                 pl011_modem_status(uap);
    1538                 :          3 :                         if (status & UART011_TXIS)
    1539                 :          2 :                                 pl011_tx_chars(uap, uap->irq_locked);
    1540                 :            : 
    1541                 :          3 :                         if (pass_counter-- == 0)
    1542                 :            :                                 break;
    1543                 :            : 
    1544                 :          3 :                         status = pl011_read(uap, REG_RIS) & uap->im;
    1545                 :          3 :                 } while (status != 0);
    1546                 :            :                 handled = 1;
    1547                 :            :         }
    1548                 :            : 
    1549                 :            :         spin_unlock_irqrestore(&uap->port.lock, flags);
    1550                 :            : 
    1551                 :          3 :         return IRQ_RETVAL(handled);
    1552                 :            : }
    1553                 :            : 
    1554                 :          3 : static unsigned int pl011_tx_empty(struct uart_port *port)
    1555                 :            : {
    1556                 :            :         struct uart_amba_port *uap =
    1557                 :            :             container_of(port, struct uart_amba_port, port);
    1558                 :            : 
    1559                 :            :         /* Allow feature register bits to be inverted to work around errata */
    1560                 :          3 :         unsigned int status = pl011_read(uap, REG_FR) ^ uap->vendor->inv_fr;
    1561                 :            : 
    1562                 :          3 :         return status & (uap->vendor->fr_busy | UART01x_FR_TXFF) ?
    1563                 :          3 :                                                         0 : TIOCSER_TEMT;
    1564                 :            : }
    1565                 :            : 
    1566                 :          0 : static unsigned int pl011_get_mctrl(struct uart_port *port)
    1567                 :            : {
    1568                 :            :         struct uart_amba_port *uap =
    1569                 :            :             container_of(port, struct uart_amba_port, port);
    1570                 :            :         unsigned int result = 0;
    1571                 :            :         unsigned int status = pl011_read(uap, REG_FR);
    1572                 :            : 
    1573                 :            : #define TIOCMBIT(uartbit, tiocmbit)     \
    1574                 :            :         if (status & uartbit)               \
    1575                 :            :                 result |= tiocmbit
    1576                 :            : 
    1577                 :          0 :         TIOCMBIT(UART01x_FR_DCD, TIOCM_CAR);
    1578                 :          0 :         TIOCMBIT(uap->vendor->fr_dsr, TIOCM_DSR);
    1579                 :          0 :         TIOCMBIT(uap->vendor->fr_cts, TIOCM_CTS);
    1580                 :          0 :         TIOCMBIT(uap->vendor->fr_ri, TIOCM_RNG);
    1581                 :            : #undef TIOCMBIT
    1582                 :          0 :         return result;
    1583                 :            : }
    1584                 :            : 
    1585                 :          3 : static void pl011_set_mctrl(struct uart_port *port, unsigned int mctrl)
    1586                 :            : {
    1587                 :            :         struct uart_amba_port *uap =
    1588                 :            :             container_of(port, struct uart_amba_port, port);
    1589                 :            :         unsigned int cr;
    1590                 :            : 
    1591                 :            :         cr = pl011_read(uap, REG_CR);
    1592                 :            : 
    1593                 :            : #define TIOCMBIT(tiocmbit, uartbit)             \
    1594                 :            :         if (mctrl & tiocmbit)               \
    1595                 :            :                 cr |= uartbit;          \
    1596                 :            :         else                            \
    1597                 :            :                 cr &= ~uartbit
    1598                 :            : 
    1599                 :          3 :         TIOCMBIT(TIOCM_RTS, UART011_CR_RTS);
    1600                 :          3 :         TIOCMBIT(TIOCM_DTR, UART011_CR_DTR);
    1601                 :          3 :         TIOCMBIT(TIOCM_OUT1, UART011_CR_OUT1);
    1602                 :          3 :         TIOCMBIT(TIOCM_OUT2, UART011_CR_OUT2);
    1603                 :          3 :         TIOCMBIT(TIOCM_LOOP, UART011_CR_LBE);
    1604                 :            : 
    1605                 :          3 :         if (port->status & UPSTAT_AUTORTS) {
    1606                 :            :                 /* We need to disable auto-RTS if we want to turn RTS off */
    1607                 :          0 :                 TIOCMBIT(TIOCM_RTS, UART011_CR_RTSEN);
    1608                 :            :         }
    1609                 :            : #undef TIOCMBIT
    1610                 :            : 
    1611                 :            :         pl011_write(cr, uap, REG_CR);
    1612                 :          3 : }
    1613                 :            : 
    1614                 :          0 : static void pl011_break_ctl(struct uart_port *port, int break_state)
    1615                 :            : {
    1616                 :            :         struct uart_amba_port *uap =
    1617                 :            :             container_of(port, struct uart_amba_port, port);
    1618                 :            :         unsigned long flags;
    1619                 :            :         unsigned int lcr_h;
    1620                 :            : 
    1621                 :          0 :         spin_lock_irqsave(&uap->port.lock, flags);
    1622                 :            :         lcr_h = pl011_read(uap, REG_LCRH_TX);
    1623                 :          0 :         if (break_state == -1)
    1624                 :          0 :                 lcr_h |= UART01x_LCRH_BRK;
    1625                 :            :         else
    1626                 :          0 :                 lcr_h &= ~UART01x_LCRH_BRK;
    1627                 :            :         pl011_write(lcr_h, uap, REG_LCRH_TX);
    1628                 :            :         spin_unlock_irqrestore(&uap->port.lock, flags);
    1629                 :          0 : }
    1630                 :            : 
    1631                 :            : #ifdef CONFIG_CONSOLE_POLL
    1632                 :            : 
    1633                 :          0 : static void pl011_quiesce_irqs(struct uart_port *port)
    1634                 :            : {
    1635                 :            :         struct uart_amba_port *uap =
    1636                 :            :             container_of(port, struct uart_amba_port, port);
    1637                 :            : 
    1638                 :            :         pl011_write(pl011_read(uap, REG_MIS), uap, REG_ICR);
    1639                 :            :         /*
    1640                 :            :          * There is no way to clear TXIM as this is "ready to transmit IRQ", so
    1641                 :            :          * we simply mask it. start_tx() will unmask it.
    1642                 :            :          *
    1643                 :            :          * Note we can race with start_tx(), and if the race happens, the
    1644                 :            :          * polling user might get another interrupt just after we clear it.
    1645                 :            :          * But it should be OK and can happen even w/o the race, e.g.
    1646                 :            :          * controller immediately got some new data and raised the IRQ.
    1647                 :            :          *
    1648                 :            :          * And whoever uses polling routines assumes that it manages the device
    1649                 :            :          * (including tx queue), so we're also fine with start_tx()'s caller
    1650                 :            :          * side.
    1651                 :            :          */
    1652                 :          0 :         pl011_write(pl011_read(uap, REG_IMSC) & ~UART011_TXIM, uap,
    1653                 :            :                     REG_IMSC);
    1654                 :          0 : }
    1655                 :            : 
    1656                 :          0 : static int pl011_get_poll_char(struct uart_port *port)
    1657                 :            : {
    1658                 :            :         struct uart_amba_port *uap =
    1659                 :            :             container_of(port, struct uart_amba_port, port);
    1660                 :            :         unsigned int status;
    1661                 :            : 
    1662                 :            :         /*
    1663                 :            :          * The caller might need IRQs lowered, e.g. if used with KDB NMI
    1664                 :            :          * debugger.
    1665                 :            :          */
    1666                 :          0 :         pl011_quiesce_irqs(port);
    1667                 :            : 
    1668                 :            :         status = pl011_read(uap, REG_FR);
    1669                 :          0 :         if (status & UART01x_FR_RXFE)
    1670                 :            :                 return NO_POLL_CHAR;
    1671                 :            : 
    1672                 :          0 :         return pl011_read(uap, REG_DR);
    1673                 :            : }
    1674                 :            : 
    1675                 :          0 : static void pl011_put_poll_char(struct uart_port *port,
    1676                 :            :                          unsigned char ch)
    1677                 :            : {
    1678                 :            :         struct uart_amba_port *uap =
    1679                 :            :             container_of(port, struct uart_amba_port, port);
    1680                 :            : 
    1681                 :          0 :         while (pl011_read(uap, REG_FR) & UART01x_FR_TXFF)
    1682                 :          0 :                 cpu_relax();
    1683                 :            : 
    1684                 :          0 :         pl011_write(ch, uap, REG_DR);
    1685                 :          0 : }
    1686                 :            : 
    1687                 :            : #endif /* CONFIG_CONSOLE_POLL */
    1688                 :            : 
    1689                 :          0 : unsigned long pl011_clk_round(unsigned long clk)
    1690                 :            : {
    1691                 :            :         unsigned long scaler;
    1692                 :            : 
    1693                 :            :         /*
    1694                 :            :          * If increasing a clock by less than 0.1% changes it
    1695                 :            :          * from ..999.. to ..000.., round up.
    1696                 :            :          */
    1697                 :            :         scaler = 1;
    1698                 :          3 :         while (scaler * 100000 < clk)
    1699                 :          2 :                 scaler *= 10;
    1700                 :          3 :         if ((clk + scaler - 1)/scaler % 1000 == 0)
    1701                 :          3 :                 clk = (clk/scaler + 1) * scaler;
    1702                 :            : 
    1703                 :          0 :         return clk;
    1704                 :            : }
    1705                 :            : 
    1706                 :          3 : static int pl011_hwinit(struct uart_port *port)
    1707                 :            : {
    1708                 :            :         struct uart_amba_port *uap =
    1709                 :            :             container_of(port, struct uart_amba_port, port);
    1710                 :            :         int retval;
    1711                 :            : 
    1712                 :            :         /* Optionaly enable pins to be muxed in and configured */
    1713                 :          3 :         pinctrl_pm_select_default_state(port->dev);
    1714                 :            : 
    1715                 :            :         /*
    1716                 :            :          * Try to enable the clock producer.
    1717                 :            :          */
    1718                 :          3 :         retval = clk_prepare_enable(uap->clk);
    1719                 :          3 :         if (retval)
    1720                 :            :                 return retval;
    1721                 :            : 
    1722                 :          3 :         uap->port.uartclk = pl011_clk_round(clk_get_rate(uap->clk));
    1723                 :            : 
    1724                 :            :         /* Clear pending error and receive interrupts */
    1725                 :            :         pl011_write(UART011_OEIS | UART011_BEIS | UART011_PEIS |
    1726                 :            :                     UART011_FEIS | UART011_RTIS | UART011_RXIS,
    1727                 :            :                     uap, REG_ICR);
    1728                 :            : 
    1729                 :            :         /*
    1730                 :            :          * Save interrupts enable mask, and enable RX interrupts in case if
    1731                 :            :          * the interrupt is used for NMI entry.
    1732                 :            :          */
    1733                 :          3 :         uap->im = pl011_read(uap, REG_IMSC);
    1734                 :            :         pl011_write(UART011_RTIM | UART011_RXIM, uap, REG_IMSC);
    1735                 :            : 
    1736                 :          3 :         if (dev_get_platdata(uap->port.dev)) {
    1737                 :            :                 struct amba_pl011_data *plat;
    1738                 :            : 
    1739                 :            :                 plat = dev_get_platdata(uap->port.dev);
    1740                 :          0 :                 if (plat->init)
    1741                 :          0 :                         plat->init();
    1742                 :            :         }
    1743                 :            :         return 0;
    1744                 :            : }
    1745                 :            : 
    1746                 :            : static bool pl011_split_lcrh(const struct uart_amba_port *uap)
    1747                 :            : {
    1748                 :            :         return pl011_reg_to_offset(uap, REG_LCRH_RX) !=
    1749                 :            :                pl011_reg_to_offset(uap, REG_LCRH_TX);
    1750                 :            : }
    1751                 :            : 
    1752                 :          3 : static void pl011_write_lcr_h(struct uart_amba_port *uap, unsigned int lcr_h)
    1753                 :            : {
    1754                 :            :         pl011_write(lcr_h, uap, REG_LCRH_RX);
    1755                 :          3 :         if (pl011_split_lcrh(uap)) {
    1756                 :            :                 int i;
    1757                 :            :                 /*
    1758                 :            :                  * Wait 10 PCLKs before writing LCRH_TX register,
    1759                 :            :                  * to get this delay write read only register 10 times
    1760                 :            :                  */
    1761                 :          0 :                 for (i = 0; i < 10; ++i)
    1762                 :            :                         pl011_write(0xff, uap, REG_MIS);
    1763                 :            :                 pl011_write(lcr_h, uap, REG_LCRH_TX);
    1764                 :            :         }
    1765                 :          3 : }
    1766                 :            : 
    1767                 :          3 : static int pl011_allocate_irq(struct uart_amba_port *uap)
    1768                 :            : {
    1769                 :          3 :         pl011_write(uap->im, uap, REG_IMSC);
    1770                 :            : 
    1771                 :          3 :         return request_irq(uap->port.irq, pl011_int, IRQF_SHARED, "uart-pl011", uap);
    1772                 :            : }
    1773                 :            : 
    1774                 :            : /*
    1775                 :            :  * Enable interrupts, only timeouts when using DMA
    1776                 :            :  * if initial RX DMA job failed, start in interrupt mode
    1777                 :            :  * as well.
    1778                 :            :  */
    1779                 :          3 : static void pl011_enable_interrupts(struct uart_amba_port *uap)
    1780                 :            : {
    1781                 :            :         unsigned int i;
    1782                 :            : 
    1783                 :            :         spin_lock_irq(&uap->port.lock);
    1784                 :            : 
    1785                 :            :         /* Clear out any spuriously appearing RX interrupts */
    1786                 :            :         pl011_write(UART011_RTIS | UART011_RXIS, uap, REG_ICR);
    1787                 :            : 
    1788                 :            :         /*
    1789                 :            :          * RXIS is asserted only when the RX FIFO transitions from below
    1790                 :            :          * to above the trigger threshold.  If the RX FIFO is already
    1791                 :            :          * full to the threshold this can't happen and RXIS will now be
    1792                 :            :          * stuck off.  Drain the RX FIFO explicitly to fix this:
    1793                 :            :          */
    1794                 :          0 :         for (i = 0; i < uap->fifosize * 2; ++i) {
    1795                 :          3 :                 if (pl011_read(uap, REG_FR) & UART01x_FR_RXFE)
    1796                 :            :                         break;
    1797                 :            : 
    1798                 :            :                 pl011_read(uap, REG_DR);
    1799                 :            :         }
    1800                 :            : 
    1801                 :          3 :         uap->im = UART011_RTIM;
    1802                 :          3 :         if (!pl011_dma_rx_running(uap))
    1803                 :          3 :                 uap->im |= UART011_RXIM;
    1804                 :          3 :         pl011_write(uap->im, uap, REG_IMSC);
    1805                 :            :         spin_unlock_irq(&uap->port.lock);
    1806                 :          3 : }
    1807                 :            : 
    1808                 :          3 : static int pl011_startup(struct uart_port *port)
    1809                 :            : {
    1810                 :            :         struct uart_amba_port *uap =
    1811                 :            :             container_of(port, struct uart_amba_port, port);
    1812                 :            :         unsigned int cr;
    1813                 :            :         int retval;
    1814                 :            : 
    1815                 :          3 :         retval = pl011_hwinit(port);
    1816                 :          3 :         if (retval)
    1817                 :            :                 goto clk_dis;
    1818                 :            : 
    1819                 :          3 :         retval = pl011_allocate_irq(uap);
    1820                 :          3 :         if (retval)
    1821                 :            :                 goto clk_dis;
    1822                 :            : 
    1823                 :          3 :         pl011_write(uap->vendor->ifls, uap, REG_IFLS);
    1824                 :            : 
    1825                 :            :         spin_lock_irq(&uap->port.lock);
    1826                 :            : 
    1827                 :            :         /* restore RTS and DTR */
    1828                 :          3 :         cr = uap->old_cr & (UART011_CR_RTS | UART011_CR_DTR);
    1829                 :          3 :         cr |= UART01x_CR_UARTEN | UART011_CR_RXE | UART011_CR_TXE;
    1830                 :            :         pl011_write(cr, uap, REG_CR);
    1831                 :            : 
    1832                 :            :         spin_unlock_irq(&uap->port.lock);
    1833                 :            : 
    1834                 :            :         /*
    1835                 :            :          * initialise the old status of the modem signals
    1836                 :            :          */
    1837                 :          3 :         uap->old_status = pl011_read(uap, REG_FR) & UART01x_FR_MODEM_ANY;
    1838                 :            : 
    1839                 :            :         /* Startup DMA */
    1840                 :          3 :         pl011_dma_startup(uap);
    1841                 :            : 
    1842                 :          3 :         pl011_enable_interrupts(uap);
    1843                 :            : 
    1844                 :          3 :         return 0;
    1845                 :            : 
    1846                 :            :  clk_dis:
    1847                 :          0 :         clk_disable_unprepare(uap->clk);
    1848                 :          0 :         return retval;
    1849                 :            : }
    1850                 :            : 
    1851                 :          0 : static int sbsa_uart_startup(struct uart_port *port)
    1852                 :            : {
    1853                 :            :         struct uart_amba_port *uap =
    1854                 :            :                 container_of(port, struct uart_amba_port, port);
    1855                 :            :         int retval;
    1856                 :            : 
    1857                 :          0 :         retval = pl011_hwinit(port);
    1858                 :          0 :         if (retval)
    1859                 :            :                 return retval;
    1860                 :            : 
    1861                 :          0 :         retval = pl011_allocate_irq(uap);
    1862                 :          0 :         if (retval)
    1863                 :            :                 return retval;
    1864                 :            : 
    1865                 :            :         /* The SBSA UART does not support any modem status lines. */
    1866                 :          0 :         uap->old_status = 0;
    1867                 :            : 
    1868                 :          0 :         pl011_enable_interrupts(uap);
    1869                 :            : 
    1870                 :          0 :         return 0;
    1871                 :            : }
    1872                 :            : 
    1873                 :          3 : static void pl011_shutdown_channel(struct uart_amba_port *uap,
    1874                 :            :                                         unsigned int lcrh)
    1875                 :            : {
    1876                 :            :       unsigned long val;
    1877                 :            : 
    1878                 :            :       val = pl011_read(uap, lcrh);
    1879                 :          3 :       val &= ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN);
    1880                 :            :       pl011_write(val, uap, lcrh);
    1881                 :          3 : }
    1882                 :            : 
    1883                 :            : /*
    1884                 :            :  * disable the port. It should not disable RTS and DTR.
    1885                 :            :  * Also RTS and DTR state should be preserved to restore
    1886                 :            :  * it during startup().
    1887                 :            :  */
    1888                 :          3 : static void pl011_disable_uart(struct uart_amba_port *uap)
    1889                 :            : {
    1890                 :            :         unsigned int cr;
    1891                 :            : 
    1892                 :          3 :         uap->port.status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS);
    1893                 :            :         spin_lock_irq(&uap->port.lock);
    1894                 :            :         cr = pl011_read(uap, REG_CR);
    1895                 :          3 :         uap->old_cr = cr;
    1896                 :          3 :         cr &= UART011_CR_RTS | UART011_CR_DTR;
    1897                 :          3 :         cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
    1898                 :            :         pl011_write(cr, uap, REG_CR);
    1899                 :            :         spin_unlock_irq(&uap->port.lock);
    1900                 :            : 
    1901                 :            :         /*
    1902                 :            :          * disable break condition and fifos
    1903                 :            :          */
    1904                 :          3 :         pl011_shutdown_channel(uap, REG_LCRH_RX);
    1905                 :          3 :         if (pl011_split_lcrh(uap))
    1906                 :          0 :                 pl011_shutdown_channel(uap, REG_LCRH_TX);
    1907                 :          3 : }
    1908                 :            : 
    1909                 :          3 : static void pl011_disable_interrupts(struct uart_amba_port *uap)
    1910                 :            : {
    1911                 :            :         spin_lock_irq(&uap->port.lock);
    1912                 :            : 
    1913                 :            :         /* mask all interrupts and clear all pending ones */
    1914                 :          3 :         uap->im = 0;
    1915                 :            :         pl011_write(uap->im, uap, REG_IMSC);
    1916                 :            :         pl011_write(0xffff, uap, REG_ICR);
    1917                 :            : 
    1918                 :            :         spin_unlock_irq(&uap->port.lock);
    1919                 :          3 : }
    1920                 :            : 
    1921                 :          3 : static void pl011_shutdown(struct uart_port *port)
    1922                 :            : {
    1923                 :            :         struct uart_amba_port *uap =
    1924                 :            :                 container_of(port, struct uart_amba_port, port);
    1925                 :            : 
    1926                 :          3 :         pl011_disable_interrupts(uap);
    1927                 :            : 
    1928                 :          3 :         pl011_dma_shutdown(uap);
    1929                 :            : 
    1930                 :          3 :         free_irq(uap->port.irq, uap);
    1931                 :            : 
    1932                 :          3 :         pl011_disable_uart(uap);
    1933                 :            : 
    1934                 :            :         /*
    1935                 :            :          * Shut down the clock producer
    1936                 :            :          */
    1937                 :          3 :         clk_disable_unprepare(uap->clk);
    1938                 :            :         /* Optionally let pins go into sleep states */
    1939                 :          3 :         pinctrl_pm_select_sleep_state(port->dev);
    1940                 :            : 
    1941                 :          3 :         if (dev_get_platdata(uap->port.dev)) {
    1942                 :            :                 struct amba_pl011_data *plat;
    1943                 :            : 
    1944                 :            :                 plat = dev_get_platdata(uap->port.dev);
    1945                 :          0 :                 if (plat->exit)
    1946                 :          0 :                         plat->exit();
    1947                 :            :         }
    1948                 :            : 
    1949                 :          3 :         if (uap->port.ops->flush_buffer)
    1950                 :          3 :                 uap->port.ops->flush_buffer(port);
    1951                 :          3 : }
    1952                 :            : 
    1953                 :          0 : static void sbsa_uart_shutdown(struct uart_port *port)
    1954                 :            : {
    1955                 :            :         struct uart_amba_port *uap =
    1956                 :            :                 container_of(port, struct uart_amba_port, port);
    1957                 :            : 
    1958                 :          0 :         pl011_disable_interrupts(uap);
    1959                 :            : 
    1960                 :          0 :         free_irq(uap->port.irq, uap);
    1961                 :            : 
    1962                 :          0 :         if (uap->port.ops->flush_buffer)
    1963                 :          0 :                 uap->port.ops->flush_buffer(port);
    1964                 :          0 : }
    1965                 :            : 
    1966                 :            : static void
    1967                 :          3 : pl011_setup_status_masks(struct uart_port *port, struct ktermios *termios)
    1968                 :            : {
    1969                 :          3 :         port->read_status_mask = UART011_DR_OE | 255;
    1970                 :          3 :         if (termios->c_iflag & INPCK)
    1971                 :          0 :                 port->read_status_mask |= UART011_DR_FE | UART011_DR_PE;
    1972                 :          3 :         if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
    1973                 :          0 :                 port->read_status_mask |= UART011_DR_BE;
    1974                 :            : 
    1975                 :            :         /*
    1976                 :            :          * Characters to ignore
    1977                 :            :          */
    1978                 :          3 :         port->ignore_status_mask = 0;
    1979                 :          3 :         if (termios->c_iflag & IGNPAR)
    1980                 :          0 :                 port->ignore_status_mask |= UART011_DR_FE | UART011_DR_PE;
    1981                 :          3 :         if (termios->c_iflag & IGNBRK) {
    1982                 :          0 :                 port->ignore_status_mask |= UART011_DR_BE;
    1983                 :            :                 /*
    1984                 :            :                  * If we're ignoring parity and break indicators,
    1985                 :            :                  * ignore overruns too (for real raw support).
    1986                 :            :                  */
    1987                 :          0 :                 if (termios->c_iflag & IGNPAR)
    1988                 :          0 :                         port->ignore_status_mask |= UART011_DR_OE;
    1989                 :            :         }
    1990                 :            : 
    1991                 :            :         /*
    1992                 :            :          * Ignore all characters if CREAD is not set.
    1993                 :            :          */
    1994                 :          3 :         if ((termios->c_cflag & CREAD) == 0)
    1995                 :          0 :                 port->ignore_status_mask |= UART_DUMMY_DR_RX;
    1996                 :          3 : }
    1997                 :            : 
    1998                 :            : static void
    1999                 :          3 : pl011_set_termios(struct uart_port *port, struct ktermios *termios,
    2000                 :            :                      struct ktermios *old)
    2001                 :            : {
    2002                 :            :         struct uart_amba_port *uap =
    2003                 :            :             container_of(port, struct uart_amba_port, port);
    2004                 :            :         unsigned int lcr_h, old_cr;
    2005                 :            :         unsigned long flags;
    2006                 :            :         unsigned int baud, quot, clkdiv;
    2007                 :            : 
    2008                 :          3 :         if (uap->vendor->oversampling)
    2009                 :            :                 clkdiv = 8;
    2010                 :            :         else
    2011                 :            :                 clkdiv = 16;
    2012                 :            : 
    2013                 :            :         /*
    2014                 :            :          * Ask the core to calculate the divisor for us.
    2015                 :            :          */
    2016                 :          3 :         baud = uart_get_baud_rate(port, termios, old, 0,
    2017                 :          3 :                                   port->uartclk / clkdiv);
    2018                 :            : #ifdef CONFIG_DMA_ENGINE
    2019                 :            :         /*
    2020                 :            :          * Adjust RX DMA polling rate with baud rate if not specified.
    2021                 :            :          */
    2022                 :          3 :         if (uap->dmarx.auto_poll_rate)
    2023                 :          0 :                 uap->dmarx.poll_rate = DIV_ROUND_UP(10000000, baud);
    2024                 :            : #endif
    2025                 :            : 
    2026                 :          3 :         if (baud > port->uartclk/16)
    2027                 :          0 :                 quot = DIV_ROUND_CLOSEST(port->uartclk * 8, baud);
    2028                 :            :         else
    2029                 :          3 :                 quot = DIV_ROUND_CLOSEST(port->uartclk * 4, baud);
    2030                 :            : 
    2031                 :          3 :         switch (termios->c_cflag & CSIZE) {
    2032                 :            :         case CS5:
    2033                 :            :                 lcr_h = UART01x_LCRH_WLEN_5;
    2034                 :            :                 break;
    2035                 :            :         case CS6:
    2036                 :            :                 lcr_h = UART01x_LCRH_WLEN_6;
    2037                 :          0 :                 break;
    2038                 :            :         case CS7:
    2039                 :            :                 lcr_h = UART01x_LCRH_WLEN_7;
    2040                 :          0 :                 break;
    2041                 :            :         default: // CS8
    2042                 :            :                 lcr_h = UART01x_LCRH_WLEN_8;
    2043                 :          3 :                 break;
    2044                 :            :         }
    2045                 :          3 :         if (termios->c_cflag & CSTOPB)
    2046                 :          0 :                 lcr_h |= UART01x_LCRH_STP2;
    2047                 :          3 :         if (termios->c_cflag & PARENB) {
    2048                 :          0 :                 lcr_h |= UART01x_LCRH_PEN;
    2049                 :          0 :                 if (!(termios->c_cflag & PARODD))
    2050                 :          0 :                         lcr_h |= UART01x_LCRH_EPS;
    2051                 :          0 :                 if (termios->c_cflag & CMSPAR)
    2052                 :          0 :                         lcr_h |= UART011_LCRH_SPS;
    2053                 :            :         }
    2054                 :          3 :         if (uap->fifosize > 1)
    2055                 :          3 :                 lcr_h |= UART01x_LCRH_FEN;
    2056                 :            : 
    2057                 :          3 :         spin_lock_irqsave(&port->lock, flags);
    2058                 :            : 
    2059                 :            :         /*
    2060                 :            :          * Update the per-port timeout.
    2061                 :            :          */
    2062                 :          3 :         uart_update_timeout(port, termios->c_cflag, baud);
    2063                 :            : 
    2064                 :          3 :         pl011_setup_status_masks(port, termios);
    2065                 :            : 
    2066                 :          3 :         if (UART_ENABLE_MS(port, termios->c_cflag))
    2067                 :            :                 pl011_enable_ms(port);
    2068                 :            : 
    2069                 :            :         /* first, disable everything */
    2070                 :            :         old_cr = pl011_read(uap, REG_CR);
    2071                 :            :         pl011_write(0, uap, REG_CR);
    2072                 :            : 
    2073                 :          3 :         if (termios->c_cflag & CRTSCTS) {
    2074                 :          0 :                 if (old_cr & UART011_CR_RTS)
    2075                 :          0 :                         old_cr |= UART011_CR_RTSEN;
    2076                 :            : 
    2077                 :          0 :                 old_cr |= UART011_CR_CTSEN;
    2078                 :          0 :                 port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS;
    2079                 :            :         } else {
    2080                 :          3 :                 old_cr &= ~(UART011_CR_CTSEN | UART011_CR_RTSEN);
    2081                 :          3 :                 port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS);
    2082                 :            :         }
    2083                 :            : 
    2084                 :          3 :         if (uap->vendor->oversampling) {
    2085                 :          0 :                 if (baud > port->uartclk / 16)
    2086                 :          0 :                         old_cr |= ST_UART011_CR_OVSFACT;
    2087                 :            :                 else
    2088                 :          0 :                         old_cr &= ~ST_UART011_CR_OVSFACT;
    2089                 :            :         }
    2090                 :            : 
    2091                 :            :         /*
    2092                 :            :          * Workaround for the ST Micro oversampling variants to
    2093                 :            :          * increase the bitrate slightly, by lowering the divisor,
    2094                 :            :          * to avoid delayed sampling of start bit at high speeds,
    2095                 :            :          * else we see data corruption.
    2096                 :            :          */
    2097                 :          3 :         if (uap->vendor->oversampling) {
    2098                 :          0 :                 if ((baud >= 3000000) && (baud < 3250000) && (quot > 1))
    2099                 :          0 :                         quot -= 1;
    2100                 :          0 :                 else if ((baud > 3250000) && (quot > 2))
    2101                 :          0 :                         quot -= 2;
    2102                 :            :         }
    2103                 :            :         /* Set baud rate */
    2104                 :          3 :         pl011_write(quot & 0x3f, uap, REG_FBRD);
    2105                 :          3 :         pl011_write(quot >> 6, uap, REG_IBRD);
    2106                 :            : 
    2107                 :            :         /*
    2108                 :            :          * ----------v----------v----------v----------v-----
    2109                 :            :          * NOTE: REG_LCRH_TX and REG_LCRH_RX MUST BE WRITTEN AFTER
    2110                 :            :          * REG_FBRD & REG_IBRD.
    2111                 :            :          * ----------^----------^----------^----------^-----
    2112                 :            :          */
    2113                 :          3 :         pl011_write_lcr_h(uap, lcr_h);
    2114                 :            :         pl011_write(old_cr, uap, REG_CR);
    2115                 :            : 
    2116                 :            :         spin_unlock_irqrestore(&port->lock, flags);
    2117                 :          3 : }
    2118                 :            : 
    2119                 :            : static void
    2120                 :          0 : sbsa_uart_set_termios(struct uart_port *port, struct ktermios *termios,
    2121                 :            :                       struct ktermios *old)
    2122                 :            : {
    2123                 :            :         struct uart_amba_port *uap =
    2124                 :            :             container_of(port, struct uart_amba_port, port);
    2125                 :            :         unsigned long flags;
    2126                 :            : 
    2127                 :          0 :         tty_termios_encode_baud_rate(termios, uap->fixed_baud, uap->fixed_baud);
    2128                 :            : 
    2129                 :            :         /* The SBSA UART only supports 8n1 without hardware flow control. */
    2130                 :          0 :         termios->c_cflag &= ~(CSIZE | CSTOPB | PARENB | PARODD);
    2131                 :          0 :         termios->c_cflag &= ~(CMSPAR | CRTSCTS);
    2132                 :          0 :         termios->c_cflag |= CS8 | CLOCAL;
    2133                 :            : 
    2134                 :          0 :         spin_lock_irqsave(&port->lock, flags);
    2135                 :          0 :         uart_update_timeout(port, CS8, uap->fixed_baud);
    2136                 :          0 :         pl011_setup_status_masks(port, termios);
    2137                 :            :         spin_unlock_irqrestore(&port->lock, flags);
    2138                 :          0 : }
    2139                 :            : 
    2140                 :          3 : static const char *pl011_type(struct uart_port *port)
    2141                 :            : {
    2142                 :            :         struct uart_amba_port *uap =
    2143                 :            :             container_of(port, struct uart_amba_port, port);
    2144                 :          3 :         return uap->port.type == PORT_AMBA ? uap->type : NULL;
    2145                 :            : }
    2146                 :            : 
    2147                 :            : /*
    2148                 :            :  * Release the memory region(s) being used by 'port'
    2149                 :            :  */
    2150                 :          0 : static void pl011_release_port(struct uart_port *port)
    2151                 :            : {
    2152                 :          0 :         release_mem_region(port->mapbase, SZ_4K);
    2153                 :          0 : }
    2154                 :            : 
    2155                 :            : /*
    2156                 :            :  * Request the memory region(s) being used by 'port'
    2157                 :            :  */
    2158                 :          3 : static int pl011_request_port(struct uart_port *port)
    2159                 :            : {
    2160                 :          3 :         return request_mem_region(port->mapbase, SZ_4K, "uart-pl011")
    2161                 :          3 :                         != NULL ? 0 : -EBUSY;
    2162                 :            : }
    2163                 :            : 
    2164                 :            : /*
    2165                 :            :  * Configure/autoconfigure the port.
    2166                 :            :  */
    2167                 :          3 : static void pl011_config_port(struct uart_port *port, int flags)
    2168                 :            : {
    2169                 :          3 :         if (flags & UART_CONFIG_TYPE) {
    2170                 :          3 :                 port->type = PORT_AMBA;
    2171                 :          3 :                 pl011_request_port(port);
    2172                 :            :         }
    2173                 :          3 : }
    2174                 :            : 
    2175                 :            : /*
    2176                 :            :  * verify the new serial_struct (for TIOCSSERIAL).
    2177                 :            :  */
    2178                 :          0 : static int pl011_verify_port(struct uart_port *port, struct serial_struct *ser)
    2179                 :            : {
    2180                 :            :         int ret = 0;
    2181                 :          0 :         if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA)
    2182                 :            :                 ret = -EINVAL;
    2183                 :          0 :         if (ser->irq < 0 || ser->irq >= nr_irqs)
    2184                 :            :                 ret = -EINVAL;
    2185                 :          0 :         if (ser->baud_base < 9600)
    2186                 :            :                 ret = -EINVAL;
    2187                 :          0 :         return ret;
    2188                 :            : }
    2189                 :            : 
    2190                 :            : static const struct uart_ops amba_pl011_pops = {
    2191                 :            :         .tx_empty       = pl011_tx_empty,
    2192                 :            :         .set_mctrl      = pl011_set_mctrl,
    2193                 :            :         .get_mctrl      = pl011_get_mctrl,
    2194                 :            :         .stop_tx        = pl011_stop_tx,
    2195                 :            :         .start_tx       = pl011_start_tx,
    2196                 :            :         .stop_rx        = pl011_stop_rx,
    2197                 :            :         .throttle       = pl011_throttle,
    2198                 :            :         .unthrottle     = pl011_unthrottle,
    2199                 :            :         .enable_ms      = pl011_enable_ms,
    2200                 :            :         .break_ctl      = pl011_break_ctl,
    2201                 :            :         .startup        = pl011_startup,
    2202                 :            :         .shutdown       = pl011_shutdown,
    2203                 :            :         .flush_buffer   = pl011_dma_flush_buffer,
    2204                 :            :         .set_termios    = pl011_set_termios,
    2205                 :            :         .type           = pl011_type,
    2206                 :            :         .release_port   = pl011_release_port,
    2207                 :            :         .request_port   = pl011_request_port,
    2208                 :            :         .config_port    = pl011_config_port,
    2209                 :            :         .verify_port    = pl011_verify_port,
    2210                 :            : #ifdef CONFIG_CONSOLE_POLL
    2211                 :            :         .poll_init     = pl011_hwinit,
    2212                 :            :         .poll_get_char = pl011_get_poll_char,
    2213                 :            :         .poll_put_char = pl011_put_poll_char,
    2214                 :            : #endif
    2215                 :            : };
    2216                 :            : 
    2217                 :          0 : static void sbsa_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
    2218                 :            : {
    2219                 :          0 : }
    2220                 :            : 
    2221                 :          0 : static unsigned int sbsa_uart_get_mctrl(struct uart_port *port)
    2222                 :            : {
    2223                 :          0 :         return 0;
    2224                 :            : }
    2225                 :            : 
    2226                 :            : static const struct uart_ops sbsa_uart_pops = {
    2227                 :            :         .tx_empty       = pl011_tx_empty,
    2228                 :            :         .set_mctrl      = sbsa_uart_set_mctrl,
    2229                 :            :         .get_mctrl      = sbsa_uart_get_mctrl,
    2230                 :            :         .stop_tx        = pl011_stop_tx,
    2231                 :            :         .start_tx       = pl011_start_tx,
    2232                 :            :         .stop_rx        = pl011_stop_rx,
    2233                 :            :         .startup        = sbsa_uart_startup,
    2234                 :            :         .shutdown       = sbsa_uart_shutdown,
    2235                 :            :         .set_termios    = sbsa_uart_set_termios,
    2236                 :            :         .type           = pl011_type,
    2237                 :            :         .release_port   = pl011_release_port,
    2238                 :            :         .request_port   = pl011_request_port,
    2239                 :            :         .config_port    = pl011_config_port,
    2240                 :            :         .verify_port    = pl011_verify_port,
    2241                 :            : #ifdef CONFIG_CONSOLE_POLL
    2242                 :            :         .poll_init     = pl011_hwinit,
    2243                 :            :         .poll_get_char = pl011_get_poll_char,
    2244                 :            :         .poll_put_char = pl011_put_poll_char,
    2245                 :            : #endif
    2246                 :            : };
    2247                 :            : 
    2248                 :            : static struct uart_amba_port *amba_ports[UART_NR];
    2249                 :            : 
    2250                 :            : #ifdef CONFIG_SERIAL_AMBA_PL011_CONSOLE
    2251                 :            : 
    2252                 :          1 : static void pl011_console_putchar(struct uart_port *port, int ch)
    2253                 :            : {
    2254                 :            :         struct uart_amba_port *uap =
    2255                 :            :             container_of(port, struct uart_amba_port, port);
    2256                 :            : 
    2257                 :          1 :         while (pl011_read(uap, REG_FR) & UART01x_FR_TXFF)
    2258                 :          0 :                 cpu_relax();
    2259                 :          1 :         pl011_write(ch, uap, REG_DR);
    2260                 :          1 : }
    2261                 :            : 
    2262                 :            : static void
    2263                 :          1 : pl011_console_write(struct console *co, const char *s, unsigned int count)
    2264                 :            : {
    2265                 :          1 :         struct uart_amba_port *uap = amba_ports[co->index];
    2266                 :            :         unsigned int old_cr = 0, new_cr;
    2267                 :            :         unsigned long flags;
    2268                 :            :         int locked = 1;
    2269                 :            : 
    2270                 :          1 :         clk_enable(uap->clk);
    2271                 :            : 
    2272                 :          1 :         local_irq_save(flags);
    2273                 :          1 :         if (uap->port.sysrq)
    2274                 :            :                 locked = 0;
    2275                 :          1 :         else if (oops_in_progress)
    2276                 :            :                 locked = spin_trylock(&uap->port.lock);
    2277                 :            :         else
    2278                 :            :                 spin_lock(&uap->port.lock);
    2279                 :            : 
    2280                 :            :         /*
    2281                 :            :          *      First save the CR then disable the interrupts
    2282                 :            :          */
    2283                 :          1 :         if (!uap->vendor->always_enabled) {
    2284                 :            :                 old_cr = pl011_read(uap, REG_CR);
    2285                 :          1 :                 new_cr = old_cr & ~UART011_CR_CTSEN;
    2286                 :          1 :                 new_cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
    2287                 :            :                 pl011_write(new_cr, uap, REG_CR);
    2288                 :            :         }
    2289                 :            : 
    2290                 :          1 :         uart_console_write(&uap->port, s, count, pl011_console_putchar);
    2291                 :            : 
    2292                 :            :         /*
    2293                 :            :          *      Finally, wait for transmitter to become empty and restore the
    2294                 :            :          *      TCR. Allow feature register bits to be inverted to work around
    2295                 :            :          *      errata.
    2296                 :            :          */
    2297                 :          1 :         while ((pl011_read(uap, REG_FR) ^ uap->vendor->inv_fr)
    2298                 :          1 :                                                 & uap->vendor->fr_busy)
    2299                 :          0 :                 cpu_relax();
    2300                 :          1 :         if (!uap->vendor->always_enabled)
    2301                 :            :                 pl011_write(old_cr, uap, REG_CR);
    2302                 :            : 
    2303                 :          1 :         if (locked)
    2304                 :            :                 spin_unlock(&uap->port.lock);
    2305                 :          1 :         local_irq_restore(flags);
    2306                 :            : 
    2307                 :          1 :         clk_disable(uap->clk);
    2308                 :          1 : }
    2309                 :            : 
    2310                 :            : static void __init
    2311                 :          0 : pl011_console_get_options(struct uart_amba_port *uap, int *baud,
    2312                 :            :                              int *parity, int *bits)
    2313                 :            : {
    2314                 :          0 :         if (pl011_read(uap, REG_CR) & UART01x_CR_UARTEN) {
    2315                 :            :                 unsigned int lcr_h, ibrd, fbrd;
    2316                 :            : 
    2317                 :            :                 lcr_h = pl011_read(uap, REG_LCRH_TX);
    2318                 :            : 
    2319                 :          0 :                 *parity = 'n';
    2320                 :          0 :                 if (lcr_h & UART01x_LCRH_PEN) {
    2321                 :          0 :                         if (lcr_h & UART01x_LCRH_EPS)
    2322                 :          0 :                                 *parity = 'e';
    2323                 :            :                         else
    2324                 :          0 :                                 *parity = 'o';
    2325                 :            :                 }
    2326                 :            : 
    2327                 :          0 :                 if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7)
    2328                 :          0 :                         *bits = 7;
    2329                 :            :                 else
    2330                 :          0 :                         *bits = 8;
    2331                 :            : 
    2332                 :            :                 ibrd = pl011_read(uap, REG_IBRD);
    2333                 :            :                 fbrd = pl011_read(uap, REG_FBRD);
    2334                 :            : 
    2335                 :          0 :                 *baud = uap->port.uartclk * 4 / (64 * ibrd + fbrd);
    2336                 :            : 
    2337                 :          0 :                 if (uap->vendor->oversampling) {
    2338                 :          0 :                         if (pl011_read(uap, REG_CR)
    2339                 :          0 :                                   & ST_UART011_CR_OVSFACT)
    2340                 :          0 :                                 *baud *= 2;
    2341                 :            :                 }
    2342                 :            :         }
    2343                 :          0 : }
    2344                 :            : 
    2345                 :          3 : static int __init pl011_console_setup(struct console *co, char *options)
    2346                 :            : {
    2347                 :            :         struct uart_amba_port *uap;
    2348                 :          3 :         int baud = 38400;
    2349                 :          3 :         int bits = 8;
    2350                 :          3 :         int parity = 'n';
    2351                 :          3 :         int flow = 'n';
    2352                 :            :         int ret;
    2353                 :            : 
    2354                 :            :         /*
    2355                 :            :          * Check whether an invalid uart number has been specified, and
    2356                 :            :          * if so, search for the first available port that does have
    2357                 :            :          * console support.
    2358                 :            :          */
    2359                 :          3 :         if (co->index >= UART_NR)
    2360                 :          0 :                 co->index = 0;
    2361                 :          3 :         uap = amba_ports[co->index];
    2362                 :          3 :         if (!uap)
    2363                 :            :                 return -ENODEV;
    2364                 :            : 
    2365                 :            :         /* Allow pins to be muxed in and configured */
    2366                 :          3 :         pinctrl_pm_select_default_state(uap->port.dev);
    2367                 :            : 
    2368                 :          3 :         ret = clk_prepare(uap->clk);
    2369                 :          3 :         if (ret)
    2370                 :            :                 return ret;
    2371                 :            : 
    2372                 :          3 :         if (dev_get_platdata(uap->port.dev)) {
    2373                 :            :                 struct amba_pl011_data *plat;
    2374                 :            : 
    2375                 :            :                 plat = dev_get_platdata(uap->port.dev);
    2376                 :          0 :                 if (plat->init)
    2377                 :          0 :                         plat->init();
    2378                 :            :         }
    2379                 :            : 
    2380                 :          3 :         uap->port.uartclk = pl011_clk_round(clk_get_rate(uap->clk));
    2381                 :            : 
    2382                 :          3 :         if (uap->vendor->fixed_options) {
    2383                 :          0 :                 baud = uap->fixed_baud;
    2384                 :            :         } else {
    2385                 :          3 :                 if (options)
    2386                 :          3 :                         uart_parse_options(options,
    2387                 :            :                                            &baud, &parity, &bits, &flow);
    2388                 :            :                 else
    2389                 :          0 :                         pl011_console_get_options(uap, &baud, &parity, &bits);
    2390                 :            :         }
    2391                 :            : 
    2392                 :          3 :         return uart_set_options(&uap->port, co, baud, parity, bits, flow);
    2393                 :            : }
    2394                 :            : 
    2395                 :            : /**
    2396                 :            :  *      pl011_console_match - non-standard console matching
    2397                 :            :  *      @co:      registering console
    2398                 :            :  *      @name:    name from console command line
    2399                 :            :  *      @idx:     index from console command line
    2400                 :            :  *      @options: ptr to option string from console command line
    2401                 :            :  *
    2402                 :            :  *      Only attempts to match console command lines of the form:
    2403                 :            :  *          console=pl011,mmio|mmio32,<addr>[,<options>]
    2404                 :            :  *          console=pl011,0x<addr>[,<options>]
    2405                 :            :  *      This form is used to register an initial earlycon boot console and
    2406                 :            :  *      replace it with the amba_console at pl011 driver init.
    2407                 :            :  *
    2408                 :            :  *      Performs console setup for a match (as required by interface)
    2409                 :            :  *      If no <options> are specified, then assume the h/w is already setup.
    2410                 :            :  *
    2411                 :            :  *      Returns 0 if console matches; otherwise non-zero to use default matching
    2412                 :            :  */
    2413                 :          3 : static int __init pl011_console_match(struct console *co, char *name, int idx,
    2414                 :            :                                       char *options)
    2415                 :            : {
    2416                 :            :         unsigned char iotype;
    2417                 :            :         resource_size_t addr;
    2418                 :            :         int i;
    2419                 :            : 
    2420                 :            :         /*
    2421                 :            :          * Systems affected by the Qualcomm Technologies QDF2400 E44 erratum
    2422                 :            :          * have a distinct console name, so make sure we check for that.
    2423                 :            :          * The actual implementation of the erratum occurs in the probe
    2424                 :            :          * function.
    2425                 :            :          */
    2426                 :          3 :         if ((strcmp(name, "qdf2400_e44") != 0) && (strcmp(name, "pl011") != 0))
    2427                 :            :                 return -ENODEV;
    2428                 :            : 
    2429                 :          0 :         if (uart_parse_earlycon(options, &iotype, &addr, &options))
    2430                 :            :                 return -ENODEV;
    2431                 :            : 
    2432                 :          0 :         if (iotype != UPIO_MEM && iotype != UPIO_MEM32)
    2433                 :            :                 return -ENODEV;
    2434                 :            : 
    2435                 :            :         /* try to match the port specified on the command line */
    2436                 :          0 :         for (i = 0; i < ARRAY_SIZE(amba_ports); i++) {
    2437                 :            :                 struct uart_port *port;
    2438                 :            : 
    2439                 :          0 :                 if (!amba_ports[i])
    2440                 :          0 :                         continue;
    2441                 :            : 
    2442                 :            :                 port = &amba_ports[i]->port;
    2443                 :            : 
    2444                 :          0 :                 if (port->mapbase != addr)
    2445                 :          0 :                         continue;
    2446                 :            : 
    2447                 :          0 :                 co->index = i;
    2448                 :          0 :                 port->cons = co;
    2449                 :          0 :                 return pl011_console_setup(co, options);
    2450                 :            :         }
    2451                 :            : 
    2452                 :            :         return -ENODEV;
    2453                 :            : }
    2454                 :            : 
    2455                 :            : static struct uart_driver amba_reg;
    2456                 :            : static struct console amba_console = {
    2457                 :            :         .name           = "ttyAMA",
    2458                 :            :         .write          = pl011_console_write,
    2459                 :            :         .device         = uart_console_device,
    2460                 :            :         .setup          = pl011_console_setup,
    2461                 :            :         .match          = pl011_console_match,
    2462                 :            :         .flags          = CON_PRINTBUFFER | CON_ANYTIME,
    2463                 :            :         .index          = -1,
    2464                 :            :         .data           = &amba_reg,
    2465                 :            : };
    2466                 :            : 
    2467                 :            : #define AMBA_CONSOLE    (&amba_console)
    2468                 :            : 
    2469                 :          0 : static void qdf2400_e44_putc(struct uart_port *port, int c)
    2470                 :            : {
    2471                 :          0 :         while (readl(port->membase + UART01x_FR) & UART01x_FR_TXFF)
    2472                 :          0 :                 cpu_relax();
    2473                 :          0 :         writel(c, port->membase + UART01x_DR);
    2474                 :          0 :         while (!(readl(port->membase + UART01x_FR) & UART011_FR_TXFE))
    2475                 :          0 :                 cpu_relax();
    2476                 :          0 : }
    2477                 :            : 
    2478                 :          0 : static void qdf2400_e44_early_write(struct console *con, const char *s, unsigned n)
    2479                 :            : {
    2480                 :          0 :         struct earlycon_device *dev = con->data;
    2481                 :            : 
    2482                 :          0 :         uart_console_write(&dev->port, s, n, qdf2400_e44_putc);
    2483                 :          0 : }
    2484                 :            : 
    2485                 :          0 : static void pl011_putc(struct uart_port *port, int c)
    2486                 :            : {
    2487                 :          0 :         while (readl(port->membase + UART01x_FR) & UART01x_FR_TXFF)
    2488                 :          0 :                 cpu_relax();
    2489                 :          0 :         if (port->iotype == UPIO_MEM32)
    2490                 :          0 :                 writel(c, port->membase + UART01x_DR);
    2491                 :            :         else
    2492                 :          0 :                 writeb(c, port->membase + UART01x_DR);
    2493                 :          0 :         while (readl(port->membase + UART01x_FR) & UART01x_FR_BUSY)
    2494                 :          0 :                 cpu_relax();
    2495                 :          0 : }
    2496                 :            : 
    2497                 :          0 : static void pl011_early_write(struct console *con, const char *s, unsigned n)
    2498                 :            : {
    2499                 :          0 :         struct earlycon_device *dev = con->data;
    2500                 :            : 
    2501                 :          0 :         uart_console_write(&dev->port, s, n, pl011_putc);
    2502                 :          0 : }
    2503                 :            : 
    2504                 :            : /*
    2505                 :            :  * On non-ACPI systems, earlycon is enabled by specifying
    2506                 :            :  * "earlycon=pl011,<address>" on the kernel command line.
    2507                 :            :  *
    2508                 :            :  * On ACPI ARM64 systems, an "early" console is enabled via the SPCR table,
    2509                 :            :  * by specifying only "earlycon" on the command line.  Because it requires
    2510                 :            :  * SPCR, the console starts after ACPI is parsed, which is later than a
    2511                 :            :  * traditional early console.
    2512                 :            :  *
    2513                 :            :  * To get the traditional early console that starts before ACPI is parsed,
    2514                 :            :  * specify the full "earlycon=pl011,<address>" option.
    2515                 :            :  */
    2516                 :          0 : static int __init pl011_early_console_setup(struct earlycon_device *device,
    2517                 :            :                                             const char *opt)
    2518                 :            : {
    2519                 :          0 :         if (!device->port.membase)
    2520                 :            :                 return -ENODEV;
    2521                 :            : 
    2522                 :          0 :         device->con->write = pl011_early_write;
    2523                 :            : 
    2524                 :          0 :         return 0;
    2525                 :            : }
    2526                 :            : OF_EARLYCON_DECLARE(pl011, "arm,pl011", pl011_early_console_setup);
    2527                 :            : OF_EARLYCON_DECLARE(pl011, "arm,sbsa-uart", pl011_early_console_setup);
    2528                 :            : 
    2529                 :            : /*
    2530                 :            :  * On Qualcomm Datacenter Technologies QDF2400 SOCs affected by
    2531                 :            :  * Erratum 44, traditional earlycon can be enabled by specifying
    2532                 :            :  * "earlycon=qdf2400_e44,<address>".  Any options are ignored.
    2533                 :            :  *
    2534                 :            :  * Alternatively, you can just specify "earlycon", and the early console
    2535                 :            :  * will be enabled with the information from the SPCR table.  In this
    2536                 :            :  * case, the SPCR code will detect the need for the E44 work-around,
    2537                 :            :  * and set the console name to "qdf2400_e44".
    2538                 :            :  */
    2539                 :            : static int __init
    2540                 :          0 : qdf2400_e44_early_console_setup(struct earlycon_device *device,
    2541                 :            :                                 const char *opt)
    2542                 :            : {
    2543                 :          0 :         if (!device->port.membase)
    2544                 :            :                 return -ENODEV;
    2545                 :            : 
    2546                 :          0 :         device->con->write = qdf2400_e44_early_write;
    2547                 :          0 :         return 0;
    2548                 :            : }
    2549                 :            : EARLYCON_DECLARE(qdf2400_e44, qdf2400_e44_early_console_setup);
    2550                 :            : 
    2551                 :            : #else
    2552                 :            : #define AMBA_CONSOLE    NULL
    2553                 :            : #endif
    2554                 :            : 
    2555                 :            : static struct uart_driver amba_reg = {
    2556                 :            :         .owner                  = THIS_MODULE,
    2557                 :            :         .driver_name            = "ttyAMA",
    2558                 :            :         .dev_name               = "ttyAMA",
    2559                 :            :         .major                  = SERIAL_AMBA_MAJOR,
    2560                 :            :         .minor                  = SERIAL_AMBA_MINOR,
    2561                 :            :         .nr                     = UART_NR,
    2562                 :            :         .cons                   = AMBA_CONSOLE,
    2563                 :            : };
    2564                 :            : 
    2565                 :            : #if 0
    2566                 :            : static int pl011_probe_dt_alias(int index, struct device *dev)
    2567                 :            : {
    2568                 :            :         struct device_node *np;
    2569                 :            :         static bool seen_dev_with_alias = false;
    2570                 :            :         static bool seen_dev_without_alias = false;
    2571                 :            :         int ret = index;
    2572                 :            : 
    2573                 :            :         if (!IS_ENABLED(CONFIG_OF))
    2574                 :            :                 return ret;
    2575                 :            : 
    2576                 :            :         np = dev->of_node;
    2577                 :            :         if (!np)
    2578                 :            :                 return ret;
    2579                 :            : 
    2580                 :            :         ret = of_alias_get_id(np, "serial");
    2581                 :            :         if (ret < 0) {
    2582                 :            :                 seen_dev_without_alias = true;
    2583                 :            :                 ret = index;
    2584                 :            :         } else {
    2585                 :            :                 seen_dev_with_alias = true;
    2586                 :            :                 if (ret >= ARRAY_SIZE(amba_ports) || amba_ports[ret] != NULL) {
    2587                 :            :                         dev_warn(dev, "requested serial port %d  not available.\n", ret);
    2588                 :            :                         ret = index;
    2589                 :            :                 }
    2590                 :            :         }
    2591                 :            : 
    2592                 :            :         if (seen_dev_with_alias && seen_dev_without_alias)
    2593                 :            :                 dev_warn(dev, "aliased and non-aliased serial devices found in device tree. Serial port enumeration may be unpredictable.\n");
    2594                 :            : 
    2595                 :            :         return ret;
    2596                 :            : }
    2597                 :            : #endif
    2598                 :            : 
    2599                 :            : /* unregisters the driver also if no more ports are left */
    2600                 :          0 : static void pl011_unregister_port(struct uart_amba_port *uap)
    2601                 :            : {
    2602                 :            :         int i;
    2603                 :            :         bool busy = false;
    2604                 :            : 
    2605                 :          0 :         for (i = 0; i < ARRAY_SIZE(amba_ports); i++) {
    2606                 :          0 :                 if (amba_ports[i] == uap)
    2607                 :          0 :                         amba_ports[i] = NULL;
    2608                 :          0 :                 else if (amba_ports[i])
    2609                 :            :                         busy = true;
    2610                 :            :         }
    2611                 :          0 :         pl011_dma_remove(uap);
    2612                 :          0 :         if (!busy)
    2613                 :          0 :                 uart_unregister_driver(&amba_reg);
    2614                 :          0 : }
    2615                 :            : 
    2616                 :            : static int pl011_find_free_port(void)
    2617                 :            : {
    2618                 :            :         int i;
    2619                 :            : 
    2620                 :          0 :         for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
    2621                 :          3 :                 if (amba_ports[i] == NULL)
    2622                 :          3 :                         return i;
    2623                 :            : 
    2624                 :            :         return -EBUSY;
    2625                 :            : }
    2626                 :            : 
    2627                 :          3 : static int pl011_setup_port(struct device *dev, struct uart_amba_port *uap,
    2628                 :            :                             struct resource *mmiobase, int index)
    2629                 :            : {
    2630                 :            :         void __iomem *base;
    2631                 :            : 
    2632                 :          3 :         base = devm_ioremap_resource(dev, mmiobase);
    2633                 :          3 :         if (IS_ERR(base))
    2634                 :          0 :                 return PTR_ERR(base);
    2635                 :            : 
    2636                 :            :         /* Don't use DT serial<n> aliases - it causes the device to
    2637                 :            :            be renumbered to ttyAMA1 if it is the second serial port in the
    2638                 :            :            system, even though the other one is ttyS0. The 8250 driver
    2639                 :            :            doesn't use this logic, so always remains ttyS0.
    2640                 :            :         index = pl011_probe_dt_alias(index, dev);
    2641                 :            :         */
    2642                 :            : 
    2643                 :          3 :         uap->old_cr = 0;
    2644                 :          3 :         uap->port.dev = dev;
    2645                 :          3 :         uap->port.mapbase = mmiobase->start;
    2646                 :          3 :         uap->port.membase = base;
    2647                 :          3 :         uap->port.fifosize = uap->fifosize;
    2648                 :          3 :         uap->port.flags = UPF_BOOT_AUTOCONF;
    2649                 :          3 :         uap->port.line = index;
    2650                 :          3 :         spin_lock_init(&uap->port.lock);
    2651                 :            : 
    2652                 :          3 :         amba_ports[index] = uap;
    2653                 :            : 
    2654                 :          3 :         return 0;
    2655                 :            : }
    2656                 :            : 
    2657                 :          3 : static int pl011_register_port(struct uart_amba_port *uap)
    2658                 :            : {
    2659                 :            :         int ret;
    2660                 :            : 
    2661                 :            :         /* Ensure interrupts from this UART are masked and cleared */
    2662                 :            :         pl011_write(0, uap, REG_IMSC);
    2663                 :            :         pl011_write(0xffff, uap, REG_ICR);
    2664                 :            : 
    2665                 :          3 :         if (!amba_reg.state) {
    2666                 :          3 :                 ret = uart_register_driver(&amba_reg);
    2667                 :          3 :                 if (ret < 0) {
    2668                 :          0 :                         dev_err(uap->port.dev,
    2669                 :            :                                 "Failed to register AMBA-PL011 driver\n");
    2670                 :          0 :                         return ret;
    2671                 :            :                 }
    2672                 :            :         }
    2673                 :            : 
    2674                 :          3 :         ret = uart_add_one_port(&amba_reg, &uap->port);
    2675                 :          3 :         if (ret)
    2676                 :          0 :                 pl011_unregister_port(uap);
    2677                 :            : 
    2678                 :          3 :         return ret;
    2679                 :            : }
    2680                 :            : 
    2681                 :          3 : static int pl011_probe(struct amba_device *dev, const struct amba_id *id)
    2682                 :            : {
    2683                 :            :         struct uart_amba_port *uap;
    2684                 :          3 :         struct vendor_data *vendor = id->data;
    2685                 :            :         int portnr, ret;
    2686                 :            : 
    2687                 :            :         portnr = pl011_find_free_port();
    2688                 :          3 :         if (portnr < 0)
    2689                 :            :                 return portnr;
    2690                 :            : 
    2691                 :          3 :         uap = devm_kzalloc(&dev->dev, sizeof(struct uart_amba_port),
    2692                 :            :                            GFP_KERNEL);
    2693                 :          3 :         if (!uap)
    2694                 :            :                 return -ENOMEM;
    2695                 :            : 
    2696                 :          3 :         uap->clk = devm_clk_get(&dev->dev, NULL);
    2697                 :          3 :         if (IS_ERR(uap->clk))
    2698                 :          0 :                 return PTR_ERR(uap->clk);
    2699                 :            : 
    2700                 :          3 :         if (of_property_read_bool(dev->dev.of_node, "cts-event-workaround")) {
    2701                 :          3 :             vendor->cts_event_workaround = true;
    2702                 :          3 :             dev_info(&dev->dev, "cts_event_workaround enabled\n");
    2703                 :            :         }
    2704                 :            : 
    2705                 :          3 :         uap->reg_offset = vendor->reg_offset;
    2706                 :          3 :         uap->vendor = vendor;
    2707                 :          3 :         uap->fifosize = vendor->get_fifosize(dev);
    2708                 :          3 :         uap->port.iotype = vendor->access_32b ? UPIO_MEM32 : UPIO_MEM;
    2709                 :          3 :         uap->port.irq = dev->irq[0];
    2710                 :          3 :         uap->port.ops = &amba_pl011_pops;
    2711                 :            : 
    2712                 :          3 :         snprintf(uap->type, sizeof(uap->type), "PL011 rev%u", amba_rev(dev));
    2713                 :            : 
    2714                 :          3 :         ret = pl011_setup_port(&dev->dev, uap, &dev->res, portnr);
    2715                 :          3 :         if (ret)
    2716                 :            :                 return ret;
    2717                 :            : 
    2718                 :            :         amba_set_drvdata(dev, uap);
    2719                 :            : 
    2720                 :          3 :         return pl011_register_port(uap);
    2721                 :            : }
    2722                 :            : 
    2723                 :          0 : static int pl011_remove(struct amba_device *dev)
    2724                 :            : {
    2725                 :            :         struct uart_amba_port *uap = amba_get_drvdata(dev);
    2726                 :            : 
    2727                 :          0 :         uart_remove_one_port(&amba_reg, &uap->port);
    2728                 :          0 :         pl011_unregister_port(uap);
    2729                 :          0 :         return 0;
    2730                 :            : }
    2731                 :            : 
    2732                 :            : #ifdef CONFIG_PM_SLEEP
    2733                 :            : static int pl011_suspend(struct device *dev)
    2734                 :            : {
    2735                 :            :         struct uart_amba_port *uap = dev_get_drvdata(dev);
    2736                 :            : 
    2737                 :            :         if (!uap)
    2738                 :            :                 return -EINVAL;
    2739                 :            : 
    2740                 :            :         return uart_suspend_port(&amba_reg, &uap->port);
    2741                 :            : }
    2742                 :            : 
    2743                 :            : static int pl011_resume(struct device *dev)
    2744                 :            : {
    2745                 :            :         struct uart_amba_port *uap = dev_get_drvdata(dev);
    2746                 :            : 
    2747                 :            :         if (!uap)
    2748                 :            :                 return -EINVAL;
    2749                 :            : 
    2750                 :            :         return uart_resume_port(&amba_reg, &uap->port);
    2751                 :            : }
    2752                 :            : #endif
    2753                 :            : 
    2754                 :            : static SIMPLE_DEV_PM_OPS(pl011_dev_pm_ops, pl011_suspend, pl011_resume);
    2755                 :            : 
    2756                 :          0 : static int sbsa_uart_probe(struct platform_device *pdev)
    2757                 :            : {
    2758                 :            :         struct uart_amba_port *uap;
    2759                 :            :         struct resource *r;
    2760                 :            :         int portnr, ret;
    2761                 :            :         int baudrate;
    2762                 :            : 
    2763                 :            :         /*
    2764                 :            :          * Check the mandatory baud rate parameter in the DT node early
    2765                 :            :          * so that we can easily exit with the error.
    2766                 :            :          */
    2767                 :          0 :         if (pdev->dev.of_node) {
    2768                 :            :                 struct device_node *np = pdev->dev.of_node;
    2769                 :            : 
    2770                 :            :                 ret = of_property_read_u32(np, "current-speed", &baudrate);
    2771                 :          0 :                 if (ret)
    2772                 :            :                         return ret;
    2773                 :            :         } else {
    2774                 :          0 :                 baudrate = 115200;
    2775                 :            :         }
    2776                 :            : 
    2777                 :            :         portnr = pl011_find_free_port();
    2778                 :          0 :         if (portnr < 0)
    2779                 :            :                 return portnr;
    2780                 :            : 
    2781                 :          0 :         uap = devm_kzalloc(&pdev->dev, sizeof(struct uart_amba_port),
    2782                 :            :                            GFP_KERNEL);
    2783                 :          0 :         if (!uap)
    2784                 :            :                 return -ENOMEM;
    2785                 :            : 
    2786                 :          0 :         ret = platform_get_irq(pdev, 0);
    2787                 :          0 :         if (ret < 0)
    2788                 :            :                 return ret;
    2789                 :          0 :         uap->port.irq        = ret;
    2790                 :            : 
    2791                 :            : #ifdef CONFIG_ACPI_SPCR_TABLE
    2792                 :            :         if (qdf2400_e44_present) {
    2793                 :            :                 dev_info(&pdev->dev, "working around QDF2400 SoC erratum 44\n");
    2794                 :            :                 uap->vendor = &vendor_qdt_qdf2400_e44;
    2795                 :            :         } else
    2796                 :            : #endif
    2797                 :          0 :                 uap->vendor = &vendor_sbsa;
    2798                 :            : 
    2799                 :          0 :         uap->reg_offset      = uap->vendor->reg_offset;
    2800                 :          0 :         uap->fifosize        = 32;
    2801                 :          0 :         uap->port.iotype = uap->vendor->access_32b ? UPIO_MEM32 : UPIO_MEM;
    2802                 :          0 :         uap->port.ops        = &sbsa_uart_pops;
    2803                 :          0 :         uap->fixed_baud = baudrate;
    2804                 :            : 
    2805                 :          0 :         snprintf(uap->type, sizeof(uap->type), "SBSA");
    2806                 :            : 
    2807                 :          0 :         r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
    2808                 :            : 
    2809                 :          0 :         ret = pl011_setup_port(&pdev->dev, uap, r, portnr);
    2810                 :          0 :         if (ret)
    2811                 :            :                 return ret;
    2812                 :            : 
    2813                 :            :         platform_set_drvdata(pdev, uap);
    2814                 :            : 
    2815                 :          0 :         return pl011_register_port(uap);
    2816                 :            : }
    2817                 :            : 
    2818                 :          0 : static int sbsa_uart_remove(struct platform_device *pdev)
    2819                 :            : {
    2820                 :            :         struct uart_amba_port *uap = platform_get_drvdata(pdev);
    2821                 :            : 
    2822                 :          0 :         uart_remove_one_port(&amba_reg, &uap->port);
    2823                 :          0 :         pl011_unregister_port(uap);
    2824                 :          0 :         return 0;
    2825                 :            : }
    2826                 :            : 
    2827                 :            : static const struct of_device_id sbsa_uart_of_match[] = {
    2828                 :            :         { .compatible = "arm,sbsa-uart", },
    2829                 :            :         {},
    2830                 :            : };
    2831                 :            : MODULE_DEVICE_TABLE(of, sbsa_uart_of_match);
    2832                 :            : 
    2833                 :            : static const struct acpi_device_id sbsa_uart_acpi_match[] = {
    2834                 :            :         { "ARMH0011", 0 },
    2835                 :            :         {},
    2836                 :            : };
    2837                 :            : MODULE_DEVICE_TABLE(acpi, sbsa_uart_acpi_match);
    2838                 :            : 
    2839                 :            : static struct platform_driver arm_sbsa_uart_platform_driver = {
    2840                 :            :         .probe          = sbsa_uart_probe,
    2841                 :            :         .remove         = sbsa_uart_remove,
    2842                 :            :         .driver = {
    2843                 :            :                 .name   = "sbsa-uart",
    2844                 :            :                 .of_match_table = of_match_ptr(sbsa_uart_of_match),
    2845                 :            :                 .acpi_match_table = ACPI_PTR(sbsa_uart_acpi_match),
    2846                 :            :                 .suppress_bind_attrs = IS_BUILTIN(CONFIG_SERIAL_AMBA_PL011),
    2847                 :            :         },
    2848                 :            : };
    2849                 :            : 
    2850                 :            : static const struct amba_id pl011_ids[] = {
    2851                 :            :         {
    2852                 :            :                 .id     = 0x00041011,
    2853                 :            :                 .mask   = 0x000fffff,
    2854                 :            :                 .data   = &vendor_arm,
    2855                 :            :         },
    2856                 :            :         {
    2857                 :            :                 .id     = 0x00380802,
    2858                 :            :                 .mask   = 0x00ffffff,
    2859                 :            :                 .data   = &vendor_st,
    2860                 :            :         },
    2861                 :            :         {
    2862                 :            :                 .id     = AMBA_LINUX_ID(0x00, 0x1, 0xffe),
    2863                 :            :                 .mask   = 0x00ffffff,
    2864                 :            :                 .data   = &vendor_zte,
    2865                 :            :         },
    2866                 :            :         { 0, 0 },
    2867                 :            : };
    2868                 :            : 
    2869                 :            : MODULE_DEVICE_TABLE(amba, pl011_ids);
    2870                 :            : 
    2871                 :            : static struct amba_driver pl011_driver = {
    2872                 :            :         .drv = {
    2873                 :            :                 .name   = "uart-pl011",
    2874                 :            :                 .pm     = &pl011_dev_pm_ops,
    2875                 :            :                 .suppress_bind_attrs = IS_BUILTIN(CONFIG_SERIAL_AMBA_PL011),
    2876                 :            :         },
    2877                 :            :         .id_table       = pl011_ids,
    2878                 :            :         .probe          = pl011_probe,
    2879                 :            :         .remove         = pl011_remove,
    2880                 :            : };
    2881                 :            : 
    2882                 :          3 : static int __init pl011_init(void)
    2883                 :            : {
    2884                 :          3 :         printk(KERN_INFO "Serial: AMBA PL011 UART driver\n");
    2885                 :            : 
    2886                 :          3 :         if (platform_driver_register(&arm_sbsa_uart_platform_driver))
    2887                 :          0 :                 pr_warn("could not register SBSA UART platform driver\n");
    2888                 :          3 :         return amba_driver_register(&pl011_driver);
    2889                 :            : }
    2890                 :            : 
    2891                 :          0 : static void __exit pl011_exit(void)
    2892                 :            : {
    2893                 :          0 :         platform_driver_unregister(&arm_sbsa_uart_platform_driver);
    2894                 :          0 :         amba_driver_unregister(&pl011_driver);
    2895                 :          0 : }
    2896                 :            : 
    2897                 :            : /*
    2898                 :            :  * While this can be a module, if builtin it's most likely the console
    2899                 :            :  * So let's leave module_exit but move module_init to an earlier place
    2900                 :            :  */
    2901                 :            : arch_initcall(pl011_init);
    2902                 :            : module_exit(pl011_exit);
    2903                 :            : 
    2904                 :            : MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
    2905                 :            : MODULE_DESCRIPTION("ARM AMBA serial port driver");
    2906                 :            : MODULE_LICENSE("GPL");
    

Generated by: LCOV version 1.14