Linux Audio

Check our new training course

Loading...
v4.17
  1// SPDX-License-Identifier: GPL-2.0
  2#include <linux/console.h>
  3#include <linux/kernel.h>
  4#include <linux/init.h>
  5#include <linux/string.h>
  6#include <linux/screen_info.h>
  7#include <linux/usb/ch9.h>
  8#include <linux/pci_regs.h>
  9#include <linux/pci_ids.h>
 10#include <linux/errno.h>
 11#include <asm/io.h>
 12#include <asm/processor.h>
 13#include <asm/fcntl.h>
 14#include <asm/setup.h>
 15#include <xen/hvc-console.h>
 16#include <asm/pci-direct.h>
 17#include <asm/fixmap.h>
 18#include <asm/intel-mid.h>
 19#include <asm/pgtable.h>
 20#include <linux/usb/ehci_def.h>
 21#include <linux/usb/xhci-dbgp.h>
 22#include <linux/efi.h>
 23#include <asm/efi.h>
 24#include <asm/pci_x86.h>
 25
 26/* Simple VGA output */
 27#define VGABASE		(__ISA_IO_base + 0xb8000)
 28
 29static int max_ypos = 25, max_xpos = 80;
 30static int current_ypos = 25, current_xpos;
 31
 32static void early_vga_write(struct console *con, const char *str, unsigned n)
 33{
 34	char c;
 35	int  i, k, j;
 36
 37	while ((c = *str++) != '\0' && n-- > 0) {
 38		if (current_ypos >= max_ypos) {
 39			/* scroll 1 line up */
 40			for (k = 1, j = 0; k < max_ypos; k++, j++) {
 41				for (i = 0; i < max_xpos; i++) {
 42					writew(readw(VGABASE+2*(max_xpos*k+i)),
 43					       VGABASE + 2*(max_xpos*j + i));
 44				}
 45			}
 46			for (i = 0; i < max_xpos; i++)
 47				writew(0x720, VGABASE + 2*(max_xpos*j + i));
 48			current_ypos = max_ypos-1;
 49		}
 50#ifdef CONFIG_KGDB_KDB
 51		if (c == '\b') {
 52			if (current_xpos > 0)
 53				current_xpos--;
 54		} else if (c == '\r') {
 55			current_xpos = 0;
 56		} else
 57#endif
 58		if (c == '\n') {
 59			current_xpos = 0;
 60			current_ypos++;
 61		} else if (c != '\r')  {
 62			writew(((0x7 << 8) | (unsigned short) c),
 63			       VGABASE + 2*(max_xpos*current_ypos +
 64						current_xpos++));
 65			if (current_xpos >= max_xpos) {
 66				current_xpos = 0;
 67				current_ypos++;
 68			}
 69		}
 70	}
 71}
 72
 73static struct console early_vga_console = {
 74	.name =		"earlyvga",
 75	.write =	early_vga_write,
 76	.flags =	CON_PRINTBUFFER,
 77	.index =	-1,
 78};
 79
 80/* Serial functions loosely based on a similar package from Klaus P. Gerlicher */
 81
 82static unsigned long early_serial_base = 0x3f8;  /* ttyS0 */
 83
 84#define XMTRDY          0x20
 85
 86#define DLAB		0x80
 87
 88#define TXR             0       /*  Transmit register (WRITE) */
 89#define RXR             0       /*  Receive register  (READ)  */
 90#define IER             1       /*  Interrupt Enable          */
 91#define IIR             2       /*  Interrupt ID              */
 92#define FCR             2       /*  FIFO control              */
 93#define LCR             3       /*  Line control              */
 94#define MCR             4       /*  Modem control             */
 95#define LSR             5       /*  Line Status               */
 96#define MSR             6       /*  Modem Status              */
 97#define DLL             0       /*  Divisor Latch Low         */
 98#define DLH             1       /*  Divisor latch High        */
 99
