cache-v5l2.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2019 Andes Technology Corporation
  4. * Rick Chen, Andes Technology Corporation <rick@andestech.com>
  5. */
  6. #include <common.h>
  7. #include <command.h>
  8. #include <cache.h>
  9. #include <dm.h>
  10. #include <hang.h>
  11. #include <asm/global_data.h>
  12. #include <asm/io.h>
  13. #include <dm/ofnode.h>
  14. #include <linux/bitops.h>
  15. struct l2cache {
  16. volatile u64 configure;
  17. volatile u64 control;
  18. volatile u64 hpm0;
  19. volatile u64 hpm1;
  20. volatile u64 hpm2;
  21. volatile u64 hpm3;
  22. volatile u64 error_status;
  23. volatile u64 ecc_error;
  24. volatile u64 cctl_command0;
  25. volatile u64 cctl_access_line0;
  26. volatile u64 cctl_command1;
  27. volatile u64 cctl_access_line1;
  28. volatile u64 cctl_command2;
  29. volatile u64 cctl_access_line2;
  30. volatile u64 cctl_command3;
  31. volatile u64 cctl_access_line4;
  32. volatile u64 cctl_status;
  33. };
  34. /* Configuration register */
  35. #define MEM_MAP_OFF 20
  36. #define MEM_MAP_MSK BIT(MEM_MAP_OFF)
  37. /* offset of v0 memory map (Gen1) */
  38. static u32 cmd_stride = 0x10;
  39. static u32 status_stride = 0x0;
  40. static u32 status_bit_offset = 0x4;
  41. /* Control Register */
  42. #define L2_ENABLE 0x1
  43. /* prefetch */
  44. #define IPREPETCH_OFF 3
  45. #define DPREPETCH_OFF 5
  46. #define IPREPETCH_MSK (3 << IPREPETCH_OFF)
  47. #define DPREPETCH_MSK (3 << DPREPETCH_OFF)
  48. /* tag ram */
  49. #define TRAMOCTL_OFF 8
  50. #define TRAMICTL_OFF 10
  51. #define TRAMOCTL_MSK (3 << TRAMOCTL_OFF)
  52. #define TRAMICTL_MSK BIT(TRAMICTL_OFF)
  53. /* data ram */
  54. #define DRAMOCTL_OFF 11
  55. #define DRAMICTL_OFF 13
  56. #define DRAMOCTL_MSK (3 << DRAMOCTL_OFF)
  57. #define DRAMICTL_MSK BIT(DRAMICTL_OFF)
  58. /* CCTL Command Register */
  59. #define CCTL_CMD_REG(base, hart) ((ulong)(base) + 0x40 + (hart) * (cmd_stride))
  60. #define L2_WBINVAL_ALL 0x12
  61. /* CCTL Status Register */
  62. #define CCTL_STATUS_REG(base, hart) ((ulong)(base) + 0x80 + (hart) * (status_stride))
  63. #define CCTL_STATUS_MSK(hart) (0xf << ((hart) * (status_bit_offset)))
  64. #define CCTL_STATUS_IDLE(hart) (0 << ((hart) * (status_bit_offset)))
  65. #define CCTL_STATUS_PROCESS(hart) (1 << ((hart) * (status_bit_offset)))
  66. #define CCTL_STATUS_ILLEGAL(hart) (2 << ((hart) * (status_bit_offset)))
  67. DECLARE_GLOBAL_DATA_PTR;
  68. struct v5l2_plat {
  69. struct l2cache *regs;
  70. u32 iprefetch;
  71. u32 dprefetch;
  72. u32 tram_ctl[2];
  73. u32 dram_ctl[2];
  74. };
  75. static int v5l2_enable(struct udevice *dev)
  76. {
  77. struct v5l2_plat *plat = dev_get_plat(dev);
  78. volatile struct l2cache *regs = plat->regs;
  79. if (regs)
  80. setbits_le32(&regs->control, L2_ENABLE);
  81. return 0;
  82. }
  83. static int v5l2_disable(struct udevice *dev)
  84. {
  85. struct v5l2_plat *plat = dev_get_plat(dev);
  86. volatile struct l2cache *regs = plat->regs;
  87. u8 hart = gd->arch.boot_hart;
  88. void __iomem *cctlcmd = (void __iomem *)CCTL_CMD_REG(regs, hart);
  89. if ((regs) && (readl(&regs->control) & L2_ENABLE)) {
  90. writel(L2_WBINVAL_ALL, cctlcmd);
  91. while ((readl(&regs->cctl_status) & CCTL_STATUS_MSK(hart))) {
  92. if ((readl(&regs->cctl_status) & CCTL_STATUS_ILLEGAL(hart))) {
  93. printf("L2 flush illegal! hanging...");
  94. hang();
  95. }
  96. }
  97. clrbits_le32(&regs->control, L2_ENABLE);
  98. }
  99. return 0;
  100. }
  101. static int v5l2_of_to_plat(struct udevice *dev)
  102. {
  103. struct v5l2_plat *plat = dev_get_plat(dev);
  104. struct l2cache *regs;
  105. regs = dev_read_addr_ptr(dev);
  106. plat->regs = regs;
  107. plat->iprefetch = -EINVAL;
  108. plat->dprefetch = -EINVAL;
  109. plat->tram_ctl[0] = -EINVAL;
  110. plat->dram_ctl[0] = -EINVAL;
  111. /* Instruction and data fetch prefetch depth */
  112. dev_read_u32(dev, "andes,inst-prefetch", &plat->iprefetch);
  113. dev_read_u32(dev, "andes,data-prefetch", &plat->dprefetch);
  114. /* Set tag RAM and data RAM setup and output cycle */
  115. dev_read_u32_array(dev, "andes,tag-ram-ctl", plat->tram_ctl, 2);
  116. dev_read_u32_array(dev, "andes,data-ram-ctl", plat->dram_ctl, 2);
  117. return 0;
  118. }
  119. static int v5l2_probe(struct udevice *dev)
  120. {
  121. struct v5l2_plat *plat = dev_get_plat(dev);
  122. struct l2cache *regs = plat->regs;
  123. u32 cfg_val, ctl_val;
  124. cfg_val = readl(&regs->configure);
  125. ctl_val = readl(&regs->control);
  126. /* If true, v1 memory map (Gen2) */
  127. if (cfg_val & MEM_MAP_MSK) {
  128. cmd_stride = 0x1000;
  129. status_stride = 0x1000;
  130. status_bit_offset = 0x0;
  131. }
  132. ctl_val |= L2_ENABLE;
  133. if (plat->iprefetch != -EINVAL) {
  134. ctl_val &= ~(IPREPETCH_MSK);
  135. ctl_val |= (plat->iprefetch << IPREPETCH_OFF);
  136. }
  137. if (plat->dprefetch != -EINVAL) {
  138. ctl_val &= ~(DPREPETCH_MSK);
  139. ctl_val |= (plat->dprefetch << DPREPETCH_OFF);
  140. }
  141. if (plat->tram_ctl[0] != -EINVAL) {
  142. ctl_val &= ~(TRAMOCTL_MSK | TRAMICTL_MSK);
  143. ctl_val |= plat->tram_ctl[0] << TRAMOCTL_OFF;
  144. ctl_val |= plat->tram_ctl[1] << TRAMICTL_OFF;
  145. }
  146. if (plat->dram_ctl[0] != -EINVAL) {
  147. ctl_val &= ~(DRAMOCTL_MSK | DRAMICTL_MSK);
  148. ctl_val |= plat->dram_ctl[0] << DRAMOCTL_OFF;
  149. ctl_val |= plat->dram_ctl[1] << DRAMICTL_OFF;
  150. }
  151. writel(ctl_val, &regs->control);
  152. return 0;
  153. }
  154. static const struct udevice_id v5l2_cache_ids[] = {
  155. { .compatible = "cache" },
  156. {}
  157. };
  158. static const struct cache_ops v5l2_cache_ops = {
  159. .enable = v5l2_enable,
  160. .disable = v5l2_disable,
  161. };
  162. U_BOOT_DRIVER(v5l2_cache) = {
  163. .name = "v5l2_cache",
  164. .id = UCLASS_CACHE,
  165. .of_match = v5l2_cache_ids,
  166. .of_to_plat = v5l2_of_to_plat,
  167. .probe = v5l2_probe,
  168. .plat_auto = sizeof(struct v5l2_plat),
  169. .ops = &v5l2_cache_ops,
  170. .flags = DM_FLAG_PRE_RELOC,
  171. };