Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Freescale QUICC Engine UART device driver
0004  *
0005  * Author: Timur Tabi <timur@freescale.com>
0006  *
0007  * Copyright 2007 Freescale Semiconductor, Inc.
0008  *
0009  * This driver adds support for UART devices via Freescale's QUICC Engine
0010  * found on some Freescale SOCs.
0011  *
0012  * If Soft-UART support is needed but not already present, then this driver
0013  * will request and upload the "Soft-UART" microcode upon probe.  The
0014  * filename of the microcode should be fsl_qe_ucode_uart_X_YZ.bin, where "X"
0015  * is the name of the SOC (e.g. 8323), and YZ is the revision of the SOC,
0016  * (e.g. "11" for 1.1).
0017  */
0018 
0019 #include <linux/module.h>
0020 #include <linux/serial.h>
0021 #include <linux/serial_core.h>
0022 #include <linux/slab.h>
0023 #include <linux/tty.h>
0024 #include <linux/tty_flip.h>
0025 #include <linux/io.h>
0026 #include <linux/of_address.h>
0027 #include <linux/of_irq.h>
0028 #include <linux/of_platform.h>
0029 #include <linux/dma-mapping.h>
0030 
0031 #include <linux/fs_uart_pd.h>
0032 #include <soc/fsl/qe/ucc_slow.h>
0033 
0034 #include <linux/firmware.h>
0035 #include <soc/fsl/cpm.h>
0036 
0037 #ifdef CONFIG_PPC32
0038 #include <asm/reg.h> /* mfspr, SPRN_SVR */
0039 #endif
0040 
0041 /*
0042  * The GUMR flag for Soft UART.  This would normally be defined in qe.h,
0043  * but Soft-UART is a hack and we want to keep everything related to it in
0044  * this file.
0045  */
0046 #define UCC_SLOW_GUMR_H_SUART       0x00004000      /* Soft-UART */
0047 
0048 /*
0049  * soft_uart is 1 if we need to use Soft-UART mode
0050  */
0051 static int soft_uart;
0052 /*
0053  * firmware_loaded is 1 if the firmware has been loaded, 0 otherwise.
0054  */
0055 static int firmware_loaded;
0056 
0057 /* Enable this macro to configure all serial ports in internal loopback
0058    mode */
0059 /* #define LOOPBACK */
0060 
0061 /* The major and minor device numbers are defined in
0062  * http://www.lanana.org/docs/device-list/devices-2.6+.txt.  For the QE
0063  * UART, we have major number 204 and minor numbers 46 - 49, which are the
0064  * same as for the CPM2.  This decision was made because no Freescale part
0065  * has both a CPM and a QE.
0066  */
0067 #define SERIAL_QE_MAJOR 204
0068 #define SERIAL_QE_MINOR 46
0069 
0070 /* Since we only have minor numbers 46 - 49, there is a hard limit of 4 ports */
0071 #define UCC_MAX_UART    4
0072 
0073 /* The number of buffer descriptors for receiving characters. */
0074 #define RX_NUM_FIFO     4
0075 
0076 /* The number of buffer descriptors for transmitting characters. */
0077 #define TX_NUM_FIFO     4
0078 
0079 /* The maximum size of the character buffer for a single RX BD. */
0080 #define RX_BUF_SIZE     32
0081 
0082 /* The maximum size of the character buffer for a single TX BD. */
0083 #define TX_BUF_SIZE     32
0084 
0085 /*
0086  * The number of jiffies to wait after receiving a close command before the
0087  * device is actually closed.  This allows the last few characters to be
0088  * sent over the wire.
0089  */
0090 #define UCC_WAIT_CLOSING 100
0091 
0092 struct ucc_uart_pram {
0093     struct ucc_slow_pram common;
0094     u8 res1[8];         /* reserved */
0095     __be16 maxidl;      /* Maximum idle chars */
0096     __be16 idlc;        /* temp idle counter */
0097     __be16 brkcr;       /* Break count register */
0098     __be16 parec;       /* receive parity error counter */
0099     __be16 frmec;       /* receive framing error counter */
0100     __be16 nosec;       /* receive noise counter */
0101     __be16 brkec;       /* receive break condition counter */
0102     __be16 brkln;       /* last received break length */
0103     __be16 uaddr[2];    /* UART address character 1 & 2 */
0104     __be16 rtemp;       /* Temp storage */
0105     __be16 toseq;       /* Transmit out of sequence char */
0106     __be16 cchars[8];       /* control characters 1-8 */
0107     __be16 rccm;        /* receive control character mask */
0108     __be16 rccr;        /* receive control character register */
0109     __be16 rlbc;        /* receive last break character */
0110     __be16 res2;        /* reserved */
0111     __be32 res3;        /* reserved, should be cleared */
0112     u8 res4;        /* reserved, should be cleared */
0113     u8 res5[3];         /* reserved, should be cleared */
0114     __be32 res6;        /* reserved, should be cleared */
0115     __be32 res7;        /* reserved, should be cleared */
0116     __be32 res8;        /* reserved, should be cleared */
0117     __be32 res9;        /* reserved, should be cleared */
0118     __be32 res10;       /* reserved, should be cleared */
0119     __be32 res11;       /* reserved, should be cleared */
0120     __be32 res12;       /* reserved, should be cleared */
0121     __be32 res13;       /* reserved, should be cleared */
0122 /* The rest is for Soft-UART only */
0123     __be16 supsmr;      /* 0x90, Shadow UPSMR */
0124     __be16 res92;       /* 0x92, reserved, initialize to 0 */
0125     __be32 rx_state;    /* 0x94, RX state, initialize to 0 */
0126     __be32 rx_cnt;      /* 0x98, RX count, initialize to 0 */
0127     u8 rx_length;       /* 0x9C, Char length, set to 1+CL+PEN+1+SL */
0128     u8 rx_bitmark;      /* 0x9D, reserved, initialize to 0 */
0129     u8 rx_temp_dlst_qe;     /* 0x9E, reserved, initialize to 0 */
0130     u8 res14[0xBC - 0x9F];  /* reserved */
0131     __be32 dump_ptr;    /* 0xBC, Dump pointer */
0132     __be32 rx_frame_rem;    /* 0xC0, reserved, initialize to 0 */
0133     u8 rx_frame_rem_size;   /* 0xC4, reserved, initialize to 0 */
0134     u8 tx_mode;         /* 0xC5, mode, 0=AHDLC, 1=UART */
0135     __be16 tx_state;    /* 0xC6, TX state */
0136     u8 res15[0xD0 - 0xC8];  /* reserved */
0137     __be32 resD0;       /* 0xD0, reserved, initialize to 0 */
0138     u8 resD4;           /* 0xD4, reserved, initialize to 0 */
0139     __be16 resD5;       /* 0xD5, reserved, initialize to 0 */
0140 } __attribute__ ((packed));
0141 
0142 /* SUPSMR definitions, for Soft-UART only */
0143 #define UCC_UART_SUPSMR_SL          0x8000
0144 #define UCC_UART_SUPSMR_RPM_MASK    0x6000
0145 #define UCC_UART_SUPSMR_RPM_ODD     0x0000
0146 #define UCC_UART_SUPSMR_RPM_LOW     0x2000
0147 #define UCC_UART_SUPSMR_RPM_EVEN    0x4000
0148 #define UCC_UART_SUPSMR_RPM_HIGH    0x6000
0149 #define UCC_UART_SUPSMR_PEN         0x1000
0150 #define UCC_UART_SUPSMR_TPM_MASK    0x0C00
0151 #define UCC_UART_SUPSMR_TPM_ODD     0x0000
0152 #define UCC_UART_SUPSMR_TPM_LOW     0x0400
0153 #define UCC_UART_SUPSMR_TPM_EVEN    0x0800
0154 #define UCC_UART_SUPSMR_TPM_HIGH    0x0C00
0155 #define UCC_UART_SUPSMR_FRZ         0x0100
0156 #define UCC_UART_SUPSMR_UM_MASK     0x00c0
0157 #define UCC_UART_SUPSMR_UM_NORMAL       0x0000
0158 #define UCC_UART_SUPSMR_UM_MAN_MULTI    0x0040
0159 #define UCC_UART_SUPSMR_UM_AUTO_MULTI   0x00c0
0160 #define UCC_UART_SUPSMR_CL_MASK     0x0030
0161 #define UCC_UART_SUPSMR_CL_8        0x0030
0162 #define UCC_UART_SUPSMR_CL_7        0x0020
0163 #define UCC_UART_SUPSMR_CL_6        0x0010
0164 #define UCC_UART_SUPSMR_CL_5        0x0000
0165 
0166 #define UCC_UART_TX_STATE_AHDLC     0x00
0167 #define UCC_UART_TX_STATE_UART      0x01
0168 #define UCC_UART_TX_STATE_X1        0x00
0169 #define UCC_UART_TX_STATE_X16       0x80
0170 
0171 #define UCC_UART_PRAM_ALIGNMENT 0x100
0172 
0173 #define UCC_UART_SIZE_OF_BD     UCC_SLOW_SIZE_OF_BD
0174 #define NUM_CONTROL_CHARS       8
0175 
0176 /* Private per-port data structure */
0177 struct uart_qe_port {
0178     struct uart_port port;
0179     struct ucc_slow __iomem *uccp;
0180     struct ucc_uart_pram __iomem *uccup;
0181     struct ucc_slow_info us_info;
0182     struct ucc_slow_private *us_private;
0183     struct device_node *np;
0184     unsigned int ucc_num;   /* First ucc is 0, not 1 */
0185 
0186     u16 rx_nrfifos;
0187     u16 rx_fifosize;
0188     u16 tx_nrfifos;
0189     u16 tx_fifosize;
0190     int wait_closing;
0191     u32 flags;
0192     struct qe_bd *rx_bd_base;
0193     struct qe_bd *rx_cur;
0194     struct qe_bd *tx_bd_base;
0195     struct qe_bd *tx_cur;
0196     unsigned char *tx_buf;
0197     unsigned char *rx_buf;
0198     void *bd_virt;      /* virtual address of the BD buffers */
0199     dma_addr_t bd_dma_addr; /* bus address of the BD buffers */
0200     unsigned int bd_size;   /* size of BD buffer space */
0201 };
0202 
0203 static struct uart_driver ucc_uart_driver = {
0204     .owner      = THIS_MODULE,
0205     .driver_name    = "ucc_uart",
0206     .dev_name       = "ttyQE",
0207     .major      = SERIAL_QE_MAJOR,
0208     .minor      = SERIAL_QE_MINOR,
0209     .nr         = UCC_MAX_UART,
0210 };
0211 
0212 /*
0213  * Virtual to physical address translation.
0214  *
0215  * Given the virtual address for a character buffer, this function returns
0216  * the physical (DMA) equivalent.
0217  */
0218 static inline dma_addr_t cpu2qe_addr(void *addr, struct uart_qe_port *qe_port)
0219 {
0220     if (likely((addr >= qe_port->bd_virt)) &&
0221         (addr < (qe_port->bd_virt + qe_port->bd_size)))
0222         return qe_port->bd_dma_addr + (addr - qe_port->bd_virt);
0223 
0224     /* something nasty happened */
0225     printk(KERN_ERR "%s: addr=%p\n", __func__, addr);
0226     BUG();
0227     return 0;
0228 }
0229 
0230 /*
0231  * Physical to virtual address translation.
0232  *
0233  * Given the physical (DMA) address for a character buffer, this function
0234  * returns the virtual equivalent.
0235  */
0236 static inline void *qe2cpu_addr(dma_addr_t addr, struct uart_qe_port *qe_port)
0237 {
0238     /* sanity check */
0239     if (likely((addr >= qe_port->bd_dma_addr) &&
0240            (addr < (qe_port->bd_dma_addr + qe_port->bd_size))))
0241         return qe_port->bd_virt + (addr - qe_port->bd_dma_addr);
0242 
0243     /* something nasty happened */
0244     printk(KERN_ERR "%s: addr=%llx\n", __func__, (u64)addr);
0245     BUG();
0246     return NULL;
0247 }
0248 
0249 /*
0250  * Return 1 if the QE is done transmitting all buffers for this port
0251  *
0252  * This function scans each BD in sequence.  If we find a BD that is not
0253  * ready (READY=1), then we return 0 indicating that the QE is still sending
0254  * data.  If we reach the last BD (WRAP=1), then we know we've scanned
0255  * the entire list, and all BDs are done.
0256  */
0257 static unsigned int qe_uart_tx_empty(struct uart_port *port)
0258 {
0259     struct uart_qe_port *qe_port =
0260         container_of(port, struct uart_qe_port, port);
0261     struct qe_bd *bdp = qe_port->tx_bd_base;
0262 
0263     while (1) {
0264         if (ioread16be(&bdp->status) & BD_SC_READY)
0265             /* This BD is not done, so return "not done" */
0266             return 0;
0267 
0268         if (ioread16be(&bdp->status) & BD_SC_WRAP)
0269             /*
0270              * This BD is done and it's the last one, so return
0271              * "done"
0272              */
0273             return 1;
0274 
0275         bdp++;
0276     }
0277 }
0278 
0279 /*
0280  * Set the modem control lines
0281  *
0282  * Although the QE can control the modem control lines (e.g. CTS), we
0283  * don't need that support. This function must exist, however, otherwise
0284  * the kernel will panic.
0285  */
0286 static void qe_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
0287 {
0288 }
0289 
0290 /*
0291  * Get the current modem control line status
0292  *
0293  * Although the QE can control the modem control lines (e.g. CTS), this
0294  * driver currently doesn't support that, so we always return Carrier
0295  * Detect, Data Set Ready, and Clear To Send.
0296  */
0297 static unsigned int qe_uart_get_mctrl(struct uart_port *port)
0298 {
0299     return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
0300 }
0301 
0302 /*
0303  * Disable the transmit interrupt.
0304  *
0305  * Although this function is called "stop_tx", it does not actually stop
0306  * transmission of data.  Instead, it tells the QE to not generate an
0307  * interrupt when the UCC is finished sending characters.
0308  */
0309 static void qe_uart_stop_tx(struct uart_port *port)
0310 {
0311     struct uart_qe_port *qe_port =
0312         container_of(port, struct uart_qe_port, port);
0313 
0314     qe_clrbits_be16(&qe_port->uccp->uccm, UCC_UART_UCCE_TX);
0315 }
0316 
0317 /*
0318  * Transmit as many characters to the HW as possible.
0319  *
0320  * This function will attempt to stuff of all the characters from the
0321  * kernel's transmit buffer into TX BDs.
0322  *
0323  * A return value of non-zero indicates that it successfully stuffed all
0324  * characters from the kernel buffer.
0325  *
0326  * A return value of zero indicates that there are still characters in the
0327  * kernel's buffer that have not been transmitted, but there are no more BDs
0328  * available.  This function should be called again after a BD has been made
0329  * available.
0330  */
0331 static int qe_uart_tx_pump(struct uart_qe_port *qe_port)
0332 {
0333     struct qe_bd *bdp;
0334     unsigned char *p;
0335     unsigned int count;
0336     struct uart_port *port = &qe_port->port;
0337     struct circ_buf *xmit = &port->state->xmit;
0338 
0339     /* Handle xon/xoff */
0340     if (port->x_char) {
0341         /* Pick next descriptor and fill from buffer */
0342         bdp = qe_port->tx_cur;
0343 
0344         p = qe2cpu_addr(be32_to_cpu(bdp->buf), qe_port);
0345 
0346         *p++ = port->x_char;
0347         iowrite16be(1, &bdp->length);
0348         qe_setbits_be16(&bdp->status, BD_SC_READY);
0349         /* Get next BD. */
0350         if (ioread16be(&bdp->status) & BD_SC_WRAP)
0351             bdp = qe_port->tx_bd_base;
0352         else
0353             bdp++;
0354         qe_port->tx_cur = bdp;
0355 
0356         port->icount.tx++;
0357         port->x_char = 0;
0358         return 1;
0359     }
0360 
0361     if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
0362         qe_uart_stop_tx(port);
0363         return 0;
0364     }
0365 
0366     /* Pick next descriptor and fill from buffer */
0367     bdp = qe_port->tx_cur;
0368 
0369     while (!(ioread16be(&bdp->status) & BD_SC_READY) &&
0370            (xmit->tail != xmit->head)) {
0371         count = 0;
0372         p = qe2cpu_addr(be32_to_cpu(bdp->buf), qe_port);
0373         while (count < qe_port->tx_fifosize) {
0374             *p++ = xmit->buf[xmit->tail];
0375             xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
0376             port->icount.tx++;
0377             count++;
0378             if (xmit->head == xmit->tail)
0379                 break;
0380         }
0381 
0382         iowrite16be(count, &bdp->length);
0383         qe_setbits_be16(&bdp->status, BD_SC_READY);
0384 
0385         /* Get next BD. */
0386         if (ioread16be(&bdp->status) & BD_SC_WRAP)
0387             bdp = qe_port->tx_bd_base;
0388         else
0389             bdp++;
0390     }
0391     qe_port->tx_cur = bdp;
0392 
0393     if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
0394         uart_write_wakeup(port);
0395 
0396     if (uart_circ_empty(xmit)) {
0397         /* The kernel buffer is empty, so turn off TX interrupts.  We
0398            don't need to be told when the QE is finished transmitting
0399            the data. */
0400         qe_uart_stop_tx(port);
0401         return 0;
0402     }
0403 
0404     return 1;
0405 }
0406 
0407 /*
0408  * Start transmitting data
0409  *
0410  * This function will start transmitting any available data, if the port
0411  * isn't already transmitting data.
0412  */
0413 static void qe_uart_start_tx(struct uart_port *port)
0414 {
0415     struct uart_qe_port *qe_port =
0416         container_of(port, struct uart_qe_port, port);
0417 
0418     /* If we currently are transmitting, then just return */
0419     if (ioread16be(&qe_port->uccp->uccm) & UCC_UART_UCCE_TX)
0420         return;
0421 
0422     /* Otherwise, pump the port and start transmission */
0423     if (qe_uart_tx_pump(qe_port))
0424         qe_setbits_be16(&qe_port->uccp->uccm, UCC_UART_UCCE_TX);
0425 }
0426 
0427 /*
0428  * Stop transmitting data
0429  */
0430 static void qe_uart_stop_rx(struct uart_port *port)
0431 {
0432     struct uart_qe_port *qe_port =
0433         container_of(port, struct uart_qe_port, port);
0434 
0435     qe_clrbits_be16(&qe_port->uccp->uccm, UCC_UART_UCCE_RX);
0436 }
0437 
0438 /* Start or stop sending  break signal
0439  *
0440  * This function controls the sending of a break signal.  If break_state=1,
0441  * then we start sending a break signal.  If break_state=0, then we stop
0442  * sending the break signal.
0443  */
0444 static void qe_uart_break_ctl(struct uart_port *port, int break_state)
0445 {
0446     struct uart_qe_port *qe_port =
0447         container_of(port, struct uart_qe_port, port);
0448 
0449     if (break_state)
0450         ucc_slow_stop_tx(qe_port->us_private);
0451     else
0452         ucc_slow_restart_tx(qe_port->us_private);
0453 }
0454 
0455 /* ISR helper function for receiving character.
0456  *
0457  * This function is called by the ISR to handling receiving characters
0458  */
0459 static void qe_uart_int_rx(struct uart_qe_port *qe_port)
0460 {
0461     int i;
0462     unsigned char ch, *cp;
0463     struct uart_port *port = &qe_port->port;
0464     struct tty_port *tport = &port->state->port;
0465     struct qe_bd *bdp;
0466     u16 status;
0467     unsigned int flg;
0468 
0469     /* Just loop through the closed BDs and copy the characters into
0470      * the buffer.
0471      */
0472     bdp = qe_port->rx_cur;
0473     while (1) {
0474         status = ioread16be(&bdp->status);
0475 
0476         /* If this one is empty, then we assume we've read them all */
0477         if (status & BD_SC_EMPTY)
0478             break;
0479 
0480         /* get number of characters, and check space in RX buffer */
0481         i = ioread16be(&bdp->length);
0482 
0483         /* If we don't have enough room in RX buffer for the entire BD,
0484          * then we try later, which will be the next RX interrupt.
0485          */
0486         if (tty_buffer_request_room(tport, i) < i) {
0487             dev_dbg(port->dev, "ucc-uart: no room in RX buffer\n");
0488             return;
0489         }
0490 
0491         /* get pointer */
0492         cp = qe2cpu_addr(be32_to_cpu(bdp->buf), qe_port);
0493 
0494         /* loop through the buffer */
0495         while (i-- > 0) {
0496             ch = *cp++;
0497             port->icount.rx++;
0498             flg = TTY_NORMAL;
0499 
0500             if (!i && status &
0501                 (BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV))
0502                 goto handle_error;
0503             if (uart_handle_sysrq_char(port, ch))
0504                 continue;
0505 
0506 error_return:
0507             tty_insert_flip_char(tport, ch, flg);
0508 
0509         }
0510 
0511         /* This BD is ready to be used again. Clear status. get next */
0512         qe_clrsetbits_be16(&bdp->status,
0513                    BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV | BD_SC_ID,
0514                    BD_SC_EMPTY);
0515         if (ioread16be(&bdp->status) & BD_SC_WRAP)
0516             bdp = qe_port->rx_bd_base;
0517         else
0518             bdp++;
0519 
0520     }
0521 
0522     /* Write back buffer pointer */
0523     qe_port->rx_cur = bdp;
0524 
0525     /* Activate BH processing */
0526     tty_flip_buffer_push(tport);
0527 
0528     return;
0529 
0530     /* Error processing */
0531 
0532 handle_error:
0533     /* Statistics */
0534     if (status & BD_SC_BR)
0535         port->icount.brk++;
0536     if (status & BD_SC_PR)
0537         port->icount.parity++;
0538     if (status & BD_SC_FR)
0539         port->icount.frame++;
0540     if (status & BD_SC_OV)
0541         port->icount.overrun++;
0542 
0543     /* Mask out ignored conditions */
0544     status &= port->read_status_mask;
0545 
0546     /* Handle the remaining ones */
0547     if (status & BD_SC_BR)
0548         flg = TTY_BREAK;
0549     else if (status & BD_SC_PR)
0550         flg = TTY_PARITY;
0551     else if (status & BD_SC_FR)
0552         flg = TTY_FRAME;
0553 
0554     /* Overrun does not affect the current character ! */
0555     if (status & BD_SC_OV)
0556         tty_insert_flip_char(tport, 0, TTY_OVERRUN);
0557     port->sysrq = 0;
0558     goto error_return;
0559 }
0560 
0561 /* Interrupt handler
0562  *
0563  * This interrupt handler is called after a BD is processed.
0564  */
0565 static irqreturn_t qe_uart_int(int irq, void *data)
0566 {
0567     struct uart_qe_port *qe_port = (struct uart_qe_port *) data;
0568     struct ucc_slow __iomem *uccp = qe_port->uccp;
0569     u16 events;
0570 
0571     /* Clear the interrupts */
0572     events = ioread16be(&uccp->ucce);
0573     iowrite16be(events, &uccp->ucce);
0574 
0575     if (events & UCC_UART_UCCE_BRKE)
0576         uart_handle_break(&qe_port->port);
0577 
0578     if (events & UCC_UART_UCCE_RX)
0579         qe_uart_int_rx(qe_port);
0580 
0581     if (events & UCC_UART_UCCE_TX)
0582         qe_uart_tx_pump(qe_port);
0583 
0584     return events ? IRQ_HANDLED : IRQ_NONE;
0585 }
0586 
0587 /* Initialize buffer descriptors
0588  *
0589  * This function initializes all of the RX and TX buffer descriptors.
0590  */
0591 static void qe_uart_initbd(struct uart_qe_port *qe_port)
0592 {
0593     int i;
0594     void *bd_virt;
0595     struct qe_bd *bdp;
0596 
0597     /* Set the physical address of the host memory buffers in the buffer
0598      * descriptors, and the virtual address for us to work with.
0599      */
0600     bd_virt = qe_port->bd_virt;
0601     bdp = qe_port->rx_bd_base;
0602     qe_port->rx_cur = qe_port->rx_bd_base;
0603     for (i = 0; i < (qe_port->rx_nrfifos - 1); i++) {
0604         iowrite16be(BD_SC_EMPTY | BD_SC_INTRPT, &bdp->status);
0605         iowrite32be(cpu2qe_addr(bd_virt, qe_port), &bdp->buf);
0606         iowrite16be(0, &bdp->length);
0607         bd_virt += qe_port->rx_fifosize;
0608         bdp++;
0609     }
0610 
0611     /* */
0612     iowrite16be(BD_SC_WRAP | BD_SC_EMPTY | BD_SC_INTRPT, &bdp->status);
0613     iowrite32be(cpu2qe_addr(bd_virt, qe_port), &bdp->buf);
0614     iowrite16be(0, &bdp->length);
0615 
0616     /* Set the physical address of the host memory
0617      * buffers in the buffer descriptors, and the
0618      * virtual address for us to work with.
0619      */
0620     bd_virt = qe_port->bd_virt +
0621         L1_CACHE_ALIGN(qe_port->rx_nrfifos * qe_port->rx_fifosize);
0622     qe_port->tx_cur = qe_port->tx_bd_base;
0623     bdp = qe_port->tx_bd_base;
0624     for (i = 0; i < (qe_port->tx_nrfifos - 1); i++) {
0625         iowrite16be(BD_SC_INTRPT, &bdp->status);
0626         iowrite32be(cpu2qe_addr(bd_virt, qe_port), &bdp->buf);
0627         iowrite16be(0, &bdp->length);
0628         bd_virt += qe_port->tx_fifosize;
0629         bdp++;
0630     }
0631 
0632     /* Loopback requires the preamble bit to be set on the first TX BD */
0633 #ifdef LOOPBACK
0634     qe_setbits_be16(&qe_port->tx_cur->status, BD_SC_P);
0635 #endif
0636 
0637     iowrite16be(BD_SC_WRAP | BD_SC_INTRPT, &bdp->status);
0638     iowrite32be(cpu2qe_addr(bd_virt, qe_port), &bdp->buf);
0639     iowrite16be(0, &bdp->length);
0640 }
0641 
0642 /*
0643  * Initialize a UCC for UART.
0644  *
0645  * This function configures a given UCC to be used as a UART device. Basic
0646  * UCC initialization is handled in qe_uart_request_port().  This function
0647  * does all the UART-specific stuff.
0648  */
0649 static void qe_uart_init_ucc(struct uart_qe_port *qe_port)
0650 {
0651     u32 cecr_subblock;
0652     struct ucc_slow __iomem *uccp = qe_port->uccp;
0653     struct ucc_uart_pram *uccup = qe_port->uccup;
0654 
0655     unsigned int i;
0656 
0657     /* First, disable TX and RX in the UCC */
0658     ucc_slow_disable(qe_port->us_private, COMM_DIR_RX_AND_TX);
0659 
0660     /* Program the UCC UART parameter RAM */
0661     iowrite8(UCC_BMR_GBL | UCC_BMR_BO_BE, &uccup->common.rbmr);
0662     iowrite8(UCC_BMR_GBL | UCC_BMR_BO_BE, &uccup->common.tbmr);
0663     iowrite16be(qe_port->rx_fifosize, &uccup->common.mrblr);
0664     iowrite16be(0x10, &uccup->maxidl);
0665     iowrite16be(1, &uccup->brkcr);
0666     iowrite16be(0, &uccup->parec);
0667     iowrite16be(0, &uccup->frmec);
0668     iowrite16be(0, &uccup->nosec);
0669     iowrite16be(0, &uccup->brkec);
0670     iowrite16be(0, &uccup->uaddr[0]);
0671     iowrite16be(0, &uccup->uaddr[1]);
0672     iowrite16be(0, &uccup->toseq);
0673     for (i = 0; i < 8; i++)
0674         iowrite16be(0xC000, &uccup->cchars[i]);
0675     iowrite16be(0xc0ff, &uccup->rccm);
0676 
0677     /* Configure the GUMR registers for UART */
0678     if (soft_uart) {
0679         /* Soft-UART requires a 1X multiplier for TX */
0680         qe_clrsetbits_be32(&uccp->gumr_l,
0681                    UCC_SLOW_GUMR_L_MODE_MASK | UCC_SLOW_GUMR_L_TDCR_MASK | UCC_SLOW_GUMR_L_RDCR_MASK,
0682                    UCC_SLOW_GUMR_L_MODE_UART | UCC_SLOW_GUMR_L_TDCR_1 | UCC_SLOW_GUMR_L_RDCR_16);
0683 
0684         qe_clrsetbits_be32(&uccp->gumr_h, UCC_SLOW_GUMR_H_RFW,
0685                    UCC_SLOW_GUMR_H_TRX | UCC_SLOW_GUMR_H_TTX);
0686     } else {
0687         qe_clrsetbits_be32(&uccp->gumr_l,
0688                    UCC_SLOW_GUMR_L_MODE_MASK | UCC_SLOW_GUMR_L_TDCR_MASK | UCC_SLOW_GUMR_L_RDCR_MASK,
0689                    UCC_SLOW_GUMR_L_MODE_UART | UCC_SLOW_GUMR_L_TDCR_16 | UCC_SLOW_GUMR_L_RDCR_16);
0690 
0691         qe_clrsetbits_be32(&uccp->gumr_h,
0692                    UCC_SLOW_GUMR_H_TRX | UCC_SLOW_GUMR_H_TTX,
0693                    UCC_SLOW_GUMR_H_RFW);
0694     }
0695 
0696 #ifdef LOOPBACK
0697     qe_clrsetbits_be32(&uccp->gumr_l, UCC_SLOW_GUMR_L_DIAG_MASK,
0698                UCC_SLOW_GUMR_L_DIAG_LOOP);
0699     qe_clrsetbits_be32(&uccp->gumr_h,
0700                UCC_SLOW_GUMR_H_CTSP | UCC_SLOW_GUMR_H_RSYN,
0701                UCC_SLOW_GUMR_H_CDS);
0702 #endif
0703 
0704     /* Disable rx interrupts  and clear all pending events.  */
0705     iowrite16be(0, &uccp->uccm);
0706     iowrite16be(0xffff, &uccp->ucce);
0707     iowrite16be(0x7e7e, &uccp->udsr);
0708 
0709     /* Initialize UPSMR */
0710     iowrite16be(0, &uccp->upsmr);
0711 
0712     if (soft_uart) {
0713         iowrite16be(0x30, &uccup->supsmr);
0714         iowrite16be(0, &uccup->res92);
0715         iowrite32be(0, &uccup->rx_state);
0716         iowrite32be(0, &uccup->rx_cnt);
0717         iowrite8(0, &uccup->rx_bitmark);
0718         iowrite8(10, &uccup->rx_length);
0719         iowrite32be(0x4000, &uccup->dump_ptr);
0720         iowrite8(0, &uccup->rx_temp_dlst_qe);
0721         iowrite32be(0, &uccup->rx_frame_rem);
0722         iowrite8(0, &uccup->rx_frame_rem_size);
0723         /* Soft-UART requires TX to be 1X */
0724         iowrite8(UCC_UART_TX_STATE_UART | UCC_UART_TX_STATE_X1,
0725                 &uccup->tx_mode);
0726         iowrite16be(0, &uccup->tx_state);
0727         iowrite8(0, &uccup->resD4);
0728         iowrite16be(0, &uccup->resD5);
0729 
0730         /* Set UART mode.
0731          * Enable receive and transmit.
0732          */
0733 
0734         /* From the microcode errata:
0735          * 1.GUMR_L register, set mode=0010 (QMC).
0736          * 2.Set GUMR_H[17] bit. (UART/AHDLC mode).
0737          * 3.Set GUMR_H[19:20] (Transparent mode)
0738          * 4.Clear GUMR_H[26] (RFW)
0739          * ...
0740          * 6.Receiver must use 16x over sampling
0741          */
0742         qe_clrsetbits_be32(&uccp->gumr_l,
0743                    UCC_SLOW_GUMR_L_MODE_MASK | UCC_SLOW_GUMR_L_TDCR_MASK | UCC_SLOW_GUMR_L_RDCR_MASK,
0744                    UCC_SLOW_GUMR_L_MODE_QMC | UCC_SLOW_GUMR_L_TDCR_16 | UCC_SLOW_GUMR_L_RDCR_16);
0745 
0746         qe_clrsetbits_be32(&uccp->gumr_h,
0747                    UCC_SLOW_GUMR_H_RFW | UCC_SLOW_GUMR_H_RSYN,
0748                    UCC_SLOW_GUMR_H_SUART | UCC_SLOW_GUMR_H_TRX | UCC_SLOW_GUMR_H_TTX | UCC_SLOW_GUMR_H_TFL);
0749 
0750 #ifdef LOOPBACK
0751         qe_clrsetbits_be32(&uccp->gumr_l, UCC_SLOW_GUMR_L_DIAG_MASK,
0752                    UCC_SLOW_GUMR_L_DIAG_LOOP);
0753         qe_clrbits_be32(&uccp->gumr_h,
0754                 UCC_SLOW_GUMR_H_CTSP | UCC_SLOW_GUMR_H_CDS);
0755 #endif
0756 
0757         cecr_subblock = ucc_slow_get_qe_cr_subblock(qe_port->ucc_num);
0758         qe_issue_cmd(QE_INIT_TX_RX, cecr_subblock,
0759             QE_CR_PROTOCOL_UNSPECIFIED, 0);
0760     } else {
0761         cecr_subblock = ucc_slow_get_qe_cr_subblock(qe_port->ucc_num);
0762         qe_issue_cmd(QE_INIT_TX_RX, cecr_subblock,
0763             QE_CR_PROTOCOL_UART, 0);
0764     }
0765 }
0766 
0767 /*
0768  * Initialize the port.
0769  */
0770 static int qe_uart_startup(struct uart_port *port)
0771 {
0772     struct uart_qe_port *qe_port =
0773         container_of(port, struct uart_qe_port, port);
0774     int ret;
0775 
0776     /*
0777      * If we're using Soft-UART mode, then we need to make sure the
0778      * firmware has been uploaded first.
0779      */
0780     if (soft_uart && !firmware_loaded) {
0781         dev_err(port->dev, "Soft-UART firmware not uploaded\n");
0782         return -ENODEV;
0783     }
0784 
0785     qe_uart_initbd(qe_port);
0786     qe_uart_init_ucc(qe_port);
0787 
0788     /* Install interrupt handler. */
0789     ret = request_irq(port->irq, qe_uart_int, IRQF_SHARED, "ucc-uart",
0790         qe_port);
0791     if (ret) {
0792         dev_err(port->dev, "could not claim IRQ %u\n", port->irq);
0793         return ret;
0794     }
0795 
0796     /* Startup rx-int */
0797     qe_setbits_be16(&qe_port->uccp->uccm, UCC_UART_UCCE_RX);
0798     ucc_slow_enable(qe_port->us_private, COMM_DIR_RX_AND_TX);
0799 
0800     return 0;
0801 }
0802 
0803 /*
0804  * Shutdown the port.
0805  */
0806 static void qe_uart_shutdown(struct uart_port *port)
0807 {
0808     struct uart_qe_port *qe_port =
0809         container_of(port, struct uart_qe_port, port);
0810     struct ucc_slow __iomem *uccp = qe_port->uccp;
0811     unsigned int timeout = 20;
0812 
0813     /* Disable RX and TX */
0814 
0815     /* Wait for all the BDs marked sent */
0816     while (!qe_uart_tx_empty(port)) {
0817         if (!--timeout) {
0818             dev_warn(port->dev, "shutdown timeout\n");
0819             break;
0820         }
0821         set_current_state(TASK_UNINTERRUPTIBLE);
0822         schedule_timeout(2);
0823     }
0824 
0825     if (qe_port->wait_closing) {
0826         /* Wait a bit longer */
0827         set_current_state(TASK_UNINTERRUPTIBLE);
0828         schedule_timeout(qe_port->wait_closing);
0829     }
0830 
0831     /* Stop uarts */
0832     ucc_slow_disable(qe_port->us_private, COMM_DIR_RX_AND_TX);
0833     qe_clrbits_be16(&uccp->uccm, UCC_UART_UCCE_TX | UCC_UART_UCCE_RX);
0834 
0835     /* Shut them really down and reinit buffer descriptors */
0836     ucc_slow_graceful_stop_tx(qe_port->us_private);
0837     qe_uart_initbd(qe_port);
0838 
0839     free_irq(port->irq, qe_port);
0840 }
0841 
0842 /*
0843  * Set the serial port parameters.
0844  */
0845 static void qe_uart_set_termios(struct uart_port *port,
0846                 struct ktermios *termios, struct ktermios *old)
0847 {
0848     struct uart_qe_port *qe_port =
0849         container_of(port, struct uart_qe_port, port);
0850     struct ucc_slow __iomem *uccp = qe_port->uccp;
0851     unsigned int baud;
0852     unsigned long flags;
0853     u16 upsmr = ioread16be(&uccp->upsmr);
0854     struct ucc_uart_pram __iomem *uccup = qe_port->uccup;
0855     u16 supsmr = ioread16be(&uccup->supsmr);
0856     u8 char_length = 2; /* 1 + CL + PEN + 1 + SL */
0857 
0858     /* Character length programmed into the mode register is the
0859      * sum of: 1 start bit, number of data bits, 0 or 1 parity bit,
0860      * 1 or 2 stop bits, minus 1.
0861      * The value 'bits' counts this for us.
0862      */
0863 
0864     /* byte size */
0865     upsmr &= UCC_UART_UPSMR_CL_MASK;
0866     supsmr &= UCC_UART_SUPSMR_CL_MASK;
0867 
0868     switch (termios->c_cflag & CSIZE) {
0869     case CS5:
0870         upsmr |= UCC_UART_UPSMR_CL_5;
0871         supsmr |= UCC_UART_SUPSMR_CL_5;
0872         char_length += 5;
0873         break;
0874     case CS6:
0875         upsmr |= UCC_UART_UPSMR_CL_6;
0876         supsmr |= UCC_UART_SUPSMR_CL_6;
0877         char_length += 6;
0878         break;
0879     case CS7:
0880         upsmr |= UCC_UART_UPSMR_CL_7;
0881         supsmr |= UCC_UART_SUPSMR_CL_7;
0882         char_length += 7;
0883         break;
0884     default:    /* case CS8 */
0885         upsmr |= UCC_UART_UPSMR_CL_8;
0886         supsmr |= UCC_UART_SUPSMR_CL_8;
0887         char_length += 8;
0888         break;
0889     }
0890 
0891     /* If CSTOPB is set, we want two stop bits */
0892     if (termios->c_cflag & CSTOPB) {
0893         upsmr |= UCC_UART_UPSMR_SL;
0894         supsmr |= UCC_UART_SUPSMR_SL;
0895         char_length++;  /* + SL */
0896     }
0897 
0898     if (termios->c_cflag & PARENB) {
0899         upsmr |= UCC_UART_UPSMR_PEN;
0900         supsmr |= UCC_UART_SUPSMR_PEN;
0901         char_length++;  /* + PEN */
0902 
0903         if (!(termios->c_cflag & PARODD)) {
0904             upsmr &= ~(UCC_UART_UPSMR_RPM_MASK |
0905                    UCC_UART_UPSMR_TPM_MASK);
0906             upsmr |= UCC_UART_UPSMR_RPM_EVEN |
0907                 UCC_UART_UPSMR_TPM_EVEN;
0908             supsmr &= ~(UCC_UART_SUPSMR_RPM_MASK |
0909                     UCC_UART_SUPSMR_TPM_MASK);
0910             supsmr |= UCC_UART_SUPSMR_RPM_EVEN |
0911                 UCC_UART_SUPSMR_TPM_EVEN;
0912         }
0913     }
0914 
0915     /*
0916      * Set up parity check flag
0917      */
0918     port->read_status_mask = BD_SC_EMPTY | BD_SC_OV;
0919     if (termios->c_iflag & INPCK)
0920         port->read_status_mask |= BD_SC_FR | BD_SC_PR;
0921     if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
0922         port->read_status_mask |= BD_SC_BR;
0923 
0924     /*
0925      * Characters to ignore
0926      */
0927     port->ignore_status_mask = 0;
0928     if (termios->c_iflag & IGNPAR)
0929         port->ignore_status_mask |= BD_SC_PR | BD_SC_FR;
0930     if (termios->c_iflag & IGNBRK) {
0931         port->ignore_status_mask |= BD_SC_BR;
0932         /*
0933          * If we're ignore parity and break indicators, ignore
0934          * overruns too.  (For real raw support).
0935          */
0936         if (termios->c_iflag & IGNPAR)
0937             port->ignore_status_mask |= BD_SC_OV;
0938     }
0939     /*
0940      * !!! ignore all characters if CREAD is not set
0941      */
0942     if ((termios->c_cflag & CREAD) == 0)
0943         port->read_status_mask &= ~BD_SC_EMPTY;
0944 
0945     baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
0946 
0947     /* Do we really need a spinlock here? */
0948     spin_lock_irqsave(&port->lock, flags);
0949 
0950     /* Update the per-port timeout. */
0951     uart_update_timeout(port, termios->c_cflag, baud);
0952 
0953     iowrite16be(upsmr, &uccp->upsmr);
0954     if (soft_uart) {
0955         iowrite16be(supsmr, &uccup->supsmr);
0956         iowrite8(char_length, &uccup->rx_length);
0957 
0958         /* Soft-UART requires a 1X multiplier for TX */
0959         qe_setbrg(qe_port->us_info.rx_clock, baud, 16);
0960         qe_setbrg(qe_port->us_info.tx_clock, baud, 1);
0961     } else {
0962         qe_setbrg(qe_port->us_info.rx_clock, baud, 16);
0963         qe_setbrg(qe_port->us_info.tx_clock, baud, 16);
0964     }
0965 
0966     spin_unlock_irqrestore(&port->lock, flags);
0967 }
0968 
0969 /*
0970  * Return a pointer to a string that describes what kind of port this is.
0971  */
0972 static const char *qe_uart_type(struct uart_port *port)
0973 {
0974     return "QE";
0975 }
0976 
0977 /*
0978  * Allocate any memory and I/O resources required by the port.
0979  */
0980 static int qe_uart_request_port(struct uart_port *port)
0981 {
0982     int ret;
0983     struct uart_qe_port *qe_port =
0984         container_of(port, struct uart_qe_port, port);
0985     struct ucc_slow_info *us_info = &qe_port->us_info;
0986     struct ucc_slow_private *uccs;
0987     unsigned int rx_size, tx_size;
0988     void *bd_virt;
0989     dma_addr_t bd_dma_addr = 0;
0990 
0991     ret = ucc_slow_init(us_info, &uccs);
0992     if (ret) {
0993         dev_err(port->dev, "could not initialize UCC%u\n",
0994                qe_port->ucc_num);
0995         return ret;
0996     }
0997 
0998     qe_port->us_private = uccs;
0999     qe_port->uccp = uccs->us_regs;
1000     qe_port->uccup = (struct ucc_uart_pram *) uccs->us_pram;
1001     qe_port->rx_bd_base = uccs->rx_bd;
1002     qe_port->tx_bd_base = uccs->tx_bd;
1003 
1004     /*
1005      * Allocate the transmit and receive data buffers.
1006      */
1007 
1008     rx_size = L1_CACHE_ALIGN(qe_port->rx_nrfifos * qe_port->rx_fifosize);
1009     tx_size = L1_CACHE_ALIGN(qe_port->tx_nrfifos * qe_port->tx_fifosize);
1010 
1011     bd_virt = dma_alloc_coherent(port->dev, rx_size + tx_size, &bd_dma_addr,
1012         GFP_KERNEL);
1013     if (!bd_virt) {
1014         dev_err(port->dev, "could not allocate buffer descriptors\n");
1015         return -ENOMEM;
1016     }
1017 
1018     qe_port->bd_virt = bd_virt;
1019     qe_port->bd_dma_addr = bd_dma_addr;
1020     qe_port->bd_size = rx_size + tx_size;
1021 
1022     qe_port->rx_buf = bd_virt;
1023     qe_port->tx_buf = qe_port->rx_buf + rx_size;
1024 
1025     return 0;
1026 }
1027 
1028 /*
1029  * Configure the port.
1030  *
1031  * We say we're a CPM-type port because that's mostly true.  Once the device
1032  * is configured, this driver operates almost identically to the CPM serial
1033  * driver.
1034  */
1035 static void qe_uart_config_port(struct uart_port *port, int flags)
1036 {
1037     if (flags & UART_CONFIG_TYPE) {
1038         port->type = PORT_CPM;
1039         qe_uart_request_port(port);
1040     }
1041 }
1042 
1043 /*
1044  * Release any memory and I/O resources that were allocated in
1045  * qe_uart_request_port().
1046  */
1047 static void qe_uart_release_port(struct uart_port *port)
1048 {
1049     struct uart_qe_port *qe_port =
1050         container_of(port, struct uart_qe_port, port);
1051     struct ucc_slow_private *uccs = qe_port->us_private;
1052 
1053     dma_free_coherent(port->dev, qe_port->bd_size, qe_port->bd_virt,
1054               qe_port->bd_dma_addr);
1055 
1056     ucc_slow_free(uccs);
1057 }
1058 
1059 /*
1060  * Verify that the data in serial_struct is suitable for this device.
1061  */
1062 static int qe_uart_verify_port(struct uart_port *port,
1063                    struct serial_struct *ser)
1064 {
1065     if (ser->type != PORT_UNKNOWN && ser->type != PORT_CPM)
1066         return -EINVAL;
1067 
1068     if (ser->irq < 0 || ser->irq >= nr_irqs)
1069         return -EINVAL;
1070 
1071     if (ser->baud_base < 9600)
1072         return -EINVAL;
1073 
1074     return 0;
1075 }
1076 /* UART operations
1077  *
1078  * Details on these functions can be found in Documentation/driver-api/serial/driver.rst
1079  */
1080 static const struct uart_ops qe_uart_pops = {
1081     .tx_empty       = qe_uart_tx_empty,
1082     .set_mctrl      = qe_uart_set_mctrl,
1083     .get_mctrl      = qe_uart_get_mctrl,
1084     .stop_tx    = qe_uart_stop_tx,
1085     .start_tx       = qe_uart_start_tx,
1086     .stop_rx    = qe_uart_stop_rx,
1087     .break_ctl      = qe_uart_break_ctl,
1088     .startup    = qe_uart_startup,
1089     .shutdown       = qe_uart_shutdown,
1090     .set_termios    = qe_uart_set_termios,
1091     .type       = qe_uart_type,
1092     .release_port   = qe_uart_release_port,
1093     .request_port   = qe_uart_request_port,
1094     .config_port    = qe_uart_config_port,
1095     .verify_port    = qe_uart_verify_port,
1096 };
1097 
1098 
1099 #ifdef CONFIG_PPC32
1100 /*
1101  * Obtain the SOC model number and revision level
1102  *
1103  * This function parses the device tree to obtain the SOC model.  It then
1104  * reads the SVR register to the revision.
1105  *
1106  * The device tree stores the SOC model two different ways.
1107  *
1108  * The new way is:
1109  *
1110  *          cpu@0 {
1111  *              compatible = "PowerPC,8323";
1112  *              device_type = "cpu";
1113  *              ...
1114  *
1115  *
1116  * The old way is:
1117  *           PowerPC,8323@0 {
1118  *              device_type = "cpu";
1119  *              ...
1120  *
1121  * This code first checks the new way, and then the old way.
1122  */
1123 static unsigned int soc_info(unsigned int *rev_h, unsigned int *rev_l)
1124 {
1125     struct device_node *np;
1126     const char *soc_string;
1127     unsigned int svr;
1128     unsigned int soc;
1129 
1130     /* Find the CPU node */
1131     np = of_find_node_by_type(NULL, "cpu");
1132     if (!np)
1133         return 0;
1134     /* Find the compatible property */
1135     soc_string = of_get_property(np, "compatible", NULL);
1136     if (!soc_string)
1137         /* No compatible property, so try the name. */
1138         soc_string = np->name;
1139 
1140     of_node_put(np);
1141 
1142     /* Extract the SOC number from the "PowerPC," string */
1143     if ((sscanf(soc_string, "PowerPC,%u", &soc) != 1) || !soc)
1144         return 0;
1145 
1146     /* Get the revision from the SVR */
1147     svr = mfspr(SPRN_SVR);
1148     *rev_h = (svr >> 4) & 0xf;
1149     *rev_l = svr & 0xf;
1150 
1151     return soc;
1152 }
1153 
1154 /*
1155  * requst_firmware_nowait() callback function
1156  *
1157  * This function is called by the kernel when a firmware is made available,
1158  * or if it times out waiting for the firmware.
1159  */
1160 static void uart_firmware_cont(const struct firmware *fw, void *context)
1161 {
1162     struct qe_firmware *firmware;
1163     struct device *dev = context;
1164     int ret;
1165 
1166     if (!fw) {
1167         dev_err(dev, "firmware not found\n");
1168         return;
1169     }
1170 
1171     firmware = (struct qe_firmware *) fw->data;
1172 
1173     if (firmware->header.length != fw->size) {
1174         dev_err(dev, "invalid firmware\n");
1175         goto out;
1176     }
1177 
1178     ret = qe_upload_firmware(firmware);
1179     if (ret) {
1180         dev_err(dev, "could not load firmware\n");
1181         goto out;
1182     }
1183 
1184     firmware_loaded = 1;
1185  out:
1186     release_firmware(fw);
1187 }
1188 
1189 static int soft_uart_init(struct platform_device *ofdev)
1190 {
1191     struct device_node *np = ofdev->dev.of_node;
1192     struct qe_firmware_info *qe_fw_info;
1193     int ret;
1194 
1195     if (of_find_property(np, "soft-uart", NULL)) {
1196         dev_dbg(&ofdev->dev, "using Soft-UART mode\n");
1197         soft_uart = 1;
1198     } else {
1199         return 0;
1200     }
1201 
1202     qe_fw_info = qe_get_firmware_info();
1203 
1204     /* Check if the firmware has been uploaded. */
1205     if (qe_fw_info && strstr(qe_fw_info->id, "Soft-UART")) {
1206         firmware_loaded = 1;
1207     } else {
1208         char filename[32];
1209         unsigned int soc;
1210         unsigned int rev_h;
1211         unsigned int rev_l;
1212 
1213         soc = soc_info(&rev_h, &rev_l);
1214         if (!soc) {
1215             dev_err(&ofdev->dev, "unknown CPU model\n");
1216             return -ENXIO;
1217         }
1218         sprintf(filename, "fsl_qe_ucode_uart_%u_%u%u.bin",
1219             soc, rev_h, rev_l);
1220 
1221         dev_info(&ofdev->dev, "waiting for firmware %s\n",
1222              filename);
1223 
1224         /*
1225          * We call request_firmware_nowait instead of
1226          * request_firmware so that the driver can load and
1227          * initialize the ports without holding up the rest of
1228          * the kernel.  If hotplug support is enabled in the
1229          * kernel, then we use it.
1230          */
1231         ret = request_firmware_nowait(THIS_MODULE,
1232                           FW_ACTION_UEVENT, filename, &ofdev->dev,
1233                           GFP_KERNEL, &ofdev->dev, uart_firmware_cont);
1234         if (ret) {
1235             dev_err(&ofdev->dev,
1236                 "could not load firmware %s\n",
1237                 filename);
1238             return ret;
1239         }
1240     }
1241     return 0;
1242 }
1243 
1244 #else /* !CONFIG_PPC32 */
1245 
1246 static int soft_uart_init(struct platform_device *ofdev)
1247 {
1248     return 0;
1249 }
1250 
1251 #endif
1252 
1253 
1254 static int ucc_uart_probe(struct platform_device *ofdev)
1255 {
1256     struct device_node *np = ofdev->dev.of_node;
1257     const char *sprop;      /* String OF properties */
1258     struct uart_qe_port *qe_port = NULL;
1259     struct resource res;
1260     u32 val;
1261     int ret;
1262 
1263     /*
1264      * Determine if we need Soft-UART mode
1265      */
1266     ret = soft_uart_init(ofdev);
1267     if (ret)
1268         return ret;
1269 
1270     qe_port = kzalloc(sizeof(struct uart_qe_port), GFP_KERNEL);
1271     if (!qe_port) {
1272         dev_err(&ofdev->dev, "can't allocate QE port structure\n");
1273         return -ENOMEM;
1274     }
1275 
1276     /* Search for IRQ and mapbase */
1277     ret = of_address_to_resource(np, 0, &res);
1278     if (ret) {
1279         dev_err(&ofdev->dev, "missing 'reg' property in device tree\n");
1280         goto out_free;
1281     }
1282     if (!res.start) {
1283         dev_err(&ofdev->dev, "invalid 'reg' property in device tree\n");
1284         ret = -EINVAL;
1285         goto out_free;
1286     }
1287     qe_port->port.mapbase = res.start;
1288 
1289     /* Get the UCC number (device ID) */
1290     /* UCCs are numbered 1-7 */
1291     if (of_property_read_u32(np, "cell-index", &val)) {
1292         if (of_property_read_u32(np, "device-id", &val)) {
1293             dev_err(&ofdev->dev, "UCC is unspecified in device tree\n");
1294             ret = -EINVAL;
1295             goto out_free;
1296         }
1297     }
1298 
1299     if (val < 1 || val > UCC_MAX_NUM) {
1300         dev_err(&ofdev->dev, "no support for UCC%u\n", val);
1301         ret = -ENODEV;
1302         goto out_free;
1303     }
1304     qe_port->ucc_num = val - 1;
1305 
1306     /*
1307      * In the future, we should not require the BRG to be specified in the
1308      * device tree.  If no clock-source is specified, then just pick a BRG
1309      * to use.  This requires a new QE library function that manages BRG
1310      * assignments.
1311      */
1312 
1313     sprop = of_get_property(np, "rx-clock-name", NULL);
1314     if (!sprop) {
1315         dev_err(&ofdev->dev, "missing rx-clock-name in device tree\n");
1316         ret = -ENODEV;
1317         goto out_free;
1318     }
1319 
1320     qe_port->us_info.rx_clock = qe_clock_source(sprop);
1321     if ((qe_port->us_info.rx_clock < QE_BRG1) ||
1322         (qe_port->us_info.rx_clock > QE_BRG16)) {
1323         dev_err(&ofdev->dev, "rx-clock-name must be a BRG for UART\n");
1324         ret = -ENODEV;
1325         goto out_free;
1326     }
1327 
1328 #ifdef LOOPBACK
1329     /* In internal loopback mode, TX and RX must use the same clock */
1330     qe_port->us_info.tx_clock = qe_port->us_info.rx_clock;
1331 #else
1332     sprop = of_get_property(np, "tx-clock-name", NULL);
1333     if (!sprop) {
1334         dev_err(&ofdev->dev, "missing tx-clock-name in device tree\n");
1335         ret = -ENODEV;
1336         goto out_free;
1337     }
1338     qe_port->us_info.tx_clock = qe_clock_source(sprop);
1339 #endif
1340     if ((qe_port->us_info.tx_clock < QE_BRG1) ||
1341         (qe_port->us_info.tx_clock > QE_BRG16)) {
1342         dev_err(&ofdev->dev, "tx-clock-name must be a BRG for UART\n");
1343         ret = -ENODEV;
1344         goto out_free;
1345     }
1346 
1347     /* Get the port number, numbered 0-3 */
1348     if (of_property_read_u32(np, "port-number", &val)) {
1349         dev_err(&ofdev->dev, "missing port-number in device tree\n");
1350         ret = -EINVAL;
1351         goto out_free;
1352     }
1353     qe_port->port.line = val;
1354     if (qe_port->port.line >= UCC_MAX_UART) {
1355         dev_err(&ofdev->dev, "port-number must be 0-%u\n",
1356             UCC_MAX_UART - 1);
1357         ret = -EINVAL;
1358         goto out_free;
1359     }
1360 
1361     qe_port->port.irq = irq_of_parse_and_map(np, 0);
1362     if (qe_port->port.irq == 0) {
1363         dev_err(&ofdev->dev, "could not map IRQ for UCC%u\n",
1364                qe_port->ucc_num + 1);
1365         ret = -EINVAL;
1366         goto out_free;
1367     }
1368 
1369     /*
1370      * Newer device trees have an "fsl,qe" compatible property for the QE
1371      * node, but we still need to support older device trees.
1372      */
1373     np = of_find_compatible_node(NULL, NULL, "fsl,qe");
1374     if (!np) {
1375         np = of_find_node_by_type(NULL, "qe");
1376         if (!np) {
1377             dev_err(&ofdev->dev, "could not find 'qe' node\n");
1378             ret = -EINVAL;
1379             goto out_free;
1380         }
1381     }
1382 
1383     if (of_property_read_u32(np, "brg-frequency", &val)) {
1384         dev_err(&ofdev->dev,
1385                "missing brg-frequency in device tree\n");
1386         ret = -EINVAL;
1387         goto out_np;
1388     }
1389 
1390     if (val)
1391         qe_port->port.uartclk = val;
1392     else {
1393         if (!IS_ENABLED(CONFIG_PPC32)) {
1394             dev_err(&ofdev->dev,
1395                 "invalid brg-frequency in device tree\n");
1396             ret = -EINVAL;
1397             goto out_np;
1398         }
1399 
1400         /*
1401          * Older versions of U-Boot do not initialize the brg-frequency
1402          * property, so in this case we assume the BRG frequency is
1403          * half the QE bus frequency.
1404          */
1405         if (of_property_read_u32(np, "bus-frequency", &val)) {
1406             dev_err(&ofdev->dev,
1407                 "missing QE bus-frequency in device tree\n");
1408             ret = -EINVAL;
1409             goto out_np;
1410         }
1411         if (val)
1412             qe_port->port.uartclk = val / 2;
1413         else {
1414             dev_err(&ofdev->dev,
1415                 "invalid QE bus-frequency in device tree\n");
1416             ret = -EINVAL;
1417             goto out_np;
1418         }
1419     }
1420 
1421     spin_lock_init(&qe_port->port.lock);
1422     qe_port->np = np;
1423     qe_port->port.dev = &ofdev->dev;
1424     qe_port->port.ops = &qe_uart_pops;
1425     qe_port->port.iotype = UPIO_MEM;
1426 
1427     qe_port->tx_nrfifos = TX_NUM_FIFO;
1428     qe_port->tx_fifosize = TX_BUF_SIZE;
1429     qe_port->rx_nrfifos = RX_NUM_FIFO;
1430     qe_port->rx_fifosize = RX_BUF_SIZE;
1431 
1432     qe_port->wait_closing = UCC_WAIT_CLOSING;
1433     qe_port->port.fifosize = 512;
1434     qe_port->port.flags = UPF_BOOT_AUTOCONF | UPF_IOREMAP;
1435 
1436     qe_port->us_info.ucc_num = qe_port->ucc_num;
1437     qe_port->us_info.regs = (phys_addr_t) res.start;
1438     qe_port->us_info.irq = qe_port->port.irq;
1439 
1440     qe_port->us_info.rx_bd_ring_len = qe_port->rx_nrfifos;
1441     qe_port->us_info.tx_bd_ring_len = qe_port->tx_nrfifos;
1442 
1443     /* Make sure ucc_slow_init() initializes both TX and RX */
1444     qe_port->us_info.init_tx = 1;
1445     qe_port->us_info.init_rx = 1;
1446 
1447     /* Add the port to the uart sub-system.  This will cause
1448      * qe_uart_config_port() to be called, so the us_info structure must
1449      * be initialized.
1450      */
1451     ret = uart_add_one_port(&ucc_uart_driver, &qe_port->port);
1452     if (ret) {
1453         dev_err(&ofdev->dev, "could not add /dev/ttyQE%u\n",
1454                qe_port->port.line);
1455         goto out_np;
1456     }
1457 
1458     platform_set_drvdata(ofdev, qe_port);
1459 
1460     dev_info(&ofdev->dev, "UCC%u assigned to /dev/ttyQE%u\n",
1461         qe_port->ucc_num + 1, qe_port->port.line);
1462 
1463     /* Display the mknod command for this device */
1464     dev_dbg(&ofdev->dev, "mknod command is 'mknod /dev/ttyQE%u c %u %u'\n",
1465            qe_port->port.line, SERIAL_QE_MAJOR,
1466            SERIAL_QE_MINOR + qe_port->port.line);
1467 
1468     return 0;
1469 out_np:
1470     of_node_put(np);
1471 out_free:
1472     kfree(qe_port);
1473     return ret;
1474 }
1475 
1476 static int ucc_uart_remove(struct platform_device *ofdev)
1477 {
1478     struct uart_qe_port *qe_port = platform_get_drvdata(ofdev);
1479 
1480     dev_info(&ofdev->dev, "removing /dev/ttyQE%u\n", qe_port->port.line);
1481 
1482     uart_remove_one_port(&ucc_uart_driver, &qe_port->port);
1483 
1484     kfree(qe_port);
1485 
1486     return 0;
1487 }
1488 
1489 static const struct of_device_id ucc_uart_match[] = {
1490     {
1491         .type = "serial",
1492         .compatible = "ucc_uart",
1493     },
1494     {
1495         .compatible = "fsl,t1040-ucc-uart",
1496     },
1497     {},
1498 };
1499 MODULE_DEVICE_TABLE(of, ucc_uart_match);
1500 
1501 static struct platform_driver ucc_uart_of_driver = {
1502     .driver = {
1503         .name = "ucc_uart",
1504         .of_match_table    = ucc_uart_match,
1505     },
1506     .probe      = ucc_uart_probe,
1507     .remove     = ucc_uart_remove,
1508 };
1509 
1510 static int __init ucc_uart_init(void)
1511 {
1512     int ret;
1513 
1514     printk(KERN_INFO "Freescale QUICC Engine UART device driver\n");
1515 #ifdef LOOPBACK
1516     printk(KERN_INFO "ucc-uart: Using loopback mode\n");
1517 #endif
1518 
1519     ret = uart_register_driver(&ucc_uart_driver);
1520     if (ret) {
1521         printk(KERN_ERR "ucc-uart: could not register UART driver\n");
1522         return ret;
1523     }
1524 
1525     ret = platform_driver_register(&ucc_uart_of_driver);
1526     if (ret) {
1527         printk(KERN_ERR
1528                "ucc-uart: could not register platform driver\n");
1529         uart_unregister_driver(&ucc_uart_driver);
1530     }
1531 
1532     return ret;
1533 }
1534 
1535 static void __exit ucc_uart_exit(void)
1536 {
1537     printk(KERN_INFO
1538            "Freescale QUICC Engine UART device driver unloading\n");
1539 
1540     platform_driver_unregister(&ucc_uart_of_driver);
1541     uart_unregister_driver(&ucc_uart_driver);
1542 }
1543 
1544 module_init(ucc_uart_init);
1545 module_exit(ucc_uart_exit);
1546 
1547 MODULE_DESCRIPTION("Freescale QUICC Engine (QE) UART");
1548 MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
1549 MODULE_LICENSE("GPL v2");
1550 MODULE_ALIAS_CHARDEV_MAJOR(SERIAL_QE_MAJOR);
1551