0001
0002
0003
0004
0005
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
0035 #define REG_FDACSR 0x00
0036 #define REG_FDCFG 0x08
0037 #define REG_FDSTAT 0x10
0038 #define REG_FDRX 0x18
0039 #define REG_FDTX(N) (0x20+0x8*(N))
0040
0041
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)
0067 #define REG_FDSTAT_RXF BIT(2)
0068 #define REG_FDSTAT_TXE BIT(1)
0069 #define REG_FDSTAT_TXF BIT(0)
0070
0071
0072 #define CONSOLE_CHANNEL 1
0073
0074 #define NUM_TTY_CHANNELS 16
0075
0076 #define RX_BUF_SIZE 1024
0077
0078
0079
0080
0081
0082 #define FDC_TTY_POLL (HZ / 50)
0083
0084 struct mips_ejtag_fdc_tty;
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
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
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
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
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
0184
0185
0186
0187
0188
0189
0190 struct fdc_word {
0191 u32 word;
0192 unsigned int bytes;
0193 };
0194
0195
0196
0197
0198
0199
0200
0201
0202
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212
0213
0214
0215
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
0236 switch (word.bytes) {
0237 case 4:
0238
0239 if ((word.word >> 8) != 0x808080 &&
0240 (word.word >> 16) != 0x8181 &&
0241 (word.word >> 24) != 0x82)
0242 break;
0243
0244 word.bytes = 3;
0245 word.word &= 0x00ffffff;
0246 fallthrough;
0247 case 3:
0248
0249 word.word |= 0x82000000;
0250 break;
0251 case 2:
0252
0253 word.word |= 0x81810000;
0254 break;
0255 case 1:
0256
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
0282
0283
0284
0285
0286
0287
0288
0289
0290
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
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
0314 u8 inc[4];
0315
0316 local_irq_save(flags);
0317 cpu = smp_processor_id();
0318 regs = cons->regs[cpu];
0319
0320 if (!regs) {
0321 regs = mips_cdmm_early_probe(0xfd);
0322 cons->regs[cpu] = regs;
0323 }
0324
0325 if (IS_ERR(regs))
0326 goto out;
0327 while (count) {
0328
0329
0330
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
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
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
0375 if (c->initialised)
0376 goto out;
0377
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
0404
0405
0406
0407
0408
0409
0410
0411
0412
0413
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
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
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
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
0478
0479
0480
0481
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
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
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
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
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
0528 ++priv->xmit_next;
0529 if (priv->xmit_next >= NUM_TTY_CHANNELS)
0530 priv->xmit_next = 0;
0531 }
0532
0533
0534 ret = mips_ejtag_fdc_put_chan(priv, priv->xmit_next);
0535
0536
0537
0538
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
0552
0553
0554
0555
0556
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
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
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
0586 if (channel == CONFIG_MIPS_EJTAG_FDC_KGDB_CHAN) {
0587 if (buf[i] == '\x03') {
0588 handle_sysrq('g');
0589 continue;
0590 }
0591 }
0592 #endif
0593
0594 if (channel == mips_ejtag_fdc_con.cons.index) {
0595 if (buf[i] == '\x0f') {
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
0607
0608
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
0622 raw_spin_lock(&priv->lock);
0623 if (priv->xmit_full && !(stat & REG_FDSTAT_TXF)) {
0624 priv->xmit_full = false;
0625
0626
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
0633 wake_up_interruptible(&priv->waitqueue);
0634 }
0635 raw_spin_unlock(&priv->lock);
0636 }
0637
0638
0639
0640
0641
0642
0643
0644
0645
0646
0647
0648
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
0656
0657
0658
0659
0660
0661
0662
0663 if (smp_processor_id() != priv->cpu)
0664 return IRQ_NONE;
0665
0666
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
0676
0677
0678
0679
0680
0681
0682
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
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
0703 if (tty_port_alloc_xmit_buf(port) < 0)
0704 goto err;
0705
0706
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
0736
0737
0738 wake_up_interruptible(&priv->waitqueue);
0739 wait_for_completion(&dport->xmit_empty);
0740 }
0741
0742
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
0748 kfree(rx_buf);
0749
0750
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
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
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
0808
0809
0810
0811
0812
0813
0814
0815
0816
0817 spin_lock(&dport->xmit_lock);
0818
0819 total = min(total, (int)(priv->xmit_size - dport->xmit_cnt));
0820 atomic_add(total, &priv->xmit_total);
0821 dport->xmit_cnt += total;
0822
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
0833 if (count)
0834 reinit_completion(&dport->xmit_empty);
0835 spin_unlock(&dport->xmit_lock);
0836
0837
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
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
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
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
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;
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
0948 init_completion(&dport->xmit_empty);
0949 complete(&dport->xmit_empty);
0950 }
0951
0952
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
0960
0961
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
0972 priv->irq = get_c0_fdc_int();
0973
0974
0975 if (priv->irq >= 0) {
0976
0977
0978
0979
0980
0981
0982
0983
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
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
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
1007
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
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
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
1080
1081
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
1092 priv->removing = false;
1093 add_timer_on(&priv->poll_timer, dev->cpu);
1094 }
1095
1096
1097
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
1151 static unsigned int kgdbfdc_rbuflen;
1152 static unsigned int kgdbfdc_rpos;
1153 static char kgdbfdc_rbuf[4];
1154
1155
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
1165 cpu = smp_processor_id();
1166 regs = mips_ejtag_fdc_con.regs[cpu];
1167
1168 if (!regs) {
1169 regs = mips_cdmm_early_probe(0xfd);
1170 mips_ejtag_fdc_con.regs[cpu] = regs;
1171 }
1172
1173 if (IS_ERR(regs))
1174 return regs;
1175
1176 return regs;
1177 }
1178
1179
1180 static int kgdbfdc_read_char(void)
1181 {
1182 unsigned int stat, channel, data;
1183 void __iomem *regs;
1184
1185
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
1195 do {
1196 stat = __raw_readl(regs + REG_FDSTAT);
1197
1198
1199 if (stat & REG_FDSTAT_RXE)
1200 return NO_POLL_CHAR;
1201
1202
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
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
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
1224 word = mips_ejtag_fdc_encode(bufs, &kgdbfdc_wbuflen, 1);
1225
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
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
1242 static void kgdbfdc_flush(void)
1243 {
1244 while (kgdbfdc_wbuflen)
1245 kgdbfdc_push_one();
1246 }
1247
1248
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