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