dev.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. const struct of_device_id *id;
  104. struct host1x *host;
  105. struct resource *regs;
  106. int syncpt_irq;
  107. int err;
  108. id = of_match_device(host1x_of_match, &pdev->dev);
  109. if (!id)
  110. return -EINVAL;
  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. host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
  122. if (!host)
  123. return -ENOMEM;
  124. mutex_init(&host->devices_lock);
  125. INIT_LIST_HEAD(&host->devices);
  126. INIT_LIST_HEAD(&host->list);
  127. host->dev = &pdev->dev;
  128. host->info = id->data;
  129. /* set common host1x device data */
  130. platform_set_drvdata(pdev, host);
  131. host->regs = devm_ioremap_resource(&pdev->dev, regs);
  132. if (IS_ERR(host->regs))
  133. return PTR_ERR(host->regs);
  134. dma_set_mask_and_coherent(host->dev, host->info->dma_mask);
  135. if (host->info->init) {
  136. err = host->info->init(host);
  137. if (err)
  138. return err;
  139. }
  140. host->clk = devm_clk_get(&pdev->dev, NULL);
  141. if (IS_ERR(host->clk)) {
  142. dev_err(&pdev->dev, "failed to get clock\n");
  143. err = PTR_ERR(host->clk);
  144. return err;
  145. }
  146. host->rst = devm_reset_control_get(&pdev->dev, "host1x");
  147. if (IS_ERR(host->rst)) {
  148. err = PTR_ERR(host->rst);
  149. dev_err(&pdev->dev, "failed to get reset: %d\n", err);
  150. return err;
  151. }
  152. if (iommu_present(&platform_bus_type)) {
  153. struct iommu_domain_geometry *geometry;
  154. unsigned long order;
  155. host->domain = iommu_domain_alloc(&platform_bus_type);
  156. if (!host->domain)
  157. return -ENOMEM;
  158. err = iommu_attach_device(host->domain, &pdev->dev);
  159. if (err == -ENODEV) {
  160. iommu_domain_free(host->domain);
  161. host->domain = NULL;
  162. goto skip_iommu;
  163. } else if (err) {
  164. goto fail_free_domain;
  165. }
  166. geometry = &host->domain->geometry;
  167. order = __ffs(host->domain->pgsize_bitmap);
  168. init_iova_domain(&host->iova, 1UL << order,
  169. geometry->aperture_start >> order,
  170. geometry->aperture_end >> order);
  171. host->iova_end = geometry->aperture_end;
  172. }
  173. skip_iommu:
  174. err = host1x_channel_list_init(&host->channel_list,
  175. host->info->nb_channels);
  176. if (err) {
  177. dev_err(&pdev->dev, "failed to initialize channel list\n");
  178. goto fail_detach_device;
  179. }
  180. err = clk_prepare_enable(host->clk);
  181. if (err < 0) {
  182. dev_err(&pdev->dev, "failed to enable clock\n");
  183. goto fail_free_channels;
  184. }
  185. err = reset_control_deassert(host->rst);
  186. if (err < 0) {
  187. dev_err(&pdev->dev, "failed to deassert reset: %d\n", err);
  188. goto fail_unprepare_disable;
  189. }
  190. err = host1x_syncpt_init(host);
  191. if (err) {
  192. dev_err(&pdev->dev, "failed to initialize syncpts\n");
  193. goto fail_reset_assert;
  194. }
  195. err = host1x_intr_init(host, syncpt_irq);
  196. if (err) {
  197. dev_err(&pdev->dev, "failed to initialize interrupts\n");
  198. goto fail_deinit_syncpt;
  199. }
  200. host1x_debug_init(host);
  201. err = host1x_register(host);
  202. if (err < 0)
  203. goto fail_deinit_intr;
  204. return 0;
  205. fail_deinit_intr:
  206. host1x_intr_deinit(host);
  207. fail_deinit_syncpt:
  208. host1x_syncpt_deinit(host);
  209. fail_reset_assert:
  210. reset_control_assert(host->rst);
  211. fail_unprepare_disable:
  212. clk_disable_unprepare(host->clk);
  213. fail_free_channels:
  214. host1x_channel_list_free(&host->channel_list);
  215. fail_detach_device:
  216. if (host->domain) {
  217. put_iova_domain(&host->iova);
  218. iommu_detach_device(host->domain, &pdev->dev);
  219. }
  220. fail_free_domain:
  221. if (host->domain)
  222. iommu_domain_free(host->domain);
  223. return err;
  224. }
  225. static int host1x_remove(struct platform_device *pdev)
  226. {
  227. struct host1x *host = platform_get_drvdata(pdev);
  228. host1x_unregister(host);
  229. host1x_intr_deinit(host);
  230. host1x_syncpt_deinit(host);
  231. reset_control_assert(host->rst);
  232. clk_disable_unprepare(host->clk);
  233. if (host->domain) {
  234. put_iova_domain(&host->iova);
  235. iommu_detach_device(host->domain, &pdev->dev);
  236. iommu_domain_free(host->domain);
  237. }
  238. return 0;
  239. }
  240. static struct platform_driver tegra_host1x_driver = {
  241. .driver = {
  242. .name = "tegra-host1x",
  243. .of_match_table = host1x_of_match,
  244. },
  245. .probe = host1x_probe,
  246. .remove = host1x_remove,
  247. };
  248. static struct platform_driver * const drivers[] = {
  249. &tegra_host1x_driver,
  250. &tegra_mipi_driver,
  251. };
  252. static int __init tegra_host1x_init(void)
  253. {
  254. int err;
  255. err = bus_register(&host1x_bus_type);
  256. if (err < 0)
  257. return err;
  258. err = platform_register_drivers(drivers, ARRAY_SIZE(drivers));
  259. if (err < 0)
  260. bus_unregister(&host1x_bus_type);
  261. return err;
  262. }
  263. module_init(tegra_host1x_init);
  264. static void __exit tegra_host1x_exit(void)
  265. {
  266. platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
  267. bus_unregister(&host1x_bus_type);
  268. }
  269. module_exit(tegra_host1x_exit);
  270. MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
  271. MODULE_AUTHOR("Terje Bergstrom <tbergstrom@nvidia.com>");
  272. MODULE_DESCRIPTION("Host1x driver for Tegra products");
  273. MODULE_LICENSE("GPL");