mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Antony Pavlov <antonynpavlov@gmail.com>
To: barebox@lists.infradead.org
Subject: [PATCH v3 04/10] RISC-V: add nmon nano-monitor
Date: Tue, 18 Dec 2018 10:19:37 +0300	[thread overview]
Message-ID: <20181218071943.2560-5-antonynpavlov@gmail.com> (raw)
In-Reply-To: <20181218071943.2560-1-antonynpavlov@gmail.com>

nmon is a tiny (<1024 bytes) monitor program
for the RV32I processors.

It can operate with NO working RAM at all!

It uses only the processor registers and NS16550-compatible
UART port for operation, so it can be used for a memory
controller setup code debugging.

Signed-off-by: Antony Pavlov <antonynpavlov@gmail.com>
---
 arch/riscv/Kconfig                  |  24 +++
 arch/riscv/boot/start.S             |   8 +
 arch/riscv/include/asm/riscv_nmon.h | 234 ++++++++++++++++++++++++++++
 3 files changed, 266 insertions(+)

diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig
index ee532ac11a..f1e69377e5 100644
--- a/arch/riscv/Kconfig
+++ b/arch/riscv/Kconfig
@@ -63,3 +63,27 @@ config BUILTIN_DTB_NAME
 source arch/riscv/mach-erizo/Kconfig
 
 endmenu
+
+menu "RISC-V specific settings"
+
+config HAS_NMON
+	bool
+
+config NMON
+	bool "nmon"
+	depends on HAS_NMON
+	depends on DEBUG_LL
+	help
+	  Say yes here to add the nmon to pbl.
+	  nmon -- nano-monitor program for the RISC-V processors.
+	  It can operate with NO working RAM, using only
+	  the processor registers.
+
+config NMON_HELP
+	bool "nmon help message"
+	depends on NMON
+	help
+	  Say yes here to get the nmon commands message on
+	  every nmon start.
+
+endmenu
diff --git a/arch/riscv/boot/start.S b/arch/riscv/boot/start.S
index d13708740b..d1dbe48b7b 100644
--- a/arch/riscv/boot/start.S
+++ b/arch/riscv/boot/start.S
@@ -18,12 +18,20 @@
 
 #include <asm-generic/memory_layout.h>
 
+#include "mach/debug_ll.h"
+
+#include "asm/riscv_nmon.h"
+
 	.text
 	.section ".text_entry"
 	.align 2
 
 .globl _start
 _start:
+	debug_ll_ns16550_init
+
+	riscv_nmon
+
 	li	sp, STACK_BASE + STACK_SIZE
 
 	/* copy barebox to link location */
