1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include <linux/module.h>
22#include <linux/serial.h>
23#include <linux/serial_core.h>
24#include <linux/io.h>
25#include <linux/of_platform.h>
26#include <linux/dma-mapping.h>
27
28#include <linux/fs_uart_pd.h>
29#include <asm/ucc_slow.h>
30
31#include <linux/firmware.h>
32#include <asm/reg.h>
33
34
35
36
37
38
39#define UCC_SLOW_GUMR_H_SUART 0x00004000
40
41
42
43
44static int soft_uart;
45
46
47
48static int firmware_loaded;
49
50
51
52
53
54
55
56
57
58
59
60#define SERIAL_QE_MAJOR 204
61#define SERIAL_QE_MINOR 46
62
63
64#define UCC_MAX_UART 4
65
66
67#define RX_NUM_FIFO 4
68
69
70#define TX_NUM_FIFO 4
71
72
73#define RX_BUF_SIZE 32
74
75
76#define TX_BUF_SIZE 32
77
78
79
80
81
82
83#define UCC_WAIT_CLOSING 100
84
85struct ucc_uart_pram {
86 struct ucc_slow_pram common;
87 u8 res1[8];
88 __be16 maxidl;
89 __be16 idlc;
90 __be16 brkcr;
91 __be16 parec;
92 __be16 frmec;
93 __be16 nosec;
94 __be16 brkec;
95 __be16 brkln;
96 __be16 uaddr[2];
97 __be16 rtemp;
98 __be16 toseq;
99 __be16 cchars[8];
100 __be16 rccm;
101 __be16 rccr;
102 __be16 rlbc;
103 __be16 res2;
104 __be32 res3;
105 u8 res4;
106 u8 res5[3];
107 __be32 res6;
108 __be32 res7;
109 __be32 res8;
110 __be32 res9;
111 __be32 res10;
112 __be32 res11;
113 __be32 res12;
114 __be32 res13;
115
116 __be16 supsmr;
117 __be16 res92;
118 __be32 rx_state;
119 __be32 rx_cnt;
120 u8 rx_length;
121 u8 rx_bitmark;
122 u8 rx_temp_dlst_qe;
123 u8 res14[0xBC - 0x9F];
124 __be32 dump_ptr;
125 __be32 rx_frame_rem;
126 u8 rx_frame_rem_size;
127 u8 tx_mode;
128 __be16 tx_state;
129 u8 res15[0xD0 - 0xC8];
130 __be32 resD0;
131 u8 resD4;
132 __be16 resD5;
133} __attribute__ ((packed));
134
135
136#define UCC_UART_SUPSMR_SL 0x8000
137#define UCC_UART_SUPSMR_RPM_MASK 0x6000
138#define UCC_UART_SUPSMR_RPM_ODD 0x0000
139#define UCC_UART_SUPSMR_RPM_LOW 0x2000
140#define UCC_UART_SUPSMR_RPM_EVEN 0x4000
141#define UCC_UART_SUPSMR_RPM_HIGH 0x6000
142#define UCC_UART_SUPSMR_PEN 0x1000
143#define UCC_UART_SUPSMR_TPM_MASK 0x0C00
144#define UCC_UART_SUPSMR_TPM_ODD 0x0000
145#define UCC_UART_SUPSMR_TPM_LOW 0x0400
146#define UCC_UART_SUPSMR_TPM_EVEN 0x0800
147#define UCC_UART_SUPSMR_TPM_HIGH 0x0C00
148#define UCC_UART_SUPSMR_FRZ 0x0100
149#define UCC_UART_SUPSMR_UM_MASK 0x00c0
150#define UCC_UART_SUPSMR_UM_NORMAL 0x0000
151#define UCC_UART_SUPSMR_UM_MAN_MULTI 0x0040
152#define UCC_UART_SUPSMR_UM_AUTO_MULTI 0x00c0
153#define UCC_UART_SUPSMR_CL_MASK 0x0030
154#define UCC_UART_SUPSMR_CL_8 0x0030
155#define UCC_UART_SUPSMR_CL_7 0x0020
156#define UCC_UART_SUPSMR_CL_6 0x0010
157#define UCC_UART_SUPSMR_CL_5 0x0000
158
159#define UCC_UART_TX_STATE_AHDLC 0x00
160#define UCC_UART_TX_STATE_UART 0x01
161#define UCC_UART_TX_STATE_X1 0x00
162#define UCC_UART_TX_STATE_X16 0x80
163
164#define UCC_UART_PRAM_ALIGNMENT 0x100
165
166#define UCC_UART_SIZE_OF_BD UCC_SLOW_SIZE_OF_BD
167#define NUM_CONTROL_CHARS 8
168
169
170struct uart_qe_port {
171 struct uart_port port;
172 struct ucc_slow __iomem *uccp;
173 struct ucc_uart_pram __iomem *uccup;
174 struct ucc_slow_info us_info;
175 struct ucc_slow_private *us_private;
176 struct device_node *np;
177 unsigned int ucc_num;
178
179 u16 rx_nrfifos;
180 u16 rx_fifosize;
181 u16 tx_nrfifos;
182 u16 tx_fifosize;
183 int wait_closing;
184 u32 flags;
185 struct qe_bd *rx_bd_base;
186 struct qe_bd *rx_cur;
187 struct qe_bd *tx_bd_base;
188 struct qe_bd *tx_cur;
189 unsigned char *tx_buf;
190 unsigned char *rx_buf;
191 void *bd_virt;
192 dma_addr_t bd_dma_addr;
193 unsigned int bd_size;
194};
195
196static struct uart_driver ucc_uart_driver = {
197 .owner = THIS_MODULE,
198 .driver_name = "ucc_uart",
199 .dev_name = "ttyQE",
200 .major = SERIAL_QE_MAJOR,
201 .minor = SERIAL_QE_MINOR,
202 .nr = UCC_MAX_UART,
203};
204
205
206
207
208
209
210
211static inline dma_addr_t cpu2qe_addr(void *addr, struct uart_qe_port *qe_port)
212{
213 if (likely((addr >= qe_port->bd_virt)) &&
214 (addr < (qe_port->bd_virt + qe_port->bd_size)))
215 return qe_port->bd_dma_addr + (addr - qe_port->bd_virt);
216
217
218 printk(KERN_ERR "%s: addr=%p\n", __func__, addr);
219 BUG();
220 return 0;
221}
222
223
224
225
226
227
228
229static inline void *qe2cpu_addr(dma_addr_t addr, struct uart_qe_port *qe_port)
230{
231
232 if (likely((addr >= qe_port->bd_dma_addr) &&
233 (addr < (qe_port->bd_dma_addr + qe_port->bd_size))))
234 return qe_port->bd_virt + (addr - qe_port->bd_dma_addr);
235
236
237 printk(KERN_ERR "%s: addr=%x\n", __func__, addr);
238 BUG();
239 return NULL;
240}
241
242
243
244
245
246
247
248
249
250static unsigned int qe_uart_tx_empty(struct uart_port *port)
251{
252 struct uart_qe_port *qe_port =
253 container_of(port, struct uart_qe_port, port);
254 struct qe_bd *bdp = qe_port->tx_bd_base;
255
256 while (1) {
257 if (in_be16(&bdp->status) & BD_SC_READY)
258
259 return 0;
260
261 if (in_be16(&bdp->status) & BD_SC_WRAP)
262
263
264
265
266 return 1;
267
268 bdp++;
269 };
270}
271
272
273
274
275
276
277
278
279void qe_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
280{
281}
282
283
284
285
286
287
288
289
290static unsigned int qe_uart_get_mctrl(struct uart_port *port)
291{
292 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
293}
294
295
296
297
298
299
300
301
302static void qe_uart_stop_tx(struct uart_port *port)
303{
304 struct uart_qe_port *qe_port =
305 container_of(port, struct uart_qe_port, port);
306
307 clrbits16(&qe_port->uccp->uccm, UCC_UART_UCCE_TX);
308}
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324static int qe_uart_tx_pump(struct uart_qe_port *qe_port)
325{
326 struct qe_bd *bdp;
327 unsigned char *p;
328 unsigned int count;
329 struct uart_port *port = &qe_port->port;
330 struct circ_buf *xmit = &port->info->xmit;
331
332 bdp = qe_port->rx_cur;
333
334
335 if (port->x_char) {
336
337 bdp = qe_port->tx_cur;
338
339 p = qe2cpu_addr(bdp->buf, qe_port);
340
341 *p++ = port->x_char;
342 out_be16(&bdp->length, 1);
343 setbits16(&bdp->status, BD_SC_READY);
344
345 if (in_be16(&bdp->status) & BD_SC_WRAP)
346 bdp = qe_port->tx_bd_base;
347 else
348 bdp++;
349 qe_port->tx_cur = bdp;
350
351 port->icount.tx++;
352 port->x_char = 0;
353 return 1;
354 }
355
356 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
357 qe_uart_stop_tx(port);
358 return 0;
359 }
360
361
362 bdp = qe_port->tx_cur;
363
364 while (!(in_be16(&bdp->status) & BD_SC_READY) &&
365 (xmit->tail != xmit->head)) {
366 count = 0;
367 p = qe2cpu_addr(bdp->buf, qe_port);
368 while (count < qe_port->tx_fifosize) {
369 *p++ = xmit->buf[xmit->tail];
370 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
371 port->icount.tx++;
372 count++;
373 if (xmit->head == xmit->tail)
374 break;
375 }
376
377 out_be16(&bdp->length, count);
378 setbits16(&bdp->status, BD_SC_READY);
379
380
381 if (in_be16(&bdp->status) & BD_SC_WRAP)
382 bdp = qe_port->tx_bd_base;
383 else
384 bdp++;
385 }
386 qe_port->tx_cur = bdp;
387
388 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
389 uart_write_wakeup(port);
390
391 if (uart_circ_empty(xmit)) {
392
393
394
395 qe_uart_stop_tx(port);
396 return 0;
397 }
398
399 return 1;
400}
401
402
403
404
405
406
407
408static void qe_uart_start_tx(struct uart_port *port)
409{
410 struct uart_qe_port *qe_port =
411 container_of(port, struct uart_qe_port, port);
412
413
414 if (in_be16(&qe_port->uccp->uccm) & UCC_UART_UCCE_TX)
415 return;
416
417
418 if (qe_uart_tx_pump(qe_port))
419 setbits16(&qe_port->uccp->uccm, UCC_UART_UCCE_TX);
420}
421
422
423
424
425static void qe_uart_stop_rx(struct uart_port *port)
426{
427 struct uart_qe_port *qe_port =
428 container_of(port, struct uart_qe_port, port);
429
430 clrbits16(&qe_port->uccp->uccm, UCC_UART_UCCE_RX);
431}
432
433
434
435
436
437
438
439static void qe_uart_enable_ms(struct uart_port *port)
440{
441}
442
443
444
445
446
447
448
449static void qe_uart_break_ctl(struct uart_port *port, int break_state)
450{
451 struct uart_qe_port *qe_port =
452 container_of(port, struct uart_qe_port, port);
453
454 if (break_state)
455 ucc_slow_stop_tx(qe_port->us_private);
456 else
457 ucc_slow_restart_tx(qe_port->us_private);
458}
459
460
461
462
463
464static void qe_uart_int_rx(struct uart_qe_port *qe_port)
465{
466 int i;
467 unsigned char ch, *cp;
468 struct uart_port *port = &qe_port->port;
469 struct tty_struct *tty = port->info->port.tty;
470 struct qe_bd *bdp;
471 u16 status;
472 unsigned int flg;
473
474
475
476
477 bdp = qe_port->rx_cur;
478 while (1) {
479 status = in_be16(&bdp->status);
480
481
482 if (status & BD_SC_EMPTY)
483 break;
484
485
486 i = in_be16(&bdp->length);
487
488
489
490
491 if (tty_buffer_request_room(tty, i) < i) {
492 dev_dbg(port->dev, "ucc-uart: no room in RX buffer\n");
493 return;
494 }
495
496
497 cp = qe2cpu_addr(bdp->buf, qe_port);
498
499
500 while (i-- > 0) {
501 ch = *cp++;
502 port->icount.rx++;
503 flg = TTY_NORMAL;
504
505 if (!i && status &
506 (BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV))
507 goto handle_error;
508 if (uart_handle_sysrq_char(port, ch))
509 continue;
510
511error_return:
512 tty_insert_flip_char(tty, ch, flg);
513
514 }
515
516
517 clrsetbits_be16(&bdp->status, BD_SC_BR | BD_SC_FR | BD_SC_PR |
518 BD_SC_OV | BD_SC_ID, BD_SC_EMPTY);
519 if (in_be16(&bdp->status) & BD_SC_WRAP)
520 bdp = qe_port->rx_bd_base;
521 else
522 bdp++;
523
524 }
525
526
527 qe_port->rx_cur = bdp;
528
529
530 tty_flip_buffer_push(tty);
531
532 return;
533
534
535
536handle_error:
537
538 if (status & BD_SC_BR)
539 port->icount.brk++;
540 if (status & BD_SC_PR)
541 port->icount.parity++;
542 if (status & BD_SC_FR)
543 port->icount.frame++;
544 if (status & BD_SC_OV)
545 port->icount.overrun++;
546
547
548 status &= port->read_status_mask;
549
550
551 if (status & BD_SC_BR)
552 flg = TTY_BREAK;
553 else if (status & BD_SC_PR)
554 flg = TTY_PARITY;
555 else if (status & BD_SC_FR)
556 flg = TTY_FRAME;
557
558
559 if (status & BD_SC_OV)
560 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
561#ifdef SUPPORT_SYSRQ
562 port->sysrq = 0;
563#endif
564 goto error_return;
565}
566
567
568
569
570
571static irqreturn_t qe_uart_int(int irq, void *data)
572{
573 struct uart_qe_port *qe_port = (struct uart_qe_port *) data;
574 struct ucc_slow __iomem *uccp = qe_port->uccp;
575 u16 events;
576
577
578 events = in_be16(&uccp->ucce);
579 out_be16(&uccp->ucce, events);
580
581 if (events & UCC_UART_UCCE_BRKE)
582 uart_handle_break(&qe_port->port);
583
584 if (events & UCC_UART_UCCE_RX)
585 qe_uart_int_rx(qe_port);
586
587 if (events & UCC_UART_UCCE_TX)
588 qe_uart_tx_pump(qe_port);
589
590 return events ? IRQ_HANDLED : IRQ_NONE;
591}
592
593
594
595
596
597static void qe_uart_initbd(struct uart_qe_port *qe_port)
598{
599 int i;
600 void *bd_virt;
601 struct qe_bd *bdp;
602
603
604
605
606 bd_virt = qe_port->bd_virt;
607 bdp = qe_port->rx_bd_base;
608 qe_port->rx_cur = qe_port->rx_bd_base;
609 for (i = 0; i < (qe_port->rx_nrfifos - 1); i++) {
610 out_be16(&bdp->status, BD_SC_EMPTY | BD_SC_INTRPT);
611 out_be32(&bdp->buf, cpu2qe_addr(bd_virt, qe_port));
612 out_be16(&bdp->length, 0);
613 bd_virt += qe_port->rx_fifosize;
614 bdp++;
615 }
616
617
618 out_be16(&bdp->status, BD_SC_WRAP | BD_SC_EMPTY | BD_SC_INTRPT);
619 out_be32(&bdp->buf, cpu2qe_addr(bd_virt, qe_port));
620 out_be16(&bdp->length, 0);
621
622
623
624
625
626 bd_virt = qe_port->bd_virt +
627 L1_CACHE_ALIGN(qe_port->rx_nrfifos * qe_port->rx_fifosize);
628 qe_port->tx_cur = qe_port->tx_bd_base;
629 bdp = qe_port->tx_bd_base;
630 for (i = 0; i < (qe_port->tx_nrfifos - 1); i++) {
631 out_be16(&bdp->status, BD_SC_INTRPT);
632 out_be32(&bdp->buf, cpu2qe_addr(bd_virt, qe_port));
633 out_be16(&bdp->length, 0);
634 bd_virt += qe_port->tx_fifosize;
635 bdp++;
636 }
637
638
639#ifdef LOOPBACK
640 setbits16(&qe_port->tx_cur->status, BD_SC_P);
641#endif
642
643 out_be16(&bdp->status, BD_SC_WRAP | BD_SC_INTRPT);
644 out_be32(&bdp->buf, cpu2qe_addr(bd_virt, qe_port));
645 out_be16(&bdp->length, 0);
646}
647
648
649
650
651
652
653
654
655static void qe_uart_init_ucc(struct uart_qe_port *qe_port)
656{
657 u32 cecr_subblock;
658 struct ucc_slow __iomem *uccp = qe_port->uccp;
659 struct ucc_uart_pram *uccup = qe_port->uccup;
660
661 unsigned int i;
662
663
664 ucc_slow_disable(qe_port->us_private, COMM_DIR_RX_AND_TX);
665
666
667 out_8(&uccup->common.rbmr, UCC_BMR_GBL | UCC_BMR_BO_BE);
668 out_8(&uccup->common.tbmr, UCC_BMR_GBL | UCC_BMR_BO_BE);
669 out_be16(&uccup->common.mrblr, qe_port->rx_fifosize);
670 out_be16(&uccup->maxidl, 0x10);
671 out_be16(&uccup->brkcr, 1);
672 out_be16(&uccup->parec, 0);
673 out_be16(&uccup->frmec, 0);
674 out_be16(&uccup->nosec, 0);
675 out_be16(&uccup->brkec, 0);
676 out_be16(&uccup->uaddr[0], 0);
677 out_be16(&uccup->uaddr[1], 0);
678 out_be16(&uccup->toseq, 0);
679 for (i = 0; i < 8; i++)
680 out_be16(&uccup->cchars[i], 0xC000);
681 out_be16(&uccup->rccm, 0xc0ff);
682
683
684 if (soft_uart)
685
686 clrsetbits_be32(&uccp->gumr_l,
687 UCC_SLOW_GUMR_L_MODE_MASK | UCC_SLOW_GUMR_L_TDCR_MASK |
688 UCC_SLOW_GUMR_L_RDCR_MASK,
689 UCC_SLOW_GUMR_L_MODE_UART | UCC_SLOW_GUMR_L_TDCR_1 |
690 UCC_SLOW_GUMR_L_RDCR_16);
691 else
692 clrsetbits_be32(&uccp->gumr_l,
693 UCC_SLOW_GUMR_L_MODE_MASK | UCC_SLOW_GUMR_L_TDCR_MASK |
694 UCC_SLOW_GUMR_L_RDCR_MASK,
695 UCC_SLOW_GUMR_L_MODE_UART | UCC_SLOW_GUMR_L_TDCR_16 |
696 UCC_SLOW_GUMR_L_RDCR_16);
697
698 clrsetbits_be32(&uccp->gumr_h, UCC_SLOW_GUMR_H_RFW,
699 UCC_SLOW_GUMR_H_TRX | UCC_SLOW_GUMR_H_TTX);
700
701#ifdef LOOPBACK
702 clrsetbits_be32(&uccp->gumr_l, UCC_SLOW_GUMR_L_DIAG_MASK,
703 UCC_SLOW_GUMR_L_DIAG_LOOP);
704 clrsetbits_be32(&uccp->gumr_h,
705 UCC_SLOW_GUMR_H_CTSP | UCC_SLOW_GUMR_H_RSYN,
706 UCC_SLOW_GUMR_H_CDS);
707#endif
708
709
710 out_be16(&uccp->uccm, 0);
711 out_be16(&uccp->ucce, 0xffff);
712 out_be16(&uccp->udsr, 0x7e7e);
713
714
715 out_be16(&uccp->upsmr, 0);
716
717 if (soft_uart) {
718 out_be16(&uccup->supsmr, 0x30);
719 out_be16(&uccup->res92, 0);
720 out_be32(&uccup->rx_state, 0);
721 out_be32(&uccup->rx_cnt, 0);
722 out_8(&uccup->rx_bitmark, 0);
723 out_8(&uccup->rx_length, 10);
724 out_be32(&uccup->dump_ptr, 0x4000);
725 out_8(&uccup->rx_temp_dlst_qe, 0);
726 out_be32(&uccup->rx_frame_rem, 0);
727 out_8(&uccup->rx_frame_rem_size, 0);
728
729 out_8(&uccup->tx_mode,
730 UCC_UART_TX_STATE_UART | UCC_UART_TX_STATE_X1);
731 out_be16(&uccup->tx_state, 0);
732 out_8(&uccup->resD4, 0);
733 out_be16(&uccup->resD5, 0);
734
735
736
737
738
739
740
741
742
743
744
745
746
747 clrsetbits_be32(&uccp->gumr_l,
748 UCC_SLOW_GUMR_L_MODE_MASK | UCC_SLOW_GUMR_L_TDCR_MASK |
749 UCC_SLOW_GUMR_L_RDCR_MASK,
750 UCC_SLOW_GUMR_L_MODE_QMC | UCC_SLOW_GUMR_L_TDCR_16 |
751 UCC_SLOW_GUMR_L_RDCR_16);
752
753 clrsetbits_be32(&uccp->gumr_h,
754 UCC_SLOW_GUMR_H_RFW | UCC_SLOW_GUMR_H_RSYN,
755 UCC_SLOW_GUMR_H_SUART | UCC_SLOW_GUMR_H_TRX |
756 UCC_SLOW_GUMR_H_TTX | UCC_SLOW_GUMR_H_TFL);
757
758#ifdef LOOPBACK
759 clrsetbits_be32(&uccp->gumr_l, UCC_SLOW_GUMR_L_DIAG_MASK,
760 UCC_SLOW_GUMR_L_DIAG_LOOP);
761 clrbits32(&uccp->gumr_h, UCC_SLOW_GUMR_H_CTSP |
762 UCC_SLOW_GUMR_H_CDS);
763#endif
764
765 cecr_subblock = ucc_slow_get_qe_cr_subblock(qe_port->ucc_num);
766 qe_issue_cmd(QE_INIT_TX_RX, cecr_subblock,
767 QE_CR_PROTOCOL_UNSPECIFIED, 0);
768 }
769}
770
771
772
773
774static int qe_uart_startup(struct uart_port *port)
775{
776 struct uart_qe_port *qe_port =
777 container_of(port, struct uart_qe_port, port);
778 int ret;
779
780
781
782
783
784 if (soft_uart && !firmware_loaded) {
785 dev_err(port->dev, "Soft-UART firmware not uploaded\n");
786 return -ENODEV;
787 }
788
789 qe_uart_initbd(qe_port);
790 qe_uart_init_ucc(qe_port);
791
792
793 ret = request_irq(port->irq, qe_uart_int, IRQF_SHARED, "ucc-uart",
794 qe_port);
795 if (ret) {
796 dev_err(port->dev, "could not claim IRQ %u\n", port->irq);
797 return ret;
798 }
799
800
801 setbits16(&qe_port->uccp->uccm, UCC_UART_UCCE_RX);
802 ucc_slow_enable(qe_port->us_private, COMM_DIR_RX_AND_TX);
803
804 return 0;
805}
806
807
808
809
810static void qe_uart_shutdown(struct uart_port *port)
811{
812 struct uart_qe_port *qe_port =
813 container_of(port, struct uart_qe_port, port);
814 struct ucc_slow __iomem *uccp = qe_port->uccp;
815 unsigned int timeout = 20;
816
817
818
819
820 while (!qe_uart_tx_empty(port)) {
821 if (!--timeout) {
822 dev_warn(port->dev, "shutdown timeout\n");
823 break;
824 }
825 set_current_state(TASK_UNINTERRUPTIBLE);
826 schedule_timeout(2);
827 }
828
829 if (qe_port->wait_closing) {
830
831 set_current_state(TASK_UNINTERRUPTIBLE);
832 schedule_timeout(qe_port->wait_closing);
833 }
834
835
836 ucc_slow_disable(qe_port->us_private, COMM_DIR_RX_AND_TX);
837 clrbits16(&uccp->uccm, UCC_UART_UCCE_TX | UCC_UART_UCCE_RX);
838
839
840 ucc_slow_graceful_stop_tx(qe_port->us_private);
841 qe_uart_initbd(qe_port);
842
843 free_irq(port->irq, qe_port);
844}
845
846
847
848
849static void qe_uart_set_termios(struct uart_port *port,
850 struct ktermios *termios, struct ktermios *old)
851{
852 struct uart_qe_port *qe_port =
853 container_of(port, struct uart_qe_port, port);
854 struct ucc_slow __iomem *uccp = qe_port->uccp;
855 unsigned int baud;
856 unsigned long flags;
857 u16 upsmr = in_be16(&uccp->upsmr);
858 struct ucc_uart_pram __iomem *uccup = qe_port->uccup;
859 u16 supsmr = in_be16(&uccup->supsmr);
860 u8 char_length = 2;
861
862
863
864
865
866
867
868
869 upsmr &= UCC_UART_UPSMR_CL_MASK;
870 supsmr &= UCC_UART_SUPSMR_CL_MASK;
871
872 switch (termios->c_cflag & CSIZE) {
873 case CS5:
874 upsmr |= UCC_UART_UPSMR_CL_5;
875 supsmr |= UCC_UART_SUPSMR_CL_5;
876 char_length += 5;
877 break;
878 case CS6:
879 upsmr |= UCC_UART_UPSMR_CL_6;
880 supsmr |= UCC_UART_SUPSMR_CL_6;
881 char_length += 6;
882 break;
883 case CS7:
884 upsmr |= UCC_UART_UPSMR_CL_7;
885 supsmr |= UCC_UART_SUPSMR_CL_7;
886 char_length += 7;
887 break;
888 default:
889 upsmr |= UCC_UART_UPSMR_CL_8;
890 supsmr |= UCC_UART_SUPSMR_CL_8;
891 char_length += 8;
892 break;
893 }
894
895
896 if (termios->c_cflag & CSTOPB) {
897 upsmr |= UCC_UART_UPSMR_SL;
898 supsmr |= UCC_UART_SUPSMR_SL;
899 char_length++;
900 }
901
902 if (termios->c_cflag & PARENB) {
903 upsmr |= UCC_UART_UPSMR_PEN;
904 supsmr |= UCC_UART_SUPSMR_PEN;
905 char_length++;
906
907 if (!(termios->c_cflag & PARODD)) {
908 upsmr &= ~(UCC_UART_UPSMR_RPM_MASK |
909 UCC_UART_UPSMR_TPM_MASK);
910 upsmr |= UCC_UART_UPSMR_RPM_EVEN |
911 UCC_UART_UPSMR_TPM_EVEN;
912 supsmr &= ~(UCC_UART_SUPSMR_RPM_MASK |
913 UCC_UART_SUPSMR_TPM_MASK);
914 supsmr |= UCC_UART_SUPSMR_RPM_EVEN |
915 UCC_UART_SUPSMR_TPM_EVEN;
916 }
917 }
918
919
920
921
922 port->read_status_mask = BD_SC_EMPTY | BD_SC_OV;
923 if (termios->c_iflag & INPCK)
924 port->read_status_mask |= BD_SC_FR | BD_SC_PR;
925 if (termios->c_iflag & (BRKINT | PARMRK))
926 port->read_status_mask |= BD_SC_BR;
927
928
929
930
931 port->ignore_status_mask = 0;
932 if (termios->c_iflag & IGNPAR)
933 port->ignore_status_mask |= BD_SC_PR | BD_SC_FR;
934 if (termios->c_iflag & IGNBRK) {
935 port->ignore_status_mask |= BD_SC_BR;
936
937
938
939
940 if (termios->c_iflag & IGNPAR)
941 port->ignore_status_mask |= BD_SC_OV;
942 }
943
944
945
946 if ((termios->c_cflag & CREAD) == 0)
947 port->read_status_mask &= ~BD_SC_EMPTY;
948
949 baud = uart_get_baud_rate(port, termios, old, 0, 115200);
950
951
952 spin_lock_irqsave(&port->lock, flags);
953
954 out_be16(&uccp->upsmr, upsmr);
955 if (soft_uart) {
956 out_be16(&uccup->supsmr, supsmr);
957 out_8(&uccup->rx_length, char_length);
958
959
960 qe_setbrg(qe_port->us_info.rx_clock, baud, 16);
961 qe_setbrg(qe_port->us_info.tx_clock, baud, 1);
962 } else {
963 qe_setbrg(qe_port->us_info.rx_clock, baud, 16);
964 qe_setbrg(qe_port->us_info.tx_clock, baud, 16);
965 }
966
967 spin_unlock_irqrestore(&port->lock, flags);
968}
969
970
971
972
973static const char *qe_uart_type(struct uart_port *port)
974{
975 return "QE";
976}
977
978
979
980
981static int qe_uart_request_port(struct uart_port *port)
982{
983 int ret;
984 struct uart_qe_port *qe_port =
985 container_of(port, struct uart_qe_port, port);
986 struct ucc_slow_info *us_info = &qe_port->us_info;
987 struct ucc_slow_private *uccs;
988 unsigned int rx_size, tx_size;
989 void *bd_virt;
990 dma_addr_t bd_dma_addr = 0;
991
992 ret = ucc_slow_init(us_info, &uccs);
993 if (ret) {
994 dev_err(port->dev, "could not initialize UCC%u\n",
995 qe_port->ucc_num);
996 return ret;
997 }
998
999 qe_port->us_private = uccs;
1000 qe_port->uccp = uccs->us_regs;
1001 qe_port->uccup = (struct ucc_uart_pram *) uccs->us_pram;
1002 qe_port->rx_bd_base = uccs->rx_bd;
1003 qe_port->tx_bd_base = uccs->tx_bd;
1004
1005
1006
1007
1008
1009 rx_size = L1_CACHE_ALIGN(qe_port->rx_nrfifos * qe_port->rx_fifosize);
1010 tx_size = L1_CACHE_ALIGN(qe_port->tx_nrfifos * qe_port->tx_fifosize);
1011
1012 bd_virt = dma_alloc_coherent(port->dev, rx_size + tx_size, &bd_dma_addr,
1013 GFP_KERNEL);
1014 if (!bd_virt) {
1015 dev_err(port->dev, "could not allocate buffer descriptors\n");
1016 return -ENOMEM;
1017 }
1018
1019 qe_port->bd_virt = bd_virt;
1020 qe_port->bd_dma_addr = bd_dma_addr;
1021 qe_port->bd_size = rx_size + tx_size;
1022
1023 qe_port->rx_buf = bd_virt;
1024 qe_port->tx_buf = qe_port->rx_buf + rx_size;
1025
1026 return 0;
1027}
1028
1029
1030
1031
1032
1033
1034
1035
1036static void qe_uart_config_port(struct uart_port *port, int flags)
1037{
1038 if (flags & UART_CONFIG_TYPE) {
1039 port->type = PORT_CPM;
1040 qe_uart_request_port(port);
1041 }
1042}
1043
1044
1045
1046
1047
1048static void qe_uart_release_port(struct uart_port *port)
1049{
1050 struct uart_qe_port *qe_port =
1051 container_of(port, struct uart_qe_port, port);
1052 struct ucc_slow_private *uccs = qe_port->us_private;
1053
1054 dma_free_coherent(port->dev, qe_port->bd_size, qe_port->bd_virt,
1055 qe_port->bd_dma_addr);
1056
1057 ucc_slow_free(uccs);
1058}
1059
1060
1061
1062
1063static int qe_uart_verify_port(struct uart_port *port,
1064 struct serial_struct *ser)
1065{
1066 if (ser->type != PORT_UNKNOWN && ser->type != PORT_CPM)
1067 return -EINVAL;
1068
1069 if (ser->irq < 0 || ser->irq >= nr_irqs)
1070 return -EINVAL;
1071
1072 if (ser->baud_base < 9600)
1073 return -EINVAL;
1074
1075 return 0;
1076}
1077
1078
1079
1080
1081static struct uart_ops qe_uart_pops = {
1082 .tx_empty = qe_uart_tx_empty,
1083 .set_mctrl = qe_uart_set_mctrl,
1084 .get_mctrl = qe_uart_get_mctrl,
1085 .stop_tx = qe_uart_stop_tx,
1086 .start_tx = qe_uart_start_tx,
1087 .stop_rx = qe_uart_stop_rx,
1088 .enable_ms = qe_uart_enable_ms,
1089 .break_ctl = qe_uart_break_ctl,
1090 .startup = qe_uart_startup,
1091 .shutdown = qe_uart_shutdown,
1092 .set_termios = qe_uart_set_termios,
1093 .type = qe_uart_type,
1094 .release_port = qe_uart_release_port,
1095 .request_port = qe_uart_request_port,
1096 .config_port = qe_uart_config_port,
1097 .verify_port = qe_uart_verify_port,
1098};
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123static 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
1131 np = of_find_node_by_type(NULL, "cpu");
1132 if (!np)
1133 return 0;
1134
1135 soc_string = of_get_property(np, "compatible", NULL);
1136 if (!soc_string)
1137
1138 soc_string = np->name;
1139
1140
1141 if ((sscanf(soc_string, "PowerPC,%u", &soc) != 1) || !soc)
1142 return 0;
1143
1144
1145 svr = mfspr(SPRN_SVR);
1146 *rev_h = (svr >> 4) & 0xf;
1147 *rev_l = svr & 0xf;
1148
1149 return soc;
1150}
1151
1152
1153
1154
1155
1156
1157
1158static void uart_firmware_cont(const struct firmware *fw, void *context)
1159{
1160 struct qe_firmware *firmware;
1161 struct device *dev = context;
1162 int ret;
1163
1164 if (!fw) {
1165 dev_err(dev, "firmware not found\n");
1166 return;
1167 }
1168
1169 firmware = (struct qe_firmware *) fw->data;
1170
1171 if (firmware->header.length != fw->size) {
1172 dev_err(dev, "invalid firmware\n");
1173 return;
1174 }
1175
1176 ret = qe_upload_firmware(firmware);
1177 if (ret) {
1178 dev_err(dev, "could not load firmware\n");
1179 return;
1180 }
1181
1182 firmware_loaded = 1;
1183}
1184
1185static int ucc_uart_probe(struct of_device *ofdev,
1186 const struct of_device_id *match)
1187{
1188 struct device_node *np = ofdev->node;
1189 const unsigned int *iprop;
1190 const char *sprop;
1191 struct uart_qe_port *qe_port = NULL;
1192 struct resource res;
1193 int ret;
1194
1195
1196
1197
1198 if (of_find_property(np, "soft-uart", NULL)) {
1199 dev_dbg(&ofdev->dev, "using Soft-UART mode\n");
1200 soft_uart = 1;
1201 }
1202
1203
1204
1205
1206
1207 if (soft_uart) {
1208 struct qe_firmware_info *qe_fw_info;
1209
1210 qe_fw_info = qe_get_firmware_info();
1211
1212
1213 if (qe_fw_info && strstr(qe_fw_info->id, "Soft-UART")) {
1214 firmware_loaded = 1;
1215 } else {
1216 char filename[32];
1217 unsigned int soc;
1218 unsigned int rev_h;
1219 unsigned int rev_l;
1220
1221 soc = soc_info(&rev_h, &rev_l);
1222 if (!soc) {
1223 dev_err(&ofdev->dev, "unknown CPU model\n");
1224 return -ENXIO;
1225 }
1226 sprintf(filename, "fsl_qe_ucode_uart_%u_%u%u.bin",
1227 soc, rev_h, rev_l);
1228
1229 dev_info(&ofdev->dev, "waiting for firmware %s\n",
1230 filename);
1231
1232
1233
1234
1235
1236
1237
1238
1239 ret = request_firmware_nowait(THIS_MODULE,
1240 FW_ACTION_HOTPLUG, filename, &ofdev->dev,
1241 &ofdev->dev, uart_firmware_cont);
1242 if (ret) {
1243 dev_err(&ofdev->dev,
1244 "could not load firmware %s\n",
1245 filename);
1246 return ret;
1247 }
1248 }
1249 }
1250
1251 qe_port = kzalloc(sizeof(struct uart_qe_port), GFP_KERNEL);
1252 if (!qe_port) {
1253 dev_err(&ofdev->dev, "can't allocate QE port structure\n");
1254 return -ENOMEM;
1255 }
1256
1257
1258 ret = of_address_to_resource(np, 0, &res);
1259 if (ret) {
1260 dev_err(&ofdev->dev, "missing 'reg' property in device tree\n");
1261 kfree(qe_port);
1262 return ret;
1263 }
1264 if (!res.start) {
1265 dev_err(&ofdev->dev, "invalid 'reg' property in device tree\n");
1266 kfree(qe_port);
1267 return -EINVAL;
1268 }
1269 qe_port->port.mapbase = res.start;
1270
1271
1272
1273 iprop = of_get_property(np, "cell-index", NULL);
1274 if (!iprop) {
1275 iprop = of_get_property(np, "device-id", NULL);
1276 if (!iprop) {
1277 kfree(qe_port);
1278 dev_err(&ofdev->dev, "UCC is unspecified in "
1279 "device tree\n");
1280 return -EINVAL;
1281 }
1282 }
1283
1284 if ((*iprop < 1) || (*iprop > UCC_MAX_NUM)) {
1285 dev_err(&ofdev->dev, "no support for UCC%u\n", *iprop);
1286 kfree(qe_port);
1287 return -ENODEV;
1288 }
1289 qe_port->ucc_num = *iprop - 1;
1290
1291
1292
1293
1294
1295
1296
1297
1298 sprop = of_get_property(np, "rx-clock-name", NULL);
1299 if (!sprop) {
1300 dev_err(&ofdev->dev, "missing rx-clock-name in device tree\n");
1301 kfree(qe_port);
1302 return -ENODEV;
1303 }
1304
1305 qe_port->us_info.rx_clock = qe_clock_source(sprop);
1306 if ((qe_port->us_info.rx_clock < QE_BRG1) ||
1307 (qe_port->us_info.rx_clock > QE_BRG16)) {
1308 dev_err(&ofdev->dev, "rx-clock-name must be a BRG for UART\n");
1309 kfree(qe_port);
1310 return -ENODEV;
1311 }
1312
1313#ifdef LOOPBACK
1314
1315 qe_port->us_info.tx_clock = qe_port->us_info.rx_clock;
1316#else
1317 sprop = of_get_property(np, "tx-clock-name", NULL);
1318 if (!sprop) {
1319 dev_err(&ofdev->dev, "missing tx-clock-name in device tree\n");
1320 kfree(qe_port);
1321 return -ENODEV;
1322 }
1323 qe_port->us_info.tx_clock = qe_clock_source(sprop);
1324#endif
1325 if ((qe_port->us_info.tx_clock < QE_BRG1) ||
1326 (qe_port->us_info.tx_clock > QE_BRG16)) {
1327 dev_err(&ofdev->dev, "tx-clock-name must be a BRG for UART\n");
1328 kfree(qe_port);
1329 return -ENODEV;
1330 }
1331
1332
1333 iprop = of_get_property(np, "port-number", NULL);
1334 if (!iprop) {
1335 dev_err(&ofdev->dev, "missing port-number in device tree\n");
1336 kfree(qe_port);
1337 return -EINVAL;
1338 }
1339 qe_port->port.line = *iprop;
1340 if (qe_port->port.line >= UCC_MAX_UART) {
1341 dev_err(&ofdev->dev, "port-number must be 0-%u\n",
1342 UCC_MAX_UART - 1);
1343 kfree(qe_port);
1344 return -EINVAL;
1345 }
1346
1347 qe_port->port.irq = irq_of_parse_and_map(np, 0);
1348 if (qe_port->port.irq == NO_IRQ) {
1349 dev_err(&ofdev->dev, "could not map IRQ for UCC%u\n",
1350 qe_port->ucc_num + 1);
1351 kfree(qe_port);
1352 return -EINVAL;
1353 }
1354
1355
1356
1357
1358
1359 np = of_find_compatible_node(NULL, NULL, "fsl,qe");
1360 if (!np) {
1361 np = of_find_node_by_type(NULL, "qe");
1362 if (!np) {
1363 dev_err(&ofdev->dev, "could not find 'qe' node\n");
1364 kfree(qe_port);
1365 return -EINVAL;
1366 }
1367 }
1368
1369 iprop = of_get_property(np, "brg-frequency", NULL);
1370 if (!iprop) {
1371 dev_err(&ofdev->dev,
1372 "missing brg-frequency in device tree\n");
1373 kfree(qe_port);
1374 return -EINVAL;
1375 }
1376
1377 if (*iprop)
1378 qe_port->port.uartclk = *iprop;
1379 else {
1380
1381
1382
1383
1384
1385 iprop = of_get_property(np, "bus-frequency", NULL);
1386 if (!iprop) {
1387 dev_err(&ofdev->dev,
1388 "missing QE bus-frequency in device tree\n");
1389 kfree(qe_port);
1390 return -EINVAL;
1391 }
1392 if (*iprop)
1393 qe_port->port.uartclk = *iprop / 2;
1394 else {
1395 dev_err(&ofdev->dev,
1396 "invalid QE bus-frequency in device tree\n");
1397 kfree(qe_port);
1398 return -EINVAL;
1399 }
1400 }
1401
1402 spin_lock_init(&qe_port->port.lock);
1403 qe_port->np = np;
1404 qe_port->port.dev = &ofdev->dev;
1405 qe_port->port.ops = &qe_uart_pops;
1406 qe_port->port.iotype = UPIO_MEM;
1407
1408 qe_port->tx_nrfifos = TX_NUM_FIFO;
1409 qe_port->tx_fifosize = TX_BUF_SIZE;
1410 qe_port->rx_nrfifos = RX_NUM_FIFO;
1411 qe_port->rx_fifosize = RX_BUF_SIZE;
1412
1413 qe_port->wait_closing = UCC_WAIT_CLOSING;
1414 qe_port->port.fifosize = 512;
1415 qe_port->port.flags = UPF_BOOT_AUTOCONF | UPF_IOREMAP;
1416
1417 qe_port->us_info.ucc_num = qe_port->ucc_num;
1418 qe_port->us_info.regs = (phys_addr_t) res.start;
1419 qe_port->us_info.irq = qe_port->port.irq;
1420
1421 qe_port->us_info.rx_bd_ring_len = qe_port->rx_nrfifos;
1422 qe_port->us_info.tx_bd_ring_len = qe_port->tx_nrfifos;
1423
1424
1425 qe_port->us_info.init_tx = 1;
1426 qe_port->us_info.init_rx = 1;
1427
1428
1429
1430
1431
1432 ret = uart_add_one_port(&ucc_uart_driver, &qe_port->port);
1433 if (ret) {
1434 dev_err(&ofdev->dev, "could not add /dev/ttyQE%u\n",
1435 qe_port->port.line);
1436 kfree(qe_port);
1437 return ret;
1438 }
1439
1440 dev_set_drvdata(&ofdev->dev, qe_port);
1441
1442 dev_info(&ofdev->dev, "UCC%u assigned to /dev/ttyQE%u\n",
1443 qe_port->ucc_num + 1, qe_port->port.line);
1444
1445
1446 dev_dbg(&ofdev->dev, "mknod command is 'mknod /dev/ttyQE%u c %u %u'\n",
1447 qe_port->port.line, SERIAL_QE_MAJOR,
1448 SERIAL_QE_MINOR + qe_port->port.line);
1449
1450 return 0;
1451}
1452
1453static int ucc_uart_remove(struct of_device *ofdev)
1454{
1455 struct uart_qe_port *qe_port = dev_get_drvdata(&ofdev->dev);
1456
1457 dev_info(&ofdev->dev, "removing /dev/ttyQE%u\n", qe_port->port.line);
1458
1459 uart_remove_one_port(&ucc_uart_driver, &qe_port->port);
1460
1461 dev_set_drvdata(&ofdev->dev, NULL);
1462 kfree(qe_port);
1463
1464 return 0;
1465}
1466
1467static struct of_device_id ucc_uart_match[] = {
1468 {
1469 .type = "serial",
1470 .compatible = "ucc_uart",
1471 },
1472 {},
1473};
1474MODULE_DEVICE_TABLE(of, ucc_uart_match);
1475
1476static struct of_platform_driver ucc_uart_of_driver = {
1477 .owner = THIS_MODULE,
1478 .name = "ucc_uart",
1479 .match_table = ucc_uart_match,
1480 .probe = ucc_uart_probe,
1481 .remove = ucc_uart_remove,
1482};
1483
1484static int __init ucc_uart_init(void)
1485{
1486 int ret;
1487
1488 printk(KERN_INFO "Freescale QUICC Engine UART device driver\n");
1489#ifdef LOOPBACK
1490 printk(KERN_INFO "ucc-uart: Using loopback mode\n");
1491#endif
1492
1493 ret = uart_register_driver(&ucc_uart_driver);
1494 if (ret) {
1495 printk(KERN_ERR "ucc-uart: could not register UART driver\n");
1496 return ret;
1497 }
1498
1499 ret = of_register_platform_driver(&ucc_uart_of_driver);
1500 if (ret)
1501 printk(KERN_ERR
1502 "ucc-uart: could not register platform driver\n");
1503
1504 return ret;
1505}
1506
1507static void __exit ucc_uart_exit(void)
1508{
1509 printk(KERN_INFO
1510 "Freescale QUICC Engine UART device driver unloading\n");
1511
1512 of_unregister_platform_driver(&ucc_uart_of_driver);
1513 uart_unregister_driver(&ucc_uart_driver);
1514}
1515
1516module_init(ucc_uart_init);
1517module_exit(ucc_uart_exit);
1518
1519MODULE_DESCRIPTION("Freescale QUICC Engine (QE) UART");
1520MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
1521MODULE_LICENSE("GPL v2");
1522MODULE_ALIAS_CHARDEV_MAJOR(SERIAL_QE_MAJOR);
1523
1524