dev.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * Tegra host1x driver
  3. *
  4. * Copyright (c) 2010-2013, NVIDIA Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/clk.h>
  19. #include <linux/dma-mapping.h>
  20. #include <linux/io.h>
  21. #include <linux/list.h>
  22. #include <linux/module.h>
  23. #include <linux/of_device.h>
  24. #include <linux/of.h>
  25. #include <linux/slab.h>
  26. #define CREATE_TRACE_POINTS
  27. #include <trace/events/host1x.h>
  28. #undef CREATE_TRACE_POINTS
  29. #include "bus.h"
  30. #include "channel.h"
  31. #include "debug.h"
  32. #include "dev.h"
  33. #include "intr.h"
  34. #include "hw/host1x01.h"
  35. #include "hw/host1x02.h"
  36. #include "hw/host1x04.h"
  37. #include "hw/host1x05.h"
  38. void host1x_sync_writel(struct host1x *host1x, u32 v, u32 r)
  39. {
  40. void __iomem *sync_regs = host1x->regs + host1x->info->sync_offset;
  41. writel(v, sync_regs + r);
  42. }
  43. u32 host1x_sync_readl(struct host1x *host1x, u32 r)
  44. {
  45. void __iomem *sync_regs = host1x->regs + host1x->info->sync_offset;
  46. return readl(sync_regs + r);
  47. }
  48. void host1x_ch_writel(struct host1x_channel *ch, u32 v, u32 r)
  49. {
  50. writel(v, ch->regs + r);
  51. }
  52. u32 host1x_ch_readl(struct host1x_channel *ch, u32 r)
  53. {
  54. return readl(ch->regs + r);
  55. }
  56. static const struct host1x_info host1x01_info = {
  57. .nb_channels = 8,
  58. .nb_pts = 32,
  59. .nb_mlocks = 16,
  60. .nb_bases = 8,
  61. .init = host1x01_init,
  62. .sync_offset = 0x3000,
  63. .dma_mask = DMA_BIT_MASK(32),
  64. };
  65. static const struct host1x_info host1x02_info = {
  66. .nb_channels = 9,
  67. .nb_pts = 32,
  68. .nb_mlocks = 16,
  69. .nb_bases = 12,
  70. .init = host1x02_init,
  71. .sync_offset = 0x3000,
  72. .dma_mask = DMA_BIT_MASK(32),
  73. };
  74. static const struct host1x_info host1x04_info = {
  75. .nb_channels = 12,
  76. .nb_pts = 192,
  77. .nb_mlocks = 16,
  78. .nb_bases = 64,
  79. .init = host1x04_init,
  80. .sync_offset = 0x2100,
  81. .dma_mask = DMA_BIT_MASK(34),
  82. };
  83. static const struct host1x_info host1x05_info = {
  84. .nb_channels = 14,
  85. .nb_pts = 192,
  86. .nb_mlocks = 16,
  87. .nb_bases = 64,
  88. .init = host1x05_init,
  89. .sync_offset = 0x2100,
  90. .dma_mask = DMA_BIT_MASK(34),
  91. };
  92. static const struct of_device_id host1x_of_match[] = {
  93. { .compatible = "nvidia,tegra210-host1x", .data = &host1x05_info, },
  94. { .compatible = "nvidia,tegra124-host1x", .data = &host1x04_info, },
  95. { .compatible = "nvidia,tegra114-host1x", .data = &host1x02_info, },
  96. { .compatible = "nvidia,tegra30-host1x", .data = &host1x01_info, },
  97. { .compatible = "nvidia,tegra20-host1x", .data = &host1x01_info, },
  98. { },
  99. };
  100. MODULE_DEVICE_TABLE(of, host1x_of_match);
  101. static int host1x_probe(struct platform_device *pdev)
  102. {
  103. struct host1x *host;
  104. struct resource *regs;
  105. int syncpt_irq;
  106. int err;
  107. host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
  108. if (!host)
  109. return -ENOMEM;
  110. host->info = of_device_get_match_data(&pdev->dev);
  111. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  112. if (!regs) {
  113. dev_err(&pdev->dev, "failed to get registers\n");
  114. return -ENXIO;
  115. }
  116. syncpt_irq = platform_get_irq(pdev, 0);
  117. if (syncpt_irq < 0) {
  118. dev_err(&pdev->dev, "failed to get IRQ: %d\n", syncpt_irq);
  119. return syncpt_irq;
  120. }
  121. mutex_init(&host->devices_lock);
  122. INIT_LIST_HEAD(&host->devices);
  123. INIT_LIST_HEAD(&host->list);
  124. host->dev = &pdev->dev;
  125. /* set common host1x device data */
  126. platform_set_drvdata(pdev, host);
  127. host->regs = devm_ioremap_resource(&pdev->dev, regs);
  128. if (IS_ERR(host->regs))
  129. return PTR_ERR(host->regs);
  130. dma_set_mask_and_coherent(host->dev, host->info->dma_mask);
  131. if (host->info->init) {
  132. err = host->info->init(host);
  133. if (err)
  134. return err;
  135. }
  136. host->clk = devm_clk_get(&pdev->dev, NULL);
  137. if (IS_ERR(host->clk)) {
  138. dev_err(&pdev->dev, "failed to get clock\n");
  139. err = PTR_ERR(host->clk);
  140. return err;
  141. }
  142. host->rst = devm_reset_control_get(&pdev->dev, "host1x");
  143. if (IS_ERR(host->rst)) {
  144. err = PTR_ERR(host->rst);
  145. dev_err(&pdev->dev, "failed to get reset: %d\n", err);
  146. return err;
  147. }
  148. if (iommu_present(&platform_bus_type)) {
  149. struct iommu_domain_geometry *geometry;
  150. unsigned long order;
  151. host->domain = iommu_domain_alloc(&platform_bus_type);
  152. if (!host->domain)
  153. return -ENOMEM;
  154. err = iommu_attach_device(host->domain, &pdev->dev);
  155. if (err == -ENODEV) {
  156. iommu_domain_free(host->domain);
  157. host->domain = NULL;
  158. goto skip_iommu;
  159. } else if (err) {
  160. goto fail_free_domain;
  161. }
  162. geometry = &host->domain->geometry;
  163. order = __ffs(host->domain->pgsize_bitmap);
  164. init_iova_domain(&host->iova, 1UL << order,
  165. geometry->aperture_start >> order,
  166. geometry->aperture_end >> order);
  167. host->iova_end = geometry->aperture_end;
  168. }
  169. skip_iommu:
  170. err = host1x_channel_list_init(&host->channel_list,
  171. host->info->nb_channels);
  172. if (err) {
  173. dev_err(&pdev->dev, "failed to initialize channel list\n");
  174. goto fail_detach_device;
  175. }
  176. err = clk_prepare_enable(host->clk);
  177. if (err < 0) {
  178. dev_err(&pdev->dev, "failed to enable clock\n");
  179. goto fail_free_channels;
  180. }
  181. err = reset_control_deassert(host->rst);
  182. if (err < 0) {
  183. dev_err(&pdev->dev, "failed to deassert reset: %d\n", err);
  184. goto fail_unprepare_disable;
  185. }
  186. err = host1x_syncpt_init(host);
  187. if (err) {
  188. dev_err(&pdev->dev, "failed to initialize syncpts\n");
  189. goto fail_reset_assert;
  190. }
  191. err = host1x_intr_init(host, syncpt_irq);
  192. if (err) {
  193. dev_err(&pdev->dev, "failed to initialize interrupts\n");
  194. goto fail_deinit_syncpt;
  195. }
  196. host1x_debug_init(host);
  197. err = host1x_register(host);
  198. if (err < 0)
  199. goto fail_deinit_intr;
  200. return 0;
  201. fail_deinit_intr:
  202. host1x_intr_deinit(host);
  203. fail_deinit_syncpt:
  204. host1x_syncpt_deinit(host);
  205. fail_reset_assert:
  206. reset_control_assert(host->rst);
  207. fail_unprepare_disable:
  208. clk_disable_unprepare(host->clk);
  209. fail_free_channels:
  210. host1x_channel_list_free(&host->channel_list);
  211. fail_detach_device:
  212. if (host->domain) {
  213. put_iova_domain(&host->iova);
  214. iommu_detach_device(host->domain, &pdev->dev);
  215. }
  216. fail_free_domain:
  217. if (host->domain)
  218. iommu_domain_free(host->domain);
  219. return err;
  220. }
  221. static int host1x_remove(struct platform_device *pdev)
  222. {
  223. struct host1x *host = platform_get_drvdata(pdev);
  224. host1x_unregister(host);
  225. host1x_intr_deinit(host);
  226. host1x_syncpt_deinit(host);
  227. reset_control_assert(host->rst);
  228. clk_disable_unprepare(host->clk);
  229. if (host->domain) {
  230. put_iova_domain(&host->iova);
  231. iommu_detach_device(host->domain, &pdev->dev);
  232. iommu_domain_free(host->domain);
  233. }
  234. return 0;
  235. }
  236. static struct platform_driver tegra_host1x_driver = {
  237. .driver = {
  238. .name = "tegra-host1x",
  239. .of_match_table = host1x_of_match,
  240. },
  241. .probe = host1x_probe,
  242. .remove = host1x_remove,
  243. };
  244. static struct platform_driver * const drivers[] = {
  245. &tegra_host1x_driver,
  246. &tegra_mipi_driver,
  247. };
  248. static int __init tegra_host1x_init(void)
  249. {
  250. int err;
  251. err = bus_register(&host1x_bus_type);
  252. if (err < 0)
  253. return err;
  254. err = platform_register_drivers(drivers, ARRAY_SIZE(drivers));
  255. if (err < 0)
  256. bus_unregister(&host1x_bus_type);
  257. return err;
  258. }
  259. module_init(tegra_host1x_init);
  260. static void __exit tegra_host1x_exit(void)
  261. {
  262. platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
  263. bus_unregister(&host1x_bus_type);
  264. }
  265. module_exit(tegra_host1x_exit);
  266. MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
  267. MODULE_AUTHOR("Terje Bergstrom <tbergstrom@nvidia.com>");
  268. MODULE_DESCRIPTION("Host1x driver for Tegra products");
  269. MODULE_LICENSE("GPL");