diff --git a/arch/riscv/include/asm/riscv_nmon.h b/arch/riscv/include/asm/riscv_nmon.h
new file mode 100644
index 0000000000..caf213cdd8
--- /dev/null
+++ b/arch/riscv/include/asm/riscv_nmon.h
@@ -0,0 +1,234 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * nano-monitor for RISC-V CPU
+ *
+ * Copyright (C) 2016, 2017 Antony Pavlov <antonynpavlov@gmail.com>
+ *
+ * This file is part of barebox.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ */
+
+#ifndef __ASM_RISCV_NMON_H
+#define __ASM_RISCV_NMON_H
+
+#define CODE_ESC	0x1b
+
+.macro nmon_outs msg
+
+	la	a1, \msg
+
+	jal	_nmon_outs
+
+.endm
+
+/*
+ * output a 32-bit value in hex
+ */
+.macro debug_ll_outhexw
+#ifdef CONFIG_DEBUG_LL
+	move	t6, a0
+	li	t5, 32
+
+202:
+	addi	t5, t5, -4
+	srl	a0, t6, t5
+
+	/* output one hex digit */
+	andi	a0, a0, 15
+	li	t4, 10
+	blt	a0, t4, 203f
+
+	addi	a0, a0, ('a' - '9' - 1)
+
+203:
+	addi	a0, a0, '0'
+
+	debug_ll_outc_a0
+
+	li	t4, 1
+	bge	t5, t4, 202b
+
+#endif /* CONFIG_DEBUG_LL */
+.endm
+
+.macro riscv_nmon
+
+#ifdef CONFIG_NMON
+
+nmon_main_help:
+#ifdef CONFIG_NMON_HELP
+	nmon_outs	msg_nmon_help
+#endif /* CONFIG_NMON_HELP */
+
+nmon_main:
+	nmon_outs	msg_prompt
+
+	debug_ll_getc
+
+	li	a0, 'q'
+	bne	s0, a0, 3f
+
+	jal	_nmon_outc_a0
+
+	j	nmon_exit
+
+3:
+	li	a0, 'd'
+	beq	s0, a0, nmon_cmd_d
+
+	li	a0, 'w'
+	beq	s0, a0, nmon_cmd_w
+
+	li	a0, 'g'
+	beq	s0, a0, nmon_cmd_g
+
+	j	nmon_main_help
+
+nmon_cmd_d:
+	jal	_nmon_outc_a0
+
+	li	a0, ' '
+	jal	_nmon_outc_a0
+
+	jal	_nmon_gethexw
+
+	nmon_outs	msg_nl
+
+	lw	a0, (s0)
+	debug_ll_outhexw
+
+	j	nmon_main
+
+nmon_cmd_w:
+	jal	_nmon_outc_a0
+
+	li	a0, ' '
+	jal	_nmon_outc_a0
+
+	jal	_nmon_gethexw
+	move	s2, s0
+
+	li	a0, ' '
+	jal	_nmon_outc_a0
+	jal	_nmon_gethexw
+
+	sw	s0, 0(s2)
+	j	nmon_main
+
+nmon_cmd_g:
+	jal	_nmon_outc_a0
+
+	li	a0, ' '
+	jal	_nmon_outc_a0
+
+	jal	_nmon_gethexw
+	move	s2, s0
+
+	nmon_outs	msg_nl
+
+	jalr	s2
+	j	nmon_main
+
+_nmon_outc_a0:
+	debug_ll_outc_a0
+	jr	ra
+
+_nmon_outs:
+
+	lb	a0, 0(a1)
+	addi	a1, a1, 1
+	beqz	a0, _nmon_jr_ra_exit
+
+	debug_ll_outc_a0
+
+	j	_nmon_outs
+
+_nmon_gethexw:
+
+	li	t3, 8
+	li	t2, 0
+
+_get_hex_digit:
+	debug_ll_getc
+
+	li	s1, CODE_ESC
+	beq	s0, s1, nmon_main
+
+	li	s1, '0'
+	bge	s0, s1, 0f
+	j	_get_hex_digit
+
+0:
+	li	s1, '9'
+	ble	s0, s1, 9f
+
+	li	s1, 'f'
+	ble	s0, s1, 1f
+	j	_get_hex_digit
+
+1:
+	li	s1, 'a'
+	bge	s0, s1, 8f
+
+	j	_get_hex_digit
+
+8: /* s0 \in {'a', 'b' ... 'f'} */
+	sub	a3, s0, s1
+	addi	a3, a3, 0xa
+	j	0f
+
+9: /* s0 \in {'0', '1' ... '9'} */
+	li	a3, '0'
+	sub	a3, s0, a3
+
+0:	move	a0, s0
+	debug_ll_outc_a0
+
+	sll	t2, t2, 4
+	or	t2, t2, a3
+	li	t0, 1
+	sub	t3, t3, t0
+
+	beqz	t3, 0f
+
+	j	_get_hex_digit
+
+0:
+	move	s0, t2
+
+_nmon_jr_ra_exit:
+	jr	ra
+
+msg_prompt:
+	.asciz "\r\nnmon> "
+
+msg_nl:
+	.asciz "\r\n"
+
+msg_bsp:
+	.asciz "\b \b"
+
+#ifdef CONFIG_NMON_HELP
+msg_nmon_help:
+	.ascii "\r\n\r\nnmon commands:\r\n"
+	.ascii " q - quit\r\n"
+	.ascii " d <addr> - read 32-bit word from <addr>\r\n"
+	.ascii " w <addr> <val> - write 32-bit word to <addr>\r\n"
+	.ascii " g <addr> - jump to <addr>\r\n"
+	.asciz "   use <ESC> key to interrupt current command\r\n"
+#endif /* CONFIG_NMON_HELP */
+
+	.align 2
+nmon_exit:
+	nmon_outs	msg_nl
+
+#endif /* CONFIG_NMON */
+
+.endm
+
+#endif /* __ASM_RISCV_NMON_H */
-- 
2.20.0


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  parent reply	other threads:[~2018-12-18  7:20 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-12-18  7:19 [PATCH v3 00/10] Add initial RISC-V architecture support Antony Pavlov
2018-12-18  7:19 ` [PATCH v3 01/10] " Antony Pavlov
2018-12-18  7:19 ` [PATCH v3 02/10] RISC-V: add Erizo SoC support Antony Pavlov
2018-12-18  7:19 ` [PATCH v3 03/10] RISC-V: add low-level debug macros for ns16550 Antony Pavlov
2018-12-18  8:33   ` Oleksij Rempel
2018-12-18  7:19 ` Antony Pavlov [this message]
2018-12-18  7:19 ` [PATCH v3 05/10] RISC-V: erizo: add DEBUG_LL support Antony Pavlov
2018-12-18  7:19 ` [PATCH v3 06/10] RISC-V: erizo: enable nmon Antony Pavlov
2018-12-18  7:19 ` [PATCH v3 07/10] RISC-V: erizo: add nmon image creation Antony Pavlov
2018-12-18  7:19 ` [PATCH v3 08/10] RISC-V: add erizo_generic_defconfig Antony Pavlov
2018-12-18  7:19 ` [PATCH v3 09/10] scripts: add nmon-loader Antony Pavlov
2019-08-11  9:59   ` Antony Pavlov
2019-08-12  7:41     ` Sascha Hauer
2019-08-12  9:01       ` Antony Pavlov
2018-12-18  7:19 ` [PATCH v3 10/10] Documentation: add RISC-V docs Antony Pavlov
2019-01-03 11:18 ` [PATCH v3 00/10] Add initial RISC-V architecture support Sascha Hauer
2019-01-04  8:47   ` Antony Pavlov
2019-01-07  7:54     ` Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20181218071943.2560-5-antonynpavlov@gmail.com \
    --to=antonynpavlov@gmail.com \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox