bcm2835_sdhci.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * This code was extracted from:
  3. * git://github.com/gonzoua/u-boot-pi.git master
  4. * and hence presumably (C) 2012 Oleksandr Tymoshenko
  5. *
  6. * Tweaks for U-Boot upstreaming
  7. * (C) 2012 Stephen Warren
  8. *
  9. * Portions (e.g. read/write macros, concepts for back-to-back register write
  10. * timing workarounds) obviously extracted from the Linux kernel at:
  11. * https://github.com/raspberrypi/linux.git rpi-3.6.y
  12. *
  13. * The Linux kernel code has the following (c) and license, which is hence
  14. * propagated to Oleksandr's tree and here:
  15. *
  16. * Support for SDHCI device on 2835
  17. * Based on sdhci-bcm2708.c (c) 2010 Broadcom
  18. *
  19. * This program is free software; you can redistribute it and/or modify
  20. * it under the terms of the GNU General Public License version 2 as
  21. * published by the Free Software Foundation.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU General Public License
  29. * along with this program; if not, write to the Free Software
  30. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  31. */
  32. /* Supports:
  33. * SDHCI platform device - Arasan SD controller in BCM2708
  34. *
  35. * Inspired by sdhci-pci.c, by Pierre Ossman
  36. */
  37. #include <common.h>
  38. #include <malloc.h>
  39. #include <sdhci.h>
  40. #include <mach/timer.h>
  41. #include <mach/sdhci.h>
  42. /* 400KHz is max freq for card ID etc. Use that as min */
  43. #define MIN_FREQ 400000
  44. struct bcm2835_sdhci_host {
  45. struct sdhci_host host;
  46. uint twoticks_delay;
  47. ulong last_write;
  48. };
  49. static inline struct bcm2835_sdhci_host *to_bcm(struct sdhci_host *host)
  50. {
  51. return (struct bcm2835_sdhci_host *)host;
  52. }
  53. static inline void bcm2835_sdhci_raw_writel(struct sdhci_host *host, u32 val,
  54. int reg)
  55. {
  56. struct bcm2835_sdhci_host *bcm_host = to_bcm(host);
  57. /*
  58. * The Arasan has a bugette whereby it may lose the content of
  59. * successive writes to registers that are within two SD-card clock
  60. * cycles of each other (a clock domain crossing problem).
  61. * It seems, however, that the data register does not have this problem.
  62. * (Which is just as well - otherwise we'd have to nobble the DMA engine
  63. * too)
  64. */
  65. while (timer_get_us() - bcm_host->last_write < bcm_host->twoticks_delay)
  66. ;
  67. writel(val, host->ioaddr + reg);
  68. bcm_host->last_write = timer_get_us();
  69. }
  70. static inline u32 bcm2835_sdhci_raw_readl(struct sdhci_host *host, int reg)
  71. {
  72. return readl(host->ioaddr + reg);
  73. }
  74. static void bcm2835_sdhci_writel(struct sdhci_host *host, u32 val, int reg)
  75. {
  76. bcm2835_sdhci_raw_writel(host, val, reg);
  77. }
  78. static void bcm2835_sdhci_writew(struct sdhci_host *host, u16 val, int reg)
  79. {
  80. static u32 shadow;
  81. u32 oldval = (reg == SDHCI_COMMAND) ? shadow :
  82. bcm2835_sdhci_raw_readl(host, reg & ~3);
  83. u32 word_num = (reg >> 1) & 1;
  84. u32 word_shift = word_num * 16;
  85. u32 mask = 0xffff << word_shift;
  86. u32 newval = (oldval & ~mask) | (val << word_shift);
  87. if (reg == SDHCI_TRANSFER_MODE)
  88. shadow = newval;
  89. else
  90. bcm2835_sdhci_raw_writel(host, newval, reg & ~3);
  91. }
  92. static void bcm2835_sdhci_writeb(struct sdhci_host *host, u8 val, int reg)
  93. {
  94. u32 oldval = bcm2835_sdhci_raw_readl(host, reg & ~3);
  95. u32 byte_num = reg & 3;
  96. u32 byte_shift = byte_num * 8;
  97. u32 mask = 0xff << byte_shift;
  98. u32 newval = (oldval & ~mask) | (val << byte_shift);
  99. bcm2835_sdhci_raw_writel(host, newval, reg & ~3);
  100. }
  101. static u32 bcm2835_sdhci_readl(struct sdhci_host *host, int reg)
  102. {
  103. u32 val = bcm2835_sdhci_raw_readl(host, reg);
  104. return val;
  105. }
  106. static u16 bcm2835_sdhci_readw(struct sdhci_host *host, int reg)
  107. {
  108. u32 val = bcm2835_sdhci_raw_readl(host, (reg & ~3));
  109. u32 word_num = (reg >> 1) & 1;
  110. u32 word_shift = word_num * 16;
  111. u32 word = (val >> word_shift) & 0xffff;
  112. return word;
  113. }
  114. static u8 bcm2835_sdhci_readb(struct sdhci_host *host, int reg)
  115. {
  116. u32 val = bcm2835_sdhci_raw_readl(host, (reg & ~3));
  117. u32 byte_num = reg & 3;
  118. u32 byte_shift = byte_num * 8;
  119. u32 byte = (val >> byte_shift) & 0xff;
  120. return byte;
  121. }
  122. static const struct sdhci_ops bcm2835_ops = {
  123. .write_l = bcm2835_sdhci_writel,
  124. .write_w = bcm2835_sdhci_writew,
  125. .write_b = bcm2835_sdhci_writeb,
  126. .read_l = bcm2835_sdhci_readl,
  127. .read_w = bcm2835_sdhci_readw,
  128. .read_b = bcm2835_sdhci_readb,
  129. };
  130. int bcm2835_sdhci_init(u32 regbase, u32 emmc_freq)
  131. {
  132. struct bcm2835_sdhci_host *bcm_host;
  133. struct sdhci_host *host;
  134. bcm_host = calloc(1, sizeof(*bcm_host));
  135. if (!bcm_host) {
  136. printf("sdhci_host calloc fail!\n");
  137. return -ENOMEM;
  138. }
  139. /*
  140. * See the comments in bcm2835_sdhci_raw_writel().
  141. *
  142. * This should probably be dynamically calculated based on the actual
  143. * frequency. However, this is the longest we'll have to wait, and
  144. * doesn't seem to slow access down too much, so the added complexity
  145. * doesn't seem worth it for now.
  146. *
  147. * 1/MIN_FREQ is (max) time per tick of eMMC clock.
  148. * 2/MIN_FREQ is time for two ticks.
  149. * Multiply by 1000000 to get uS per two ticks.
  150. * +1 for hack rounding.
  151. */
  152. bcm_host->twoticks_delay = ((2 * 1000000) / MIN_FREQ) + 1;
  153. bcm_host->last_write = 0;
  154. host = &bcm_host->host;
  155. host->name = "bcm2835_sdhci";
  156. host->ioaddr = (void *)(unsigned long)regbase;
  157. host->quirks = SDHCI_QUIRK_BROKEN_VOLTAGE | SDHCI_QUIRK_BROKEN_R1B |
  158. SDHCI_QUIRK_WAIT_SEND_CMD | SDHCI_QUIRK_NO_HISPD_BIT;
  159. host->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
  160. host->ops = &bcm2835_ops;
  161. add_sdhci(host, emmc_freq, MIN_FREQ);
  162. return 0;
  163. }