fuse-tegra20.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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->apbdma.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 int tegra20_fuse_probe(struct tegra_fuse *fuse)
  80. {
  81. dma_cap_mask_t mask;
  82. dma_cap_zero(mask);
  83. dma_cap_set(DMA_SLAVE, mask);
  84. fuse->apbdma.chan = dma_request_channel(mask, NULL, NULL);
  85. if (!fuse->apbdma.chan)
  86. return -EPROBE_DEFER;
  87. fuse->apbdma.virt = dma_alloc_coherent(fuse->dev, sizeof(u32),
  88. &fuse->apbdma.phys,
  89. GFP_KERNEL);
  90. if (!fuse->apbdma.virt) {
  91. dma_release_channel(fuse->apbdma.chan);
  92. return -ENOMEM;
  93. }
  94. fuse->apbdma.config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  95. fuse->apbdma.config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  96. fuse->apbdma.config.src_maxburst = 1;
  97. fuse->apbdma.config.dst_maxburst = 1;
  98. init_completion(&fuse->apbdma.wait);
  99. mutex_init(&fuse->apbdma.lock);
  100. fuse->read = tegra20_fuse_read;
  101. return 0;
  102. }
  103. static const struct tegra_fuse_info tegra20_fuse_info = {
  104. .read = tegra20_fuse_read,
  105. .size = 0x1f8,
  106. .spare = 0x100,
  107. };
  108. /* Early boot code. This code is called before the devices are created */
  109. static void __init tegra20_fuse_add_randomness(void)
  110. {
  111. u32 randomness[7];
  112. randomness[0] = tegra_sku_info.sku_id;
  113. randomness[1] = tegra_read_straps();
  114. randomness[2] = tegra_read_chipid();
  115. randomness[3] = tegra_sku_info.cpu_process_id << 16;
  116. randomness[3] |= tegra_sku_info.soc_process_id;
  117. randomness[4] = tegra_sku_info.cpu_speedo_id << 16;
  118. randomness[4] |= tegra_sku_info.soc_speedo_id;
  119. randomness[5] = tegra_fuse_read_early(FUSE_UID_LOW);
  120. randomness[6] = tegra_fuse_read_early(FUSE_UID_HIGH);
  121. add_device_randomness(randomness, sizeof(randomness));
  122. }
  123. static void __init tegra20_fuse_init(struct tegra_fuse *fuse)
  124. {
  125. fuse->read_early = tegra20_fuse_read_early;
  126. tegra_init_revision();
  127. fuse->soc->speedo_init(&tegra_sku_info);
  128. tegra20_fuse_add_randomness();
  129. }
  130. const struct tegra_fuse_soc tegra20_fuse_soc = {
  131. .init = tegra20_fuse_init,
  132. .speedo_init = tegra20_init_speedo_data,
  133. .probe = tegra20_fuse_probe,
  134. .info = &tegra20_fuse_info,
  135. };