1/*
2 * Copyright 2006, Broadcom Corporation
3 * All Rights Reserved.
4 *
5 * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
6 * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
7 * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
8 * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
9 *
10 * Carsten Langgaard, carstenl@mips.com
11 * Copyright (C) 2000 MIPS Technologies, Inc.  All rights reserved.
12 *
13 * ########################################################################
14 *
15 *  This program is free software; you can distribute it and/or modify it
16 *  under the terms of the GNU General Public License (Version 2) as
17 *  published by the Free Software Foundation.
18 *
19 *  This program is distributed in the hope it will be useful, but WITHOUT
20 *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
22 *  for more details.
23 *
24 *  You should have received a copy of the GNU General Public License along
25 *  with this program; if not, write to the Free Software Foundation, Inc.,
26 *  59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
27 *
28 * ########################################################################
29 *
30 * This is the interface to the remote debugger stub.
31 *
32 */
33
34#include <linux/serialP.h>
35#include <linux/serial_reg.h>
36
37#include <asm/serial.h>
38#include <asm/io.h>
39
40static struct async_struct kdb_port_info = {0};
41
42static __inline__ unsigned int serial_in(struct async_struct *info, int offset)
43{
44	return readb((unsigned long) info->iomem_base +
45		     (offset<<info->iomem_reg_shift));
46}
47
48static __inline__ void serial_out(struct async_struct *info, int offset,
49				  int value)
50{
51	writeb(value, (unsigned long) info->iomem_base +
52	       (offset<<info->iomem_reg_shift));
53}
54
55void rs_kgdb_hook(struct serial_state *ser) {
56	int t;
57
58	kdb_port_info.state = ser;
59	kdb_port_info.magic = SERIAL_MAGIC;
60	kdb_port_info.port = ser->port;
61	kdb_port_info.flags = ser->flags;
62	kdb_port_info.iomem_base = ser->iomem_base;
63	kdb_port_info.iomem_reg_shift = ser->iomem_reg_shift;
64	kdb_port_info.MCR = UART_MCR_DTR | UART_MCR_RTS;
65
66	/*
67	 * Clear all interrupts
68	 */
69	serial_in(&kdb_port_info, UART_LSR);
70	serial_in(&kdb_port_info, UART_RX);
71	serial_in(&kdb_port_info, UART_IIR);
72	serial_in(&kdb_port_info, UART_MSR);
73
74	/*
75	 * Now, initialize the UART
76	 */
77	serial_out(&kdb_port_info, UART_LCR, UART_LCR_WLEN8);	/* reset DLAB */
78	serial_out(&kdb_port_info, UART_MCR, kdb_port_info.MCR);
79
80	/*
81	 * and set the speed of the serial port
82	 * (currently hardwired to 115200 8N1
83	 */
84
85	/* baud rate is fixed to 115200 (is this sufficient?)*/
86	t = kdb_port_info.state->baud_base / 115200;
87	/* set DLAB */
88	serial_out(&kdb_port_info, UART_LCR, UART_LCR_WLEN8 | UART_LCR_DLAB);
89	serial_out(&kdb_port_info, UART_DLL, t & 0xff);/* LS of divisor */
90	serial_out(&kdb_port_info, UART_DLM, t >> 8);  /* MS of divisor */
91	/* reset DLAB */
92	serial_out(&kdb_port_info, UART_LCR, UART_LCR_WLEN8);
93}
94
95int putDebugChar(char c)
96{
97
98	if (!kdb_port_info.state) { 	/* need to init device first */
99		return 0;
100	}
101
102	while ((serial_in(&kdb_port_info, UART_LSR) & UART_LSR_THRE) == 0)
103		;
104
105	serial_out(&kdb_port_info, UART_TX, c);
106
107	return 1;
108}
109
110char getDebugChar(void)
111{
112	if (!kdb_port_info.state) { 	/* need to init device first */
113		return 0;
114	}
115
116	while (!(serial_in(&kdb_port_info, UART_LSR) & 1))
117		;
118
119	return(serial_in(&kdb_port_info, UART_RX));
120}
121