21 #include <linux/module.h>
22 #include <linux/serial.h>
23 #include <linux/serial_core.h>
24 #include <linux/slab.h>
25 #include <linux/tty.h>
42 #define UCC_SLOW_GUMR_H_SUART 0x00004000
51 static int firmware_loaded;
63 #define SERIAL_QE_MAJOR 204
64 #define SERIAL_QE_MINOR 46
67 #define UCC_MAX_UART 4
76 #define RX_BUF_SIZE 32
79 #define TX_BUF_SIZE 32
86 #define UCC_WAIT_CLOSING 100
139 #define UCC_UART_SUPSMR_SL 0x8000
140 #define UCC_UART_SUPSMR_RPM_MASK 0x6000
141 #define UCC_UART_SUPSMR_RPM_ODD 0x0000
142 #define UCC_UART_SUPSMR_RPM_LOW 0x2000
143 #define UCC_UART_SUPSMR_RPM_EVEN 0x4000
144 #define UCC_UART_SUPSMR_RPM_HIGH 0x6000
145 #define UCC_UART_SUPSMR_PEN 0x1000
146 #define UCC_UART_SUPSMR_TPM_MASK 0x0C00
147 #define UCC_UART_SUPSMR_TPM_ODD 0x0000
148 #define UCC_UART_SUPSMR_TPM_LOW 0x0400
149 #define UCC_UART_SUPSMR_TPM_EVEN 0x0800
150 #define UCC_UART_SUPSMR_TPM_HIGH 0x0C00
151 #define UCC_UART_SUPSMR_FRZ 0x0100
152 #define UCC_UART_SUPSMR_UM_MASK 0x00c0
153 #define UCC_UART_SUPSMR_UM_NORMAL 0x0000
154 #define UCC_UART_SUPSMR_UM_MAN_MULTI 0x0040
155 #define UCC_UART_SUPSMR_UM_AUTO_MULTI 0x00c0
156 #define UCC_UART_SUPSMR_CL_MASK 0x0030
157 #define UCC_UART_SUPSMR_CL_8 0x0030
158 #define UCC_UART_SUPSMR_CL_7 0x0020
159 #define UCC_UART_SUPSMR_CL_6 0x0010
160 #define UCC_UART_SUPSMR_CL_5 0x0000
162 #define UCC_UART_TX_STATE_AHDLC 0x00
163 #define UCC_UART_TX_STATE_UART 0x01
164 #define UCC_UART_TX_STATE_X1 0x00
165 #define UCC_UART_TX_STATE_X16 0x80
167 #define UCC_UART_PRAM_ALIGNMENT 0x100
169 #define UCC_UART_SIZE_OF_BD UCC_SLOW_SIZE_OF_BD
170 #define NUM_CONTROL_CHARS 8
201 .driver_name =
"ucc_uart",
293 static unsigned int qe_uart_get_mctrl(
struct uart_port *port)
305 static void qe_uart_stop_tx(
struct uart_port *port)
310 clrbits16(&qe_port->
uccp->uccm, UCC_UART_UCCE_TX);
327 static int qe_uart_tx_pump(
struct uart_qe_port *qe_port)
342 p = qe2cpu_addr(bdp->buf, qe_port);
360 qe_uart_stop_tx(port);
370 p = qe2cpu_addr(bdp->buf, qe_port);
371 while (count < qe_port->tx_fifosize) {
398 qe_uart_stop_tx(port);
411 static void qe_uart_start_tx(
struct uart_port *port)
417 if (
in_be16(&qe_port->
uccp->uccm) & UCC_UART_UCCE_TX)
421 if (qe_uart_tx_pump(qe_port))
422 setbits16(&qe_port->
uccp->uccm, UCC_UART_UCCE_TX);
428 static void qe_uart_stop_rx(
struct uart_port *port)
433 clrbits16(&qe_port->
uccp->uccm, UCC_UART_UCCE_RX);
442 static void qe_uart_enable_ms(
struct uart_port *port)
452 static void qe_uart_break_ctl(
struct uart_port *port,
int break_state)
467 static void qe_uart_int_rx(
struct uart_qe_port *qe_port)
470 unsigned char ch, *
cp;
482 status =
in_be16(&bdp->status);
495 dev_dbg(port->
dev,
"ucc-uart: no room in RX buffer\n");
500 cp = qe2cpu_addr(bdp->buf, qe_port);
515 tty_insert_flip_char(tty, ch, flg);
554 if (status & BD_SC_BR)
556 else if (status & BD_SC_PR)
558 else if (status & BD_SC_FR)
562 if (status & BD_SC_OV)
584 if (events & UCC_UART_UCCE_BRKE)
585 uart_handle_break(&qe_port->
port);
587 if (events & UCC_UART_UCCE_RX)
588 qe_uart_int_rx(qe_port);
590 if (events & UCC_UART_UCCE_TX)
591 qe_uart_tx_pump(qe_port);
600 static void qe_uart_initbd(
struct uart_qe_port *qe_port)
612 for (i = 0; i < (qe_port->
rx_nrfifos - 1); i++) {
614 out_be32(&bdp->buf, cpu2qe_addr(bd_virt, qe_port));
622 out_be32(&bdp->buf, cpu2qe_addr(bd_virt, qe_port));
633 for (i = 0; i < (qe_port->
tx_nrfifos - 1); i++) {
635 out_be32(&bdp->buf, cpu2qe_addr(bd_virt, qe_port));
647 out_be32(&bdp->buf, cpu2qe_addr(bd_virt, qe_port));
658 static void qe_uart_init_ucc(
struct uart_qe_port *qe_port)
670 out_8(&uccup->
common.rbmr, UCC_BMR_GBL | UCC_BMR_BO_BE);
671 out_8(&uccup->
common.tbmr, UCC_BMR_GBL | UCC_BMR_BO_BE);
682 for (i = 0; i < 8; i++)
689 clrsetbits_be32(&uccp->gumr_l,
690 UCC_SLOW_GUMR_L_MODE_MASK | UCC_SLOW_GUMR_L_TDCR_MASK |
691 UCC_SLOW_GUMR_L_RDCR_MASK,
692 UCC_SLOW_GUMR_L_MODE_UART | UCC_SLOW_GUMR_L_TDCR_1 |
693 UCC_SLOW_GUMR_L_RDCR_16);
695 clrsetbits_be32(&uccp->gumr_h, UCC_SLOW_GUMR_H_RFW,
696 UCC_SLOW_GUMR_H_TRX | UCC_SLOW_GUMR_H_TTX);
698 clrsetbits_be32(&uccp->gumr_l,
699 UCC_SLOW_GUMR_L_MODE_MASK | UCC_SLOW_GUMR_L_TDCR_MASK |
700 UCC_SLOW_GUMR_L_RDCR_MASK,
701 UCC_SLOW_GUMR_L_MODE_UART | UCC_SLOW_GUMR_L_TDCR_16 |
702 UCC_SLOW_GUMR_L_RDCR_16);
704 clrsetbits_be32(&uccp->gumr_h,
705 UCC_SLOW_GUMR_H_TRX | UCC_SLOW_GUMR_H_TTX,
706 UCC_SLOW_GUMR_H_RFW);
710 clrsetbits_be32(&uccp->gumr_l, UCC_SLOW_GUMR_L_DIAG_MASK,
711 UCC_SLOW_GUMR_L_DIAG_LOOP);
712 clrsetbits_be32(&uccp->gumr_h,
713 UCC_SLOW_GUMR_H_CTSP | UCC_SLOW_GUMR_H_RSYN,
714 UCC_SLOW_GUMR_H_CDS);
755 clrsetbits_be32(&uccp->gumr_l,
756 UCC_SLOW_GUMR_L_MODE_MASK | UCC_SLOW_GUMR_L_TDCR_MASK |
757 UCC_SLOW_GUMR_L_RDCR_MASK,
758 UCC_SLOW_GUMR_L_MODE_QMC | UCC_SLOW_GUMR_L_TDCR_16 |
759 UCC_SLOW_GUMR_L_RDCR_16);
761 clrsetbits_be32(&uccp->gumr_h,
762 UCC_SLOW_GUMR_H_RFW | UCC_SLOW_GUMR_H_RSYN,
764 UCC_SLOW_GUMR_H_TTX | UCC_SLOW_GUMR_H_TFL);
767 clrsetbits_be32(&uccp->gumr_l, UCC_SLOW_GUMR_L_DIAG_MASK,
768 UCC_SLOW_GUMR_L_DIAG_LOOP);
769 clrbits32(&uccp->gumr_h, UCC_SLOW_GUMR_H_CTSP |
770 UCC_SLOW_GUMR_H_CDS);
775 QE_CR_PROTOCOL_UNSPECIFIED, 0);
779 QE_CR_PROTOCOL_UART, 0);
786 static int qe_uart_startup(
struct uart_port *port)
796 if (soft_uart && !firmware_loaded) {
797 dev_err(port->
dev,
"Soft-UART firmware not uploaded\n");
801 qe_uart_initbd(qe_port);
802 qe_uart_init_ucc(qe_port);
813 setbits16(&qe_port->
uccp->uccm, UCC_UART_UCCE_RX);
822 static void qe_uart_shutdown(
struct uart_port *port)
832 while (!qe_uart_tx_empty(port)) {
849 clrbits16(&uccp->uccm, UCC_UART_UCCE_TX | UCC_UART_UCCE_RX);
853 qe_uart_initbd(qe_port);
861 static void qe_uart_set_termios(
struct uart_port *port,
881 upsmr &= UCC_UART_UPSMR_CL_MASK;
886 upsmr |= UCC_UART_UPSMR_CL_5;
891 upsmr |= UCC_UART_UPSMR_CL_6;
896 upsmr |= UCC_UART_UPSMR_CL_7;
901 upsmr |= UCC_UART_UPSMR_CL_8;
909 upsmr |= UCC_UART_UPSMR_SL;
915 upsmr |= UCC_UART_UPSMR_PEN;
920 upsmr &= ~(UCC_UART_UPSMR_RPM_MASK |
921 UCC_UART_UPSMR_TPM_MASK);
922 upsmr |= UCC_UART_UPSMR_RPM_EVEN |
923 UCC_UART_UPSMR_TPM_EVEN;
982 spin_unlock_irqrestore(&port->
lock, flags);
988 static const char *qe_uart_type(
struct uart_port *port)
996 static int qe_uart_request_port(
struct uart_port *port)
1003 unsigned int rx_size, tx_size;
1009 dev_err(port->
dev,
"could not initialize UCC%u\n",
1030 dev_err(port->
dev,
"could not allocate buffer descriptors\n");
1036 qe_port->
bd_size = rx_size + tx_size;
1038 qe_port->
rx_buf = bd_virt;
1051 static void qe_uart_config_port(
struct uart_port *port,
int flags)
1055 qe_uart_request_port(port);
1063 static void qe_uart_release_port(
struct uart_port *port)
1078 static int qe_uart_verify_port(
struct uart_port *port,
1096 static struct uart_ops qe_uart_pops = {
1097 .tx_empty = qe_uart_tx_empty,
1099 .get_mctrl = qe_uart_get_mctrl,
1100 .stop_tx = qe_uart_stop_tx,
1101 .start_tx = qe_uart_start_tx,
1102 .stop_rx = qe_uart_stop_rx,
1103 .enable_ms = qe_uart_enable_ms,
1104 .break_ctl = qe_uart_break_ctl,
1105 .startup = qe_uart_startup,
1106 .shutdown = qe_uart_shutdown,
1107 .set_termios = qe_uart_set_termios,
1108 .type = qe_uart_type,
1109 .release_port = qe_uart_release_port,
1110 .request_port = qe_uart_request_port,
1111 .config_port = qe_uart_config_port,
1112 .verify_port = qe_uart_verify_port,
1138 static unsigned int soc_info(
unsigned int *rev_h,
unsigned int *rev_l)
1141 const char *soc_string;
1153 soc_string = np->
name;
1156 if ((
sscanf(soc_string,
"PowerPC,%u", &soc) != 1) || !soc)
1160 svr =
mfspr(SPRN_SVR);
1161 *rev_h = (svr >> 4) & 0xf;
1180 dev_err(dev,
"firmware not found\n");
1184 firmware = (
struct qe_firmware *) fw->
data;
1186 if (firmware->header.length != fw->
size) {
1187 dev_err(dev,
"invalid firmware\n");
1193 dev_err(dev,
"could not load firmware\n");
1197 firmware_loaded = 1;
1205 const unsigned int *iprop;
1215 dev_dbg(&ofdev->
dev,
"using Soft-UART mode\n");
1224 struct qe_firmware_info *qe_fw_info;
1229 if (qe_fw_info &&
strstr(qe_fw_info->id,
"Soft-UART")) {
1230 firmware_loaded = 1;
1237 soc = soc_info(&rev_h, &rev_l);
1242 sprintf(filename,
"fsl_qe_ucode_uart_%u_%u%u.bin",
1245 dev_info(&ofdev->
dev,
"waiting for firmware %s\n",
1260 "could not load firmware %s\n",
1269 dev_err(&ofdev->
dev,
"can't allocate QE port structure\n");
1276 dev_err(&ofdev->
dev,
"missing 'reg' property in device tree\n");
1280 dev_err(&ofdev->
dev,
"invalid 'reg' property in device tree\n");
1284 qe_port->
port.mapbase =
res.start;
1292 dev_err(&ofdev->
dev,
"UCC is unspecified in "
1300 dev_err(&ofdev->
dev,
"no support for UCC%u\n", *iprop);
1304 qe_port->
ucc_num = *iprop - 1;
1315 dev_err(&ofdev->
dev,
"missing rx-clock-name in device tree\n");
1321 if ((qe_port->
us_info.rx_clock < QE_BRG1) ||
1322 (qe_port->
us_info.rx_clock > QE_BRG16)) {
1323 dev_err(&ofdev->
dev,
"rx-clock-name must be a BRG for UART\n");
1334 dev_err(&ofdev->
dev,
"missing tx-clock-name in device tree\n");
1340 if ((qe_port->
us_info.tx_clock < QE_BRG1) ||
1341 (qe_port->
us_info.tx_clock > QE_BRG16)) {
1342 dev_err(&ofdev->
dev,
"tx-clock-name must be a BRG for UART\n");
1350 dev_err(&ofdev->
dev,
"missing port-number in device tree\n");
1354 qe_port->
port.line = *iprop;
1356 dev_err(&ofdev->
dev,
"port-number must be 0-%u\n",
1363 if (qe_port->
port.irq == 0) {
1364 dev_err(&ofdev->
dev,
"could not map IRQ for UCC%u\n",
1378 dev_err(&ofdev->
dev,
"could not find 'qe' node\n");
1387 "missing brg-frequency in device tree\n");
1393 qe_port->
port.uartclk = *iprop;
1403 "missing QE bus-frequency in device tree\n");
1408 qe_port->
port.uartclk = *iprop / 2;
1411 "invalid QE bus-frequency in device tree\n");
1419 qe_port->
port.dev = &ofdev->
dev;
1420 qe_port->
port.ops = &qe_uart_pops;
1429 qe_port->
port.fifosize = 512;
1449 dev_err(&ofdev->
dev,
"could not add /dev/ttyQE%u\n",
1450 qe_port->
port.line);
1456 dev_info(&ofdev->
dev,
"UCC%u assigned to /dev/ttyQE%u\n",
1460 dev_dbg(&ofdev->
dev,
"mknod command is 'mknod /dev/ttyQE%u c %u %u'\n",
1489 .compatible =
"ucc_uart",
1499 .of_match_table = ucc_uart_match,
1501 .probe = ucc_uart_probe,
1502 .remove = ucc_uart_remove,
1505 static int __init ucc_uart_init(
void)
1523 "ucc-uart: could not register platform driver\n");
1528 static void __exit ucc_uart_exit(
void)
1531 "Freescale QUICC Engine UART device driver unloading\n");