From 2355ba148a49c8d36b88b159552052c9908bfa26 Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Mon, 26 Jul 2021 17:06:05 +0800 Subject: [PATCH 1/9] openocd: use upstream nixpkgs version if recent enough --- artiq-fast/pkgs/openocd.nix | 39 ++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/artiq-fast/pkgs/openocd.nix b/artiq-fast/pkgs/openocd.nix index 9d32c24..0d56cac 100644 --- a/artiq-fast/pkgs/openocd.nix +++ b/artiq-fast/pkgs/openocd.nix @@ -1,6 +1,13 @@ -{ stdenv, lib, fetchFromGitHub, autoreconfHook, libftdi, libusb1, pkgconfig, hidapi }: +{ stdenv, buildEnv, lib, fetchFromGitHub, autoreconfHook, libftdi, libusb1, pkgconfig, hidapi, openocd }: +let +bscan_spi_bitstreams = fetchFromGitHub { + owner = "quartiq"; + repo = "bscan_spi_bitstreams"; + rev = "01d8f819f15baf9a8cc5d96945a51e4d267ff564"; + sha256 = "1zqv47kzgvbn4c8cr019a6wcja7gn5h1z4kvw5bhpc72fyhagal9"; +}; -stdenv.mkDerivation rec { +openocd-mlabs = stdenv.mkDerivation rec { pname = "openocd-mlabs"; version = "0.10.0"; @@ -11,12 +18,7 @@ stdenv.mkDerivation rec { rev = "c383a57adcff332b2c5cf8d55a84626285b42c2c"; sha256 = "0xlj9cs72acx3zqagvr7f1c0v6lnqhl8fgrlhgmhmvk5n9knk492"; }; - bscan_spi_bitstreams = fetchFromGitHub { - owner = "quartiq"; - repo = "bscan_spi_bitstreams"; - rev = "01d8f819f15baf9a8cc5d96945a51e4d267ff564"; - sha256 = "1zqv47kzgvbn4c8cr019a6wcja7gn5h1z4kvw5bhpc72fyhagal9"; - }; + nativeBuildInputs = [ pkgconfig ]; buildInputs = [ autoreconfHook libftdi libusb1 hidapi ]; @@ -71,4 +73,23 @@ stdenv.mkDerivation rec { maintainers = with maintainers; [ sb0 ]; platforms = platforms.linux; }; -} +}; + +bscan_spi_bitstreams-pkg = stdenv.mkDerivation { + name = "bscan_spi_bitstreams"; + src = bscan_spi_bitstreams; + phases = ["installPhase"]; + installPhase = + '' + mkdir -p $out/share/bscan-spi-bitstreams + cp $src/*.bit $out/share/bscan-spi-bitstreams + ''; +}; + +openocd-upstream = buildEnv { + name = "openocd-bscanspi"; + paths = [ openocd bscan_spi_bitstreams-pkg ]; +}; + +in + if lib.versionAtLeast openocd.version "0.11" then openocd-upstream else openocd-mlabs From f595103f178434d924c86ffc68ab81ab325fa114 Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Mon, 26 Jul 2021 17:21:57 +0800 Subject: [PATCH 2/9] conda: fix access to bscan-spi-bitstreams --- artiq-fast/conda/bscan-spi-bitstreams.nix | 2 +- artiq-fast/default.nix | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/artiq-fast/conda/bscan-spi-bitstreams.nix b/artiq-fast/conda/bscan-spi-bitstreams.nix index e70379c..ba78405 100644 --- a/artiq-fast/conda/bscan-spi-bitstreams.nix +++ b/artiq-fast/conda/bscan-spi-bitstreams.nix @@ -6,7 +6,7 @@ let mkdir -p $out/fake-conda; # work around yet more idiotic conda behavior - build breaks if write permissions aren't set on source files. - cp --no-preserve=mode,ownership -R ${bscan_spi_bitstreams} workaround-conda + cp --no-preserve=mode,ownership -L -R ${bscan_spi_bitstreams} workaround-conda pushd workaround-conda tar cf $out/src.tar . popd diff --git a/artiq-fast/default.nix b/artiq-fast/default.nix index 46be656..8f74b10 100644 --- a/artiq-fast/default.nix +++ b/artiq-fast/default.nix @@ -76,7 +76,7 @@ let }; conda-bscan-spi-bitstreams = import ./conda/bscan-spi-bitstreams.nix { inherit pkgs; - inherit (mainPackages.openocd) bscan_spi_bitstreams; + bscan_spi_bitstreams = "${mainPackages.openocd}/share/bscan-spi-bitstreams"; }; conda-artiq = import ./conda/artiq.nix { inherit pkgs; }; conda-asyncserial = import ./conda/build.nix { inherit pkgs; } { From c9efc20aeb5ad5b81fe9d987fa5dc35e37b7dd9b Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Tue, 27 Jul 2021 10:22:07 +0800 Subject: [PATCH 3/9] switch everything to nixpkgs 21.05 No problems found during testing and OpenOCD changes make HITL tricky while keeping 20.09. --- artiq-fast/default.nix | 2 +- artiq-fast/pkgs/openocd.nix | 109 ++++++------------------------------ artiq-zynq.nix | 1 - hydra/artiq.json | 12 ++-- 4 files changed, 25 insertions(+), 99 deletions(-) diff --git a/artiq-fast/default.nix b/artiq-fast/default.nix index 8f74b10..bed127c 100644 --- a/artiq-fast/default.nix +++ b/artiq-fast/default.nix @@ -35,7 +35,7 @@ let libartiq-support = callPackage ./pkgs/libartiq-support.nix { inherit rustc; }; artiq = callPackage ./pkgs/artiq.nix { inherit pythonDeps binutils-or1k binutils-arm llvm-or1k llvmlite-artiq libartiq-support lit outputcheck; }; artiq-env = (pkgs.python3.withPackages(ps: [ artiq ])).overrideAttrs (oldAttrs: { name = "${pkgs.python3.name}-artiq-env-${artiq.version}"; }); - openocd = callPackage ./pkgs/openocd.nix { autoreconfHook = pkgs.autoreconfHook269 or pkgs.autoreconfHook; }; + openocd = callPackage ./pkgs/openocd.nix { }; }; condaNoarch = { diff --git a/artiq-fast/pkgs/openocd.nix b/artiq-fast/pkgs/openocd.nix index 0d56cac..0549faf 100644 --- a/artiq-fast/pkgs/openocd.nix +++ b/artiq-fast/pkgs/openocd.nix @@ -1,95 +1,22 @@ -{ stdenv, buildEnv, lib, fetchFromGitHub, autoreconfHook, libftdi, libusb1, pkgconfig, hidapi, openocd }: +{ stdenv, buildEnv, lib, fetchFromGitHub, openocd }: let -bscan_spi_bitstreams = fetchFromGitHub { - owner = "quartiq"; - repo = "bscan_spi_bitstreams"; - rev = "01d8f819f15baf9a8cc5d96945a51e4d267ff564"; - sha256 = "1zqv47kzgvbn4c8cr019a6wcja7gn5h1z4kvw5bhpc72fyhagal9"; -}; - -openocd-mlabs = stdenv.mkDerivation rec { - pname = "openocd-mlabs"; - version = "0.10.0"; - - src = fetchFromGitHub { - owner = "m-labs"; - repo = "openocd"; - fetchSubmodules = true; - rev = "c383a57adcff332b2c5cf8d55a84626285b42c2c"; - sha256 = "0xlj9cs72acx3zqagvr7f1c0v6lnqhl8fgrlhgmhmvk5n9knk492"; - }; - - - nativeBuildInputs = [ pkgconfig ]; - buildInputs = [ autoreconfHook libftdi libusb1 hidapi ]; - - configureFlags = [ - "--enable-jtag_vpi" - "--enable-usb_blaster_libftdi" - "--enable-amtjtagaccel" - "--enable-gw16012" - "--enable-presto_libftdi" - "--enable-openjtag_ftdi" - "--enable-oocd_trace" - "--enable-buspirate" - "--enable-sysfsgpio" - "--enable-remote-bitbang" - "--disable-werror" - ]; - - NIX_CFLAGS_COMPILE = [ - "-Wno-implicit-fallthrough" - "-Wno-format-truncation" - "-Wno-format-overflow" - "-Wno-error=tautological-compare" - ]; - - postInstall = '' - mkdir -p "$out/etc/udev/rules.d" - rules="$out/share/openocd/contrib/60-openocd.rules" - if [ ! -f "$rules" ]; then - echo "$rules is missing, must update the Nix file." - exit 1 - fi - ln -s "$rules" "$out/etc/udev/rules.d/" - - mkdir -p "$out/share/bscan-spi-bitstreams" - cp ${bscan_spi_bitstreams}/*.bit "$out/share/bscan-spi-bitstreams" - ''; - - meta = with lib; { - description = "Free and Open On-Chip Debugging, In-System Programming and Boundary-Scan Testing"; - longDescription = '' - OpenOCD provides on-chip programming and debugging support with a layered - architecture of JTAG interface and TAP support, debug target support - (e.g. ARM, MIPS), and flash chip drivers (e.g. CFI, NAND, etc.). Several - network interfaces are available for interactiving with OpenOCD: HTTP, - telnet, TCL, and GDB. The GDB server enables OpenOCD to function as a - "remote target" for source-level debugging of embedded systems using the - GNU GDB program. + bscan_spi_bitstreams-pkg = stdenv.mkDerivation { + name = "bscan_spi_bitstreams"; + src = fetchFromGitHub { + owner = "quartiq"; + repo = "bscan_spi_bitstreams"; + rev = "01d8f819f15baf9a8cc5d96945a51e4d267ff564"; + sha256 = "1zqv47kzgvbn4c8cr019a6wcja7gn5h1z4kvw5bhpc72fyhagal9"; + }; + phases = ["installPhase"]; + installPhase = + '' + mkdir -p $out/share/bscan-spi-bitstreams + cp $src/*.bit $out/share/bscan-spi-bitstreams ''; - homepage = http://openocd.sourceforge.net/; - license = licenses.gpl2Plus; - maintainers = with maintainers; [ sb0 ]; - platforms = platforms.linux; }; -}; - -bscan_spi_bitstreams-pkg = stdenv.mkDerivation { - name = "bscan_spi_bitstreams"; - src = bscan_spi_bitstreams; - phases = ["installPhase"]; - installPhase = - '' - mkdir -p $out/share/bscan-spi-bitstreams - cp $src/*.bit $out/share/bscan-spi-bitstreams - ''; -}; - -openocd-upstream = buildEnv { - name = "openocd-bscanspi"; - paths = [ openocd bscan_spi_bitstreams-pkg ]; -}; - in - if lib.versionAtLeast openocd.version "0.11" then openocd-upstream else openocd-mlabs + buildEnv { + name = "openocd-bscanspi"; + paths = [ openocd bscan_spi_bitstreams-pkg ]; + } diff --git a/artiq-zynq.nix b/artiq-zynq.nix index 2423339..7d1518f 100644 --- a/artiq-zynq.nix +++ b/artiq-zynq.nix @@ -45,7 +45,6 @@ in export SZL=${szlEnv} pushd ${} export NIX_SSHOPTS="-F /dev/null -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o LogLevel=ERROR -i /opt/hydra_id_rsa" - ssh $NIX_SSHOPTS rpi-4 "nix-store -r ${} --add-root openocd-pkgs --indirect && nix-env -e openocd && nix-env -iA openocd -f openocd-pkgs && rm openocd-pkgs" bash ${}/remote_run.sh -h rpi-4 -o "$NIX_SSHOPTS" -d ${artiq-zynq.zc706-nist_qc2-jtag} popd diff --git a/hydra/artiq.json b/hydra/artiq.json index 73c1aa3..88db575 100644 --- a/hydra/artiq.json +++ b/hydra/artiq.json @@ -65,7 +65,7 @@ "emailoverride": "", "keepnr": 10, "inputs": { - "nixpkgs": { "type": "git", "value": "git://github.com/NixOS/nixpkgs nixos-20.09", "emailresponsible": false }, + "nixpkgs": { "type": "git", "value": "git://github.com/NixOS/nixpkgs nixos-21.05", "emailresponsible": false }, "nixScripts": { "type": "git", "value": "https://git.m-labs.hk/M-Labs/nix-scripts.git", "emailresponsible": false }, "artiqSrc": { "type": "git", "value": "git://github.com/m-labs/artiq.git release-6 1", "emailresponsible": false } } @@ -82,7 +82,7 @@ "emailoverride": "", "keepnr": 10, "inputs": { - "nixpkgs": { "type": "git", "value": "git://github.com/NixOS/nixpkgs nixos-20.09", "emailresponsible": false }, + "nixpkgs": { "type": "git", "value": "git://github.com/NixOS/nixpkgs nixos-21.05", "emailresponsible": false }, "nixScripts": { "type": "git", "value": "https://git.m-labs.hk/M-Labs/nix-scripts.git", "emailresponsible": false }, "sinaraSystemsSrc": { "type": "git", "value": "https://git.m-labs.hk/M-Labs/sinara-systems.git master 1", "emailresponsible": false }, "artiq-fast": { "type": "sysbuild", "value": "artiq:fast:generated-nix", "emailresponsible": false } @@ -100,7 +100,7 @@ "emailoverride": "", "keepnr": 10, "inputs": { - "nixpkgs": { "type": "git", "value": "git://github.com/NixOS/nixpkgs nixos-20.09", "emailresponsible": false }, + "nixpkgs": { "type": "git", "value": "git://github.com/NixOS/nixpkgs nixos-21.05", "emailresponsible": false }, "nixScripts": { "type": "git", "value": "https://git.m-labs.hk/M-Labs/nix-scripts.git", "emailresponsible": false }, "a6p": { "type": "boolean", "value": "true" }, "artiq-board-generated": { "type": "sysbuild", "value": "artiq:board-generated:generated-nix", "emailresponsible": false } @@ -119,7 +119,7 @@ "emailoverride": "", "keepnr": 10, "inputs": { - "nixpkgs": { "type": "git", "value": "git://github.com/NixOS/nixpkgs nixos-20.09", "emailresponsible": false }, + "nixpkgs": { "type": "git", "value": "git://github.com/NixOS/nixpkgs nixos-21.05", "emailresponsible": false }, "nixScripts": { "type": "git", "value": "https://git.m-labs.hk/M-Labs/nix-scripts.git", "emailresponsible": false }, "artiqSrc": { "type": "git", "value": "git://github.com/m-labs/artiq.git release-5 1", "emailresponsible": false } } @@ -136,7 +136,7 @@ "emailoverride": "", "keepnr": 10, "inputs": { - "nixpkgs": { "type": "git", "value": "git://github.com/NixOS/nixpkgs nixos-20.09", "emailresponsible": false }, + "nixpkgs": { "type": "git", "value": "git://github.com/NixOS/nixpkgs nixos-21.05", "emailresponsible": false }, "nixScripts": { "type": "git", "value": "https://git.m-labs.hk/M-Labs/nix-scripts.git", "emailresponsible": false }, "sinaraSystemsSrc": { "type": "git", "value": "https://git.m-labs.hk/M-Labs/sinara-systems.git master 1", "emailresponsible": false }, "a6p": { "type": "boolean", "value": "false" }, @@ -220,7 +220,7 @@ "emailoverride": "", "keepnr": 10, "inputs": { - "nixpkgs": { "type": "git", "value": "git://github.com/NixOS/nixpkgs nixos-20.09", "emailresponsible": false }, + "nixpkgs": { "type": "git", "value": "git://github.com/NixOS/nixpkgs nixos-21.05", "emailresponsible": false }, "nixScripts": { "type": "git", "value": "https://git.m-labs.hk/M-Labs/nix-scripts.git", "emailresponsible": false }, "artiq-fast": { "type": "sysbuild", "value": "artiq:fast:generated-nix", "emailresponsible": false }, "artiq-zynq": { "type": "git", "value": "https://git.m-labs.hk/M-Labs/artiq-zynq.git release-6", "emailresponsible": false }, From e79318171bb39d3efea78f925a6600b095f1f2aa Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Tue, 27 Jul 2021 11:07:08 +0800 Subject: [PATCH 4/9] update misoc Use misoc with newer (risc-v) libunwind for beta. Update misoc for ARTIQ legacy to the version used in stable, so we do not have to carry too many versions around. unstable-2021-02-15 has seen good testing with 10/100 Ethernet support on Kasli being the main change. --- artiq-fast/default.nix | 3 ++- artiq-fast/pkgs/python-deps.nix | 10 +++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/artiq-fast/default.nix b/artiq-fast/default.nix index bed127c..d3af6aa 100644 --- a/artiq-fast/default.nix +++ b/artiq-fast/default.nix @@ -2,7 +2,8 @@ with pkgs; let artiq6 = pkgs.lib.strings.versionAtLeast mainPackages.artiq.version "6.0"; - pythonDeps = import ./pkgs/python-deps.nix { inherit (pkgs) lib fetchgit fetchFromGitHub python3Packages; misoc-new = artiq6; }; + artiq7 = pkgs.lib.strings.versionAtLeast mainPackages.artiq.version "7.0"; + pythonDeps = import ./pkgs/python-deps.nix { inherit (pkgs) lib fetchgit fetchFromGitHub python3Packages; misoc-new = artiq7; }; boards = [ { target = "kasli"; variant = "tester"; } diff --git a/artiq-fast/pkgs/python-deps.nix b/artiq-fast/pkgs/python-deps.nix index c0d511b..da90002 100644 --- a/artiq-fast/pkgs/python-deps.nix +++ b/artiq-fast/pkgs/python-deps.nix @@ -86,21 +86,21 @@ rec { misoc = python3Packages.buildPythonPackage { pname = "misoc"; - version = if misoc-new then "unstable-2021-02-15" else "unstable-2020-05-29"; + version = if misoc-new then "unstable-2021-07-22" else "unstable-2021-02-15"; src = if misoc-new then (fetchFromGitHub { owner = "m-labs"; repo = "misoc"; - rev = "d84551418042cea0891ea743442e20684b51e77a"; - sha256 = "1id5qjr9dl4r3vi6jdn7dfpnr2wb08nrm3nfscn18clbbdxybyjn"; + rev = "79a27a82050266ce4d99d35ca0fe6b53a44649ac"; + sha256 = "10pq3ll3h7mrfgk552nlr80v26gr4mvmpb5pd2h5ck2s9gpkkb82"; fetchSubmodules = true; }) else (fetchFromGitHub { owner = "m-labs"; repo = "misoc"; - rev = "7e5fe8d38835175202dad2c51d37b20b76fd9e16"; - sha256 = "0i8bppz7x2s45lx9n49c0r87pqps09z35yzc17amvx21qsplahxn"; + rev = "d84551418042cea0891ea743442e20684b51e77a"; + sha256 = "1id5qjr9dl4r3vi6jdn7dfpnr2wb08nrm3nfscn18clbbdxybyjn"; fetchSubmodules = true; }); From 9a76e0fdb670806039942216a540a00f0f5cf56a Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Tue, 27 Jul 2021 18:25:35 +0800 Subject: [PATCH 5/9] openocd: patch SPI flash snafu. Closes #62 --- artiq-fast/pkgs/openocd-jtagspi.nix | 24 ++++++++++++++++++++++++ artiq-fast/pkgs/openocd.nix | 6 +++++- 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 artiq-fast/pkgs/openocd-jtagspi.nix diff --git a/artiq-fast/pkgs/openocd-jtagspi.nix b/artiq-fast/pkgs/openocd-jtagspi.nix new file mode 100644 index 0000000..fa41f94 --- /dev/null +++ b/artiq-fast/pkgs/openocd-jtagspi.nix @@ -0,0 +1,24 @@ +diff --git a/src/flash/nor/spi.c b/src/flash/nor/spi.c +index af72ffc4..e5c9a9bd 100644 +--- a/src/flash/nor/spi.c ++++ b/src/flash/nor/spi.c +@@ -90,8 +90,8 @@ const struct flash_device flash_devices[] = { + FLASH_ID("mac 25r6435f", 0x03, 0x00, 0x02, 0xd8, 0xc7, 0x001728c2, 0x100, 0x10000, 0x800000), + FLASH_ID("micron n25q064", 0x03, 0xeb, 0x02, 0xd8, 0xc7, 0x0017ba20, 0x100, 0x10000, 0x800000), + FLASH_ID("micron n25q128", 0x03, 0xeb, 0x02, 0xd8, 0xc7, 0x0018ba20, 0x100, 0x10000, 0x1000000), +- FLASH_ID("micron n25q256 3v", 0x13, 0xec, 0x12, 0xdc, 0xc7, 0x0019ba20, 0x100, 0x10000, 0x2000000), +- FLASH_ID("micron n25q256 1.8v", 0x13, 0xec, 0x12, 0xdc, 0xc7, 0x0019bb20, 0x100, 0x10000, 0x2000000), ++ FLASH_ID("micron n25q256 3v", 0x03, 0xeb, 0x02, 0xd8, 0xc7, 0x0019ba20, 0x100, 0x10000, 0x2000000), ++ FLASH_ID("micron n25q256 1.8v", 0x03, 0xeb, 0x02, 0xd8, 0xc7, 0x0019bb20, 0x100, 0x10000, 0x2000000), + FLASH_ID("micron mt25ql512", 0x13, 0xec, 0x12, 0xdc, 0xc7, 0x0020ba20, 0x100, 0x10000, 0x4000000), + FLASH_ID("micron mt25ql01", 0x13, 0xec, 0x12, 0xdc, 0xc7, 0x0021ba20, 0x100, 0x10000, 0x8000000), + FLASH_ID("micron mt25ql02", 0x13, 0xec, 0x12, 0xdc, 0xc7, 0x0022ba20, 0x100, 0x10000, 0x10000000), +@@ -124,7 +124,7 @@ const struct flash_device flash_devices[] = { + FLASH_ID("issi is25lp064", 0x03, 0x00, 0x02, 0xd8, 0xc7, 0x0017609d, 0x100, 0x10000, 0x800000), + FLASH_ID("issi is25lp128d", 0x03, 0xeb, 0x02, 0xd8, 0xc7, 0x0018609d, 0x100, 0x10000, 0x1000000), + FLASH_ID("issi is25wp128d", 0x03, 0xeb, 0x02, 0xd8, 0xc7, 0x0018709d, 0x100, 0x10000, 0x1000000), +- FLASH_ID("issi is25lp256d", 0x13, 0xec, 0x12, 0xdc, 0xc7, 0x0019609d, 0x100, 0x10000, 0x2000000), ++ FLASH_ID("issi is25lp256d", 0x03, 0xeb, 0x02, 0xd8, 0xc7, 0x0019609d, 0x100, 0x10000, 0x2000000), + FLASH_ID("issi is25wp256d", 0x13, 0xec, 0x12, 0xdc, 0xc7, 0x0019709d, 0x100, 0x10000, 0x2000000), + FLASH_ID("issi is25lp512m", 0x13, 0xec, 0x12, 0xdc, 0xc7, 0x001a609d, 0x100, 0x10000, 0x4000000), + FLASH_ID("issi is25wp512m", 0x13, 0xec, 0x12, 0xdc, 0xc7, 0x001a709d, 0x100, 0x10000, 0x4000000), diff --git a/artiq-fast/pkgs/openocd.nix b/artiq-fast/pkgs/openocd.nix index 0549faf..c9b2b11 100644 --- a/artiq-fast/pkgs/openocd.nix +++ b/artiq-fast/pkgs/openocd.nix @@ -15,8 +15,12 @@ let cp $src/*.bit $out/share/bscan-spi-bitstreams ''; }; + # https://docs.lambdaconcept.com/screamer/troubleshooting.html#error-contents-differ + openocd-fixed = openocd.overrideAttrs(oa: { + patches = oa.patches or [] ++ [ ./openocd-jtagspi.nix ]; + }); in buildEnv { name = "openocd-bscanspi"; - paths = [ openocd bscan_spi_bitstreams-pkg ]; + paths = [ openocd-fixed bscan_spi_bitstreams-pkg ]; } From 367c3d5ed5617a9af4dcaa42d361b57372f4e37b Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Tue, 27 Jul 2021 18:33:50 +0800 Subject: [PATCH 6/9] openocd: better SPI flash patch (#62) --- artiq-fast/pkgs/openocd-jtagspi.nix | 595 ++++++++++++++++++++++++++-- artiq-fast/pkgs/openocd.nix | 2 +- 2 files changed, 572 insertions(+), 25 deletions(-) diff --git a/artiq-fast/pkgs/openocd-jtagspi.nix b/artiq-fast/pkgs/openocd-jtagspi.nix index fa41f94..af184c9 100644 --- a/artiq-fast/pkgs/openocd-jtagspi.nix +++ b/artiq-fast/pkgs/openocd-jtagspi.nix @@ -1,24 +1,571 @@ -diff --git a/src/flash/nor/spi.c b/src/flash/nor/spi.c -index af72ffc4..e5c9a9bd 100644 ---- a/src/flash/nor/spi.c -+++ b/src/flash/nor/spi.c -@@ -90,8 +90,8 @@ const struct flash_device flash_devices[] = { - FLASH_ID("mac 25r6435f", 0x03, 0x00, 0x02, 0xd8, 0xc7, 0x001728c2, 0x100, 0x10000, 0x800000), - FLASH_ID("micron n25q064", 0x03, 0xeb, 0x02, 0xd8, 0xc7, 0x0017ba20, 0x100, 0x10000, 0x800000), - FLASH_ID("micron n25q128", 0x03, 0xeb, 0x02, 0xd8, 0xc7, 0x0018ba20, 0x100, 0x10000, 0x1000000), -- FLASH_ID("micron n25q256 3v", 0x13, 0xec, 0x12, 0xdc, 0xc7, 0x0019ba20, 0x100, 0x10000, 0x2000000), -- FLASH_ID("micron n25q256 1.8v", 0x13, 0xec, 0x12, 0xdc, 0xc7, 0x0019bb20, 0x100, 0x10000, 0x2000000), -+ FLASH_ID("micron n25q256 3v", 0x03, 0xeb, 0x02, 0xd8, 0xc7, 0x0019ba20, 0x100, 0x10000, 0x2000000), -+ FLASH_ID("micron n25q256 1.8v", 0x03, 0xeb, 0x02, 0xd8, 0xc7, 0x0019bb20, 0x100, 0x10000, 0x2000000), - FLASH_ID("micron mt25ql512", 0x13, 0xec, 0x12, 0xdc, 0xc7, 0x0020ba20, 0x100, 0x10000, 0x4000000), - FLASH_ID("micron mt25ql01", 0x13, 0xec, 0x12, 0xdc, 0xc7, 0x0021ba20, 0x100, 0x10000, 0x8000000), - FLASH_ID("micron mt25ql02", 0x13, 0xec, 0x12, 0xdc, 0xc7, 0x0022ba20, 0x100, 0x10000, 0x10000000), -@@ -124,7 +124,7 @@ const struct flash_device flash_devices[] = { - FLASH_ID("issi is25lp064", 0x03, 0x00, 0x02, 0xd8, 0xc7, 0x0017609d, 0x100, 0x10000, 0x800000), - FLASH_ID("issi is25lp128d", 0x03, 0xeb, 0x02, 0xd8, 0xc7, 0x0018609d, 0x100, 0x10000, 0x1000000), - FLASH_ID("issi is25wp128d", 0x03, 0xeb, 0x02, 0xd8, 0xc7, 0x0018709d, 0x100, 0x10000, 0x1000000), -- FLASH_ID("issi is25lp256d", 0x13, 0xec, 0x12, 0xdc, 0xc7, 0x0019609d, 0x100, 0x10000, 0x2000000), -+ FLASH_ID("issi is25lp256d", 0x03, 0xeb, 0x02, 0xd8, 0xc7, 0x0019609d, 0x100, 0x10000, 0x2000000), - FLASH_ID("issi is25wp256d", 0x13, 0xec, 0x12, 0xdc, 0xc7, 0x0019709d, 0x100, 0x10000, 0x2000000), - FLASH_ID("issi is25lp512m", 0x13, 0xec, 0x12, 0xdc, 0xc7, 0x001a609d, 0x100, 0x10000, 0x4000000), - FLASH_ID("issi is25wp512m", 0x13, 0xec, 0x12, 0xdc, 0xc7, 0x001a709d, 0x100, 0x10000, 0x4000000), +diff '--color=auto' -Naur openocd-0.11.0/src/flash/nor/jtagspi.c openocd-0.11.0.new/src/flash/nor/jtagspi.c +--- openocd-0.11.0/src/flash/nor/jtagspi.c 2020-12-10 03:43:09.000000000 +0800 ++++ openocd-0.11.0.new/src/flash/nor/jtagspi.c 2021-07-27 18:31:32.179205804 +0800 +@@ -82,7 +82,7 @@ + struct scan_field fields[6]; + uint8_t marker = 1; + uint8_t xfer_bits_buf[4]; +- uint8_t addr_buf[3]; ++ uint8_t addr_buf[4]; + uint8_t *data_buf; + uint32_t xfer_bits; + int is_read, lenb, n; +@@ -102,9 +102,12 @@ + + xfer_bits = 8 + len - 1; + /* cmd + read/write - 1 due to the counter implementation */ +- if (addr) +- xfer_bits += 24; +- h_u32_to_be(xfer_bits_buf, xfer_bits); ++ if (addr) { ++ if (bank->size > (1 << 24)) ++ xfer_bits += 32; ++ else ++ xfer_bits += 24; ++ } + flip_u8(xfer_bits_buf, xfer_bits_buf, 4); + fields[n].num_bits = 32; + fields[n].out_value = xfer_bits_buf; +@@ -118,9 +121,15 @@ + n++; + + if (addr) { +- h_u24_to_be(addr_buf, *addr); +- flip_u8(addr_buf, addr_buf, 3); +- fields[n].num_bits = 24; ++ if (bank->size > (1 << 24)) { ++ h_u32_to_be(addr_buf, *addr); ++ flip_u8(addr_buf, addr_buf, 4); ++ fields[n].num_bits = 32; ++ } else { ++ h_u24_to_be(addr_buf, *addr); ++ flip_u8(addr_buf, addr_buf, 3); ++ fields[n].num_bits = 24; ++ } + fields[n].out_value = addr_buf; + fields[n].in_value = NULL; + n++; +@@ -304,11 +313,14 @@ + struct jtagspi_flash_bank *info = bank->driver_priv; + int retval; + int64_t t0 = timeval_ms(); ++ uint8_t erase_cmd = info->dev->erase_cmd; + + retval = jtagspi_write_enable(bank); + if (retval != ERROR_OK) + return retval; +- jtagspi_cmd(bank, info->dev->erase_cmd, &bank->sectors[sector].offset, NULL, 0); ++ if (bank->size > (1 << 24)) ++ erase_cmd = SPIFLASH_4BYTE_SECTOR_ERASE; ++ jtagspi_cmd(bank, erase_cmd, &bank->sectors[sector].offset, NULL, 0); + retval = jtagspi_wait(bank, JTAGSPI_MAX_TIMEOUT); + LOG_INFO("sector %u took %" PRId64 " ms", sector, timeval_ms() - t0); + return retval; +@@ -374,24 +386,36 @@ + static int jtagspi_read(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count) + { + struct jtagspi_flash_bank *info = bank->driver_priv; ++ uint8_t read_cmd = SPIFLASH_READ; + + if (!(info->probed)) { + LOG_ERROR("Flash bank not yet probed."); + return ERROR_FLASH_BANK_NOT_PROBED; + } ++ if (count >= (1 << 28)) { ++ LOG_ERROR("Read too large."); ++ return ERROR_FAIL; ++ } + +- jtagspi_cmd(bank, SPIFLASH_READ, &offset, buffer, -count*8); ++ if (bank->size > (1 << 24)) ++ read_cmd = SPIFLASH_4BYTE_READ; ++ jtagspi_cmd(bank, read_cmd, &offset, buffer, -count*8); + return ERROR_OK; + } + + static int jtagspi_page_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count) + { + int retval; ++ uint8_t program_cmd = SPIFLASH_PAGE_PROGRAM; + + retval = jtagspi_write_enable(bank); + if (retval != ERROR_OK) + return retval; +- jtagspi_cmd(bank, SPIFLASH_PAGE_PROGRAM, &offset, (uint8_t *) buffer, count*8); ++ ++ if (bank->size > (1 << 24)) ++ program_cmd = SPIFLASH_4BYTE_PAGE_PROGRAM; ++ ++ jtagspi_cmd(bank, program_cmd, &offset, (uint8_t *) buffer, count*8); + return jtagspi_wait(bank, JTAGSPI_MAX_TIMEOUT); + } + +diff '--color=auto' -Naur openocd-0.11.0/src/flash/nor/jtagspi.c.orig openocd-0.11.0.new/src/flash/nor/jtagspi.c.orig +--- openocd-0.11.0/src/flash/nor/jtagspi.c.orig 1970-01-01 08:00:00.000000000 +0800 ++++ openocd-0.11.0.new/src/flash/nor/jtagspi.c.orig 2020-12-10 03:43:09.000000000 +0800 +@@ -0,0 +1,452 @@ ++/*************************************************************************** ++ * Copyright (C) 2015 Robert Jordens * ++ * * ++ * This program is free software; you can redistribute it and/or modify * ++ * it under the terms of the GNU General Public License as published by * ++ * the Free Software Foundation; either version 2 of the License, or * ++ * (at your option) any later version. * ++ * * ++ * 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. * ++ * * ++ * You should have received a copy of the GNU General Public License * ++ * along with this program. If not, see . * ++ ***************************************************************************/ ++ ++#ifdef HAVE_CONFIG_H ++#include "config.h" ++#endif ++ ++#include "imp.h" ++#include ++#include ++#include ++ ++#define JTAGSPI_MAX_TIMEOUT 3000 ++ ++ ++struct jtagspi_flash_bank { ++ struct jtag_tap *tap; ++ const struct flash_device *dev; ++ bool probed; ++ uint32_t ir; ++}; ++ ++FLASH_BANK_COMMAND_HANDLER(jtagspi_flash_bank_command) ++{ ++ struct jtagspi_flash_bank *info; ++ ++ if (CMD_ARGC < 7) ++ return ERROR_COMMAND_SYNTAX_ERROR; ++ ++ info = malloc(sizeof(struct jtagspi_flash_bank)); ++ if (info == NULL) { ++ LOG_ERROR("no memory for flash bank info"); ++ return ERROR_FAIL; ++ } ++ bank->driver_priv = info; ++ ++ info->tap = NULL; ++ info->probed = false; ++ COMMAND_PARSE_NUMBER(u32, CMD_ARGV[6], info->ir); ++ ++ return ERROR_OK; ++} ++ ++static void jtagspi_set_ir(struct flash_bank *bank) ++{ ++ struct jtagspi_flash_bank *info = bank->driver_priv; ++ struct scan_field field; ++ uint8_t buf[4] = { 0 }; ++ ++ LOG_DEBUG("loading jtagspi ir"); ++ buf_set_u32(buf, 0, info->tap->ir_length, info->ir); ++ field.num_bits = info->tap->ir_length; ++ field.out_value = buf; ++ field.in_value = NULL; ++ jtag_add_ir_scan(info->tap, &field, TAP_IDLE); ++} ++ ++static void flip_u8(uint8_t *in, uint8_t *out, int len) ++{ ++ for (int i = 0; i < len; i++) ++ out[i] = flip_u32(in[i], 8); ++} ++ ++static int jtagspi_cmd(struct flash_bank *bank, uint8_t cmd, ++ uint32_t *addr, uint8_t *data, int len) ++{ ++ struct jtagspi_flash_bank *info = bank->driver_priv; ++ struct scan_field fields[6]; ++ uint8_t marker = 1; ++ uint8_t xfer_bits_buf[4]; ++ uint8_t addr_buf[3]; ++ uint8_t *data_buf; ++ uint32_t xfer_bits; ++ int is_read, lenb, n; ++ ++ /* LOG_DEBUG("cmd=0x%02x len=%i", cmd, len); */ ++ ++ is_read = (len < 0); ++ if (is_read) ++ len = -len; ++ ++ n = 0; ++ ++ fields[n].num_bits = 1; ++ fields[n].out_value = ▮ ++ fields[n].in_value = NULL; ++ n++; ++ ++ xfer_bits = 8 + len - 1; ++ /* cmd + read/write - 1 due to the counter implementation */ ++ if (addr) ++ xfer_bits += 24; ++ h_u32_to_be(xfer_bits_buf, xfer_bits); ++ flip_u8(xfer_bits_buf, xfer_bits_buf, 4); ++ fields[n].num_bits = 32; ++ fields[n].out_value = xfer_bits_buf; ++ fields[n].in_value = NULL; ++ n++; ++ ++ cmd = flip_u32(cmd, 8); ++ fields[n].num_bits = 8; ++ fields[n].out_value = &cmd; ++ fields[n].in_value = NULL; ++ n++; ++ ++ if (addr) { ++ h_u24_to_be(addr_buf, *addr); ++ flip_u8(addr_buf, addr_buf, 3); ++ fields[n].num_bits = 24; ++ fields[n].out_value = addr_buf; ++ fields[n].in_value = NULL; ++ n++; ++ } ++ ++ lenb = DIV_ROUND_UP(len, 8); ++ data_buf = malloc(lenb); ++ if (lenb > 0) { ++ if (data_buf == NULL) { ++ LOG_ERROR("no memory for spi buffer"); ++ return ERROR_FAIL; ++ } ++ if (is_read) { ++ fields[n].num_bits = jtag_tap_count_enabled(); ++ fields[n].out_value = NULL; ++ fields[n].in_value = NULL; ++ n++; ++ ++ fields[n].out_value = NULL; ++ fields[n].in_value = data_buf; ++ } else { ++ flip_u8(data, data_buf, lenb); ++ fields[n].out_value = data_buf; ++ fields[n].in_value = NULL; ++ } ++ fields[n].num_bits = len; ++ n++; ++ } ++ ++ jtagspi_set_ir(bank); ++ /* passing from an IR scan to SHIFT-DR clears BYPASS registers */ ++ jtag_add_dr_scan(info->tap, n, fields, TAP_IDLE); ++ int retval = jtag_execute_queue(); ++ ++ if (is_read) ++ flip_u8(data_buf, data, lenb); ++ free(data_buf); ++ return retval; ++} ++ ++static int jtagspi_probe(struct flash_bank *bank) ++{ ++ struct jtagspi_flash_bank *info = bank->driver_priv; ++ struct flash_sector *sectors; ++ uint8_t in_buf[3]; ++ uint32_t id, sectorsize; ++ ++ if (info->probed) ++ free(bank->sectors); ++ info->probed = false; ++ ++ if (bank->target->tap == NULL) { ++ LOG_ERROR("Target has no JTAG tap"); ++ return ERROR_FAIL; ++ } ++ info->tap = bank->target->tap; ++ ++ jtagspi_cmd(bank, SPIFLASH_READ_ID, NULL, in_buf, -24); ++ /* the table in spi.c has the manufacturer byte (first) as the lsb */ ++ id = le_to_h_u24(in_buf); ++ ++ info->dev = NULL; ++ for (const struct flash_device *p = flash_devices; p->name ; p++) ++ if (p->device_id == id) { ++ info->dev = p; ++ break; ++ } ++ ++ if (!(info->dev)) { ++ LOG_ERROR("Unknown flash device (ID 0x%08" PRIx32 ")", id); ++ return ERROR_FAIL; ++ } ++ ++ LOG_INFO("Found flash device \'%s\' (ID 0x%08" PRIx32 ")", ++ info->dev->name, info->dev->device_id); ++ ++ /* Set correct size value */ ++ bank->size = info->dev->size_in_bytes; ++ if (bank->size <= (1UL << 16)) ++ LOG_WARNING("device needs 2-byte addresses - not implemented"); ++ if (bank->size > (1UL << 24)) ++ LOG_WARNING("device needs paging or 4-byte addresses - not implemented"); ++ ++ /* if no sectors, treat whole bank as single sector */ ++ sectorsize = info->dev->sectorsize ? ++ info->dev->sectorsize : info->dev->size_in_bytes; ++ ++ /* create and fill sectors array */ ++ bank->num_sectors = info->dev->size_in_bytes / sectorsize; ++ sectors = malloc(sizeof(struct flash_sector) * bank->num_sectors); ++ if (sectors == NULL) { ++ LOG_ERROR("not enough memory"); ++ return ERROR_FAIL; ++ } ++ ++ for (unsigned int sector = 0; sector < bank->num_sectors; sector++) { ++ sectors[sector].offset = sector * sectorsize; ++ sectors[sector].size = sectorsize; ++ sectors[sector].is_erased = -1; ++ sectors[sector].is_protected = 0; ++ } ++ ++ bank->sectors = sectors; ++ info->probed = true; ++ return ERROR_OK; ++} ++ ++static int jtagspi_read_status(struct flash_bank *bank, uint32_t *status) ++{ ++ uint8_t buf; ++ int err = jtagspi_cmd(bank, SPIFLASH_READ_STATUS, NULL, &buf, -8); ++ if (err == ERROR_OK) { ++ *status = buf; ++ /* LOG_DEBUG("status=0x%08" PRIx32, *status); */ ++ } ++ ++ return err; ++} ++ ++static int jtagspi_wait(struct flash_bank *bank, int timeout_ms) ++{ ++ uint32_t status; ++ int64_t t0 = timeval_ms(); ++ int64_t dt; ++ ++ do { ++ dt = timeval_ms() - t0; ++ ++ int retval = jtagspi_read_status(bank, &status); ++ if (retval != ERROR_OK) ++ return retval; ++ ++ if ((status & SPIFLASH_BSY_BIT) == 0) { ++ LOG_DEBUG("waited %" PRId64 " ms", dt); ++ return ERROR_OK; ++ } ++ alive_sleep(1); ++ } while (dt <= timeout_ms); ++ ++ LOG_ERROR("timeout, device still busy"); ++ return ERROR_FAIL; ++} ++ ++static int jtagspi_write_enable(struct flash_bank *bank) ++{ ++ uint32_t status; ++ ++ jtagspi_cmd(bank, SPIFLASH_WRITE_ENABLE, NULL, NULL, 0); ++ ++ int retval = jtagspi_read_status(bank, &status); ++ if (retval != ERROR_OK) ++ return retval; ++ ++ if ((status & SPIFLASH_WE_BIT) == 0) { ++ LOG_ERROR("Cannot enable write to flash. Status=0x%08" PRIx32, status); ++ return ERROR_FAIL; ++ } ++ return ERROR_OK; ++} ++ ++static int jtagspi_bulk_erase(struct flash_bank *bank) ++{ ++ struct jtagspi_flash_bank *info = bank->driver_priv; ++ int retval; ++ int64_t t0 = timeval_ms(); ++ ++ if (info->dev->chip_erase_cmd == 0x00) ++ return ERROR_FLASH_OPER_UNSUPPORTED; ++ ++ retval = jtagspi_write_enable(bank); ++ if (retval != ERROR_OK) ++ return retval; ++ jtagspi_cmd(bank, info->dev->chip_erase_cmd, NULL, NULL, 0); ++ retval = jtagspi_wait(bank, bank->num_sectors*JTAGSPI_MAX_TIMEOUT); ++ LOG_INFO("took %" PRId64 " ms", timeval_ms() - t0); ++ return retval; ++} ++ ++static int jtagspi_sector_erase(struct flash_bank *bank, unsigned int sector) ++{ ++ struct jtagspi_flash_bank *info = bank->driver_priv; ++ int retval; ++ int64_t t0 = timeval_ms(); ++ ++ retval = jtagspi_write_enable(bank); ++ if (retval != ERROR_OK) ++ return retval; ++ jtagspi_cmd(bank, info->dev->erase_cmd, &bank->sectors[sector].offset, NULL, 0); ++ retval = jtagspi_wait(bank, JTAGSPI_MAX_TIMEOUT); ++ LOG_INFO("sector %u took %" PRId64 " ms", sector, timeval_ms() - t0); ++ return retval; ++} ++ ++static int jtagspi_erase(struct flash_bank *bank, unsigned int first, ++ unsigned int last) ++{ ++ struct jtagspi_flash_bank *info = bank->driver_priv; ++ int retval = ERROR_OK; ++ ++ LOG_DEBUG("erase from sector %u to sector %u", first, last); ++ ++ if ((last < first) || (last >= bank->num_sectors)) { ++ LOG_ERROR("Flash sector invalid"); ++ return ERROR_FLASH_SECTOR_INVALID; ++ } ++ ++ if (!(info->probed)) { ++ LOG_ERROR("Flash bank not probed"); ++ return ERROR_FLASH_BANK_NOT_PROBED; ++ } ++ ++ for (unsigned int sector = first; sector <= last; sector++) { ++ if (bank->sectors[sector].is_protected) { ++ LOG_ERROR("Flash sector %u protected", sector); ++ return ERROR_FAIL; ++ } ++ } ++ ++ if (first == 0 && last == (bank->num_sectors - 1) ++ && info->dev->chip_erase_cmd != info->dev->erase_cmd) { ++ LOG_DEBUG("Trying bulk erase."); ++ retval = jtagspi_bulk_erase(bank); ++ if (retval == ERROR_OK) ++ return retval; ++ else ++ LOG_WARNING("Bulk flash erase failed. Falling back to sector erase."); ++ } ++ ++ if (info->dev->erase_cmd == 0x00) ++ return ERROR_FLASH_OPER_UNSUPPORTED; ++ ++ for (unsigned int sector = first; sector <= last; sector++) { ++ retval = jtagspi_sector_erase(bank, sector); ++ if (retval != ERROR_OK) { ++ LOG_ERROR("Sector erase failed."); ++ break; ++ } ++ } ++ ++ return retval; ++} ++ ++static int jtagspi_protect(struct flash_bank *bank, int set, unsigned int first, ++ unsigned int last) ++{ ++ for (unsigned int sector = first; sector <= last; sector++) ++ bank->sectors[sector].is_protected = set; ++ return ERROR_OK; ++} ++ ++static int jtagspi_read(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count) ++{ ++ struct jtagspi_flash_bank *info = bank->driver_priv; ++ ++ if (!(info->probed)) { ++ LOG_ERROR("Flash bank not yet probed."); ++ return ERROR_FLASH_BANK_NOT_PROBED; ++ } ++ ++ jtagspi_cmd(bank, SPIFLASH_READ, &offset, buffer, -count*8); ++ return ERROR_OK; ++} ++ ++static int jtagspi_page_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count) ++{ ++ int retval; ++ ++ retval = jtagspi_write_enable(bank); ++ if (retval != ERROR_OK) ++ return retval; ++ jtagspi_cmd(bank, SPIFLASH_PAGE_PROGRAM, &offset, (uint8_t *) buffer, count*8); ++ return jtagspi_wait(bank, JTAGSPI_MAX_TIMEOUT); ++} ++ ++static int jtagspi_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count) ++{ ++ struct jtagspi_flash_bank *info = bank->driver_priv; ++ int retval; ++ uint32_t n, pagesize; ++ ++ if (!(info->probed)) { ++ LOG_ERROR("Flash bank not yet probed."); ++ return ERROR_FLASH_BANK_NOT_PROBED; ++ } ++ ++ /* if no write pagesize, use reasonable default */ ++ pagesize = info->dev->pagesize ? info->dev->pagesize : SPIFLASH_DEF_PAGESIZE; ++ ++ for (n = 0; n < count; n += pagesize) { ++ retval = jtagspi_page_write(bank, buffer + n, offset + n, ++ MIN(count - n, pagesize)); ++ if (retval != ERROR_OK) { ++ LOG_ERROR("page write error"); ++ return retval; ++ } ++ LOG_DEBUG("wrote page at 0x%08" PRIx32, offset + n); ++ } ++ return ERROR_OK; ++} ++ ++static int jtagspi_info(struct flash_bank *bank, char *buf, int buf_size) ++{ ++ struct jtagspi_flash_bank *info = bank->driver_priv; ++ ++ if (!(info->probed)) { ++ snprintf(buf, buf_size, "\nJTAGSPI flash bank not probed yet\n"); ++ return ERROR_OK; ++ } ++ ++ snprintf(buf, buf_size, "\nSPIFI flash information:\n" ++ " Device \'%s\' (ID 0x%08" PRIx32 ")\n", ++ info->dev->name, info->dev->device_id); ++ ++ return ERROR_OK; ++} ++ ++const struct flash_driver jtagspi_flash = { ++ .name = "jtagspi", ++ .flash_bank_command = jtagspi_flash_bank_command, ++ .erase = jtagspi_erase, ++ .protect = jtagspi_protect, ++ .write = jtagspi_write, ++ .read = jtagspi_read, ++ .probe = jtagspi_probe, ++ .auto_probe = jtagspi_probe, ++ .erase_check = default_flash_blank_check, ++ .info = jtagspi_info, ++ .free_driver_priv = default_flash_free_driver_priv, ++}; +diff '--color=auto' -Naur openocd-0.11.0/src/flash/nor/spi.h openocd-0.11.0.new/src/flash/nor/spi.h +--- openocd-0.11.0/src/flash/nor/spi.h 2020-12-10 03:43:09.000000000 +0800 ++++ openocd-0.11.0.new/src/flash/nor/spi.h 2021-07-27 18:32:08.106935700 +0800 +@@ -87,6 +87,9 @@ + #define SPIFLASH_PAGE_PROGRAM 0x02 /* Page Program */ + #define SPIFLASH_FAST_READ 0x0B /* Fast Read */ + #define SPIFLASH_READ 0x03 /* Normal Read */ ++#define SPIFLASH_4BYTE_READ 0x13 /* Read with 4 byte address */ ++#define SPIFLASH_4BYTE_SECTOR_ERASE 0xDC /* Sector Erase with 4 byte address */ ++#define SPIFLASH_4BYTE_PAGE_PROGRAM 0x12 /* Page Program with 4 byte address */ + #define SPIFLASH_MASS_ERASE 0xC7 /* Mass Erase */ + #define SPIFLASH_READ_SFDP 0x5A /* Read Serial Flash Discoverable Parameters */ + diff --git a/artiq-fast/pkgs/openocd.nix b/artiq-fast/pkgs/openocd.nix index c9b2b11..04fac77 100644 --- a/artiq-fast/pkgs/openocd.nix +++ b/artiq-fast/pkgs/openocd.nix @@ -15,7 +15,7 @@ let cp $src/*.bit $out/share/bscan-spi-bitstreams ''; }; - # https://docs.lambdaconcept.com/screamer/troubleshooting.html#error-contents-differ + # based on https://github.com/m-labs/openocd/commit/17395320816c0bcc4f3401633197a851aeda20ce openocd-fixed = openocd.overrideAttrs(oa: { patches = oa.patches or [] ++ [ ./openocd-jtagspi.nix ]; }); From 1319a2f536014a869b24648506bb26dfca75dbca Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Tue, 27 Jul 2021 18:34:56 +0800 Subject: [PATCH 7/9] openocd: clean up SPI flash patch (#62) --- artiq-fast/pkgs/openocd-jtagspi.nix | 456 ---------------------------- 1 file changed, 456 deletions(-) diff --git a/artiq-fast/pkgs/openocd-jtagspi.nix b/artiq-fast/pkgs/openocd-jtagspi.nix index af184c9..ef1ba62 100644 --- a/artiq-fast/pkgs/openocd-jtagspi.nix +++ b/artiq-fast/pkgs/openocd-jtagspi.nix @@ -100,462 +100,6 @@ diff '--color=auto' -Naur openocd-0.11.0/src/flash/nor/jtagspi.c openocd-0.11.0. return jtagspi_wait(bank, JTAGSPI_MAX_TIMEOUT); } -diff '--color=auto' -Naur openocd-0.11.0/src/flash/nor/jtagspi.c.orig openocd-0.11.0.new/src/flash/nor/jtagspi.c.orig ---- openocd-0.11.0/src/flash/nor/jtagspi.c.orig 1970-01-01 08:00:00.000000000 +0800 -+++ openocd-0.11.0.new/src/flash/nor/jtagspi.c.orig 2020-12-10 03:43:09.000000000 +0800 -@@ -0,0 +1,452 @@ -+/*************************************************************************** -+ * Copyright (C) 2015 Robert Jordens * -+ * * -+ * This program is free software; you can redistribute it and/or modify * -+ * it under the terms of the GNU General Public License as published by * -+ * the Free Software Foundation; either version 2 of the License, or * -+ * (at your option) any later version. * -+ * * -+ * 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. * -+ * * -+ * You should have received a copy of the GNU General Public License * -+ * along with this program. If not, see . * -+ ***************************************************************************/ -+ -+#ifdef HAVE_CONFIG_H -+#include "config.h" -+#endif -+ -+#include "imp.h" -+#include -+#include -+#include -+ -+#define JTAGSPI_MAX_TIMEOUT 3000 -+ -+ -+struct jtagspi_flash_bank { -+ struct jtag_tap *tap; -+ const struct flash_device *dev; -+ bool probed; -+ uint32_t ir; -+}; -+ -+FLASH_BANK_COMMAND_HANDLER(jtagspi_flash_bank_command) -+{ -+ struct jtagspi_flash_bank *info; -+ -+ if (CMD_ARGC < 7) -+ return ERROR_COMMAND_SYNTAX_ERROR; -+ -+ info = malloc(sizeof(struct jtagspi_flash_bank)); -+ if (info == NULL) { -+ LOG_ERROR("no memory for flash bank info"); -+ return ERROR_FAIL; -+ } -+ bank->driver_priv = info; -+ -+ info->tap = NULL; -+ info->probed = false; -+ COMMAND_PARSE_NUMBER(u32, CMD_ARGV[6], info->ir); -+ -+ return ERROR_OK; -+} -+ -+static void jtagspi_set_ir(struct flash_bank *bank) -+{ -+ struct jtagspi_flash_bank *info = bank->driver_priv; -+ struct scan_field field; -+ uint8_t buf[4] = { 0 }; -+ -+ LOG_DEBUG("loading jtagspi ir"); -+ buf_set_u32(buf, 0, info->tap->ir_length, info->ir); -+ field.num_bits = info->tap->ir_length; -+ field.out_value = buf; -+ field.in_value = NULL; -+ jtag_add_ir_scan(info->tap, &field, TAP_IDLE); -+} -+ -+static void flip_u8(uint8_t *in, uint8_t *out, int len) -+{ -+ for (int i = 0; i < len; i++) -+ out[i] = flip_u32(in[i], 8); -+} -+ -+static int jtagspi_cmd(struct flash_bank *bank, uint8_t cmd, -+ uint32_t *addr, uint8_t *data, int len) -+{ -+ struct jtagspi_flash_bank *info = bank->driver_priv; -+ struct scan_field fields[6]; -+ uint8_t marker = 1; -+ uint8_t xfer_bits_buf[4]; -+ uint8_t addr_buf[3]; -+ uint8_t *data_buf; -+ uint32_t xfer_bits; -+ int is_read, lenb, n; -+ -+ /* LOG_DEBUG("cmd=0x%02x len=%i", cmd, len); */ -+ -+ is_read = (len < 0); -+ if (is_read) -+ len = -len; -+ -+ n = 0; -+ -+ fields[n].num_bits = 1; -+ fields[n].out_value = ▮ -+ fields[n].in_value = NULL; -+ n++; -+ -+ xfer_bits = 8 + len - 1; -+ /* cmd + read/write - 1 due to the counter implementation */ -+ if (addr) -+ xfer_bits += 24; -+ h_u32_to_be(xfer_bits_buf, xfer_bits); -+ flip_u8(xfer_bits_buf, xfer_bits_buf, 4); -+ fields[n].num_bits = 32; -+ fields[n].out_value = xfer_bits_buf; -+ fields[n].in_value = NULL; -+ n++; -+ -+ cmd = flip_u32(cmd, 8); -+ fields[n].num_bits = 8; -+ fields[n].out_value = &cmd; -+ fields[n].in_value = NULL; -+ n++; -+ -+ if (addr) { -+ h_u24_to_be(addr_buf, *addr); -+ flip_u8(addr_buf, addr_buf, 3); -+ fields[n].num_bits = 24; -+ fields[n].out_value = addr_buf; -+ fields[n].in_value = NULL; -+ n++; -+ } -+ -+ lenb = DIV_ROUND_UP(len, 8); -+ data_buf = malloc(lenb); -+ if (lenb > 0) { -+ if (data_buf == NULL) { -+ LOG_ERROR("no memory for spi buffer"); -+ return ERROR_FAIL; -+ } -+ if (is_read) { -+ fields[n].num_bits = jtag_tap_count_enabled(); -+ fields[n].out_value = NULL; -+ fields[n].in_value = NULL; -+ n++; -+ -+ fields[n].out_value = NULL; -+ fields[n].in_value = data_buf; -+ } else { -+ flip_u8(data, data_buf, lenb); -+ fields[n].out_value = data_buf; -+ fields[n].in_value = NULL; -+ } -+ fields[n].num_bits = len; -+ n++; -+ } -+ -+ jtagspi_set_ir(bank); -+ /* passing from an IR scan to SHIFT-DR clears BYPASS registers */ -+ jtag_add_dr_scan(info->tap, n, fields, TAP_IDLE); -+ int retval = jtag_execute_queue(); -+ -+ if (is_read) -+ flip_u8(data_buf, data, lenb); -+ free(data_buf); -+ return retval; -+} -+ -+static int jtagspi_probe(struct flash_bank *bank) -+{ -+ struct jtagspi_flash_bank *info = bank->driver_priv; -+ struct flash_sector *sectors; -+ uint8_t in_buf[3]; -+ uint32_t id, sectorsize; -+ -+ if (info->probed) -+ free(bank->sectors); -+ info->probed = false; -+ -+ if (bank->target->tap == NULL) { -+ LOG_ERROR("Target has no JTAG tap"); -+ return ERROR_FAIL; -+ } -+ info->tap = bank->target->tap; -+ -+ jtagspi_cmd(bank, SPIFLASH_READ_ID, NULL, in_buf, -24); -+ /* the table in spi.c has the manufacturer byte (first) as the lsb */ -+ id = le_to_h_u24(in_buf); -+ -+ info->dev = NULL; -+ for (const struct flash_device *p = flash_devices; p->name ; p++) -+ if (p->device_id == id) { -+ info->dev = p; -+ break; -+ } -+ -+ if (!(info->dev)) { -+ LOG_ERROR("Unknown flash device (ID 0x%08" PRIx32 ")", id); -+ return ERROR_FAIL; -+ } -+ -+ LOG_INFO("Found flash device \'%s\' (ID 0x%08" PRIx32 ")", -+ info->dev->name, info->dev->device_id); -+ -+ /* Set correct size value */ -+ bank->size = info->dev->size_in_bytes; -+ if (bank->size <= (1UL << 16)) -+ LOG_WARNING("device needs 2-byte addresses - not implemented"); -+ if (bank->size > (1UL << 24)) -+ LOG_WARNING("device needs paging or 4-byte addresses - not implemented"); -+ -+ /* if no sectors, treat whole bank as single sector */ -+ sectorsize = info->dev->sectorsize ? -+ info->dev->sectorsize : info->dev->size_in_bytes; -+ -+ /* create and fill sectors array */ -+ bank->num_sectors = info->dev->size_in_bytes / sectorsize; -+ sectors = malloc(sizeof(struct flash_sector) * bank->num_sectors); -+ if (sectors == NULL) { -+ LOG_ERROR("not enough memory"); -+ return ERROR_FAIL; -+ } -+ -+ for (unsigned int sector = 0; sector < bank->num_sectors; sector++) { -+ sectors[sector].offset = sector * sectorsize; -+ sectors[sector].size = sectorsize; -+ sectors[sector].is_erased = -1; -+ sectors[sector].is_protected = 0; -+ } -+ -+ bank->sectors = sectors; -+ info->probed = true; -+ return ERROR_OK; -+} -+ -+static int jtagspi_read_status(struct flash_bank *bank, uint32_t *status) -+{ -+ uint8_t buf; -+ int err = jtagspi_cmd(bank, SPIFLASH_READ_STATUS, NULL, &buf, -8); -+ if (err == ERROR_OK) { -+ *status = buf; -+ /* LOG_DEBUG("status=0x%08" PRIx32, *status); */ -+ } -+ -+ return err; -+} -+ -+static int jtagspi_wait(struct flash_bank *bank, int timeout_ms) -+{ -+ uint32_t status; -+ int64_t t0 = timeval_ms(); -+ int64_t dt; -+ -+ do { -+ dt = timeval_ms() - t0; -+ -+ int retval = jtagspi_read_status(bank, &status); -+ if (retval != ERROR_OK) -+ return retval; -+ -+ if ((status & SPIFLASH_BSY_BIT) == 0) { -+ LOG_DEBUG("waited %" PRId64 " ms", dt); -+ return ERROR_OK; -+ } -+ alive_sleep(1); -+ } while (dt <= timeout_ms); -+ -+ LOG_ERROR("timeout, device still busy"); -+ return ERROR_FAIL; -+} -+ -+static int jtagspi_write_enable(struct flash_bank *bank) -+{ -+ uint32_t status; -+ -+ jtagspi_cmd(bank, SPIFLASH_WRITE_ENABLE, NULL, NULL, 0); -+ -+ int retval = jtagspi_read_status(bank, &status); -+ if (retval != ERROR_OK) -+ return retval; -+ -+ if ((status & SPIFLASH_WE_BIT) == 0) { -+ LOG_ERROR("Cannot enable write to flash. Status=0x%08" PRIx32, status); -+ return ERROR_FAIL; -+ } -+ return ERROR_OK; -+} -+ -+static int jtagspi_bulk_erase(struct flash_bank *bank) -+{ -+ struct jtagspi_flash_bank *info = bank->driver_priv; -+ int retval; -+ int64_t t0 = timeval_ms(); -+ -+ if (info->dev->chip_erase_cmd == 0x00) -+ return ERROR_FLASH_OPER_UNSUPPORTED; -+ -+ retval = jtagspi_write_enable(bank); -+ if (retval != ERROR_OK) -+ return retval; -+ jtagspi_cmd(bank, info->dev->chip_erase_cmd, NULL, NULL, 0); -+ retval = jtagspi_wait(bank, bank->num_sectors*JTAGSPI_MAX_TIMEOUT); -+ LOG_INFO("took %" PRId64 " ms", timeval_ms() - t0); -+ return retval; -+} -+ -+static int jtagspi_sector_erase(struct flash_bank *bank, unsigned int sector) -+{ -+ struct jtagspi_flash_bank *info = bank->driver_priv; -+ int retval; -+ int64_t t0 = timeval_ms(); -+ -+ retval = jtagspi_write_enable(bank); -+ if (retval != ERROR_OK) -+ return retval; -+ jtagspi_cmd(bank, info->dev->erase_cmd, &bank->sectors[sector].offset, NULL, 0); -+ retval = jtagspi_wait(bank, JTAGSPI_MAX_TIMEOUT); -+ LOG_INFO("sector %u took %" PRId64 " ms", sector, timeval_ms() - t0); -+ return retval; -+} -+ -+static int jtagspi_erase(struct flash_bank *bank, unsigned int first, -+ unsigned int last) -+{ -+ struct jtagspi_flash_bank *info = bank->driver_priv; -+ int retval = ERROR_OK; -+ -+ LOG_DEBUG("erase from sector %u to sector %u", first, last); -+ -+ if ((last < first) || (last >= bank->num_sectors)) { -+ LOG_ERROR("Flash sector invalid"); -+ return ERROR_FLASH_SECTOR_INVALID; -+ } -+ -+ if (!(info->probed)) { -+ LOG_ERROR("Flash bank not probed"); -+ return ERROR_FLASH_BANK_NOT_PROBED; -+ } -+ -+ for (unsigned int sector = first; sector <= last; sector++) { -+ if (bank->sectors[sector].is_protected) { -+ LOG_ERROR("Flash sector %u protected", sector); -+ return ERROR_FAIL; -+ } -+ } -+ -+ if (first == 0 && last == (bank->num_sectors - 1) -+ && info->dev->chip_erase_cmd != info->dev->erase_cmd) { -+ LOG_DEBUG("Trying bulk erase."); -+ retval = jtagspi_bulk_erase(bank); -+ if (retval == ERROR_OK) -+ return retval; -+ else -+ LOG_WARNING("Bulk flash erase failed. Falling back to sector erase."); -+ } -+ -+ if (info->dev->erase_cmd == 0x00) -+ return ERROR_FLASH_OPER_UNSUPPORTED; -+ -+ for (unsigned int sector = first; sector <= last; sector++) { -+ retval = jtagspi_sector_erase(bank, sector); -+ if (retval != ERROR_OK) { -+ LOG_ERROR("Sector erase failed."); -+ break; -+ } -+ } -+ -+ return retval; -+} -+ -+static int jtagspi_protect(struct flash_bank *bank, int set, unsigned int first, -+ unsigned int last) -+{ -+ for (unsigned int sector = first; sector <= last; sector++) -+ bank->sectors[sector].is_protected = set; -+ return ERROR_OK; -+} -+ -+static int jtagspi_read(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count) -+{ -+ struct jtagspi_flash_bank *info = bank->driver_priv; -+ -+ if (!(info->probed)) { -+ LOG_ERROR("Flash bank not yet probed."); -+ return ERROR_FLASH_BANK_NOT_PROBED; -+ } -+ -+ jtagspi_cmd(bank, SPIFLASH_READ, &offset, buffer, -count*8); -+ return ERROR_OK; -+} -+ -+static int jtagspi_page_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count) -+{ -+ int retval; -+ -+ retval = jtagspi_write_enable(bank); -+ if (retval != ERROR_OK) -+ return retval; -+ jtagspi_cmd(bank, SPIFLASH_PAGE_PROGRAM, &offset, (uint8_t *) buffer, count*8); -+ return jtagspi_wait(bank, JTAGSPI_MAX_TIMEOUT); -+} -+ -+static int jtagspi_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count) -+{ -+ struct jtagspi_flash_bank *info = bank->driver_priv; -+ int retval; -+ uint32_t n, pagesize; -+ -+ if (!(info->probed)) { -+ LOG_ERROR("Flash bank not yet probed."); -+ return ERROR_FLASH_BANK_NOT_PROBED; -+ } -+ -+ /* if no write pagesize, use reasonable default */ -+ pagesize = info->dev->pagesize ? info->dev->pagesize : SPIFLASH_DEF_PAGESIZE; -+ -+ for (n = 0; n < count; n += pagesize) { -+ retval = jtagspi_page_write(bank, buffer + n, offset + n, -+ MIN(count - n, pagesize)); -+ if (retval != ERROR_OK) { -+ LOG_ERROR("page write error"); -+ return retval; -+ } -+ LOG_DEBUG("wrote page at 0x%08" PRIx32, offset + n); -+ } -+ return ERROR_OK; -+} -+ -+static int jtagspi_info(struct flash_bank *bank, char *buf, int buf_size) -+{ -+ struct jtagspi_flash_bank *info = bank->driver_priv; -+ -+ if (!(info->probed)) { -+ snprintf(buf, buf_size, "\nJTAGSPI flash bank not probed yet\n"); -+ return ERROR_OK; -+ } -+ -+ snprintf(buf, buf_size, "\nSPIFI flash information:\n" -+ " Device \'%s\' (ID 0x%08" PRIx32 ")\n", -+ info->dev->name, info->dev->device_id); -+ -+ return ERROR_OK; -+} -+ -+const struct flash_driver jtagspi_flash = { -+ .name = "jtagspi", -+ .flash_bank_command = jtagspi_flash_bank_command, -+ .erase = jtagspi_erase, -+ .protect = jtagspi_protect, -+ .write = jtagspi_write, -+ .read = jtagspi_read, -+ .probe = jtagspi_probe, -+ .auto_probe = jtagspi_probe, -+ .erase_check = default_flash_blank_check, -+ .info = jtagspi_info, -+ .free_driver_priv = default_flash_free_driver_priv, -+}; diff '--color=auto' -Naur openocd-0.11.0/src/flash/nor/spi.h openocd-0.11.0.new/src/flash/nor/spi.h --- openocd-0.11.0/src/flash/nor/spi.h 2020-12-10 03:43:09.000000000 +0800 +++ openocd-0.11.0.new/src/flash/nor/spi.h 2021-07-27 18:32:08.106935700 +0800 From 8d8ea8b3773fe5b6563382460c1b94c0c81a88e7 Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Thu, 29 Jul 2021 14:49:12 +0800 Subject: [PATCH 8/9] bump misoc-new --- artiq-fast/pkgs/python-deps.nix | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/artiq-fast/pkgs/python-deps.nix b/artiq-fast/pkgs/python-deps.nix index da90002..78e32cd 100644 --- a/artiq-fast/pkgs/python-deps.nix +++ b/artiq-fast/pkgs/python-deps.nix @@ -86,14 +86,14 @@ rec { misoc = python3Packages.buildPythonPackage { pname = "misoc"; - version = if misoc-new then "unstable-2021-07-22" else "unstable-2021-02-15"; + version = if misoc-new then "unstable-2021-07-29" else "unstable-2021-02-15"; src = if misoc-new then (fetchFromGitHub { owner = "m-labs"; repo = "misoc"; - rev = "79a27a82050266ce4d99d35ca0fe6b53a44649ac"; - sha256 = "10pq3ll3h7mrfgk552nlr80v26gr4mvmpb5pd2h5ck2s9gpkkb82"; + rev = "f048d1fbfd48c93542d2f1e591139f95c7c9cde6"; + sha256 = "1ny0d0q6mnjsxq746q4chn2grfac8brs4w60k9fxk2h5abvfgij8"; fetchSubmodules = true; }) else (fetchFromGitHub { From bb1585fbbd307d0d709bedb2180cbbd7cbd9e46b Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Thu, 29 Jul 2021 15:30:09 +0800 Subject: [PATCH 9/9] openocd: revert to lambdaconcept patch --- artiq-fast/pkgs/openocd-jtagspi.diff | 24 ++++++ artiq-fast/pkgs/openocd-jtagspi.nix | 115 --------------------------- artiq-fast/pkgs/openocd.nix | 4 +- 3 files changed, 26 insertions(+), 117 deletions(-) create mode 100644 artiq-fast/pkgs/openocd-jtagspi.diff delete mode 100644 artiq-fast/pkgs/openocd-jtagspi.nix diff --git a/artiq-fast/pkgs/openocd-jtagspi.diff b/artiq-fast/pkgs/openocd-jtagspi.diff new file mode 100644 index 0000000..fa41f94 --- /dev/null +++ b/artiq-fast/pkgs/openocd-jtagspi.diff @@ -0,0 +1,24 @@ +diff --git a/src/flash/nor/spi.c b/src/flash/nor/spi.c +index af72ffc4..e5c9a9bd 100644 +--- a/src/flash/nor/spi.c ++++ b/src/flash/nor/spi.c +@@ -90,8 +90,8 @@ const struct flash_device flash_devices[] = { + FLASH_ID("mac 25r6435f", 0x03, 0x00, 0x02, 0xd8, 0xc7, 0x001728c2, 0x100, 0x10000, 0x800000), + FLASH_ID("micron n25q064", 0x03, 0xeb, 0x02, 0xd8, 0xc7, 0x0017ba20, 0x100, 0x10000, 0x800000), + FLASH_ID("micron n25q128", 0x03, 0xeb, 0x02, 0xd8, 0xc7, 0x0018ba20, 0x100, 0x10000, 0x1000000), +- FLASH_ID("micron n25q256 3v", 0x13, 0xec, 0x12, 0xdc, 0xc7, 0x0019ba20, 0x100, 0x10000, 0x2000000), +- FLASH_ID("micron n25q256 1.8v", 0x13, 0xec, 0x12, 0xdc, 0xc7, 0x0019bb20, 0x100, 0x10000, 0x2000000), ++ FLASH_ID("micron n25q256 3v", 0x03, 0xeb, 0x02, 0xd8, 0xc7, 0x0019ba20, 0x100, 0x10000, 0x2000000), ++ FLASH_ID("micron n25q256 1.8v", 0x03, 0xeb, 0x02, 0xd8, 0xc7, 0x0019bb20, 0x100, 0x10000, 0x2000000), + FLASH_ID("micron mt25ql512", 0x13, 0xec, 0x12, 0xdc, 0xc7, 0x0020ba20, 0x100, 0x10000, 0x4000000), + FLASH_ID("micron mt25ql01", 0x13, 0xec, 0x12, 0xdc, 0xc7, 0x0021ba20, 0x100, 0x10000, 0x8000000), + FLASH_ID("micron mt25ql02", 0x13, 0xec, 0x12, 0xdc, 0xc7, 0x0022ba20, 0x100, 0x10000, 0x10000000), +@@ -124,7 +124,7 @@ const struct flash_device flash_devices[] = { + FLASH_ID("issi is25lp064", 0x03, 0x00, 0x02, 0xd8, 0xc7, 0x0017609d, 0x100, 0x10000, 0x800000), + FLASH_ID("issi is25lp128d", 0x03, 0xeb, 0x02, 0xd8, 0xc7, 0x0018609d, 0x100, 0x10000, 0x1000000), + FLASH_ID("issi is25wp128d", 0x03, 0xeb, 0x02, 0xd8, 0xc7, 0x0018709d, 0x100, 0x10000, 0x1000000), +- FLASH_ID("issi is25lp256d", 0x13, 0xec, 0x12, 0xdc, 0xc7, 0x0019609d, 0x100, 0x10000, 0x2000000), ++ FLASH_ID("issi is25lp256d", 0x03, 0xeb, 0x02, 0xd8, 0xc7, 0x0019609d, 0x100, 0x10000, 0x2000000), + FLASH_ID("issi is25wp256d", 0x13, 0xec, 0x12, 0xdc, 0xc7, 0x0019709d, 0x100, 0x10000, 0x2000000), + FLASH_ID("issi is25lp512m", 0x13, 0xec, 0x12, 0xdc, 0xc7, 0x001a609d, 0x100, 0x10000, 0x4000000), + FLASH_ID("issi is25wp512m", 0x13, 0xec, 0x12, 0xdc, 0xc7, 0x001a709d, 0x100, 0x10000, 0x4000000), diff --git a/artiq-fast/pkgs/openocd-jtagspi.nix b/artiq-fast/pkgs/openocd-jtagspi.nix deleted file mode 100644 index ef1ba62..0000000 --- a/artiq-fast/pkgs/openocd-jtagspi.nix +++ /dev/null @@ -1,115 +0,0 @@ -diff '--color=auto' -Naur openocd-0.11.0/src/flash/nor/jtagspi.c openocd-0.11.0.new/src/flash/nor/jtagspi.c ---- openocd-0.11.0/src/flash/nor/jtagspi.c 2020-12-10 03:43:09.000000000 +0800 -+++ openocd-0.11.0.new/src/flash/nor/jtagspi.c 2021-07-27 18:31:32.179205804 +0800 -@@ -82,7 +82,7 @@ - struct scan_field fields[6]; - uint8_t marker = 1; - uint8_t xfer_bits_buf[4]; -- uint8_t addr_buf[3]; -+ uint8_t addr_buf[4]; - uint8_t *data_buf; - uint32_t xfer_bits; - int is_read, lenb, n; -@@ -102,9 +102,12 @@ - - xfer_bits = 8 + len - 1; - /* cmd + read/write - 1 due to the counter implementation */ -- if (addr) -- xfer_bits += 24; -- h_u32_to_be(xfer_bits_buf, xfer_bits); -+ if (addr) { -+ if (bank->size > (1 << 24)) -+ xfer_bits += 32; -+ else -+ xfer_bits += 24; -+ } - flip_u8(xfer_bits_buf, xfer_bits_buf, 4); - fields[n].num_bits = 32; - fields[n].out_value = xfer_bits_buf; -@@ -118,9 +121,15 @@ - n++; - - if (addr) { -- h_u24_to_be(addr_buf, *addr); -- flip_u8(addr_buf, addr_buf, 3); -- fields[n].num_bits = 24; -+ if (bank->size > (1 << 24)) { -+ h_u32_to_be(addr_buf, *addr); -+ flip_u8(addr_buf, addr_buf, 4); -+ fields[n].num_bits = 32; -+ } else { -+ h_u24_to_be(addr_buf, *addr); -+ flip_u8(addr_buf, addr_buf, 3); -+ fields[n].num_bits = 24; -+ } - fields[n].out_value = addr_buf; - fields[n].in_value = NULL; - n++; -@@ -304,11 +313,14 @@ - struct jtagspi_flash_bank *info = bank->driver_priv; - int retval; - int64_t t0 = timeval_ms(); -+ uint8_t erase_cmd = info->dev->erase_cmd; - - retval = jtagspi_write_enable(bank); - if (retval != ERROR_OK) - return retval; -- jtagspi_cmd(bank, info->dev->erase_cmd, &bank->sectors[sector].offset, NULL, 0); -+ if (bank->size > (1 << 24)) -+ erase_cmd = SPIFLASH_4BYTE_SECTOR_ERASE; -+ jtagspi_cmd(bank, erase_cmd, &bank->sectors[sector].offset, NULL, 0); - retval = jtagspi_wait(bank, JTAGSPI_MAX_TIMEOUT); - LOG_INFO("sector %u took %" PRId64 " ms", sector, timeval_ms() - t0); - return retval; -@@ -374,24 +386,36 @@ - static int jtagspi_read(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count) - { - struct jtagspi_flash_bank *info = bank->driver_priv; -+ uint8_t read_cmd = SPIFLASH_READ; - - if (!(info->probed)) { - LOG_ERROR("Flash bank not yet probed."); - return ERROR_FLASH_BANK_NOT_PROBED; - } -+ if (count >= (1 << 28)) { -+ LOG_ERROR("Read too large."); -+ return ERROR_FAIL; -+ } - -- jtagspi_cmd(bank, SPIFLASH_READ, &offset, buffer, -count*8); -+ if (bank->size > (1 << 24)) -+ read_cmd = SPIFLASH_4BYTE_READ; -+ jtagspi_cmd(bank, read_cmd, &offset, buffer, -count*8); - return ERROR_OK; - } - - static int jtagspi_page_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count) - { - int retval; -+ uint8_t program_cmd = SPIFLASH_PAGE_PROGRAM; - - retval = jtagspi_write_enable(bank); - if (retval != ERROR_OK) - return retval; -- jtagspi_cmd(bank, SPIFLASH_PAGE_PROGRAM, &offset, (uint8_t *) buffer, count*8); -+ -+ if (bank->size > (1 << 24)) -+ program_cmd = SPIFLASH_4BYTE_PAGE_PROGRAM; -+ -+ jtagspi_cmd(bank, program_cmd, &offset, (uint8_t *) buffer, count*8); - return jtagspi_wait(bank, JTAGSPI_MAX_TIMEOUT); - } - -diff '--color=auto' -Naur openocd-0.11.0/src/flash/nor/spi.h openocd-0.11.0.new/src/flash/nor/spi.h ---- openocd-0.11.0/src/flash/nor/spi.h 2020-12-10 03:43:09.000000000 +0800 -+++ openocd-0.11.0.new/src/flash/nor/spi.h 2021-07-27 18:32:08.106935700 +0800 -@@ -87,6 +87,9 @@ - #define SPIFLASH_PAGE_PROGRAM 0x02 /* Page Program */ - #define SPIFLASH_FAST_READ 0x0B /* Fast Read */ - #define SPIFLASH_READ 0x03 /* Normal Read */ -+#define SPIFLASH_4BYTE_READ 0x13 /* Read with 4 byte address */ -+#define SPIFLASH_4BYTE_SECTOR_ERASE 0xDC /* Sector Erase with 4 byte address */ -+#define SPIFLASH_4BYTE_PAGE_PROGRAM 0x12 /* Page Program with 4 byte address */ - #define SPIFLASH_MASS_ERASE 0xC7 /* Mass Erase */ - #define SPIFLASH_READ_SFDP 0x5A /* Read Serial Flash Discoverable Parameters */ - diff --git a/artiq-fast/pkgs/openocd.nix b/artiq-fast/pkgs/openocd.nix index 04fac77..3f3070c 100644 --- a/artiq-fast/pkgs/openocd.nix +++ b/artiq-fast/pkgs/openocd.nix @@ -15,9 +15,9 @@ let cp $src/*.bit $out/share/bscan-spi-bitstreams ''; }; - # based on https://github.com/m-labs/openocd/commit/17395320816c0bcc4f3401633197a851aeda20ce + # https://docs.lambdaconcept.com/screamer/troubleshooting.html#error-contents-differ openocd-fixed = openocd.overrideAttrs(oa: { - patches = oa.patches or [] ++ [ ./openocd-jtagspi.nix ]; + patches = oa.patches or [] ++ [ ./openocd-jtagspi.diff ]; }); in buildEnv {