0216-bcm2835-sdhost-Adjust-to-core-clock-changes.patch 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. From 1ce5a11b0faf0657a65a510ba960554bbfee2410 Mon Sep 17 00:00:00 2001
  2. From: Phil Elwell <phil@raspberrypi.org>
  3. Date: Wed, 30 Mar 2016 16:33:09 +0100
  4. Subject: [PATCH] bcm2835-sdhost: Adjust to core clock changes
  5. The SDHOST block uses the core clock, so previously it has been
  6. necessary to prevent the core clock from changing in order to maintain
  7. performance and prevent accidental SD bus overclocking.
  8. With this patch the sdhost driver is notified of clock changes, allowing
  9. it to delay them while an SD access is outstanding and to delay new SD
  10. accesses while the clock is changing. This feature is disabled in the
  11. case where the core frequency can never change.
  12. Now that the driver copes with changes to the core clock, it is safe
  13. to disable the io_is_busy feature of the on-demand governor.
  14. Signed-off-by: Phil Elwell <phil@raspberrypi.org>
  15. ---
  16. drivers/mmc/host/Kconfig | 1 +
  17. drivers/mmc/host/bcm2835-sdhost.c | 140 ++++++++++++++++++++++++++++++++------
  18. 2 files changed, 119 insertions(+), 22 deletions(-)
  19. --- a/drivers/mmc/host/Kconfig
  20. +++ b/drivers/mmc/host/Kconfig
  21. @@ -36,6 +36,7 @@ config MMC_BCM2835_PIO_DMA_BARRIER
  22. config MMC_BCM2835_SDHOST
  23. tristate "Support for the SDHost controller on BCM2708/9"
  24. depends on MACH_BCM2708 || MACH_BCM2709 || ARCH_BCM2835
  25. + depends on RASPBERRYPI_FIRMWARE
  26. help
  27. This selects the SDHost controller on BCM2835/6.
  28. --- a/drivers/mmc/host/bcm2835-sdhost.c
  29. +++ b/drivers/mmc/host/bcm2835-sdhost.c
  30. @@ -50,6 +50,10 @@
  31. #include <linux/of_dma.h>
  32. #include <linux/time.h>
  33. #include <linux/workqueue.h>
  34. +#include <linux/cpufreq.h>
  35. +#include <linux/semaphore.h>
  36. +#include <soc/bcm2835/raspberrypi-firmware.h>
  37. +
  38. #define DRIVER_NAME "sdhost-bcm2835"
  39. @@ -136,6 +140,8 @@
  40. #define MHZ 1000000
  41. +#define RPI_FIRMWARE_CLOCK_CORE 4
  42. +
  43. struct bcm2835_host {
  44. spinlock_t lock;
  45. @@ -151,7 +157,9 @@ struct bcm2835_host {
  46. bool slow_card; /* Force 11-bit divisor */
  47. - unsigned int max_clk; /* Max possible freq */
  48. + unsigned int max_clk; /* Max src clock freq */
  49. + unsigned int min_clk; /* Min src clock freq */
  50. + unsigned int cur_clk; /* Current src clock freq */
  51. struct tasklet_struct finish_tasklet; /* Tasklet structures */
  52. @@ -183,6 +191,7 @@ struct bcm2835_host {
  53. unsigned int use_sbc:1; /* Send CMD23 */
  54. unsigned int debug:1; /* Enable debug output */
  55. + unsigned int variable_clock:1; /* The core clock may change */
  56. /*DMA part*/
  57. struct dma_chan *dma_chan_rxtx; /* DMA channel for reads and writes */
  58. @@ -208,6 +217,9 @@ struct bcm2835_host {
  59. u32 pio_limit; /* Maximum block count for PIO (0 = always DMA) */
  60. u32 sectors; /* Cached card size in sectors */
  61. +
  62. + struct notifier_block cpufreq_nb; /* The cpufreq callback list item */
  63. + struct semaphore cpufreq_semaphore; /* Interlock between SD activity and cpufreq changes */
  64. };
  65. #if ENABLE_LOG
  66. @@ -227,6 +239,10 @@ static u32 sdhost_log_idx;
  67. static spinlock_t log_lock;
  68. static void __iomem *timer_base;
  69. +static int bcm2835_sdhost_cpufreq_callback(struct notifier_block *nb,
  70. + unsigned long action, void *data);
  71. +static unsigned int get_core_clock(unsigned int mode);
  72. +
  73. #define LOG_ENTRIES (256*1)
  74. #define LOG_SIZE (sizeof(LOG_ENTRY_T)*LOG_ENTRIES)
  75. @@ -448,20 +464,14 @@ static void bcm2835_sdhost_reset(struct
  76. static void bcm2835_sdhost_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
  77. -static void bcm2835_sdhost_init(struct bcm2835_host *host, int soft)
  78. +static void bcm2835_sdhost_init(struct bcm2835_host *host)
  79. {
  80. - pr_debug("bcm2835_sdhost_init(%d)\n", soft);
  81. + pr_debug("bcm2835_sdhost_init()\n");
  82. /* Set interrupt enables */
  83. host->hcfg = SDHCFG_BUSY_IRPT_EN;
  84. bcm2835_sdhost_reset_internal(host);
  85. -
  86. - if (soft) {
  87. - /* force clock reconfiguration */
  88. - host->clock = 0;
  89. - bcm2835_sdhost_set_ios(host->mmc, &host->mmc->ios);
  90. - }
  91. }
  92. static void bcm2835_sdhost_wait_transfer_complete(struct bcm2835_host *host)
  93. @@ -1499,10 +1509,10 @@ static irqreturn_t bcm2835_sdhost_irq(in
  94. return result;
  95. }
  96. -void bcm2835_sdhost_set_clock(struct bcm2835_host *host, unsigned int clock)
  97. +void bcm2835_sdhost_set_clock(struct bcm2835_host *host)
  98. {
  99. int div = 0; /* Initialized for compiler warning */
  100. - unsigned int input_clock = clock;
  101. + unsigned int clock = host->clock;
  102. if (host->debug)
  103. pr_info("%s: set_clock(%d)\n", mmc_hostname(host->mmc), clock);
  104. @@ -1543,17 +1553,17 @@ void bcm2835_sdhost_set_clock(struct bcm
  105. return;
  106. }
  107. - div = host->max_clk / clock;
  108. + div = host->cur_clk / clock;
  109. if (div < 2)
  110. div = 2;
  111. - if ((host->max_clk / div) > clock)
  112. + if ((host->cur_clk / div) > clock)
  113. div++;
  114. div -= 2;
  115. if (div > SDCDIV_MAX_CDIV)
  116. div = SDCDIV_MAX_CDIV;
  117. - clock = host->max_clk / (div + 2);
  118. + clock = host->cur_clk / (div + 2);
  119. host->mmc->actual_clock = clock;
  120. /* Calibrate some delays */
  121. @@ -1561,7 +1571,7 @@ void bcm2835_sdhost_set_clock(struct bcm
  122. host->ns_per_fifo_word = (1000000000/clock) *
  123. ((host->mmc->caps & MMC_CAP_4_BIT_DATA) ? 8 : 32);
  124. - if (clock > input_clock) {
  125. + if (clock > host->clock) {
  126. /* Save the closest value, to make it easier
  127. to reduce in the event of error */
  128. host->overclock_50 = (clock/MHZ);
  129. @@ -1587,9 +1597,9 @@ void bcm2835_sdhost_set_clock(struct bcm
  130. bcm2835_sdhost_write(host, host->mmc->actual_clock/2, SDTOUT);
  131. if (host->debug)
  132. - pr_info("%s: clock=%d -> max_clk=%d, cdiv=%x (actual clock %d)\n",
  133. - mmc_hostname(host->mmc), input_clock,
  134. - host->max_clk, host->cdiv, host->mmc->actual_clock);
  135. + pr_info("%s: clock=%d -> cur_clk=%d, cdiv=%x (actual clock %d)\n",
  136. + mmc_hostname(host->mmc), host->clock,
  137. + host->cur_clk, host->cdiv, host->mmc->actual_clock);
  138. }
  139. static void bcm2835_sdhost_request(struct mmc_host *mmc, struct mmc_request *mrq)
  140. @@ -1638,6 +1648,13 @@ static void bcm2835_sdhost_request(struc
  141. (mrq->data->blocks > host->pio_limit))
  142. bcm2835_sdhost_prepare_dma(host, mrq->data);
  143. + if (host->variable_clock &&
  144. + (down_killable(&host->cpufreq_semaphore) != 0)) {
  145. + mrq->cmd->error = -EINTR;
  146. + mmc_request_done(mmc, mrq);
  147. + return;
  148. + }
  149. +
  150. spin_lock_irqsave(&host->lock, flags);
  151. WARN_ON(host->mrq != NULL);
  152. @@ -1687,6 +1704,52 @@ static void bcm2835_sdhost_request(struc
  153. spin_unlock_irqrestore(&host->lock, flags);
  154. }
  155. +static int bcm2835_sdhost_cpufreq_callback(struct notifier_block *nb,
  156. + unsigned long action, void *data)
  157. +{
  158. + struct cpufreq_freqs *freq = data;
  159. + struct bcm2835_host *host;
  160. +
  161. + host = container_of(nb, struct bcm2835_host, cpufreq_nb);
  162. +
  163. + if (freq->cpu == 0) {
  164. + switch (action) {
  165. + case CPUFREQ_PRECHANGE:
  166. + if (down_killable(&host->cpufreq_semaphore) != 0)
  167. + return NOTIFY_BAD;
  168. + break;
  169. + case CPUFREQ_POSTCHANGE:
  170. + if (freq->new > freq->old)
  171. + host->cur_clk = host->max_clk;
  172. + else
  173. + host->cur_clk = host->min_clk;
  174. + bcm2835_sdhost_set_clock(host);
  175. + up(&host->cpufreq_semaphore);
  176. + break;
  177. + default:
  178. + break;
  179. + }
  180. + }
  181. + return NOTIFY_OK;
  182. +}
  183. +
  184. +static unsigned int get_core_clock(unsigned int mode)
  185. +{
  186. + struct rpi_firmware *fw = rpi_firmware_get(NULL);
  187. + struct {
  188. + u32 id;
  189. + u32 val;
  190. + } packet;
  191. + int ret;
  192. +
  193. + packet.id = RPI_FIRMWARE_CLOCK_CORE;
  194. + ret = rpi_firmware_property(fw, mode, &packet, sizeof(packet));
  195. + if (ret)
  196. + return 0;
  197. +
  198. + return packet.val;
  199. +}
  200. +
  201. static void bcm2835_sdhost_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  202. {
  203. @@ -1700,13 +1763,16 @@ static void bcm2835_sdhost_set_ios(struc
  204. ios->clock, ios->power_mode, ios->bus_width,
  205. ios->timing, ios->signal_voltage, ios->drv_type);
  206. + if (ios->clock && !host->cur_clk)
  207. + host->cur_clk = get_core_clock(RPI_FIRMWARE_GET_CLOCK_RATE);
  208. +
  209. spin_lock_irqsave(&host->lock, flags);
  210. log_event("IOS<", ios->clock, 0);
  211. if (!ios->clock || ios->clock != host->clock) {
  212. - bcm2835_sdhost_set_clock(host, ios->clock);
  213. host->clock = ios->clock;
  214. + bcm2835_sdhost_set_clock(host);
  215. }
  216. /* set bus width */
  217. @@ -1795,7 +1861,7 @@ static void bcm2835_sdhost_tasklet_finis
  218. host->overclock_50--;
  219. pr_warn("%s: reducing overclock due to errors\n",
  220. mmc_hostname(host->mmc));
  221. - bcm2835_sdhost_set_clock(host,50*MHZ);
  222. + bcm2835_sdhost_set_clock(host);
  223. mrq->cmd->error = -EILSEQ;
  224. mrq->cmd->retries = 1;
  225. }
  226. @@ -1813,6 +1879,9 @@ static void bcm2835_sdhost_tasklet_finis
  227. spin_unlock_irqrestore(&host->lock, flags);
  228. + if (host->variable_clock)
  229. + up(&host->cpufreq_semaphore);
  230. +
  231. if (terminate_chan)
  232. {
  233. int err = dmaengine_terminate_all(terminate_chan);
  234. @@ -1915,10 +1984,10 @@ int bcm2835_sdhost_add_host(struct bcm28
  235. setup_timer(&host->timer, bcm2835_sdhost_timeout,
  236. (unsigned long)host);
  237. - bcm2835_sdhost_init(host, 0);
  238. + bcm2835_sdhost_init(host);
  239. ret = request_irq(host->irq, bcm2835_sdhost_irq, 0 /*IRQF_SHARED*/,
  240. - mmc_hostname(mmc), host);
  241. + mmc_hostname(mmc), host);
  242. if (ret) {
  243. pr_err("%s: failed to request IRQ %d: %d\n",
  244. mmc_hostname(mmc), host->irq, ret);
  245. @@ -1953,6 +2022,7 @@ static int bcm2835_sdhost_probe(struct p
  246. struct bcm2835_host *host;
  247. struct mmc_host *mmc;
  248. const __be32 *addr;
  249. + unsigned int max_clk;
  250. int ret;
  251. pr_debug("bcm2835_sdhost_probe\n");
  252. @@ -2062,6 +2132,28 @@ static int bcm2835_sdhost_probe(struct p
  253. if (ret)
  254. goto err;
  255. + /* Query the core clock frequencies */
  256. + host->min_clk = get_core_clock(RPI_FIRMWARE_GET_MIN_CLOCK_RATE);
  257. + max_clk = get_core_clock(RPI_FIRMWARE_GET_MAX_CLOCK_RATE);
  258. + if (max_clk != host->max_clk) {
  259. + pr_warn("%s: Expected max clock %d, found %d\n",
  260. + mmc_hostname(mmc), host->max_clk, max_clk);
  261. + host->max_clk = max_clk;
  262. + }
  263. +
  264. + if (host->min_clk != host->max_clk) {
  265. + host->cpufreq_nb.notifier_call =
  266. + bcm2835_sdhost_cpufreq_callback;
  267. + sema_init(&host->cpufreq_semaphore, 1);
  268. + cpufreq_register_notifier(&host->cpufreq_nb,
  269. + CPUFREQ_TRANSITION_NOTIFIER);
  270. + host->variable_clock = 1;
  271. + host->cur_clk = 0; /* Get this later */
  272. + } else {
  273. + host->variable_clock = 0;
  274. + host->cur_clk = host->max_clk;
  275. + }
  276. +
  277. platform_set_drvdata(pdev, host);
  278. pr_debug("bcm2835_sdhost_probe -> OK\n");
  279. @@ -2081,6 +2173,10 @@ static int bcm2835_sdhost_remove(struct
  280. pr_debug("bcm2835_sdhost_remove\n");
  281. + if (host->variable_clock)
  282. + cpufreq_unregister_notifier(&host->cpufreq_nb,
  283. + CPUFREQ_TRANSITION_NOTIFIER);
  284. +
  285. mmc_remove_host(host->mmc);
  286. bcm2835_sdhost_set_power(host, false);