diff options
Diffstat (limited to 'drivers/mmc/host/mmc_spi.c')
-rw-r--r-- | drivers/mmc/host/mmc_spi.c | 188 |
1 files changed, 133 insertions, 55 deletions
diff --git a/drivers/mmc/host/mmc_spi.c b/drivers/mmc/host/mmc_spi.c index 72f8bde4877a..f48349d18c92 100644 --- a/drivers/mmc/host/mmc_spi.c +++ b/drivers/mmc/host/mmc_spi.c | |||
@@ -24,7 +24,7 @@ | |||
24 | * along with this program; if not, write to the Free Software | 24 | * along with this program; if not, write to the Free Software |
25 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | 25 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
26 | */ | 26 | */ |
27 | #include <linux/hrtimer.h> | 27 | #include <linux/sched.h> |
28 | #include <linux/delay.h> | 28 | #include <linux/delay.h> |
29 | #include <linux/bio.h> | 29 | #include <linux/bio.h> |
30 | #include <linux/dma-mapping.h> | 30 | #include <linux/dma-mapping.h> |
@@ -95,7 +95,7 @@ | |||
95 | * reads which takes nowhere near that long. Older cards may be able to use | 95 | * reads which takes nowhere near that long. Older cards may be able to use |
96 | * shorter timeouts ... but why bother? | 96 | * shorter timeouts ... but why bother? |
97 | */ | 97 | */ |
98 | #define r1b_timeout ktime_set(3, 0) | 98 | #define r1b_timeout (HZ * 3) |
99 | 99 | ||
100 | 100 | ||
101 | /****************************************************************************/ | 101 | /****************************************************************************/ |
@@ -183,12 +183,11 @@ mmc_spi_readbytes(struct mmc_spi_host *host, unsigned len) | |||
183 | return status; | 183 | return status; |
184 | } | 184 | } |
185 | 185 | ||
186 | static int | 186 | static int mmc_spi_skip(struct mmc_spi_host *host, unsigned long timeout, |
187 | mmc_spi_skip(struct mmc_spi_host *host, ktime_t timeout, unsigned n, u8 byte) | 187 | unsigned n, u8 byte) |
188 | { | 188 | { |
189 | u8 *cp = host->data->status; | 189 | u8 *cp = host->data->status; |
190 | 190 | unsigned long start = jiffies; | |
191 | timeout = ktime_add(timeout, ktime_get()); | ||
192 | 191 | ||
193 | while (1) { | 192 | while (1) { |
194 | int status; | 193 | int status; |
@@ -203,22 +202,26 @@ mmc_spi_skip(struct mmc_spi_host *host, ktime_t timeout, unsigned n, u8 byte) | |||
203 | return cp[i]; | 202 | return cp[i]; |
204 | } | 203 | } |
205 | 204 | ||
206 | /* REVISIT investigate msleep() to avoid busy-wait I/O | 205 | if (time_is_before_jiffies(start + timeout)) |
207 | * in at least some cases. | ||
208 | */ | ||
209 | if (ktime_to_ns(ktime_sub(ktime_get(), timeout)) > 0) | ||
210 | break; | 206 | break; |
207 | |||
208 | /* If we need long timeouts, we may release the CPU. | ||
209 | * We use jiffies here because we want to have a relation | ||
210 | * between elapsed time and the blocking of the scheduler. | ||
211 | */ | ||
212 | if (time_is_before_jiffies(start+1)) | ||
213 | schedule(); | ||
211 | } | 214 | } |
212 | return -ETIMEDOUT; | 215 | return -ETIMEDOUT; |
213 | } | 216 | } |
214 | 217 | ||
215 | static inline int | 218 | static inline int |
216 | mmc_spi_wait_unbusy(struct mmc_spi_host *host, ktime_t timeout) | 219 | mmc_spi_wait_unbusy(struct mmc_spi_host *host, unsigned long timeout) |
217 | { | 220 | { |
218 | return mmc_spi_skip(host, timeout, sizeof(host->data->status), 0); | 221 | return mmc_spi_skip(host, timeout, sizeof(host->data->status), 0); |
219 | } | 222 | } |
220 | 223 | ||
221 | static int mmc_spi_readtoken(struct mmc_spi_host *host, ktime_t timeout) | 224 | static int mmc_spi_readtoken(struct mmc_spi_host *host, unsigned long timeout) |
222 | { | 225 | { |
223 | return mmc_spi_skip(host, timeout, 1, 0xff); | 226 | return mmc_spi_skip(host, timeout, 1, 0xff); |
224 | } | 227 | } |
@@ -251,6 +254,10 @@ static int mmc_spi_response_get(struct mmc_spi_host *host, | |||
251 | u8 *cp = host->data->status; | 254 | u8 *cp = host->data->status; |
252 | u8 *end = cp + host->t.len; | 255 | u8 *end = cp + host->t.len; |
253 | int value = 0; | 256 | int value = 0; |
257 | int bitshift; | ||
258 | u8 leftover = 0; | ||
259 | unsigned short rotator; | ||
260 | int i; | ||
254 | char tag[32]; | 261 | char tag[32]; |
255 | 262 | ||
256 | snprintf(tag, sizeof(tag), " ... CMD%d response SPI_%s", | 263 | snprintf(tag, sizeof(tag), " ... CMD%d response SPI_%s", |
@@ -268,9 +275,8 @@ static int mmc_spi_response_get(struct mmc_spi_host *host, | |||
268 | 275 | ||
269 | /* Data block reads (R1 response types) may need more data... */ | 276 | /* Data block reads (R1 response types) may need more data... */ |
270 | if (cp == end) { | 277 | if (cp == end) { |
271 | unsigned i; | ||
272 | |||
273 | cp = host->data->status; | 278 | cp = host->data->status; |
279 | end = cp+1; | ||
274 | 280 | ||
275 | /* Card sends N(CR) (== 1..8) bytes of all-ones then one | 281 | /* Card sends N(CR) (== 1..8) bytes of all-ones then one |
276 | * status byte ... and we already scanned 2 bytes. | 282 | * status byte ... and we already scanned 2 bytes. |
@@ -295,20 +301,34 @@ static int mmc_spi_response_get(struct mmc_spi_host *host, | |||
295 | } | 301 | } |
296 | 302 | ||
297 | checkstatus: | 303 | checkstatus: |
298 | if (*cp & 0x80) { | 304 | bitshift = 0; |
299 | dev_dbg(&host->spi->dev, "%s: INVALID RESPONSE, %02x\n", | 305 | if (*cp & 0x80) { |
300 | tag, *cp); | 306 | /* Houston, we have an ugly card with a bit-shifted response */ |
301 | value = -EBADR; | 307 | rotator = *cp++ << 8; |
302 | goto done; | 308 | /* read the next byte */ |
309 | if (cp == end) { | ||
310 | value = mmc_spi_readbytes(host, 1); | ||
311 | if (value < 0) | ||
312 | goto done; | ||
313 | cp = host->data->status; | ||
314 | end = cp+1; | ||
315 | } | ||
316 | rotator |= *cp++; | ||
317 | while (rotator & 0x8000) { | ||
318 | bitshift++; | ||
319 | rotator <<= 1; | ||
320 | } | ||
321 | cmd->resp[0] = rotator >> 8; | ||
322 | leftover = rotator; | ||
323 | } else { | ||
324 | cmd->resp[0] = *cp++; | ||
303 | } | 325 | } |
304 | |||
305 | cmd->resp[0] = *cp++; | ||
306 | cmd->error = 0; | 326 | cmd->error = 0; |
307 | 327 | ||
308 | /* Status byte: the entire seven-bit R1 response. */ | 328 | /* Status byte: the entire seven-bit R1 response. */ |
309 | if (cmd->resp[0] != 0) { | 329 | if (cmd->resp[0] != 0) { |
310 | if ((R1_SPI_PARAMETER | R1_SPI_ADDRESS | 330 | if ((R1_SPI_PARAMETER | R1_SPI_ADDRESS |
311 | | R1_SPI_ILLEGAL_COMMAND) | 331 | | R1_SPI_ILLEGAL_COMMAND) |
312 | & cmd->resp[0]) | 332 | & cmd->resp[0]) |
313 | value = -EINVAL; | 333 | value = -EINVAL; |
314 | else if (R1_SPI_COM_CRC & cmd->resp[0]) | 334 | else if (R1_SPI_COM_CRC & cmd->resp[0]) |
@@ -336,12 +356,45 @@ checkstatus: | |||
336 | * SPI R5 == R1 + data byte; IO_RW_DIRECT | 356 | * SPI R5 == R1 + data byte; IO_RW_DIRECT |
337 | */ | 357 | */ |
338 | case MMC_RSP_SPI_R2: | 358 | case MMC_RSP_SPI_R2: |
339 | cmd->resp[0] |= *cp << 8; | 359 | /* read the next byte */ |
360 | if (cp == end) { | ||
361 | value = mmc_spi_readbytes(host, 1); | ||
362 | if (value < 0) | ||
363 | goto done; | ||
364 | cp = host->data->status; | ||
365 | end = cp+1; | ||
366 | } | ||
367 | if (bitshift) { | ||
368 | rotator = leftover << 8; | ||
369 | rotator |= *cp << bitshift; | ||
370 | cmd->resp[0] |= (rotator & 0xFF00); | ||
371 | } else { | ||
372 | cmd->resp[0] |= *cp << 8; | ||
373 | } | ||
340 | break; | 374 | break; |
341 | 375 | ||
342 | /* SPI R3, R4, or R7 == R1 + 4 bytes */ | 376 | /* SPI R3, R4, or R7 == R1 + 4 bytes */ |
343 | case MMC_RSP_SPI_R3: | 377 | case MMC_RSP_SPI_R3: |
344 | cmd->resp[1] = get_unaligned_be32(cp); | 378 | rotator = leftover << 8; |
379 | cmd->resp[1] = 0; | ||
380 | for (i = 0; i < 4; i++) { | ||
381 | cmd->resp[1] <<= 8; | ||
382 | /* read the next byte */ | ||
383 | if (cp == end) { | ||
384 | value = mmc_spi_readbytes(host, 1); | ||
385 | if (value < 0) | ||
386 | goto done; | ||
387 | cp = host->data->status; | ||
388 | end = cp+1; | ||
389 | } | ||
390 | if (bitshift) { | ||
391 | rotator |= *cp++ << bitshift; | ||
392 | cmd->resp[1] |= (rotator >> 8); | ||
393 | rotator <<= 8; | ||
394 | } else { | ||
395 | cmd->resp[1] |= *cp++; | ||
396 | } | ||
397 | } | ||
345 | break; | 398 | break; |
346 | 399 | ||
347 | /* SPI R1 == just one status byte */ | 400 | /* SPI R1 == just one status byte */ |
@@ -607,7 +660,7 @@ mmc_spi_setup_data_message( | |||
607 | */ | 660 | */ |
608 | static int | 661 | static int |
609 | mmc_spi_writeblock(struct mmc_spi_host *host, struct spi_transfer *t, | 662 | mmc_spi_writeblock(struct mmc_spi_host *host, struct spi_transfer *t, |
610 | ktime_t timeout) | 663 | unsigned long timeout) |
611 | { | 664 | { |
612 | struct spi_device *spi = host->spi; | 665 | struct spi_device *spi = host->spi; |
613 | int status, i; | 666 | int status, i; |
@@ -717,11 +770,13 @@ mmc_spi_writeblock(struct mmc_spi_host *host, struct spi_transfer *t, | |||
717 | */ | 770 | */ |
718 | static int | 771 | static int |
719 | mmc_spi_readblock(struct mmc_spi_host *host, struct spi_transfer *t, | 772 | mmc_spi_readblock(struct mmc_spi_host *host, struct spi_transfer *t, |
720 | ktime_t timeout) | 773 | unsigned long timeout) |
721 | { | 774 | { |
722 | struct spi_device *spi = host->spi; | 775 | struct spi_device *spi = host->spi; |
723 | int status; | 776 | int status; |
724 | struct scratch *scratch = host->data; | 777 | struct scratch *scratch = host->data; |
778 | unsigned int bitshift; | ||
779 | u8 leftover; | ||
725 | 780 | ||
726 | /* At least one SD card sends an all-zeroes byte when N(CX) | 781 | /* At least one SD card sends an all-zeroes byte when N(CX) |
727 | * applies, before the all-ones bytes ... just cope with that. | 782 | * applies, before the all-ones bytes ... just cope with that. |
@@ -733,38 +788,60 @@ mmc_spi_readblock(struct mmc_spi_host *host, struct spi_transfer *t, | |||
733 | if (status == 0xff || status == 0) | 788 | if (status == 0xff || status == 0) |
734 | status = mmc_spi_readtoken(host, timeout); | 789 | status = mmc_spi_readtoken(host, timeout); |
735 | 790 | ||
736 | if (status == SPI_TOKEN_SINGLE) { | 791 | if (status < 0) { |
737 | if (host->dma_dev) { | 792 | dev_dbg(&spi->dev, "read error %02x (%d)\n", status, status); |
738 | dma_sync_single_for_device(host->dma_dev, | 793 | return status; |
739 | host->data_dma, sizeof(*scratch), | 794 | } |
740 | DMA_BIDIRECTIONAL); | ||
741 | dma_sync_single_for_device(host->dma_dev, | ||
742 | t->rx_dma, t->len, | ||
743 | DMA_FROM_DEVICE); | ||
744 | } | ||
745 | 795 | ||
746 | status = spi_sync(spi, &host->m); | 796 | /* The token may be bit-shifted... |
797 | * the first 0-bit precedes the data stream. | ||
798 | */ | ||
799 | bitshift = 7; | ||
800 | while (status & 0x80) { | ||
801 | status <<= 1; | ||
802 | bitshift--; | ||
803 | } | ||
804 | leftover = status << 1; | ||
747 | 805 | ||
748 | if (host->dma_dev) { | 806 | if (host->dma_dev) { |
749 | dma_sync_single_for_cpu(host->dma_dev, | 807 | dma_sync_single_for_device(host->dma_dev, |
750 | host->data_dma, sizeof(*scratch), | 808 | host->data_dma, sizeof(*scratch), |
751 | DMA_BIDIRECTIONAL); | 809 | DMA_BIDIRECTIONAL); |
752 | dma_sync_single_for_cpu(host->dma_dev, | 810 | dma_sync_single_for_device(host->dma_dev, |
753 | t->rx_dma, t->len, | 811 | t->rx_dma, t->len, |
754 | DMA_FROM_DEVICE); | 812 | DMA_FROM_DEVICE); |
755 | } | 813 | } |
756 | 814 | ||
757 | } else { | 815 | status = spi_sync(spi, &host->m); |
758 | dev_dbg(&spi->dev, "read error %02x (%d)\n", status, status); | ||
759 | 816 | ||
760 | /* we've read extra garbage, timed out, etc */ | 817 | if (host->dma_dev) { |
761 | if (status < 0) | 818 | dma_sync_single_for_cpu(host->dma_dev, |
762 | return status; | 819 | host->data_dma, sizeof(*scratch), |
820 | DMA_BIDIRECTIONAL); | ||
821 | dma_sync_single_for_cpu(host->dma_dev, | ||
822 | t->rx_dma, t->len, | ||
823 | DMA_FROM_DEVICE); | ||
824 | } | ||
763 | 825 | ||
764 | /* low four bits are an R2 subset, fifth seems to be | 826 | if (bitshift) { |
765 | * vendor specific ... map them all to generic error.. | 827 | /* Walk through the data and the crc and do |
828 | * all the magic to get byte-aligned data. | ||
766 | */ | 829 | */ |
767 | return -EIO; | 830 | u8 *cp = t->rx_buf; |
831 | unsigned int len; | ||
832 | unsigned int bitright = 8 - bitshift; | ||
833 | u8 temp; | ||
834 | for (len = t->len; len; len--) { | ||
835 | temp = *cp; | ||
836 | *cp++ = leftover | (temp >> bitshift); | ||
837 | leftover = temp << bitright; | ||
838 | } | ||
839 | cp = (u8 *) &scratch->crc_val; | ||
840 | temp = *cp; | ||
841 | *cp++ = leftover | (temp >> bitshift); | ||
842 | leftover = temp << bitright; | ||
843 | temp = *cp; | ||
844 | *cp = leftover | (temp >> bitshift); | ||
768 | } | 845 | } |
769 | 846 | ||
770 | if (host->mmc->use_spi_crc) { | 847 | if (host->mmc->use_spi_crc) { |
@@ -803,7 +880,7 @@ mmc_spi_data_do(struct mmc_spi_host *host, struct mmc_command *cmd, | |||
803 | unsigned n_sg; | 880 | unsigned n_sg; |
804 | int multiple = (data->blocks > 1); | 881 | int multiple = (data->blocks > 1); |
805 | u32 clock_rate; | 882 | u32 clock_rate; |
806 | ktime_t timeout; | 883 | unsigned long timeout; |
807 | 884 | ||
808 | if (data->flags & MMC_DATA_READ) | 885 | if (data->flags & MMC_DATA_READ) |
809 | direction = DMA_FROM_DEVICE; | 886 | direction = DMA_FROM_DEVICE; |
@@ -817,8 +894,9 @@ mmc_spi_data_do(struct mmc_spi_host *host, struct mmc_command *cmd, | |||
817 | else | 894 | else |
818 | clock_rate = spi->max_speed_hz; | 895 | clock_rate = spi->max_speed_hz; |
819 | 896 | ||
820 | timeout = ktime_add_ns(ktime_set(0, 0), data->timeout_ns + | 897 | timeout = data->timeout_ns + |
821 | data->timeout_clks * 1000000 / clock_rate); | 898 | data->timeout_clks * 1000000 / clock_rate; |
899 | timeout = usecs_to_jiffies((unsigned int)(timeout / 1000)) + 1; | ||
822 | 900 | ||
823 | /* Handle scatterlist segments one at a time, with synch for | 901 | /* Handle scatterlist segments one at a time, with synch for |
824 | * each 512-byte block | 902 | * each 512-byte block |