1 /***********************license start***************
2 * Copyright (c) 2003-2010 Cavium Inc. (support@cavium.com). All rights
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * * Neither the name of Cavium Inc. nor the names of
19 * its contributors may be used to endorse or promote products
20 * derived from this software without specific prior written
23 * This Software, including technical data, may be subject to U.S. export control
24 * laws, including the U.S. Export Administration Act and its associated
25 * regulations, and may be subject to export or import regulations in other
28 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
29 * AND WITH ALL FAULTS AND CAVIUM INC. MAKES NO PROMISES, REPRESENTATIONS OR
30 * WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT TO
31 * THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY REPRESENTATION OR
32 * DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT DEFECTS, AND CAVIUM
33 * SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES OF TITLE,
34 * MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR PURPOSE, LACK OF
35 * VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION OR
36 * CORRESPONDENCE TO DESCRIPTION. THE ENTIRE RISK ARISING OUT OF USE OR
37 * PERFORMANCE OF THE SOFTWARE LIES WITH YOU.
38 ***********************license end**************************************/
41 #ifdef CVMX_BUILD_FOR_LINUX_KERNEL
42 #include <linux/module.h>
43 #include <asm/octeon/cvmx.h>
44 #include <asm/octeon/cvmx-debug.h>
45 #include <asm/octeon/cvmx-uart.h>
46 #include <asm/octeon/octeon-boot-info.h>
47 #include <asm/octeon/cvmx-spinlock.h>
49 int cvmx_debug_uart = 1;
53 #include "executive-config.h"
55 #include "cvmx-debug.h"
56 #include "cvmx-uart.h"
57 #include "cvmx-spinlock.h"
58 #include "octeon-boot-info.h"
62 * NOTE: CARE SHOULD BE TAKEN USING STD C LIBRARY FUNCTIONS IN
63 * THIS FILE IF SOMEONE PUTS A BREAKPOINT ON THOSE FUNCTIONS
64 * DEBUGGING WILL FAIL.
68 #ifdef CVMX_BUILD_FOR_TOOLCHAIN
69 #pragma weak cvmx_uart_enable_intr
70 int cvmx_debug_uart = 1;
74 /* Default to second uart port for backward compatibility. The default (if
75 -debug does not set the uart number) can now be overridden with
76 CVMX_DEBUG_COMM_UART_NUM. */
77 #ifndef CVMX_DEBUG_COMM_UART_NUM
78 # define CVMX_DEBUG_COMM_UART_NUM 1
81 static CVMX_SHARED cvmx_spinlock_t cvmx_debug_uart_lock;
84 * Interrupt handler for debugger Control-C interrupts.
86 * @param irq_number IRQ interrupt number
87 * @param registers CPU registers at the time of the interrupt
88 * @param user_arg Unused user argument
90 void cvmx_debug_uart_process_debug_interrupt(int irq_number, uint64_t registers[32], void *user_arg)
92 cvmx_uart_lsr_t lsrval;
94 /* Check for a Control-C interrupt from the debugger. This loop will eat
95 all input received on the uart */
96 lsrval.u64 = cvmx_read_csr(CVMX_MIO_UARTX_LSR(cvmx_debug_uart));
99 int c = cvmx_read_csr(CVMX_MIO_UARTX_RBR(cvmx_debug_uart));
102 register uint64_t tmp;
103 #ifndef CVMX_BUILD_FOR_LINUX_KERNEL
107 /* Pulse MCD0 signal on Ctrl-C to stop all the cores. Also
108 set the MCD0 to be not masked by this core so we know
109 the signal is received by someone */
112 "ori %0, %0, 0x1110\n"
116 lsrval.u64 = cvmx_read_csr(CVMX_MIO_UARTX_LSR(cvmx_debug_uart));
121 static void cvmx_debug_uart_init(void)
123 if (cvmx_debug_uart == -1)
124 cvmx_debug_uart = CVMX_DEBUG_COMM_UART_NUM;
127 static void cvmx_debug_uart_install_break_handler(void)
129 #ifndef CVMX_BUILD_FOR_LINUX_KERNEL
130 #ifdef CVMX_BUILD_FOR_TOOLCHAIN
131 if (cvmx_uart_enable_intr)
133 cvmx_uart_enable_intr(cvmx_debug_uart, cvmx_debug_uart_process_debug_interrupt);
138 * Routines to handle hex data
143 static inline int cvmx_debug_uart_hex(char ch)
145 if ((ch >= 'a') && (ch <= 'f'))
146 return(ch - 'a' + 10);
147 if ((ch >= '0') && (ch <= '9'))
149 if ((ch >= 'A') && (ch <= 'F'))
150 return(ch - 'A' + 10);
154 /* Get a packet from the UART, return 0 on failure and 1 on success. */
156 static int cvmx_debug_uart_getpacket(char *buffer, size_t size)
160 unsigned char checksum;
165 ch = cvmx_uart_read_byte_with_timeout(cvmx_debug_uart, &timedout, __SHRT_MAX__);
170 /* if this is not the start character, ignore it. */
178 /* now, read until a # or end of buffer is found */
181 ch = cvmx_uart_read_byte(cvmx_debug_uart);
186 checksum = checksum + ch;
194 char csumchars0, csumchars1;
198 csumchars0 = cvmx_uart_read_byte(cvmx_debug_uart);
199 csumchars1 = cvmx_uart_read_byte(cvmx_debug_uart);
200 n0 = cvmx_debug_uart_hex(csumchars0);
201 n1 = cvmx_debug_uart_hex(csumchars1);
202 if (n0 == -1 || n1 == -1)
205 xmitcsum = (n0 << 4) | n1;
206 return checksum == xmitcsum;
212 /* Put the hex value of t into str. */
213 static void cvmx_debug_uart_strhex(char *str, unsigned char t)
215 char hexchar[] = "0123456789ABCDEF";
216 str[0] = hexchar[(t>>4)];
217 str[1] = hexchar[t&0xF];
221 static int cvmx_debug_uart_putpacket(char *packet)
225 unsigned char *ptr = (unsigned char *) packet;
228 for (csum = 0, i = 0; ptr[i]; i++)
230 cvmx_debug_uart_strhex(csumstr, csum);
232 cvmx_spinlock_lock(&cvmx_debug_uart_lock);
233 cvmx_uart_write_byte(cvmx_debug_uart, '$');
234 cvmx_uart_write_string(cvmx_debug_uart, packet);
235 cvmx_uart_write_byte(cvmx_debug_uart, '#');
236 cvmx_uart_write_string(cvmx_debug_uart, csumstr);
237 cvmx_spinlock_unlock(&cvmx_debug_uart_lock);
242 static void cvmx_debug_uart_change_core(int oldcore, int newcore)
244 #ifndef CVMX_BUILD_FOR_LINUX_KERNEL
245 cvmx_ciu_intx0_t irq_control;
247 irq_control.u64 = cvmx_read_csr(CVMX_CIU_INTX_EN0(newcore * 2));
248 irq_control.s.uart |= (1u<<cvmx_debug_uart);
249 cvmx_write_csr(CVMX_CIU_INTX_EN0(newcore * 2), irq_control.u64);
251 /* Disable interrupts to this core since he is about to die */
252 irq_control.u64 = cvmx_read_csr(CVMX_CIU_INTX_EN0(oldcore * 2));
253 irq_control.s.uart &= ~(1u<<cvmx_debug_uart);
254 cvmx_write_csr(CVMX_CIU_INTX_EN0(oldcore* 2), irq_control.u64);
258 cvmx_debug_comm_t cvmx_debug_uart_comm =
260 .init = cvmx_debug_uart_init,
261 .install_break_handler = cvmx_debug_uart_install_break_handler,
263 .getpacket = cvmx_debug_uart_getpacket,
264 .putpacket = cvmx_debug_uart_putpacket,
265 .wait_for_resume = NULL,
266 .change_core = cvmx_debug_uart_change_core,