9
0
Fork 0
barebox/common/console.c

429 lines
8.3 KiB
C
Raw Normal View History

2002-11-03 00:01:44 +00:00
/*
* (C) Copyright 2000
* Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
*
* See file CREDITS for list of people who contributed to this
* project.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
2007-07-05 16:01:46 +00:00
#include <config.h>
2002-11-03 00:01:44 +00:00
#include <common.h>
#include <stdarg.h>
#include <malloc.h>
#include <param.h>
2002-11-03 00:01:44 +00:00
#include <console.h>
#include <driver.h>
#include <fs.h>
2007-07-12 07:31:07 +00:00
#include <init.h>
2007-09-21 19:30:59 +00:00
#include <clock.h>
#include <kfifo.h>
#include <module.h>
#include <poller.h>
#include <linux/list.h>
#include <linux/stringify.h>
2002-11-03 00:01:44 +00:00
LIST_HEAD(console_list);
EXPORT_SYMBOL(console_list);
2007-07-12 07:31:07 +00:00
#define CONSOLE_UNINITIALIZED 0
#define CONSOLE_INITIALIZED_BUFFER 1
#define CONSOLE_INIT_FULL 2
2007-07-12 07:31:07 +00:00
#define to_console_dev(d) container_of(d, struct console_device, class_dev)
static int initialized = 0;
#define CONSOLE_BUFFER_SIZE 1024
static char console_input_buffer[CONSOLE_BUFFER_SIZE];
static char console_output_buffer[CONSOLE_BUFFER_SIZE];
static struct kfifo __console_input_fifo;
static struct kfifo __console_output_fifo;
static struct kfifo *console_input_fifo = &__console_input_fifo;
static struct kfifo *console_output_fifo = &__console_output_fifo;
static int console_std_set(struct device_d *dev, struct param_d *param,
const char *val)
{
struct console_device *cdev = to_console_dev(dev);
char active[4];
unsigned int flag = 0, i = 0;
if (!val)
dev_param_set_generic(dev, param, NULL);
if (strchr(val, 'i') && cdev->f_caps & CONSOLE_STDIN) {
active[i++] = 'i';
flag |= CONSOLE_STDIN;
}
if (strchr(val, 'o') && cdev->f_caps & CONSOLE_STDOUT) {
active[i++] = 'o';
flag |= CONSOLE_STDOUT;
}
if (strchr(val, 'e') && cdev->f_caps & CONSOLE_STDERR) {
active[i++] = 'e';
flag |= CONSOLE_STDERR;
}
active[i] = 0;
cdev->f_active = flag;
dev_param_set_generic(dev, param, active);
if (initialized < CONSOLE_INIT_FULL) {
char ch;
initialized = CONSOLE_INIT_FULL;
barebox_banner();
while (kfifo_getc(console_output_fifo, &ch) == 0)
console_putc(CONSOLE_STDOUT, ch);
}
return 0;
}
static int console_baudrate_set(struct device_d *dev, struct param_d *param,
const char *val)
{
struct console_device *cdev = to_console_dev(dev);
int baudrate;
char baudstr[16];
unsigned char c;
if (!val)
dev_param_set_generic(dev, param, NULL);
baudrate = simple_strtoul(val, NULL, 10);
if (cdev->f_active) {
printf("## Switch baudrate to %d bps and press ENTER ...\n",
baudrate);
mdelay(50);
cdev->setbrg(cdev, baudrate);
mdelay(50);
do {
c = getc();
} while (c != '\r' && c != '\n');
} else
cdev->setbrg(cdev, baudrate);
sprintf(baudstr, "%d", baudrate);
dev_param_set_generic(dev, param, baudstr);
return 0;
}
static void console_init_early(void)
{
kfifo_init(console_input_fifo, console_input_buffer,
CONSOLE_BUFFER_SIZE);
kfifo_init(console_output_fifo, console_output_buffer,
CONSOLE_BUFFER_SIZE);
initialized = CONSOLE_INITIALIZED_BUFFER;
}
int console_register(struct console_device *newcdev)
2002-11-03 00:01:44 +00:00
{
struct device_d *dev = &newcdev->class_dev;
int activate = 0;
if (initialized == CONSOLE_UNINITIALIZED)
console_init_early();
dev->id = DEVICE_ID_DYNAMIC;
strcpy(dev->name, "cs");
if (newcdev->dev)
dev_add_child(newcdev->dev, dev);
platform_device_register(dev);
2007-07-05 16:02:18 +00:00
if (newcdev->setbrg) {
dev_add_param(dev, "baudrate", console_baudrate_set, NULL, 0);
dev_set_param(dev, "baudrate", __stringify(CONFIG_BAUDRATE));
}
dev_add_param(dev, "active", console_std_set, NULL, 0);
if (IS_ENABLED(CONFIG_CONSOLE_ACTIVATE_FIRST)) {
if (list_empty(&console_list))
activate = 1;
} else if (IS_ENABLED(CONFIG_CONSOLE_ACTIVATE_ALL)) {
activate = 1;
}
if (newcdev->dev && of_device_is_stdout_path(newcdev->dev))
activate = 1;
list_add_tail(&newcdev->list, &console_list);
if (activate)
dev_set_param(dev, "active", "ioe");
return 0;
}
2007-10-07 12:27:24 +00:00
EXPORT_SYMBOL(console_register);
2002-11-03 00:01:44 +00:00
int console_unregister(struct console_device *cdev)
{
struct device_d *dev = &cdev->class_dev;
int status;
list_del(&cdev->list);
if (list_empty(&console_list))
initialized = CONSOLE_UNINITIALIZED;
status = unregister_device(dev);
if (!status)
memset(cdev, 0, sizeof(*cdev));
return status;
}
EXPORT_SYMBOL(console_unregister);
static int getc_raw(void)
{
struct console_device *cdev;
int active = 0;
while (1) {
for_each_console(cdev) {
if (!(cdev->f_active & CONSOLE_STDIN))
continue;
active = 1;
if (cdev->tstc(cdev))
return cdev->getc(cdev);
}
if (!active)
/* no active console found. bail out */
return -1;
}
}
2002-11-03 00:01:44 +00:00
static int tstc_raw(void)
{
struct console_device *cdev;
for_each_console(cdev) {
if (!(cdev->f_active & CONSOLE_STDIN))
continue;
if (cdev->tstc(cdev))
return 1;
}
return 0;
}
2007-09-21 19:30:59 +00:00
int getc(void)
{
unsigned char ch;
uint64_t start;
/*
* For 100us we read the characters from the serial driver
* into a kfifo. This helps us not to lose characters
* in small hardware fifos.
*/
2007-09-21 19:30:59 +00:00
start = get_time_ns();
while (1) {
if (tstc_raw()) {
kfifo_putc(console_input_fifo, getc_raw());
2007-09-21 19:30:59 +00:00
start = get_time_ns();
}
if (is_timeout(start, 100 * USECOND) &&
kfifo_len(console_input_fifo))
2007-09-21 19:30:59 +00:00
break;
}
kfifo_getc(console_input_fifo, &ch);
2007-09-21 19:30:59 +00:00
return ch;
}
2007-10-07 12:27:24 +00:00
EXPORT_SYMBOL(getc);
2007-09-21 19:30:59 +00:00
int fgetc(int fd)
{
char c;
2002-11-03 00:01:44 +00:00
if (!fd)
return getc();
return read(fd, &c, 1);
2002-11-03 00:01:44 +00:00
}
2007-10-07 12:27:24 +00:00
EXPORT_SYMBOL(fgetc);
2002-11-03 00:01:44 +00:00
int tstc(void)
2002-11-03 00:01:44 +00:00
{
return kfifo_len(console_input_fifo) || tstc_raw();
2002-11-03 00:01:44 +00:00
}
2007-10-07 12:27:24 +00:00
EXPORT_SYMBOL(tstc);
2002-11-03 00:01:44 +00:00
void console_putc(unsigned int ch, char c)
2002-11-03 00:01:44 +00:00
{
struct console_device *cdev;
int init = initialized;
2007-07-12 07:31:07 +00:00
switch (init) {
case CONSOLE_UNINITIALIZED:
console_init_early();
/* fall through */
case CONSOLE_INITIALIZED_BUFFER:
kfifo_putc(console_output_fifo, c);
2007-07-12 07:31:07 +00:00
return;
case CONSOLE_INIT_FULL:
for_each_console(cdev) {
2007-07-12 07:31:07 +00:00
if (cdev->f_active & ch) {
if (c == '\n')
cdev->putc(cdev, '\r');
cdev->putc(cdev, c);
2007-07-12 07:31:07 +00:00
}
}
2007-07-12 07:31:07 +00:00
return;
default:
/* If we have problems inititalizing our data
* get them early
*/
hang();
}
2002-11-03 00:01:44 +00:00
}
2007-10-07 12:27:24 +00:00
EXPORT_SYMBOL(console_putc);
2002-11-03 00:01:44 +00:00
int fputc(int fd, char c)
2002-11-03 00:01:44 +00:00
{
if(list_empty(&console_list)) {
2007-07-12 07:31:07 +00:00
if(!fd)
console_putc(0, c);
return 0;
}
if (fd == 1)
putchar(c);
else if (fd == 2)
eputc(c);
else
return write(fd, &c, 1);
return 0;
2002-11-03 00:01:44 +00:00
}
2007-10-07 12:27:24 +00:00
EXPORT_SYMBOL(fputc);
2002-11-03 00:01:44 +00:00
int console_puts(unsigned int ch, const char *str)
2002-11-03 00:01:44 +00:00
{
2007-07-12 07:31:07 +00:00
const char *s = str;
int n = 0;
2007-07-12 07:31:07 +00:00
while (*s) {
if (*s == '\n') {
2007-07-12 07:31:07 +00:00
console_putc(ch, '\r');
n++;
}
console_putc(ch, *s);
n++;
2007-07-12 07:31:07 +00:00
s++;
}
return n;
}
2007-10-07 12:27:24 +00:00
EXPORT_SYMBOL(console_puts);
int fputs(int fd, const char *s)
{
if (fd == 1)
return puts(s);
else if (fd == 2)
return eputs(s);
else
return write(fd, s, strlen(s));
2002-11-03 00:01:44 +00:00
}
2007-10-07 12:27:24 +00:00
EXPORT_SYMBOL(fputs);
2002-11-03 00:01:44 +00:00
void console_flush(void)
{
struct console_device *cdev;
for_each_console(cdev) {
if (cdev->flush)
cdev->flush(cdev);
}
}
EXPORT_SYMBOL(console_flush);
int fprintf(int file, const char *fmt, ...)
2007-07-05 16:02:03 +00:00
{
va_list args;
char printbuffer[CFG_PBSIZE];
va_start (args, fmt);
/* For this to work, printbuffer must be larger than
* anything we ever want to print.
*/
vsprintf (printbuffer, fmt, args);
2007-07-05 16:02:03 +00:00
va_end (args);
/* Print the string */
return fputs(file, printbuffer);
2007-07-05 16:02:03 +00:00
}
2007-10-07 12:27:24 +00:00
EXPORT_SYMBOL(fprintf);
2007-07-05 16:02:03 +00:00
int printf (const char *fmt, ...)
2002-11-03 00:01:44 +00:00
{
va_list args;
uint i;
char printbuffer[CFG_PBSIZE];
va_start (args, fmt);
/* For this to work, printbuffer must be larger than
* anything we ever want to print.
*/
i = vsprintf (printbuffer, fmt, args);
va_end (args);
/* Print the string */
puts (printbuffer);
return i;
2002-11-03 00:01:44 +00:00
}
EXPORT_SYMBOL(printf);
2007-07-05 16:02:03 +00:00
int vprintf (const char *fmt, va_list args)
Patches by Murray Jensen, 17 Jun 2003: - Hymod board database mods: add "who" field and new xilinx chip types - provide new "init_cmd_timeout()" function so code external to "common/main.c" can use the "reset_cmd_timeout()" function before entering the main loop - add DTT support for adm1021 (new file dtt/adm1021.c; config slightly different. see include/configs/hymod.h for an example (requires CONFIG_DTT_ADM1021, CONFIG_DTT_SENSORS, and CFG_DTT_ADM1021 defined) - add new "eeprom_probe()" function which has similar args and behaves in a similar way to "eeprom_read()" etc. - add 8260 FCC ethernet loopback code (new "eth_loopback_test()" function which is enabled by defining CONFIG_ETHER_LOOPBACK_TEST) - gdbtools copyright update - ensure that set_msr() executes the "sync" and "isync" instructions after the "mtmsr" instruction in cpu/mpc8260/interrupts.c - 8260 I/O ports fix: Open Drain should be set last when configuring - add SIU IRQ defines for 8260 - allow LDSCRIPT override and OBJCFLAGS initialization: change to config.mk to allow board configurations to override the GNU linker script, selected via the LDSCRIPT, make variable, and to give an initial value to the OBJCFLAGS make variable - 8260 i2c enhancement: o correctly extends the timeout depending on the size of all queued messages for both transmit and receive o will not continue with receive if transmit times out o ensures that the error callback is done for all queued tx and rx messages o correctly detects both tx and rx timeouts, only delivers one to the callback, and does not overwrite an earlier error o logic in i2c_probe now correct - add "vprintf()" function so that "panic()" function can be technically correct - many Hymod board changes
2003-06-19 23:40:20 +00:00
{
uint i;
char printbuffer[CFG_PBSIZE];
/* For this to work, printbuffer must be larger than
* anything we ever want to print.
*/
i = vsprintf (printbuffer, fmt, args);
/* Print the string */
puts (printbuffer);
return i;
Patches by Murray Jensen, 17 Jun 2003: - Hymod board database mods: add "who" field and new xilinx chip types - provide new "init_cmd_timeout()" function so code external to "common/main.c" can use the "reset_cmd_timeout()" function before entering the main loop - add DTT support for adm1021 (new file dtt/adm1021.c; config slightly different. see include/configs/hymod.h for an example (requires CONFIG_DTT_ADM1021, CONFIG_DTT_SENSORS, and CFG_DTT_ADM1021 defined) - add new "eeprom_probe()" function which has similar args and behaves in a similar way to "eeprom_read()" etc. - add 8260 FCC ethernet loopback code (new "eth_loopback_test()" function which is enabled by defining CONFIG_ETHER_LOOPBACK_TEST) - gdbtools copyright update - ensure that set_msr() executes the "sync" and "isync" instructions after the "mtmsr" instruction in cpu/mpc8260/interrupts.c - 8260 I/O ports fix: Open Drain should be set last when configuring - add SIU IRQ defines for 8260 - allow LDSCRIPT override and OBJCFLAGS initialization: change to config.mk to allow board configurations to override the GNU linker script, selected via the LDSCRIPT, make variable, and to give an initial value to the OBJCFLAGS make variable - 8260 i2c enhancement: o correctly extends the timeout depending on the size of all queued messages for both transmit and receive o will not continue with receive if transmit times out o ensures that the error callback is done for all queued tx and rx messages o correctly detects both tx and rx timeouts, only delivers one to the callback, and does not overwrite an earlier error o logic in i2c_probe now correct - add "vprintf()" function so that "panic()" function can be technically correct - many Hymod board changes
2003-06-19 23:40:20 +00:00
}
2007-10-07 12:27:24 +00:00
EXPORT_SYMBOL(vprintf);
Patches by Murray Jensen, 17 Jun 2003: - Hymod board database mods: add "who" field and new xilinx chip types - provide new "init_cmd_timeout()" function so code external to "common/main.c" can use the "reset_cmd_timeout()" function before entering the main loop - add DTT support for adm1021 (new file dtt/adm1021.c; config slightly different. see include/configs/hymod.h for an example (requires CONFIG_DTT_ADM1021, CONFIG_DTT_SENSORS, and CFG_DTT_ADM1021 defined) - add new "eeprom_probe()" function which has similar args and behaves in a similar way to "eeprom_read()" etc. - add 8260 FCC ethernet loopback code (new "eth_loopback_test()" function which is enabled by defining CONFIG_ETHER_LOOPBACK_TEST) - gdbtools copyright update - ensure that set_msr() executes the "sync" and "isync" instructions after the "mtmsr" instruction in cpu/mpc8260/interrupts.c - 8260 I/O ports fix: Open Drain should be set last when configuring - add SIU IRQ defines for 8260 - allow LDSCRIPT override and OBJCFLAGS initialization: change to config.mk to allow board configurations to override the GNU linker script, selected via the LDSCRIPT, make variable, and to give an initial value to the OBJCFLAGS make variable - 8260 i2c enhancement: o correctly extends the timeout depending on the size of all queued messages for both transmit and receive o will not continue with receive if transmit times out o ensures that the error callback is done for all queued tx and rx messages o correctly detects both tx and rx timeouts, only delivers one to the callback, and does not overwrite an earlier error o logic in i2c_probe now correct - add "vprintf()" function so that "panic()" function can be technically correct - many Hymod board changes
2003-06-19 23:40:20 +00:00
#ifndef ARCH_HAS_CTRLC
2002-11-03 00:01:44 +00:00
/* test if ctrl-c was pressed */
int ctrlc (void)
{
poller_call();
if (tstc() && getc() == 3)
return 1;
2002-11-03 00:01:44 +00:00
return 0;
}
2007-10-07 12:27:24 +00:00
EXPORT_SYMBOL(ctrlc);
#endif /* ARCH_HAS_CTRC */