intel_s1000: scripts: debug, debugserver and flash scripts

This patchset creates Debug, Debugserver and Flash scripts
ensuring support in the ZephyrBinaryRunner mode.

Change-Id: Ib4f7820b1c6a045bd67cf4a031be99cf61e65eca
Signed-off-by: Rajavardhan Gundi <rajavardhan.gundi@intel.com>
Signed-off-by: Anas Nashif <anas.nashif@intel.com>
diff --git a/boards/common/intel_s1000_crb.board.cmake b/boards/common/intel_s1000_crb.board.cmake
new file mode 100644
index 0000000..8d977dd
--- /dev/null
+++ b/boards/common/intel_s1000_crb.board.cmake
@@ -0,0 +1,12 @@
+set(BOARD_FLASH_RUNNER intel_s1000)
+set(BOARD_DEBUG_RUNNER intel_s1000)
+
+board_finalize_runner_args(intel_s1000
+  "--board-dir=${ZEPHYR_BASE}/boards/xtensa/intel_s1000_crb/"
+  "--xt-ocd-dir=/opt/Tensilica/xocd-12.0.4/xt-ocd"
+  "--ocd-topology=topology_dsp0_flyswatter2.xml"
+  "--ocd-jtag-instr=dsp0_gdb.txt"
+  "--gdb-flash-file=load_elf.txt"
+  "--gdb=${TOOLCHAIN_HOME}/bin/xt-gdb"
+  "--kernel-elf=zephyr/zephyr.elf"
+)
diff --git a/boards/xtensa/intel_s1000_crb/board.cmake b/boards/xtensa/intel_s1000_crb/board.cmake
new file mode 100644
index 0000000..26b0499
--- /dev/null
+++ b/boards/xtensa/intel_s1000_crb/board.cmake
@@ -0,0 +1 @@
+include(${ZEPHYR_BASE}/boards/common/intel_s1000_crb.board.cmake)
diff --git a/boards/xtensa/intel_s1000_crb/support/dsp0_gdb.txt b/boards/xtensa/intel_s1000_crb/support/dsp0_gdb.txt
new file mode 100644
index 0000000..bf2c4c9
--- /dev/null
+++ b/boards/xtensa/intel_s1000_crb/support/dsp0_gdb.txt
@@ -0,0 +1,32 @@
+#Initialization script format
+#COMMAND    LENGTH     DATA
+#
+#COMMAND: 1 - Scan DR branch
+#         2 - Scan IR branch
+#         3 - Send TRST
+#
+#LENGTH: Number of bits to send out (in decimal format).
+#
+#DATA:   Data to send out. A sequence of bytes separated by space.
+#        The rightmost bit is scanned out first. E.g. (B4 B3 B2 B1 B0)
+#
+## Examples
+#
+# Perform TAP Reset:
+3
+## all stap disable
+2   8    12
+1   6    0
+#   Scan out MTAP IDCODE command (0x2) a265013h
+## all stap enable
+2   8    12
+1   6    01
+#
+#
+# Scan out (2 cores):
+#   Send Trax Access IR command to 1st core and bypass to 2nd
+#2   10   3 9F
+#   Write to DOSR register of 1st core (NAR part)
+#1    9   1 07
+#   Write 32 bits to DOSR register (NDR part)
+#1   33   1 98 13 45 ab
diff --git a/boards/xtensa/intel_s1000_crb/support/load_elf.txt b/boards/xtensa/intel_s1000_crb/support/load_elf.txt
new file mode 100644
index 0000000..f92eab0
--- /dev/null
+++ b/boards/xtensa/intel_s1000_crb/support/load_elf.txt
@@ -0,0 +1,14 @@
+target remote localhost:20000
+
+# make SRAM writable
+set *(0x71d10) = 0
+set *(0x71d20) = 0
+
+# disable xtensa core power saving
+set *(0x71F90) = 0x71
+
+set pagination off
+set confirm off
+load zephyr/zephyr.elf
+file zephyr/zephyr.elf
+c
diff --git a/boards/xtensa/intel_s1000_crb/support/topology_dsp0_flyswatter2.xml b/boards/xtensa/intel_s1000_crb/support/topology_dsp0_flyswatter2.xml
new file mode 100644
index 0000000..495b99b
--- /dev/null
+++ b/boards/xtensa/intel_s1000_crb/support/topology_dsp0_flyswatter2.xml
@@ -0,0 +1,21 @@
+<configuration>
+  <controller id='Controller0' module='ft2232' probe='flyswatter2' speed='10MHz' />
+  <driver id='XtensaDriver0' cust-idcode='00140101,LX6' module='xtensa' step-intr='mask,stepover,setps' />
+
+  <driver id='TraxDriver0'   module='trax' />
+  <chain controller='Controller0'>
+    <tap id='TAP2' irwidth='8' />
+    <tap id='TAP0' irwidth='5' />
+  </chain>
+
+  <system module='jtag'>
+    <component id='Component0' tap='TAP0' config='trax' />
+
+  </system>
+  <device id='Xtensa0' component='Component0' driver='XtensaDriver0' />
+
+  <application id='GDBStub0' module='gdbstub' port='20000'>
+   <target device='Xtensa0' />
+  </application>
+
+</configuration>
diff --git a/scripts/support/runner/__init__.py b/scripts/support/runner/__init__.py
index 124f209..2d0cc4e 100644
--- a/scripts/support/runner/__init__.py
+++ b/scripts/support/runner/__init__.py
@@ -21,5 +21,6 @@
 from . import pyocd
 from . import qemu
 from . import xtensa
