]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/contrib/octeon-sdk/cvmx-debug-uart.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / sys / contrib / octeon-sdk / cvmx-debug-uart.c
1 /***********************license start***************
2  * Copyright (c) 2003-2010  Cavium Networks (support@cavium.com). All rights
3  * reserved.
4  *
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  *   * Redistributions of source code must retain the above copyright
11  *     notice, this list of conditions and the following disclaimer.
12  *
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.
17
18  *   * Neither the name of Cavium Networks nor the names of
19  *     its contributors may be used to endorse or promote products
20  *     derived from this software without specific prior written
21  *     permission.
22
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
26  * countries.
27
28  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
29  * AND WITH ALL FAULTS AND CAVIUM  NETWORKS 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**************************************/
39
40
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>
48
49 int cvmx_debug_uart = 1;
50
51 #else
52 #include <limits.h>
53 #include "executive-config.h"
54 #include "cvmx.h"
55 #include "cvmx-debug.h"
56 #include "cvmx-uart.h"
57 #include "cvmx-spinlock.h"
58
59 #ifndef __OCTEON_NEWLIB__
60 #include "../../bootloader/u-boot/include/octeon_mem_map.h"
61 #else
62 #include "octeon-boot-info.h"
63 #endif
64
65 #endif
66
67
68 #ifdef __OCTEON_NEWLIB__
69 #pragma weak cvmx_uart_enable_intr
70 int cvmx_debug_uart = 1;
71 #endif
72
73
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
79 #endif
80
81 static CVMX_SHARED cvmx_spinlock_t cvmx_debug_uart_lock;
82
83 /**
84  * Interrupt handler for debugger Control-C interrupts.
85  *
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
89  */
90 void cvmx_debug_uart_process_debug_interrupt(int irq_number, uint64_t registers[32], void *user_arg)
91 {
92     cvmx_uart_lsr_t lsrval;
93
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));
97     while (lsrval.s.dr)
98     {
99         int c = cvmx_read_csr(CVMX_MIO_UARTX_RBR(cvmx_debug_uart));
100         if (c == '\003')
101         {
102             register uint64_t tmp;
103 #ifndef CVMX_BUILD_FOR_LINUX_KERNEL
104             fflush(stderr);
105             fflush(stdout);
106 #endif
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 */
110             asm volatile (
111                 "dmfc0 %0, $22\n"
112                 "ori   %0, %0, 0x1110\n"
113                 "dmtc0 %0, $22\n"
114                 : "=r" (tmp));
115         }
116         lsrval.u64 = cvmx_read_csr(CVMX_MIO_UARTX_LSR(cvmx_debug_uart));
117     }
118 }
119
120
121 static void cvmx_debug_uart_init(void)
122 {
123     if (cvmx_debug_uart == -1)
124         cvmx_debug_uart = CVMX_DEBUG_COMM_UART_NUM;
125 }
126
127 static void cvmx_debug_uart_install_break_handler(void)
128 {
129 #ifndef CVMX_BUILD_FOR_LINUX_KERNEL
130 #ifdef __OCTEON_NEWLIB__
131     if (cvmx_uart_enable_intr)
132 #endif
133         cvmx_uart_enable_intr(cvmx_debug_uart, cvmx_debug_uart_process_debug_interrupt);
134 #endif
135 }
136
137 /* Get a packet from the UART, return 0 on failure and 1 on success. */
138
139 static int cvmx_debug_uart_getpacket(char *buffer, size_t size)
140 {
141     while (1)
142     {
143         unsigned char checksum;
144         int timedout = 0;
145         size_t count;
146         char ch;
147
148         ch = cvmx_uart_read_byte_with_timeout(cvmx_debug_uart, &timedout, __SHRT_MAX__);
149
150         if (timedout)
151             return 0;
152
153         /* if this is not the start character, ignore it. */
154         if (ch != '$')
155             continue;
156
157         retry:
158         checksum = 0;
159         count = 0;
160
161         /* now, read until a # or end of buffer is found */
162         while (count < size)
163         {
164             ch = cvmx_uart_read_byte(cvmx_debug_uart);
165             if (ch == '$')
166                 goto retry;
167             if (ch == '#')
168                 break;
169             checksum = checksum + ch;
170             buffer[count] = ch;
171             count = count + 1;
172         }
173         buffer[count] = 0;
174
175         if (ch == '#')
176         {
177             char csumchars[2];
178             unsigned xmitcsum;
179             int n;
180
181             csumchars[0] = cvmx_uart_read_byte(cvmx_debug_uart);
182             csumchars[1] = cvmx_uart_read_byte(cvmx_debug_uart);
183             n = sscanf(csumchars, "%2x", &xmitcsum);
184             if (n != 1)
185                 return 1;
186
187             return checksum == xmitcsum;
188         }
189     }
190     return 0;
191 }
192
193 static int cvmx_debug_uart_putpacket(char *packet)
194 {
195     size_t i;
196     unsigned char csum;
197     unsigned char *ptr = (unsigned char *) packet;
198     char csumstr[3];
199
200     for (csum = 0, i = 0; ptr[i]; i++)
201       csum += ptr[i];
202     sprintf(csumstr, "%02x", csum);
203
204     cvmx_spinlock_lock(&cvmx_debug_uart_lock);
205     cvmx_uart_write_byte(cvmx_debug_uart, '$');
206     cvmx_uart_write_string(cvmx_debug_uart, packet);
207     cvmx_uart_write_byte(cvmx_debug_uart, '#');
208     cvmx_uart_write_string(cvmx_debug_uart, csumstr);
209     cvmx_spinlock_unlock(&cvmx_debug_uart_lock);
210
211     return 0;
212 }
213
214 static void cvmx_debug_uart_change_core(int oldcore, int newcore)
215 {
216 #ifndef CVMX_BUILD_FOR_LINUX_KERNEL
217     cvmx_ciu_intx0_t irq_control;
218
219     irq_control.u64 = cvmx_read_csr(CVMX_CIU_INTX_EN0(newcore * 2));
220     irq_control.s.uart |= (1<<cvmx_debug_uart);
221     cvmx_write_csr(CVMX_CIU_INTX_EN0(newcore * 2), irq_control.u64);
222
223     /* Disable interrupts to this core since he is about to die */
224     irq_control.u64 = cvmx_read_csr(CVMX_CIU_INTX_EN0(oldcore * 2));
225     irq_control.s.uart &= ~(1<<cvmx_debug_uart);
226     cvmx_write_csr(CVMX_CIU_INTX_EN0(oldcore* 2), irq_control.u64);
227 #endif
228 }
229
230 cvmx_debug_comm_t cvmx_debug_uart_comm =
231 {
232   .init = cvmx_debug_uart_init,
233   .install_break_handler = cvmx_debug_uart_install_break_handler,
234   .needs_proxy = 1,
235   .getpacket = cvmx_debug_uart_getpacket,
236   .putpacket = cvmx_debug_uart_putpacket,
237   .wait_for_resume = NULL,
238   .change_core = cvmx_debug_uart_change_core,
239 };