Juan Castillo | facdd1c | 2015-08-12 12:53:02 +0100 | [diff] [blame] | 1 | /* |
Roberto Vargas | ab29dca | 2017-07-26 14:15:07 +0100 | [diff] [blame] | 2 | * Copyright (c) 2015-2017, ARM Limited and Contributors. All rights reserved. |
Juan Castillo | facdd1c | 2015-08-12 12:53:02 +0100 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Juan Castillo | facdd1c | 2015-08-12 12:53:02 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <errno.h> |
| 8 | #include <mmio.h> |
Antonio Nino Diaz | d7da2f8 | 2018-10-10 11:14:44 +0100 | [diff] [blame] | 9 | #include <v2m_flash.h> |
Juan Castillo | facdd1c | 2015-08-12 12:53:02 +0100 | [diff] [blame] | 10 | |
Antonio Nino Diaz | d7da2f8 | 2018-10-10 11:14:44 +0100 | [diff] [blame] | 11 | /* |
| 12 | * This file supplies a low level interface to the vexpress NOR flash |
| 13 | * memory of juno and fvp. This memory is organized as an interleaved |
| 14 | * memory of two chips with a 16 bit word. It means that every 32 bit |
| 15 | * access is going to access to two different chips. This is very |
| 16 | * important when we send commands or read status of the chips. |
| 17 | */ |
Juan Castillo | facdd1c | 2015-08-12 12:53:02 +0100 | [diff] [blame] | 18 | |
| 19 | /* |
| 20 | * DWS ready poll retries. The number of retries in this driver have been |
| 21 | * obtained empirically from Juno. FVP implements a zero wait state NOR flash |
| 22 | * model |
| 23 | */ |
| 24 | #define DWS_WORD_PROGRAM_RETRIES 1000 |
Roberto Vargas | ea21edc | 2017-07-28 10:38:24 +0100 | [diff] [blame] | 25 | #define DWS_WORD_ERASE_RETRIES 3000000 |
Roberto Vargas | 86a610e | 2017-07-26 14:37:56 +0100 | [diff] [blame] | 26 | #define DWS_WORD_LOCK_RETRIES 1000 |
Juan Castillo | facdd1c | 2015-08-12 12:53:02 +0100 | [diff] [blame] | 27 | |
Roberto Vargas | ab29dca | 2017-07-26 14:15:07 +0100 | [diff] [blame] | 28 | /* Helper macro to detect end of command */ |
| 29 | #define NOR_CMD_END (NOR_DWS | NOR_DWS << 16l) |
| 30 | |
Roberto Vargas | 6ca1911 | 2017-07-28 10:43:28 +0100 | [diff] [blame] | 31 | /* Helper macros to access two flash banks in parallel */ |
| 32 | #define NOR_2X16(d) ((d << 16) | (d & 0xffff)) |
| 33 | |
| 34 | static unsigned int nor_status(uintptr_t base_addr) |
| 35 | { |
| 36 | unsigned long status; |
| 37 | |
| 38 | nor_send_cmd(base_addr, NOR_CMD_READ_STATUS_REG); |
| 39 | status = mmio_read_32(base_addr); |
| 40 | status |= status >> 16; /* merge status from both flash banks */ |
| 41 | |
| 42 | return status & 0xFFFF; |
| 43 | } |
| 44 | |
| 45 | /* |
| 46 | * Poll Write State Machine. |
| 47 | * Return values: |
Juan Castillo | facdd1c | 2015-08-12 12:53:02 +0100 | [diff] [blame] | 48 | * 0 = WSM ready |
| 49 | * -EBUSY = WSM busy after the number of retries |
| 50 | */ |
Roberto Vargas | ea21edc | 2017-07-28 10:38:24 +0100 | [diff] [blame] | 51 | static int nor_poll_dws(uintptr_t base_addr, unsigned long int retries) |
Juan Castillo | facdd1c | 2015-08-12 12:53:02 +0100 | [diff] [blame] | 52 | { |
Roberto Vargas | ab29dca | 2017-07-26 14:15:07 +0100 | [diff] [blame] | 53 | unsigned long status; |
Juan Castillo | facdd1c | 2015-08-12 12:53:02 +0100 | [diff] [blame] | 54 | |
Roberto Vargas | ab29dca | 2017-07-26 14:15:07 +0100 | [diff] [blame] | 55 | do { |
Juan Castillo | facdd1c | 2015-08-12 12:53:02 +0100 | [diff] [blame] | 56 | nor_send_cmd(base_addr, NOR_CMD_READ_STATUS_REG); |
| 57 | status = mmio_read_32(base_addr); |
Roberto Vargas | ab29dca | 2017-07-26 14:15:07 +0100 | [diff] [blame] | 58 | if ((status & NOR_CMD_END) == NOR_CMD_END) |
| 59 | return 0; |
| 60 | } while (retries-- > 0); |
Juan Castillo | facdd1c | 2015-08-12 12:53:02 +0100 | [diff] [blame] | 61 | |
Roberto Vargas | ab29dca | 2017-07-26 14:15:07 +0100 | [diff] [blame] | 62 | return -EBUSY; |
Juan Castillo | facdd1c | 2015-08-12 12:53:02 +0100 | [diff] [blame] | 63 | } |
| 64 | |
Roberto Vargas | 6ca1911 | 2017-07-28 10:43:28 +0100 | [diff] [blame] | 65 | /* |
| 66 | * Return values: |
| 67 | * 0 = success |
| 68 | * -EPERM = Device protected or Block locked |
| 69 | * -EIO = General I/O error |
| 70 | */ |
| 71 | static int nor_full_status_check(uintptr_t base_addr) |
| 72 | { |
| 73 | unsigned long status; |
| 74 | |
| 75 | /* Full status check */ |
| 76 | status = nor_status(base_addr); |
| 77 | |
| 78 | if (status & (NOR_PS | NOR_BLS | NOR_ESS | NOR_PSS)) |
| 79 | return -EPERM; |
| 80 | if (status & (NOR_VPPS | NOR_ES)) |
| 81 | return -EIO; |
| 82 | return 0; |
| 83 | } |
| 84 | |
Juan Castillo | facdd1c | 2015-08-12 12:53:02 +0100 | [diff] [blame] | 85 | void nor_send_cmd(uintptr_t base_addr, unsigned long cmd) |
| 86 | { |
| 87 | mmio_write_32(base_addr, NOR_2X16(cmd)); |
| 88 | } |
| 89 | |
| 90 | /* |
Roberto Vargas | ab29dca | 2017-07-26 14:15:07 +0100 | [diff] [blame] | 91 | * This function programs a word in the flash. Be aware that it only |
| 92 | * can reset bits that were previously set. It cannot set bits that |
| 93 | * were previously reset. The resulting bits = old_bits & new bits. |
Juan Castillo | facdd1c | 2015-08-12 12:53:02 +0100 | [diff] [blame] | 94 | * Return values: |
Roberto Vargas | 6ca1911 | 2017-07-28 10:43:28 +0100 | [diff] [blame] | 95 | * 0 = success |
| 96 | * otherwise it returns a negative value |
Juan Castillo | facdd1c | 2015-08-12 12:53:02 +0100 | [diff] [blame] | 97 | */ |
| 98 | int nor_word_program(uintptr_t base_addr, unsigned long data) |
| 99 | { |
| 100 | uint32_t status; |
| 101 | int ret; |
| 102 | |
Roberto Vargas | 2e94e73 | 2017-07-26 15:17:24 +0100 | [diff] [blame] | 103 | nor_send_cmd(base_addr, NOR_CMD_CLEAR_STATUS_REG); |
| 104 | |
Juan Castillo | facdd1c | 2015-08-12 12:53:02 +0100 | [diff] [blame] | 105 | /* Set the device in write word mode */ |
| 106 | nor_send_cmd(base_addr, NOR_CMD_WORD_PROGRAM); |
| 107 | mmio_write_32(base_addr, data); |
| 108 | |
| 109 | ret = nor_poll_dws(base_addr, DWS_WORD_PROGRAM_RETRIES); |
Roberto Vargas | ab29dca | 2017-07-26 14:15:07 +0100 | [diff] [blame] | 110 | if (ret == 0) { |
| 111 | /* Full status check */ |
| 112 | nor_send_cmd(base_addr, NOR_CMD_READ_STATUS_REG); |
| 113 | status = mmio_read_32(base_addr); |
Juan Castillo | facdd1c | 2015-08-12 12:53:02 +0100 | [diff] [blame] | 114 | |
Roberto Vargas | ab29dca | 2017-07-26 14:15:07 +0100 | [diff] [blame] | 115 | if (status & (NOR_PS | NOR_BLS)) { |
| 116 | nor_send_cmd(base_addr, NOR_CMD_CLEAR_STATUS_REG); |
| 117 | ret = -EPERM; |
| 118 | } |
Juan Castillo | facdd1c | 2015-08-12 12:53:02 +0100 | [diff] [blame] | 119 | } |
| 120 | |
Roberto Vargas | 6ca1911 | 2017-07-28 10:43:28 +0100 | [diff] [blame] | 121 | if (ret == 0) |
| 122 | ret = nor_full_status_check(base_addr); |
Juan Castillo | facdd1c | 2015-08-12 12:53:02 +0100 | [diff] [blame] | 123 | nor_send_cmd(base_addr, NOR_CMD_READ_ARRAY); |
Roberto Vargas | 6ca1911 | 2017-07-28 10:43:28 +0100 | [diff] [blame] | 124 | |
Juan Castillo | facdd1c | 2015-08-12 12:53:02 +0100 | [diff] [blame] | 125 | return ret; |
| 126 | } |
| 127 | |
Roberto Vargas | ab29dca | 2017-07-26 14:15:07 +0100 | [diff] [blame] | 128 | /* |
Roberto Vargas | ea21edc | 2017-07-28 10:38:24 +0100 | [diff] [blame] | 129 | * Erase a full 256K block |
| 130 | * Return values: |
| 131 | * 0 = success |
Roberto Vargas | 6ca1911 | 2017-07-28 10:43:28 +0100 | [diff] [blame] | 132 | * otherwise it returns a negative value |
Roberto Vargas | ea21edc | 2017-07-28 10:38:24 +0100 | [diff] [blame] | 133 | */ |
| 134 | int nor_erase(uintptr_t base_addr) |
| 135 | { |
| 136 | int ret; |
| 137 | |
| 138 | nor_send_cmd(base_addr, NOR_CMD_CLEAR_STATUS_REG); |
| 139 | |
| 140 | nor_send_cmd(base_addr, NOR_CMD_BLOCK_ERASE); |
| 141 | nor_send_cmd(base_addr, NOR_CMD_BLOCK_ERASE_ACK); |
| 142 | |
| 143 | ret = nor_poll_dws(base_addr, DWS_WORD_ERASE_RETRIES); |
Roberto Vargas | 6ca1911 | 2017-07-28 10:43:28 +0100 | [diff] [blame] | 144 | if (ret == 0) |
| 145 | ret = nor_full_status_check(base_addr); |
Roberto Vargas | ea21edc | 2017-07-28 10:38:24 +0100 | [diff] [blame] | 146 | nor_send_cmd(base_addr, NOR_CMD_READ_ARRAY); |
| 147 | |
| 148 | return ret; |
| 149 | } |
| 150 | |
| 151 | /* |
Roberto Vargas | ab29dca | 2017-07-26 14:15:07 +0100 | [diff] [blame] | 152 | * Lock a full 256 block |
Roberto Vargas | 86a610e | 2017-07-26 14:37:56 +0100 | [diff] [blame] | 153 | * Return values: |
| 154 | * 0 = success |
| 155 | * otherwise it returns a negative value |
Roberto Vargas | ab29dca | 2017-07-26 14:15:07 +0100 | [diff] [blame] | 156 | */ |
Roberto Vargas | 86a610e | 2017-07-26 14:37:56 +0100 | [diff] [blame] | 157 | int nor_lock(uintptr_t base_addr) |
Juan Castillo | facdd1c | 2015-08-12 12:53:02 +0100 | [diff] [blame] | 158 | { |
Roberto Vargas | 86a610e | 2017-07-26 14:37:56 +0100 | [diff] [blame] | 159 | int ret; |
| 160 | |
Roberto Vargas | 2e94e73 | 2017-07-26 15:17:24 +0100 | [diff] [blame] | 161 | nor_send_cmd(base_addr, NOR_CMD_CLEAR_STATUS_REG); |
| 162 | |
Juan Castillo | facdd1c | 2015-08-12 12:53:02 +0100 | [diff] [blame] | 163 | nor_send_cmd(base_addr, NOR_CMD_LOCK_UNLOCK); |
Roberto Vargas | 2e94e73 | 2017-07-26 15:17:24 +0100 | [diff] [blame] | 164 | nor_send_cmd(base_addr, NOR_LOCK_BLOCK); |
Roberto Vargas | 86a610e | 2017-07-26 14:37:56 +0100 | [diff] [blame] | 165 | |
| 166 | ret = nor_poll_dws(base_addr, DWS_WORD_LOCK_RETRIES); |
Roberto Vargas | 6ca1911 | 2017-07-28 10:43:28 +0100 | [diff] [blame] | 167 | if (ret == 0) |
| 168 | ret = nor_full_status_check(base_addr); |
Juan Castillo | facdd1c | 2015-08-12 12:53:02 +0100 | [diff] [blame] | 169 | nor_send_cmd(base_addr, NOR_CMD_READ_ARRAY); |
Roberto Vargas | 86a610e | 2017-07-26 14:37:56 +0100 | [diff] [blame] | 170 | |
| 171 | return ret; |
Juan Castillo | facdd1c | 2015-08-12 12:53:02 +0100 | [diff] [blame] | 172 | } |
| 173 | |
Roberto Vargas | ab29dca | 2017-07-26 14:15:07 +0100 | [diff] [blame] | 174 | /* |
| 175 | * unlock a full 256 block |
Roberto Vargas | 86a610e | 2017-07-26 14:37:56 +0100 | [diff] [blame] | 176 | * Return values: |
| 177 | * 0 = success |
| 178 | * otherwise it returns a negative value |
Roberto Vargas | ab29dca | 2017-07-26 14:15:07 +0100 | [diff] [blame] | 179 | */ |
Roberto Vargas | 86a610e | 2017-07-26 14:37:56 +0100 | [diff] [blame] | 180 | int nor_unlock(uintptr_t base_addr) |
Juan Castillo | facdd1c | 2015-08-12 12:53:02 +0100 | [diff] [blame] | 181 | { |
Roberto Vargas | 86a610e | 2017-07-26 14:37:56 +0100 | [diff] [blame] | 182 | int ret; |
| 183 | |
Roberto Vargas | 2e94e73 | 2017-07-26 15:17:24 +0100 | [diff] [blame] | 184 | nor_send_cmd(base_addr, NOR_CMD_CLEAR_STATUS_REG); |
| 185 | |
Juan Castillo | facdd1c | 2015-08-12 12:53:02 +0100 | [diff] [blame] | 186 | nor_send_cmd(base_addr, NOR_CMD_LOCK_UNLOCK); |
Roberto Vargas | 2e94e73 | 2017-07-26 15:17:24 +0100 | [diff] [blame] | 187 | nor_send_cmd(base_addr, NOR_UNLOCK_BLOCK); |
Roberto Vargas | 86a610e | 2017-07-26 14:37:56 +0100 | [diff] [blame] | 188 | |
| 189 | ret = nor_poll_dws(base_addr, DWS_WORD_LOCK_RETRIES); |
Roberto Vargas | 6ca1911 | 2017-07-28 10:43:28 +0100 | [diff] [blame] | 190 | if (ret == 0) |
| 191 | ret = nor_full_status_check(base_addr); |
Juan Castillo | facdd1c | 2015-08-12 12:53:02 +0100 | [diff] [blame] | 192 | nor_send_cmd(base_addr, NOR_CMD_READ_ARRAY); |
Roberto Vargas | 86a610e | 2017-07-26 14:37:56 +0100 | [diff] [blame] | 193 | |
| 194 | return ret; |
Juan Castillo | facdd1c | 2015-08-12 12:53:02 +0100 | [diff] [blame] | 195 | } |