mc.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*
  2. * Copyright (C) 2014 NVIDIA CORPORATION. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/of.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/slab.h>
  15. #include <linux/sort.h>
  16. #include <soc/tegra/fuse.h>
  17. #include "mc.h"
  18. #define MC_INTSTATUS 0x000
  19. #define MC_INTMASK 0x004
  20. #define MC_ERR_STATUS 0x08
  21. #define MC_ERR_STATUS_TYPE_SHIFT 28
  22. #define MC_ERR_STATUS_TYPE_INVALID_SMMU_PAGE (6 << MC_ERR_STATUS_TYPE_SHIFT)
  23. #define MC_ERR_STATUS_TYPE_MASK (0x7 << MC_ERR_STATUS_TYPE_SHIFT)
  24. #define MC_ERR_STATUS_READABLE (1 << 27)
  25. #define MC_ERR_STATUS_WRITABLE (1 << 26)
  26. #define MC_ERR_STATUS_NONSECURE (1 << 25)
  27. #define MC_ERR_STATUS_ADR_HI_SHIFT 20
  28. #define MC_ERR_STATUS_ADR_HI_MASK 0x3
  29. #define MC_ERR_STATUS_SECURITY (1 << 17)
  30. #define MC_ERR_STATUS_RW (1 << 16)
  31. #define MC_ERR_ADR 0x0c
  32. #define MC_EMEM_ARB_CFG 0x90
  33. #define MC_EMEM_ARB_CFG_CYCLES_PER_UPDATE(x) (((x) & 0x1ff) << 0)
  34. #define MC_EMEM_ARB_CFG_CYCLES_PER_UPDATE_MASK 0x1ff
  35. #define MC_EMEM_ARB_MISC0 0xd8
  36. #define MC_EMEM_ADR_CFG 0x54
  37. #define MC_EMEM_ADR_CFG_EMEM_NUMDEV BIT(0)
  38. static const struct of_device_id tegra_mc_of_match[] = {
  39. #ifdef CONFIG_ARCH_TEGRA_3x_SOC
  40. { .compatible = "nvidia,tegra30-mc", .data = &tegra30_mc_soc },
  41. #endif
  42. #ifdef CONFIG_ARCH_TEGRA_114_SOC
  43. { .compatible = "nvidia,tegra114-mc", .data = &tegra114_mc_soc },
  44. #endif
  45. #ifdef CONFIG_ARCH_TEGRA_124_SOC
  46. { .compatible = "nvidia,tegra124-mc", .data = &tegra124_mc_soc },
  47. #endif
  48. #ifdef CONFIG_ARCH_TEGRA_132_SOC
  49. { .compatible = "nvidia,tegra132-mc", .data = &tegra132_mc_soc },
  50. #endif
  51. #ifdef CONFIG_ARCH_TEGRA_210_SOC
  52. { .compatible = "nvidia,tegra210-mc", .data = &tegra210_mc_soc },
  53. #endif
  54. { }
  55. };
  56. MODULE_DEVICE_TABLE(of, tegra_mc_of_match);
  57. static int tegra_mc_setup_latency_allowance(struct tegra_mc *mc)
  58. {
  59. unsigned long long tick;
  60. unsigned int i;
  61. u32 value;
  62. /* compute the number of MC clock cycles per tick */
  63. tick = mc->tick * clk_get_rate(mc->clk);
  64. do_div(tick, NSEC_PER_SEC);
  65. value = readl(mc->regs + MC_EMEM_ARB_CFG);
  66. value &= ~MC_EMEM_ARB_CFG_CYCLES_PER_UPDATE_MASK;
  67. value |= MC_EMEM_ARB_CFG_CYCLES_PER_UPDATE(tick);
  68. writel(value, mc->regs + MC_EMEM_ARB_CFG);
  69. /* write latency allowance defaults */
  70. for (i = 0; i < mc->soc->num_clients; i++) {
  71. const struct tegra_mc_la *la = &mc->soc->clients[i].la;
  72. u32 value;
  73. value = readl(mc->regs + la->reg);
  74. value &= ~(la->mask << la->shift);
  75. value |= (la->def & la->mask) << la->shift;
  76. writel(value, mc->regs + la->reg);
  77. }
  78. return 0;
  79. }
  80. void tegra_mc_write_emem_configuration(struct tegra_mc *mc, unsigned long rate)
  81. {
  82. unsigned int i;
  83. struct tegra_mc_timing *timing = NULL;
  84. for (i = 0; i < mc->num_timings; i++) {
  85. if (mc->timings[i].rate == rate) {
  86. timing = &mc->timings[i];
  87. break;
  88. }
  89. }
  90. if (!timing) {
  91. dev_err(mc->dev, "no memory timing registered for rate %lu\n",
  92. rate);
  93. return;
  94. }
  95. for (i = 0; i < mc->soc->num_emem_regs; ++i)
  96. mc_writel(mc, timing->emem_data[i], mc->soc->emem_regs[i]);
  97. }
  98. unsigned int tegra_mc_get_emem_device_count(struct tegra_mc *mc)
  99. {
  100. u8 dram_count;
  101. dram_count = mc_readl(mc, MC_EMEM_ADR_CFG);
  102. dram_count &= MC_EMEM_ADR_CFG_EMEM_NUMDEV;
  103. dram_count++;
  104. return dram_count;
  105. }
  106. static int load_one_timing(struct tegra_mc *mc,
  107. struct tegra_mc_timing *timing,
  108. struct device_node *node)
  109. {
  110. int err;
  111. u32 tmp;
  112. err = of_property_read_u32(node, "clock-frequency", &tmp);
  113. if (err) {
  114. dev_err(mc->dev,
  115. "timing %s: failed to read rate\n", node->name);
  116. return err;
  117. }
  118. timing->rate = tmp;
  119. timing->emem_data = devm_kcalloc(mc->dev, mc->soc->num_emem_regs,
  120. sizeof(u32), GFP_KERNEL);
  121. if (!timing->emem_data)
  122. return -ENOMEM;
  123. err = of_property_read_u32_array(node, "nvidia,emem-configuration",
  124. timing->emem_data,
  125. mc->soc->num_emem_regs);
  126. if (err) {
  127. dev_err(mc->dev,
  128. "timing %s: failed to read EMEM configuration\n",
  129. node->name);
  130. return err;
  131. }
  132. return 0;
  133. }
  134. static int load_timings(struct tegra_mc *mc, struct device_node *node)
  135. {
  136. struct device_node *child;
  137. struct tegra_mc_timing *timing;
  138. int child_count = of_get_child_count(node);
  139. int i = 0, err;
  140. mc->timings = devm_kcalloc(mc->dev, child_count, sizeof(*timing),
  141. GFP_KERNEL);
  142. if (!mc->timings)
  143. return -ENOMEM;
  144. mc->num_timings = child_count;
  145. for_each_child_of_node(node, child) {
  146. timing = &mc->timings[i++];
  147. err = load_one_timing(mc, timing, child);
  148. if (err)
  149. return err;
  150. }
  151. return 0;
  152. }
  153. static int tegra_mc_setup_timings(struct tegra_mc *mc)
  154. {
  155. struct device_node *node;
  156. u32 ram_code, node_ram_code;
  157. int err;
  158. ram_code = tegra_read_ram_code();
  159. mc->num_timings = 0;
  160. for_each_child_of_node(mc->dev->of_node, node) {
  161. err = of_property_read_u32(node, "nvidia,ram-code",
  162. &node_ram_code);
  163. if (err || (node_ram_code != ram_code)) {
  164. of_node_put(node);
  165. continue;
  166. }
  167. err = load_timings(mc, node);
  168. if (err)
  169. return err;
  170. of_node_put(node);
  171. break;
  172. }
  173. if (mc->num_timings == 0)
  174. dev_warn(mc->dev,
  175. "no memory timings for RAM code %u registered\n",
  176. ram_code);
  177. return 0;
  178. }
  179. static const char *const status_names[32] = {
  180. [ 1] = "External interrupt",
  181. [ 6] = "EMEM address decode error",
  182. [ 8] = "Security violation",
  183. [ 9] = "EMEM arbitration error",
  184. [10] = "Page fault",
  185. [11] = "Invalid APB ASID update",
  186. [12] = "VPR violation",
  187. [13] = "Secure carveout violation",
  188. [16] = "MTS carveout violation",
  189. };
  190. static const char *const error_names[8] = {
  191. [2] = "EMEM decode error",
  192. [3] = "TrustZone violation",
  193. [4] = "Carveout violation",
  194. [6] = "SMMU translation error",
  195. };
  196. static irqreturn_t tegra_mc_irq(int irq, void *data)
  197. {
  198. struct tegra_mc *mc = data;
  199. unsigned long status;
  200. unsigned int bit;
  201. /* mask all interrupts to avoid flooding */
  202. status = mc_readl(mc, MC_INTSTATUS) & mc->soc->intmask;
  203. if (!status)
  204. return IRQ_NONE;
  205. for_each_set_bit(bit, &status, 32) {
  206. const char *error = status_names[bit] ?: "unknown";
  207. const char *client = "unknown", *desc;
  208. const char *direction, *secure;
  209. phys_addr_t addr = 0;
  210. unsigned int i;
  211. char perm[7];
  212. u8 id, type;
  213. u32 value;
  214. value = mc_readl(mc, MC_ERR_STATUS);
  215. #ifdef CONFIG_PHYS_ADDR_T_64BIT
  216. if (mc->soc->num_address_bits > 32) {
  217. addr = ((value >> MC_ERR_STATUS_ADR_HI_SHIFT) &
  218. MC_ERR_STATUS_ADR_HI_MASK);
  219. addr <<= 32;
  220. }
  221. #endif
  222. if (value & MC_ERR_STATUS_RW)
  223. direction = "write";
  224. else
  225. direction = "read";
  226. if (value & MC_ERR_STATUS_SECURITY)
  227. secure = "secure ";
  228. else
  229. secure = "";
  230. id = value & mc->soc->client_id_mask;
  231. for (i = 0; i < mc->soc->num_clients; i++) {
  232. if (mc->soc->clients[i].id == id) {
  233. client = mc->soc->clients[i].name;
  234. break;
  235. }
  236. }
  237. type = (value & MC_ERR_STATUS_TYPE_MASK) >>
  238. MC_ERR_STATUS_TYPE_SHIFT;
  239. desc = error_names[type];
  240. switch (value & MC_ERR_STATUS_TYPE_MASK) {
  241. case MC_ERR_STATUS_TYPE_INVALID_SMMU_PAGE:
  242. perm[0] = ' ';
  243. perm[1] = '[';
  244. if (value & MC_ERR_STATUS_READABLE)
  245. perm[2] = 'R';
  246. else
  247. perm[2] = '-';
  248. if (value & MC_ERR_STATUS_WRITABLE)
  249. perm[3] = 'W';
  250. else
  251. perm[3] = '-';
  252. if (value & MC_ERR_STATUS_NONSECURE)
  253. perm[4] = '-';
  254. else
  255. perm[4] = 'S';
  256. perm[5] = ']';
  257. perm[6] = '\0';
  258. break;
  259. default:
  260. perm[0] = '\0';
  261. break;
  262. }
  263. value = mc_readl(mc, MC_ERR_ADR);
  264. addr |= value;
  265. dev_err_ratelimited(mc->dev, "%s: %s%s @%pa: %s (%s%s)\n",
  266. client, secure, direction, &addr, error,
  267. desc, perm);
  268. }
  269. /* clear interrupts */
  270. mc_writel(mc, status, MC_INTSTATUS);
  271. return IRQ_HANDLED;
  272. }
  273. static int tegra_mc_probe(struct platform_device *pdev)
  274. {
  275. const struct of_device_id *match;
  276. struct resource *res;
  277. struct tegra_mc *mc;
  278. int err;
  279. match = of_match_node(tegra_mc_of_match, pdev->dev.of_node);
  280. if (!match)
  281. return -ENODEV;
  282. mc = devm_kzalloc(&pdev->dev, sizeof(*mc), GFP_KERNEL);
  283. if (!mc)
  284. return -ENOMEM;
  285. platform_set_drvdata(pdev, mc);
  286. mc->soc = match->data;
  287. mc->dev = &pdev->dev;
  288. /* length of MC tick in nanoseconds */
  289. mc->tick = 30;
  290. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  291. mc->regs = devm_ioremap_resource(&pdev->dev, res);
  292. if (IS_ERR(mc->regs))
  293. return PTR_ERR(mc->regs);
  294. mc->clk = devm_clk_get(&pdev->dev, "mc");
  295. if (IS_ERR(mc->clk)) {
  296. dev_err(&pdev->dev, "failed to get MC clock: %ld\n",
  297. PTR_ERR(mc->clk));
  298. return PTR_ERR(mc->clk);
  299. }
  300. err = tegra_mc_setup_latency_allowance(mc);
  301. if (err < 0) {
  302. dev_err(&pdev->dev, "failed to setup latency allowance: %d\n",
  303. err);
  304. return err;
  305. }
  306. err = tegra_mc_setup_timings(mc);
  307. if (err < 0) {
  308. dev_err(&pdev->dev, "failed to setup timings: %d\n", err);
  309. return err;
  310. }
  311. if (IS_ENABLED(CONFIG_TEGRA_IOMMU_SMMU)) {
  312. mc->smmu = tegra_smmu_probe(&pdev->dev, mc->soc->smmu, mc);
  313. if (IS_ERR(mc->smmu)) {
  314. dev_err(&pdev->dev, "failed to probe SMMU: %ld\n",
  315. PTR_ERR(mc->smmu));
  316. return PTR_ERR(mc->smmu);
  317. }
  318. }
  319. mc->irq = platform_get_irq(pdev, 0);
  320. if (mc->irq < 0) {
  321. dev_err(&pdev->dev, "interrupt not specified\n");
  322. return mc->irq;
  323. }
  324. err = devm_request_irq(&pdev->dev, mc->irq, tegra_mc_irq, IRQF_SHARED,
  325. dev_name(&pdev->dev), mc);
  326. if (err < 0) {
  327. dev_err(&pdev->dev, "failed to request IRQ#%u: %d\n", mc->irq,
  328. err);
  329. return err;
  330. }
  331. WARN(!mc->soc->client_id_mask, "Missing client ID mask for this SoC\n");
  332. mc_writel(mc, mc->soc->intmask, MC_INTMASK);
  333. return 0;
  334. }
  335. static struct platform_driver tegra_mc_driver = {
  336. .driver = {
  337. .name = "tegra-mc",
  338. .of_match_table = tegra_mc_of_match,
  339. .suppress_bind_attrs = true,
  340. },
  341. .prevent_deferred_probe = true,
  342. .probe = tegra_mc_probe,
  343. };
  344. static int tegra_mc_init(void)
  345. {
  346. return platform_driver_register(&tegra_mc_driver);
  347. }
  348. arch_initcall(tegra_mc_init);
  349. MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
  350. MODULE_DESCRIPTION("NVIDIA Tegra Memory Controller driver");
  351. MODULE_LICENSE("GPL v2");