Sundarajan Srinivasan | 15516f2 | 2013-12-04 17:11:36 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2013-2014, The Linux Foundation. All rights reserved. |
Channagoud Kadabi | 4ed511f | 2013-03-11 16:10:18 -0700 | [diff] [blame] | 2 | * |
| 3 | * Redistribution and use in source and binary forms, with or without |
| 4 | * modification, are permitted provided that the following conditions are |
| 5 | * met: |
| 6 | * * Redistributions of source code must retain the above copyright |
| 7 | * notice, this list of conditions and the following disclaimer. |
| 8 | * * Redistributions in binary form must reproduce the above |
| 9 | * copyright notice, this list of conditions and the following |
| 10 | * disclaimer in the documentation and/or other materials provided |
| 11 | * with the distribution. |
| 12 | * * Neither the name of The Linux Foundation nor the names of its |
| 13 | * contributors may be used to endorse or promote products derived |
| 14 | * from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 20 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 23 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 24 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 25 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 26 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #include <stdlib.h> |
| 30 | #include <stdint.h> |
Channagoud Kadabi | 4ed511f | 2013-03-11 16:10:18 -0700 | [diff] [blame] | 31 | #include <mmc_wrapper.h> |
Channagoud Kadabi | a17fc42 | 2013-09-11 12:25:34 -0700 | [diff] [blame] | 32 | #include <mmc_sdhci.h> |
Channagoud Kadabi | 4ed511f | 2013-03-11 16:10:18 -0700 | [diff] [blame] | 33 | #include <sdhci.h> |
Channagoud Kadabi | a17fc42 | 2013-09-11 12:25:34 -0700 | [diff] [blame] | 34 | #include <ufs.h> |
Channagoud Kadabi | 4ed511f | 2013-03-11 16:10:18 -0700 | [diff] [blame] | 35 | #include <target.h> |
Oliver Wang | cee448d | 2013-10-22 18:40:13 +0800 | [diff] [blame] | 36 | #include <string.h> |
Channagoud Kadabi | 4ed511f | 2013-03-11 16:10:18 -0700 | [diff] [blame] | 37 | |
| 38 | /* |
Channagoud Kadabi | a17fc42 | 2013-09-11 12:25:34 -0700 | [diff] [blame] | 39 | * Weak function for UFS. |
| 40 | * These are needed to avoid link errors for platforms which |
| 41 | * do not support UFS. Its better to keep this inside the |
| 42 | * mmc wrapper. |
| 43 | */ |
| 44 | __WEAK int ufs_write(struct ufs_dev *dev, uint64_t data_addr, addr_t in, uint32_t len) |
| 45 | { |
| 46 | return 0; |
| 47 | } |
| 48 | |
| 49 | __WEAK int ufs_read(struct ufs_dev *dev, uint64_t data_addr, addr_t in, uint32_t len) |
| 50 | { |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | __WEAK uint32_t ufs_get_page_size(struct ufs_dev *dev) |
| 55 | { |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | __WEAK uint32_t ufs_get_serial_num(struct ufs_dev *dev) |
| 60 | { |
| 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | __WEAK uint64_t ufs_get_dev_capacity(struct ufs_dev *dev) |
| 65 | { |
| 66 | return 0; |
| 67 | } |
| 68 | |
Sundarajan Srinivasan | 7b74ed2 | 2013-11-01 16:01:06 -0700 | [diff] [blame] | 69 | __WEAK uint32_t ufs_get_erase_blk_size(struct ufs_dev *dev) |
| 70 | { |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | __WEAK int ufs_erase(struct ufs_dev* dev, uint64_t start_lba, uint32_t num_blocks) |
| 75 | { |
| 76 | return 0; |
| 77 | } |
Sundarajan Srinivasan | 15516f2 | 2013-12-04 17:11:36 -0800 | [diff] [blame] | 78 | |
| 79 | __WEAK uint8_t ufs_get_num_of_luns(struct ufs_dev* dev) |
| 80 | { |
| 81 | return 0; |
| 82 | } |
| 83 | |
Channagoud Kadabi | a17fc42 | 2013-09-11 12:25:34 -0700 | [diff] [blame] | 84 | /* |
Channagoud Kadabi | 4ed511f | 2013-03-11 16:10:18 -0700 | [diff] [blame] | 85 | * Function: get mmc card |
| 86 | * Arg : None |
| 87 | * Return : Pointer to mmc card structure |
| 88 | * Flow : Get the card pointer from the device structure |
| 89 | */ |
| 90 | static struct mmc_card *get_mmc_card() |
| 91 | { |
Channagoud Kadabi | a17fc42 | 2013-09-11 12:25:34 -0700 | [diff] [blame] | 92 | void *dev; |
Channagoud Kadabi | 4ed511f | 2013-03-11 16:10:18 -0700 | [diff] [blame] | 93 | struct mmc_card *card; |
| 94 | |
| 95 | dev = target_mmc_device(); |
Channagoud Kadabi | a17fc42 | 2013-09-11 12:25:34 -0700 | [diff] [blame] | 96 | card = &((struct mmc_device*)dev)->card; |
Channagoud Kadabi | 4ed511f | 2013-03-11 16:10:18 -0700 | [diff] [blame] | 97 | |
| 98 | return card; |
| 99 | } |
| 100 | |
| 101 | /* |
| 102 | * Function: mmc_write |
| 103 | * Arg : Data address on card, data length, i/p buffer |
| 104 | * Return : 0 on Success, non zero on failure |
| 105 | * Flow : Write the data from in to the card |
| 106 | */ |
| 107 | uint32_t mmc_write(uint64_t data_addr, uint32_t data_len, void *in) |
| 108 | { |
Channagoud Kadabi | 53f1be7 | 2013-06-05 17:56:11 -0700 | [diff] [blame] | 109 | uint32_t val = 0; |
Channagoud Kadabi | a17fc42 | 2013-09-11 12:25:34 -0700 | [diff] [blame] | 110 | int ret = 0; |
| 111 | uint32_t block_size = 0; |
Channagoud Kadabi | 4ed511f | 2013-03-11 16:10:18 -0700 | [diff] [blame] | 112 | uint32_t write_size = SDHCI_ADMA_MAX_TRANS_SZ; |
Channagoud Kadabi | 53f1be7 | 2013-06-05 17:56:11 -0700 | [diff] [blame] | 113 | uint8_t *sptr = (uint8_t *)in; |
Channagoud Kadabi | a17fc42 | 2013-09-11 12:25:34 -0700 | [diff] [blame] | 114 | void *dev; |
Channagoud Kadabi | 4ed511f | 2013-03-11 16:10:18 -0700 | [diff] [blame] | 115 | |
| 116 | dev = target_mmc_device(); |
| 117 | |
Channagoud Kadabi | a17fc42 | 2013-09-11 12:25:34 -0700 | [diff] [blame] | 118 | block_size = mmc_get_device_blocksize(); |
Channagoud Kadabi | 4ed511f | 2013-03-11 16:10:18 -0700 | [diff] [blame] | 119 | |
Channagoud Kadabi | a17fc42 | 2013-09-11 12:25:34 -0700 | [diff] [blame] | 120 | ASSERT(!(data_addr % block_size)); |
Channagoud Kadabi | 4ed511f | 2013-03-11 16:10:18 -0700 | [diff] [blame] | 121 | |
Channagoud Kadabi | a17fc42 | 2013-09-11 12:25:34 -0700 | [diff] [blame] | 122 | if (data_len % block_size) |
| 123 | data_len = ROUNDUP(data_len, block_size); |
| 124 | |
Sundarajan Srinivasan | 9786c45 | 2014-03-21 17:34:58 -0700 | [diff] [blame^] | 125 | if (platform_boot_dev_isemmc()) |
Channagoud Kadabi | a17fc42 | 2013-09-11 12:25:34 -0700 | [diff] [blame] | 126 | { |
| 127 | /* TODO: This function is aware of max data that can be |
| 128 | * tranferred using sdhci adma mode, need to have a cleaner |
| 129 | * implementation to keep this function independent of sdhci |
| 130 | * limitations |
| 131 | */ |
| 132 | while (data_len > write_size) { |
| 133 | val = mmc_sdhci_write((struct mmc_device *)dev, (void *)sptr, (data_addr / block_size), (write_size / block_size)); |
| 134 | if (val) |
| 135 | { |
| 136 | dprintf(CRITICAL, "Failed Writing block @ %x\n", (data_addr / block_size)); |
| 137 | return val; |
| 138 | } |
| 139 | sptr += write_size; |
| 140 | data_addr += write_size; |
| 141 | data_len -= write_size; |
Channagoud Kadabi | 53f1be7 | 2013-06-05 17:56:11 -0700 | [diff] [blame] | 142 | } |
Channagoud Kadabi | a17fc42 | 2013-09-11 12:25:34 -0700 | [diff] [blame] | 143 | |
| 144 | if (data_len) |
| 145 | val = mmc_sdhci_write((struct mmc_device *)dev, (void *)sptr, (data_addr / block_size), (data_len / block_size)); |
| 146 | |
| 147 | if (val) |
| 148 | dprintf(CRITICAL, "Failed Writing block @ %x\n", (data_addr / block_size)); |
Channagoud Kadabi | 4ed511f | 2013-03-11 16:10:18 -0700 | [diff] [blame] | 149 | } |
Channagoud Kadabi | a17fc42 | 2013-09-11 12:25:34 -0700 | [diff] [blame] | 150 | else |
| 151 | { |
| 152 | arch_clean_invalidate_cache_range((addr_t)in, data_len); |
Channagoud Kadabi | 4ed511f | 2013-03-11 16:10:18 -0700 | [diff] [blame] | 153 | |
Channagoud Kadabi | a17fc42 | 2013-09-11 12:25:34 -0700 | [diff] [blame] | 154 | ret = ufs_write((struct ufs_dev *)dev, data_addr, (addr_t)in, (data_len / block_size)); |
Channagoud Kadabi | 53f1be7 | 2013-06-05 17:56:11 -0700 | [diff] [blame] | 155 | |
Channagoud Kadabi | a17fc42 | 2013-09-11 12:25:34 -0700 | [diff] [blame] | 156 | if (ret) |
| 157 | { |
| 158 | dprintf(CRITICAL, "Error: UFS write failed writing to block: %llu\n", data_addr); |
| 159 | val = 1; |
| 160 | } |
| 161 | } |
Channagoud Kadabi | 4ed511f | 2013-03-11 16:10:18 -0700 | [diff] [blame] | 162 | |
| 163 | return val; |
| 164 | } |
| 165 | |
| 166 | /* |
| 167 | * Function: mmc_read |
| 168 | * Arg : Data address on card, o/p buffer & data length |
| 169 | * Return : 0 on Success, non zero on failure |
| 170 | * Flow : Read data from the card to out |
| 171 | */ |
| 172 | uint32_t mmc_read(uint64_t data_addr, uint32_t *out, uint32_t data_len) |
| 173 | { |
| 174 | uint32_t ret = 0; |
Channagoud Kadabi | a17fc42 | 2013-09-11 12:25:34 -0700 | [diff] [blame] | 175 | uint32_t block_size; |
Channagoud Kadabi | 4ed511f | 2013-03-11 16:10:18 -0700 | [diff] [blame] | 176 | uint32_t read_size = SDHCI_ADMA_MAX_TRANS_SZ; |
Channagoud Kadabi | a17fc42 | 2013-09-11 12:25:34 -0700 | [diff] [blame] | 177 | void *dev; |
Channagoud Kadabi | 53f1be7 | 2013-06-05 17:56:11 -0700 | [diff] [blame] | 178 | uint8_t *sptr = (uint8_t *)out; |
Channagoud Kadabi | 4ed511f | 2013-03-11 16:10:18 -0700 | [diff] [blame] | 179 | |
Channagoud Kadabi | 4ed511f | 2013-03-11 16:10:18 -0700 | [diff] [blame] | 180 | dev = target_mmc_device(); |
Channagoud Kadabi | a17fc42 | 2013-09-11 12:25:34 -0700 | [diff] [blame] | 181 | block_size = mmc_get_device_blocksize(); |
Channagoud Kadabi | 4ed511f | 2013-03-11 16:10:18 -0700 | [diff] [blame] | 182 | |
Channagoud Kadabi | a17fc42 | 2013-09-11 12:25:34 -0700 | [diff] [blame] | 183 | ASSERT(!(data_addr % block_size)); |
| 184 | ASSERT(!(data_len % block_size)); |
| 185 | |
| 186 | |
Sundarajan Srinivasan | 9786c45 | 2014-03-21 17:34:58 -0700 | [diff] [blame^] | 187 | if (platform_boot_dev_isemmc()) |
Channagoud Kadabi | a17fc42 | 2013-09-11 12:25:34 -0700 | [diff] [blame] | 188 | { |
| 189 | /* TODO: This function is aware of max data that can be |
| 190 | * tranferred using sdhci adma mode, need to have a cleaner |
| 191 | * implementation to keep this function independent of sdhci |
| 192 | * limitations |
| 193 | */ |
| 194 | while (data_len > read_size) { |
| 195 | ret = mmc_sdhci_read((struct mmc_device *)dev, (void *)sptr, (data_addr / block_size), (read_size / block_size)); |
| 196 | if (ret) |
| 197 | { |
| 198 | dprintf(CRITICAL, "Failed Reading block @ %x\n", (data_addr / block_size)); |
| 199 | return ret; |
| 200 | } |
| 201 | sptr += read_size; |
| 202 | data_addr += read_size; |
| 203 | data_len -= read_size; |
| 204 | } |
| 205 | |
| 206 | if (data_len) |
| 207 | ret = mmc_sdhci_read((struct mmc_device *)dev, (void *)sptr, (data_addr / block_size), (data_len / block_size)); |
| 208 | |
| 209 | if (ret) |
| 210 | dprintf(CRITICAL, "Failed Reading block @ %x\n", (data_addr / block_size)); |
| 211 | } |
| 212 | else |
| 213 | { |
| 214 | ret = ufs_read((struct ufs_dev *) dev, data_addr, (addr_t)out, (data_len / block_size)); |
Channagoud Kadabi | 53f1be7 | 2013-06-05 17:56:11 -0700 | [diff] [blame] | 215 | if (ret) |
| 216 | { |
Channagoud Kadabi | a17fc42 | 2013-09-11 12:25:34 -0700 | [diff] [blame] | 217 | dprintf(CRITICAL, "Error: UFS read failed writing to block: %llu\n", data_addr); |
Channagoud Kadabi | 53f1be7 | 2013-06-05 17:56:11 -0700 | [diff] [blame] | 218 | } |
Channagoud Kadabi | a17fc42 | 2013-09-11 12:25:34 -0700 | [diff] [blame] | 219 | |
| 220 | arch_invalidate_cache_range((addr_t)out, data_len); |
Channagoud Kadabi | 4ed511f | 2013-03-11 16:10:18 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Channagoud Kadabi | 4ed511f | 2013-03-11 16:10:18 -0700 | [diff] [blame] | 223 | return ret; |
| 224 | } |
| 225 | |
Sundarajan Srinivasan | 7b74ed2 | 2013-11-01 16:01:06 -0700 | [diff] [blame] | 226 | |
Channagoud Kadabi | 4ed511f | 2013-03-11 16:10:18 -0700 | [diff] [blame] | 227 | /* |
Oliver Wang | cee448d | 2013-10-22 18:40:13 +0800 | [diff] [blame] | 228 | * Function: mmc get erase unit size |
| 229 | * Arg : None |
| 230 | * Return : Returns the erase unit size of the storage |
| 231 | * Flow : Get the erase unit size from the card |
| 232 | */ |
| 233 | |
| 234 | uint32_t mmc_get_eraseunit_size() |
| 235 | { |
| 236 | uint32_t erase_unit_sz = 0; |
| 237 | |
Sundarajan Srinivasan | 9786c45 | 2014-03-21 17:34:58 -0700 | [diff] [blame^] | 238 | if (platform_boot_dev_isemmc()) { |
Oliver Wang | cee448d | 2013-10-22 18:40:13 +0800 | [diff] [blame] | 239 | struct mmc_device *dev; |
| 240 | struct mmc_card *card; |
| 241 | |
| 242 | dev = target_mmc_device(); |
| 243 | card = &dev->card; |
| 244 | /* |
| 245 | * Calculate the erase unit size, |
| 246 | * 1. Based on emmc 4.5 spec for emmc card |
| 247 | * 2. Use SD Card Status info for SD cards |
| 248 | */ |
| 249 | if (MMC_CARD_MMC(card)) |
| 250 | { |
| 251 | /* |
| 252 | * Calculate the erase unit size as per the emmc specification v4.5 |
| 253 | */ |
| 254 | if (dev->card.ext_csd[MMC_ERASE_GRP_DEF]) |
| 255 | erase_unit_sz = (MMC_HC_ERASE_MULT * dev->card.ext_csd[MMC_HC_ERASE_GRP_SIZE]) / MMC_BLK_SZ; |
| 256 | else |
| 257 | erase_unit_sz = (dev->card.csd.erase_grp_size + 1) * (dev->card.csd.erase_grp_mult + 1); |
| 258 | } |
| 259 | else |
| 260 | erase_unit_sz = dev->card.ssr.au_size * dev->card.ssr.num_aus; |
| 261 | } |
| 262 | |
| 263 | return erase_unit_sz; |
| 264 | } |
| 265 | |
| 266 | /* |
| 267 | * Function: Zero out blk_len blocks at the blk_addr by writing zeros. The |
| 268 | * function can be used when we want to erase the blocks not |
| 269 | * aligned with the mmc erase group. |
| 270 | * Arg : Block address & length |
| 271 | * Return : Returns 0 |
| 272 | * Flow : Erase the card from specified addr |
| 273 | */ |
| 274 | |
| 275 | static uint32_t mmc_zero_out(struct mmc_device* dev, uint32_t blk_addr, uint32_t num_blks) |
| 276 | { |
| 277 | uint32_t *out; |
Channagoud Kadabi | a87af97 | 2014-03-10 15:26:48 -0700 | [diff] [blame] | 278 | uint32_t block_size = mmc_get_device_blocksize(); |
| 279 | uint32_t erase_size = (block_size * num_blks); |
| 280 | uint32_t scratch_size = target_get_max_flash_size(); |
Oliver Wang | cee448d | 2013-10-22 18:40:13 +0800 | [diff] [blame] | 281 | |
| 282 | dprintf(INFO, "erasing 0x%x:0x%x\n", blk_addr, num_blks); |
Oliver Wang | cee448d | 2013-10-22 18:40:13 +0800 | [diff] [blame] | 283 | |
Channagoud Kadabi | a87af97 | 2014-03-10 15:26:48 -0700 | [diff] [blame] | 284 | if (erase_size <= scratch_size) |
Oliver Wang | cee448d | 2013-10-22 18:40:13 +0800 | [diff] [blame] | 285 | { |
Channagoud Kadabi | a87af97 | 2014-03-10 15:26:48 -0700 | [diff] [blame] | 286 | /* Use scratch address if the unaligned blocks */ |
| 287 | out = (uint32_t *) target_get_scratch_address(); |
| 288 | } |
| 289 | else |
| 290 | { |
| 291 | dprintf(CRITICAL, "Erase Fail: Erase size: %u is bigger than scratch region:%u\n", scratch_size); |
Oliver Wang | cee448d | 2013-10-22 18:40:13 +0800 | [diff] [blame] | 292 | return 1; |
| 293 | } |
Oliver Wang | cee448d | 2013-10-22 18:40:13 +0800 | [diff] [blame] | 294 | |
Channagoud Kadabi | a87af97 | 2014-03-10 15:26:48 -0700 | [diff] [blame] | 295 | memset((void *)out, 0, erase_size); |
| 296 | |
| 297 | if (mmc_sdhci_write(dev, out, blk_addr, num_blks)) |
Oliver Wang | cee448d | 2013-10-22 18:40:13 +0800 | [diff] [blame] | 298 | { |
Channagoud Kadabi | a87af97 | 2014-03-10 15:26:48 -0700 | [diff] [blame] | 299 | dprintf(CRITICAL, "failed to erase the partition: %x\n", blk_addr); |
| 300 | return 1; |
Oliver Wang | cee448d | 2013-10-22 18:40:13 +0800 | [diff] [blame] | 301 | } |
Channagoud Kadabi | a87af97 | 2014-03-10 15:26:48 -0700 | [diff] [blame] | 302 | |
Oliver Wang | cee448d | 2013-10-22 18:40:13 +0800 | [diff] [blame] | 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | /* |
Channagoud Kadabi | 4ed511f | 2013-03-11 16:10:18 -0700 | [diff] [blame] | 307 | * Function: mmc erase card |
| 308 | * Arg : Block address & length |
| 309 | * Return : Returns 0 |
Channagoud Kadabi | d87c6b1 | 2013-05-29 15:22:03 -0700 | [diff] [blame] | 310 | * Flow : Erase the card from specified addr |
Channagoud Kadabi | 4ed511f | 2013-03-11 16:10:18 -0700 | [diff] [blame] | 311 | */ |
| 312 | uint32_t mmc_erase_card(uint64_t addr, uint64_t len) |
| 313 | { |
Oliver Wang | cee448d | 2013-10-22 18:40:13 +0800 | [diff] [blame] | 314 | struct mmc_device *dev; |
Channagoud Kadabi | a17fc42 | 2013-09-11 12:25:34 -0700 | [diff] [blame] | 315 | uint32_t block_size; |
Oliver Wang | cee448d | 2013-10-22 18:40:13 +0800 | [diff] [blame] | 316 | uint32_t unaligned_blks; |
| 317 | uint32_t head_unit; |
| 318 | uint32_t tail_unit; |
| 319 | uint32_t erase_unit_sz; |
| 320 | uint32_t blk_addr; |
| 321 | uint32_t blk_count; |
| 322 | uint64_t blks_to_erase; |
Channagoud Kadabi | d87c6b1 | 2013-05-29 15:22:03 -0700 | [diff] [blame] | 323 | |
Sundarajan Srinivasan | 7b74ed2 | 2013-11-01 16:01:06 -0700 | [diff] [blame] | 324 | block_size = mmc_get_device_blocksize(); |
| 325 | |
| 326 | dev = target_mmc_device(); |
| 327 | |
| 328 | ASSERT(!(addr % block_size)); |
| 329 | ASSERT(!(len % block_size)); |
| 330 | |
Sundarajan Srinivasan | 9786c45 | 2014-03-21 17:34:58 -0700 | [diff] [blame^] | 331 | if (platform_boot_dev_isemmc()) |
Channagoud Kadabi | d87c6b1 | 2013-05-29 15:22:03 -0700 | [diff] [blame] | 332 | { |
Oliver Wang | cee448d | 2013-10-22 18:40:13 +0800 | [diff] [blame] | 333 | erase_unit_sz = mmc_get_eraseunit_size(); |
| 334 | dprintf(SPEW, "erase_unit_sz:0x%x\n", erase_unit_sz); |
Channagoud Kadabi | a17fc42 | 2013-09-11 12:25:34 -0700 | [diff] [blame] | 335 | |
Oliver Wang | cee448d | 2013-10-22 18:40:13 +0800 | [diff] [blame] | 336 | blk_addr = addr / block_size; |
| 337 | blk_count = len / block_size; |
| 338 | |
Oliver Wang | cee448d | 2013-10-22 18:40:13 +0800 | [diff] [blame] | 339 | dprintf(INFO, "Erasing card: 0x%x:0x%x\n", blk_addr, blk_count); |
| 340 | |
| 341 | head_unit = blk_addr / erase_unit_sz; |
| 342 | tail_unit = (blk_addr + blk_count - 1) / erase_unit_sz; |
| 343 | |
| 344 | if (tail_unit - head_unit <= 1) |
| 345 | { |
| 346 | dprintf(INFO, "SDHCI unit erase not required\n"); |
| 347 | return mmc_zero_out(dev, blk_addr, blk_count); |
| 348 | } |
| 349 | |
| 350 | unaligned_blks = erase_unit_sz - (blk_addr % erase_unit_sz); |
| 351 | |
| 352 | if (unaligned_blks < erase_unit_sz) |
| 353 | { |
| 354 | dprintf(SPEW, "Handling unaligned head blocks\n"); |
| 355 | if (mmc_zero_out(dev, blk_addr, unaligned_blks)) |
| 356 | return 1; |
| 357 | |
| 358 | blk_addr += unaligned_blks; |
| 359 | blk_count -= unaligned_blks; |
| 360 | } |
| 361 | |
| 362 | unaligned_blks = blk_count % erase_unit_sz; |
| 363 | blks_to_erase = blk_count - unaligned_blks; |
| 364 | |
| 365 | dprintf(SPEW, "Performing SDHCI erase: 0x%x:0x%x\n", blk_addr, blks_to_erase); |
| 366 | if (mmc_sdhci_erase((struct mmc_device *)dev, blk_addr, blks_to_erase * block_size)) |
Channagoud Kadabi | a17fc42 | 2013-09-11 12:25:34 -0700 | [diff] [blame] | 367 | { |
| 368 | dprintf(CRITICAL, "MMC erase failed\n"); |
| 369 | return 1; |
| 370 | } |
Oliver Wang | cee448d | 2013-10-22 18:40:13 +0800 | [diff] [blame] | 371 | |
| 372 | blk_addr += blks_to_erase; |
| 373 | |
| 374 | if (unaligned_blks) |
| 375 | { |
| 376 | dprintf(SPEW, "Handling unaligned tail blocks\n"); |
| 377 | if (mmc_zero_out(dev, blk_addr, unaligned_blks)) |
| 378 | return 1; |
| 379 | } |
| 380 | |
Channagoud Kadabi | d87c6b1 | 2013-05-29 15:22:03 -0700 | [diff] [blame] | 381 | } |
Sundarajan Srinivasan | 7b74ed2 | 2013-11-01 16:01:06 -0700 | [diff] [blame] | 382 | else |
| 383 | { |
| 384 | if(ufs_erase((struct ufs_dev *)dev, addr, (len / block_size))) |
| 385 | { |
| 386 | dprintf(CRITICAL, "mmc_erase_card: UFS erase failed\n"); |
| 387 | return 1; |
| 388 | } |
| 389 | } |
Channagoud Kadabi | a17fc42 | 2013-09-11 12:25:34 -0700 | [diff] [blame] | 390 | |
Channagoud Kadabi | 4ed511f | 2013-03-11 16:10:18 -0700 | [diff] [blame] | 391 | return 0; |
| 392 | } |
| 393 | |
| 394 | /* |
| 395 | * Function: mmc get psn |
| 396 | * Arg : None |
| 397 | * Return : Returns the product serial number |
| 398 | * Flow : Get the PSN from card |
| 399 | */ |
| 400 | uint32_t mmc_get_psn(void) |
| 401 | { |
Sundarajan Srinivasan | 9786c45 | 2014-03-21 17:34:58 -0700 | [diff] [blame^] | 402 | if (platform_boot_dev_isemmc()) |
Channagoud Kadabi | a17fc42 | 2013-09-11 12:25:34 -0700 | [diff] [blame] | 403 | { |
| 404 | struct mmc_card *card; |
Channagoud Kadabi | 4ed511f | 2013-03-11 16:10:18 -0700 | [diff] [blame] | 405 | |
Channagoud Kadabi | a17fc42 | 2013-09-11 12:25:34 -0700 | [diff] [blame] | 406 | card = get_mmc_card(); |
Channagoud Kadabi | 4ed511f | 2013-03-11 16:10:18 -0700 | [diff] [blame] | 407 | |
Channagoud Kadabi | a17fc42 | 2013-09-11 12:25:34 -0700 | [diff] [blame] | 408 | return card->cid.psn; |
| 409 | } |
| 410 | else |
| 411 | { |
| 412 | void *dev; |
| 413 | |
| 414 | dev = target_mmc_device(); |
| 415 | |
| 416 | return ufs_get_serial_num((struct ufs_dev *)dev); |
| 417 | } |
Channagoud Kadabi | 4ed511f | 2013-03-11 16:10:18 -0700 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | /* |
| 421 | * Function: mmc get capacity |
| 422 | * Arg : None |
| 423 | * Return : Returns the density of the emmc card |
| 424 | * Flow : Get the density from card |
| 425 | */ |
| 426 | uint64_t mmc_get_device_capacity() |
| 427 | { |
Sundarajan Srinivasan | 9786c45 | 2014-03-21 17:34:58 -0700 | [diff] [blame^] | 428 | if (platform_boot_dev_isemmc()) |
Channagoud Kadabi | a17fc42 | 2013-09-11 12:25:34 -0700 | [diff] [blame] | 429 | { |
| 430 | struct mmc_card *card; |
Channagoud Kadabi | 4ed511f | 2013-03-11 16:10:18 -0700 | [diff] [blame] | 431 | |
Channagoud Kadabi | a17fc42 | 2013-09-11 12:25:34 -0700 | [diff] [blame] | 432 | card = get_mmc_card(); |
Channagoud Kadabi | 4ed511f | 2013-03-11 16:10:18 -0700 | [diff] [blame] | 433 | |
Channagoud Kadabi | a17fc42 | 2013-09-11 12:25:34 -0700 | [diff] [blame] | 434 | return card->capacity; |
| 435 | } |
| 436 | else |
| 437 | { |
| 438 | void *dev; |
| 439 | |
| 440 | dev = target_mmc_device(); |
| 441 | |
| 442 | return ufs_get_dev_capacity((struct ufs_dev *)dev); |
| 443 | } |
Channagoud Kadabi | 4ed511f | 2013-03-11 16:10:18 -0700 | [diff] [blame] | 444 | } |
| 445 | |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 446 | /* |
Channagoud Kadabi | a17fc42 | 2013-09-11 12:25:34 -0700 | [diff] [blame] | 447 | * Function: mmc get blocksize |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 448 | * Arg : None |
Channagoud Kadabi | a17fc42 | 2013-09-11 12:25:34 -0700 | [diff] [blame] | 449 | * Return : Returns the block size of the storage |
| 450 | * Flow : Get the block size form the card |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 451 | */ |
| 452 | uint32_t mmc_get_device_blocksize() |
| 453 | { |
Sundarajan Srinivasan | 9786c45 | 2014-03-21 17:34:58 -0700 | [diff] [blame^] | 454 | if (platform_boot_dev_isemmc()) |
Channagoud Kadabi | a17fc42 | 2013-09-11 12:25:34 -0700 | [diff] [blame] | 455 | { |
| 456 | struct mmc_card *card; |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 457 | |
Channagoud Kadabi | a17fc42 | 2013-09-11 12:25:34 -0700 | [diff] [blame] | 458 | card = get_mmc_card(); |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 459 | |
Channagoud Kadabi | a17fc42 | 2013-09-11 12:25:34 -0700 | [diff] [blame] | 460 | return card->block_size; |
| 461 | } |
| 462 | else |
| 463 | { |
| 464 | void *dev; |
| 465 | |
| 466 | dev = target_mmc_device(); |
| 467 | |
| 468 | return ufs_get_page_size((struct ufs_dev *)dev); |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | /* |
| 473 | * Function: storage page size |
| 474 | * Arg : None |
| 475 | * Return : Returns the page size for the card |
| 476 | * Flow : Get the page size for storage |
| 477 | */ |
| 478 | uint32_t mmc_page_size() |
| 479 | { |
Sundarajan Srinivasan | 9786c45 | 2014-03-21 17:34:58 -0700 | [diff] [blame^] | 480 | if (platform_boot_dev_isemmc()) |
Channagoud Kadabi | a17fc42 | 2013-09-11 12:25:34 -0700 | [diff] [blame] | 481 | { |
| 482 | return BOARD_KERNEL_PAGESIZE; |
| 483 | } |
| 484 | else |
| 485 | { |
| 486 | void *dev; |
| 487 | |
| 488 | dev = target_mmc_device(); |
| 489 | |
| 490 | return ufs_get_page_size((struct ufs_dev *)dev); |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | /* |
| 495 | * Function: mmc device sleep |
| 496 | * Arg : None |
| 497 | * Return : Clean up function for storage |
| 498 | * Flow : Put the mmc card to sleep |
| 499 | */ |
| 500 | void mmc_device_sleep() |
| 501 | { |
| 502 | void *dev; |
| 503 | dev = target_mmc_device(); |
| 504 | |
Sundarajan Srinivasan | 9786c45 | 2014-03-21 17:34:58 -0700 | [diff] [blame^] | 505 | if (platform_boot_dev_isemmc()) |
Channagoud Kadabi | a17fc42 | 2013-09-11 12:25:34 -0700 | [diff] [blame] | 506 | { |
| 507 | mmc_put_card_to_sleep((struct mmc_device *)dev); |
| 508 | } |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 509 | } |
Sundarajan Srinivasan | 15516f2 | 2013-12-04 17:11:36 -0800 | [diff] [blame] | 510 | |
| 511 | /* |
| 512 | * Function : mmc set LUN for ufs |
| 513 | * Arg : LUN number |
| 514 | * Return type : void |
| 515 | */ |
| 516 | void mmc_set_lun(uint8_t lun) |
| 517 | { |
| 518 | void *dev; |
| 519 | dev = target_mmc_device(); |
| 520 | |
Sundarajan Srinivasan | 9786c45 | 2014-03-21 17:34:58 -0700 | [diff] [blame^] | 521 | if (!platform_boot_dev_isemmc()) |
Sundarajan Srinivasan | 15516f2 | 2013-12-04 17:11:36 -0800 | [diff] [blame] | 522 | { |
| 523 | ((struct ufs_dev*)dev)->current_lun = lun; |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | /* |
| 528 | * Function : mmc get LUN from ufs |
| 529 | * Arg : LUN number |
| 530 | * Return type : lun number for UFS and 0 for emmc |
| 531 | */ |
| 532 | uint8_t mmc_get_lun(void) |
| 533 | { |
| 534 | void *dev; |
| 535 | uint8_t lun=0; |
| 536 | |
| 537 | dev = target_mmc_device(); |
| 538 | |
Sundarajan Srinivasan | 9786c45 | 2014-03-21 17:34:58 -0700 | [diff] [blame^] | 539 | if (!platform_boot_dev_isemmc()) |
Sundarajan Srinivasan | 15516f2 | 2013-12-04 17:11:36 -0800 | [diff] [blame] | 540 | { |
| 541 | lun = ((struct ufs_dev*)dev)->current_lun; |
| 542 | } |
| 543 | |
| 544 | return lun; |
| 545 | } |
| 546 | |
| 547 | void mmc_read_partition_table(uint8_t arg) |
| 548 | { |
| 549 | void *dev; |
| 550 | uint8_t lun = 0; |
| 551 | uint8_t max_luns; |
| 552 | |
| 553 | dev = target_mmc_device(); |
| 554 | |
Sundarajan Srinivasan | 9786c45 | 2014-03-21 17:34:58 -0700 | [diff] [blame^] | 555 | if(!platform_boot_dev_isemmc()) |
Sundarajan Srinivasan | 15516f2 | 2013-12-04 17:11:36 -0800 | [diff] [blame] | 556 | { |
| 557 | max_luns = ufs_get_num_of_luns((struct ufs_dev*)dev); |
| 558 | |
| 559 | ASSERT(max_luns); |
| 560 | |
| 561 | for(lun = arg; lun < max_luns; lun++) |
| 562 | { |
| 563 | mmc_set_lun(lun); |
| 564 | |
| 565 | if(partition_read_table()) |
| 566 | { |
| 567 | dprintf(CRITICAL, "Error reading the partition table info for lun %d\n", lun); |
| 568 | } |
| 569 | } |
| 570 | } |
| 571 | else |
| 572 | { |
| 573 | if(partition_read_table()) |
| 574 | { |
| 575 | dprintf(CRITICAL, "Error reading the partition table info\n"); |
| 576 | } |
| 577 | } |
| 578 | } |