fuse-tegra20.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright (c) 2013-2014, NVIDIA CORPORATION. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. * Based on drivers/misc/eeprom/sunxi_sid.c
  17. */
  18. #include <linux/device.h>
  19. #include <linux/clk.h>
  20. #include <linux/completion.h>
  21. #include <linux/dmaengine.h>
  22. #include <linux/dma-mapping.h>
  23. #include <linux/err.h>
  24. #include <linux/io.h>
  25. #include <linux/kernel.h>
  26. #include <linux/kobject.h>
  27. #include <linux/of_device.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/random.h>
  30. #include <soc/tegra/fuse.h>
  31. #include "fuse.h"
  32. #define FUSE_BEGIN 0x100
  33. #define FUSE_UID_LOW 0x08
  34. #define FUSE_UID_HIGH 0x0c
  35. static u32 tegra20_fuse_read_early(struct tegra_fuse *fuse, unsigned int offset)
  36. {
  37. return readl_relaxed(fuse->base + FUSE_BEGIN + offset);
  38. }
  39. static void apb_dma_complete(void *args)
  40. {
  41. struct tegra_fuse *fuse = args;
  42. complete(&fuse->apbdma.wait);
  43. }
  44. static u32 tegra20_fuse_read(struct tegra_fuse *fuse, unsigned int offset)
  45. {
  46. unsigned long flags = DMA_PREP_INTERRUPT | DMA_CTRL_ACK;
  47. struct dma_async_tx_descriptor *dma_desc;
  48. unsigned long time_left;
  49. u32 value = 0;
  50. int err;
  51. mutex_lock(&fuse->apbdma.lock);
  52. fuse->apbdma.config.src_addr = fuse->phys + FUSE_BEGIN + offset;
  53. err = dmaengine_slave_config(fuse->apbdma.chan, &fuse->apbdma.config);
  54. if (err)
  55. goto out;
  56. dma_desc = dmaengine_prep_slave_single(fuse->apbdma.chan,
  57. fuse->apbdma.phys,
  58. sizeof(u32), DMA_DEV_TO_MEM,
  59. flags);
  60. if (!dma_desc)
  61. goto out;
  62. dma_desc->callback = apb_dma_complete;
  63. dma_desc->callback_param = fuse;
  64. reinit_completion(&fuse->apbdma.wait);
  65. clk_prepare_enable(fuse->clk);
  66. dmaengine_submit(dma_desc);
  67. dma_async_issue_pending(fuse->apbdma.chan);
  68. time_left = wait_for_completion_timeout(&fuse->apbdma.wait,
  69. msecs_to_jiffies(50));
  70. if (WARN(time_left == 0, "apb read dma timed out"))
  71. dmaengine_terminate_all(fuse->apbdma.chan);
  72. else
  73. value = *fuse->apbdma.virt;
  74. clk_disable_unprepare(fuse->clk);
  75. out:
  76. mutex_unlock(&fuse->apbdma.lock);
  77. return value;
  78. }
  79. static bool dma_filter(struct dma_chan *chan, void *filter_param)
  80. {
  81. struct device_node *np = chan->device->dev->of_node;
  82. return of_device_is_compatible(np, "nvidia,tegra20-apbdma");
  83. }
  84. static int tegra20_fuse_probe(struct tegra_fuse *fuse)
  85. {
  86. dma_cap_mask_t mask;
  87. dma_cap_zero(mask);
  88. dma_cap_set(DMA_SLAVE, mask);
  89. fuse->apbdma.chan = __dma_request_channel(&mask, dma_filter, NULL);
  90. if (!fuse->apbdma.chan)
  91. return -EPROBE_DEFER;
  92. fuse->apbdma.virt = dma_alloc_coherent(fuse->dev, sizeof(u32),
  93. &fuse->apbdma.phys,
  94. GFP_KERNEL);
  95. if (!fuse->apbdma.virt) {
  96. dma_release_channel(fuse->apbdma.chan);
  97. return -ENOMEM;
  98. }
  99. fuse->apbdma.config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  100. fuse->apbdma.config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  101. fuse->apbdma.config.src_maxburst = 1;
  102. fuse->apbdma.config.dst_maxburst = 1;
  103. fuse->apbdma.config.direction = DMA_DEV_TO_MEM;
  104. fuse->apbdma.config.device_fc = false;
  105. init_completion(&fuse->apbdma.wait);
  106. mutex_init(&fuse->apbdma.lock);
  107. fuse->read = tegra20_fuse_read;
  108. return 0;
  109. }
  110. static const struct tegra_fuse_info tegra20_fuse_info = {
  111. .read = tegra20_fuse_read,
  112. .size = 0x1f8,
  113. .spare = 0x100,
  114. };
  115. /* Early boot code. This code is called before the devices are created */
  116. static void __init tegra20_fuse_add_randomness(void)
  117. {
  118. u32 randomness[7];
  119. randomness[0] = tegra_sku_info.sku_id;
  120. randomness[1] = tegra_read_straps();
  121. randomness[2] = tegra_read_chipid();
  122. randomness[3] = tegra_sku_info.cpu_process_id << 16;
  123. randomness[3] |= tegra_sku_info.soc_process_id;
  124. randomness[4] = tegra_sku_info.cpu_speedo_id << 16;
  125. randomness[4] |= tegra_sku_info.soc_speedo_id;
  126. randomness[5] = tegra_fuse_read_early(FUSE_UID_LOW);
  127. randomness[6] = tegra_fuse_read_early(FUSE_UID_HIGH);
  128. add_device_randomness(randomness, sizeof(randomness));
  129. }
  130. static void __init tegra20_fuse_init(struct tegra_fuse *fuse)
  131. {
  132. fuse->read_early = tegra20_fuse_read_early;
  133. tegra_init_revision();
  134. fuse->soc->speedo_init(&tegra_sku_info);
  135. tegra20_fuse_add_randomness();
  136. }
  137. const struct tegra_fuse_soc tegra20_fuse_soc = {
  138. .init = tegra20_fuse_init,
  139. .speedo_init = tegra20_init_speedo_data,
  140. .probe = tegra20_fuse_probe,
  141. .info = &tegra20_fuse_info,
  142. };