+from . import intel_s1000
 
 __all__ = ['ZephyrBinaryRunner']
diff --git a/scripts/support/runner/intel_s1000.py b/scripts/support/runner/intel_s1000.py
new file mode 100644
index 0000000..31c4c43
--- /dev/null
+++ b/scripts/support/runner/intel_s1000.py
@@ -0,0 +1,138 @@
+# Copyright (c) 2018 Intel Corporation.
+#
+# SPDX-License-Identifier: Apache-2.0
+
+'''Runner for debugging and flashing Intel_s1000 devices'''
+from os import path
+from .core import ZephyrBinaryRunner
+import time
+import signal
+
+DEFAULT_XT_GDB_PORT = 20000
+
+class intel_s1000BinaryRunner(ZephyrBinaryRunner):
+    '''Runner front-end for Intel_s1000.'''
+
+    def __init__(self,
+                 board_dir, xt_ocd_dir,
+                 ocd_topology, ocd_jtag_instr, gdb_flash_file,
+                 elf_name, gdb,
+                 gdb_port=DEFAULT_XT_GDB_PORT, debug=False):
+        super(intel_s1000BinaryRunner, self).__init__(debug=debug)
+        self.board_dir = board_dir
+        self.xt_ocd_dir = xt_ocd_dir
+        self.ocd_topology = ocd_topology
+        self.ocd_jtag_instr = ocd_jtag_instr
+        self.gdb_flash_file = gdb_flash_file
+        self.elf_name = elf_name
+        self.gdb_cmd = gdb
+        self.gdb_port = gdb_port
+
+    @classmethod
+    def name(cls):
+        return 'intel_s1000'
+
+    @classmethod
+    def do_add_parser(cls, parser):
+        # Required
+
+        # Optional
+        parser.add_argument('--gdb-port', default=DEFAULT_XT_GDB_PORT,
+                            help='xt-gdb port, defaults to 20000')
+        parser.add_argument('--xt-ocd-dir', default='/opt/Tensilica/xocd-12.0.4/xt-ocd',
+                            help='ocd-dir, defaults to /opt/Tensilica/xocd-12.0.4/xt-ocd')
+        parser.add_argument('--ocd-topology', default='topology_dsp0_flyswatter2.xml',
+                            help='ocd-topology, defaults to topology_dsp0_flyswatter2.xml')
+        parser.add_argument('--ocd-jtag-instr', default='dsp0_gdb.txt',
+                            help='ocd-jtag-instr, defaults to dsp0_gdb.txt')
+        parser.add_argument('--gdb-flash-file', default='load_elf.txt',
+                            help='gdb-flash-file, defaults to load_elf.txt')
+
+    @classmethod
+    def create_from_args(command, args):
+        return intel_s1000BinaryRunner(
+            args.board_dir, args.xt_ocd_dir,
+            args.ocd_topology, args.ocd_jtag_instr, args.gdb_flash_file,
+            args.kernel_elf, args.gdb,
+            gdb_port=args.gdb_port, debug=args.verbose)
+
+    def do_run(self, command, **kwargs):
+        kwargs['ocd-topology'] =  path.join(self.board_dir, 'support',  self.ocd_topology)
+        kwargs['ocd-jtag-instr'] = path.join(self.board_dir, 'support', self.ocd_jtag_instr)
+        kwargs['gdb-flash-file'] = path.join(self.board_dir, 'support', self.gdb_flash_file)
+
+        if command == 'flash':
+            self.flash(**kwargs)
+        elif command == 'debugserver':
+            self.debugserver(**kwargs)
+        else:
+            self.do_debug()
+
+    def flash(self, **kwargs):
+        topology_file = kwargs['ocd-topology']
+        jtag_instr_file = kwargs['ocd-jtag-instr']
+        gdb_flash_file = kwargs['gdb-flash-file']
+
+        self.print_gdbserver_message(self.gdb_port)
+        server_cmd = [self.xt_ocd_dir,
+                        '-c', topology_file,
+                        '-I', jtag_instr_file]
+
+        # Start the server
+        # Note that XTOCD always fails the first time. It has to be
+        # relaunched the second time to work.
+        self.popen_ignore_int(server_cmd)
+        time.sleep(3)
+        server_proc = self.popen_ignore_int(server_cmd)
+        time.sleep(3)
+
+        # Start the client
+        gdb_cmd = [self.gdb_cmd, '-x', gdb_flash_file]
+        client_proc = self.popen_ignore_int(gdb_cmd)
+
+        # Wait for 3 seconds (waiting for XTGDB to finish)
+        time.sleep(3)
+
+        # At this point, the ELF image is loaded and the program is in
+        # execution. Now we can quit the client (xt-gdb) and the server
+        # (xt-ocd) as they are not needed anymore. The loaded program
+        # (ELF) will continue to run though.
+        client_proc.terminate()
+        server_proc.terminate()
+
+    def do_debug(self):
+        if self.elf_name is None:
+            raise ValueError('Cannot debug; elf is missing')
+        if self.gdb_cmd is None:
+            raise ValueError('Cannot debug; no gdb specified')
+
+        gdb_cmd = [self.gdb_cmd,
+                    '-ex', 'target remote :{}'.format(self.gdb_port),
+                    self.elf_name]
+
+        # The below statement will consume the "^C" keypress ensuring
+        # the python main application doesn't exit. This is important
+        # since ^C in gdb means a "halt" operation.
+        previous = signal.signal(signal.SIGINT, signal.SIG_IGN)
+        try:
+            self.check_call(gdb_cmd)
+        finally:
+            signal.signal(signal.SIGINT, previous)
+
+    def print_gdbserver_message(self, gdb_port):
+        print('Intel S1000 GDB server running on port {}'.format(gdb_port))
+
+    def debugserver(self, **kwargs):
+        topology_file = kwargs['ocd-topology']
+        jtag_instr_file = kwargs['ocd-jtag-instr']
+
+        self.print_gdbserver_message(self.gdb_port)
+        server_cmd = [self.xt_ocd_dir,
+                        '-c', topology_file,
+                        '-I', jtag_instr_file]
+
+        # Note that XTOCD always fails the first time. It has to be
+        # relaunched the second time to work.
+        self.popen_ignore_int(server_cmd)
+        time.sleep(3)
+        self.check_call(server_cmd)