fuse-tegra20.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2013-2014, NVIDIA CORPORATION. All rights reserved.
  4. *
  5. * Based on drivers/misc/eeprom/sunxi_sid.c
  6. */
  7. #include <linux/device.h>
  8. #include <linux/clk.h>
  9. #include <linux/completion.h>
  10. #include <linux/dmaengine.h>
  11. #include <linux/dma-mapping.h>
  12. #include <linux/err.h>
  13. #include <linux/io.h>
  14. #include <linux/kernel.h>
  15. #include <linux/kobject.h>
  16. #include <linux/of_device.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/random.h>
  19. #include <soc/tegra/fuse.h>
  20. #include "fuse.h"
  21. #define FUSE_BEGIN 0x100
  22. #define FUSE_UID_LOW 0x08
  23. #define FUSE_UID_HIGH 0x0c
  24. static u32 tegra20_fuse_read_early(struct tegra_fuse *fuse, unsigned int offset)
  25. {
  26. return readl_relaxed(fuse->base + FUSE_BEGIN + offset);
  27. }
  28. static void apb_dma_complete(void *args)
  29. {
  30. struct tegra_fuse *fuse = args;
  31. complete(&fuse->apbdma.wait);
  32. }
  33. static u32 tegra20_fuse_read(struct tegra_fuse *fuse, unsigned int offset)
  34. {
  35. unsigned long flags = DMA_PREP_INTERRUPT | DMA_CTRL_ACK;
  36. struct dma_async_tx_descriptor *dma_desc;
  37. unsigned long time_left;
  38. u32 value = 0;
  39. int err;
  40. mutex_lock(&fuse->apbdma.lock);
  41. fuse->apbdma.config.src_addr = fuse->phys + FUSE_BEGIN + offset;
  42. err = dmaengine_slave_config(fuse->apbdma.chan, &fuse->apbdma.config);
  43. if (err)
  44. goto out;
  45. dma_desc = dmaengine_prep_slave_single(fuse->apbdma.chan,
  46. fuse->apbdma.phys,
  47. sizeof(u32), DMA_DEV_TO_MEM,
  48. flags);
  49. if (!dma_desc)
  50. goto out;
  51. dma_desc->callback = apb_dma_complete;
  52. dma_desc->callback_param = fuse;
  53. reinit_completion(&fuse->apbdma.wait);
  54. clk_prepare_enable(fuse->clk);
  55. dmaengine_submit(dma_desc);
  56. dma_async_issue_pending(fuse->apbdma.chan);
  57. time_left = wait_for_completion_timeout(&fuse->apbdma.wait,
  58. msecs_to_jiffies(50));
  59. if (WARN(time_left == 0, "apb read dma timed out"))
  60. dmaengine_terminate_all(fuse->apbdma.chan);
  61. else
  62. value = *fuse->apbdma.virt;
  63. clk_disable_unprepare(fuse->clk);
  64. out:
  65. mutex_unlock(&fuse->apbdma.lock);
  66. return value;
  67. }
  68. static bool dma_filter(struct dma_chan *chan, void *filter_param)
  69. {
  70. struct device_node *np = chan->device->dev->of_node;
  71. return of_device_is_compatible(np, "nvidia,tegra20-apbdma");
  72. }
  73. static int tegra20_fuse_probe(struct tegra_fuse *fuse)
  74. {
  75. dma_cap_mask_t mask;
  76. dma_cap_zero(mask);
  77. dma_cap_set(DMA_SLAVE, mask);
  78. fuse->apbdma.chan = dma_request_channel(mask, dma_filter, NULL);
  79. if (!fuse->apbdma.chan)
  80. return -EPROBE_DEFER;
  81. fuse->apbdma.virt = dma_alloc_coherent(fuse->dev, sizeof(u32),
  82. &fuse->apbdma.phys,
  83. GFP_KERNEL);
  84. if (!fuse->apbdma.virt) {
  85. dma_release_channel(fuse->apbdma.chan);
  86. return -ENOMEM;
  87. }
  88. fuse->apbdma.config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  89. fuse->apbdma.config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  90. fuse->apbdma.config.src_maxburst = 1;
  91. fuse->apbdma.config.dst_maxburst = 1;
  92. fuse->apbdma.config.direction = DMA_DEV_TO_MEM;
  93. fuse->apbdma.config.device_fc = false;
  94. init_completion(&fuse->apbdma.wait);
  95. mutex_init(&fuse->apbdma.lock);
  96. fuse->read = tegra20_fuse_read;
  97. return 0;
  98. }
  99. static const struct tegra_fuse_info tegra20_fuse_info = {
  100. .read = tegra20_fuse_read,
  101. .size = 0x1f8,
  102. .spare = 0x100,
  103. };
  104. /* Early boot code. This code is called before the devices are created */
  105. static void __init tegra20_fuse_add_randomness(void)
  106. {
  107. u32 randomness[7];
  108. randomness[0] = tegra_sku_info.sku_id;
  109. randomness[1] = tegra_read_straps();
  110. randomness[2] = tegra_read_chipid();
  111. randomness[3] = tegra_sku_info.cpu_process_id << 16;
  112. randomness[3] |= tegra_sku_info.soc_process_id;
  113. randomness[4] = tegra_sku_info.cpu_speedo_id << 16;
  114. randomness[4] |= tegra_sku_info.soc_speedo_id;
  115. randomness[5] = tegra_fuse_read_early(FUSE_UID_LOW);
  116. randomness[6] = tegra_fuse_read_early(FUSE_UID_HIGH);
  117. add_device_randomness(randomness, sizeof(randomness));
  118. }
  119. static void __init tegra20_fuse_init(struct tegra_fuse *fuse)
  120. {
  121. fuse->read_early = tegra20_fuse_read_early;
  122. tegra_init_revision();
  123. fuse->soc->speedo_init(&tegra_sku_info);
  124. tegra20_fuse_add_randomness();
  125. }
  126. const struct tegra_fuse_soc tegra20_fuse_soc = {
  127. .init = tegra20_fuse_init,
  128. .speedo_init = tegra20_init_speedo_data,
  129. .probe = tegra20_fuse_probe,
  130. .info = &tegra20_fuse_info,
  131. };