1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+ 2da61fa5fSPaul Burton /* 3da61fa5fSPaul Burton * Copyright 2008, Freescale Semiconductor, Inc 4da61fa5fSPaul Burton * Andy Fleming 5da61fa5fSPaul Burton * 6da61fa5fSPaul Burton * Based vaguely on the Linux code 7da61fa5fSPaul Burton */ 8da61fa5fSPaul Burton 9da61fa5fSPaul Burton #include <config.h> 10da61fa5fSPaul Burton #include <common.h> 1133fb211dSSimon Glass #include <dm.h> 12da61fa5fSPaul Burton #include <part.h> 13180f87fcSTom Rini #include <div64.h> 14180f87fcSTom Rini #include <linux/math64.h> 15da61fa5fSPaul Burton #include "mmc_private.h" 16da61fa5fSPaul Burton 17da61fa5fSPaul Burton static ulong mmc_erase_t(struct mmc *mmc, ulong start, lbaint_t blkcnt) 18da61fa5fSPaul Burton { 19da61fa5fSPaul Burton struct mmc_cmd cmd; 20da61fa5fSPaul Burton ulong end; 21da61fa5fSPaul Burton int err, start_cmd, end_cmd; 22da61fa5fSPaul Burton 23da61fa5fSPaul Burton if (mmc->high_capacity) { 24da61fa5fSPaul Burton end = start + blkcnt - 1; 25da61fa5fSPaul Burton } else { 26da61fa5fSPaul Burton end = (start + blkcnt - 1) * mmc->write_bl_len; 27da61fa5fSPaul Burton start *= mmc->write_bl_len; 28da61fa5fSPaul Burton } 29da61fa5fSPaul Burton 30da61fa5fSPaul Burton if (IS_SD(mmc)) { 31da61fa5fSPaul Burton start_cmd = SD_CMD_ERASE_WR_BLK_START; 32da61fa5fSPaul Burton end_cmd = SD_CMD_ERASE_WR_BLK_END; 33da61fa5fSPaul Burton } else { 34da61fa5fSPaul Burton start_cmd = MMC_CMD_ERASE_GROUP_START; 35da61fa5fSPaul Burton end_cmd = MMC_CMD_ERASE_GROUP_END; 36da61fa5fSPaul Burton } 37da61fa5fSPaul Burton 38da61fa5fSPaul Burton cmd.cmdidx = start_cmd; 39da61fa5fSPaul Burton cmd.cmdarg = start; 40da61fa5fSPaul Burton cmd.resp_type = MMC_RSP_R1; 41da61fa5fSPaul Burton 42da61fa5fSPaul Burton err = mmc_send_cmd(mmc, &cmd, NULL); 43da61fa5fSPaul Burton if (err) 44da61fa5fSPaul Burton goto err_out; 45da61fa5fSPaul Burton 46da61fa5fSPaul Burton cmd.cmdidx = end_cmd; 47da61fa5fSPaul Burton cmd.cmdarg = end; 48da61fa5fSPaul Burton 49da61fa5fSPaul Burton err = mmc_send_cmd(mmc, &cmd, NULL); 50da61fa5fSPaul Burton if (err) 51da61fa5fSPaul Burton goto err_out; 52da61fa5fSPaul Burton 53da61fa5fSPaul Burton cmd.cmdidx = MMC_CMD_ERASE; 541aa2d074SEric Nelson cmd.cmdarg = MMC_ERASE_ARG; 55da61fa5fSPaul Burton cmd.resp_type = MMC_RSP_R1b; 56da61fa5fSPaul Burton 57da61fa5fSPaul Burton err = mmc_send_cmd(mmc, &cmd, NULL); 58da61fa5fSPaul Burton if (err) 59da61fa5fSPaul Burton goto err_out; 60da61fa5fSPaul Burton 61da61fa5fSPaul Burton return 0; 62da61fa5fSPaul Burton 63da61fa5fSPaul Burton err_out: 64da61fa5fSPaul Burton puts("mmc erase failed\n"); 65da61fa5fSPaul Burton return err; 66da61fa5fSPaul Burton } 67da61fa5fSPaul Burton 68561e624cSSimon Glass #ifdef CONFIG_BLK 69561e624cSSimon Glass ulong mmc_berase(struct udevice *dev, lbaint_t start, lbaint_t blkcnt) 70561e624cSSimon Glass #else 71561e624cSSimon Glass ulong mmc_berase(struct blk_desc *block_dev, lbaint_t start, lbaint_t blkcnt) 72561e624cSSimon Glass #endif 73da61fa5fSPaul Burton { 74561e624cSSimon Glass #ifdef CONFIG_BLK 75561e624cSSimon Glass struct blk_desc *block_dev = dev_get_uclass_platdata(dev); 76561e624cSSimon Glass #endif 77bcce53d0SSimon Glass int dev_num = block_dev->devnum; 78da61fa5fSPaul Burton int err = 0; 79180f87fcSTom Rini u32 start_rem, blkcnt_rem; 80da61fa5fSPaul Burton struct mmc *mmc = find_mmc_device(dev_num); 81da61fa5fSPaul Burton lbaint_t blk = 0, blk_r = 0; 82da61fa5fSPaul Burton int timeout = 1000; 83da61fa5fSPaul Burton 84da61fa5fSPaul Burton if (!mmc) 85da61fa5fSPaul Burton return -1; 86da61fa5fSPaul Burton 8769f45cd5SSimon Glass err = blk_select_hwpart_devnum(IF_TYPE_MMC, dev_num, 8869f45cd5SSimon Glass block_dev->hwpart); 89873cc1d7SStephen Warren if (err < 0) 90873cc1d7SStephen Warren return -1; 91873cc1d7SStephen Warren 92180f87fcSTom Rini /* 93180f87fcSTom Rini * We want to see if the requested start or total block count are 94180f87fcSTom Rini * unaligned. We discard the whole numbers and only care about the 95180f87fcSTom Rini * remainder. 96180f87fcSTom Rini */ 97180f87fcSTom Rini err = div_u64_rem(start, mmc->erase_grp_size, &start_rem); 98180f87fcSTom Rini err = div_u64_rem(blkcnt, mmc->erase_grp_size, &blkcnt_rem); 99180f87fcSTom Rini if (start_rem || blkcnt_rem) 100da61fa5fSPaul Burton printf("\n\nCaution! Your devices Erase group is 0x%x\n" 101da61fa5fSPaul Burton "The erase range would be change to " 102da61fa5fSPaul Burton "0x" LBAF "~0x" LBAF "\n\n", 103da61fa5fSPaul Burton mmc->erase_grp_size, start & ~(mmc->erase_grp_size - 1), 104da61fa5fSPaul Burton ((start + blkcnt + mmc->erase_grp_size) 105da61fa5fSPaul Burton & ~(mmc->erase_grp_size - 1)) - 1); 106da61fa5fSPaul Burton 107da61fa5fSPaul Burton while (blk < blkcnt) { 108e492dbb4SPeng Fan if (IS_SD(mmc) && mmc->ssr.au) { 109e492dbb4SPeng Fan blk_r = ((blkcnt - blk) > mmc->ssr.au) ? 110e492dbb4SPeng Fan mmc->ssr.au : (blkcnt - blk); 111e492dbb4SPeng Fan } else { 112da61fa5fSPaul Burton blk_r = ((blkcnt - blk) > mmc->erase_grp_size) ? 113da61fa5fSPaul Burton mmc->erase_grp_size : (blkcnt - blk); 114e492dbb4SPeng Fan } 115da61fa5fSPaul Burton err = mmc_erase_t(mmc, start + blk, blk_r); 116da61fa5fSPaul Burton if (err) 117da61fa5fSPaul Burton break; 118da61fa5fSPaul Burton 119da61fa5fSPaul Burton blk += blk_r; 120da61fa5fSPaul Burton 121da61fa5fSPaul Burton /* Waiting for the ready status */ 122da61fa5fSPaul Burton if (mmc_send_status(mmc, timeout)) 123da61fa5fSPaul Burton return 0; 124da61fa5fSPaul Burton } 125da61fa5fSPaul Burton 126da61fa5fSPaul Burton return blk; 127da61fa5fSPaul Burton } 128da61fa5fSPaul Burton 129da61fa5fSPaul Burton static ulong mmc_write_blocks(struct mmc *mmc, lbaint_t start, 130da61fa5fSPaul Burton lbaint_t blkcnt, const void *src) 131da61fa5fSPaul Burton { 132da61fa5fSPaul Burton struct mmc_cmd cmd; 133da61fa5fSPaul Burton struct mmc_data data; 134da61fa5fSPaul Burton int timeout = 1000; 135da61fa5fSPaul Burton 136c40fdca6SSimon Glass if ((start + blkcnt) > mmc_get_blk_desc(mmc)->lba) { 137da61fa5fSPaul Burton printf("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n", 138c40fdca6SSimon Glass start + blkcnt, mmc_get_blk_desc(mmc)->lba); 139da61fa5fSPaul Burton return 0; 140da61fa5fSPaul Burton } 141da61fa5fSPaul Burton 142da61fa5fSPaul Burton if (blkcnt == 0) 143da61fa5fSPaul Burton return 0; 144da61fa5fSPaul Burton else if (blkcnt == 1) 145da61fa5fSPaul Burton cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK; 146da61fa5fSPaul Burton else 147da61fa5fSPaul Burton cmd.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK; 148da61fa5fSPaul Burton 149da61fa5fSPaul Burton if (mmc->high_capacity) 150da61fa5fSPaul Burton cmd.cmdarg = start; 151da61fa5fSPaul Burton else 152da61fa5fSPaul Burton cmd.cmdarg = start * mmc->write_bl_len; 153da61fa5fSPaul Burton 154da61fa5fSPaul Burton cmd.resp_type = MMC_RSP_R1; 155da61fa5fSPaul Burton 156da61fa5fSPaul Burton data.src = src; 157da61fa5fSPaul Burton data.blocks = blkcnt; 158da61fa5fSPaul Burton data.blocksize = mmc->write_bl_len; 159da61fa5fSPaul Burton data.flags = MMC_DATA_WRITE; 160da61fa5fSPaul Burton 161da61fa5fSPaul Burton if (mmc_send_cmd(mmc, &cmd, &data)) { 162da61fa5fSPaul Burton printf("mmc write failed\n"); 163da61fa5fSPaul Burton return 0; 164da61fa5fSPaul Burton } 165da61fa5fSPaul Burton 166da61fa5fSPaul Burton /* SPI multiblock writes terminate using a special 167da61fa5fSPaul Burton * token, not a STOP_TRANSMISSION request. 168da61fa5fSPaul Burton */ 169da61fa5fSPaul Burton if (!mmc_host_is_spi(mmc) && blkcnt > 1) { 170da61fa5fSPaul Burton cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION; 171da61fa5fSPaul Burton cmd.cmdarg = 0; 172da61fa5fSPaul Burton cmd.resp_type = MMC_RSP_R1b; 173da61fa5fSPaul Burton if (mmc_send_cmd(mmc, &cmd, NULL)) { 174da61fa5fSPaul Burton printf("mmc fail to send stop cmd\n"); 175da61fa5fSPaul Burton return 0; 176da61fa5fSPaul Burton } 177da61fa5fSPaul Burton } 178da61fa5fSPaul Burton 179da61fa5fSPaul Burton /* Waiting for the ready status */ 180da61fa5fSPaul Burton if (mmc_send_status(mmc, timeout)) 181da61fa5fSPaul Burton return 0; 182da61fa5fSPaul Burton 183da61fa5fSPaul Burton return blkcnt; 184da61fa5fSPaul Burton } 185da61fa5fSPaul Burton 18633fb211dSSimon Glass #ifdef CONFIG_BLK 18733fb211dSSimon Glass ulong mmc_bwrite(struct udevice *dev, lbaint_t start, lbaint_t blkcnt, 18833fb211dSSimon Glass const void *src) 18933fb211dSSimon Glass #else 1904101f687SSimon Glass ulong mmc_bwrite(struct blk_desc *block_dev, lbaint_t start, lbaint_t blkcnt, 1917c4213f6SStephen Warren const void *src) 19233fb211dSSimon Glass #endif 193da61fa5fSPaul Burton { 19433fb211dSSimon Glass #ifdef CONFIG_BLK 19533fb211dSSimon Glass struct blk_desc *block_dev = dev_get_uclass_platdata(dev); 19633fb211dSSimon Glass #endif 197bcce53d0SSimon Glass int dev_num = block_dev->devnum; 198da61fa5fSPaul Burton lbaint_t cur, blocks_todo = blkcnt; 199873cc1d7SStephen Warren int err; 200da61fa5fSPaul Burton 201da61fa5fSPaul Burton struct mmc *mmc = find_mmc_device(dev_num); 202da61fa5fSPaul Burton if (!mmc) 203da61fa5fSPaul Burton return 0; 204da61fa5fSPaul Burton 20569f45cd5SSimon Glass err = blk_select_hwpart_devnum(IF_TYPE_MMC, dev_num, block_dev->hwpart); 206873cc1d7SStephen Warren if (err < 0) 207873cc1d7SStephen Warren return 0; 208873cc1d7SStephen Warren 209da61fa5fSPaul Burton if (mmc_set_blocklen(mmc, mmc->write_bl_len)) 210da61fa5fSPaul Burton return 0; 211da61fa5fSPaul Burton 212da61fa5fSPaul Burton do { 21393bfd616SPantelis Antoniou cur = (blocks_todo > mmc->cfg->b_max) ? 21493bfd616SPantelis Antoniou mmc->cfg->b_max : blocks_todo; 215da61fa5fSPaul Burton if (mmc_write_blocks(mmc, start, cur, src) != cur) 216da61fa5fSPaul Burton return 0; 217da61fa5fSPaul Burton blocks_todo -= cur; 218da61fa5fSPaul Burton start += cur; 219da61fa5fSPaul Burton src += cur * mmc->write_bl_len; 220da61fa5fSPaul Burton } while (blocks_todo > 0); 221da61fa5fSPaul Burton 222da61fa5fSPaul Burton return blkcnt; 223da61fa5fSPaul Burton } 224