Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * TTY driver for MIPS EJTAG Fast Debug Channels.
0004  *
0005  * Copyright (C) 2007-2015 Imagination Technologies Ltd
0006  */
0007 
0008 #include <linux/atomic.h>
0009 #include <linux/bitops.h>
0010 #include <linux/completion.h>
0011 #include <linux/console.h>
0012 #include <linux/delay.h>
0013 #include <linux/export.h>
0014 #include <linux/init.h>
0015 #include <linux/interrupt.h>
0016 #include <linux/kernel.h>
0017 #include <linux/kgdb.h>
0018 #include <linux/kthread.h>
0019 #include <linux/sched.h>
0020 #include <linux/serial.h>
0021 #include <linux/serial_core.h>
0022 #include <linux/slab.h>
0023 #include <linux/spinlock.h>
0024 #include <linux/string.h>
0025 #include <linux/timer.h>
0026 #include <linux/tty.h>
0027 #include <linux/tty_driver.h>
0028 #include <linux/tty_flip.h>
0029 #include <linux/uaccess.h>
0030 
0031 #include <asm/cdmm.h>
0032 #include <asm/irq.h>
0033 
0034 /* Register offsets */
0035 #define REG_FDACSR  0x00    /* FDC Access Control and Status Register */
0036 #define REG_FDCFG   0x08    /* FDC Configuration Register */
0037 #define REG_FDSTAT  0x10    /* FDC Status Register */
0038 #define REG_FDRX    0x18    /* FDC Receive Register */
0039 #define REG_FDTX(N) (0x20+0x8*(N))  /* FDC Transmit Register n (0..15) */
0040 
0041 /* Register fields */
0042 
0043 #define REG_FDCFG_TXINTTHRES_SHIFT  18
0044 #define REG_FDCFG_TXINTTHRES        (0x3 << REG_FDCFG_TXINTTHRES_SHIFT)
0045 #define REG_FDCFG_TXINTTHRES_DISABLED   (0x0 << REG_FDCFG_TXINTTHRES_SHIFT)
0046 #define REG_FDCFG_TXINTTHRES_EMPTY  (0x1 << REG_FDCFG_TXINTTHRES_SHIFT)
0047 #define REG_FDCFG_TXINTTHRES_NOTFULL    (0x2 << REG_FDCFG_TXINTTHRES_SHIFT)
0048 #define REG_FDCFG_TXINTTHRES_NEAREMPTY  (0x3 << REG_FDCFG_TXINTTHRES_SHIFT)
0049 #define REG_FDCFG_RXINTTHRES_SHIFT  16
0050 #define REG_FDCFG_RXINTTHRES        (0x3 << REG_FDCFG_RXINTTHRES_SHIFT)
0051 #define REG_FDCFG_RXINTTHRES_DISABLED   (0x0 << REG_FDCFG_RXINTTHRES_SHIFT)
0052 #define REG_FDCFG_RXINTTHRES_FULL   (0x1 << REG_FDCFG_RXINTTHRES_SHIFT)
0053 #define REG_FDCFG_RXINTTHRES_NOTEMPTY   (0x2 << REG_FDCFG_RXINTTHRES_SHIFT)
0054 #define REG_FDCFG_RXINTTHRES_NEARFULL   (0x3 << REG_FDCFG_RXINTTHRES_SHIFT)
0055 #define REG_FDCFG_TXFIFOSIZE_SHIFT  8
0056 #define REG_FDCFG_TXFIFOSIZE        (0xff << REG_FDCFG_TXFIFOSIZE_SHIFT)
0057 #define REG_FDCFG_RXFIFOSIZE_SHIFT  0
0058 #define REG_FDCFG_RXFIFOSIZE        (0xff << REG_FDCFG_RXFIFOSIZE_SHIFT)
0059 
0060 #define REG_FDSTAT_TXCOUNT_SHIFT    24
0061 #define REG_FDSTAT_TXCOUNT      (0xff << REG_FDSTAT_TXCOUNT_SHIFT)
0062 #define REG_FDSTAT_RXCOUNT_SHIFT    16
0063 #define REG_FDSTAT_RXCOUNT      (0xff << REG_FDSTAT_RXCOUNT_SHIFT)
0064 #define REG_FDSTAT_RXCHAN_SHIFT     4
0065 #define REG_FDSTAT_RXCHAN       (0xf << REG_FDSTAT_RXCHAN_SHIFT)
0066 #define REG_FDSTAT_RXE          BIT(3)  /* Rx Empty */
0067 #define REG_FDSTAT_RXF          BIT(2)  /* Rx Full */
0068 #define REG_FDSTAT_TXE          BIT(1)  /* Tx Empty */
0069 #define REG_FDSTAT_TXF          BIT(0)  /* Tx Full */
0070 
0071 /* Default channel for the early console */
0072 #define CONSOLE_CHANNEL      1
0073 
0074 #define NUM_TTY_CHANNELS     16
0075 
0076 #define RX_BUF_SIZE 1024
0077 
0078 /*
0079  * When the IRQ is unavailable, the FDC state must be polled for incoming data
0080  * and space becoming available in TX FIFO.
0081  */
0082 #define FDC_TTY_POLL (HZ / 50)
0083 
0084 struct mips_ejtag_fdc_tty;
0085 
0086 /**
0087  * struct mips_ejtag_fdc_tty_port - Wrapper struct for FDC tty_port.
0088  * @port:       TTY port data
0089  * @driver:     TTY driver.
0090  * @rx_lock:        Lock for rx_buf.
0091  *          This protects between the hard interrupt and user
0092  *          context. It's also held during read SWITCH operations.
0093  * @rx_buf:     Read buffer.
0094  * @xmit_lock:      Lock for xmit_*, and port.xmit_buf.
0095  *          This protects between user context and kernel thread.
0096  *          It is used from chars_in_buffer()/write_room() TTY
0097  *          callbacks which are used during wait operations, so a
0098  *          mutex is unsuitable.
0099  * @xmit_cnt:       Size of xmit buffer contents.
0100  * @xmit_head:      Head of xmit buffer where data is written.
0101  * @xmit_tail:      Tail of xmit buffer where data is read.
0102  * @xmit_empty:     Completion for xmit buffer being empty.
0103  */
0104 struct mips_ejtag_fdc_tty_port {
0105     struct tty_port          port;
0106     struct mips_ejtag_fdc_tty   *driver;
0107     raw_spinlock_t           rx_lock;
0108     void                *rx_buf;
0109     spinlock_t           xmit_lock;
0110     unsigned int             xmit_cnt;
0111     unsigned int             xmit_head;
0112     unsigned int             xmit_tail;
0113     struct completion        xmit_empty;
0114 };
0115 
0116 /**
0117  * struct mips_ejtag_fdc_tty - Driver data for FDC as a whole.
0118  * @dev:        FDC device (for dev_*() logging).
0119  * @driver:     TTY driver.
0120  * @cpu:        CPU number for this FDC.
0121  * @fdc_name:       FDC name (not for base of channel names).
0122  * @driver_name:    Base of driver name.
0123  * @ports:      Per-channel data.
0124  * @waitqueue:      Wait queue for waiting for TX data, or for space in TX
0125  *          FIFO.
0126  * @lock:       Lock to protect FDCFG (interrupt enable).
0127  * @thread:     KThread for writing out data to FDC.
0128  * @reg:        FDC registers.
0129  * @tx_fifo:        TX FIFO size.
0130  * @xmit_size:      Size of each port's xmit buffer.
0131  * @xmit_total:     Total number of bytes (from all ports) to transmit.
0132  * @xmit_next:      Next port number to transmit from (round robin).
0133  * @xmit_full:      Indicates TX FIFO is full, we're waiting for space.
0134  * @irq:        IRQ number (negative if no IRQ).
0135  * @removing:       Indicates the device is being removed and @poll_timer
0136  *          should not be restarted.
0137  * @poll_timer:     Timer for polling for interrupt events when @irq < 0.
0138  * @sysrq_pressed:  Whether the magic sysrq key combination has been
0139  *          detected. See mips_ejtag_fdc_handle().
0140  */
0141 struct mips_ejtag_fdc_tty {
0142     struct device           *dev;
0143     struct tty_driver       *driver;
0144     unsigned int             cpu;
0145     char                 fdc_name[16];
0146     char                 driver_name[16];
0147     struct mips_ejtag_fdc_tty_port   ports[NUM_TTY_CHANNELS];
0148     wait_queue_head_t        waitqueue;
0149     raw_spinlock_t           lock;
0150     struct task_struct      *thread;
0151 
0152     void __iomem            *reg;
0153     u8               tx_fifo;
0154 
0155     unsigned int             xmit_size;
0156     atomic_t             xmit_total;
0157     unsigned int             xmit_next;
0158     bool                 xmit_full;
0159 
0160     int              irq;
0161     bool                 removing;
0162     struct timer_list        poll_timer;
0163 
0164 #ifdef CONFIG_MAGIC_SYSRQ
0165     bool                 sysrq_pressed;
0166 #endif
0167 };
0168 
0169 /* Hardware access */
0170 
0171 static inline void mips_ejtag_fdc_write(struct mips_ejtag_fdc_tty *priv,
0172                     unsigned int offs, unsigned int data)
0173 {
0174     __raw_writel(data, priv->reg + offs);
0175 }
0176 
0177 static inline unsigned int mips_ejtag_fdc_read(struct mips_ejtag_fdc_tty *priv,
0178                            unsigned int offs)
0179 {
0180     return __raw_readl(priv->reg + offs);
0181 }
0182 
0183 /* Encoding of byte stream in FDC words */
0184 
0185 /**
0186  * struct fdc_word - FDC word encoding some number of bytes of data.
0187  * @word:       Raw FDC word.
0188  * @bytes:      Number of bytes encoded by @word.
0189  */
0190 struct fdc_word {
0191     u32     word;
0192     unsigned int    bytes;
0193 };
0194 
0195 /*
0196  * This is a compact encoding which allows every 1 byte, 2 byte, and 3 byte
0197  * sequence to be encoded in a single word, while allowing the majority of 4
0198  * byte sequences (including all ASCII and common binary data) to be encoded in
0199  * a single word too.
0200  *    _______________________ _____________
0201  *   |       FDC Word        |             |
0202  *   |31-24|23-16|15-8 | 7-0 |    Bytes    |
0203  *   |_____|_____|_____|_____|_____________|
0204  *   |     |     |     |     |             |
0205  *   |0x80 |0x80 |0x80 |  WW | WW          |
0206  *   |0x81 |0x81 |  XX |  WW | WW XX       |
0207  *   |0x82 |  YY |  XX |  WW | WW XX YY    |
0208  *   |  ZZ |  YY |  XX |  WW | WW XX YY ZZ |
0209  *   |_____|_____|_____|_____|_____________|
0210  *
0211  * Note that the 4-byte encoding can only be used where none of the other 3
0212  * encodings match, otherwise it must fall back to the 3 byte encoding.
0213  */
0214 
0215 /* ranges >= 1 && sizes[0] >= 1 */
0216 static struct fdc_word mips_ejtag_fdc_encode(const char **ptrs,
0217                          unsigned int *sizes,
0218                          unsigned int ranges)
0219 {
0220     struct fdc_word word = { 0, 0 };
0221     const char **ptrs_end = ptrs + ranges;
0222 
0223     for (; ptrs < ptrs_end; ++ptrs) {
0224         const char *ptr = *(ptrs++);
0225         const char *end = ptr + *(sizes++);
0226 
0227         for (; ptr < end; ++ptr) {
0228             word.word |= (u8)*ptr << (8*word.bytes);
0229             ++word.bytes;
0230             if (word.bytes == 4)
0231                 goto done;
0232         }
0233     }
0234 done:
0235     /* Choose the appropriate encoding */
0236     switch (word.bytes) {
0237     case 4:
0238         /* 4 byte encoding, but don't match the 1-3 byte encodings */
0239         if ((word.word >> 8) != 0x808080 &&
0240             (word.word >> 16) != 0x8181 &&
0241             (word.word >> 24) != 0x82)
0242             break;
0243         /* Fall back to a 3 byte encoding */
0244         word.bytes = 3;
0245         word.word &= 0x00ffffff;
0246         fallthrough;
0247     case 3:
0248         /* 3 byte encoding */
0249         word.word |= 0x82000000;
0250         break;
0251     case 2:
0252         /* 2 byte encoding */
0253         word.word |= 0x81810000;
0254         break;
0255     case 1:
0256         /* 1 byte encoding */
0257         word.word |= 0x80808000;
0258         break;
0259     }
0260     return word;
0261 }
0262 
0263 static unsigned int mips_ejtag_fdc_decode(u32 word, char *buf)
0264 {
0265     buf[0] = (u8)word;
0266     word >>= 8;
0267     if (word == 0x808080)
0268         return 1;
0269     buf[1] = (u8)word;
0270     word >>= 8;
0271     if (word == 0x8181)
0272         return 2;
0273     buf[2] = (u8)word;
0274     word >>= 8;
0275     if (word == 0x82)
0276         return 3;
0277     buf[3] = (u8)word;
0278     return 4;
0279 }
0280 
0281 /* Console operations */
0282 
0283 /**
0284  * struct mips_ejtag_fdc_console - Wrapper struct for FDC consoles.
0285  * @cons:       Console object.
0286  * @tty_drv:        TTY driver associated with this console.
0287  * @lock:       Lock to protect concurrent access to other fields.
0288  *          This is raw because it may be used very early.
0289  * @initialised:    Whether the console is initialised.
0290  * @regs:       Registers base address for each CPU.
0291  */
0292 struct mips_ejtag_fdc_console {
0293     struct console       cons;
0294     struct tty_driver   *tty_drv;
0295     raw_spinlock_t       lock;
0296     bool             initialised;
0297     void __iomem        *regs[NR_CPUS];
0298 };
0299 
0300 /* Low level console write shared by early console and normal console */
0301 static void mips_ejtag_fdc_console_write(struct console *c, const char *s,
0302                      unsigned int count)
0303 {
0304     struct mips_ejtag_fdc_console *cons =
0305         container_of(c, struct mips_ejtag_fdc_console, cons);
0306     void __iomem *regs;
0307     struct fdc_word word;
0308     unsigned long flags;
0309     unsigned int i, buf_len, cpu;
0310     bool done_cr = false;
0311     char buf[4];
0312     const char *buf_ptr = buf;
0313     /* Number of bytes of input data encoded up to each byte in buf */
0314     u8 inc[4];
0315 
0316     local_irq_save(flags);
0317     cpu = smp_processor_id();
0318     regs = cons->regs[cpu];
0319     /* First console output on this CPU? */
0320     if (!regs) {
0321         regs = mips_cdmm_early_probe(0xfd);
0322         cons->regs[cpu] = regs;
0323     }
0324     /* Already tried and failed to find FDC on this CPU? */
0325     if (IS_ERR(regs))
0326         goto out;
0327     while (count) {
0328         /*
0329          * Copy the next few characters to a buffer so we can inject
0330          * carriage returns before newlines.
0331          */
0332         for (buf_len = 0, i = 0; buf_len < 4 && i < count; ++buf_len) {
0333             if (s[i] == '\n' && !done_cr) {
0334                 buf[buf_len] = '\r';
0335                 done_cr = true;
0336             } else {
0337                 buf[buf_len] = s[i];
0338                 done_cr = false;
0339                 ++i;
0340             }
0341             inc[buf_len] = i;
0342         }
0343         word = mips_ejtag_fdc_encode(&buf_ptr, &buf_len, 1);
0344         count -= inc[word.bytes - 1];
0345         s += inc[word.bytes - 1];
0346 
0347         /* Busy wait until there's space in fifo */
0348         while (__raw_readl(regs + REG_FDSTAT) & REG_FDSTAT_TXF)
0349             ;
0350         __raw_writel(word.word, regs + REG_FDTX(c->index));
0351     }
0352 out:
0353     local_irq_restore(flags);
0354 }
0355 
0356 static struct tty_driver *mips_ejtag_fdc_console_device(struct console *c,
0357                             int *index)
0358 {
0359     struct mips_ejtag_fdc_console *cons =
0360         container_of(c, struct mips_ejtag_fdc_console, cons);
0361 
0362     *index = c->index;
0363     return cons->tty_drv;
0364 }
0365 
0366 /* Initialise an FDC console (early or normal */
0367 static int __init mips_ejtag_fdc_console_init(struct mips_ejtag_fdc_console *c)
0368 {
0369     void __iomem *regs;
0370     unsigned long flags;
0371     int ret = 0;
0372 
0373     raw_spin_lock_irqsave(&c->lock, flags);
0374     /* Don't init twice */
0375     if (c->initialised)
0376         goto out;
0377     /* Look for the FDC device */
0378     regs = mips_cdmm_early_probe(0xfd);
0379     if (IS_ERR(regs)) {
0380         ret = PTR_ERR(regs);
0381         goto out;
0382     }
0383 
0384     c->initialised = true;
0385     c->regs[smp_processor_id()] = regs;
0386     register_console(&c->cons);
0387 out:
0388     raw_spin_unlock_irqrestore(&c->lock, flags);
0389     return ret;
0390 }
0391 
0392 static struct mips_ejtag_fdc_console mips_ejtag_fdc_con = {
0393     .cons   = {
0394         .name   = "fdc",
0395         .write  = mips_ejtag_fdc_console_write,
0396         .device = mips_ejtag_fdc_console_device,
0397         .flags  = CON_PRINTBUFFER,
0398         .index  = -1,
0399     },
0400     .lock   = __RAW_SPIN_LOCK_UNLOCKED(mips_ejtag_fdc_con.lock),
0401 };
0402 
0403 /* TTY RX/TX operations */
0404 
0405 /**
0406  * mips_ejtag_fdc_put_chan() - Write out a block of channel data.
0407  * @priv:   Pointer to driver private data.
0408  * @chan:   Channel number.
0409  *
0410  * Write a single block of data out to the debug adapter. If the circular buffer
0411  * is wrapped then only the first block is written.
0412  *
0413  * Returns: The number of bytes that were written.
0414  */
0415 static unsigned int mips_ejtag_fdc_put_chan(struct mips_ejtag_fdc_tty *priv,
0416                         unsigned int chan)
0417 {
0418     struct mips_ejtag_fdc_tty_port *dport;
0419     struct tty_struct *tty;
0420     const char *ptrs[2];
0421     unsigned int sizes[2] = { 0 };
0422     struct fdc_word word = { .bytes = 0 };
0423     unsigned long flags;
0424 
0425     dport = &priv->ports[chan];
0426     spin_lock(&dport->xmit_lock);
0427     if (dport->xmit_cnt) {
0428         ptrs[0] = dport->port.xmit_buf + dport->xmit_tail;
0429         sizes[0] = min_t(unsigned int,
0430                  priv->xmit_size - dport->xmit_tail,
0431                  dport->xmit_cnt);
0432         ptrs[1] = dport->port.xmit_buf;
0433         sizes[1] = dport->xmit_cnt - sizes[0];
0434         word = mips_ejtag_fdc_encode(ptrs, sizes, 1 + !!sizes[1]);
0435 
0436         dev_dbg(priv->dev, "%s%u: out %08x: \"%*pE%*pE\"\n",
0437             priv->driver_name, chan, word.word,
0438             min_t(int, word.bytes, sizes[0]), ptrs[0],
0439             max_t(int, 0, word.bytes - sizes[0]), ptrs[1]);
0440 
0441         local_irq_save(flags);
0442         /* Maybe we raced with the console and TX FIFO is full */
0443         if (mips_ejtag_fdc_read(priv, REG_FDSTAT) & REG_FDSTAT_TXF)
0444             word.bytes = 0;
0445         else
0446             mips_ejtag_fdc_write(priv, REG_FDTX(chan), word.word);
0447         local_irq_restore(flags);
0448 
0449         dport->xmit_cnt -= word.bytes;
0450         if (!dport->xmit_cnt) {
0451             /* Reset pointers to avoid wraps */
0452             dport->xmit_head = 0;
0453             dport->xmit_tail = 0;
0454             complete(&dport->xmit_empty);
0455         } else {
0456             dport->xmit_tail += word.bytes;
0457             if (dport->xmit_tail >= priv->xmit_size)
0458                 dport->xmit_tail -= priv->xmit_size;
0459         }
0460         atomic_sub(word.bytes, &priv->xmit_total);
0461     }
0462     spin_unlock(&dport->xmit_lock);
0463 
0464     /* If we've made more data available, wake up tty */
0465     if (sizes[0] && word.bytes) {
0466         tty = tty_port_tty_get(&dport->port);
0467         if (tty) {
0468             tty_wakeup(tty);
0469             tty_kref_put(tty);
0470         }
0471     }
0472 
0473     return word.bytes;
0474 }
0475 
0476 /**
0477  * mips_ejtag_fdc_put() - Kernel thread to write out channel data to FDC.
0478  * @arg:    Driver pointer.
0479  *
0480  * This kernel thread runs while @priv->xmit_total != 0, and round robins the
0481  * channels writing out blocks of buffered data to the FDC TX FIFO.
0482  */
0483 static int mips_ejtag_fdc_put(void *arg)
0484 {
0485     struct mips_ejtag_fdc_tty *priv = arg;
0486     struct mips_ejtag_fdc_tty_port *dport;
0487     unsigned int ret;
0488     u32 cfg;
0489 
0490     __set_current_state(TASK_RUNNING);
0491     while (!kthread_should_stop()) {
0492         /* Wait for data to actually write */
0493         wait_event_interruptible(priv->waitqueue,
0494                      atomic_read(&priv->xmit_total) ||
0495                      kthread_should_stop());
0496         if (kthread_should_stop())
0497             break;
0498 
0499         /* Wait for TX FIFO space to write data */
0500         raw_spin_lock_irq(&priv->lock);
0501         if (mips_ejtag_fdc_read(priv, REG_FDSTAT) & REG_FDSTAT_TXF) {
0502             priv->xmit_full = true;
0503             if (priv->irq >= 0) {
0504                 /* Enable TX interrupt */
0505                 cfg = mips_ejtag_fdc_read(priv, REG_FDCFG);
0506                 cfg &= ~REG_FDCFG_TXINTTHRES;
0507                 cfg |= REG_FDCFG_TXINTTHRES_NOTFULL;
0508                 mips_ejtag_fdc_write(priv, REG_FDCFG, cfg);
0509             }
0510         }
0511         raw_spin_unlock_irq(&priv->lock);
0512         wait_event_interruptible(priv->waitqueue,
0513                      !(mips_ejtag_fdc_read(priv, REG_FDSTAT)
0514                        & REG_FDSTAT_TXF) ||
0515                      kthread_should_stop());
0516         if (kthread_should_stop())
0517             break;
0518 
0519         /* Find next channel with data to output */
0520         for (;;) {
0521             dport = &priv->ports[priv->xmit_next];
0522             spin_lock(&dport->xmit_lock);
0523             ret = dport->xmit_cnt;
0524             spin_unlock(&dport->xmit_lock);
0525             if (ret)
0526                 break;
0527             /* Round robin */
0528             ++priv->xmit_next;
0529             if (priv->xmit_next >= NUM_TTY_CHANNELS)
0530                 priv->xmit_next = 0;
0531         }
0532 
0533         /* Try writing data to the chosen channel */
0534         ret = mips_ejtag_fdc_put_chan(priv, priv->xmit_next);
0535 
0536         /*
0537          * If anything was output, move on to the next channel so as not
0538          * to starve other channels.
0539          */
0540         if (ret) {
0541             ++priv->xmit_next;
0542             if (priv->xmit_next >= NUM_TTY_CHANNELS)
0543                 priv->xmit_next = 0;
0544         }
0545     }
0546 
0547     return 0;
0548 }
0549 
0550 /**
0551  * mips_ejtag_fdc_handle() - Handle FDC events.
0552  * @priv:   Pointer to driver private data.
0553  *
0554  * Handle FDC events, such as new incoming data which needs draining out of the
0555  * RX FIFO and feeding into the appropriate TTY ports, and space becoming
0556  * available in the TX FIFO which would allow more data to be written out.
0557  */
0558 static void mips_ejtag_fdc_handle(struct mips_ejtag_fdc_tty *priv)
0559 {
0560     struct mips_ejtag_fdc_tty_port *dport;
0561     unsigned int stat, channel, data, cfg, i, flipped;
0562     int len;
0563     char buf[4];
0564 
0565     for (;;) {
0566         /* Find which channel the next FDC word is destined for */
0567         stat = mips_ejtag_fdc_read(priv, REG_FDSTAT);
0568         if (stat & REG_FDSTAT_RXE)
0569             break;
0570         channel = (stat & REG_FDSTAT_RXCHAN) >> REG_FDSTAT_RXCHAN_SHIFT;
0571         dport = &priv->ports[channel];
0572 
0573         /* Read out the FDC word, decode it, and pass to tty layer */
0574         raw_spin_lock(&dport->rx_lock);
0575         data = mips_ejtag_fdc_read(priv, REG_FDRX);
0576 
0577         len = mips_ejtag_fdc_decode(data, buf);
0578         dev_dbg(priv->dev, "%s%u: in  %08x: \"%*pE\"\n",
0579             priv->driver_name, channel, data, len, buf);
0580 
0581         flipped = 0;
0582         for (i = 0; i < len; ++i) {
0583 #ifdef CONFIG_MAGIC_SYSRQ
0584 #ifdef CONFIG_MIPS_EJTAG_FDC_KGDB
0585             /* Support just Ctrl+C with KGDB channel */
0586             if (channel == CONFIG_MIPS_EJTAG_FDC_KGDB_CHAN) {
0587                 if (buf[i] == '\x03') { /* ^C */
0588                     handle_sysrq('g');
0589                     continue;
0590                 }
0591             }
0592 #endif
0593             /* Support Ctrl+O for console channel */
0594             if (channel == mips_ejtag_fdc_con.cons.index) {
0595                 if (buf[i] == '\x0f') { /* ^O */
0596                     priv->sysrq_pressed =
0597                         !priv->sysrq_pressed;
0598                     if (priv->sysrq_pressed)
0599                         continue;
0600                 } else if (priv->sysrq_pressed) {
0601                     handle_sysrq(buf[i]);
0602                     priv->sysrq_pressed = false;
0603                     continue;
0604                 }
0605             }
0606 #endif /* CONFIG_MAGIC_SYSRQ */
0607 
0608             /* Check the port isn't being shut down */
0609             if (!dport->rx_buf)
0610                 continue;
0611 
0612             flipped += tty_insert_flip_char(&dport->port, buf[i],
0613                             TTY_NORMAL);
0614         }
0615         if (flipped)
0616             tty_flip_buffer_push(&dport->port);
0617 
0618         raw_spin_unlock(&dport->rx_lock);
0619     }
0620 
0621     /* If TX FIFO no longer full we may be able to write more data */
0622     raw_spin_lock(&priv->lock);
0623     if (priv->xmit_full && !(stat & REG_FDSTAT_TXF)) {
0624         priv->xmit_full = false;
0625 
0626         /* Disable TX interrupt */
0627         cfg = mips_ejtag_fdc_read(priv, REG_FDCFG);
0628         cfg &= ~REG_FDCFG_TXINTTHRES;
0629         cfg |= REG_FDCFG_TXINTTHRES_DISABLED;
0630         mips_ejtag_fdc_write(priv, REG_FDCFG, cfg);
0631 
0632         /* Wait the kthread so it can try writing more data */
0633         wake_up_interruptible(&priv->waitqueue);
0634     }
0635     raw_spin_unlock(&priv->lock);
0636 }
0637 
0638 /**
0639  * mips_ejtag_fdc_isr() - Interrupt handler.
0640  * @irq:    IRQ number.
0641  * @dev_id: Pointer to driver private data.
0642  *
0643  * This is the interrupt handler, used when interrupts are enabled.
0644  *
0645  * It simply triggers the common FDC handler code.
0646  *
0647  * Returns: IRQ_HANDLED if an FDC interrupt was pending.
0648  *      IRQ_NONE otherwise.
0649  */
0650 static irqreturn_t mips_ejtag_fdc_isr(int irq, void *dev_id)
0651 {
0652     struct mips_ejtag_fdc_tty *priv = dev_id;
0653 
0654     /*
0655      * We're not using proper per-cpu IRQs, so we must be careful not to
0656      * handle IRQs on CPUs we're not interested in.
0657      *
0658      * Ideally proper per-cpu IRQ handlers could be used, but that doesn't
0659      * fit well with the whole sharing of the main CPU IRQ lines. When we
0660      * have something with a GIC that routes the FDC IRQs (i.e. no sharing
0661      * between handlers) then support could be added more easily.
0662      */
0663     if (smp_processor_id() != priv->cpu)
0664         return IRQ_NONE;
0665 
0666     /* If no FDC interrupt pending, it wasn't for us */
0667     if (!(read_c0_cause() & CAUSEF_FDCI))
0668         return IRQ_NONE;
0669 
0670     mips_ejtag_fdc_handle(priv);
0671     return IRQ_HANDLED;
0672 }
0673 
0674 /**
0675  * mips_ejtag_fdc_tty_timer() - Poll FDC for incoming data.
0676  * @opaque: Pointer to driver private data.
0677  *
0678  * This is the timer handler for when interrupts are disabled and polling the
0679  * FDC state is required.
0680  *
0681  * It simply triggers the common FDC handler code and arranges for further
0682  * polling.
0683  */
0684 static void mips_ejtag_fdc_tty_timer(struct timer_list *t)
0685 {
0686     struct mips_ejtag_fdc_tty *priv = from_timer(priv, t, poll_timer);
0687 
0688     mips_ejtag_fdc_handle(priv);
0689     if (!priv->removing)
0690         mod_timer(&priv->poll_timer, jiffies + FDC_TTY_POLL);
0691 }
0692 
0693 /* TTY Port operations */
0694 
0695 static int mips_ejtag_fdc_tty_port_activate(struct tty_port *port,
0696                         struct tty_struct *tty)
0697 {
0698     struct mips_ejtag_fdc_tty_port *dport =
0699         container_of(port, struct mips_ejtag_fdc_tty_port, port);
0700     void *rx_buf;
0701 
0702     /* Allocate the buffer we use for writing data */
0703     if (tty_port_alloc_xmit_buf(port) < 0)
0704         goto err;
0705 
0706     /* Allocate the buffer we use for reading data */
0707     rx_buf = kzalloc(RX_BUF_SIZE, GFP_KERNEL);
0708     if (!rx_buf)
0709         goto err_free_xmit;
0710 
0711     raw_spin_lock_irq(&dport->rx_lock);
0712     dport->rx_buf = rx_buf;
0713     raw_spin_unlock_irq(&dport->rx_lock);
0714 
0715     return 0;
0716 err_free_xmit:
0717     tty_port_free_xmit_buf(port);
0718 err:
0719     return -ENOMEM;
0720 }
0721 
0722 static void mips_ejtag_fdc_tty_port_shutdown(struct tty_port *port)
0723 {
0724     struct mips_ejtag_fdc_tty_port *dport =
0725         container_of(port, struct mips_ejtag_fdc_tty_port, port);
0726     struct mips_ejtag_fdc_tty *priv = dport->driver;
0727     void *rx_buf;
0728     unsigned int count;
0729 
0730     spin_lock(&dport->xmit_lock);
0731     count = dport->xmit_cnt;
0732     spin_unlock(&dport->xmit_lock);
0733     if (count) {
0734         /*
0735          * There's still data to write out, so wake and wait for the
0736          * writer thread to drain the buffer.
0737          */
0738         wake_up_interruptible(&priv->waitqueue);
0739         wait_for_completion(&dport->xmit_empty);
0740     }
0741 
0742     /* Null the read buffer (timer could still be running!) */
0743     raw_spin_lock_irq(&dport->rx_lock);
0744     rx_buf = dport->rx_buf;
0745     dport->rx_buf = NULL;
0746     raw_spin_unlock_irq(&dport->rx_lock);
0747     /* Free the read buffer */
0748     kfree(rx_buf);
0749 
0750     /* Free the write buffer */
0751     tty_port_free_xmit_buf(port);
0752 }
0753 
0754 static const struct tty_port_operations mips_ejtag_fdc_tty_port_ops = {
0755     .activate   = mips_ejtag_fdc_tty_port_activate,
0756     .shutdown   = mips_ejtag_fdc_tty_port_shutdown,
0757 };
0758 
0759 /* TTY operations */
0760 
0761 static int mips_ejtag_fdc_tty_install(struct tty_driver *driver,
0762                       struct tty_struct *tty)
0763 {
0764     struct mips_ejtag_fdc_tty *priv = driver->driver_state;
0765 
0766     tty->driver_data = &priv->ports[tty->index];
0767     return tty_port_install(&priv->ports[tty->index].port, driver, tty);
0768 }
0769 
0770 static int mips_ejtag_fdc_tty_open(struct tty_struct *tty, struct file *filp)
0771 {
0772     return tty_port_open(tty->port, tty, filp);
0773 }
0774 
0775 static void mips_ejtag_fdc_tty_close(struct tty_struct *tty, struct file *filp)
0776 {
0777     return tty_port_close(tty->port, tty, filp);
0778 }
0779 
0780 static void mips_ejtag_fdc_tty_hangup(struct tty_struct *tty)
0781 {
0782     struct mips_ejtag_fdc_tty_port *dport = tty->driver_data;
0783     struct mips_ejtag_fdc_tty *priv = dport->driver;
0784 
0785     /* Drop any data in the xmit buffer */
0786     spin_lock(&dport->xmit_lock);
0787     if (dport->xmit_cnt) {
0788         atomic_sub(dport->xmit_cnt, &priv->xmit_total);
0789         dport->xmit_cnt = 0;
0790         dport->xmit_head = 0;
0791         dport->xmit_tail = 0;
0792         complete(&dport->xmit_empty);
0793     }
0794     spin_unlock(&dport->xmit_lock);
0795 
0796     tty_port_hangup(tty->port);
0797 }
0798 
0799 static int mips_ejtag_fdc_tty_write(struct tty_struct *tty,
0800                     const unsigned char *buf, int total)
0801 {
0802     int count, block;
0803     struct mips_ejtag_fdc_tty_port *dport = tty->driver_data;
0804     struct mips_ejtag_fdc_tty *priv = dport->driver;
0805 
0806     /*
0807      * Write to output buffer.
0808      *
0809      * The reason that we asynchronously write the buffer is because if we
0810      * were to write the buffer synchronously then because the channels are
0811      * per-CPU the buffer would be written to the channel of whatever CPU
0812      * we're running on.
0813      *
0814      * What we actually want to happen is have all input and output done on
0815      * one CPU.
0816      */
0817     spin_lock(&dport->xmit_lock);
0818     /* Work out how many bytes we can write to the xmit buffer */
0819     total = min(total, (int)(priv->xmit_size - dport->xmit_cnt));
0820     atomic_add(total, &priv->xmit_total);
0821     dport->xmit_cnt += total;
0822     /* Write the actual bytes (may need splitting if it wraps) */
0823     for (count = total; count; count -= block) {
0824         block = min(count, (int)(priv->xmit_size - dport->xmit_head));
0825         memcpy(dport->port.xmit_buf + dport->xmit_head, buf, block);
0826         dport->xmit_head += block;
0827         if (dport->xmit_head >= priv->xmit_size)
0828             dport->xmit_head -= priv->xmit_size;
0829         buf += block;
0830     }
0831     count = dport->xmit_cnt;
0832     /* Xmit buffer no longer empty? */
0833     if (count)
0834         reinit_completion(&dport->xmit_empty);
0835     spin_unlock(&dport->xmit_lock);
0836 
0837     /* Wake up the kthread */
0838     if (total)
0839         wake_up_interruptible(&priv->waitqueue);
0840     return total;
0841 }
0842 
0843 static unsigned int mips_ejtag_fdc_tty_write_room(struct tty_struct *tty)
0844 {
0845     struct mips_ejtag_fdc_tty_port *dport = tty->driver_data;
0846     struct mips_ejtag_fdc_tty *priv = dport->driver;
0847     unsigned int room;
0848 
0849     /* Report the space in the xmit buffer */
0850     spin_lock(&dport->xmit_lock);
0851     room = priv->xmit_size - dport->xmit_cnt;
0852     spin_unlock(&dport->xmit_lock);
0853 
0854     return room;
0855 }
0856 
0857 static unsigned int mips_ejtag_fdc_tty_chars_in_buffer(struct tty_struct *tty)
0858 {
0859     struct mips_ejtag_fdc_tty_port *dport = tty->driver_data;
0860     unsigned int chars;
0861 
0862     /* Report the number of bytes in the xmit buffer */
0863     spin_lock(&dport->xmit_lock);
0864     chars = dport->xmit_cnt;
0865     spin_unlock(&dport->xmit_lock);
0866 
0867     return chars;
0868 }
0869 
0870 static const struct tty_operations mips_ejtag_fdc_tty_ops = {
0871     .install        = mips_ejtag_fdc_tty_install,
0872     .open           = mips_ejtag_fdc_tty_open,
0873     .close          = mips_ejtag_fdc_tty_close,
0874     .hangup         = mips_ejtag_fdc_tty_hangup,
0875     .write          = mips_ejtag_fdc_tty_write,
0876     .write_room     = mips_ejtag_fdc_tty_write_room,
0877     .chars_in_buffer    = mips_ejtag_fdc_tty_chars_in_buffer,
0878 };
0879 
0880 int __weak get_c0_fdc_int(void)
0881 {
0882     return -1;
0883 }
0884 
0885 static int mips_ejtag_fdc_tty_probe(struct mips_cdmm_device *dev)
0886 {
0887     int ret, nport;
0888     struct mips_ejtag_fdc_tty_port *dport;
0889     struct mips_ejtag_fdc_tty *priv;
0890     struct tty_driver *driver;
0891     unsigned int cfg, tx_fifo;
0892 
0893     priv = devm_kzalloc(&dev->dev, sizeof(*priv), GFP_KERNEL);
0894     if (!priv)
0895         return -ENOMEM;
0896     priv->cpu = dev->cpu;
0897     priv->dev = &dev->dev;
0898     mips_cdmm_set_drvdata(dev, priv);
0899     atomic_set(&priv->xmit_total, 0);
0900     raw_spin_lock_init(&priv->lock);
0901 
0902     priv->reg = devm_ioremap(priv->dev, dev->res.start,
0903                      resource_size(&dev->res));
0904     if (!priv->reg) {
0905         dev_err(priv->dev, "ioremap failed for resource %pR\n",
0906             &dev->res);
0907         return -ENOMEM;
0908     }
0909 
0910     cfg = mips_ejtag_fdc_read(priv, REG_FDCFG);
0911     tx_fifo = (cfg & REG_FDCFG_TXFIFOSIZE) >> REG_FDCFG_TXFIFOSIZE_SHIFT;
0912     /* Disable interrupts */
0913     cfg &= ~(REG_FDCFG_TXINTTHRES | REG_FDCFG_RXINTTHRES);
0914     cfg |= REG_FDCFG_TXINTTHRES_DISABLED;
0915     cfg |= REG_FDCFG_RXINTTHRES_DISABLED;
0916     mips_ejtag_fdc_write(priv, REG_FDCFG, cfg);
0917 
0918     /* Make each port's xmit FIFO big enough to fill FDC TX FIFO */
0919     priv->xmit_size = min(tx_fifo * 4, (unsigned int)UART_XMIT_SIZE);
0920 
0921     driver = tty_alloc_driver(NUM_TTY_CHANNELS, TTY_DRIVER_REAL_RAW);
0922     if (IS_ERR(driver))
0923         return PTR_ERR(driver);
0924     priv->driver = driver;
0925 
0926     driver->driver_name = "ejtag_fdc";
0927     snprintf(priv->fdc_name, sizeof(priv->fdc_name), "ttyFDC%u", dev->cpu);
0928     snprintf(priv->driver_name, sizeof(priv->driver_name), "%sc",
0929          priv->fdc_name);
0930     driver->name = priv->driver_name;
0931     driver->major = 0; /* Auto-allocate */
0932     driver->minor_start = 0;
0933     driver->type = TTY_DRIVER_TYPE_SERIAL;
0934     driver->subtype = SERIAL_TYPE_NORMAL;
0935     driver->init_termios = tty_std_termios;
0936     driver->init_termios.c_cflag |= CLOCAL;
0937     driver->driver_state = priv;
0938 
0939     tty_set_operations(driver, &mips_ejtag_fdc_tty_ops);
0940     for (nport = 0; nport < NUM_TTY_CHANNELS; nport++) {
0941         dport = &priv->ports[nport];
0942         dport->driver = priv;
0943         tty_port_init(&dport->port);
0944         dport->port.ops = &mips_ejtag_fdc_tty_port_ops;
0945         raw_spin_lock_init(&dport->rx_lock);
0946         spin_lock_init(&dport->xmit_lock);
0947         /* The xmit buffer starts empty, i.e. completely written */
0948         init_completion(&dport->xmit_empty);
0949         complete(&dport->xmit_empty);
0950     }
0951 
0952     /* Set up the console */
0953     mips_ejtag_fdc_con.regs[dev->cpu] = priv->reg;
0954     if (dev->cpu == 0)
0955         mips_ejtag_fdc_con.tty_drv = driver;
0956 
0957     init_waitqueue_head(&priv->waitqueue);
0958     /*
0959      * Bind the writer thread to the right CPU so it can't migrate.
0960      * The channels are per-CPU and we want all channel I/O to be on a
0961      * single predictable CPU.
0962      */
0963     priv->thread = kthread_run_on_cpu(mips_ejtag_fdc_put, priv,
0964                       dev->cpu, "ttyFDC/%u");
0965     if (IS_ERR(priv->thread)) {
0966         ret = PTR_ERR(priv->thread);
0967         dev_err(priv->dev, "Couldn't create kthread (%d)\n", ret);
0968         goto err_destroy_ports;
0969     }
0970 
0971     /* Look for an FDC IRQ */
0972     priv->irq = get_c0_fdc_int();
0973 
0974     /* Try requesting the IRQ */
0975     if (priv->irq >= 0) {
0976         /*
0977          * IRQF_SHARED, IRQF_COND_SUSPEND: The FDC IRQ may be shared with
0978          * other local interrupts such as the timer which sets
0979          * IRQF_TIMER (including IRQF_NO_SUSPEND).
0980          *
0981          * IRQF_NO_THREAD: The FDC IRQ isn't individually maskable so it
0982          * cannot be deferred and handled by a thread on RT kernels. For
0983          * this reason any spinlocks used from the ISR are raw.
0984          */
0985         ret = devm_request_irq(priv->dev, priv->irq, mips_ejtag_fdc_isr,
0986                        IRQF_PERCPU | IRQF_SHARED |
0987                        IRQF_NO_THREAD | IRQF_COND_SUSPEND,
0988                        priv->fdc_name, priv);
0989         if (ret)
0990             priv->irq = -1;
0991     }
0992     if (priv->irq >= 0) {
0993         /* IRQ is usable, enable RX interrupt */
0994         raw_spin_lock_irq(&priv->lock);
0995         cfg = mips_ejtag_fdc_read(priv, REG_FDCFG);
0996         cfg &= ~REG_FDCFG_RXINTTHRES;
0997         cfg |= REG_FDCFG_RXINTTHRES_NOTEMPTY;
0998         mips_ejtag_fdc_write(priv, REG_FDCFG, cfg);
0999         raw_spin_unlock_irq(&priv->lock);
1000     } else {
1001         /* If we didn't get an usable IRQ, poll instead */
1002         timer_setup(&priv->poll_timer, mips_ejtag_fdc_tty_timer,
1003                 TIMER_PINNED);
1004         priv->poll_timer.expires = jiffies + FDC_TTY_POLL;
1005         /*
1006          * Always attach the timer to the right CPU. The channels are
1007          * per-CPU so all polling should be from a single CPU.
1008          */
1009         add_timer_on(&priv->poll_timer, dev->cpu);
1010 
1011         dev_info(priv->dev, "No usable IRQ, polling enabled\n");
1012     }
1013 
1014     ret = tty_register_driver(driver);
1015     if (ret < 0) {
1016         dev_err(priv->dev, "Couldn't install tty driver (%d)\n", ret);
1017         goto err_stop_irq;
1018     }
1019 
1020     return 0;
1021 
1022 err_stop_irq:
1023     if (priv->irq >= 0) {
1024         raw_spin_lock_irq(&priv->lock);
1025         cfg = mips_ejtag_fdc_read(priv, REG_FDCFG);
1026         /* Disable interrupts */
1027         cfg &= ~(REG_FDCFG_TXINTTHRES | REG_FDCFG_RXINTTHRES);
1028         cfg |= REG_FDCFG_TXINTTHRES_DISABLED;
1029         cfg |= REG_FDCFG_RXINTTHRES_DISABLED;
1030         mips_ejtag_fdc_write(priv, REG_FDCFG, cfg);
1031         raw_spin_unlock_irq(&priv->lock);
1032     } else {
1033         priv->removing = true;
1034         del_timer_sync(&priv->poll_timer);
1035     }
1036     kthread_stop(priv->thread);
1037 err_destroy_ports:
1038     if (dev->cpu == 0)
1039         mips_ejtag_fdc_con.tty_drv = NULL;
1040     for (nport = 0; nport < NUM_TTY_CHANNELS; nport++) {
1041         dport = &priv->ports[nport];
1042         tty_port_destroy(&dport->port);
1043     }
1044     tty_driver_kref_put(priv->driver);
1045     return ret;
1046 }
1047 
1048 static int mips_ejtag_fdc_tty_cpu_down(struct mips_cdmm_device *dev)
1049 {
1050     struct mips_ejtag_fdc_tty *priv = mips_cdmm_get_drvdata(dev);
1051     unsigned int cfg;
1052 
1053     if (priv->irq >= 0) {
1054         raw_spin_lock_irq(&priv->lock);
1055         cfg = mips_ejtag_fdc_read(priv, REG_FDCFG);
1056         /* Disable interrupts */
1057         cfg &= ~(REG_FDCFG_TXINTTHRES | REG_FDCFG_RXINTTHRES);
1058         cfg |= REG_FDCFG_TXINTTHRES_DISABLED;
1059         cfg |= REG_FDCFG_RXINTTHRES_DISABLED;
1060         mips_ejtag_fdc_write(priv, REG_FDCFG, cfg);
1061         raw_spin_unlock_irq(&priv->lock);
1062     } else {
1063         priv->removing = true;
1064         del_timer_sync(&priv->poll_timer);
1065     }
1066     kthread_stop(priv->thread);
1067 
1068     return 0;
1069 }
1070 
1071 static int mips_ejtag_fdc_tty_cpu_up(struct mips_cdmm_device *dev)
1072 {
1073     struct mips_ejtag_fdc_tty *priv = mips_cdmm_get_drvdata(dev);
1074     unsigned int cfg;
1075     int ret = 0;
1076 
1077     if (priv->irq >= 0) {
1078         /*
1079          * IRQ is usable, enable RX interrupt
1080          * This must be before kthread is restarted, as kthread may
1081          * enable TX interrupt.
1082          */
1083         raw_spin_lock_irq(&priv->lock);
1084         cfg = mips_ejtag_fdc_read(priv, REG_FDCFG);
1085         cfg &= ~(REG_FDCFG_TXINTTHRES | REG_FDCFG_RXINTTHRES);
1086         cfg |= REG_FDCFG_TXINTTHRES_DISABLED;
1087         cfg |= REG_FDCFG_RXINTTHRES_NOTEMPTY;
1088         mips_ejtag_fdc_write(priv, REG_FDCFG, cfg);
1089         raw_spin_unlock_irq(&priv->lock);
1090     } else {
1091         /* Restart poll timer */
1092         priv->removing = false;
1093         add_timer_on(&priv->poll_timer, dev->cpu);
1094     }
1095 
1096     /* Restart the kthread */
1097     /* Bind it back to the right CPU and set it off */
1098     priv->thread = kthread_run_on_cpu(mips_ejtag_fdc_put, priv,
1099                       dev->cpu, "ttyFDC/%u");
1100     if (IS_ERR(priv->thread)) {
1101         ret = PTR_ERR(priv->thread);
1102         dev_err(priv->dev, "Couldn't re-create kthread (%d)\n", ret);
1103         goto out;
1104     }
1105 out:
1106     return ret;
1107 }
1108 
1109 static const struct mips_cdmm_device_id mips_ejtag_fdc_tty_ids[] = {
1110     { .type = 0xfd },
1111     { }
1112 };
1113 
1114 static struct mips_cdmm_driver mips_ejtag_fdc_tty_driver = {
1115     .drv        = {
1116         .name   = "mips_ejtag_fdc",
1117     },
1118     .probe      = mips_ejtag_fdc_tty_probe,
1119     .cpu_down   = mips_ejtag_fdc_tty_cpu_down,
1120     .cpu_up     = mips_ejtag_fdc_tty_cpu_up,
1121     .id_table   = mips_ejtag_fdc_tty_ids,
1122 };
1123 builtin_mips_cdmm_driver(mips_ejtag_fdc_tty_driver);
1124 
1125 static int __init mips_ejtag_fdc_init_console(void)
1126 {
1127     return mips_ejtag_fdc_console_init(&mips_ejtag_fdc_con);
1128 }
1129 console_initcall(mips_ejtag_fdc_init_console);
1130 
1131 #ifdef CONFIG_MIPS_EJTAG_FDC_EARLYCON
1132 static struct mips_ejtag_fdc_console mips_ejtag_fdc_earlycon = {
1133     .cons   = {
1134         .name   = "early_fdc",
1135         .write  = mips_ejtag_fdc_console_write,
1136         .flags  = CON_PRINTBUFFER | CON_BOOT,
1137         .index  = CONSOLE_CHANNEL,
1138     },
1139     .lock   = __RAW_SPIN_LOCK_UNLOCKED(mips_ejtag_fdc_earlycon.lock),
1140 };
1141 
1142 int __init setup_early_fdc_console(void)
1143 {
1144     return mips_ejtag_fdc_console_init(&mips_ejtag_fdc_earlycon);
1145 }
1146 #endif
1147 
1148 #ifdef CONFIG_MIPS_EJTAG_FDC_KGDB
1149 
1150 /* read buffer to allow decompaction */
1151 static unsigned int kgdbfdc_rbuflen;
1152 static unsigned int kgdbfdc_rpos;
1153 static char kgdbfdc_rbuf[4];
1154 
1155 /* write buffer to allow compaction */
1156 static unsigned int kgdbfdc_wbuflen;
1157 static char kgdbfdc_wbuf[4];
1158 
1159 static void __iomem *kgdbfdc_setup(void)
1160 {
1161     void __iomem *regs;
1162     unsigned int cpu;
1163 
1164     /* Find address, piggy backing off console percpu regs */
1165     cpu = smp_processor_id();
1166     regs = mips_ejtag_fdc_con.regs[cpu];
1167     /* First console output on this CPU? */
1168     if (!regs) {
1169         regs = mips_cdmm_early_probe(0xfd);
1170         mips_ejtag_fdc_con.regs[cpu] = regs;
1171     }
1172     /* Already tried and failed to find FDC on this CPU? */
1173     if (IS_ERR(regs))
1174         return regs;
1175 
1176     return regs;
1177 }
1178 
1179 /* read a character from the read buffer, filling from FDC RX FIFO */
1180 static int kgdbfdc_read_char(void)
1181 {
1182     unsigned int stat, channel, data;
1183     void __iomem *regs;
1184 
1185     /* No more data, try and read another FDC word from RX FIFO */
1186     if (kgdbfdc_rpos >= kgdbfdc_rbuflen) {
1187         kgdbfdc_rpos = 0;
1188         kgdbfdc_rbuflen = 0;
1189 
1190         regs = kgdbfdc_setup();
1191         if (IS_ERR(regs))
1192             return NO_POLL_CHAR;
1193 
1194         /* Read next word from KGDB channel */
1195         do {
1196             stat = __raw_readl(regs + REG_FDSTAT);
1197 
1198             /* No data waiting? */
1199             if (stat & REG_FDSTAT_RXE)
1200                 return NO_POLL_CHAR;
1201 
1202             /* Read next word */
1203             channel = (stat & REG_FDSTAT_RXCHAN) >>
1204                     REG_FDSTAT_RXCHAN_SHIFT;
1205             data = __raw_readl(regs + REG_FDRX);
1206         } while (channel != CONFIG_MIPS_EJTAG_FDC_KGDB_CHAN);
1207 
1208         /* Decode into rbuf */
1209         kgdbfdc_rbuflen = mips_ejtag_fdc_decode(data, kgdbfdc_rbuf);
1210     }
1211     pr_devel("kgdbfdc r %c\n", kgdbfdc_rbuf[kgdbfdc_rpos]);
1212     return kgdbfdc_rbuf[kgdbfdc_rpos++];
1213 }
1214 
1215 /* push an FDC word from write buffer to TX FIFO */
1216 static void kgdbfdc_push_one(void)
1217 {
1218     const char *bufs[1] = { kgdbfdc_wbuf };
1219     struct fdc_word word;
1220     void __iomem *regs;
1221     unsigned int i;
1222 
1223     /* Construct a word from any data in buffer */
1224     word = mips_ejtag_fdc_encode(bufs, &kgdbfdc_wbuflen, 1);
1225     /* Relocate any remaining data to beginning of buffer */
1226     kgdbfdc_wbuflen -= word.bytes;
1227     for (i = 0; i < kgdbfdc_wbuflen; ++i)
1228         kgdbfdc_wbuf[i] = kgdbfdc_wbuf[i + word.bytes];
1229 
1230     regs = kgdbfdc_setup();
1231     if (IS_ERR(regs))
1232         return;
1233 
1234     /* Busy wait until there's space in fifo */
1235     while (__raw_readl(regs + REG_FDSTAT) & REG_FDSTAT_TXF)
1236         ;
1237     __raw_writel(word.word,
1238              regs + REG_FDTX(CONFIG_MIPS_EJTAG_FDC_KGDB_CHAN));
1239 }
1240 
1241 /* flush the whole write buffer to the TX FIFO */
1242 static void kgdbfdc_flush(void)
1243 {
1244     while (kgdbfdc_wbuflen)
1245         kgdbfdc_push_one();
1246 }
1247 
1248 /* write a character into the write buffer, writing out if full */
1249 static void kgdbfdc_write_char(u8 chr)
1250 {
1251     pr_devel("kgdbfdc w %c\n", chr);
1252     kgdbfdc_wbuf[kgdbfdc_wbuflen++] = chr;
1253     if (kgdbfdc_wbuflen >= sizeof(kgdbfdc_wbuf))
1254         kgdbfdc_push_one();
1255 }
1256 
1257 static struct kgdb_io kgdbfdc_io_ops = {
1258     .name       = "kgdbfdc",
1259     .read_char  = kgdbfdc_read_char,
1260     .write_char = kgdbfdc_write_char,
1261     .flush      = kgdbfdc_flush,
1262 };
1263 
1264 static int __init kgdbfdc_init(void)
1265 {
1266     kgdb_register_io_module(&kgdbfdc_io_ops);
1267     return 0;
1268 }
1269 early_initcall(kgdbfdc_init);
1270 #endif