100static unsigned int io_serial_in(unsigned long addr, int offset)
101{
102	return inb(addr + offset);
103}
104
105static void io_serial_out(unsigned long addr, int offset, int value)
106{
107	outb(value, addr + offset);
108}
109
110static unsigned int (*serial_in)(unsigned long addr, int offset) = io_serial_in;
111static void (*serial_out)(unsigned long addr, int offset, int value) = io_serial_out;
112
113static int early_serial_putc(unsigned char ch)
114{
115	unsigned timeout = 0xffff;
116
117	while ((serial_in(early_serial_base, LSR) & XMTRDY) == 0 && --timeout)
118		cpu_relax();
119	serial_out(early_serial_base, TXR, ch);
120	return timeout ? 0 : -1;
121}
122
123static void early_serial_write(struct console *con, const char *s, unsigned n)
124{
125	while (*s && n-- > 0) {
126		if (*s == '\n')
127			early_serial_putc('\r');
128		early_serial_putc(*s);
129		s++;
130	}
131}
132
133static __init void early_serial_hw_init(unsigned divisor)
134{
135	unsigned char c;
136
137	serial_out(early_serial_base, LCR, 0x3);	/* 8n1 */
138	serial_out(early_serial_base, IER, 0);	/* no interrupt */
139	serial_out(early_serial_base, FCR, 0);	/* no fifo */
140	serial_out(early_serial_base, MCR, 0x3);	/* DTR + RTS */
141
142	c = serial_in(early_serial_base, LCR);
143	serial_out(early_serial_base, LCR, c | DLAB);
144	serial_out(early_serial_base, DLL, divisor & 0xff);
145	serial_out(early_serial_base, DLH, (divisor >> 8) & 0xff);
146	serial_out(early_serial_base, LCR, c & ~DLAB);
147}
148
149#define DEFAULT_BAUD 9600
150
151static __init void early_serial_init(char *s)
152{
 
153	unsigned divisor;
154	unsigned long baud = DEFAULT_BAUD;
155	char *e;
156
157	if (*s == ',')
158		++s;
159
160	if (*s) {
161		unsigned port;
162		if (!strncmp(s, "0x", 2)) {
163			early_serial_base = simple_strtoul(s, &e, 16);
164		} else {
165			static const int __initconst bases[] = { 0x3f8, 0x2f8 };
166
167			if (!strncmp(s, "ttyS", 4))
168				s += 4;
169			port = simple_strtoul(s, &e, 10);
170			if (port > 1 || s == e)
171				port = 0;
172			early_serial_base = bases[port];
173		}
174		s += strcspn(s, ",");
175		if (*s == ',')
176			s++;
177	}
178
179	if (*s) {
180		baud = simple_strtoull(s, &e, 0);
181
182		if (baud == 0 || s == e)
183			baud = DEFAULT_BAUD;
184	}
185
186	/* Convert from baud to divisor value */
187	divisor = 115200 / baud;
188
189	/* These will always be IO based ports */
190	serial_in = io_serial_in;
191	serial_out = io_serial_out;
192
193	/* Set up the HW */
194	early_serial_hw_init(divisor);
195}
196
197#ifdef CONFIG_PCI
198static void mem32_serial_out(unsigned long addr, int offset, int value)
199{
200	u32 __iomem *vaddr = (u32 __iomem *)addr;
201	/* shift implied by pointer type */
202	writel(value, vaddr + offset);
203}
204
205static unsigned int mem32_serial_in(unsigned long addr, int offset)
206{
207	u32 __iomem *vaddr = (u32 __iomem *)addr;
208	/* shift implied by pointer type */
209	return readl(vaddr + offset);
210}
211
212/*
213 * early_pci_serial_init()
214 *
215 * This function is invoked when the early_printk param starts with "pciserial"
216 * The rest of the param should be ",B:D.F,baud" where B, D & F describe the
217 * location of a PCI device that must be a UART device.
218 */
219static __init void early_pci_serial_init(char *s)
220{
221	unsigned divisor;
222	unsigned long baud = DEFAULT_BAUD;
223	u8 bus, slot, func;
224	u32 classcode, bar0;
225	u16 cmdreg;
226	char *e;
227
228
229	/*
230	 * First, part the param to get the BDF values
231	 */
232	if (*s == ',')
233		++s;
234
235	if (*s == 0)
236		return;
237
238	bus = (u8)simple_strtoul(s, &e, 16);
239	s = e;
240	if (*s != ':')
241		return;
242	++s;
243	slot = (u8)simple_strtoul(s, &e, 16);
244	s = e;
245	if (*s != '.')
246		return;
247	++s;
248	func = (u8)simple_strtoul(s, &e, 16);
249	s = e;
250
251	/* A baud might be following */
252	if (*s == ',')
253		s++;
254
255	/*
256	 * Second, find the device from the BDF
257	 */
258	cmdreg = read_pci_config(bus, slot, func, PCI_COMMAND);
259	classcode = read_pci_config(bus, slot, func, PCI_CLASS_REVISION);
260	bar0 = read_pci_config(bus, slot, func, PCI_BASE_ADDRESS_0);
261
262	/*
263	 * Verify it is a UART type device
264	 */
265	if (((classcode >> 16 != PCI_CLASS_COMMUNICATION_MODEM) &&
266	     (classcode >> 16 != PCI_CLASS_COMMUNICATION_SERIAL)) ||
267	   (((classcode >> 8) & 0xff) != 0x02)) /* 16550 I/F at BAR0 */
268		return;
269
270	/*
271	 * Determine if it is IO or memory mapped
272	 */
273	if (bar0 & 0x01) {
274		/* it is IO mapped */
275		serial_in = io_serial_in;
276		serial_out = io_serial_out;
277		early_serial_base = bar0&0xfffffffc;
278		write_pci_config(bus, slot, func, PCI_COMMAND,
279						cmdreg|PCI_COMMAND_IO);
280	} else {
281		/* It is memory mapped - assume 32-bit alignment */
282		serial_in = mem32_serial_in;
283		serial_out = mem32_serial_out;
284		/* WARNING! assuming the address is always in the first 4G */
285		early_serial_base =
286			(unsigned long)early_ioremap(bar0 & 0xfffffff0, 0x10);
287		write_pci_config(bus, slot, func, PCI_COMMAND,
288						cmdreg|PCI_COMMAND_MEMORY);
289	}
290
291	/*
292	 * Lastly, initialize the hardware
293	 */
294	if (*s) {
295		if (strcmp(s, "nocfg") == 0)
296			/* Sometimes, we want to leave the UART alone
297			 * and assume the BIOS has set it up correctly.
298			 * "nocfg" tells us this is the case, and we
299			 * should do no more setup.
300			 */
301			return;
302		if (kstrtoul(s, 0, &baud) < 0 || baud == 0)
303			baud = DEFAULT_BAUD;
304	}
305
306	/* Convert from baud to divisor value */
307	divisor = 115200 / baud;
308
309	/* Set up the HW */
310	early_serial_hw_init(divisor);
 
 
311}
312#endif
313
314static struct console early_serial_console = {
315	.name =		"earlyser",
316	.write =	early_serial_write,
317	.flags =	CON_PRINTBUFFER,
318	.index =	-1,
319};
320
321static void early_console_register(struct console *con, int keep_early)
322{
323	if (con->index != -1) {
324		printk(KERN_CRIT "ERROR: earlyprintk= %s already used\n",
325		       con->name);
326		return;
327	}
328	early_console = con;
329	if (keep_early)
330		early_console->flags &= ~CON_BOOT;
331	else
332		early_console->flags |= CON_BOOT;
333	register_console(early_console);
334}
335
336static int __init setup_early_printk(char *buf)
337{
338	int keep;
339
340	if (!buf)
341		return 0;
342
343	if (early_console)
344		return 0;
345
346	keep = (strstr(buf, "keep") != NULL);
347
348	while (*buf != '\0') {
349		if (!strncmp(buf, "serial", 6)) {
350			buf += 6;
351			early_serial_init(buf);
352			early_console_register(&early_serial_console, keep);
353			if (!strncmp(buf, ",ttyS", 5))
354				buf += 5;
355		}
356		if (!strncmp(buf, "ttyS", 4)) {
357			early_serial_init(buf + 4);
358			early_console_register(&early_serial_console, keep);
359		}
360#ifdef CONFIG_PCI
361		if (!strncmp(buf, "pciserial", 9)) {
362			early_pci_serial_init(buf + 9);
363			early_console_register(&early_serial_console, keep);
364			buf += 9; /* Keep from match the above "serial" */
365		}
366#endif
367		if (!strncmp(buf, "vga", 3) &&
368		    boot_params.screen_info.orig_video_isVGA == 1) {
369			max_xpos = boot_params.screen_info.orig_video_cols;
370			max_ypos = boot_params.screen_info.orig_video_lines;
371			current_ypos = boot_params.screen_info.orig_y;
372			early_console_register(&early_vga_console, keep);
373		}
374#ifdef CONFIG_EARLY_PRINTK_DBGP
375		if (!strncmp(buf, "dbgp", 4) && !early_dbgp_init(buf + 4))
376			early_console_register(&early_dbgp_console, keep);
377#endif
378#ifdef CONFIG_HVC_XEN
379		if (!strncmp(buf, "xen", 3))
380			early_console_register(&xenboot_console, keep);
381#endif
 
 
 
 
 
 
 
 
 
 
 
382#ifdef CONFIG_EARLY_PRINTK_EFI
383		if (!strncmp(buf, "efi", 3))
384			early_console_register(&early_efi_console, keep);
385#endif
386#ifdef CONFIG_EARLY_PRINTK_USB_XDBC
387		if (!strncmp(buf, "xdbc", 4))
388			early_xdbc_parse_parameter(buf + 4);
389#endif
390
391		buf++;
392	}
393	return 0;
394}
395
396early_param("earlyprintk", setup_early_printk);
v3.15
 
  1#include <linux/console.h>
  2#include <linux/kernel.h>
  3#include <linux/init.h>
  4#include <linux/string.h>
  5#include <linux/screen_info.h>
  6#include <linux/usb/ch9.h>
  7#include <linux/pci_regs.h>
  8#include <linux/pci_ids.h>
  9#include <linux/errno.h>
 10#include <asm/io.h>
 11#include <asm/processor.h>
 12#include <asm/fcntl.h>
 13#include <asm/setup.h>
 14#include <xen/hvc-console.h>
 15#include <asm/pci-direct.h>
 16#include <asm/fixmap.h>
 17#include <asm/intel-mid.h>
 18#include <asm/pgtable.h>
 19#include <linux/usb/ehci_def.h>
 
 20#include <linux/efi.h>
 21#include <asm/efi.h>
 
 22
 23/* Simple VGA output */
 24#define VGABASE		(__ISA_IO_base + 0xb8000)
 25
 26static int max_ypos = 25, max_xpos = 80;
 27static int current_ypos = 25, current_xpos;
 28
 29static void early_vga_write(struct console *con, const char *str, unsigned n)
 30{
 31	char c;
 32	int  i, k, j;
 33
 34	while ((c = *str++) != '\0' && n-- > 0) {
 35		if (current_ypos >= max_ypos) {
 36			/* scroll 1 line up */
 37			for (k = 1, j = 0; k < max_ypos; k++, j++) {
 38				for (i = 0; i < max_xpos; i++) {
 39					writew(readw(VGABASE+2*(max_xpos*k+i)),
 40					       VGABASE + 2*(max_xpos*j + i));
 41				}
 42			}
 43			for (i = 0; i < max_xpos; i++)
 44				writew(0x720, VGABASE + 2*(max_xpos*j + i));
 45			current_ypos = max_ypos-1;
 46		}
 47#ifdef CONFIG_KGDB_KDB
 48		if (c == '\b') {
 49			if (current_xpos > 0)
 50				current_xpos--;
 51		} else if (c == '\r') {
 52			current_xpos = 0;
 53		} else
 54#endif
 55		if (c == '\n') {
 56			current_xpos = 0;
 57			current_ypos++;
 58		} else if (c != '\r')  {
 59			writew(((0x7 << 8) | (unsigned short) c),
 60			       VGABASE + 2*(max_xpos*current_ypos +
 61						current_xpos++));
 62			if (current_xpos >= max_xpos) {
 63				current_xpos = 0;
 64				current_ypos++;
 65			}
 66		}
 67	}
 68}
 69
 70static struct console early_vga_console = {
 71	.name =		"earlyvga",
 72	.write =	early_vga_write,
 73	.flags =	CON_PRINTBUFFER,
 74	.index =	-1,
 75};
 76
 77/* Serial functions loosely based on a similar package from Klaus P. Gerlicher */
 78
 79static int early_serial_base = 0x3f8;  /* ttyS0 */
 80
 81#define XMTRDY          0x20
 82
 83#define DLAB		0x80
 84
 85#define TXR             0       /*  Transmit register (WRITE) */
 86#define RXR             0       /*  Receive register  (READ)  */
 87#define IER             1       /*  Interrupt Enable          */
 88#define IIR             2       /*  Interrupt ID              */
 89#define FCR             2       /*  FIFO control              */
 90#define LCR             3       /*  Line control              */
 91#define MCR             4       /*  Modem control             */
 92#define LSR             5       /*  Line Status               */
 93#define MSR             6       /*  Modem Status              */
 94#define DLL             0       /*  Divisor Latch Low         */
 95#define DLH             1       /*  Divisor latch High        */
 96
 
 
 
 
 
 
 
 
 
 
 
 
 
 97static int early_serial_putc(unsigned char ch)
 98{
 99	unsigned timeout = 0xffff;
100
101	while ((inb(early_serial_base + LSR) & XMTRDY) == 0 && --timeout)
102		cpu_relax();
103	outb(ch, early_serial_base + TXR);
104	return timeout ? 0 : -1;
105}
106
107static void early_serial_write(struct console *con, const char *s, unsigned n)
108{
109	while (*s && n-- > 0) {
110		if (*s == '\n')
111			early_serial_putc('\r');
112		early_serial_putc(*s);
113		s++;
114	}
115}
116
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117#define DEFAULT_BAUD 9600
118
119static __init void early_serial_init(char *s)
120{
121	unsigned char c;
122	unsigned divisor;
123	unsigned baud = DEFAULT_BAUD;
124	char *e;
125
126	if (*s == ',')
127		++s;
128
129	if (*s) {
130		unsigned port;
131		if (!strncmp(s, "0x", 2)) {
132			early_serial_base = simple_strtoul(s, &e, 16);
133		} else {
134			static const int __initconst bases[] = { 0x3f8, 0x2f8 };
135
136			if (!strncmp(s, "ttyS", 4))
137				s += 4;
138			port = simple_strtoul(s, &e, 10);
139			if (port > 1 || s == e)
140				port = 0;
141			early_serial_base = bases[port];
142		}
143		s += strcspn(s, ",");
144		if (*s == ',')
145			s++;
146	}
147
148	outb(0x3, early_serial_base + LCR);	/* 8n1 */
149	outb(0, early_serial_base + IER);	/* no interrupt */
150	outb(0, early_serial_base + FCR);	/* no fifo */
151	outb(0x3, early_serial_base + MCR);	/* DTR + RTS */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
152
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
153	if (*s) {
154		baud = simple_strtoul(s, &e, 0);
155		if (baud == 0 || s == e)
 
 
 
 
 
 
156			baud = DEFAULT_BAUD;
157	}
158
 
159	divisor = 115200 / baud;
160	c = inb(early_serial_base + LCR);
161	outb(c | DLAB, early_serial_base + LCR);
162	outb(divisor & 0xff, early_serial_base + DLL);
163	outb((divisor >> 8) & 0xff, early_serial_base + DLH);
164	outb(c & ~DLAB, early_serial_base + LCR);
165}
 
