18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * arch/xtensa/platforms/iss/console.c 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * This file is subject to the terms and conditions of the GNU General Public 58c2ecf20Sopenharmony_ci * License. See the file "COPYING" in the main directory of this archive 68c2ecf20Sopenharmony_ci * for more details. 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci * Copyright (C) 2001-2005 Tensilica Inc. 98c2ecf20Sopenharmony_ci * Authors Christian Zankel, Joe Taylor 108c2ecf20Sopenharmony_ci */ 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ci#include <linux/module.h> 138c2ecf20Sopenharmony_ci#include <linux/kernel.h> 148c2ecf20Sopenharmony_ci#include <linux/sched.h> 158c2ecf20Sopenharmony_ci#include <linux/console.h> 168c2ecf20Sopenharmony_ci#include <linux/init.h> 178c2ecf20Sopenharmony_ci#include <linux/mm.h> 188c2ecf20Sopenharmony_ci#include <linux/major.h> 198c2ecf20Sopenharmony_ci#include <linux/param.h> 208c2ecf20Sopenharmony_ci#include <linux/seq_file.h> 218c2ecf20Sopenharmony_ci#include <linux/serial.h> 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ci#include <linux/uaccess.h> 248c2ecf20Sopenharmony_ci#include <asm/irq.h> 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ci#include <platform/simcall.h> 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci#include <linux/tty.h> 298c2ecf20Sopenharmony_ci#include <linux/tty_flip.h> 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ci#define SERIAL_MAX_NUM_LINES 1 328c2ecf20Sopenharmony_ci#define SERIAL_TIMER_VALUE (HZ / 10) 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_cistatic struct tty_driver *serial_driver; 358c2ecf20Sopenharmony_cistatic struct tty_port serial_port; 368c2ecf20Sopenharmony_cistatic struct timer_list serial_timer; 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_cistatic DEFINE_SPINLOCK(timer_lock); 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_cistatic char *serial_version = "0.1"; 418c2ecf20Sopenharmony_cistatic char *serial_name = "ISS serial driver"; 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_ci/* 448c2ecf20Sopenharmony_ci * This routine is called whenever a serial port is opened. It 458c2ecf20Sopenharmony_ci * enables interrupts for a serial port, linking in its async structure into 468c2ecf20Sopenharmony_ci * the IRQ chain. It also performs the serial-specific 478c2ecf20Sopenharmony_ci * initialization for the tty structure. 488c2ecf20Sopenharmony_ci */ 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_cistatic void rs_poll(struct timer_list *); 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_cistatic int rs_open(struct tty_struct *tty, struct file * filp) 538c2ecf20Sopenharmony_ci{ 548c2ecf20Sopenharmony_ci tty->port = &serial_port; 558c2ecf20Sopenharmony_ci spin_lock_bh(&timer_lock); 568c2ecf20Sopenharmony_ci if (tty->count == 1) { 578c2ecf20Sopenharmony_ci timer_setup(&serial_timer, rs_poll, 0); 588c2ecf20Sopenharmony_ci mod_timer(&serial_timer, jiffies + SERIAL_TIMER_VALUE); 598c2ecf20Sopenharmony_ci } 608c2ecf20Sopenharmony_ci spin_unlock_bh(&timer_lock); 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci return 0; 638c2ecf20Sopenharmony_ci} 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci/* 678c2ecf20Sopenharmony_ci * ------------------------------------------------------------ 688c2ecf20Sopenharmony_ci * iss_serial_close() 698c2ecf20Sopenharmony_ci * 708c2ecf20Sopenharmony_ci * This routine is called when the serial port gets closed. First, we 718c2ecf20Sopenharmony_ci * wait for the last remaining data to be sent. Then, we unlink its 728c2ecf20Sopenharmony_ci * async structure from the interrupt chain if necessary, and we free 738c2ecf20Sopenharmony_ci * that IRQ if nothing is left in the chain. 748c2ecf20Sopenharmony_ci * ------------------------------------------------------------ 758c2ecf20Sopenharmony_ci */ 768c2ecf20Sopenharmony_cistatic void rs_close(struct tty_struct *tty, struct file * filp) 778c2ecf20Sopenharmony_ci{ 788c2ecf20Sopenharmony_ci spin_lock_bh(&timer_lock); 798c2ecf20Sopenharmony_ci if (tty->count == 1) 808c2ecf20Sopenharmony_ci del_timer_sync(&serial_timer); 818c2ecf20Sopenharmony_ci spin_unlock_bh(&timer_lock); 828c2ecf20Sopenharmony_ci} 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_cistatic int rs_write(struct tty_struct * tty, 868c2ecf20Sopenharmony_ci const unsigned char *buf, int count) 878c2ecf20Sopenharmony_ci{ 888c2ecf20Sopenharmony_ci /* see drivers/char/serialX.c to reference original version */ 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci simc_write(1, buf, count); 918c2ecf20Sopenharmony_ci return count; 928c2ecf20Sopenharmony_ci} 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_cistatic void rs_poll(struct timer_list *unused) 958c2ecf20Sopenharmony_ci{ 968c2ecf20Sopenharmony_ci struct tty_port *port = &serial_port; 978c2ecf20Sopenharmony_ci int i = 0; 988c2ecf20Sopenharmony_ci int rd = 1; 998c2ecf20Sopenharmony_ci unsigned char c; 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ci spin_lock(&timer_lock); 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ci while (simc_poll(0)) { 1048c2ecf20Sopenharmony_ci rd = simc_read(0, &c, 1); 1058c2ecf20Sopenharmony_ci if (rd <= 0) 1068c2ecf20Sopenharmony_ci break; 1078c2ecf20Sopenharmony_ci tty_insert_flip_char(port, c, TTY_NORMAL); 1088c2ecf20Sopenharmony_ci i++; 1098c2ecf20Sopenharmony_ci } 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_ci if (i) 1128c2ecf20Sopenharmony_ci tty_flip_buffer_push(port); 1138c2ecf20Sopenharmony_ci if (rd) 1148c2ecf20Sopenharmony_ci mod_timer(&serial_timer, jiffies + SERIAL_TIMER_VALUE); 1158c2ecf20Sopenharmony_ci spin_unlock(&timer_lock); 1168c2ecf20Sopenharmony_ci} 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_cistatic int rs_put_char(struct tty_struct *tty, unsigned char ch) 1208c2ecf20Sopenharmony_ci{ 1218c2ecf20Sopenharmony_ci return rs_write(tty, &ch, 1); 1228c2ecf20Sopenharmony_ci} 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_cistatic void rs_flush_chars(struct tty_struct *tty) 1258c2ecf20Sopenharmony_ci{ 1268c2ecf20Sopenharmony_ci} 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_cistatic int rs_write_room(struct tty_struct *tty) 1298c2ecf20Sopenharmony_ci{ 1308c2ecf20Sopenharmony_ci /* Let's say iss can always accept 2K characters.. */ 1318c2ecf20Sopenharmony_ci return 2 * 1024; 1328c2ecf20Sopenharmony_ci} 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_cistatic int rs_chars_in_buffer(struct tty_struct *tty) 1358c2ecf20Sopenharmony_ci{ 1368c2ecf20Sopenharmony_ci /* the iss doesn't buffer characters */ 1378c2ecf20Sopenharmony_ci return 0; 1388c2ecf20Sopenharmony_ci} 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_cistatic void rs_hangup(struct tty_struct *tty) 1418c2ecf20Sopenharmony_ci{ 1428c2ecf20Sopenharmony_ci /* Stub, once again.. */ 1438c2ecf20Sopenharmony_ci} 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_cistatic void rs_wait_until_sent(struct tty_struct *tty, int timeout) 1468c2ecf20Sopenharmony_ci{ 1478c2ecf20Sopenharmony_ci /* Stub, once again.. */ 1488c2ecf20Sopenharmony_ci} 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_cistatic int rs_proc_show(struct seq_file *m, void *v) 1518c2ecf20Sopenharmony_ci{ 1528c2ecf20Sopenharmony_ci seq_printf(m, "serinfo:1.0 driver:%s\n", serial_version); 1538c2ecf20Sopenharmony_ci return 0; 1548c2ecf20Sopenharmony_ci} 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_cistatic const struct tty_operations serial_ops = { 1578c2ecf20Sopenharmony_ci .open = rs_open, 1588c2ecf20Sopenharmony_ci .close = rs_close, 1598c2ecf20Sopenharmony_ci .write = rs_write, 1608c2ecf20Sopenharmony_ci .put_char = rs_put_char, 1618c2ecf20Sopenharmony_ci .flush_chars = rs_flush_chars, 1628c2ecf20Sopenharmony_ci .write_room = rs_write_room, 1638c2ecf20Sopenharmony_ci .chars_in_buffer = rs_chars_in_buffer, 1648c2ecf20Sopenharmony_ci .hangup = rs_hangup, 1658c2ecf20Sopenharmony_ci .wait_until_sent = rs_wait_until_sent, 1668c2ecf20Sopenharmony_ci .proc_show = rs_proc_show, 1678c2ecf20Sopenharmony_ci}; 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_ciint __init rs_init(void) 1708c2ecf20Sopenharmony_ci{ 1718c2ecf20Sopenharmony_ci int ret; 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ci serial_driver = alloc_tty_driver(SERIAL_MAX_NUM_LINES); 1748c2ecf20Sopenharmony_ci if (!serial_driver) 1758c2ecf20Sopenharmony_ci return -ENOMEM; 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_ci tty_port_init(&serial_port); 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_ci pr_info("%s %s\n", serial_name, serial_version); 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_ci /* Initialize the tty_driver structure */ 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ci serial_driver->driver_name = "iss_serial"; 1848c2ecf20Sopenharmony_ci serial_driver->name = "ttyS"; 1858c2ecf20Sopenharmony_ci serial_driver->major = TTY_MAJOR; 1868c2ecf20Sopenharmony_ci serial_driver->minor_start = 64; 1878c2ecf20Sopenharmony_ci serial_driver->type = TTY_DRIVER_TYPE_SERIAL; 1888c2ecf20Sopenharmony_ci serial_driver->subtype = SERIAL_TYPE_NORMAL; 1898c2ecf20Sopenharmony_ci serial_driver->init_termios = tty_std_termios; 1908c2ecf20Sopenharmony_ci serial_driver->init_termios.c_cflag = 1918c2ecf20Sopenharmony_ci B9600 | CS8 | CREAD | HUPCL | CLOCAL; 1928c2ecf20Sopenharmony_ci serial_driver->flags = TTY_DRIVER_REAL_RAW; 1938c2ecf20Sopenharmony_ci 1948c2ecf20Sopenharmony_ci tty_set_operations(serial_driver, &serial_ops); 1958c2ecf20Sopenharmony_ci tty_port_link_device(&serial_port, serial_driver, 0); 1968c2ecf20Sopenharmony_ci 1978c2ecf20Sopenharmony_ci ret = tty_register_driver(serial_driver); 1988c2ecf20Sopenharmony_ci if (ret) { 1998c2ecf20Sopenharmony_ci pr_err("Couldn't register serial driver\n"); 2008c2ecf20Sopenharmony_ci tty_driver_kref_put(serial_driver); 2018c2ecf20Sopenharmony_ci tty_port_destroy(&serial_port); 2028c2ecf20Sopenharmony_ci 2038c2ecf20Sopenharmony_ci return ret; 2048c2ecf20Sopenharmony_ci } 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_ci return 0; 2078c2ecf20Sopenharmony_ci} 2088c2ecf20Sopenharmony_ci 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_cistatic __exit void rs_exit(void) 2118c2ecf20Sopenharmony_ci{ 2128c2ecf20Sopenharmony_ci int error; 2138c2ecf20Sopenharmony_ci 2148c2ecf20Sopenharmony_ci if ((error = tty_unregister_driver(serial_driver))) 2158c2ecf20Sopenharmony_ci pr_err("ISS_SERIAL: failed to unregister serial driver (%d)\n", 2168c2ecf20Sopenharmony_ci error); 2178c2ecf20Sopenharmony_ci put_tty_driver(serial_driver); 2188c2ecf20Sopenharmony_ci tty_port_destroy(&serial_port); 2198c2ecf20Sopenharmony_ci} 2208c2ecf20Sopenharmony_ci 2218c2ecf20Sopenharmony_ci 2228c2ecf20Sopenharmony_ci/* We use `late_initcall' instead of just `__initcall' as a workaround for 2238c2ecf20Sopenharmony_ci * the fact that (1) simcons_tty_init can't be called before tty_init, 2248c2ecf20Sopenharmony_ci * (2) tty_init is called via `module_init', (3) if statically linked, 2258c2ecf20Sopenharmony_ci * module_init == device_init, and (4) there's no ordering of init lists. 2268c2ecf20Sopenharmony_ci * We can do this easily because simcons is always statically linked, but 2278c2ecf20Sopenharmony_ci * other tty drivers that depend on tty_init and which must use 2288c2ecf20Sopenharmony_ci * `module_init' to declare their init routines are likely to be broken. 2298c2ecf20Sopenharmony_ci */ 2308c2ecf20Sopenharmony_ci 2318c2ecf20Sopenharmony_cilate_initcall(rs_init); 2328c2ecf20Sopenharmony_ci 2338c2ecf20Sopenharmony_ci 2348c2ecf20Sopenharmony_ci#ifdef CONFIG_SERIAL_CONSOLE 2358c2ecf20Sopenharmony_ci 2368c2ecf20Sopenharmony_cistatic void iss_console_write(struct console *co, const char *s, unsigned count) 2378c2ecf20Sopenharmony_ci{ 2388c2ecf20Sopenharmony_ci int len = strlen(s); 2398c2ecf20Sopenharmony_ci 2408c2ecf20Sopenharmony_ci if (s != 0 && *s != 0) 2418c2ecf20Sopenharmony_ci simc_write(1, s, count < len ? count : len); 2428c2ecf20Sopenharmony_ci} 2438c2ecf20Sopenharmony_ci 2448c2ecf20Sopenharmony_cistatic struct tty_driver* iss_console_device(struct console *c, int *index) 2458c2ecf20Sopenharmony_ci{ 2468c2ecf20Sopenharmony_ci *index = c->index; 2478c2ecf20Sopenharmony_ci return serial_driver; 2488c2ecf20Sopenharmony_ci} 2498c2ecf20Sopenharmony_ci 2508c2ecf20Sopenharmony_ci 2518c2ecf20Sopenharmony_cistatic struct console sercons = { 2528c2ecf20Sopenharmony_ci .name = "ttyS", 2538c2ecf20Sopenharmony_ci .write = iss_console_write, 2548c2ecf20Sopenharmony_ci .device = iss_console_device, 2558c2ecf20Sopenharmony_ci .flags = CON_PRINTBUFFER, 2568c2ecf20Sopenharmony_ci .index = -1 2578c2ecf20Sopenharmony_ci}; 2588c2ecf20Sopenharmony_ci 2598c2ecf20Sopenharmony_cistatic int __init iss_console_init(void) 2608c2ecf20Sopenharmony_ci{ 2618c2ecf20Sopenharmony_ci register_console(&sercons); 2628c2ecf20Sopenharmony_ci return 0; 2638c2ecf20Sopenharmony_ci} 2648c2ecf20Sopenharmony_ci 2658c2ecf20Sopenharmony_ciconsole_initcall(iss_console_init); 2668c2ecf20Sopenharmony_ci 2678c2ecf20Sopenharmony_ci#endif /* CONFIG_SERIAL_CONSOLE */ 2688c2ecf20Sopenharmony_ci 269