LCOV - code coverage report
Current view: top level - lkbce/drivers/mmc/core - mmc_ops.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 166 242 68.6 %
Date: 2017-01-25 Functions: 12 17 70.6 %

          Line data    Source code
       1             : /*
       2             :  *  linux/drivers/mmc/core/mmc_ops.h
       3             :  *
       4             :  *  Copyright 2006-2007 Pierre Ossman
       5             :  *
       6             :  * This program is free software; you can redistribute it and/or modify
       7             :  * it under the terms of the GNU General Public License as published by
       8             :  * the Free Software Foundation; either version 2 of the License, or (at
       9             :  * your option) any later version.
      10             :  */
      11             : 
      12             : #include <linux/types.h>
      13             : #include <linux/scatterlist.h>
      14             : 
      15             : #include <linux/mmc/host.h>
      16             : #include <linux/mmc/card.h>
      17             : #include <linux/mmc/mmc.h>
      18             : 
      19             : #include "core.h"
      20             : #include "mmc_ops.h"
      21             : 
      22             : static int _mmc_select_card(struct mmc_host *host, struct mmc_card *card)
      23             : {
      24          31 :         int err;
      25          31 :         struct mmc_command cmd;
      26          31 : 
      27         186 :         BUG_ON(!host);
      28             : 
      29          31 :         memset(&cmd, 0, sizeof(struct mmc_command));
      30             : 
      31          31 :         cmd.opcode = MMC_SELECT_CARD;
      32             : 
      33          62 :         if (card) {
      34          31 :                 cmd.arg = card->rca << 16;
      35          31 :                 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
      36             :         } else {
      37          31 :                 cmd.arg = 0;
      38          31 :                 cmd.flags = MMC_RSP_NONE | MMC_CMD_AC;
      39             :         }
      40             : 
      41          62 :         err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
      42          62 :         if (err)
      43          31 :                 return err;
      44             : 
      45          31 :         return 0;
      46             : }
      47             : 
      48             : int mmc_select_card(struct mmc_card *card)
      49             : {
      50         182 :         BUG_ON(!card);
      51          26 : 
      52          78 :         return _mmc_select_card(card->host, card);
      53             : }
      54             : 
      55             : int mmc_deselect_cards(struct mmc_host *host)
      56             : {
      57          20 :         return _mmc_select_card(host, NULL);
      58             : }
      59             : 
      60             : int mmc_card_sleepawake(struct mmc_host *host, int sleep)
      61             : {
      62           4 :         struct mmc_command cmd;
      63           8 :         struct mmc_card *card = host->card;
      64           4 :         int err;
      65             : 
      66           8 :         if (sleep)
      67           8 :                 mmc_deselect_cards(host);
      68             : 
      69           8 :         memset(&cmd, 0, sizeof(struct mmc_command));
      70             : 
      71           8 :         cmd.opcode = MMC_SLEEP_AWAKE;
      72           8 :         cmd.arg = card->rca << 16;
      73          16 :         if (sleep)
      74           8 :                 cmd.arg |= 1 << 15;
      75             : 
      76           8 :         cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
      77          16 :         err = mmc_wait_for_cmd(host, &cmd, 0);
      78           8 :         if (err)
      79           4 :                 return err;
      80             : 
      81             :         /*
      82             :          * If the host does not wait while the card signals busy, then we will
      83             :          * will have to wait the sleep/awake timeout.  Note, we cannot use the
      84             :          * SEND_STATUS command to poll the status because that command (and most
      85             :          * others) is invalid while the card sleeps.
      86             :          */
      87           8 :         if (!(host->caps & MMC_CAP_WAIT_WHILE_BUSY))
      88          12 :                 mmc_delay(DIV_ROUND_UP(card->ext_csd.sa_timeout, 10000));
      89             : 
      90          16 :         if (!sleep)
      91          16 :                 err = mmc_select_card(card);
      92             : 
      93          12 :         return err;
      94             : }
      95             : 
      96             : int mmc_go_idle(struct mmc_host *host)
      97             : {
      98           3 :         int err;
      99           3 :         struct mmc_command cmd;
     100             : 
     101             :         /*
     102             :          * Non-SPI hosts need to prevent chipselect going active during
     103             :          * GO_IDLE; that would put chips into SPI mode.  Remind them of
     104             :          * that in case of hardware that won't pull up DAT3/nCS otherwise.
     105             :          *
     106             :          * SPI hosts ignore ios.chip_select; it's managed according to
     107             :          * rules that must accomodate non-MMC slaves which this layer
     108             :          * won't even know about.
     109             :          */
     110           6 :         if (!mmc_host_is_spi(host)) {
     111           6 :                 mmc_set_chip_select(host, MMC_CS_HIGH);
     112           9 :                 mmc_delay(1);
     113             :         }
     114             : 
     115           6 :         memset(&cmd, 0, sizeof(struct mmc_command));
     116             : 
     117           6 :         cmd.opcode = MMC_GO_IDLE_STATE;
     118           6 :         cmd.arg = 0;
     119           6 :         cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_NONE | MMC_CMD_BC;
     120             : 
     121          12 :         err = mmc_wait_for_cmd(host, &cmd, 0);
     122             : 
     123           9 :         mmc_delay(1);
     124             : 
     125           6 :         if (!mmc_host_is_spi(host)) {
     126           6 :                 mmc_set_chip_select(host, MMC_CS_DONTCARE);
     127           9 :                 mmc_delay(1);
     128             :         }
     129             : 
     130           6 :         host->use_spi_crc = 0;
     131             : 
     132           6 :         return err;
     133             : }
     134             : 
     135             : int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
     136             : {
     137           0 :         struct mmc_command cmd;
     138           0 :         int i, err = 0;
     139           0 : 
     140           0 :         BUG_ON(!host);
     141             : 
     142           0 :         memset(&cmd, 0, sizeof(struct mmc_command));
     143             : 
     144           0 :         cmd.opcode = MMC_SEND_OP_COND;
     145           0 :         cmd.arg = mmc_host_is_spi(host) ? 0 : ocr;
     146           0 :         cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R3 | MMC_CMD_BCR;
     147             : 
     148           0 :         for (i = 100; i; i--) {
     149           0 :                 err = mmc_wait_for_cmd(host, &cmd, 0);
     150           0 :                 if (err)
     151           0 :                         break;
     152             : 
     153             :                 /* if we're just probing, do a single pass */
     154           0 :                 if (ocr == 0)
     155           0 :                         break;
     156             : 
     157             :                 /* otherwise wait until reset completes */
     158           0 :                 if (mmc_host_is_spi(host)) {
     159           0 :                         if (!(cmd.resp[0] & R1_SPI_IDLE))
     160           0 :                                 break;
     161             :                 } else {
     162           0 :                         if (cmd.resp[0] & MMC_CARD_BUSY)
     163           0 :                                 break;
     164             :                 }
     165             : 
     166           0 :                 err = -ETIMEDOUT;
     167             : 
     168           0 :                 mmc_delay(10);
     169             :         }
     170             : 
     171           0 :         if (rocr && !mmc_host_is_spi(host))
     172           0 :                 *rocr = cmd.resp[0];
     173             : 
     174           0 :         return err;
     175             : }
     176             : 
     177             : int mmc_all_send_cid(struct mmc_host *host, u32 *cid)
     178             : {
     179           3 :         int err;
     180           3 :         struct mmc_command cmd;
     181           3 : 
     182          21 :         BUG_ON(!host);
     183          18 :         BUG_ON(!cid);
     184             : 
     185           3 :         memset(&cmd, 0, sizeof(struct mmc_command));
     186             : 
     187           3 :         cmd.opcode = MMC_ALL_SEND_CID;
     188           3 :         cmd.arg = 0;
     189           3 :         cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
     190             : 
     191           6 :         err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
     192           6 :         if (err)
     193           3 :                 return err;
     194             : 
     195           3 :         memcpy(cid, cmd.resp, sizeof(u32) * 4);
     196             : 
     197           3 :         return 0;
     198             : }
     199             : 
     200             : int mmc_set_relative_addr(struct mmc_card *card)
     201             : {
     202           0 :         int err;
     203           0 :         struct mmc_command cmd;
     204           0 : 
     205           0 :         BUG_ON(!card);
     206           0 :         BUG_ON(!card->host);
     207             : 
     208           0 :         memset(&cmd, 0, sizeof(struct mmc_command));
     209             : 
     210           0 :         cmd.opcode = MMC_SET_RELATIVE_ADDR;
     211           0 :         cmd.arg = card->rca << 16;
     212           0 :         cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
     213             : 
     214           0 :         err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
     215           0 :         if (err)
     216           0 :                 return err;
     217             : 
     218           0 :         return 0;
     219             : }
     220             : 
     221             : static int
     222             : mmc_send_cxd_native(struct mmc_host *host, u32 arg, u32 *cxd, int opcode)
     223             : {
     224          15 :         int err;
     225          15 :         struct mmc_command cmd;
     226          15 : 
     227         105 :         BUG_ON(!host);
     228          90 :         BUG_ON(!cxd);
     229             : 
     230          15 :         memset(&cmd, 0, sizeof(struct mmc_command));
     231             : 
     232          15 :         cmd.opcode = opcode;
     233          15 :         cmd.arg = arg;
     234          15 :         cmd.flags = MMC_RSP_R2 | MMC_CMD_AC;
     235             : 
     236          30 :         err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
     237          30 :         if (err)
     238          15 :                 return err;
     239             : 
     240          15 :         memcpy(cxd, cmd.resp, sizeof(u32) * 4);
     241             : 
     242          15 :         return 0;
     243             : }
     244             : 
     245             : static int
     246             : mmc_send_cxd_data(struct mmc_card *card, struct mmc_host *host,
     247             :                 u32 opcode, void *buf, unsigned len)
     248             : {
     249          15 :         struct mmc_request mrq;
     250          15 :         struct mmc_command cmd;
     251          15 :         struct mmc_data data;
     252          15 :         struct scatterlist sg;
     253          15 :         void *data_buf;
     254             : 
     255             :         /* dma onto stack is unsafe/nonportable, but callers to this
     256             :          * routine normally provide temporary on-stack buffers ...
     257             :          */
     258          30 :         data_buf = kmalloc(len, GFP_KERNEL);
     259          30 :         if (data_buf == NULL)
     260          15 :                 return -ENOMEM;
     261             : 
     262          15 :         memset(&mrq, 0, sizeof(struct mmc_request));
     263          15 :         memset(&cmd, 0, sizeof(struct mmc_command));
     264          15 :         memset(&data, 0, sizeof(struct mmc_data));
     265             : 
     266          15 :         mrq.cmd = &cmd;
     267          15 :         mrq.data = &data;
     268             : 
     269          15 :         cmd.opcode = opcode;
     270          15 :         cmd.arg = 0;
     271             : 
     272             :         /* NOTE HACK:  the MMC_RSP_SPI_R1 is always correct here, but we
     273             :          * rely on callers to never use this with "native" calls for reading
     274             :          * CSD or CID.  Native versions of those commands use the R2 type,
     275             :          * not R1 plus a data block.
     276             :          */
     277          15 :         cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
     278             : 
     279          15 :         data.blksz = len;
     280          15 :         data.blocks = 1;
     281          15 :         data.flags = MMC_DATA_READ;
     282          15 :         data.sg = &sg;
     283          15 :         data.sg_len = 1;
     284             : 
     285          15 :         sg_init_one(&sg, data_buf, len);
     286             : 
     287          30 :         if (opcode == MMC_SEND_CSD || opcode == MMC_SEND_CID) {
     288             :                 /*
     289             :                  * The spec states that CSR and CID accesses have a timeout
     290             :                  * of 64 clock cycles.
     291             :                  */
     292          15 :                 data.timeout_ns = 0;
     293          15 :                 data.timeout_clks = 64;
     294             :         } else
     295          30 :                 mmc_set_data_timeout(&data, card);
     296             : 
     297          60 :         mmc_wait_for_req(host, &mrq);
     298             : 
     299          15 :         memcpy(buf, data_buf, len);
     300          15 :         kfree(data_buf);
     301             : 
     302          30 :         if (cmd.error)
     303          15 :                 return cmd.error;
     304          30 :         if (data.error)
     305          15 :                 return data.error;
     306             : 
     307          15 :         return 0;
     308             : }
     309             : 
     310             : int mmc_send_csd(struct mmc_card *card, u32 *csd)
     311             : {
     312          12 :         int ret, i;
     313          12 : 
     314          36 :         if (!mmc_host_is_spi(card->host))
     315          48 :                 return mmc_send_cxd_native(card->host, card->rca << 16,
     316             :                                 csd, MMC_SEND_CSD);
     317             : 
     318          36 :         ret = mmc_send_cxd_data(card, card->host, MMC_SEND_CSD, csd, 16);
     319          24 :         if (ret)
     320          12 :                 return ret;
     321             : 
     322          60 :         for (i = 0;i < 4;i++)
     323          48 :                 csd[i] = be32_to_cpu(csd[i]);
     324          24 : 
     325          12 :         return 0;
     326             : }
     327             : 
     328             : int mmc_send_cid(struct mmc_host *host, u32 *cid)
     329             : {
     330           3 :         int ret, i;
     331           3 : 
     332           9 :         if (!mmc_host_is_spi(host)) {
     333          12 :                 if (!host->card)
     334           3 :                         return -EINVAL;
     335           9 :                 return mmc_send_cxd_native(host, host->card->rca << 16,
     336             :                                 cid, MMC_SEND_CID);
     337             :         }
     338             : 
     339           9 :         ret = mmc_send_cxd_data(NULL, host, MMC_SEND_CID, cid, 16);
     340           6 :         if (ret)
     341           3 :                 return ret;
     342             : 
     343          15 :         for (i = 0;i < 4;i++)
     344          12 :                 cid[i] = be32_to_cpu(cid[i]);
     345           6 : 
     346           3 :         return 0;
     347             : }
     348             : 
     349             : int mmc_send_ext_csd(struct mmc_card *card, u8 *ext_csd)
     350             : {
     351           0 :         return mmc_send_cxd_data(card, card->host, MMC_SEND_EXT_CSD,
     352             :                         ext_csd, 512);
     353             : }
     354             : 
     355             : int mmc_spi_read_ocr(struct mmc_host *host, int highcap, u32 *ocrp)
     356             : {
     357           0 :         struct mmc_command cmd;
     358           0 :         int err;
     359             : 
     360           0 :         memset(&cmd, 0, sizeof(struct mmc_command));
     361             : 
     362           0 :         cmd.opcode = MMC_SPI_READ_OCR;
     363           0 :         cmd.arg = highcap ? (1 << 30) : 0;
     364           0 :         cmd.flags = MMC_RSP_SPI_R3;
     365             : 
     366           0 :         err = mmc_wait_for_cmd(host, &cmd, 0);
     367             : 
     368           0 :         *ocrp = cmd.resp[1];
     369           0 :         return err;
     370             : }
     371             : 
     372             : int mmc_spi_set_crc(struct mmc_host *host, int use_crc)
     373             : {
     374          22 :         struct mmc_command cmd;
     375          22 :         int err;
     376             : 
     377          22 :         memset(&cmd, 0, sizeof(struct mmc_command));
     378             : 
     379          22 :         cmd.opcode = MMC_SPI_CRC_ON_OFF;
     380          22 :         cmd.flags = MMC_RSP_SPI_R1;
     381          22 :         cmd.arg = use_crc;
     382             : 
     383          44 :         err = mmc_wait_for_cmd(host, &cmd, 0);
     384          44 :         if (!err)
     385          22 :                 host->use_spi_crc = use_crc;
     386          22 :         return err;
     387             : }
     388             : 
     389             : int mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value)
     390             : {
     391           0 :         int err;
     392           0 :         struct mmc_command cmd;
     393           0 :         u32 status;
     394           0 : 
     395           0 :         BUG_ON(!card);
     396           0 :         BUG_ON(!card->host);
     397             : 
     398           0 :         memset(&cmd, 0, sizeof(struct mmc_command));
     399             : 
     400           0 :         cmd.opcode = MMC_SWITCH;
     401           0 :         cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
     402             :                   (index << 16) |
     403             :                   (value << 8) |
     404             :                   set;
     405           0 :         cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
     406             : 
     407           0 :         err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
     408           0 :         if (err)
     409           0 :                 return err;
     410             : 
     411             :         /* Must check status to be sure of no errors */
     412             :         do {
     413           0 :                 err = mmc_send_status(card, &status);
     414           0 :                 if (err)
     415           0 :                         return err;
     416           0 :                 if (card->host->caps & MMC_CAP_WAIT_WHILE_BUSY)
     417           0 :                         break;
     418           0 :                 if (mmc_host_is_spi(card->host))
     419           0 :                         break;
     420           0 :         } while (R1_CURRENT_STATE(status) == 7);
     421             : 
     422           0 :         if (mmc_host_is_spi(card->host)) {
     423           0 :                 if (status & R1_SPI_ILLEGAL_COMMAND)
     424           0 :                         return -EBADMSG;
     425             :         } else {
     426           0 :                 if (status & 0xFDFFA000)
     427           0 :                         printk(KERN_WARNING "%s: unexpected status %#x after "
     428             :                                "switch", mmc_hostname(card->host), status);
     429           0 :                 if (status & R1_SWITCH_ERROR)
     430           0 :                         return -EBADMSG;
     431             :         }
     432             : 
     433           0 :         return 0;
     434             : }
     435             : 
     436             : int mmc_send_status(struct mmc_card *card, u32 *status)
     437             : {
     438           1 :         int err;
     439           1 :         struct mmc_command cmd;
     440           1 : 
     441           7 :         BUG_ON(!card);
     442           7 :         BUG_ON(!card->host);
     443             : 
     444           1 :         memset(&cmd, 0, sizeof(struct mmc_command));
     445             : 
     446           1 :         cmd.opcode = MMC_SEND_STATUS;
     447           2 :         if (!mmc_host_is_spi(card->host))
     448           1 :                 cmd.arg = card->rca << 16;
     449           1 :         cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
     450             : 
     451           2 :         err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
     452           2 :         if (err)
     453           1 :                 return err;
     454             : 
     455             :         /* NOTE: callers are required to understand the difference
     456             :          * between "native" and SPI format status words!
     457             :          */
     458           2 :         if (status)
     459           1 :                 *status = cmd.resp[0];
     460             : 
     461           1 :         return 0;
     462             : }
     463             : 

Generated by: LCOV version 1.10