Repostiory containing DAPLink source code with Reset Pin workaround for HANI_IOT board.

Upstream: https://github.com/ARMmbed/DAPLink

Committer:
Pawel Zarembski
Date:
Tue Apr 07 12:55:42 2020 +0200
Revision:
0:01f31e923fe2
hani: DAPLink with reset workaround

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Pawel Zarembski 0:01f31e923fe2 1 /**
Pawel Zarembski 0:01f31e923fe2 2 * @file target_reset_Lseries.c
Pawel Zarembski 0:01f31e923fe2 3 * @brief Target reset for the Kinetis L series
Pawel Zarembski 0:01f31e923fe2 4 *
Pawel Zarembski 0:01f31e923fe2 5 * DAPLink Interface Firmware
Pawel Zarembski 0:01f31e923fe2 6 * Copyright (c) 2009-2019, ARM Limited, All Rights Reserved
Pawel Zarembski 0:01f31e923fe2 7 * SPDX-License-Identifier: Apache-2.0
Pawel Zarembski 0:01f31e923fe2 8 *
Pawel Zarembski 0:01f31e923fe2 9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
Pawel Zarembski 0:01f31e923fe2 10 * not use this file except in compliance with the License.
Pawel Zarembski 0:01f31e923fe2 11 * You may obtain a copy of the License at
Pawel Zarembski 0:01f31e923fe2 12 *
Pawel Zarembski 0:01f31e923fe2 13 * http://www.apache.org/licenses/LICENSE-2.0
Pawel Zarembski 0:01f31e923fe2 14 *
Pawel Zarembski 0:01f31e923fe2 15 * Unless required by applicable law or agreed to in writing, software
Pawel Zarembski 0:01f31e923fe2 16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
Pawel Zarembski 0:01f31e923fe2 17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Pawel Zarembski 0:01f31e923fe2 18 * See the License for the specific language governing permissions and
Pawel Zarembski 0:01f31e923fe2 19 * limitations under the License.
Pawel Zarembski 0:01f31e923fe2 20 */
Pawel Zarembski 0:01f31e923fe2 21
Pawel Zarembski 0:01f31e923fe2 22 #include "swd_host.h"
Pawel Zarembski 0:01f31e923fe2 23 #include "info.h"
Pawel Zarembski 0:01f31e923fe2 24 #include "target_family.h"
Pawel Zarembski 0:01f31e923fe2 25
Pawel Zarembski 0:01f31e923fe2 26 #define MDM_STATUS 0x01000000
Pawel Zarembski 0:01f31e923fe2 27 #define MDM_CTRL 0x01000004 //
Pawel Zarembski 0:01f31e923fe2 28 #define MDM_IDR 0x010000fc // read-only identification register
Pawel Zarembski 0:01f31e923fe2 29 #define MDM_ID 0x001c0020 // L series
Pawel Zarembski 0:01f31e923fe2 30
Pawel Zarembski 0:01f31e923fe2 31 static void target_before_init_debug(void)
Pawel Zarembski 0:01f31e923fe2 32 {
Pawel Zarembski 0:01f31e923fe2 33 swd_set_target_reset(1);
Pawel Zarembski 0:01f31e923fe2 34 }
Pawel Zarembski 0:01f31e923fe2 35
Pawel Zarembski 0:01f31e923fe2 36 static uint8_t target_unlock_sequence(void)
Pawel Zarembski 0:01f31e923fe2 37 {
Pawel Zarembski 0:01f31e923fe2 38 uint32_t val;
Pawel Zarembski 0:01f31e923fe2 39
Pawel Zarembski 0:01f31e923fe2 40 // read the device ID
Pawel Zarembski 0:01f31e923fe2 41 if (!swd_read_ap(MDM_IDR, &val)) {
Pawel Zarembski 0:01f31e923fe2 42 return 0;
Pawel Zarembski 0:01f31e923fe2 43 }
Pawel Zarembski 0:01f31e923fe2 44
Pawel Zarembski 0:01f31e923fe2 45 // verify the result
Pawel Zarembski 0:01f31e923fe2 46 if (val != MDM_ID) {
Pawel Zarembski 0:01f31e923fe2 47 return 0;
Pawel Zarembski 0:01f31e923fe2 48 }
Pawel Zarembski 0:01f31e923fe2 49
Pawel Zarembski 0:01f31e923fe2 50 if (!swd_read_ap(MDM_STATUS, &val)) {
Pawel Zarembski 0:01f31e923fe2 51 return 0;
Pawel Zarembski 0:01f31e923fe2 52 }
Pawel Zarembski 0:01f31e923fe2 53
Pawel Zarembski 0:01f31e923fe2 54 // flash in secured mode
Pawel Zarembski 0:01f31e923fe2 55 if (val & (1 << 2)) {
Pawel Zarembski 0:01f31e923fe2 56 // hold the device in reset
Pawel Zarembski 0:01f31e923fe2 57 swd_set_target_reset(1);
Pawel Zarembski 0:01f31e923fe2 58
Pawel Zarembski 0:01f31e923fe2 59 // write the mass-erase enable bit
Pawel Zarembski 0:01f31e923fe2 60 if (!swd_write_ap(MDM_CTRL, 1)) {
Pawel Zarembski 0:01f31e923fe2 61 return 0;
Pawel Zarembski 0:01f31e923fe2 62 }
Pawel Zarembski 0:01f31e923fe2 63
Pawel Zarembski 0:01f31e923fe2 64 while (1) {
Pawel Zarembski 0:01f31e923fe2 65 // wait until mass erase is started
Pawel Zarembski 0:01f31e923fe2 66 if (!swd_read_ap(MDM_STATUS, &val)) {
Pawel Zarembski 0:01f31e923fe2 67 return 0;
Pawel Zarembski 0:01f31e923fe2 68 }
Pawel Zarembski 0:01f31e923fe2 69
Pawel Zarembski 0:01f31e923fe2 70 if (val & 1) {
Pawel Zarembski 0:01f31e923fe2 71 break;
Pawel Zarembski 0:01f31e923fe2 72 }
Pawel Zarembski 0:01f31e923fe2 73 }
Pawel Zarembski 0:01f31e923fe2 74
Pawel Zarembski 0:01f31e923fe2 75 // mass erase in progress
Pawel Zarembski 0:01f31e923fe2 76 while (1) {
Pawel Zarembski 0:01f31e923fe2 77 // keep reading until procedure is complete
Pawel Zarembski 0:01f31e923fe2 78 if (!swd_read_ap(MDM_CTRL, &val)) {
Pawel Zarembski 0:01f31e923fe2 79 return 0;
Pawel Zarembski 0:01f31e923fe2 80 }
Pawel Zarembski 0:01f31e923fe2 81
Pawel Zarembski 0:01f31e923fe2 82 if (val == 0) {
Pawel Zarembski 0:01f31e923fe2 83 break;
Pawel Zarembski 0:01f31e923fe2 84 }
Pawel Zarembski 0:01f31e923fe2 85 }
Pawel Zarembski 0:01f31e923fe2 86 }
Pawel Zarembski 0:01f31e923fe2 87
Pawel Zarembski 0:01f31e923fe2 88 return 1;
Pawel Zarembski 0:01f31e923fe2 89 }
Pawel Zarembski 0:01f31e923fe2 90
Pawel Zarembski 0:01f31e923fe2 91 // Check Flash Configuration Field bytes at address 0x400-0x40f to ensure that flash security
Pawel Zarembski 0:01f31e923fe2 92 // won't be enabled.
Pawel Zarembski 0:01f31e923fe2 93 //
Pawel Zarembski 0:01f31e923fe2 94 // FCF bytes:
Pawel Zarembski 0:01f31e923fe2 95 // [0x0-0x7]=backdoor key
Pawel Zarembski 0:01f31e923fe2 96 // [0x8-0xb]=flash protection bytes
Pawel Zarembski 0:01f31e923fe2 97 // [0xc]=FSEC:
Pawel Zarembski 0:01f31e923fe2 98 // [7:6]=KEYEN (2'b10 is backdoor key enabled, all others backdoor key disabled)
Pawel Zarembski 0:01f31e923fe2 99 // [5:4]=MEEN (2'b10 mass erase disabled, all other mass erase enabled)
Pawel Zarembski 0:01f31e923fe2 100 // [3:2]=FSLACC (2'b00 and 2'b11 factory access enabled, 2'b01 and 2'b10 factory access disabled)
Pawel Zarembski 0:01f31e923fe2 101 // [1:0]=SEC (2'b10 flash security disabled, all other flash security enabled)
Pawel Zarembski 0:01f31e923fe2 102 // [0xd]=FOPT
Pawel Zarembski 0:01f31e923fe2 103 // [0xe]=EEPROM protection bytes (FlexNVM devices only)
Pawel Zarembski 0:01f31e923fe2 104 // [0xf]=data flash protection bytes (FlexNVM devices only)
Pawel Zarembski 0:01f31e923fe2 105 //
Pawel Zarembski 0:01f31e923fe2 106 // This function checks that:
Pawel Zarembski 0:01f31e923fe2 107 // - FSEC does not disable mass erase or secure the device.
Pawel Zarembski 0:01f31e923fe2 108 //
Pawel Zarembski 0:01f31e923fe2 109 uint8_t static security_bits_set(uint32_t addr, uint8_t *data, uint32_t size)
Pawel Zarembski 0:01f31e923fe2 110 {
Pawel Zarembski 0:01f31e923fe2 111 const uint32_t fsec_addr = 0x40C;
Pawel Zarembski 0:01f31e923fe2 112
Pawel Zarembski 0:01f31e923fe2 113 if ((addr <= fsec_addr) && (addr + size) > fsec_addr) {
Pawel Zarembski 0:01f31e923fe2 114 uint8_t fsec = data[fsec_addr - addr];
Pawel Zarembski 0:01f31e923fe2 115
Pawel Zarembski 0:01f31e923fe2 116 // make sure we can unsecure the device or dont program at all
Pawel Zarembski 0:01f31e923fe2 117 if ((fsec & 0x30) == 0x20) {
Pawel Zarembski 0:01f31e923fe2 118 // Dont allow programming mass-erase disabled state
Pawel Zarembski 0:01f31e923fe2 119 return 1;
Pawel Zarembski 0:01f31e923fe2 120 }
Pawel Zarembski 0:01f31e923fe2 121
Pawel Zarembski 0:01f31e923fe2 122 // Security is OK long as we can mass-erase (comment the following out to enable target security)
Pawel Zarembski 0:01f31e923fe2 123 if ((fsec & 0x03) != 0x02) {
Pawel Zarembski 0:01f31e923fe2 124 return 1;
Pawel Zarembski 0:01f31e923fe2 125 }
Pawel Zarembski 0:01f31e923fe2 126 }
Pawel Zarembski 0:01f31e923fe2 127
Pawel Zarembski 0:01f31e923fe2 128 return 0;
Pawel Zarembski 0:01f31e923fe2 129 }
Pawel Zarembski 0:01f31e923fe2 130
Pawel Zarembski 0:01f31e923fe2 131 const target_family_descriptor_t g_nxp_kinetis_lseries = {
Pawel Zarembski 0:01f31e923fe2 132 .family_id = kNXP_KinetisL_FamilyID,
Pawel Zarembski 0:01f31e923fe2 133 .default_reset_type = kHardwareReset,
Pawel Zarembski 0:01f31e923fe2 134 .target_before_init_debug = target_before_init_debug,
Pawel Zarembski 0:01f31e923fe2 135 .target_unlock_sequence = target_unlock_sequence,
Pawel Zarembski 0:01f31e923fe2 136 .security_bits_set = security_bits_set,
Pawel Zarembski 0:01f31e923fe2 137 };