xref: /OK3568_Linux_fs/kernel/arch/sparc/prom/printf.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * printf.c:  Internal prom library printf facility.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
6*4882a593Smuzhiyun  * Copyright (C) 1997 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
7*4882a593Smuzhiyun  * Copyright (c) 2002 Pete Zaitcev (zaitcev@yahoo.com)
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * We used to warn all over the code: DO NOT USE prom_printf(),
10*4882a593Smuzhiyun  * and yet people do. Anton's banking code was outputting banks
11*4882a593Smuzhiyun  * with prom_printf for most of the 2.4 lifetime. Since an effective
12*4882a593Smuzhiyun  * stick is not available, we deployed a carrot: an early printk
13*4882a593Smuzhiyun  * through PROM by means of -p boot option. This ought to fix it.
14*4882a593Smuzhiyun  * USE printk; if you need, deploy -p.
15*4882a593Smuzhiyun  */
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun #include <linux/kernel.h>
18*4882a593Smuzhiyun #include <linux/compiler.h>
19*4882a593Smuzhiyun #include <linux/spinlock.h>
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun #include <asm/openprom.h>
22*4882a593Smuzhiyun #include <asm/oplib.h>
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun #define CONSOLE_WRITE_BUF_SIZE	1024
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun static char ppbuf[1024];
27*4882a593Smuzhiyun static char console_write_buf[CONSOLE_WRITE_BUF_SIZE];
28*4882a593Smuzhiyun static DEFINE_RAW_SPINLOCK(console_write_lock);
29*4882a593Smuzhiyun 
prom_write(const char * buf,unsigned int n)30*4882a593Smuzhiyun void notrace prom_write(const char *buf, unsigned int n)
31*4882a593Smuzhiyun {
32*4882a593Smuzhiyun 	unsigned int dest_len;
33*4882a593Smuzhiyun 	unsigned long flags;
34*4882a593Smuzhiyun 	char *dest;
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun 	dest = console_write_buf;
37*4882a593Smuzhiyun 	raw_spin_lock_irqsave(&console_write_lock, flags);
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun 	dest_len = 0;
40*4882a593Smuzhiyun 	while (n-- != 0) {
41*4882a593Smuzhiyun 		char ch = *buf++;
42*4882a593Smuzhiyun 		if (ch == '\n') {
43*4882a593Smuzhiyun 			*dest++ = '\r';
44*4882a593Smuzhiyun 			dest_len++;
45*4882a593Smuzhiyun 		}
46*4882a593Smuzhiyun 		*dest++ = ch;
47*4882a593Smuzhiyun 		dest_len++;
48*4882a593Smuzhiyun 		if (dest_len >= CONSOLE_WRITE_BUF_SIZE - 1) {
49*4882a593Smuzhiyun 			prom_console_write_buf(console_write_buf, dest_len);
50*4882a593Smuzhiyun 			dest = console_write_buf;
51*4882a593Smuzhiyun 			dest_len = 0;
52*4882a593Smuzhiyun 		}
53*4882a593Smuzhiyun 	}
54*4882a593Smuzhiyun 	if (dest_len)
55*4882a593Smuzhiyun 		prom_console_write_buf(console_write_buf, dest_len);
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun 	raw_spin_unlock_irqrestore(&console_write_lock, flags);
58*4882a593Smuzhiyun }
59*4882a593Smuzhiyun 
prom_printf(const char * fmt,...)60*4882a593Smuzhiyun void notrace prom_printf(const char *fmt, ...)
61*4882a593Smuzhiyun {
62*4882a593Smuzhiyun 	va_list args;
63*4882a593Smuzhiyun 	int i;
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun 	va_start(args, fmt);
66*4882a593Smuzhiyun 	i = vscnprintf(ppbuf, sizeof(ppbuf), fmt, args);
67*4882a593Smuzhiyun 	va_end(args);
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun 	prom_write(ppbuf, i);
70*4882a593Smuzhiyun }
71