0225-bcm2835-sdhost-Firmware-manages-the-clock-divisor.patch 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. From b14e4255f43ccf38b4f9df8f5793c9c4098e0ab9 Mon Sep 17 00:00:00 2001
  2. From: Phil Elwell <phil@raspberrypi.org>
  3. Date: Mon, 4 Apr 2016 16:03:18 +0100
  4. Subject: [PATCH 225/381] bcm2835-sdhost: Firmware manages the clock divisor
  5. The bcm2835-sdhost driver hands control of the CDIV clock divisor
  6. register to matching firmware, allowing it to adjust to a changing
  7. core clock. This removes the need to use the performance governor or
  8. to enable io_is_busy on the on-demand governor in order to get the
  9. best SD performance.
  10. N.B. As SD clocks must be an integer divisor of the core clock, it is
  11. possible that the SD clock for "turbo" mode can be different (even
  12. lower) than "normal" mode.
  13. Signed-off-by: Phil Elwell <phil@raspberrypi.org>
  14. ---
  15. drivers/mmc/host/bcm2835-sdhost.c | 120 ++++++++++++++++++-----------
  16. include/soc/bcm2835/raspberrypi-firmware.h | 1 +
  17. 2 files changed, 74 insertions(+), 47 deletions(-)
  18. --- a/drivers/mmc/host/bcm2835-sdhost.c
  19. +++ b/drivers/mmc/host/bcm2835-sdhost.c
  20. @@ -50,6 +50,7 @@
  21. #include <linux/of_dma.h>
  22. #include <linux/time.h>
  23. #include <linux/workqueue.h>
  24. +#include <soc/bcm2835/raspberrypi-firmware.h>
  25. #define DRIVER_NAME "sdhost-bcm2835"
  26. @@ -183,6 +184,7 @@ struct bcm2835_host {
  27. unsigned int use_sbc:1; /* Send CMD23 */
  28. unsigned int debug:1; /* Enable debug output */
  29. + unsigned int firmware_sets_cdiv:1; /* Let the firmware manage the clock */
  30. /*DMA part*/
  31. struct dma_chan *dma_chan_rxtx; /* DMA channel for reads and writes */
  32. @@ -430,7 +432,7 @@ static void bcm2835_sdhost_reset_interna
  33. host->clock = 0;
  34. host->sectors = 0;
  35. bcm2835_sdhost_write(host, host->hcfg, SDHCFG);
  36. - bcm2835_sdhost_write(host, host->cdiv, SDCDIV);
  37. + bcm2835_sdhost_write(host, SDCDIV_MAX_CDIV, SDCDIV);
  38. mmiowb();
  39. }
  40. @@ -1534,62 +1536,75 @@ void bcm2835_sdhost_set_clock(struct bcm
  41. host->mmc->actual_clock = 0;
  42. - if (clock < 100000) {
  43. - /* Can't stop the clock, but make it as slow as possible
  44. - * to show willing
  45. - */
  46. - host->cdiv = SDCDIV_MAX_CDIV;
  47. - bcm2835_sdhost_write(host, host->cdiv, SDCDIV);
  48. - return;
  49. - }
  50. -
  51. - div = host->max_clk / clock;
  52. - if (div < 2)
  53. - div = 2;
  54. - if ((host->max_clk / div) > clock)
  55. - div++;
  56. - div -= 2;
  57. + if (host->firmware_sets_cdiv) {
  58. + u32 msg[3] = { clock, 0, 0 };
  59. - if (div > SDCDIV_MAX_CDIV)
  60. - div = SDCDIV_MAX_CDIV;
  61. + rpi_firmware_property(rpi_firmware_get(NULL),
  62. + RPI_FIRMWARE_SET_SDHOST_CLOCK,
  63. + &msg, sizeof(msg));
  64. - clock = host->max_clk / (div + 2);
  65. - host->mmc->actual_clock = clock;
  66. + clock = max(msg[1], msg[2]);
  67. + } else {
  68. + if (clock < 100000) {
  69. + /* Can't stop the clock, but make it as slow as
  70. + * possible to show willing
  71. + */
  72. + host->cdiv = SDCDIV_MAX_CDIV;
  73. + bcm2835_sdhost_write(host, host->cdiv, SDCDIV);
  74. + return;
  75. + }
  76. +
  77. + div = host->max_clk / clock;
  78. + if (div < 2)
  79. + div = 2;
  80. + if ((host->max_clk / div) > clock)
  81. + div++;
  82. + div -= 2;
  83. +
  84. + if (div > SDCDIV_MAX_CDIV)
  85. + div = SDCDIV_MAX_CDIV;
  86. +
  87. + clock = host->max_clk / (div + 2);
  88. +
  89. + host->cdiv = div;
  90. + bcm2835_sdhost_write(host, host->cdiv, SDCDIV);
  91. +
  92. + if (host->debug)
  93. + pr_info("%s: clock=%d -> max_clk=%d, cdiv=%x "
  94. + "(actual clock %d)\n",
  95. + mmc_hostname(host->mmc), input_clock,
  96. + host->max_clk, host->cdiv,
  97. + clock);
  98. + }
  99. /* Calibrate some delays */
  100. host->ns_per_fifo_word = (1000000000/clock) *
  101. ((host->mmc->caps & MMC_CAP_4_BIT_DATA) ? 8 : 32);
  102. - if (clock > input_clock) {
  103. - /* Save the closest value, to make it easier
  104. - to reduce in the event of error */
  105. - host->overclock_50 = (clock/MHZ);
  106. -
  107. - if (clock != host->overclock) {
  108. - pr_warn("%s: overclocking to %dHz\n",
  109. - mmc_hostname(host->mmc), clock);
  110. - host->overclock = clock;
  111. + if (input_clock == 50 * MHZ) {
  112. + if (clock > input_clock) {
  113. + /* Save the closest value, to make it easier
  114. + to reduce in the event of error */
  115. + host->overclock_50 = (clock/MHZ);
  116. +
  117. + if (clock != host->overclock) {
  118. + pr_warn("%s: overclocking to %dHz\n",
  119. + mmc_hostname(host->mmc), clock);
  120. + host->overclock = clock;
  121. + }
  122. + } else if (host->overclock) {
  123. + host->overclock = 0;
  124. + if (clock == 50 * MHZ)
  125. + pr_warn("%s: cancelling overclock\n",
  126. + mmc_hostname(host->mmc));
  127. }
  128. }
  129. - else if (host->overclock)
  130. - {
  131. - host->overclock = 0;
  132. - if (clock == 50 * MHZ)
  133. - pr_warn("%s: cancelling overclock\n",
  134. - mmc_hostname(host->mmc));
  135. - }
  136. -
  137. - host->cdiv = div;
  138. - bcm2835_sdhost_write(host, host->cdiv, SDCDIV);
  139. /* Set the timeout to 500ms */
  140. - bcm2835_sdhost_write(host, host->mmc->actual_clock/2, SDTOUT);
  141. + bcm2835_sdhost_write(host, clock/2, SDTOUT);
  142. - if (host->debug)
  143. - pr_info("%s: clock=%d -> max_clk=%d, cdiv=%x (actual clock %d)\n",
  144. - mmc_hostname(host->mmc), input_clock,
  145. - host->max_clk, host->cdiv, host->mmc->actual_clock);
  146. + host->mmc->actual_clock = clock;
  147. }
  148. static void bcm2835_sdhost_request(struct mmc_host *mmc, struct mmc_request *mrq)
  149. @@ -1704,11 +1719,6 @@ static void bcm2835_sdhost_set_ios(struc
  150. log_event("IOS<", ios->clock, 0);
  151. - if (!ios->clock || ios->clock != host->clock) {
  152. - bcm2835_sdhost_set_clock(host, ios->clock);
  153. - host->clock = ios->clock;
  154. - }
  155. -
  156. /* set bus width */
  157. host->hcfg &= ~SDHCFG_WIDE_EXT_BUS;
  158. if (ios->bus_width == MMC_BUS_WIDTH_4)
  159. @@ -1721,6 +1731,11 @@ static void bcm2835_sdhost_set_ios(struc
  160. bcm2835_sdhost_write(host, host->hcfg, SDHCFG);
  161. + if (!ios->clock || ios->clock != host->clock) {
  162. + bcm2835_sdhost_set_clock(host, ios->clock);
  163. + host->clock = ios->clock;
  164. + }
  165. +
  166. mmiowb();
  167. spin_unlock_irqrestore(&host->lock, flags);
  168. @@ -1953,6 +1968,7 @@ static int bcm2835_sdhost_probe(struct p
  169. struct bcm2835_host *host;
  170. struct mmc_host *mmc;
  171. const __be32 *addr;
  172. + u32 msg[3];
  173. int ret;
  174. pr_debug("bcm2835_sdhost_probe\n");
  175. @@ -2058,6 +2074,16 @@ static int bcm2835_sdhost_probe(struct p
  176. else
  177. mmc->caps |= MMC_CAP_4_BIT_DATA;
  178. + msg[0] = 0;
  179. + msg[1] = ~0;
  180. + msg[2] = ~0;
  181. +
  182. + rpi_firmware_property(rpi_firmware_get(NULL),
  183. + RPI_FIRMWARE_SET_SDHOST_CLOCK,
  184. + &msg, sizeof(msg));
  185. +
  186. + host->firmware_sets_cdiv = (msg[1] != ~0);
  187. +
  188. ret = bcm2835_sdhost_add_host(host);
  189. if (ret)
  190. goto err;
  191. --- a/include/soc/bcm2835/raspberrypi-firmware.h
  192. +++ b/include/soc/bcm2835/raspberrypi-firmware.h
  193. @@ -79,6 +79,7 @@ enum rpi_firmware_property_tag {
  194. RPI_FIRMWARE_SET_VOLTAGE = 0x00038003,
  195. RPI_FIRMWARE_SET_TURBO = 0x00038009,
  196. RPI_FIRMWARE_SET_CUSTOMER_OTP = 0x00038021,
  197. + RPI_FIRMWARE_SET_SDHOST_CLOCK = 0x00038042,
  198. /* Dispmanx TAGS */
  199. RPI_FIRMWARE_FRAMEBUFFER_ALLOCATE = 0x00040001,