1 2 3/* Platform device Usage : 4 * 5 * Since PSCs can have multiple function, the correct driver for each one 6 * is selected by calling mpc52xx_match_psc_function(...). The function 7 * handled by this driver is "uart". 8 * 9 * The driver init all necessary registers to place the PSC in uart mode without 10 * DCD. However, the pin multiplexing aren't changed and should be set either 11 * by the bootloader or in the platform init code. 12 * 13 * The idx field must be equal to the PSC index ( e.g. 0 for PSC1, 1 for PSC2, 14 * and so on). So the PSC1 is mapped to /dev/ttyPSC0, PSC2 to /dev/ttyPSC1 and 15 * so on. But be warned, it's an ABSOLUTE REQUIREMENT ! This is needed mainly 16 * fpr the console code : without this 1:1 mapping, at early boot time, when we 17 * are parsing the kernel args console=ttyPSC?, we wouldn't know which PSC it 18 * will be mapped to. 19 */ 20 21/* OF Platform device Usage : 22 * 23 * This driver is only used for PSCs configured in uart mode. The device 24 * tree will have a node for each PSC in uart mode w/ device_type = "serial" 25 * and "mpc52xx-psc-uart" in the compatible string 26 * 27 * By default, PSC devices are enumerated in the order they are found. However 28 * a particular PSC number can be forces by adding 'device_no = <port#>' 29 * to the device node. 30 * 31 * The driver init all necessary registers to place the PSC in uart mode without 32 * DCD. However, the pin multiplexing aren't changed and should be set either 33 * by the bootloader or in the platform init code. 34 */ 35 36#undef DEBUG 37 38#include <linux/device.h> 39#include <linux/module.h> 40#include <linux/tty.h> 41#include <linux/serial.h> 42#include <linux/sysrq.h> 43#include <linux/console.h> 44 45#include <asm/delay.h> 46#include <asm/io.h> 47 48#if defined(CONFIG_PPC_MERGE) 49#include <asm/of_platform.h> 50#else 51#include <linux/platform_device.h> 52#endif 53 54#include <asm/mpc52xx.h> 55#include <asm/mpc52xx_psc.h> 56 57#if defined(CONFIG_SERIAL_MPC52xx_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) 58#define SUPPORT_SYSRQ 59#endif 60 61#include <linux/serial_core.h> 62 63 64/* We've been assigned a range on the "Low-density serial ports" major */ 65#define SERIAL_PSC_MAJOR 204 66#define SERIAL_PSC_MINOR 148 67 68 69#define ISR_PASS_LIMIT 256 /* Max number of iteration in the interrupt */ 70 71 72static struct uart_port mpc52xx_uart_ports[MPC52xx_PSC_MAXNUM]; 73 /* Rem: - We use the read_status_mask as a shadow of 74 * psc->mpc52xx_psc_imr 75 * - It's important that is array is all zero on start as we 76 * use it to know if it's initialized or not ! If it's not sure 77 * it's cleared, then a memset(...,0,...) should be added to 78 * the console_init 79 */ 80#if defined(CONFIG_PPC_MERGE) 81/* lookup table for matching device nodes to index numbers */ 82static struct device_node *mpc52xx_uart_nodes[MPC52xx_PSC_MAXNUM]; 83 84static void mpc52xx_uart_of_enumerate(void); 85#endif 86 87#define PSC(port) ((struct mpc52xx_psc __iomem *)((port)->membase)) 88 89 90/* Forward declaration of the interruption handling routine */ 91static irqreturn_t mpc52xx_uart_int(int irq,void *dev_id); 92 93 94/* Simple macro to test if a port is console or not. This one is taken 95 * for serial_core.c and maybe should be moved to serial_core.h ? */ 96#ifdef CONFIG_SERIAL_CORE_CONSOLE 97#define uart_console(port) ((port)->cons && (port)->cons->index == (port)->line) 98#else 99#define uart_console(port) (0) 100#endif 101 102#if defined(CONFIG_PPC_MERGE) 103static struct of_device_id mpc52xx_uart_of_match[] = { 104 { .type = "serial", .compatible = "mpc5200-psc-uart", }, 105 {}, 106}; 107#endif 108 109 110/* ======================================================================== */ 111/* UART operations */ 112/* ======================================================================== */ 113 114static unsigned int 115mpc52xx_uart_tx_empty(struct uart_port *port) 116{ 117 int status = in_be16(&PSC(port)->mpc52xx_psc_status); 118 return (status & MPC52xx_PSC_SR_TXEMP) ? TIOCSER_TEMT : 0; 119} 120 121static void 122mpc52xx_uart_set_mctrl(struct uart_port *port, unsigned int mctrl) 123{ 124 /* Not implemented */ 125} 126 127static unsigned int 128mpc52xx_uart_get_mctrl(struct uart_port *port) 129{ 130 /* Not implemented */ 131 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR; 132} 133 134static void 135mpc52xx_uart_stop_tx(struct uart_port *port) 136{ 137 /* port->lock taken by caller */ 138 port->read_status_mask &= ~MPC52xx_PSC_IMR_TXRDY; 139 out_be16(&PSC(port)->mpc52xx_psc_imr,port->read_status_mask); 140} 141 142static void 143mpc52xx_uart_start_tx(struct uart_port *port) 144{ 145 /* port->lock taken by caller */ 146 port->read_status_mask |= MPC52xx_PSC_IMR_TXRDY; 147 out_be16(&PSC(port)->mpc52xx_psc_imr,port->read_status_mask); 148} 149 150static void 151mpc52xx_uart_send_xchar(struct uart_port *port, char ch) 152{ 153 unsigned long flags; 154 spin_lock_irqsave(&port->lock, flags); 155 156 port->x_char = ch; 157 if (ch) { 158 /* Make sure tx interrupts are on */ 159 /* Truly necessary ??? They should be anyway */ 160 port->read_status_mask |= MPC52xx_PSC_IMR_TXRDY; 161 out_be16(&PSC(port)->mpc52xx_psc_imr,port->read_status_mask); 162 } 163 164 spin_unlock_irqrestore(&port->lock, flags); 165} 166 167static void 168mpc52xx_uart_stop_rx(struct uart_port *port) 169{ 170 /* port->lock taken by caller */ 171 port->read_status_mask &= ~MPC52xx_PSC_IMR_RXRDY; 172 out_be16(&PSC(port)->mpc52xx_psc_imr,port->read_status_mask); 173} 174 175static void 176mpc52xx_uart_enable_ms(struct uart_port *port) 177{ 178 /* Not implemented */ 179} 180 181static void 182mpc52xx_uart_break_ctl(struct uart_port *port, int ctl) 183{ 184 unsigned long flags; 185 spin_lock_irqsave(&port->lock, flags); 186 187 if ( ctl == -1 ) 188 out_8(&PSC(port)->command,MPC52xx_PSC_START_BRK); 189 else 190 out_8(&PSC(port)->command,MPC52xx_PSC_STOP_BRK); 191 192 spin_unlock_irqrestore(&port->lock, flags); 193} 194 195static int 196mpc52xx_uart_startup(struct uart_port *port) 197{ 198 struct mpc52xx_psc __iomem *psc = PSC(port); 199 int ret; 200 201 /* Request IRQ */ 202 ret = request_irq(port->irq, mpc52xx_uart_int, 203 IRQF_DISABLED | IRQF_SAMPLE_RANDOM, "mpc52xx_psc_uart", port); 204 if (ret) 205 return ret; 206 207 /* Reset/activate the port, clear and enable interrupts */ 208 out_8(&psc->command,MPC52xx_PSC_RST_RX); 209 out_8(&psc->command,MPC52xx_PSC_RST_TX); 210 211 out_be32(&psc->sicr,0); /* UART mode DCD ignored */ 212 213 out_be16(&psc->mpc52xx_psc_clock_select, 0xdd00); /* /16 prescaler on */ 214 215 out_8(&psc->rfcntl, 0x00); 216 out_be16(&psc->rfalarm, 0x1ff); 217 out_8(&psc->tfcntl, 0x07); 218 out_be16(&psc->tfalarm, 0x80); 219 220 port->read_status_mask |= MPC52xx_PSC_IMR_RXRDY | MPC52xx_PSC_IMR_TXRDY; 221 out_be16(&psc->mpc52xx_psc_imr,port->read_status_mask); 222 223 out_8(&psc->command,MPC52xx_PSC_TX_ENABLE); 224 out_8(&psc->command,MPC52xx_PSC_RX_ENABLE); 225 226 return 0; 227} 228 229static void 230mpc52xx_uart_shutdown(struct uart_port *port) 231{ 232 struct mpc52xx_psc __iomem *psc = PSC(port); 233 234 /* Shut down the port. Leave TX active if on a console port */ 235 out_8(&psc->command,MPC52xx_PSC_RST_RX); 236 if (!uart_console(port)) 237 out_8(&psc->command,MPC52xx_PSC_RST_TX); 238 239 port->read_status_mask = 0; 240 out_be16(&psc->mpc52xx_psc_imr,port->read_status_mask); 241 242 /* Release interrupt */ 243 free_irq(port->irq, port); 244} 245 246static void 247mpc52xx_uart_set_termios(struct uart_port *port, struct ktermios *new, 248 struct ktermios *old) 249{ 250 struct mpc52xx_psc __iomem *psc = PSC(port); 251 unsigned long flags; 252 unsigned char mr1, mr2; 253 unsigned short ctr; 254 unsigned int j, baud, quot; 255 256 /* Prepare what we're gonna write */ 257 mr1 = 0; 258 259 switch (new->c_cflag & CSIZE) { 260 case CS5: mr1 |= MPC52xx_PSC_MODE_5_BITS; 261 break; 262 case CS6: mr1 |= MPC52xx_PSC_MODE_6_BITS; 263 break; 264 case CS7: mr1 |= MPC52xx_PSC_MODE_7_BITS; 265 break; 266 case CS8: 267 default: mr1 |= MPC52xx_PSC_MODE_8_BITS; 268 } 269 270 if (new->c_cflag & PARENB) { 271 mr1 |= (new->c_cflag & PARODD) ? 272 MPC52xx_PSC_MODE_PARODD : MPC52xx_PSC_MODE_PAREVEN; 273 } else 274 mr1 |= MPC52xx_PSC_MODE_PARNONE; 275 276 277 mr2 = 0; 278 279 if (new->c_cflag & CSTOPB) 280 mr2 |= MPC52xx_PSC_MODE_TWO_STOP; 281 else 282 mr2 |= ((new->c_cflag & CSIZE) == CS5) ? 283 MPC52xx_PSC_MODE_ONE_STOP_5_BITS : 284 MPC52xx_PSC_MODE_ONE_STOP; 285 286 287 baud = uart_get_baud_rate(port, new, old, 0, port->uartclk/16); 288 quot = uart_get_divisor(port, baud); 289 ctr = quot & 0xffff; 290 291 /* Get the lock */ 292 spin_lock_irqsave(&port->lock, flags); 293 294 /* Update the per-port timeout */ 295 uart_update_timeout(port, new->c_cflag, baud); 296 297 /* Do our best to flush TX & RX, so we don't loose anything */ 298 /* But we don't wait indefinitly ! */ 299 j = 5000000; /* Maximum wait */ 300 /* while (j-- && mpc52xx_uart_int_rx_chars(port)); */ 301 while (!(in_be16(&psc->mpc52xx_psc_status) & MPC52xx_PSC_SR_TXEMP) && 302 --j) 303 udelay(1); 304 305 if (!j) 306 printk( KERN_ERR "mpc52xx_uart.c: " 307 "Unable to flush RX & TX fifos in-time in set_termios." 308 "Some chars may have been lost.\n" ); 309 310 /* Reset the TX & RX */ 311 out_8(&psc->command,MPC52xx_PSC_RST_RX); 312 out_8(&psc->command,MPC52xx_PSC_RST_TX); 313 314 /* Send new mode settings */ 315 out_8(&psc->command,MPC52xx_PSC_SEL_MODE_REG_1); 316 out_8(&psc->mode,mr1); 317 out_8(&psc->mode,mr2); 318 out_8(&psc->ctur,ctr >> 8); 319 out_8(&psc->ctlr,ctr & 0xff); 320 321 /* Reenable TX & RX */ 322 out_8(&psc->command,MPC52xx_PSC_TX_ENABLE); 323 out_8(&psc->command,MPC52xx_PSC_RX_ENABLE); 324 325 /* We're all set, release the lock */ 326 spin_unlock_irqrestore(&port->lock, flags); 327} 328 329static const char * 330mpc52xx_uart_type(struct uart_port *port) 331{ 332 return port->type == PORT_MPC52xx ? "MPC52xx PSC" : NULL; 333} 334 335static void 336mpc52xx_uart_release_port(struct uart_port *port) 337{ 338 if (port->flags & UPF_IOREMAP) { /* remapped by us ? */ 339 iounmap(port->membase); 340 port->membase = NULL; 341 } 342 343 release_mem_region(port->mapbase, sizeof(struct mpc52xx_psc)); 344} 345 346static int 347mpc52xx_uart_request_port(struct uart_port *port) 348{ 349 int err; 350 351 if (port->flags & UPF_IOREMAP) /* Need to remap ? */ 352 port->membase = ioremap(port->mapbase, 353 sizeof(struct mpc52xx_psc)); 354 355 if (!port->membase) 356 return -EINVAL; 357 358 err = request_mem_region(port->mapbase, sizeof(struct mpc52xx_psc), 359 "mpc52xx_psc_uart") != NULL ? 0 : -EBUSY; 360 361 if (err && (port->flags & UPF_IOREMAP)) { 362 iounmap(port->membase); 363 port->membase = NULL; 364 } 365 366 return err; 367} 368 369static void 370mpc52xx_uart_config_port(struct uart_port *port, int flags) 371{ 372 if ( (flags & UART_CONFIG_TYPE) && 373 (mpc52xx_uart_request_port(port) == 0) ) 374 port->type = PORT_MPC52xx; 375} 376 377static int 378mpc52xx_uart_verify_port(struct uart_port *port, struct serial_struct *ser) 379{ 380 if ( ser->type != PORT_UNKNOWN && ser->type != PORT_MPC52xx ) 381 return -EINVAL; 382 383 if ( (ser->irq != port->irq) || 384 (ser->io_type != SERIAL_IO_MEM) || 385 (ser->baud_base != port->uartclk) || 386 (ser->iomem_base != (void*)port->mapbase) || 387 (ser->hub6 != 0 ) ) 388 return -EINVAL; 389 390 return 0; 391} 392 393 394static struct uart_ops mpc52xx_uart_ops = { 395 .tx_empty = mpc52xx_uart_tx_empty, 396 .set_mctrl = mpc52xx_uart_set_mctrl, 397 .get_mctrl = mpc52xx_uart_get_mctrl, 398 .stop_tx = mpc52xx_uart_stop_tx, 399 .start_tx = mpc52xx_uart_start_tx, 400 .send_xchar = mpc52xx_uart_send_xchar, 401 .stop_rx = mpc52xx_uart_stop_rx, 402 .enable_ms = mpc52xx_uart_enable_ms, 403 .break_ctl = mpc52xx_uart_break_ctl, 404 .startup = mpc52xx_uart_startup, 405 .shutdown = mpc52xx_uart_shutdown, 406 .set_termios = mpc52xx_uart_set_termios, 407/* .pm = mpc52xx_uart_pm, Not supported yet */ 408/* .set_wake = mpc52xx_uart_set_wake, Not supported yet */ 409 .type = mpc52xx_uart_type, 410 .release_port = mpc52xx_uart_release_port, 411 .request_port = mpc52xx_uart_request_port, 412 .config_port = mpc52xx_uart_config_port, 413 .verify_port = mpc52xx_uart_verify_port 414}; 415 416 417/* ======================================================================== */ 418/* Interrupt handling */ 419/* ======================================================================== */ 420 421static inline int 422mpc52xx_uart_int_rx_chars(struct uart_port *port) 423{ 424 struct tty_struct *tty = port->info->tty; 425 unsigned char ch, flag; 426 unsigned short status; 427 428 /* While we can read, do so ! */ 429 while ( (status = in_be16(&PSC(port)->mpc52xx_psc_status)) & 430 MPC52xx_PSC_SR_RXRDY) { 431 432 /* Get the char */ 433 ch = in_8(&PSC(port)->mpc52xx_psc_buffer_8); 434 435 /* Handle sysreq char */ 436#ifdef SUPPORT_SYSRQ 437 if (uart_handle_sysrq_char(port, ch)) { 438 port->sysrq = 0; 439 continue; 440 } 441#endif 442 443 /* Store it */ 444 445 flag = TTY_NORMAL; 446 port->icount.rx++; 447 448 if ( status & (MPC52xx_PSC_SR_PE | 449 MPC52xx_PSC_SR_FE | 450 MPC52xx_PSC_SR_RB) ) { 451 452 if (status & MPC52xx_PSC_SR_RB) { 453 flag = TTY_BREAK; 454 uart_handle_break(port); 455 } else if (status & MPC52xx_PSC_SR_PE) 456 flag = TTY_PARITY; 457 else if (status & MPC52xx_PSC_SR_FE) 458 flag = TTY_FRAME; 459 460 /* Clear error condition */ 461 out_8(&PSC(port)->command,MPC52xx_PSC_RST_ERR_STAT); 462 463 } 464 tty_insert_flip_char(tty, ch, flag); 465 if (status & MPC52xx_PSC_SR_OE) { 466 /* 467 * Overrun is special, since it's 468 * reported immediately, and doesn't 469 * affect the current character 470 */ 471 tty_insert_flip_char(tty, 0, TTY_OVERRUN); 472 } 473 } 474 475 tty_flip_buffer_push(tty); 476 477 return in_be16(&PSC(port)->mpc52xx_psc_status) & MPC52xx_PSC_SR_RXRDY; 478} 479 480static inline int 481mpc52xx_uart_int_tx_chars(struct uart_port *port) 482{ 483 struct circ_buf *xmit = &port->info->xmit; 484 485 /* Process out of band chars */ 486 if (port->x_char) { 487 out_8(&PSC(port)->mpc52xx_psc_buffer_8, port->x_char); 488 port->icount.tx++; 489 port->x_char = 0; 490 return 1; 491 } 492 493 /* Nothing to do ? */ 494 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) { 495 mpc52xx_uart_stop_tx(port); 496 return 0; 497 } 498 499 /* Send chars */ 500 while (in_be16(&PSC(port)->mpc52xx_psc_status) & MPC52xx_PSC_SR_TXRDY) { 501 out_8(&PSC(port)->mpc52xx_psc_buffer_8, xmit->buf[xmit->tail]); 502 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); 503 port->icount.tx++; 504 if (uart_circ_empty(xmit)) 505 break; 506 } 507 508 /* Wake up */ 509 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 510 uart_write_wakeup(port); 511 512 /* Maybe we're done after all */ 513 if (uart_circ_empty(xmit)) { 514 mpc52xx_uart_stop_tx(port); 515 return 0; 516 } 517 518 return 1; 519} 520 521static irqreturn_t 522mpc52xx_uart_int(int irq, void *dev_id) 523{ 524 struct uart_port *port = dev_id; 525 unsigned long pass = ISR_PASS_LIMIT; 526 unsigned int keepgoing; 527 unsigned short status; 528 529 spin_lock(&port->lock); 530 531 /* While we have stuff to do, we continue */ 532 do { 533 /* If we don't find anything to do, we stop */ 534 keepgoing = 0; 535 536 /* Read status */ 537 status = in_be16(&PSC(port)->mpc52xx_psc_isr); 538 status &= port->read_status_mask; 539 540 /* Do we need to receive chars ? */ 541 /* For this RX interrupts must be on and some chars waiting */ 542 if ( status & MPC52xx_PSC_IMR_RXRDY ) 543 keepgoing |= mpc52xx_uart_int_rx_chars(port); 544 545 /* Do we need to send chars ? */ 546 /* For this, TX must be ready and TX interrupt enabled */ 547 if ( status & MPC52xx_PSC_IMR_TXRDY ) 548 keepgoing |= mpc52xx_uart_int_tx_chars(port); 549 550 /* Limit number of iteration */ 551 if ( !(--pass) ) 552 keepgoing = 0; 553 554 } while (keepgoing); 555 556 spin_unlock(&port->lock); 557 558 return IRQ_HANDLED; 559} 560 561 562/* ======================================================================== */ 563/* Console ( if applicable ) */ 564/* ======================================================================== */ 565 566#ifdef CONFIG_SERIAL_MPC52xx_CONSOLE 567 568static void __init 569mpc52xx_console_get_options(struct uart_port *port, 570 int *baud, int *parity, int *bits, int *flow) 571{ 572 struct mpc52xx_psc __iomem *psc = PSC(port); 573 unsigned char mr1; 574 575 pr_debug("mpc52xx_console_get_options(port=%p)\n", port); 576 577 /* Read the mode registers */ 578 out_8(&psc->command,MPC52xx_PSC_SEL_MODE_REG_1); 579 mr1 = in_8(&psc->mode); 580 581 /* CT{U,L}R are write-only ! */ 582 *baud = CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD; 583#if !defined(CONFIG_PPC_MERGE) 584 if (__res.bi_baudrate) 585 *baud = __res.bi_baudrate; 586#endif 587 588 /* Parse them */ 589 switch (mr1 & MPC52xx_PSC_MODE_BITS_MASK) { 590 case MPC52xx_PSC_MODE_5_BITS: *bits = 5; break; 591 case MPC52xx_PSC_MODE_6_BITS: *bits = 6; break; 592 case MPC52xx_PSC_MODE_7_BITS: *bits = 7; break; 593 case MPC52xx_PSC_MODE_8_BITS: 594 default: *bits = 8; 595 } 596 597 if (mr1 & MPC52xx_PSC_MODE_PARNONE) 598 *parity = 'n'; 599 else 600 *parity = mr1 & MPC52xx_PSC_MODE_PARODD ? 'o' : 'e'; 601} 602 603static void 604mpc52xx_console_write(struct console *co, const char *s, unsigned int count) 605{ 606 struct uart_port *port = &mpc52xx_uart_ports[co->index]; 607 struct mpc52xx_psc __iomem *psc = PSC(port); 608 unsigned int i, j; 609 610 /* Disable interrupts */ 611 out_be16(&psc->mpc52xx_psc_imr, 0); 612 613 /* Wait the TX buffer to be empty */ 614 j = 5000000; /* Maximum wait */ 615 while (!(in_be16(&psc->mpc52xx_psc_status) & MPC52xx_PSC_SR_TXEMP) && 616 --j) 617 udelay(1); 618 619 /* Write all the chars */ 620 for (i = 0; i < count; i++, s++) { 621 /* Line return handling */ 622 if (*s == '\n') 623 out_8(&psc->mpc52xx_psc_buffer_8, '\r'); 624 625 /* Send the char */ 626 out_8(&psc->mpc52xx_psc_buffer_8, *s); 627 628 /* Wait the TX buffer to be empty */ 629 j = 20000; /* Maximum wait */ 630 while (!(in_be16(&psc->mpc52xx_psc_status) & 631 MPC52xx_PSC_SR_TXEMP) && --j) 632 udelay(1); 633 } 634 635 /* Restore interrupt state */ 636 out_be16(&psc->mpc52xx_psc_imr, port->read_status_mask); 637} 638 639#if !defined(CONFIG_PPC_MERGE) 640static int __init 641mpc52xx_console_setup(struct console *co, char *options) 642{ 643 struct uart_port *port = &mpc52xx_uart_ports[co->index]; 644 645 int baud = CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD; 646 int bits = 8; 647 int parity = 'n'; 648 int flow = 'n'; 649 650 if (co->index < 0 || co->index >= MPC52xx_PSC_MAXNUM) 651 return -EINVAL; 652 653 /* Basic port init. Needed since we use some uart_??? func before 654 * real init for early access */ 655 spin_lock_init(&port->lock); 656 port->uartclk = __res.bi_ipbfreq / 2; /* Look at CTLR doc */ 657 port->ops = &mpc52xx_uart_ops; 658 port->mapbase = MPC52xx_PA(MPC52xx_PSCx_OFFSET(co->index+1)); 659 660 /* We ioremap ourself */ 661 port->membase = ioremap(port->mapbase, MPC52xx_PSC_SIZE); 662 if (port->membase == NULL) 663 return -EINVAL; 664 665 /* Setup the port parameters accoding to options */ 666 if (options) 667 uart_parse_options(options, &baud, &parity, &bits, &flow); 668 else 669 mpc52xx_console_get_options(port, &baud, &parity, &bits, &flow); 670 671 return uart_set_options(port, co, baud, parity, bits, flow); 672} 673 674#else 675 676static int __init 677mpc52xx_console_setup(struct console *co, char *options) 678{ 679 struct uart_port *port = &mpc52xx_uart_ports[co->index]; 680 struct device_node *np = mpc52xx_uart_nodes[co->index]; 681 unsigned int ipb_freq; 682 struct resource res; 683 int ret; 684 685 int baud = CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD; 686 int bits = 8; 687 int parity = 'n'; 688 int flow = 'n'; 689 690 pr_debug("mpc52xx_console_setup co=%p, co->index=%i, options=%s\n", 691 co, co->index, options); 692 693 if ((co->index < 0) || (co->index > MPC52xx_PSC_MAXNUM)) { 694 pr_debug("PSC%x out of range\n", co->index); 695 return -EINVAL; 696 } 697 698 if (!np) { 699 pr_debug("PSC%x not found in device tree\n", co->index); 700 return -EINVAL; 701 } 702 703 pr_debug("Console on ttyPSC%x is %s\n", 704 co->index, mpc52xx_uart_nodes[co->index]->full_name); 705 706 /* Fetch register locations */ 707 if ((ret = of_address_to_resource(np, 0, &res)) != 0) { 708 pr_debug("Could not get resources for PSC%x\n", co->index); 709 return ret; 710 } 711 712 /* Search for bus-frequency property in this node or a parent */ 713 if ((ipb_freq = mpc52xx_find_ipb_freq(np)) == 0) { 714 pr_debug("Could not find IPB bus frequency!\n"); 715 return -EINVAL; 716 } 717 718 /* Basic port init. Needed since we use some uart_??? func before 719 * real init for early access */ 720 spin_lock_init(&port->lock); 721 port->uartclk = ipb_freq / 2; 722 port->ops = &mpc52xx_uart_ops; 723 port->mapbase = res.start; 724 port->membase = ioremap(res.start, sizeof(struct mpc52xx_psc)); 725 port->irq = irq_of_parse_and_map(np, 0); 726 727 if (port->membase == NULL) 728 return -EINVAL; 729 730 pr_debug("mpc52xx-psc uart at %lx, mapped to %p, irq=%x, freq=%i\n", 731 port->mapbase, port->membase, port->irq, port->uartclk); 732 733 /* Setup the port parameters accoding to options */ 734 if (options) 735 uart_parse_options(options, &baud, &parity, &bits, &flow); 736 else 737 mpc52xx_console_get_options(port, &baud, &parity, &bits, &flow); 738 739 pr_debug("Setting console parameters: %i %i%c1 flow=%c\n", 740 baud, bits, parity, flow); 741 742 return uart_set_options(port, co, baud, parity, bits, flow); 743} 744#endif /* defined(CONFIG_PPC_MERGE) */ 745 746 747static struct uart_driver mpc52xx_uart_driver; 748 749static struct console mpc52xx_console = { 750 .name = "ttyPSC", 751 .write = mpc52xx_console_write, 752 .device = uart_console_device, 753 .setup = mpc52xx_console_setup, 754 .flags = CON_PRINTBUFFER, 755 .index = -1, /* Specified on the cmdline (e.g. console=ttyPSC0 ) */ 756 .data = &mpc52xx_uart_driver, 757}; 758 759 760static int __init 761mpc52xx_console_init(void) 762{ 763#if defined(CONFIG_PPC_MERGE) 764 mpc52xx_uart_of_enumerate(); 765#endif 766 register_console(&mpc52xx_console); 767 return 0; 768} 769 770console_initcall(mpc52xx_console_init); 771 772#define MPC52xx_PSC_CONSOLE &mpc52xx_console 773#else 774#define MPC52xx_PSC_CONSOLE NULL 775#endif 776 777 778/* ======================================================================== */ 779/* UART Driver */ 780/* ======================================================================== */ 781 782static struct uart_driver mpc52xx_uart_driver = { 783 .owner = THIS_MODULE, 784 .driver_name = "mpc52xx_psc_uart", 785 .dev_name = "ttyPSC", 786 .major = SERIAL_PSC_MAJOR, 787 .minor = SERIAL_PSC_MINOR, 788 .nr = MPC52xx_PSC_MAXNUM, 789 .cons = MPC52xx_PSC_CONSOLE, 790}; 791 792 793#if !defined(CONFIG_PPC_MERGE) 794/* ======================================================================== */ 795/* Platform Driver */ 796/* ======================================================================== */ 797 798static int __devinit 799mpc52xx_uart_probe(struct platform_device *dev) 800{ 801 struct resource *res = dev->resource; 802 803 struct uart_port *port = NULL; 804 int i, idx, ret; 805 806 /* Check validity & presence */ 807 idx = dev->id; 808 if (idx < 0 || idx >= MPC52xx_PSC_MAXNUM) 809 return -EINVAL; 810 811 if (!mpc52xx_match_psc_function(idx,"uart")) 812 return -ENODEV; 813 814 /* Init the port structure */ 815 port = &mpc52xx_uart_ports[idx]; 816 817 spin_lock_init(&port->lock); 818 port->uartclk = __res.bi_ipbfreq / 2; /* Look at CTLR doc */ 819 port->fifosize = 512; 820 port->iotype = UPIO_MEM; 821 port->flags = UPF_BOOT_AUTOCONF | 822 ( uart_console(port) ? 0 : UPF_IOREMAP ); 823 port->line = idx; 824 port->ops = &mpc52xx_uart_ops; 825 port->dev = &dev->dev; 826 827 /* Search for IRQ and mapbase */ 828 for (i=0 ; i<dev->num_resources ; i++, res++) { 829 if (res->flags & IORESOURCE_MEM) 830 port->mapbase = res->start; 831 else if (res->flags & IORESOURCE_IRQ) 832 port->irq = res->start; 833 } 834 if (!port->irq || !port->mapbase) 835 return -EINVAL; 836 837 /* Add the port to the uart sub-system */ 838 ret = uart_add_one_port(&mpc52xx_uart_driver, port); 839 if (!ret) 840 platform_set_drvdata(dev, (void*)port); 841 842 return ret; 843} 844 845static int 846mpc52xx_uart_remove(struct platform_device *dev) 847{ 848 struct uart_port *port = (struct uart_port *) platform_get_drvdata(dev); 849 850 platform_set_drvdata(dev, NULL); 851 852 if (port) 853 uart_remove_one_port(&mpc52xx_uart_driver, port); 854 855 return 0; 856} 857 858#ifdef CONFIG_PM 859static int 860mpc52xx_uart_suspend(struct platform_device *dev, pm_message_t state) 861{ 862 struct uart_port *port = (struct uart_port *) platform_get_drvdata(dev); 863 864 if (port) 865 uart_suspend_port(&mpc52xx_uart_driver, port); 866 867 return 0; 868} 869 870static int 871mpc52xx_uart_resume(struct platform_device *dev) 872{ 873 struct uart_port *port = (struct uart_port *) platform_get_drvdata(dev); 874 875 if (port) 876 uart_resume_port(&mpc52xx_uart_driver, port); 877 878 return 0; 879} 880#endif 881 882 883static struct platform_driver mpc52xx_uart_platform_driver = { 884 .probe = mpc52xx_uart_probe, 885 .remove = mpc52xx_uart_remove, 886#ifdef CONFIG_PM 887 .suspend = mpc52xx_uart_suspend, 888 .resume = mpc52xx_uart_resume, 889#endif 890 .driver = { 891 .name = "mpc52xx-psc", 892 }, 893}; 894#endif /* !defined(CONFIG_PPC_MERGE) */ 895 896 897#if defined(CONFIG_PPC_MERGE) 898/* ======================================================================== */ 899/* OF Platform Driver */ 900/* ======================================================================== */ 901 902static int __devinit 903mpc52xx_uart_of_probe(struct of_device *op, const struct of_device_id *match) 904{ 905 int idx = -1; 906 unsigned int ipb_freq; 907 struct uart_port *port = NULL; 908 struct resource res; 909 int ret; 910 911 dev_dbg(&op->dev, "mpc52xx_uart_probe(op=%p, match=%p)\n", op, match); 912 913 /* Check validity & presence */ 914 for (idx = 0; idx < MPC52xx_PSC_MAXNUM; idx++) 915 if (mpc52xx_uart_nodes[idx] == op->node) 916 break; 917 if (idx >= MPC52xx_PSC_MAXNUM) 918 return -EINVAL; 919 pr_debug("Found %s assigned to ttyPSC%x\n", 920 mpc52xx_uart_nodes[idx]->full_name, idx); 921 922 /* Search for bus-frequency property in this node or a parent */ 923 if ((ipb_freq = mpc52xx_find_ipb_freq(op->node)) == 0) { 924 dev_dbg(&op->dev, "Could not find IPB bus frequency!\n"); 925 return -EINVAL; 926 } 927 928 /* Init the port structure */ 929 port = &mpc52xx_uart_ports[idx]; 930 931 spin_lock_init(&port->lock); 932 port->uartclk = ipb_freq / 2; 933 port->fifosize = 512; 934 port->iotype = UPIO_MEM; 935 port->flags = UPF_BOOT_AUTOCONF | 936 ( uart_console(port) ? 0 : UPF_IOREMAP ); 937 port->line = idx; 938 port->ops = &mpc52xx_uart_ops; 939 port->dev = &op->dev; 940 941 /* Search for IRQ and mapbase */ 942 if ((ret = of_address_to_resource(op->node, 0, &res)) != 0) 943 return ret; 944 945 port->mapbase = res.start; 946 port->irq = irq_of_parse_and_map(op->node, 0); 947 948 dev_dbg(&op->dev, "mpc52xx-psc uart at %lx, irq=%x, freq=%i\n", 949 port->mapbase, port->irq, port->uartclk); 950 951 if ((port->irq==NO_IRQ) || !port->mapbase) { 952 printk(KERN_ERR "Could not allocate resources for PSC\n"); 953 return -EINVAL; 954 } 955 956 /* Add the port to the uart sub-system */ 957 ret = uart_add_one_port(&mpc52xx_uart_driver, port); 958 if (!ret) 959 dev_set_drvdata(&op->dev, (void*)port); 960 961 return ret; 962} 963 964static int 965mpc52xx_uart_of_remove(struct of_device *op) 966{ 967 struct uart_port *port = dev_get_drvdata(&op->dev); 968 dev_set_drvdata(&op->dev, NULL); 969 970 if (port) { 971 uart_remove_one_port(&mpc52xx_uart_driver, port); 972 irq_dispose_mapping(port->irq); 973 } 974 975 return 0; 976} 977 978#ifdef CONFIG_PM 979static int 980mpc52xx_uart_of_suspend(struct of_device *op, pm_message_t state) 981{ 982 struct uart_port *port = (struct uart_port *) dev_get_drvdata(&op->dev); 983 984 if (port) 985 uart_suspend_port(&mpc52xx_uart_driver, port); 986 987 return 0; 988} 989 990static int 991mpc52xx_uart_of_resume(struct of_device *op) 992{ 993 struct uart_port *port = (struct uart_port *) dev_get_drvdata(&op->dev); 994 995 if (port) 996 uart_resume_port(&mpc52xx_uart_driver, port); 997 998 return 0; 999} 1000#endif 1001 1002static void 1003mpc52xx_uart_of_assign(struct device_node *np, int idx) 1004{ 1005 int free_idx = -1; 1006 int i; 1007 1008 /* Find the first free node */ 1009 for (i = 0; i < MPC52xx_PSC_MAXNUM; i++) { 1010 if (mpc52xx_uart_nodes[i] == NULL) { 1011 free_idx = i; 1012 break; 1013 } 1014 } 1015 1016 if ((idx < 0) || (idx >= MPC52xx_PSC_MAXNUM)) 1017 idx = free_idx; 1018 1019 if (idx < 0) 1020 return; /* No free slot; abort */ 1021 1022 /* If the slot is already occupied, then swap slots */ 1023 if (mpc52xx_uart_nodes[idx] && (free_idx != -1)) 1024 mpc52xx_uart_nodes[free_idx] = mpc52xx_uart_nodes[idx]; 1025 mpc52xx_uart_nodes[i] = np; 1026} 1027 1028static void 1029mpc52xx_uart_of_enumerate(void) 1030{ 1031 static int enum_done = 0; 1032 struct device_node *np; 1033 const unsigned int *devno; 1034 int i; 1035 1036 if (enum_done) 1037 return; 1038 1039 for_each_node_by_type(np, "serial") { 1040 if (!of_match_node(mpc52xx_uart_of_match, np)) 1041 continue; 1042 1043 /* Is a particular device number requested? */ 1044 devno = of_get_property(np, "port-number", NULL); 1045 mpc52xx_uart_of_assign(of_node_get(np), devno ? *devno : -1); 1046 } 1047 1048 enum_done = 1; 1049 1050 for (i = 0; i < MPC52xx_PSC_MAXNUM; i++) { 1051 if (mpc52xx_uart_nodes[i]) 1052 pr_debug("%s assigned to ttyPSC%x\n", 1053 mpc52xx_uart_nodes[i]->full_name, i); 1054 } 1055} 1056 1057MODULE_DEVICE_TABLE(of, mpc52xx_uart_of_match); 1058 1059static struct of_platform_driver mpc52xx_uart_of_driver = { 1060 .owner = THIS_MODULE, 1061 .name = "mpc52xx-psc-uart", 1062 .match_table = mpc52xx_uart_of_match, 1063 .probe = mpc52xx_uart_of_probe, 1064 .remove = mpc52xx_uart_of_remove, 1065#ifdef CONFIG_PM 1066 .suspend = mpc52xx_uart_of_suspend, 1067 .resume = mpc52xx_uart_of_resume, 1068#endif 1069 .driver = { 1070 .name = "mpc52xx-psc-uart", 1071 }, 1072}; 1073#endif /* defined(CONFIG_PPC_MERGE) */ 1074 1075 1076/* ======================================================================== */ 1077/* Module */ 1078/* ======================================================================== */ 1079 1080static int __init 1081mpc52xx_uart_init(void) 1082{ 1083 int ret; 1084 1085 printk(KERN_INFO "Serial: MPC52xx PSC UART driver\n"); 1086 1087 if ((ret = uart_register_driver(&mpc52xx_uart_driver)) != 0) { 1088 printk(KERN_ERR "%s: uart_register_driver failed (%i)\n", 1089 __FILE__, ret); 1090 return ret; 1091 } 1092 1093#if defined(CONFIG_PPC_MERGE) 1094 mpc52xx_uart_of_enumerate(); 1095 1096 ret = of_register_platform_driver(&mpc52xx_uart_of_driver); 1097 if (ret) { 1098 printk(KERN_ERR "%s: of_register_platform_driver failed (%i)\n", 1099 __FILE__, ret); 1100 uart_unregister_driver(&mpc52xx_uart_driver); 1101 return ret; 1102 } 1103#else 1104 ret = platform_driver_register(&mpc52xx_uart_platform_driver); 1105 if (ret) { 1106 printk(KERN_ERR "%s: platform_driver_register failed (%i)\n", 1107 __FILE__, ret); 1108 uart_unregister_driver(&mpc52xx_uart_driver); 1109 return ret; 1110 } 1111#endif 1112 1113 return 0; 1114} 1115 1116static void __exit 1117mpc52xx_uart_exit(void) 1118{ 1119#if defined(CONFIG_PPC_MERGE) 1120 of_unregister_platform_driver(&mpc52xx_uart_of_driver); 1121#else 1122 platform_driver_unregister(&mpc52xx_uart_platform_driver); 1123#endif 1124 uart_unregister_driver(&mpc52xx_uart_driver); 1125} 1126 1127 1128module_init(mpc52xx_uart_init); 1129module_exit(mpc52xx_uart_exit); 1130 1131MODULE_AUTHOR("Sylvain Munaut <tnt@246tNt.com>"); 1132MODULE_DESCRIPTION("Freescale MPC52xx PSC UART"); 1133MODULE_LICENSE("GPL"); 1134