Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

mmc: block: Implement card_busy_detect() for busy detection

To complete a data write request we poll for the card's status register
by sending CMD13. The are other scenarios when this polling method are
needed, which is why we here moves this code to it's own function. No
functional change.

Signed-off-by: Ulf Hansson <ulf.hansson@linaro.org>
Signed-off-by: Chris Ball <chris@printf.net>

authored by

Ulf Hansson and committed by
Chris Ball
c49433fb bcc3e172

+47 -35
+47 -35
drivers/mmc/card/block.c
··· 749 749 return err; 750 750 } 751 751 752 + static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms, 753 + struct request *req, int *gen_err) 754 + { 755 + unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms); 756 + int err = 0; 757 + u32 status; 758 + 759 + do { 760 + err = get_card_status(card, &status, 5); 761 + if (err) { 762 + pr_err("%s: error %d requesting status\n", 763 + req->rq_disk->disk_name, err); 764 + return err; 765 + } 766 + 767 + if (status & R1_ERROR) { 768 + pr_err("%s: %s: error sending status cmd, status %#x\n", 769 + req->rq_disk->disk_name, __func__, status); 770 + *gen_err = 1; 771 + } 772 + 773 + /* 774 + * Timeout if the device never becomes ready for data and never 775 + * leaves the program state. 776 + */ 777 + if (time_after(jiffies, timeout)) { 778 + pr_err("%s: Card stuck in programming state! %s %s\n", 779 + mmc_hostname(card->host), 780 + req->rq_disk->disk_name, __func__); 781 + return -ETIMEDOUT; 782 + } 783 + 784 + /* 785 + * Some cards mishandle the status bits, 786 + * so make sure to check both the busy 787 + * indication and the card state. 788 + */ 789 + } while (!(status & R1_READY_FOR_DATA) || 790 + (R1_CURRENT_STATE(status) == R1_STATE_PRG)); 791 + 792 + return err; 793 + } 794 + 752 795 #define ERR_NOMEDIUM 3 753 796 #define ERR_RETRY 2 754 797 #define ERR_ABORT 1 ··· 1199 1156 * program mode, which we have to wait for it to complete. 1200 1157 */ 1201 1158 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) { 1202 - u32 status; 1203 - unsigned long timeout; 1159 + int err; 1204 1160 1205 1161 /* Check stop command response */ 1206 1162 if (brq->stop.resp[0] & R1_ERROR) { ··· 1209 1167 gen_err = 1; 1210 1168 } 1211 1169 1212 - timeout = jiffies + msecs_to_jiffies(MMC_BLK_TIMEOUT_MS); 1213 - do { 1214 - int err = get_card_status(card, &status, 5); 1215 - if (err) { 1216 - pr_err("%s: error %d requesting status\n", 1217 - req->rq_disk->disk_name, err); 1218 - return MMC_BLK_CMD_ERR; 1219 - } 1220 - 1221 - if (status & R1_ERROR) { 1222 - pr_err("%s: %s: general error sending status command, card status %#x\n", 1223 - req->rq_disk->disk_name, __func__, 1224 - status); 1225 - gen_err = 1; 1226 - } 1227 - 1228 - /* Timeout if the device never becomes ready for data 1229 - * and never leaves the program state. 1230 - */ 1231 - if (time_after(jiffies, timeout)) { 1232 - pr_err("%s: Card stuck in programming state!"\ 1233 - " %s %s\n", mmc_hostname(card->host), 1234 - req->rq_disk->disk_name, __func__); 1235 - 1236 - return MMC_BLK_CMD_ERR; 1237 - } 1238 - /* 1239 - * Some cards mishandle the status bits, 1240 - * so make sure to check both the busy 1241 - * indication and the card state. 1242 - */ 1243 - } while (!(status & R1_READY_FOR_DATA) || 1244 - (R1_CURRENT_STATE(status) == R1_STATE_PRG)); 1170 + err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, req, &gen_err); 1171 + if (err) 1172 + return MMC_BLK_CMD_ERR; 1245 1173 } 1246 1174 1247 1175 /* if general error occurs, retry the write operation. */