34 #include <linux/device.h>
35 #include <linux/module.h>
36 #include <linux/tty.h>
38 #include <linux/serial.h>
50 #if defined(CONFIG_SERIAL_MPC52xx_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
54 #include <linux/serial_core.h>
58 #define SERIAL_PSC_MAJOR 204
59 #define SERIAL_PSC_MINOR 148
62 #define ISR_PASS_LIMIT 256
77 static void mpc52xx_uart_of_enumerate(
void);
80 #define PSC(port) ((struct mpc52xx_psc __iomem *)((port)->membase))
90 #ifdef CONFIG_SERIAL_CORE_CONSOLE
91 #define uart_console(port) \
92 ((port)->cons && (port)->cons->index == (port)->line)
94 #define uart_console(port) (0)
132 out_be16(&psc->mpc52xx_psc_clock_select, prescaler);
133 out_8(&psc->ctur, divisor >> 8);
134 out_8(&psc->ctlr, divisor & 0xff);
137 #ifdef CONFIG_PPC_MPC52xx
138 #define FIFO_52xx(port) ((struct mpc52xx_psc_fifo __iomem *)(PSC(port)+1))
159 static int mpc52xx_psc_raw_tx_rdy(
struct uart_port *port)
166 static int mpc52xx_psc_rx_rdy(
struct uart_port *port)
173 static int mpc52xx_psc_tx_rdy(
struct uart_port *port)
180 static int mpc52xx_psc_tx_empty(
struct uart_port *port)
186 static void mpc52xx_psc_start_tx(
struct uart_port *port)
192 static void mpc52xx_psc_stop_tx(
struct uart_port *port)
198 static void mpc52xx_psc_stop_rx(
struct uart_port *port)
204 static void mpc52xx_psc_rx_clr_irq(
struct uart_port *port)
208 static void mpc52xx_psc_tx_clr_irq(
struct uart_port *port)
212 static void mpc52xx_psc_write_char(
struct uart_port *port,
unsigned char c)
217 static unsigned char mpc52xx_psc_read_char(
struct uart_port *port)
222 static void mpc52xx_psc_cw_disable_ints(
struct uart_port *port)
227 static void mpc52xx_psc_cw_restore_ints(
struct uart_port *port)
232 static unsigned int mpc5200_psc_set_baudrate(
struct uart_port *port,
241 port->
uartclk / (32 * 0xffff) + 1,
243 divisor = (port->
uartclk + 16 *
baud) / (32 * baud);
246 mpc52xx_set_divisor(
PSC(port), 0xdd00, divisor);
250 static unsigned int mpc5200b_psc_set_baudrate(
struct uart_port *port,
261 port->
uartclk / (32 * 0xffff) + 1,
267 if (divisor > 0xffff || baud <= 115200) {
268 divisor = (divisor + 4) / 8;
272 mpc52xx_set_divisor(
PSC(port), prescaler, divisor);
285 return mpc5xxx_uart_process_int(port);
288 static struct psc_ops mpc52xx_psc_ops = {
290 .raw_rx_rdy = mpc52xx_psc_raw_rx_rdy,
291 .raw_tx_rdy = mpc52xx_psc_raw_tx_rdy,
292 .rx_rdy = mpc52xx_psc_rx_rdy,
293 .tx_rdy = mpc52xx_psc_tx_rdy,
294 .tx_empty = mpc52xx_psc_tx_empty,
295 .stop_rx = mpc52xx_psc_stop_rx,
296 .start_tx = mpc52xx_psc_start_tx,
297 .stop_tx = mpc52xx_psc_stop_tx,
298 .rx_clr_irq = mpc52xx_psc_rx_clr_irq,
299 .tx_clr_irq = mpc52xx_psc_tx_clr_irq,
300 .write_char = mpc52xx_psc_write_char,
301 .read_char = mpc52xx_psc_read_char,
302 .cw_disable_ints = mpc52xx_psc_cw_disable_ints,
303 .cw_restore_ints = mpc52xx_psc_cw_restore_ints,
304 .set_baudrate = mpc5200_psc_set_baudrate,
305 .get_irq = mpc52xx_psc_get_irq,
306 .handle_irq = mpc52xx_psc_handle_irq,
309 static struct psc_ops mpc5200b_psc_ops = {
311 .raw_rx_rdy = mpc52xx_psc_raw_rx_rdy,
312 .raw_tx_rdy = mpc52xx_psc_raw_tx_rdy,
313 .rx_rdy = mpc52xx_psc_rx_rdy,
314 .tx_rdy = mpc52xx_psc_tx_rdy,
315 .tx_empty = mpc52xx_psc_tx_empty,
316 .stop_rx = mpc52xx_psc_stop_rx,
317 .start_tx = mpc52xx_psc_start_tx,
318 .stop_tx = mpc52xx_psc_stop_tx,
319 .rx_clr_irq = mpc52xx_psc_rx_clr_irq,
320 .tx_clr_irq = mpc52xx_psc_tx_clr_irq,
321 .write_char = mpc52xx_psc_write_char,
322 .read_char = mpc52xx_psc_read_char,
323 .cw_disable_ints = mpc52xx_psc_cw_disable_ints,
324 .cw_restore_ints = mpc52xx_psc_cw_restore_ints,
325 .set_baudrate = mpc5200b_psc_set_baudrate,
326 .get_irq = mpc52xx_psc_get_irq,
327 .handle_irq = mpc52xx_psc_handle_irq,
332 #ifdef CONFIG_PPC_MPC512x
333 #define FIFO_512x(port) ((struct mpc512x_psc_fifo __iomem *)(PSC(port)+1))
344 static struct psc_fifoc
__iomem *psc_fifoc;
345 static unsigned int psc_fifoc_irq;
354 out_be32(&FIFO_512x(port)->txalarm, 1);
355 out_be32(&FIFO_512x(port)->tximr, 0);
359 out_be32(&FIFO_512x(port)->rxalarm, 1);
360 out_be32(&FIFO_512x(port)->rximr, 0);
366 static int mpc512x_psc_raw_rx_rdy(
struct uart_port *port)
371 static int mpc512x_psc_raw_tx_rdy(
struct uart_port *port)
376 static int mpc512x_psc_rx_rdy(
struct uart_port *port)
378 return in_be32(&FIFO_512x(port)->rxsr)
379 &
in_be32(&FIFO_512x(port)->rximr)
383 static int mpc512x_psc_tx_rdy(
struct uart_port *port)
385 return in_be32(&FIFO_512x(port)->txsr)
386 &
in_be32(&FIFO_512x(port)->tximr)
390 static int mpc512x_psc_tx_empty(
struct uart_port *port)
392 return in_be32(&FIFO_512x(port)->txsr)
396 static void mpc512x_psc_stop_rx(
struct uart_port *port)
398 unsigned long rx_fifo_imr;
400 rx_fifo_imr =
in_be32(&FIFO_512x(port)->rximr);
402 out_be32(&FIFO_512x(port)->rximr, rx_fifo_imr);
405 static void mpc512x_psc_start_tx(
struct uart_port *port)
407 unsigned long tx_fifo_imr;
409 tx_fifo_imr =
in_be32(&FIFO_512x(port)->tximr);
411 out_be32(&FIFO_512x(port)->tximr, tx_fifo_imr);
414 static void mpc512x_psc_stop_tx(
struct uart_port *port)
416 unsigned long tx_fifo_imr;
418 tx_fifo_imr =
in_be32(&FIFO_512x(port)->tximr);
420 out_be32(&FIFO_512x(port)->tximr, tx_fifo_imr);
423 static void mpc512x_psc_rx_clr_irq(
struct uart_port *port)
428 static void mpc512x_psc_tx_clr_irq(
struct uart_port *port)
433 static void mpc512x_psc_write_char(
struct uart_port *port,
unsigned char c)
438 static unsigned char mpc512x_psc_read_char(
struct uart_port *port)
443 static void mpc512x_psc_cw_disable_ints(
struct uart_port *port)
446 in_be32(&FIFO_512x(port)->tximr) << 16 |
447 in_be32(&FIFO_512x(port)->rximr);
448 out_be32(&FIFO_512x(port)->tximr, 0);
449 out_be32(&FIFO_512x(port)->rximr, 0);
452 static void mpc512x_psc_cw_restore_ints(
struct uart_port *port)
459 static unsigned int mpc512x_psc_set_baudrate(
struct uart_port *port,
481 port->
uartclk / (16 * 0xffff) + 1,
483 divisor = (port->
uartclk + 8 *
baud) / (16 * baud);
486 mpc52xx_set_divisor(
PSC(port), 0xdd00, divisor);
491 static int __init mpc512x_psc_fifoc_init(
void)
496 "fsl,mpc5121-psc-fifo");
498 pr_err(
"%s: Can't find FIFOC node\n", __func__);
504 pr_err(
"%s: Can't map FIFOC\n", __func__);
511 if (psc_fifoc_irq == 0) {
512 pr_err(
"%s: Can't get FIFOC irq\n", __func__);
520 static void __exit mpc512x_psc_fifoc_uninit(
void)
528 unsigned long fifoc_int;
532 fifoc_int =
in_be32(&psc_fifoc->fifoc_int);
535 psc_num = (port->
mapbase & 0xf00) >> 8;
536 if (
test_bit(psc_num, &fifoc_int) ||
538 return mpc5xxx_uart_process_int(port);
552 psc_num = (port->
mapbase & 0xf00) >> 8;
553 snprintf(clk_name,
sizeof(clk_name),
"psc%d_clk", psc_num);
555 if (IS_ERR(psc_clk)) {
556 dev_err(port->
dev,
"Failed to get PSC clock entry!\n");
560 dev_dbg(port->
dev,
"%s %sable\n", clk_name, enable ?
"en" :
"dis");
573 port->
irq = psc_fifoc_irq;
576 static struct psc_ops mpc512x_psc_ops = {
578 .raw_rx_rdy = mpc512x_psc_raw_rx_rdy,
579 .raw_tx_rdy = mpc512x_psc_raw_tx_rdy,
580 .rx_rdy = mpc512x_psc_rx_rdy,
581 .tx_rdy = mpc512x_psc_tx_rdy,
582 .tx_empty = mpc512x_psc_tx_empty,
583 .stop_rx = mpc512x_psc_stop_rx,
584 .start_tx = mpc512x_psc_start_tx,
585 .stop_tx = mpc512x_psc_stop_tx,
586 .rx_clr_irq = mpc512x_psc_rx_clr_irq,
587 .tx_clr_irq = mpc512x_psc_tx_clr_irq,
588 .write_char = mpc512x_psc_write_char,
589 .read_char = mpc512x_psc_read_char,
590 .cw_disable_ints = mpc512x_psc_cw_disable_ints,
591 .cw_restore_ints = mpc512x_psc_cw_restore_ints,
592 .set_baudrate = mpc512x_psc_set_baudrate,
593 .clock = mpc512x_psc_clock,
594 .fifoc_init = mpc512x_psc_fifoc_init,
595 .fifoc_uninit = mpc512x_psc_fifoc_uninit,
596 .get_irq = mpc512x_psc_get_irq,
597 .handle_irq = mpc512x_psc_handle_irq,
608 mpc52xx_uart_tx_empty(
struct uart_port *port)
614 mpc52xx_uart_set_mctrl(
struct uart_port *port,
unsigned int mctrl)
623 mpc52xx_uart_get_mctrl(
struct uart_port *port)
637 mpc52xx_uart_stop_tx(
struct uart_port *port)
644 mpc52xx_uart_start_tx(
struct uart_port *port)
651 mpc52xx_uart_send_xchar(
struct uart_port *port,
char ch)
663 spin_unlock_irqrestore(&port->
lock, flags);
667 mpc52xx_uart_stop_rx(
struct uart_port *port)
674 mpc52xx_uart_enable_ms(
struct uart_port *port)
679 in_8(&psc->mpc52xx_psc_ipcr);
698 spin_unlock_irqrestore(&port->
lock, flags);
702 mpc52xx_uart_startup(
struct uart_port *port)
707 if (psc_ops->
clock) {
708 ret = psc_ops->
clock(port, 1);
715 port->
irqflags,
"mpc52xx_psc_uart", port);
734 mpc52xx_uart_shutdown(
struct uart_port *port)
747 psc_ops->
clock(port, 0);
759 unsigned char mr1, mr2;
766 switch (new->c_cflag &
CSIZE) {
777 if (new->c_cflag &
PARENB) {
778 if (new->c_cflag &
CMSPAR)
782 mr1 |= (
new->c_cflag &
PARODD) ?
790 if (new->c_cflag &
CSTOPB)
793 mr2 |= ((
new->c_cflag &
CSIZE) ==
CS5) ?
812 while (!mpc52xx_uart_tx_empty(port) && --j)
817 "Unable to flush RX & TX fifos in-time in set_termios."
818 "Some chars may have been lost.\n");
834 mpc52xx_uart_enable_ms(port);
841 spin_unlock_irqrestore(&port->
lock, flags);
845 mpc52xx_uart_type(
struct uart_port *port)
855 mpc52xx_uart_release_port(
struct uart_port *port)
867 mpc52xx_uart_request_port(
struct uart_port *port)
879 "mpc52xx_psc_uart") !=
NULL ? 0 : -
EBUSY;
890 mpc52xx_uart_config_port(
struct uart_port *port,
int flags)
893 && (mpc52xx_uart_request_port(port) == 0))
903 if ((ser->
irq != port->
irq) ||
914 static struct uart_ops mpc52xx_uart_ops = {
915 .tx_empty = mpc52xx_uart_tx_empty,
916 .set_mctrl = mpc52xx_uart_set_mctrl,
917 .get_mctrl = mpc52xx_uart_get_mctrl,
918 .stop_tx = mpc52xx_uart_stop_tx,
919 .start_tx = mpc52xx_uart_start_tx,
920 .send_xchar = mpc52xx_uart_send_xchar,
921 .stop_rx = mpc52xx_uart_stop_rx,
922 .enable_ms = mpc52xx_uart_enable_ms,
923 .break_ctl = mpc52xx_uart_break_ctl,
924 .startup = mpc52xx_uart_startup,
925 .shutdown = mpc52xx_uart_shutdown,
926 .set_termios = mpc52xx_uart_set_termios,
929 .type = mpc52xx_uart_type,
930 .release_port = mpc52xx_uart_release_port,
931 .request_port = mpc52xx_uart_request_port,
932 .config_port = mpc52xx_uart_config_port,
933 .verify_port = mpc52xx_uart_verify_port
942 mpc52xx_uart_int_rx_chars(
struct uart_port *port)
945 unsigned char ch,
flag;
974 uart_handle_break(port);
989 tty_insert_flip_char(tty, ch, flag);
1001 spin_unlock(&port->
lock);
1003 spin_lock(&port->
lock);
1009 mpc52xx_uart_int_tx_chars(
struct uart_port *port)
1023 mpc52xx_uart_stop_tx(port);
1042 mpc52xx_uart_stop_tx(port);
1050 mpc5xxx_uart_process_int(
struct uart_port *port)
1053 unsigned int keepgoing;
1062 if (psc_ops->
rx_rdy(port))
1063 keepgoing |= mpc52xx_uart_int_rx_chars(port);
1066 if (psc_ops->
tx_rdy(port))
1067 keepgoing |= mpc52xx_uart_int_tx_chars(port);
1080 }
while (keepgoing);
1086 mpc52xx_uart_int(
int irq,
void *
dev_id)
1091 spin_lock(&port->
lock);
1095 spin_unlock(&port->
lock);
1104 #ifdef CONFIG_SERIAL_MPC52xx_CONSOLE
1107 mpc52xx_console_get_options(
struct uart_port *port,
1108 int *baud,
int *parity,
int *
bits,
int *flow)
1113 pr_debug(
"mpc52xx_console_get_options(port=%p)\n", port);
1120 *baud = CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD;
1145 mpc52xx_console_write(
struct console *co,
const char *
s,
unsigned int count)
1155 while (!mpc52xx_uart_tx_empty(port) && --j)
1159 for (i = 0; i <
count; i++, s++) {
1169 while (!mpc52xx_uart_tx_empty(port) && --j)
1183 unsigned int uartclk;
1187 int baud = CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD;
1192 pr_debug(
"mpc52xx_console_setup co=%p, co->index=%i, options=%s\n",
1193 co, co->
index, options);
1205 pr_debug(
"Console on ttyPSC%x is %s\n",
1217 pr_debug(
"Could not find uart clock frequency!\n");
1225 port->
ops = &mpc52xx_uart_ops;
1233 pr_debug(
"mpc52xx-psc uart at %p, mapped to %p, irq=%x, freq=%i\n",
1241 mpc52xx_console_get_options(port, &baud, &parity, &bits, &flow);
1243 pr_debug(
"Setting console parameters: %i %i%c1 flow=%c\n",
1244 baud, bits, parity, flow);
1252 static struct console mpc52xx_console = {
1254 .write = mpc52xx_console_write,
1256 .setup = mpc52xx_console_setup,
1259 .data = &mpc52xx_uart_driver,
1264 mpc52xx_console_init(
void)
1266 mpc52xx_uart_of_enumerate();
1273 #define MPC52xx_PSC_CONSOLE &mpc52xx_console
1275 #define MPC52xx_PSC_CONSOLE NULL
1284 .driver_name =
"mpc52xx_psc_uart",
1285 .dev_name =
"ttyPSC",
1297 #ifdef CONFIG_PPC_MPC52xx
1298 { .compatible =
"fsl,mpc5200b-psc-uart", .data = &mpc5200b_psc_ops, },
1299 { .compatible =
"fsl,mpc5200-psc-uart", .data = &mpc52xx_psc_ops, },
1301 { .compatible =
"mpc5200-psc-uart", .data = &mpc52xx_psc_ops, },
1303 { .compatible =
"mpc5200-serial", .data = &mpc52xx_psc_ops, },
1305 #ifdef CONFIG_PPC_MPC512x
1306 { .compatible =
"fsl,mpc5121-psc-uart", .data = &mpc512x_psc_ops, },
1314 unsigned int uartclk;
1321 if (mpc52xx_uart_nodes[idx] == op->
dev.of_node)
1323 if (idx >= MPC52xx_PSC_MAXNUM)
1325 pr_debug(
"Found %s assigned to ttyPSC%x\n",
1326 mpc52xx_uart_nodes[idx]->full_name, idx);
1333 dev_dbg(&op->
dev,
"Could not find uart clock frequency!\n");
1338 port = &mpc52xx_uart_ports[
idx];
1347 port->
ops = &mpc52xx_uart_ops;
1357 dev_dbg(&op->
dev,
"Could not allocate resources for PSC\n");
1362 if (port->
irq == 0) {
1367 dev_dbg(&op->
dev,
"mpc52xx-psc uart at %p, irq=%x, freq=%i\n",
1422 if (mpc52xx_uart_nodes[i] ==
NULL) {
1424 mpc52xx_uart_nodes[
i] = np;
1431 mpc52xx_uart_of_enumerate(
void)
1433 static int enum_done;
1442 for_each_matching_node(np, mpc52xx_uart_of_match) {
1444 psc_ops = match->
data;
1445 mpc52xx_uart_of_assign(np);
1451 if (mpc52xx_uart_nodes[i])
1452 pr_debug(
"%s assigned to ttyPSC%x\n",
1453 mpc52xx_uart_nodes[i]->full_name, i);
1460 .probe = mpc52xx_uart_of_probe,
1461 .remove = mpc52xx_uart_of_remove,
1463 .suspend = mpc52xx_uart_of_suspend,
1464 .resume = mpc52xx_uart_of_resume,
1467 .name =
"mpc52xx-psc-uart",
1469 .of_match_table = mpc52xx_uart_of_match,
1479 mpc52xx_uart_init(
void)
1492 mpc52xx_uart_of_enumerate();
1515 mpc52xx_uart_exit(
void)