166
167static struct console early_serial_console = {
168	.name =		"earlyser",
169	.write =	early_serial_write,
170	.flags =	CON_PRINTBUFFER,
171	.index =	-1,
172};
173
174static inline void early_console_register(struct console *con, int keep_early)
175{
176	if (con->index != -1) {
177		printk(KERN_CRIT "ERROR: earlyprintk= %s already used\n",
178		       con->name);
179		return;
180	}
181	early_console = con;
182	if (keep_early)
183		early_console->flags &= ~CON_BOOT;
184	else
185		early_console->flags |= CON_BOOT;
186	register_console(early_console);
187}
188
189static int __init setup_early_printk(char *buf)
190{
191	int keep;
192
193	if (!buf)
194		return 0;
195
196	if (early_console)
197		return 0;
198
199	keep = (strstr(buf, "keep") != NULL);
200
201	while (*buf != '\0') {
202		if (!strncmp(buf, "serial", 6)) {
203			buf += 6;
204			early_serial_init(buf);
205			early_console_register(&early_serial_console, keep);
206			if (!strncmp(buf, ",ttyS", 5))
207				buf += 5;
208		}
209		if (!strncmp(buf, "ttyS", 4)) {
210			early_serial_init(buf + 4);
211			early_console_register(&early_serial_console, keep);
212		}
 
 
 
 
 
 
 
213		if (!strncmp(buf, "vga", 3) &&
214		    boot_params.screen_info.orig_video_isVGA == 1) {
215			max_xpos = boot_params.screen_info.orig_video_cols;
216			max_ypos = boot_params.screen_info.orig_video_lines;
217			current_ypos = boot_params.screen_info.orig_y;
218			early_console_register(&early_vga_console, keep);
219		}
220#ifdef CONFIG_EARLY_PRINTK_DBGP
221		if (!strncmp(buf, "dbgp", 4) && !early_dbgp_init(buf + 4))
222			early_console_register(&early_dbgp_console, keep);
223#endif
224#ifdef CONFIG_HVC_XEN
225		if (!strncmp(buf, "xen", 3))
226			early_console_register(&xenboot_console, keep);
227#endif
228#ifdef CONFIG_EARLY_PRINTK_INTEL_MID
229		if (!strncmp(buf, "mrst", 4)) {
230			mrst_early_console_init();
231			early_console_register(&early_mrst_console, keep);
232		}
233
234		if (!strncmp(buf, "hsu", 3)) {
235			hsu_early_console_init(buf + 3);
236			early_console_register(&early_hsu_console, keep);
237		}
238#endif
239#ifdef CONFIG_EARLY_PRINTK_EFI
240		if (!strncmp(buf, "efi", 3))
241			early_console_register(&early_efi_console, keep);
 
 
 
 
242#endif
243
244		buf++;
245	}
246	return 0;
247}
248
249early_param("earlyprintk", setup_early_printk);